@open-artifact/host-plugin 0.0.0-stage → 0.14.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 +202 -0
- package/NOTICE +5 -0
- package/README.md +191 -2
- package/package.json +42 -4
- package/src/contract.d.ts +264 -0
- package/src/contract.js +1000 -0
- package/src/index.d.ts +832 -0
- package/src/index.js +338 -0
- package/src/testing.d.ts +169 -0
- package/src/testing.js +364 -0
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@open-artifact/host-plugin/contract`: the rules of the plugin API that the host and the test kit share, so a plugin
|
|
3
|
+
* that passes its tests under the kit loads and behaves the same on the host.
|
|
4
|
+
*
|
|
5
|
+
* - {@link checkPlugin}: what a plugin object must look like (its `id`, `apiVersion`, hooks), what is reported and
|
|
6
|
+
* ignored (unknown keys and hooks), and what refuses it.
|
|
7
|
+
* - {@link validateConfig}: the operator's settings against the plugin's schema, `{"env": "NAME"}` references
|
|
8
|
+
* resolved, defaults filled in, secrets never echoed.
|
|
9
|
+
* - The answer checks of every hook (`check*`): an answer of the wrong shape counts as a failure.
|
|
10
|
+
* - {@link withTimeLimit}: one hook call under its time limit.
|
|
11
|
+
* - The key-value store's limits ({@link KV_LIMITS}), route path matching and the same-origin rule for writes.
|
|
12
|
+
*
|
|
13
|
+
* Plugin authors do not need this module; it is public so the rules are written down in one place.
|
|
14
|
+
*/
|
|
15
|
+
import { type ArtifactRole, type ConfigSchema, type GuardAction, type HostPlugin, type Identity, type PluginEventName, type PluginUi } from './index.ts';
|
|
16
|
+
/** The plugin API versions this release runs (the current one first). A version here but not current runs through an adapter. */
|
|
17
|
+
export declare const SUPPORTED_API_VERSIONS: readonly number[];
|
|
18
|
+
/** Every event of API version 1, in the order the documentation lists them. */
|
|
19
|
+
export declare const EVENT_NAMES: readonly PluginEventName[];
|
|
20
|
+
/** Every guarded action of API version 1. */
|
|
21
|
+
export declare const GUARD_ACTIONS: readonly GuardAction[];
|
|
22
|
+
/** The hooks of API version 1 besides the event handlers (`on.<event>`). */
|
|
23
|
+
export declare const HOOK_NAMES: readonly ["auth.authenticate", "auth.signIn", "auth.identity", "auth.signOutUrl", "authz.role", "authz.artifacts", "guard", "wake.deliver", "routes", "ui"];
|
|
24
|
+
/** A hook's name: one of {@link HOOK_NAMES} or `on.<event>`. */
|
|
25
|
+
export type HookName = (typeof HOOK_NAMES)[number] | `on.${PluginEventName}`;
|
|
26
|
+
/** The members of `ctx` (for `ctx.host.supports('ctx.kv')`). */
|
|
27
|
+
export declare const CONTEXT_FEATURES: readonly string[];
|
|
28
|
+
/** `ctx.host.artifacts.list`: the page size unless the plugin asks for another, and the most one call answers. */
|
|
29
|
+
export declare const ARTIFACT_LIST_DEFAULT = 50;
|
|
30
|
+
export declare const ARTIFACT_LIST_MAX = 200;
|
|
31
|
+
/** `ctx.host.artifacts.list`'s options checked: a TypeError for a wrong one, the limit clamped to the cap. */
|
|
32
|
+
export declare function artifactListOptions(options: unknown): {
|
|
33
|
+
owner?: string;
|
|
34
|
+
after?: string;
|
|
35
|
+
limit: number;
|
|
36
|
+
};
|
|
37
|
+
/** Everything `ctx.host.supports` answers true for on a host that runs all of API version 1. */
|
|
38
|
+
export declare const ALL_FEATURES: readonly string[];
|
|
39
|
+
/** Which kind of failure policy a hook has (decision: sign-in, permission and guard hooks fail closed). */
|
|
40
|
+
export declare function failsClosed(hook: string): boolean;
|
|
41
|
+
/** Each hook call's time limit unless the operator sets `timeoutMs`, ms. */
|
|
42
|
+
export declare const DEFAULT_TIMEOUT_MS = 5000;
|
|
43
|
+
/** The longest `timeoutMs` an operator may set, ms. */
|
|
44
|
+
export declare const MAX_TIMEOUT_MS = 60000;
|
|
45
|
+
/** How long the host reuses a plugin's role answers unless it sets `authz.cacheSeconds`. */
|
|
46
|
+
export declare const DEFAULT_CACHE_SECONDS = 30;
|
|
47
|
+
/** The longest `authz.cacheSeconds` the host honours (a day). */
|
|
48
|
+
export declare const MAX_CACHE_SECONDS = 86400;
|
|
49
|
+
/**
|
|
50
|
+
* The key-value store's limits, per plugin. Keys up to 512 bytes (room for a prefix and an id or URL); values up to
|
|
51
|
+
* 64 KiB of JSON (a cached answer or a queued webhook, well under every database's row limit, D1's 1 MB included);
|
|
52
|
+
* 10,000 live keys (bounds what one plugin can put in the site's catalog: at most ~640 MB in the worst case, and
|
|
53
|
+
* typically far less); a listing returns at most 1000 entries at a time.
|
|
54
|
+
*/
|
|
55
|
+
export declare const KV_LIMITS: {
|
|
56
|
+
readonly maxKeyBytes: 512;
|
|
57
|
+
readonly maxValueBytes: number;
|
|
58
|
+
readonly maxKeys: 10000;
|
|
59
|
+
readonly maxList: 1000;
|
|
60
|
+
readonly defaultList: 100;
|
|
61
|
+
};
|
|
62
|
+
/** Lower-case letters, digits and single `-` between them; at most 64 characters. */
|
|
63
|
+
export declare const PLUGIN_ID: RegExp;
|
|
64
|
+
export declare const MAX_PLUGIN_ID = 64;
|
|
65
|
+
/** ` Did you mean "x"?` when a known name is close to `name`, else ``. */
|
|
66
|
+
export declare function didYouMean(name: string, known: readonly string[]): string;
|
|
67
|
+
/** What a value is, for a message, without showing it. */
|
|
68
|
+
export declare function kindOf(value: unknown): string;
|
|
69
|
+
/**
|
|
70
|
+
* Why a plugin's `apiVersion` keeps it from running here, or null when it may run. The messages say what to do:
|
|
71
|
+
* update the host for a newer version, update the plugin for one this host no longer runs.
|
|
72
|
+
*/
|
|
73
|
+
export declare function apiVersionProblem(label: string, version: unknown, hostVersion?: string): string | null;
|
|
74
|
+
/** A route, checked: its method, path segments and whether it is public. */
|
|
75
|
+
export interface CheckedRoute {
|
|
76
|
+
method: string;
|
|
77
|
+
path: string;
|
|
78
|
+
segments: string[];
|
|
79
|
+
public: boolean;
|
|
80
|
+
/** Its index in the plugin's `routes`. */
|
|
81
|
+
index: number;
|
|
82
|
+
}
|
|
83
|
+
/** A plugin object, checked: what the host and the test kit run. */
|
|
84
|
+
export interface CheckedPlugin {
|
|
85
|
+
id: string;
|
|
86
|
+
apiVersion: number;
|
|
87
|
+
name: string;
|
|
88
|
+
version: string | null;
|
|
89
|
+
description: string | null;
|
|
90
|
+
/** The object itself: hooks are called on it (`plugin.auth.authenticate(…)`, so `this` works in methods). */
|
|
91
|
+
source: HostPlugin;
|
|
92
|
+
schema: ConfigSchema;
|
|
93
|
+
/** The hooks it has, by {@link HookName}. */
|
|
94
|
+
hooks: HookName[];
|
|
95
|
+
cacheSeconds: number;
|
|
96
|
+
/** Whether a session `auth.authenticate` made is asked again on each request (`auth.recheck`, default true). */
|
|
97
|
+
recheck: boolean;
|
|
98
|
+
routes: CheckedRoute[];
|
|
99
|
+
ui: {
|
|
100
|
+
links: NonNullable<PluginUi['links']>;
|
|
101
|
+
brand: NonNullable<PluginUi['brand']> | null;
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
/** {@link checkPlugin}'s answer: the plugin when it can run, why not (`errors`), and what was ignored (`warnings`). */
|
|
105
|
+
export interface PluginCheck {
|
|
106
|
+
plugin: CheckedPlugin | null;
|
|
107
|
+
errors: string[];
|
|
108
|
+
warnings: string[];
|
|
109
|
+
}
|
|
110
|
+
/** Whether `href` is a link the host may show: an `https:`/`http:` URL, or a path on the site. */
|
|
111
|
+
export declare function isSafeHref(href: unknown): href is string;
|
|
112
|
+
/**
|
|
113
|
+
* Checks a plugin object (a module's default export). `label` names it in messages (the `use` of its entry).
|
|
114
|
+
*
|
|
115
|
+
* Refused (`errors`): not an object; a bad `id`; an `apiVersion` this host does not run; a hook that is not a
|
|
116
|
+
* function where one must be; a bad config schema, route or UI link. Reported once and ignored (`warnings`): keys
|
|
117
|
+
* and hooks this host does not know, at any level, and events it does not know under `on`.
|
|
118
|
+
*/
|
|
119
|
+
export declare function checkPlugin(value: unknown, label: string, options?: {
|
|
120
|
+
hostVersion?: string;
|
|
121
|
+
}): PluginCheck;
|
|
122
|
+
/** {@link validateConfig}'s answer. `secrets` are the secret values in `config`, for the host to blank out of its log. */
|
|
123
|
+
export interface ConfigCheck {
|
|
124
|
+
config: Record<string, unknown>;
|
|
125
|
+
errors: string[];
|
|
126
|
+
warnings: string[];
|
|
127
|
+
secrets: string[];
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* The operator's settings for a plugin, checked against its schema:
|
|
131
|
+
*
|
|
132
|
+
* - every value of the right type (a `{"env": "NAME"}` value is read from `env` and parsed: numbers, `true`/`false`,
|
|
133
|
+
* a JSON list or a comma-separated one); a variable that is not set (or empty) is an error naming the plugin, the
|
|
134
|
+
* setting and the variable;
|
|
135
|
+
* - required settings present (an empty string does not count); defaults filled in;
|
|
136
|
+
* - a setting the schema does not have is an error (a typo must not pass silently), with a suggestion;
|
|
137
|
+
* - a `secret` written into the configuration instead of `{"env": …}` is a warning.
|
|
138
|
+
*
|
|
139
|
+
* No message ever shows a secret's value, nor the value of a setting it does not know (it may be a mistyped secret).
|
|
140
|
+
*/
|
|
141
|
+
export declare function validateConfig(pluginId: string, schema: ConfigSchema, values: unknown, env?: Readonly<Record<string, string | undefined>>): ConfigCheck;
|
|
142
|
+
/** Replaces every secret in `text` with `[secret]`. */
|
|
143
|
+
export declare function redact(text: string, secrets: readonly string[]): string;
|
|
144
|
+
/** Whether a value is an artifact role. */
|
|
145
|
+
export declare function isArtifactRole(value: unknown): value is ArtifactRole;
|
|
146
|
+
/** An identity a plugin answered, cleaned (unknown fields dropped), or null when it is not one. */
|
|
147
|
+
export declare function checkIdentity(value: unknown): Identity | null;
|
|
148
|
+
/** A sentence a plugin gave (a refusal, a denial), trimmed and cut to 500 characters, or null when it is not one. */
|
|
149
|
+
export declare function checkSentence(value: unknown): string | null;
|
|
150
|
+
/** Whether a value is a `Response` (also one made by another copy of the Fetch API). */
|
|
151
|
+
export declare function isResponse(value: unknown): value is Response;
|
|
152
|
+
/** The path to return to after a sign-in: a path on the site, else `/`. */
|
|
153
|
+
export declare function safePath(value: unknown): string;
|
|
154
|
+
/** How a hook call ended: its value, or why not. */
|
|
155
|
+
export type CallOutcome<T> = {
|
|
156
|
+
ok: true;
|
|
157
|
+
value: T;
|
|
158
|
+
} | {
|
|
159
|
+
ok: false;
|
|
160
|
+
reason: 'threw' | 'timeout';
|
|
161
|
+
error: unknown;
|
|
162
|
+
};
|
|
163
|
+
/**
|
|
164
|
+
* Runs `fn` with a time limit: the outcome of the call, or `timeout` when it takes longer than `ms`. `controller`
|
|
165
|
+
* is aborted at the limit (the hook's `ctx.signal`). A hook that hangs keeps running in the background; nobody
|
|
166
|
+
* waits for it.
|
|
167
|
+
*/
|
|
168
|
+
export declare function withTimeLimit<T>(fn: () => T | Promise<T>, ms: number, controller?: AbortController): Promise<CallOutcome<T>>;
|
|
169
|
+
/** A failure's message for the log. */
|
|
170
|
+
export declare function messageOf(error: unknown): string;
|
|
171
|
+
/** The parameters of `path` under a route's segments, or null when it does not match. */
|
|
172
|
+
export declare function matchRoute(segments: readonly string[], path: readonly string[]): Record<string, string> | null;
|
|
173
|
+
/**
|
|
174
|
+
* The host's same-origin rule for writes, as it applies to plugin routes that are not `public`: reads always pass;
|
|
175
|
+
* a write passes with `Sec-Fetch-Site: same-origin` (or `none` without cookies), or, from a client that sends no
|
|
176
|
+
* `Sec-Fetch-Site`, with no `Origin` or the site's own.
|
|
177
|
+
*/
|
|
178
|
+
export declare function isSameOriginWrite(request: Request, appOrigin: string): boolean;
|
|
179
|
+
/** The UTF-8 length of a string. */
|
|
180
|
+
export declare function byteLength(text: string): number;
|
|
181
|
+
/** Checks a key; throws a `TypeError` the plugin sees. */
|
|
182
|
+
export declare function checkKvKey(key: unknown): string;
|
|
183
|
+
/** A value as stored (JSON); throws a `TypeError` the plugin sees. */
|
|
184
|
+
export declare function encodeKvValue(value: unknown): string;
|
|
185
|
+
/** When a value set now with `ttlSeconds` expires (ISO-8601), or null; throws a `TypeError` for a bad TTL. */
|
|
186
|
+
export declare function kvExpiry(now: Date, ttlSeconds: unknown): string | null;
|
|
187
|
+
/** A listing's limit: default 100, at most 1000. */
|
|
188
|
+
export declare function kvListLimit(limit: unknown): number;
|
|
189
|
+
/** The error for a store that is full. */
|
|
190
|
+
export declare function kvFull(): Error;
|
|
191
|
+
/** An answer, checked: what it means, or why it is of the wrong shape. */
|
|
192
|
+
export type Checked<T> = {
|
|
193
|
+
ok: true;
|
|
194
|
+
value: T;
|
|
195
|
+
} | {
|
|
196
|
+
ok: false;
|
|
197
|
+
message: string;
|
|
198
|
+
};
|
|
199
|
+
/** How a hook call ended, its answer checked: `shape` when the answer is not one the hook may give. */
|
|
200
|
+
export type HookOutcome<T> = {
|
|
201
|
+
ok: true;
|
|
202
|
+
value: T;
|
|
203
|
+
} | {
|
|
204
|
+
ok: false;
|
|
205
|
+
reason: 'threw' | 'timeout' | 'shape';
|
|
206
|
+
message: string;
|
|
207
|
+
error?: unknown;
|
|
208
|
+
};
|
|
209
|
+
/** Calls a hook under its time limit and checks its answer. Never throws. */
|
|
210
|
+
export declare function runHook<T>(fn: () => unknown, check: (answer: unknown) => Checked<T>, ms: number, controller?: AbortController): Promise<HookOutcome<T>>;
|
|
211
|
+
/**
|
|
212
|
+
* The answer check of every hook: what an answer means, or why it is not one the hook may give. An answer of the
|
|
213
|
+
* wrong shape is a failure (closed or open by the hook's kind, see {@link failsClosed}).
|
|
214
|
+
*/
|
|
215
|
+
export declare const answers: {
|
|
216
|
+
/** `auth.authenticate`: an identity, or null (nothing: null too). */
|
|
217
|
+
readonly authenticate: (answer: unknown) => Checked<Identity | null>;
|
|
218
|
+
/** `auth.signIn.start`: a Response. */
|
|
219
|
+
readonly signInStart: (answer: unknown) => Checked<Response>;
|
|
220
|
+
/** `auth.signIn.finish`: `{identity, returnTo?}` (returnTo made a path on the site). */
|
|
221
|
+
readonly signInFinish: (answer: unknown) => Checked<{
|
|
222
|
+
identity: Identity;
|
|
223
|
+
returnTo?: string;
|
|
224
|
+
}>;
|
|
225
|
+
/** `auth.identity`: the same person (same `subject`) changed, `{reject}`, or nothing (`keep`). */
|
|
226
|
+
readonly identity: (answer: unknown, subject: string) => Checked<{
|
|
227
|
+
keep: true;
|
|
228
|
+
} | {
|
|
229
|
+
identity: Identity;
|
|
230
|
+
} | {
|
|
231
|
+
reject: string;
|
|
232
|
+
}>;
|
|
233
|
+
/** `auth.signOutUrl`: an `https:`/`http:` URL or a path, or nothing (null). */
|
|
234
|
+
readonly signOutUrl: (answer: unknown) => Checked<string | null>;
|
|
235
|
+
/** `authz.role`: a role, null (no access), or nothing (`opinion: false`). */
|
|
236
|
+
readonly role: (answer: unknown) => Checked<{
|
|
237
|
+
opinion: false;
|
|
238
|
+
} | {
|
|
239
|
+
opinion: true;
|
|
240
|
+
role: ArtifactRole | null;
|
|
241
|
+
}>;
|
|
242
|
+
/** `authz.artifacts`: artifact ids (nothing: none). */
|
|
243
|
+
readonly artifacts: (answer: unknown) => Checked<string[]>;
|
|
244
|
+
/** `guard`: nothing (allowed) or `{deny: sentence}`. */
|
|
245
|
+
readonly guard: (answer: unknown) => Checked<{
|
|
246
|
+
deny: string;
|
|
247
|
+
} | null>;
|
|
248
|
+
/** `wake.deliver`: `{delivered: true}` (true), or nothing (false). */
|
|
249
|
+
readonly deliver: (answer: unknown) => Checked<boolean>;
|
|
250
|
+
/** A route handler: a Response. */
|
|
251
|
+
readonly route: (answer: unknown) => Checked<Response>;
|
|
252
|
+
/** An event handler: anything (its answer is not used). */
|
|
253
|
+
readonly event: () => Checked<null>;
|
|
254
|
+
};
|
|
255
|
+
/** Whether an error is the plugin API's `HttpError` (also from another copy of this package): a route's chosen status. */
|
|
256
|
+
export declare function httpErrorOf(error: unknown): {
|
|
257
|
+
status: number;
|
|
258
|
+
message: string;
|
|
259
|
+
} | null;
|
|
260
|
+
/**
|
|
261
|
+
* Runs a plugin's `validate(config)` the way every host does: once, after {@link validateConfig} passed. Returns the
|
|
262
|
+
* problems, each naming the plugin (`Plugin "acme": …`); an exception counts as one problem with its message.
|
|
263
|
+
*/
|
|
264
|
+
export declare function runValidate(plugin: CheckedPlugin, config: Readonly<Record<string, unknown>>): string[];
|