2017-09-05 22:38:32 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const EventEmitter = require('events');
|
2017-09-01 17:03:41 +02:00
|
|
|
const assert = require('assert');
|
|
|
|
const path = require('path');
|
2017-09-05 22:38:32 +02:00
|
|
|
const util = require('util');
|
2017-09-01 17:03:41 +02:00
|
|
|
const {
|
2018-06-26 17:31:36 +02:00
|
|
|
ERR_WORKER_PATH,
|
2017-09-20 23:23:31 +02:00
|
|
|
ERR_WORKER_UNSERIALIZABLE_ERROR,
|
|
|
|
ERR_WORKER_UNSUPPORTED_EXTENSION,
|
2019-01-04 19:02:25 +01:00
|
|
|
ERR_WORKER_INVALID_EXEC_ARGV,
|
|
|
|
ERR_INVALID_ARG_TYPE,
|
2017-09-01 17:03:41 +02:00
|
|
|
} = require('internal/errors').codes;
|
2018-12-11 16:24:22 +01:00
|
|
|
const { validateString } = require('internal/validators');
|
2019-01-23 23:21:46 +01:00
|
|
|
const { getOptionValue } = require('internal/options');
|
2017-09-05 22:38:32 +02:00
|
|
|
|
2018-09-23 19:14:57 +02:00
|
|
|
const {
|
2018-12-23 04:53:05 +01:00
|
|
|
drainMessagePort,
|
|
|
|
MessageChannel,
|
|
|
|
messageTypes,
|
|
|
|
kPort,
|
|
|
|
kIncrementsPortRef,
|
|
|
|
kWaitingStreams,
|
|
|
|
kStdioWantsMoreDataCallback,
|
|
|
|
setupPortReferencing,
|
|
|
|
ReadableWorkerStdio,
|
|
|
|
WritableWorkerStdio,
|
|
|
|
} = require('internal/worker/io');
|
2018-12-24 00:31:06 +01:00
|
|
|
const { deserializeError } = require('internal/error-serdes');
|
2018-09-09 04:45:10 +02:00
|
|
|
const { pathToFileURL } = require('url');
|
2017-09-05 22:38:32 +02:00
|
|
|
|
2017-09-01 17:03:41 +02:00
|
|
|
const {
|
2019-02-01 23:47:38 +01:00
|
|
|
ownsProcessState,
|
|
|
|
isMainThread,
|
2019-01-29 19:06:17 +01:00
|
|
|
threadId,
|
2019-02-01 23:47:38 +01:00
|
|
|
Worker: WorkerImpl,
|
2017-09-01 17:03:41 +02:00
|
|
|
} = internalBinding('worker');
|
|
|
|
|
|
|
|
const kHandle = Symbol('kHandle');
|
|
|
|
const kPublicPort = Symbol('kPublicPort');
|
|
|
|
const kDispose = Symbol('kDispose');
|
|
|
|
const kOnExit = Symbol('kOnExit');
|
|
|
|
const kOnMessage = Symbol('kOnMessage');
|
|
|
|
const kOnCouldNotSerializeErr = Symbol('kOnCouldNotSerializeErr');
|
|
|
|
const kOnErrorMessage = Symbol('kOnErrorMessage');
|
2018-05-13 23:25:14 +02:00
|
|
|
const kParentSideStdio = Symbol('kParentSideStdio');
|
2017-09-05 22:38:32 +02:00
|
|
|
|
|
|
|
const debug = util.debuglog('worker');
|
|
|
|
|
2017-09-01 17:03:41 +02:00
|
|
|
class Worker extends EventEmitter {
|
|
|
|
constructor(filename, options = {}) {
|
|
|
|
super();
|
|
|
|
debug(`[${threadId}] create new worker`, filename, options);
|
2018-12-11 16:24:22 +01:00
|
|
|
validateString(filename, 'filename');
|
2019-01-04 19:02:25 +01:00
|
|
|
if (options.execArgv && !Array.isArray(options.execArgv)) {
|
|
|
|
throw new ERR_INVALID_ARG_TYPE('options.execArgv',
|
|
|
|
'array',
|
|
|
|
options.execArgv);
|
|
|
|
}
|
2017-09-20 23:23:31 +02:00
|
|
|
if (!options.eval) {
|
2018-06-26 17:31:36 +02:00
|
|
|
if (!path.isAbsolute(filename) &&
|
|
|
|
!filename.startsWith('./') &&
|
|
|
|
!filename.startsWith('../') &&
|
|
|
|
!filename.startsWith('.' + path.sep) &&
|
|
|
|
!filename.startsWith('..' + path.sep)) {
|
|
|
|
throw new ERR_WORKER_PATH(filename);
|
2017-09-20 23:23:31 +02:00
|
|
|
}
|
2018-06-26 17:31:36 +02:00
|
|
|
filename = path.resolve(filename);
|
|
|
|
|
2017-09-20 23:23:31 +02:00
|
|
|
const ext = path.extname(filename);
|
|
|
|
if (ext !== '.js' && ext !== '.mjs') {
|
|
|
|
throw new ERR_WORKER_UNSUPPORTED_EXTENSION(ext);
|
|
|
|
}
|
2017-09-01 17:03:41 +02:00
|
|
|
}
|
|
|
|
|
2018-09-09 04:45:10 +02:00
|
|
|
const url = options.eval ? null : pathToFileURL(filename);
|
2017-09-01 17:03:41 +02:00
|
|
|
// Set up the C++ handle for the worker, as well as some internal wiring.
|
2019-01-04 19:02:25 +01:00
|
|
|
this[kHandle] = new WorkerImpl(url, options.execArgv);
|
|
|
|
if (this[kHandle].invalidExecArgv) {
|
|
|
|
throw new ERR_WORKER_INVALID_EXEC_ARGV(this[kHandle].invalidExecArgv);
|
|
|
|
}
|
2017-09-01 17:03:41 +02:00
|
|
|
this[kHandle].onexit = (code) => this[kOnExit](code);
|
|
|
|
this[kPort] = this[kHandle].messagePort;
|
|
|
|
this[kPort].on('message', (data) => this[kOnMessage](data));
|
|
|
|
this[kPort].start();
|
|
|
|
this[kPort].unref();
|
2018-05-13 23:25:14 +02:00
|
|
|
this[kPort][kWaitingStreams] = 0;
|
2017-09-01 17:03:41 +02:00
|
|
|
debug(`[${threadId}] created Worker with ID ${this.threadId}`);
|
|
|
|
|
2018-05-13 23:25:14 +02:00
|
|
|
let stdin = null;
|
|
|
|
if (options.stdin)
|
|
|
|
stdin = new WritableWorkerStdio(this[kPort], 'stdin');
|
|
|
|
const stdout = new ReadableWorkerStdio(this[kPort], 'stdout');
|
|
|
|
if (!options.stdout) {
|
|
|
|
stdout[kIncrementsPortRef] = false;
|
|
|
|
pipeWithoutWarning(stdout, process.stdout);
|
|
|
|
}
|
|
|
|
const stderr = new ReadableWorkerStdio(this[kPort], 'stderr');
|
|
|
|
if (!options.stderr) {
|
|
|
|
stderr[kIncrementsPortRef] = false;
|
|
|
|
pipeWithoutWarning(stderr, process.stderr);
|
|
|
|
}
|
|
|
|
|
|
|
|
this[kParentSideStdio] = { stdin, stdout, stderr };
|
|
|
|
|
2017-09-01 17:03:41 +02:00
|
|
|
const { port1, port2 } = new MessageChannel();
|
|
|
|
this[kPublicPort] = port1;
|
|
|
|
this[kPublicPort].on('message', (message) => this.emit('message', message));
|
|
|
|
setupPortReferencing(this[kPublicPort], this, 'message');
|
|
|
|
this[kPort].postMessage({
|
2018-06-26 02:14:41 +02:00
|
|
|
type: messageTypes.LOAD_SCRIPT,
|
2017-09-01 17:03:41 +02:00
|
|
|
filename,
|
|
|
|
doEval: !!options.eval,
|
|
|
|
workerData: options.workerData,
|
2018-05-13 23:25:14 +02:00
|
|
|
publicPort: port2,
|
2019-01-23 23:21:46 +01:00
|
|
|
manifestSrc: getOptionValue('--experimental-policy') ?
|
|
|
|
require('internal/process/policy').src :
|
|
|
|
null,
|
2018-05-13 23:25:14 +02:00
|
|
|
hasStdin: !!options.stdin
|
2017-09-01 17:03:41 +02:00
|
|
|
}, [port2]);
|
|
|
|
// Actually start the new thread now that everything is in place.
|
|
|
|
this[kHandle].startThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
[kOnExit](code) {
|
|
|
|
debug(`[${threadId}] hears end event for Worker ${this.threadId}`);
|
2018-12-23 04:53:05 +01:00
|
|
|
drainMessagePort(this[kPublicPort]);
|
|
|
|
drainMessagePort(this[kPort]);
|
2017-09-01 17:03:41 +02:00
|
|
|
this[kDispose]();
|
|
|
|
this.emit('exit', code);
|
|
|
|
this.removeAllListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
[kOnCouldNotSerializeErr]() {
|
|
|
|
this.emit('error', new ERR_WORKER_UNSERIALIZABLE_ERROR());
|
|
|
|
}
|
|
|
|
|
|
|
|
[kOnErrorMessage](serialized) {
|
|
|
|
// This is what is called for uncaught exceptions.
|
|
|
|
const error = deserializeError(serialized);
|
|
|
|
this.emit('error', error);
|
|
|
|
}
|
|
|
|
|
|
|
|
[kOnMessage](message) {
|
|
|
|
switch (message.type) {
|
2018-06-26 02:14:41 +02:00
|
|
|
case messageTypes.UP_AND_RUNNING:
|
2017-09-01 17:03:41 +02:00
|
|
|
return this.emit('online');
|
2018-06-26 02:14:41 +02:00
|
|
|
case messageTypes.COULD_NOT_SERIALIZE_ERROR:
|
2017-09-01 17:03:41 +02:00
|
|
|
return this[kOnCouldNotSerializeErr]();
|
2018-06-26 02:14:41 +02:00
|
|
|
case messageTypes.ERROR_MESSAGE:
|
2017-09-01 17:03:41 +02:00
|
|
|
return this[kOnErrorMessage](message.error);
|
2018-06-26 02:14:41 +02:00
|
|
|
case messageTypes.STDIO_PAYLOAD:
|
2018-05-13 23:25:14 +02:00
|
|
|
{
|
|
|
|
const { stream, chunk, encoding } = message;
|
|
|
|
return this[kParentSideStdio][stream].push(chunk, encoding);
|
|
|
|
}
|
2018-06-26 02:14:41 +02:00
|
|
|
case messageTypes.STDIO_WANTS_MORE_DATA:
|
2018-05-13 23:25:14 +02:00
|
|
|
{
|
|
|
|
const { stream } = message;
|
|
|
|
return this[kParentSideStdio][stream][kStdioWantsMoreDataCallback]();
|
|
|
|
}
|
2017-09-01 17:03:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
assert.fail(`Unknown worker message type ${message.type}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
[kDispose]() {
|
|
|
|
this[kHandle].onexit = null;
|
|
|
|
this[kHandle] = null;
|
|
|
|
this[kPort] = null;
|
|
|
|
this[kPublicPort] = null;
|
2018-05-13 23:25:14 +02:00
|
|
|
|
|
|
|
const { stdout, stderr } = this[kParentSideStdio];
|
|
|
|
this[kParentSideStdio] = null;
|
|
|
|
|
|
|
|
if (!stdout._readableState.ended) {
|
|
|
|
debug(`[${threadId}] explicitly closes stdout for ${this.threadId}`);
|
|
|
|
stdout.push(null);
|
|
|
|
}
|
|
|
|
if (!stderr._readableState.ended) {
|
|
|
|
debug(`[${threadId}] explicitly closes stderr for ${this.threadId}`);
|
|
|
|
stderr.push(null);
|
|
|
|
}
|
2017-09-01 17:03:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
postMessage(...args) {
|
|
|
|
this[kPublicPort].postMessage(...args);
|
|
|
|
}
|
|
|
|
|
|
|
|
terminate(callback) {
|
|
|
|
if (this[kHandle] === null) return;
|
|
|
|
|
|
|
|
debug(`[${threadId}] terminates Worker with ID ${this.threadId}`);
|
|
|
|
|
|
|
|
if (typeof callback !== 'undefined')
|
|
|
|
this.once('exit', (exitCode) => callback(null, exitCode));
|
|
|
|
|
|
|
|
this[kHandle].stopThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
ref() {
|
|
|
|
if (this[kHandle] === null) return;
|
|
|
|
|
|
|
|
this[kHandle].ref();
|
|
|
|
this[kPublicPort].ref();
|
|
|
|
}
|
|
|
|
|
|
|
|
unref() {
|
|
|
|
if (this[kHandle] === null) return;
|
|
|
|
|
|
|
|
this[kHandle].unref();
|
|
|
|
this[kPublicPort].unref();
|
|
|
|
}
|
|
|
|
|
|
|
|
get threadId() {
|
|
|
|
if (this[kHandle] === null) return -1;
|
|
|
|
|
|
|
|
return this[kHandle].threadId;
|
|
|
|
}
|
2018-05-13 23:25:14 +02:00
|
|
|
|
|
|
|
get stdin() {
|
|
|
|
return this[kParentSideStdio].stdin;
|
|
|
|
}
|
|
|
|
|
|
|
|
get stdout() {
|
|
|
|
return this[kParentSideStdio].stdout;
|
|
|
|
}
|
|
|
|
|
|
|
|
get stderr() {
|
|
|
|
return this[kParentSideStdio].stderr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function pipeWithoutWarning(source, dest) {
|
|
|
|
const sourceMaxListeners = source._maxListeners;
|
|
|
|
const destMaxListeners = dest._maxListeners;
|
|
|
|
source.setMaxListeners(Infinity);
|
|
|
|
dest.setMaxListeners(Infinity);
|
|
|
|
|
|
|
|
source.pipe(dest);
|
|
|
|
|
|
|
|
source._maxListeners = sourceMaxListeners;
|
|
|
|
dest._maxListeners = destMaxListeners;
|
|
|
|
}
|
|
|
|
|
2017-09-05 22:38:32 +02:00
|
|
|
module.exports = {
|
2019-02-01 23:47:38 +01:00
|
|
|
ownsProcessState,
|
|
|
|
isMainThread,
|
2017-09-01 17:03:41 +02:00
|
|
|
threadId,
|
|
|
|
Worker,
|
2017-09-05 22:38:32 +02:00
|
|
|
};
|