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.
- package/README.md +38 -27
- 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 -555
- 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 -34
- 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 -30
- package/src/runtime/pool.js +0 -391
- 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 -151
- package/src/shared/abortSignal.d.ts +0 -23
- package/src/shared/abortSignal.js +0 -134
- package/src/types.d.ts +0 -346
- 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/README.md
CHANGED
|
@@ -11,18 +11,22 @@
|
|
|
11
11
|
[](https://deno.com/)
|
|
12
12
|
[](https://bun.sh/)
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
and Bun. Our mission is to make JavaScript a multicore language with real
|
|
16
|
-
inter-runtime communication.
|
|
14
|
+
Website: [knittingdocs.netlify.app](https://knittingdocs.netlify.app/)
|
|
17
15
|
|
|
18
|
-
|
|
19
|
-
`
|
|
20
|
-
|
|
16
|
+
If you are an agent trying to understand the project, the website also serves an
|
|
17
|
+
[`llms.txt`](https://knittingdocs.netlify.app/llms.txt) file with a compact map
|
|
18
|
+
of the docs.
|
|
21
19
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
20
|
+
Knitting is a worker pool built on shared-memory IPC for Node.js, Deno, and Bun.
|
|
21
|
+
It lets you call work running on other threads or processes as if it were a
|
|
22
|
+
normal async function.
|
|
23
|
+
|
|
24
|
+
Because calls move through shared memory instead of `postMessage` or sockets,
|
|
25
|
+
some workloads can be 5x to 25x faster than the usual worker-message path.
|
|
26
|
+
|
|
27
|
+
Use it when part of your program should run somewhere else: CPU-heavy work,
|
|
28
|
+
bursty small jobs, runtime-isolated code, Docker or bwrap workers, long-running
|
|
29
|
+
tools, or cross-runtime process work that still needs to be fast and typed.
|
|
26
30
|
|
|
27
31
|
You export a function or task, spin up a pool, and call it like a normal async
|
|
28
32
|
function:
|
|
@@ -31,29 +35,28 @@ function:
|
|
|
31
35
|
const result = await pool.call.resizeImage(file);
|
|
32
36
|
```
|
|
33
37
|
|
|
34
|
-
|
|
38
|
+
Most of the time, you only have to take care of four things:
|
|
35
39
|
|
|
36
40
|
- Export a function or task
|
|
37
41
|
- Create a pool
|
|
38
42
|
- Call it
|
|
39
43
|
- Let `using` or `shutdown()` close the pool
|
|
40
44
|
|
|
41
|
-
Under the hood,
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
processes.
|
|
45
|
+
Under the hood, Knitting handles scheduling across worker threads or separate
|
|
46
|
+
processes, plus signals, timeouts, lifecycles, memory allocation, cleanup, and
|
|
47
|
+
cross-runtime shared memory.
|
|
45
48
|
|
|
46
49
|
## Why use it?
|
|
47
50
|
|
|
48
|
-
- Easy to use:
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
51
|
+
- Easy to use: spin up threads or processes with a small API.
|
|
52
|
+
- Great type support: pass primitives, JSON, promises of those values, and
|
|
53
|
+
special types like typed arrays, `Node Buffer`, `Envelope`, and
|
|
54
|
+
`ProcessSharedBuffer`.
|
|
52
55
|
- Runtime flexibility: the same API across Node.js, Deno, and Bun.
|
|
53
56
|
- Worker choices: use threads for fast pools or processes for stronger
|
|
54
57
|
isolation.
|
|
55
|
-
-
|
|
56
|
-
|
|
58
|
+
- Practical defaults: strict worker permissions, payload-size limits, task
|
|
59
|
+
timeouts, abort-aware tasks, and worker hard timeouts.
|
|
57
60
|
|
|
58
61
|
## Requirements
|
|
59
62
|
|
|
@@ -96,9 +99,9 @@ if (isMain) {
|
|
|
96
99
|
}
|
|
97
100
|
```
|
|
98
101
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
+
Use the `isMain` guard when a module can be loaded by both the host and its
|
|
103
|
+
workers. Export tasks at module scope so Knitting can find them, then create and
|
|
104
|
+
use the pool only from the main program.
|
|
102
105
|
|
|
103
106
|
## The Mental Model
|
|
104
107
|
|
|
@@ -335,9 +338,17 @@ Common options you might tweak:
|
|
|
335
338
|
| `worker.runtime` | Choose `"thread"` or `"process"` workers. |
|
|
336
339
|
| `worker.processSharedMemory` | Process-worker memory discovery: `"inherit"` by default on POSIX, or `"named"` for wrappers/containers that cannot preserve fd 0. |
|
|
337
340
|
| `permission` | Runtime permission policy for workers. |
|
|
341
|
+
| `host.dispatcher` | Experimental host dispatcher topology: `"per-thread"` or `"serial-channel"`. |
|
|
338
342
|
| `debug` | Enable diagnostics (`host`, `globals`, `signals`, `imports`, `lifecycle`) or use `KNITTING_DEBUG`. |
|
|
339
343
|
| `source` | Worker source override for advanced runtimes. |
|
|
340
344
|
|
|
345
|
+
Most users can leave `host.dispatcher` alone. The current default is
|
|
346
|
+
experimental: Bun and single-worker pools use `"per-thread"`, while multi-worker
|
|
347
|
+
Node/Deno pools use `"serial-channel"` because it tends to behave well for
|
|
348
|
+
bursty HTTP-style fan-out. If you are tuning a server or comparing runtimes, you
|
|
349
|
+
can force either mode with `KNITTING_DISPATCHER=per-thread` or
|
|
350
|
+
`KNITTING_DISPATCHER=serial-channel`.
|
|
351
|
+
|
|
341
352
|
### Worker bootstrap
|
|
342
353
|
|
|
343
354
|
Use `worker.bootstrap` when a worker needs privileged setup before task modules
|
|
@@ -656,7 +667,7 @@ copying the whole payload for every call.
|
|
|
656
667
|
import {
|
|
657
668
|
getDefaultProcessSharedBufferPrimitives,
|
|
658
669
|
ProcessSharedBuffer,
|
|
659
|
-
} from "knitting/
|
|
670
|
+
} from "knitting/shared-memory";
|
|
660
671
|
import { createPool, isMain, task } from "knitting";
|
|
661
672
|
|
|
662
673
|
export const readFirstCell = task<ProcessSharedBuffer, number>({
|
|
@@ -701,7 +712,7 @@ name; the other opens that same name.
|
|
|
701
712
|
import {
|
|
702
713
|
getDefaultProcessSharedBufferPrimitives,
|
|
703
714
|
ProcessSharedBuffer,
|
|
704
|
-
} from "knitting/
|
|
715
|
+
} from "knitting/shared-memory";
|
|
705
716
|
|
|
706
717
|
const name = "knitting-demo-channel";
|
|
707
718
|
const primitives = getDefaultProcessSharedBufferPrimitives();
|
|
@@ -750,7 +761,7 @@ import { createPool, isMain, task } from "knitting";
|
|
|
750
761
|
import {
|
|
751
762
|
getDefaultProcessSharedBufferPrimitives,
|
|
752
763
|
ProcessSharedBuffer,
|
|
753
|
-
} from "knitting/
|
|
764
|
+
} from "knitting/shared-memory";
|
|
754
765
|
|
|
755
766
|
export const readCounter = task<ProcessSharedBuffer, number>({
|
|
756
767
|
f: (shared) => Atomics.load(shared.view(Int32Array), 0),
|