1 | const onlisten = require('on-net-listen')
|
2 | const fs = require('fs')
|
3 | const net = require('net')
|
4 |
|
5 | onlisten(function (addr) {
|
6 | this.destroy()
|
7 | const port = Buffer.from(addr.port + '')
|
8 | fs.writeSync(3, port, 0, port.length)
|
9 | signal(3, function () {
|
10 | process.emit('beforeExit')
|
11 | })
|
12 | })
|
13 |
|
14 | function signal (fd, cb) {
|
15 | const s = new net.Socket({ fd, readable: true, writable: false })
|
16 | s.unref()
|
17 | s.on('error', () => {})
|
18 | s.on('close', cb)
|
19 | }
|