This commit is contained in:
2022-07-18 02:50:52 +00:00
parent befd344ab0
commit 06181b34d6
8569 changed files with 818704 additions and 352705 deletions
+3 -2
View File
@@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/
/// <reference types="node" />
import { ChildMessage, OnEnd, OnStart, WorkerInterface, WorkerOptions } from '../types';
import { ChildMessage, OnCustomMessage, OnEnd, OnStart, WorkerInterface, WorkerOptions } from '../types';
/**
* This class wraps the child process and provides a nice interface to
* communicate with. It takes care of:
@@ -30,6 +30,7 @@ export default class ChildProcessWorker implements WorkerInterface {
private _request;
private _retries;
private _onProcessEnd;
private _onCustomMessage;
private _fakeStream;
private _stdout;
private _stderr;
@@ -40,7 +41,7 @@ export default class ChildProcessWorker implements WorkerInterface {
private _shutdown;
private _onMessage;
private _onExit;
send(request: ChildMessage, onProcessStart: OnStart, onProcessEnd: OnEnd): void;
send(request: ChildMessage, onProcessStart: OnStart, onProcessEnd: OnEnd, onCustomMessage: OnCustomMessage): void;
waitForExit(): Promise<void>;
forceExit(): void;
getWorkerId(): number;
+13 -5
View File
@@ -109,6 +109,8 @@ class ChildProcessWorker {
_defineProperty(this, '_onProcessEnd', void 0);
_defineProperty(this, '_onCustomMessage', void 0);
_defineProperty(this, '_fakeStream', void 0);
_defineProperty(this, '_stdout', void 0);
@@ -214,6 +216,7 @@ class ChildProcessWorker {
}
_onMessage(response) {
// TODO: Add appropriate type check
let error;
switch (response[0]) {
@@ -226,7 +229,7 @@ class ChildProcessWorker {
error = response[4];
if (error != null && typeof error === 'object') {
const extra = error; // @ts-ignore: no index
const extra = error; // @ts-expect-error: no index
const NativeCtor = global[response[1]];
const Ctor = typeof NativeCtor === 'function' ? NativeCtor : Error;
@@ -235,7 +238,6 @@ class ChildProcessWorker {
error.stack = response[3];
for (const key in extra) {
// @ts-ignore: adding custom properties to errors.
error[key] = extra[key];
}
}
@@ -245,8 +247,7 @@ class ChildProcessWorker {
break;
case _types().PARENT_MESSAGE_SETUP_ERROR:
error = new Error('Error when calling setup: ' + response[2]); // @ts-ignore: adding custom properties to errors.
error = new Error('Error when calling setup: ' + response[2]);
error.type = response[1];
error.stack = response[3];
@@ -254,6 +255,11 @@ class ChildProcessWorker {
break;
case _types().PARENT_MESSAGE_CUSTOM:
this._onCustomMessage(response[1]);
break;
default:
throw new TypeError('Unexpected response from worker: ' + response[0]);
}
@@ -275,7 +281,7 @@ class ChildProcessWorker {
}
}
send(request, onProcessStart, onProcessEnd) {
send(request, onProcessStart, onProcessEnd, onCustomMessage) {
onProcessStart(this);
this._onProcessEnd = (...args) => {
@@ -285,6 +291,8 @@ class ChildProcessWorker {
return onProcessEnd(...args);
};
this._onCustomMessage = (...arg) => onCustomMessage(...arg);
this._request = request;
this._retries = 0;
+3 -2
View File
@@ -5,13 +5,14 @@
* LICENSE file in the root directory of this source tree.
*/
/// <reference types="node" />
import { ChildMessage, OnEnd, OnStart, WorkerInterface, WorkerOptions } from '../types';
import { ChildMessage, OnCustomMessage, OnEnd, OnStart, WorkerInterface, WorkerOptions } from '../types';
export default class ExperimentalWorker implements WorkerInterface {
private _worker;
private _options;
private _request;
private _retries;
private _onProcessEnd;
private _onCustomMessage;
private _fakeStream;
private _stdout;
private _stderr;
@@ -25,7 +26,7 @@ export default class ExperimentalWorker implements WorkerInterface {
private _onExit;
waitForExit(): Promise<void>;
forceExit(): void;
send(request: ChildMessage, onProcessStart: OnStart, onProcessEnd: OnEnd): void;
send(request: ChildMessage, onProcessStart: OnStart, onProcessEnd: OnEnd, onCustomMessage: OnCustomMessage): void;
getWorkerId(): number;
getStdout(): NodeJS.ReadableStream | null;
getStderr(): NodeJS.ReadableStream | null;
+15 -4
View File
@@ -127,6 +127,8 @@ class ExperimentalWorker {
_defineProperty(this, '_onProcessEnd', void 0);
_defineProperty(this, '_onCustomMessage', void 0);
_defineProperty(this, '_fakeStream', void 0);
_defineProperty(this, '_stdout', void 0);
@@ -156,6 +158,8 @@ class ExperimentalWorker {
path().resolve(__dirname, './threadChild.js'),
{
eval: false,
// @ts-expect-error: added in newer versions
resourceLimits: this._options.resourceLimits,
stderr: true,
stdout: true,
workerData: {
@@ -246,7 +250,7 @@ class ExperimentalWorker {
error = response[4];
if (error != null && typeof error === 'object') {
const extra = error; // @ts-ignore: no index
const extra = error; // @ts-expect-error: no index
const NativeCtor = global[response[1]];
const Ctor = typeof NativeCtor === 'function' ? NativeCtor : Error;
@@ -255,7 +259,7 @@ class ExperimentalWorker {
error.stack = response[3];
for (const key in extra) {
// @ts-ignore: no index
// @ts-expect-error: no index
error[key] = extra[key];
}
}
@@ -265,7 +269,7 @@ class ExperimentalWorker {
break;
case _types().PARENT_MESSAGE_SETUP_ERROR:
error = new Error('Error when calling setup: ' + response[2]); // @ts-ignore: adding custom properties to errors.
error = new Error('Error when calling setup: ' + response[2]); // @ts-expect-error: adding custom properties to errors.
error.type = response[1];
error.stack = response[3];
@@ -274,6 +278,11 @@ class ExperimentalWorker {
break;
case _types().PARENT_MESSAGE_CUSTOM:
this._onCustomMessage(response[1]);
break;
default:
throw new TypeError('Unexpected response from worker: ' + response[0]);
}
@@ -301,7 +310,7 @@ class ExperimentalWorker {
this._worker.terminate();
}
send(request, onProcessStart, onProcessEnd) {
send(request, onProcessStart, onProcessEnd, onCustomMessage) {
onProcessStart(this);
this._onProcessEnd = (...args) => {
@@ -311,6 +320,8 @@ class ExperimentalWorker {
return onProcessEnd(...args);
};
this._onCustomMessage = (...arg) => onCustomMessage(...arg);
this._request = request;
this._retries = 0;
+9
View File
@@ -0,0 +1,9 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
/// <reference types="node" />
declare const messageParent: (message: unknown, parentProcess?: NodeJS.Process) => void;
export default messageParent;
+51
View File
@@ -0,0 +1,51 @@
'use strict';
Object.defineProperty(exports, '__esModule', {
value: true
});
exports.default = void 0;
function _types() {
const data = require('../types');
_types = function () {
return data;
};
return data;
}
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
const isWorkerThread = () => {
try {
// `Require` here to support Node v10
const {isMainThread, parentPort} = require('worker_threads');
return !isMainThread && parentPort;
} catch {
return false;
}
};
const messageParent = (message, parentProcess = process) => {
try {
if (isWorkerThread()) {
// `Require` here to support Node v10
const {parentPort} = require('worker_threads');
parentPort.postMessage([_types().PARENT_MESSAGE_CUSTOM, message]);
} else if (typeof parentProcess.send === 'function') {
parentProcess.send([_types().PARENT_MESSAGE_CUSTOM, message]);
}
} catch {
throw new Error('"messageParent" can only be used inside a worker');
}
};
var _default = messageParent;
exports.default = _default;
+6 -1
View File
@@ -133,6 +133,11 @@ function execMethod(method, args) {
execFunction(main.setup, main, setupArgs, execHelper, reportInitializeError);
}
const isPromise = obj =>
!!obj &&
(typeof obj === 'object' || typeof obj === 'function') &&
typeof obj.then === 'function';
function execFunction(fn, ctx, args, onResult, onError) {
let result;
@@ -143,7 +148,7 @@ function execFunction(fn, ctx, args, onResult, onError) {
return;
}
if (result && typeof result.then === 'function') {
if (isPromise(result)) {
result.then(onResult, onError);
} else {
onResult(result);
+6 -1
View File
@@ -146,6 +146,11 @@ function execMethod(method, args) {
execFunction(main.setup, main, setupArgs, execHelper, reportInitializeError);
}
const isPromise = obj =>
!!obj &&
(typeof obj === 'object' || typeof obj === 'function') &&
typeof obj.then === 'function';
function execFunction(fn, ctx, args, onResult, onError) {
let result;
@@ -156,7 +161,7 @@ function execFunction(fn, ctx, args, onResult, onError) {
return;
}
if (result && typeof result.then === 'function') {
if (isPromise(result)) {
result.then(onResult, onError);
} else {
onResult(result);