knitting 0.1.56 → 0.1.60

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 (77) hide show
  1. package/README.md +254 -40
  2. package/knitting.d.ts +3 -2
  3. package/knitting.js +3 -3
  4. package/map.md +21 -9
  5. package/package.json +7 -1
  6. package/scripts/build-compiled-worker.ts +276 -0
  7. package/scripts/compiled-worker/porffor.d.ts +5 -0
  8. package/scripts/compiled-worker/runtime.ts +529 -0
  9. package/scripts/compiled-worker/task-shim.ts +32 -0
  10. package/src/api.d.ts +24 -1
  11. package/src/api.js +199 -20
  12. package/src/common/module-url.d.ts +1 -0
  13. package/src/common/module-url.js +30 -2
  14. package/src/common/node-compat.d.ts +2 -0
  15. package/src/common/path-canonical.js +20 -6
  16. package/src/common/runtime.d.ts +2 -1
  17. package/src/common/runtime.js +18 -4
  18. package/src/common/task-source.d.ts +2 -0
  19. package/src/common/task-source.js +17 -1
  20. package/src/common/worker-runtime.js +16 -7
  21. package/src/connections/buffer-reference-native.js +25 -1
  22. package/src/connections/buffer-reference.d.ts +23 -2
  23. package/src/connections/buffer-reference.js +143 -29
  24. package/src/connections/bun.d.ts +1 -1
  25. package/src/connections/bun.js +25 -14
  26. package/src/connections/deno.d.ts +3 -1
  27. package/src/connections/deno.js +35 -14
  28. package/src/connections/external-array-buffer.d.ts +2 -0
  29. package/src/connections/external-array-buffer.js +33 -0
  30. package/src/connections/node-addons.d.ts +11 -0
  31. package/src/connections/node-addons.js +49 -1
  32. package/src/connections/node-ffi-api.d.ts +19 -0
  33. package/src/connections/node-ffi-api.js +28 -0
  34. package/src/connections/node-ffi.d.ts +27 -0
  35. package/src/connections/node-ffi.js +262 -0
  36. package/src/connections/node.d.ts +1 -1
  37. package/src/connections/node.js +20 -20
  38. package/src/connections/package-assets.js +25 -15
  39. package/src/connections/posix.d.ts +42 -0
  40. package/src/connections/posix.js +101 -0
  41. package/src/connections/process-shared-buffer.d.ts +1 -0
  42. package/src/connections/process-shared-buffer.js +11 -45
  43. package/src/connections/types.d.ts +4 -0
  44. package/src/connections/windows.d.ts +19 -10
  45. package/src/connections/windows.js +87 -66
  46. package/src/error.d.ts +7 -6
  47. package/src/error.js +8 -7
  48. package/src/memory/lock.d.ts +87 -77
  49. package/src/memory/lock.js +103 -84
  50. package/src/memory/payloadCodec.d.ts +11 -2
  51. package/src/memory/payloadCodec.js +127 -31
  52. package/src/permission/compatibility.d.ts +28 -0
  53. package/src/permission/compatibility.js +239 -0
  54. package/src/permission/index.d.ts +2 -0
  55. package/src/permission/index.js +1 -0
  56. package/src/permission/protocol.d.ts +1 -0
  57. package/src/permission/protocol.js +54 -14
  58. package/src/runtime/compiled-artifact.d.ts +21 -0
  59. package/src/runtime/compiled-artifact.js +260 -0
  60. package/src/runtime/compiled-builder.d.ts +8 -0
  61. package/src/runtime/compiled-builder.js +42 -0
  62. package/src/runtime/compiled-worker.d.ts +27 -0
  63. package/src/runtime/compiled-worker.js +562 -0
  64. package/src/runtime/inline-executor.js +27 -15
  65. package/src/runtime/pool.d.ts +7 -2
  66. package/src/runtime/pool.js +74 -6
  67. package/src/runtime/process-worker.d.ts +1 -1
  68. package/src/runtime/process-worker.js +48 -8
  69. package/src/runtime/tx-queue.d.ts +3 -1
  70. package/src/runtime/tx-queue.js +4 -1
  71. package/src/types.d.ts +48 -6
  72. package/src/worker/composable-runners.js +3 -2
  73. package/src/worker/loop.d.ts +1 -0
  74. package/src/worker/loop.js +25 -12
  75. package/src/worker/task-loader.d.ts +5 -4
  76. package/src/worker/task-loader.js +13 -7
  77. package/src/worker/timers.js +8 -6
@@ -1,10 +1,8 @@
1
- import { getNodeBuiltinModule } from "../common/node-compat.js";
2
1
  import { FileDescriptor, } from "./file-descriptor.js";
3
2
  import { RUNTIME } from "../common/runtime.js";
4
3
  import { createBunConnectionPrimitives } from "./bun.js";
5
4
  import { createDenoConnectionPrimitives } from "./deno.js";
6
- import { loadNodeNativeAddon } from "./node-addons.js";
7
- import { expectFd, expectPositiveSize, readCreateMode, readCreateName, readRequiredCreateName, readCreateSize, } from "./types.js";
5
+ import { createNodeConnectionPrimitives } from "./node.js";
8
6
  export const PROCESS_SHARED_BUFFER_BRAND = Symbol.for("knitting.processSharedBuffer");
9
7
  export const PROCESS_SHARED_BUFFER_NUMERIC_TRANSFER = Symbol.for("knitting.processSharedBuffer.numericTransfer");
10
8
  const EXTERNAL_PAYLOAD_BRAND = Symbol.for("knitting.payloadCodec");
@@ -39,51 +37,15 @@ const decodeKind = (value) => {
39
37
  }
40
38
  };
41
39
  let defaultPrimitives;
42
- const fromDefaultNodeNativeMapping = (mapped) => {
43
- const mapping = {
44
- runtime: "node",
45
- fd: mapped.fd,
46
- size: mapped.size,
47
- byteLength: mapped.sab.byteLength,
48
- buffer: mapped.sab,
49
- kind: "shared-array-buffer",
50
- sab: mapped.sab,
51
- baseAddressMod64: mapped.baseAddressMod64,
52
- };
53
- if (mapped.name !== undefined && mapped.name.length > 0) {
54
- mapping.name = mapped.name;
55
- }
56
- return mapping;
57
- };
58
- const createDefaultNodePrimitives = () => {
59
- const nodeModule = getNodeBuiltinModule("node:module");
60
- if (nodeModule === undefined) {
61
- throw new TypeError("ProcessSharedBuffer needs connection primitives in this runtime");
62
- }
63
- const require = nodeModule.createRequire(import.meta.url);
64
- const addon = loadNodeNativeAddon(require, "knitting_shared_memory");
65
- return {
66
- createSharedMemory: (options) => {
67
- const size = expectPositiveSize(readCreateSize(options));
68
- const mode = readCreateMode(options);
69
- const name = mode === "anonymous"
70
- ? readCreateName(options, "knitting_shared_memory")
71
- : readRequiredCreateName(options);
72
- return fromDefaultNodeNativeMapping(addon.createSharedMemory(size, name, mode));
73
- },
74
- mapSharedMemory: (options) => {
75
- const fd = expectFd(options.fd);
76
- const size = expectPositiveSize(options.size);
77
- return fromDefaultNodeNativeMapping(addon.mapSharedMemory(fd, size, options.name));
78
- },
79
- };
80
- };
40
+ const processSharedBufferInstances = new WeakSet();
81
41
  const createDefaultPrimitives = () => {
82
42
  if (RUNTIME === "bun")
83
43
  return createBunConnectionPrimitives();
84
44
  if (RUNTIME === "deno")
85
45
  return createDenoConnectionPrimitives();
86
- return createDefaultNodePrimitives();
46
+ if (RUNTIME === "node")
47
+ return createNodeConnectionPrimitives();
48
+ throw new TypeError("ProcessSharedBuffer needs connection primitives in this runtime");
87
49
  };
88
50
  export const setDefaultProcessSharedBufferPrimitives = (primitives) => {
89
51
  defaultPrimitives = primitives;
@@ -136,6 +98,7 @@ export class ProcessSharedBuffer {
136
98
  this.descriptor = descriptor;
137
99
  this.byteOffset = byteOffset;
138
100
  this.byteLength = byteLength;
101
+ processSharedBufferInstances.add(this);
139
102
  }
140
103
  static create(options, creator = getDefaultProcessSharedBufferPrimitives()) {
141
104
  return ProcessSharedBuffer.fromMapping(creator.createSharedMemory(options));
@@ -251,6 +214,9 @@ export class ProcessSharedBuffer {
251
214
  return this.stringify();
252
215
  }
253
216
  }
217
+ export const isProcessSharedBufferValue = (value) => value !== null &&
218
+ typeof value === "object" &&
219
+ processSharedBufferInstances.has(value);
254
220
  export const parseProcessSharedBufferMetadata = (input) => {
255
221
  const value = typeof input === "string" ? JSON.parse(input) : input;
256
222
  if (!isRecord(value)) {
@@ -271,8 +237,8 @@ export const parseProcessSharedBufferMetadata = (input) => {
271
237
  };
272
238
  };
273
239
  const processSharedBufferGlobal = globalThis;
274
- const codecs = processSharedBufferGlobal.__KNITTING_PAYLOAD_CODECS__ ??=
275
- Object.create(null);
240
+ const codecs = processSharedBufferGlobal.__KNITTING_PAYLOAD_CODECS__ ??= Object
241
+ .create(null);
276
242
  codecs[PROCESS_SHARED_BUFFER_CODEC_ID] = {
277
243
  decode: (metadata) => ProcessSharedBuffer.fromMetadata(metadata),
278
244
  decodeNumeric: (metadata) => ProcessSharedBuffer[PROCESS_SHARED_BUFFER_NUMERIC_TRANSFER](metadata),
@@ -31,6 +31,10 @@ export type MapSharedMemoryOptions = {
31
31
  fd: number;
32
32
  name?: string;
33
33
  size: number;
34
+ /**
35
+ * When false, map the supplied descriptor directly. Ownership transfers to
36
+ * the returned mapping only after mapping succeeds; failures leave it open.
37
+ */
34
38
  duplicateFd?: boolean;
35
39
  };
36
40
  export type SharedMemoryConnectionPrimitives<Mapping extends SharedMemoryMapping = SharedMemoryMapping> = {
@@ -1,28 +1,37 @@
1
+ import { type NodeFfiLibrary } from "./node-ffi-api.js";
1
2
  import { type CreateSharedMemoryOptions, type MapSharedMemoryOptions, type SharedMemoryConnectionPrimitives, type SharedMemoryMapping } from "./types.js";
3
+ declare const WINDOWS_CREATE_SHARED_MEMORY = "knitting_windows_create_shared_memory";
4
+ declare const WINDOWS_MAP_SHARED_MEMORY = "knitting_windows_map_shared_memory";
5
+ declare const WINDOWS_CLOSE_SHARED_MEMORY = "knitting_windows_close_shared_memory";
6
+ type WindowsSharedMemorySymbols = {
7
+ [WINDOWS_CREATE_SHARED_MEMORY]: (size: bigint, name: Uint16Array, mode: number, out: Uint8Array) => number;
8
+ [WINDOWS_MAP_SHARED_MEMORY]: (handle: bigint, size: bigint, name: Uint16Array, out: Uint8Array) => number;
9
+ [WINDOWS_CLOSE_SHARED_MEMORY]: (mapping: Uint8Array) => number;
10
+ };
2
11
  type DenoWindowsLibrary = {
3
- symbols: {
4
- knitting_windows_create_shared_memory: (size: bigint, name: Uint16Array, mode: number, out: Uint8Array) => number;
5
- knitting_windows_map_shared_memory: (handle: bigint, size: bigint, name: Uint16Array, out: Uint8Array) => number;
6
- knitting_windows_close_shared_memory: (mapping: Uint8Array) => number;
7
- };
12
+ symbols: WindowsSharedMemorySymbols;
8
13
  close: () => void;
9
14
  };
10
15
  type BunWindowsLibrary = {
11
- symbols: {
12
- knitting_windows_create_shared_memory: (size: bigint, name: Uint16Array, mode: number, out: Uint8Array) => number;
13
- knitting_windows_map_shared_memory: (handle: bigint, size: bigint, name: Uint16Array, out: Uint8Array) => number;
14
- knitting_windows_close_shared_memory: (mapping: Uint8Array) => number;
15
- };
16
+ symbols: WindowsSharedMemorySymbols;
17
+ };
18
+ type NodeWindowsLibrary = {
19
+ functions: WindowsSharedMemorySymbols;
20
+ lib: NodeFfiLibrary;
16
21
  };
17
22
  export declare const isWindowsRuntime: () => boolean;
18
23
  export declare const windowsSharedMemoryPrebuildPath: () => string;
19
24
  export declare const makeWindowsAnonymousSharedMemoryName: (runtime: string) => string;
20
25
  export declare const openDenoWindowsSharedMemoryLibrary: () => DenoWindowsLibrary;
21
26
  export declare const openBunWindowsSharedMemoryLibrary: () => BunWindowsLibrary;
27
+ export declare const openNodeWindowsSharedMemoryLibrary: () => NodeWindowsLibrary;
22
28
  export declare const createDenoWindowsSharedMemory: (options: number | CreateSharedMemoryOptions, lib?: DenoWindowsLibrary) => SharedMemoryMapping<ArrayBuffer>;
23
29
  export declare const mapDenoWindowsSharedMemory: (options: MapSharedMemoryOptions, lib?: DenoWindowsLibrary) => SharedMemoryMapping<ArrayBuffer>;
24
30
  export declare const createDenoWindowsConnectionPrimitives: (lib?: DenoWindowsLibrary) => SharedMemoryConnectionPrimitives<SharedMemoryMapping<ArrayBuffer>>;
25
31
  export declare const createBunWindowsSharedMemory: (options: number | CreateSharedMemoryOptions, lib?: BunWindowsLibrary) => SharedMemoryMapping<ArrayBuffer>;
26
32
  export declare const mapBunWindowsSharedMemory: (options: MapSharedMemoryOptions, lib?: BunWindowsLibrary) => SharedMemoryMapping<ArrayBuffer>;
27
33
  export declare const createBunWindowsConnectionPrimitives: (lib?: BunWindowsLibrary) => SharedMemoryConnectionPrimitives<SharedMemoryMapping<ArrayBuffer>>;
34
+ export declare const createNodeWindowsSharedMemory: (options: number | CreateSharedMemoryOptions, lib?: NodeWindowsLibrary) => SharedMemoryMapping<ArrayBuffer>;
35
+ export declare const mapNodeWindowsSharedMemory: (options: MapSharedMemoryOptions, lib?: NodeWindowsLibrary) => SharedMemoryMapping<ArrayBuffer>;
36
+ export declare const createNodeWindowsConnectionPrimitives: (lib?: NodeWindowsLibrary) => SharedMemoryConnectionPrimitives<SharedMemoryMapping<ArrayBuffer>>;
28
37
  export {};
@@ -1,4 +1,6 @@
1
1
  import { resolveKnittingPackageAsset } from "./package-assets.js";
2
+ import { requireDetachedExternalArrayBuffer } from "./external-array-buffer.js";
3
+ import { getNodeFfi, } from "./node-ffi-api.js";
2
4
  import { expectFd, expectPositiveSize, readCreateMode, readCreateSize, readRequiredCreateName, } from "./types.js";
3
5
  const WINDOWS_CREATE_MODE_CREATE = 1;
4
6
  const WINDOWS_CREATE_MODE_OPEN = 2;
@@ -7,6 +9,9 @@ const POINTER_OFFSET = 0;
7
9
  const HANDLE_OFFSET = 8;
8
10
  const SIZE_OFFSET = 16;
9
11
  const BASE_ADDRESS_MOD_64_OFFSET = 24;
12
+ const WINDOWS_CREATE_SHARED_MEMORY = "knitting_windows_create_shared_memory";
13
+ const WINDOWS_MAP_SHARED_MEMORY = "knitting_windows_map_shared_memory";
14
+ const WINDOWS_CLOSE_SHARED_MEMORY = "knitting_windows_close_shared_memory";
10
15
  const FFIType = {
11
16
  i32: 5,
12
17
  u32: 6,
@@ -105,34 +110,59 @@ const checkWindowsResult = (result, message) => {
105
110
  }
106
111
  };
107
112
  const createRecord = () => new Uint8Array(WINDOWS_MAPPING_RECORD_BYTES);
108
- export const openDenoWindowsSharedMemoryLibrary = () => getDeno().dlopen(windowsSharedMemoryPrebuildPath(), {
109
- knitting_windows_create_shared_memory: {
113
+ const DENO_WINDOWS_SYMBOLS = {
114
+ [WINDOWS_CREATE_SHARED_MEMORY]: {
110
115
  parameters: ["u64", "buffer", "u32", "buffer"],
111
116
  result: "i32",
112
117
  },
113
- knitting_windows_map_shared_memory: {
118
+ [WINDOWS_MAP_SHARED_MEMORY]: {
114
119
  parameters: ["u64", "u64", "buffer", "buffer"],
115
120
  result: "i32",
116
121
  },
117
- knitting_windows_close_shared_memory: {
122
+ [WINDOWS_CLOSE_SHARED_MEMORY]: {
118
123
  parameters: ["buffer"],
119
124
  result: "i32",
120
125
  },
121
- });
122
- export const openBunWindowsSharedMemoryLibrary = () => getBunFFI().dlopen(windowsSharedMemoryPrebuildPath(), {
123
- knitting_windows_create_shared_memory: {
126
+ };
127
+ const BUN_WINDOWS_SYMBOLS = {
128
+ [WINDOWS_CREATE_SHARED_MEMORY]: {
124
129
  args: [FFIType.i64, FFIType.ptr, FFIType.u32, FFIType.ptr],
125
130
  returns: FFIType.i32,
126
131
  },
127
- knitting_windows_map_shared_memory: {
132
+ [WINDOWS_MAP_SHARED_MEMORY]: {
128
133
  args: [FFIType.i64, FFIType.i64, FFIType.ptr, FFIType.ptr],
129
134
  returns: FFIType.i32,
130
135
  },
131
- knitting_windows_close_shared_memory: {
136
+ [WINDOWS_CLOSE_SHARED_MEMORY]: {
132
137
  args: [FFIType.ptr],
133
138
  returns: FFIType.i32,
134
139
  },
135
- });
140
+ };
141
+ const NODE_WINDOWS_SYMBOLS = {
142
+ [WINDOWS_CREATE_SHARED_MEMORY]: {
143
+ arguments: ["u64", "pointer", "u32", "pointer"],
144
+ return: "i32",
145
+ },
146
+ [WINDOWS_MAP_SHARED_MEMORY]: {
147
+ arguments: ["u64", "u64", "pointer", "pointer"],
148
+ return: "i32",
149
+ },
150
+ [WINDOWS_CLOSE_SHARED_MEMORY]: {
151
+ arguments: ["pointer"],
152
+ return: "i32",
153
+ },
154
+ };
155
+ export const openDenoWindowsSharedMemoryLibrary = () => getDeno().dlopen(windowsSharedMemoryPrebuildPath(), DENO_WINDOWS_SYMBOLS);
156
+ export const openBunWindowsSharedMemoryLibrary = () => getBunFFI().dlopen(windowsSharedMemoryPrebuildPath(), BUN_WINDOWS_SYMBOLS);
157
+ let cachedNodeWindowsLibrary;
158
+ export const openNodeWindowsSharedMemoryLibrary = () => {
159
+ if (cachedNodeWindowsLibrary !== undefined) {
160
+ return cachedNodeWindowsLibrary;
161
+ }
162
+ const opened = getNodeFfi().dlopen(windowsSharedMemoryPrebuildPath(), NODE_WINDOWS_SYMBOLS);
163
+ cachedNodeWindowsLibrary = opened;
164
+ return opened;
165
+ };
136
166
  const closeWindowsMapping = (record, closeSharedMemory) => {
137
167
  let closed = false;
138
168
  return () => {
@@ -142,6 +172,19 @@ const closeWindowsMapping = (record, closeSharedMemory) => {
142
172
  checkWindowsResult(closeSharedMemory(record), "knitting_windows_close_shared_memory");
143
173
  };
144
174
  };
175
+ const makeWindowsMapping = (runtime, mapping, name, arrayBuffer, unsafePointer, close) => ({
176
+ runtime,
177
+ fd: fdFromHandle(mapping.handle),
178
+ name,
179
+ size: mapping.size,
180
+ byteLength: arrayBuffer.byteLength,
181
+ buffer: arrayBuffer,
182
+ kind: "external-array-buffer",
183
+ arrayBuffer,
184
+ unsafePointer,
185
+ baseAddressMod64: mapping.baseAddressMod64,
186
+ close,
187
+ });
145
188
  const fromDenoWindowsRecord = (record, name, lib) => {
146
189
  const mapping = readWindowsNativeMapping(record);
147
190
  const pointer = getDeno().UnsafePointer?.create(mapping.address);
@@ -150,19 +193,7 @@ const fromDenoWindowsRecord = (record, name, lib) => {
150
193
  }
151
194
  const arrayBuffer = new (getDeno().UnsafePointerView)(pointer)
152
195
  .getArrayBuffer(mapping.size);
153
- return {
154
- runtime: "deno",
155
- fd: fdFromHandle(mapping.handle),
156
- name,
157
- size: mapping.size,
158
- byteLength: arrayBuffer.byteLength,
159
- buffer: arrayBuffer,
160
- kind: "external-array-buffer",
161
- arrayBuffer,
162
- unsafePointer: pointer,
163
- baseAddressMod64: mapping.baseAddressMod64,
164
- close: closeWindowsMapping(record, lib.symbols.knitting_windows_close_shared_memory),
165
- };
196
+ return makeWindowsMapping("deno", mapping, name, arrayBuffer, pointer, closeWindowsMapping(record, lib.symbols[WINDOWS_CLOSE_SHARED_MEMORY]));
166
197
  };
167
198
  const fromBunWindowsRecord = (record, name, lib) => {
168
199
  const mapping = readWindowsNativeMapping(record);
@@ -170,55 +201,45 @@ const fromBunWindowsRecord = (record, name, lib) => {
170
201
  throw new RangeError("Windows mapping pointer is too large for Bun FFI");
171
202
  }
172
203
  const arrayBuffer = getBunFFI().toArrayBuffer(Number(mapping.address), 0, mapping.size);
173
- return {
174
- runtime: "bun",
175
- fd: fdFromHandle(mapping.handle),
176
- name,
177
- size: mapping.size,
178
- byteLength: arrayBuffer.byteLength,
179
- buffer: arrayBuffer,
180
- kind: "external-array-buffer",
181
- arrayBuffer,
182
- unsafePointer: Number(mapping.address),
183
- baseAddressMod64: mapping.baseAddressMod64,
184
- close: closeWindowsMapping(record, lib.symbols.knitting_windows_close_shared_memory),
185
- };
186
- };
187
- export const createDenoWindowsSharedMemory = (options, lib = openDenoWindowsSharedMemoryLibrary()) => {
188
- const size = expectPositiveSize(readCreateSize(options));
189
- const { mode, name } = readWindowsCreate(options, "deno");
190
- const record = createRecord();
191
- checkWindowsResult(lib.symbols.knitting_windows_create_shared_memory(BigInt(size), encodeWideCString(name), mode, record), "knitting_windows_create_shared_memory");
192
- return fromDenoWindowsRecord(record, name, lib);
204
+ return makeWindowsMapping("bun", mapping, name, arrayBuffer, Number(mapping.address), closeWindowsMapping(record, lib.symbols[WINDOWS_CLOSE_SHARED_MEMORY]));
193
205
  };
194
- export const mapDenoWindowsSharedMemory = (options, lib = openDenoWindowsSharedMemoryLibrary()) => {
195
- const fd = expectFd(options.fd);
196
- const size = expectPositiveSize(options.size);
197
- const record = createRecord();
198
- checkWindowsResult(lib.symbols.knitting_windows_map_shared_memory(BigInt(fd), BigInt(size), encodeWideCString(options.name), record), "knitting_windows_map_shared_memory");
199
- return fromDenoWindowsRecord(record, options.name, lib);
206
+ const fromNodeWindowsRecord = (record, name, lib) => {
207
+ const mapping = readWindowsNativeMapping(record);
208
+ const arrayBuffer = getNodeFfi().toArrayBuffer(mapping.address, mapping.size, false);
209
+ let closed = false;
210
+ return makeWindowsMapping("node", mapping, name, arrayBuffer, mapping.address, () => {
211
+ if (closed)
212
+ return;
213
+ requireDetachedExternalArrayBuffer(arrayBuffer);
214
+ checkWindowsResult(lib.functions[WINDOWS_CLOSE_SHARED_MEMORY](record), WINDOWS_CLOSE_SHARED_MEMORY);
215
+ closed = true;
216
+ });
200
217
  };
201
- export const createDenoWindowsConnectionPrimitives = (lib = openDenoWindowsSharedMemoryLibrary()) => ({
202
- runtime: "deno",
203
- createSharedMemory: (options) => createDenoWindowsSharedMemory(options, lib),
204
- mapSharedMemory: (options) => mapDenoWindowsSharedMemory(options, lib),
205
- });
206
- export const createBunWindowsSharedMemory = (options, lib = openBunWindowsSharedMemoryLibrary()) => {
218
+ const createWindowsSharedMemory = (options, runtime, lib, createSharedMemory, fromRecord) => {
207
219
  const size = expectPositiveSize(readCreateSize(options));
208
- const { mode, name } = readWindowsCreate(options, "bun");
220
+ const { mode, name } = readWindowsCreate(options, runtime);
209
221
  const record = createRecord();
210
- checkWindowsResult(lib.symbols.knitting_windows_create_shared_memory(BigInt(size), encodeWideCString(name), mode, record), "knitting_windows_create_shared_memory");
211
- return fromBunWindowsRecord(record, name, lib);
222
+ checkWindowsResult(createSharedMemory(BigInt(size), encodeWideCString(name), mode, record), WINDOWS_CREATE_SHARED_MEMORY);
223
+ return fromRecord(record, name, lib);
212
224
  };
213
- export const mapBunWindowsSharedMemory = (options, lib = openBunWindowsSharedMemoryLibrary()) => {
225
+ const mapWindowsSharedMemory = (options, lib, mapSharedMemory, fromRecord) => {
214
226
  const fd = expectFd(options.fd);
215
227
  const size = expectPositiveSize(options.size);
216
228
  const record = createRecord();
217
- checkWindowsResult(lib.symbols.knitting_windows_map_shared_memory(BigInt(fd), BigInt(size), encodeWideCString(options.name), record), "knitting_windows_map_shared_memory");
218
- return fromBunWindowsRecord(record, options.name, lib);
219
- };
220
- export const createBunWindowsConnectionPrimitives = (lib = openBunWindowsSharedMemoryLibrary()) => ({
221
- runtime: "bun",
222
- createSharedMemory: (options) => createBunWindowsSharedMemory(options, lib),
223
- mapSharedMemory: (options) => mapBunWindowsSharedMemory(options, lib),
229
+ checkWindowsResult(mapSharedMemory(BigInt(fd), BigInt(size), encodeWideCString(options.name), record), WINDOWS_MAP_SHARED_MEMORY);
230
+ return fromRecord(record, options.name, lib);
231
+ };
232
+ export const createDenoWindowsSharedMemory = (options, lib = openDenoWindowsSharedMemoryLibrary()) => createWindowsSharedMemory(options, "deno", lib, lib.symbols[WINDOWS_CREATE_SHARED_MEMORY], fromDenoWindowsRecord);
233
+ export const mapDenoWindowsSharedMemory = (options, lib = openDenoWindowsSharedMemoryLibrary()) => mapWindowsSharedMemory(options, lib, lib.symbols[WINDOWS_MAP_SHARED_MEMORY], fromDenoWindowsRecord);
234
+ const createWindowsConnectionPrimitives = (runtime, lib, createSharedMemory, mapSharedMemory) => ({
235
+ runtime,
236
+ createSharedMemory: (options) => createSharedMemory(options, lib),
237
+ mapSharedMemory: (options) => mapSharedMemory(options, lib),
224
238
  });
239
+ export const createDenoWindowsConnectionPrimitives = (lib = openDenoWindowsSharedMemoryLibrary()) => createWindowsConnectionPrimitives("deno", lib, createDenoWindowsSharedMemory, mapDenoWindowsSharedMemory);
240
+ export const createBunWindowsSharedMemory = (options, lib = openBunWindowsSharedMemoryLibrary()) => createWindowsSharedMemory(options, "bun", lib, lib.symbols[WINDOWS_CREATE_SHARED_MEMORY], fromBunWindowsRecord);
241
+ export const mapBunWindowsSharedMemory = (options, lib = openBunWindowsSharedMemoryLibrary()) => mapWindowsSharedMemory(options, lib, lib.symbols[WINDOWS_MAP_SHARED_MEMORY], fromBunWindowsRecord);
242
+ export const createBunWindowsConnectionPrimitives = (lib = openBunWindowsSharedMemoryLibrary()) => createWindowsConnectionPrimitives("bun", lib, createBunWindowsSharedMemory, mapBunWindowsSharedMemory);
243
+ export const createNodeWindowsSharedMemory = (options, lib = openNodeWindowsSharedMemoryLibrary()) => createWindowsSharedMemory(options, "node", lib, lib.functions[WINDOWS_CREATE_SHARED_MEMORY], fromNodeWindowsRecord);
244
+ export const mapNodeWindowsSharedMemory = (options, lib = openNodeWindowsSharedMemoryLibrary()) => mapWindowsSharedMemory(options, lib, lib.functions[WINDOWS_MAP_SHARED_MEMORY], fromNodeWindowsRecord);
245
+ export const createNodeWindowsConnectionPrimitives = (lib = openNodeWindowsSharedMemoryLibrary()) => createWindowsConnectionPrimitives("node", lib, createNodeWindowsSharedMemory, mapNodeWindowsSharedMemory);
package/src/error.d.ts CHANGED
@@ -1,10 +1,11 @@
1
1
  import { type PromisePayloadHandler, type Task } from "./memory/lock.js";
2
- export declare enum ErrorKnitting {
3
- Function = 0,
4
- Symbol = 1,
5
- Json = 2,
6
- Serializable = 3
7
- }
2
+ export declare const ErrorKnitting: {
3
+ readonly Function: 0;
4
+ readonly Symbol: 1;
5
+ readonly Json: 2;
6
+ readonly Serializable: 3;
7
+ };
8
+ export type ErrorKnitting = typeof ErrorKnitting[keyof typeof ErrorKnitting];
8
9
  export declare const encoderError: ({ task, type, onPromise, detail, }: {
9
10
  task: Task;
10
11
  type: ErrorKnitting;
package/src/error.js CHANGED
@@ -1,12 +1,13 @@
1
1
  import { beginPromisePayload, finishPromisePayload, TaskFlag, TaskIndex, } from "./memory/lock.js";
2
2
  import { RUNTIME_IS_MAIN_THREAD } from "./common/worker-runtime.js";
3
- export var ErrorKnitting;
4
- (function (ErrorKnitting) {
5
- ErrorKnitting[ErrorKnitting["Function"] = 0] = "Function";
6
- ErrorKnitting[ErrorKnitting["Symbol"] = 1] = "Symbol";
7
- ErrorKnitting[ErrorKnitting["Json"] = 2] = "Json";
8
- ErrorKnitting[ErrorKnitting["Serializable"] = 3] = "Serializable";
9
- })(ErrorKnitting || (ErrorKnitting = {}));
3
+ // const object, not `enum`: Andromeda's Nova engine can't parse `enum`. Same
4
+ // value access and emit on Node/Deno/Bun.
5
+ export const ErrorKnitting = {
6
+ Function: 0,
7
+ Symbol: 1,
8
+ Json: 2,
9
+ Serializable: 3,
10
+ };
10
11
  const reasonFrom = (task, type, detail) => {
11
12
  switch (type) {
12
13
  case ErrorKnitting.Function: {
@@ -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;
@@ -168,7 +177,7 @@ type ResolveHostOptions = {
168
177
  * - encode/decode are not re-entrant; payload codec uses a shared scratch buffer.
169
178
  */
170
179
  export type Lock2 = ReturnType<typeof lock2>;
171
- export declare const lock2: ({ headers, headerSlotStrideU32, LockBoundSector, payload, payloadConfig, payloadSector, textCompat, resultList, toSentList, recycleList, }: {
180
+ export declare const lock2: ({ headers, headerSlotStrideU32, LockBoundSector, payload, payloadConfig, payloadSector, textCompat, resultList, toSentList, recycleList, processBoundary, }: {
172
181
  headers?: SharedBufferSource;
173
182
  headerSlotStrideU32?: number;
174
183
  LockBoundSector?: SharedBufferSource;
@@ -179,6 +188,7 @@ export declare const lock2: ({ headers, headerSlotStrideU32, LockBoundSector, pa
179
188
  toSentList?: RingQueue<Task>;
180
189
  resultList?: RingQueue<Task>;
181
190
  recycleList?: RingQueue<Task>;
191
+ processBoundary?: boolean;
182
192
  }) => {
183
193
  enlist: (task: Task) => true;
184
194
  encode: (task: Task, state?: number) => boolean;