@ohos-ports/rspack-core 1.7.11-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +22 -0
- package/README.md +15 -0
- package/compiled/@swc/types/index.d.ts +2134 -0
- package/compiled/@swc/types/index.js +19 -0
- package/compiled/@swc/types/license +201 -0
- package/compiled/@swc/types/package.json +1 -0
- package/compiled/browserslist-load-config/index.d.ts +21 -0
- package/compiled/browserslist-load-config/index.js +252 -0
- package/compiled/browserslist-load-config/license +21 -0
- package/compiled/browserslist-load-config/package.json +1 -0
- package/compiled/tinypool/LICENSE +24 -0
- package/compiled/tinypool/README.md +20 -0
- package/compiled/tinypool/dist/common-Qw-RoVFD.js +28 -0
- package/compiled/tinypool/dist/entry/process.d.ts +1 -0
- package/compiled/tinypool/dist/entry/process.js +72 -0
- package/compiled/tinypool/dist/entry/utils.d.ts +7 -0
- package/compiled/tinypool/dist/entry/utils.js +3 -0
- package/compiled/tinypool/dist/entry/worker.d.ts +1 -0
- package/compiled/tinypool/dist/entry/worker.js +76 -0
- package/compiled/tinypool/dist/index.d.ts +195 -0
- package/compiled/tinypool/dist/index.js +820 -0
- package/compiled/tinypool/dist/utils-B--2TaWv.js +38 -0
- package/compiled/tinypool/dist/utils-De75vAgL.js +10 -0
- package/compiled/tinypool/package.json +39 -0
- package/compiled/watchpack/index.d.ts +217 -0
- package/compiled/watchpack/index.js +3124 -0
- package/compiled/watchpack/license +20 -0
- package/compiled/watchpack/package.json +1 -0
- package/compiled/webpack-sources/index.js +4675 -0
- package/compiled/webpack-sources/license +21 -0
- package/compiled/webpack-sources/package.json +1 -0
- package/compiled/webpack-sources/types.d.ts +458 -0
- package/dist/BuildInfo.d.ts +17 -0
- package/dist/Chunk.d.ts +16 -0
- package/dist/ChunkGraph.d.ts +9 -0
- package/dist/Chunks.d.ts +12 -0
- package/dist/CodeGenerationResults.d.ts +1 -0
- package/dist/Compilation.d.ts +438 -0
- package/dist/Compiler.d.ts +226 -0
- package/dist/ConcatenatedModule.d.ts +1 -0
- package/dist/ContextModule.d.ts +1 -0
- package/dist/ContextModuleFactory.d.ts +13 -0
- package/dist/Diagnostics.d.ts +7 -0
- package/dist/Entrypoint.d.ts +2 -0
- package/dist/ErrorHelpers.d.ts +3 -0
- package/dist/ExecuteModulePlugin.d.ts +4 -0
- package/dist/ExportsInfo.d.ts +20 -0
- package/dist/ExternalModule.d.ts +1 -0
- package/dist/FileSystem.d.ts +54 -0
- package/dist/FileSystemInfo.d.ts +5 -0
- package/dist/Module.d.ts +42 -0
- package/dist/ModuleGraph.d.ts +19 -0
- package/dist/ModuleTypeConstants.d.ts +8 -0
- package/dist/MultiCompiler.d.ts +82 -0
- package/dist/MultiStats.d.ts +23 -0
- package/dist/MultiWatching.d.ts +27 -0
- package/dist/NativeWatchFileSystem.d.ts +23 -0
- package/dist/NormalModule.d.ts +15 -0
- package/dist/NormalModuleFactory.d.ts +23 -0
- package/dist/Resolver.d.ts +30 -0
- package/dist/ResolverFactory.d.ts +17 -0
- package/dist/RspackError.d.ts +12 -0
- package/dist/RuleSetCompiler.d.ts +9 -0
- package/dist/RuntimeGlobals.d.ts +356 -0
- package/dist/RuntimeModule.d.ts +34 -0
- package/dist/Stats.d.ts +17 -0
- package/dist/Template.d.ts +77 -0
- package/dist/VirtualModulesPlugin.d.ts +12 -0
- package/dist/Watching.d.ts +56 -0
- package/dist/builtin-loader/index.d.ts +1 -0
- package/dist/builtin-loader/lightningcss/index.d.ts +117 -0
- package/dist/builtin-loader/swc/collectTypeScriptInfo.d.ts +21 -0
- package/dist/builtin-loader/swc/index.d.ts +5 -0
- package/dist/builtin-loader/swc/pluginImport.d.ts +33 -0
- package/dist/builtin-loader/swc/types.d.ts +89 -0
- package/dist/builtin-plugin/APIPlugin.d.ts +9 -0
- package/dist/builtin-plugin/ArrayPushCallbackChunkFormatPlugin.d.ts +9 -0
- package/dist/builtin-plugin/AssetModulesPlugin.d.ts +9 -0
- package/dist/builtin-plugin/AsyncWebAssemblyModulesPlugin.d.ts +9 -0
- package/dist/builtin-plugin/BannerPlugin.d.ts +40 -0
- package/dist/builtin-plugin/BundlerInfoRspackPlugin.d.ts +14 -0
- package/dist/builtin-plugin/CaseSensitivePlugin.d.ts +9 -0
- package/dist/builtin-plugin/ChunkPrefetchPreloadPlugin.d.ts +9 -0
- package/dist/builtin-plugin/CircularDependencyRspackPlugin.d.ts +54 -0
- package/dist/builtin-plugin/CommonJsChunkFormatPlugin.d.ts +9 -0
- package/dist/builtin-plugin/ContextReplacementPlugin.d.ts +9 -0
- package/dist/builtin-plugin/CopyRspackPlugin.d.ts +14 -0
- package/dist/builtin-plugin/CssChunkingPlugin.d.ts +21 -0
- package/dist/builtin-plugin/CssModulesPlugin.d.ts +9 -0
- package/dist/builtin-plugin/DataUriPlugin.d.ts +9 -0
- package/dist/builtin-plugin/DefinePlugin.d.ts +16 -0
- package/dist/builtin-plugin/DeterministicChunkIdsPlugin.d.ts +7 -0
- package/dist/builtin-plugin/DeterministicModuleIdsPlugin.d.ts +7 -0
- package/dist/builtin-plugin/DllEntryPlugin.d.ts +12 -0
- package/dist/builtin-plugin/DllReferenceAgencyPlugin.d.ts +11 -0
- package/dist/builtin-plugin/DynamicEntryPlugin.d.ts +12 -0
- package/dist/builtin-plugin/ElectronTargetPlugin.d.ts +9 -0
- package/dist/builtin-plugin/EnableChunkLoadingPlugin.d.ts +25 -0
- package/dist/builtin-plugin/EnableLibraryPlugin.d.ts +11 -0
- package/dist/builtin-plugin/EnableWasmLoadingPlugin.d.ts +9 -0
- package/dist/builtin-plugin/EnsureChunkConditionsPlugin.d.ts +9 -0
- package/dist/builtin-plugin/EntryPlugin.d.ts +32 -0
- package/dist/builtin-plugin/EsmLibraryPlugin.d.ts +11 -0
- package/dist/builtin-plugin/EvalDevToolModulePlugin.d.ts +11 -0
- package/dist/builtin-plugin/EvalSourceMapDevToolPlugin.d.ts +10 -0
- package/dist/builtin-plugin/ExternalsPlugin.d.ts +12 -0
- package/dist/builtin-plugin/FetchCompileAsyncWasmPlugin.d.ts +9 -0
- package/dist/builtin-plugin/FileUriPlugin.d.ts +9 -0
- package/dist/builtin-plugin/FlagAllModulesAsUsedPlugin.d.ts +9 -0
- package/dist/builtin-plugin/FlagDependencyExportsPlugin.d.ts +9 -0
- package/dist/builtin-plugin/FlagDependencyUsagePlugin.d.ts +9 -0
- package/dist/builtin-plugin/HotModuleReplacementPlugin.d.ts +7 -0
- package/dist/builtin-plugin/HttpExternalsRspackPlugin.d.ts +9 -0
- package/dist/builtin-plugin/HttpUriPlugin.d.ts +36 -0
- package/dist/builtin-plugin/IgnorePlugin.d.ts +19 -0
- package/dist/builtin-plugin/InferAsyncModulesPlugin.d.ts +9 -0
- package/dist/builtin-plugin/InlineExportsPlugin.d.ts +9 -0
- package/dist/builtin-plugin/JavascriptModulesPlugin.d.ts +15 -0
- package/dist/builtin-plugin/JsLoaderRspackPlugin.d.ts +10 -0
- package/dist/builtin-plugin/JsonModulesPlugin.d.ts +9 -0
- package/dist/builtin-plugin/LibManifestPlugin.d.ts +17 -0
- package/dist/builtin-plugin/LightningCssMinimizerRspackPlugin.d.ts +32 -0
- package/dist/builtin-plugin/LimitChunkCountPlugin.d.ts +14 -0
- package/dist/builtin-plugin/MangleExportsPlugin.d.ts +9 -0
- package/dist/builtin-plugin/MergeDuplicateChunksPlugin.d.ts +9 -0
- package/dist/builtin-plugin/ModuleChunkFormatPlugin.d.ts +9 -0
- package/dist/builtin-plugin/ModuleConcatenationPlugin.d.ts +7 -0
- package/dist/builtin-plugin/ModuleInfoHeaderPlugin.d.ts +9 -0
- package/dist/builtin-plugin/NamedChunkIdsPlugin.d.ts +9 -0
- package/dist/builtin-plugin/NamedModuleIdsPlugin.d.ts +9 -0
- package/dist/builtin-plugin/NaturalChunkIdsPlugin.d.ts +7 -0
- package/dist/builtin-plugin/NaturalModuleIdsPlugin.d.ts +7 -0
- package/dist/builtin-plugin/NoEmitOnErrorsPlugin.d.ts +9 -0
- package/dist/builtin-plugin/NodeTargetPlugin.d.ts +9 -0
- package/dist/builtin-plugin/NormalModuleReplacementPlugin.d.ts +10 -0
- package/dist/builtin-plugin/OccurrenceChunkIdsPlugin.d.ts +10 -0
- package/dist/builtin-plugin/ProgressPlugin.d.ts +11 -0
- package/dist/builtin-plugin/ProvidePlugin.d.ts +10 -0
- package/dist/builtin-plugin/RealContentHashPlugin.d.ts +9 -0
- package/dist/builtin-plugin/RemoveDuplicateModulesPlugin.d.ts +9 -0
- package/dist/builtin-plugin/RemoveEmptyChunksPlugin.d.ts +9 -0
- package/dist/builtin-plugin/RsdoctorPlugin.d.ts +49 -0
- package/dist/builtin-plugin/RslibPlugin.d.ts +11 -0
- package/dist/builtin-plugin/RstestPlugin.d.ts +11 -0
- package/dist/builtin-plugin/RuntimeChunkPlugin.d.ts +11 -0
- package/dist/builtin-plugin/RuntimePlugin.d.ts +29 -0
- package/dist/builtin-plugin/SideEffectsFlagPlugin.d.ts +9 -0
- package/dist/builtin-plugin/SizeLimitsPlugin.d.ts +19 -0
- package/dist/builtin-plugin/SourceMapDevToolPlugin.d.ts +11 -0
- package/dist/builtin-plugin/SplitChunksPlugin.d.ts +11 -0
- package/dist/builtin-plugin/SubresourceIntegrityPlugin.d.ts +39 -0
- package/dist/builtin-plugin/SwcJsMinimizerPlugin.d.ts +215 -0
- package/dist/builtin-plugin/URLPlugin.d.ts +9 -0
- package/dist/builtin-plugin/WebWorkerTemplatePlugin.d.ts +7 -0
- package/dist/builtin-plugin/WorkerPlugin.d.ts +14 -0
- package/dist/builtin-plugin/base.d.ts +45 -0
- package/dist/builtin-plugin/css-extract/index.d.ts +24 -0
- package/dist/builtin-plugin/css-extract/loader.d.ts +21 -0
- package/dist/builtin-plugin/css-extract/utils.d.ts +6 -0
- package/dist/builtin-plugin/html-plugin/hooks.d.ts +32 -0
- package/dist/builtin-plugin/html-plugin/index.d.ts +2 -0
- package/dist/builtin-plugin/html-plugin/options.d.ts +82 -0
- package/dist/builtin-plugin/html-plugin/plugin.d.ts +24 -0
- package/dist/builtin-plugin/html-plugin/taps.d.ts +2 -0
- package/dist/builtin-plugin/index.d.ts +84 -0
- package/dist/builtin-plugin/lazy-compilation/lazyCompilation.d.ts +10 -0
- package/dist/builtin-plugin/lazy-compilation/middleware.d.ts +12 -0
- package/dist/config/adapter.d.ts +8 -0
- package/dist/config/adapterRuleUse.d.ts +391 -0
- package/dist/config/browserslistTargetHandler.d.ts +15 -0
- package/dist/config/defaults.d.ts +13 -0
- package/dist/config/devServer.d.ts +314 -0
- package/dist/config/index.d.ts +5 -0
- package/dist/config/normalization.d.ts +174 -0
- package/dist/config/target.d.ts +93 -0
- package/dist/config/types.d.ts +2432 -0
- package/dist/container/ContainerPlugin.d.ts +41 -0
- package/dist/container/ContainerReferencePlugin.d.ts +34 -0
- package/dist/container/ModuleFederationManifestPlugin.d.ts +37 -0
- package/dist/container/ModuleFederationPlugin.d.ts +17 -0
- package/dist/container/ModuleFederationPluginV1.d.ts +22 -0
- package/dist/container/ModuleFederationRuntimePlugin.d.ts +16 -0
- package/dist/container/index.d.ts +4 -0
- package/dist/container/options.d.ts +3 -0
- package/dist/cssExtractHmr.js +124 -0
- package/dist/cssExtractLoader.js +160 -0
- package/dist/error/ConcurrentCompilationError.d.ts +14 -0
- package/dist/exports.d.ts +178 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +12638 -0
- package/dist/lib/AbstractMethodError.d.ts +23 -0
- package/dist/lib/Cache.d.ts +65 -0
- package/dist/lib/CacheFacade.d.ts +139 -0
- package/dist/lib/DllPlugin.d.ts +42 -0
- package/dist/lib/DllReferencePlugin.d.ts +119 -0
- package/dist/lib/EntryOptionPlugin.d.ts +33 -0
- package/dist/lib/EnvironmentPlugin.d.ts +22 -0
- package/dist/lib/HookWebpackError.d.ts +40 -0
- package/dist/lib/IgnoreWarningsPlugin.d.ts +25 -0
- package/dist/lib/LoaderOptionsPlugin.d.ts +28 -0
- package/dist/lib/LoaderTargetPlugin.d.ts +24 -0
- package/dist/lib/ModuleFilenameHelpers.d.ts +53 -0
- package/dist/lib/WebpackError.d.ts +21 -0
- package/dist/lib/cache/MemoryCachePlugin.d.ts +5 -0
- package/dist/lib/cache/getLazyHashedEtag.d.ts +35 -0
- package/dist/lib/cache/mergeEtags.d.ts +17 -0
- package/dist/loader-runner/LoaderLoadingError.d.ts +13 -0
- package/dist/loader-runner/ModuleError.d.ts +13 -0
- package/dist/loader-runner/index.d.ts +36 -0
- package/dist/loader-runner/loadLoader.d.ts +12 -0
- package/dist/loader-runner/service.d.ts +73 -0
- package/dist/loader-runner/utils.d.ts +7 -0
- package/dist/loader-runner/worker.d.ts +14 -0
- package/dist/logging/Logger.d.ts +61 -0
- package/dist/logging/createConsoleLogger.d.ts +35 -0
- package/dist/logging/truncateArgs.d.ts +16 -0
- package/dist/moduleFederationDefaultRuntime.js +1 -0
- package/dist/node/NodeEnvironmentPlugin.d.ts +10 -0
- package/dist/node/NodeTemplatePlugin.d.ts +18 -0
- package/dist/node/NodeWatchFileSystem.d.ts +18 -0
- package/dist/node/nodeConsole.d.ts +15 -0
- package/dist/rspack.d.ts +25 -0
- package/dist/rspackOptionsApply.d.ts +4 -0
- package/dist/runtime/cssExtractHmr.d.ts +4 -0
- package/dist/runtime/moduleFederationDefaultRuntime.d.ts +2 -0
- package/dist/sharing/ConsumeSharedPlugin.d.ts +42 -0
- package/dist/sharing/ProvideSharedPlugin.d.ts +35 -0
- package/dist/sharing/SharePlugin.d.ts +53 -0
- package/dist/sharing/ShareRuntimePlugin.d.ts +9 -0
- package/dist/sharing/utils.d.ts +1 -0
- package/dist/stats/DefaultStatsFactoryPlugin.d.ts +20 -0
- package/dist/stats/DefaultStatsPresetPlugin.d.ts +4 -0
- package/dist/stats/DefaultStatsPrinterPlugin.d.ts +13 -0
- package/dist/stats/StatsFactory.d.ts +57 -0
- package/dist/stats/StatsPrinter.d.ts +57 -0
- package/dist/stats/statsFactoryUtils.d.ts +341 -0
- package/dist/swc.d.ts +7 -0
- package/dist/taps/compilation.d.ts +8 -0
- package/dist/taps/compiler.d.ts +2 -0
- package/dist/taps/contextModuleFactory.d.ts +2 -0
- package/dist/taps/index.d.ts +6 -0
- package/dist/taps/javascriptModules.d.ts +2 -0
- package/dist/taps/normalModuleFactory.d.ts +2 -0
- package/dist/taps/types.d.ts +11 -0
- package/dist/trace/index.d.ts +46 -0
- package/dist/trace/traceHookPlugin.d.ts +5 -0
- package/dist/util/ArrayQueue.d.ts +45 -0
- package/dist/util/AsyncTask.d.ts +7 -0
- package/dist/util/MergeCaller.d.ts +10 -0
- package/dist/util/SizeFormatHelpers.d.ts +10 -0
- package/dist/util/SplitChunkSize.d.ts +5 -0
- package/dist/util/assertNotNil.d.ts +1 -0
- package/dist/util/assetCondition.d.ts +2 -0
- package/dist/util/asyncLib.d.ts +54 -0
- package/dist/util/bindingVersionCheck.d.ts +5 -0
- package/dist/util/cleverMerge.d.ts +24 -0
- package/dist/util/comparators.d.ts +16 -0
- package/dist/util/createHash.d.ts +16 -0
- package/dist/util/createReadonlyMap.d.ts +1 -0
- package/dist/util/fake.d.ts +14 -0
- package/dist/util/fs.d.ts +375 -0
- package/dist/util/hash/index.d.ts +26 -0
- package/dist/util/hash/md4.d.ts +11 -0
- package/dist/util/hash/wasm-hash.d.ts +51 -0
- package/dist/util/hash/xxhash64.d.ts +11 -0
- package/dist/util/identifier.d.ts +31 -0
- package/dist/util/index.d.ts +9 -0
- package/dist/util/memoize.d.ts +2 -0
- package/dist/util/runtime.d.ts +2 -0
- package/dist/util/smartGrouping.d.ts +21 -0
- package/dist/util/source.d.ts +6 -0
- package/dist/util/validateConfig.d.ts +5 -0
- package/dist/worker.js +978 -0
- package/hot/dev-server.js +75 -0
- package/hot/emitter.js +21 -0
- package/hot/lazy-compilation-node.js +106 -0
- package/hot/lazy-compilation-web.js +98 -0
- package/hot/log-apply-result.js +49 -0
- package/hot/log.js +79 -0
- package/hot/only-dev-server.js +103 -0
- package/hot/poll.js +40 -0
- package/hot/signal.js +66 -0
- package/module.d.ts +273 -0
- package/package.json +79 -0
|
@@ -0,0 +1,820 @@
|
|
|
1
|
+
import { isMovable, isTaskQueue, isTransferable, kFieldCount, kQueueOptions, kRequestCountField, kResponseCountField, kTransferable, kValue, markMovable } from "./common-Qw-RoVFD.js";
|
|
2
|
+
import { MessageChannel, MessagePort, Worker, receiveMessageOnPort } from "node:worker_threads";
|
|
3
|
+
import { EventEmitterAsyncResource, once } from "node:events";
|
|
4
|
+
import { AsyncResource } from "node:async_hooks";
|
|
5
|
+
import { URL, fileURLToPath } from "node:url";
|
|
6
|
+
import { join } from "node:path";
|
|
7
|
+
import { inspect, types } from "node:util";
|
|
8
|
+
import assert from "node:assert";
|
|
9
|
+
import { performance } from "node:perf_hooks";
|
|
10
|
+
import { readFileSync } from "node:fs";
|
|
11
|
+
import os from "node:os";
|
|
12
|
+
import childProcess, { fork } from "node:child_process";
|
|
13
|
+
|
|
14
|
+
//#region src/physicalCpuCount.ts
|
|
15
|
+
function exec(command) {
|
|
16
|
+
const output = childProcess.execSync(command, {
|
|
17
|
+
encoding: "utf8",
|
|
18
|
+
stdio: [
|
|
19
|
+
null,
|
|
20
|
+
null,
|
|
21
|
+
null
|
|
22
|
+
]
|
|
23
|
+
});
|
|
24
|
+
return output;
|
|
25
|
+
}
|
|
26
|
+
let amount;
|
|
27
|
+
try {
|
|
28
|
+
const platform = os.platform();
|
|
29
|
+
if (platform === "linux") {
|
|
30
|
+
const output1 = exec("cat /proc/cpuinfo | grep \"physical id\" | sort |uniq | wc -l");
|
|
31
|
+
const output2 = exec("cat /proc/cpuinfo | grep \"core id\" | sort | uniq | wc -l");
|
|
32
|
+
const physicalCpuAmount = parseInt(output1.trim(), 10);
|
|
33
|
+
const physicalCoreAmount = parseInt(output2.trim(), 10);
|
|
34
|
+
amount = physicalCpuAmount * physicalCoreAmount;
|
|
35
|
+
} else if (platform === "darwin") {
|
|
36
|
+
const output = exec("sysctl -n hw.physicalcpu_max");
|
|
37
|
+
amount = parseInt(output.trim(), 10);
|
|
38
|
+
} else if (platform === "win32") throw new Error();
|
|
39
|
+
else {
|
|
40
|
+
const cores = os.cpus().filter(function(cpu, index) {
|
|
41
|
+
const hasHyperthreading = cpu.model.includes("Intel");
|
|
42
|
+
const isOdd = index % 2 === 1;
|
|
43
|
+
return !hasHyperthreading || isOdd;
|
|
44
|
+
});
|
|
45
|
+
amount = cores.length;
|
|
46
|
+
}
|
|
47
|
+
} catch {
|
|
48
|
+
amount = os.cpus().length;
|
|
49
|
+
}
|
|
50
|
+
if (amount === 0) amount = os.cpus().length;
|
|
51
|
+
|
|
52
|
+
//#endregion
|
|
53
|
+
//#region src/runtime/thread-worker.ts
|
|
54
|
+
var ThreadWorker = class {
|
|
55
|
+
name = "ThreadWorker";
|
|
56
|
+
runtime = "worker_threads";
|
|
57
|
+
initialize(options) {
|
|
58
|
+
this.thread = new Worker(fileURLToPath(import.meta.url + "/../entry/worker.js"), options);
|
|
59
|
+
this.threadId = this.thread.threadId;
|
|
60
|
+
}
|
|
61
|
+
async terminate() {
|
|
62
|
+
const output = await this.thread.terminate();
|
|
63
|
+
this.channel?.onClose?.();
|
|
64
|
+
return output;
|
|
65
|
+
}
|
|
66
|
+
postMessage(message, transferListItem) {
|
|
67
|
+
return this.thread.postMessage(message, transferListItem);
|
|
68
|
+
}
|
|
69
|
+
on(event, callback) {
|
|
70
|
+
return this.thread.on(event, callback);
|
|
71
|
+
}
|
|
72
|
+
once(event, callback) {
|
|
73
|
+
return this.thread.once(event, callback);
|
|
74
|
+
}
|
|
75
|
+
emit(event, ...data) {
|
|
76
|
+
return this.thread.emit(event, ...data);
|
|
77
|
+
}
|
|
78
|
+
ref() {
|
|
79
|
+
return this.thread.ref();
|
|
80
|
+
}
|
|
81
|
+
unref() {
|
|
82
|
+
return this.thread.unref();
|
|
83
|
+
}
|
|
84
|
+
setChannel(channel) {
|
|
85
|
+
if (channel.onMessage) throw new Error("{ runtime: 'worker_threads' } doesn't support channel.onMessage. Use transferListItem for listening to messages instead.");
|
|
86
|
+
if (channel.postMessage) throw new Error("{ runtime: 'worker_threads' } doesn't support channel.postMessage. Use transferListItem for sending to messages instead.");
|
|
87
|
+
if (this.channel && this.channel !== channel) this.channel.onClose?.();
|
|
88
|
+
this.channel = channel;
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
//#endregion
|
|
93
|
+
//#region src/runtime/process-worker.ts
|
|
94
|
+
const __tinypool_worker_message__ = true;
|
|
95
|
+
const SIGKILL_TIMEOUT = 1e3;
|
|
96
|
+
var ProcessWorker = class {
|
|
97
|
+
name = "ProcessWorker";
|
|
98
|
+
runtime = "child_process";
|
|
99
|
+
isTerminating = false;
|
|
100
|
+
initialize(options) {
|
|
101
|
+
this.process = fork(fileURLToPath(import.meta.url + "/../entry/process.js"), options.argv, {
|
|
102
|
+
...options,
|
|
103
|
+
stdio: "pipe",
|
|
104
|
+
env: {
|
|
105
|
+
...options.env,
|
|
106
|
+
TINYPOOL_WORKER_ID: options.workerData[0].workerId.toString()
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
process.stdout.setMaxListeners(1 + process.stdout.getMaxListeners());
|
|
110
|
+
process.stderr.setMaxListeners(1 + process.stderr.getMaxListeners());
|
|
111
|
+
this.process.stdout?.pipe(process.stdout);
|
|
112
|
+
this.process.stderr?.pipe(process.stderr);
|
|
113
|
+
this.threadId = this.process.pid;
|
|
114
|
+
this.process.on("exit", this.onUnexpectedExit);
|
|
115
|
+
this.waitForExit = new Promise((r) => this.process.on("exit", r));
|
|
116
|
+
}
|
|
117
|
+
onUnexpectedExit = () => {
|
|
118
|
+
this.process.emit("error", new Error("Worker exited unexpectedly"));
|
|
119
|
+
};
|
|
120
|
+
async terminate() {
|
|
121
|
+
this.isTerminating = true;
|
|
122
|
+
this.process.off("exit", this.onUnexpectedExit);
|
|
123
|
+
const sigkillTimeout = setTimeout(() => this.process.kill("SIGKILL"), SIGKILL_TIMEOUT);
|
|
124
|
+
this.process.kill();
|
|
125
|
+
await this.waitForExit;
|
|
126
|
+
this.process.stdout?.unpipe(process.stdout);
|
|
127
|
+
this.process.stderr?.unpipe(process.stderr);
|
|
128
|
+
this.port?.close();
|
|
129
|
+
this.channel?.onClose?.();
|
|
130
|
+
clearTimeout(sigkillTimeout);
|
|
131
|
+
}
|
|
132
|
+
setChannel(channel) {
|
|
133
|
+
if (this.channel && this.channel !== channel) this.channel.onClose?.();
|
|
134
|
+
this.channel = channel;
|
|
135
|
+
this.channel.onMessage?.((message) => {
|
|
136
|
+
this.send(message);
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
send(message) {
|
|
140
|
+
if (!this.isTerminating) this.process.send(message);
|
|
141
|
+
}
|
|
142
|
+
postMessage(message, transferListItem) {
|
|
143
|
+
transferListItem?.forEach((item) => {
|
|
144
|
+
if (item instanceof MessagePort) {
|
|
145
|
+
this.port = item;
|
|
146
|
+
this.port.start();
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
if (this.port) this.port.on("message", (message$1) => this.send({
|
|
150
|
+
...message$1,
|
|
151
|
+
source: "port",
|
|
152
|
+
__tinypool_worker_message__
|
|
153
|
+
}));
|
|
154
|
+
return this.send({
|
|
155
|
+
...message,
|
|
156
|
+
source: "pool",
|
|
157
|
+
__tinypool_worker_message__
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
on(event, callback) {
|
|
161
|
+
return this.process.on(event, (data) => {
|
|
162
|
+
if (event === "error") return callback(data);
|
|
163
|
+
if (!data || !data.__tinypool_worker_message__) return this.channel?.postMessage?.(data);
|
|
164
|
+
if (data.source === "pool") callback(data);
|
|
165
|
+
else if (data.source === "port") this.port.postMessage(data);
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
once(event, callback) {
|
|
169
|
+
return this.process.once(event, callback);
|
|
170
|
+
}
|
|
171
|
+
emit(event, ...data) {
|
|
172
|
+
return this.process.emit(event, ...data);
|
|
173
|
+
}
|
|
174
|
+
ref() {
|
|
175
|
+
return this.process.ref();
|
|
176
|
+
}
|
|
177
|
+
unref() {
|
|
178
|
+
this.port?.unref();
|
|
179
|
+
this.process.channel?.unref?.();
|
|
180
|
+
if (hasUnref(this.process.stdout)) this.process.stdout.unref();
|
|
181
|
+
if (hasUnref(this.process.stderr)) this.process.stderr.unref();
|
|
182
|
+
return this.process.unref();
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
function hasUnref(stream) {
|
|
186
|
+
return stream != null && "unref" in stream && typeof stream.unref === "function";
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
//#endregion
|
|
190
|
+
//#region src/index.ts
|
|
191
|
+
const cpuCount = amount;
|
|
192
|
+
function onabort(abortSignal, listener) {
|
|
193
|
+
if ("addEventListener" in abortSignal) abortSignal.addEventListener("abort", listener, { once: true });
|
|
194
|
+
else abortSignal.once("abort", listener);
|
|
195
|
+
}
|
|
196
|
+
var AbortError = class extends Error {
|
|
197
|
+
constructor() {
|
|
198
|
+
super("The task has been aborted");
|
|
199
|
+
}
|
|
200
|
+
get name() {
|
|
201
|
+
return "AbortError";
|
|
202
|
+
}
|
|
203
|
+
};
|
|
204
|
+
var CancelError = class extends Error {
|
|
205
|
+
constructor() {
|
|
206
|
+
super("The task has been cancelled");
|
|
207
|
+
}
|
|
208
|
+
get name() {
|
|
209
|
+
return "CancelError";
|
|
210
|
+
}
|
|
211
|
+
};
|
|
212
|
+
var ArrayTaskQueue = class {
|
|
213
|
+
tasks = [];
|
|
214
|
+
get size() {
|
|
215
|
+
return this.tasks.length;
|
|
216
|
+
}
|
|
217
|
+
shift() {
|
|
218
|
+
return this.tasks.shift();
|
|
219
|
+
}
|
|
220
|
+
push(task) {
|
|
221
|
+
this.tasks.push(task);
|
|
222
|
+
}
|
|
223
|
+
remove(task) {
|
|
224
|
+
const index = this.tasks.indexOf(task);
|
|
225
|
+
assert.notStrictEqual(index, -1);
|
|
226
|
+
this.tasks.splice(index, 1);
|
|
227
|
+
}
|
|
228
|
+
cancel() {
|
|
229
|
+
while (this.tasks.length > 0) {
|
|
230
|
+
const task = this.tasks.pop();
|
|
231
|
+
task?.cancel();
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
};
|
|
235
|
+
const kDefaultOptions = {
|
|
236
|
+
filename: null,
|
|
237
|
+
name: "default",
|
|
238
|
+
runtime: "worker_threads",
|
|
239
|
+
minThreads: Math.max(cpuCount / 2, 1),
|
|
240
|
+
maxThreads: cpuCount,
|
|
241
|
+
idleTimeout: 0,
|
|
242
|
+
maxQueue: Infinity,
|
|
243
|
+
concurrentTasksPerWorker: 1,
|
|
244
|
+
useAtomics: true,
|
|
245
|
+
taskQueue: new ArrayTaskQueue(),
|
|
246
|
+
trackUnmanagedFds: true
|
|
247
|
+
};
|
|
248
|
+
const kDefaultRunOptions = {
|
|
249
|
+
transferList: void 0,
|
|
250
|
+
filename: null,
|
|
251
|
+
signal: null,
|
|
252
|
+
name: null
|
|
253
|
+
};
|
|
254
|
+
var DirectlyTransferable = class {
|
|
255
|
+
#value;
|
|
256
|
+
constructor(value) {
|
|
257
|
+
this.#value = value;
|
|
258
|
+
}
|
|
259
|
+
get [kTransferable]() {
|
|
260
|
+
return this.#value;
|
|
261
|
+
}
|
|
262
|
+
get [kValue]() {
|
|
263
|
+
return this.#value;
|
|
264
|
+
}
|
|
265
|
+
};
|
|
266
|
+
var ArrayBufferViewTransferable = class {
|
|
267
|
+
#view;
|
|
268
|
+
constructor(view) {
|
|
269
|
+
this.#view = view;
|
|
270
|
+
}
|
|
271
|
+
get [kTransferable]() {
|
|
272
|
+
return this.#view.buffer;
|
|
273
|
+
}
|
|
274
|
+
get [kValue]() {
|
|
275
|
+
return this.#view;
|
|
276
|
+
}
|
|
277
|
+
};
|
|
278
|
+
let taskIdCounter = 0;
|
|
279
|
+
function maybeFileURLToPath(filename) {
|
|
280
|
+
return filename.startsWith("file:") ? fileURLToPath(new URL(filename)) : filename;
|
|
281
|
+
}
|
|
282
|
+
var TaskInfo = class extends AsyncResource {
|
|
283
|
+
abortListener = null;
|
|
284
|
+
workerInfo = null;
|
|
285
|
+
constructor(task, transferList, filename, name, callback, abortSignal, triggerAsyncId, channel) {
|
|
286
|
+
super("Tinypool.Task", {
|
|
287
|
+
requireManualDestroy: true,
|
|
288
|
+
triggerAsyncId
|
|
289
|
+
});
|
|
290
|
+
this.callback = callback;
|
|
291
|
+
this.task = task;
|
|
292
|
+
this.transferList = transferList;
|
|
293
|
+
this.cancel = () => this.callback(new CancelError(), null);
|
|
294
|
+
this.channel = channel;
|
|
295
|
+
if (isMovable(task)) {
|
|
296
|
+
/* istanbul ignore if */
|
|
297
|
+
if (this.transferList == null) this.transferList = [];
|
|
298
|
+
this.transferList = this.transferList.concat(task[kTransferable]);
|
|
299
|
+
this.task = task[kValue];
|
|
300
|
+
}
|
|
301
|
+
this.filename = filename;
|
|
302
|
+
this.name = name;
|
|
303
|
+
this.taskId = taskIdCounter++;
|
|
304
|
+
this.abortSignal = abortSignal;
|
|
305
|
+
this.created = performance.now();
|
|
306
|
+
this.started = 0;
|
|
307
|
+
}
|
|
308
|
+
releaseTask() {
|
|
309
|
+
const ret = this.task;
|
|
310
|
+
this.task = null;
|
|
311
|
+
return ret;
|
|
312
|
+
}
|
|
313
|
+
done(err, result) {
|
|
314
|
+
this.emitDestroy();
|
|
315
|
+
this.runInAsyncScope(this.callback, null, err, result);
|
|
316
|
+
if (this.abortSignal && this.abortListener) if ("removeEventListener" in this.abortSignal && this.abortListener) this.abortSignal.removeEventListener("abort", this.abortListener);
|
|
317
|
+
else this.abortSignal.off("abort", this.abortListener);
|
|
318
|
+
}
|
|
319
|
+
get [kQueueOptions]() {
|
|
320
|
+
return kQueueOptions in this.task ? this.task[kQueueOptions] : null;
|
|
321
|
+
}
|
|
322
|
+
};
|
|
323
|
+
var AsynchronouslyCreatedResource = class {
|
|
324
|
+
onreadyListeners = [];
|
|
325
|
+
markAsReady() {
|
|
326
|
+
const listeners = this.onreadyListeners;
|
|
327
|
+
assert(listeners !== null);
|
|
328
|
+
this.onreadyListeners = null;
|
|
329
|
+
for (const listener of listeners) listener();
|
|
330
|
+
}
|
|
331
|
+
isReady() {
|
|
332
|
+
return this.onreadyListeners === null;
|
|
333
|
+
}
|
|
334
|
+
onReady(fn) {
|
|
335
|
+
if (this.onreadyListeners === null) {
|
|
336
|
+
fn();
|
|
337
|
+
return;
|
|
338
|
+
}
|
|
339
|
+
this.onreadyListeners.push(fn);
|
|
340
|
+
}
|
|
341
|
+
};
|
|
342
|
+
var AsynchronouslyCreatedResourcePool = class {
|
|
343
|
+
pendingItems = new Set();
|
|
344
|
+
readyItems = new Set();
|
|
345
|
+
constructor(maximumUsage) {
|
|
346
|
+
this.maximumUsage = maximumUsage;
|
|
347
|
+
this.onAvailableListeners = [];
|
|
348
|
+
}
|
|
349
|
+
add(item) {
|
|
350
|
+
this.pendingItems.add(item);
|
|
351
|
+
item.onReady(() => {
|
|
352
|
+
/* istanbul ignore else */
|
|
353
|
+
if (this.pendingItems.has(item)) {
|
|
354
|
+
this.pendingItems.delete(item);
|
|
355
|
+
this.readyItems.add(item);
|
|
356
|
+
this.maybeAvailable(item);
|
|
357
|
+
}
|
|
358
|
+
});
|
|
359
|
+
}
|
|
360
|
+
delete(item) {
|
|
361
|
+
this.pendingItems.delete(item);
|
|
362
|
+
this.readyItems.delete(item);
|
|
363
|
+
}
|
|
364
|
+
findAvailable() {
|
|
365
|
+
let minUsage = this.maximumUsage;
|
|
366
|
+
let candidate = null;
|
|
367
|
+
for (const item of this.readyItems) {
|
|
368
|
+
const usage = item.currentUsage();
|
|
369
|
+
if (usage === 0) return item;
|
|
370
|
+
if (usage < minUsage) {
|
|
371
|
+
candidate = item;
|
|
372
|
+
minUsage = usage;
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
return candidate;
|
|
376
|
+
}
|
|
377
|
+
*[Symbol.iterator]() {
|
|
378
|
+
yield* this.pendingItems;
|
|
379
|
+
yield* this.readyItems;
|
|
380
|
+
}
|
|
381
|
+
get size() {
|
|
382
|
+
return this.pendingItems.size + this.readyItems.size;
|
|
383
|
+
}
|
|
384
|
+
maybeAvailable(item) {
|
|
385
|
+
/* istanbul ignore else */
|
|
386
|
+
if (item.currentUsage() < this.maximumUsage) for (const listener of this.onAvailableListeners) listener(item);
|
|
387
|
+
}
|
|
388
|
+
onAvailable(fn) {
|
|
389
|
+
this.onAvailableListeners.push(fn);
|
|
390
|
+
}
|
|
391
|
+
};
|
|
392
|
+
const Errors = {
|
|
393
|
+
ThreadTermination: () => new Error("Terminating worker thread"),
|
|
394
|
+
FilenameNotProvided: () => new Error("filename must be provided to run() or in options object"),
|
|
395
|
+
TaskQueueAtLimit: () => new Error("Task queue is at limit"),
|
|
396
|
+
NoTaskQueueAvailable: () => new Error("No task queue available and all Workers are busy")
|
|
397
|
+
};
|
|
398
|
+
var WorkerInfo = class extends AsynchronouslyCreatedResource {
|
|
399
|
+
idleTimeout = null;
|
|
400
|
+
lastSeenResponseCount = 0;
|
|
401
|
+
constructor(worker, port, workerId, freeWorkerId, onMessage, filename, teardown) {
|
|
402
|
+
super();
|
|
403
|
+
this.worker = worker;
|
|
404
|
+
this.workerId = workerId;
|
|
405
|
+
this.freeWorkerId = freeWorkerId;
|
|
406
|
+
this.teardown = teardown;
|
|
407
|
+
this.filename = filename;
|
|
408
|
+
this.port = port;
|
|
409
|
+
this.port.on("message", (message) => this._handleResponse(message));
|
|
410
|
+
this.onMessage = onMessage;
|
|
411
|
+
this.taskInfos = new Map();
|
|
412
|
+
this.sharedBuffer = new Int32Array(new SharedArrayBuffer(kFieldCount * Int32Array.BYTES_PER_ELEMENT));
|
|
413
|
+
}
|
|
414
|
+
async destroy(timeout) {
|
|
415
|
+
let resolve;
|
|
416
|
+
let reject;
|
|
417
|
+
const ret = new Promise((res, rej) => {
|
|
418
|
+
resolve = res;
|
|
419
|
+
reject = rej;
|
|
420
|
+
});
|
|
421
|
+
if (this.teardown && this.filename) {
|
|
422
|
+
const { teardown, filename } = this;
|
|
423
|
+
await new Promise((resolve$1, reject$1) => {
|
|
424
|
+
this.postTask(new TaskInfo({}, [], filename, teardown, (error, result) => error ? reject$1(error) : resolve$1(result), null, 1, void 0));
|
|
425
|
+
});
|
|
426
|
+
}
|
|
427
|
+
const timer = timeout ? setTimeout(() => reject(new Error("Failed to terminate worker")), timeout) : null;
|
|
428
|
+
this.worker.terminate().then(() => {
|
|
429
|
+
if (timer !== null) clearTimeout(timer);
|
|
430
|
+
this.port.close();
|
|
431
|
+
this.clearIdleTimeout();
|
|
432
|
+
for (const taskInfo of this.taskInfos.values()) taskInfo.done(Errors.ThreadTermination());
|
|
433
|
+
this.taskInfos.clear();
|
|
434
|
+
resolve();
|
|
435
|
+
});
|
|
436
|
+
return ret;
|
|
437
|
+
}
|
|
438
|
+
clearIdleTimeout() {
|
|
439
|
+
if (this.idleTimeout !== null) {
|
|
440
|
+
clearTimeout(this.idleTimeout);
|
|
441
|
+
this.idleTimeout = null;
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
ref() {
|
|
445
|
+
this.port.ref();
|
|
446
|
+
return this;
|
|
447
|
+
}
|
|
448
|
+
unref() {
|
|
449
|
+
this.port.unref();
|
|
450
|
+
return this;
|
|
451
|
+
}
|
|
452
|
+
_handleResponse(message) {
|
|
453
|
+
this.usedMemory = message.usedMemory;
|
|
454
|
+
this.onMessage(message);
|
|
455
|
+
if (this.taskInfos.size === 0) this.unref();
|
|
456
|
+
}
|
|
457
|
+
postTask(taskInfo) {
|
|
458
|
+
assert(!this.taskInfos.has(taskInfo.taskId));
|
|
459
|
+
const message = {
|
|
460
|
+
task: taskInfo.releaseTask(),
|
|
461
|
+
taskId: taskInfo.taskId,
|
|
462
|
+
filename: taskInfo.filename,
|
|
463
|
+
name: taskInfo.name
|
|
464
|
+
};
|
|
465
|
+
try {
|
|
466
|
+
if (taskInfo.channel) this.worker.setChannel?.(taskInfo.channel);
|
|
467
|
+
this.port.postMessage(message, taskInfo.transferList);
|
|
468
|
+
} catch (err) {
|
|
469
|
+
taskInfo.done(err);
|
|
470
|
+
return;
|
|
471
|
+
}
|
|
472
|
+
taskInfo.workerInfo = this;
|
|
473
|
+
this.taskInfos.set(taskInfo.taskId, taskInfo);
|
|
474
|
+
this.ref();
|
|
475
|
+
this.clearIdleTimeout();
|
|
476
|
+
Atomics.add(this.sharedBuffer, kRequestCountField, 1);
|
|
477
|
+
Atomics.notify(this.sharedBuffer, kRequestCountField, 1);
|
|
478
|
+
}
|
|
479
|
+
processPendingMessages() {
|
|
480
|
+
const actualResponseCount = Atomics.load(this.sharedBuffer, kResponseCountField);
|
|
481
|
+
if (actualResponseCount !== this.lastSeenResponseCount) {
|
|
482
|
+
this.lastSeenResponseCount = actualResponseCount;
|
|
483
|
+
let entry;
|
|
484
|
+
while ((entry = receiveMessageOnPort(this.port)) !== void 0) this._handleResponse(entry.message);
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
isRunningAbortableTask() {
|
|
488
|
+
if (this.taskInfos.size !== 1) return false;
|
|
489
|
+
const [first] = this.taskInfos;
|
|
490
|
+
const [, task] = first || [];
|
|
491
|
+
return task?.abortSignal !== null;
|
|
492
|
+
}
|
|
493
|
+
currentUsage() {
|
|
494
|
+
if (this.isRunningAbortableTask()) return Infinity;
|
|
495
|
+
return this.taskInfos.size;
|
|
496
|
+
}
|
|
497
|
+
};
|
|
498
|
+
var ThreadPool = class {
|
|
499
|
+
skipQueue = [];
|
|
500
|
+
completed = 0;
|
|
501
|
+
start = performance.now();
|
|
502
|
+
inProcessPendingMessages = false;
|
|
503
|
+
startingUp = false;
|
|
504
|
+
workerFailsDuringBootstrap = false;
|
|
505
|
+
constructor(publicInterface, options) {
|
|
506
|
+
this.publicInterface = publicInterface;
|
|
507
|
+
this.taskQueue = options.taskQueue || new ArrayTaskQueue();
|
|
508
|
+
const filename = options.filename ? maybeFileURLToPath(options.filename) : null;
|
|
509
|
+
this.options = {
|
|
510
|
+
...kDefaultOptions,
|
|
511
|
+
...options,
|
|
512
|
+
filename,
|
|
513
|
+
maxQueue: 0
|
|
514
|
+
};
|
|
515
|
+
if (options.maxThreads !== void 0 && this.options.minThreads >= options.maxThreads) this.options.minThreads = options.maxThreads;
|
|
516
|
+
if (options.minThreads !== void 0 && this.options.maxThreads <= options.minThreads) this.options.maxThreads = options.minThreads;
|
|
517
|
+
if (options.maxQueue === "auto") this.options.maxQueue = this.options.maxThreads ** 2;
|
|
518
|
+
else this.options.maxQueue = options.maxQueue ?? kDefaultOptions.maxQueue;
|
|
519
|
+
this.workerIds = new Map(new Array(this.options.maxThreads).fill(0).map((_, i) => [i + 1, true]));
|
|
520
|
+
this.workers = new AsynchronouslyCreatedResourcePool(this.options.concurrentTasksPerWorker);
|
|
521
|
+
this.workers.onAvailable((w) => this._onWorkerAvailable(w));
|
|
522
|
+
this.startingUp = true;
|
|
523
|
+
this._ensureMinimumWorkers();
|
|
524
|
+
this.startingUp = false;
|
|
525
|
+
}
|
|
526
|
+
_ensureEnoughWorkersForTaskQueue() {
|
|
527
|
+
while (this.workers.size < this.taskQueue.size && this.workers.size < this.options.maxThreads) this._addNewWorker();
|
|
528
|
+
}
|
|
529
|
+
_ensureMaximumWorkers() {
|
|
530
|
+
while (this.workers.size < this.options.maxThreads) this._addNewWorker();
|
|
531
|
+
}
|
|
532
|
+
_ensureMinimumWorkers() {
|
|
533
|
+
while (this.workers.size < this.options.minThreads) this._addNewWorker();
|
|
534
|
+
}
|
|
535
|
+
_addNewWorker() {
|
|
536
|
+
const workerIds = this.workerIds;
|
|
537
|
+
let workerId;
|
|
538
|
+
workerIds.forEach((isIdAvailable, _workerId$1) => {
|
|
539
|
+
if (isIdAvailable && !workerId) {
|
|
540
|
+
workerId = _workerId$1;
|
|
541
|
+
workerIds.set(_workerId$1, false);
|
|
542
|
+
}
|
|
543
|
+
});
|
|
544
|
+
const tinypoolPrivateData = { workerId };
|
|
545
|
+
const worker = this.options.runtime === "child_process" ? new ProcessWorker() : new ThreadWorker();
|
|
546
|
+
worker.initialize({
|
|
547
|
+
env: this.options.env,
|
|
548
|
+
argv: this.options.argv,
|
|
549
|
+
execArgv: this.options.execArgv,
|
|
550
|
+
resourceLimits: this.options.resourceLimits,
|
|
551
|
+
workerData: [tinypoolPrivateData, this.options.workerData],
|
|
552
|
+
trackUnmanagedFds: this.options.trackUnmanagedFds
|
|
553
|
+
});
|
|
554
|
+
const onMessage = (message$1) => {
|
|
555
|
+
const { taskId, result } = message$1;
|
|
556
|
+
const taskInfo = workerInfo.taskInfos.get(taskId);
|
|
557
|
+
workerInfo.taskInfos.delete(taskId);
|
|
558
|
+
if (!this.shouldRecycleWorker(taskInfo)) this.workers.maybeAvailable(workerInfo);
|
|
559
|
+
/* istanbul ignore if */
|
|
560
|
+
if (taskInfo === void 0) {
|
|
561
|
+
const err = new Error(`Unexpected message from Worker: ${inspect(message$1)}`);
|
|
562
|
+
this.publicInterface.emit("error", err);
|
|
563
|
+
} else taskInfo.done(message$1.error, result);
|
|
564
|
+
this._processPendingMessages();
|
|
565
|
+
};
|
|
566
|
+
const { port1, port2 } = new MessageChannel();
|
|
567
|
+
const workerInfo = new WorkerInfo(worker, port1, workerId, () => workerIds.set(workerId, true), onMessage, this.options.filename, this.options.teardown);
|
|
568
|
+
if (this.startingUp) workerInfo.markAsReady();
|
|
569
|
+
const message = {
|
|
570
|
+
filename: this.options.filename,
|
|
571
|
+
name: this.options.name,
|
|
572
|
+
port: port2,
|
|
573
|
+
sharedBuffer: workerInfo.sharedBuffer,
|
|
574
|
+
useAtomics: this.options.useAtomics
|
|
575
|
+
};
|
|
576
|
+
worker.postMessage(message, [port2]);
|
|
577
|
+
worker.on("message", (message$1) => {
|
|
578
|
+
if (message$1.ready === true) {
|
|
579
|
+
port1.start();
|
|
580
|
+
if (workerInfo.currentUsage() === 0) workerInfo.unref();
|
|
581
|
+
if (!workerInfo.isReady()) workerInfo.markAsReady();
|
|
582
|
+
return;
|
|
583
|
+
}
|
|
584
|
+
worker.emit("error", new Error(`Unexpected message on Worker: ${inspect(message$1)}`));
|
|
585
|
+
});
|
|
586
|
+
worker.on("error", (err) => {
|
|
587
|
+
worker.ref = () => {};
|
|
588
|
+
const taskInfos = [...workerInfo.taskInfos.values()];
|
|
589
|
+
workerInfo.taskInfos.clear();
|
|
590
|
+
this._removeWorker(workerInfo);
|
|
591
|
+
if (workerInfo.isReady() && !this.workerFailsDuringBootstrap) this._ensureMinimumWorkers();
|
|
592
|
+
else this.workerFailsDuringBootstrap = true;
|
|
593
|
+
if (taskInfos.length > 0) for (const taskInfo of taskInfos) taskInfo.done(err, null);
|
|
594
|
+
else this.publicInterface.emit("error", err);
|
|
595
|
+
});
|
|
596
|
+
worker.unref();
|
|
597
|
+
port1.on("close", () => {
|
|
598
|
+
worker.ref();
|
|
599
|
+
});
|
|
600
|
+
this.workers.add(workerInfo);
|
|
601
|
+
}
|
|
602
|
+
_processPendingMessages() {
|
|
603
|
+
if (this.inProcessPendingMessages || !this.options.useAtomics) return;
|
|
604
|
+
this.inProcessPendingMessages = true;
|
|
605
|
+
try {
|
|
606
|
+
for (const workerInfo of this.workers) workerInfo.processPendingMessages();
|
|
607
|
+
} finally {
|
|
608
|
+
this.inProcessPendingMessages = false;
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
_removeWorker(workerInfo) {
|
|
612
|
+
workerInfo.freeWorkerId();
|
|
613
|
+
this.workers.delete(workerInfo);
|
|
614
|
+
return workerInfo.destroy(this.options.terminateTimeout);
|
|
615
|
+
}
|
|
616
|
+
_onWorkerAvailable(workerInfo) {
|
|
617
|
+
while ((this.taskQueue.size > 0 || this.skipQueue.length > 0) && workerInfo.currentUsage() < this.options.concurrentTasksPerWorker) {
|
|
618
|
+
const taskInfo = this.skipQueue.shift() || this.taskQueue.shift();
|
|
619
|
+
if (taskInfo.abortSignal && workerInfo.taskInfos.size > 0) {
|
|
620
|
+
this.skipQueue.push(taskInfo);
|
|
621
|
+
break;
|
|
622
|
+
}
|
|
623
|
+
const now = performance.now();
|
|
624
|
+
taskInfo.started = now;
|
|
625
|
+
workerInfo.postTask(taskInfo);
|
|
626
|
+
this._maybeDrain();
|
|
627
|
+
return;
|
|
628
|
+
}
|
|
629
|
+
if (workerInfo.taskInfos.size === 0 && this.workers.size > this.options.minThreads) workerInfo.idleTimeout = setTimeout(() => {
|
|
630
|
+
assert.strictEqual(workerInfo.taskInfos.size, 0);
|
|
631
|
+
if (this.workers.size > this.options.minThreads) this._removeWorker(workerInfo);
|
|
632
|
+
}, this.options.idleTimeout).unref();
|
|
633
|
+
}
|
|
634
|
+
runTask(task, options) {
|
|
635
|
+
let { filename, name } = options;
|
|
636
|
+
const { transferList = [], signal = null, channel } = options;
|
|
637
|
+
if (filename == null) filename = this.options.filename;
|
|
638
|
+
if (name == null) name = this.options.name;
|
|
639
|
+
if (typeof filename !== "string") return Promise.reject(Errors.FilenameNotProvided());
|
|
640
|
+
filename = maybeFileURLToPath(filename);
|
|
641
|
+
let resolve;
|
|
642
|
+
let reject;
|
|
643
|
+
const ret = new Promise((res, rej) => {
|
|
644
|
+
resolve = res;
|
|
645
|
+
reject = rej;
|
|
646
|
+
});
|
|
647
|
+
const taskInfo = new TaskInfo(task, transferList, filename, name, (err, result) => {
|
|
648
|
+
this.completed++;
|
|
649
|
+
if (err !== null) reject(err);
|
|
650
|
+
if (this.shouldRecycleWorker(taskInfo)) this._removeWorker(taskInfo.workerInfo).then(() => this._ensureMinimumWorkers()).then(() => this._ensureEnoughWorkersForTaskQueue()).then(() => resolve(result)).catch(reject);
|
|
651
|
+
else resolve(result);
|
|
652
|
+
}, signal, this.publicInterface.asyncResource.asyncId(), channel);
|
|
653
|
+
if (signal !== null) {
|
|
654
|
+
if (signal.aborted) return Promise.reject(new AbortError());
|
|
655
|
+
taskInfo.abortListener = () => {
|
|
656
|
+
reject(new AbortError());
|
|
657
|
+
if (taskInfo.workerInfo !== null) {
|
|
658
|
+
this._removeWorker(taskInfo.workerInfo);
|
|
659
|
+
this._ensureMinimumWorkers();
|
|
660
|
+
} else this.taskQueue.remove(taskInfo);
|
|
661
|
+
};
|
|
662
|
+
onabort(signal, taskInfo.abortListener);
|
|
663
|
+
}
|
|
664
|
+
if (this.taskQueue.size > 0) {
|
|
665
|
+
const totalCapacity = this.options.maxQueue + this.pendingCapacity();
|
|
666
|
+
if (this.taskQueue.size >= totalCapacity) if (this.options.maxQueue === 0) return Promise.reject(Errors.NoTaskQueueAvailable());
|
|
667
|
+
else return Promise.reject(Errors.TaskQueueAtLimit());
|
|
668
|
+
else {
|
|
669
|
+
if (this.workers.size < this.options.maxThreads) this._addNewWorker();
|
|
670
|
+
this.taskQueue.push(taskInfo);
|
|
671
|
+
}
|
|
672
|
+
return ret;
|
|
673
|
+
}
|
|
674
|
+
let workerInfo = this.workers.findAvailable();
|
|
675
|
+
if (workerInfo !== null && workerInfo.currentUsage() > 0 && signal) workerInfo = null;
|
|
676
|
+
let waitingForNewWorker = false;
|
|
677
|
+
if ((workerInfo === null || workerInfo.currentUsage() > 0) && this.workers.size < this.options.maxThreads) {
|
|
678
|
+
this._addNewWorker();
|
|
679
|
+
waitingForNewWorker = true;
|
|
680
|
+
}
|
|
681
|
+
if (workerInfo === null) {
|
|
682
|
+
if (this.options.maxQueue <= 0 && !waitingForNewWorker) return Promise.reject(Errors.NoTaskQueueAvailable());
|
|
683
|
+
else this.taskQueue.push(taskInfo);
|
|
684
|
+
return ret;
|
|
685
|
+
}
|
|
686
|
+
const now = performance.now();
|
|
687
|
+
taskInfo.started = now;
|
|
688
|
+
workerInfo.postTask(taskInfo);
|
|
689
|
+
this._maybeDrain();
|
|
690
|
+
return ret;
|
|
691
|
+
}
|
|
692
|
+
shouldRecycleWorker(taskInfo) {
|
|
693
|
+
if (taskInfo?.workerInfo?.shouldRecycle) return true;
|
|
694
|
+
if (this.options.isolateWorkers && taskInfo?.workerInfo) return true;
|
|
695
|
+
if (!this.options.isolateWorkers && this.options.maxMemoryLimitBeforeRecycle !== void 0 && (taskInfo?.workerInfo?.usedMemory || 0) > this.options.maxMemoryLimitBeforeRecycle) return true;
|
|
696
|
+
return false;
|
|
697
|
+
}
|
|
698
|
+
pendingCapacity() {
|
|
699
|
+
return this.workers.pendingItems.size * this.options.concurrentTasksPerWorker;
|
|
700
|
+
}
|
|
701
|
+
_maybeDrain() {
|
|
702
|
+
if (this.taskQueue.size === 0 && this.skipQueue.length === 0) this.publicInterface.emit("drain");
|
|
703
|
+
}
|
|
704
|
+
async destroy() {
|
|
705
|
+
while (this.skipQueue.length > 0) {
|
|
706
|
+
const taskInfo = this.skipQueue.shift();
|
|
707
|
+
taskInfo.done(new Error("Terminating worker thread"));
|
|
708
|
+
}
|
|
709
|
+
while (this.taskQueue.size > 0) {
|
|
710
|
+
const taskInfo = this.taskQueue.shift();
|
|
711
|
+
taskInfo.done(new Error("Terminating worker thread"));
|
|
712
|
+
}
|
|
713
|
+
const exitEvents = [];
|
|
714
|
+
while (this.workers.size > 0) {
|
|
715
|
+
const [workerInfo] = this.workers;
|
|
716
|
+
exitEvents.push(once(workerInfo.worker, "exit"));
|
|
717
|
+
this._removeWorker(workerInfo);
|
|
718
|
+
}
|
|
719
|
+
await Promise.all(exitEvents);
|
|
720
|
+
}
|
|
721
|
+
async recycleWorkers(options = {}) {
|
|
722
|
+
const runtimeChanged = options?.runtime && options.runtime !== this.options.runtime;
|
|
723
|
+
if (options?.runtime) this.options.runtime = options.runtime;
|
|
724
|
+
if (this.options.isolateWorkers && !runtimeChanged) return;
|
|
725
|
+
const exitEvents = [];
|
|
726
|
+
Array.from(this.workers).filter((workerInfo) => {
|
|
727
|
+
if (workerInfo.currentUsage() === 0) {
|
|
728
|
+
exitEvents.push(once(workerInfo.worker, "exit"));
|
|
729
|
+
this._removeWorker(workerInfo);
|
|
730
|
+
} else workerInfo.shouldRecycle = true;
|
|
731
|
+
});
|
|
732
|
+
await Promise.all(exitEvents);
|
|
733
|
+
this._ensureMinimumWorkers();
|
|
734
|
+
}
|
|
735
|
+
};
|
|
736
|
+
var Tinypool = class extends EventEmitterAsyncResource {
|
|
737
|
+
#pool;
|
|
738
|
+
constructor(options = {}) {
|
|
739
|
+
if (options.minThreads !== void 0 && options.minThreads > 0 && options.minThreads < 1) options.minThreads = Math.max(1, Math.floor(options.minThreads * cpuCount));
|
|
740
|
+
if (options.maxThreads !== void 0 && options.maxThreads > 0 && options.maxThreads < 1) options.maxThreads = Math.max(1, Math.floor(options.maxThreads * cpuCount));
|
|
741
|
+
super({
|
|
742
|
+
...options,
|
|
743
|
+
name: "Tinypool"
|
|
744
|
+
});
|
|
745
|
+
if (options.minThreads !== void 0 && options.maxThreads !== void 0 && options.minThreads > options.maxThreads) throw new RangeError("options.minThreads and options.maxThreads must not conflict");
|
|
746
|
+
this.#pool = new ThreadPool(this, options);
|
|
747
|
+
}
|
|
748
|
+
run(task, options = kDefaultRunOptions) {
|
|
749
|
+
const { transferList, filename, name, signal, runtime, channel } = options;
|
|
750
|
+
return this.#pool.runTask(task, {
|
|
751
|
+
transferList,
|
|
752
|
+
filename,
|
|
753
|
+
name,
|
|
754
|
+
signal,
|
|
755
|
+
runtime,
|
|
756
|
+
channel
|
|
757
|
+
});
|
|
758
|
+
}
|
|
759
|
+
async destroy() {
|
|
760
|
+
await this.#pool.destroy();
|
|
761
|
+
this.emitDestroy();
|
|
762
|
+
}
|
|
763
|
+
get options() {
|
|
764
|
+
return this.#pool.options;
|
|
765
|
+
}
|
|
766
|
+
get threads() {
|
|
767
|
+
const ret = [];
|
|
768
|
+
for (const workerInfo of this.#pool.workers) ret.push(workerInfo.worker);
|
|
769
|
+
return ret;
|
|
770
|
+
}
|
|
771
|
+
get queueSize() {
|
|
772
|
+
const pool = this.#pool;
|
|
773
|
+
return Math.max(pool.taskQueue.size - pool.pendingCapacity(), 0);
|
|
774
|
+
}
|
|
775
|
+
cancelPendingTasks() {
|
|
776
|
+
const pool = this.#pool;
|
|
777
|
+
pool.taskQueue.cancel();
|
|
778
|
+
}
|
|
779
|
+
async recycleWorkers(options = {}) {
|
|
780
|
+
await this.#pool.recycleWorkers(options);
|
|
781
|
+
}
|
|
782
|
+
get completed() {
|
|
783
|
+
return this.#pool.completed;
|
|
784
|
+
}
|
|
785
|
+
get duration() {
|
|
786
|
+
return performance.now() - this.#pool.start;
|
|
787
|
+
}
|
|
788
|
+
static get isWorkerThread() {
|
|
789
|
+
return process.__tinypool_state__?.isWorkerThread || false;
|
|
790
|
+
}
|
|
791
|
+
static get workerData() {
|
|
792
|
+
return process.__tinypool_state__?.workerData || void 0;
|
|
793
|
+
}
|
|
794
|
+
static get version() {
|
|
795
|
+
const { version } = JSON.parse(readFileSync(join(__dirname, "../package.json"), "utf-8"));
|
|
796
|
+
return version;
|
|
797
|
+
}
|
|
798
|
+
static move(val) {
|
|
799
|
+
if (val != null && typeof val === "object" && typeof val !== "function") {
|
|
800
|
+
if (!isTransferable(val)) if (types.isArrayBufferView(val)) val = new ArrayBufferViewTransferable(val);
|
|
801
|
+
else val = new DirectlyTransferable(val);
|
|
802
|
+
markMovable(val);
|
|
803
|
+
}
|
|
804
|
+
return val;
|
|
805
|
+
}
|
|
806
|
+
static get transferableSymbol() {
|
|
807
|
+
return kTransferable;
|
|
808
|
+
}
|
|
809
|
+
static get valueSymbol() {
|
|
810
|
+
return kValue;
|
|
811
|
+
}
|
|
812
|
+
static get queueOptionsSymbol() {
|
|
813
|
+
return kQueueOptions;
|
|
814
|
+
}
|
|
815
|
+
};
|
|
816
|
+
const _workerId = process.__tinypool_state__?.workerId;
|
|
817
|
+
var src_default = Tinypool;
|
|
818
|
+
|
|
819
|
+
//#endregion
|
|
820
|
+
export { Tinypool, src_default as default, isMovable, isTaskQueue, isTransferable, kFieldCount, kQueueOptions, kRequestCountField, kResponseCountField, kTransferable, kValue, markMovable, _workerId as workerId };
|