@nervur.org/installer-node 0.0.0-stage → 0.37.2

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 (364) hide show
  1. package/LICENSE +105 -0
  2. package/README.md +159 -2
  3. package/SECURITY.md +8 -0
  4. package/bin/nervur.js +5 -0
  5. package/cargo/emscripten-module.wasm +0 -0
  6. package/cargo/manifest.json +1 -0
  7. package/cargo/sqlite3.wasm +0 -0
  8. package/cargo/table-i-i.wasm +0 -0
  9. package/cargo/table-i-ii.wasm +0 -0
  10. package/cargo/table-i-iii.wasm +0 -0
  11. package/cargo/table-i-iiii.wasm +0 -0
  12. package/cargo/table-i-iiiii.wasm +0 -0
  13. package/cargo/table-i-iiij.wasm +0 -0
  14. package/cargo/table-i-ij.wasm +0 -0
  15. package/cargo/table-v-ii.wasm +0 -0
  16. package/cargo/table-v-iii.wasm +0 -0
  17. package/cargo/worker.js +30808 -0
  18. package/organs/console/node/guard.d.ts +10 -0
  19. package/organs/console/node/guard.js +52 -0
  20. package/organs/console/node/index.d.ts +4 -0
  21. package/organs/console/node/index.js +5 -0
  22. package/organs/console/node/line.d.ts +16 -0
  23. package/organs/console/node/line.js +150 -0
  24. package/organs/console/node/main.js +51 -0
  25. package/organs/console/node/program.d.ts +1 -0
  26. package/organs/console/node/program.js +5 -0
  27. package/organs/console/node/proof.d.ts +15 -0
  28. package/organs/console/node/proof.js +84 -0
  29. package/organs/console/node/socket.d.ts +15 -0
  30. package/organs/console/node/socket.js +99 -0
  31. package/organs/disk/index.d.ts +1 -0
  32. package/organs/disk/index.js +2 -0
  33. package/organs/disk/node/files.js +162 -0
  34. package/organs/disk/node/main.js +20 -0
  35. package/organs/disk/node/program.d.ts +1 -0
  36. package/organs/disk/node/program.js +5 -0
  37. package/organs/disk/organ.d.ts +5 -0
  38. package/organs/disk/organ.js +69 -0
  39. package/organs/network/node/fill.d.ts +21 -0
  40. package/organs/network/node/fill.js +195 -0
  41. package/organs/network/node/flows.js +66 -0
  42. package/organs/network/node/half.js +260 -0
  43. package/organs/network/node/lines.d.ts +29 -0
  44. package/organs/network/node/lines.js +236 -0
  45. package/organs/network/node/link.d.ts +24 -0
  46. package/organs/network/node/link.js +215 -0
  47. package/organs/network/node/main.js +41 -0
  48. package/organs/network/node/moved.d.ts +2 -0
  49. package/organs/network/node/moved.js +20 -0
  50. package/organs/network/node/near.d.ts +63 -0
  51. package/organs/network/node/near.js +263 -0
  52. package/organs/network/node/program.d.ts +1 -0
  53. package/organs/network/node/program.js +5 -0
  54. package/organs/network/node/schemes.d.ts +1 -0
  55. package/organs/network/node/schemes.js +2 -0
  56. package/organs/network/node/sent.d.ts +13 -0
  57. package/organs/network/node/sent.js +101 -0
  58. package/organs/network/node/served.d.ts +19 -0
  59. package/organs/network/node/served.js +237 -0
  60. package/organs/screen/node/client.js +3 -0
  61. package/organs/screen/node/follow.js +266 -0
  62. package/organs/screen/node/frames.js +26 -0
  63. package/organs/screen/node/held.js +27 -0
  64. package/organs/screen/node/http.js +179 -0
  65. package/organs/screen/node/lay.js +116 -0
  66. package/organs/screen/node/leave.js +27 -0
  67. package/organs/screen/node/linked.js +69 -0
  68. package/organs/screen/node/listen.js +61 -0
  69. package/organs/screen/node/look.js +121 -0
  70. package/organs/screen/node/loop.js +154 -0
  71. package/organs/screen/node/main.js +74 -0
  72. package/organs/screen/node/mirror.js +112 -0
  73. package/organs/screen/node/notice.js +44 -0
  74. package/organs/screen/node/presser.js +11 -0
  75. package/organs/screen/node/presses.js +108 -0
  76. package/organs/screen/node/program.d.ts +1 -0
  77. package/organs/screen/node/program.js +5 -0
  78. package/organs/screen/node/shown.js +205 -0
  79. package/organs/screen/node/standing.js +29 -0
  80. package/organs/screen/node/state.js +80 -0
  81. package/organs/screen/node/tell.js +47 -0
  82. package/organs/screen/node/uid.js +65 -0
  83. package/organs/screen/node/worn.js +23 -0
  84. package/organs/shell/index.d.ts +1 -0
  85. package/organs/shell/index.js +2 -0
  86. package/organs/shell/node/main.js +26 -0
  87. package/organs/shell/node/output.js +70 -0
  88. package/organs/shell/node/pressure.js +14 -0
  89. package/organs/shell/node/program.d.ts +1 -0
  90. package/organs/shell/node/program.js +5 -0
  91. package/organs/shell/node/runs.js +220 -0
  92. package/organs/shell/node/told.js +70 -0
  93. package/organs/shell/organ.d.ts +5 -0
  94. package/organs/shell/organ.js +59 -0
  95. package/package.json +43 -4
  96. package/src/command/act.d.ts +31 -0
  97. package/src/command/act.js +261 -0
  98. package/src/command/answers.d.ts +3 -0
  99. package/src/command/answers.js +232 -0
  100. package/src/command/args.d.ts +14 -0
  101. package/src/command/args.js +220 -0
  102. package/src/command/arrive.d.ts +2 -0
  103. package/src/command/arrive.js +21 -0
  104. package/src/command/asked.d.ts +18 -0
  105. package/src/command/asked.js +49 -0
  106. package/src/command/backup.d.ts +18 -0
  107. package/src/command/backup.js +261 -0
  108. package/src/command/before.d.ts +40 -0
  109. package/src/command/before.js +249 -0
  110. package/src/command/born.d.ts +27 -0
  111. package/src/command/born.js +103 -0
  112. package/src/command/bridge.d.ts +19 -0
  113. package/src/command/bridge.js +152 -0
  114. package/src/command/chain.d.ts +9 -0
  115. package/src/command/chain.js +48 -0
  116. package/src/command/channel.d.ts +29 -0
  117. package/src/command/channel.js +263 -0
  118. package/src/command/choices.d.ts +35 -0
  119. package/src/command/choices.js +173 -0
  120. package/src/command/claim.d.ts +12 -0
  121. package/src/command/claim.js +46 -0
  122. package/src/command/commands.d.ts +16 -0
  123. package/src/command/commands.js +257 -0
  124. package/src/command/ends.d.ts +16 -0
  125. package/src/command/ends.js +78 -0
  126. package/src/command/follow.d.ts +12 -0
  127. package/src/command/follow.js +66 -0
  128. package/src/command/given.d.ts +39 -0
  129. package/src/command/given.js +53 -0
  130. package/src/command/held.d.ts +15 -0
  131. package/src/command/held.js +56 -0
  132. package/src/command/index.d.ts +1 -0
  133. package/src/command/index.js +203 -0
  134. package/src/command/jailer.d.ts +28 -0
  135. package/src/command/jailer.js +135 -0
  136. package/src/command/laid.d.ts +6 -0
  137. package/src/command/laid.js +31 -0
  138. package/src/command/left.d.ts +9 -0
  139. package/src/command/left.js +34 -0
  140. package/src/command/line.d.ts +7 -0
  141. package/src/command/line.js +39 -0
  142. package/src/command/link.d.ts +30 -0
  143. package/src/command/link.js +123 -0
  144. package/src/command/lobes.d.ts +1 -0
  145. package/src/command/lobes.js +10 -0
  146. package/src/command/locks.d.ts +17 -0
  147. package/src/command/locks.js +76 -0
  148. package/src/command/mcp/calls.d.ts +27 -0
  149. package/src/command/mcp/calls.js +155 -0
  150. package/src/command/mcp/notices.d.ts +22 -0
  151. package/src/command/mcp/notices.js +203 -0
  152. package/src/command/mcp/resources.d.ts +31 -0
  153. package/src/command/mcp/resources.js +108 -0
  154. package/src/command/mcp/server.d.ts +19 -0
  155. package/src/command/mcp/server.js +149 -0
  156. package/src/command/mcp/tools.d.ts +39 -0
  157. package/src/command/mcp/tools.js +131 -0
  158. package/src/command/mcp.d.ts +8 -0
  159. package/src/command/mcp.js +39 -0
  160. package/src/command/noticed.d.ts +11 -0
  161. package/src/command/noticed.js +55 -0
  162. package/src/command/older.d.ts +11 -0
  163. package/src/command/older.js +45 -0
  164. package/src/command/open.d.ts +24 -0
  165. package/src/command/open.js +107 -0
  166. package/src/command/passphrase.d.ts +6 -0
  167. package/src/command/passphrase.js +82 -0
  168. package/src/command/reclaim.d.ts +7 -0
  169. package/src/command/reclaim.js +47 -0
  170. package/src/command/refused.d.ts +4 -0
  171. package/src/command/refused.js +27 -0
  172. package/src/command/release.d.ts +27 -0
  173. package/src/command/release.js +153 -0
  174. package/src/command/repo.d.ts +31 -0
  175. package/src/command/repo.js +133 -0
  176. package/src/command/rewrap.d.ts +16 -0
  177. package/src/command/rewrap.js +175 -0
  178. package/src/command/runtime.d.ts +25 -0
  179. package/src/command/runtime.js +175 -0
  180. package/src/command/salvage.d.ts +4 -0
  181. package/src/command/salvage.js +42 -0
  182. package/src/command/screen.d.ts +14 -0
  183. package/src/command/screen.js +62 -0
  184. package/src/command/spawn.d.ts +23 -0
  185. package/src/command/spawn.js +213 -0
  186. package/src/command/start.d.ts +34 -0
  187. package/src/command/start.js +246 -0
  188. package/src/command/state.d.ts +5 -0
  189. package/src/command/state.js +39 -0
  190. package/src/command/token.d.ts +1 -0
  191. package/src/command/token.js +9 -0
  192. package/src/command/trial.d.ts +18 -0
  193. package/src/command/trial.js +108 -0
  194. package/src/command/undone.d.ts +3 -0
  195. package/src/command/undone.js +21 -0
  196. package/src/command/vouch.d.ts +1 -0
  197. package/src/command/vouch.js +15 -0
  198. package/src/command/windows-processes.d.ts +2 -0
  199. package/src/command/windows-processes.js +22 -0
  200. package/src/command/wipe.d.ts +9 -0
  201. package/src/command/wipe.js +105 -0
  202. package/src/command/words.d.ts +5 -0
  203. package/src/command/words.js +53 -0
  204. package/src/door/claim.d.ts +5 -0
  205. package/src/door/claim.js +21 -0
  206. package/src/door/claimed.d.ts +7 -0
  207. package/src/door/claimed.js +110 -0
  208. package/src/door/configuration.d.ts +40 -0
  209. package/src/door/configuration.js +206 -0
  210. package/src/door/design.d.ts +13 -0
  211. package/src/door/design.js +71 -0
  212. package/src/door/digests.d.ts +7 -0
  213. package/src/door/digests.js +36 -0
  214. package/src/door/distribution.d.ts +25 -0
  215. package/src/door/distribution.js +88 -0
  216. package/src/door/door.d.ts +41 -0
  217. package/src/door/door.js +219 -0
  218. package/src/door/dressed.d.ts +34 -0
  219. package/src/door/dressed.js +182 -0
  220. package/src/door/faculties.d.ts +29 -0
  221. package/src/door/faculties.js +88 -0
  222. package/src/door/longest.d.ts +6 -0
  223. package/src/door/longest.js +25 -0
  224. package/src/fills/account-only.d.ts +3 -0
  225. package/src/fills/account-only.js +24 -0
  226. package/src/fills/boot-key.d.ts +21 -0
  227. package/src/fills/boot-key.js +70 -0
  228. package/src/fills/boot-move.d.ts +25 -0
  229. package/src/fills/boot-move.js +86 -0
  230. package/src/fills/bridge.d.ts +13 -0
  231. package/src/fills/bridge.js +86 -0
  232. package/src/fills/chance.d.ts +2 -0
  233. package/src/fills/chance.js +7 -0
  234. package/src/fills/crypto.d.ts +2 -0
  235. package/src/fills/crypto.js +94 -0
  236. package/src/fills/flushed.d.ts +9 -0
  237. package/src/fills/flushed.js +80 -0
  238. package/src/fills/hardware.d.ts +18 -0
  239. package/src/fills/hardware.js +70 -0
  240. package/src/fills/held.d.ts +31 -0
  241. package/src/fills/held.js +171 -0
  242. package/src/fills/keepers.d.ts +23 -0
  243. package/src/fills/keepers.js +151 -0
  244. package/src/fills/keyring.d.ts +19 -0
  245. package/src/fills/keyring.js +131 -0
  246. package/src/fills/pages.d.ts +3 -0
  247. package/src/fills/pages.js +260 -0
  248. package/src/fills/reserve.d.ts +11 -0
  249. package/src/fills/reserve.js +72 -0
  250. package/src/fills/time.d.ts +2 -0
  251. package/src/fills/time.js +29 -0
  252. package/src/fills/unlocker.d.ts +6 -0
  253. package/src/fills/unlocker.js +26 -0
  254. package/src/fills/vitals.d.ts +8 -0
  255. package/src/fills/vitals.js +100 -0
  256. package/src/fills/windows-start.d.ts +2 -0
  257. package/src/fills/windows-start.js +21 -0
  258. package/src/halves/build.d.ts +10 -0
  259. package/src/halves/build.js +219 -0
  260. package/src/halves/end.d.ts +17 -0
  261. package/src/halves/end.js +157 -0
  262. package/src/halves/genome.d.ts +3 -0
  263. package/src/halves/genome.js +21 -0
  264. package/src/halves/handler.d.ts +8 -0
  265. package/src/halves/handler.js +40 -0
  266. package/src/halves/index.d.ts +5 -0
  267. package/src/halves/index.js +6 -0
  268. package/src/halves/laid.d.ts +12 -0
  269. package/src/halves/laid.js +103 -0
  270. package/src/halves/lent.d.ts +6 -0
  271. package/src/halves/lent.js +147 -0
  272. package/src/halves/native.d.ts +12 -0
  273. package/src/halves/native.js +53 -0
  274. package/src/halves/statements.d.ts +23 -0
  275. package/src/halves/statements.js +165 -0
  276. package/src/halves/tokens.d.ts +22 -0
  277. package/src/halves/tokens.js +135 -0
  278. package/src/link/link.d.ts +4 -0
  279. package/src/link/link.js +4 -0
  280. package/src/link/placeholder.d.ts +1 -0
  281. package/src/link/placeholder.js +21 -0
  282. package/src/link/steps.d.ts +52 -0
  283. package/src/link/steps.js +118 -0
  284. package/src/lobes/calls.d.ts +76 -0
  285. package/src/lobes/calls.js +61 -0
  286. package/src/lobes/channel.d.ts +76 -0
  287. package/src/lobes/channel.js +202 -0
  288. package/src/lobes/index.d.ts +1 -0
  289. package/src/lobes/index.js +2 -0
  290. package/src/lobes/lobes.d.ts +15 -0
  291. package/src/lobes/lobes.js +244 -0
  292. package/src/lobes/main.js +107 -0
  293. package/src/lobes/thread.d.ts +22 -0
  294. package/src/lobes/thread.js +270 -0
  295. package/src/organism/act.d.ts +8 -0
  296. package/src/organism/act.js +63 -0
  297. package/src/organism/beat.d.ts +5 -0
  298. package/src/organism/beat.js +45 -0
  299. package/src/organism/compiled.d.ts +2 -0
  300. package/src/organism/compiled.js +24 -0
  301. package/src/organism/folder.d.ts +88 -0
  302. package/src/organism/folder.js +259 -0
  303. package/src/organism/main.d.ts +1 -0
  304. package/src/organism/main.js +11 -0
  305. package/src/organism/organism.d.ts +1 -0
  306. package/src/organism/organism.js +220 -0
  307. package/src/organism/said.d.ts +12 -0
  308. package/src/organism/said.js +30 -0
  309. package/src/organism/told.d.ts +5 -0
  310. package/src/organism/told.js +110 -0
  311. package/src/organism/unlock.d.ts +8 -0
  312. package/src/organism/unlock.js +95 -0
  313. package/src/organism/upgrade.d.ts +24 -0
  314. package/src/organism/upgrade.js +145 -0
  315. package/src/organism/via.d.ts +7 -0
  316. package/src/organism/via.js +20 -0
  317. package/src/service/body.d.ts +8 -0
  318. package/src/service/body.js +34 -0
  319. package/src/service/handler.d.ts +20 -0
  320. package/src/service/handler.js +128 -0
  321. package/src/service/index.d.ts +4 -0
  322. package/src/service/index.js +16 -0
  323. package/src/service/manager.d.ts +18 -0
  324. package/src/service/manager.js +185 -0
  325. package/src/service/service.d.ts +24 -0
  326. package/src/service/service.js +200 -0
  327. package/src/service/swap.d.ts +12 -0
  328. package/src/service/swap.js +74 -0
  329. package/src/service/watchdog.d.ts +6 -0
  330. package/src/service/watchdog.js +31 -0
  331. package/src/tcp/crowd.d.ts +18 -0
  332. package/src/tcp/crowd.js +85 -0
  333. package/src/tcp/dial.d.ts +23 -0
  334. package/src/tcp/dial.js +102 -0
  335. package/src/tcp/frame.d.ts +23 -0
  336. package/src/tcp/frame.js +159 -0
  337. package/src/tcp/listen.d.ts +50 -0
  338. package/src/tcp/listen.js +207 -0
  339. package/src/tcp/source.d.ts +2 -0
  340. package/src/tcp/source.js +26 -0
  341. package/src/version/index.d.ts +1 -0
  342. package/src/version/index.js +2 -0
  343. package/src/version/version.d.ts +1 -0
  344. package/src/version/version.js +2 -0
  345. package/src/work/first.d.ts +11 -0
  346. package/src/work/first.js +91 -0
  347. package/src/work/index.d.ts +2 -0
  348. package/src/work/index.js +15 -0
  349. package/src/work/work.d.ts +20 -0
  350. package/src/work/work.js +136 -0
  351. package/src/ws/dial.d.ts +7 -0
  352. package/src/ws/dial.js +252 -0
  353. package/src/ws/dialing.d.ts +50 -0
  354. package/src/ws/dialing.js +72 -0
  355. package/src/ws/frame.d.ts +32 -0
  356. package/src/ws/frame.js +124 -0
  357. package/src/ws/handshake.d.ts +4 -0
  358. package/src/ws/handshake.js +27 -0
  359. package/src/ws/line.d.ts +45 -0
  360. package/src/ws/line.js +250 -0
  361. package/src/ws/listen.d.ts +42 -0
  362. package/src/ws/listen.js +272 -0
  363. package/src/ws/rfc6455.d.ts +18 -0
  364. package/src/ws/rfc6455.js +187 -0
@@ -0,0 +1,244 @@
1
+ // SPDX-License-Identifier: FSL-1.1-ALv2
2
+ import { totalmem } from 'node:os';
3
+
4
+
5
+
6
+ import { Thread } from './thread.js';
7
+
8
+ export const GRACE = 20;
9
+
10
+ export const WARM = 2;
11
+
12
+ const MEBIBYTE = 1024 * 1024;
13
+ const MOST_MEMORY = 2048 * MEBIBYTE;
14
+ const HOST = 320 * MEBIBYTE;
15
+ const WORKER = 16 * MEBIBYTE;
16
+
17
+ export const totalOf = (memory , bytes = totalmem()) =>
18
+ Math.max(1, Math.floor((bytes - HOST) / (WORKER + memory)));
19
+
20
+
21
+
22
+
23
+
24
+
25
+
26
+
27
+
28
+
29
+
30
+ class Threads {
31
+ #idle = [];
32
+ #atWork = new Map ();
33
+ #data ;
34
+ #defaults ;
35
+ #limit ;
36
+ #grace ;
37
+ #code ;
38
+ #compiled ;
39
+ #exits = new Set ();
40
+ #standing = 0;
41
+ #stopped = false;
42
+
43
+ constructor(
44
+ gear ,
45
+ code ,
46
+ limit ,
47
+ ) {
48
+ const { memory, rate, total = totalOf(memory), warm = WARM, grace = GRACE, compiled } = gear;
49
+ this.#data = { rate, memory };
50
+ this.#compiled = compiled;
51
+ this.#defaults = { total, warm };
52
+ this.#limit = limit;
53
+ this.#grace = grace;
54
+ this.#code = code;
55
+ this.#fillPool();
56
+ }
57
+
58
+ get #total() {
59
+ return this.#limit('workers') ?? this.#defaults.total;
60
+ }
61
+
62
+ get #warm() {
63
+ return this.#limit('warm') ?? this.#defaults.warm;
64
+ }
65
+
66
+ get #graceNow() {
67
+ return this.#limit('grace') ?? this.#grace;
68
+ }
69
+
70
+ #made(crossing = this.#data.memory) {
71
+ const around = {
72
+ grace: () => this.#graceNow,
73
+ code: this.#code,
74
+ ...(this.#compiled === undefined ? {} : { compiled: this.#compiled }),
75
+ gone: (thread , stood ) => this.#gone(thread, stood),
76
+ free: (thread ) => this.#free(thread),
77
+ };
78
+ const thread = new Thread(this.#data, around, crossing);
79
+ this.#exits.add(thread.exited);
80
+ void thread.exited.then(() => this.#exits.delete(thread.exited));
81
+ return thread;
82
+ }
83
+
84
+ #crossing(lobe ) {
85
+ return Math.min(this.#limit('lobeMemory', lobe) ?? this.#data.memory, MOST_MEMORY);
86
+ }
87
+
88
+ #ready(crossing ) {
89
+ const at = this.#idle.findLastIndex((one) => one.crossing >= crossing);
90
+ if (at >= 0) return this.#idle.splice(at, 1)[0] ?? null;
91
+ const smaller = this.#idle.pop();
92
+ if (smaller === undefined) return null;
93
+ smaller.end();
94
+ this.#standing -= 1;
95
+ return null;
96
+ }
97
+
98
+ #fillPool() {
99
+ for (; !this.#stopped && this.#idle.length < this.#warm && this.#standing < this.#total; this.#standing += 1)
100
+ this.#idle.push(this.#made());
101
+ }
102
+
103
+ #gone(thread , stood ) {
104
+ const at = this.#idle.indexOf(thread);
105
+ if (at >= 0) this.#idle.splice(at, 1);
106
+ if (thread.lobe !== null && this.#atWork.get(thread.lobe) === thread) this.#atWork.delete(thread.lobe);
107
+ if (!thread.core) this.#standing -= 1;
108
+ if (stood) this.#fillPool();
109
+ }
110
+
111
+ #free(thread ) {
112
+ if (thread.core) return thread.rest();
113
+ if (thread.lobe !== null) this.#atWork.delete(thread.lobe);
114
+ thread.lobe = null;
115
+ if (this.#idle.length >= 2 * this.#warm) {
116
+ thread.end();
117
+ this.#standing -= 1;
118
+ return;
119
+ }
120
+ thread.rest();
121
+ this.#idle.push(thread);
122
+ }
123
+
124
+ place(lobe , core ) {
125
+ const found = this.#atWork.get(lobe);
126
+ if (found !== undefined) return found;
127
+ const crossing = this.#crossing(lobe);
128
+ let thread = this.#ready(crossing);
129
+ if (thread !== null && core) this.#standing -= 1;
130
+ if (thread === null && (core || this.#standing < this.#total)) {
131
+ thread = this.#made(crossing);
132
+ if (!core) this.#standing += 1;
133
+ }
134
+ if (thread === null) return null;
135
+ thread.lobe = lobe;
136
+ thread.core = core;
137
+ this.#atWork.set(lobe, thread);
138
+ this.#fillPool();
139
+ return thread;
140
+ }
141
+
142
+ end() {
143
+ this.#stopped = true;
144
+ for (const thread of [...this.#idle, ...this.#atWork.values()]) thread.end();
145
+ this.#idle.length = 0;
146
+ this.#atWork.clear();
147
+ }
148
+
149
+ async ended() {
150
+ await Promise.all(this.#exits);
151
+ }
152
+ }
153
+
154
+
155
+
156
+
157
+
158
+
159
+
160
+
161
+
162
+
163
+ class Far {
164
+ id ;
165
+ gear ;
166
+ fell ;
167
+ #threads ;
168
+ #opening ;
169
+ #thread = null;
170
+ #begin = () => undefined;
171
+ reached = false;
172
+ ended = false;
173
+
174
+ constructor(id , threads , opening , gear , fell ) {
175
+ this.id = id;
176
+ this.gear = gear;
177
+ this.fell = fell;
178
+ this.#threads = threads;
179
+ this.#opening = opening;
180
+ }
181
+
182
+ begin() {
183
+ this.#begin();
184
+ }
185
+
186
+ start(work , step ) {
187
+ const { lobe, core, reads, digest, input } = this.#opening;
188
+ const { kind, memory } = this.gear;
189
+ this.#begin = () => {
190
+ this.#thread = this.#threads.place(lobe, core);
191
+ if (this.#thread === null) {
192
+ this.ended = true;
193
+ return step({ busy: 'no thread is free for a lobe to work in' });
194
+ }
195
+ this.#thread.call(this, {
196
+ call: { op: 'start', lobe, digest, input, kind, reads, ...(memory === undefined ? {} : { memory }) },
197
+ work,
198
+ step,
199
+ });
200
+ };
201
+ this.#begin();
202
+ }
203
+
204
+ #called(asked ) {
205
+ if (this.#thread === null) throw new TypeError('the run has not started');
206
+ this.#thread.call(this, asked);
207
+ }
208
+
209
+ settle = (wait, answer, work, step) =>
210
+ this.#called({ call: { op: 'settle', wait, answer }, work, step });
211
+
212
+ renew = (work ) => this.#thread?.renew(this, work);
213
+
214
+ run = (input, work, ran) => this.#called({ call: { op: 'run', input }, work, ran });
215
+
216
+ read = (source, work, readout) => this.#called({ call: { op: 'read', source }, work, readout });
217
+
218
+ end() {
219
+ if (this.ended) return;
220
+ this.ended = true;
221
+ this.#thread?.finish(this);
222
+ }
223
+ }
224
+
225
+ export const nodeLobes = (gear ) => {
226
+ const made = [];
227
+ const lobesOf = (code, limit = () => undefined) => {
228
+ const threads = new Threads(gear, code, limit);
229
+ made.push(threads);
230
+ let ids = 0;
231
+ return {
232
+ fill: (lobe, core, reads) => ({
233
+ reads,
234
+ counted: false,
235
+ open: (_lobe, digest, input, askGear, fell) => {
236
+ ids += 1;
237
+ return new Far(ids, threads, { lobe, core, reads, digest, input }, askGear, fell);
238
+ },
239
+ }),
240
+ end: () => threads.end(),
241
+ };
242
+ };
243
+ return Object.assign(lobesOf, { ended: async () => void (await Promise.all(made.map((one) => one.ended()))) });
244
+ };
@@ -0,0 +1,107 @@
1
+ // SPDX-License-Identifier: FSL-1.1-ALv2
2
+ import { setFlagsFromString } from 'node:v8';
3
+ import { runInNewContext } from 'node:vm';
4
+ import { isMainThread, workerData, } from 'node:worker_threads';
5
+ import { fillOn, genomeCodec, } from '@nervur.org/genome/cage';
6
+ import { loadQuickJS, makeEngine } from '@nervur.org/genome/engine';
7
+ import { nodeCrypto } from '../fills/crypto.js';
8
+
9
+ import { threadEnd, } from './channel.js';
10
+
11
+ const REST = 100;
12
+
13
+ const UNBOUNDED = Number.POSITIVE_INFINITY;
14
+
15
+ const gearOf = (
16
+ end ,
17
+ run ,
18
+ kind ,
19
+ reads ,
20
+ memory ,
21
+ ) => ({
22
+ kind,
23
+ ...(memory === undefined ? {} : { memory }),
24
+ codec: genomeCodec,
25
+ crypto: nodeCrypto,
26
+ cells: {
27
+ get: (key) => end.ask({ name: 'get', run, key }),
28
+ set: (key, value) => end.ask({ name: 'set', run, key, value }),
29
+ delete: (key) => end.ask({ name: 'delete', run, key }),
30
+ range: (range) => end.ask({ name: 'range', run, range }),
31
+ },
32
+ ask: (ask) => end.ask({ name: 'ask', run, ask }),
33
+ run: (input) => end.ask({ name: 'run', run, input }),
34
+ ...(reads ? { read: (source ) => end.ask({ name: 'read', run, source }) } : {}),
35
+ });
36
+
37
+ const collector = () => {
38
+ setFlagsFromString('--expose-gc');
39
+ const collect = runInNewContext('gc');
40
+ return () => {
41
+ if (typeof collect === 'function') collect();
42
+ };
43
+ };
44
+
45
+ const calling = (fill , end , tell ) => {
46
+ const runs = new Map ();
47
+ const stepOf =
48
+ (number , run ) =>
49
+ (told) => {
50
+ if ('cut' in told || 'busy' in told) return;
51
+ if (!('waiting' in told)) runs.delete(run);
52
+ tell({ number, step: told });
53
+ };
54
+ const opened = (call ) => {
55
+ const { number, run, lobe, digest, input, kind, reads, memory } = call;
56
+ const apart = fill.open(lobe, digest, input, gearOf(end, run, kind, reads, memory), () => undefined);
57
+ runs.set(run, apart);
58
+ apart.start(UNBOUNDED, stepOf(number, run));
59
+ };
60
+ const onLive = (live , call ) => {
61
+ const { number } = call;
62
+ if (call.op === 'settle') return live.settle(call.wait, call.answer, UNBOUNDED, stepOf(number, call.run));
63
+ if (call.op === 'run')
64
+ return live.run(call.input, UNBOUNDED, (ran) => {
65
+ if (!('cut' in ran) && !('busy' in ran)) tell({ number, ran });
66
+ });
67
+ return live.read(call.source, UNBOUNDED, (readout) => {
68
+ if (!('cut' in readout) && !('busy' in readout)) tell({ number, readout });
69
+ });
70
+ };
71
+ return (call) => {
72
+ end.running(call.number);
73
+ if (call.op === 'start') return opened(call);
74
+ const live = runs.get(call.run);
75
+ if (call.op === 'end') {
76
+ runs.delete(call.run);
77
+ live?.end();
78
+ return tell({ number: call.number, done: true });
79
+ }
80
+ if (live !== undefined) return onLive(live, call);
81
+ tell({ number: call.number, step: { broken: 'the run has ended' } });
82
+ };
83
+ };
84
+
85
+ const start = async (port , { shared, rate, memory } ) => {
86
+ const end = threadEnd(shared);
87
+ const code = (digest ) => end.ask({ name: 'code', run: 0, digest });
88
+ const engine = makeEngine(await loadQuickJS(), rate, undefined, end.cut);
89
+ const fill = fillOn({ engine, crypto: nodeCrypto, codec: genomeCodec, memory, rests: true }, code);
90
+ const called = calling(fill, end, (came) => port.postMessage({ ...came, worked: end.worked() } ));
91
+ const collect = collector();
92
+ let resting ;
93
+ port.on('message', (told ) => {
94
+ clearTimeout(resting);
95
+ if (told.op === 'idle')
96
+ resting = setTimeout(() => {
97
+ fill.rest();
98
+ collect();
99
+ }, REST);
100
+ else called(told);
101
+ });
102
+ port.postMessage({ ready: true } );
103
+ };
104
+
105
+ if (isMainThread) throw new Error('a lobe thread runs in a worker');
106
+ const data = workerData;
107
+ void start(data.port, data);
@@ -0,0 +1,22 @@
1
+ import type { Asked, Standing, ThreadData } from './calls.js';
2
+ type Around = {
3
+ grace(): number;
4
+ readonly code: (digest: string) => string | null;
5
+ readonly compiled?: string;
6
+ gone(thread: Thread, stood: boolean): void;
7
+ free(thread: Thread): void;
8
+ };
9
+ export declare class Thread {
10
+ #private;
11
+ lobe: string | null;
12
+ core: boolean;
13
+ readonly exited: Promise<void>;
14
+ readonly crossing: number;
15
+ constructor(data: Omit<ThreadData, 'shared' | 'port'>, around: Around, crossing?: number);
16
+ call(run: Standing, asked: Asked): void;
17
+ renew(run: Standing, work: number): void;
18
+ finish(run: Standing): void;
19
+ rest(): void;
20
+ end(): void;
21
+ }
22
+ export {};
@@ -0,0 +1,270 @@
1
+ // SPDX-License-Identifier: FSL-1.1-ALv2
2
+ import { extname } from 'node:path';
3
+ import { fileURLToPath } from 'node:url';
4
+ import { MessageChannel, receiveMessageOnPort, Worker, } from 'node:worker_threads';
5
+
6
+ import { brainEnd, processorWorked, sharedOf, } from './channel.js';
7
+
8
+ const WORKER_FILE = new URL(`./main${extname(fileURLToPath(import.meta.url))}`, import.meta.url);
9
+
10
+
11
+
12
+
13
+
14
+
15
+
16
+
17
+
18
+
19
+
20
+ const finishes = (heard ) => 'step' in heard && !('waiting' in heard.step);
21
+
22
+
23
+
24
+
25
+
26
+
27
+
28
+
29
+
30
+
31
+
32
+
33
+
34
+ const LONGEST_TIMER = 2 ** 31 - 1;
35
+
36
+ export class Thread {
37
+ #worker ;
38
+ #port ;
39
+ #channel ;
40
+ #around ;
41
+ #queue = [];
42
+ #runs = new Set ();
43
+ #calling = null;
44
+ #ready = false;
45
+ #number = 0;
46
+ #graced = null;
47
+ #ended = false;
48
+ lobe = null;
49
+ core = false;
50
+
51
+ exited ;
52
+
53
+ crossing ;
54
+
55
+ constructor(data , around , crossing = data.memory) {
56
+ const shared = sharedOf(crossing);
57
+ const { port1, port2 } = new MessageChannel();
58
+ this.crossing = crossing;
59
+ this.#around = around;
60
+ this.#port = port1;
61
+ this.#channel = brainEnd(shared, (question) => this.#served(question));
62
+ this.#worker = new Worker(WORKER_FILE, {
63
+ workerData: { ...data, shared, port: port2 } ,
64
+ transferList: [port2],
65
+ ...(around.compiled === undefined ? {} : { env: { ...process.env, NODE_COMPILE_CACHE: around.compiled } }),
66
+ });
67
+ this.exited = new Promise((done) => this.#worker.once('exit', () => done()));
68
+ this.#worker.unref();
69
+ this.#port.unref();
70
+ this.#port.on('message', (heard ) => this.#heard(heard));
71
+ this.#worker.on('error', (error) => this.#fall(`the lobe's thread broke: ${error.message}`));
72
+ this.#worker.on('exit', () => this.#fall("the lobe's thread ended"));
73
+ }
74
+
75
+ call(run , asked ) {
76
+ this.#runs.add(run);
77
+ this.#queue.push({ ...asked, run });
78
+ this.#pump();
79
+ }
80
+
81
+ renew(run , work ) {
82
+ const calling = this.#calling;
83
+ if (calling?.run !== run || calling.cut || (calling.timer === null && !calling.reading)) return;
84
+ clearTimeout(calling.timer ?? undefined);
85
+ calling.budget = this.#channel.worked() + work;
86
+ if (!calling.reading) this.#watch(calling, work);
87
+ }
88
+
89
+ #watch(calling , span ) {
90
+ if (span > LONGEST_TIMER) return;
91
+ calling.timer = setTimeout(() => this.#expired(calling), Math.max(0, Math.ceil(span)));
92
+ calling.timer.unref();
93
+ }
94
+
95
+ #read() {
96
+ for (let told = receiveMessageOnPort(this.#port); told !== undefined; told = receiveMessageOnPort(this.#port)) {
97
+ const heard = told.message;
98
+ this.#heard(heard);
99
+ }
100
+ }
101
+
102
+ finish(run ) {
103
+ for (let at = this.#queue.length - 1; at >= 0; at -= 1) if (this.#queue[at]?.run === run) this.#queue.splice(at, 1);
104
+ this.#runs.delete(run);
105
+ const calling = this.#calling;
106
+ if (calling?.run === run) this.#cut(calling);
107
+ if (run.reached) this.#queue.push({ run, call: { op: 'end' }, work: Infinity, done: true });
108
+ this.#pump();
109
+ this.#settled();
110
+ }
111
+
112
+ rest() {
113
+ this.#port.postMessage({ op: 'idle' } , []);
114
+ }
115
+
116
+ end() {
117
+ this.#ended = true;
118
+ this.#stop();
119
+ }
120
+
121
+ #stop() {
122
+ if (this.#calling?.timer != null) clearTimeout(this.#calling.timer);
123
+ if (this.#graced !== null) clearTimeout(this.#graced);
124
+ this.#channel.close();
125
+ this.#port.close();
126
+ void this.#worker.terminate();
127
+ }
128
+
129
+ #pump() {
130
+ if (!this.#ready || this.#calling !== null || this.#ended) return;
131
+ const next = this.#queue.shift();
132
+ if (next === undefined) return;
133
+ this.#number = (this.#number + 1) | 0;
134
+ const number = this.#number;
135
+ next.run.reached = true;
136
+ const calling = { ...next, number, budget: next.work, timer: null, reading: false, cut: false };
137
+ this.#calling = calling;
138
+ this.#worker.ref();
139
+ this.#channel.begun();
140
+ this.#port.postMessage({ ...next.call, number, run: next.run.id } , []);
141
+ this.#watch(calling, next.work);
142
+ }
143
+
144
+ #expired(calling ) {
145
+ calling.timer = null;
146
+ this.#read();
147
+ if (this.#calling !== calling || calling.cut) return;
148
+ const most = this.#channel.worked();
149
+ if (most < calling.budget) return this.#watch(calling, calling.budget - most);
150
+ calling.reading = true;
151
+ void processorWorked(this.#channel, calling.number, this.#worker).then((worked) => {
152
+ calling.reading = false;
153
+ this.#read();
154
+ if (this.#calling !== calling || calling.cut) return;
155
+ if (worked !== null && worked < calling.budget) return this.#watch(calling, calling.budget - worked);
156
+ this.#cut(calling);
157
+ Thread.#tellCut(calling, worked ?? most);
158
+ });
159
+ }
160
+
161
+ static #tellCut(calling , worked ) {
162
+ if ('step' in calling) calling.step({ cut: true }, worked);
163
+ else if ('ran' in calling) calling.ran({ cut: true }, worked);
164
+ else if ('readout' in calling) calling.readout({ cut: true }, worked);
165
+ }
166
+
167
+ #cut(calling ) {
168
+ calling.cut = true;
169
+ if (calling.timer !== null) clearTimeout(calling.timer);
170
+ calling.timer = null;
171
+ this.#channel.cut(calling.number);
172
+ const grace = this.#around.grace();
173
+ if (this.#graced !== null || grace > LONGEST_TIMER) return;
174
+ this.#graced = setTimeout(() => {
175
+ this.#read();
176
+ if (this.#calling === calling) this.#fall(null);
177
+ }, grace);
178
+ this.#graced.unref();
179
+ }
180
+
181
+ #heard(heard ) {
182
+ if (this.#ended) return;
183
+ if ('ready' in heard) {
184
+ this.#ready = true;
185
+ return this.#pump();
186
+ }
187
+ const calling = this.#calling;
188
+ if (calling?.number !== heard.number) return;
189
+ if (calling.timer !== null) clearTimeout(calling.timer);
190
+ if (this.#graced !== null) clearTimeout(this.#graced);
191
+ this.#graced = null;
192
+ this.#calling = null;
193
+ this.#worker.unref();
194
+ if (finishes(heard)) {
195
+ calling.run.ended = true;
196
+ this.#runs.delete(calling.run);
197
+ }
198
+ if (!calling.cut && heard.worked > calling.budget) Thread.#tellCut(calling, heard.worked);
199
+ else if (!calling.cut) Thread.#tell(calling, heard, heard.worked);
200
+ this.#pump();
201
+ this.#settled();
202
+ }
203
+
204
+ static #tell(calling , heard , worked ) {
205
+ if ('step' in calling && 'step' in heard) calling.step(heard.step, worked);
206
+ else if ('ran' in calling && 'ran' in heard) calling.ran(heard.ran, worked);
207
+ else if ('readout' in calling && 'readout' in heard) calling.readout(heard.readout, worked);
208
+ else if ('step' in heard && 'broken' in heard.step) Thread.#broken(calling, heard.step.broken, worked);
209
+ }
210
+
211
+ static #broken(calling , broken , worked ) {
212
+ if ('ran' in calling) calling.ran({ broken }, worked);
213
+ else if ('readout' in calling) calling.readout({ broken }, worked);
214
+ }
215
+
216
+ #settled() {
217
+ if (this.#ended || this.#runs.size > 0 || this.#calling !== null || this.#queue.length > 0) return;
218
+ this.#around.free(this);
219
+ }
220
+
221
+ #served(question ) {
222
+ try {
223
+ return this.#serve(question);
224
+ } catch (thrown) {
225
+ return { gone: thrown instanceof Error ? thrown.message : String(thrown) };
226
+ }
227
+ }
228
+
229
+ #serve(question ) {
230
+ if (question.name === 'code') return { answer: this.#around.code(question.digest) };
231
+ const calling = this.#calling;
232
+ if (calling?.run.id !== question.run || calling.cut || calling.run.ended) return { gone: 'the run is cut' };
233
+ const { gear } = calling.run;
234
+ switch (question.name) {
235
+ case 'get':
236
+ return { answer: gear.cells.get(question.key) };
237
+ case 'delete':
238
+ return { answer: gear.cells.delete(question.key) };
239
+ case 'set':
240
+ return { answer: gear.cells.set(question.key, question.value) };
241
+ case 'range':
242
+ return { answer: gear.cells.range(question.range) };
243
+ case 'ask':
244
+ return { answer: gear.ask(question.ask) };
245
+ case 'read':
246
+ return gear.read === undefined
247
+ ? { gone: "read is the library's alone" }
248
+ : { answer: gear.read(question.source) };
249
+ default:
250
+ return gear.run === undefined
251
+ ? { gone: 'this run runs nothing' }
252
+ : { answer: gear.run(question.input, undefined) };
253
+ }
254
+ }
255
+
256
+ #fall(why ) {
257
+ if (this.#ended) return;
258
+ this.#ended = true;
259
+ this.#stop();
260
+ this.#around.gone(this, this.#ready);
261
+ const runs = [...this.#runs];
262
+ this.#runs.clear();
263
+ const begins = (run ) => this.#ready && !run.reached;
264
+ for (const run of runs.filter((one) => !begins(one))) {
265
+ run.ended = true;
266
+ run.fell(why);
267
+ }
268
+ for (const run of runs.filter(begins)) run.begin();
269
+ }
270
+ }
@@ -0,0 +1,8 @@
1
+ import { type Listening, type Typed } from '../../organs/console/node/index.js';
2
+ import type { Door } from '../door/door.js';
3
+ export declare const actOf: (home: string) => string;
4
+ export type Acting = Pick<Door, 'seal'>;
5
+ export declare const actAnswer: (acting: Acting) => Typed;
6
+ export declare const listenAct: (home: string, acting: Acting) => Promise<Listening | {
7
+ readonly why: string;
8
+ }>;