@funcd-dev/shim 0.6.0 → 0.7.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/blob.d.ts +3 -2
- package/dist/blob.js +8 -3
- package/dist/invoke.d.ts +12 -0
- package/dist/invoke.js +56 -0
- package/dist/kv.d.ts +3 -2
- package/dist/kv.js +8 -3
- package/package.json +1 -1
package/dist/blob.d.ts
CHANGED
|
@@ -13,5 +13,6 @@ export interface BlobClient {
|
|
|
13
13
|
list(binding: string, prefix?: string): Promise<string[]>;
|
|
14
14
|
signedUrl(binding: string, key: string, opts?: SignOptions): Promise<string>;
|
|
15
15
|
}
|
|
16
|
-
/** Build the context.blob client.
|
|
17
|
-
|
|
16
|
+
/** Build the context.blob client. In a pool, `member` names the calling function on every request
|
|
17
|
+
* (funcd checks it against the pool's members); the solo shim passes none. */
|
|
18
|
+
export declare function makeBlob(member?: string): BlobClient;
|
package/dist/blob.js
CHANGED
|
@@ -4,7 +4,8 @@
|
|
|
4
4
|
// spec.blob). Reached only through the built-in shim — no @aws-sdk, no keypair. The blob twin of context.kv;
|
|
5
5
|
// v1 is bytes-in-memory (streaming is a v2 follow-up).
|
|
6
6
|
import http from 'node:http';
|
|
7
|
-
|
|
7
|
+
import { MEMBER_HEADER } from "./invoke.js";
|
|
8
|
+
function send(method, path, body, member) {
|
|
8
9
|
return new Promise((resolve, reject) => {
|
|
9
10
|
const socketPath = process.env.FUNCD_INVOKE_SOCKET;
|
|
10
11
|
if (!socketPath) {
|
|
@@ -14,6 +15,8 @@ function request(method, path, body) {
|
|
|
14
15
|
const headers = {};
|
|
15
16
|
if (body)
|
|
16
17
|
headers['content-length'] = body.byteLength;
|
|
18
|
+
if (member)
|
|
19
|
+
headers[MEMBER_HEADER] = member;
|
|
17
20
|
const req = http.request({ socketPath, path, method, headers }, (res) => {
|
|
18
21
|
const chunks = [];
|
|
19
22
|
// A connection that drops mid-body errors the response, not the request, and never ends it.
|
|
@@ -43,8 +46,10 @@ const json = (verb, r, text) => {
|
|
|
43
46
|
});
|
|
44
47
|
}
|
|
45
48
|
};
|
|
46
|
-
/** Build the context.blob client.
|
|
47
|
-
|
|
49
|
+
/** Build the context.blob client. In a pool, `member` names the calling function on every request
|
|
50
|
+
* (funcd checks it against the pool's members); the solo shim passes none. */
|
|
51
|
+
export function makeBlob(member) {
|
|
52
|
+
const request = (method, path, body) => send(method, path, body, member);
|
|
48
53
|
return {
|
|
49
54
|
async get(binding, key) {
|
|
50
55
|
const r = await request('GET', keyPath(binding, key));
|
package/dist/invoke.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/** Build the context.invoke implementation. The returned function POSTs `input` to
|
|
2
|
+
* /invoke/<alias> over the worker-node UDS and resolves the target's JSON output, or rejects on a
|
|
3
|
+
* non-2xx (no link → 403, unknown target → 404, bad input → 422, the target's nested in-flight cap
|
|
4
|
+
* reached → 429, target down/timeout → 503) or on a 2xx body that is not JSON. */
|
|
5
|
+
/** The local API request header that names the calling pool member; funcd serves the call as that
|
|
6
|
+
* member when it belongs to the pool, else 403. A solo sandbox sends none. */
|
|
7
|
+
export declare const MEMBER_HEADER = "X-Funcd-Member";
|
|
8
|
+
export interface InvokeOptions {
|
|
9
|
+
/** The pool member making the calls; unset in the solo shim. */
|
|
10
|
+
member?: string;
|
|
11
|
+
}
|
|
12
|
+
export declare function makeInvoke(opts?: InvokeOptions): <I = unknown, O = unknown>(alias: string, input: I) => Promise<O>;
|
package/dist/invoke.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
// Synchronous fn-to-fn invoke over the worker-node local API (HTTP-over-UDS, ADR-0064). The
|
|
2
|
+
// platform bind-mounts the per-sandbox socket and provides its path via FUNCD_INVOKE_SOCKET; the
|
|
3
|
+
// handler calls context.invoke(alias, input) and the platform brokers the call to the linked target.
|
|
4
|
+
import http from 'node:http';
|
|
5
|
+
/** Build the context.invoke implementation. The returned function POSTs `input` to
|
|
6
|
+
* /invoke/<alias> over the worker-node UDS and resolves the target's JSON output, or rejects on a
|
|
7
|
+
* non-2xx (no link → 403, unknown target → 404, bad input → 422, the target's nested in-flight cap
|
|
8
|
+
* reached → 429, target down/timeout → 503) or on a 2xx body that is not JSON. */
|
|
9
|
+
/** The local API request header that names the calling pool member; funcd serves the call as that
|
|
10
|
+
* member when it belongs to the pool, else 403. A solo sandbox sends none. */
|
|
11
|
+
export const MEMBER_HEADER = 'X-Funcd-Member';
|
|
12
|
+
export function makeInvoke(opts = {}) {
|
|
13
|
+
return (alias, input) => new Promise((resolve, reject) => {
|
|
14
|
+
const socketPath = process.env.FUNCD_INVOKE_SOCKET;
|
|
15
|
+
if (!socketPath) {
|
|
16
|
+
reject(new Error('context.invoke: worker-node local API socket unavailable (FUNCD_INVOKE_SOCKET unset)'));
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
const body = JSON.stringify(input ?? null);
|
|
20
|
+
const headers = {
|
|
21
|
+
'content-type': 'application/json',
|
|
22
|
+
'content-length': Buffer.byteLength(body),
|
|
23
|
+
};
|
|
24
|
+
if (opts.member)
|
|
25
|
+
headers[MEMBER_HEADER] = opts.member;
|
|
26
|
+
const req = http.request({
|
|
27
|
+
socketPath,
|
|
28
|
+
path: `/invoke/${encodeURIComponent(alias)}`,
|
|
29
|
+
method: 'POST',
|
|
30
|
+
headers,
|
|
31
|
+
}, (res) => {
|
|
32
|
+
const chunks = [];
|
|
33
|
+
// A connection that drops mid-body errors the response, not the request, and never ends it.
|
|
34
|
+
res.on('error', (err) => reject(new Error(`context.invoke("${alias}") failed: connection closed before the reply ended`, { cause: err })));
|
|
35
|
+
res.on('data', (c) => chunks.push(c));
|
|
36
|
+
res.on('end', () => {
|
|
37
|
+
const text = Buffer.concat(chunks).toString('utf8');
|
|
38
|
+
const status = res.statusCode ?? 0;
|
|
39
|
+
if (status >= 200 && status < 300) {
|
|
40
|
+
// This listener runs outside the Promise executor: a parse error thrown here would be uncaught.
|
|
41
|
+
try {
|
|
42
|
+
resolve((text ? JSON.parse(text) : null));
|
|
43
|
+
}
|
|
44
|
+
catch (err) {
|
|
45
|
+
reject(new Error(`context.invoke("${alias}") failed: ${status} reply is not JSON: ${text}`, { cause: err }));
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
reject(new Error(`context.invoke("${alias}") failed: ${status} ${text}`));
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
req.on('error', (err) => reject(new Error(`context.invoke("${alias}") failed: ${err.message}`, { cause: err })));
|
|
54
|
+
req.end(body);
|
|
55
|
+
});
|
|
56
|
+
}
|
package/dist/kv.d.ts
CHANGED
|
@@ -11,5 +11,6 @@ export interface KVClient {
|
|
|
11
11
|
del(binding: string, key: string): Promise<void>;
|
|
12
12
|
list(binding: string, prefix?: string): Promise<string[]>;
|
|
13
13
|
}
|
|
14
|
-
/** Build the context.kv client.
|
|
15
|
-
|
|
14
|
+
/** Build the context.kv client. In a pool, `member` names the calling function on every request
|
|
15
|
+
* (funcd checks it against the pool's members); the solo shim passes none. */
|
|
16
|
+
export declare function makeKV(member?: string): KVClient;
|
package/dist/kv.js
CHANGED
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
// same per-sandbox socket as context.invoke (FUNCD_INVOKE_SOCKET); the platform routes /kv/… to the
|
|
3
3
|
// PDP-authorized Facade with the sandbox's namespace identity. Reached only through the built-in shim.
|
|
4
4
|
import http from 'node:http';
|
|
5
|
-
|
|
5
|
+
import { MEMBER_HEADER } from "./invoke.js";
|
|
6
|
+
function send(method, path, body, member) {
|
|
6
7
|
return new Promise((resolve, reject) => {
|
|
7
8
|
const socketPath = process.env.FUNCD_INVOKE_SOCKET;
|
|
8
9
|
if (!socketPath) {
|
|
@@ -12,6 +13,8 @@ function request(method, path, body) {
|
|
|
12
13
|
const headers = {};
|
|
13
14
|
if (body)
|
|
14
15
|
headers['content-length'] = body.byteLength;
|
|
16
|
+
if (member)
|
|
17
|
+
headers[MEMBER_HEADER] = member;
|
|
15
18
|
const req = http.request({ socketPath, path, method, headers }, (res) => {
|
|
16
19
|
const chunks = [];
|
|
17
20
|
// A connection that drops mid-body errors the response, not the request, and never ends it.
|
|
@@ -41,8 +44,10 @@ const json = (verb, r, text) => {
|
|
|
41
44
|
});
|
|
42
45
|
}
|
|
43
46
|
};
|
|
44
|
-
/** Build the context.kv client.
|
|
45
|
-
|
|
47
|
+
/** Build the context.kv client. In a pool, `member` names the calling function on every request
|
|
48
|
+
* (funcd checks it against the pool's members); the solo shim passes none. */
|
|
49
|
+
export function makeKV(member) {
|
|
50
|
+
const request = (method, path, body) => send(method, path, body, member);
|
|
46
51
|
return {
|
|
47
52
|
async get(binding, key) {
|
|
48
53
|
const r = await request('GET', keyPath(binding, key));
|