@nyte-ai/host 0.0.0-stage → 0.0.19
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.
- package/LICENSE +21 -0
- package/README.md +6 -2
- package/THIRD-PARTY-NOTICES.md +866 -0
- package/dist/chunks/canvas-preview-0cmbkp2j.js +65 -0
- package/dist/chunks/canvas-preview-0nq75h9k.js +7 -0
- package/dist/chunks/canvas-preview-11nadadc.js +241 -0
- package/dist/chunks/canvas-preview-1sq7z9jq.js +17 -0
- package/dist/chunks/canvas-preview-2jwbe14k.js +130 -0
- package/dist/chunks/canvas-preview-2zdyh9ny.js +26 -0
- package/dist/chunks/canvas-preview-31w7xm8z.js +34 -0
- package/dist/chunks/canvas-preview-4b6sayhg.js +295 -0
- package/dist/chunks/canvas-preview-4e0pxa32.js +63 -0
- package/dist/chunks/canvas-preview-518dh8by.js +169 -0
- package/dist/chunks/canvas-preview-55s0y68g.js +387 -0
- package/dist/chunks/canvas-preview-5ekjx6sw.js +613 -0
- package/dist/chunks/canvas-preview-5mex725c.js +14 -0
- package/dist/chunks/canvas-preview-5z63am63.js +295 -0
- package/dist/chunks/canvas-preview-65xqjw7f.js +20 -0
- package/dist/chunks/canvas-preview-69fkk9yn.js +95 -0
- package/dist/chunks/canvas-preview-8xc0vn7v.js +458 -0
- package/dist/chunks/canvas-preview-9aqczacs.js +73 -0
- package/dist/chunks/canvas-preview-af86jz8t.js +141 -0
- package/dist/chunks/canvas-preview-aq5j4p25.js +240 -0
- package/dist/chunks/canvas-preview-b2v8wfnz.js +36 -0
- package/dist/chunks/canvas-preview-befkjea2.js +60 -0
- package/dist/chunks/canvas-preview-bxnqt8cd.js +103 -0
- package/dist/chunks/canvas-preview-cawy6vbz.js +352 -0
- package/dist/chunks/canvas-preview-crmzxxt8.js +518 -0
- package/dist/chunks/canvas-preview-drctegz4.js +591 -0
- package/dist/chunks/canvas-preview-e3vnkkc5.js +134 -0
- package/dist/chunks/canvas-preview-ec2mt7es.js +108 -0
- package/dist/chunks/canvas-preview-fxa0rfb0.js +61 -0
- package/dist/chunks/canvas-preview-g5fkjs34.js +598 -0
- package/dist/chunks/canvas-preview-jeddjex8.js +166 -0
- package/dist/chunks/canvas-preview-k1yg4fgp.js +571 -0
- package/dist/chunks/canvas-preview-kdqgrstc.js +194 -0
- package/dist/chunks/canvas-preview-kn7kvbfj.js +12 -0
- package/dist/chunks/canvas-preview-kp75gb63.js +141 -0
- package/dist/chunks/canvas-preview-mgcx2b2p.js +271 -0
- package/dist/chunks/canvas-preview-n2a35zv5.js +81 -0
- package/dist/chunks/canvas-preview-pkzqg96v.js +63 -0
- package/dist/chunks/canvas-preview-rp4x6y1h.js +144 -0
- package/dist/chunks/canvas-preview-s94ebwf5.js +112 -0
- package/dist/chunks/canvas-preview-skzwvw4d.js +23 -0
- package/dist/chunks/canvas-preview-srgfpdmf.js +60 -0
- package/dist/chunks/canvas-preview-tjhabynd.js +252 -0
- package/dist/chunks/canvas-preview-tnqe7k0b.js +374 -0
- package/dist/chunks/canvas-preview-vaatfznm.js +189 -0
- package/dist/chunks/canvas-preview-vm9zdp2t.js +398 -0
- package/dist/chunks/canvas-preview-w54ymtb3.js +272 -0
- package/dist/chunks/canvas-preview-wg80rbc2.js +46 -0
- package/dist/chunks/canvas-preview-y5y6ghaw.js +125 -0
- package/dist/chunks/canvas-preview-ycepb05s.js +176 -0
- package/dist/chunks/canvas-preview-yjz0f7n4.js +50 -0
- package/dist/chunks/canvas-preview-zaaj99wd.js +244 -0
- package/dist/src/canvas-preview.d.ts +29 -0
- package/dist/src/canvas-preview.js +271 -0
- package/dist/src/credentials.d.ts +44 -0
- package/dist/src/credentials.js +10 -0
- package/dist/src/deploy/cloudflare/worker.d.ts +50 -0
- package/dist/src/deploy/cloudflare/worker.js +196 -0
- package/dist/src/deploy/vercel/nitro.config.d.ts +2 -0
- package/dist/src/deploy/vercel/nitro.config.js +30 -0
- package/dist/src/deploy/vercel/server/routes/[...].d.ts +2 -0
- package/dist/src/deploy/vercel/server/routes/[...].js +18 -0
- package/dist/src/deploy/vercel/server/utils/host.d.ts +13 -0
- package/dist/src/deploy/vercel/server/utils/host.js +7 -0
- package/dist/src/deploy/vercel/server/utils/repair.d.ts +1 -0
- package/dist/src/deploy/vercel/server/utils/repair.js +6 -0
- package/dist/src/deploy/vercel/server/utils/step.d.ts +10 -0
- package/dist/src/deploy/vercel/server/utils/step.js +6 -0
- package/dist/src/deploy/vercel/server/workflows/drive.d.ts +14 -0
- package/dist/src/deploy/vercel/server/workflows/drive.js +13 -0
- package/dist/src/durable-object.d.ts +14 -0
- package/dist/src/durable-object.js +231 -0
- package/dist/src/hash.d.ts +1 -0
- package/dist/src/hash.js +6 -0
- package/dist/src/identity-key.d.ts +11 -0
- package/dist/src/identity-key.js +9 -0
- package/dist/src/identity.d.ts +12 -0
- package/dist/src/identity.js +6 -0
- package/dist/src/images/exif.d.ts +1 -0
- package/dist/src/images/exif.js +6 -0
- package/dist/src/images/node.d.ts +37 -0
- package/dist/src/images/node.js +12 -0
- package/dist/src/images/photon.d.ts +3 -0
- package/dist/src/images/photon.js +7 -0
- package/dist/src/images/worker.d.ts +1 -0
- package/dist/src/images/worker.js +23 -0
- package/dist/src/index.d.ts +20 -0
- package/dist/src/index.js +121 -0
- package/dist/src/key-file.d.ts +1 -0
- package/dist/src/key-file.js +7 -0
- package/dist/src/listen-fetch.d.ts +8 -0
- package/dist/src/listen-fetch.js +9 -0
- package/dist/src/listen.d.ts +4 -0
- package/dist/src/listen.js +10 -0
- package/dist/src/local/command.d.ts +20 -0
- package/dist/src/local/command.js +12 -0
- package/dist/src/local/feed.d.ts +7 -0
- package/dist/src/local/feed.js +6 -0
- package/dist/src/local/files.d.ts +18 -0
- package/dist/src/local/files.js +13 -0
- package/dist/src/local/git-watch.d.ts +15 -0
- package/dist/src/local/git-watch.js +7 -0
- package/dist/src/local/git.d.ts +5 -0
- package/dist/src/local/git.js +13 -0
- package/dist/src/local/github.d.ts +30 -0
- package/dist/src/local/github.js +9 -0
- package/dist/src/local/identity.d.ts +3 -0
- package/dist/src/local/identity.js +7 -0
- package/dist/src/local/jobs.d.ts +7 -0
- package/dist/src/local/jobs.js +7 -0
- package/dist/src/local/legacy-trust.d.ts +10 -0
- package/dist/src/local/legacy-trust.js +6 -0
- package/dist/src/local/patch.d.ts +16 -0
- package/dist/src/local/patch.js +17 -0
- package/dist/src/local/provider.d.ts +11 -0
- package/dist/src/local/provider.js +20 -0
- package/dist/src/local/snapshots.d.ts +7 -0
- package/dist/src/local/snapshots.js +11 -0
- package/dist/src/local/text.d.ts +1 -0
- package/dist/src/local/text.js +6 -0
- package/dist/src/local/tree.d.ts +50 -0
- package/dist/src/local/tree.js +16 -0
- package/dist/src/local/workspace-migration.d.ts +2 -0
- package/dist/src/local/workspace-migration.js +7 -0
- package/dist/src/local/worktrees.d.ts +46 -0
- package/dist/src/local/worktrees.js +14 -0
- package/dist/src/mcp/config.d.ts +75 -0
- package/dist/src/mcp/config.js +12 -0
- package/dist/src/mcp/connect.d.ts +15 -0
- package/dist/src/mcp/connect.js +9 -0
- package/dist/src/mcp/hub.d.ts +19 -0
- package/dist/src/mcp/hub.js +11 -0
- package/dist/src/mcp/oauth.d.ts +46 -0
- package/dist/src/mcp/oauth.js +12 -0
- package/dist/src/mcp/tools.d.ts +29 -0
- package/dist/src/mcp/tools.js +12 -0
- package/dist/src/open.d.ts +14 -0
- package/dist/src/open.js +46 -0
- package/dist/src/plugins/loader.d.ts +21 -0
- package/dist/src/plugins/loader.js +11 -0
- package/dist/src/postgres.d.ts +19 -0
- package/dist/src/postgres.js +8 -0
- package/dist/src/providers.d.ts +16 -0
- package/dist/src/providers.js +112 -0
- package/dist/src/secrets.d.ts +12 -0
- package/dist/src/secrets.js +8 -0
- package/dist/src/serve/binding.d.ts +39 -0
- package/dist/src/serve/binding.js +6 -0
- package/dist/src/serve/handler.d.ts +15 -0
- package/dist/src/serve/handler.js +8 -0
- package/dist/src/serve/stream-server.d.ts +6 -0
- package/dist/src/serve/stream-server.js +6 -0
- package/dist/src/session-index.d.ts +11 -0
- package/dist/src/session-index.js +11 -0
- package/dist/src/shell-env.d.ts +14 -0
- package/dist/src/shell-env.js +108 -0
- package/dist/src/sqlite-workers.d.ts +6 -0
- package/dist/src/sqlite-workers.js +6 -0
- package/dist/src/sqlite.d.ts +8 -0
- package/dist/src/sqlite.js +11 -0
- package/dist/src/store-rows.d.ts +13 -0
- package/dist/src/store-rows.js +26 -0
- package/dist/src/store-usage.d.ts +25 -0
- package/dist/src/store-usage.js +19 -0
- package/dist/src/usage-history.d.ts +54 -0
- package/dist/src/usage-history.js +14 -0
- package/dist/src/usage-scan.d.ts +24 -0
- package/dist/src/usage-scan.js +65 -0
- package/dist/src/usage-service.d.ts +23 -0
- package/dist/src/usage-service.js +16 -0
- package/dist/src/usage-summary.d.ts +9 -0
- package/dist/src/usage-summary.js +16 -0
- package/dist/src/usage-types.d.ts +86 -0
- package/dist/src/usage-types.js +0 -0
- package/dist/src/usage-worker.d.ts +1 -0
- package/dist/src/usage-worker.js +29 -0
- package/dist/src/usage.d.ts +195 -0
- package/dist/src/usage.js +23 -0
- package/package.json +140 -3
- package/src/canvas-preview.ts +403 -0
- package/src/credentials.ts +294 -0
- package/src/deploy/cloudflare/README.md +3 -0
- package/src/deploy/cloudflare/worker.ts +306 -0
- package/src/deploy/cloudflare/wrangler.jsonc +12 -0
- package/src/deploy/vercel/README.md +3 -0
- package/src/deploy/vercel/nitro.config.ts +29 -0
- package/src/deploy/vercel/server/routes/[...].ts +10 -0
- package/src/deploy/vercel/server/utils/host.ts +104 -0
- package/src/deploy/vercel/server/utils/repair.ts +35 -0
- package/src/deploy/vercel/server/utils/step.ts +25 -0
- package/src/deploy/vercel/server/workflows/drive.ts +46 -0
- package/src/deploy/vercel/tsconfig.json +9 -0
- package/src/deploy/vercel/types/bun.d.ts +2 -0
- package/src/durable-object.ts +386 -0
- package/src/hash.ts +5 -0
- package/src/identity-key.ts +20 -0
- package/src/identity.ts +66 -0
- package/src/images/exif.ts +69 -0
- package/src/images/node.ts +163 -0
- package/src/images/photon.ts +141 -0
- package/src/images/worker.ts +29 -0
- package/src/index.ts +39 -0
- package/src/key-file.ts +53 -0
- package/src/listen-fetch.ts +409 -0
- package/src/listen.ts +228 -0
- package/src/local/command.ts +161 -0
- package/src/local/feed.ts +70 -0
- package/src/local/files.ts +817 -0
- package/src/local/git-watch.ts +199 -0
- package/src/local/git.ts +919 -0
- package/src/local/github.ts +691 -0
- package/src/local/identity.ts +21 -0
- package/src/local/jobs.ts +573 -0
- package/src/local/legacy-trust.ts +105 -0
- package/src/local/patch.ts +334 -0
- package/src/local/provider.ts +184 -0
- package/src/local/snapshots.ts +778 -0
- package/src/local/text.ts +9 -0
- package/src/local/tree.ts +398 -0
- package/src/local/workspace-migration.ts +95 -0
- package/src/local/worktrees.ts +239 -0
- package/src/mcp/config.ts +330 -0
- package/src/mcp/connect.ts +92 -0
- package/src/mcp/hub.ts +836 -0
- package/src/mcp/oauth.ts +436 -0
- package/src/mcp/tools.ts +333 -0
- package/src/open.ts +449 -0
- package/src/plugins/loader.ts +186 -0
- package/src/postgres.ts +492 -0
- package/src/providers.ts +135 -0
- package/src/secrets.ts +31 -0
- package/src/serve/binding.ts +629 -0
- package/src/serve/handler.ts +362 -0
- package/src/serve/stream-server.ts +176 -0
- package/src/session-index.ts +99 -0
- package/src/shell-env.ts +153 -0
- package/src/sqlite-workers.ts +283 -0
- package/src/sqlite.ts +553 -0
- package/src/store-rows.ts +110 -0
- package/src/store-usage.ts +347 -0
- package/src/usage-history.ts +219 -0
- package/src/usage-scan.ts +81 -0
- package/src/usage-service.ts +82 -0
- package/src/usage-summary.ts +79 -0
- package/src/usage-types.ts +87 -0
- package/src/usage-worker.ts +24 -0
- package/src/usage.ts +776 -0
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import type { ChildProcess, SpawnOptions } from "node:child_process";
|
|
3
|
+
import { constants } from "node:fs";
|
|
4
|
+
import { access, stat } from "node:fs/promises";
|
|
5
|
+
import { delimiter, extname, isAbsolute, join } from "node:path";
|
|
6
|
+
import { SdkError } from "@nyte-ai/schema";
|
|
7
|
+
import { processEnvironment } from "@nyte-ai/core";
|
|
8
|
+
|
|
9
|
+
export type Ran = {
|
|
10
|
+
readonly code: number | null;
|
|
11
|
+
readonly stdout: Buffer;
|
|
12
|
+
readonly stderr: string;
|
|
13
|
+
readonly truncated: boolean;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export async function run(
|
|
17
|
+
file: string,
|
|
18
|
+
args: readonly string[],
|
|
19
|
+
options: {
|
|
20
|
+
readonly cwd: string;
|
|
21
|
+
readonly env?: Readonly<Record<string, string>>;
|
|
22
|
+
readonly input?: string | Uint8Array;
|
|
23
|
+
readonly limit?: number;
|
|
24
|
+
/** Kills the command after this long; it then ends with a null code. */
|
|
25
|
+
readonly timeoutMs?: number;
|
|
26
|
+
},
|
|
27
|
+
): Promise<Ran> {
|
|
28
|
+
await processEnvironment();
|
|
29
|
+
const resolved = process.platform === "win32" ? ((await findExecutable(file)) ?? file) : file;
|
|
30
|
+
|
|
31
|
+
return new Promise((resolve, reject) => {
|
|
32
|
+
const child = spawnCommand(resolved, args, {
|
|
33
|
+
cwd: options.cwd,
|
|
34
|
+
env: { ...process.env, ...options.env },
|
|
35
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
36
|
+
windowsHide: true,
|
|
37
|
+
timeout: options.timeoutMs,
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
const stdout: Buffer[] = [];
|
|
41
|
+
const stderr: Buffer[] = [];
|
|
42
|
+
const limit = options.limit ?? Number.POSITIVE_INFINITY;
|
|
43
|
+
let size = 0;
|
|
44
|
+
let truncated = false;
|
|
45
|
+
|
|
46
|
+
child.on("error", (error) =>
|
|
47
|
+
reject(new SdkError("unavailable", `Cannot run ${file}: ${error.message}`)),
|
|
48
|
+
);
|
|
49
|
+
child.stdout?.on("data", (chunk: Buffer) => {
|
|
50
|
+
if (truncated) return;
|
|
51
|
+
size += chunk.byteLength;
|
|
52
|
+
|
|
53
|
+
if (size > limit) {
|
|
54
|
+
truncated = true;
|
|
55
|
+
stdout.push(chunk.subarray(0, chunk.byteLength - (size - limit)));
|
|
56
|
+
child.kill("SIGKILL");
|
|
57
|
+
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
stdout.push(chunk);
|
|
62
|
+
});
|
|
63
|
+
child.stderr?.on("data", (chunk: Buffer) => stderr.push(chunk));
|
|
64
|
+
child.stdin?.on("error", () => undefined);
|
|
65
|
+
child.on("close", (code) =>
|
|
66
|
+
resolve({
|
|
67
|
+
code,
|
|
68
|
+
stdout: Buffer.concat(stdout),
|
|
69
|
+
stderr: Buffer.concat(stderr).toString("utf8"),
|
|
70
|
+
truncated,
|
|
71
|
+
}),
|
|
72
|
+
);
|
|
73
|
+
child.stdin?.end(options.input);
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** Spawns `file` as `spawn` does, except that on Windows a `.cmd` or `.bat` file runs through `cmd.exe` with its arguments escaped, as Node no longer starts batch files without a shell. */
|
|
78
|
+
export function spawnCommand(
|
|
79
|
+
file: string,
|
|
80
|
+
args: readonly string[],
|
|
81
|
+
options: SpawnOptions,
|
|
82
|
+
): ChildProcess {
|
|
83
|
+
if (process.platform !== "win32" || !/\.(cmd|bat)$/i.test(file))
|
|
84
|
+
return spawn(file, args, options);
|
|
85
|
+
const shim = /node_modules[\\/]\.bin[\\/][^\\/]+\.cmd$/i.test(file);
|
|
86
|
+
const line = [escapeCommand(file), ...args.map((arg) => escapeArgument(arg, shim))].join(" ");
|
|
87
|
+
|
|
88
|
+
return spawn(process.env.ComSpec ?? "cmd.exe", ["/d", "/s", "/c", `"${line}"`], {
|
|
89
|
+
...options,
|
|
90
|
+
windowsVerbatimArguments: true,
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** Ends `child` with `signal`; on Windows, which has no signals, it ends the child's whole process tree with `taskkill /T /F`, as `kill` alone would leave what a batch file or a shell started running. */
|
|
95
|
+
export function killTree(child: ChildProcess, signal: NodeJS.Signals): void {
|
|
96
|
+
if (process.platform !== "win32" || child.pid === undefined) {
|
|
97
|
+
child.kill(signal);
|
|
98
|
+
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
spawn("taskkill", ["/pid", String(child.pid), "/T", "/F"], {
|
|
103
|
+
stdio: "ignore",
|
|
104
|
+
windowsHide: true,
|
|
105
|
+
}).on("error", () => child.kill(signal));
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const CMD_META = /([()\][%!^"`<>&|;, *?])/g;
|
|
109
|
+
|
|
110
|
+
function escapeCommand(command: string): string {
|
|
111
|
+
return command.replace(CMD_META, "^$1");
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function escapeArgument(argument: string, twice: boolean): string {
|
|
115
|
+
const quoted = `"${argument.replace(/(\\*)"/g, '$1$1\\"').replace(/(\\*)$/, "$1$1")}"`;
|
|
116
|
+
const escaped = quoted.replace(CMD_META, "^$1");
|
|
117
|
+
|
|
118
|
+
return twice ? escaped.replace(CMD_META, "^$1") : escaped;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export async function findExecutable(
|
|
122
|
+
name: string,
|
|
123
|
+
directories: readonly string[] = [],
|
|
124
|
+
): Promise<string | null> {
|
|
125
|
+
const pathext = (process.env.PATHEXT ?? ".EXE;.CMD;.BAT").split(";").filter((ext) => ext !== "");
|
|
126
|
+
|
|
127
|
+
const extensions =
|
|
128
|
+
process.platform !== "win32" ? [""] : extname(name) === "" ? pathext : ["", ...pathext];
|
|
129
|
+
|
|
130
|
+
if (isAbsolute(name)) {
|
|
131
|
+
for (const extension of extensions)
|
|
132
|
+
if (await executable(`${name}${extension}`)) return `${name}${extension}`;
|
|
133
|
+
|
|
134
|
+
return null;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
await processEnvironment();
|
|
138
|
+
const path = (process.env.PATH ?? "").split(delimiter).filter((entry) => entry !== "");
|
|
139
|
+
|
|
140
|
+
for (const directory of [...directories, ...path])
|
|
141
|
+
for (const extension of extensions) {
|
|
142
|
+
const candidate = join(directory, `${name}${extension}`);
|
|
143
|
+
|
|
144
|
+
if (await executable(candidate)) return candidate;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
return null;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
async function executable(path: string): Promise<boolean> {
|
|
151
|
+
const found = await stat(path).catch(() => null);
|
|
152
|
+
|
|
153
|
+
return (
|
|
154
|
+
found !== null &&
|
|
155
|
+
found.isFile() &&
|
|
156
|
+
(await access(path, constants.X_OK).then(
|
|
157
|
+
() => true,
|
|
158
|
+
() => false,
|
|
159
|
+
))
|
|
160
|
+
);
|
|
161
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import type { Disposer } from "@nyte-ai/schema";
|
|
2
|
+
import { closable } from "@nyte-ai/core";
|
|
3
|
+
|
|
4
|
+
export type FeedSink<T> = {
|
|
5
|
+
push(value: T): void;
|
|
6
|
+
fail(error: unknown): void;
|
|
7
|
+
end(): void;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export function feed<T>(
|
|
11
|
+
start: (sink: FeedSink<T>) => Disposer,
|
|
12
|
+
merge: (waiting: T, next: T) => T = (_waiting, next) => next,
|
|
13
|
+
): AsyncIterable<T> {
|
|
14
|
+
return closable<T>(() => {
|
|
15
|
+
const state: {
|
|
16
|
+
slot: { readonly value: T } | null;
|
|
17
|
+
outcome:
|
|
18
|
+
| { readonly kind: "ended" }
|
|
19
|
+
| { readonly kind: "failed"; readonly error: unknown }
|
|
20
|
+
| null;
|
|
21
|
+
wake: (() => void) | null;
|
|
22
|
+
} = { slot: null, outcome: null, wake: null };
|
|
23
|
+
|
|
24
|
+
const signal = (): void => {
|
|
25
|
+
const resolve = state.wake;
|
|
26
|
+
state.wake = null;
|
|
27
|
+
resolve?.();
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const dispose = start({
|
|
31
|
+
push(value) {
|
|
32
|
+
if (state.outcome !== null) return;
|
|
33
|
+
state.slot = { value: state.slot === null ? value : merge(state.slot.value, value) };
|
|
34
|
+
signal();
|
|
35
|
+
},
|
|
36
|
+
fail(error) {
|
|
37
|
+
state.outcome ??= { kind: "failed", error };
|
|
38
|
+
signal();
|
|
39
|
+
},
|
|
40
|
+
end() {
|
|
41
|
+
state.outcome ??= { kind: "ended" };
|
|
42
|
+
signal();
|
|
43
|
+
},
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
return {
|
|
47
|
+
async pull(): Promise<IteratorResult<T>> {
|
|
48
|
+
for (;;) {
|
|
49
|
+
const held = state.slot;
|
|
50
|
+
|
|
51
|
+
if (held !== null) {
|
|
52
|
+
state.slot = null;
|
|
53
|
+
|
|
54
|
+
return { done: false, value: held.value };
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const outcome = state.outcome;
|
|
58
|
+
|
|
59
|
+
if (outcome?.kind === "failed") throw outcome.error;
|
|
60
|
+
|
|
61
|
+
if (outcome?.kind === "ended") return { done: true, value: undefined };
|
|
62
|
+
await new Promise<void>((resolve) => {
|
|
63
|
+
state.wake = resolve;
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
},
|
|
67
|
+
close: () => dispose(),
|
|
68
|
+
};
|
|
69
|
+
});
|
|
70
|
+
}
|