@funcd-dev/shim 0.14.0 → 0.16.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/invoke.d.ts +8 -0
- package/dist/invoke.js +72 -0
- package/dist/types.d.ts +21 -0
- package/dist/types.js +9 -1
- package/package.json +1 -1
package/dist/invoke.d.ts
CHANGED
|
@@ -16,3 +16,11 @@ export interface InvokeOptions {
|
|
|
16
16
|
bound?: number;
|
|
17
17
|
}
|
|
18
18
|
export declare function makeInvoke(opts?: InvokeOptions): <I = unknown, O = unknown>(alias: string, input: I) => Promise<O>;
|
|
19
|
+
/** The local API header that marks a fetch answer as the target's reply; a call funcd refused carries none. */
|
|
20
|
+
export declare const FETCH_REPLY_HEADER = "x-funcd-fetch-reply";
|
|
21
|
+
/** Build the context.fetch implementation (funcd ADR-0243). The returned function sends the request to
|
|
22
|
+
* /fetch/<alias><path> over the worker-node UDS, which funcd forwards to the linked Function that serves HTTP, and
|
|
23
|
+
* resolves with its whole reply as a Response, whatever its status. It rejects, as fetch does when no reply comes,
|
|
24
|
+
* when funcd refuses the call before it reaches the target (no link → 403, a target that does not serve HTTP → 409,
|
|
25
|
+
* the nested in-flight cap → 429), when the request cannot be sent, and when its reply is cut off. */
|
|
26
|
+
export declare function makeFetch(opts?: InvokeOptions): (alias: string, path: string, init?: RequestInit) => Promise<Response>;
|
package/dist/invoke.js
CHANGED
|
@@ -67,3 +67,75 @@ export function makeInvoke(opts = {}) {
|
|
|
67
67
|
req.end(body);
|
|
68
68
|
});
|
|
69
69
|
}
|
|
70
|
+
/** The statuses whose Response carries no body. */
|
|
71
|
+
const NULL_BODY = new Set([101, 204, 205, 304]);
|
|
72
|
+
/** The local API header that marks a fetch answer as the target's reply; a call funcd refused carries none. */
|
|
73
|
+
export const FETCH_REPLY_HEADER = 'x-funcd-fetch-reply';
|
|
74
|
+
/** Build the context.fetch implementation (funcd ADR-0243). The returned function sends the request to
|
|
75
|
+
* /fetch/<alias><path> over the worker-node UDS, which funcd forwards to the linked Function that serves HTTP, and
|
|
76
|
+
* resolves with its whole reply as a Response, whatever its status. It rejects, as fetch does when no reply comes,
|
|
77
|
+
* when funcd refuses the call before it reaches the target (no link → 403, a target that does not serve HTTP → 409,
|
|
78
|
+
* the nested in-flight cap → 429), when the request cannot be sent, and when its reply is cut off. */
|
|
79
|
+
export function makeFetch(opts = {}) {
|
|
80
|
+
return async (alias, path, init) => {
|
|
81
|
+
// ADR-0165: the CLIENT span brackets the whole call, so it opens before any check that can fail.
|
|
82
|
+
const span = startClientSpan(opts.sink ?? null, alias, opts.member, opts.bound);
|
|
83
|
+
const fail = (err, httpStatus) => {
|
|
84
|
+
span?.end('ERROR', err.message, httpStatus);
|
|
85
|
+
throw err;
|
|
86
|
+
};
|
|
87
|
+
const socketPath = process.env.FUNCD_INVOKE_SOCKET;
|
|
88
|
+
if (!socketPath) {
|
|
89
|
+
return fail(new Error('context.fetch: worker-node local API socket unavailable (FUNCD_INVOKE_SOCKET unset)'));
|
|
90
|
+
}
|
|
91
|
+
if (!path.startsWith('/')) {
|
|
92
|
+
return fail(new Error(`context.fetch("${alias}"): path "${path}" must start with "/"`));
|
|
93
|
+
}
|
|
94
|
+
const request = new Request(new URL(path, 'http://funcd.local'), init);
|
|
95
|
+
const body = request.body ? Buffer.from(await request.arrayBuffer()) : undefined;
|
|
96
|
+
const headers = {};
|
|
97
|
+
request.headers.forEach((value, key) => {
|
|
98
|
+
headers[key] = value;
|
|
99
|
+
});
|
|
100
|
+
if (body)
|
|
101
|
+
headers['content-length'] = String(body.length);
|
|
102
|
+
if (opts.member)
|
|
103
|
+
headers[MEMBER_HEADER] = opts.member;
|
|
104
|
+
if (span)
|
|
105
|
+
headers.traceparent = span.traceparent;
|
|
106
|
+
const target = new URL(request.url);
|
|
107
|
+
const reply = await new Promise((resolve, reject) => {
|
|
108
|
+
const req = http.request({
|
|
109
|
+
socketPath,
|
|
110
|
+
path: `/fetch/${encodeURIComponent(alias)}${target.pathname}${target.search}`,
|
|
111
|
+
method: request.method,
|
|
112
|
+
headers,
|
|
113
|
+
}, (res) => {
|
|
114
|
+
const chunks = [];
|
|
115
|
+
res.on('error', (err) => reject(new Error(`context.fetch("${alias}") failed: connection closed before the reply ended`, { cause: err })));
|
|
116
|
+
res.on('data', (c) => chunks.push(c));
|
|
117
|
+
res.on('end', () => {
|
|
118
|
+
const replyHeaders = new Headers();
|
|
119
|
+
for (const [key, value] of Object.entries(res.headers)) {
|
|
120
|
+
for (const v of Array.isArray(value) ? value : [value]) {
|
|
121
|
+
if (v !== undefined)
|
|
122
|
+
replyHeaders.append(key, v);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
resolve({ status: res.statusCode ?? 0, headers: replyHeaders, body: Buffer.concat(chunks) });
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
req.on('error', (err) => reject(new Error(`context.fetch("${alias}") failed: ${err.message}`, { cause: err })));
|
|
129
|
+
req.end(body);
|
|
130
|
+
}).catch((err) => fail(err));
|
|
131
|
+
if (!reply.headers.has(FETCH_REPLY_HEADER)) {
|
|
132
|
+
return fail(new Error(`context.fetch("${alias}") failed: ${reply.status} ${reply.body.toString('utf8')}`), reply.status);
|
|
133
|
+
}
|
|
134
|
+
reply.headers.delete(FETCH_REPLY_HEADER);
|
|
135
|
+
span?.end(reply.status >= 500 ? 'ERROR' : 'OK', '', reply.status);
|
|
136
|
+
return new Response(NULL_BODY.has(reply.status) ? null : new Uint8Array(reply.body), {
|
|
137
|
+
status: reply.status,
|
|
138
|
+
headers: reply.headers,
|
|
139
|
+
});
|
|
140
|
+
};
|
|
141
|
+
}
|
package/dist/types.d.ts
CHANGED
|
@@ -30,6 +30,11 @@ export interface FunctionContext {
|
|
|
30
30
|
* `data`, and the other keys are dropped. To send an object that has its own `data` key, wrap it in an
|
|
31
31
|
* envelope: `{ specversion: '1.0', data: { data: [1], page: 2 } }`. */
|
|
32
32
|
invoke<I = unknown, O = unknown>(alias: string, input: I): Promise<O>;
|
|
33
|
+
/** Send an HTTP request to a linked Function that serves HTTP (spec.protocol: http) by its spec.links alias
|
|
34
|
+
* (funcd ADR-0243). `path` starts with "/" and may carry a query. The reply comes back whole, at most 1 MiB, with
|
|
35
|
+
* its status and headers; funcd answers an error status, and calls nothing, for an undeclared link or a target
|
|
36
|
+
* that does not serve HTTP (call it with invoke). */
|
|
37
|
+
fetch(alias: string, path: string, init?: RequestInit): Promise<Response>;
|
|
33
38
|
/** Namespace-scoped key-value storage (ADR-0069): get/put/del a binding's key, or list keys. */
|
|
34
39
|
kv: KVClient;
|
|
35
40
|
/** Binding-scoped blob storage (ADR-0127): get/put/del/list a bound prefix's objects, or mint a
|
|
@@ -43,6 +48,22 @@ export interface FunctionContext {
|
|
|
43
48
|
}
|
|
44
49
|
/** A function handler: receives the context + CloudEvent, returns a response (or nothing). */
|
|
45
50
|
export type Handler<In = unknown, Out = unknown> = (context: FunctionContext, event: CloudEvent<In>) => Out | Promise<Out>;
|
|
51
|
+
/** An HTTP app (funcd ADR-0243): Hono, or any object whose fetch answers a Web Request. */
|
|
52
|
+
export interface HttpApp {
|
|
53
|
+
fetch(request: Request, env: {
|
|
54
|
+
funcd: FunctionContext;
|
|
55
|
+
}): Response | Promise<Response>;
|
|
56
|
+
}
|
|
57
|
+
/** The tag http(app) puts on its handler. A global symbol, so the copy of http() bundled into a function and the
|
|
58
|
+
* shim's own agree. */
|
|
59
|
+
export declare const HTTP_HANDLER: unique symbol;
|
|
60
|
+
/** What a Function with spec.protocol: http exports: the shim calls it with each request it rebuilds. */
|
|
61
|
+
export type HttpHandler = ((request: Request, context: FunctionContext) => Response | Promise<Response>) & {
|
|
62
|
+
readonly [HTTP_HANDLER]: true;
|
|
63
|
+
};
|
|
64
|
+
/** http makes a fetch app the export of a Function with spec.protocol: http (funcd ADR-0243):
|
|
65
|
+
* `export const handle = http(app)`. A Hono route reaches the function context as `c.env.funcd`. */
|
|
66
|
+
export declare function http(app: HttpApp): HttpHandler;
|
|
46
67
|
/** Json — the explicit "arbitrary JSON value" contract type (ADR-0058). Declare `FuncInput`/
|
|
47
68
|
* `FuncOutput = Json`, or a field `payload: Json`, when the shape is genuinely unknown; the
|
|
48
69
|
* generated contract is the empty schema `{}` (accepts any JSON). Typed as `unknown` so the
|
package/dist/types.js
CHANGED
|
@@ -1 +1,9 @@
|
|
|
1
|
-
|
|
1
|
+
/** The tag http(app) puts on its handler. A global symbol, so the copy of http() bundled into a function and the
|
|
2
|
+
* shim's own agree. */
|
|
3
|
+
export const HTTP_HANDLER = Symbol.for('funcd.http');
|
|
4
|
+
/** http makes a fetch app the export of a Function with spec.protocol: http (funcd ADR-0243):
|
|
5
|
+
* `export const handle = http(app)`. A Hono route reaches the function context as `c.env.funcd`. */
|
|
6
|
+
export function http(app) {
|
|
7
|
+
const handler = (request, context) => app.fetch(request, { funcd: context });
|
|
8
|
+
return Object.assign(handler, { [HTTP_HANDLER]: true });
|
|
9
|
+
}
|