knitting 0.1.53 → 0.1.55

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.
Files changed (135) hide show
  1. package/README.md +38 -27
  2. package/knitting.d.ts +797 -5
  3. package/knitting.js +9780 -5
  4. package/map.md +9 -12
  5. package/package.json +14 -13
  6. package/shared-memory.d.ts +155 -0
  7. package/shared-memory.js +1342 -0
  8. package/unsafe.d.ts +71 -1
  9. package/unsafe.js +878 -1
  10. package/utils.d.ts +31 -1
  11. package/utils.js +116 -1
  12. package/process-shared-buffer.d.ts +0 -1
  13. package/process-shared-buffer.js +0 -1
  14. package/src/api.d.ts +0 -69
  15. package/src/api.js +0 -555
  16. package/src/common/envelope.d.ts +0 -17
  17. package/src/common/envelope.js +0 -22
  18. package/src/common/module-url.d.ts +0 -1
  19. package/src/common/module-url.js +0 -24
  20. package/src/common/node-compat.d.ts +0 -20
  21. package/src/common/node-compat.js +0 -24
  22. package/src/common/path-canonical.d.ts +0 -6
  23. package/src/common/path-canonical.js +0 -41
  24. package/src/common/runtime.d.ts +0 -15
  25. package/src/common/runtime.js +0 -91
  26. package/src/common/shared-buffer-region.d.ts +0 -11
  27. package/src/common/shared-buffer-region.js +0 -21
  28. package/src/common/shared-buffer-text.d.ts +0 -16
  29. package/src/common/shared-buffer-text.js +0 -65
  30. package/src/common/task-source.d.ts +0 -3
  31. package/src/common/task-source.js +0 -84
  32. package/src/common/task-symbol.d.ts +0 -1
  33. package/src/common/task-symbol.js +0 -1
  34. package/src/common/with-resolvers.d.ts +0 -9
  35. package/src/common/with-resolvers.js +0 -23
  36. package/src/common/worker-runtime.d.ts +0 -42
  37. package/src/common/worker-runtime.js +0 -61
  38. package/src/connections/buffer-reference-native.d.ts +0 -56
  39. package/src/connections/buffer-reference-native.js +0 -217
  40. package/src/connections/buffer-reference.d.ts +0 -78
  41. package/src/connections/buffer-reference.js +0 -461
  42. package/src/connections/bun.d.ts +0 -22
  43. package/src/connections/bun.js +0 -214
  44. package/src/connections/deno.d.ts +0 -22
  45. package/src/connections/deno.js +0 -205
  46. package/src/connections/file-descriptor.d.ts +0 -39
  47. package/src/connections/file-descriptor.js +0 -161
  48. package/src/connections/index.d.ts +0 -4
  49. package/src/connections/index.js +0 -4
  50. package/src/connections/node-addons.d.ts +0 -5
  51. package/src/connections/node-addons.js +0 -43
  52. package/src/connections/node-buffer-pointer.d.ts +0 -20
  53. package/src/connections/node-buffer-pointer.js +0 -16
  54. package/src/connections/node.d.ts +0 -30
  55. package/src/connections/node.js +0 -62
  56. package/src/connections/posix.d.ts +0 -32
  57. package/src/connections/posix.js +0 -77
  58. package/src/connections/process-shared-buffer.d.ts +0 -75
  59. package/src/connections/process-shared-buffer.js +0 -279
  60. package/src/connections/shared-array-buffer-payload.d.ts +0 -36
  61. package/src/connections/shared-array-buffer-payload.js +0 -235
  62. package/src/connections/types.d.ts +0 -50
  63. package/src/connections/types.js +0 -37
  64. package/src/connections/windows.d.ts +0 -28
  65. package/src/connections/windows.js +0 -224
  66. package/src/debug/env-diff.d.ts +0 -26
  67. package/src/debug/env-diff.js +0 -49
  68. package/src/debug/gate.d.ts +0 -18
  69. package/src/debug/gate.js +0 -69
  70. package/src/debug/handle.d.ts +0 -23
  71. package/src/debug/handle.js +0 -48
  72. package/src/error.d.ts +0 -13
  73. package/src/error.js +0 -49
  74. package/src/ipc/tools/ring-queue.d.ts +0 -33
  75. package/src/ipc/tools/ring-queue.js +0 -159
  76. package/src/ipc/transport/shared-memory.d.ts +0 -23
  77. package/src/ipc/transport/shared-memory.js +0 -35
  78. package/src/memory/byte-carpet.d.ts +0 -73
  79. package/src/memory/byte-carpet.js +0 -157
  80. package/src/memory/lock.d.ts +0 -201
  81. package/src/memory/lock.js +0 -739
  82. package/src/memory/payload-config.d.ts +0 -31
  83. package/src/memory/payload-config.js +0 -67
  84. package/src/memory/payloadCodec.d.ts +0 -46
  85. package/src/memory/payloadCodec.js +0 -1334
  86. package/src/memory/regionRegistry.d.ts +0 -17
  87. package/src/memory/regionRegistry.js +0 -285
  88. package/src/memory/shared-buffer-io.d.ts +0 -55
  89. package/src/memory/shared-buffer-io.js +0 -403
  90. package/src/permission/index.d.ts +0 -2
  91. package/src/permission/index.js +0 -2
  92. package/src/permission/protocol.d.ts +0 -166
  93. package/src/permission/protocol.js +0 -650
  94. package/src/runtime/balancer.d.ts +0 -19
  95. package/src/runtime/balancer.js +0 -149
  96. package/src/runtime/dispatcher.d.ts +0 -34
  97. package/src/runtime/dispatcher.js +0 -142
  98. package/src/runtime/inline-executor.d.ts +0 -10
  99. package/src/runtime/inline-executor.js +0 -282
  100. package/src/runtime/pool.d.ts +0 -30
  101. package/src/runtime/pool.js +0 -391
  102. package/src/runtime/process-worker.d.ts +0 -92
  103. package/src/runtime/process-worker.js +0 -673
  104. package/src/runtime/tx-queue.d.ts +0 -26
  105. package/src/runtime/tx-queue.js +0 -151
  106. package/src/shared/abortSignal.d.ts +0 -23
  107. package/src/shared/abortSignal.js +0 -134
  108. package/src/types.d.ts +0 -346
  109. package/src/types.js +0 -2
  110. package/src/utils/http.d.ts +0 -29
  111. package/src/utils/http.js +0 -100
  112. package/src/worker/bootstrap.d.ts +0 -5
  113. package/src/worker/bootstrap.js +0 -78
  114. package/src/worker/composable-runners.d.ts +0 -12
  115. package/src/worker/composable-runners.js +0 -97
  116. package/src/worker/loop.d.ts +0 -2
  117. package/src/worker/loop.js +0 -382
  118. package/src/worker/process-worker-bootstrap.d.ts +0 -8
  119. package/src/worker/process-worker-bootstrap.js +0 -160
  120. package/src/worker/rx-queue.d.ts +0 -25
  121. package/src/worker/rx-queue.js +0 -173
  122. package/src/worker/safety/index.d.ts +0 -4
  123. package/src/worker/safety/index.js +0 -4
  124. package/src/worker/safety/performance.d.ts +0 -1
  125. package/src/worker/safety/performance.js +0 -17
  126. package/src/worker/safety/process.d.ts +0 -2
  127. package/src/worker/safety/process.js +0 -79
  128. package/src/worker/safety/startup.d.ts +0 -16
  129. package/src/worker/safety/startup.js +0 -30
  130. package/src/worker/safety/worker-data.d.ts +0 -2
  131. package/src/worker/safety/worker-data.js +0 -36
  132. package/src/worker/task-loader.d.ts +0 -27
  133. package/src/worker/task-loader.js +0 -78
  134. package/src/worker/timers.d.ts +0 -18
  135. package/src/worker/timers.js +0 -116
@@ -1,151 +0,0 @@
1
- import { makeTask, resetTaskLocalFlags, runTaskFinalizers, TaskIndex, } from "../memory/lock.js";
2
- import { withResolvers } from "../common/with-resolvers.js";
3
- import { AbortSignalPoolExhausted, OneShotDeferred, } from "../shared/abortSignal.js";
4
- import { withBufferReferenceReturnReleaser } from "../connections/buffer-reference.js";
5
- const SLOT_INDEX_MASK = 31;
6
- const SLOT_META_MASK = 0x07ffffff;
7
- const SLOT_META_SHIFT = 5;
8
- const FUNCTION_ID_MASK = 0xffff;
9
- const FUNCTION_META_MASK = 0xffff;
10
- const FUNCTION_META_SHIFT = 16;
11
- const ABORT_SIGNAL_META_OFFSET = 1;
12
- const NO_ABORT_SIGNAL = -1;
13
- const p_now = performance.now.bind(performance);
14
- export function createHostTxQueue({ max, lock, returnLock, releaseBufferReferenceReturn, abortSignals, now, }) {
15
- const PLACE_HOLDER = (_) => {
16
- throw ("UNREACHABLE FROM PLACE HOLDER (main)");
17
- };
18
- const newSlot = (id) => {
19
- const task = makeTask();
20
- task[TaskIndex.ID] = id;
21
- task[TaskIndex.FunctionID] = 0;
22
- task.value = null;
23
- task.resolve = PLACE_HOLDER;
24
- task.reject = PLACE_HOLDER;
25
- return task;
26
- };
27
- const initialSize = max ?? 10;
28
- const queue = Array.from({ length: initialSize }, (_, index) => newSlot(index));
29
- const freeSockets = Array.from({ length: initialSize }, (_, i) => i);
30
- const freePush = (id) => freeSockets.push(id);
31
- const freePop = () => freeSockets.pop();
32
- const queuePush = (task) => queue.push(task);
33
- const { publish, flushPending, hasPendingFrames, getPendingFrameCount, getPendingPromiseCount, resetPendingState, } = lock;
34
- let inUsed = 0 | 0;
35
- const resetSignal = abortSignals?.resetSignal;
36
- const nowTime = now ?? p_now;
37
- const resolveReturn = returnLock.resolveHost({
38
- queue,
39
- activeRejectPlaceholder: PLACE_HOLDER,
40
- onResolved: (task) => {
41
- inUsed = (inUsed - 1) | 0;
42
- resetTaskLocalFlags(task);
43
- runTaskFinalizers(task);
44
- task.value = null;
45
- task.resolve = PLACE_HOLDER;
46
- task.reject = PLACE_HOLDER;
47
- freePush(task[TaskIndex.ID]);
48
- },
49
- });
50
- const completeFrame = releaseBufferReferenceReturn === undefined
51
- ? resolveReturn
52
- : () => withBufferReferenceReturnReleaser(releaseBufferReferenceReturn, resolveReturn);
53
- const txIdle = () => getPendingFrameCount() === 0 && inUsed === getPendingPromiseCount();
54
- const rejectAll = (reason) => {
55
- for (let index = 0; index < queue.length; index++) {
56
- const slot = queue[index];
57
- if (slot.reject !== PLACE_HOLDER) {
58
- try {
59
- slot.reject(reason);
60
- }
61
- catch {
62
- }
63
- runTaskFinalizers(slot);
64
- slot.resolve = PLACE_HOLDER;
65
- slot.reject = PLACE_HOLDER;
66
- queue[index] = newSlot(index);
67
- }
68
- }
69
- resetPendingState();
70
- inUsed = 0 | 0;
71
- };
72
- const flushToWorker = () => flushPending();
73
- const enqueueKnown = (task) => {
74
- return publish(task);
75
- };
76
- return {
77
- rejectAll,
78
- hasPendingFrames,
79
- txIdle,
80
- completeFrame,
81
- enqueue: (functionID, timeout, abortSignal) => {
82
- const HAS_TIMER = timeout !== undefined;
83
- const functionIDMasked = functionID & FUNCTION_ID_MASK;
84
- const USE_SIGNAL = abortSignal !== undefined &&
85
- abortSignals !== undefined;
86
- return (rawArgs) => {
87
- if (inUsed === queue.length) {
88
- const newSize = inUsed + 32;
89
- let current = queue.length;
90
- while (newSize > current) {
91
- queuePush(newSlot(current));
92
- freePush(current);
93
- current++;
94
- }
95
- }
96
- const index = freePop();
97
- const slot = queue[index];
98
- const deferred = withResolvers();
99
- slot[TaskIndex.FunctionID] = functionIDMasked;
100
- if (USE_SIGNAL) {
101
- const maybeSignal = abortSignals.getSignal();
102
- if (maybeSignal === abortSignals.closeNow) {
103
- return Promise.reject(AbortSignalPoolExhausted);
104
- }
105
- new OneShotDeferred(deferred, () => resetSignal(maybeSignal), () => {
106
- abortSignals.setSignal(maybeSignal);
107
- });
108
- const encodedSignalMeta = ((maybeSignal + ABORT_SIGNAL_META_OFFSET) & FUNCTION_META_MASK) >>>
109
- 0;
110
- slot[TaskIndex.FunctionID] =
111
- ((encodedSignalMeta << FUNCTION_META_SHIFT) | functionIDMasked) >>>
112
- 0;
113
- }
114
- slot.value = rawArgs;
115
- slot[TaskIndex.ID] = index;
116
- slot.resolve = deferred.resolve;
117
- slot.reject = deferred.reject;
118
- if (HAS_TIMER) {
119
- slot[TaskIndex.slotBuffer] = ((slot[TaskIndex.slotBuffer] & SLOT_INDEX_MASK) |
120
- ((((nowTime() >>> 0) & SLOT_META_MASK) << SLOT_META_SHIFT) >>> 0)) >>> 0;
121
- }
122
- void publish(slot);
123
- inUsed = (inUsed + 1) | 0;
124
- return deferred.promise;
125
- };
126
- },
127
- flushToWorker,
128
- enqueueKnown,
129
- settlePromisePayload: (task, isRejected, value) => {
130
- if (task.reject === PLACE_HOLDER)
131
- return false;
132
- if (isRejected) {
133
- try {
134
- task.reject(value);
135
- }
136
- catch {
137
- }
138
- resetTaskLocalFlags(task);
139
- runTaskFinalizers(task);
140
- task.value = null;
141
- task.resolve = PLACE_HOLDER;
142
- task.reject = PLACE_HOLDER;
143
- inUsed = (inUsed - 1) | 0;
144
- freePush(task[TaskIndex.ID]);
145
- return false;
146
- }
147
- task.value = value;
148
- return enqueueKnown(task);
149
- },
150
- };
151
- }
@@ -1,23 +0,0 @@
1
- import { withResolvers } from "../common/with-resolvers.js";
2
- import { type SharedBufferSource } from "../common/shared-buffer-region.js";
3
- export declare const AbortSignalPoolExhausted: unique symbol;
4
- export declare const EnqueuedAbortSignal: unique symbol;
5
- export type SignalAbortStore = ReturnType<typeof signalAbortFactory>;
6
- export type SetSignalResult = -1 | 0 | 1;
7
- export declare const signalAbortFactory: ({ sab, maxSignals, }: {
8
- sab: SharedBufferSource;
9
- maxSignals?: number;
10
- }) => {
11
- max: number;
12
- closeNow: number;
13
- getSignal: () => number;
14
- setSignal: (signal: number) => SetSignalResult;
15
- abortAll: () => number;
16
- hasAborted: (signal: number) => boolean;
17
- resetSignal: (signal: number) => boolean;
18
- inUseCount: () => number;
19
- };
20
- export declare class OneShotDeferred<T> {
21
- #private;
22
- constructor(deferred: ReturnType<typeof withResolvers<T>>, onSettle: () => void, onEmptyReject?: () => void);
23
- }
@@ -1,134 +0,0 @@
1
- import { toSharedBufferRegion, } from "../common/shared-buffer-region.js";
2
- const SLOT_BITS = 32;
3
- const SLOT_MASK = SLOT_BITS - 1;
4
- export const AbortSignalPoolExhausted = Symbol.for("knitting.abortSignal.poolExhausted");
5
- export const EnqueuedAbortSignal = Symbol.for("knitting.abortSignal.enqueuedSignal");
6
- export const signalAbortFactory = ({ sab, maxSignals, }) => {
7
- const sabRegion = toSharedBufferRegion(sab);
8
- const atomicView = new Uint32Array(sabRegion.sab, sabRegion.byteOffset, sabRegion.byteLength / Uint32Array.BYTES_PER_ELEMENT);
9
- const size = atomicView.length;
10
- const inUse = new Uint32Array(size);
11
- const physicalMax = size * SLOT_BITS;
12
- const max = (() => {
13
- if (!Number.isFinite(maxSignals))
14
- return physicalMax;
15
- const parsed = Math.floor(maxSignals);
16
- if (parsed <= 0)
17
- return physicalMax;
18
- return Math.min(parsed, physicalMax);
19
- })();
20
- const closeNow = max + 1;
21
- let current = 0;
22
- let cursor = 0;
23
- const getSignal = () => {
24
- if (current >= max)
25
- return closeNow;
26
- for (let step = 0; step < size; step++) {
27
- const word = (cursor + step) % size;
28
- const wordBase = word << 5;
29
- const remaining = max - wordBase;
30
- if (remaining <= 0)
31
- continue;
32
- const allowedMask = remaining >= SLOT_BITS
33
- ? 0xFFFFFFFF
34
- : ((1 << remaining) - 1) >>> 0;
35
- const freeBits = ((~inUse[word]) & allowedMask) >>> 0;
36
- if (freeBits === 0)
37
- continue;
38
- const bit = (freeBits & -freeBits) >>> 0;
39
- inUse[word] = (inUse[word] | bit) >>> 0;
40
- current = (current + 1) | 0;
41
- cursor = (word + 1) % size;
42
- // Recycled slot must start in "not aborted" state.
43
- Atomics.and(atomicView, word, ~bit);
44
- const bitIndex = 31 - Math.clz32(bit);
45
- return (word << 5) + bitIndex;
46
- }
47
- return closeNow;
48
- };
49
- const setSignal = (signal) => {
50
- // 0 => sentinel "abort because pool/size is exhausted"
51
- if (signal === closeNow)
52
- return 0;
53
- // -1 => invalid signal id
54
- if (!Number.isInteger(signal))
55
- return -1;
56
- if (signal < 0 || signal >= max)
57
- return -1;
58
- // 1 => signal bit was set as aborted
59
- const word = signal >>> 5;
60
- const bit = 1 << (signal & SLOT_MASK);
61
- Atomics.or(atomicView, word, bit);
62
- return 1;
63
- };
64
- const abortAll = () => {
65
- for (let word = 0; word < size; word++) {
66
- Atomics.store(atomicView, word, inUse[word]);
67
- }
68
- return current;
69
- };
70
- const hasAborted = (signal) => {
71
- if (signal === closeNow)
72
- return true;
73
- if (!Number.isInteger(signal))
74
- return false;
75
- if (signal < 0 || signal >= max)
76
- return false;
77
- const word = signal >>> 5;
78
- const bit = 1 << (signal & SLOT_MASK);
79
- return (Atomics.load(atomicView, word) & bit) !== 0;
80
- };
81
- const resetSignal = (signal) => {
82
- if (signal === closeNow)
83
- return false;
84
- if (!Number.isInteger(signal))
85
- return false;
86
- if (signal < 0 || signal >= max)
87
- return false;
88
- const word = signal >>> 5;
89
- const bit = 1 << (signal & SLOT_MASK);
90
- const used = (inUse[word] & bit) !== 0;
91
- if (!used)
92
- return false;
93
- inUse[word] = (inUse[word] & ~bit) >>> 0;
94
- if (current > 0)
95
- current = (current - 1) | 0;
96
- cursor = word;
97
- // Clear aborted flag for future reuse.
98
- Atomics.and(atomicView, word, ~bit);
99
- return true;
100
- };
101
- return {
102
- max,
103
- closeNow,
104
- getSignal,
105
- setSignal,
106
- abortAll,
107
- hasAborted,
108
- resetSignal,
109
- inUseCount: () => current,
110
- };
111
- };
112
- export class OneShotDeferred {
113
- #triggered = false;
114
- constructor(deferred, onSettle, onEmptyReject) {
115
- const settleOnce = (fn) => (...args) => {
116
- if (this.#triggered)
117
- return;
118
- this.#triggered = true;
119
- onSettle();
120
- fn(...args);
121
- };
122
- deferred.resolve = settleOnce(deferred.resolve);
123
- deferred.reject = settleOnce(deferred.reject);
124
- deferred.promise.reject = (reason) => {
125
- if (this.#triggered)
126
- return;
127
- if (reason === undefined && onEmptyReject !== undefined) {
128
- onEmptyReject();
129
- return;
130
- }
131
- deferred.reject(reason);
132
- };
133
- }
134
- }
package/src/types.d.ts DELETED
@@ -1,346 +0,0 @@
1
- import { endpointSymbol } from "./common/task-symbol.js";
2
- import type { Envelope, EnvelopeBody, EnvelopeHeader } from "./common/envelope.js";
3
- import type { LockBufferTextCompat, SharedBufferTextCompat } from "./common/shared-buffer-text.js";
4
- import type { SharedBufferRegion, SharedBufferSource } from "./common/shared-buffer-region.js";
5
- import type { PayloadBufferMode, PayloadBufferOptions } from "./memory/payload-config.js";
6
- import type { PermissionProtocol, PermissionProtocolInput, ResolvedPermissionProtocol } from "./permission/protocol.js";
7
- type WorkerCall = {
8
- fnNumber: number;
9
- timeout?: TaskTimeout;
10
- abortSignal?: AbortSignalOption;
11
- };
12
- type WorkerInvoke = (args: Uint8Array) => Promise<unknown>;
13
- interface WorkerContext {
14
- txIdle(): boolean;
15
- call(descriptor: WorkerCall): WorkerInvoke;
16
- kills(): Promise<void>;
17
- }
18
- type CreateContext = WorkerContext;
19
- type WorkerData = {
20
- sab: SharedBufferSource;
21
- abortSignalSAB?: SharedBufferSource;
22
- abortSignalMax?: number;
23
- list: string[];
24
- ids: number[];
25
- names: string[];
26
- thread: number;
27
- totalNumberOfThread: number;
28
- debug?: DebugOptions;
29
- startAt: number;
30
- workerOptions?: WorkerSettings;
31
- at: number[];
32
- lock: LockBuffers;
33
- returnLock: LockBuffers;
34
- payloadConfig?: PayloadBufferOptions;
35
- bufferReferenceReturn?: "copy" | "borrow";
36
- permission?: ResolvedPermissionProtocol;
37
- };
38
- type UnsafeOptions = {
39
- /**
40
- * Experimental `BufferReference` return lifetime.
41
- *
42
- * `"copy"` is safe after worker release. `"borrow"` skips the Deno/Bun copy,
43
- * but must be released before producer shutdown and must not outlive its ref.
44
- */
45
- BufferReferenceReturn?: "copy" | "borrow";
46
- };
47
- type LockBuffers = {
48
- headers: SharedBufferSource;
49
- headerSlotStrideU32?: number;
50
- lockSector: SharedBufferSource;
51
- payload: SharedBufferSource;
52
- payloadSector: SharedBufferSource;
53
- textCompat?: LockBufferTextCompat;
54
- };
55
- type JSONValue = string | number | boolean | null | JSONArray | JSONObject;
56
- interface JSONObject {
57
- [key: string]: JSONValue;
58
- }
59
- interface JSONArray extends Array<JSONValue> {
60
- }
61
- type Serializable = string | object | number | boolean | bigint;
62
- type ValidInput = bigint | void | JSONValue | symbol | ArrayBuffer | Uint8Array | Int32Array | Float64Array | BigInt64Array | BigUint64Array | DataView | Error | Date | Envelope<EnvelopeHeader, EnvelopeBody>;
63
- type Args = ValidInput | Serializable;
64
- type MaybePromise<T> = T | Promise<T>;
65
- /** Blob payloads are not supported; pass ArrayBuffer/typed arrays instead. */
66
- type NoBlob<T> = T extends Blob ? never : T;
67
- /** Task input may be a direct value or native Promise; thenables are plain values. */
68
- type TaskInput = NoBlob<Args> | Promise<NoBlob<Args>>;
69
- /** Per-call timeout. A number is milliseconds; object form can return a default. */
70
- type TaskTimeout = number | {
71
- time: number;
72
- maybe?: true;
73
- default?: unknown;
74
- error?: unknown;
75
- };
76
- type BivariantCallback<Args extends unknown[], R> = {
77
- bivarianceHack(...args: Args): R;
78
- }["bivarianceHack"];
79
- type AbortSignalConfig = {
80
- readonly hasAborted: true;
81
- };
82
- /** `true` or config injects an abort toolkit as the task's second parameter. */
83
- type AbortSignalOption = true | AbortSignalConfig | undefined;
84
- type AbortSignalMethods<AS extends AbortSignalOption> = AS extends undefined ? never : {
85
- hasAborted: () => boolean;
86
- };
87
- type AbortSignalToolkit<AS extends AbortSignalOption> = AbortSignalMethods<AS>;
88
- type TaskFn<A extends TaskInput, B extends Args, AS extends AbortSignalOption = undefined> = BivariantCallback<AS extends undefined ? [NoBlob<Awaited<A>>] : [NoBlob<Awaited<A>>, AbortSignalToolkit<AS>], MaybePromise<NoBlob<B>>>;
89
- type PromiseWithMaybeReject<T> = Promise<T> & {
90
- reject: (reason?: unknown) => void;
91
- };
92
- type TaskLike<AS extends AbortSignalOption = AbortSignalOption> = {
93
- readonly f: (...args: any[]) => any;
94
- } & (AS extends undefined ? {
95
- readonly abortSignal?: undefined;
96
- } : {
97
- readonly abortSignal: AS;
98
- });
99
- type TaskFunctionLike = (...args: any[]) => any;
100
- type Composed<A extends TaskInput = Args, B extends Args = Args, AS extends AbortSignalOption = undefined> = FixPoint<A, B, AS> & SecondPart;
101
- type tasks = Record<string, Composed<any, any, AbortSignalOption> | TaskFunctionLike>;
102
- type ComposedWithKey = Composed<any, any, AbortSignalOption> & {
103
- name: string;
104
- };
105
- type PromiseWrapped<F extends (...args: any[]) => any, AS extends AbortSignalOption = undefined> = (...args: PromisifyCallArgs<F, AS>) => AS extends undefined ? Promise<Awaited<ReturnType<F>>> : PromiseWithMaybeReject<Awaited<ReturnType<F>>>;
106
- type PromiseInput<T> = T | Promise<T>;
107
- type PromisifyArgs<T extends unknown[]> = {
108
- [K in keyof T]: PromiseInput<T[K]>;
109
- };
110
- type NormalizeUndefinedSingleArg<T extends unknown[]> = T extends [undefined] ? [] | [undefined] : T;
111
- type AbortAwareCallArgs<T extends unknown[]> = T extends [
112
- ...infer Head,
113
- AbortSignalToolkit<any>
114
- ] ? NormalizeUndefinedSingleArg<Head> : NormalizeUndefinedSingleArg<T>;
115
- type HostCallArgs<F extends (...args: any[]) => any, AS extends AbortSignalOption> = AS extends undefined ? Parameters<F> : AbortAwareCallArgs<Parameters<F>>;
116
- type PromisifyCallArgs<F extends (...args: any[]) => any, AS extends AbortSignalOption> = HostCallArgs<F, AS> extends infer T ? T extends unknown[] ? PromisifyArgs<T> : never : never;
117
- type TaskCallable<T> = T extends TaskLike<any> ? T["f"] : T extends TaskFunctionLike ? T : never;
118
- type AbortSignalOfTask<T> = T extends {
119
- readonly abortSignal: infer AS;
120
- } ? Extract<AS, AbortSignalOption> : undefined;
121
- type FunctionMapType<T extends Record<string, TaskLike<any> | TaskFunctionLike>> = {
122
- [K in keyof T]: PromiseWrapped<TaskCallable<T[K]>, AbortSignalOfTask<T[K]>>;
123
- };
124
- interface FixPointBase<A extends TaskInput, B extends Args, AS extends AbortSignalOption = undefined> {
125
- /** Worker function. It receives one value; use a tuple/object for many inputs. */
126
- readonly f: TaskFn<A, B, AS>;
127
- /** Soft call timeout. Use `worker.hardTimeoutMs` for runaway CPU walls. */
128
- readonly timeout?: TaskTimeout;
129
- }
130
- type FixPoint<A extends TaskInput, B extends Args, AS extends AbortSignalOption = undefined> = FixPointBase<A, B, AS> & (AS extends undefined ? {
131
- readonly abortSignal?: undefined;
132
- } : {
133
- readonly abortSignal: AS;
134
- });
135
- type ImportTaskOptions<A extends TaskInput = void, B extends Args = void, AS extends AbortSignalOption = undefined> = Omit<FixPoint<A, B, AS>, "f"> & {
136
- /** Module imported by the worker only. Relative paths resolve from caller. */
137
- readonly href: string;
138
- /** Plain function export name. Defaults to `"default"`; do not target `task()`. */
139
- readonly name?: string;
140
- };
141
- type SecondPart = {
142
- readonly [endpointSymbol]: true;
143
- readonly id: number;
144
- /** Logical export order used to match worker tasks before names are known. */
145
- readonly at: number;
146
- readonly importedFrom: string;
147
- /**
148
- * Imported tasks stay on worker lanes so imports run under worker permissions.
149
- */
150
- readonly imported?: boolean;
151
- };
152
- type SingleTaskPool<A extends TaskInput = Args, B extends Args = Args, AS extends AbortSignalOption = undefined> = {
153
- /** Invoke the single task. Arguments may be native Promises. */
154
- call: PromiseWrapped<TaskFn<A, B, AS>, AS>;
155
- /** Await worker teardown now. `using` disposes at scope exit without awaiting. */
156
- shutdown: (delayMs?: number) => Promise<void>;
157
- /** Starts shutdown at scope exit. Use `shutdown()` when you must await it. */
158
- [Symbol.dispose]: () => void;
159
- };
160
- type Pool<T extends Record<string, TaskLike<any> | TaskFunctionLike>> = {
161
- /** Await worker teardown now. `using` disposes at scope exit without awaiting. */
162
- shutdown: (delayMs?: number) => Promise<void>;
163
- /** Starts shutdown at scope exit. Use `shutdown()` when you must await it. */
164
- [Symbol.dispose]: () => void;
165
- /**
166
- * Typed task callers. Each call accepts the task input or a native Promise.
167
- * Thrown errors/rejections reject here as Error objects with cause chains.
168
- */
169
- call: FunctionMapType<T>;
170
- };
171
- type ReturnFixed<A extends TaskInput = undefined, B extends Args = undefined, AS extends AbortSignalOption = undefined> = FixPoint<A, B, AS> & SecondPart & {
172
- createPool: (options?: CreatePool) => SingleTaskPool<A, B, AS>;
173
- };
174
- type External = unknown;
175
- type Inliner = {
176
- position?: "first" | "last";
177
- /**
178
- * Inline tasks per event loop tick.
179
- * Defaults to 1 when inliner is enabled.
180
- */
181
- batchSize?: number;
182
- /**
183
- * Minimum in-flight calls before routing can use the inline host lane.
184
- * Defaults to 1 (inline lane available immediately).
185
- */
186
- dispatchThreshold?: number;
187
- };
188
- type BalancerStrategy = "roundRobin" | "robinRound" | "firstIdle" | "randomLane" | "firstIdleOrRandom";
189
- type Balancer = BalancerStrategy | {
190
- /**
191
- * Optional. Defaults to "roundRobin".
192
- */
193
- strategy?: BalancerStrategy;
194
- };
195
- /** Debug namespaces for host setup, worker state, imports, globals, and lifecycle. */
196
- type DebugNamespace = "host" | "globals" | "signals" | "imports" | "lifecycle";
197
- type DebugFlags = {
198
- [Namespace in DebugNamespace]?: boolean;
199
- };
200
- /** Pass `true` for all debug, or enable namespaces by name. */
201
- type DebugOptions = boolean | DebugFlags;
202
- type WorkerBootstrapContext = {
203
- readonly thread: number;
204
- readonly totalNumberOfThread: number;
205
- readonly runtime: "node" | "deno" | "bun" | "unknown";
206
- };
207
- type WorkerBootstrapFunction<Data = unknown> = (data: Data, context: WorkerBootstrapContext) => MaybePromise<void>;
208
- type WorkerBootstrapOptions<Data = unknown> = {
209
- /**
210
- * Module imported inside the worker before task modules are imported.
211
- * Relative paths are resolved from the `createPool(...)` caller.
212
- */
213
- href: string;
214
- /**
215
- * Exported bootstrap function name. Defaults to `"default"`.
216
- */
217
- name?: string;
218
- /**
219
- * Structured data passed to the bootstrap function.
220
- */
221
- data?: Data;
222
- };
223
- type WorkerSettings = {
224
- resolveAfterFinishingAll?: true;
225
- /**
226
- * Privileged async worker hook that runs once before task modules import.
227
- * Use it to shape the worker environment before user task code loads.
228
- */
229
- bootstrap?: WorkerBootstrapOptions;
230
- /**
231
- * Experimental worker runtime.
232
- * "thread" uses Worker/worker_threads. "process" spawns another JavaScript
233
- * runtime, useful for process isolation, bwrap, and containers.
234
- */
235
- runtime?: "thread" | "process";
236
- /**
237
- * Runtime executable to use when runtime is "process". Defaults to "deno".
238
- */
239
- processRuntime?: "bun" | "deno" | "node";
240
- /**
241
- * Command argv to prepend before the process worker runtime command.
242
- * Useful for wrappers such as systemd-run, cgexec, nice, taskset, or
243
- * docker. Knitting appends the runtime command after this prefix.
244
- *
245
- * Example:
246
- * ["systemd-run", "--scope", "-p", "MemoryMax=500M", "-p", "CPUQuota=25%"]
247
- *
248
- * With containers, use processSharedMemory: "named", share the IPC
249
- * namespace, mount the worker files at the same path, and forward
250
- * KNITTING_PROCESS_WORKER plus KNITTING_PROCESS_WORKER_BOOT.
251
- */
252
- processCommandPrefix?: string[];
253
- /**
254
- * How process workers discover their shared-memory control channel.
255
- *
256
- * "inherit" keeps the POSIX fd-inheritance path and is the default outside
257
- * Windows. "named" creates an OS-named shared-memory object that wrappers
258
- * such as containers can reopen by name when they share the same IPC
259
- * namespace.
260
- */
261
- processSharedMemory?: ProcessSharedMemoryMode | ProcessSharedMemorySettings;
262
- timers?: WorkerTimers;
263
- /**
264
- * Hard task execution timeout in milliseconds.
265
- * When exceeded, the pool is force-shutdown to stop runaway CPU tasks.
266
- */
267
- hardTimeoutMs?: number;
268
- };
269
- type ProcessSharedMemoryMode = "inherit" | "named";
270
- type ProcessSharedMemorySettings = {
271
- mode?: ProcessSharedMemoryMode;
272
- namePrefix?: string;
273
- unlinkOnShutdown?: boolean;
274
- };
275
- type WorkerTimers = {
276
- /**
277
- * Busy-spin budget before parking (microseconds).
278
- */
279
- spinMicroseconds?: number;
280
- /**
281
- * Atomics.wait timeout when parked (milliseconds).
282
- */
283
- parkMs?: number;
284
- /**
285
- * Atomics.pause duration during spin (nanoseconds).
286
- * Set to 0 (or less) to disable pause calls.
287
- */
288
- pauseNanoseconds?: number;
289
- };
290
- type DispatcherSettings = {
291
- /**
292
- * How many immediate notify loops before backoff kicks in.
293
- */
294
- stallFreeLoops?: number;
295
- /**
296
- * Max backoff delay (milliseconds).
297
- */
298
- maxBackoffMs?: number;
299
- };
300
- type CreatePool = {
301
- /** Number of workers. Default: 1. */
302
- threads?: number;
303
- /** Add a host inline lane for regular tasks. Imported tasks still use workers. */
304
- inliner?: Inliner;
305
- balancer?: Balancer;
306
- worker?: WorkerSettings;
307
- /**
308
- * Payload transport settings. Default dynamic payload cap is about 8 MiB
309
- * (`payloadMaxByteLength >> 3`, with a 64 MiB growth cap).
310
- */
311
- payload?: PayloadBufferOptions;
312
- /**
313
- * Experimental unsafe options.
314
- */
315
- unsafe?: UnsafeOptions;
316
- /**
317
- * Abort-aware signal pool capacity.
318
- * Defaults to `258`.
319
- */
320
- abortSignalCapacity?: number;
321
- /**
322
- * Host dispatcher backoff and scheduling options.
323
- */
324
- host?: DispatcherSettings;
325
- /**
326
- * Extra Node.js execArgv flags for worker threads (e.g. ["--expose-gc"]).
327
- * Defaults to process.execArgv plus "--expose-gc" when allowed.
328
- */
329
- workerExecArgv?: string[];
330
- /**
331
- * Runtime permission protocol.
332
- * Omit to use strict defaults with `allowImport: true`; worker console is
333
- * quiet unless `permission: { console: true }`.
334
- *
335
- * Task code cannot terminate the host: process/Deno exit APIs are blocked.
336
- * Use `"strict"` (default for object mode) or `"unsafe"`.
337
- * Accepts object form for fine-grained permission controls.
338
- */
339
- permission?: PermissionProtocolInput;
340
- debug?: DebugOptions;
341
- source?: string;
342
- };
343
- export type { AbortSignalConfig as AbortSignalConfig, AbortSignalMethods as AbortSignalMethods, AbortSignalOption as AbortSignalOption, AbortSignalToolkit as AbortSignalToolkit, Args as Args, Balancer as Balancer, BalancerStrategy as BalancerStrategy, Composed as Composed, ComposedWithKey as ComposedWithKey, CreateContext as CreateContext, CreatePool as CreatePool, DebugNamespace as DebugNamespace, DebugOptions as DebugOptions, DispatcherSettings as DispatcherSettings, Envelope as Envelope, EnvelopeBody as EnvelopeBody, EnvelopeHeader as EnvelopeHeader, External as External, FixPoint as FixPoint, FunctionMapType as FunctionMapType, ImportTaskOptions as ImportTaskOptions, Inliner as Inliner, LockBuffers as LockBuffers, LockBufferTextCompat as LockBufferTextCompat, MaybePromise as MaybePromise, PayloadBufferMode as PayloadBufferMode, PayloadBufferOptions as PayloadBufferOptions, PermissionProtocol as PermissionProtocol, PermissionProtocolInput as PermissionProtocolInput, Pool as Pool, ProcessSharedMemoryMode as ProcessSharedMemoryMode, ProcessSharedMemorySettings as ProcessSharedMemorySettings, ResolvedPermissionProtocol as ResolvedPermissionProtocol, ReturnFixed as ReturnFixed, SecondPart as SecondPart, SharedBufferRegion as SharedBufferRegion, SharedBufferSource as SharedBufferSource, SharedBufferTextCompat as SharedBufferTextCompat, SingleTaskPool as SingleTaskPool, TaskFn as TaskFn, TaskInput as TaskInput, tasks as tasks, TaskTimeout as TaskTimeout, ValidInput as ValidInput, WorkerBootstrapContext as WorkerBootstrapContext, WorkerBootstrapFunction as WorkerBootstrapFunction, WorkerBootstrapOptions as WorkerBootstrapOptions, WorkerCall as WorkerCall, WorkerContext as WorkerContext, WorkerData as WorkerData, WorkerInvoke as WorkerInvoke, WorkerSettings as WorkerSettings, WorkerTimers as WorkerTimers, };
344
- export type { Task as Task } from "./memory/lock.js";
345
- export { LockBound as LockBound, PayloadBuffer as PayloadBuffer, PayloadSignal as PayloadSignal, TaskIndex as TaskIndex, } from "./memory/lock.js";
346
- export type { RegisterMalloc as RegisterMalloc } from "./memory/regionRegistry.js";
package/src/types.js DELETED
@@ -1,2 +0,0 @@
1
- import { endpointSymbol } from "./common/task-symbol.js";
2
- export { LockBound as LockBound, PayloadBuffer as PayloadBuffer, PayloadSignal as PayloadSignal, TaskIndex as TaskIndex, } from "./memory/lock.js";