@plitzi/sdk-runtime 0.0.0-stage → 0.39.3
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/CHANGELOG.md +18 -0
- package/README.md +84 -2
- package/dist/chunks/service-DDDJomiH.mjs +2322 -0
- package/dist/contract/runtime.d.ts +3 -0
- package/dist/functions-api.d.ts +724 -0
- package/dist/host/host.d.ts +42 -0
- package/dist/host/index.d.ts +3 -0
- package/dist/host/index.mjs +107 -0
- package/dist/host/runner.d.ts +11 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.mjs +5 -0
- package/dist/prelude.js +1 -0
- package/dist/runner/cpuMeter.d.ts +35 -0
- package/dist/runner/driver.d.ts +37 -0
- package/dist/runner/guest/bytes.d.ts +27 -0
- package/dist/runner/guest/crypto.d.ts +47 -0
- package/dist/runner/guest/events.d.ts +152 -0
- package/dist/runner/guest/fetch.d.ts +305 -0
- package/dist/runner/guest/install.d.ts +23 -0
- package/dist/runner/guest/types.d.ts +38 -0
- package/dist/runner/index.d.ts +6 -0
- package/dist/runner/index.mjs +1 -0
- package/dist/runner/isolate.d.ts +65 -0
- package/dist/runner/memoryGuard.d.ts +25 -0
- package/dist/runner/prelude.d.ts +10 -0
- package/dist/runner/service.d.ts +76 -0
- package/dist/runner/turns.d.ts +12 -0
- package/package.json +64 -3
|
@@ -0,0 +1,724 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Who may start a run through ONE way in. Resolved against the auth kernel — never re-implemented here.
|
|
3
|
+
*
|
|
4
|
+
* It belongs to the trigger and not to the action, because the two are different questions: a webhook is reachable
|
|
5
|
+
* by anyone who learns its URL and is gated on its signature, while the same flow called from a page may well
|
|
6
|
+
* require a session. Stating it once for the action forced the loosest of its ways in onto all of them.
|
|
7
|
+
*
|
|
8
|
+
* There is no implicit default: a trigger that declares no access is refused at save time rather than falling back
|
|
9
|
+
* to something a reader has to guess at.
|
|
10
|
+
*/
|
|
11
|
+
declare type ActionAccess =
|
|
12
|
+
/** Anyone, including an anonymous visitor. Implicit for a webhook; anywhere else it is a deliberate choice. */
|
|
13
|
+
{
|
|
14
|
+
mode: 'public';
|
|
15
|
+
}
|
|
16
|
+
/** Any request carrying a valid session for this space. */
|
|
17
|
+
| {
|
|
18
|
+
mode: 'session';
|
|
19
|
+
}
|
|
20
|
+
/** A session holding every one of these space permissions. */
|
|
21
|
+
| {
|
|
22
|
+
mode: 'role';
|
|
23
|
+
permissions: string[];
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* The `kv` tasks' own view: values rather than strings, and a TTL that means what a caller expects.
|
|
28
|
+
*
|
|
29
|
+
* Built by `createKvStore` over an {@link ActionKvAdapter}, and then narrowed to one space by `namespaceKv`. A
|
|
30
|
+
* task never sees an adapter.
|
|
31
|
+
*/
|
|
32
|
+
declare type ActionKvStore = {
|
|
33
|
+
get: (key: string) => Promise<unknown>;
|
|
34
|
+
set: (key: string, value: unknown, ttlSeconds?: number) => Promise<void>;
|
|
35
|
+
delete: (key: string) => Promise<void>;
|
|
36
|
+
increment: (key: string, amount: number, ttlSeconds?: number) => Promise<number>;
|
|
37
|
+
/**
|
|
38
|
+
* Writes `next` only if the key still holds `expected` — the value `get` answered — or, with `expected` undefined,
|
|
39
|
+
* only if it holds nothing. Answers whether it wrote: read, change, and write back with this, and a writer that
|
|
40
|
+
* lost the race is told so instead of undoing the other.
|
|
41
|
+
*/
|
|
42
|
+
swap: (key: string, expected: unknown, next: unknown, ttlSeconds?: number) => Promise<boolean>;
|
|
43
|
+
/**
|
|
44
|
+
* Puts `entry` in the list — replacing the one with its id — keeping the highest `keep` by score, and answers what
|
|
45
|
+
* that dropped, for whatever it named to be let go of too. A list has no lifetime: it lives until its entries are
|
|
46
|
+
* removed.
|
|
47
|
+
*/
|
|
48
|
+
listPut: (list: string, entry: KvListEntry, options?: KvListPutOptions) => Promise<KvListPut>;
|
|
49
|
+
listRange: (list: string, range?: KvListRange) => Promise<KvListEntry[]>;
|
|
50
|
+
/** Takes the entry with `id` out of the list, and answers whether it was there. */
|
|
51
|
+
listRemove: (list: string, id: string) => Promise<boolean>;
|
|
52
|
+
/**
|
|
53
|
+
* Writes the list's entry `id` as `next` — or takes it out, `next` null — only if it is still `expected`, the entry
|
|
54
|
+
* `listRange` answered (or, `expected` undefined, only if there is none). Answers whether it wrote: `swap`, for one
|
|
55
|
+
* entry of a list.
|
|
56
|
+
*/
|
|
57
|
+
listSwap: (list: string, id: string, expected: KvListEntry | undefined, next: KvListEntry | null) => Promise<boolean>;
|
|
58
|
+
/**
|
|
59
|
+
* Reads the list's entry `id`, hands it to `change` and writes back what that answers — `{ score, value }`, or `null`
|
|
60
|
+
* to take it out — reading and asking again when somebody wrote in between. `undefined` writes nothing. A vote that
|
|
61
|
+
* re-scores an entry while the track it is starts was the race (a run's queue): the entry gone, nothing is put back.
|
|
62
|
+
*/
|
|
63
|
+
listChange: (list: string, id: string, change: (current: KvListEntry | undefined) => {
|
|
64
|
+
score: number;
|
|
65
|
+
value: unknown;
|
|
66
|
+
} | null | undefined | Promise<{
|
|
67
|
+
score: number;
|
|
68
|
+
value: unknown;
|
|
69
|
+
} | null | undefined>) => Promise<KvListEntry | null | undefined>;
|
|
70
|
+
/**
|
|
71
|
+
* Reads the key, hands its value to `change` and writes back what that answers — reading and asking again when
|
|
72
|
+
* somebody wrote in between, so no writer undoes another. `change` answering `undefined` writes nothing. Answers what
|
|
73
|
+
* was written. `lifetime` is seconds, or worked out from the value written.
|
|
74
|
+
*/
|
|
75
|
+
change: <T>(key: string, change: (current: unknown) => T | undefined | Promise<T | undefined>, lifetime?: KvChangeLifetime<T>) => Promise<T | undefined>;
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* A step refusing to go on, with a reason written for whoever called — a password too easy to guess, a board that is
|
|
80
|
+
* read-only, a quota reached.
|
|
81
|
+
*
|
|
82
|
+
* What any other step throws stays in the run's record and never reaches the caller: an error can carry a query, a
|
|
83
|
+
* URL, a credential's name, and a page is shown to anybody. This one says, by its type, that its message was written
|
|
84
|
+
* to be read. A run that ends on it answers `status: 'failed'` with the message as `error`, which a page's flow reads
|
|
85
|
+
* as `{{ step.error }}` — the same place a refusal before the run began puts its own.
|
|
86
|
+
*
|
|
87
|
+
* `code` names the refusal for code that catches it (`oauth-not-connected`): what to tell apart by, never the message,
|
|
88
|
+
* which is written for a person and may change.
|
|
89
|
+
*/
|
|
90
|
+
export declare class ActionRefusal extends Error {
|
|
91
|
+
code?: string;
|
|
92
|
+
constructor(message: string, options?: {
|
|
93
|
+
code?: string;
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Declares a space's functions. An identity at run time: it is here for the types, and so the same file is valid
|
|
99
|
+
* wherever it is loaded — natively in a self-hosted server, or built by the platform and run in its sandbox.
|
|
100
|
+
*/
|
|
101
|
+
export declare const defineFunctions: (definition: FunctionsDefinition) => FunctionsDefinition;
|
|
102
|
+
|
|
103
|
+
/** Declares a space runtime — an identity, for the types: `export default defineRuntime({ start: … })`. */
|
|
104
|
+
export declare const defineRuntime: (runtime: SpaceRuntime) => SpaceRuntime;
|
|
105
|
+
|
|
106
|
+
declare type Environment = 'production' | 'staging' | 'development' | 'main';
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* What a task — or every task and route, declared once for all of them — asks to be given beyond an invocation's
|
|
110
|
+
* default: CPU time, awaits not counted; wall time, from the moment it starts; and its size, the memory it runs with.
|
|
111
|
+
* Never more than the space's plan or the server allows; asking for more than the server does is a problem when the
|
|
112
|
+
* functions are saved, and a size the plan does not include is refused when the task runs, naming the plan.
|
|
113
|
+
*/
|
|
114
|
+
declare type FunctionAskedLimits = {
|
|
115
|
+
cpuMs?: number;
|
|
116
|
+
wallMs?: number;
|
|
117
|
+
size?: FunctionSize;
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Everything a space's code can do besides computing — one object, the same wherever it runs. In a self-hosted server
|
|
122
|
+
* it is built from the run's own context; in the sandbox each method is a call back to the platform, which scopes it
|
|
123
|
+
* to the space and the run before doing it.
|
|
124
|
+
*/
|
|
125
|
+
export declare type FunctionContext = {
|
|
126
|
+
spaceId: number;
|
|
127
|
+
environment: Environment;
|
|
128
|
+
runId: string;
|
|
129
|
+
/** How the run was started: `call`, `render`, `schedule`, `webhook`… */
|
|
130
|
+
trigger: string;
|
|
131
|
+
/** `user:<id>` for a session, `ip:<address>` for everyone else — what a limit per person keys on. */
|
|
132
|
+
callerId: string;
|
|
133
|
+
user?: FunctionUser;
|
|
134
|
+
/**
|
|
135
|
+
* The space's own key/value store: `get`, `set`, `swap`, `increment`, lists — and `change`, to read, change and write
|
|
136
|
+
* a value back without undoing a writer that got there first.
|
|
137
|
+
*/
|
|
138
|
+
kv: ActionKvStore;
|
|
139
|
+
/**
|
|
140
|
+
* Counts one more of `bucket` — per caller unless `per: 'everyone'` — and says whether it was within `most` in every
|
|
141
|
+
* `perSeconds`. The same count as a flow's `flow.rateLimit` step on that bucket. What to answer when it is not is the
|
|
142
|
+
* code's.
|
|
143
|
+
*/
|
|
144
|
+
rateLimit: (bucket: string, limit: RateLimit) => Promise<RateCount>;
|
|
145
|
+
/**
|
|
146
|
+
* Signs `value` with a key of the space's own that the platform keeps and the code never sees: HMAC-SHA-256,
|
|
147
|
+
* base64url. What a link, an invitation or a key handed to a page is made of. Refused on a server with no signing
|
|
148
|
+
* secret.
|
|
149
|
+
*/
|
|
150
|
+
sign: (value: string) => Promise<string>;
|
|
151
|
+
/** Whether `signature` is what `sign` answered for `value`, compared in constant time. */
|
|
152
|
+
verify: (value: string, signature: string) => Promise<boolean>;
|
|
153
|
+
/** To the hosts the space declared (`allow.hosts`) — and, with `credential`, with one of its secrets written in. */
|
|
154
|
+
fetch: FunctionFetch;
|
|
155
|
+
/** Says something on one of the space's realtime channels, as the server. */
|
|
156
|
+
publish: (topic: string, type: string, data: unknown) => Promise<void>;
|
|
157
|
+
/**
|
|
158
|
+
* Who is on a topic of one of the space's `presence: true` channels now — every page connected to it, on whichever
|
|
159
|
+
* replica — the earliest arrived first. A room's listeners, counted by the server rather than by what pages report.
|
|
160
|
+
*/
|
|
161
|
+
presence: (topic: string) => Promise<FunctionPresence>;
|
|
162
|
+
/**
|
|
163
|
+
* The space's people, read by another: `get('mika')` or `get(7)` — public fields alone, and only of someone who
|
|
164
|
+
* belongs to the space (the deployment says who: its visitors and members on Plitzi, every account of its own on a
|
|
165
|
+
* server of yours). `undefined` for anyone else, never "exists elsewhere": a profile page, a chat's names.
|
|
166
|
+
*/
|
|
167
|
+
visitors: {
|
|
168
|
+
get: (who: string | number) => Promise<FunctionVisitor | undefined>;
|
|
169
|
+
};
|
|
170
|
+
/** Lets the visitor into one topic of a private (`grant: true`) channel. */
|
|
171
|
+
grant: (topic: string, ttlSeconds?: number) => Promise<string>;
|
|
172
|
+
/** Takes a grant back — or every grant for the topic, naming none. */
|
|
173
|
+
revoke: (topic: string, grant?: string) => Promise<void>;
|
|
174
|
+
/**
|
|
175
|
+
* Starts one of the space's actions — one with a `later` trigger — in so many seconds, on the server, whether or not a
|
|
176
|
+
* page is still open: a turn that runs out, a bot's move, a hold that lapses. A `key` names it: set again under the
|
|
177
|
+
* same key, the one still waiting is replaced. Answers the job and when it is due (epoch ms, by the queue's clock).
|
|
178
|
+
* `ctx.later({ action: 'game.timeout', in: 45, input: { room }, key: 'turn:' + room })`.
|
|
179
|
+
*/
|
|
180
|
+
later: (request: LaterRequest) => Promise<LaterAnswer>;
|
|
181
|
+
/** Drops what waits under `key` — the timer of a game that ended — and answers how many. */
|
|
182
|
+
cancelLater: (key: string) => Promise<number>;
|
|
183
|
+
/**
|
|
184
|
+
* One file of the space's data — a project's `src/data/<file>` — parsed, as of the version the run belongs to: what
|
|
185
|
+
* a page must not carry, read here and answered only as far as it is shown. Read-only. `ctx.data('products.json')`.
|
|
186
|
+
*/
|
|
187
|
+
data: (file: string) => Promise<unknown>;
|
|
188
|
+
/** A line for whoever reads the run: the builder's Try, and the deployment's logs. */
|
|
189
|
+
log: (...values: unknown[]) => void;
|
|
190
|
+
/** OAuth 2.0 through the space's declared connections — see {@link FunctionOAuth}. */
|
|
191
|
+
oauth: FunctionOAuth;
|
|
192
|
+
/** The space's jobs — see {@link FunctionJobs}. */
|
|
193
|
+
jobs: FunctionJobs;
|
|
194
|
+
/** Progress for a caller that asked for a stream — a no-op when nobody did. */
|
|
195
|
+
emit: (chunk: unknown) => void;
|
|
196
|
+
/** Aborted when the run is: cancelled, or out of time. Hand it to what can stop early. */
|
|
197
|
+
signal: AbortSignal;
|
|
198
|
+
};
|
|
199
|
+
|
|
200
|
+
/** What a function's `fetch` answers: a web `Response`, whichever runner it is in. */
|
|
201
|
+
export declare type FunctionFetch = (url: string | URL, init?: FunctionFetchInit) => Promise<Response>;
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* A request a function makes: `fetch`'s own init, and `credential` — the id of one of the space's credentials, whose
|
|
205
|
+
* values the platform writes into the request where it says `{{credential.<key>}}` (in the URL, a header or the
|
|
206
|
+
* body). The code never holds the value; it names it.
|
|
207
|
+
*/
|
|
208
|
+
export declare type FunctionFetchInit = {
|
|
209
|
+
method?: string;
|
|
210
|
+
headers?: Record<string, string>;
|
|
211
|
+
body?: string;
|
|
212
|
+
credential?: string;
|
|
213
|
+
/** Cuts the request short when it aborts (`AbortSignal.timeout(12_000)`); the run's own end cuts it regardless. */
|
|
214
|
+
signal?: AbortSignal;
|
|
215
|
+
};
|
|
216
|
+
|
|
217
|
+
/** A job of the space's: work longer than a request, run on the server slice by slice until it answers. */
|
|
218
|
+
export declare type FunctionJob<I extends Record<string, unknown> = Record<string, unknown>, S = unknown, R = unknown> = {
|
|
219
|
+
/**
|
|
220
|
+
* Who may watch it (`$job:<id>`, `useJob`) besides whoever started it — a channel's access, read against the visitor's
|
|
221
|
+
* permissions in the space. Absent, only whoever started it.
|
|
222
|
+
*/
|
|
223
|
+
watch?: ActionAccess;
|
|
224
|
+
/** How long it may run, every slice together, in seconds — within what the server allows. */
|
|
225
|
+
maxSeconds?: number;
|
|
226
|
+
/**
|
|
227
|
+
* The work. What it answers is the job's result; an `ActionRefusal` fails it without a retry. A method, not a property:
|
|
228
|
+
* so a job typed with an input and a state of its own is still one of `FunctionsDefinition['jobs']`, and one written
|
|
229
|
+
* in place there gets an object for its input and `unknown` for its state.
|
|
230
|
+
*/
|
|
231
|
+
run(input: I, job: FunctionJobHandle<S>, ctx: FunctionContext): R | Promise<R>;
|
|
232
|
+
};
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* What a job's `run` is handed besides its input: the job, and how it keeps where it is. A job runs in slices — a pause,
|
|
236
|
+
* a lost server or its time in one go ending one — and every slice but the first starts from what the one before saved.
|
|
237
|
+
*/
|
|
238
|
+
export declare type FunctionJobHandle<S = unknown> = {
|
|
239
|
+
id: string;
|
|
240
|
+
name: string;
|
|
241
|
+
/** What it last saved — `undefined` on its first slice, and whenever it never saved. */
|
|
242
|
+
state: S | undefined;
|
|
243
|
+
/** Which attempt this is, from 1: a slice after a failure is the next attempt, from the last save. */
|
|
244
|
+
attempt: number;
|
|
245
|
+
/**
|
|
246
|
+
* Keeps `state` durably: the next slice starts with it. JSON, within what the server keeps for one job. A method, not
|
|
247
|
+
* a property: so a job whose state has a type of its own is still one of `FunctionsDefinition['jobs']`.
|
|
248
|
+
*/
|
|
249
|
+
save(state: S): Promise<void>;
|
|
250
|
+
/** How far it is, from 0 to 1, and what it is doing — for whoever watches it. Never fails the job. */
|
|
251
|
+
report: (fraction: number, step?: string) => void;
|
|
252
|
+
/** Why this slice is asked to end, once it is (`signal` aborted): save, and return. */
|
|
253
|
+
readonly stopping: FunctionJobStop | undefined;
|
|
254
|
+
/** Aborted when the slice is asked to end — the same as `ctx.signal`. Some seconds later it is cut. */
|
|
255
|
+
signal: AbortSignal;
|
|
256
|
+
};
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* The space's jobs, from its code: what starts one, reads it and stops it. Who may is the code's to decide — these are
|
|
260
|
+
* called from its routes and tasks, behind its own checks.
|
|
261
|
+
*/
|
|
262
|
+
export declare type FunctionJobs = {
|
|
263
|
+
/**
|
|
264
|
+
* Starts one of the jobs this code declares (`jobs` in `defineFunctions`), with `input`: answered at once, the job
|
|
265
|
+
* `pending`. With `key`, at most one of that key runs at a time — starting it again while one is under way answers that
|
|
266
|
+
* one (a button pressed twice), and once it ended starts it over with the new input.
|
|
267
|
+
*/
|
|
268
|
+
start: (name: string, input?: Record<string, unknown>, options?: {
|
|
269
|
+
key?: string;
|
|
270
|
+
}) => Promise<FunctionJobView>;
|
|
271
|
+
/** One of the space's jobs, as whoever may watch it sees it — or nothing, when there is none by that id. */
|
|
272
|
+
get: (id: string) => Promise<FunctionJobView | undefined>;
|
|
273
|
+
/** Sets it aside: it stops once its code saved, and waits as `paused`. Answers whether anything changed. */
|
|
274
|
+
pause: (id: string) => Promise<boolean>;
|
|
275
|
+
/** Puts a paused job back in the queue, to go on from what it saved. */
|
|
276
|
+
resume: (id: string) => Promise<boolean>;
|
|
277
|
+
/** Stops it for good: dropped if it waits, aborted if it runs. */
|
|
278
|
+
cancel: (id: string) => Promise<boolean>;
|
|
279
|
+
};
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* Why a slice of a job is asked to end (`job.stopping`): it was set aside (`pause`) or cancelled (`cancel`), its time in
|
|
283
|
+
* one go is up (`limit`) or the server is going away (`shutdown`). Whichever, what was saved is where it goes on from.
|
|
284
|
+
*/
|
|
285
|
+
export declare type FunctionJobStop = 'pause' | 'cancel' | 'limit' | 'shutdown';
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* A job as whoever may watch it sees it: what `ctx.jobs.get` answers, what `$job:<id>` says each time it moves, and
|
|
289
|
+
* what `useJob` holds. Never its input or what it saved: those are its code's.
|
|
290
|
+
*/
|
|
291
|
+
declare type FunctionJobView = {
|
|
292
|
+
id: string;
|
|
293
|
+
/** The job's name, as the functions declare it. */
|
|
294
|
+
name: string;
|
|
295
|
+
status: JobStatus;
|
|
296
|
+
/** From 0 to 1, as its code last reported. */
|
|
297
|
+
progress?: number;
|
|
298
|
+
/** What it is doing, in its code's words. */
|
|
299
|
+
step?: string;
|
|
300
|
+
/** What it answered, once it succeeded. */
|
|
301
|
+
result?: unknown;
|
|
302
|
+
/** Why it failed — for a refusal, its code's own words; for anything else, that it failed. */
|
|
303
|
+
error?: string;
|
|
304
|
+
/** Epoch ms. */
|
|
305
|
+
createdAt: number;
|
|
306
|
+
updatedAt: number;
|
|
307
|
+
};
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* OAuth 2.0 on behalf of whoever the space serves, through the connections it declared (`oauth` in its functions): the
|
|
311
|
+
* consent, the code exchanged, the token kept sealed and renewed by the platform. The code never holds a token, as it
|
|
312
|
+
* never holds a credential — it names the connection.
|
|
313
|
+
*/
|
|
314
|
+
export declare type FunctionOAuth = {
|
|
315
|
+
/**
|
|
316
|
+
* Where to send the visitor to consent (authorization code, PKCE). They come back to `/fn/oauth/callback` — the
|
|
317
|
+
* address to register with the provider, on the space's host — and from there to `returnTo`, a path of the space,
|
|
318
|
+
* with `?oauth=<connection>&oauth_result=connected` or `&oauth_error=<what>`. `authorizeParams` adds to the
|
|
319
|
+
* connection's for this consent alone — `{ prompt: 'select_account' }` to choose another account — never one the
|
|
320
|
+
* platform writes (`state`, `redirect_uri`, `scope`, PKCE's).
|
|
321
|
+
*/
|
|
322
|
+
connect: (connection: string, options: {
|
|
323
|
+
returnTo: string;
|
|
324
|
+
authorizeParams?: Record<string, string>;
|
|
325
|
+
} & FunctionOAuthOwner) => Promise<{
|
|
326
|
+
url: string;
|
|
327
|
+
}>;
|
|
328
|
+
status: (connection: string, options?: FunctionOAuthOwner) => Promise<FunctionOAuthStatus>;
|
|
329
|
+
/**
|
|
330
|
+
* A request to the provider's API with the token written in (`Authorization: Bearer`), renewed when it is about to
|
|
331
|
+
* lapse and once more if the answer is 401. Only to the hosts the space declared (`allow.hosts`). With nobody
|
|
332
|
+
* connected it refuses, `code: 'oauth-not-connected'` ({@link FunctionOAuthRefusal}).
|
|
333
|
+
*/
|
|
334
|
+
fetch: (connection: string, url: string | URL, init?: Omit<FunctionFetchInit, 'credential'> & FunctionOAuthOwner) => Promise<Response>;
|
|
335
|
+
/** Forgets the connection — revoked at the provider when it says where — and answers whether there was one. */
|
|
336
|
+
disconnect: (connection: string, options?: FunctionOAuthOwner) => Promise<boolean>;
|
|
337
|
+
/**
|
|
338
|
+
* The access token itself, renewed first when it is about to lapse — only of a connection that declares
|
|
339
|
+
* `exposeToken: true`, for a provider's SDK in the page that asks for it (`getOAuthToken`). Whoever the code hands it
|
|
340
|
+
* to holds it until it lapses: answer it only to whom the connection is (its owner), from a route that checks so.
|
|
341
|
+
*/
|
|
342
|
+
token: (connection: string, options?: FunctionOAuthOwner) => Promise<FunctionOAuthToken>;
|
|
343
|
+
/**
|
|
344
|
+
* The address providers send a visitor back to (`…/fn/oauth/callback`) — the one this run's `connect` hands them, to
|
|
345
|
+
* show whoever registers the app with a provider. The space's host the request came on, or the one the deployment
|
|
346
|
+
* fixed (`functions.oauth.callbackOrigin`); refused in a run no visitor's request started, when none was fixed.
|
|
347
|
+
*/
|
|
348
|
+
callbackUrl: () => Promise<string>;
|
|
349
|
+
};
|
|
350
|
+
|
|
351
|
+
/** What a space's functions declare, read from the bundle when it is saved: its tasks, routes and reachable hosts. */
|
|
352
|
+
/**
|
|
353
|
+
* One OAuth 2.0 connection a space's code makes on its visitors' (or its own) behalf, declared as data: any provider,
|
|
354
|
+
* none of them known to the platform by name. `issuer` for one that publishes OpenID metadata — its endpoints are read
|
|
355
|
+
* from `<issuer>/.well-known/openid-configuration` — or the endpoints themselves for one that does not.
|
|
356
|
+
*/
|
|
357
|
+
declare type FunctionOAuthConnection = {
|
|
358
|
+
/** An OpenID provider's issuer (`https://accounts.google.com`): the endpoints are discovered from it. */
|
|
359
|
+
issuer?: string;
|
|
360
|
+
/** Where the visitor is sent to consent, when there is no `issuer` (or to override what it publishes). */
|
|
361
|
+
authorizeUrl?: string;
|
|
362
|
+
/** Where the code is exchanged and the token refreshed, when there is no `issuer` (or to override it). */
|
|
363
|
+
tokenUrl?: string;
|
|
364
|
+
/** Where a token is revoked on `disconnect`, when the provider has one. */
|
|
365
|
+
revokeUrl?: string;
|
|
366
|
+
/** The space's credential holding the app's `clientId` and `clientSecret`: the code names it, never holds it. */
|
|
367
|
+
credential: string;
|
|
368
|
+
/** What is asked for, as the provider spells it. */
|
|
369
|
+
scopes?: string[];
|
|
370
|
+
/** More of the consent URL, as the provider needs it: `{ access_type: 'offline', prompt: 'consent' }`. */
|
|
371
|
+
authorizeParams?: Record<string, string>;
|
|
372
|
+
/** How the app proves who it is to `tokenUrl`: in the body (the default) or as HTTP Basic. */
|
|
373
|
+
clientAuth?: 'body' | 'basic';
|
|
374
|
+
/**
|
|
375
|
+
* The code may read the access token (`ctx.oauth.token`), for a provider's SDK that runs in the page and asks for it
|
|
376
|
+
* itself — Spotify's Web Playback, Google's Picker. Off by default: the token then leaves the server, as far as the
|
|
377
|
+
* code hands it. The refresh token never does.
|
|
378
|
+
*/
|
|
379
|
+
exposeToken?: boolean;
|
|
380
|
+
};
|
|
381
|
+
|
|
382
|
+
/**
|
|
383
|
+
* Whose an OAuth connection is: a signed-in visitor's own by default (`user:<id>`); `space` for one the whole space
|
|
384
|
+
* shares; or a key the code keeps for whom it serves — a profile, a team (`profile:ana`). Letters, digits and `:._@-`.
|
|
385
|
+
*/
|
|
386
|
+
export declare type FunctionOAuthOwner = {
|
|
387
|
+
owner?: string;
|
|
388
|
+
};
|
|
389
|
+
|
|
390
|
+
/** What `ctx.oauth.status` says of a connection — never its token. */
|
|
391
|
+
export declare type FunctionOAuthStatus = {
|
|
392
|
+
connected: boolean;
|
|
393
|
+
scopes?: string[];
|
|
394
|
+
expiresAt?: number;
|
|
395
|
+
};
|
|
396
|
+
|
|
397
|
+
/** The access token `ctx.oauth.token` hands out — never the refresh token, which stays with the platform. */
|
|
398
|
+
export declare type FunctionOAuthToken = {
|
|
399
|
+
token: string;
|
|
400
|
+
type: string;
|
|
401
|
+
expiresAt?: number;
|
|
402
|
+
scopes?: string[];
|
|
403
|
+
};
|
|
404
|
+
|
|
405
|
+
/** Who is on a presence channel's topic: how many, and each page with who is signed in on it. */
|
|
406
|
+
declare type FunctionPresence = {
|
|
407
|
+
count: number;
|
|
408
|
+
members: {
|
|
409
|
+
from: string;
|
|
410
|
+
user?: {
|
|
411
|
+
id: number;
|
|
412
|
+
name: string;
|
|
413
|
+
};
|
|
414
|
+
since: number;
|
|
415
|
+
}[];
|
|
416
|
+
};
|
|
417
|
+
|
|
418
|
+
/**
|
|
419
|
+
* An HTTP endpoint of the space, under `/fn/`: a web-standard handler, the shape every edge runtime runs. The request
|
|
420
|
+
* carries no credentials — `ctx.user` says who is asking — and the response sets no cookies: the host's are the
|
|
421
|
+
* platform's.
|
|
422
|
+
*/
|
|
423
|
+
export declare type FunctionRoute = (request: Request, ctx: FunctionRouteContext) => Response | Promise<Response>;
|
|
424
|
+
|
|
425
|
+
/** What a route's handler gets besides the request: the context, and the `:params` its key named, by name. */
|
|
426
|
+
export declare type FunctionRouteContext = FunctionContext & {
|
|
427
|
+
params: Record<string, string>;
|
|
428
|
+
};
|
|
429
|
+
|
|
430
|
+
/**
|
|
431
|
+
* What each file at the top of a space's `functions/` exports by default — its tasks, its routes, and where its code
|
|
432
|
+
* may reach — merged into the one definition a bundle exports (`mergeFunctions`).
|
|
433
|
+
*/
|
|
434
|
+
export declare type FunctionsDefinition = {
|
|
435
|
+
/** The hosts `ctx.fetch` may reach. Anything else is refused before it leaves. */
|
|
436
|
+
allow?: {
|
|
437
|
+
hosts?: string[];
|
|
438
|
+
};
|
|
439
|
+
tasks?: FunctionTask<never>[];
|
|
440
|
+
/** `'GET /board-assets/:board/:asset'` → a handler, served at `/fn/board-assets/…`. */
|
|
441
|
+
routes?: Record<string, FunctionRoute>;
|
|
442
|
+
/**
|
|
443
|
+
* More CPU, time or memory than an invocation gets by default, for every task and route — a task may ask for its
|
|
444
|
+
* own.
|
|
445
|
+
*/
|
|
446
|
+
limits?: FunctionAskedLimits;
|
|
447
|
+
/**
|
|
448
|
+
* The OAuth connections its code makes (`ctx.oauth`), by the name it calls them — any provider, as data: its `issuer`
|
|
449
|
+
* (OpenID) or its endpoints, the credential with the app's id and secret, the scopes.
|
|
450
|
+
*/
|
|
451
|
+
oauth?: Record<string, FunctionOAuthConnection>;
|
|
452
|
+
/** Its jobs, by name (`ctx.jobs.start(name, input)`): work that outlives a request, run slice by slice. */
|
|
453
|
+
jobs?: Record<string, FunctionJob>;
|
|
454
|
+
};
|
|
455
|
+
|
|
456
|
+
/**
|
|
457
|
+
* A space's own functions, as everything outside the runner sees them: the builder, the CLI, the MCP and the
|
|
458
|
+
* platform that stores them. The contract the code itself is written against is `@plitzi/sdk-runtime`.
|
|
459
|
+
*/
|
|
460
|
+
/**
|
|
461
|
+
* How much memory a task runs with, by name (`FUNCTION_SIZES` in `@plitzi/sdk-shared/actions`): small unless it asks
|
|
462
|
+
* for more. The runner keeps a task's memory for it whole while it runs, so a small one leaves room for many beside it;
|
|
463
|
+
* a space's plan says which sizes its tasks may ask for.
|
|
464
|
+
*/
|
|
465
|
+
declare type FunctionSize = 'small' | 'medium' | 'large';
|
|
466
|
+
|
|
467
|
+
/**
|
|
468
|
+
* One step a server action can take, written by the space. The shape of the platform's own tasks — addressed as
|
|
469
|
+
* `<namespace>.<action>`, drawn in the builder from `params` — run with a {@link FunctionContext} rather than the
|
|
470
|
+
* platform's full one.
|
|
471
|
+
*
|
|
472
|
+
* `R` is what `run` answers: `taskAction(task, …)` hands it, with `T`, to a page that runs the action
|
|
473
|
+
* (`useServerAction`), so `FunctionTask<{ id: string }, Habit>` is typed at both ends.
|
|
474
|
+
*/
|
|
475
|
+
export declare type FunctionTask<T extends Record<string, unknown> = Record<string, unknown>, R = unknown> = {
|
|
476
|
+
namespace: string;
|
|
477
|
+
action: string;
|
|
478
|
+
title: string;
|
|
479
|
+
description?: string;
|
|
480
|
+
params: Record<keyof T, InteractionCallbackParam<T>>;
|
|
481
|
+
/**
|
|
482
|
+
* More CPU, time or memory than an invocation gets by default — `{ cpuMs: 1000, size: 'medium' }` for a task that
|
|
483
|
+
* reshapes a big feed — up to what the space's plan and the server allow. Over the functions' own `limits`, for this
|
|
484
|
+
* task.
|
|
485
|
+
*/
|
|
486
|
+
limits?: FunctionAskedLimits;
|
|
487
|
+
run: (params: T, ctx: FunctionContext) => R | Promise<R>;
|
|
488
|
+
};
|
|
489
|
+
|
|
490
|
+
/**
|
|
491
|
+
* Who started the run, as a space's own code may know them: everything a function decides by — who, their roles and
|
|
492
|
+
* permissions — and never their session. A token inside the sandbox is a token a function could send anywhere.
|
|
493
|
+
*/
|
|
494
|
+
export declare type FunctionUser = {
|
|
495
|
+
id: number;
|
|
496
|
+
username: string;
|
|
497
|
+
email: string;
|
|
498
|
+
verified: boolean;
|
|
499
|
+
permissions: string[];
|
|
500
|
+
roles: string[];
|
|
501
|
+
};
|
|
502
|
+
|
|
503
|
+
/**
|
|
504
|
+
* A person of the space as another's code may read them: who they are to a page — never their email, their roles or
|
|
505
|
+
* anything they did not show. `name` and `avatar` where the deployment keeps them.
|
|
506
|
+
*/
|
|
507
|
+
declare type FunctionVisitor = {
|
|
508
|
+
id: number;
|
|
509
|
+
username: string;
|
|
510
|
+
name?: string;
|
|
511
|
+
avatar?: string;
|
|
512
|
+
};
|
|
513
|
+
|
|
514
|
+
declare type InteractionCallbackParam<T extends Record<string, unknown> = Record<string, unknown>> = {
|
|
515
|
+
canBind?: boolean;
|
|
516
|
+
label?: string;
|
|
517
|
+
when?: boolean | ((params: InteractionCallbackParamValues<T>) => boolean);
|
|
518
|
+
/**
|
|
519
|
+
* This param names one of the space's credentials, of this provider. Serializable on purpose: an editor offers the
|
|
520
|
+
* space's credentials of that kind to pick from, rather than a text box somebody has to type an identifier into.
|
|
521
|
+
*/
|
|
522
|
+
credentialProvider?: SpaceCredentialProvider;
|
|
523
|
+
/**
|
|
524
|
+
* A space's task refuses to run without it — empty, or only spaces — saying so with the param's `label`, before its
|
|
525
|
+
* code runs: what a task otherwise checked by hand in its first lines.
|
|
526
|
+
*/
|
|
527
|
+
required?: boolean;
|
|
528
|
+
} &
|
|
529
|
+
/** Handed to a task as text, whatever was written or bound — a number as its digits — at most `maxLength` long. */
|
|
530
|
+
({
|
|
531
|
+
type: 'text';
|
|
532
|
+
defaultValue?: string | number;
|
|
533
|
+
maxLength?: number;
|
|
534
|
+
}
|
|
535
|
+
/** Written as text — a number or a template — and handed to the callback as a number, within `min` and `max`. */
|
|
536
|
+
| {
|
|
537
|
+
type: 'number';
|
|
538
|
+
defaultValue?: number;
|
|
539
|
+
min?: number;
|
|
540
|
+
max?: number;
|
|
541
|
+
} | {
|
|
542
|
+
type: 'textarea';
|
|
543
|
+
defaultValue?: string | number;
|
|
544
|
+
maxLength?: number;
|
|
545
|
+
} | {
|
|
546
|
+
type: 'codemirror-text';
|
|
547
|
+
defaultValue?: string;
|
|
548
|
+
} | {
|
|
549
|
+
type: 'codemirror-json';
|
|
550
|
+
defaultValue?: string;
|
|
551
|
+
} | {
|
|
552
|
+
type: 'boolean';
|
|
553
|
+
defaultValue?: boolean;
|
|
554
|
+
}
|
|
555
|
+
/**
|
|
556
|
+
* Several values, handed to a task as a list of text: bound to a list it is that list, and written by hand it is one
|
|
557
|
+
* a line (or separated by commas) — at most `maxItems`. A run's room took its genres as comma text before it.
|
|
558
|
+
*/
|
|
559
|
+
| {
|
|
560
|
+
type: 'list';
|
|
561
|
+
defaultValue?: string[];
|
|
562
|
+
maxItems?: number;
|
|
563
|
+
} | {
|
|
564
|
+
/**
|
|
565
|
+
* Several elements of the space, stored as their ids. The editor offers the elements of `elementType` to pick
|
|
566
|
+
* from — it is the one that knows the page — so a step never asks for ids typed by hand.
|
|
567
|
+
*/
|
|
568
|
+
type: 'elements';
|
|
569
|
+
defaultValue?: string[];
|
|
570
|
+
elementType?: string;
|
|
571
|
+
} | {
|
|
572
|
+
type: 'select';
|
|
573
|
+
defaultValue?: string;
|
|
574
|
+
options: {
|
|
575
|
+
label: string;
|
|
576
|
+
value: string;
|
|
577
|
+
}[] | ((params: InteractionCallbackParamValues<T>) => {
|
|
578
|
+
label: string;
|
|
579
|
+
value: string;
|
|
580
|
+
}[]);
|
|
581
|
+
} | {
|
|
582
|
+
type: (params: InteractionCallbackParamValues<T>) => InteractionParamType;
|
|
583
|
+
defaultValue?: string | number | boolean | string[];
|
|
584
|
+
options?: {
|
|
585
|
+
label: string;
|
|
586
|
+
value: string;
|
|
587
|
+
}[] | ((params: InteractionCallbackParamValues<T>) => {
|
|
588
|
+
label: string;
|
|
589
|
+
value: string;
|
|
590
|
+
}[]);
|
|
591
|
+
});
|
|
592
|
+
|
|
593
|
+
declare type InteractionCallbackParamValues<T extends Record<string, unknown> = Record<string, unknown>> = T;
|
|
594
|
+
|
|
595
|
+
declare type InteractionParamType = 'boolean' | 'select' | 'text' | 'number' | 'textarea' | 'codemirror-text' | 'codemirror-json' | 'elements' | 'list';
|
|
596
|
+
|
|
597
|
+
/**
|
|
598
|
+
* The durable queue the server's work waits in: a schedule's fires, the runs set for later, and the jobs of the spaces'
|
|
599
|
+
* functions — one queue, one contract (`JobQueue`), whatever store a deployment keeps it in.
|
|
600
|
+
*/
|
|
601
|
+
/**
|
|
602
|
+
* Where a job is, as the only durable answer to "did it happen".
|
|
603
|
+
*
|
|
604
|
+
* `pending` is waiting to be taken, `running` is held by a worker under a lease, `paused` is set aside until somebody
|
|
605
|
+
* resumes it — a job of a space's functions with what it saved, one that failed every attempt it was given (for somebody
|
|
606
|
+
* to read its runs and resume it once fixed, every attempt again, or cancel it), one no worker may hold — and the other
|
|
607
|
+
* four are terminal, except that an operator may put a `failed`, `dead` or `cancelled` one back to `pending`. `dead` is
|
|
608
|
+
* a job that can never run: its action is gone or switched off, or a visitor's call was kept past a day.
|
|
609
|
+
*/
|
|
610
|
+
declare type JobStatus = 'pending' | 'running' | 'paused' | 'succeeded' | 'failed' | 'dead' | 'cancelled';
|
|
611
|
+
|
|
612
|
+
/**
|
|
613
|
+
* The lifetime a change writes with: a number of seconds, one worked out from the value written, or none.
|
|
614
|
+
*/
|
|
615
|
+
declare type KvChangeLifetime<T> = number | ((next: T) => number | undefined);
|
|
616
|
+
|
|
617
|
+
/** One entry of a list: what it is called, where it sorts, and what it carries. */
|
|
618
|
+
declare type KvListEntry = {
|
|
619
|
+
id: string;
|
|
620
|
+
score: number;
|
|
621
|
+
value: unknown;
|
|
622
|
+
};
|
|
623
|
+
|
|
624
|
+
/** What a put did: whether the entry went in, and what `keep` dropped to make room. */
|
|
625
|
+
declare type KvListPut = {
|
|
626
|
+
stored: boolean;
|
|
627
|
+
dropped: KvListEntry[];
|
|
628
|
+
};
|
|
629
|
+
|
|
630
|
+
declare type KvListPutOptions = {
|
|
631
|
+
/** How many to keep, the highest scores; what falls past it is dropped. {@link MAX_LIST_ENTRIES} at most. */
|
|
632
|
+
keep?: number;
|
|
633
|
+
/**
|
|
634
|
+
* Replace an entry already there only with a score at least as high — a player's best, or the latest of two writes
|
|
635
|
+
* that arrived out of order. Otherwise the one there stays and nothing is written.
|
|
636
|
+
*/
|
|
637
|
+
higherOnly?: boolean;
|
|
638
|
+
};
|
|
639
|
+
|
|
640
|
+
declare type KvListRange = {
|
|
641
|
+
/** How many entries to skip first. 0 when absent. */
|
|
642
|
+
offset?: number;
|
|
643
|
+
/** How many to answer. All of them when absent. */
|
|
644
|
+
limit?: number;
|
|
645
|
+
/** `desc` — the highest score first — when absent. */
|
|
646
|
+
order?: 'asc' | 'desc';
|
|
647
|
+
};
|
|
648
|
+
|
|
649
|
+
/** The job it became, and when it is due — epoch ms, by the queue's clock. */
|
|
650
|
+
declare type LaterAnswer = {
|
|
651
|
+
id: string;
|
|
652
|
+
at: number;
|
|
653
|
+
};
|
|
654
|
+
|
|
655
|
+
/** What a run asks to start later: one of the space's actions, in so many seconds, with its input — and a name. */
|
|
656
|
+
declare type LaterRequest = {
|
|
657
|
+
/** The action, by its identifier. It says it may be started this way with a `later` trigger. */
|
|
658
|
+
action: string;
|
|
659
|
+
/** Seconds from now, by the queue's clock — whole or not, from 0 to a month. */
|
|
660
|
+
in: number;
|
|
661
|
+
input?: Record<string, unknown>;
|
|
662
|
+
/**
|
|
663
|
+
* What it is known by in this space: setting another under the same key replaces the one still waiting — a turn's
|
|
664
|
+
* timer moved on by a move — and `cancelLater(key)` drops it. The one already running is left be.
|
|
665
|
+
*/
|
|
666
|
+
key?: string;
|
|
667
|
+
};
|
|
668
|
+
|
|
669
|
+
/** Where a limit stands after counting one more: whether that one was within it, and how many are left. */
|
|
670
|
+
declare type RateCount = {
|
|
671
|
+
allowed: boolean;
|
|
672
|
+
count: number;
|
|
673
|
+
remaining: number;
|
|
674
|
+
};
|
|
675
|
+
|
|
676
|
+
/** How much of something is allowed: `most` in every `perSeconds`, for each caller or for everyone together. */
|
|
677
|
+
declare type RateLimit = {
|
|
678
|
+
most: number;
|
|
679
|
+
perSeconds: number;
|
|
680
|
+
per?: 'caller' | 'everyone';
|
|
681
|
+
/**
|
|
682
|
+
* Over the limit, the call is refused with these words — the step's error, on the page — rather than answered
|
|
683
|
+
* `allowed: false` for the code to check: `ctx.rateLimit('make', { most: 5, perSeconds: 600, refuse: 'Wait ten minutes' })`.
|
|
684
|
+
*/
|
|
685
|
+
refuse?: string;
|
|
686
|
+
};
|
|
687
|
+
|
|
688
|
+
declare type SpaceCredentialProvider = 's3' | 'r2' | 'ssr' | 'custom' | 'smtp';
|
|
689
|
+
|
|
690
|
+
/**
|
|
691
|
+
* A space's own server code, run as code of its own — Node, its dependencies, its connections — beside the platform
|
|
692
|
+
* that serves the space: a runtime of the space, where the sandbox's functions are not enough. `start` is handed its
|
|
693
|
+
* variables and answers what it serves.
|
|
694
|
+
*/
|
|
695
|
+
export declare type SpaceRuntime = {
|
|
696
|
+
start: (context: SpaceRuntimeContext) => SpaceRuntimeParts | Promise<SpaceRuntimeParts>;
|
|
697
|
+
};
|
|
698
|
+
|
|
699
|
+
/** What a runtime starts with. */
|
|
700
|
+
export declare type SpaceRuntimeContext = {
|
|
701
|
+
/** Its variables: those the platform keeps for it (`REDIS_URL`…), or a self-hosted server's own environment. */
|
|
702
|
+
env: Readonly<Record<string, string | undefined>>;
|
|
703
|
+
/** Where people reach the space — what a link written for a person, or an agent, is handed. */
|
|
704
|
+
publicUrl: string;
|
|
705
|
+
};
|
|
706
|
+
|
|
707
|
+
/**
|
|
708
|
+
* A path of the space's own answered by the runtime: a web handler, streaming if it likes — an agent's endpoint, a
|
|
709
|
+
* webhook that speaks a protocol of its own. The visitor's cookies and `Authorization` never reach it: the host's are
|
|
710
|
+
* the platform's.
|
|
711
|
+
*/
|
|
712
|
+
export declare type SpaceRuntimeEndpoint = (request: Request) => Response | Promise<Response>;
|
|
713
|
+
|
|
714
|
+
/** What a started runtime is made of. */
|
|
715
|
+
export declare type SpaceRuntimeParts = {
|
|
716
|
+
/** Its tasks and `/fn/` routes, run with a `ctx` that is the platform's — the space's `kv`, its channels, its key. */
|
|
717
|
+
functions?: FunctionsDefinition;
|
|
718
|
+
/** Paths it answers itself, each with everything beneath it (`/mcp` answers `/mcp/…`) — see {@link endpointProblem}. */
|
|
719
|
+
endpoints?: Record<string, SpaceRuntimeEndpoint>;
|
|
720
|
+
/** Called when it is stopped: close what `start` opened. */
|
|
721
|
+
close?: () => Promise<void>;
|
|
722
|
+
};
|
|
723
|
+
|
|
724
|
+
export { }
|