@funcd-dev/shim 0.30.0 → 0.31.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/addon.d.ts +33 -0
- package/dist/addon.js +112 -0
- package/dist/types.d.ts +5 -0
- package/package.json +1 -1
package/dist/addon.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { type InvokeOptions } from './invoke.ts';
|
|
2
|
+
/** The local API header that marks an answer as the engine's; a call that funcd refused carries none. */
|
|
3
|
+
export declare const ADDON_REPLY_HEADER = "x-funcd-addon-reply";
|
|
4
|
+
/** funcd's problem, the body of a refusal (RFC 9457). */
|
|
5
|
+
export interface AddonProblem {
|
|
6
|
+
type: string;
|
|
7
|
+
title: string;
|
|
8
|
+
status: number;
|
|
9
|
+
detail: string;
|
|
10
|
+
}
|
|
11
|
+
/** A call that funcd refused before it reached the engine. */
|
|
12
|
+
export declare class AddonError extends Error {
|
|
13
|
+
/** The HTTP status of the refusal. */
|
|
14
|
+
readonly status: number;
|
|
15
|
+
/** funcd's problem. */
|
|
16
|
+
readonly problem: AddonProblem;
|
|
17
|
+
/** The seconds to wait before a retry, from the Retry-After of a 503, as while the namespace is held. */
|
|
18
|
+
readonly retryAfter?: number;
|
|
19
|
+
constructor(alias: string, problem: AddonProblem, retryAfter?: number);
|
|
20
|
+
}
|
|
21
|
+
export interface AddonClient {
|
|
22
|
+
/** Sends an HTTP request to the engine of the add-on object that the function binds under alias, through funcd, and
|
|
23
|
+
* returns the engine's answer as a Response, whatever its status; funcd follows no redirect. path starts with "/"
|
|
24
|
+
* and may carry a query, and init sets the method, the headers and the body, as for the global fetch. Rejects with
|
|
25
|
+
* an AddonError when funcd refuses the call: 403 for an alias the function does not bind or a call a Policy
|
|
26
|
+
* forbids, 404 for a path the object's edge entry does not serve, 413 for a request or an answer body over
|
|
27
|
+
* addons.callPayloadLimit, 1 MiB by default, and 503 while the object is not Ready, while its namespace is held
|
|
28
|
+
* (with retryAfter), or when the engine does not answer within addons.callTimeout, 10 seconds by default. */
|
|
29
|
+
fetch(alias: string, path: string, init?: RequestInit): Promise<Response>;
|
|
30
|
+
}
|
|
31
|
+
/** Build context.addon: fetch sends the request to /addon/<alias><path> on the local API, as the pool member when one
|
|
32
|
+
* is given, with the CLIENT span's traceparent. */
|
|
33
|
+
export declare function makeAddon(opts?: InvokeOptions): AddonClient;
|
package/dist/addon.js
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
// context.addon (funcd ADR-0338): a call of the engine of an add-on object that the function binds, through funcd's
|
|
2
|
+
// worker-node local API, which checks the binding and the path before it forwards the request.
|
|
3
|
+
import http from 'node:http';
|
|
4
|
+
import { MEMBER_HEADER } from "./invoke.js";
|
|
5
|
+
import { startClientSpan } from "./tracespan.js";
|
|
6
|
+
/** The local API header that marks an answer as the engine's; a call that funcd refused carries none. */
|
|
7
|
+
export const ADDON_REPLY_HEADER = 'x-funcd-addon-reply';
|
|
8
|
+
/** The statuses whose Response carries no body. */
|
|
9
|
+
const NULL_BODY = new Set([101, 204, 205, 304]);
|
|
10
|
+
/** A call that funcd refused before it reached the engine. */
|
|
11
|
+
export class AddonError extends Error {
|
|
12
|
+
/** The HTTP status of the refusal. */
|
|
13
|
+
status;
|
|
14
|
+
/** funcd's problem. */
|
|
15
|
+
problem;
|
|
16
|
+
/** The seconds to wait before a retry, from the Retry-After of a 503, as while the namespace is held. */
|
|
17
|
+
retryAfter;
|
|
18
|
+
constructor(alias, problem, retryAfter) {
|
|
19
|
+
super(`context.addon.fetch("${alias}") failed: ${problem.status} ${problem.detail}`);
|
|
20
|
+
this.name = 'AddonError';
|
|
21
|
+
this.status = problem.status;
|
|
22
|
+
this.problem = problem;
|
|
23
|
+
this.retryAfter = retryAfter;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
/** Build context.addon: fetch sends the request to /addon/<alias><path> on the local API, as the pool member when one
|
|
27
|
+
* is given, with the CLIENT span's traceparent. */
|
|
28
|
+
export function makeAddon(opts = {}) {
|
|
29
|
+
return {
|
|
30
|
+
async fetch(alias, path, init) {
|
|
31
|
+
const span = startClientSpan(opts.sink ?? null, alias, opts.member, opts.bound);
|
|
32
|
+
const fail = (err, httpStatus) => {
|
|
33
|
+
span?.end('ERROR', err.message, httpStatus);
|
|
34
|
+
throw err;
|
|
35
|
+
};
|
|
36
|
+
const socketPath = process.env.FUNCD_INVOKE_SOCKET;
|
|
37
|
+
if (!socketPath) {
|
|
38
|
+
return fail(new Error('context.addon: worker-node local API socket unavailable (FUNCD_INVOKE_SOCKET unset)'));
|
|
39
|
+
}
|
|
40
|
+
if (!path.startsWith('/')) {
|
|
41
|
+
return fail(new Error(`context.addon.fetch("${alias}"): path "${path}" must start with "/"`));
|
|
42
|
+
}
|
|
43
|
+
const request = new Request(new URL(path, 'http://funcd.local'), init);
|
|
44
|
+
const body = request.body ? Buffer.from(await request.arrayBuffer()) : undefined;
|
|
45
|
+
const headers = {};
|
|
46
|
+
request.headers.forEach((value, key) => {
|
|
47
|
+
headers[key] = value;
|
|
48
|
+
});
|
|
49
|
+
if (body)
|
|
50
|
+
headers['content-length'] = String(body.length);
|
|
51
|
+
if (opts.member)
|
|
52
|
+
headers[MEMBER_HEADER] = opts.member;
|
|
53
|
+
if (span)
|
|
54
|
+
headers.traceparent = span.traceparent;
|
|
55
|
+
const target = new URL(request.url);
|
|
56
|
+
const reply = await new Promise((resolve, reject) => {
|
|
57
|
+
const req = http.request({
|
|
58
|
+
socketPath,
|
|
59
|
+
path: `/addon/${encodeURIComponent(alias)}${target.pathname}${target.search}`,
|
|
60
|
+
method: request.method,
|
|
61
|
+
headers,
|
|
62
|
+
}, (res) => {
|
|
63
|
+
const chunks = [];
|
|
64
|
+
res.on('error', (err) => reject(new Error(`context.addon.fetch("${alias}") failed: connection closed before the reply ended`, {
|
|
65
|
+
cause: err,
|
|
66
|
+
})));
|
|
67
|
+
res.on('data', (c) => chunks.push(c));
|
|
68
|
+
res.on('end', () => {
|
|
69
|
+
const replyHeaders = new Headers();
|
|
70
|
+
for (const [key, value] of Object.entries(res.headers)) {
|
|
71
|
+
for (const v of Array.isArray(value) ? value : [value]) {
|
|
72
|
+
if (v !== undefined)
|
|
73
|
+
replyHeaders.append(key, v);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
resolve({ status: res.statusCode ?? 0, headers: replyHeaders, body: Buffer.concat(chunks) });
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
req.on('error', (err) => reject(new Error(`context.addon.fetch("${alias}") failed: ${err.message}`, { cause: err })));
|
|
80
|
+
req.end(body);
|
|
81
|
+
}).catch((err) => fail(err));
|
|
82
|
+
if (!reply.headers.has(ADDON_REPLY_HEADER)) {
|
|
83
|
+
return fail(refusal(alias, reply.status, reply.headers, reply.body), reply.status);
|
|
84
|
+
}
|
|
85
|
+
reply.headers.delete(ADDON_REPLY_HEADER);
|
|
86
|
+
span?.end(reply.status >= 500 ? 'ERROR' : 'OK', '', reply.status);
|
|
87
|
+
return new Response(NULL_BODY.has(reply.status) ? null : new Uint8Array(reply.body), {
|
|
88
|
+
status: reply.status,
|
|
89
|
+
headers: reply.headers,
|
|
90
|
+
});
|
|
91
|
+
},
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
/** refusal is funcd's problem as an AddonError; a body that is no problem keeps its text as the detail. */
|
|
95
|
+
function refusal(alias, status, headers, body) {
|
|
96
|
+
const text = body.toString('utf8');
|
|
97
|
+
let problem = { type: 'about:blank', title: '', status, detail: text };
|
|
98
|
+
try {
|
|
99
|
+
const parsed = JSON.parse(text);
|
|
100
|
+
problem = {
|
|
101
|
+
type: parsed.type ?? 'about:blank',
|
|
102
|
+
title: parsed.title ?? '',
|
|
103
|
+
status: parsed.status ?? status,
|
|
104
|
+
detail: parsed.detail ?? '',
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
catch {
|
|
108
|
+
// not a problem body: keep its text
|
|
109
|
+
}
|
|
110
|
+
const after = status === 503 ? Number.parseInt(headers.get('retry-after') ?? '', 10) : Number.NaN;
|
|
111
|
+
return new AddonError(alias, problem, Number.isFinite(after) ? after : undefined);
|
|
112
|
+
}
|
package/dist/types.d.ts
CHANGED
|
@@ -4,12 +4,14 @@ import type { SQLClient } from './sql.ts';
|
|
|
4
4
|
import type { ConfigClient } from './config.ts';
|
|
5
5
|
import type { SecretClient } from './secret.ts';
|
|
6
6
|
import type { LakeClient } from './lake.ts';
|
|
7
|
+
import type { AddonClient } from './addon.ts';
|
|
7
8
|
export type { KVClient, KVEntry, PutOptions } from './kv.ts';
|
|
8
9
|
export type { BlobClient, SignOptions } from './blob.ts';
|
|
9
10
|
export type { SQLClient, SQLExecResult, SQLParam, SQLParams, SQLRow, SQLStatement, SQLValues, } from './sql.ts';
|
|
10
11
|
export type { ConfigClient } from './config.ts';
|
|
11
12
|
export type { SecretClient } from './secret.ts';
|
|
12
13
|
export type { LakeClient, LakeEndpoint } from './lake.ts';
|
|
14
|
+
export type { AddonClient, AddonError, AddonProblem } from './addon.ts';
|
|
13
15
|
/** A CloudEvent — the normalized trigger envelope (ADR-0023). */
|
|
14
16
|
export interface CloudEvent<T = unknown> {
|
|
15
17
|
id: string;
|
|
@@ -63,6 +65,9 @@ export interface FunctionContext {
|
|
|
63
65
|
/** The Lakes that the function binds, by alias: the address of each Lake's gateway, and the token that the gateway
|
|
64
66
|
* accepts from the function. */
|
|
65
67
|
lake: LakeClient;
|
|
68
|
+
/** The add-on objects that the function binds, by alias: fetch calls an object's engine through funcd, which serves
|
|
69
|
+
* only the paths that the object's edge entry serves. */
|
|
70
|
+
addon: AddonClient;
|
|
66
71
|
/** The whole milliseconds left before the call's deadline, 0 once it has passed, or null when the call has none. */
|
|
67
72
|
remainingMs(): number | null;
|
|
68
73
|
/** Who signed in to the call, when funcd's edge signed a user in with an OpenID Connect provider: provider is the
|