@yaks/cli 0.0.0-stage → 0.2.36

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 (331) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +1059 -2
  3. package/_dist/account-forward.d.ts +12 -0
  4. package/_dist/account-forward.d.ts.map +1 -0
  5. package/_dist/accounts.d.ts +23 -0
  6. package/_dist/accounts.d.ts.map +1 -0
  7. package/_dist/answer.d.ts +88 -0
  8. package/_dist/answer.d.ts.map +1 -0
  9. package/_dist/apply.d.ts +17 -0
  10. package/_dist/apply.d.ts.map +1 -0
  11. package/_dist/args.d.ts +86 -0
  12. package/_dist/args.d.ts.map +1 -0
  13. package/_dist/commands.d.ts +15 -0
  14. package/_dist/commands.d.ts.map +1 -0
  15. package/_dist/complete.d.ts +15 -0
  16. package/_dist/complete.d.ts.map +1 -0
  17. package/_dist/config.d.ts +37 -0
  18. package/_dist/config.d.ts.map +1 -0
  19. package/_dist/dependencies.d.ts +6 -0
  20. package/_dist/dependencies.d.ts.map +1 -0
  21. package/_dist/facets.d.ts +71 -0
  22. package/_dist/facets.d.ts.map +1 -0
  23. package/_dist/grammar.d.ts +5 -0
  24. package/_dist/grammar.d.ts.map +1 -0
  25. package/_dist/host.d.ts +164 -0
  26. package/_dist/host.d.ts.map +1 -0
  27. package/_dist/install.d.ts +43 -0
  28. package/_dist/install.d.ts.map +1 -0
  29. package/_dist/main.d.ts +16 -0
  30. package/_dist/main.d.ts.map +1 -0
  31. package/_dist/memo.d.ts +60 -0
  32. package/_dist/memo.d.ts.map +1 -0
  33. package/_dist/mod.d.ts +46 -0
  34. package/_dist/mod.d.ts.map +1 -0
  35. package/_dist/page.d.ts +73 -0
  36. package/_dist/page.d.ts.map +1 -0
  37. package/_dist/platform.d.ts +27 -0
  38. package/_dist/platform.d.ts.map +1 -0
  39. package/_dist/reader.d.ts +10 -0
  40. package/_dist/reader.d.ts.map +1 -0
  41. package/_dist/release.d.ts +48 -0
  42. package/_dist/release.d.ts.map +1 -0
  43. package/_dist/remote.d.ts +13 -0
  44. package/_dist/remote.d.ts.map +1 -0
  45. package/_dist/reported.d.ts +11 -0
  46. package/_dist/reported.d.ts.map +1 -0
  47. package/_dist/roles.d.ts +2 -0
  48. package/_dist/roles.d.ts.map +1 -0
  49. package/_dist/roster.d.ts +41 -0
  50. package/_dist/roster.d.ts.map +1 -0
  51. package/_dist/rpc.d.ts +29 -0
  52. package/_dist/rpc.d.ts.map +1 -0
  53. package/_dist/run.d.ts +164 -0
  54. package/_dist/run.d.ts.map +1 -0
  55. package/_dist/runtime.d.ts +4 -0
  56. package/_dist/runtime.d.ts.map +1 -0
  57. package/_dist/searching.d.ts +7 -0
  58. package/_dist/searching.d.ts.map +1 -0
  59. package/_dist/show.d.ts +22 -0
  60. package/_dist/show.d.ts.map +1 -0
  61. package/_dist/store.d.ts +25 -0
  62. package/_dist/store.d.ts.map +1 -0
  63. package/_dist/terminal.d.ts +17 -0
  64. package/_dist/terminal.d.ts.map +1 -0
  65. package/_dist/tool.d.ts +55 -0
  66. package/_dist/tool.d.ts.map +1 -0
  67. package/_dist/tty.d.ts +3 -0
  68. package/_dist/tty.d.ts.map +1 -0
  69. package/_dist/vault.d.ts +9 -0
  70. package/_dist/vault.d.ts.map +1 -0
  71. package/_dist/words.d.ts +11 -0
  72. package/_dist/words.d.ts.map +1 -0
  73. package/_dist/yak.d.ts +10 -0
  74. package/_dist/yak.d.ts.map +1 -0
  75. package/account-forward.js +40 -0
  76. package/account-forward.js.map +1 -0
  77. package/account-forward.ts +50 -0
  78. package/accounts.js +162 -0
  79. package/accounts.js.map +1 -0
  80. package/accounts.ts +228 -0
  81. package/answer.js +394 -0
  82. package/answer.js.map +1 -0
  83. package/answer.ts +569 -0
  84. package/apply.js +49 -0
  85. package/apply.js.map +1 -0
  86. package/apply.ts +54 -0
  87. package/args.js +420 -0
  88. package/args.js.map +1 -0
  89. package/args.ts +494 -0
  90. package/code.js +209 -0
  91. package/code.js.map +1 -0
  92. package/code.ts +227 -0
  93. package/commands.js +131 -0
  94. package/commands.js.map +1 -0
  95. package/commands.ts +142 -0
  96. package/complete.js +104 -0
  97. package/complete.js.map +1 -0
  98. package/complete.ts +152 -0
  99. package/config.js +125 -0
  100. package/config.js.map +1 -0
  101. package/config.ts +180 -0
  102. package/declarations.js +67 -0
  103. package/declarations.js.map +1 -0
  104. package/declarations.ts +84 -0
  105. package/deno.json +1 -0
  106. package/dependencies.js +126 -0
  107. package/dependencies.js.map +1 -0
  108. package/dependencies.ts +141 -0
  109. package/derive.js +102 -0
  110. package/derive.js.map +1 -0
  111. package/derive.ts +125 -0
  112. package/drain.js +83 -0
  113. package/drain.js.map +1 -0
  114. package/drain.ts +115 -0
  115. package/duties.ts +17 -0
  116. package/exports.json +1486 -0
  117. package/external.js +373 -0
  118. package/external.js.map +1 -0
  119. package/external.ts +435 -0
  120. package/facets.js +214 -0
  121. package/facets.js.map +1 -0
  122. package/facets.ts +232 -0
  123. package/feedback.js +54 -0
  124. package/feedback.js.map +1 -0
  125. package/feedback.ts +57 -0
  126. package/files.js +84 -0
  127. package/files.js.map +1 -0
  128. package/files.ts +112 -0
  129. package/get_starts.js +31 -0
  130. package/get_starts.js.map +1 -0
  131. package/get_starts.ts +34 -0
  132. package/git-credential.js +79 -0
  133. package/git-credential.js.map +1 -0
  134. package/git-credential.ts +77 -0
  135. package/grammar.js +4 -0
  136. package/grammar.js.map +1 -0
  137. package/grammar.ts +5 -0
  138. package/held.js +51 -0
  139. package/held.js.map +1 -0
  140. package/held.ts +66 -0
  141. package/host.js +1174 -0
  142. package/host.js.map +1 -0
  143. package/host.ts +1343 -0
  144. package/init.js +138 -0
  145. package/init.js.map +1 -0
  146. package/init.ts +139 -0
  147. package/install.js +216 -0
  148. package/install.js.map +1 -0
  149. package/install.ts +223 -0
  150. package/lifetime.js +56 -0
  151. package/lifetime.js.map +1 -0
  152. package/lifetime.ts +66 -0
  153. package/local-page.ts +114 -0
  154. package/local.js +417 -0
  155. package/local.js.map +1 -0
  156. package/local.ts +537 -0
  157. package/lockfile.js +20 -0
  158. package/lockfile.js.map +1 -0
  159. package/lockfile.ts +32 -0
  160. package/machines.js +33 -0
  161. package/machines.js.map +1 -0
  162. package/machines.ts +51 -0
  163. package/made/4f855610bdabcd736100d34e4e467ea5.json +1 -0
  164. package/main.js +534 -0
  165. package/main.js.map +1 -0
  166. package/main.ts +547 -0
  167. package/mcp.js +108 -0
  168. package/mcp.js.map +1 -0
  169. package/mcp.ts +113 -0
  170. package/member.js +103 -0
  171. package/member.js.map +1 -0
  172. package/member.ts +133 -0
  173. package/memo.js +503 -0
  174. package/memo.js.map +1 -0
  175. package/memo.ts +634 -0
  176. package/mod.js +46 -0
  177. package/mod.js.map +1 -0
  178. package/mod.ts +99 -0
  179. package/operations.js +53 -0
  180. package/operations.js.map +1 -0
  181. package/operations.ts +68 -0
  182. package/package.json +115 -4
  183. package/page.js +211 -0
  184. package/page.js.map +1 -0
  185. package/page.ts +308 -0
  186. package/parser.js +45 -0
  187. package/parser.js.map +1 -0
  188. package/parser.ts +77 -0
  189. package/people-search.js +112 -0
  190. package/people-search.js.map +1 -0
  191. package/people-search.ts +162 -0
  192. package/platform.js +184 -0
  193. package/platform.js.map +1 -0
  194. package/platform.ts +224 -0
  195. package/platform_effects.js +5 -0
  196. package/platform_effects.js.map +1 -0
  197. package/platform_effects.ts +13 -0
  198. package/prepare.js +40 -0
  199. package/prepare.js.map +1 -0
  200. package/prepare.ts +46 -0
  201. package/read_activity.js +54 -0
  202. package/read_activity.js.map +1 -0
  203. package/read_activity.ts +61 -0
  204. package/read_thread.js +387 -0
  205. package/read_thread.js.map +1 -0
  206. package/read_thread.ts +506 -0
  207. package/read_worker.ts +164 -0
  208. package/reader.js +48 -0
  209. package/reader.js.map +1 -0
  210. package/reader.ts +57 -0
  211. package/release.js +82 -0
  212. package/release.js.map +1 -0
  213. package/release.json +127 -0
  214. package/release.ts +98 -0
  215. package/remote-page.ts +70 -0
  216. package/remote.js +96 -0
  217. package/remote.js.map +1 -0
  218. package/remote.ts +153 -0
  219. package/report.js +83 -0
  220. package/report.js.map +1 -0
  221. package/report.ts +101 -0
  222. package/reported.js +32 -0
  223. package/reported.js.map +1 -0
  224. package/reported.ts +38 -0
  225. package/restart.js +349 -0
  226. package/restart.js.map +1 -0
  227. package/restart.ts +478 -0
  228. package/restore.js +32 -0
  229. package/restore.js.map +1 -0
  230. package/restore.ts +38 -0
  231. package/roles.js +32 -0
  232. package/roles.js.map +1 -0
  233. package/roles.ts +49 -0
  234. package/roster.js +65 -0
  235. package/roster.js.map +1 -0
  236. package/roster.ts +84 -0
  237. package/rpc.js +113 -0
  238. package/rpc.js.map +1 -0
  239. package/rpc.ts +159 -0
  240. package/run.js +533 -0
  241. package/run.js.map +1 -0
  242. package/run.ts +741 -0
  243. package/runtime.js +381 -0
  244. package/runtime.js.map +1 -0
  245. package/runtime.ts +433 -0
  246. package/sandboxes.js +26 -0
  247. package/sandboxes.js.map +1 -0
  248. package/sandboxes.ts +30 -0
  249. package/searching.js +33 -0
  250. package/searching.js.map +1 -0
  251. package/searching.ts +50 -0
  252. package/show.js +109 -0
  253. package/show.js.map +1 -0
  254. package/show.ts +144 -0
  255. package/shown.js +13 -0
  256. package/shown.js.map +1 -0
  257. package/shown.ts +44 -0
  258. package/stall.js +90 -0
  259. package/stall.js.map +1 -0
  260. package/stall.ts +156 -0
  261. package/stall_profile.js +113 -0
  262. package/stall_profile.js.map +1 -0
  263. package/stall_profile.ts +142 -0
  264. package/stall_wait.js +183 -0
  265. package/stall_wait.js.map +1 -0
  266. package/stall_wait.ts +249 -0
  267. package/stall_wait_worker.ts +11 -0
  268. package/start.js +349 -0
  269. package/start.js.map +1 -0
  270. package/start.ts +343 -0
  271. package/starvation.js +147 -0
  272. package/starvation.js.map +1 -0
  273. package/starvation.ts +224 -0
  274. package/store.js +118 -0
  275. package/store.js.map +1 -0
  276. package/store.ts +193 -0
  277. package/subcommands.js +62 -0
  278. package/subcommands.js.map +1 -0
  279. package/subcommands.ts +77 -0
  280. package/terminal.js +93 -0
  281. package/terminal.js.map +1 -0
  282. package/terminal.ts +128 -0
  283. package/text.js +20 -0
  284. package/text.js.map +1 -0
  285. package/text.ts +37 -0
  286. package/tool.js +76 -0
  287. package/tool.js.map +1 -0
  288. package/tool.ts +157 -0
  289. package/trace_control.js +231 -0
  290. package/trace_control.js.map +1 -0
  291. package/trace_control.ts +233 -0
  292. package/traces.js +138 -0
  293. package/traces.js.map +1 -0
  294. package/traces.ts +160 -0
  295. package/tracker.js +22 -0
  296. package/tracker.js.map +1 -0
  297. package/tracker.ts +27 -0
  298. package/tty.js +82 -0
  299. package/tty.js.map +1 -0
  300. package/tty.ts +142 -0
  301. package/unit_failure.js +178 -0
  302. package/unit_failure.js.map +1 -0
  303. package/unit_failure.ts +191 -0
  304. package/upkeep_thread.js +32 -0
  305. package/upkeep_thread.js.map +1 -0
  306. package/upkeep_thread.ts +35 -0
  307. package/upkeep_worker.ts +27 -0
  308. package/vault.js +186 -0
  309. package/vault.js.map +1 -0
  310. package/vault.ts +220 -0
  311. package/vocab-ids.js +63 -0
  312. package/vocab-ids.js.map +1 -0
  313. package/vocab-ids.ts +62 -0
  314. package/vocab.json +35 -0
  315. package/warm.js +138 -0
  316. package/warm.js.map +1 -0
  317. package/warm.ts +171 -0
  318. package/watch_revision.js +25 -0
  319. package/watch_revision.js.map +1 -0
  320. package/watch_revision.ts +30 -0
  321. package/words.js +29 -0
  322. package/words.js.map +1 -0
  323. package/words.ts +41 -0
  324. package/workspaces.js +40 -0
  325. package/workspaces.js.map +1 -0
  326. package/workspaces.ts +58 -0
  327. package/yak-unit-failure.conf +6 -0
  328. package/yak-unit-handoff.conf +2 -0
  329. package/yak.js +123 -0
  330. package/yak.js.map +1 -0
  331. package/yak.ts +136 -0
package/install.ts ADDED
@@ -0,0 +1,223 @@
1
+ import * as system from '@yaks/process/system'
2
+ import * as disk from '@yaks/file/io'
3
+ import { command as launchCommand } from '@yaks/process/command'
4
+ // `deno run -A jsr:@yaks/cli/install`: puts `yak` on this machine at the
5
+ // release this module came from, so the command, every plugin a config names
6
+ // by package (./config.ts `located`) and the canvas it bundles are one set.
7
+ //
8
+ // A bare `deno install -g jsr:@yaks/cli/yak` is not that on the day a release
9
+ // publishes (./release.ts): the install takes the release before, and a `yak`
10
+ // pinned to the new one has every plugin it names refused until the day is
11
+ // out, since a global install's command keeps none of the flags it was
12
+ // installed with. What it does keep is a config, copied beside the command,
13
+ // so the config written here is the release's (./release.ts `released`).
14
+ //
15
+ // It installs the newest release a day old, whole; `--minimum-dependency-age=0`
16
+ // takes the release published today. Deno keeps the release list it fetched,
17
+ // so an update adds `--reload=jsr:@yaks/cli`, or it installs the release it
18
+ // found before.
19
+
20
+ import cli from './package.json' with { type: 'json' }
21
+ import { released } from './release.js'
22
+
23
+ /** The `deno` arguments that install `yak` at `version` under `config`.
24
+ *
25
+ * ```ts
26
+ * import { assertEquals } from '@std/assert'
27
+ * assertEquals(install('1.2.3', '/tmp/c.json').at(-1), 'jsr:@yaks/cli@1.2.3/yak')
28
+ * ```
29
+ */
30
+ export let install = (version: string, config: string): string[] => [
31
+ 'install',
32
+ '--global',
33
+ '--allow-all',
34
+ // Deno's own lines as a first command fetches modules say nothing to whoever
35
+ // ran it; its errors print all the same.
36
+ '--quiet',
37
+ // TODO: drop once Deno honors deno.json's "no-legacy-abort" (2.9.1 does not)
38
+ '--unstable-no-legacy-abort',
39
+ '--force',
40
+ '--name',
41
+ 'yak',
42
+ '--config',
43
+ config,
44
+ `jsr:${cli.name}@${version}/yak`,
45
+ ]
46
+
47
+ /** The `deno` arguments that run `yak` at `version` under `config`, with
48
+ * `args`: what the installed command runs, without installing it.
49
+ *
50
+ * ```ts
51
+ * import { assertEquals } from '@std/assert'
52
+ * assertEquals(run('1.2.3', '/tmp/c.json', ['help']).slice(-2), ['jsr:@yaks/cli@1.2.3/yak', 'help'])
53
+ * ```
54
+ */
55
+ export let run = (
56
+ version: string,
57
+ config: string,
58
+ args: string[],
59
+ ): string[] => [
60
+ 'run',
61
+ '--allow-all',
62
+ '--config',
63
+ config,
64
+ `jsr:${cli.name}@${version}/yak`,
65
+ ...args,
66
+ ]
67
+
68
+ /** A first `yak init` and `yak task list`, against a scratch graph in `dir`:
69
+ * a command imports the plugins a config names by name, which `deno install`
70
+ * never fetched, since it follows literal imports only. Run here, the wait to
71
+ * fetch and compile them is part of installing, and a person's first command
72
+ * finds them. */
73
+ export let warming = (dir: string): string[][] => [
74
+ ['--config', `${dir}/warm/yak.json`, 'init'],
75
+ ['--config', `${dir}/warm/yak.json`, 'task', 'list'],
76
+ ]
77
+
78
+ /** A first `yak serve` on that scratch graph, at `port`, asked for its canvas
79
+ * and stopped once it says it is ready, in the file `ready`: serving imports
80
+ * the facets of the web role, bundles the page, and, as it warms, starts its
81
+ * readers and the workers for its duty roles, none of which a command before
82
+ * it fetched, so a person's first `yak serve` would otherwise download them as
83
+ * it starts.
84
+ *
85
+ * ```ts
86
+ * import { assertEquals } from '@std/assert'
87
+ * assertEquals(serving('/tmp/i', 9).slice(-5), ['serve', '--port', '9', '--ready', '/tmp/i/ready'])
88
+ * ```
89
+ */
90
+ export let serving = (dir: string, port: number): string[] => [
91
+ '--config',
92
+ `${dir}/warm/yak.json`,
93
+ 'serve',
94
+ '--port',
95
+ String(port),
96
+ '--ready',
97
+ `${dir}/ready`,
98
+ ]
99
+
100
+ /** What a warm-up of `yak serve` asks for once it listens, after the page at
101
+ * `/`: everything the page loads (its canvas, stylesheet, icons, manifest),
102
+ * which serve makes from the files Deno then keeps, so a first `yak serve`
103
+ * answers with them offline too.
104
+ *
105
+ * ```ts
106
+ * import { assertEquals } from '@std/assert'
107
+ * assertEquals(loads('<link href="/web/s.css" /><a href="https://x"><script src="/web/app.js">'), ['/web/s.css', '/web/app.js'])
108
+ * ```
109
+ */
110
+ export let loads = (page: string): string[] =>
111
+ [...page.matchAll(/(?:href|src)="(\/[^"]*)"/g)].map(([, path]) => path)
112
+
113
+ // Once `path` exists, or an error at `by`.
114
+ let appeared = async (path: string, by: number) => {
115
+ for (;;) {
116
+ try {
117
+ return void await disk.stat(path)
118
+ } catch (error) {
119
+ if (!disk.isNotFound(error)) throw error
120
+ }
121
+ if (Date.now() > by) throw new Error(`${path} never appeared`)
122
+ await new Promise((r) => setTimeout(r, 100))
123
+ }
124
+ }
125
+
126
+ // A port nothing listens on, from the system, released for serve to take.
127
+ let free = () => {
128
+ let listener = Deno.listen({ hostname: '127.0.0.1', port: 0 })
129
+ let { port } = listener.addr as Deno.NetAddr
130
+ listener.close()
131
+ return port
132
+ }
133
+
134
+ // The answer to `url`, once something listens there and makes it, or an
135
+ // error at `by`. A page that fails is asked for again, since serve makes it
136
+ // again on the next request (./page.ts `kept`): on a cold cache the canvas's
137
+ // first make can outrun serve's limit while it downloads, and what it fetched
138
+ // stays for the next.
139
+ let answered = async (url: string, by: number) => {
140
+ for (;;) {
141
+ let failure: unknown
142
+ try {
143
+ let res = await fetch(url)
144
+ let body = await res.text()
145
+ if (res.ok) return body
146
+ failure = new Error(`${url} answered ${res.status}: ${body}`)
147
+ } catch (error) {
148
+ if (!(error instanceof TypeError)) throw error
149
+ failure = error
150
+ }
151
+ if (Date.now() > by) throw failure
152
+ await new Promise((r) => setTimeout(r, 100))
153
+ }
154
+ }
155
+
156
+ let deno = (args: string[], cwd: string, quiet = false) =>
157
+ launchCommand(system.execPath(), {
158
+ args,
159
+ cwd,
160
+ stdout: quiet ? 'null' : 'inherit',
161
+ stderr: quiet ? 'piped' : 'inherit',
162
+ }).output()
163
+
164
+ if (import.meta.main) {
165
+ let dir = await disk.makeTempDir({ prefix: 'yak-install-' })
166
+ try {
167
+ let config = `${dir}/deno.json`
168
+ await disk.writeTextFile(config, JSON.stringify(released))
169
+ // Run from the empty directory, since `deno install` also folds in the
170
+ // dependencies of any package.json it finds above where it runs.
171
+ let { code } = await deno(install(cli.version, config), dir)
172
+ if (code == 0) {
173
+ console.log('fetching the plugins a first command needs…')
174
+ // A warm-up is a first command: one that fails is a `yak` that does not
175
+ // run, whatever `deno install` said.
176
+ let failed: string | undefined
177
+ for (let args of warming(dir)) {
178
+ let warmed = await deno(run(cli.version, config, args), dir, true)
179
+ if (!warmed.success) {
180
+ console.error(new TextDecoder().decode(warmed.stderr).trim())
181
+ failed = `yak ${args.slice(2).join(' ')}`
182
+ break
183
+ }
184
+ }
185
+ if (!failed) {
186
+ let port = free()
187
+ let server = launchCommand(system.execPath(), {
188
+ args: run(cli.version, config, serving(dir, port)),
189
+ cwd: dir,
190
+ stdout: 'null',
191
+ stderr: 'null',
192
+ }).spawn()
193
+ try {
194
+ let by = Date.now() + 300_000
195
+ let at = (path: string) =>
196
+ answered(`http://127.0.0.1:${port}${path}`, by)
197
+ for (let path of loads(await at('/'))) await at(path)
198
+ await appeared(`${dir}/ready`, by)
199
+ } catch (error) {
200
+ console.error(String(error))
201
+ failed = 'yak serve'
202
+ } finally {
203
+ server.kill('SIGINT')
204
+ await server.status
205
+ }
206
+ }
207
+ if (failed) {
208
+ console.error(
209
+ `yak ${cli.version} is installed but does not run: \`${failed}\` failed (above)`,
210
+ )
211
+ code = 1
212
+ } else {
213
+ console.log(
214
+ `yak ${cli.version} is installed. Next: yak auth yaks.app ` +
215
+ 'signs you in to yaks.app, or yak init starts a graph on this machine',
216
+ )
217
+ }
218
+ }
219
+ system.setExitCode(code)
220
+ } finally {
221
+ await disk.remove(dir, { recursive: true })
222
+ }
223
+ }
package/lifetime.js ADDED
@@ -0,0 +1,56 @@
1
+ import * as disk from "@yaks/file/io";
2
+ // A worker serves an existing file, never an unlinked SQLite connection. A
3
+ // fallback also belongs to the web that requested it; managed workers do not.
4
+ /** Capture the file identity before opening SQLite, so a removed graph cannot
5
+ * be silently recreated, and a replacement at the same path is a new graph.
6
+ * A `tether` is a stream its starter holds open and never writes: its end,
7
+ * however the starter ended, ends the worker's reason to serve. A worker's
8
+ * `ready` file ({@link superseded}) ends it once a newer one of its role is
9
+ * ready. */ export let lifetime = async (path, tether, ready)=>{
10
+ let replaced = ready ? superseded(ready) : ()=>Promise.resolve(false);
11
+ let original = path == ':memory:' ? undefined : await disk.stat(path);
12
+ let cut = false;
13
+ let end = ()=>void (cut = true);
14
+ tether?.pipeTo(new WritableStream()).then(end, end);
15
+ return async ()=>{
16
+ if (cut || await replaced()) return false;
17
+ try {
18
+ if (original) {
19
+ let current = await disk.stat(path);
20
+ if (current.dev != original.dev || current.ino != original.ino) {
21
+ return false;
22
+ }
23
+ }
24
+ return true;
25
+ } catch (error) {
26
+ if (error instanceof disk.errors.NotFound) return false;
27
+ throw error;
28
+ }
29
+ };
30
+ };
31
+ // `<dir>/<role>-<instance>.ready` (./restart.ts `readyFile`). The service
32
+ // template's default instance is boot, and handovers mint UUIDs.
33
+ let INSTANCE = /^(.+)-(?:boot|[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12})\.ready$/;
34
+ /**
35
+ * Whether an instance of the same role became ready after this one: its
36
+ * replacement, which it leaves to. The handover's own process stops the old
37
+ * instances once its replacements are ready, but a handover that dies between
38
+ * the two (killed with the box out of memory, T-1463) left two of every role
39
+ * running, each starting agents. So the old one leaves on its own.
40
+ */ export let superseded = (ready)=>{
41
+ let at = ready.lastIndexOf('/');
42
+ let dir = ready.slice(0, at);
43
+ let role = INSTANCE.exec(ready.slice(at + 1))?.[1];
44
+ let mtime = (path)=>disk.stat(path).then((s)=>s.mtime?.getTime(), ()=>undefined);
45
+ return async ()=>{
46
+ let mine = role && await mtime(ready);
47
+ if (!mine) return false;
48
+ for await (let file of disk.readDir(dir)){
49
+ if (INSTANCE.exec(file.name)?.[1] != role) continue;
50
+ let theirs = await mtime(`${dir}/${file.name}`);
51
+ if (theirs && theirs > mine) return true;
52
+ }
53
+ return false;
54
+ };
55
+ };
56
+ //# sourceMappingURL=lifetime.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lifetime.js","sources":["./lifetime.ts"],"names":[],"mappings":"AAAA,YAAY,+BAAoC;AAChD,2EAA2E;AAC3E,8EAA8E;AAE9E;;;;;UAKU,GACV,OAAO,IAAI,WAAW,OACpB,MACA,QACA;EAEA,IAAI,WAAW,QAAQ,WAAW,SAAS,IAAM,QAAQ,OAAO,CAAC;EACjE,IAAI,WAAW,QAAQ,aAAa,YAAY,MAAM,KAAK,IAAI,CAAC;EAChE,IAAI,MAAM;EACV,IAAI,MAAM,IAAM,KAAK,CAAC,MAAM,IAAI;EAChC,QAAQ,OAAO,IAAI,kBAAkB,KAAK,KAAK;EAC/C,OAAO;IACL,IAAI,OAAO,MAAM,YAAY,OAAO;IACpC,IAAI;MACF,IAAI,UAAU;QACZ,IAAI,UAAU,MAAM,KAAK,IAAI,CAAC;QAC9B,IAAI,QAAQ,GAAG,IAAI,SAAS,GAAG,IAAI,QAAQ,GAAG,IAAI,SAAS,GAAG,EAAE;UAC9D,OAAO;QACT;MACF;MACA,OAAO;IACT,EAAE,OAAO,OAAO;MACd,IAAI,iBAAiB,KAAK,MAAM,CAAC,QAAQ,EAAE,OAAO;MAClD,MAAM;IACR;EACF;AACF,EAAC;AAED,0EAA0E;AAC1E,iEAAiE;AACjE,IAAI,WACF;AAEF;;;;;;CAMC,GACD,OAAO,IAAI,aAAa,CAAC;EACvB,IAAI,KAAK,MAAM,WAAW,CAAC;EAC3B,IAAI,MAAM,MAAM,KAAK,CAAC,GAAG;EACzB,IAAI,OAAO,SAAS,IAAI,CAAC,MAAM,KAAK,CAAC,KAAK,KAAK,CAAC,EAAE;EAClD,IAAI,QAAQ,CAAC,OACX,KAAK,IAAI,CAAC,MAAM,IAAI,CAAC,CAAC,IAAM,EAAE,KAAK,EAAE,WAAW,IAAM;EACxD,OAAO;IACL,IAAI,OAAO,QAAQ,MAAM,MAAM;IAC/B,IAAI,CAAC,MAAM,OAAO;IAClB,WAAW,IAAI,QAAQ,KAAK,OAAO,CAAC,KAAM;MACxC,IAAI,SAAS,IAAI,CAAC,KAAK,IAAI,GAAG,CAAC,EAAE,IAAI,MAAM;MAC3C,IAAI,SAAS,MAAM,MAAM,GAAG,IAAI,CAAC,EAAE,KAAK,IAAI,EAAE;MAC9C,IAAI,UAAU,SAAS,MAAM,OAAO;IACtC;IACA,OAAO;EACT;AACF,EAAC"}
package/lifetime.ts ADDED
@@ -0,0 +1,66 @@
1
+ import * as disk from '@yaks/file/io'
2
+ // A worker serves an existing file, never an unlinked SQLite connection. A
3
+ // fallback also belongs to the web that requested it; managed workers do not.
4
+
5
+ /** Capture the file identity before opening SQLite, so a removed graph cannot
6
+ * be silently recreated, and a replacement at the same path is a new graph.
7
+ * A `tether` is a stream its starter holds open and never writes: its end,
8
+ * however the starter ended, ends the worker's reason to serve. A worker's
9
+ * `ready` file ({@link superseded}) ends it once a newer one of its role is
10
+ * ready. */
11
+ export let lifetime = async (
12
+ path: string,
13
+ tether?: ReadableStream<Uint8Array>,
14
+ ready?: string,
15
+ ) => {
16
+ let replaced = ready ? superseded(ready) : () => Promise.resolve(false)
17
+ let original = path == ':memory:' ? undefined : await disk.stat(path)
18
+ let cut = false
19
+ let end = () => void (cut = true)
20
+ tether?.pipeTo(new WritableStream()).then(end, end)
21
+ return async (): Promise<boolean> => {
22
+ if (cut || await replaced()) return false
23
+ try {
24
+ if (original) {
25
+ let current = await disk.stat(path)
26
+ if (current.dev != original.dev || current.ino != original.ino) {
27
+ return false
28
+ }
29
+ }
30
+ return true
31
+ } catch (error) {
32
+ if (error instanceof disk.errors.NotFound) return false
33
+ throw error
34
+ }
35
+ }
36
+ }
37
+
38
+ // `<dir>/<role>-<instance>.ready` (./restart.ts `readyFile`). The service
39
+ // template's default instance is boot, and handovers mint UUIDs.
40
+ let INSTANCE =
41
+ /^(.+)-(?:boot|[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12})\.ready$/
42
+
43
+ /**
44
+ * Whether an instance of the same role became ready after this one: its
45
+ * replacement, which it leaves to. The handover's own process stops the old
46
+ * instances once its replacements are ready, but a handover that dies between
47
+ * the two (killed with the box out of memory, T-1463) left two of every role
48
+ * running, each starting agents. So the old one leaves on its own.
49
+ */
50
+ export let superseded = (ready: string): () => Promise<boolean> => {
51
+ let at = ready.lastIndexOf('/')
52
+ let dir = ready.slice(0, at)
53
+ let role = INSTANCE.exec(ready.slice(at + 1))?.[1]
54
+ let mtime = (path: string) =>
55
+ disk.stat(path).then((s) => s.mtime?.getTime(), () => undefined)
56
+ return async () => {
57
+ let mine = role && await mtime(ready)
58
+ if (!mine) return false
59
+ for await (let file of disk.readDir(dir)) {
60
+ if (INSTANCE.exec(file.name)?.[1] != role) continue
61
+ let theirs = await mtime(`${dir}/${file.name}`)
62
+ if (theirs && theirs > mine) return true
63
+ }
64
+ return false
65
+ }
66
+ }
package/local-page.ts ADDED
@@ -0,0 +1,114 @@
1
+ // A page bundled from a checkout: esbuild resolves its imports through the npm
2
+ // workspace. ./page.ts imports this only when it makes a body, so a host that
3
+ // makes none never loads esbuild.
4
+ import { context, type PluginBuild } from 'npm:esbuild@0.28.1'
5
+ import { imported } from 'jsr:@yaks/fp@0.2.36/imports'
6
+ import { fileURLToPath } from 'node:url'
7
+ import { relative } from 'node:path'
8
+ import {
9
+ type Built,
10
+ type Bundling,
11
+ CHUNK,
12
+ compiled,
13
+ finished,
14
+ pageExternals,
15
+ type Written,
16
+ } from './page.ts'
17
+
18
+ let FILE_URLS = {
19
+ name: 'file-urls',
20
+ setup(build: PluginBuild) {
21
+ build.onResolve(
22
+ { filter: /^file:/ },
23
+ ({ path }) => ({ path: fileURLToPath(path) }),
24
+ )
25
+ },
26
+ }
27
+
28
+ // React and the packages like it choose their production build, without
29
+ // development checks, by this.
30
+ let PRODUCTION = { 'process.env.NODE_ENV': '"production"' }
31
+
32
+ let BROWSER = {
33
+ bundle: true,
34
+ format: 'esm',
35
+ platform: 'browser',
36
+ target: 'esnext',
37
+ jsx: 'automatic',
38
+ jsxImportSource: 'react',
39
+ minify: true,
40
+ define: PRODUCTION,
41
+ write: false,
42
+ metafile: true,
43
+ logLevel: 'silent',
44
+ } as const
45
+
46
+ // The modules bundled are loaded for the browser, by this loader: what
47
+ // watches this process's imports sees them too (./code.ts).
48
+ let watched = (dir: URL) => ({ metafile }: Built) =>
49
+ imported(
50
+ Object.keys(metafile?.inputs ?? {}).filter((i) => i != '<stdin>')
51
+ .map((i) => new URL(i, dir).href),
52
+ )
53
+
54
+ export let bundle = async (
55
+ entry: URL | Written,
56
+ signal?: AbortSignal,
57
+ opts: Bundling = {},
58
+ ): Promise<string> => {
59
+ let at = entry instanceof URL ? entry : entry.at
60
+ let dir = new URL('./', at)
61
+ let build = await context({
62
+ absWorkingDir: fileURLToPath(dir),
63
+ ...entry instanceof URL ? { entryPoints: [fileURLToPath(entry)] } : {
64
+ stdin: {
65
+ contents: entry.code,
66
+ sourcefile: fileURLToPath(at),
67
+ resolveDir: fileURLToPath(new URL('./', at)),
68
+ loader: 'ts',
69
+ },
70
+ },
71
+ plugins: [pageExternals(opts.external), FILE_URLS],
72
+ ...BROWSER,
73
+ alias: opts.alias,
74
+ format: opts.format ?? BROWSER.format,
75
+ })
76
+ return compiled(build, signal, watched(dir))
77
+ }
78
+
79
+ /** ./page.ts `split`: one build of every entry, its shared modules in
80
+ * chunks the entries import from the site's root. */
81
+ export let split = async (
82
+ entries: Record<string, URL>,
83
+ signal?: AbortSignal,
84
+ opts: Bundling = {},
85
+ ): Promise<
86
+ { entries: Record<string, string>; chunks: Record<string, string> }
87
+ > => {
88
+ let dir = new URL('./', Object.values(entries)[0])
89
+ let out = fileURLToPath(dir)
90
+ let build = await context({
91
+ absWorkingDir: out,
92
+ entryPoints: Object.fromEntries(
93
+ Object.entries(entries).map((
94
+ [name, at],
95
+ ) => [name.replace(/\.js$/, ''), fileURLToPath(at)]),
96
+ ),
97
+ plugins: [pageExternals(opts.external), FILE_URLS],
98
+ splitting: true,
99
+ outdir: out,
100
+ entryNames: '[name]',
101
+ chunkNames: `${CHUNK}.[hash]`,
102
+ publicPath: '/',
103
+ ...BROWSER,
104
+ })
105
+ let built = await finished(build, signal)
106
+ watched(dir)(built)
107
+ let made = { entries: {}, chunks: {} } as Awaited<ReturnType<typeof split>>
108
+ for (let { path, text } of built.outputFiles!) {
109
+ let name = relative(out, path)
110
+ if (name in entries) made.entries[name] = text
111
+ else made.chunks[name] = text
112
+ }
113
+ return made
114
+ }