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
package/README.md CHANGED
@@ -11,18 +11,22 @@
11
11
  [![Deno](https://img.shields.io/badge/deno-2%2B-000000?logo=deno&logoColor=white)](https://deno.com/)
12
12
  [![Bun](https://img.shields.io/badge/bun-1%2B-f472b6?logo=bun&logoColor=white)](https://bun.sh/)
13
13
 
14
- Knitting is a worker pool over a shared-memory IPC runtime for Node.js, Deno,
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
- Thanks to its memory design, it can be 5x to 25x faster than using
19
- `postMessages` , bypassing OS socket communication entirely with a novel
20
- protocol written from scratch.
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
- Use it for parts of your program that should run in different environments, such
23
- as CPU-intensive tasks, small jobs, runtime-isolated tasks, custom isolation for
24
- workers in Docker or bwrap environments, long-running tools, or any processes
25
- that require speed and type flexibility.
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
- So you only have to take care of 4 things:
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, we take care of scheduling and orchestration across worker
42
- threads or separate processes, also handling signals, timeouts, life cycles,
43
- memory allocation, garbage collection, and cross-runtime memory over the
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: Have a multithreaded environment or process with few lines of
49
- code.
50
- - Great type support: pass primitives, JSON, Promise of these, and special types
51
- (typed arrays, `Node Buffer`, `Envelope`, and `ProcessSharedBuffer`).
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
- - All out-of-the-box experiences: strict-by-default permissions, payload-size
56
- limits, task timeouts, abort-aware tasks, and worker hard timeouts.
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
- The `isMain` guard when the same module is loaded by workers or process. Export
100
- exposes the tasks or functions at module scope, so knitting maps down the
101
- imports, then use and use the pool only from the main program.
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/process-shared-buffer";
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/process-shared-buffer";
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/process-shared-buffer";
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),