@funcd-dev/shim 0.33.0 → 0.35.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/dist/job.d.ts +58 -0
- package/dist/job.js +75 -0
- package/dist/types.d.ts +12 -1
- package/package.json +1 -1
package/dist/job.d.ts
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { Json } from './types.ts';
|
|
2
|
+
/** funcd's problem, the body of a refusal (RFC 9457). */
|
|
3
|
+
export interface JobProblem {
|
|
4
|
+
type: string;
|
|
5
|
+
title: string;
|
|
6
|
+
status: number;
|
|
7
|
+
detail: string;
|
|
8
|
+
}
|
|
9
|
+
/** A start or a read of a run that funcd refused. */
|
|
10
|
+
export declare class JobError extends Error {
|
|
11
|
+
/** The HTTP status of the refusal. */
|
|
12
|
+
readonly status: number;
|
|
13
|
+
/** funcd's problem. */
|
|
14
|
+
readonly problem: JobProblem;
|
|
15
|
+
constructor(call: string, problem: JobProblem);
|
|
16
|
+
}
|
|
17
|
+
/** A run of a Job, as context.job.get reads it. */
|
|
18
|
+
export interface JobRunState {
|
|
19
|
+
/** The run's name. */
|
|
20
|
+
name: string;
|
|
21
|
+
/** Pending, Previewing, AwaitingApproval, Running, Succeeded, Failed, Cancelled or Rejected. */
|
|
22
|
+
phase: string;
|
|
23
|
+
/** Why the run is in its phase, empty while it runs. */
|
|
24
|
+
reason: string;
|
|
25
|
+
/** When the run started running, an RFC 3339 time; null before. */
|
|
26
|
+
startedAt: string | null;
|
|
27
|
+
/** When the run ended, an RFC 3339 time; null before. */
|
|
28
|
+
endedAt: string | null;
|
|
29
|
+
/** The handler's answer once the run Succeeded; null before. */
|
|
30
|
+
result: Json;
|
|
31
|
+
}
|
|
32
|
+
export interface JobClient {
|
|
33
|
+
/** Starts a run of the Job that the function binds under alias, with input as the run's input, and returns the
|
|
34
|
+
* run's name at once, without waiting for the run. Without name, funcd names the run <job>-<id>, and a start sent
|
|
35
|
+
* again starts a second run; with name, a start sent again with the same input returns that run, so a function
|
|
36
|
+
* called more than once for one event, as at-least-once delivery does, names the run from the event's id. A run of
|
|
37
|
+
* a Job without approval.bypass waits for a person's approval. Rejects with a JobError: 403 when the function binds
|
|
38
|
+
* no Job under alias or a Policy forbids it, 404 when the Job does not exist, 409 when another run holds the name,
|
|
39
|
+
* 413 when the input passes job.payloadLimit, and 422 when it does not match the Job's input schema. */
|
|
40
|
+
start(alias: string, input?: unknown, name?: string): Promise<string>;
|
|
41
|
+
/** Reads run, a run of the Job that the function binds under alias: its phase, its reason, its times and, once it
|
|
42
|
+
* Succeeded, its result. Rejects with a JobError: 403 when the function binds no Job under alias or a Policy
|
|
43
|
+
* forbids it, and 404 when the run is not one of that Job's, or funcd removed it after job.retention. */
|
|
44
|
+
get(alias: string, run: string): Promise<JobRunState>;
|
|
45
|
+
}
|
|
46
|
+
/** An approval of a run's preview (funcd ADR-0348): who approved it, the digest the approval names, and the document
|
|
47
|
+
* of that preview. */
|
|
48
|
+
export interface Approval {
|
|
49
|
+
approvedBy: string;
|
|
50
|
+
digest: string;
|
|
51
|
+
preview: Json;
|
|
52
|
+
}
|
|
53
|
+
/** Build context.job. In a pool, `member` names the calling function on every request; the solo shim passes none. */
|
|
54
|
+
export declare function makeJob(member?: string): JobClient;
|
|
55
|
+
/** readApproval reads the approval of the caller's own run from funcd (funcd ADR-0348): null for a run with none, as a
|
|
56
|
+
* run of a Job with approval.bypass. It fails when funcd does not answer the run, so a handler never runs with an
|
|
57
|
+
* approval it could not read. */
|
|
58
|
+
export declare function readApproval(): Promise<Approval | null>;
|
package/dist/job.js
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
// context.job (funcd ADR-0349): a Function starts a run of a Job that it binds, and reads it back, through funcd's
|
|
2
|
+
// worker-node local API, which checks the binding and Policies and admits the run as a person's. A Job's worker also
|
|
3
|
+
// reads its own run there, with the approval its handler's context carries (funcd ADR-0348).
|
|
4
|
+
import { send } from "./socket.js";
|
|
5
|
+
/** A start or a read of a run that funcd refused. */
|
|
6
|
+
export class JobError extends Error {
|
|
7
|
+
/** The HTTP status of the refusal. */
|
|
8
|
+
status;
|
|
9
|
+
/** funcd's problem. */
|
|
10
|
+
problem;
|
|
11
|
+
constructor(call, problem) {
|
|
12
|
+
super(`${call} failed: ${problem.status} ${problem.detail}`);
|
|
13
|
+
this.name = 'JobError';
|
|
14
|
+
this.status = problem.status;
|
|
15
|
+
this.problem = problem;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
/** refusal is funcd's problem as a JobError. A body that is no problem, as the 422 of an input off the Job's contract,
|
|
19
|
+
* which answers as a Function's call does, keeps its text as the detail, as context.invoke's error does. */
|
|
20
|
+
function refusal(call, r) {
|
|
21
|
+
const text = r.body.toString('utf8');
|
|
22
|
+
let problem = { type: 'about:blank', title: '', status: r.status, detail: text };
|
|
23
|
+
try {
|
|
24
|
+
const parsed = JSON.parse(text);
|
|
25
|
+
problem = {
|
|
26
|
+
type: parsed.type ?? 'about:blank',
|
|
27
|
+
title: parsed.title ?? '',
|
|
28
|
+
status: parsed.status ?? r.status,
|
|
29
|
+
detail: parsed.detail ?? text,
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
catch {
|
|
33
|
+
// not JSON: keep its text
|
|
34
|
+
}
|
|
35
|
+
return new JobError(call, problem);
|
|
36
|
+
}
|
|
37
|
+
const parsed = (call, r) => {
|
|
38
|
+
try {
|
|
39
|
+
return JSON.parse(r.body.toString('utf8'));
|
|
40
|
+
}
|
|
41
|
+
catch (err) {
|
|
42
|
+
throw new Error(`${call} failed: ${r.status} reply is not JSON: ${err.message}`, { cause: err });
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
const ok = (r) => r.status >= 200 && r.status < 300;
|
|
46
|
+
/** Build context.job. In a pool, `member` names the calling function on every request; the solo shim passes none. */
|
|
47
|
+
export function makeJob(member) {
|
|
48
|
+
return {
|
|
49
|
+
async start(alias, input, name) {
|
|
50
|
+
const call = `context.job.start("${alias}")`;
|
|
51
|
+
const body = Buffer.from(JSON.stringify(name === undefined ? { input } : { input, name }), 'utf8');
|
|
52
|
+
const r = await send('context.job', 'POST', `/job/${encodeURIComponent(alias)}`, body, member, 'application/json');
|
|
53
|
+
if (!ok(r))
|
|
54
|
+
throw refusal(call, r);
|
|
55
|
+
return parsed(call, r).run;
|
|
56
|
+
},
|
|
57
|
+
async get(alias, run) {
|
|
58
|
+
const call = `context.job.get("${alias}", "${run}")`;
|
|
59
|
+
const r = await send('context.job', 'GET', `/job/${encodeURIComponent(alias)}/${encodeURIComponent(run)}`, undefined, member);
|
|
60
|
+
if (!ok(r))
|
|
61
|
+
throw refusal(call, r);
|
|
62
|
+
return parsed(call, r);
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
/** readApproval reads the approval of the caller's own run from funcd (funcd ADR-0348): null for a run with none, as a
|
|
67
|
+
* run of a Job with approval.bypass. It fails when funcd does not answer the run, so a handler never runs with an
|
|
68
|
+
* approval it could not read. */
|
|
69
|
+
export async function readApproval() {
|
|
70
|
+
const call = 'context.approval';
|
|
71
|
+
const r = await send(call, 'GET', '/job/run', undefined, undefined);
|
|
72
|
+
if (r.status !== 200)
|
|
73
|
+
throw refusal(call, r);
|
|
74
|
+
return parsed(call, r).approval ?? null;
|
|
75
|
+
}
|
package/dist/types.d.ts
CHANGED
|
@@ -5,6 +5,7 @@ import type { ConfigClient } from './config.ts';
|
|
|
5
5
|
import type { SecretClient } from './secret.ts';
|
|
6
6
|
import type { LakeClient } from './lake.ts';
|
|
7
7
|
import type { AddonClient } from './addon.ts';
|
|
8
|
+
import type { Approval, JobClient } from './job.ts';
|
|
8
9
|
export type { KVClient, KVEntry, PutOptions } from './kv.ts';
|
|
9
10
|
export type { BlobClient, SignOptions } from './blob.ts';
|
|
10
11
|
export type { SQLClient, SQLExecResult, SQLParam, SQLParams, SQLRow, SQLStatement, SQLValues, } from './sql.ts';
|
|
@@ -12,6 +13,7 @@ export type { ConfigClient } from './config.ts';
|
|
|
12
13
|
export type { SecretClient } from './secret.ts';
|
|
13
14
|
export type { LakeClient, LakeEndpoint } from './lake.ts';
|
|
14
15
|
export type { AddonClient, AddonError, AddonProblem } from './addon.ts';
|
|
16
|
+
export type { Approval, JobClient, JobError, JobProblem, JobRunState } from './job.ts';
|
|
15
17
|
/** A CloudEvent — the normalized trigger envelope (ADR-0023). */
|
|
16
18
|
export interface CloudEvent<T = unknown> {
|
|
17
19
|
id: string;
|
|
@@ -68,6 +70,8 @@ export interface FunctionContext {
|
|
|
68
70
|
/** The add-on objects that the function binds, by alias: fetch calls an object's engine through funcd, which serves
|
|
69
71
|
* only the paths that the object's edge entry serves. */
|
|
70
72
|
addon: AddonClient;
|
|
73
|
+
/** The Jobs that the function binds, by alias: start a run of one with an input, and read a run back. */
|
|
74
|
+
job: JobClient;
|
|
71
75
|
/** The whole milliseconds left before the call's deadline, 0 once it has passed, or null when the call has none. */
|
|
72
76
|
remainingMs(): number | null;
|
|
73
77
|
/** Who signed in to the call, when funcd's edge signed a user in with an OpenID Connect provider: provider is the
|
|
@@ -91,10 +95,17 @@ export interface RunInfo {
|
|
|
91
95
|
phase: 'run' | 'preview';
|
|
92
96
|
startedBy: string;
|
|
93
97
|
}
|
|
94
|
-
/** The context a Job's handler
|
|
98
|
+
/** The context a Job's handler and its preview get (funcd ADR-0347, ADR-0348): the FunctionContext plus its run and,
|
|
99
|
+
* in the run's call, the approval it runs under. */
|
|
95
100
|
export interface JobContext extends FunctionContext {
|
|
96
101
|
run: RunInfo;
|
|
102
|
+
/** The person's approval of the preview that this run runs: who approved it, the preview's digest and its document.
|
|
103
|
+
* Null in the preview call, and for a run of a Job with approval.bypass. */
|
|
104
|
+
approval: Approval | null;
|
|
97
105
|
}
|
|
106
|
+
/** A Job module's optional preview export (funcd ADR-0348): called in a run's preview call, before a person approves
|
|
107
|
+
* the run, it answers what the run would change, as JSON. A module with none previews its run's input. */
|
|
108
|
+
export type Preview<In = unknown> = (context: JobContext, event: CloudEvent<In>) => Json | Promise<Json>;
|
|
98
109
|
/** A function handler: receives the context + CloudEvent, returns a response (or nothing). */
|
|
99
110
|
export type Handler<In = unknown, Out = unknown> = (context: FunctionContext, event: CloudEvent<In>) => Out | Promise<Out>;
|
|
100
111
|
/** An HTTP app (funcd ADR-0243): Hono, or any object whose fetch answers a Web Request. */
|