1'use strict'; 2const common = require('../common'); 3 4const assert = require('assert'); 5const fs = require('fs'); 6const { Server } = require('net'); 7const { Worker, isMainThread, parentPort } = require('worker_threads'); 8 9if (isMainThread) { 10 const w = new Worker(__filename); 11 let fd = null; 12 w.on('message', common.mustCall((fd_) => { 13 assert.strictEqual(typeof fd_, 'number'); 14 fd = fd_; 15 })); 16 w.on('exit', common.mustCall(() => { 17 if (fd === -1) { 18 // This happens when server sockets don’t have file descriptors, 19 // i.e. on Windows. 20 return; 21 } 22 assert.throws(() => fs.fstatSync(fd), { code: 'EBADF' }); 23 })); 24} else { 25 const server = new Server(); 26 server.listen(0); 27 parentPort.postMessage(server._handle.fd); 28 server.unref(); 29} 30