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

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 (362) hide show
  1. package/LICENSE +105 -0
  2. package/README.md +158 -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 +30756 -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 +256 -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 +41 -0
  109. package/src/command/before.js +237 -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 +17 -0
  193. package/src/command/trial.js +92 -0
  194. package/src/command/vouch.d.ts +1 -0
  195. package/src/command/vouch.js +15 -0
  196. package/src/command/windows-processes.d.ts +2 -0
  197. package/src/command/windows-processes.js +22 -0
  198. package/src/command/wipe.d.ts +9 -0
  199. package/src/command/wipe.js +105 -0
  200. package/src/command/words.d.ts +5 -0
  201. package/src/command/words.js +53 -0
  202. package/src/door/claim.d.ts +5 -0
  203. package/src/door/claim.js +21 -0
  204. package/src/door/claimed.d.ts +7 -0
  205. package/src/door/claimed.js +110 -0
  206. package/src/door/configuration.d.ts +40 -0
  207. package/src/door/configuration.js +206 -0
  208. package/src/door/design.d.ts +13 -0
  209. package/src/door/design.js +71 -0
  210. package/src/door/digests.d.ts +7 -0
  211. package/src/door/digests.js +36 -0
  212. package/src/door/distribution.d.ts +25 -0
  213. package/src/door/distribution.js +88 -0
  214. package/src/door/door.d.ts +41 -0
  215. package/src/door/door.js +219 -0
  216. package/src/door/dressed.d.ts +34 -0
  217. package/src/door/dressed.js +182 -0
  218. package/src/door/faculties.d.ts +29 -0
  219. package/src/door/faculties.js +88 -0
  220. package/src/door/longest.d.ts +6 -0
  221. package/src/door/longest.js +25 -0
  222. package/src/fills/account-only.d.ts +3 -0
  223. package/src/fills/account-only.js +24 -0
  224. package/src/fills/boot-key.d.ts +21 -0
  225. package/src/fills/boot-key.js +70 -0
  226. package/src/fills/boot-move.d.ts +25 -0
  227. package/src/fills/boot-move.js +86 -0
  228. package/src/fills/bridge.d.ts +13 -0
  229. package/src/fills/bridge.js +86 -0
  230. package/src/fills/chance.d.ts +2 -0
  231. package/src/fills/chance.js +7 -0
  232. package/src/fills/crypto.d.ts +2 -0
  233. package/src/fills/crypto.js +94 -0
  234. package/src/fills/flushed.d.ts +9 -0
  235. package/src/fills/flushed.js +80 -0
  236. package/src/fills/hardware.d.ts +18 -0
  237. package/src/fills/hardware.js +70 -0
  238. package/src/fills/held.d.ts +31 -0
  239. package/src/fills/held.js +171 -0
  240. package/src/fills/keepers.d.ts +23 -0
  241. package/src/fills/keepers.js +151 -0
  242. package/src/fills/keyring.d.ts +19 -0
  243. package/src/fills/keyring.js +131 -0
  244. package/src/fills/pages.d.ts +3 -0
  245. package/src/fills/pages.js +260 -0
  246. package/src/fills/reserve.d.ts +11 -0
  247. package/src/fills/reserve.js +72 -0
  248. package/src/fills/time.d.ts +2 -0
  249. package/src/fills/time.js +29 -0
  250. package/src/fills/unlocker.d.ts +6 -0
  251. package/src/fills/unlocker.js +26 -0
  252. package/src/fills/vitals.d.ts +8 -0
  253. package/src/fills/vitals.js +100 -0
  254. package/src/fills/windows-start.d.ts +2 -0
  255. package/src/fills/windows-start.js +21 -0
  256. package/src/halves/build.d.ts +10 -0
  257. package/src/halves/build.js +219 -0
  258. package/src/halves/end.d.ts +17 -0
  259. package/src/halves/end.js +157 -0
  260. package/src/halves/genome.d.ts +3 -0
  261. package/src/halves/genome.js +21 -0
  262. package/src/halves/handler.d.ts +8 -0
  263. package/src/halves/handler.js +40 -0
  264. package/src/halves/index.d.ts +5 -0
  265. package/src/halves/index.js +6 -0
  266. package/src/halves/laid.d.ts +12 -0
  267. package/src/halves/laid.js +103 -0
  268. package/src/halves/lent.d.ts +6 -0
  269. package/src/halves/lent.js +147 -0
  270. package/src/halves/native.d.ts +12 -0
  271. package/src/halves/native.js +53 -0
  272. package/src/halves/statements.d.ts +23 -0
  273. package/src/halves/statements.js +165 -0
  274. package/src/halves/tokens.d.ts +22 -0
  275. package/src/halves/tokens.js +135 -0
  276. package/src/link/link.d.ts +4 -0
  277. package/src/link/link.js +4 -0
  278. package/src/link/placeholder.d.ts +1 -0
  279. package/src/link/placeholder.js +21 -0
  280. package/src/link/steps.d.ts +52 -0
  281. package/src/link/steps.js +118 -0
  282. package/src/lobes/calls.d.ts +76 -0
  283. package/src/lobes/calls.js +61 -0
  284. package/src/lobes/channel.d.ts +76 -0
  285. package/src/lobes/channel.js +202 -0
  286. package/src/lobes/index.d.ts +1 -0
  287. package/src/lobes/index.js +2 -0
  288. package/src/lobes/lobes.d.ts +15 -0
  289. package/src/lobes/lobes.js +244 -0
  290. package/src/lobes/main.js +107 -0
  291. package/src/lobes/thread.d.ts +22 -0
  292. package/src/lobes/thread.js +270 -0
  293. package/src/organism/act.d.ts +8 -0
  294. package/src/organism/act.js +63 -0
  295. package/src/organism/beat.d.ts +5 -0
  296. package/src/organism/beat.js +45 -0
  297. package/src/organism/compiled.d.ts +2 -0
  298. package/src/organism/compiled.js +24 -0
  299. package/src/organism/folder.d.ts +88 -0
  300. package/src/organism/folder.js +259 -0
  301. package/src/organism/main.d.ts +1 -0
  302. package/src/organism/main.js +11 -0
  303. package/src/organism/organism.d.ts +1 -0
  304. package/src/organism/organism.js +219 -0
  305. package/src/organism/said.d.ts +12 -0
  306. package/src/organism/said.js +30 -0
  307. package/src/organism/told.d.ts +5 -0
  308. package/src/organism/told.js +110 -0
  309. package/src/organism/unlock.d.ts +8 -0
  310. package/src/organism/unlock.js +95 -0
  311. package/src/organism/upgrade.d.ts +24 -0
  312. package/src/organism/upgrade.js +145 -0
  313. package/src/organism/via.d.ts +7 -0
  314. package/src/organism/via.js +20 -0
  315. package/src/service/body.d.ts +8 -0
  316. package/src/service/body.js +34 -0
  317. package/src/service/handler.d.ts +20 -0
  318. package/src/service/handler.js +128 -0
  319. package/src/service/index.d.ts +4 -0
  320. package/src/service/index.js +16 -0
  321. package/src/service/manager.d.ts +18 -0
  322. package/src/service/manager.js +185 -0
  323. package/src/service/service.d.ts +24 -0
  324. package/src/service/service.js +200 -0
  325. package/src/service/swap.d.ts +12 -0
  326. package/src/service/swap.js +74 -0
  327. package/src/service/watchdog.d.ts +6 -0
  328. package/src/service/watchdog.js +31 -0
  329. package/src/tcp/crowd.d.ts +18 -0
  330. package/src/tcp/crowd.js +85 -0
  331. package/src/tcp/dial.d.ts +23 -0
  332. package/src/tcp/dial.js +102 -0
  333. package/src/tcp/frame.d.ts +23 -0
  334. package/src/tcp/frame.js +159 -0
  335. package/src/tcp/listen.d.ts +50 -0
  336. package/src/tcp/listen.js +207 -0
  337. package/src/tcp/source.d.ts +2 -0
  338. package/src/tcp/source.js +26 -0
  339. package/src/version/index.d.ts +1 -0
  340. package/src/version/index.js +2 -0
  341. package/src/version/version.d.ts +1 -0
  342. package/src/version/version.js +2 -0
  343. package/src/work/first.d.ts +11 -0
  344. package/src/work/first.js +91 -0
  345. package/src/work/index.d.ts +2 -0
  346. package/src/work/index.js +15 -0
  347. package/src/work/work.d.ts +20 -0
  348. package/src/work/work.js +136 -0
  349. package/src/ws/dial.d.ts +7 -0
  350. package/src/ws/dial.js +252 -0
  351. package/src/ws/dialing.d.ts +50 -0
  352. package/src/ws/dialing.js +72 -0
  353. package/src/ws/frame.d.ts +32 -0
  354. package/src/ws/frame.js +124 -0
  355. package/src/ws/handshake.d.ts +4 -0
  356. package/src/ws/handshake.js +27 -0
  357. package/src/ws/line.d.ts +45 -0
  358. package/src/ws/line.js +250 -0
  359. package/src/ws/listen.d.ts +42 -0
  360. package/src/ws/listen.js +272 -0
  361. package/src/ws/rfc6455.d.ts +18 -0
  362. package/src/ws/rfc6455.js +187 -0
@@ -0,0 +1,266 @@
1
+ // SPDX-License-Identifier: FSL-1.1-ALv2
2
+
3
+ import { FRAME_SIZE, LONGEST_TIMEOUT } from '../../../src/door/longest.js';
4
+ import { heldOf, readThrough, } from './held.js';
5
+
6
+
7
+
8
+
9
+
10
+
11
+
12
+
13
+
14
+
15
+ export const MEDIA = new Map([
16
+ ['avif', 'image/avif'],
17
+ ['gif', 'image/gif'],
18
+ ['jpg', 'image/jpeg'],
19
+ ['png', 'image/png'],
20
+ ['webp', 'image/webp'],
21
+ ['mp3', 'audio/mpeg'],
22
+ ['ogg', 'audio/ogg'],
23
+ ['wav', 'audio/wav'],
24
+ ]);
25
+
26
+
27
+
28
+
29
+
30
+
31
+
32
+
33
+
34
+
35
+ export const MOST_FILE = 4 * FRAME_SIZE;
36
+
37
+
38
+
39
+
40
+
41
+
42
+
43
+
44
+ const hex = (bytes ) =>
45
+ [...new Uint8Array(bytes)].map((byte) => byte.toString(16).padStart(2, '0')).join('');
46
+ const sha256Of = async (bytes ) => hex(await crypto.subtle.digest('SHA-256', bytes));
47
+
48
+ const fetchedOf = async (
49
+ channel ,
50
+ size ,
51
+ reaches ,
52
+ ) => {
53
+ const { url, expires } = channel;
54
+ if (typeof url !== 'string' || typeof expires !== 'number') return { why: 'a file names its address and expiry' };
55
+ if (expires <= reaches.now()) return { why: 'the file’s address has expired' };
56
+ const got = await reaches.get(url, size);
57
+ if ('why' in got) return got;
58
+ return got.status === 200 ? { bytes: got.bytes } : { why: `the file’s address answered ${got.status}` };
59
+ };
60
+
61
+ const fileOf = async (channel , reaches , read ) => {
62
+ const { size, sha256, press } = channel;
63
+ const ext = typeof channel['ext'] === 'string' ? channel['ext'] : '';
64
+ const type = MEDIA.get(ext);
65
+ if (typeof size !== 'number' || typeof sha256 !== 'string' || type === undefined)
66
+ return { why: 'a file names its size, SHA-256 and kind' };
67
+ if (size > MOST_FILE) return { why: `a file past the cap of ${MOST_FILE} bytes is not followed` };
68
+ const got =
69
+ channel['url'] === undefined
70
+ ? await heldOf(typeof press === 'string' ? press : '', size, read)
71
+ : await fetchedOf(channel, size, reaches);
72
+ if ('why' in got) return got;
73
+ if (got.bytes.length !== size) return { why: 'the file is not the size its lobe said' };
74
+ const digest = await sha256Of(got.bytes);
75
+ if (digest !== sha256) return { why: 'the file is not the SHA-256 its lobe said' };
76
+ return { kind: 'file', name: `${digest}.${ext}`, type, size, sha256: digest, bytes: got.bytes };
77
+ };
78
+
79
+ const sendable = async (
80
+ channel ,
81
+ given ,
82
+ now ,
83
+ ) => {
84
+ const { url, expires, size, sha256 } = channel;
85
+ if (typeof url !== 'string' || typeof expires !== 'number' || typeof size !== 'number' || typeof sha256 !== 'string')
86
+ return { why: 'an upload names its address, expiry, size and SHA-256' };
87
+ if (expires <= now) return { why: 'the upload’s address has expired' };
88
+ const bytes = given.get(sha256);
89
+ if (bytes === undefined) return { why: 'the file is not held on this device' };
90
+ if (bytes.length !== size) return { why: 'the file held is not the size its press carried' };
91
+ return (await sha256Of(bytes)) === sha256
92
+ ? { url, bytes }
93
+ : { why: 'the file held is not the SHA-256 its press carried' };
94
+ };
95
+
96
+ const uploadOf = async (channel , reaches , given ) => {
97
+ const press = typeof channel['press'] === 'string' ? channel['press'] : '';
98
+ const sent = await sendable(channel, given, reaches.now());
99
+ if ('why' in sent) return { kind: 'upload', press, landed: false, why: sent.why };
100
+ const put = await reaches.put(sent.url, sent.bytes);
101
+ if ('why' in put) return { kind: 'upload', press, landed: false, why: put.why };
102
+ return put.status >= 200 && put.status < 300
103
+ ? { kind: 'upload', press, landed: true }
104
+ : { kind: 'upload', press, landed: false, why: `the upload’s address answered ${put.status}` };
105
+ };
106
+
107
+ export const isFile = (value ) => {
108
+ if (typeof value !== 'object' || value === null || Array.isArray(value) || Object.keys(value).length !== 4)
109
+ return false;
110
+ const [name, type, size, sha256] = ['name', 'type', 'size', 'sha256'].map((key) => Reflect.get(value, key));
111
+ return (
112
+ typeof name === 'string' &&
113
+ typeof type === 'string' &&
114
+ typeof size === 'number' &&
115
+ Number.isInteger(size) &&
116
+ size >= 0 &&
117
+ typeof sha256 === 'string' &&
118
+ /^[0-9a-f]{64}$/u.test(sha256)
119
+ );
120
+ };
121
+
122
+ export const giveFile = async (kept , bytes ) => {
123
+ if (bytes.length > MOST_FILE) return { why: `a file past the cap of ${MOST_FILE} bytes is not held` };
124
+ const sha256 = await sha256Of(bytes);
125
+ kept.given.delete(sha256);
126
+ kept.given.set(sha256, bytes);
127
+ for (const [name] of kept.given) {
128
+ if (kept.given.size <= MOST_KEPT) break;
129
+ kept.given.delete(name);
130
+ }
131
+ return sha256;
132
+ };
133
+
134
+ export const followChannel = async (
135
+ channel ,
136
+ reaches ,
137
+ read ,
138
+ given = new Map(),
139
+ ) => {
140
+ const { kind } = channel;
141
+ if (kind === 'file') {
142
+ const got = await fileOf(channel, reaches, read);
143
+ const press = typeof channel['press'] === 'string' ? channel['press'] : '';
144
+ return 'why' in got ? { kind, press, landed: false, why: got.why } : got;
145
+ }
146
+ if (kind === 'upload') return uploadOf(channel, reaches, given);
147
+ if (kind === 'call' || kind === 'stream') return { why: 'unavailable', kind };
148
+ return { why: 'a channel is a file, an upload, a call or a stream' };
149
+ };
150
+
151
+ const putWithin =
152
+ (quiet ) =>
153
+ async (url, bytes) => {
154
+ const stop = new AbortController();
155
+ const still = setTimeout(() => stop.abort(), quiet);
156
+ try {
157
+ const response = await fetch(url, {
158
+ method: 'PUT',
159
+ body: bytes,
160
+ redirect: 'error',
161
+ credentials: 'omit',
162
+ cache: 'no-store',
163
+ signal: stop.signal,
164
+ });
165
+ await response.body?.cancel();
166
+ return { status: response.status };
167
+ } catch (thrown) {
168
+ return { why: `the file could not be sent: ${thrown instanceof Error ? thrown.message : String(thrown)}` };
169
+ } finally {
170
+ clearTimeout(still);
171
+ stop.abort();
172
+ }
173
+ };
174
+
175
+ export const fetchedWithin = (quiet = LONGEST_TIMEOUT) => ({
176
+ now: Date.now,
177
+ put: putWithin(quiet),
178
+ get: async (url, size) => {
179
+ const stop = new AbortController();
180
+ let still = setTimeout(() => stop.abort(), quiet);
181
+ try {
182
+ const response = await fetch(url, {
183
+ redirect: 'error',
184
+ credentials: 'omit',
185
+ cache: 'no-store',
186
+ signal: stop.signal,
187
+ });
188
+ const parts = [];
189
+ let length = 0;
190
+ for await (const part of response.body ?? []) {
191
+ length += part.length;
192
+ if (length > size) break;
193
+ parts.push(part);
194
+ clearTimeout(still);
195
+ still = setTimeout(() => stop.abort(), quiet);
196
+ }
197
+ if (length > size) return { why: 'the file is past the size its lobe said' };
198
+ const bytes = new Uint8Array(length);
199
+ let at = 0;
200
+ for (const part of parts) {
201
+ bytes.set(part, at);
202
+ at += part.length;
203
+ }
204
+ return { status: response.status, bytes };
205
+ } catch (thrown) {
206
+ return { why: `the file could not be fetched: ${thrown instanceof Error ? thrown.message : String(thrown)}` };
207
+ } finally {
208
+ clearTimeout(still);
209
+ stop.abort();
210
+ }
211
+ },
212
+ });
213
+
214
+ export const fetched = fetchedWithin();
215
+
216
+
217
+
218
+
219
+
220
+
221
+
222
+
223
+ export const MOST_KEPT = 8;
224
+
225
+ export const keptAnew = () => ({
226
+ following: null,
227
+ files: new Map(),
228
+ given: new Map(),
229
+ pending: Promise.resolve(),
230
+ });
231
+
232
+ export const followSeen = (
233
+ kept ,
234
+ channel ,
235
+ reaches ,
236
+ post ,
237
+ done ,
238
+ ) => {
239
+ const key = channel === undefined ? null : JSON.stringify(channel);
240
+ if (key === kept.following) return Promise.resolve();
241
+ kept.following = key;
242
+ if (channel === undefined) return Promise.resolve();
243
+ kept.pending = followChannel(channel, reaches, readThrough(post), kept.given).then((followed) => {
244
+ if (kept.following !== key && !('landed' in followed)) return;
245
+ if (!('bytes' in followed)) return done(followed);
246
+ kept.files.set(followed.name, followed);
247
+ for (const [name] of kept.files) {
248
+ if (kept.files.size <= MOST_KEPT) break;
249
+ kept.files.delete(name);
250
+ }
251
+ return done(followed);
252
+ });
253
+ return kept.pending;
254
+ };
255
+
256
+ export const tellLanded = (
257
+ post ,
258
+ followed ,
259
+ ) => {
260
+ if ('landed' in followed)
261
+ void post('landed', {
262
+ id: followed.press,
263
+ landed: followed.landed,
264
+ ...(followed.why === undefined ? {} : { why: followed.why }),
265
+ });
266
+ };
@@ -0,0 +1,26 @@
1
+ // SPDX-License-Identifier: FSL-1.1-ALv2
2
+
3
+
4
+
5
+
6
+
7
+ const MOST_ROUTES = 64;
8
+
9
+ export const makeFrames = (wired ) => {
10
+ const held = new Map ();
11
+ return {
12
+ of: (route ) => {
13
+ const one = held.get(route);
14
+ return one !== undefined && one.wired === wired() ? one.frame : undefined;
15
+ },
16
+ keep: (route , frame ) => {
17
+ held.delete(route);
18
+ held.set(route, { frame, wired: wired() });
19
+ if (held.size <= MOST_ROUTES) return;
20
+ for (const oldest of held.keys()) {
21
+ held.delete(oldest);
22
+ return;
23
+ }
24
+ },
25
+ };
26
+ };
@@ -0,0 +1,27 @@
1
+ // SPDX-License-Identifier: FSL-1.1-ALv2
2
+
3
+
4
+
5
+
6
+ export const readThrough =
7
+ (post ) =>
8
+ (press, at) =>
9
+ post('piece', { id: press, at });
10
+
11
+ export const heldOf = async (
12
+ press ,
13
+ size ,
14
+ read ,
15
+ ) => {
16
+ const bytes = new Uint8Array(size);
17
+ for (let at = 0; at < size;) {
18
+ const piece = await read(press, at);
19
+ if ('error' in piece) return { why: `the file’s piece from ${at} was refused: ${piece.error.message}` };
20
+ const got = piece.result;
21
+ if (!(got instanceof Uint8Array) || got.length === 0 || got.length > size - at)
22
+ return { why: `the file’s piece from ${at} is no bytes of it` };
23
+ bytes.set(got, at);
24
+ at += got.length;
25
+ }
26
+ return { bytes };
27
+ };
@@ -0,0 +1,179 @@
1
+ // SPDX-License-Identifier: FSL-1.1-ALv2
2
+ import { createServer, } from 'node:http';
3
+ import { routeOf } from '@nervur.org/genome/screen';
4
+ import { dataText, error } from '@nervur.org/genome/standard';
5
+ import { SURFACE_TEXT } from './client.js';
6
+
7
+ import { MOST_FILE } from './follow.js';
8
+ import { tellOf, } from './tell.js';
9
+
10
+
11
+
12
+
13
+
14
+
15
+
16
+
17
+
18
+
19
+
20
+
21
+
22
+ const POLICY =
23
+ "default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' data:; font-src 'self' data:; connect-src 'self'; frame-ancestors 'none'; base-uri 'none'; form-action 'none'";
24
+ const SECURITY = {
25
+ 'content-security-policy': POLICY,
26
+ 'x-content-type-options': 'nosniff',
27
+ 'referrer-policy': 'no-referrer',
28
+ 'cache-control': 'no-store',
29
+ };
30
+
31
+ const PAGE = `<!doctype html>
32
+ <html lang="en">
33
+ <head>
34
+ <meta charset="utf-8">
35
+ <meta name="viewport" content="width=device-width, initial-scale=1">
36
+ <title>Nervur</title>
37
+ <script type="module" src="/.nervur/surface.js"></script>
38
+ </head>
39
+ <body></body>
40
+ </html>
41
+ `;
42
+
43
+ const PRESS_WEIGHT = 64 * 1024;
44
+ const SHEET = /^\/\.nervur\/skin\/([0-9a-f]{64})\.css$/;
45
+ const ASSET = /^\/\.nervur\/asset\/([0-9a-f]{64}\.[a-z0-9]{3,5})$/;
46
+
47
+ export const hostAllowed = (host , port ) =>
48
+ host === `127.0.0.1:${port}` || host === `localhost:${port}`;
49
+
50
+ export const originAllowed = (origin , port ) =>
51
+ origin === `http://127.0.0.1:${port}` || origin === `http://localhost:${port}`;
52
+
53
+ const send = (res , status , type , body ) => {
54
+ res.writeHead(status, { ...SECURITY, 'content-type': type });
55
+ res.end(body);
56
+ };
57
+ const refuse = (res , status , why ) =>
58
+ send(res, status, 'text/plain; charset=utf-8', `${why}\n`);
59
+
60
+ const bytesOf = (req , most ) =>
61
+ new Promise((done) => {
62
+ const chunks = [];
63
+ let weight = 0;
64
+ req.on('data', (chunk ) => {
65
+ weight += chunk.length;
66
+ if (weight > most) {
67
+ chunks.length = 0;
68
+ req.destroy();
69
+ done(null);
70
+ } else chunks.push(chunk);
71
+ });
72
+ req.on('end', () => done(Buffer.concat(chunks)));
73
+ req.on('error', () => done(null));
74
+ });
75
+
76
+ const bodyOf = async (req ) => {
77
+ const bytes = await bytesOf(req, PRESS_WEIGHT);
78
+ try {
79
+ return bytes === null ? null : JSON.parse(bytes.toString('utf8'));
80
+ } catch {
81
+ return null;
82
+ }
83
+ };
84
+
85
+ const queryTell = (url ) => {
86
+ const query = new URL(url ?? '/', 'http://surface.invalid').searchParams;
87
+ const [route, width, language, reduced, theme] = ['route', 'width', 'language', 'reduced', 'theme'].map((name) =>
88
+ query.get(name),
89
+ );
90
+ return tellOf({
91
+ ...(route === null ? {} : { route }),
92
+ ...(width === null ? {} : { width: Number(width) }),
93
+ ...(language === null ? {} : { language }),
94
+ ...(reduced === null ? {} : { reduced: reduced === '1' }),
95
+ ...(theme === null ? {} : { theme }),
96
+ });
97
+ };
98
+
99
+ const streamed = (req , res , routes ) => {
100
+ const read = queryTell(req.url);
101
+ if (read.tell === undefined) return refuse(res, read.refused?.status ?? 400, read.refused?.message ?? 'a tell');
102
+ res.writeHead(200, { ...SECURITY, 'content-type': 'text/event-stream' });
103
+ res.write(': open\n\n');
104
+ const close = routes.stream((message) => void res.write(`data: ${message}\n\n`), read.tell);
105
+ res.on('close', close);
106
+ req.on('error', close);
107
+ };
108
+
109
+ const posted = async (
110
+ req ,
111
+ res ,
112
+ port ,
113
+ what ,
114
+ handle ,
115
+ ) => {
116
+ if (!originAllowed(req.headers.origin, port)) return refuse(res, 403, `${what} comes from the surface itself`);
117
+ if (req.headers['content-type'] !== 'application/json') return refuse(res, 415, `${what} is JSON`);
118
+ const body = await bodyOf(req);
119
+ if (body === null) return refuse(res, 400, `${what} is one JSON value, small`);
120
+ const reply = await handle(body);
121
+ send(res, reply.status, 'application/json', reply.body);
122
+ };
123
+
124
+ const given = async (req , res , port , routes ) => {
125
+ if (!originAllowed(req.headers.origin, port)) return refuse(res, 403, 'a file comes from the surface itself');
126
+ if (req.headers['content-type'] !== 'application/octet-stream') return refuse(res, 415, 'a file is its bytes');
127
+ const bytes = await bytesOf(req, MOST_FILE);
128
+ if (bytes === null) return refuse(res, 413, `a file is at most ${MOST_FILE} bytes`);
129
+ const reply = await routes.file(new Uint8Array(bytes));
130
+ send(res, reply.status, 'application/json', reply.body);
131
+ };
132
+
133
+ const told = async (body , routes ) => {
134
+ const read = tellOf(body);
135
+ if (read.tell === undefined)
136
+ return {
137
+ status: read.refused?.status ?? 400,
138
+ body: dataText(error('refused', read.refused?.message ?? 'a tell')),
139
+ };
140
+ return routes.tell(read.tell);
141
+ };
142
+
143
+ const routed = async (req , res , routes , port ) => {
144
+ const path = (req.url ?? '').split('?')[0] ?? '';
145
+ const method = req.method ?? '';
146
+ const sheet = SHEET.exec(path)?.[1];
147
+ const asset = ASSET.exec(path)?.[1];
148
+ const posts = ['/.nervur/press', '/.nervur/tell', '/.nervur/file', '/.nervur/link'].includes(path);
149
+ const own = posts || ['/.nervur/surface.js', '/.nervur/stream'].includes(path);
150
+ const page = !own && sheet === undefined && asset === undefined && !('why' in routeOf(path));
151
+ if (!own && !page && sheet === undefined && asset === undefined) return refuse(res, 404, 'no such page');
152
+ if (posts) {
153
+ if (method !== 'POST') return refuse(res, 405, 'this is posted');
154
+ if (path === '/.nervur/file') return given(req, res, port, routes);
155
+ if (path === '/.nervur/press') return posted(req, res, port, 'a press', routes.press);
156
+ if (path === '/.nervur/link') return posted(req, res, port, 'a link', routes.link);
157
+ return posted(req, res, port, 'a tell', (body) => told(body, routes));
158
+ }
159
+ if (method !== 'GET') return refuse(res, 405, 'this is read');
160
+ if (page) return send(res, 200, 'text/html; charset=utf-8', PAGE);
161
+ if (path === '/.nervur/surface.js') return send(res, 200, 'text/javascript; charset=utf-8', SURFACE_TEXT);
162
+ if (path === '/.nervur/stream') return streamed(req, res, routes);
163
+ if (asset !== undefined) {
164
+ const found = await routes.asset(asset);
165
+ return found === null ? refuse(res, 404, 'no such asset') : send(res, 200, found.type, found.bytes);
166
+ }
167
+ const text = sheet === undefined ? null : await routes.sheet(sheet);
168
+ if (text === null) return refuse(res, 404, 'no such sheet');
169
+ send(res, 200, 'text/css; charset=utf-8', text);
170
+ };
171
+ export const surfaceServer = (routes , portOf ) =>
172
+ createServer((req, res) => {
173
+ const port = portOf();
174
+ if (!hostAllowed(req.headers.host, port)) return refuse(res, 421, 'this surface answers its own address alone');
175
+ void routed(req, res, routes, port).catch(() => {
176
+ if (!res.headersSent) refuse(res, 500, 'the surface failed');
177
+ res.end();
178
+ });
179
+ });
@@ -0,0 +1,116 @@
1
+ // SPDX-License-Identifier: FSL-1.1-ALv2
2
+ import {
3
+ laid,
4
+ motionTokensOf,
5
+
6
+
7
+
8
+
9
+ } from '@nervur.org/genome/screen';
10
+ import { isFields, readText, } from '@nervur.org/genome/standard';
11
+ import { MEDIA } from './follow.js';
12
+
13
+
14
+
15
+
16
+
17
+
18
+
19
+
20
+
21
+
22
+
23
+
24
+
25
+
26
+
27
+
28
+
29
+
30
+
31
+
32
+
33
+
34
+ const KINDS = new Map([
35
+ ['woff2', 'font/woff2'],
36
+ ['woff', 'font/woff'],
37
+ ['ttf', 'font/ttf'],
38
+ ['otf', 'font/otf'],
39
+ ...MEDIA,
40
+ ]);
41
+ const HOME_INPUTS = new Set(['password']);
42
+
43
+ export const BARE = {
44
+ home: { sheets: [], classes: new Set(), inputs: HOME_INPUTS },
45
+ lobes: { sheets: [], classes: new Set(), inputs: new Set() },
46
+ };
47
+
48
+ const partOf = (one ) => {
49
+ if (!isFields(one)) return null;
50
+ const { path, digest, text, base64 } = one;
51
+ if (typeof path !== 'string' || typeof digest !== 'string') return null;
52
+ if (typeof text === 'string') return { path, digest, text };
53
+ return typeof base64 === 'string' ? { path, digest, base64 } : null;
54
+ };
55
+
56
+ const fieldsIn = (parts , path ) => {
57
+ const text = parts.find((one) => one.path === path)?.text;
58
+ if (text === undefined) return null;
59
+ const read = readText(text);
60
+ return 'data' in read && isFields(read.data) ? read.data : null;
61
+ };
62
+
63
+ const settingsOf = (laidData ) => {
64
+ const [light, dark] = isFields(laidData) ? [laidData['light'], laidData['dark']] : [];
65
+ return { ...(isFields(light) ? { light } : {}), ...(isFields(dark) ? { dark } : {}) };
66
+ };
67
+
68
+ export const designOf = (data ) => {
69
+ const files = isFields(data) ? data['files'] : undefined;
70
+ if (!Array.isArray(files)) return { why: 'the design lobe answered no files' };
71
+ const parts = [];
72
+ for (const one of files) {
73
+ const read = partOf(one);
74
+ if (read === null) return { why: 'the design lobe answered a part that is no part' };
75
+ parts.push(read);
76
+ }
77
+ const named = fieldsIn(parts, 'design.json');
78
+ const name = named?.['name'];
79
+ if (typeof name !== 'string') return { why: 'the design holds no design.json that names it' };
80
+ return {
81
+ name,
82
+ parts,
83
+ settings: settingsOf(isFields(data) ? data['laid'] : undefined),
84
+ tokens: fieldsIn(parts, 'tokens/light.json') ?? {},
85
+ };
86
+ };
87
+
88
+ const assetsOf = (parts ) => {
89
+ const found = new Map ();
90
+ for (const { path, digest, base64 } of parts) {
91
+ const type = KINDS.get(path.split('.').at(-1) ?? '');
92
+ if (type !== undefined && base64 !== undefined)
93
+ found.set(`${digest}.${path.split('.').at(-1)}`, { type, bytes: Buffer.from(base64, 'base64') });
94
+ }
95
+ return found;
96
+ };
97
+
98
+ const wearOf = (sheets , classes , inputs ) => ({
99
+ sheets,
100
+ classes: new Set(classes),
101
+ inputs,
102
+ });
103
+
104
+ export const layOf = (design , access ) => {
105
+ const made = laid(design.parts, design.settings, access);
106
+ if ('why' in made) return made;
107
+ const sheets = new Map(made.sheets.map(({ digest, text }) => [digest, text]));
108
+ return {
109
+ sheets,
110
+ assets: assetsOf(design.parts),
111
+ home: wearOf(made.home, made.classes.home, HOME_INPUTS),
112
+ lobes: wearOf(made.lobes, made.classes.lobes, new Set()),
113
+ motion: motionTokensOf({ ...design.tokens, ...design.settings.light }, design.name),
114
+ ...(made.fonts === undefined ? {} : { fonts: made.fonts }),
115
+ };
116
+ };
@@ -0,0 +1,27 @@
1
+ // SPDX-License-Identifier: FSL-1.1-ALv2
2
+ import { dataText, error, isFields, readData, } from '@nervur.org/genome/standard';
3
+
4
+ import { ROOT, } from './look.js';
5
+
6
+
7
+ const heldBy = (state ) => {
8
+ const leave = state.seen?.way?.['leave'];
9
+ const press = isFields(leave) ? leave['press'] : undefined;
10
+ return isFields(press) ? press : undefined;
11
+ };
12
+
13
+ const failure = (status , code , message ) => ({
14
+ status,
15
+ body: dataText(error(code, message)),
16
+ });
17
+
18
+ export const wayPressed = async (window , state , way , values ) => {
19
+ const read = readData(way);
20
+ const named = 'data' in read && isFields(read.data) ? dataText(read.data) : null;
21
+ const held = heldBy(state);
22
+ const method = held?.['method'];
23
+ if (held === undefined || named === null || dataText(held) !== named || typeof method !== 'string')
24
+ return failure(409, 'stale', 'the way holds no such press now');
25
+ const answer = await window.press(state.worn.designLobe ?? ROOT, method, held['args'] ?? {}, undefined, null, values);
26
+ return 'error' in answer ? { status: 422, body: dataText(answer) } : { status: 204 };
27
+ };