@funcd-dev/shim 0.10.0 → 0.11.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.
@@ -5,6 +5,7 @@ export interface InvContext {
5
5
  traceId: string;
6
6
  spanId: string;
7
7
  parentId: string;
8
+ deadline?: number;
8
9
  }
9
10
  /** invStore holds the active InvContext for the duration of a handler call (and its async
10
11
  * continuations). Per-thread: each worker/process imports its own instance, which is correct —
@@ -13,3 +14,13 @@ export declare const invStore: AsyncLocalStorage<InvContext>;
13
14
  /** currentInv returns the active invocation context, or undefined outside an invocation (e.g. a
14
15
  * pre-invocation log line) — in which case log records emit empty inv/trace/span (back-compat). */
15
16
  export declare function currentInv(): InvContext | undefined;
17
+ /** parseTimeoutMs reads a timeout in milliseconds: a decimal integer in 1..2_147_482_647, else undefined. */
18
+ export declare function parseTimeoutMs(value: string | undefined): number | undefined;
19
+ /** nowMs reads the deadline clock in milliseconds. process.hrtime is one monotonic clock for every thread of the
20
+ * process, so a deadline the pool host sets holds in the member's worker (ADR-0226). */
21
+ export declare function nowMs(): number;
22
+ /** deadlineFrom turns a valid X-Funcd-Timeout-Ms (funcd ADR-0151) into the call's deadline; undefined without one. */
23
+ export declare function deadlineFrom(header: string | undefined): number | undefined;
24
+ /** remainingMs is FunctionContext.remainingMs: the whole milliseconds left before the active call's deadline, floored
25
+ * at 0; null without a deadline. */
26
+ export declare function remainingMs(): number | null;
@@ -2,7 +2,8 @@
2
2
  // each handler call so BOTH the trace span (tracespan.ts) and the log capture (funclog.ts read it in
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
- // (inv/trace_id/span_id were hardcoded empty); this is where it is introduced.
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
7
  //
7
8
  // Built-ins only (node:async_hooks) so it bundles into shim.mjs/pool.mjs.
8
9
  import { AsyncLocalStorage } from 'node:async_hooks';
@@ -15,3 +16,28 @@ export const invStore = new AsyncLocalStorage();
15
16
  export function currentInv() {
16
17
  return invStore.getStore();
17
18
  }
19
+ // setTimeout's largest delay (2^31 - 1 ms) less the pool's 1 s margin.
20
+ const maxTimeoutMs = 2_147_482_647;
21
+ /** parseTimeoutMs reads a timeout in milliseconds: a decimal integer in 1..2_147_482_647, else undefined. */
22
+ export function parseTimeoutMs(value) {
23
+ if (value === undefined || !/^[0-9]+$/.test(value))
24
+ return undefined;
25
+ const ms = Number(value);
26
+ return ms >= 1 && ms <= maxTimeoutMs ? ms : undefined;
27
+ }
28
+ /** nowMs reads the deadline clock in milliseconds. process.hrtime is one monotonic clock for every thread of the
29
+ * process, so a deadline the pool host sets holds in the member's worker (ADR-0226). */
30
+ export function nowMs() {
31
+ return Number(process.hrtime.bigint()) / 1e6;
32
+ }
33
+ /** deadlineFrom turns a valid X-Funcd-Timeout-Ms (funcd ADR-0151) into the call's deadline; undefined without one. */
34
+ export function deadlineFrom(header) {
35
+ const ms = parseTimeoutMs(header);
36
+ return ms === undefined ? undefined : nowMs() + ms;
37
+ }
38
+ /** remainingMs is FunctionContext.remainingMs: the whole milliseconds left before the active call's deadline, floored
39
+ * at 0; null without a deadline. */
40
+ export function remainingMs() {
41
+ const deadline = currentInv()?.deadline;
42
+ return deadline === undefined ? null : Math.max(0, Math.floor(deadline - nowMs()));
43
+ }
@@ -8,8 +8,9 @@ export declare function parseTraceparent(tp: string | undefined): {
8
8
  } | null;
9
9
  /** newInvContext establishes the invocation identity: adopt the traceparent's trace-id + parent
10
10
  * span-id when present, else mint a root (fresh 16-byte trace). The span-id is the engine-provided
11
- * one (ADR-0105, X-Funcd-Span-Id) when a valid hex16 is given, else freshly minted (direct invoke). */
12
- export declare function newInvContext(tp: string | undefined, providedSpanId?: string): InvContext;
11
+ * 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
+ export declare function newInvContext(tp: string | undefined, providedSpanId?: string, deadline?: number): InvContext;
13
14
  /** A live invocation span: run the handler inside its context (so logs correlate), then end() it
14
15
  * with the outcome (which emits the span record on the channel). end() is idempotent. */
15
16
  export interface Span {
@@ -22,8 +23,9 @@ export interface Span {
22
23
  * the incoming `traceparent`; `spanId` is the engine-provided span-id to USE (ADR-0105,
23
24
  * X-Funcd-Span-Id — else mint); `links` are fan-in edges (X-Funcd-Span-Links) attached to the span;
24
25
  * `member` is the pool member, stamped as `funcd.member` (none in the solo shim); `bound` caps the span
25
- * record line (FUNCD_FUNCLOG_MAX_RECORD_BYTES). */
26
- export declare function startSpan(sink: Sink | null, name: string, tp: string | undefined, spanId?: string, links?: string[], member?: string, bound?: number): Span;
26
+ * 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;
27
29
  /** The CLIENT span of one context.invoke call (ADR-0165). */
28
30
  export interface ClientSpan {
29
31
  /** 00-<caller trace>-<this span>-01, stamped on POST /invoke/{alias}. */
package/dist/tracespan.js CHANGED
@@ -29,8 +29,9 @@ export function parseTraceparent(tp) {
29
29
  }
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
- * one (ADR-0105, X-Funcd-Span-Id) when a valid hex16 is given, else freshly minted (direct invoke). */
33
- export function newInvContext(tp, providedSpanId) {
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
+ export function newInvContext(tp, providedSpanId, deadline) {
34
35
  const adopted = parseTraceparent(tp);
35
36
  const spanId = providedSpanId && /^[0-9a-f]{16}$/.test(providedSpanId) ? providedSpanId : randomBytes(8).toString('hex');
36
37
  return {
@@ -38,6 +39,7 @@ export function newInvContext(tp, providedSpanId) {
38
39
  traceId: adopted ? adopted.traceId : randomBytes(16).toString('hex'),
39
40
  spanId,
40
41
  parentId: adopted ? adopted.parentId : '',
42
+ deadline,
41
43
  };
42
44
  }
43
45
  /** emitSpan writes one span record. `ids` names the span (trace, span, parent, inv): the invocation's
@@ -88,9 +90,10 @@ function boundSpan(rec, bound) {
88
90
  * the incoming `traceparent`; `spanId` is the engine-provided span-id to USE (ADR-0105,
89
91
  * X-Funcd-Span-Id — else mint); `links` are fan-in edges (X-Funcd-Span-Links) attached to the span;
90
92
  * `member` is the pool member, stamped as `funcd.member` (none in the solo shim); `bound` caps the span
91
- * record line (FUNCD_FUNCLOG_MAX_RECORD_BYTES). */
92
- export function startSpan(sink, name, tp, spanId, links = [], member, bound = DEFAULT_MAX_RECORD_BYTES) {
93
- const inv = newInvContext(tp, spanId);
93
+ * 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);
94
97
  const startNs = Date.now() * 1e6;
95
98
  const t0 = process.hrtime.bigint();
96
99
  const validLinks = links.filter((l) => /^[0-9a-f]{16}$/.test(l));
package/dist/types.d.ts CHANGED
@@ -33,6 +33,8 @@ export interface FunctionContext {
33
33
  /** Binding-scoped blob storage (ADR-0127): get/put/del/list a bound prefix's objects, or mint a
34
34
  * presigned URL — the blob twin of kv. */
35
35
  blob: BlobClient;
36
+ /** Milliseconds left before the call's deadline (X-Funcd-Timeout-Ms), floored at 0; null without one (ADR-0226). */
37
+ remainingMs(): number | null;
36
38
  }
37
39
  /** A function handler: receives the context + CloudEvent, returns a response (or nothing). */
38
40
  export type Handler<In = unknown, Out = unknown> = (context: FunctionContext, event: CloudEvent<In>) => Out | Promise<Out>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@funcd-dev/shim",
3
- "version": "0.10.0",
3
+ "version": "0.11.0",
4
4
  "type": "module",
5
5
  "description": "Types and the contract build for writing funcd functions in TypeScript",
6
6
  "license": "Apache-2.0",