feat: initial commit

This commit is contained in:
Carlos
2024-08-12 23:06:35 -04:00
parent c581ce5ad2
commit de4ae5d068
776 changed files with 3868 additions and 15649 deletions

View File

@ -22,63 +22,63 @@ Object.defineProperty(exports, 'messageParent', {
return _messageParent.default;
}
});
function _os() {
const data = require('os');
_os = function () {
return data;
};
return data;
}
function _path() {
const data = require('path');
_path = function () {
return data;
};
return data;
}
function _url() {
const data = require('url');
_url = function () {
return data;
};
return data;
}
var _Farm = _interopRequireDefault(require('./Farm'));
var _WorkerPool = _interopRequireDefault(require('./WorkerPool'));
var _PriorityQueue = _interopRequireDefault(require('./PriorityQueue'));
var _FifoQueue = _interopRequireDefault(require('./FifoQueue'));
var _messageParent = _interopRequireDefault(require('./workers/messageParent'));
function _interopRequireDefault(obj) {
return obj && obj.__esModule ? obj : {default: obj};
}
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;
}
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
function getExposedMethods(workerPath, options) {
let exposedMethods = options.exposedMethods; // If no methods list is given, try getting it by auto-requiring the module.
let exposedMethods = options.exposedMethods;
// If no methods list is given, try getting it by auto-requiring the module.
if (!exposedMethods) {
const module = require(workerPath);
exposedMethods = Object.keys(module).filter(
// @ts-expect-error: no index
name => typeof module[name] === 'function'
);
if (typeof module === 'function') {
exposedMethods = [...exposedMethods, 'default'];
}
}
return exposedMethods;
}
function getNumberOfCpus() {
return typeof _os().availableParallelism === 'function'
? (0, _os().availableParallelism)()
: (0, _os().cpus)().length;
}
/**
* The Jest farm (publicly called "Worker") is a class that allows you to queue
* methods across multiple child processes, in order to parallelize work. This
@ -104,61 +104,35 @@ function getExposedMethods(workerPath, options) {
* processed by the same worker. This is specially useful if your workers
* are caching results.
*/
class Worker {
_ending;
_farm;
_options;
_workerPool;
constructor(workerPath, options) {
var _this$_options$enable,
_this$_options$forkOp,
_this$_options$maxRet,
_this$_options$numWor,
_this$_options$resour,
_this$_options$setupA;
_defineProperty(this, '_ending', void 0);
_defineProperty(this, '_farm', void 0);
_defineProperty(this, '_options', void 0);
_defineProperty(this, '_workerPool', void 0);
this._options = {...options};
this._ending = false;
const workerPoolOptions = {
enableWorkerThreads:
(_this$_options$enable = this._options.enableWorkerThreads) !== null &&
_this$_options$enable !== void 0
? _this$_options$enable
: false,
forkOptions:
(_this$_options$forkOp = this._options.forkOptions) !== null &&
_this$_options$forkOp !== void 0
? _this$_options$forkOp
: {},
maxRetries:
(_this$_options$maxRet = this._options.maxRetries) !== null &&
_this$_options$maxRet !== void 0
? _this$_options$maxRet
: 3,
numWorkers:
(_this$_options$numWor = this._options.numWorkers) !== null &&
_this$_options$numWor !== void 0
? _this$_options$numWor
: Math.max((0, _os().cpus)().length - 1, 1),
resourceLimits:
(_this$_options$resour = this._options.resourceLimits) !== null &&
_this$_options$resour !== void 0
? _this$_options$resour
: {},
setupArgs:
(_this$_options$setupA = this._options.setupArgs) !== null &&
_this$_options$setupA !== void 0
? _this$_options$setupA
: []
this._options = {
...options
};
this._ending = false;
if (typeof workerPath !== 'string') {
workerPath = workerPath.href;
}
if (workerPath.startsWith('file:')) {
workerPath = (0, _url().fileURLToPath)(workerPath);
} else if (!(0, _path().isAbsolute)(workerPath)) {
throw new Error(`'workerPath' must be absolute, got '${workerPath}'`);
}
const workerPoolOptions = {
enableWorkerThreads: this._options.enableWorkerThreads ?? false,
forkOptions: this._options.forkOptions ?? {},
idleMemoryLimit: this._options.idleMemoryLimit,
maxRetries: this._options.maxRetries ?? 3,
numWorkers:
this._options.numWorkers ?? Math.max(getNumberOfCpus() - 1, 1),
resourceLimits: this._options.resourceLimits ?? {},
setupArgs: this._options.setupArgs ?? []
};
if (this._options.WorkerPool) {
// @ts-expect-error: constructor target any?
this._workerPool = new this._options.WorkerPool(
workerPath,
workerPoolOptions
@ -166,7 +140,6 @@ class Worker {
} else {
this._workerPool = new _WorkerPool.default(workerPath, workerPoolOptions);
}
this._farm = new _Farm.default(
workerPoolOptions.numWorkers,
this._workerPool.send.bind(this._workerPool),
@ -176,48 +149,44 @@ class Worker {
workerSchedulingPolicy: this._options.workerSchedulingPolicy
}
);
this._bindExposedWorkerMethods(workerPath, this._options);
}
_bindExposedWorkerMethods(workerPath, options) {
getExposedMethods(workerPath, options).forEach(name => {
if (name.startsWith('_')) {
return;
}
// eslint-disable-next-line no-prototype-builtins
if (this.constructor.prototype.hasOwnProperty(name)) {
throw new TypeError('Cannot define a method called ' + name);
} // @ts-expect-error: dynamic extension of the class instance is expected.
throw new TypeError(`Cannot define a method called ${name}`);
}
// @ts-expect-error: dynamic extension of the class instance is expected.
this[name] = this._callFunctionWithArgs.bind(this, name);
});
}
_callFunctionWithArgs(method, ...args) {
if (this._ending) {
throw new Error('Farm is ended, no more calls can be done to it');
}
return this._farm.doWork(method, ...args);
}
getStderr() {
return this._workerPool.getStderr();
}
getStdout() {
return this._workerPool.getStdout();
}
async start() {
await this._workerPool.start();
}
async end() {
if (this._ending) {
throw new Error('Farm is ended, no more calls can be done to it');
}
this._ending = true;
return this._workerPool.end();
}
}
exports.Worker = Worker;