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,29 +0,0 @@
1
- export type BufferLike = ArrayBuffer | SharedArrayBuffer | ArrayBufferView;
2
- export type NumberFormat = "f64" | "f32" | "i32";
3
- export type NumberBufferOptions = {
4
- format?: NumberFormat;
5
- };
6
- /** View ArrayBuffer/SharedArrayBuffer/typed-array bytes as Uint8Array. */
7
- export declare const bufferToBytes: (source: BufferLike) => Uint8Array;
8
- /** Copy bytes into a SharedArrayBuffer for zero-copy worker inputs. */
9
- export declare const bytesToBuffer: (source: BufferLike) => SharedArrayBuffer;
10
- /** Decode UTF-8 bytes from ArrayBuffer/SharedArrayBuffer/typed-array input. */
11
- export declare const bufferToString: (source: BufferLike) => string;
12
- /** Encode UTF-8 text into a SharedArrayBuffer. */
13
- export declare const stringToBuffer: (text: string) => SharedArrayBuffer;
14
- /** Decode JSON from UTF-8 bytes. */
15
- export declare const bufferToJson: <T = unknown>(source: BufferLike) => T;
16
- /** Encode JSON into a SharedArrayBuffer. */
17
- export declare const jsonToBuffer: (value: unknown) => SharedArrayBuffer;
18
- /** Encode numbers into a SharedArrayBuffer (`f64` by default). */
19
- export declare const numbersToBuffer: (numbers: ArrayLike<number>, options?: NumberBufferOptions) => SharedArrayBuffer;
20
- /** Decode numeric typed-array views from worker-friendly byte buffers. */
21
- export declare function bufferToNumbers(source: BufferLike, options?: {
22
- format?: "f64";
23
- }): Float64Array;
24
- export declare function bufferToNumbers(source: BufferLike, options: {
25
- format: "f32";
26
- }): Float32Array;
27
- export declare function bufferToNumbers(source: BufferLike, options: {
28
- format: "i32";
29
- }): Int32Array;
package/src/utils/http.js DELETED
@@ -1,100 +0,0 @@
1
- const nodeBuffer = globalThis.Buffer;
2
- const hasNodeBuffer = typeof nodeBuffer?.from === "function" &&
3
- typeof nodeBuffer?.byteLength === "function";
4
- const textEncoder = new TextEncoder();
5
- const textDecoder = new TextDecoder();
6
- const hasSharedArrayBuffer = typeof SharedArrayBuffer === "function";
7
- const isSharedArrayBuffer = (value) => hasSharedArrayBuffer && value instanceof SharedArrayBuffer;
8
- const BYTES_PER_ELEMENT = {
9
- f64: 8,
10
- f32: 4,
11
- i32: 4,
12
- };
13
- const toBytes = (source) => {
14
- if (source instanceof Uint8Array)
15
- return source;
16
- if (ArrayBuffer.isView(source)) {
17
- return new Uint8Array(source.buffer, source.byteOffset, source.byteLength);
18
- }
19
- if (source instanceof ArrayBuffer || isSharedArrayBuffer(source)) {
20
- return new Uint8Array(source);
21
- }
22
- throw new TypeError("Expected an ArrayBuffer, SharedArrayBuffer, or typed-array view.");
23
- };
24
- /** View ArrayBuffer/SharedArrayBuffer/typed-array bytes as Uint8Array. */
25
- export const bufferToBytes = (source) => toBytes(source);
26
- /** Copy bytes into a SharedArrayBuffer for zero-copy worker inputs. */
27
- export const bytesToBuffer = (source) => {
28
- const bytes = toBytes(source);
29
- const sab = new SharedArrayBuffer(bytes.byteLength);
30
- new Uint8Array(sab).set(bytes);
31
- return sab;
32
- };
33
- const makeNumberView = (format, buffer, byteOffset, length) => {
34
- switch (format) {
35
- case "f64":
36
- return new Float64Array(buffer, byteOffset, length);
37
- case "f32":
38
- return new Float32Array(buffer, byteOffset, length);
39
- case "i32":
40
- return new Int32Array(buffer, byteOffset, length);
41
- }
42
- };
43
- /** Decode UTF-8 bytes from ArrayBuffer/SharedArrayBuffer/typed-array input. */
44
- export const bufferToString = (source) => {
45
- const bytes = toBytes(source);
46
- if (hasNodeBuffer) {
47
- return nodeBuffer
48
- .from(bytes.buffer, bytes.byteOffset, bytes.byteLength)
49
- .toString("utf8");
50
- }
51
- return textDecoder.decode(bytes);
52
- };
53
- /** Encode UTF-8 text into a SharedArrayBuffer. */
54
- export const stringToBuffer = (text) => {
55
- if (typeof text !== "string") {
56
- throw new TypeError("stringToBuffer expects a string.");
57
- }
58
- if (hasNodeBuffer) {
59
- const byteLength = nodeBuffer.byteLength(text, "utf8");
60
- const sab = new SharedArrayBuffer(byteLength);
61
- nodeBuffer.from(sab).write(text, "utf8");
62
- return sab;
63
- }
64
- const encoded = textEncoder.encode(text);
65
- const sab = new SharedArrayBuffer(encoded.byteLength);
66
- new Uint8Array(sab).set(encoded);
67
- return sab;
68
- };
69
- /** Decode JSON from UTF-8 bytes. */
70
- export const bufferToJson = (source) => JSON.parse(bufferToString(source));
71
- /** Encode JSON into a SharedArrayBuffer. */
72
- export const jsonToBuffer = (value) => {
73
- const text = JSON.stringify(value);
74
- if (typeof text !== "string") {
75
- throw new TypeError("jsonToBuffer: value is not JSON-serializable (stringify returned undefined).");
76
- }
77
- return stringToBuffer(text);
78
- };
79
- /** Encode numbers into a SharedArrayBuffer (`f64` by default). */
80
- export const numbersToBuffer = (numbers, options) => {
81
- const format = options?.format ?? "f64";
82
- const sab = new SharedArrayBuffer(numbers.length * BYTES_PER_ELEMENT[format]);
83
- makeNumberView(format, sab, 0, numbers.length).set(numbers);
84
- return sab;
85
- };
86
- export function bufferToNumbers(source, options) {
87
- const format = options?.format ?? "f64";
88
- const bytesPer = BYTES_PER_ELEMENT[format];
89
- const bytes = toBytes(source);
90
- if (bytes.byteLength % bytesPer !== 0) {
91
- throw new RangeError(`bufferToNumbers: byte length ${bytes.byteLength} is not a multiple of ` +
92
- `${bytesPer} for format "${format}".`);
93
- }
94
- const length = bytes.byteLength / bytesPer;
95
- if (bytes.byteOffset % bytesPer === 0) {
96
- return makeNumberView(format, bytes.buffer, bytes.byteOffset, length);
97
- }
98
- const copy = bytes.slice();
99
- return makeNumberView(format, copy.buffer, 0, length);
100
- }
@@ -1,5 +0,0 @@
1
- export declare const runWorkerBootstrap: ({ bootstrap, thread, totalNumberOfThread, }: {
2
- bootstrap: unknown;
3
- thread: number;
4
- totalNumberOfThread: number;
5
- }) => Promise<void>;
@@ -1,78 +0,0 @@
1
- var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) {
2
- if (typeof path === "string" && /^\.\.?\//.test(path)) {
3
- return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
4
- return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
5
- });
6
- }
7
- return path;
8
- };
9
- import { RUNTIME } from "../common/runtime.js";
10
- import { toModuleUrl } from "../common/module-url.js";
11
- import { ProcessSharedBuffer } from "../connections/process-shared-buffer.js";
12
- const DEFAULT_BOOTSTRAP_EXPORT_NAME = "default";
13
- const isWorkerBootstrapOptions = (value) => {
14
- if (!value || typeof value !== "object")
15
- return false;
16
- const candidate = value;
17
- return typeof candidate.href === "string" &&
18
- candidate.href.length > 0 &&
19
- (candidate.name === undefined ||
20
- (typeof candidate.name === "string" && candidate.name.length > 0));
21
- };
22
- const isProcessSharedBufferMetadata = (value) => {
23
- try {
24
- ProcessSharedBuffer.fromMetadata(value);
25
- return true;
26
- }
27
- catch {
28
- return false;
29
- }
30
- };
31
- const reviveWorkerBootstrapValue = (value, seen = new WeakMap()) => {
32
- if (isProcessSharedBufferMetadata(value)) {
33
- return ProcessSharedBuffer.fromMetadata(value);
34
- }
35
- if (value === null || typeof value !== "object")
36
- return value;
37
- const existing = seen.get(value);
38
- if (existing !== undefined)
39
- return existing;
40
- if (Array.isArray(value)) {
41
- const out = [];
42
- seen.set(value, out);
43
- for (const item of value) {
44
- out.push(reviveWorkerBootstrapValue(item, seen));
45
- }
46
- return out;
47
- }
48
- const prototype = Object.getPrototypeOf(value);
49
- if (prototype !== Object.prototype && prototype !== null)
50
- return value;
51
- const out = {};
52
- seen.set(value, out);
53
- for (const [key, item] of Object.entries(value)) {
54
- out[key] = reviveWorkerBootstrapValue(item, seen);
55
- }
56
- return out;
57
- };
58
- export const runWorkerBootstrap = async ({ bootstrap, thread, totalNumberOfThread, }) => {
59
- if (bootstrap === undefined)
60
- return;
61
- if (!isWorkerBootstrapOptions(bootstrap)) {
62
- throw new TypeError("worker.bootstrap must include a non-empty href");
63
- }
64
- const module = await import(__rewriteRelativeImportExtension(toModuleUrl(bootstrap.href)));
65
- const name = bootstrap.name ?? DEFAULT_BOOTSTRAP_EXPORT_NAME;
66
- const selected = module[name];
67
- if (typeof selected !== "function") {
68
- const available = Object.keys(module).join(", ");
69
- throw new TypeError(`worker.bootstrap expected export "${name}" from "${bootstrap.href}" to be a function.` +
70
- ` Available exports: ${available || "(none)"}`);
71
- }
72
- const context = Object.freeze({
73
- thread,
74
- totalNumberOfThread,
75
- runtime: RUNTIME,
76
- });
77
- await selected(reviveWorkerBootstrapValue(bootstrap.data), context);
78
- };
@@ -1,12 +0,0 @@
1
- import { type Task } from "../memory/lock.js";
2
- import type { TimeoutSpec } from "./task-loader.js";
3
- type WorkerJob = (args: unknown, abortToolkit?: unknown) => unknown;
4
- type SlotRunner = (slot: Task) => unknown;
5
- type HasAborted = (signal: number) => boolean;
6
- export declare const composeWorkerRunner: ({ job, timeout, hasAborted, now, }: {
7
- job: WorkerJob;
8
- timeout?: TimeoutSpec;
9
- hasAborted?: HasAborted;
10
- now?: () => number;
11
- }) => SlotRunner;
12
- export {};
@@ -1,97 +0,0 @@
1
- import { getTaskFunctionMeta, getTaskSlotMeta, TASK_SLOT_META_VALUE_MASK, } from "../memory/lock.js";
2
- const ABORT_SIGNAL_META_OFFSET = 1;
3
- const TIMEOUT_KIND_RESOLVE = 1;
4
- const p_now = performance.now.bind(performance);
5
- const raceTimeout = (promise, ms, resolveOnTimeout, timeoutValue) => new Promise((resolve, reject) => {
6
- let done = false;
7
- const timer = setTimeout(() => {
8
- if (done)
9
- return;
10
- done = true;
11
- if (resolveOnTimeout)
12
- resolve(timeoutValue);
13
- else
14
- reject(timeoutValue);
15
- }, ms);
16
- promise.then((value) => {
17
- if (done)
18
- return;
19
- done = true;
20
- clearTimeout(timer);
21
- resolve(value);
22
- }, (err) => {
23
- if (done)
24
- return;
25
- done = true;
26
- clearTimeout(timer);
27
- reject(err);
28
- });
29
- });
30
- const nowStamp = (now) => (Math.floor(now()) & TASK_SLOT_META_VALUE_MASK) >>> 0;
31
- const applyTimeoutBudget = (promise, slot, spec, now) => {
32
- const elapsed = (nowStamp(now) - getTaskSlotMeta(slot)) & TASK_SLOT_META_VALUE_MASK;
33
- const remaining = spec.ms - elapsed;
34
- if (!(remaining > 0)) {
35
- // Prevent late unhandled rejections from the original promise.
36
- promise.then(() => { }, () => { });
37
- return spec.kind === TIMEOUT_KIND_RESOLVE
38
- ? Promise.resolve(spec.value)
39
- : Promise.reject(spec.value);
40
- }
41
- const timeoutMs = Math.max(1, Math.floor(remaining));
42
- return raceTimeout(promise, timeoutMs, spec.kind === TIMEOUT_KIND_RESOLVE, spec.value);
43
- };
44
- const NO_ABORT_SIGNAL = -1;
45
- const readSignal = (slot) => {
46
- const encodedSignal = getTaskFunctionMeta(slot);
47
- if (encodedSignal === 0)
48
- return NO_ABORT_SIGNAL;
49
- const signal = (encodedSignal - ABORT_SIGNAL_META_OFFSET) | 0;
50
- return signal >= 0 ? signal : NO_ABORT_SIGNAL;
51
- };
52
- const makeToolkitCache = (hasAborted) => {
53
- const bySignal = [];
54
- return (signal) => {
55
- let toolkit = bySignal[signal];
56
- if (toolkit)
57
- return toolkit;
58
- const hasAbortedMethod = () => hasAborted(signal);
59
- toolkit = {
60
- hasAborted: hasAbortedMethod,
61
- };
62
- bySignal[signal] = toolkit;
63
- return toolkit;
64
- };
65
- };
66
- export const composeWorkerRunner = ({ job, timeout, hasAborted, now, }) => {
67
- const nowTime = now ?? p_now;
68
- if (!hasAborted) {
69
- if (!timeout) {
70
- return (slot) => job(slot.value);
71
- }
72
- return (slot) => {
73
- const result = job(slot.value);
74
- if (!(result instanceof Promise))
75
- return result;
76
- return applyTimeoutBudget(result, slot, timeout, nowTime);
77
- };
78
- }
79
- const getToolkit = makeToolkitCache(hasAborted);
80
- if (!timeout) {
81
- return (slot) => {
82
- const signal = readSignal(slot);
83
- if (signal === NO_ABORT_SIGNAL)
84
- return job(slot.value);
85
- return job(slot.value, getToolkit(signal));
86
- };
87
- }
88
- return (slot) => {
89
- const signal = readSignal(slot);
90
- const result = signal === NO_ABORT_SIGNAL
91
- ? job(slot.value)
92
- : job(slot.value, getToolkit(signal));
93
- if (!(result instanceof Promise))
94
- return result;
95
- return applyTimeoutBudget(result, slot, timeout, nowTime);
96
- };
97
- };
@@ -1,2 +0,0 @@
1
- import type { WorkerData } from "../types.js";
2
- export declare const workerMainLoop: (startupData: WorkerData) => Promise<void>;