@nyte-ai/host 0.0.0-stage → 0.0.18
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 +800 -0
- package/dist/chunks/canvas-preview-0cmbkp2j.js +65 -0
- package/dist/chunks/canvas-preview-0nq75h9k.js +7 -0
- package/dist/chunks/canvas-preview-0nrnbt76.js +497 -0
- package/dist/chunks/canvas-preview-1sq7z9jq.js +17 -0
- package/dist/chunks/canvas-preview-1ta3avah.js +56 -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-5hxat7hh.js +239 -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-75db60xy.js +457 -0
- package/dist/chunks/canvas-preview-76fwvd6e.js +272 -0
- package/dist/chunks/canvas-preview-8xgt67ka.js +598 -0
- package/dist/chunks/canvas-preview-98aj5p34.js +176 -0
- package/dist/chunks/canvas-preview-9aqczacs.js +73 -0
- package/dist/chunks/canvas-preview-a3yw0j70.js +72 -0
- package/dist/chunks/canvas-preview-azg56359.js +68 -0
- package/dist/chunks/canvas-preview-d9wwhbtz.js +240 -0
- package/dist/chunks/canvas-preview-ehf3vp31.js +411 -0
- package/dist/chunks/canvas-preview-evh92ywd.js +369 -0
- package/dist/chunks/canvas-preview-ewgspmsd.js +81 -0
- package/dist/chunks/canvas-preview-g3qj3y3k.js +156 -0
- package/dist/chunks/canvas-preview-h8hshnff.js +63 -0
- package/dist/chunks/canvas-preview-j1tnf51w.js +36 -0
- package/dist/chunks/canvas-preview-kdqgrstc.js +194 -0
- package/dist/chunks/canvas-preview-kn7kvbfj.js +12 -0
- package/dist/chunks/canvas-preview-m02v4yy6.js +386 -0
- package/dist/chunks/canvas-preview-mgcx2b2p.js +271 -0
- package/dist/chunks/canvas-preview-rp4x6y1h.js +144 -0
- package/dist/chunks/canvas-preview-rp6fz0w5.js +130 -0
- package/dist/chunks/canvas-preview-s94ebwf5.js +112 -0
- package/dist/chunks/canvas-preview-senpjwep.js +518 -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-vaatfznm.js +189 -0
- package/dist/chunks/canvas-preview-vax9ne4e.js +341 -0
- package/dist/chunks/canvas-preview-wg80rbc2.js +46 -0
- package/dist/chunks/canvas-preview-xn10d4f8.js +608 -0
- package/dist/chunks/canvas-preview-yjz0f7n4.js +50 -0
- package/dist/chunks/canvas-preview-ymgz0937.js +587 -0
- package/dist/chunks/canvas-preview-yrfz2ak6.js +134 -0
- package/dist/chunks/canvas-preview-zaaj99wd.js +244 -0
- package/dist/chunks/canvas-preview-zn2d0k9d.js +133 -0
- package/dist/src/canvas-preview.d.ts +29 -0
- package/dist/src/canvas-preview.js +267 -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/index.d.ts +21 -0
- package/dist/src/index.js +120 -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 +15 -0
- package/dist/src/local/command.js +8 -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 +6 -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 +28 -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 +42 -0
- package/dist/src/open.js +44 -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 +7 -0
- package/dist/src/sqlite.js +10 -0
- package/dist/src/store-owner.d.ts +15 -0
- package/dist/src/store-owner.js +9 -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 +16 -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 +22 -0
- package/dist/src/usage-service.js +12 -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 +25 -0
- package/dist/src/usage.d.ts +72 -0
- package/dist/src/usage.js +13 -0
- package/package.json +132 -3
- package/src/canvas-preview.ts +402 -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/index.ts +21 -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 +109 -0
- package/src/local/feed.ts +70 -0
- package/src/local/files.ts +812 -0
- package/src/local/git-watch.ts +189 -0
- package/src/local/git.ts +914 -0
- package/src/local/github.ts +692 -0
- package/src/local/identity.ts +21 -0
- package/src/local/jobs.ts +587 -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 +776 -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 +105 -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 +469 -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 +628 -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 +528 -0
- package/src/store-owner.ts +98 -0
- package/src/store-rows.ts +110 -0
- package/src/store-usage.ts +344 -0
- package/src/usage-scan.ts +81 -0
- package/src/usage-service.ts +73 -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 +701 -0
|
@@ -0,0 +1,411 @@
|
|
|
1
|
+
// src/local/jobs.ts
|
|
2
|
+
import { spawn } from "node:child_process";
|
|
3
|
+
import { createRequire } from "node:module";
|
|
4
|
+
import { resolve } from "node:path";
|
|
5
|
+
import { asId, SdkError } from "@nyte-ai/schema";
|
|
6
|
+
import {
|
|
7
|
+
processEnvironment,
|
|
8
|
+
closable,
|
|
9
|
+
dev,
|
|
10
|
+
readTraceparent,
|
|
11
|
+
startSpan,
|
|
12
|
+
tracing
|
|
13
|
+
} from "@nyte-ai/core";
|
|
14
|
+
var OUTPUT_LIMIT = 1024 * 1024;
|
|
15
|
+
var PAUSE_BYTES = 256 * 1024;
|
|
16
|
+
var RESUME_BYTES = 64 * 1024;
|
|
17
|
+
var STOP_GRACE_MS = 2000;
|
|
18
|
+
var CLOSE_GRACE_MS = 2000;
|
|
19
|
+
var QUIET_MS = 300;
|
|
20
|
+
var DONE = { done: true, value: undefined };
|
|
21
|
+
var ESC = "\x1B";
|
|
22
|
+
var BEL = "\x07";
|
|
23
|
+
var PROMPT_MARKER = new RegExp(`${ESC}\\]133;([ABCD])`, "g");
|
|
24
|
+
var loaded;
|
|
25
|
+
function localExec2(root, options) {
|
|
26
|
+
const readers = new Set;
|
|
27
|
+
const custom = options?.loadPty;
|
|
28
|
+
const load = custom === undefined ? loadPty : () => ptyFrom(custom);
|
|
29
|
+
options?.signal?.addEventListener("abort", () => {
|
|
30
|
+
for (const close of readers)
|
|
31
|
+
close();
|
|
32
|
+
}, { once: true });
|
|
33
|
+
return async (command, input) => {
|
|
34
|
+
if (input?.signal?.aborted === true)
|
|
35
|
+
throw new SdkError("unavailable", "aborted");
|
|
36
|
+
if (typeof command !== "string" && command.length === 0 && input?.pty === undefined)
|
|
37
|
+
throw new SdkError("invalid", "An empty command runs only in a pty, as the login shell.");
|
|
38
|
+
const cwd = input?.cwd === undefined ? root : resolve(root, input.cwd);
|
|
39
|
+
await processEnvironment();
|
|
40
|
+
const env = { ...process.env, ...input?.env };
|
|
41
|
+
const trace = readTraceparent(input?.env?.TRACEPARENT);
|
|
42
|
+
const spawned = startSpan("pty.spawn", trace, { pty: input?.pty !== undefined });
|
|
43
|
+
const log = tracedOutput(createOutputLog(OUTPUT_LIMIT), trace);
|
|
44
|
+
let settle = () => {
|
|
45
|
+
return;
|
|
46
|
+
};
|
|
47
|
+
const ended = new Promise((done) => {
|
|
48
|
+
settle = done;
|
|
49
|
+
});
|
|
50
|
+
let stopping = false;
|
|
51
|
+
let finished = null;
|
|
52
|
+
const finish = (code) => {
|
|
53
|
+
if (finished !== null)
|
|
54
|
+
return;
|
|
55
|
+
finished = { kind: "exited", code, reason: stopping ? "terminated" : "exited" };
|
|
56
|
+
dev("jobs", "exit", { code, reason: finished.reason });
|
|
57
|
+
log.end(code);
|
|
58
|
+
settle(finished);
|
|
59
|
+
};
|
|
60
|
+
const driver = input?.pty === undefined ? await pipe(command, { cwd, env, protocol: input?.stdio === "protocol" }, log, finish) : await terminal(command, { cwd, env, size: input.pty, shell: options?.shell?.() ?? undefined, load }, log, finish);
|
|
61
|
+
spawned();
|
|
62
|
+
dev("jobs", "start", {
|
|
63
|
+
program: typeof command === "string" ? command.split(/\s/, 1)[0] : command[0] ?? null,
|
|
64
|
+
args: typeof command === "string" ? null : command.length - 1,
|
|
65
|
+
pty: driver.pty
|
|
66
|
+
});
|
|
67
|
+
let stopped = null;
|
|
68
|
+
const stop = () => {
|
|
69
|
+
stopped ??= (async () => {
|
|
70
|
+
if (finished !== null)
|
|
71
|
+
return finished;
|
|
72
|
+
stopping = true;
|
|
73
|
+
driver.kill(driver.pty ? "SIGHUP" : "SIGTERM");
|
|
74
|
+
const timer = setTimeout(() => driver.kill("SIGKILL"), STOP_GRACE_MS);
|
|
75
|
+
try {
|
|
76
|
+
return await ended;
|
|
77
|
+
} finally {
|
|
78
|
+
clearTimeout(timer);
|
|
79
|
+
}
|
|
80
|
+
})();
|
|
81
|
+
return stopped;
|
|
82
|
+
};
|
|
83
|
+
input?.signal?.addEventListener("abort", () => void stop(), { once: true });
|
|
84
|
+
const own = new Set;
|
|
85
|
+
const close = async () => {
|
|
86
|
+
for (const detach of own)
|
|
87
|
+
detach();
|
|
88
|
+
};
|
|
89
|
+
const job = {
|
|
90
|
+
id: asId(crypto.randomUUID()),
|
|
91
|
+
key: null,
|
|
92
|
+
write: (bytes) => driver.write(bytes),
|
|
93
|
+
output: () => closable(() => {
|
|
94
|
+
const reader = log.reader();
|
|
95
|
+
const detach = () => {
|
|
96
|
+
own.delete(detach);
|
|
97
|
+
readers.delete(detach);
|
|
98
|
+
reader.close();
|
|
99
|
+
};
|
|
100
|
+
own.add(detach);
|
|
101
|
+
readers.add(detach);
|
|
102
|
+
return { pull: () => reader.next(), close: detach };
|
|
103
|
+
}),
|
|
104
|
+
async resize(size) {
|
|
105
|
+
if (!driver.pty)
|
|
106
|
+
return { kind: "not-pty" };
|
|
107
|
+
return finished === null && driver.resize(size) ? { kind: "resized" } : { kind: "missing" };
|
|
108
|
+
},
|
|
109
|
+
idle: async () => finished === null && driver.idle(),
|
|
110
|
+
stop: async () => ({ kind: "settled", outcome: await stop() }),
|
|
111
|
+
settled: (request) => waitEnd(ended, request),
|
|
112
|
+
close,
|
|
113
|
+
[Symbol.asyncDispose]: close
|
|
114
|
+
};
|
|
115
|
+
return job;
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
async function pipe(command, options, log, finish) {
|
|
119
|
+
const group = process.platform !== "win32";
|
|
120
|
+
const [file, ...args] = typeof command === "string" ? [command] : command;
|
|
121
|
+
const settings = {
|
|
122
|
+
cwd: options.cwd,
|
|
123
|
+
env: options.env,
|
|
124
|
+
detached: group,
|
|
125
|
+
stdio: "pipe",
|
|
126
|
+
windowsHide: true
|
|
127
|
+
};
|
|
128
|
+
const child = typeof command === "string" ? spawn(command, { ...settings, shell: true }) : spawn(file ?? "", args, settings);
|
|
129
|
+
await new Promise((started, failed) => {
|
|
130
|
+
child.once("spawn", started);
|
|
131
|
+
child.once("error", (error) => failed(new SdkError("unavailable", `Cannot start ${file ?? ""}: ${error.message}`)));
|
|
132
|
+
});
|
|
133
|
+
child.stdout?.on("data", (chunk) => log.push(chunk));
|
|
134
|
+
child.stderr?.on("data", (chunk) => log.push(chunk, options.protocol ? "stderr" : undefined));
|
|
135
|
+
if (options.protocol)
|
|
136
|
+
log.throttle({
|
|
137
|
+
pause: () => {
|
|
138
|
+
child.stdout?.pause();
|
|
139
|
+
child.stderr?.pause();
|
|
140
|
+
},
|
|
141
|
+
resume: () => {
|
|
142
|
+
child.stdout?.resume();
|
|
143
|
+
child.stderr?.resume();
|
|
144
|
+
}
|
|
145
|
+
});
|
|
146
|
+
child.stdin?.on("error", () => {
|
|
147
|
+
return;
|
|
148
|
+
});
|
|
149
|
+
child.on("close", (code) => finish(code));
|
|
150
|
+
child.on("exit", (code) => {
|
|
151
|
+
setTimeout(() => {
|
|
152
|
+
child.stdout?.destroy();
|
|
153
|
+
child.stderr?.destroy();
|
|
154
|
+
finish(code);
|
|
155
|
+
}, CLOSE_GRACE_MS).unref();
|
|
156
|
+
});
|
|
157
|
+
return {
|
|
158
|
+
pty: false,
|
|
159
|
+
async write(bytes) {
|
|
160
|
+
const stdin = child.stdin;
|
|
161
|
+
if (stdin === null || !stdin.writable)
|
|
162
|
+
throw new SdkError("unavailable", "The process no longer reads its input.");
|
|
163
|
+
await new Promise((written) => stdin.write(bytes, () => written()));
|
|
164
|
+
},
|
|
165
|
+
resize: () => false,
|
|
166
|
+
idle: () => false,
|
|
167
|
+
kill(signal) {
|
|
168
|
+
try {
|
|
169
|
+
if (group && child.pid !== undefined)
|
|
170
|
+
process.kill(-child.pid, signal);
|
|
171
|
+
else
|
|
172
|
+
child.kill(signal);
|
|
173
|
+
} catch {
|
|
174
|
+
child.kill(signal);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
function terminalEnv(env) {
|
|
180
|
+
const { ELECTRON_RUN_AS_NODE: _electron, NODE_OPTIONS: _node, ...rest } = env;
|
|
181
|
+
return { ...rest, TERM: "xterm-256color", COLORTERM: "truecolor", TERM_PROGRAM: "Nyte" };
|
|
182
|
+
}
|
|
183
|
+
async function terminal(command, options, log, finish) {
|
|
184
|
+
const pty = options.load();
|
|
185
|
+
if (pty === null)
|
|
186
|
+
throw new SdkError("unavailable", "Terminals need @lydell/node-pty, which does not load on this platform.");
|
|
187
|
+
const windows = process.platform === "win32";
|
|
188
|
+
const shell = options.shell ?? (windows ? process.env.COMSPEC ?? "cmd.exe" : process.env.SHELL ?? "/bin/sh");
|
|
189
|
+
const [file, ...args] = typeof command === "string" ? windows ? [shell, "/d", "/s", "/c", command] : ["/bin/sh", "-c", command] : command.length === 0 ? windows ? [shell] : [shell, "-l"] : command;
|
|
190
|
+
let term;
|
|
191
|
+
try {
|
|
192
|
+
term = pty.spawn(file ?? shell, args, {
|
|
193
|
+
name: "xterm-256color",
|
|
194
|
+
cols: options.size.columns,
|
|
195
|
+
rows: options.size.rows,
|
|
196
|
+
cwd: options.cwd,
|
|
197
|
+
env: terminalEnv(options.env)
|
|
198
|
+
});
|
|
199
|
+
} catch (error) {
|
|
200
|
+
throw new SdkError("unavailable", `Cannot start ${file ?? shell} in a pty: ${error instanceof Error ? error.message : String(error)}`);
|
|
201
|
+
}
|
|
202
|
+
const encoder = new TextEncoder;
|
|
203
|
+
let marker = null;
|
|
204
|
+
let line = "";
|
|
205
|
+
let quietSince = 0;
|
|
206
|
+
term.onData((text) => {
|
|
207
|
+
log.push(encoder.encode(text));
|
|
208
|
+
quietSince = Date.now();
|
|
209
|
+
marker = lastMarker(text) ?? marker;
|
|
210
|
+
const plain = withoutEscapes(text);
|
|
211
|
+
const broken = plain.lastIndexOf(`
|
|
212
|
+
`);
|
|
213
|
+
line = (broken < 0 ? line + plain : plain.slice(broken + 1)).slice(-1024);
|
|
214
|
+
const returned = line.replace(/\r+$/, "").lastIndexOf("\r");
|
|
215
|
+
if (returned >= 0)
|
|
216
|
+
line = line.slice(returned + 1);
|
|
217
|
+
});
|
|
218
|
+
term.onExit(({ exitCode, signal }) => finish(signal ? null : exitCode));
|
|
219
|
+
log.throttle({ pause: () => term.pause(), resume: () => term.resume() });
|
|
220
|
+
return {
|
|
221
|
+
pty: true,
|
|
222
|
+
async write(bytes) {
|
|
223
|
+
term.write(Buffer.from(bytes));
|
|
224
|
+
},
|
|
225
|
+
resize(size) {
|
|
226
|
+
term.resize(size.columns, size.rows);
|
|
227
|
+
return true;
|
|
228
|
+
},
|
|
229
|
+
idle() {
|
|
230
|
+
if (marker !== null)
|
|
231
|
+
return marker !== "C";
|
|
232
|
+
return quietSince > 0 && Date.now() - quietSince >= QUIET_MS && line.trim() !== "";
|
|
233
|
+
},
|
|
234
|
+
kill(signal) {
|
|
235
|
+
try {
|
|
236
|
+
term.kill(windows ? undefined : signal);
|
|
237
|
+
} catch {}
|
|
238
|
+
}
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
function lastMarker(text) {
|
|
242
|
+
return Array.from(text.matchAll(PROMPT_MARKER), (match) => match[1]).at(-1) ?? null;
|
|
243
|
+
}
|
|
244
|
+
function withoutEscapes(text) {
|
|
245
|
+
let plain = "";
|
|
246
|
+
let at = 0;
|
|
247
|
+
while (at < text.length) {
|
|
248
|
+
const char = text[at] ?? "";
|
|
249
|
+
if (char !== ESC) {
|
|
250
|
+
plain += char;
|
|
251
|
+
at += 1;
|
|
252
|
+
continue;
|
|
253
|
+
}
|
|
254
|
+
const kind = text[at + 1] ?? "";
|
|
255
|
+
at += 2;
|
|
256
|
+
if (kind === "[") {
|
|
257
|
+
while (at < text.length && !/[@-~]/.test(text[at] ?? ""))
|
|
258
|
+
at += 1;
|
|
259
|
+
at += 1;
|
|
260
|
+
} else if (kind === "]") {
|
|
261
|
+
while (at < text.length && text[at] !== BEL && !(text[at] === ESC && text[at + 1] === "\\"))
|
|
262
|
+
at += 1;
|
|
263
|
+
at += text[at] === BEL ? 1 : 2;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
return plain;
|
|
267
|
+
}
|
|
268
|
+
function loadPty() {
|
|
269
|
+
if (loaded === undefined)
|
|
270
|
+
loaded = ptyFrom(() => createRequire(import.meta.url)("@lydell/node-pty"));
|
|
271
|
+
return loaded;
|
|
272
|
+
}
|
|
273
|
+
function ptyFrom(load) {
|
|
274
|
+
try {
|
|
275
|
+
const required = load();
|
|
276
|
+
return fitsPtyModule(required) ? required : null;
|
|
277
|
+
} catch {
|
|
278
|
+
return null;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
function fitsPtyModule(value) {
|
|
282
|
+
return typeof value === "object" && value !== null && "spawn" in value && typeof value.spawn === "function";
|
|
283
|
+
}
|
|
284
|
+
function waitEnd(ended, request) {
|
|
285
|
+
const signal = request?.signal;
|
|
286
|
+
if (signal === undefined)
|
|
287
|
+
return ended;
|
|
288
|
+
if (signal.aborted)
|
|
289
|
+
return Promise.reject(new SdkError("unavailable", "aborted"));
|
|
290
|
+
return new Promise((settled, failed) => {
|
|
291
|
+
const abort = () => failed(new SdkError("unavailable", "aborted"));
|
|
292
|
+
signal.addEventListener("abort", abort, { once: true });
|
|
293
|
+
ended.then((end) => {
|
|
294
|
+
signal.removeEventListener("abort", abort);
|
|
295
|
+
settled(end);
|
|
296
|
+
}, failed);
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
function tracedOutput(log, trace) {
|
|
300
|
+
if (!tracing.enabled)
|
|
301
|
+
return log;
|
|
302
|
+
let ended = null;
|
|
303
|
+
let bytes = 0;
|
|
304
|
+
return {
|
|
305
|
+
...log,
|
|
306
|
+
push(chunk, stream) {
|
|
307
|
+
ended ??= startSpan("pty.output", trace);
|
|
308
|
+
bytes += chunk.byteLength;
|
|
309
|
+
log.push(chunk, stream);
|
|
310
|
+
},
|
|
311
|
+
end(code) {
|
|
312
|
+
ended?.({ bytes, code });
|
|
313
|
+
log.end(code);
|
|
314
|
+
}
|
|
315
|
+
};
|
|
316
|
+
}
|
|
317
|
+
function createOutputLog(limit) {
|
|
318
|
+
const state = { chunks: [], first: 0, size: 0, pushed: 0, exit: null };
|
|
319
|
+
const waiters = new Set;
|
|
320
|
+
const positions = new Set;
|
|
321
|
+
let source = null;
|
|
322
|
+
let paused = false;
|
|
323
|
+
const behind = () => Math.max(0, ...[...positions].map((position) => state.pushed - position.read));
|
|
324
|
+
const pace = () => {
|
|
325
|
+
if (source === null)
|
|
326
|
+
return;
|
|
327
|
+
const lag = behind();
|
|
328
|
+
if (!paused && lag > PAUSE_BYTES) {
|
|
329
|
+
paused = true;
|
|
330
|
+
source.pause();
|
|
331
|
+
} else if (paused && lag < RESUME_BYTES) {
|
|
332
|
+
paused = false;
|
|
333
|
+
source.resume();
|
|
334
|
+
}
|
|
335
|
+
};
|
|
336
|
+
const wake = () => {
|
|
337
|
+
const pending = [...waiters];
|
|
338
|
+
waiters.clear();
|
|
339
|
+
for (const resolve of pending)
|
|
340
|
+
resolve();
|
|
341
|
+
};
|
|
342
|
+
return {
|
|
343
|
+
throttle(next) {
|
|
344
|
+
source = next;
|
|
345
|
+
},
|
|
346
|
+
push(bytes, stream) {
|
|
347
|
+
state.chunks.push(stream === undefined ? { kind: "bytes", data: bytes } : { kind: "bytes", data: bytes, stream });
|
|
348
|
+
state.size += bytes.byteLength;
|
|
349
|
+
state.pushed += bytes.byteLength;
|
|
350
|
+
while (state.size > limit && state.chunks.length > 1) {
|
|
351
|
+
state.size -= state.chunks.shift()?.data.byteLength ?? 0;
|
|
352
|
+
state.first += 1;
|
|
353
|
+
}
|
|
354
|
+
pace();
|
|
355
|
+
wake();
|
|
356
|
+
},
|
|
357
|
+
end(code) {
|
|
358
|
+
state.exit ??= { code };
|
|
359
|
+
wake();
|
|
360
|
+
},
|
|
361
|
+
reader() {
|
|
362
|
+
let cursor = 0;
|
|
363
|
+
let done = false;
|
|
364
|
+
let waiting = null;
|
|
365
|
+
const position = { read: 0 };
|
|
366
|
+
positions.add(position);
|
|
367
|
+
return {
|
|
368
|
+
async next() {
|
|
369
|
+
for (;; ) {
|
|
370
|
+
if (done)
|
|
371
|
+
return DONE;
|
|
372
|
+
if (cursor < state.first) {
|
|
373
|
+
cursor = state.first;
|
|
374
|
+
position.read = state.pushed - state.size;
|
|
375
|
+
return { done: false, value: { kind: "gap" } };
|
|
376
|
+
}
|
|
377
|
+
const chunk = state.chunks[cursor - state.first];
|
|
378
|
+
if (chunk !== undefined) {
|
|
379
|
+
cursor += 1;
|
|
380
|
+
position.read += chunk.data.byteLength;
|
|
381
|
+
pace();
|
|
382
|
+
return { done: false, value: chunk };
|
|
383
|
+
}
|
|
384
|
+
if (state.exit !== null) {
|
|
385
|
+
done = true;
|
|
386
|
+
return { done: false, value: { kind: "exit", code: state.exit.code } };
|
|
387
|
+
}
|
|
388
|
+
await new Promise((woken) => {
|
|
389
|
+
waiting = woken;
|
|
390
|
+
waiters.add(woken);
|
|
391
|
+
});
|
|
392
|
+
waiting = null;
|
|
393
|
+
}
|
|
394
|
+
},
|
|
395
|
+
close() {
|
|
396
|
+
done = true;
|
|
397
|
+
positions.delete(position);
|
|
398
|
+
pace();
|
|
399
|
+
const resolve = waiting;
|
|
400
|
+
waiting = null;
|
|
401
|
+
if (resolve !== null) {
|
|
402
|
+
waiters.delete(resolve);
|
|
403
|
+
resolve();
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
};
|
|
407
|
+
}
|
|
408
|
+
};
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
export { localExec2 };
|