@yaks/process 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 (111) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +249 -2
  3. package/_dist/clock.d.ts +5 -0
  4. package/_dist/clock.d.ts.map +1 -0
  5. package/_dist/command.d.ts +43 -0
  6. package/_dist/command.d.ts.map +1 -0
  7. package/_dist/comp.d.ts +49 -0
  8. package/_dist/comp.d.ts.map +1 -0
  9. package/_dist/env.d.ts +8 -0
  10. package/_dist/env.d.ts.map +1 -0
  11. package/_dist/graph.d.ts +3 -0
  12. package/_dist/graph.d.ts.map +1 -0
  13. package/_dist/http.d.ts +11 -0
  14. package/_dist/http.d.ts.map +1 -0
  15. package/_dist/isolate.d.ts +11 -0
  16. package/_dist/isolate.d.ts.map +1 -0
  17. package/_dist/mod.d.ts +49 -0
  18. package/_dist/mod.d.ts.map +1 -0
  19. package/_dist/modules.d.ts +31 -0
  20. package/_dist/modules.d.ts.map +1 -0
  21. package/_dist/platform.d.ts +7 -0
  22. package/_dist/platform.d.ts.map +1 -0
  23. package/_dist/plugin.d.ts +15 -0
  24. package/_dist/plugin.d.ts.map +1 -0
  25. package/_dist/run.d.ts +219 -0
  26. package/_dist/run.d.ts.map +1 -0
  27. package/_dist/self.d.ts +32 -0
  28. package/_dist/self.d.ts.map +1 -0
  29. package/_dist/socket.d.ts +11 -0
  30. package/_dist/socket.d.ts.map +1 -0
  31. package/_dist/store.d.ts +22 -0
  32. package/_dist/store.d.ts.map +1 -0
  33. package/_dist/system.d.ts +64 -0
  34. package/_dist/system.d.ts.map +1 -0
  35. package/_dist/trash.d.ts +6 -0
  36. package/_dist/trash.d.ts.map +1 -0
  37. package/_dist/views.d.ts +4 -0
  38. package/_dist/views.d.ts.map +1 -0
  39. package/_dist/vocab.d.ts +8 -0
  40. package/_dist/vocab.d.ts.map +1 -0
  41. package/_dist/wind.d.ts +35 -0
  42. package/_dist/wind.d.ts.map +1 -0
  43. package/clock.js +11 -0
  44. package/clock.js.map +1 -0
  45. package/clock.ts +11 -0
  46. package/command.js +172 -0
  47. package/command.js.map +1 -0
  48. package/command.ts +215 -0
  49. package/comp.js +54 -0
  50. package/comp.js.map +1 -0
  51. package/comp.ts +123 -0
  52. package/deno.json +1 -0
  53. package/detached.js +60 -0
  54. package/detached.js.map +1 -0
  55. package/detached.ts +92 -0
  56. package/detached_worker.ts +22 -0
  57. package/env.js +13 -0
  58. package/env.js.map +1 -0
  59. package/env.ts +24 -0
  60. package/graph.js +7 -0
  61. package/graph.js.map +1 -0
  62. package/graph.ts +8 -0
  63. package/http.js +158 -0
  64. package/http.js.map +1 -0
  65. package/http.ts +184 -0
  66. package/isolate.js +41 -0
  67. package/isolate.js.map +1 -0
  68. package/isolate.ts +52 -0
  69. package/mod.js +49 -0
  70. package/mod.js.map +1 -0
  71. package/mod.ts +50 -0
  72. package/modules.js +41 -0
  73. package/modules.js.map +1 -0
  74. package/modules.ts +69 -0
  75. package/package.json +81 -4
  76. package/platform.js +9 -0
  77. package/platform.js.map +1 -0
  78. package/platform.ts +9 -0
  79. package/plugin.js +27 -0
  80. package/plugin.js.map +1 -0
  81. package/plugin.ts +28 -0
  82. package/run.js +899 -0
  83. package/run.js.map +1 -0
  84. package/run.ts +1139 -0
  85. package/self.js +93 -0
  86. package/self.js.map +1 -0
  87. package/self.ts +144 -0
  88. package/socket.js +38 -0
  89. package/socket.js.map +1 -0
  90. package/socket.ts +49 -0
  91. package/store.js +30 -0
  92. package/store.js.map +1 -0
  93. package/store.ts +50 -0
  94. package/system.js +161 -0
  95. package/system.js.map +1 -0
  96. package/system.ts +216 -0
  97. package/testing.ts +39 -0
  98. package/tidy.ts +28 -0
  99. package/trash.js +117 -0
  100. package/trash.js.map +1 -0
  101. package/trash.ts +117 -0
  102. package/views.js +28 -0
  103. package/views.js.map +1 -0
  104. package/views.ts +30 -0
  105. package/vocab.js +16 -0
  106. package/vocab.js.map +1 -0
  107. package/vocab.json +154 -0
  108. package/vocab.ts +18 -0
  109. package/wind.js +88 -0
  110. package/wind.js.map +1 -0
  111. package/wind.ts +114 -0
package/self.js ADDED
@@ -0,0 +1,93 @@
1
+ import * as system from "./system.js";
2
+ import { EXIT, PROCESS, RUNTIME } from "./comp.js";
3
+ let mine;
4
+ /** This process, as an entity — minted once, the same for every graph it
5
+ * opens. */ export let selfEid = ()=>mine ??= crypto.randomUUID();
6
+ /** Run as `eid`: the name the process starting this Worker thread gave it, so
7
+ * that process can speak of the thread after it is gone. Called before
8
+ * anything asks {@link selfEid}; a thread already named otherwise refuses. */ export let become = (eid)=>{
9
+ if (mine && mine != eid) throw new Error(`this thread is ${mine} already`);
10
+ mine = eid;
11
+ };
12
+ // The program a person ran: the executable, or under `deno` the script it
13
+ // runs — `yak.ts` is `yak` — since `deno run … yak.ts task new` is what a `yak`
14
+ // on the PATH execs, and `yak task new` is what the person typed.
15
+ let program = ()=>{
16
+ let exe = system.execPath().split('/').pop() ?? 'deno';
17
+ if (!/^(deno|node)(\.exe)?$/.test(exe)) return exe;
18
+ let script = new URL(system.mainModule).pathname.split('/').pop() ?? '';
19
+ return script.replace(/\.[cm]?[jt]sx?$/, '') || exe;
20
+ };
21
+ let line = ()=>{
22
+ try {
23
+ return [
24
+ program(),
25
+ ...system.args
26
+ ].join(' ');
27
+ } catch {
28
+ return 'yak';
29
+ }
30
+ };
31
+ let here = ()=>{
32
+ try {
33
+ return system.cwd();
34
+ } catch {
35
+ return undefined;
36
+ }
37
+ };
38
+ // The operating system and its release, or only its name where reading the
39
+ // release is not permitted.
40
+ let operating = ()=>{
41
+ try {
42
+ return `${system.build.os} ${system.osRelease()}`;
43
+ } catch {
44
+ return system.build.os;
45
+ }
46
+ };
47
+ /** What this process runs on: the runtime, its version and the operating
48
+ * system. */ export let runtime = ()=>({
49
+ name: system.runtimeName,
50
+ version: system.runtimeVersion,
51
+ os: operating()
52
+ });
53
+ /**
54
+ * The bundle a process writes when it starts: itself, running, and what it
55
+ * runs on.
56
+ *
57
+ * ```ts
58
+ * import { started } from '@yaks/process'
59
+ *
60
+ * // await graph.apply([started()])
61
+ * ```
62
+ */ export let started = (o = {})=>{
63
+ let cwd = o.cwd ?? here();
64
+ return {
65
+ entity: {
66
+ eid: selfEid()
67
+ },
68
+ [PROCESS]: {
69
+ pid: o.pid ?? system.pid,
70
+ command: o.command ?? line(),
71
+ ...cwd ? {
72
+ cwd
73
+ } : {},
74
+ ...o.roles ? {
75
+ roles: o.roles
76
+ } : {}
77
+ },
78
+ [RUNTIME]: runtime()
79
+ };
80
+ };
81
+ /**
82
+ * The bundle it writes at the end. A code it does not know is left out rather
83
+ * than guessed — an `exit` with nothing in it still records that the run is
84
+ * over, which is the fact anything reading the row is after.
85
+ */ export let ended = (code)=>({
86
+ entity: {
87
+ eid: selfEid()
88
+ },
89
+ [EXIT]: code == null ? {} : {
90
+ code
91
+ }
92
+ });
93
+ //# sourceMappingURL=self.js.map
package/self.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"self.js","sources":["./self.ts"],"names":[],"mappings":"AAAA,YAAY,0BAAyB;AAsCrC,SAAS,IAAI,EAAE,OAAO,EAAE,OAAO,oBAAiC;AAEhE,IAAI;AAEJ;UACU,GACV,OAAO,IAAI,UAAU,IAAW,SAAS,OAAO,UAAU,GAAS;AAEnE;;4EAE4E,GAC5E,OAAO,IAAI,SAAS,CAAC;EACnB,IAAI,QAAQ,QAAQ,KAAK,MAAM,IAAI,MAAM,CAAC,eAAe,EAAE,KAAK,QAAQ,CAAC;EACzE,OAAO;AACT,EAAC;AAeD,0EAA0E;AAC1E,gFAAgF;AAChF,kEAAkE;AAClE,IAAI,UAAU;EACZ,IAAI,MAAM,OAAO,QAAQ,GAAG,KAAK,CAAC,KAAK,GAAG,MAAM;EAChD,IAAI,CAAC,wBAAwB,IAAI,CAAC,MAAM,OAAO;EAC/C,IAAI,SAAS,IAAI,IAAI,OAAO,UAAU,EAAE,QAAQ,CA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0 +1,144 @@
1
+ import * as system from './system.js'
2
+ // The process you are IN, recorded like any other.
3
+ //
4
+ // This package already describes a program running on a machine, with
5
+ // `process{pid, command, cwd}` and then `exit{code}` once it is over, and it
6
+ // describes programs somebody here launched. The one it never described was
7
+ // the program doing the launching. So it does now: a process opening a graph
8
+ // writes a row for itself on the way in, with the `runtime` it runs on, and
9
+ // records its exit code on the way out.
10
+ //
11
+ // Two things follow from that row, and they are why it is worth writing.
12
+ //
13
+ // A write comes through something even when no client made a request — the
14
+ // rules a transaction fires, the effects that run after it commits, a file
15
+ // somebody imports — and that something is this run of this program, not a
16
+ // name a config file made up. So this row is the instrument the server writes
17
+ // through (@yaks/cli), which makes `created.via` on any row the answer to "which
18
+ // run wrote this", and a child process's row, written by its parent,
19
+ // identifies its parent without needing a property for it. A process is never
20
+ // the actor in `by`: it acts for nobody.
21
+ //
22
+ // A run that opened a graph says which roles it serves (@yaks/cli `compose`):
23
+ // the graph, `web`, `effects`, a plugin's service by its name. Start-up work
24
+ // is owed by the one that starts working the effects, not by every run
25
+ // (@yaks/effects `start`), since a command passing through changes nothing it
26
+ // would do.
27
+ //
28
+ // The id is minted once, in memory, and it is a uuid rather than something
29
+ // derived from the process: two runs of one command are two different runs, and
30
+ // the kernel recycles a pid within the hour.
31
+ //
32
+ // A Worker thread that opens a graph is a run of its own, with a row of its
33
+ // own in the pid it runs in. It has to be: what it holds must be free the
34
+ // moment it is over, and its pid goes on after it, so its `exit` is what says
35
+ // it is over (./run.ts `gone`). A process that ends its thread where it stands
36
+ // names it first ({@link become}), and writes that `exit` for it.
37
+
38
+ import type { Bundle, Eid } from 'jsr:@yaks/graph@0.2.36'
39
+ import { EXIT, PROCESS, RUNTIME, type Runtime } from './comp.js'
40
+
41
+ let mine: Eid | undefined
42
+
43
+ /** This process, as an entity — minted once, the same for every graph it
44
+ * opens. */
45
+ export let selfEid = (): Eid => mine ??= crypto.randomUUID() as Eid
46
+
47
+ /** Run as `eid`: the name the process starting this Worker thread gave it, so
48
+ * that process can speak of the thread after it is gone. Called before
49
+ * anything asks {@link selfEid}; a thread already named otherwise refuses. */
50
+ export let become = (eid: Eid): void => {
51
+ if (mine && mine != eid) throw new Error(`this thread is ${mine} already`)
52
+ mine = eid
53
+ }
54
+
55
+ /** What a run records about itself where the runtime cannot tell it (a test
56
+ * standing in for a process, a caller naming its children its own way). */
57
+ export type SelfOpts = {
58
+ /** the process id (default this one's) */
59
+ pid?: number
60
+ /** the command line, argv joined by spaces (default this one's) */
61
+ command?: string
62
+ /** where it runs (default this one's) */
63
+ cwd?: string
64
+ /** the roles it serves where it opened a graph (@yaks/cli `compose`) */
65
+ roles?: string[]
66
+ }
67
+
68
+ // The program a person ran: the executable, or under `deno` the script it
69
+ // runs — `yak.ts` is `yak` — since `deno run … yak.ts task new` is what a `yak`
70
+ // on the PATH execs, and `yak task new` is what the person typed.
71
+ let program = (): string => {
72
+ let exe = system.execPath().split('/').pop() ?? 'deno'
73
+ if (!/^(deno|node)(\.exe)?$/.test(exe)) return exe
74
+ let script = new URL(system.mainModule).pathname.split('/').pop() ?? ''
75
+ return script.replace(/\.[cm]?[jt]sx?$/, '') || exe
76
+ }
77
+
78
+ let line = (): string => {
79
+ try {
80
+ return [program(), ...system.args].join(' ')
81
+ } catch {
82
+ return 'yak'
83
+ }
84
+ }
85
+
86
+ let here = (): string | undefined => {
87
+ try {
88
+ return system.cwd()
89
+ } catch {
90
+ return undefined
91
+ }
92
+ }
93
+
94
+ // The operating system and its release, or only its name where reading the
95
+ // release is not permitted.
96
+ let operating = (): string => {
97
+ try {
98
+ return `${system.build.os} ${system.osRelease()}`
99
+ } catch {
100
+ return system.build.os
101
+ }
102
+ }
103
+
104
+ /** What this process runs on: the runtime, its version and the operating
105
+ * system. */
106
+ export let runtime = (): Runtime => ({
107
+ name: system.runtimeName,
108
+ version: system.runtimeVersion,
109
+ os: operating(),
110
+ })
111
+
112
+ /**
113
+ * The bundle a process writes when it starts: itself, running, and what it
114
+ * runs on.
115
+ *
116
+ * ```ts
117
+ * import { started } from '@yaks/process'
118
+ *
119
+ * // await graph.apply([started()])
120
+ * ```
121
+ */
122
+ export let started = (o: SelfOpts = {}): Bundle => {
123
+ let cwd = o.cwd ?? here()
124
+ return {
125
+ entity: { eid: selfEid() },
126
+ [PROCESS]: {
127
+ pid: o.pid ?? system.pid,
128
+ command: o.command ?? line(),
129
+ ...cwd ? { cwd } : {},
130
+ ...o.roles ? { roles: o.roles } : {},
131
+ },
132
+ [RUNTIME]: runtime(),
133
+ }
134
+ }
135
+
136
+ /**
137
+ * The bundle it writes at the end. A code it does not know is left out rather
138
+ * than guessed — an `exit` with nothing in it still records that the run is
139
+ * over, which is the fact anything reading the row is after.
140
+ */
141
+ export let ended = (code?: number | null): Bundle => ({
142
+ entity: { eid: selfEid() },
143
+ [EXIT]: code == null ? {} : { code },
144
+ })
package/socket.js ADDED
@@ -0,0 +1,38 @@
1
+ // A byte connection. Callers own framing and deadlines; the boundary owns I/O.
2
+ import { createConnection } from 'node:net';
3
+ import { Readable } from 'node:stream';
4
+ export let isConnectionRefused = (error)=>{
5
+ let e = error;
6
+ return e?.code == 'ECONNREFUSED' || e?.name == 'ConnectionRefused';
7
+ };
8
+ /** Connect to an operating-system socket; protocols stay with the caller. */ export let connect = async (options)=>{
9
+ let socket = createConnection({
10
+ path: options.path
11
+ });
12
+ await new Promise((resolve, reject)=>{
13
+ socket.once('connect', resolve);
14
+ socket.once('error', reject);
15
+ });
16
+ let reader = Readable.toWeb(socket).getReader();
17
+ let held = new Uint8Array(0);
18
+ return {
19
+ read: async (buffer)=>{
20
+ if (!held.length) {
21
+ let next = await reader.read();
22
+ if (next.done) return null;
23
+ held = next.value;
24
+ }
25
+ let count = Math.min(buffer.length, held.length);
26
+ buffer.set(held.subarray(0, count));
27
+ held = held.subarray(count);
28
+ return count;
29
+ },
30
+ write: (bytes)=>new Promise((resolve, reject)=>{
31
+ socket.write(bytes, (error)=>error ? reject(error) : resolve(bytes.length));
32
+ }),
33
+ close: ()=>{
34
+ socket.destroy();
35
+ }
36
+ };
37
+ };
38
+ //# sourceMappingURL=socket.js.map
package/socket.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"socket.js","sources":["./socket.ts"],"names":[],"mappings":"AAAA,+EAA+E;AAC/E,SAAS,gBAAgB,QAAQ,WAAU;AAC3C,SAAS,QAAQ,QAAQ,cAAa;AAQtC,OAAO,IAAI,sBAAsB,CAAC;EAChC,IAAI,IAAI;EACR,OAAO,GAAG,QAAQ,kBAAkB,GAAG,QAAQ;AACjD,EAAC;AAED,2EAA2E,GAC3E,OAAO,IAAI,UAAU,OACnB;EAEA,IAAI,SAAS,iBAAiB;IAAE,MAAM,QAAQ,IAAI;EAAC;EACnD,MAAM,IAAI,QAAc,CAAC,SAAS;IAChC,OAAO,IAAI,CAAC,WAAW;IACvB,OAAO,IAAI,CAAC,SAAS;EACvB;EACA,IAAI,SAAS,AAAC,SAAS,KAAK,CAAC,QAC1B,SAAS;EACZ,IAAI,OAAmB,IAAI,WAAW;EACtC,OAAO;IACL,MAAM,OAAO;MACX,IAAI,CAAC,KAAK,MAAM,EAAE;QAChB,IAAI,OAAO,MAAM,OAAO,IAAI;QAC5B,IAAI,KAAK,IAAI,EAAE,OAAO;QACtB,OAAO,KAAK,KAAK;MACnB;MACA,IAAI,QAAQ,KAAK,GAAG,CAAC,OAAO,MAAM,EAAE,KAAK,MAAM;MAC/C,OAAO,GAAG,CAAC,KAAK,QAAQ,CAAC,GAAG;MAC5B,OAAO,KAAK,QAAQ,CAAC;MACrB,OAAO;IACT;IACA,OAAO,CAAC,QACN,IAAI,QAAQ,CAAC,SAAS;QACpB,OAAO,KAAK,CAAC,OAAO,CAAC,QACnB,QAAQ,OAAO,SAAS,QAAQ,MAAM,MAAM;MAChD;IACF,OAAO;MACL,OAAO,OAAO;IAChB;EACF;AACF,EAAC"}
package/socket.ts ADDED
@@ -0,0 +1,49 @@
1
+ // A byte connection. Callers own framing and deadlines; the boundary owns I/O.
2
+ import { createConnection } from 'node:net'
3
+ import { Readable } from 'node:stream'
4
+
5
+ export type Connection = {
6
+ read: (buffer: Uint8Array) => Promise<number | null>
7
+ write: (bytes: Uint8Array) => Promise<number>
8
+ close: () => void
9
+ }
10
+
11
+ export let isConnectionRefused = (error: unknown): boolean => {
12
+ let e = error as { code?: string; name?: string } | undefined
13
+ return e?.code == 'ECONNREFUSED' || e?.name == 'ConnectionRefused'
14
+ }
15
+
16
+ /** Connect to an operating-system socket; protocols stay with the caller. */
17
+ export let connect = async (
18
+ options: { transport: 'unix'; path: string },
19
+ ): Promise<Connection> => {
20
+ let socket = createConnection({ path: options.path })
21
+ await new Promise<void>((resolve, reject) => {
22
+ socket.once('connect', resolve)
23
+ socket.once('error', reject)
24
+ })
25
+ let reader = (Readable.toWeb(socket) as ReadableStream<Uint8Array>)
26
+ .getReader()
27
+ let held: Uint8Array = new Uint8Array(0)
28
+ return {
29
+ read: async (buffer) => {
30
+ if (!held.length) {
31
+ let next = await reader.read()
32
+ if (next.done) return null
33
+ held = next.value
34
+ }
35
+ let count = Math.min(buffer.length, held.length)
36
+ buffer.set(held.subarray(0, count))
37
+ held = held.subarray(count)
38
+ return count
39
+ },
40
+ write: (bytes) =>
41
+ new Promise((resolve, reject) => {
42
+ socket.write(bytes, (error) =>
43
+ error ? reject(error) : resolve(bytes.length))
44
+ }),
45
+ close: () => {
46
+ socket.destroy()
47
+ },
48
+ }
49
+ }
package/store.js ADDED
@@ -0,0 +1,30 @@
1
+ // The graph interface a supervisor needs, which is four methods wide: write a
2
+ // list of bundles in one transaction, read given rows whole, ask which
3
+ // processes have not exited, and ask which programs are wanted.
4
+ //
5
+ // It is a named interface rather than a whole `Graph` because those reads are
6
+ // the only ones this package makes, and a caller that keeps its processes in a
7
+ // database of its own (the fleet's src/processes.ts does) can implement them
8
+ // with one statement each instead of standing up a query engine. Over a
9
+ // @yaks/graph, {@link store} is that adapter and each read is one ordinary
10
+ // read.
11
+ import { line } from "@yaks/query";
12
+ import { EXIT, PROCESS, SERVICE } from "./comp.js";
13
+ /** The query {@link store} runs: every process with no `exit` yet, whole. */ export let RUNNING = line`.${PROCESS}&!${EXIT}&*`;
14
+ /** The query {@link store} runs for what is wanted: every service row. */ export let SERVICES = line`.${SERVICE}&*`;
15
+ /**
16
+ * A @yaks/graph as this package's store — every read is an ordinary one, so
17
+ * nothing here needs privileged access.
18
+ *
19
+ * ```ts
20
+ * import { store } from '@yaks/process'
21
+ *
22
+ * // let processes = store(graph)
23
+ * ```
24
+ */ export let store = (graph)=>({
25
+ apply: (bundles)=>graph.apply(bundles),
26
+ get: (eids)=>graph.get(eids),
27
+ running: ()=>graph.read(RUNNING),
28
+ services: ()=>graph.read(SERVICES)
29
+ });
30
+ //# sourceMappingURL=store.js.map
package/store.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"store.js","sources":["./store.ts"],"names":[],"mappings":"AAAA,8EAA8E;AAC9E,uEAAuE;AACvE,gEAAgE;AAChE,EAAE;AACF,8EAA8E;AAC9E,+EAA+E;AAC/E,6EAA6E;AAC7E,wEAAwE;AACxE,2EAA2E;AAC3E,QAAQ;AAGR,SAAqB,IAAI,2BAAgC;AACzD,SAAS,IAAI,EAAE,OAAO,EAAE,OAAO,oBAAmB;AAelD,2EAA2E,GAC3E,OAAO,IAAI,UAAiB,IAAI,CAAC,CAAC,EAAE,QAAQ,EAAE,EAAE,KAAK,EAAE,CAAC,CAAA;AAExD,wEAAwE,GACxE,OAAO,IAAI,WAAkB,IAAI,CAAC,CAAC,EAAE,QAAQ,EAAE,CAAC,CAAA;AAEhD;;;;;;;;;CASC,GACD,OAAO,IAAI,QAAQ,CAAC,QAAwD,CAAC;IAC3E,OAAO,CAAC,UAAY,MAAM,KAAK,CAAC;IAChC,KAAK,CAAC,OAAS,MAAM,GAAG,CAAC;IACzB,SAAS,IAAM,MAAM,IAAI,CAAC;IAC1B,UAAU,IAAM,MAAM,IAAI,CAAC;EAC7B,CAAC,EAAC"}
package/store.ts ADDED
@@ -0,0 +1,50 @@
1
+ // The graph interface a supervisor needs, which is four methods wide: write a
2
+ // list of bundles in one transaction, read given rows whole, ask which
3
+ // processes have not exited, and ask which programs are wanted.
4
+ //
5
+ // It is a named interface rather than a whole `Graph` because those reads are
6
+ // the only ones this package makes, and a caller that keeps its processes in a
7
+ // database of its own (the fleet's src/processes.ts does) can implement them
8
+ // with one statement each instead of standing up a query engine. Over a
9
+ // @yaks/graph, {@link store} is that adapter and each read is one ordinary
10
+ // read.
11
+
12
+ import type { Bundle, Graph } from 'jsr:@yaks/graph@0.2.36'
13
+ import { type Built, line } from '@yaks/query'
14
+ import { EXIT, PROCESS, SERVICE } from './comp.js'
15
+
16
+ /** Where a supervisor's rows live. */
17
+ export type Store = {
18
+ /** write these bundles in one transaction → the bundles as written */
19
+ apply: (bundles: Bundle[]) => Bundle[] | Promise<Bundle[]>
20
+ /** the rows these eids name, whole; an eid nothing stands for is left out */
21
+ get: (eids: string[]) => Bundle[] | Promise<Bundle[]>
22
+ /** every process that has not exited, as whole bundles */
23
+ running: () => Bundle[] | Promise<Bundle[]>
24
+ /** every service row, whole — its `process`, `exit` and `stop` components
25
+ * come back in the same bundle, because they are on the same entity */
26
+ services: () => Bundle[] | Promise<Bundle[]>
27
+ }
28
+
29
+ /** The query {@link store} runs: every process with no `exit` yet, whole. */
30
+ export let RUNNING: Built = line`.${PROCESS}&!${EXIT}&*`
31
+
32
+ /** The query {@link store} runs for what is wanted: every service row. */
33
+ export let SERVICES: Built = line`.${SERVICE}&*`
34
+
35
+ /**
36
+ * A @yaks/graph as this package's store — every read is an ordinary one, so
37
+ * nothing here needs privileged access.
38
+ *
39
+ * ```ts
40
+ * import { store } from '@yaks/process'
41
+ *
42
+ * // let processes = store(graph)
43
+ * ```
44
+ */
45
+ export let store = (graph: Pick<Graph, 'read' | 'apply' | 'get'>): Store => ({
46
+ apply: (bundles) => graph.apply(bundles),
47
+ get: (eids) => graph.get(eids),
48
+ running: () => graph.read(RUNNING),
49
+ services: () => graph.read(SERVICES),
50
+ })
package/system.js ADDED
@@ -0,0 +1,161 @@
1
+ // This program's operating-system boundary. Callers do not depend on its runtime.
2
+ import process from 'node:process';
3
+ import { cpus, hostname as host, loadavg, release, uptime } from 'node:os';
4
+ import { format, inspect } from 'node:util';
5
+ import { readSync, writeSync } from 'node:fs';
6
+ import { Readable, Writable } from 'node:stream';
7
+ import { pathToFileURL } from 'node:url';
8
+ import { constants } from 'node:os';
9
+ export { env } from "./env.js";
10
+ export { build, os } from "./platform.js";
11
+ export { cpus, format, inspect, loadavg };
12
+ export { clearInterval, clearTimeout, setInterval, setTimeout } from 'node:timers';
13
+ export let args = process.argv.slice(2);
14
+ export let pid = process.pid;
15
+ export let ppid = process.ppid;
16
+ export let cwd = ()=>process.cwd();
17
+ export let execPath = ()=>process.execPath;
18
+ // Deno names a module run from a URL (`deno run jsr:…`) only as
19
+ // `Deno.mainModule`: its `process.argv[1]` is a placeholder file.
20
+ let deno = globalThis.Deno;
21
+ export let mainModule = deno?.mainModule ?? (process.argv[1] ? pathToFileURL(process.argv[1]).href : '');
22
+ // Deno tells a main module whose top-level await rejected as an unhandled
23
+ // rejection, the same way it tells a promise nobody held. Importing the main
24
+ // module after it failed rejects with that same reason; an import made while
25
+ // it still runs says nothing of how it ends, so each question imports anew. A
26
+ // Worker has no `Deno.mainModule`, and its `argv[1]` names the main thread's
27
+ // module, so a Worker never imports it.
28
+ let NONE = Symbol('none');
29
+ /** Whether `reason`, met as an unhandled rejection, is the main module's own
30
+ * failure (a top-level await that rejected) rather than a promise nobody
31
+ * held. Answers within `ms`. */ export let mainFailed = async (reason, ms = 50)=>{
32
+ if (!deno?.mainModule) return false;
33
+ let timer;
34
+ let failure = await Promise.race([
35
+ import(deno.mainModule).then(()=>NONE, (failure)=>failure),
36
+ new Promise((done)=>timer = setTimeout(done, ms, NONE))
37
+ ]);
38
+ clearTimeout(timer);
39
+ return failure !== NONE && failure === reason;
40
+ };
41
+ export let version = {
42
+ ...process.versions
43
+ };
44
+ export let runtimeName = process.versions.deno ? 'deno' : 'node';
45
+ export let runtimeVersion = process.versions.deno ?? process.versions.node;
46
+ export let osRelease = release;
47
+ export let uid = ()=>process.getuid?.() ?? null;
48
+ /** When this machine last started, in milliseconds since the epoch. */ export let booted = ()=>Date.now() - uptime() * 1000;
49
+ /** What this machine calls itself. */ export let hostname = ()=>host();
50
+ export let exit = (code)=>process.exit(code);
51
+ export let setExitCode = (code)=>{
52
+ process.exitCode = code;
53
+ };
54
+ export let kill = (pid, signal = 'SIGTERM')=>{
55
+ process.kill(pid, signal);
56
+ };
57
+ /** The parent's IPC channel closing, including before this listener starts.
58
+ * The channel is separate from stdin and is not inherited by grandchildren. */ export let disconnected = (source = process)=>new Promise((resolve)=>source.connected ? source.once('disconnect', resolve) : resolve());
59
+ export let addSignalListener = (signal, fn)=>{
60
+ process.on(signal, fn);
61
+ };
62
+ export let removeSignalListener = (signal, fn)=>{
63
+ process.off(signal, fn);
64
+ };
65
+ export let cpuUsage = process.cpuUsage;
66
+ export let memoryUsage = ()=>process.memoryUsage();
67
+ let trims;
68
+ let libc = ()=>{
69
+ let deno = globalThis.Deno;
70
+ if (!deno || process.platform != 'linux') return null;
71
+ try {
72
+ return deno.dlopen('libc.so.6', {
73
+ malloc_trim: {
74
+ parameters: [
75
+ 'usize'
76
+ ],
77
+ result: 'i32'
78
+ }
79
+ });
80
+ } catch {
81
+ // A libc that is not glibc's.
82
+ return null;
83
+ }
84
+ };
85
+ /** Gives the memory this process freed back to the system, where it can. */ export let trim = ()=>{
86
+ ;
87
+ (trims === undefined ? trims = libc() : trims)?.symbols.malloc_trim(0n);
88
+ };
89
+ export let signalCode = (signal)=>signal ? 128 + (constants.signals[signal] ?? 0) : 1;
90
+ export let unrefTimer = (timer)=>{
91
+ if (timer && typeof timer == 'object' && 'unref' in timer && typeof timer.unref == 'function') timer.unref();
92
+ };
93
+ let input;
94
+ export let stdin = {
95
+ isTerminal: ()=>!!process.stdin.isTTY,
96
+ setRaw: (raw, _options)=>{
97
+ process.stdin.setRawMode(raw);
98
+ },
99
+ get readable () {
100
+ return input ??= Readable.toWeb(process.stdin);
101
+ },
102
+ read: async (buffer, signal)=>{
103
+ signal?.throwIfAborted();
104
+ let stream = process.stdin;
105
+ let chunk = stream.read(buffer.length);
106
+ if (!chunk) {
107
+ if (stream.readableEnded) return null;
108
+ await new Promise((resolve, reject)=>{
109
+ let clear = ()=>{
110
+ stream.off('readable', ready);
111
+ stream.off('error', failed);
112
+ stream.off('end', ready);
113
+ signal?.removeEventListener('abort', aborted);
114
+ };
115
+ let ready = ()=>{
116
+ clear();
117
+ resolve();
118
+ };
119
+ let failed = (error)=>{
120
+ clear();
121
+ reject(error);
122
+ };
123
+ let aborted = ()=>{
124
+ clear();
125
+ reject(signal?.reason);
126
+ };
127
+ stream.once('readable', ready);
128
+ stream.once('error', failed);
129
+ stream.once('end', ready);
130
+ signal?.addEventListener('abort', aborted, {
131
+ once: true
132
+ });
133
+ });
134
+ signal?.throwIfAborted();
135
+ chunk = stream.read(buffer.length) ?? stream.read();
136
+ }
137
+ if (!chunk) return null;
138
+ let count = Math.min(buffer.length, chunk.length);
139
+ buffer.set(chunk.subarray(0, count));
140
+ if (chunk.length > count) stream.unshift(chunk.subarray(count));
141
+ return count;
142
+ },
143
+ readSync: (buffer)=>readSync(0, buffer, 0, buffer.length, null) || null
144
+ };
145
+ let output = (stream, fd)=>({
146
+ isTerminal: ()=>!!stream.isTTY,
147
+ writeSync: (buffer)=>writeSync(fd, buffer),
148
+ write: (buffer)=>new Promise((resolve, reject)=>{
149
+ stream.write(buffer, (error)=>error ? reject(error) : resolve(buffer.length));
150
+ }),
151
+ get writable () {
152
+ return Writable.toWeb(stream);
153
+ }
154
+ });
155
+ export let stdout = output(process.stdout, 1);
156
+ export let stderr = output(process.stderr, 2);
157
+ export let consoleSize = ()=>({
158
+ columns: process.stdout.columns ?? 80,
159
+ rows: process.stdout.rows ?? 24
160
+ });
161
+ //# sourceMappingURL=system.js.map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