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
@@ -9,10 +9,11 @@ type GetFunctionParams = {
9
9
  permission?: ResolvedPermissionProtocol;
10
10
  };
11
11
  type WorkerCallable = (args: unknown, abortToolkit?: unknown) => unknown;
12
- export declare const enum TimeoutKind {
13
- Reject = 0,
14
- Resolve = 1
15
- }
12
+ export declare const TimeoutKind: {
13
+ readonly Reject: 0;
14
+ readonly Resolve: 1;
15
+ };
16
+ export type TimeoutKind = typeof TimeoutKind[keyof typeof TimeoutKind];
16
17
  export type TimeoutSpec = {
17
18
  ms: number;
18
19
  kind: TimeoutKind;
@@ -1,27 +1,33 @@
1
1
  import { endpointSymbol } from "../common/task-symbol.js";
2
- import { toModuleUrl } from "../common/module-url.js";
2
+ import { toImportSpecifier, toModuleUrl } from "../common/module-url.js";
3
+ // const object, not `const enum`: Andromeda's Nova engine can't parse `enum`.
4
+ // No longer inlined, but only read on cold paths.
5
+ export const TimeoutKind = {
6
+ Reject: 0,
7
+ Resolve: 1,
8
+ };
3
9
  const normalizeTimeout = (timeout) => {
4
10
  if (timeout == null)
5
11
  return undefined;
6
12
  if (typeof timeout === "number") {
7
13
  const ms = Math.floor(timeout);
8
14
  return ms >= 0
9
- ? { ms, kind: 0 /* TimeoutKind.Reject */, value: new Error("Task timeout") }
15
+ ? { ms, kind: TimeoutKind.Reject, value: new Error("Task timeout") }
10
16
  : undefined;
11
17
  }
12
18
  const ms = Math.floor(timeout.time);
13
19
  if (!(ms >= 0))
14
20
  return undefined;
15
21
  if ("default" in timeout) {
16
- return { ms, kind: 1 /* TimeoutKind.Resolve */, value: timeout.default };
22
+ return { ms, kind: TimeoutKind.Resolve, value: timeout.default };
17
23
  }
18
24
  if (timeout.maybe === true) {
19
- return { ms, kind: 1 /* TimeoutKind.Resolve */, value: undefined };
25
+ return { ms, kind: TimeoutKind.Resolve, value: undefined };
20
26
  }
21
27
  if ("error" in timeout) {
22
- return { ms, kind: 0 /* TimeoutKind.Reject */, value: timeout.error };
28
+ return { ms, kind: TimeoutKind.Reject, value: timeout.error };
23
29
  }
24
- return { ms, kind: 0 /* TimeoutKind.Reject */, value: new Error("Task timeout") };
30
+ return { ms, kind: TimeoutKind.Reject, value: new Error("Task timeout") };
25
31
  };
26
32
  const composeWorkerCallable = (fixed, _permission) => {
27
33
  return fixed.f;
@@ -30,7 +36,7 @@ export const getFunctions = async ({ list, ids, names, at, permission }) => {
30
36
  const modules = list.map((specifier) => toModuleUrl(specifier));
31
37
  const nameSet = new Set(names);
32
38
  const results = await Promise.all(modules.map(async (imports) => {
33
- const module = (await import(imports));
39
+ const module = (await import(toImportSpecifier(imports)));
34
40
  const fixedTasks = Object.entries(module)
35
41
  .filter(([_, value]) => value != null && typeof value === "object" &&
36
42
  //@ts-ignore Reason -> trust me
@@ -1,7 +1,7 @@
1
- var Comment;
2
- (function (Comment) {
3
- Comment[Comment["thisIsAHint"] = 0] = "thisIsAHint";
4
- })(Comment || (Comment = {}));
1
+ // const object, not `enum`: Andromeda's Nova engine can't parse `enum`.
2
+ const Comment = {
3
+ thisIsAHint: 0,
4
+ };
5
5
  const maybeGc = (() => {
6
6
  const host = globalThis;
7
7
  const gc = typeof host.gc === "function"
@@ -51,6 +51,7 @@ const isPlainNodeWindows = runtimeGlobals.process?.platform === "win32" &&
51
51
  // runners appear to hang. The native wait already polls internally, so this is
52
52
  // just bounding each poll slice.
53
53
  const nativeWaitTimeoutMs = (parkMs) => isPlainNodeWindows ? 1 : Number.isFinite(parkMs) ? parkMs : Infinity;
54
+ const pollingWaitTimeoutMs = (parkMs) => Number.isFinite(parkMs) ? Math.min(Math.max(parkMs, 0), 1) : 1;
54
55
  export const whilePausing = ({ pauseInNanoseconds }) => {
55
56
  const forNanoseconds = pauseInNanoseconds ?? DEFAULT_PAUSE_TIME;
56
57
  if (!a_pause || forNanoseconds <= 0)
@@ -108,8 +109,9 @@ export const sleepUntilChanged = ({ at, opView, pauseInNanoseconds, rxStatus, tx
108
109
  }
109
110
  else if (a_wait && waitFallbackView) {
110
111
  // waitFallbackView is never notified — this wait only ever times out and
111
- // re-polls, so it must stay finite or the worker would hang forever.
112
- a_wait(waitFallbackView, 0, 0, Number.isFinite(parkMs) ? parkMs : Infinity);
112
+ // re-polls. Keep the slice short and finite so FFI-backed process workers
113
+ // stay responsive without a cross-process native wake primitive.
114
+ a_wait(waitFallbackView, 0, 0, pollingWaitTimeoutMs(parkMs));
113
115
  }
114
116
  a_store(rxStatus, 0, 1);
115
117
  };