@funcd-dev/shim 0.29.0 → 0.30.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/invcontext.d.ts +2 -0
- package/dist/invcontext.js +2 -1
- package/dist/principal.d.ts +10 -0
- package/dist/principal.js +37 -0
- package/dist/tracespan.d.ts +6 -4
- package/dist/tracespan.js +7 -5
- package/dist/types.d.ts +10 -0
- package/package.json +1 -1
package/dist/invcontext.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
2
|
+
import type { Principal } from './principal.ts';
|
|
2
3
|
/** The identity of the currently-executing invocation. */
|
|
3
4
|
export interface InvContext {
|
|
4
5
|
inv: string;
|
|
@@ -6,6 +7,7 @@ export interface InvContext {
|
|
|
6
7
|
spanId: string;
|
|
7
8
|
parentId: string;
|
|
8
9
|
deadline?: number;
|
|
10
|
+
principal?: Principal;
|
|
9
11
|
}
|
|
10
12
|
/** invStore holds the active InvContext for the duration of a handler call (and its async
|
|
11
13
|
* continuations). Per-thread: each worker/process imports its own instance, which is correct —
|
package/dist/invcontext.js
CHANGED
|
@@ -3,7 +3,8 @@
|
|
|
3
3
|
// buildRecord) tag their records with the SAME inv/trace/span ids — the logs↔trace correlation that
|
|
4
4
|
// closes ADR-0081's provenance open question. ADR-0081 specified this carrier but never shipped it
|
|
5
5
|
// (inv/trace_id/span_id were hardcoded empty); this is where it is introduced. It also carries the
|
|
6
|
-
// call's deadline, which ctx.remainingMs() reads (ADR-0226).
|
|
6
|
+
// call's deadline, which ctx.remainingMs() reads (ADR-0226), and its principal, which ctx.principal reads
|
|
7
|
+
// (funcd ADR-0335): the handler's context is one object for every call.
|
|
7
8
|
//
|
|
8
9
|
// Built-ins only (node:async_hooks) so it bundles into shim.mjs/pool.mjs.
|
|
9
10
|
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { FunctionContext } from './types.ts';
|
|
2
|
+
/** The principal of a signed-in call, as ctx.principal holds it. */
|
|
3
|
+
export type Principal = NonNullable<FunctionContext['principal']>;
|
|
4
|
+
/** The headers funcd's edge sets for a signed-in call. An http app receives them as sent. */
|
|
5
|
+
export declare const PRINCIPAL_HEADERS: ReadonlySet<string>;
|
|
6
|
+
/** principalFrom builds a call's principal from its headers: provider and claims from X-Funcd-Principal, the standard
|
|
7
|
+
* Base64 of the JSON {provider, claims}, id from X-Funcd-Principal-Id and name from X-Funcd-Principal-Name. It is
|
|
8
|
+
* undefined when X-Funcd-Principal or X-Funcd-Principal-Id is absent or empty, or when X-Funcd-Principal is not that
|
|
9
|
+
* encoding of an object with a provider name and an object of claims. */
|
|
10
|
+
export declare function principalFrom(headers: Headers): Principal | undefined;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/** The headers funcd's edge sets for a signed-in call. An http app receives them as sent. */
|
|
2
|
+
export const PRINCIPAL_HEADERS = new Set([
|
|
3
|
+
'x-funcd-principal',
|
|
4
|
+
'x-funcd-principal-id',
|
|
5
|
+
'x-funcd-principal-name',
|
|
6
|
+
'x-funcd-id-token',
|
|
7
|
+
]);
|
|
8
|
+
// Standard Base64 with its padding, the form the edge writes.
|
|
9
|
+
const BASE64 = /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/;
|
|
10
|
+
const isObject = (value) => typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
11
|
+
// Node reads each byte of a header value as one Latin-1 character; the edge writes the values as UTF-8.
|
|
12
|
+
const utf8 = (value) => Buffer.from(value, 'latin1').toString('utf8');
|
|
13
|
+
/** principalFrom builds a call's principal from its headers: provider and claims from X-Funcd-Principal, the standard
|
|
14
|
+
* Base64 of the JSON {provider, claims}, id from X-Funcd-Principal-Id and name from X-Funcd-Principal-Name. It is
|
|
15
|
+
* undefined when X-Funcd-Principal or X-Funcd-Principal-Id is absent or empty, or when X-Funcd-Principal is not that
|
|
16
|
+
* encoding of an object with a provider name and an object of claims. */
|
|
17
|
+
export function principalFrom(headers) {
|
|
18
|
+
const encoded = headers.get('x-funcd-principal');
|
|
19
|
+
const id = headers.get('x-funcd-principal-id');
|
|
20
|
+
if (!encoded || !id || !BASE64.test(encoded))
|
|
21
|
+
return undefined;
|
|
22
|
+
let value;
|
|
23
|
+
try {
|
|
24
|
+
value = JSON.parse(new TextDecoder('utf-8', { fatal: true }).decode(Buffer.from(encoded, 'base64')));
|
|
25
|
+
}
|
|
26
|
+
catch {
|
|
27
|
+
return undefined;
|
|
28
|
+
}
|
|
29
|
+
if (!isObject(value) || typeof value.provider !== 'string' || value.provider === '' || !isObject(value.claims)) {
|
|
30
|
+
return undefined;
|
|
31
|
+
}
|
|
32
|
+
const principal = { provider: value.provider, id: utf8(id), claims: value.claims };
|
|
33
|
+
const name = headers.get('x-funcd-principal-name');
|
|
34
|
+
if (name)
|
|
35
|
+
principal.name = utf8(name);
|
|
36
|
+
return principal;
|
|
37
|
+
}
|
package/dist/tracespan.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { type InvContext } from './invcontext.ts';
|
|
2
2
|
import { type Sink } from './funclog.ts';
|
|
3
|
+
import type { Principal } from './principal.ts';
|
|
3
4
|
/** parseTraceparent parses a W3C `traceparent` (`00-<trace32>-<span16>-<flags>`), returning the
|
|
4
5
|
* trace-id + parent span-id, or null when absent/malformed/all-zero (⇒ the caller mints a root). */
|
|
5
6
|
export declare function parseTraceparent(tp: string | undefined): {
|
|
@@ -9,8 +10,9 @@ export declare function parseTraceparent(tp: string | undefined): {
|
|
|
9
10
|
/** newInvContext establishes the invocation identity: adopt the traceparent's trace-id + parent
|
|
10
11
|
* span-id when present, else mint a root (fresh 16-byte trace). The span-id is the engine-provided
|
|
11
12
|
* one (ADR-0105, X-Funcd-Span-Id) when a valid hex16 is given, else freshly minted (direct invoke).
|
|
12
|
-
* `deadline` is the call's deadline (ADR-0226), absent without a valid X-Funcd-Timeout-Ms
|
|
13
|
-
|
|
13
|
+
* `deadline` is the call's deadline (ADR-0226), absent without a valid X-Funcd-Timeout-Ms; `principal` is who signed
|
|
14
|
+
* in to the call (funcd ADR-0335), absent when nobody did. */
|
|
15
|
+
export declare function newInvContext(tp: string | undefined, providedSpanId?: string, deadline?: number, principal?: Principal): InvContext;
|
|
14
16
|
/** A live invocation span: run the handler inside its context (so logs correlate), then end() it
|
|
15
17
|
* with the outcome (which emits the span record on the channel). end() is idempotent. */
|
|
16
18
|
export interface Span {
|
|
@@ -24,8 +26,8 @@ export interface Span {
|
|
|
24
26
|
* X-Funcd-Span-Id — else mint); `links` are fan-in edges (X-Funcd-Span-Links) attached to the span;
|
|
25
27
|
* `member` is the pool member, stamped as `funcd.member` (none in the solo shim); `bound` caps the span
|
|
26
28
|
* record line (FUNCD_FUNCLOG_MAX_RECORD_BYTES); `deadline` is the call's deadline, which ctx.remainingMs()
|
|
27
|
-
* reads (ADR-0226). */
|
|
28
|
-
export declare function startSpan(sink: Sink | null, name: string, tp: string | undefined, spanId?: string, links?: string[], member?: string, bound?: number, deadline?: number): Span;
|
|
29
|
+
* reads (ADR-0226); `principal` is who signed in to the call, which ctx.principal reads (funcd ADR-0335). */
|
|
30
|
+
export declare function startSpan(sink: Sink | null, name: string, tp: string | undefined, spanId?: string, links?: string[], member?: string, bound?: number, deadline?: number, principal?: Principal): Span;
|
|
29
31
|
/** The CLIENT span of one context.invoke call (ADR-0165). */
|
|
30
32
|
export interface ClientSpan {
|
|
31
33
|
/** 00-<caller trace>-<this span>-01, stamped on POST /invoke/{alias}. */
|
package/dist/tracespan.js
CHANGED
|
@@ -30,8 +30,9 @@ export function parseTraceparent(tp) {
|
|
|
30
30
|
/** newInvContext establishes the invocation identity: adopt the traceparent's trace-id + parent
|
|
31
31
|
* span-id when present, else mint a root (fresh 16-byte trace). The span-id is the engine-provided
|
|
32
32
|
* one (ADR-0105, X-Funcd-Span-Id) when a valid hex16 is given, else freshly minted (direct invoke).
|
|
33
|
-
* `deadline` is the call's deadline (ADR-0226), absent without a valid X-Funcd-Timeout-Ms
|
|
34
|
-
|
|
33
|
+
* `deadline` is the call's deadline (ADR-0226), absent without a valid X-Funcd-Timeout-Ms; `principal` is who signed
|
|
34
|
+
* in to the call (funcd ADR-0335), absent when nobody did. */
|
|
35
|
+
export function newInvContext(tp, providedSpanId, deadline, principal) {
|
|
35
36
|
const adopted = parseTraceparent(tp);
|
|
36
37
|
const spanId = providedSpanId && /^[0-9a-f]{16}$/.test(providedSpanId) ? providedSpanId : randomBytes(8).toString('hex');
|
|
37
38
|
return {
|
|
@@ -40,6 +41,7 @@ export function newInvContext(tp, providedSpanId, deadline) {
|
|
|
40
41
|
spanId,
|
|
41
42
|
parentId: adopted ? adopted.parentId : '',
|
|
42
43
|
deadline,
|
|
44
|
+
principal,
|
|
43
45
|
};
|
|
44
46
|
}
|
|
45
47
|
/** emitSpan writes one span record. `ids` names the span (trace, span, parent, inv): the invocation's
|
|
@@ -91,9 +93,9 @@ function boundSpan(rec, bound) {
|
|
|
91
93
|
* X-Funcd-Span-Id — else mint); `links` are fan-in edges (X-Funcd-Span-Links) attached to the span;
|
|
92
94
|
* `member` is the pool member, stamped as `funcd.member` (none in the solo shim); `bound` caps the span
|
|
93
95
|
* record line (FUNCD_FUNCLOG_MAX_RECORD_BYTES); `deadline` is the call's deadline, which ctx.remainingMs()
|
|
94
|
-
* reads (ADR-0226). */
|
|
95
|
-
export function startSpan(sink, name, tp, spanId, links = [], member, bound = DEFAULT_MAX_RECORD_BYTES, deadline) {
|
|
96
|
-
const inv = newInvContext(tp, spanId, deadline);
|
|
96
|
+
* reads (ADR-0226); `principal` is who signed in to the call, which ctx.principal reads (funcd ADR-0335). */
|
|
97
|
+
export function startSpan(sink, name, tp, spanId, links = [], member, bound = DEFAULT_MAX_RECORD_BYTES, deadline, principal) {
|
|
98
|
+
const inv = newInvContext(tp, spanId, deadline, principal);
|
|
97
99
|
const startNs = Date.now() * 1e6;
|
|
98
100
|
const t0 = process.hrtime.bigint();
|
|
99
101
|
const validLinks = links.filter((l) => /^[0-9a-f]{16}$/.test(l));
|
package/dist/types.d.ts
CHANGED
|
@@ -65,6 +65,16 @@ export interface FunctionContext {
|
|
|
65
65
|
lake: LakeClient;
|
|
66
66
|
/** The whole milliseconds left before the call's deadline, 0 once it has passed, or null when the call has none. */
|
|
67
67
|
remainingMs(): number | null;
|
|
68
|
+
/** Who signed in to the call, when funcd's edge signed a user in with an OpenID Connect provider: provider is the
|
|
69
|
+
* name of the OIDCProvider, id the user's subject (the sub claim), name the first of the claims name,
|
|
70
|
+
* preferred_username and email that is not empty, and claims the payload of the token that signed the user in, the
|
|
71
|
+
* ID token of a browser session or the Bearer token of an API call. Undefined when nobody signed in. */
|
|
72
|
+
principal?: {
|
|
73
|
+
provider: string;
|
|
74
|
+
id: string;
|
|
75
|
+
name?: string;
|
|
76
|
+
claims: Record<string, unknown>;
|
|
77
|
+
};
|
|
68
78
|
}
|
|
69
79
|
/** A function handler: receives the context + CloudEvent, returns a response (or nothing). */
|
|
70
80
|
export type Handler<In = unknown, Out = unknown> = (context: FunctionContext, event: CloudEvent<In>) => Out | Promise<Out>;
|