mirror of
https://github.com/nodejs/node.git
synced 2025-05-12 02:02:29 +00:00

PR-URL: https://github.com/nodejs/node/pull/20876 Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Shingo Inoue <leko.noor@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com> Reviewed-By: John-David Dalton <john.david.dalton@gmail.com> Reviewed-By: Gus Caplan <me@gus.host>
31 lines
843 B
JavaScript
31 lines
843 B
JavaScript
// Flags: --experimental-worker
|
||
'use strict';
|
||
const common = require('../common');
|
||
const assert = require('assert');
|
||
const { Worker, isMainThread, parentPort } = require('worker_threads');
|
||
const { Server } = require('net');
|
||
const fs = require('fs');
|
||
|
||
if (isMainThread) {
|
||
const w = new Worker(__filename);
|
||
let fd = null;
|
||
w.on('message', common.mustCall((fd_) => {
|
||
assert.strictEqual(typeof fd_, 'number');
|
||
fd = fd_;
|
||
}));
|
||
w.on('exit', common.mustCall((code) => {
|
||
if (fd === -1) {
|
||
// This happens when server sockets don’t have file descriptors,
|
||
// i.e. on Windows.
|
||
return;
|
||
}
|
||
common.expectsError(() => fs.fstatSync(fd),
|
||
{ code: 'EBADF' });
|
||
}));
|
||
} else {
|
||
const server = new Server();
|
||
server.listen(0);
|
||
parentPort.postMessage(server._handle.fd);
|
||
server.unref();
|
||
}
|