Updating NPM dependencies

This commit is contained in:
Drew Heavner
2020-11-11 13:30:52 +00:00
committed by GitHub
parent 7a6fb66c63
commit bd487ef324
9068 changed files with 1400893 additions and 587661 deletions

View File

@@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/
/// <reference types="node" />
import { WorkerInterface, ChildMessage, OnEnd, OnStart, WorkerOptions, ParentMessage } 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:
@@ -27,21 +27,25 @@ import { WorkerInterface, ChildMessage, OnEnd, OnStart, WorkerOptions, ParentMes
export default class ChildProcessWorker implements WorkerInterface {
private _child;
private _options;
private _onProcessEnd;
private _fakeStream;
private _request;
private _retries;
private _stderr;
private _onProcessEnd;
private _onCustomMessage;
private _fakeStream;
private _stdout;
private _stderr;
private _exitPromise;
private _resolveExitPromise;
constructor(options: WorkerOptions);
initialize(): void;
private _shutdown;
onMessage(response: ParentMessage): void;
onExit(exitCode: number): void;
send(request: ChildMessage, onProcessStart: OnStart, onProcessEnd: OnEnd): void;
private _onMessage;
private _onExit;
send(request: ChildMessage, onProcessStart: OnStart, onProcessEnd: OnEnd, onCustomMessage: OnCustomMessage): void;
waitForExit(): Promise<void>;
forceExit(): void;
getWorkerId(): number;
getStdout(): NodeJS.ReadableStream | null;
getStderr(): NodeJS.ReadableStream | null;
private _getFakeStream;
}
//# sourceMappingURL=ChildProcessWorker.d.ts.map

View File

@@ -1 +0,0 @@
{"version":3,"file":"ChildProcessWorker.d.ts","sourceRoot":"","sources":["../../src/workers/ChildProcessWorker.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;;AAOH,OAAO,EAKL,eAAe,EACf,YAAY,EACZ,KAAK,EACL,OAAO,EACP,aAAa,EACb,aAAa,EACd,MAAM,UAAU,CAAC;AAElB;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,CAAC,OAAO,OAAO,kBAAmB,YAAW,eAAe;IAChE,OAAO,CAAC,MAAM,CAAgB;IAC9B,OAAO,CAAC,QAAQ,CAAgB;IAChC,OAAO,CAAC,aAAa,CAAS;IAC9B,OAAO,CAAC,WAAW,CAAqB;IACxC,OAAO,CAAC,QAAQ,CAAsB;IACtC,OAAO,CAAC,QAAQ,CAAU;IAC1B,OAAO,CAAC,OAAO,CAAwC;IACvD,OAAO,CAAC,OAAO,CAAwC;gBAE3C,OAAO,EAAE,aAAa;IAUlC,UAAU;IAiEV,OAAO,CAAC,SAAS;IAQjB,SAAS,CAAC,QAAQ,EAAE,aAAa;IA6CjC,MAAM,CAAC,QAAQ,EAAE,MAAM;IAYvB,IAAI,CAAC,OAAO,EAAE,YAAY,EAAE,cAAc,EAAE,OAAO,EAAE,YAAY,EAAE,KAAK;IAcxE,WAAW;IAIX,SAAS,IAAI,MAAM,CAAC,cAAc,GAAG,IAAI;IAIzC,SAAS,IAAI,MAAM,CAAC,cAAc,GAAG,IAAI;IAIzC,OAAO,CAAC,cAAc;CAMvB"}

View File

@@ -6,9 +6,9 @@ Object.defineProperty(exports, '__esModule', {
exports.default = void 0;
function _child_process() {
const data = _interopRequireDefault(require('child_process'));
const data = require('child_process');
_child_process = function _child_process() {
_child_process = function () {
return data;
};
@@ -18,7 +18,7 @@ function _child_process() {
function _stream() {
const data = require('stream');
_stream = function _stream() {
_stream = function () {
return data;
};
@@ -28,7 +28,7 @@ function _stream() {
function _mergeStream() {
const data = _interopRequireDefault(require('merge-stream'));
_mergeStream = function _mergeStream() {
_mergeStream = function () {
return data;
};
@@ -36,39 +36,29 @@ function _mergeStream() {
}
function _supportsColor() {
const data = _interopRequireDefault(require('supports-color'));
const data = require('supports-color');
_supportsColor = function _supportsColor() {
_supportsColor = function () {
return data;
};
return data;
}
var _types = require('../types');
function _types() {
const data = require('../types');
_types = function () {
return data;
};
return data;
}
function _interopRequireDefault(obj) {
return obj && obj.__esModule ? obj : {default: obj};
}
function _objectSpread(target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i] != null ? arguments[i] : {};
var ownKeys = Object.keys(source);
if (typeof Object.getOwnPropertySymbols === 'function') {
ownKeys = ownKeys.concat(
Object.getOwnPropertySymbols(source).filter(function(sym) {
return Object.getOwnPropertyDescriptor(source, sym).enumerable;
})
);
}
ownKeys.forEach(function(key) {
_defineProperty(target, key, source[key]);
});
}
return target;
}
function _defineProperty(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
@@ -83,6 +73,11 @@ function _defineProperty(obj, key, value) {
return obj;
}
const SIGNAL_BASE_EXIT_CODE = 128;
const SIGKILL_EXIT_CODE = SIGNAL_BASE_EXIT_CODE + 9;
const SIGTERM_EXIT_CODE = SIGNAL_BASE_EXIT_CODE + 15; // How long to wait after SIGTERM before sending SIGKILL
const SIGKILL_DELAY = 500;
/**
* This class wraps the child process and provides a nice interface to
* communicate with. It takes care of:
@@ -101,59 +96,64 @@ function _defineProperty(obj, key, value) {
* field is changed to "true", so that other workers which might encounter the
* same call skip it.
*/
class ChildProcessWorker {
constructor(options) {
_defineProperty(this, '_child', void 0);
_defineProperty(this, '_options', void 0);
_defineProperty(this, '_onProcessEnd', void 0);
_defineProperty(this, '_fakeStream', void 0);
_defineProperty(this, '_request', void 0);
_defineProperty(this, '_retries', void 0);
_defineProperty(this, '_stderr', void 0);
_defineProperty(this, '_onProcessEnd', void 0);
_defineProperty(this, '_onCustomMessage', void 0);
_defineProperty(this, '_fakeStream', void 0);
_defineProperty(this, '_stdout', void 0);
_defineProperty(this, '_stderr', void 0);
_defineProperty(this, '_exitPromise', void 0);
_defineProperty(this, '_resolveExitPromise', void 0);
this._options = options;
this._fakeStream = null;
this._request = null;
this._stderr = null;
this._fakeStream = null;
this._stdout = null;
this._stderr = null;
this._exitPromise = new Promise(resolve => {
this._resolveExitPromise = resolve;
});
this.initialize();
}
initialize() {
const forceColor = _supportsColor().default.stdout
const forceColor = _supportsColor().stdout
? {
FORCE_COLOR: '1'
}
: {};
const child = _child_process().default.fork(
const child = (0, _child_process().fork)(
require.resolve('./processChild'),
[],
_objectSpread(
{
cwd: process.cwd(),
env: _objectSpread(
{},
process.env,
{
JEST_WORKER_ID: String(this._options.workerId + 1)
},
forceColor
),
// Suppress --debug / --inspect flags while preserving others (like --harmony).
execArgv: process.execArgv.filter(v => !/^--(debug|inspect)/.test(v)),
silent: true
{
cwd: process.cwd(),
env: {
...process.env,
JEST_WORKER_ID: String(this._options.workerId + 1),
// 0-indexed workerId, 1-indexed JEST_WORKER_ID
...forceColor
},
this._options.forkOptions
)
// Suppress --debug / --inspect flags while preserving others (like --harmony).
execArgv: process.execArgv.filter(v => !/^--(debug|inspect)/.test(v)),
silent: true,
...this._options.forkOptions
}
);
if (child.stdout) {
@@ -176,10 +176,10 @@ class ChildProcessWorker {
this._stderr.add(child.stderr);
}
child.on('message', this.onMessage.bind(this));
child.on('exit', this.onExit.bind(this));
child.on('message', this._onMessage.bind(this));
child.on('exit', this._onExit.bind(this));
child.send([
_types.CHILD_MESSAGE_INITIALIZE,
_types().CHILD_MESSAGE_INITIALIZE,
false,
this._options.workerPath,
this._options.setupArgs
@@ -191,8 +191,9 @@ class ChildProcessWorker {
if (this._retries > this._options.maxRetries) {
const error = new Error('Call retries were exceeded');
this.onMessage([
_types.PARENT_MESSAGE_CLIENT_ERROR,
this._onMessage([
_types().PARENT_MESSAGE_CLIENT_ERROR,
error.name,
error.message,
error.stack,
@@ -210,22 +211,25 @@ class ChildProcessWorker {
this._fakeStream = null;
}
this._resolveExitPromise();
}
onMessage(response) {
_onMessage(response) {
// TODO: Add appropriate type check
let error;
switch (response[0]) {
case _types.PARENT_MESSAGE_OK:
case _types().PARENT_MESSAGE_OK:
this._onProcessEnd(null, response[1]);
break;
case _types.PARENT_MESSAGE_CLIENT_ERROR:
case _types().PARENT_MESSAGE_CLIENT_ERROR:
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;
@@ -234,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];
}
}
@@ -243,9 +246,8 @@ 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.
case _types().PARENT_MESSAGE_SETUP_ERROR:
error = new Error('Error when calling setup: ' + response[2]);
error.type = response[1];
error.stack = response[3];
@@ -253,13 +255,22 @@ class ChildProcessWorker {
break;
case _types().PARENT_MESSAGE_CUSTOM:
this._onCustomMessage(response[1]);
break;
default:
throw new TypeError('Unexpected response from worker: ' + response[0]);
}
}
onExit(exitCode) {
if (exitCode !== 0) {
_onExit(exitCode) {
if (
exitCode !== 0 &&
exitCode !== SIGTERM_EXIT_CODE &&
exitCode !== SIGKILL_EXIT_CODE
) {
this.initialize();
if (this._request) {
@@ -270,7 +281,7 @@ class ChildProcessWorker {
}
}
send(request, onProcessStart, onProcessEnd) {
send(request, onProcessStart, onProcessEnd, onCustomMessage) {
onProcessStart(this);
this._onProcessEnd = (...args) => {
@@ -280,12 +291,29 @@ class ChildProcessWorker {
return onProcessEnd(...args);
};
this._onCustomMessage = (...arg) => onCustomMessage(...arg);
this._request = request;
this._retries = 0;
this._child.send(request);
}
waitForExit() {
return this._exitPromise;
}
forceExit() {
this._child.kill('SIGTERM');
const sigkillTimeout = setTimeout(
() => this._child.kill('SIGKILL'),
SIGKILL_DELAY
);
this._exitPromise.then(() => clearTimeout(sigkillTimeout));
}
getWorkerId() {
return this._options.workerId;
}
@@ -300,7 +328,7 @@ class ChildProcessWorker {
_getFakeStream() {
if (!this._fakeStream) {
this._fakeStream = new (_stream()).PassThrough();
this._fakeStream = new (_stream().PassThrough)();
}
return this._fakeStream;

View File

@@ -5,25 +5,30 @@
* LICENSE file in the root directory of this source tree.
*/
/// <reference types="node" />
import { ChildMessage, OnEnd, OnStart, WorkerOptions, WorkerInterface, ParentMessage } from '../types';
import { ChildMessage, OnCustomMessage, OnEnd, OnStart, WorkerInterface, WorkerOptions } from '../types';
export default class ExperimentalWorker implements WorkerInterface {
private _worker;
private _options;
private _onProcessEnd;
private _request;
private _retries;
private _stderr;
private _stdout;
private _onProcessEnd;
private _onCustomMessage;
private _fakeStream;
private _stdout;
private _stderr;
private _exitPromise;
private _resolveExitPromise;
private _forceExited;
constructor(options: WorkerOptions);
initialize(): void;
private _shutdown;
onMessage(response: ParentMessage): void;
onExit(exitCode: number): void;
send(request: ChildMessage, onProcessStart: OnStart, onProcessEnd: OnEnd): void;
private _onMessage;
private _onExit;
waitForExit(): Promise<void>;
forceExit(): void;
send(request: ChildMessage, onProcessStart: OnStart, onProcessEnd: OnEnd, onCustomMessage: OnCustomMessage): void;
getWorkerId(): number;
getStdout(): NodeJS.ReadableStream | null;
getStderr(): NodeJS.ReadableStream | null;
private _getFakeStream;
}
//# sourceMappingURL=NodeThreadsWorker.d.ts.map

View File

@@ -1 +0,0 @@
{"version":3,"file":"NodeThreadsWorker.d.ts","sourceRoot":"","sources":["../../src/workers/NodeThreadsWorker.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;;AASH,OAAO,EAKL,YAAY,EACZ,KAAK,EACL,OAAO,EACP,aAAa,EACb,eAAe,EACf,aAAa,EACd,MAAM,UAAU,CAAC;AAElB,MAAM,CAAC,OAAO,OAAO,kBAAmB,YAAW,eAAe;IAChE,OAAO,CAAC,OAAO,CAAU;IACzB,OAAO,CAAC,QAAQ,CAAgB;IAChC,OAAO,CAAC,aAAa,CAAS;IAC9B,OAAO,CAAC,QAAQ,CAAsB;IACtC,OAAO,CAAC,QAAQ,CAAU;IAC1B,OAAO,CAAC,OAAO,CAAwC;IACvD,OAAO,CAAC,OAAO,CAAwC;IACvD,OAAO,CAAC,WAAW,CAAqB;gBAE5B,OAAO,EAAE,aAAa;IAUlC,UAAU;IAkEV,OAAO,CAAC,SAAS;IAQjB,SAAS,CAAC,QAAQ,EAAE,aAAa;IA2CjC,MAAM,CAAC,QAAQ,EAAE,MAAM;IAYvB,IAAI,CAAC,OAAO,EAAE,YAAY,EAAE,cAAc,EAAE,OAAO,EAAE,YAAY,EAAE,KAAK;IAexE,WAAW;IAIX,SAAS,IAAI,MAAM,CAAC,cAAc,GAAG,IAAI;IAIzC,SAAS,IAAI,MAAM,CAAC,cAAc,GAAG,IAAI;IAIzC,OAAO,CAAC,cAAc;CAMvB"}

View File

@@ -5,10 +5,10 @@ Object.defineProperty(exports, '__esModule', {
});
exports.default = void 0;
function _path() {
const data = _interopRequireDefault(require('path'));
function path() {
const data = _interopRequireWildcard(require('path'));
_path = function _path() {
path = function () {
return data;
};
@@ -18,7 +18,7 @@ function _path() {
function _stream() {
const data = require('stream');
_stream = function _stream() {
_stream = function () {
return data;
};
@@ -28,7 +28,7 @@ function _stream() {
function _worker_threads() {
const data = require('worker_threads');
_worker_threads = function _worker_threads() {
_worker_threads = function () {
return data;
};
@@ -38,35 +38,67 @@ function _worker_threads() {
function _mergeStream() {
const data = _interopRequireDefault(require('merge-stream'));
_mergeStream = function _mergeStream() {
_mergeStream = function () {
return data;
};
return data;
}
var _types = require('../types');
function _types() {
const data = require('../types');
_types = function () {
return data;
};
return data;
}
function _interopRequireDefault(obj) {
return obj && obj.__esModule ? obj : {default: obj};
}
function _objectSpread(target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i] != null ? arguments[i] : {};
var ownKeys = Object.keys(source);
if (typeof Object.getOwnPropertySymbols === 'function') {
ownKeys = ownKeys.concat(
Object.getOwnPropertySymbols(source).filter(function(sym) {
return Object.getOwnPropertyDescriptor(source, sym).enumerable;
})
);
}
ownKeys.forEach(function(key) {
_defineProperty(target, key, source[key]);
});
function _getRequireWildcardCache() {
if (typeof WeakMap !== 'function') return null;
var cache = new WeakMap();
_getRequireWildcardCache = function () {
return cache;
};
return cache;
}
function _interopRequireWildcard(obj) {
if (obj && obj.__esModule) {
return obj;
}
return target;
if (obj === null || (typeof obj !== 'object' && typeof obj !== 'function')) {
return {default: obj};
}
var cache = _getRequireWildcardCache();
if (cache && cache.has(obj)) {
return cache.get(obj);
}
var newObj = {};
var hasPropertyDescriptor =
Object.defineProperty && Object.getOwnPropertyDescriptor;
for (var key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
var desc = hasPropertyDescriptor
? Object.getOwnPropertyDescriptor(obj, key)
: null;
if (desc && (desc.get || desc.set)) {
Object.defineProperty(newObj, key, desc);
} else {
newObj[key] = obj[key];
}
}
}
newObj.default = obj;
if (cache) {
cache.set(obj, newObj);
}
return newObj;
}
function _defineProperty(obj, key, value) {
@@ -89,47 +121,58 @@ class ExperimentalWorker {
_defineProperty(this, '_options', void 0);
_defineProperty(this, '_onProcessEnd', void 0);
_defineProperty(this, '_request', void 0);
_defineProperty(this, '_retries', void 0);
_defineProperty(this, '_stderr', void 0);
_defineProperty(this, '_onProcessEnd', void 0);
_defineProperty(this, '_stdout', void 0);
_defineProperty(this, '_onCustomMessage', void 0);
_defineProperty(this, '_fakeStream', void 0);
_defineProperty(this, '_stdout', void 0);
_defineProperty(this, '_stderr', void 0);
_defineProperty(this, '_exitPromise', void 0);
_defineProperty(this, '_resolveExitPromise', void 0);
_defineProperty(this, '_forceExited', void 0);
this._options = options;
this._request = null;
this._stderr = null;
this._stdout = null;
this._fakeStream = null;
this._stdout = null;
this._stderr = null;
this._exitPromise = new Promise(resolve => {
this._resolveExitPromise = resolve;
});
this._forceExited = false;
this.initialize();
}
initialize() {
this._worker = new (_worker_threads()).Worker(
_path().default.resolve(__dirname, './threadChild.js'),
this._worker = new (_worker_threads().Worker)(
path().resolve(__dirname, './threadChild.js'),
{
eval: false,
// @ts-expect-error: added in newer versions
resourceLimits: this._options.resourceLimits,
stderr: true,
stdout: true,
workerData: _objectSpread(
{
cwd: process.cwd(),
env: _objectSpread({}, process.env, {
JEST_WORKER_ID: String(this._options.workerId + 1) // 0-indexed workerId, 1-indexed JEST_WORKER_ID
}),
// Suppress --debug / --inspect flags while preserving others (like --harmony).
execArgv: process.execArgv.filter(
v => !/^--(debug|inspect)/.test(v)
),
silent: true
workerData: {
cwd: process.cwd(),
env: {
...process.env,
JEST_WORKER_ID: String(this._options.workerId + 1) // 0-indexed workerId, 1-indexed JEST_WORKER_ID
},
this._options.forkOptions
)
// Suppress --debug / --inspect flags while preserving others (like --harmony).
execArgv: process.execArgv.filter(v => !/^--(debug|inspect)/.test(v)),
silent: true,
...this._options.forkOptions
}
}
);
@@ -153,12 +196,12 @@ class ExperimentalWorker {
this._stderr.add(this._worker.stderr);
}
this._worker.on('message', this.onMessage.bind(this));
this._worker.on('message', this._onMessage.bind(this));
this._worker.on('exit', this.onExit.bind(this));
this._worker.on('exit', this._onExit.bind(this));
this._worker.postMessage([
_types.CHILD_MESSAGE_INITIALIZE,
_types().CHILD_MESSAGE_INITIALIZE,
false,
this._options.workerPath,
this._options.setupArgs
@@ -170,8 +213,9 @@ class ExperimentalWorker {
if (this._retries > this._options.maxRetries) {
const error = new Error('Call retries were exceeded');
this.onMessage([
_types.PARENT_MESSAGE_CLIENT_ERROR,
this._onMessage([
_types().PARENT_MESSAGE_CLIENT_ERROR,
error.name,
error.message,
error.stack,
@@ -189,22 +233,24 @@ class ExperimentalWorker {
this._fakeStream = null;
}
this._resolveExitPromise();
}
onMessage(response) {
_onMessage(response) {
let error;
switch (response[0]) {
case _types.PARENT_MESSAGE_OK:
case _types().PARENT_MESSAGE_OK:
this._onProcessEnd(null, response[1]);
break;
case _types.PARENT_MESSAGE_CLIENT_ERROR:
case _types().PARENT_MESSAGE_CLIENT_ERROR:
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;
@@ -213,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];
}
}
@@ -222,8 +268,8 @@ 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.
case _types().PARENT_MESSAGE_SETUP_ERROR:
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];
@@ -232,13 +278,18 @@ class ExperimentalWorker {
break;
case _types().PARENT_MESSAGE_CUSTOM:
this._onCustomMessage(response[1]);
break;
default:
throw new TypeError('Unexpected response from worker: ' + response[0]);
}
}
onExit(exitCode) {
if (exitCode !== 0) {
_onExit(exitCode) {
if (exitCode !== 0 && !this._forceExited) {
this.initialize();
if (this._request) {
@@ -249,7 +300,17 @@ class ExperimentalWorker {
}
}
send(request, onProcessStart, onProcessEnd) {
waitForExit() {
return this._exitPromise;
}
forceExit() {
this._forceExited = true;
this._worker.terminate();
}
send(request, onProcessStart, onProcessEnd, onCustomMessage) {
onProcessStart(this);
this._onProcessEnd = (...args) => {
@@ -259,6 +320,8 @@ class ExperimentalWorker {
return onProcessEnd(...args);
};
this._onCustomMessage = (...arg) => onCustomMessage(...arg);
this._request = request;
this._retries = 0;
@@ -279,7 +342,7 @@ class ExperimentalWorker {
_getFakeStream() {
if (!this._fakeStream) {
this._fakeStream = new (_stream()).PassThrough();
this._fakeStream = new (_stream().PassThrough)();
}
return this._fakeStream;

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;

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;

View File

@@ -5,4 +5,3 @@
* LICENSE file in the root directory of this source tree.
*/
export {};
//# sourceMappingURL=processChild.d.ts.map

View File

@@ -1 +0,0 @@
{"version":3,"file":"processChild.d.ts","sourceRoot":"","sources":["../../src/workers/processChild.ts"],"names":[],"mappings":"AAAA;;;;;GAKG"}

View File

@@ -1,39 +1,21 @@
'use strict';
var _types = require('../types');
function _types() {
const data = require('../types');
function _objectSpread(target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i] != null ? arguments[i] : {};
var ownKeys = Object.keys(source);
if (typeof Object.getOwnPropertySymbols === 'function') {
ownKeys = ownKeys.concat(
Object.getOwnPropertySymbols(source).filter(function(sym) {
return Object.getOwnPropertyDescriptor(source, sym).enumerable;
})
);
}
ownKeys.forEach(function(key) {
_defineProperty(target, key, source[key]);
});
}
return target;
}
function _defineProperty(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
_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.
*/
let file = null;
let setupArgs = [];
let initialized = false;
@@ -51,20 +33,20 @@ let initialized = false;
* non-zero exit code.
*/
process.on('message', request => {
const messageListener = request => {
switch (request[0]) {
case _types.CHILD_MESSAGE_INITIALIZE:
case _types().CHILD_MESSAGE_INITIALIZE:
const init = request;
file = init[2];
setupArgs = request[3];
break;
case _types.CHILD_MESSAGE_CALL:
case _types().CHILD_MESSAGE_CALL:
const call = request;
execMethod(call[2], call[3]);
break;
case _types.CHILD_MESSAGE_END:
case _types().CHILD_MESSAGE_END:
end();
break;
@@ -73,22 +55,24 @@ process.on('message', request => {
'Unexpected request from parent process: ' + request[0]
);
}
});
};
process.on('message', messageListener);
function reportSuccess(result) {
if (!process || !process.send) {
throw new Error('Child can only be used on a forked process');
}
process.send([_types.PARENT_MESSAGE_OK, result]);
process.send([_types().PARENT_MESSAGE_OK, result]);
}
function reportClientError(error) {
return reportError(error, _types.PARENT_MESSAGE_CLIENT_ERROR);
return reportError(error, _types().PARENT_MESSAGE_CLIENT_ERROR);
}
function reportInitializeError(error) {
return reportError(error, _types.PARENT_MESSAGE_SETUP_ERROR);
return reportError(error, _types().PARENT_MESSAGE_SETUP_ERROR);
}
function reportError(error, type) {
@@ -105,7 +89,7 @@ function reportError(error, type) {
error.constructor && error.constructor.name,
error.message,
error.stack,
typeof error === 'object' ? _objectSpread({}, error) : error
typeof error === 'object' ? {...error} : error
]);
}
@@ -121,7 +105,8 @@ function end() {
}
function exitProcess() {
process.exit(0);
// Clean up open handles so the process ideally exits gracefully
process.removeListener('message', messageListener);
}
function execMethod(method, args) {
@@ -148,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;
@@ -158,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);

View File

@@ -5,4 +5,3 @@
* LICENSE file in the root directory of this source tree.
*/
export {};
//# sourceMappingURL=threadChild.d.ts.map

View File

@@ -1 +0,0 @@
{"version":3,"file":"threadChild.d.ts","sourceRoot":"","sources":["../../src/workers/threadChild.ts"],"names":[],"mappings":"AAAA;;;;;GAKG"}

View File

@@ -3,47 +3,29 @@
function _worker_threads() {
const data = require('worker_threads');
_worker_threads = function _worker_threads() {
_worker_threads = function () {
return data;
};
return data;
}
var _types = require('../types');
function _types() {
const data = require('../types');
function _objectSpread(target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i] != null ? arguments[i] : {};
var ownKeys = Object.keys(source);
if (typeof Object.getOwnPropertySymbols === 'function') {
ownKeys = ownKeys.concat(
Object.getOwnPropertySymbols(source).filter(function(sym) {
return Object.getOwnPropertyDescriptor(source, sym).enumerable;
})
);
}
ownKeys.forEach(function(key) {
_defineProperty(target, key, source[key]);
});
}
return target;
}
function _defineProperty(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
_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.
*/
let file = null;
let setupArgs = [];
let initialized = false;
@@ -61,20 +43,20 @@ let initialized = false;
* non-zero exit code.
*/
_worker_threads().parentPort.on('message', request => {
const messageListener = request => {
switch (request[0]) {
case _types.CHILD_MESSAGE_INITIALIZE:
case _types().CHILD_MESSAGE_INITIALIZE:
const init = request;
file = init[2];
setupArgs = request[3];
break;
case _types.CHILD_MESSAGE_CALL:
case _types().CHILD_MESSAGE_CALL:
const call = request;
execMethod(call[2], call[3]);
break;
case _types.CHILD_MESSAGE_END:
case _types().CHILD_MESSAGE_END:
end();
break;
@@ -83,22 +65,27 @@ _worker_threads().parentPort.on('message', request => {
'Unexpected request from parent process: ' + request[0]
);
}
});
};
_worker_threads().parentPort.on('message', messageListener);
function reportSuccess(result) {
if (_worker_threads().isMainThread) {
throw new Error('Child can only be used on a forked process');
}
_worker_threads().parentPort.postMessage([_types.PARENT_MESSAGE_OK, result]);
_worker_threads().parentPort.postMessage([
_types().PARENT_MESSAGE_OK,
result
]);
}
function reportClientError(error) {
return reportError(error, _types.PARENT_MESSAGE_CLIENT_ERROR);
return reportError(error, _types().PARENT_MESSAGE_CLIENT_ERROR);
}
function reportInitializeError(error) {
return reportError(error, _types.PARENT_MESSAGE_SETUP_ERROR);
return reportError(error, _types().PARENT_MESSAGE_SETUP_ERROR);
}
function reportError(error, type) {
@@ -115,7 +102,7 @@ function reportError(error, type) {
error.constructor && error.constructor.name,
error.message,
error.stack,
typeof error === 'object' ? _objectSpread({}, error) : error
typeof error === 'object' ? {...error} : error
]);
}
@@ -131,7 +118,8 @@ function end() {
}
function exitProcess() {
process.exit(0);
// Clean up open handles so the worker ideally exits gracefully
_worker_threads().parentPort.removeListener('message', messageListener);
}
function execMethod(method, args) {
@@ -158,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;
@@ -168,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);