@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
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import type { Context } from "./extension.js";
|
|
2
|
+
import { type Infer, type Type } from "./schema.js";
|
|
3
|
+
import { type Payload, type ResourceOwner, type SandboxLaunch, type SandboxNeeds, type SandboxPaths, type SandboxPlan } from "./wire.js";
|
|
4
|
+
/** What an interrupt of a sandboxed launch can promise (extension.SandboxTerm, SandboxKill). */
|
|
5
|
+
export declare const SandboxSignals: {
|
|
6
|
+
/** A termination signal sent to the executor's process group reaches the sandboxed command. */
|
|
7
|
+
readonly Term: "term";
|
|
8
|
+
/** The sandboxed command runs outside the group, so only killing the wrapper stops it. */
|
|
9
|
+
readonly Kill: "kill";
|
|
10
|
+
};
|
|
11
|
+
/** One launch to wrap (extension.SandboxProvider.Plan). */
|
|
12
|
+
export interface SandboxPlanRequest<C> {
|
|
13
|
+
type: string;
|
|
14
|
+
owner: ResourceOwner;
|
|
15
|
+
/** The binding's sandbox config, decoded by the provider's schema. */
|
|
16
|
+
config: C;
|
|
17
|
+
/** This attempt's host-set paths. */
|
|
18
|
+
paths: SandboxPaths;
|
|
19
|
+
/** The launch's needs, the resource's and the host's protected set already merged in. */
|
|
20
|
+
needs: SandboxNeeds;
|
|
21
|
+
launch: SandboxLaunch;
|
|
22
|
+
}
|
|
23
|
+
/** A wrapped launch (extension.SandboxPlan). The host computes its digest from argv and data. */
|
|
24
|
+
export interface SandboxPlanOut {
|
|
25
|
+
/** What runs in place of the launch's argv. */
|
|
26
|
+
argv: string[];
|
|
27
|
+
env?: string[];
|
|
28
|
+
/** SandboxSignals.Term or Kill. */
|
|
29
|
+
signals?: string;
|
|
30
|
+
/** One line for history. */
|
|
31
|
+
summary?: string;
|
|
32
|
+
/** The effective policy, for history; the digest covers it. */
|
|
33
|
+
data?: Payload;
|
|
34
|
+
/** Where the wrapper runs argv, when not the launch's directory. */
|
|
35
|
+
dir?: string;
|
|
36
|
+
}
|
|
37
|
+
/** A recovered segment's plan, as the host pinned it, and its processes (extension.SandboxProvider.Adopt). */
|
|
38
|
+
export interface SandboxAdoptRequest {
|
|
39
|
+
type: string;
|
|
40
|
+
plan: SandboxPlan;
|
|
41
|
+
/** The executor's wrapper that runs the plan. */
|
|
42
|
+
parent: number;
|
|
43
|
+
/** The sandbox root's pid, recorded as it started; 0 when it never started or has exited. */
|
|
44
|
+
root: number;
|
|
45
|
+
}
|
|
46
|
+
/** The context of a sandbox provider's call. */
|
|
47
|
+
export interface SandboxContext extends Context {
|
|
48
|
+
/** The extension's private state directory, from initialize. */
|
|
49
|
+
stateDir: string;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* A process sandbox provider type (extension.SandboxProvider), which a
|
|
53
|
+
* binding's sandbox config names.
|
|
54
|
+
*
|
|
55
|
+
* plan applies the operator's policy and checks every need against it. It
|
|
56
|
+
* never widens the policy: refuse a need outside it with
|
|
57
|
+
* Code.SandboxDenied, and anything it can't enforce with
|
|
58
|
+
* Code.SandboxUnavailable (a ProtocolError, whose message crosses to the
|
|
59
|
+
* executor). adopt proves, when a restarted host recovers a segment, that
|
|
60
|
+
* the process tree running the plan is still inside the boundary it
|
|
61
|
+
* launched; anything it can't prove, a plan it never made included, is a
|
|
62
|
+
* refusal, and the segment isn't adopted. Anything else thrown crosses as
|
|
63
|
+
* internal, which the host treats as the sandbox being unavailable, so
|
|
64
|
+
* nothing runs.
|
|
65
|
+
*/
|
|
66
|
+
export interface SandboxProviderSpec<S extends Type<unknown>> {
|
|
67
|
+
/** The sandbox config's schema: validate (kind sandbox) refuses a config it doesn't match. */
|
|
68
|
+
config?: S;
|
|
69
|
+
/**
|
|
70
|
+
* Checks the schema can't express; throw to refuse the config. A
|
|
71
|
+
* ConfigError says which field is wrong and why; anything else thrown is
|
|
72
|
+
* reported only as invalid.
|
|
73
|
+
*/
|
|
74
|
+
validate?: (config: Infer<S>) => void | Promise<void>;
|
|
75
|
+
plan: (r: SandboxPlanRequest<Infer<S>>, ctx: SandboxContext) => SandboxPlanOut | Promise<SandboxPlanOut>;
|
|
76
|
+
adopt: (r: SandboxAdoptRequest, ctx: SandboxContext) => void | Promise<void>;
|
|
77
|
+
}
|
|
78
|
+
/** @internal */
|
|
79
|
+
export interface SandboxHandler {
|
|
80
|
+
validate: (config: unknown) => Promise<void>;
|
|
81
|
+
serve: (method: string, raw: unknown, ctx: SandboxContext) => Promise<unknown>;
|
|
82
|
+
}
|
|
83
|
+
/** @internal */
|
|
84
|
+
export declare function sandboxHandler<S extends Type<unknown>>(type: string, spec: SandboxProviderSpec<S>, params: <T extends Type<unknown>>(schema: T, raw: unknown) => Infer<T>): SandboxHandler;
|
package/dist/sandbox.js
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
// Sandbox providers: sandbox_provider.* and validate (kind sandbox), as
|
|
2
|
+
// protocol/sdk serves sdk.Extension.Sandboxes (protocol/sdk/surfaces.go).
|
|
3
|
+
// The host binds a provider to an attempt (extension.BindSandbox): it
|
|
4
|
+
// merges the resource's needs and its own protected set into the launch's
|
|
5
|
+
// needs and pins the plan's digest, computed from the argv and data the
|
|
6
|
+
// provider answers, so the provider only turns a policy and needs into a
|
|
7
|
+
// wrapped launch, and later proves a recovered one still runs inside it.
|
|
8
|
+
import { Code, ConfigError, ProtocolError } from "./errors.js";
|
|
9
|
+
import { diagnose, matches } from "./schema.js";
|
|
10
|
+
import { SandboxProviderAdoptParams, SandboxProviderPlanParams, } from "./wire.js";
|
|
11
|
+
/** What an interrupt of a sandboxed launch can promise (extension.SandboxTerm, SandboxKill). */
|
|
12
|
+
export const SandboxSignals = {
|
|
13
|
+
/** A termination signal sent to the executor's process group reaches the sandboxed command. */
|
|
14
|
+
Term: "term",
|
|
15
|
+
/** The sandboxed command runs outside the group, so only killing the wrapper stops it. */
|
|
16
|
+
Kill: "kill",
|
|
17
|
+
};
|
|
18
|
+
/** @internal */
|
|
19
|
+
export function sandboxHandler(type, spec, params) {
|
|
20
|
+
const schema = spec.config;
|
|
21
|
+
return {
|
|
22
|
+
validate: async (raw) => {
|
|
23
|
+
const c = raw ?? {};
|
|
24
|
+
if (schema !== undefined && !matches(schema, c)) {
|
|
25
|
+
const diagnostics = diagnose(schema, c);
|
|
26
|
+
if (diagnostics.length > 0)
|
|
27
|
+
throw new ConfigError(diagnostics);
|
|
28
|
+
throw new ProtocolError(Code.Invalid, "invalid sandbox config");
|
|
29
|
+
}
|
|
30
|
+
try {
|
|
31
|
+
await spec.validate?.(c);
|
|
32
|
+
}
|
|
33
|
+
catch (err) {
|
|
34
|
+
if (err instanceof ConfigError)
|
|
35
|
+
throw err;
|
|
36
|
+
throw new ProtocolError(Code.Invalid, "invalid sandbox config");
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
serve: async (method, raw, ctx) => {
|
|
40
|
+
switch (method) {
|
|
41
|
+
case "sandbox_provider.plan": {
|
|
42
|
+
const p = params(SandboxProviderPlanParams, raw);
|
|
43
|
+
const c = p.config.data ?? {};
|
|
44
|
+
// A stored binding passed validate; one whose config no longer
|
|
45
|
+
// matches (a package upgrade changed the schema) runs nothing.
|
|
46
|
+
if (schema !== undefined && !matches(schema, c))
|
|
47
|
+
throw new ProtocolError(Code.SandboxUnavailable, "the binding's sandbox config doesn't match the provider's");
|
|
48
|
+
const plan = await spec.plan({
|
|
49
|
+
type,
|
|
50
|
+
owner: p.owner,
|
|
51
|
+
config: c,
|
|
52
|
+
paths: p.paths,
|
|
53
|
+
needs: p.needs,
|
|
54
|
+
launch: p.launch,
|
|
55
|
+
}, ctx);
|
|
56
|
+
if (!Array.isArray(plan?.argv) || plan.argv.length === 0)
|
|
57
|
+
throw new ProtocolError(Code.SandboxUnavailable, "the sandbox provider planned no command");
|
|
58
|
+
const out = { argv: [...plan.argv] };
|
|
59
|
+
if (plan.env?.length)
|
|
60
|
+
out.env = [...plan.env];
|
|
61
|
+
if (plan.signals)
|
|
62
|
+
out.signals = plan.signals;
|
|
63
|
+
if (plan.summary)
|
|
64
|
+
out.summary = plan.summary;
|
|
65
|
+
if (plan.data !== undefined)
|
|
66
|
+
out.data = plan.data;
|
|
67
|
+
if (plan.dir)
|
|
68
|
+
out.dir = plan.dir;
|
|
69
|
+
return out;
|
|
70
|
+
}
|
|
71
|
+
case "sandbox_provider.adopt": {
|
|
72
|
+
const p = params(SandboxProviderAdoptParams, raw);
|
|
73
|
+
await spec.adopt({
|
|
74
|
+
type,
|
|
75
|
+
plan: { ...p.plan, argv: p.plan.argv ?? [] },
|
|
76
|
+
parent: p.parent,
|
|
77
|
+
root: p.root,
|
|
78
|
+
}, ctx);
|
|
79
|
+
return {};
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
throw new ProtocolError(Code.MethodNotFound, `no such method ${method}`);
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
}
|
package/dist/schema.d.ts
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import type { ConfigDiagnostic } from "./errors.js";
|
|
2
|
+
declare const phantom: unique symbol;
|
|
3
|
+
declare const optionalMark: unique symbol;
|
|
4
|
+
/** A JSON Schema that accepts values of type T. */
|
|
5
|
+
export type Type<T> = Readonly<Record<string, unknown>> & {
|
|
6
|
+
readonly [phantom]?: T;
|
|
7
|
+
};
|
|
8
|
+
/** The type of the values a schema accepts. */
|
|
9
|
+
export type Infer<S> = S extends Type<infer T> ? T : never;
|
|
10
|
+
type Optional<T> = Type<T> & {
|
|
11
|
+
readonly [optionalMark]: true;
|
|
12
|
+
};
|
|
13
|
+
type Shape = Record<string, Type<unknown>>;
|
|
14
|
+
type RequiredKeys<S extends Shape> = {
|
|
15
|
+
[K in keyof S]: S[K] extends {
|
|
16
|
+
readonly [optionalMark]: true;
|
|
17
|
+
} ? never : K;
|
|
18
|
+
}[keyof S];
|
|
19
|
+
type Flatten<T> = {
|
|
20
|
+
[K in keyof T]: T[K];
|
|
21
|
+
} & {};
|
|
22
|
+
type ObjectOf<S extends Shape> = Flatten<{
|
|
23
|
+
[K in RequiredKeys<S>]: Infer<S[K]>;
|
|
24
|
+
} & {
|
|
25
|
+
[K in Exclude<keyof S, RequiredKeys<S>>]?: Infer<S[K]>;
|
|
26
|
+
}>;
|
|
27
|
+
interface Doc {
|
|
28
|
+
/** Becomes the schema's description, which an agent reads in its toolbelt. */
|
|
29
|
+
description?: string;
|
|
30
|
+
}
|
|
31
|
+
interface ObjectOptions extends Doc {
|
|
32
|
+
/** Accept fields the shape doesn't name, and ignore them. */
|
|
33
|
+
open?: boolean;
|
|
34
|
+
}
|
|
35
|
+
export declare const t: {
|
|
36
|
+
string(o?: Doc & {
|
|
37
|
+
format?: string;
|
|
38
|
+
}): Type<string>;
|
|
39
|
+
/** One of the given strings. */
|
|
40
|
+
enum<const V extends readonly [string, ...string[]]>(values: V, o?: Doc): Type<V[number]>;
|
|
41
|
+
integer(o?: Doc): Type<number>;
|
|
42
|
+
number(o?: Doc): Type<number>;
|
|
43
|
+
boolean(o?: Doc): Type<boolean>;
|
|
44
|
+
/** Exactly this value: tells apart the members of a union. */
|
|
45
|
+
literal<const V extends string | number | boolean>(value: V, o?: Doc): Type<V>;
|
|
46
|
+
array<T>(items: Type<T>, o?: Doc): Type<T[]>;
|
|
47
|
+
/** An object of these fields; each is required unless t.optional. */
|
|
48
|
+
object<S extends Shape>(shape: S, o?: ObjectOptions): Type<ObjectOf<S>>;
|
|
49
|
+
/** An object with any keys, each holding a value of one schema. */
|
|
50
|
+
record<T>(values: Type<T>, o?: Doc): Type<Record<string, T>>;
|
|
51
|
+
/** A field that may be left out. */
|
|
52
|
+
optional<T>(inner: Type<T>): Optional<T>;
|
|
53
|
+
/** inner, or null (Go sends a nil slice or map as null). */
|
|
54
|
+
nullable<T>(inner: Type<T>): Type<T | null>;
|
|
55
|
+
/** Any JSON value, passed through as it is. */
|
|
56
|
+
any(o?: Doc): Type<unknown>;
|
|
57
|
+
};
|
|
58
|
+
/** Reports whether v is a JSON object (not null, not an array). */
|
|
59
|
+
export declare function isRecord(v: unknown): v is Record<string, unknown>;
|
|
60
|
+
/** Reports whether value matches schema, narrowing it to the schema's type. */
|
|
61
|
+
export declare function matches<S extends Type<unknown>>(schema: S, value: unknown): value is Infer<S>;
|
|
62
|
+
/** Why value does not match schema, or undefined when it does. */
|
|
63
|
+
export declare function check(schema: Readonly<Record<string, unknown>>, value: unknown, path?: string): string | undefined;
|
|
64
|
+
/**
|
|
65
|
+
* What about value does not match schema, as config diagnostics: each at
|
|
66
|
+
* its field's JSON pointer, with code invalid_type, invalid_value,
|
|
67
|
+
* missing_field or unknown_field. Empty when it matches. Messages never
|
|
68
|
+
* quote the value.
|
|
69
|
+
*/
|
|
70
|
+
export declare function diagnose(schema: Readonly<Record<string, unknown>>, value: unknown, path?: string): ConfigDiagnostic[];
|
|
71
|
+
export {};
|
package/dist/schema.js
ADDED
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
// Schemas that are also types. `t` builds a JSON Schema, and Infer gives the
|
|
2
|
+
// TypeScript type of the values it accepts, so a declaration and its type
|
|
3
|
+
// cannot disagree. The SDK decodes everything that crosses the wire through
|
|
4
|
+
// one: an operation's input (strictly, as the Go SDK does), an entry's
|
|
5
|
+
// config, and the host's own messages (openly, so a newer host's extra
|
|
6
|
+
// fields are ignored rather than refused).
|
|
7
|
+
// Symbol keys never reach JSON, so the marks stay out of the manifest.
|
|
8
|
+
const optionalMark = Symbol("optional");
|
|
9
|
+
function withDoc(schema, o) {
|
|
10
|
+
return o?.description === undefined
|
|
11
|
+
? schema
|
|
12
|
+
: { ...schema, description: o.description };
|
|
13
|
+
}
|
|
14
|
+
function isOptional(s) {
|
|
15
|
+
return optionalMark in s;
|
|
16
|
+
}
|
|
17
|
+
export const t = {
|
|
18
|
+
string(o) {
|
|
19
|
+
return withDoc(o?.format === undefined
|
|
20
|
+
? { type: "string" }
|
|
21
|
+
: { type: "string", format: o.format }, o);
|
|
22
|
+
},
|
|
23
|
+
/** One of the given strings. */
|
|
24
|
+
enum(values, o) {
|
|
25
|
+
return withDoc({ type: "string", enum: [...values] }, o);
|
|
26
|
+
},
|
|
27
|
+
integer(o) {
|
|
28
|
+
return withDoc({ type: "integer" }, o);
|
|
29
|
+
},
|
|
30
|
+
number(o) {
|
|
31
|
+
return withDoc({ type: "number" }, o);
|
|
32
|
+
},
|
|
33
|
+
boolean(o) {
|
|
34
|
+
return withDoc({ type: "boolean" }, o);
|
|
35
|
+
},
|
|
36
|
+
/** Exactly this value: tells apart the members of a union. */
|
|
37
|
+
literal(value, o) {
|
|
38
|
+
return withDoc({ const: value }, o);
|
|
39
|
+
},
|
|
40
|
+
array(items, o) {
|
|
41
|
+
return withDoc({ type: "array", items }, o);
|
|
42
|
+
},
|
|
43
|
+
/** An object of these fields; each is required unless t.optional. */
|
|
44
|
+
object(shape, o) {
|
|
45
|
+
const required = Object.keys(shape).filter((k) => {
|
|
46
|
+
const field = shape[k];
|
|
47
|
+
return field !== undefined && !isOptional(field);
|
|
48
|
+
});
|
|
49
|
+
const s = { type: "object", properties: shape };
|
|
50
|
+
if (!o?.open)
|
|
51
|
+
s.additionalProperties = false;
|
|
52
|
+
if (required.length > 0)
|
|
53
|
+
s.required = required;
|
|
54
|
+
return withDoc(s, o);
|
|
55
|
+
},
|
|
56
|
+
/** An object with any keys, each holding a value of one schema. */
|
|
57
|
+
record(values, o) {
|
|
58
|
+
return withDoc({ type: "object", additionalProperties: values }, o);
|
|
59
|
+
},
|
|
60
|
+
/** A field that may be left out. */
|
|
61
|
+
optional(inner) {
|
|
62
|
+
return { ...inner, [optionalMark]: true };
|
|
63
|
+
},
|
|
64
|
+
/** inner, or null (Go sends a nil slice or map as null). */
|
|
65
|
+
nullable(inner) {
|
|
66
|
+
return { anyOf: [inner, { type: "null" }] };
|
|
67
|
+
},
|
|
68
|
+
/** Any JSON value, passed through as it is. */
|
|
69
|
+
any(o) {
|
|
70
|
+
return withDoc({}, o);
|
|
71
|
+
},
|
|
72
|
+
};
|
|
73
|
+
/** Reports whether v is a JSON object (not null, not an array). */
|
|
74
|
+
export function isRecord(v) {
|
|
75
|
+
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
76
|
+
}
|
|
77
|
+
/** Reports whether value matches schema, narrowing it to the schema's type. */
|
|
78
|
+
export function matches(schema, value) {
|
|
79
|
+
return check(schema, value) === undefined;
|
|
80
|
+
}
|
|
81
|
+
/** Why value does not match schema, or undefined when it does. */
|
|
82
|
+
export function check(schema, value, path = "") {
|
|
83
|
+
const field = path === "" ? "the input" : `field "${path}"`;
|
|
84
|
+
const wrongType = `${field} has the wrong type (got a JSON ${jsonKind(value)})`;
|
|
85
|
+
if (Array.isArray(schema.anyOf)) {
|
|
86
|
+
const options = schema.anyOf.filter(isRecord);
|
|
87
|
+
return options.some((s) => check(s, value, path) === undefined)
|
|
88
|
+
? undefined
|
|
89
|
+
: wrongType;
|
|
90
|
+
}
|
|
91
|
+
if ("const" in schema && value !== schema.const)
|
|
92
|
+
return `${field} must be ${JSON.stringify(schema.const)}`;
|
|
93
|
+
switch (schema.type) {
|
|
94
|
+
case "null":
|
|
95
|
+
return value === null ? undefined : wrongType;
|
|
96
|
+
case "string":
|
|
97
|
+
if (typeof value !== "string")
|
|
98
|
+
return wrongType;
|
|
99
|
+
if (Array.isArray(schema.enum) && !schema.enum.includes(value))
|
|
100
|
+
return `${field} must be one of ${schema.enum.map((v) => JSON.stringify(v)).join(", ")}`;
|
|
101
|
+
return undefined;
|
|
102
|
+
case "integer":
|
|
103
|
+
return typeof value === "number" && Number.isInteger(value)
|
|
104
|
+
? undefined
|
|
105
|
+
: wrongType;
|
|
106
|
+
case "number":
|
|
107
|
+
return typeof value === "number" ? undefined : wrongType;
|
|
108
|
+
case "boolean":
|
|
109
|
+
return typeof value === "boolean" ? undefined : wrongType;
|
|
110
|
+
case "array": {
|
|
111
|
+
if (!Array.isArray(value))
|
|
112
|
+
return wrongType;
|
|
113
|
+
const items = isRecord(schema.items) ? schema.items : {};
|
|
114
|
+
for (const [i, item] of value.entries()) {
|
|
115
|
+
const why = check(items, item, `${path}[${i}]`);
|
|
116
|
+
if (why !== undefined)
|
|
117
|
+
return why;
|
|
118
|
+
}
|
|
119
|
+
return undefined;
|
|
120
|
+
}
|
|
121
|
+
case "object":
|
|
122
|
+
return checkObject(schema, value, path, wrongType);
|
|
123
|
+
default:
|
|
124
|
+
return undefined;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
function checkObject(schema, value, path, wrongType) {
|
|
128
|
+
if (!isRecord(value))
|
|
129
|
+
return path === "" ? "the input must be a JSON object" : wrongType;
|
|
130
|
+
const props = isRecord(schema.properties) ? schema.properties : {};
|
|
131
|
+
const required = Array.isArray(schema.required) ? schema.required : [];
|
|
132
|
+
for (const k of required) {
|
|
133
|
+
if (typeof k === "string" && !(k in value))
|
|
134
|
+
return `missing field "${join(path, k)}"`;
|
|
135
|
+
}
|
|
136
|
+
for (const [k, v] of Object.entries(value)) {
|
|
137
|
+
const p = props[k];
|
|
138
|
+
if (isRecord(p)) {
|
|
139
|
+
const why = check(p, v, join(path, k));
|
|
140
|
+
if (why !== undefined)
|
|
141
|
+
return why;
|
|
142
|
+
}
|
|
143
|
+
else if (schema.additionalProperties === false) {
|
|
144
|
+
return `unknown field "${join(path, k)}"`;
|
|
145
|
+
}
|
|
146
|
+
else if (isRecord(schema.additionalProperties)) {
|
|
147
|
+
const why = check(schema.additionalProperties, v, join(path, k));
|
|
148
|
+
if (why !== undefined)
|
|
149
|
+
return why;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return undefined;
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* What about value does not match schema, as config diagnostics: each at
|
|
156
|
+
* its field's JSON pointer, with code invalid_type, invalid_value,
|
|
157
|
+
* missing_field or unknown_field. Empty when it matches. Messages never
|
|
158
|
+
* quote the value.
|
|
159
|
+
*/
|
|
160
|
+
export function diagnose(schema, value, path = "") {
|
|
161
|
+
const wrong = (what) => [
|
|
162
|
+
{ path, code: "invalid_type", message: `must be ${what}` },
|
|
163
|
+
];
|
|
164
|
+
if (Array.isArray(schema.anyOf)) {
|
|
165
|
+
const options = schema.anyOf.filter(isRecord);
|
|
166
|
+
return options.some((s) => check(s, value) === undefined)
|
|
167
|
+
? []
|
|
168
|
+
: [{ path, code: "invalid_type", message: "has the wrong type" }];
|
|
169
|
+
}
|
|
170
|
+
if ("const" in schema && value !== schema.const)
|
|
171
|
+
return [
|
|
172
|
+
{
|
|
173
|
+
path,
|
|
174
|
+
code: "invalid_value",
|
|
175
|
+
message: `must be ${JSON.stringify(schema.const)}`,
|
|
176
|
+
},
|
|
177
|
+
];
|
|
178
|
+
switch (schema.type) {
|
|
179
|
+
case "null":
|
|
180
|
+
return value === null ? [] : wrong("null");
|
|
181
|
+
case "string":
|
|
182
|
+
if (typeof value !== "string")
|
|
183
|
+
return wrong("a string");
|
|
184
|
+
if (Array.isArray(schema.enum) && !schema.enum.includes(value))
|
|
185
|
+
return [
|
|
186
|
+
{
|
|
187
|
+
path,
|
|
188
|
+
code: "invalid_value",
|
|
189
|
+
message: `must be one of ${schema.enum.map((v) => JSON.stringify(v)).join(", ")}`,
|
|
190
|
+
},
|
|
191
|
+
];
|
|
192
|
+
return [];
|
|
193
|
+
case "integer":
|
|
194
|
+
return typeof value === "number" && Number.isInteger(value)
|
|
195
|
+
? []
|
|
196
|
+
: wrong("a whole number");
|
|
197
|
+
case "number":
|
|
198
|
+
return typeof value === "number" ? [] : wrong("a number");
|
|
199
|
+
case "boolean":
|
|
200
|
+
return typeof value === "boolean" ? [] : wrong("true or false");
|
|
201
|
+
case "array": {
|
|
202
|
+
if (!Array.isArray(value))
|
|
203
|
+
return wrong("an array");
|
|
204
|
+
const items = isRecord(schema.items) ? schema.items : {};
|
|
205
|
+
return value.flatMap((item, i) => diagnose(items, item, pointer(path, i)));
|
|
206
|
+
}
|
|
207
|
+
case "object": {
|
|
208
|
+
if (!isRecord(value))
|
|
209
|
+
return wrong("an object");
|
|
210
|
+
const props = isRecord(schema.properties) ? schema.properties : {};
|
|
211
|
+
const required = Array.isArray(schema.required) ? schema.required : [];
|
|
212
|
+
const out = [];
|
|
213
|
+
for (const k of required) {
|
|
214
|
+
if (typeof k === "string" && !(k in value))
|
|
215
|
+
out.push({
|
|
216
|
+
path: pointer(path, k),
|
|
217
|
+
code: "missing_field",
|
|
218
|
+
message: "required field is missing",
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
for (const [k, v] of Object.entries(value)) {
|
|
222
|
+
const p = props[k];
|
|
223
|
+
if (isRecord(p))
|
|
224
|
+
out.push(...diagnose(p, v, pointer(path, k)));
|
|
225
|
+
else if (schema.additionalProperties === false)
|
|
226
|
+
out.push({
|
|
227
|
+
path: pointer(path, k),
|
|
228
|
+
code: "unknown_field",
|
|
229
|
+
message: `unknown field; allowed fields are ${Object.keys(props).join(", ")}`,
|
|
230
|
+
});
|
|
231
|
+
else if (isRecord(schema.additionalProperties))
|
|
232
|
+
out.push(...diagnose(schema.additionalProperties, v, pointer(path, k)));
|
|
233
|
+
}
|
|
234
|
+
return out;
|
|
235
|
+
}
|
|
236
|
+
default:
|
|
237
|
+
return [];
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
function pointer(path, key) {
|
|
241
|
+
return `${path}/${String(key).replaceAll("~", "~0").replaceAll("/", "~1")}`;
|
|
242
|
+
}
|
|
243
|
+
function join(path, key) {
|
|
244
|
+
return path === "" ? key : `${path}.${key}`;
|
|
245
|
+
}
|
|
246
|
+
function jsonKind(v) {
|
|
247
|
+
if (v === null)
|
|
248
|
+
return "null";
|
|
249
|
+
if (Array.isArray(v))
|
|
250
|
+
return "array";
|
|
251
|
+
return typeof v === "object" ? "object" : typeof v;
|
|
252
|
+
}
|
package/dist/setup.d.ts
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { ProtocolError } from "./errors.js";
|
|
2
|
+
import type { Context } from "./extension.js";
|
|
3
|
+
import type { Host } from "./host.js";
|
|
4
|
+
import { type ConfigCandidateResult, type SetupRunParams, type SetupRunResult, type Work } from "./wire.js";
|
|
5
|
+
/** The change states core knows (extension.ChangeProposed, ChangeApproved); others (rejected, withdrawn, applied) are shown as they are. */
|
|
6
|
+
export declare const ChangeState: {
|
|
7
|
+
/** Waiting on the operator. */
|
|
8
|
+
readonly Proposed: "proposed";
|
|
9
|
+
/** Approved and waiting to be applied. */
|
|
10
|
+
readonly Approved: "approved";
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* One proposed change (extension.CandidateChange): an edit at a JSON
|
|
14
|
+
* pointer of the config file, with its reason, the redacted value before
|
|
15
|
+
* and after (undefined when there is none: an add, a remove) and its
|
|
16
|
+
* problems. approve and reject are the IDs of the extension's actions that
|
|
17
|
+
* decide it, when offered now; reject takes an optional reason as input.
|
|
18
|
+
*/
|
|
19
|
+
export interface CandidateChange {
|
|
20
|
+
id: string;
|
|
21
|
+
op: string;
|
|
22
|
+
pointer: string;
|
|
23
|
+
state: string;
|
|
24
|
+
why?: string;
|
|
25
|
+
before?: unknown;
|
|
26
|
+
after?: unknown;
|
|
27
|
+
problems?: string[];
|
|
28
|
+
approve?: string;
|
|
29
|
+
reject?: string;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* The configuration a Work proposes (extension.Candidate). handle is one
|
|
33
|
+
* the host gave the extension (host.configBase, host.configEdit) on the
|
|
34
|
+
* config file text with every approved change made, baseGeneration the
|
|
35
|
+
* generation it is based on and hash its hash (host.configValidate). The
|
|
36
|
+
* host serves the text behind it to the operator alone, who applies it;
|
|
37
|
+
* nothing else can. Leave handle out while nothing is approved: there is
|
|
38
|
+
* nothing to apply yet. approveAll and validate are the IDs of the actions
|
|
39
|
+
* that approve every open change and validate the approved ones, when
|
|
40
|
+
* offered now.
|
|
41
|
+
*/
|
|
42
|
+
export interface Candidate {
|
|
43
|
+
handle?: string;
|
|
44
|
+
baseGeneration?: string;
|
|
45
|
+
hash?: string;
|
|
46
|
+
/** Every change proposed on the Work, in proposal order. */
|
|
47
|
+
changes?: CandidateChange[];
|
|
48
|
+
approveAll?: string;
|
|
49
|
+
validate?: string;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Says what a Work proposes (extension.ConfigCandidate, manifest
|
|
53
|
+
* config_candidate): undefined for a Work with nothing proposed. The host
|
|
54
|
+
* asks only an extension with the config.read permission, which
|
|
55
|
+
* registering one asks for, about Work of a kind it owns, and bounds the
|
|
56
|
+
* call to five seconds, asking again after a KV conflict.
|
|
57
|
+
*/
|
|
58
|
+
export type ConfigCandidateFn = (work: Work, ctx: Context) => Candidate | undefined | Promise<Candidate | undefined>;
|
|
59
|
+
/** c on the wire (protocol.FromCandidate); undefined is a Work with nothing proposed. */
|
|
60
|
+
export declare function candidateResult(c: Candidate | undefined): ConfigCandidateResult;
|
|
61
|
+
/** The candidate a config.candidate answer carries (ConfigCandidateResult.Candidate), or undefined when it has none. */
|
|
62
|
+
export declare function candidateOf(r: ConfigCandidateResult): Candidate | undefined;
|
|
63
|
+
/**
|
|
64
|
+
* Thrown by a SetupHost's prompt or choose when the session takes no
|
|
65
|
+
* interactive input (extension.ErrSetupNoInput): a flow run with flags
|
|
66
|
+
* alone, say. Fall back to a default or ask for a flag.
|
|
67
|
+
*/
|
|
68
|
+
export declare class SetupNoInput extends Error {
|
|
69
|
+
constructor();
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* A setup flow's usage error (extension.ErrSetupUsage): the arguments it
|
|
73
|
+
* was given don't make sense. It crosses as invalid with its message,
|
|
74
|
+
* which the operator reads with the flow's usage, so it must be fixed text
|
|
75
|
+
* with no secret in it.
|
|
76
|
+
*/
|
|
77
|
+
export declare class SetupUsageError extends ProtocolError {
|
|
78
|
+
constructor(message: string);
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* What a setup flow drives the operator through (extension.SetupHost),
|
|
82
|
+
* each call one of the host's setup.* callbacks for this run. The host
|
|
83
|
+
* answers them only while the run lasts.
|
|
84
|
+
*/
|
|
85
|
+
export interface SetupHost {
|
|
86
|
+
/** The existing config document, when there is one, so a flow can reuse connections or pick a free scope ID. */
|
|
87
|
+
readonly config: Uint8Array | undefined;
|
|
88
|
+
/** The operator's working directory. */
|
|
89
|
+
readonly cwd: string;
|
|
90
|
+
/** Open Conduit's private state directory, for files a flow must prepare. */
|
|
91
|
+
readonly stateDir: string;
|
|
92
|
+
/** `--dry-run`: no secrets are stored and nothing is saved. */
|
|
93
|
+
readonly dryRun: boolean;
|
|
94
|
+
/** Shows the operator a line of text. A failure to show it is ignored, as the Go SDK ignores it. */
|
|
95
|
+
print(text: string): Promise<void>;
|
|
96
|
+
/** Asks a question, answering default when the operator just presses enter; secret hides what they type. Throws SetupNoInput without interactive input. */
|
|
97
|
+
prompt(question: string, o?: {
|
|
98
|
+
default?: string;
|
|
99
|
+
secret?: boolean;
|
|
100
|
+
}): Promise<string>;
|
|
101
|
+
/** Asks the operator to choose one of options, answering its index. Throws SetupNoInput without interactive input. */
|
|
102
|
+
choose(question: string, options: string[]): Promise<number>;
|
|
103
|
+
/** Opens a URL in the operator's browser, or shows it. */
|
|
104
|
+
openURL(url: string): Promise<void>;
|
|
105
|
+
/** Stores a secret through the host's secret store (keyring, else the owner-only secrets file) and answers its reference, such as "keyring:bot-token", for the config to name rather than the value. In a dry run it stores nothing and answers the reference it would use. */
|
|
106
|
+
storeSecret(name: string, value: string): Promise<string>;
|
|
107
|
+
/** Finds a program on the operator's PATH: its path, or undefined. */
|
|
108
|
+
lookPath(name: string): Promise<string | undefined>;
|
|
109
|
+
/** Runs a command attached to the operator's terminal in dir. */
|
|
110
|
+
run(dir: string, name: string, args?: string[], signal?: AbortSignal): Promise<void>;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* What a setup flow found (extension.SetupResult), which the host merges
|
|
114
|
+
* into the config: each connection is added when none with its id exists
|
|
115
|
+
* (an existing one is kept), and the scope replaces one with the same id
|
|
116
|
+
* only with --force. summary lines are printed once the config is saved,
|
|
117
|
+
* and next lists follow-up commands.
|
|
118
|
+
*/
|
|
119
|
+
export interface SetupResult {
|
|
120
|
+
connections?: unknown[];
|
|
121
|
+
scope?: unknown;
|
|
122
|
+
summary?: string[];
|
|
123
|
+
next?: string[];
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* An `open-conduit init <type>` flow (extension.Setup, manifest setup).
|
|
127
|
+
* type is the setup type, summary and usage what `init` lists, and
|
|
128
|
+
* firstRun marks the setup whose result, with no flags, is the
|
|
129
|
+
* configuration a daemon with no config file and no stored generation
|
|
130
|
+
* runs on. run gets the flow's arguments and the SetupHost of the run.
|
|
131
|
+
* Throw SetupUsageError for arguments that don't make sense; any other
|
|
132
|
+
* ProtocolError crosses with its code and message, and anything else
|
|
133
|
+
* thrown as a fixed internal error.
|
|
134
|
+
*/
|
|
135
|
+
export interface SetupSpec {
|
|
136
|
+
type: string;
|
|
137
|
+
summary: string;
|
|
138
|
+
usage?: string;
|
|
139
|
+
firstRun?: boolean;
|
|
140
|
+
run: (args: string[], host: SetupHost, ctx: Context) => SetupResult | Promise<SetupResult>;
|
|
141
|
+
}
|
|
142
|
+
/** The SetupHost over the setup.* callbacks of run p. */
|
|
143
|
+
export declare function setupHost(host: Host, p: SetupRunParams, signal: AbortSignal): SetupHost;
|
|
144
|
+
/** r on the wire (protocol.SetupRunResult). */
|
|
145
|
+
export declare function setupResult(r: SetupResult): SetupRunResult;
|