tmux-ide 2.9.20 → 2.9.21
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/bin/cli.js +14684 -3865
- package/package.json +3 -2
- package/packages/contracts/src/command-catalog.ts +116 -0
- package/packages/contracts/src/commands.ts +5 -4
- package/packages/contracts/src/daemon-events.ts +2 -1
- package/packages/contracts/src/index.ts +2 -0
- package/packages/contracts/src/plain-text.ts +61 -0
- package/packages/daemon/dist/command-center/command-catalog-routes.js +78 -0
- package/packages/daemon/dist/command-center/plugin-routes.js +84 -0
- package/packages/daemon/dist/command-center/server.js +13 -0
- package/packages/daemon/dist/lib/cli-action-bridge.js +15 -0
- package/packages/daemon/dist/lib/daemon-embed.js +24 -1
- package/packages/daemon/dist/lib/headless-daemon.js +1 -0
- package/packages/daemon/dist/lib/worktree.js +18 -0
- package/packages/daemon/dist/native/TmuxIdeNotifier.app/Contents/Info.plist +2 -2
- package/packages/daemon/dist/native/TmuxIdeNotifier.app/Contents/MacOS/tmux-ide-notifier +0 -0
- package/packages/daemon/dist/native/TmuxIdeNotifier.app/Contents/Resources/Assets.car +0 -0
- package/packages/daemon/dist/native/TmuxIdeNotifier.app/Contents/_CodeSignature/CodeResources +2 -2
- package/packages/daemon/dist/plugins/action-catalog.js +129 -0
- package/packages/daemon/dist/plugins/action-cli.js +212 -0
- package/packages/daemon/dist/plugins/api-table.js +303 -0
- package/packages/daemon/dist/plugins/cli.js +556 -0
- package/packages/daemon/dist/plugins/consent.js +107 -0
- package/packages/daemon/dist/plugins/daemon-plugins.js +132 -0
- package/packages/daemon/dist/plugins/discovery.js +142 -0
- package/packages/daemon/dist/plugins/examples/worktree-tasks.js +115 -0
- package/packages/daemon/dist/plugins/exec-sandbox.js +202 -0
- package/packages/daemon/dist/plugins/host/actions-prelude.js +279 -0
- package/packages/daemon/dist/plugins/host/entry.js +4 -0
- package/packages/daemon/dist/plugins/host/host-api-prelude.js +232 -0
- package/packages/daemon/dist/plugins/host/lua-state.js +493 -0
- package/packages/daemon/dist/plugins/host/main.js +322 -0
- package/packages/daemon/dist/plugins/host/sandbox.js +299 -0
- package/packages/daemon/dist/plugins/host-calls.js +279 -0
- package/packages/daemon/dist/plugins/manifest.js +69 -0
- package/packages/daemon/dist/plugins/plugin-exec.js +261 -0
- package/packages/daemon/dist/plugins/plugin-log.js +54 -0
- package/packages/daemon/dist/plugins/plugin-store.js +74 -0
- package/packages/daemon/dist/plugins/protocol.js +252 -0
- package/packages/daemon/dist/plugins/safe-files.js +97 -0
- package/packages/daemon/dist/plugins/supervisor.js +790 -0
- package/packages/daemon/dist/plugins/worktree-calls.js +154 -0
- package/packages/daemon/dist/tui/mirror/runtime/application-home-agents-owner.js +27 -0
- package/packages/daemon/dist/tui/mirror/runtime/application-home-experience.js +2 -0
- package/packages/daemon/dist/tui/mirror/runtime/application-palette-command-owner.js +30 -2
- package/packages/daemon/dist/tui/mirror/runtime/application-plugin-commands.js +152 -0
- package/packages/daemon/dist/tui/mirror/runtime/application-plugin-form.js +251 -0
- package/packages/daemon/dist/tui/mirror/workspace/application-command-description.js +14 -0
- package/packages/daemon/dist/tui/mirror/workspace/palette-list-layout.js +5 -0
- package/packages/daemon/src/command-center/command-catalog-routes.ts +116 -0
- package/packages/daemon/src/command-center/plugin-routes.ts +110 -0
- package/packages/daemon/src/command-center/server.ts +15 -0
- package/packages/daemon/src/lib/cli-action-bridge.ts +19 -0
- package/packages/daemon/src/lib/daemon-embed.ts +42 -0
- package/packages/daemon/src/lib/headless-daemon.ts +1 -0
- package/packages/daemon/src/lib/worktree.ts +16 -0
- package/packages/daemon/src/plugins/action-catalog.ts +141 -0
- package/packages/daemon/src/plugins/action-cli.ts +248 -0
- package/packages/daemon/src/plugins/api-table.ts +346 -0
- package/packages/daemon/src/plugins/cli.ts +630 -0
- package/packages/daemon/src/plugins/consent.ts +125 -0
- package/packages/daemon/src/plugins/daemon-plugins.ts +182 -0
- package/packages/daemon/src/plugins/discovery.ts +176 -0
- package/packages/daemon/src/plugins/examples/worktree-tasks.ts +120 -0
- package/packages/daemon/src/plugins/exec-sandbox.ts +264 -0
- package/packages/daemon/src/plugins/host/actions-prelude.ts +280 -0
- package/packages/daemon/src/plugins/host/entry.ts +5 -0
- package/packages/daemon/src/plugins/host/host-api-prelude.ts +233 -0
- package/packages/daemon/src/plugins/host/lua-state.ts +557 -0
- package/packages/daemon/src/plugins/host/main.ts +359 -0
- package/packages/daemon/src/plugins/host/sandbox.ts +301 -0
- package/packages/daemon/src/plugins/host-calls.ts +343 -0
- package/packages/daemon/src/plugins/manifest.ts +74 -0
- package/packages/daemon/src/plugins/plugin-exec.ts +326 -0
- package/packages/daemon/src/plugins/plugin-log.ts +63 -0
- package/packages/daemon/src/plugins/plugin-store.ts +80 -0
- package/packages/daemon/src/plugins/protocol.ts +275 -0
- package/packages/daemon/src/plugins/safe-files.ts +105 -0
- package/packages/daemon/src/plugins/supervisor.ts +954 -0
- package/packages/daemon/src/plugins/worktree-calls.ts +183 -0
- package/packages/daemon/src/tui/mirror/runtime/application-fleet-switcher.tsx +3 -1
- package/packages/daemon/src/tui/mirror/runtime/application-home-agents-owner.ts +32 -0
- package/packages/daemon/src/tui/mirror/runtime/application-home-experience.ts +2 -0
- package/packages/daemon/src/tui/mirror/runtime/application-palette-command-owner.ts +42 -2
- package/packages/daemon/src/tui/mirror/runtime/application-palette-preview.tsx +7 -1
- package/packages/daemon/src/tui/mirror/runtime/application-plugin-commands.ts +229 -0
- package/packages/daemon/src/tui/mirror/runtime/application-plugin-form.ts +312 -0
- package/packages/daemon/src/tui/mirror/workspace/application-command-description.ts +44 -2
- package/packages/daemon/src/tui/mirror/workspace/palette-list-layout.ts +4 -0
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `ide.store`: a small JSON key/value store per plugin, kept in
|
|
3
|
+
* `<state>/plugin-store/<plugin>.json`. That is outside the plugin's data
|
|
4
|
+
* directory on purpose: programs a plugin runs can write there, and the
|
|
5
|
+
* daemon must never read or write through anything they planted.
|
|
6
|
+
*
|
|
7
|
+
* Reads refuse symlinks, non-regular files and files over the cap; writes
|
|
8
|
+
* go to a fresh randomly named file and are renamed into place.
|
|
9
|
+
*/
|
|
10
|
+
import { join } from "node:path";
|
|
11
|
+
import { toPlainJson } from "./host/lua-state.js";
|
|
12
|
+
import { readRegularFile, UnsafeFileError, writeFileAtomic } from "./safe-files.js";
|
|
13
|
+
export const STORE_MAX_BYTES = 1024 * 1024;
|
|
14
|
+
export const STORE_MAX_KEYS = 1000;
|
|
15
|
+
export class PluginStoreError extends Error {
|
|
16
|
+
}
|
|
17
|
+
/** Where `plugin`'s store lives. */
|
|
18
|
+
export function pluginStorePath(stateHome, plugin) {
|
|
19
|
+
return join(stateHome, "plugin-store", `${plugin}.json`);
|
|
20
|
+
}
|
|
21
|
+
export class PluginStore {
|
|
22
|
+
#path;
|
|
23
|
+
constructor(path) {
|
|
24
|
+
this.#path = path;
|
|
25
|
+
}
|
|
26
|
+
#read() {
|
|
27
|
+
let text;
|
|
28
|
+
try {
|
|
29
|
+
text = readRegularFile(this.#path, STORE_MAX_BYTES);
|
|
30
|
+
}
|
|
31
|
+
catch (error) {
|
|
32
|
+
if (error instanceof UnsafeFileError)
|
|
33
|
+
throw new PluginStoreError(`E_FAILED: the store file is unusable (${error.message})`);
|
|
34
|
+
throw error;
|
|
35
|
+
}
|
|
36
|
+
if (text === null)
|
|
37
|
+
return {};
|
|
38
|
+
try {
|
|
39
|
+
const parsed = JSON.parse(text);
|
|
40
|
+
return parsed && typeof parsed === "object" && !Array.isArray(parsed)
|
|
41
|
+
? parsed
|
|
42
|
+
: {};
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
return {};
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
get(key) {
|
|
49
|
+
const data = this.#read();
|
|
50
|
+
return Object.hasOwn(data, key) ? data[key] : null;
|
|
51
|
+
}
|
|
52
|
+
keys() {
|
|
53
|
+
return Object.keys(this.#read()).sort();
|
|
54
|
+
}
|
|
55
|
+
/** Set `key`; `null` or `undefined` deletes it. */
|
|
56
|
+
set(key, value) {
|
|
57
|
+
const data = this.#read();
|
|
58
|
+
if (value === null || value === undefined) {
|
|
59
|
+
delete data[key];
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
const plain = toPlainJson(value);
|
|
63
|
+
if (!plain.ok)
|
|
64
|
+
throw new PluginStoreError(plain.error.replace("a host function returned", "the store got"));
|
|
65
|
+
data[key] = plain.value;
|
|
66
|
+
if (Object.keys(data).length > STORE_MAX_KEYS)
|
|
67
|
+
throw new PluginStoreError(`E_LIMIT: the store holds at most ${STORE_MAX_KEYS} keys`);
|
|
68
|
+
}
|
|
69
|
+
const text = JSON.stringify(data);
|
|
70
|
+
if (Buffer.byteLength(text, "utf8") > STORE_MAX_BYTES)
|
|
71
|
+
throw new PluginStoreError("E_LIMIT: the store may hold at most 1 MiB");
|
|
72
|
+
writeFileAtomic(this.#path, text);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The daemon ↔ plugin host wire.
|
|
3
|
+
*
|
|
4
|
+
* The daemon writes newline-delimited JSON to the host's stdin. The host
|
|
5
|
+
* writes newline-delimited JSON to file descriptor 3 with synchronous writes,
|
|
6
|
+
* so a "busy" marker is on the wire before a Lua slice starts: if that slice
|
|
7
|
+
* never returns, the daemon still knows which plugin to blame. Stdout and
|
|
8
|
+
* stderr stay free for anything the runtime prints on its own.
|
|
9
|
+
*/
|
|
10
|
+
import { z } from "zod";
|
|
11
|
+
/** Version of the Lua API exposed as `ide.api`. Minor versions only add. */
|
|
12
|
+
export const PLUGIN_API_VERSION = "1.0";
|
|
13
|
+
/** Bumped when the daemon/host messages change shape. */
|
|
14
|
+
export const PLUGIN_HOST_PROTOCOL_VERSION = 1;
|
|
15
|
+
export const PLUGIN_NAME_PATTERN = /^[a-z][a-z0-9-]{0,62}$/u;
|
|
16
|
+
export const PluginNameSchemaZ = z.string().regex(PLUGIN_NAME_PATTERN);
|
|
17
|
+
/** The user's `init.lua` runs as this reserved plugin name. */
|
|
18
|
+
export const INIT_PLUGIN_NAME = "init";
|
|
19
|
+
/**
|
|
20
|
+
* Names the host and daemon use for their own work. No plugin directory may
|
|
21
|
+
* take one: `run` labels `tmux-ide lua run`, `host` the host's own log lines.
|
|
22
|
+
*/
|
|
23
|
+
/** The name the host gives the temporary state behind `tmux-ide lua run`. */
|
|
24
|
+
export const RUN_STATE_NAME = "run";
|
|
25
|
+
export const RESERVED_PLUGIN_NAMES = new Set([
|
|
26
|
+
INIT_PLUGIN_NAME,
|
|
27
|
+
RUN_STATE_NAME,
|
|
28
|
+
"host",
|
|
29
|
+
"daemon",
|
|
30
|
+
"ide",
|
|
31
|
+
"plugin",
|
|
32
|
+
"plugins",
|
|
33
|
+
"tmux-ide",
|
|
34
|
+
]);
|
|
35
|
+
export const PluginLimitsSchemaZ = z
|
|
36
|
+
.object({
|
|
37
|
+
/** Lua run time allowed per callback, excluding time spent awaiting the host. */
|
|
38
|
+
callbackBudgetMs: z.number().int().min(10).max(600_000),
|
|
39
|
+
/** Bytes a plugin's Lua state may allocate. */
|
|
40
|
+
memoryBytes: z
|
|
41
|
+
.number()
|
|
42
|
+
.int()
|
|
43
|
+
.min(1024 * 1024)
|
|
44
|
+
.max(1024 * 1024 * 1024),
|
|
45
|
+
/**
|
|
46
|
+
* Wall-clock limit for one load or callback, including time spent waiting
|
|
47
|
+
* for the host. Stops a host call that never settles.
|
|
48
|
+
*/
|
|
49
|
+
wallClockMs: z.number().int().min(100).max(3_600_000),
|
|
50
|
+
/** Log lines a plugin may emit per second before lines are dropped. */
|
|
51
|
+
logLinesPerSecond: z.number().int().min(1).max(10_000),
|
|
52
|
+
})
|
|
53
|
+
.strict();
|
|
54
|
+
export const DEFAULT_PLUGIN_LIMITS = Object.freeze({
|
|
55
|
+
callbackBudgetMs: 250,
|
|
56
|
+
memoryBytes: 64 * 1024 * 1024,
|
|
57
|
+
wallClockMs: 30_000,
|
|
58
|
+
logLinesPerSecond: 100,
|
|
59
|
+
});
|
|
60
|
+
export const PluginSpecSchemaZ = z
|
|
61
|
+
.object({
|
|
62
|
+
name: z.union([PluginNameSchemaZ, z.literal(INIT_PLUGIN_NAME)]),
|
|
63
|
+
kind: z.enum(["init", "plugin"]),
|
|
64
|
+
/** Consented content hash; the host re-hashes the directory and refuses a mismatch. */
|
|
65
|
+
contentHash: z
|
|
66
|
+
.string()
|
|
67
|
+
.regex(/^sha256:[0-9a-f]{64}$/u)
|
|
68
|
+
.optional(),
|
|
69
|
+
/** Absolute path of the entry file. */
|
|
70
|
+
entry: z.string().min(1),
|
|
71
|
+
/** Absolute directories `ide.include` may read from, in search order. */
|
|
72
|
+
includeRoots: z.array(z.string().min(1)).max(8),
|
|
73
|
+
/** Absolute directories `require` may read from, in search order. */
|
|
74
|
+
moduleRoots: z.array(z.string().min(1)).max(8),
|
|
75
|
+
settings: z.record(z.string(), z.json()),
|
|
76
|
+
/** What the owner consented to; init.lua gets everything. */
|
|
77
|
+
capabilities: z
|
|
78
|
+
.object({
|
|
79
|
+
exec: z.union([z.array(z.string()).max(32), z.literal("any")]),
|
|
80
|
+
shell: z.boolean(),
|
|
81
|
+
/** `panes.send`: type into panes and run programs as if typed there. */
|
|
82
|
+
panes: z.boolean(),
|
|
83
|
+
/** Create, open and remove git worktrees and their sessions. */
|
|
84
|
+
worktree: z.boolean(),
|
|
85
|
+
})
|
|
86
|
+
.strict(),
|
|
87
|
+
})
|
|
88
|
+
.strict();
|
|
89
|
+
/**
|
|
90
|
+
* Host functions the daemon serves for plugins (host-calls.ts). Each is
|
|
91
|
+
* exposed to the preludes as `__ide_internal.host[name]`, never directly to
|
|
92
|
+
* plugin code.
|
|
93
|
+
*/
|
|
94
|
+
export const HOST_CALL_METHODS = Object.freeze([
|
|
95
|
+
"_exec",
|
|
96
|
+
"_panes",
|
|
97
|
+
"_pane_read",
|
|
98
|
+
"_pane_send",
|
|
99
|
+
"_action",
|
|
100
|
+
"_notify",
|
|
101
|
+
"_store_get",
|
|
102
|
+
"_store_set",
|
|
103
|
+
"_store_keys",
|
|
104
|
+
"_worktree",
|
|
105
|
+
]);
|
|
106
|
+
// ---------------------------------------------------------------- daemon → host
|
|
107
|
+
export const HostCommandSchemaZ = z.discriminatedUnion("type", [
|
|
108
|
+
z
|
|
109
|
+
.object({
|
|
110
|
+
type: z.literal("configure"),
|
|
111
|
+
protocolVersion: z.literal(PLUGIN_HOST_PROTOCOL_VERSION),
|
|
112
|
+
productVersion: z.string(),
|
|
113
|
+
limits: PluginLimitsSchemaZ,
|
|
114
|
+
})
|
|
115
|
+
.strict(),
|
|
116
|
+
z.object({ type: z.literal("load"), plugin: PluginSpecSchemaZ }).strict(),
|
|
117
|
+
z.object({ type: z.literal("unload"), name: z.string() }).strict(),
|
|
118
|
+
z
|
|
119
|
+
.object({
|
|
120
|
+
type: z.literal("run"),
|
|
121
|
+
id: z.string().min(1).max(64),
|
|
122
|
+
code: z.string().max(1024 * 1024),
|
|
123
|
+
chunkName: z.string().min(1).max(200),
|
|
124
|
+
budgetMs: z.number().int().min(10).max(600_000),
|
|
125
|
+
})
|
|
126
|
+
.strict(),
|
|
127
|
+
z
|
|
128
|
+
.object({
|
|
129
|
+
type: z.literal("invoke"),
|
|
130
|
+
id: z.string().min(1).max(64),
|
|
131
|
+
plugin: z.string().min(1).max(64),
|
|
132
|
+
action: z.string().min(1).max(100),
|
|
133
|
+
args: z.record(z.string(), z.json()),
|
|
134
|
+
context: z.record(z.string(), z.json()),
|
|
135
|
+
})
|
|
136
|
+
.strict(),
|
|
137
|
+
z
|
|
138
|
+
.object({
|
|
139
|
+
type: z.literal("param-options"),
|
|
140
|
+
id: z.string().min(1).max(64),
|
|
141
|
+
plugin: z.string().min(1).max(64),
|
|
142
|
+
action: z.string().min(1).max(100),
|
|
143
|
+
param: z.string().min(1).max(64),
|
|
144
|
+
context: z.record(z.string(), z.json()),
|
|
145
|
+
})
|
|
146
|
+
.strict(),
|
|
147
|
+
z
|
|
148
|
+
.object({
|
|
149
|
+
type: z.literal("call-result"),
|
|
150
|
+
id: z.string().min(1).max(64),
|
|
151
|
+
ok: z.boolean(),
|
|
152
|
+
/** JSON text of the result. */
|
|
153
|
+
value: z
|
|
154
|
+
.string()
|
|
155
|
+
.max(8 * 1024 * 1024)
|
|
156
|
+
.optional(),
|
|
157
|
+
error: z
|
|
158
|
+
.string()
|
|
159
|
+
.max(16 * 1024)
|
|
160
|
+
.optional(),
|
|
161
|
+
})
|
|
162
|
+
.strict(),
|
|
163
|
+
z.object({ type: z.literal("shutdown") }).strict(),
|
|
164
|
+
]);
|
|
165
|
+
// ---------------------------------------------------------------- host → daemon
|
|
166
|
+
export const LogLevelSchemaZ = z.enum(["debug", "info", "warn", "error"]);
|
|
167
|
+
export const TripKindSchemaZ = z.enum(["timeout", "memory", "abort"]);
|
|
168
|
+
export const HostEventSchemaZ = z.discriminatedUnion("type", [
|
|
169
|
+
z
|
|
170
|
+
.object({
|
|
171
|
+
type: z.literal("hello"),
|
|
172
|
+
protocolVersion: z.literal(PLUGIN_HOST_PROTOCOL_VERSION),
|
|
173
|
+
pid: z.number().int(),
|
|
174
|
+
})
|
|
175
|
+
.strict(),
|
|
176
|
+
z.object({ type: z.literal("heartbeat"), seq: z.number().int() }).strict(),
|
|
177
|
+
/** A Lua slice for `plugin` is about to run synchronously. */
|
|
178
|
+
z.object({ type: z.literal("busy"), plugin: z.string() }).strict(),
|
|
179
|
+
z.object({ type: z.literal("idle"), plugin: z.string() }).strict(),
|
|
180
|
+
z
|
|
181
|
+
.object({
|
|
182
|
+
type: z.literal("log"),
|
|
183
|
+
plugin: z.string(),
|
|
184
|
+
level: LogLevelSchemaZ,
|
|
185
|
+
message: z.string(),
|
|
186
|
+
fields: z.record(z.string(), z.json()).optional(),
|
|
187
|
+
})
|
|
188
|
+
.strict(),
|
|
189
|
+
z
|
|
190
|
+
.object({
|
|
191
|
+
type: z.literal("loaded"),
|
|
192
|
+
plugin: z.string(),
|
|
193
|
+
ok: z.boolean(),
|
|
194
|
+
error: z.string().optional(),
|
|
195
|
+
memoryBytes: z.number().int().nonnegative().optional(),
|
|
196
|
+
})
|
|
197
|
+
.strict(),
|
|
198
|
+
z.object({ type: z.literal("trip"), plugin: z.string(), kind: TripKindSchemaZ }).strict(),
|
|
199
|
+
z
|
|
200
|
+
.object({
|
|
201
|
+
type: z.literal("call"),
|
|
202
|
+
id: z.string().min(1).max(64),
|
|
203
|
+
plugin: z.string().min(1).max(64),
|
|
204
|
+
method: z.string().min(1).max(64),
|
|
205
|
+
/** JSON text of the argument list. */
|
|
206
|
+
args: z.string().max(4 * 1024 * 1024),
|
|
207
|
+
/** The invoke or param-options request whose callback made the call. */
|
|
208
|
+
origin: z.string().min(1).max(64).optional(),
|
|
209
|
+
})
|
|
210
|
+
.strict(),
|
|
211
|
+
z
|
|
212
|
+
.object({
|
|
213
|
+
type: z.literal("actions"),
|
|
214
|
+
plugin: z.string(),
|
|
215
|
+
/** JSON text of the plugin's plain action descriptors. */
|
|
216
|
+
actions: z.string().max(4 * 1024 * 1024),
|
|
217
|
+
})
|
|
218
|
+
.strict(),
|
|
219
|
+
z
|
|
220
|
+
.object({
|
|
221
|
+
type: z.literal("invoke-result"),
|
|
222
|
+
id: z.string(),
|
|
223
|
+
ok: z.boolean(),
|
|
224
|
+
/** JSON text: an array whose first element is the action's result. */
|
|
225
|
+
value: z.string().optional(),
|
|
226
|
+
error: z.string().optional(),
|
|
227
|
+
})
|
|
228
|
+
.strict(),
|
|
229
|
+
z
|
|
230
|
+
.object({
|
|
231
|
+
type: z.literal("param-options-result"),
|
|
232
|
+
id: z.string(),
|
|
233
|
+
ok: z.boolean(),
|
|
234
|
+
/** JSON text `{ choices?, default? }`. */
|
|
235
|
+
options: z.string().optional(),
|
|
236
|
+
error: z.string().optional(),
|
|
237
|
+
})
|
|
238
|
+
.strict(),
|
|
239
|
+
z
|
|
240
|
+
.object({
|
|
241
|
+
type: z.literal("run-result"),
|
|
242
|
+
id: z.string(),
|
|
243
|
+
ok: z.boolean(),
|
|
244
|
+
/** JSON text of the returned values, as an array. */
|
|
245
|
+
values: z.string().optional(),
|
|
246
|
+
error: z.string().optional(),
|
|
247
|
+
logs: z.array(z.object({ level: LogLevelSchemaZ, message: z.string() }).strict()),
|
|
248
|
+
})
|
|
249
|
+
.strict(),
|
|
250
|
+
]);
|
|
251
|
+
/** Longest single protocol line either side accepts. */
|
|
252
|
+
export const MAX_PROTOCOL_LINE_BYTES = 4 * 1024 * 1024;
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* File access for paths a plugin's programs might have touched. Nothing
|
|
3
|
+
* here follows a symlink at the final path component, and writes never go
|
|
4
|
+
* through a name someone else could have created first.
|
|
5
|
+
*/
|
|
6
|
+
import { randomBytes } from "node:crypto";
|
|
7
|
+
import { closeSync, constants, fstatSync, lstatSync, mkdirSync, openSync, readSync, renameSync, rmSync, writeSync, } from "node:fs";
|
|
8
|
+
import { dirname } from "node:path";
|
|
9
|
+
export class UnsafeFileError extends Error {
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Read `path` as UTF-8 if it is a regular file of at most `maxBytes`.
|
|
13
|
+
* Returns null when it does not exist; throws when it is a symlink, not a
|
|
14
|
+
* regular file, or too large.
|
|
15
|
+
*/
|
|
16
|
+
export function readRegularFile(path, maxBytes) {
|
|
17
|
+
let fd;
|
|
18
|
+
try {
|
|
19
|
+
fd = openSync(path, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK);
|
|
20
|
+
}
|
|
21
|
+
catch (error) {
|
|
22
|
+
const code = error.code;
|
|
23
|
+
if (code === "ENOENT")
|
|
24
|
+
return null;
|
|
25
|
+
if (code === "ELOOP" || code === "EMLINK")
|
|
26
|
+
throw new UnsafeFileError(`${path} is a symlink; refusing to read it`);
|
|
27
|
+
throw error;
|
|
28
|
+
}
|
|
29
|
+
try {
|
|
30
|
+
const stat = fstatSync(fd);
|
|
31
|
+
if (!stat.isFile())
|
|
32
|
+
throw new UnsafeFileError(`${path} is not a regular file`);
|
|
33
|
+
if (stat.size > maxBytes)
|
|
34
|
+
throw new UnsafeFileError(`${path} is larger than ${maxBytes} bytes`);
|
|
35
|
+
const buffer = Buffer.alloc(maxBytes + 1);
|
|
36
|
+
let length = 0;
|
|
37
|
+
for (;;) {
|
|
38
|
+
const read = readSync(fd, buffer, length, buffer.length - length, null);
|
|
39
|
+
if (read === 0)
|
|
40
|
+
break;
|
|
41
|
+
length += read;
|
|
42
|
+
if (length > maxBytes)
|
|
43
|
+
throw new UnsafeFileError(`${path} is larger than ${maxBytes} bytes`);
|
|
44
|
+
}
|
|
45
|
+
return buffer.subarray(0, length).toString("utf8");
|
|
46
|
+
}
|
|
47
|
+
finally {
|
|
48
|
+
closeSync(fd);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Replace `path` with `text`: written to a fresh, randomly named file that
|
|
53
|
+
* must not exist yet (O_EXCL, O_NOFOLLOW), then renamed over `path`. A
|
|
54
|
+
* symlink at `path` is replaced, never followed.
|
|
55
|
+
*/
|
|
56
|
+
export function writeFileAtomic(path, text,
|
|
57
|
+
/** Tests pass a fixed name to see that a planted file there is refused. */
|
|
58
|
+
temporaryName = () => `${path}.${randomBytes(12).toString("hex")}.tmp`) {
|
|
59
|
+
mkdirSync(dirname(path), { recursive: true, mode: 0o700 });
|
|
60
|
+
const temporary = temporaryName();
|
|
61
|
+
const fd = openSync(temporary, constants.O_WRONLY | constants.O_CREAT | constants.O_EXCL | constants.O_NOFOLLOW, 0o600);
|
|
62
|
+
let renamed = false;
|
|
63
|
+
try {
|
|
64
|
+
try {
|
|
65
|
+
const data = Buffer.from(text, "utf8");
|
|
66
|
+
let written = 0;
|
|
67
|
+
while (written < data.length)
|
|
68
|
+
written += writeSync(fd, data, written);
|
|
69
|
+
}
|
|
70
|
+
finally {
|
|
71
|
+
closeSync(fd);
|
|
72
|
+
}
|
|
73
|
+
renameSync(temporary, path);
|
|
74
|
+
renamed = true;
|
|
75
|
+
}
|
|
76
|
+
finally {
|
|
77
|
+
if (!renamed)
|
|
78
|
+
rmSync(temporary, { force: true });
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Make sure `path` is a real directory (not a symlink to one), creating it
|
|
83
|
+
* if needed. Throws if something else is there.
|
|
84
|
+
*/
|
|
85
|
+
export function ensureRealDirectory(path) {
|
|
86
|
+
mkdirSync(dirname(path), { recursive: true, mode: 0o700 });
|
|
87
|
+
try {
|
|
88
|
+
mkdirSync(path, { mode: 0o700 });
|
|
89
|
+
}
|
|
90
|
+
catch (error) {
|
|
91
|
+
if (error.code !== "EEXIST")
|
|
92
|
+
throw error;
|
|
93
|
+
}
|
|
94
|
+
const stat = lstatSync(path);
|
|
95
|
+
if (stat.isSymbolicLink() || !stat.isDirectory())
|
|
96
|
+
throw new UnsafeFileError(`${path} is not a real directory`);
|
|
97
|
+
}
|