@open-conduit/sdk 0.0.0-stage → 7.1.0
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 +12 -2
- package/dist/actions.d.ts +145 -0
- package/dist/actions.js +281 -0
- package/dist/actiontest.d.ts +80 -0
- package/dist/actiontest.js +190 -0
- package/dist/artifactstore.d.ts +133 -0
- package/dist/artifactstore.js +364 -0
- package/dist/backup.d.ts +99 -0
- package/dist/backup.js +178 -0
- package/dist/containertest.d.ts +71 -0
- package/dist/containertest.js +294 -0
- package/dist/errors.d.ts +127 -0
- package/dist/errors.js +180 -0
- package/dist/executor.d.ts +374 -0
- package/dist/executor.js +556 -0
- package/dist/executortest.d.ts +183 -0
- package/dist/executortest.js +534 -0
- package/dist/extension.d.ts +855 -0
- package/dist/extension.js +1822 -0
- package/dist/feed.d.ts +20 -0
- package/dist/feed.js +88 -0
- package/dist/forward.d.ts +38 -0
- package/dist/forward.js +163 -0
- package/dist/forwardca.d.ts +21 -0
- package/dist/forwardca.js +144 -0
- package/dist/hooks.d.ts +108 -0
- package/dist/hooks.js +13 -0
- package/dist/host.d.ts +295 -0
- package/dist/host.js +486 -0
- package/dist/index.d.ts +34 -0
- package/dist/index.js +31 -0
- package/dist/jsonrpc.d.ts +39 -0
- package/dist/jsonrpc.js +350 -0
- package/dist/probe.d.ts +61 -0
- package/dist/probe.js +92 -0
- package/dist/providertest.d.ts +59 -0
- package/dist/providertest.js +168 -0
- package/dist/resources.d.ts +208 -0
- package/dist/resources.js +303 -0
- package/dist/sandbox.d.ts +84 -0
- package/dist/sandbox.js +85 -0
- package/dist/schema.d.ts +71 -0
- package/dist/schema.js +252 -0
- package/dist/setup.d.ts +145 -0
- package/dist/setup.js +179 -0
- package/dist/setuptest.d.ts +90 -0
- package/dist/setuptest.js +319 -0
- package/dist/storetest.d.ts +64 -0
- package/dist/storetest.js +740 -0
- package/dist/telemetry.d.ts +86 -0
- package/dist/telemetry.js +46 -0
- package/dist/testing.d.ts +756 -0
- package/dist/testing.js +2165 -0
- package/dist/toolbelt.d.ts +91 -0
- package/dist/toolbelt.js +216 -0
- package/dist/tunnel.d.ts +62 -0
- package/dist/tunnel.js +93 -0
- package/dist/wire.d.ts +5359 -0
- package/dist/wire.js +2211 -0
- package/package.json +29 -4
package/dist/jsonrpc.js
ADDED
|
@@ -0,0 +1,350 @@
|
|
|
1
|
+
// The protocol version 1 transport, as protocol/jsonrpc implements it:
|
|
2
|
+
// JSON-RPC 2.0 over a pair of byte streams, one JSON object per line. The
|
|
3
|
+
// connection is symmetric: either side sends requests, which is how an
|
|
4
|
+
// extension calls the host (KV, events, secrets) while serving a host
|
|
5
|
+
// request.
|
|
6
|
+
//
|
|
7
|
+
// - over maxInflight concurrently served requests, a request is refused
|
|
8
|
+
// with "overloaded" rather than queued;
|
|
9
|
+
// - a line longer than maxMessage, invalid JSON or a truncated final line
|
|
10
|
+
// closes the connection, failing every pending call with TransportError;
|
|
11
|
+
// - authored errors cross as -32000 with data.code; anything else crosses
|
|
12
|
+
// as "internal" with no detail;
|
|
13
|
+
// - "$/cancel" aborts the peer's handler signal; a cancelled call rejects
|
|
14
|
+
// at once with CancelledError and a late response is discarded.
|
|
15
|
+
import { CancelledError, Code, ProtocolError, TransportError, decode, encode, } from "./errors.js";
|
|
16
|
+
import { isRecord, matches, t } from "./schema.js";
|
|
17
|
+
/** Bounds of protocol version 1 (protocol/protocol.go). */
|
|
18
|
+
export const MaxMessageBytes = 16 << 20;
|
|
19
|
+
export const MaxChunkBytes = 256 << 10;
|
|
20
|
+
export const MaxInflight = 64;
|
|
21
|
+
/** Capacity reserved for control methods on top of MaxInflight. */
|
|
22
|
+
export const MaxControlInflight = 16;
|
|
23
|
+
const heldMethods = new Set(["executor.wait"]);
|
|
24
|
+
const controlMethods = new Set([
|
|
25
|
+
"executor.recover",
|
|
26
|
+
"executor.interrupt",
|
|
27
|
+
"executor.stop",
|
|
28
|
+
"executor.forget",
|
|
29
|
+
]);
|
|
30
|
+
/** The lane of a host-to-extension method (protocol.LaneOf). */
|
|
31
|
+
export function laneOf(method) {
|
|
32
|
+
if (heldMethods.has(method))
|
|
33
|
+
return "held";
|
|
34
|
+
if (controlMethods.has(method))
|
|
35
|
+
return "control";
|
|
36
|
+
return "bounded";
|
|
37
|
+
}
|
|
38
|
+
export const CancelMethod = "$/cancel";
|
|
39
|
+
const codeParse = -32700;
|
|
40
|
+
const codeInvalidRequest = -32600;
|
|
41
|
+
const codeMethodNotFound = -32601;
|
|
42
|
+
const codeInvalidParams = -32602;
|
|
43
|
+
const codeInternal = -32603;
|
|
44
|
+
const codeCancelled = -32800;
|
|
45
|
+
const codeApplication = -32000;
|
|
46
|
+
const WireError = t.object({
|
|
47
|
+
code: t.integer(),
|
|
48
|
+
message: t.string(),
|
|
49
|
+
data: t.optional(t.object({
|
|
50
|
+
code: t.string(),
|
|
51
|
+
reset_at: t.optional(t.string()),
|
|
52
|
+
diagnostics: t.optional(t.array(t.object({ path: t.string(), code: t.string(), message: t.string() }, { open: true }))),
|
|
53
|
+
}, { open: true })),
|
|
54
|
+
}, { open: true });
|
|
55
|
+
// Only the envelope is decoded here; params and results are decoded by
|
|
56
|
+
// whoever knows their schema.
|
|
57
|
+
const Message = t.object({
|
|
58
|
+
jsonrpc: t.literal("2.0"),
|
|
59
|
+
id: t.optional(t.nullable(t.any())),
|
|
60
|
+
method: t.optional(t.string()),
|
|
61
|
+
params: t.optional(t.any()),
|
|
62
|
+
result: t.optional(t.any()),
|
|
63
|
+
error: t.optional(t.nullable(WireError)),
|
|
64
|
+
}, { open: true });
|
|
65
|
+
function isID(v) {
|
|
66
|
+
return typeof v === "number" || typeof v === "string";
|
|
67
|
+
}
|
|
68
|
+
/** One JSON-RPC peer. */
|
|
69
|
+
export class Conn {
|
|
70
|
+
#handler;
|
|
71
|
+
#out;
|
|
72
|
+
#maxMessage;
|
|
73
|
+
#maxInflight;
|
|
74
|
+
#lane;
|
|
75
|
+
#controlInflight = 0;
|
|
76
|
+
#onProtocolError;
|
|
77
|
+
#pending = new Map();
|
|
78
|
+
#cancels = new Map();
|
|
79
|
+
#next = 0;
|
|
80
|
+
#inflight = 0;
|
|
81
|
+
#closed = false;
|
|
82
|
+
#buffered = [];
|
|
83
|
+
#bufferedBytes = 0;
|
|
84
|
+
done;
|
|
85
|
+
#finish;
|
|
86
|
+
constructor(input, output, handler, o = {}) {
|
|
87
|
+
this.#handler = handler;
|
|
88
|
+
this.#out = output;
|
|
89
|
+
this.#maxMessage = o.maxMessage ?? MaxMessageBytes;
|
|
90
|
+
this.#maxInflight = o.maxInflight ?? MaxInflight;
|
|
91
|
+
this.#lane = o.lane ?? (() => "bounded");
|
|
92
|
+
this.#onProtocolError = o.onProtocolError;
|
|
93
|
+
this.done = new Promise((resolve) => {
|
|
94
|
+
this.#finish = resolve;
|
|
95
|
+
});
|
|
96
|
+
input.on("data", (chunk) => this.#read(typeof chunk === "string" ? Buffer.from(chunk) : chunk));
|
|
97
|
+
input.on("end", () => {
|
|
98
|
+
// A partial final line (a response cut off by a crash) is a protocol
|
|
99
|
+
// error, never a result.
|
|
100
|
+
if (this.#bufferedBytes > 0)
|
|
101
|
+
this.#protocolError("truncated message");
|
|
102
|
+
else
|
|
103
|
+
this.close();
|
|
104
|
+
});
|
|
105
|
+
input.on("error", () => this.close());
|
|
106
|
+
output.on("error", () => this.close());
|
|
107
|
+
}
|
|
108
|
+
/** Reports whether the connection has ended. */
|
|
109
|
+
get closed() {
|
|
110
|
+
return this.#closed;
|
|
111
|
+
}
|
|
112
|
+
/** Ends the connection; pending calls reject with TransportError. */
|
|
113
|
+
close() {
|
|
114
|
+
if (this.#closed)
|
|
115
|
+
return;
|
|
116
|
+
this.#closed = true;
|
|
117
|
+
for (const c of this.#cancels.values())
|
|
118
|
+
c.abort();
|
|
119
|
+
for (const p of this.#pending.values())
|
|
120
|
+
p.reject(new TransportError());
|
|
121
|
+
this.#pending.clear();
|
|
122
|
+
this.#finish();
|
|
123
|
+
}
|
|
124
|
+
#protocolError(reason) {
|
|
125
|
+
this.#onProtocolError?.(reason);
|
|
126
|
+
this.close();
|
|
127
|
+
}
|
|
128
|
+
#read(chunk) {
|
|
129
|
+
let start = 0;
|
|
130
|
+
for (;;) {
|
|
131
|
+
const nl = chunk.indexOf(0x0a, start);
|
|
132
|
+
if (nl < 0)
|
|
133
|
+
break;
|
|
134
|
+
const part = chunk.subarray(start, nl);
|
|
135
|
+
if (this.#bufferedBytes + part.length >= this.#maxMessage)
|
|
136
|
+
return this.#protocolError("message exceeds the protocol bound");
|
|
137
|
+
const line = this.#bufferedBytes === 0
|
|
138
|
+
? part
|
|
139
|
+
: Buffer.concat([...this.#buffered, part]);
|
|
140
|
+
this.#buffered = [];
|
|
141
|
+
this.#bufferedBytes = 0;
|
|
142
|
+
start = nl + 1;
|
|
143
|
+
if (line.length > 0)
|
|
144
|
+
this.#message(line);
|
|
145
|
+
if (this.#closed)
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
const rest = chunk.subarray(start);
|
|
149
|
+
if (rest.length === 0)
|
|
150
|
+
return;
|
|
151
|
+
if (this.#bufferedBytes + rest.length >= this.#maxMessage)
|
|
152
|
+
return this.#protocolError("message exceeds the protocol bound");
|
|
153
|
+
this.#buffered.push(Buffer.from(rest));
|
|
154
|
+
this.#bufferedBytes += rest.length;
|
|
155
|
+
}
|
|
156
|
+
#message(line) {
|
|
157
|
+
let m;
|
|
158
|
+
try {
|
|
159
|
+
m = JSON.parse(line.toString("utf8"));
|
|
160
|
+
}
|
|
161
|
+
catch {
|
|
162
|
+
return this.#protocolError("malformed message");
|
|
163
|
+
}
|
|
164
|
+
if (!matches(Message, m))
|
|
165
|
+
return this.#protocolError("malformed message");
|
|
166
|
+
const { id, method } = m;
|
|
167
|
+
if (method === CancelMethod) {
|
|
168
|
+
if (isRecord(m.params))
|
|
169
|
+
this.#cancels.get(JSON.stringify(m.params.id))?.abort();
|
|
170
|
+
}
|
|
171
|
+
else if (method !== undefined && method !== "" && isID(id)) {
|
|
172
|
+
// Refuse rather than queue: the reader also carries the responses
|
|
173
|
+
// in-flight handlers wait for, so blocking it would deadlock them.
|
|
174
|
+
const release = this.#admit(method);
|
|
175
|
+
if (release === undefined) {
|
|
176
|
+
this.#write({
|
|
177
|
+
jsonrpc: "2.0",
|
|
178
|
+
id,
|
|
179
|
+
error: {
|
|
180
|
+
code: codeApplication,
|
|
181
|
+
message: "too many requests in flight",
|
|
182
|
+
data: { code: Code.Overloaded },
|
|
183
|
+
},
|
|
184
|
+
});
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
void this.#serve(id, method, m.params, release);
|
|
188
|
+
}
|
|
189
|
+
else if (method !== undefined && method !== "") {
|
|
190
|
+
// Other notifications are served without a reply.
|
|
191
|
+
void this.#handler(method, m.params, new AbortController().signal).catch(() => { });
|
|
192
|
+
}
|
|
193
|
+
else if (isID(id)) {
|
|
194
|
+
const key = JSON.stringify(id);
|
|
195
|
+
const p = this.#pending.get(key);
|
|
196
|
+
this.#pending.delete(key);
|
|
197
|
+
if (p === undefined)
|
|
198
|
+
return;
|
|
199
|
+
if (m.error !== undefined && m.error !== null)
|
|
200
|
+
p.reject(decodeError(m.error));
|
|
201
|
+
else
|
|
202
|
+
p.resolve(m.result);
|
|
203
|
+
}
|
|
204
|
+
else {
|
|
205
|
+
this.#protocolError("message is neither request nor response");
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
/** Takes the capacity a method is served from, or undefined when full. */
|
|
209
|
+
#admit(method) {
|
|
210
|
+
const lane = this.#lane(method);
|
|
211
|
+
if (lane === "held")
|
|
212
|
+
return () => { };
|
|
213
|
+
if (lane === "control" && this.#controlInflight < MaxControlInflight) {
|
|
214
|
+
this.#controlInflight++;
|
|
215
|
+
return () => void this.#controlInflight--;
|
|
216
|
+
}
|
|
217
|
+
if (this.#inflight >= this.#maxInflight)
|
|
218
|
+
return undefined;
|
|
219
|
+
this.#inflight++;
|
|
220
|
+
return () => void this.#inflight--;
|
|
221
|
+
}
|
|
222
|
+
async #serve(id, method, params, release) {
|
|
223
|
+
const key = JSON.stringify(id);
|
|
224
|
+
const abort = new AbortController();
|
|
225
|
+
this.#cancels.set(key, abort);
|
|
226
|
+
let reply;
|
|
227
|
+
try {
|
|
228
|
+
const result = await this.#handler(method, params, abort.signal);
|
|
229
|
+
reply = abort.signal.aborted
|
|
230
|
+
? { jsonrpc: "2.0", id, error: encodeError(new CancelledError(), true) }
|
|
231
|
+
: { jsonrpc: "2.0", id, result: result === undefined ? {} : result };
|
|
232
|
+
}
|
|
233
|
+
catch (err) {
|
|
234
|
+
reply = {
|
|
235
|
+
jsonrpc: "2.0",
|
|
236
|
+
id,
|
|
237
|
+
error: encodeError(err, abort.signal.aborted),
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
finally {
|
|
241
|
+
release();
|
|
242
|
+
this.#cancels.delete(key);
|
|
243
|
+
}
|
|
244
|
+
if (this.#closed)
|
|
245
|
+
return;
|
|
246
|
+
if (!this.#write(reply) && reply.error === undefined) {
|
|
247
|
+
// An oversized result is refused rather than closing the stream.
|
|
248
|
+
this.#write({
|
|
249
|
+
jsonrpc: "2.0",
|
|
250
|
+
id,
|
|
251
|
+
error: encodeError(new ProtocolError(Code.Invalid, "message exceeds the protocol bound"), false),
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
/** Writes one message; false when it is over the bound or unencodable. */
|
|
256
|
+
#write(m) {
|
|
257
|
+
if (this.#closed)
|
|
258
|
+
return false;
|
|
259
|
+
let data;
|
|
260
|
+
try {
|
|
261
|
+
data = Buffer.from(JSON.stringify(m) + "\n", "utf8");
|
|
262
|
+
}
|
|
263
|
+
catch {
|
|
264
|
+
return false;
|
|
265
|
+
}
|
|
266
|
+
if (data.length - 1 >= this.#maxMessage)
|
|
267
|
+
return false;
|
|
268
|
+
this.#out.write(data);
|
|
269
|
+
return true;
|
|
270
|
+
}
|
|
271
|
+
/**
|
|
272
|
+
* Sends a request and resolves with its result, undecoded. Aborting
|
|
273
|
+
* signal sends $/cancel and rejects at once with CancelledError.
|
|
274
|
+
*/
|
|
275
|
+
call(method, params = {}, signal) {
|
|
276
|
+
if (this.#closed)
|
|
277
|
+
return Promise.reject(new TransportError());
|
|
278
|
+
if (signal?.aborted)
|
|
279
|
+
return Promise.reject(new CancelledError());
|
|
280
|
+
const id = ++this.#next;
|
|
281
|
+
const key = JSON.stringify(id);
|
|
282
|
+
return new Promise((resolve, reject) => {
|
|
283
|
+
const onAbort = () => {
|
|
284
|
+
if (!this.#pending.delete(key))
|
|
285
|
+
return;
|
|
286
|
+
this.#write({ jsonrpc: "2.0", method: CancelMethod, params: { id } });
|
|
287
|
+
reject(new CancelledError());
|
|
288
|
+
};
|
|
289
|
+
this.#pending.set(key, {
|
|
290
|
+
resolve: (v) => {
|
|
291
|
+
signal?.removeEventListener("abort", onAbort);
|
|
292
|
+
resolve(v);
|
|
293
|
+
},
|
|
294
|
+
reject: (e) => {
|
|
295
|
+
signal?.removeEventListener("abort", onAbort);
|
|
296
|
+
reject(e);
|
|
297
|
+
},
|
|
298
|
+
});
|
|
299
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
300
|
+
if (!this.#write({ jsonrpc: "2.0", id, method, params })) {
|
|
301
|
+
this.#pending.delete(key);
|
|
302
|
+
signal?.removeEventListener("abort", onAbort);
|
|
303
|
+
reject(new ProtocolError(Code.Invalid, "message exceeds the protocol bound"));
|
|
304
|
+
}
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
/** Sends a notification (no response). */
|
|
308
|
+
notify(method, params = {}) {
|
|
309
|
+
this.#write({ jsonrpc: "2.0", method, params });
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
function encodeError(err, cancelled) {
|
|
313
|
+
if (cancelled)
|
|
314
|
+
return {
|
|
315
|
+
code: codeCancelled,
|
|
316
|
+
message: "request cancelled",
|
|
317
|
+
data: { code: Code.Cancelled },
|
|
318
|
+
};
|
|
319
|
+
const e = encode(err);
|
|
320
|
+
if (e.code === Code.Cancelled)
|
|
321
|
+
return { code: codeCancelled, message: e.message, data: { code: e.code } };
|
|
322
|
+
const code = e.code === Code.MethodNotFound
|
|
323
|
+
? codeMethodNotFound
|
|
324
|
+
: e.code === Code.Internal
|
|
325
|
+
? codeInternal
|
|
326
|
+
: codeApplication;
|
|
327
|
+
const data = { code: e.code };
|
|
328
|
+
if (e.resetAt !== undefined)
|
|
329
|
+
data.reset_at = e.resetAt;
|
|
330
|
+
if (e.diagnostics !== undefined)
|
|
331
|
+
data.diagnostics = e.diagnostics;
|
|
332
|
+
return { code, message: e.message, data };
|
|
333
|
+
}
|
|
334
|
+
function decodeError(e) {
|
|
335
|
+
if (e.code === codeCancelled)
|
|
336
|
+
return new CancelledError();
|
|
337
|
+
if (e.data?.code)
|
|
338
|
+
return decode(e.data.code, e.message, e.data.reset_at, e.data.diagnostics?.map(({ path, code, message }) => ({
|
|
339
|
+
path,
|
|
340
|
+
code,
|
|
341
|
+
message,
|
|
342
|
+
})));
|
|
343
|
+
if (e.code === codeMethodNotFound)
|
|
344
|
+
return new ProtocolError(Code.MethodNotFound, e.message);
|
|
345
|
+
if (e.code === codeInvalidParams ||
|
|
346
|
+
e.code === codeInvalidRequest ||
|
|
347
|
+
e.code === codeParse)
|
|
348
|
+
return new ProtocolError(Code.Invalid, e.message);
|
|
349
|
+
return new ProtocolError(Code.Internal, "internal error");
|
|
350
|
+
}
|
package/dist/probe.d.ts
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import type { Context } from "./extension.js";
|
|
2
|
+
import { type ProbeInfo, type ProbeRunParams, type ProbeRunResult } from "./wire.js";
|
|
3
|
+
import type { Host } from "./host.js";
|
|
4
|
+
/** One argument a probe takes. */
|
|
5
|
+
export interface ProbeArgSpec {
|
|
6
|
+
name: string;
|
|
7
|
+
summary?: string;
|
|
8
|
+
required?: boolean;
|
|
9
|
+
/** The value is a connection ID, which the host grants the probe for the run (ProbeContext.connection). */
|
|
10
|
+
connection?: boolean;
|
|
11
|
+
}
|
|
12
|
+
/** The connection a probe runs through. secret works only while the run lasts. */
|
|
13
|
+
export interface ProbeConnection {
|
|
14
|
+
id: string;
|
|
15
|
+
type: string;
|
|
16
|
+
config: unknown;
|
|
17
|
+
/** The names of its secrets. */
|
|
18
|
+
secrets: string[];
|
|
19
|
+
secret(name: string): Promise<string>;
|
|
20
|
+
}
|
|
21
|
+
export interface ProbeContext extends Context {
|
|
22
|
+
/** The scope a run through a connection is for; empty otherwise, since the host names one only then. */
|
|
23
|
+
scopeId: string;
|
|
24
|
+
/** The connection a connection argument named, granted for this run. */
|
|
25
|
+
connection?: ProbeConnection;
|
|
26
|
+
}
|
|
27
|
+
/** A probe's output and the command it stands for, shown to the operator ("gh auth status"). */
|
|
28
|
+
export interface ProbeOutput {
|
|
29
|
+
command?: string;
|
|
30
|
+
/** Any JSON value: safe output, with no secret and no account detail the setup doesn't need. */
|
|
31
|
+
output?: unknown;
|
|
32
|
+
}
|
|
33
|
+
export interface ProbeSpec {
|
|
34
|
+
/** One line for the agent: what the probe tells it. */
|
|
35
|
+
summary: string;
|
|
36
|
+
args?: ProbeArgSpec[];
|
|
37
|
+
/**
|
|
38
|
+
* Runs the probe without a shell. Throw ProbeFailure for a probe that
|
|
39
|
+
* ran and found a problem: its reason is shown to the agent, so it must
|
|
40
|
+
* be fixed text with no secret in it. Anything else thrown answers that
|
|
41
|
+
* the probe failed, never its message.
|
|
42
|
+
*/
|
|
43
|
+
run: (args: Record<string, string>, ctx: ProbeContext) => ProbeOutput | Promise<ProbeOutput>;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* A probe that ran and failed (extension.ErrProbeInput, or any reason it
|
|
47
|
+
* found): the message is shown to the agent, so it must be fixed and
|
|
48
|
+
* secret-free.
|
|
49
|
+
*/
|
|
50
|
+
export declare class ProbeFailure extends Error {
|
|
51
|
+
constructor(reason: string);
|
|
52
|
+
}
|
|
53
|
+
/** @internal */
|
|
54
|
+
export interface ProbeHandler {
|
|
55
|
+
info: ProbeInfo;
|
|
56
|
+
serve: (p: ProbeRunParams, ctx: Context & {
|
|
57
|
+
host: Host;
|
|
58
|
+
}) => Promise<ProbeRunResult>;
|
|
59
|
+
}
|
|
60
|
+
/** @internal */
|
|
61
|
+
export declare function probeHandler(name: string, spec: ProbeSpec): ProbeHandler;
|
package/dist/probe.js
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
// Setup probes: named, read-only checks a setup session's agent may run
|
|
2
|
+
// (extension.SetupProbe), as protocol/sdk serves sdk.Extension.Probes
|
|
3
|
+
// through probe.serve. The host lists each as "<extension>.<probe>",
|
|
4
|
+
// checks its arguments against the declaration before the probe sees
|
|
5
|
+
// them, and for an argument marked connection, hands the probe that
|
|
6
|
+
// connection for the one run: its nonsecret config, the names of its
|
|
7
|
+
// secrets and a resolver that works only while the run lasts. The agent
|
|
8
|
+
// names the connection and never sees its credential.
|
|
9
|
+
import { Code, ProtocolError } from "./errors.js";
|
|
10
|
+
import { SecretResolveResult, } from "./wire.js";
|
|
11
|
+
/**
|
|
12
|
+
* A probe that ran and failed (extension.ErrProbeInput, or any reason it
|
|
13
|
+
* found): the message is shown to the agent, so it must be fixed and
|
|
14
|
+
* secret-free.
|
|
15
|
+
*/
|
|
16
|
+
export class ProbeFailure extends Error {
|
|
17
|
+
constructor(reason) {
|
|
18
|
+
super(reason);
|
|
19
|
+
this.name = "ProbeFailure";
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
const probeName = /^[a-z][a-z0-9-]{0,62}$/;
|
|
23
|
+
/** The host's bound on a failed probe's reason. */
|
|
24
|
+
const maxReason = 300;
|
|
25
|
+
/** @internal */
|
|
26
|
+
export function probeHandler(name, spec) {
|
|
27
|
+
if (!probeName.test(name))
|
|
28
|
+
throw new Error(`sdk: probe ${name} must be a lower-case name of letters, digits and dashes`);
|
|
29
|
+
if (spec.summary.trim() === "")
|
|
30
|
+
throw new Error(`sdk: probe ${name} needs a summary`);
|
|
31
|
+
const names = new Set();
|
|
32
|
+
for (const a of spec.args ?? []) {
|
|
33
|
+
if (a.name === "" || names.has(a.name))
|
|
34
|
+
throw new Error(`sdk: probe ${name}: argument "${a.name}" is empty or repeats`);
|
|
35
|
+
names.add(a.name);
|
|
36
|
+
}
|
|
37
|
+
const info = {
|
|
38
|
+
name,
|
|
39
|
+
summary: spec.summary,
|
|
40
|
+
...(spec.args?.length
|
|
41
|
+
? {
|
|
42
|
+
args: spec.args.map((a) => ({
|
|
43
|
+
name: a.name,
|
|
44
|
+
...(a.summary ? { summary: a.summary } : {}),
|
|
45
|
+
...(a.required ? { required: true } : {}),
|
|
46
|
+
...(a.connection ? { connection: true } : {}),
|
|
47
|
+
})),
|
|
48
|
+
}
|
|
49
|
+
: {}),
|
|
50
|
+
};
|
|
51
|
+
return {
|
|
52
|
+
info,
|
|
53
|
+
serve: async (p, ctx) => {
|
|
54
|
+
const scopeId = p.scope_id ?? "";
|
|
55
|
+
const c = p.connection;
|
|
56
|
+
const probeCtx = { ...ctx, scopeId };
|
|
57
|
+
if (c !== undefined) {
|
|
58
|
+
const grant = p.grant ?? "";
|
|
59
|
+
probeCtx.connection = {
|
|
60
|
+
id: c.id,
|
|
61
|
+
type: c.type,
|
|
62
|
+
config: c.config ?? {},
|
|
63
|
+
secrets: [...(c.secrets ?? [])],
|
|
64
|
+
secret: async (secret) => {
|
|
65
|
+
const out = await ctx.host.call("secret.resolve", {
|
|
66
|
+
scope_id: scopeId,
|
|
67
|
+
connection: c.id,
|
|
68
|
+
probe_grant: grant,
|
|
69
|
+
names: [secret],
|
|
70
|
+
}, SecretResolveResult, ctx.signal);
|
|
71
|
+
const v = out.values?.[secret];
|
|
72
|
+
if (v === undefined)
|
|
73
|
+
throw new ProtocolError(Code.NotFound, "no such secret");
|
|
74
|
+
return v;
|
|
75
|
+
},
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
let got;
|
|
79
|
+
try {
|
|
80
|
+
got = await spec.run({ ...(p.args ?? {}) }, probeCtx);
|
|
81
|
+
}
|
|
82
|
+
catch (err) {
|
|
83
|
+
const reason = err instanceof ProbeFailure ? err.message : "the probe failed";
|
|
84
|
+
return { error: reason.slice(0, maxReason) || "the probe failed" };
|
|
85
|
+
}
|
|
86
|
+
return {
|
|
87
|
+
...(got?.command ? { command: got.command } : {}),
|
|
88
|
+
...(got?.output === undefined ? {} : { output: got.output }),
|
|
89
|
+
};
|
|
90
|
+
},
|
|
91
|
+
};
|
|
92
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { TestResourceProvider, TestSandboxPolicy, TestSandboxProvider, TestTunnelProvider } from "./testing.js";
|
|
2
|
+
import type { SandboxLaunch, SandboxNeeds } from "./wire.js";
|
|
3
|
+
/** A resource provider to check, with a config and mode it acquires under. */
|
|
4
|
+
export interface ResourceFixture {
|
|
5
|
+
provider: TestResourceProvider;
|
|
6
|
+
/** A resource config the provider acquires under. */
|
|
7
|
+
config: unknown;
|
|
8
|
+
/** The mode to acquire in (default: the config's own). Name fresh to check a lease is its execution's alone. */
|
|
9
|
+
mode?: string;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Checks the lease contract the host relies on
|
|
13
|
+
* (extension.ResourceProvider): acquiring again for the same owner and
|
|
14
|
+
* mode returns the same lease; lookup returns it to its owner and refuses
|
|
15
|
+
* another Work's lookup (and, for a fresh lease, which is its execution's
|
|
16
|
+
* alone, another execution's of the same Work), and an unknown reference,
|
|
17
|
+
* with resource_not_found, never creating one; reconcile keeps the lease of an
|
|
18
|
+
* owner still running, and runs again, once the owner is done, without
|
|
19
|
+
* failing.
|
|
20
|
+
*/
|
|
21
|
+
export declare function checkResourceProvider(f: ResourceFixture): Promise<void>;
|
|
22
|
+
/** A sandbox provider to check, with a launch its policy allows and needs it must deny. */
|
|
23
|
+
export interface SandboxFixture {
|
|
24
|
+
provider: TestSandboxProvider;
|
|
25
|
+
policy: TestSandboxPolicy;
|
|
26
|
+
needs: SandboxNeeds;
|
|
27
|
+
launch: SandboxLaunch;
|
|
28
|
+
/** Needs outside the policy, which plan must refuse with sandbox_denied. */
|
|
29
|
+
denied: SandboxNeeds;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Checks the sandbox contract the host relies on
|
|
33
|
+
* (extension.SandboxProvider, sandboxes.policy): a launch inside the
|
|
34
|
+
* policy is planned to a command; a need outside the policy is denied,
|
|
35
|
+
* never widened into it;
|
|
36
|
+
* and adopt refuses a plan the provider never made, so a restarted host
|
|
37
|
+
* never adopts a segment it can't prove is confined.
|
|
38
|
+
*/
|
|
39
|
+
export declare function checkSandboxProvider(f: SandboxFixture): Promise<void>;
|
|
40
|
+
/** A tunnel provider to check, with a config it runs and one validate must refuse. */
|
|
41
|
+
export interface TunnelFixture {
|
|
42
|
+
provider: TestTunnelProvider;
|
|
43
|
+
config: unknown;
|
|
44
|
+
refused: unknown;
|
|
45
|
+
/**
|
|
46
|
+
* Whether a request to the published URL reaches the listener from
|
|
47
|
+
* here, as it does for a tunnel that runs on this machine: the check
|
|
48
|
+
* then serves the address itself and fetches through the tunnel.
|
|
49
|
+
*/
|
|
50
|
+
reachable?: boolean;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Checks the tunnel contract the host relies on (extension.TunnelProvider,
|
|
54
|
+
* tunnels.policy): validate accepts the config and refuses the refused
|
|
55
|
+
* one; a run publishes a URL the host can point webhooks at; and, for a
|
|
56
|
+
* reachable tunnel, a request to it reaches the address the host gave and
|
|
57
|
+
* nothing else.
|
|
58
|
+
*/
|
|
59
|
+
export declare function checkTunnelProvider(f: TunnelFixture): Promise<void>;
|