knitting 0.1.56 → 0.1.61

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 (101) hide show
  1. package/BROWSER.md +134 -0
  2. package/README.md +347 -46
  3. package/knitting.browser.d.ts +6 -0
  4. package/knitting.browser.js +1 -0
  5. package/knitting.d.ts +3 -2
  6. package/knitting.js +3 -3
  7. package/map.md +21 -9
  8. package/package.json +17 -2
  9. package/prebuilds/darwin-arm64-node-127/knitting_buffer_pointer.node +0 -0
  10. package/prebuilds/darwin-arm64-node-127/knitting_shared_memory.node +0 -0
  11. package/prebuilds/darwin-arm64-node-127/knitting_shm.node +0 -0
  12. package/prebuilds/darwin-arm64-node-137/knitting_buffer_pointer.node +0 -0
  13. package/prebuilds/darwin-arm64-node-137/knitting_shared_memory.node +0 -0
  14. package/prebuilds/darwin-arm64-node-137/knitting_shm.node +0 -0
  15. package/prebuilds/win32-x64/knitting_windows_shared_memory.dll +0 -0
  16. package/prebuilds/win32-x64-node-127/knitting_buffer_pointer.node +0 -0
  17. package/prebuilds/win32-x64-node-127/knitting_shared_memory.node +0 -0
  18. package/prebuilds/win32-x64-node-127/knitting_shm.node +0 -0
  19. package/prebuilds/win32-x64-node-137/knitting_buffer_pointer.node +0 -0
  20. package/prebuilds/win32-x64-node-137/knitting_shared_memory.node +0 -0
  21. package/prebuilds/win32-x64-node-137/knitting_shm.node +0 -0
  22. package/scripts/build-compiled-worker.ts +276 -0
  23. package/scripts/compiled-worker/porffor.d.ts +5 -0
  24. package/scripts/compiled-worker/runtime.ts +529 -0
  25. package/scripts/compiled-worker/task-shim.ts +32 -0
  26. package/src/api.d.ts +24 -1
  27. package/src/api.js +311 -27
  28. package/src/common/module-url.d.ts +1 -0
  29. package/src/common/module-url.js +30 -2
  30. package/src/common/node-compat.d.ts +2 -0
  31. package/src/common/path-canonical.js +20 -6
  32. package/src/common/runtime.d.ts +3 -1
  33. package/src/common/runtime.js +25 -4
  34. package/src/common/task-source.d.ts +2 -0
  35. package/src/common/task-source.js +17 -1
  36. package/src/common/worker-runtime.js +16 -7
  37. package/src/connections/buffer-reference-native.js +25 -1
  38. package/src/connections/buffer-reference.d.ts +23 -2
  39. package/src/connections/buffer-reference.js +143 -29
  40. package/src/connections/bun.d.ts +1 -1
  41. package/src/connections/bun.js +25 -14
  42. package/src/connections/deno.d.ts +3 -1
  43. package/src/connections/deno.js +35 -14
  44. package/src/connections/external-array-buffer.d.ts +2 -0
  45. package/src/connections/external-array-buffer.js +33 -0
  46. package/src/connections/node-addons.d.ts +11 -0
  47. package/src/connections/node-addons.js +49 -1
  48. package/src/connections/node-ffi-api.d.ts +19 -0
  49. package/src/connections/node-ffi-api.js +28 -0
  50. package/src/connections/node-ffi.d.ts +27 -0
  51. package/src/connections/node-ffi.js +262 -0
  52. package/src/connections/node.d.ts +1 -1
  53. package/src/connections/node.js +20 -20
  54. package/src/connections/package-assets.js +25 -15
  55. package/src/connections/posix.d.ts +42 -0
  56. package/src/connections/posix.js +101 -0
  57. package/src/connections/process-shared-buffer.d.ts +1 -0
  58. package/src/connections/process-shared-buffer.js +11 -45
  59. package/src/connections/types.d.ts +4 -0
  60. package/src/connections/windows.d.ts +19 -10
  61. package/src/connections/windows.js +87 -66
  62. package/src/error.d.ts +7 -6
  63. package/src/error.js +8 -7
  64. package/src/memory/lock.d.ts +112 -77
  65. package/src/memory/lock.js +314 -87
  66. package/src/memory/payloadCodec.d.ts +11 -2
  67. package/src/memory/payloadCodec.js +129 -37
  68. package/src/memory/regionRegistry.d.ts +1 -3
  69. package/src/memory/regionRegistry.js +16 -14
  70. package/src/permission/compatibility.d.ts +28 -0
  71. package/src/permission/compatibility.js +239 -0
  72. package/src/permission/index.d.ts +2 -0
  73. package/src/permission/index.js +1 -0
  74. package/src/permission/protocol.d.ts +1 -0
  75. package/src/permission/protocol.js +59 -15
  76. package/src/runtime/compiled-artifact.d.ts +21 -0
  77. package/src/runtime/compiled-artifact.js +260 -0
  78. package/src/runtime/compiled-builder.d.ts +8 -0
  79. package/src/runtime/compiled-builder.js +42 -0
  80. package/src/runtime/compiled-worker.d.ts +27 -0
  81. package/src/runtime/compiled-worker.js +562 -0
  82. package/src/runtime/dispatcher.js +9 -0
  83. package/src/runtime/inline-executor.js +27 -15
  84. package/src/runtime/pool.d.ts +121 -5
  85. package/src/runtime/pool.js +263 -49
  86. package/src/runtime/process-worker.d.ts +3 -13
  87. package/src/runtime/process-worker.js +97 -101
  88. package/src/runtime/tx-queue.d.ts +18 -2
  89. package/src/runtime/tx-queue.js +56 -16
  90. package/src/runtime/worker-common.d.ts +13 -0
  91. package/src/runtime/worker-common.js +102 -0
  92. package/src/types.d.ts +89 -9
  93. package/src/worker/composable-runners.js +3 -2
  94. package/src/worker/loop.d.ts +1 -0
  95. package/src/worker/loop.js +52 -17
  96. package/src/worker/rx-queue.d.ts +11 -1
  97. package/src/worker/rx-queue.js +4 -1
  98. package/src/worker/task-loader.d.ts +5 -4
  99. package/src/worker/task-loader.js +13 -7
  100. package/src/worker/timers.d.ts +8 -1
  101. package/src/worker/timers.js +28 -23
@@ -1,70 +1,77 @@
1
1
  import RingQueue from "../ipc/tools/ring-queue.js";
2
+ type EncodePayloadFactory = typeof import("./payloadCodec.js").encodePayload;
3
+ type DecodePayloadFactory = typeof import("./payloadCodec.js").decodePayload;
4
+ export declare const registerLockPayloadCodec: (encode: EncodePayloadFactory, decode: DecodePayloadFactory) => void;
2
5
  import { type SharedBufferSource } from "../common/shared-buffer-region.js";
3
6
  import { type LockBufferTextCompat } from "../common/shared-buffer-text.js";
4
7
  import { type PayloadBufferOptions } from "./payload-config.js";
5
8
  /**
6
9
  * TODO: Compose all the instance where the array is passed as argument
7
10
  */
8
- export declare enum PayloadSignal {
9
- UNREACHABLE = 0,
10
- BigInt = 2,
11
- True = 3,
12
- False = 4,
13
- Undefined = 5,
14
- NaN = 6,
15
- Float64 = 9,
16
- Null = 10
17
- }
18
- export declare enum PayloadBuffer {
19
- BORDER_SIGNAL_BUFFER = 11,
20
- String = 11,
21
- Json = 12,
22
- StaticString = 15,
23
- StaticJson = 16,
24
- Binary = 17,
25
- StaticBinary = 18,
26
- Int32Array = 19,
27
- Float64Array = 20,
28
- BigInt64Array = 21,
29
- BigUint64Array = 22,
30
- DataView = 23,
31
- Error = 24,
32
- Date = 25,
33
- Symbol = 26,
34
- StaticSymbol = 27,
35
- BigInt = 28,
36
- StaticBigInt = 29,
37
- StaticInt32Array = 31,
38
- StaticFloat64Array = 32,
39
- StaticBigInt64Array = 33,
40
- StaticBigUint64Array = 34,
41
- StaticDataView = 35,
42
- ArrayBuffer = 36,
43
- StaticArrayBuffer = 37,
44
- Buffer = 38,
45
- StaticBuffer = 39,
46
- EnvelopeStaticHeader = 40,
47
- EnvelopeDynamicHeader = 41,
48
- EnvelopeStaticHeaderString = 42,
49
- EnvelopeDynamicHeaderString = 43,
50
- ExternalPayload = 44,
51
- StaticExternalPayload = 45,
52
- ProcessSharedBuffer = 46,
53
- BufferReference = 47,
54
- SharedArrayBuffer = 48,
55
- EnvelopeStaticHeaderExternal = 49,
56
- EnvelopeDynamicHeaderExternal = 50,
57
- EnvelopeStaticHeaderStringExternal = 51,
58
- EnvelopeDynamicHeaderStringExternal = 52,
59
- NumericArray = 53,
60
- StaticNumericArray = 54
61
- }
62
- export declare enum LockBound {
63
- paddingLock = 0,
64
- padding = 0,
65
- slots = 32,
66
- header = 0
67
- }
11
+ export declare const PayloadSignal: {
12
+ readonly UNREACHABLE: 0;
13
+ readonly BigInt: 2;
14
+ readonly True: 3;
15
+ readonly False: 4;
16
+ readonly Undefined: 5;
17
+ readonly NaN: 6;
18
+ readonly Float64: 9;
19
+ readonly Null: 10;
20
+ };
21
+ export type PayloadSignal = typeof PayloadSignal[keyof typeof PayloadSignal];
22
+ export declare const PayloadBuffer: {
23
+ readonly BORDER_SIGNAL_BUFFER: 11;
24
+ readonly String: 11;
25
+ readonly Json: 12;
26
+ readonly StaticString: 15;
27
+ readonly StaticJson: 16;
28
+ readonly Binary: 17;
29
+ readonly StaticBinary: 18;
30
+ readonly Int32Array: 19;
31
+ readonly Float64Array: 20;
32
+ readonly BigInt64Array: 21;
33
+ readonly BigUint64Array: 22;
34
+ readonly DataView: 23;
35
+ readonly Error: 24;
36
+ readonly Date: 25;
37
+ readonly Symbol: 26;
38
+ readonly StaticSymbol: 27;
39
+ readonly BigInt: 28;
40
+ readonly StaticBigInt: 29;
41
+ readonly StaticInt32Array: 31;
42
+ readonly StaticFloat64Array: 32;
43
+ readonly StaticBigInt64Array: 33;
44
+ readonly StaticBigUint64Array: 34;
45
+ readonly StaticDataView: 35;
46
+ readonly ArrayBuffer: 36;
47
+ readonly StaticArrayBuffer: 37;
48
+ readonly Buffer: 38;
49
+ readonly StaticBuffer: 39;
50
+ readonly EnvelopeStaticHeader: 40;
51
+ readonly EnvelopeDynamicHeader: 41;
52
+ readonly EnvelopeStaticHeaderString: 42;
53
+ readonly EnvelopeDynamicHeaderString: 43;
54
+ readonly ExternalPayload: 44;
55
+ readonly StaticExternalPayload: 45;
56
+ readonly ProcessSharedBuffer: 46;
57
+ readonly BufferReference: 47;
58
+ readonly SharedArrayBuffer: 48;
59
+ readonly EnvelopeStaticHeaderExternal: 49;
60
+ readonly EnvelopeDynamicHeaderExternal: 50;
61
+ readonly EnvelopeStaticHeaderStringExternal: 51;
62
+ readonly EnvelopeDynamicHeaderStringExternal: 52;
63
+ readonly NumericArray: 53;
64
+ readonly StaticNumericArray: 54;
65
+ };
66
+ export type PayloadBuffer = typeof PayloadBuffer[keyof typeof PayloadBuffer];
67
+ export declare const payloadBufferName: (value: number) => string;
68
+ export declare const LockBound: {
69
+ readonly paddingLock: 0;
70
+ readonly padding: 0;
71
+ readonly slots: 32;
72
+ readonly header: 0;
73
+ };
74
+ export type LockBound = typeof LockBound[keyof typeof LockBound];
68
75
  export declare const LOCK_CACHE_LINE_BYTES = 64;
69
76
  export declare const LOCK_SECTOR_BYTES = 256;
70
77
  export type Task = [
@@ -92,33 +99,34 @@ export declare const resetTaskLocalFlags: (task: Task) => void;
92
99
  export declare const addTaskFinalizer: (task: Task, finalizer: () => void) => void;
93
100
  export declare const attachPayloadTransportFinalizer: (task: Task, value: unknown) => void;
94
101
  export declare const runTaskFinalizers: (task: Task) => void;
95
- export declare enum TaskIndex {
102
+ export declare const TaskIndex: {
96
103
  /**
97
104
  * Worker -> host response flags word.
98
105
  */
99
- FlagsToHost = 0,
106
+ readonly FlagsToHost: 0;
100
107
  /**
101
108
  * Host -> worker request function id (low 16 bits).
102
109
  * High 16 bits are reserved for caller metadata on request path.
103
110
  * NOTE: shares the same storage word as `FlagsToHost`.
104
111
  */
105
- FunctionID = 0,
106
- ID = 1,
107
- Type = 2,
108
- Start = 3,
109
- End = 4,
110
- PayloadLen = 5,
112
+ readonly FunctionID: 0;
113
+ readonly ID: 1;
114
+ readonly Type: 2;
115
+ readonly Start: 3;
116
+ readonly End: 4;
117
+ readonly PayloadLen: 5;
111
118
  /**
112
119
  * Low 5 bits: region slot index (0..31).
113
120
  * High 27 bits: reserved for caller metadata (e.g. enqueue timing).
114
121
  */
115
- slotBuffer = 6,
116
- Size = 8,
122
+ readonly slotBuffer: 6;
123
+ readonly Size: 8;
117
124
  /**
118
125
  * Total slot length in Uint32 words, including the task header.
119
126
  */
120
- TotalBuff = 144
121
- }
127
+ readonly TotalBuff: 144;
128
+ };
129
+ export type TaskIndex = typeof TaskIndex[keyof typeof TaskIndex];
122
130
  export declare const TASK_SLOT_INDEX_BITS = 5;
123
131
  export declare const TASK_SLOT_INDEX_MASK: number;
124
132
  export declare const TASK_SLOT_META_BITS: number;
@@ -135,11 +143,12 @@ export declare const getTaskSlotIndex: (task: ArrayLike<number>) => number;
135
143
  export declare const setTaskSlotIndex: (task: Task, slotIndex: number) => void;
136
144
  export declare const getTaskSlotMeta: (task: ArrayLike<number>) => number;
137
145
  export declare const setTaskSlotMeta: (task: Task, value: number) => void;
138
- export declare enum TaskFlag {
139
- Reject = 1
140
- }
146
+ export declare const TaskFlag: {
147
+ readonly Reject: 1;
148
+ };
149
+ export type TaskFlag = typeof TaskFlag[keyof typeof TaskFlag];
141
150
  export declare const LOCK_WORD_BYTES: number;
142
- export declare const LOCK_HOST_BITS_OFFSET_BYTES = LockBound.paddingLock;
151
+ export declare const LOCK_HOST_BITS_OFFSET_BYTES: 0;
143
152
  export declare const LOCK_WORKER_BITS_OFFSET_BYTES = 64;
144
153
  export declare const LOCK_SECTOR_BYTE_LENGTH = 256;
145
154
  export declare const PAYLOAD_LOCK_HOST_BITS_OFFSET_BYTES: number;
@@ -150,6 +159,20 @@ export declare const HEADER_SLOT_STRIDE_BYTES: number;
150
159
  export declare const HEADER_TASK_LINE_U32: number;
151
160
  export declare const HEADER_STATIC_PAYLOAD_U32: number;
152
161
  export declare const HEADER_TASK_OFFSET_IN_SLOT_U32: number;
162
+ export declare const STEAL_ACK_SLOT_OFFSET_U32: number;
163
+ export declare const STEAL_WANT_SLOT_OFFSET_U32: number;
164
+ /**
165
+ * The payload allocator needs its own participant-owned ACK per consumer, since
166
+ * several workers can free payload slots from one shared allocator. No intent
167
+ * word: freeing needs no arbitration, a consumer only frees what it drained.
168
+ */
169
+ export declare const STEAL_PAYLOAD_ACK_SLOT_OFFSET_U32: number;
170
+ /**
171
+ * Host-owned liveness mask for stealing consumers. A dead claimant may leave
172
+ * its WANT word set forever; survivors ignore WANT from consumers whose bit is
173
+ * clear here. This preserves single-writer ownership of every WANT word.
174
+ */
175
+ export declare const STEAL_LIVE_SLOT_OFFSET_U32: number;
153
176
  export declare const HEADER_U32_LENGTH: number;
154
177
  export declare const HEADER_BYTE_LENGTH: number;
155
178
  export declare const makeTask: () => Task;
@@ -168,7 +191,7 @@ type ResolveHostOptions = {
168
191
  * - encode/decode are not re-entrant; payload codec uses a shared scratch buffer.
169
192
  */
170
193
  export type Lock2 = ReturnType<typeof lock2>;
171
- export declare const lock2: ({ headers, headerSlotStrideU32, LockBoundSector, payload, payloadConfig, payloadSector, textCompat, resultList, toSentList, recycleList, }: {
194
+ export declare const lock2: ({ headers, headerSlotStrideU32, LockBoundSector, payload, payloadConfig, payloadSector, textCompat, resultList, toSentList, recycleList, processBoundary, consumers, consumerId, regionLanes, }: {
172
195
  headers?: SharedBufferSource;
173
196
  headerSlotStrideU32?: number;
174
197
  LockBoundSector?: SharedBufferSource;
@@ -179,6 +202,17 @@ export declare const lock2: ({ headers, headerSlotStrideU32, LockBoundSector, pa
179
202
  toSentList?: RingQueue<Task>;
180
203
  resultList?: RingQueue<Task>;
181
204
  recycleList?: RingQueue<Task>;
205
+ processBoundary?: boolean;
206
+ /**
207
+ * Number of consumer endpoints sharing this lock. `1` (default) keeps the
208
+ * classic single-consumer path. `> 1` enables region-Dekker work stealing and
209
+ * is only valid on a host->worker submit lock, never on a return lock.
210
+ */
211
+ consumers?: number;
212
+ /** This endpoint's id, `0..consumers-1`. Priority is fixed by id. */
213
+ consumerId?: number;
214
+ /** Lanes claimed per Dekker handshake. Paper rule: `slots / regionLanes >= consumers + 1`. */
215
+ regionLanes?: number;
182
216
  }) => {
183
217
  enlist: (task: Task) => true;
184
218
  encode: (task: Task, state?: number) => boolean;
@@ -197,6 +231,7 @@ export declare const lock2: ({ headers, headerSlotStrideU32, LockBoundSector, pa
197
231
  getPendingFrameCount: () => number;
198
232
  getPendingPromiseCount: () => number;
199
233
  resetPendingState: () => void;
234
+ deactivateStealConsumer: (id: number) => boolean;
200
235
  takeDeferredCount: () => number;
201
236
  setPromiseHandler: (handler?: PromisePayloadHandler) => void;
202
237
  };