knitting 0.1.54 → 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.
- package/README.md +3 -3
- package/knitting.d.ts +797 -5
- package/knitting.js +9780 -5
- package/map.md +9 -12
- package/package.json +14 -13
- package/shared-memory.d.ts +155 -0
- package/shared-memory.js +1342 -0
- package/unsafe.d.ts +71 -1
- package/unsafe.js +878 -1
- package/utils.d.ts +31 -1
- package/utils.js +116 -1
- package/process-shared-buffer.d.ts +0 -1
- package/process-shared-buffer.js +0 -1
- package/src/api.d.ts +0 -69
- package/src/api.js +0 -627
- package/src/common/envelope.d.ts +0 -17
- package/src/common/envelope.js +0 -22
- package/src/common/module-url.d.ts +0 -1
- package/src/common/module-url.js +0 -24
- package/src/common/node-compat.d.ts +0 -20
- package/src/common/node-compat.js +0 -24
- package/src/common/path-canonical.d.ts +0 -6
- package/src/common/path-canonical.js +0 -41
- package/src/common/runtime.d.ts +0 -15
- package/src/common/runtime.js +0 -91
- package/src/common/shared-buffer-region.d.ts +0 -11
- package/src/common/shared-buffer-region.js +0 -21
- package/src/common/shared-buffer-text.d.ts +0 -16
- package/src/common/shared-buffer-text.js +0 -65
- package/src/common/task-source.d.ts +0 -3
- package/src/common/task-source.js +0 -84
- package/src/common/task-symbol.d.ts +0 -1
- package/src/common/task-symbol.js +0 -1
- package/src/common/with-resolvers.d.ts +0 -9
- package/src/common/with-resolvers.js +0 -23
- package/src/common/worker-runtime.d.ts +0 -42
- package/src/common/worker-runtime.js +0 -61
- package/src/connections/buffer-reference-native.d.ts +0 -56
- package/src/connections/buffer-reference-native.js +0 -217
- package/src/connections/buffer-reference.d.ts +0 -78
- package/src/connections/buffer-reference.js +0 -461
- package/src/connections/bun.d.ts +0 -22
- package/src/connections/bun.js +0 -214
- package/src/connections/deno.d.ts +0 -22
- package/src/connections/deno.js +0 -205
- package/src/connections/file-descriptor.d.ts +0 -39
- package/src/connections/file-descriptor.js +0 -161
- package/src/connections/index.d.ts +0 -4
- package/src/connections/index.js +0 -4
- package/src/connections/node-addons.d.ts +0 -5
- package/src/connections/node-addons.js +0 -43
- package/src/connections/node-buffer-pointer.d.ts +0 -20
- package/src/connections/node-buffer-pointer.js +0 -16
- package/src/connections/node.d.ts +0 -30
- package/src/connections/node.js +0 -62
- package/src/connections/posix.d.ts +0 -32
- package/src/connections/posix.js +0 -77
- package/src/connections/process-shared-buffer.d.ts +0 -75
- package/src/connections/process-shared-buffer.js +0 -279
- package/src/connections/shared-array-buffer-payload.d.ts +0 -36
- package/src/connections/shared-array-buffer-payload.js +0 -235
- package/src/connections/types.d.ts +0 -50
- package/src/connections/types.js +0 -37
- package/src/connections/windows.d.ts +0 -28
- package/src/connections/windows.js +0 -224
- package/src/debug/env-diff.d.ts +0 -26
- package/src/debug/env-diff.js +0 -49
- package/src/debug/gate.d.ts +0 -18
- package/src/debug/gate.js +0 -69
- package/src/debug/handle.d.ts +0 -23
- package/src/debug/handle.js +0 -48
- package/src/error.d.ts +0 -13
- package/src/error.js +0 -49
- package/src/ipc/tools/ring-queue.d.ts +0 -33
- package/src/ipc/tools/ring-queue.js +0 -159
- package/src/ipc/transport/shared-memory.d.ts +0 -23
- package/src/ipc/transport/shared-memory.js +0 -35
- package/src/memory/byte-carpet.d.ts +0 -73
- package/src/memory/byte-carpet.js +0 -157
- package/src/memory/lock.d.ts +0 -201
- package/src/memory/lock.js +0 -739
- package/src/memory/payload-config.d.ts +0 -31
- package/src/memory/payload-config.js +0 -67
- package/src/memory/payloadCodec.d.ts +0 -46
- package/src/memory/payloadCodec.js +0 -1334
- package/src/memory/regionRegistry.d.ts +0 -17
- package/src/memory/regionRegistry.js +0 -285
- package/src/memory/shared-buffer-io.d.ts +0 -55
- package/src/memory/shared-buffer-io.js +0 -403
- package/src/permission/index.d.ts +0 -2
- package/src/permission/index.js +0 -2
- package/src/permission/protocol.d.ts +0 -166
- package/src/permission/protocol.js +0 -650
- package/src/runtime/balancer.d.ts +0 -19
- package/src/runtime/balancer.js +0 -149
- package/src/runtime/dispatcher.d.ts +0 -40
- package/src/runtime/dispatcher.js +0 -142
- package/src/runtime/inline-executor.d.ts +0 -10
- package/src/runtime/inline-executor.js +0 -282
- package/src/runtime/pool.d.ts +0 -39
- package/src/runtime/pool.js +0 -404
- package/src/runtime/process-worker.d.ts +0 -92
- package/src/runtime/process-worker.js +0 -673
- package/src/runtime/tx-queue.d.ts +0 -26
- package/src/runtime/tx-queue.js +0 -155
- package/src/shared/abortSignal.d.ts +0 -23
- package/src/shared/abortSignal.js +0 -134
- package/src/types.d.ts +0 -357
- package/src/types.js +0 -2
- package/src/utils/http.d.ts +0 -29
- package/src/utils/http.js +0 -100
- package/src/worker/bootstrap.d.ts +0 -5
- package/src/worker/bootstrap.js +0 -78
- package/src/worker/composable-runners.d.ts +0 -12
- package/src/worker/composable-runners.js +0 -97
- package/src/worker/loop.d.ts +0 -2
- package/src/worker/loop.js +0 -382
- package/src/worker/process-worker-bootstrap.d.ts +0 -8
- package/src/worker/process-worker-bootstrap.js +0 -160
- package/src/worker/rx-queue.d.ts +0 -25
- package/src/worker/rx-queue.js +0 -173
- package/src/worker/safety/index.d.ts +0 -4
- package/src/worker/safety/index.js +0 -4
- package/src/worker/safety/performance.d.ts +0 -1
- package/src/worker/safety/performance.js +0 -17
- package/src/worker/safety/process.d.ts +0 -2
- package/src/worker/safety/process.js +0 -79
- package/src/worker/safety/startup.d.ts +0 -16
- package/src/worker/safety/startup.js +0 -30
- package/src/worker/safety/worker-data.d.ts +0 -2
- package/src/worker/safety/worker-data.js +0 -36
- package/src/worker/task-loader.d.ts +0 -27
- package/src/worker/task-loader.js +0 -78
- package/src/worker/timers.d.ts +0 -18
- package/src/worker/timers.js +0 -116
package/utils.d.ts
CHANGED
|
@@ -1 +1,31 @@
|
|
|
1
|
-
export
|
|
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;
|
|
30
|
+
|
|
31
|
+
export {};
|
package/utils.js
CHANGED
|
@@ -1 +1,116 @@
|
|
|
1
|
-
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __returnValue = (v) => v;
|
|
3
|
+
function __exportSetter(name, newValue) {
|
|
4
|
+
this[name] = __returnValue.bind(null, newValue);
|
|
5
|
+
}
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, {
|
|
9
|
+
get: all[name],
|
|
10
|
+
enumerable: true,
|
|
11
|
+
configurable: true,
|
|
12
|
+
set: __exportSetter.bind(all, name)
|
|
13
|
+
});
|
|
14
|
+
};
|
|
15
|
+
var __esm = (fn, res) => () => (fn && (res = fn(fn = 0)), res);
|
|
16
|
+
|
|
17
|
+
// src/utils/http.ts
|
|
18
|
+
var nodeBuffer = globalThis.Buffer;
|
|
19
|
+
var hasNodeBuffer = typeof nodeBuffer?.from === "function" && typeof nodeBuffer?.byteLength === "function";
|
|
20
|
+
var textEncoder = new TextEncoder;
|
|
21
|
+
var textDecoder = new TextDecoder;
|
|
22
|
+
var hasSharedArrayBuffer = typeof SharedArrayBuffer === "function";
|
|
23
|
+
var isSharedArrayBuffer = (value) => hasSharedArrayBuffer && value instanceof SharedArrayBuffer;
|
|
24
|
+
var BYTES_PER_ELEMENT = {
|
|
25
|
+
f64: 8,
|
|
26
|
+
f32: 4,
|
|
27
|
+
i32: 4
|
|
28
|
+
};
|
|
29
|
+
var toBytes = (source) => {
|
|
30
|
+
if (source instanceof Uint8Array)
|
|
31
|
+
return source;
|
|
32
|
+
if (ArrayBuffer.isView(source)) {
|
|
33
|
+
return new Uint8Array(source.buffer, source.byteOffset, source.byteLength);
|
|
34
|
+
}
|
|
35
|
+
if (source instanceof ArrayBuffer || isSharedArrayBuffer(source)) {
|
|
36
|
+
return new Uint8Array(source);
|
|
37
|
+
}
|
|
38
|
+
throw new TypeError("Expected an ArrayBuffer, SharedArrayBuffer, or typed-array view.");
|
|
39
|
+
};
|
|
40
|
+
var bufferToBytes = (source) => toBytes(source);
|
|
41
|
+
var bytesToBuffer = (source) => {
|
|
42
|
+
const bytes = toBytes(source);
|
|
43
|
+
const sab = new SharedArrayBuffer(bytes.byteLength);
|
|
44
|
+
new Uint8Array(sab).set(bytes);
|
|
45
|
+
return sab;
|
|
46
|
+
};
|
|
47
|
+
var makeNumberView = (format, buffer, byteOffset, length) => {
|
|
48
|
+
switch (format) {
|
|
49
|
+
case "f64":
|
|
50
|
+
return new Float64Array(buffer, byteOffset, length);
|
|
51
|
+
case "f32":
|
|
52
|
+
return new Float32Array(buffer, byteOffset, length);
|
|
53
|
+
case "i32":
|
|
54
|
+
return new Int32Array(buffer, byteOffset, length);
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
var bufferToString = (source) => {
|
|
58
|
+
const bytes = toBytes(source);
|
|
59
|
+
if (hasNodeBuffer) {
|
|
60
|
+
return nodeBuffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength).toString("utf8");
|
|
61
|
+
}
|
|
62
|
+
return textDecoder.decode(bytes);
|
|
63
|
+
};
|
|
64
|
+
var stringToBuffer = (text) => {
|
|
65
|
+
if (typeof text !== "string") {
|
|
66
|
+
throw new TypeError("stringToBuffer expects a string.");
|
|
67
|
+
}
|
|
68
|
+
if (hasNodeBuffer) {
|
|
69
|
+
const byteLength = nodeBuffer.byteLength(text, "utf8");
|
|
70
|
+
const sab2 = new SharedArrayBuffer(byteLength);
|
|
71
|
+
nodeBuffer.from(sab2).write(text, "utf8");
|
|
72
|
+
return sab2;
|
|
73
|
+
}
|
|
74
|
+
const encoded = textEncoder.encode(text);
|
|
75
|
+
const sab = new SharedArrayBuffer(encoded.byteLength);
|
|
76
|
+
new Uint8Array(sab).set(encoded);
|
|
77
|
+
return sab;
|
|
78
|
+
};
|
|
79
|
+
var bufferToJson = (source) => JSON.parse(bufferToString(source));
|
|
80
|
+
var jsonToBuffer = (value) => {
|
|
81
|
+
const text = JSON.stringify(value);
|
|
82
|
+
if (typeof text !== "string") {
|
|
83
|
+
throw new TypeError("jsonToBuffer: value is not JSON-serializable (stringify returned undefined).");
|
|
84
|
+
}
|
|
85
|
+
return stringToBuffer(text);
|
|
86
|
+
};
|
|
87
|
+
var numbersToBuffer = (numbers, options) => {
|
|
88
|
+
const format = options?.format ?? "f64";
|
|
89
|
+
const sab = new SharedArrayBuffer(numbers.length * BYTES_PER_ELEMENT[format]);
|
|
90
|
+
makeNumberView(format, sab, 0, numbers.length).set(numbers);
|
|
91
|
+
return sab;
|
|
92
|
+
};
|
|
93
|
+
function bufferToNumbers(source, options) {
|
|
94
|
+
const format = options?.format ?? "f64";
|
|
95
|
+
const bytesPer = BYTES_PER_ELEMENT[format];
|
|
96
|
+
const bytes = toBytes(source);
|
|
97
|
+
if (bytes.byteLength % bytesPer !== 0) {
|
|
98
|
+
throw new RangeError(`bufferToNumbers: byte length ${bytes.byteLength} is not a multiple of ` + `${bytesPer} for format "${format}".`);
|
|
99
|
+
}
|
|
100
|
+
const length = bytes.byteLength / bytesPer;
|
|
101
|
+
if (bytes.byteOffset % bytesPer === 0) {
|
|
102
|
+
return makeNumberView(format, bytes.buffer, bytes.byteOffset, length);
|
|
103
|
+
}
|
|
104
|
+
const copy = bytes.slice();
|
|
105
|
+
return makeNumberView(format, copy.buffer, 0, length);
|
|
106
|
+
}
|
|
107
|
+
export {
|
|
108
|
+
stringToBuffer,
|
|
109
|
+
numbersToBuffer,
|
|
110
|
+
jsonToBuffer,
|
|
111
|
+
bytesToBuffer,
|
|
112
|
+
bufferToString,
|
|
113
|
+
bufferToNumbers,
|
|
114
|
+
bufferToJson,
|
|
115
|
+
bufferToBytes
|
|
116
|
+
};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { getDefaultProcessSharedBufferPrimitives, parseProcessSharedBufferMetadata, PROCESS_SHARED_BUFFER_BRAND, ProcessSharedBuffer, setDefaultProcessSharedBufferPrimitives, type ProcessSharedBufferCreator, type ProcessSharedBufferMapper, type ProcessSharedBufferMetadata, type ProcessSharedBufferPrimitives, type ProcessSharedBufferRange, type ProcessSharedBufferView, type ProcessSharedBufferViewConstructor, } from "./src/connections/process-shared-buffer.js";
|
package/process-shared-buffer.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { getDefaultProcessSharedBufferPrimitives, parseProcessSharedBufferMetadata, PROCESS_SHARED_BUFFER_BRAND, ProcessSharedBuffer, setDefaultProcessSharedBufferPrimitives, } from "./src/connections/process-shared-buffer.js";
|
package/src/api.d.ts
DELETED
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
import { endpointSymbol } from "./common/task-symbol.js";
|
|
2
|
-
import type { AbortSignalConfig, AbortSignalOption, AbortSignalToolkit, Args, ComposedWithKey, CreatePool, FixPoint, ImportTaskOptions, MaybePromise, Pool, ReturnFixed, TaskInput, tasks, TaskTimeout } from "./types.js";
|
|
3
|
-
type ToListAndIds = {
|
|
4
|
-
list: string[];
|
|
5
|
-
ids: number[];
|
|
6
|
-
names: string[];
|
|
7
|
-
at: number[];
|
|
8
|
-
};
|
|
9
|
-
type ToListAndIdsFn = (args: ComposedWithKey[]) => ToListAndIds;
|
|
10
|
-
type CreatePoolFactory = (options: CreatePool) => <T extends tasks>(tasks: T) => Pool<T>;
|
|
11
|
-
type InferredTaskFunction = (...args: any[]) => MaybePromise<Args>;
|
|
12
|
-
type InferredTaskInput<F extends InferredTaskFunction, AS extends AbortSignalOption> = Parameters<F> extends [] ? void : AS extends undefined ? Parameters<F> extends [infer A] ? A extends TaskInput ? A : never : never : Parameters<F> extends [infer A] ? A extends TaskInput ? A : never : Parameters<F> extends [infer A, AbortSignalToolkit<AS>] ? A extends TaskInput ? A : never : never;
|
|
13
|
-
type InferredTaskOutput<F extends InferredTaskFunction> = Awaited<ReturnType<F>> extends infer R ? R extends Blob ? never : R extends Args ? R : never : never;
|
|
14
|
-
type InferredTaskShape<F extends InferredTaskFunction, AS extends AbortSignalOption> = [InferredTaskInput<F, AS>] extends [never] ? never : [InferredTaskOutput<F>] extends [never] ? never : {
|
|
15
|
-
readonly f: F;
|
|
16
|
-
readonly timeout?: TaskTimeout;
|
|
17
|
-
} & (AS extends undefined ? {
|
|
18
|
-
readonly abortSignal?: undefined;
|
|
19
|
-
} : {
|
|
20
|
-
readonly abortSignal: AS;
|
|
21
|
-
});
|
|
22
|
-
export declare const isMain: boolean;
|
|
23
|
-
export { endpointSymbol as endpointSymbol };
|
|
24
|
-
/**
|
|
25
|
-
* Reconstructs stable task order from top-level exports before names are bound.
|
|
26
|
-
*/
|
|
27
|
-
export declare const toListAndIds: ToListAndIdsFn;
|
|
28
|
-
/**
|
|
29
|
-
* Create a typed worker pool from module-scope exported tasks/functions.
|
|
30
|
-
*
|
|
31
|
-
* Install/import as `knitting` on npm or `@vixeny/knitting` on JSR. Requires
|
|
32
|
-
* Node 22+, Deno 2+, or Bun 1+.
|
|
33
|
-
*
|
|
34
|
-
* Use `createPool(options)({ taskA })`, call `await pool.call.taskA(arg)`, and
|
|
35
|
-
* clean up with `using pool = ...` or `await pool.shutdown()`.
|
|
36
|
-
*
|
|
37
|
-
* Guard host-only pool setup with `isMain`: workers re-import task modules, and
|
|
38
|
-
* top-level imports in those modules run in every worker. Keep task modules
|
|
39
|
-
* lean and separate from server/framework setup. Each task receives one
|
|
40
|
-
* argument; use an object or tuple for multiple values.
|
|
41
|
-
*/
|
|
42
|
-
export declare const createPool: CreatePoolFactory;
|
|
43
|
-
/**
|
|
44
|
-
* Define a worker task with options.
|
|
45
|
-
*
|
|
46
|
-
* Pass raw module-scope exported functions to `createPool` directly when you do
|
|
47
|
-
* not need options. Use `task({ f })` for timeouts, abort signals, or the
|
|
48
|
-
* single-task `.createPool()` shorthand.
|
|
49
|
-
*
|
|
50
|
-
* The function receives one argument; use a tuple/object for multiple values.
|
|
51
|
-
* Inputs may be direct values or native Promises, so `request.arrayBuffer()` can
|
|
52
|
-
* be forwarded without awaiting on the host.
|
|
53
|
-
*/
|
|
54
|
-
export declare function task<F extends InferredTaskFunction>(I: InferredTaskShape<F, undefined>): ReturnFixed<InferredTaskInput<F, undefined>, InferredTaskOutput<F>, undefined>;
|
|
55
|
-
export declare function task<F extends InferredTaskFunction>(I: InferredTaskShape<F, true>): ReturnFixed<InferredTaskInput<F, true>, InferredTaskOutput<F>, true>;
|
|
56
|
-
export declare function task<AS extends AbortSignalConfig, F extends InferredTaskFunction>(I: InferredTaskShape<F, AS>): ReturnFixed<InferredTaskInput<F, AS>, InferredTaskOutput<F>, AS>;
|
|
57
|
-
export declare function task<A extends TaskInput = void, B extends Args = void>(I: FixPoint<A, B, true>): ReturnFixed<A, B, true>;
|
|
58
|
-
export declare function task<A extends TaskInput = void, B extends Args = void, AS extends AbortSignalConfig = AbortSignalConfig>(I: FixPoint<A, B, AS>): ReturnFixed<A, B, AS>;
|
|
59
|
-
export declare function task<A extends TaskInput = void, B extends Args = void>(I: FixPoint<A, B, undefined>): ReturnFixed<A, B, undefined>;
|
|
60
|
-
/**
|
|
61
|
-
* Define a task whose worker-side code is imported only by workers.
|
|
62
|
-
*
|
|
63
|
-
* Use this for untrusted or security-sensitive code: the host keeps a typed
|
|
64
|
-
* wrapper and does not import/evaluate `href`. The target export must be a
|
|
65
|
-
* plain function, not a `task()` wrapper.
|
|
66
|
-
*/
|
|
67
|
-
export declare function importTask<A extends TaskInput = void, B extends Args = void>(options: ImportTaskOptions<A, B, true>): ReturnFixed<A, B, true>;
|
|
68
|
-
export declare function importTask<A extends TaskInput = void, B extends Args = void, AS extends AbortSignalConfig = AbortSignalConfig>(options: ImportTaskOptions<A, B, AS>): ReturnFixed<A, B, AS>;
|
|
69
|
-
export declare function importTask<A extends TaskInput = void, B extends Args = void>(options: ImportTaskOptions<A, B, undefined>): ReturnFixed<A, B, undefined>;
|