knitting 0.1.55 → 0.1.56
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/knitting.d.ts +6 -797
- package/knitting.js +6 -9780
- package/map.md +12 -8
- package/package.json +14 -6
- package/process-shared-buffer.d.ts +1 -0
- package/process-shared-buffer.js +1 -0
- package/shared-memory.d.ts +1 -155
- package/shared-memory.js +1 -1342
- package/src/api.d.ts +69 -0
- package/src/api.js +619 -0
- package/src/common/envelope.d.ts +17 -0
- package/src/common/envelope.js +22 -0
- package/src/common/module-url.d.ts +1 -0
- package/src/common/module-url.js +24 -0
- package/src/common/node-compat.d.ts +20 -0
- package/src/common/node-compat.js +24 -0
- package/src/common/path-canonical.d.ts +6 -0
- package/src/common/path-canonical.js +41 -0
- package/src/common/runtime.d.ts +15 -0
- package/src/common/runtime.js +91 -0
- package/src/common/shared-buffer-region.d.ts +11 -0
- package/src/common/shared-buffer-region.js +21 -0
- package/src/common/shared-buffer-text.d.ts +16 -0
- package/src/common/shared-buffer-text.js +65 -0
- package/src/common/task-source.d.ts +3 -0
- package/src/common/task-source.js +84 -0
- package/src/common/task-symbol.d.ts +1 -0
- package/src/common/task-symbol.js +1 -0
- package/src/common/with-resolvers.d.ts +9 -0
- package/src/common/with-resolvers.js +23 -0
- package/src/common/worker-runtime.d.ts +42 -0
- package/src/common/worker-runtime.js +61 -0
- package/src/connections/buffer-reference-native.d.ts +56 -0
- package/src/connections/buffer-reference-native.js +217 -0
- package/src/connections/buffer-reference.d.ts +78 -0
- package/src/connections/buffer-reference.js +461 -0
- package/src/connections/bun.d.ts +22 -0
- package/src/connections/bun.js +214 -0
- package/src/connections/deno.d.ts +22 -0
- package/src/connections/deno.js +205 -0
- package/src/connections/file-descriptor.d.ts +39 -0
- package/src/connections/file-descriptor.js +161 -0
- package/src/connections/index.d.ts +4 -0
- package/src/connections/index.js +4 -0
- package/src/connections/node-addons.d.ts +5 -0
- package/src/connections/node-addons.js +44 -0
- package/src/connections/node-buffer-pointer.d.ts +20 -0
- package/src/connections/node-buffer-pointer.js +16 -0
- package/src/connections/node.d.ts +30 -0
- package/src/connections/node.js +62 -0
- package/src/connections/numeric-array.d.ts +5 -0
- package/src/connections/numeric-array.js +17 -0
- package/src/connections/package-assets.d.ts +2 -0
- package/src/connections/package-assets.js +45 -0
- package/src/connections/posix.d.ts +32 -0
- package/src/connections/posix.js +77 -0
- package/src/connections/process-shared-buffer.d.ts +75 -0
- package/src/connections/process-shared-buffer.js +279 -0
- package/src/connections/shared-array-buffer-payload.d.ts +36 -0
- package/src/connections/shared-array-buffer-payload.js +235 -0
- package/src/connections/types.d.ts +50 -0
- package/src/connections/types.js +37 -0
- package/src/connections/windows.d.ts +28 -0
- package/src/connections/windows.js +224 -0
- package/src/debug/env-diff.d.ts +26 -0
- package/src/debug/env-diff.js +49 -0
- package/src/debug/gate.d.ts +18 -0
- package/src/debug/gate.js +69 -0
- package/src/debug/handle.d.ts +23 -0
- package/src/debug/handle.js +48 -0
- package/src/error.d.ts +13 -0
- package/src/error.js +49 -0
- package/src/ipc/tools/ring-queue.d.ts +33 -0
- package/src/ipc/tools/ring-queue.js +159 -0
- package/src/ipc/transport/shared-memory.d.ts +23 -0
- package/src/ipc/transport/shared-memory.js +35 -0
- package/src/memory/byte-carpet.d.ts +73 -0
- package/src/memory/byte-carpet.js +157 -0
- package/src/memory/lock.d.ts +203 -0
- package/src/memory/lock.js +741 -0
- package/src/memory/payload-config.d.ts +31 -0
- package/src/memory/payload-config.js +67 -0
- package/src/memory/payloadCodec.d.ts +46 -0
- package/src/memory/payloadCodec.js +1377 -0
- package/src/memory/regionRegistry.d.ts +17 -0
- package/src/memory/regionRegistry.js +285 -0
- package/src/memory/shared-buffer-io.d.ts +55 -0
- package/src/memory/shared-buffer-io.js +403 -0
- package/src/permission/index.d.ts +2 -0
- package/src/permission/index.js +2 -0
- package/src/permission/protocol.d.ts +166 -0
- package/src/permission/protocol.js +650 -0
- package/src/runtime/balancer.d.ts +19 -0
- package/src/runtime/balancer.js +149 -0
- package/src/runtime/dispatcher.d.ts +40 -0
- package/src/runtime/dispatcher.js +142 -0
- package/src/runtime/inline-executor.d.ts +10 -0
- package/src/runtime/inline-executor.js +282 -0
- package/src/runtime/pool.d.ts +39 -0
- package/src/runtime/pool.js +404 -0
- package/src/runtime/process-worker.d.ts +92 -0
- package/src/runtime/process-worker.js +673 -0
- package/src/runtime/tx-queue.d.ts +26 -0
- package/src/runtime/tx-queue.js +155 -0
- package/src/shared/abortSignal.d.ts +23 -0
- package/src/shared/abortSignal.js +134 -0
- package/src/types.d.ts +357 -0
- package/src/types.js +2 -0
- package/src/utils/http.d.ts +29 -0
- package/src/utils/http.js +100 -0
- package/src/worker/bootstrap.d.ts +5 -0
- package/src/worker/bootstrap.js +78 -0
- package/src/worker/composable-runners.d.ts +12 -0
- package/src/worker/composable-runners.js +97 -0
- package/src/worker/loop.d.ts +2 -0
- package/src/worker/loop.js +382 -0
- package/src/worker/process-worker-bootstrap.d.ts +8 -0
- package/src/worker/process-worker-bootstrap.js +160 -0
- package/src/worker/rx-queue.d.ts +25 -0
- package/src/worker/rx-queue.js +173 -0
- package/src/worker/safety/index.d.ts +4 -0
- package/src/worker/safety/index.js +4 -0
- package/src/worker/safety/performance.d.ts +1 -0
- package/src/worker/safety/performance.js +17 -0
- package/src/worker/safety/process.d.ts +2 -0
- package/src/worker/safety/process.js +79 -0
- package/src/worker/safety/startup.d.ts +16 -0
- package/src/worker/safety/startup.js +30 -0
- package/src/worker/safety/worker-data.d.ts +2 -0
- package/src/worker/safety/worker-data.js +36 -0
- package/src/worker/task-loader.d.ts +27 -0
- package/src/worker/task-loader.js +70 -0
- package/src/worker/timers.d.ts +18 -0
- package/src/worker/timers.js +116 -0
- package/unsafe.d.ts +1 -71
- package/unsafe.js +1 -878
- package/utils.d.ts +1 -31
- package/utils.js +1 -116
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export type BufferLike = ArrayBuffer | SharedArrayBuffer | ArrayBufferView;
|
|
2
|
+
export type NumberFormat = "f64" | "f32" | "i32";
|
|
3
|
+
export type NumberBufferOptions = {
|
|
4
|
+
format?: NumberFormat;
|
|
5
|
+
};
|
|
6
|
+
/** View ArrayBuffer/SharedArrayBuffer/typed-array bytes as Uint8Array. */
|
|
7
|
+
export declare const bufferToBytes: (source: BufferLike) => Uint8Array;
|
|
8
|
+
/** Copy bytes into a SharedArrayBuffer for zero-copy worker inputs. */
|
|
9
|
+
export declare const bytesToBuffer: (source: BufferLike) => SharedArrayBuffer;
|
|
10
|
+
/** Decode UTF-8 bytes from ArrayBuffer/SharedArrayBuffer/typed-array input. */
|
|
11
|
+
export declare const bufferToString: (source: BufferLike) => string;
|
|
12
|
+
/** Encode UTF-8 text into a SharedArrayBuffer. */
|
|
13
|
+
export declare const stringToBuffer: (text: string) => SharedArrayBuffer;
|
|
14
|
+
/** Decode JSON from UTF-8 bytes. */
|
|
15
|
+
export declare const bufferToJson: <T = unknown>(source: BufferLike) => T;
|
|
16
|
+
/** Encode JSON into a SharedArrayBuffer. */
|
|
17
|
+
export declare const jsonToBuffer: (value: unknown) => SharedArrayBuffer;
|
|
18
|
+
/** Encode numbers into a SharedArrayBuffer (`f64` by default). */
|
|
19
|
+
export declare const numbersToBuffer: (numbers: ArrayLike<number>, options?: NumberBufferOptions) => SharedArrayBuffer;
|
|
20
|
+
/** Decode numeric typed-array views from worker-friendly byte buffers. */
|
|
21
|
+
export declare function bufferToNumbers(source: BufferLike, options?: {
|
|
22
|
+
format?: "f64";
|
|
23
|
+
}): Float64Array;
|
|
24
|
+
export declare function bufferToNumbers(source: BufferLike, options: {
|
|
25
|
+
format: "f32";
|
|
26
|
+
}): Float32Array;
|
|
27
|
+
export declare function bufferToNumbers(source: BufferLike, options: {
|
|
28
|
+
format: "i32";
|
|
29
|
+
}): Int32Array;
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
const nodeBuffer = globalThis.Buffer;
|
|
2
|
+
const hasNodeBuffer = typeof nodeBuffer?.from === "function" &&
|
|
3
|
+
typeof nodeBuffer?.byteLength === "function";
|
|
4
|
+
const textEncoder = new TextEncoder();
|
|
5
|
+
const textDecoder = new TextDecoder();
|
|
6
|
+
const hasSharedArrayBuffer = typeof SharedArrayBuffer === "function";
|
|
7
|
+
const isSharedArrayBuffer = (value) => hasSharedArrayBuffer && value instanceof SharedArrayBuffer;
|
|
8
|
+
const BYTES_PER_ELEMENT = {
|
|
9
|
+
f64: 8,
|
|
10
|
+
f32: 4,
|
|
11
|
+
i32: 4,
|
|
12
|
+
};
|
|
13
|
+
const toBytes = (source) => {
|
|
14
|
+
if (source instanceof Uint8Array)
|
|
15
|
+
return source;
|
|
16
|
+
if (ArrayBuffer.isView(source)) {
|
|
17
|
+
return new Uint8Array(source.buffer, source.byteOffset, source.byteLength);
|
|
18
|
+
}
|
|
19
|
+
if (source instanceof ArrayBuffer || isSharedArrayBuffer(source)) {
|
|
20
|
+
return new Uint8Array(source);
|
|
21
|
+
}
|
|
22
|
+
throw new TypeError("Expected an ArrayBuffer, SharedArrayBuffer, or typed-array view.");
|
|
23
|
+
};
|
|
24
|
+
/** View ArrayBuffer/SharedArrayBuffer/typed-array bytes as Uint8Array. */
|
|
25
|
+
export const bufferToBytes = (source) => toBytes(source);
|
|
26
|
+
/** Copy bytes into a SharedArrayBuffer for zero-copy worker inputs. */
|
|
27
|
+
export const bytesToBuffer = (source) => {
|
|
28
|
+
const bytes = toBytes(source);
|
|
29
|
+
const sab = new SharedArrayBuffer(bytes.byteLength);
|
|
30
|
+
new Uint8Array(sab).set(bytes);
|
|
31
|
+
return sab;
|
|
32
|
+
};
|
|
33
|
+
const makeNumberView = (format, buffer, byteOffset, length) => {
|
|
34
|
+
switch (format) {
|
|
35
|
+
case "f64":
|
|
36
|
+
return new Float64Array(buffer, byteOffset, length);
|
|
37
|
+
case "f32":
|
|
38
|
+
return new Float32Array(buffer, byteOffset, length);
|
|
39
|
+
case "i32":
|
|
40
|
+
return new Int32Array(buffer, byteOffset, length);
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
/** Decode UTF-8 bytes from ArrayBuffer/SharedArrayBuffer/typed-array input. */
|
|
44
|
+
export const bufferToString = (source) => {
|
|
45
|
+
const bytes = toBytes(source);
|
|
46
|
+
if (hasNodeBuffer) {
|
|
47
|
+
return nodeBuffer
|
|
48
|
+
.from(bytes.buffer, bytes.byteOffset, bytes.byteLength)
|
|
49
|
+
.toString("utf8");
|
|
50
|
+
}
|
|
51
|
+
return textDecoder.decode(bytes);
|
|
52
|
+
};
|
|
53
|
+
/** Encode UTF-8 text into a SharedArrayBuffer. */
|
|
54
|
+
export const stringToBuffer = (text) => {
|
|
55
|
+
if (typeof text !== "string") {
|
|
56
|
+
throw new TypeError("stringToBuffer expects a string.");
|
|
57
|
+
}
|
|
58
|
+
if (hasNodeBuffer) {
|
|
59
|
+
const byteLength = nodeBuffer.byteLength(text, "utf8");
|
|
60
|
+
const sab = new SharedArrayBuffer(byteLength);
|
|
61
|
+
nodeBuffer.from(sab).write(text, "utf8");
|
|
62
|
+
return sab;
|
|
63
|
+
}
|
|
64
|
+
const encoded = textEncoder.encode(text);
|
|
65
|
+
const sab = new SharedArrayBuffer(encoded.byteLength);
|
|
66
|
+
new Uint8Array(sab).set(encoded);
|
|
67
|
+
return sab;
|
|
68
|
+
};
|
|
69
|
+
/** Decode JSON from UTF-8 bytes. */
|
|
70
|
+
export const bufferToJson = (source) => JSON.parse(bufferToString(source));
|
|
71
|
+
/** Encode JSON into a SharedArrayBuffer. */
|
|
72
|
+
export const jsonToBuffer = (value) => {
|
|
73
|
+
const text = JSON.stringify(value);
|
|
74
|
+
if (typeof text !== "string") {
|
|
75
|
+
throw new TypeError("jsonToBuffer: value is not JSON-serializable (stringify returned undefined).");
|
|
76
|
+
}
|
|
77
|
+
return stringToBuffer(text);
|
|
78
|
+
};
|
|
79
|
+
/** Encode numbers into a SharedArrayBuffer (`f64` by default). */
|
|
80
|
+
export const numbersToBuffer = (numbers, options) => {
|
|
81
|
+
const format = options?.format ?? "f64";
|
|
82
|
+
const sab = new SharedArrayBuffer(numbers.length * BYTES_PER_ELEMENT[format]);
|
|
83
|
+
makeNumberView(format, sab, 0, numbers.length).set(numbers);
|
|
84
|
+
return sab;
|
|
85
|
+
};
|
|
86
|
+
export function bufferToNumbers(source, options) {
|
|
87
|
+
const format = options?.format ?? "f64";
|
|
88
|
+
const bytesPer = BYTES_PER_ELEMENT[format];
|
|
89
|
+
const bytes = toBytes(source);
|
|
90
|
+
if (bytes.byteLength % bytesPer !== 0) {
|
|
91
|
+
throw new RangeError(`bufferToNumbers: byte length ${bytes.byteLength} is not a multiple of ` +
|
|
92
|
+
`${bytesPer} for format "${format}".`);
|
|
93
|
+
}
|
|
94
|
+
const length = bytes.byteLength / bytesPer;
|
|
95
|
+
if (bytes.byteOffset % bytesPer === 0) {
|
|
96
|
+
return makeNumberView(format, bytes.buffer, bytes.byteOffset, length);
|
|
97
|
+
}
|
|
98
|
+
const copy = bytes.slice();
|
|
99
|
+
return makeNumberView(format, copy.buffer, 0, length);
|
|
100
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) {
|
|
2
|
+
if (typeof path === "string" && /^\.\.?\//.test(path)) {
|
|
3
|
+
return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
|
|
4
|
+
return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
|
|
5
|
+
});
|
|
6
|
+
}
|
|
7
|
+
return path;
|
|
8
|
+
};
|
|
9
|
+
import { RUNTIME } from "../common/runtime.js";
|
|
10
|
+
import { toModuleUrl } from "../common/module-url.js";
|
|
11
|
+
import { ProcessSharedBuffer } from "../connections/process-shared-buffer.js";
|
|
12
|
+
const DEFAULT_BOOTSTRAP_EXPORT_NAME = "default";
|
|
13
|
+
const isWorkerBootstrapOptions = (value) => {
|
|
14
|
+
if (!value || typeof value !== "object")
|
|
15
|
+
return false;
|
|
16
|
+
const candidate = value;
|
|
17
|
+
return typeof candidate.href === "string" &&
|
|
18
|
+
candidate.href.length > 0 &&
|
|
19
|
+
(candidate.name === undefined ||
|
|
20
|
+
(typeof candidate.name === "string" && candidate.name.length > 0));
|
|
21
|
+
};
|
|
22
|
+
const isProcessSharedBufferMetadata = (value) => {
|
|
23
|
+
try {
|
|
24
|
+
ProcessSharedBuffer.fromMetadata(value);
|
|
25
|
+
return true;
|
|
26
|
+
}
|
|
27
|
+
catch {
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
const reviveWorkerBootstrapValue = (value, seen = new WeakMap()) => {
|
|
32
|
+
if (isProcessSharedBufferMetadata(value)) {
|
|
33
|
+
return ProcessSharedBuffer.fromMetadata(value);
|
|
34
|
+
}
|
|
35
|
+
if (value === null || typeof value !== "object")
|
|
36
|
+
return value;
|
|
37
|
+
const existing = seen.get(value);
|
|
38
|
+
if (existing !== undefined)
|
|
39
|
+
return existing;
|
|
40
|
+
if (Array.isArray(value)) {
|
|
41
|
+
const out = [];
|
|
42
|
+
seen.set(value, out);
|
|
43
|
+
for (const item of value) {
|
|
44
|
+
out.push(reviveWorkerBootstrapValue(item, seen));
|
|
45
|
+
}
|
|
46
|
+
return out;
|
|
47
|
+
}
|
|
48
|
+
const prototype = Object.getPrototypeOf(value);
|
|
49
|
+
if (prototype !== Object.prototype && prototype !== null)
|
|
50
|
+
return value;
|
|
51
|
+
const out = {};
|
|
52
|
+
seen.set(value, out);
|
|
53
|
+
for (const [key, item] of Object.entries(value)) {
|
|
54
|
+
out[key] = reviveWorkerBootstrapValue(item, seen);
|
|
55
|
+
}
|
|
56
|
+
return out;
|
|
57
|
+
};
|
|
58
|
+
export const runWorkerBootstrap = async ({ bootstrap, thread, totalNumberOfThread, }) => {
|
|
59
|
+
if (bootstrap === undefined)
|
|
60
|
+
return;
|
|
61
|
+
if (!isWorkerBootstrapOptions(bootstrap)) {
|
|
62
|
+
throw new TypeError("worker.bootstrap must include a non-empty href");
|
|
63
|
+
}
|
|
64
|
+
const module = await import(__rewriteRelativeImportExtension(toModuleUrl(bootstrap.href)));
|
|
65
|
+
const name = bootstrap.name ?? DEFAULT_BOOTSTRAP_EXPORT_NAME;
|
|
66
|
+
const selected = module[name];
|
|
67
|
+
if (typeof selected !== "function") {
|
|
68
|
+
const available = Object.keys(module).join(", ");
|
|
69
|
+
throw new TypeError(`worker.bootstrap expected export "${name}" from "${bootstrap.href}" to be a function.` +
|
|
70
|
+
` Available exports: ${available || "(none)"}`);
|
|
71
|
+
}
|
|
72
|
+
const context = Object.freeze({
|
|
73
|
+
thread,
|
|
74
|
+
totalNumberOfThread,
|
|
75
|
+
runtime: RUNTIME,
|
|
76
|
+
});
|
|
77
|
+
await selected(reviveWorkerBootstrapValue(bootstrap.data), context);
|
|
78
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type Task } from "../memory/lock.js";
|
|
2
|
+
import type { TimeoutSpec } from "./task-loader.js";
|
|
3
|
+
type WorkerJob = (args: unknown, abortToolkit?: unknown) => unknown;
|
|
4
|
+
type SlotRunner = (slot: Task) => unknown;
|
|
5
|
+
type HasAborted = (signal: number) => boolean;
|
|
6
|
+
export declare const composeWorkerRunner: ({ job, timeout, hasAborted, now, }: {
|
|
7
|
+
job: WorkerJob;
|
|
8
|
+
timeout?: TimeoutSpec;
|
|
9
|
+
hasAborted?: HasAborted;
|
|
10
|
+
now?: () => number;
|
|
11
|
+
}) => SlotRunner;
|
|
12
|
+
export {};
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { getTaskFunctionMeta, getTaskSlotMeta, TASK_SLOT_META_VALUE_MASK, } from "../memory/lock.js";
|
|
2
|
+
const ABORT_SIGNAL_META_OFFSET = 1;
|
|
3
|
+
const TIMEOUT_KIND_RESOLVE = 1;
|
|
4
|
+
const p_now = performance.now.bind(performance);
|
|
5
|
+
const raceTimeout = (promise, ms, resolveOnTimeout, timeoutValue) => new Promise((resolve, reject) => {
|
|
6
|
+
let done = false;
|
|
7
|
+
const timer = setTimeout(() => {
|
|
8
|
+
if (done)
|
|
9
|
+
return;
|
|
10
|
+
done = true;
|
|
11
|
+
if (resolveOnTimeout)
|
|
12
|
+
resolve(timeoutValue);
|
|
13
|
+
else
|
|
14
|
+
reject(timeoutValue);
|
|
15
|
+
}, ms);
|
|
16
|
+
promise.then((value) => {
|
|
17
|
+
if (done)
|
|
18
|
+
return;
|
|
19
|
+
done = true;
|
|
20
|
+
clearTimeout(timer);
|
|
21
|
+
resolve(value);
|
|
22
|
+
}, (err) => {
|
|
23
|
+
if (done)
|
|
24
|
+
return;
|
|
25
|
+
done = true;
|
|
26
|
+
clearTimeout(timer);
|
|
27
|
+
reject(err);
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
const nowStamp = (now) => (Math.floor(now()) & TASK_SLOT_META_VALUE_MASK) >>> 0;
|
|
31
|
+
const applyTimeoutBudget = (promise, slot, spec, now) => {
|
|
32
|
+
const elapsed = (nowStamp(now) - getTaskSlotMeta(slot)) & TASK_SLOT_META_VALUE_MASK;
|
|
33
|
+
const remaining = spec.ms - elapsed;
|
|
34
|
+
if (!(remaining > 0)) {
|
|
35
|
+
// Prevent late unhandled rejections from the original promise.
|
|
36
|
+
promise.then(() => { }, () => { });
|
|
37
|
+
return spec.kind === TIMEOUT_KIND_RESOLVE
|
|
38
|
+
? Promise.resolve(spec.value)
|
|
39
|
+
: Promise.reject(spec.value);
|
|
40
|
+
}
|
|
41
|
+
const timeoutMs = Math.max(1, Math.floor(remaining));
|
|
42
|
+
return raceTimeout(promise, timeoutMs, spec.kind === TIMEOUT_KIND_RESOLVE, spec.value);
|
|
43
|
+
};
|
|
44
|
+
const NO_ABORT_SIGNAL = -1;
|
|
45
|
+
const readSignal = (slot) => {
|
|
46
|
+
const encodedSignal = getTaskFunctionMeta(slot);
|
|
47
|
+
if (encodedSignal === 0)
|
|
48
|
+
return NO_ABORT_SIGNAL;
|
|
49
|
+
const signal = (encodedSignal - ABORT_SIGNAL_META_OFFSET) | 0;
|
|
50
|
+
return signal >= 0 ? signal : NO_ABORT_SIGNAL;
|
|
51
|
+
};
|
|
52
|
+
const makeToolkitCache = (hasAborted) => {
|
|
53
|
+
const bySignal = [];
|
|
54
|
+
return (signal) => {
|
|
55
|
+
let toolkit = bySignal[signal];
|
|
56
|
+
if (toolkit)
|
|
57
|
+
return toolkit;
|
|
58
|
+
const hasAbortedMethod = () => hasAborted(signal);
|
|
59
|
+
toolkit = {
|
|
60
|
+
hasAborted: hasAbortedMethod,
|
|
61
|
+
};
|
|
62
|
+
bySignal[signal] = toolkit;
|
|
63
|
+
return toolkit;
|
|
64
|
+
};
|
|
65
|
+
};
|
|
66
|
+
export const composeWorkerRunner = ({ job, timeout, hasAborted, now, }) => {
|
|
67
|
+
const nowTime = now ?? p_now;
|
|
68
|
+
if (!hasAborted) {
|
|
69
|
+
if (!timeout) {
|
|
70
|
+
return (slot) => job(slot.value);
|
|
71
|
+
}
|
|
72
|
+
return (slot) => {
|
|
73
|
+
const result = job(slot.value);
|
|
74
|
+
if (!(result instanceof Promise))
|
|
75
|
+
return result;
|
|
76
|
+
return applyTimeoutBudget(result, slot, timeout, nowTime);
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
const getToolkit = makeToolkitCache(hasAborted);
|
|
80
|
+
if (!timeout) {
|
|
81
|
+
return (slot) => {
|
|
82
|
+
const signal = readSignal(slot);
|
|
83
|
+
if (signal === NO_ABORT_SIGNAL)
|
|
84
|
+
return job(slot.value);
|
|
85
|
+
return job(slot.value, getToolkit(signal));
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
return (slot) => {
|
|
89
|
+
const signal = readSignal(slot);
|
|
90
|
+
const result = signal === NO_ABORT_SIGNAL
|
|
91
|
+
? job(slot.value)
|
|
92
|
+
: job(slot.value, getToolkit(signal));
|
|
93
|
+
if (!(result instanceof Promise))
|
|
94
|
+
return result;
|
|
95
|
+
return applyTimeoutBudget(result, slot, timeout, nowTime);
|
|
96
|
+
};
|
|
97
|
+
};
|