@apifuse/provider-sdk 2.2.0-beta.10 → 2.2.0-beta.12
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/AUTHORING.md +37 -0
- package/CHANGELOG.md +8 -0
- package/README.md +18 -0
- package/bin/apifuse-pack-smoke.ts +14 -0
- package/bin/apifuse-pack-types.ts +11 -1
- package/dist/config/loader.d.ts +9 -1
- package/dist/config/loader.js +9 -0
- package/dist/errors.d.ts +5 -0
- package/dist/errors.js +15 -0
- package/dist/index.d.ts +3 -3
- package/dist/index.js +1 -1
- package/dist/runtime/stealth.js +113 -47
- package/dist/server/index.d.ts +1 -1
- package/dist/server/index.js +1 -1
- package/dist/server/serve.d.ts +98 -2
- package/dist/server/serve.js +485 -23
- package/dist/stateful/errors.d.ts +14 -0
- package/dist/stateful/errors.js +14 -0
- package/dist/stateful/http-provider-event-emitter.d.ts +40 -0
- package/dist/stateful/http-provider-event-emitter.js +237 -0
- package/dist/stateful/http-session-owner-registry.d.ts +44 -0
- package/dist/stateful/http-session-owner-registry.js +210 -0
- package/dist/stateful/index.d.ts +18 -0
- package/dist/stateful/index.js +18 -0
- package/dist/stateful/provider-event-delivery-failures.d.ts +32 -0
- package/dist/stateful/provider-event-delivery-failures.js +43 -0
- package/dist/stateful/provider-event-pipeline-metrics.d.ts +46 -0
- package/dist/stateful/provider-event-pipeline-metrics.js +48 -0
- package/dist/stateful/provider-event-pipeline.d.ts +50 -0
- package/dist/stateful/provider-event-pipeline.js +1 -0
- package/dist/stateful/provider-events.d.ts +101 -0
- package/dist/stateful/provider-events.js +289 -0
- package/dist/stateful/session-key.d.ts +15 -0
- package/dist/stateful/session-key.js +86 -0
- package/dist/stateful/stateful-provider-adapter-context.d.ts +5 -0
- package/dist/stateful/stateful-provider-adapter-context.js +42 -0
- package/dist/stateful/stateful-provider-adapter-metrics.d.ts +15 -0
- package/dist/stateful/stateful-provider-adapter-metrics.js +21 -0
- package/dist/stateful/stateful-provider-adapter.d.ts +98 -0
- package/dist/stateful/stateful-provider-adapter.js +287 -0
- package/dist/stateful/stateful-provider-observability.d.ts +62 -0
- package/dist/stateful/stateful-provider-observability.js +161 -0
- package/dist/stateful/stateful-provider-owner-forwarder.d.ts +41 -0
- package/dist/stateful/stateful-provider-owner-forwarder.js +207 -0
- package/dist/stateful/stateful-provider-runtime-context.d.ts +32 -0
- package/dist/stateful/stateful-provider-runtime-context.js +60 -0
- package/dist/stateful/stateful-provider-runtime-executor.d.ts +34 -0
- package/dist/stateful/stateful-provider-runtime-executor.js +52 -0
- package/dist/stateful/stateful-provider-session-routing.d.ts +71 -0
- package/dist/stateful/stateful-provider-session-routing.js +353 -0
- package/dist/stateful/stateful-provider-session-runtime.d.ts +98 -0
- package/dist/stateful/stateful-provider-session-runtime.js +245 -0
- package/dist/stateful-signing.d.ts +18 -0
- package/dist/stateful-signing.js +27 -0
- package/dist/types.d.ts +39 -7
- package/package.json +7 -1
- package/src/config/loader.ts +22 -1
- package/src/errors.ts +15 -0
- package/src/index.ts +8 -1
- package/src/runtime/stealth.ts +127 -48
- package/src/server/index.ts +13 -1
- package/src/server/serve.ts +691 -25
- package/src/stateful/README.md +146 -0
- package/src/stateful/errors.ts +23 -0
- package/src/stateful/http-provider-event-emitter.ts +314 -0
- package/src/stateful/http-session-owner-registry.ts +306 -0
- package/src/stateful/index.ts +18 -0
- package/src/stateful/provider-event-delivery-failures.ts +80 -0
- package/src/stateful/provider-event-pipeline-metrics.ts +95 -0
- package/src/stateful/provider-event-pipeline.ts +61 -0
- package/src/stateful/provider-events.ts +462 -0
- package/src/stateful/session-key.ts +111 -0
- package/src/stateful/stateful-provider-adapter-context.ts +59 -0
- package/src/stateful/stateful-provider-adapter-metrics.ts +48 -0
- package/src/stateful/stateful-provider-adapter.ts +562 -0
- package/src/stateful/stateful-provider-observability.ts +261 -0
- package/src/stateful/stateful-provider-owner-forwarder.ts +279 -0
- package/src/stateful/stateful-provider-runtime-context.ts +92 -0
- package/src/stateful/stateful-provider-runtime-executor.ts +96 -0
- package/src/stateful/stateful-provider-session-routing.ts +555 -0
- package/src/stateful/stateful-provider-session-runtime.ts +403 -0
- package/src/stateful-signing.ts +46 -0
- package/src/types.ts +41 -7
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
export const PROVIDER_EVENT_METRIC_NAMES = [
|
|
2
|
+
"apifuse_stateful_provider_event_append_total",
|
|
3
|
+
"apifuse_stateful_provider_event_dedupe_total",
|
|
4
|
+
"apifuse_stateful_provider_event_drop_total",
|
|
5
|
+
"apifuse_stateful_provider_webhook_enqueue_total",
|
|
6
|
+
"apifuse_stateful_provider_webhook_fanout_recovered_total",
|
|
7
|
+
"apifuse_stateful_provider_webhook_delivered_total",
|
|
8
|
+
"apifuse_stateful_provider_webhook_retry_total",
|
|
9
|
+
"apifuse_stateful_provider_webhook_dead_letter_total",
|
|
10
|
+
"apifuse_stateful_provider_webhook_canceled_total",
|
|
11
|
+
"apifuse_stateful_provider_webhook_latency_ms",
|
|
12
|
+
];
|
|
13
|
+
export const NOOP_PROVIDER_EVENT_METRIC_EMITTER = {
|
|
14
|
+
increment() { },
|
|
15
|
+
observe() { },
|
|
16
|
+
};
|
|
17
|
+
export function providerEventMetricLabels(event) {
|
|
18
|
+
return {
|
|
19
|
+
eventId: event.eventId,
|
|
20
|
+
providerId: event.providerId,
|
|
21
|
+
connectionId: event.connectionId,
|
|
22
|
+
serviceAccountId: event.serviceAccountId,
|
|
23
|
+
eventType: event.eventType,
|
|
24
|
+
subjectKind: event.subject?.kind,
|
|
25
|
+
subjectId: event.subject?.id,
|
|
26
|
+
sessionKey: event.session.sessionKey,
|
|
27
|
+
generation: event.session.generation,
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
export function providerWebhookDeliveryMetricLabels(delivery) {
|
|
31
|
+
return { deliveryId: delivery.deliveryId };
|
|
32
|
+
}
|
|
33
|
+
export function emitProviderWebhookDeliveryFailureMetric(metricEmitter, delivery, status) {
|
|
34
|
+
if (status === "retrying") {
|
|
35
|
+
metricEmitter.increment("apifuse_stateful_provider_webhook_retry_total", providerWebhookDeliveryMetricLabels(delivery));
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
if (status === "dead_lettered") {
|
|
39
|
+
metricEmitter.increment("apifuse_stateful_provider_webhook_dead_letter_total", providerWebhookDeliveryMetricLabels(delivery));
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
export function providerWebhookDeliveryLatencyMs(delivery, now) {
|
|
43
|
+
const startedAt = Date.parse(delivery.lastAttemptAt ?? delivery.nextAttemptAt);
|
|
44
|
+
const completedAt = Date.parse(now);
|
|
45
|
+
if (!Number.isFinite(startedAt) || !Number.isFinite(completedAt))
|
|
46
|
+
return 0;
|
|
47
|
+
return Math.max(0, completedAt - startedAt);
|
|
48
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { ProviderEventMetricEmitter } from "./provider-event-pipeline-metrics.js";
|
|
2
|
+
import type { ProviderEvent } from "./provider-events.js";
|
|
3
|
+
import type { SessionKey } from "./session-key.js";
|
|
4
|
+
export type { ProviderEventMetricEmitter } from "./provider-event-pipeline-metrics.js";
|
|
5
|
+
export type ProviderEventOwnerFence = {
|
|
6
|
+
readonly ownerPodId: string;
|
|
7
|
+
readonly ownerEndpoint: string;
|
|
8
|
+
readonly sessionKey: SessionKey;
|
|
9
|
+
readonly generation: number;
|
|
10
|
+
};
|
|
11
|
+
export type ProviderEventPublishOptions = {
|
|
12
|
+
/** Required ownership proof forwarded to the durable platform write boundary. */
|
|
13
|
+
readonly ownerFence: ProviderEventOwnerFence;
|
|
14
|
+
/** Overrides the transport idempotency key; defaults to the event id. */
|
|
15
|
+
readonly idempotencyKey?: string;
|
|
16
|
+
};
|
|
17
|
+
export type PublishAck = {
|
|
18
|
+
/** Whether this event was accepted into the pod-local retry buffer. */
|
|
19
|
+
readonly accepted: boolean;
|
|
20
|
+
/** Number of events currently held in the pod-local retry buffer. */
|
|
21
|
+
readonly queued: number;
|
|
22
|
+
};
|
|
23
|
+
/** Fire-and-forget publication from a provider pod into the platform ingest boundary. */
|
|
24
|
+
export interface ProviderEventPublisher {
|
|
25
|
+
publish(event: ProviderEvent, options: ProviderEventPublishOptions): PublishAck;
|
|
26
|
+
}
|
|
27
|
+
export type AppendProviderEventAndEnqueueWebhooksResult = {
|
|
28
|
+
readonly appended: boolean;
|
|
29
|
+
readonly appendedCount: number;
|
|
30
|
+
readonly matchedSubscriptionCount: number;
|
|
31
|
+
readonly enqueuedCount: number;
|
|
32
|
+
readonly recoveredFanoutCount?: number;
|
|
33
|
+
readonly fenced?: boolean;
|
|
34
|
+
};
|
|
35
|
+
export type ProviderEventPipelineAppendOptions = {
|
|
36
|
+
readonly now?: string;
|
|
37
|
+
readonly idFactory?: () => string;
|
|
38
|
+
readonly metricEmitter?: ProviderEventMetricEmitter;
|
|
39
|
+
readonly beforeAppend?: () => Promise<boolean> | boolean;
|
|
40
|
+
readonly ownerFence?: ProviderEventOwnerFence;
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* Durable append-and-fanout contract implemented on the platform side.
|
|
44
|
+
*
|
|
45
|
+
* Provider-pod code must depend on ProviderEventPublisher, never ProviderEventPipeline: a pod-local
|
|
46
|
+
* publisher cannot truthfully report a durable append or downstream webhook fanout.
|
|
47
|
+
*/
|
|
48
|
+
export interface ProviderEventPipeline {
|
|
49
|
+
appendAndFanout(event: ProviderEvent, options?: ProviderEventPipelineAppendOptions): Promise<AppendProviderEventAndEnqueueWebhooksResult>;
|
|
50
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
export type ProviderEventSubject = {
|
|
2
|
+
/** Provider-defined resource category, for example `message`, `mailbox`, or `browser-context`. */
|
|
3
|
+
kind: string;
|
|
4
|
+
id: string;
|
|
5
|
+
};
|
|
6
|
+
export type ProviderEventSession = {
|
|
7
|
+
/** Canonical logical session identity shared by every stateful provider. */
|
|
8
|
+
sessionKey: string;
|
|
9
|
+
/** Ownership generation shared by every stateful provider for stale-event fencing. */
|
|
10
|
+
generation: number;
|
|
11
|
+
};
|
|
12
|
+
export interface ProviderEvent<TPayload = Record<string, unknown>> {
|
|
13
|
+
/** Universal stable identity used for transport retries and deduplication. */
|
|
14
|
+
eventId: string;
|
|
15
|
+
/** Universal provider namespace needed to route and interpret the event. */
|
|
16
|
+
providerId: string;
|
|
17
|
+
/** Optional because mailbox/browser protocols may not expose an API connection identity. */
|
|
18
|
+
connectionId?: string;
|
|
19
|
+
/** Optional because not every provider session maps to a platform service account. */
|
|
20
|
+
serviceAccountId?: string;
|
|
21
|
+
/** Universal provider-defined event classification. */
|
|
22
|
+
eventType: string;
|
|
23
|
+
/** Optional for protocol-level events without a single resource subject. */
|
|
24
|
+
subject?: ProviderEventSubject;
|
|
25
|
+
/** Optional when the upstream protocol does not provide an occurrence timestamp. */
|
|
26
|
+
occurredAt?: string;
|
|
27
|
+
/** Universal SDK observation time used for ordering and lag measurement. */
|
|
28
|
+
observedAt: string;
|
|
29
|
+
/** Universal JSON data envelope; protocols without data use an empty object. */
|
|
30
|
+
payload: TPayload;
|
|
31
|
+
/** Universal stateful ownership fence for rejecting events from stale sessions. */
|
|
32
|
+
session: ProviderEventSession;
|
|
33
|
+
providerCursor?: string;
|
|
34
|
+
dedupeKey?: string;
|
|
35
|
+
rawRef?: string;
|
|
36
|
+
}
|
|
37
|
+
export type ProviderEventIdFactory = () => string;
|
|
38
|
+
export type ProviderEventClock = () => Date;
|
|
39
|
+
export type BuildProviderEventInput<TPayload extends Record<string, unknown> = Record<string, unknown>> = Omit<ProviderEvent<TPayload>, "eventId" | "observedAt"> & {
|
|
40
|
+
eventId?: string;
|
|
41
|
+
observedAt?: string;
|
|
42
|
+
clock?: ProviderEventClock;
|
|
43
|
+
idFactory?: ProviderEventIdFactory;
|
|
44
|
+
maxPayloadBytes?: number;
|
|
45
|
+
shouldRedactPayload?: boolean;
|
|
46
|
+
/** Provider-specific credential key patterns added to the conservative SDK defaults. */
|
|
47
|
+
redactionPatterns?: readonly RegExp[];
|
|
48
|
+
};
|
|
49
|
+
export type ProviderEventListOptions = {
|
|
50
|
+
providerId?: string;
|
|
51
|
+
connectionId?: string;
|
|
52
|
+
eventType?: string;
|
|
53
|
+
subjectKind?: string;
|
|
54
|
+
subjectId?: string;
|
|
55
|
+
afterObservedAt?: string;
|
|
56
|
+
limit?: number;
|
|
57
|
+
};
|
|
58
|
+
export type ProviderEventAppendResult = {
|
|
59
|
+
event: ProviderEvent;
|
|
60
|
+
appended: boolean;
|
|
61
|
+
};
|
|
62
|
+
export interface ProviderEventLog {
|
|
63
|
+
append(event: ProviderEvent): Promise<ProviderEventAppendResult>;
|
|
64
|
+
get(eventId: string): Promise<ProviderEvent | null>;
|
|
65
|
+
list(options?: ProviderEventListOptions): Promise<ProviderEvent[]>;
|
|
66
|
+
}
|
|
67
|
+
export type ProviderEventEmitterDefaults = {
|
|
68
|
+
providerId: string;
|
|
69
|
+
connectionId?: string;
|
|
70
|
+
serviceAccountId?: string;
|
|
71
|
+
session: ProviderEventSession;
|
|
72
|
+
clock?: ProviderEventClock;
|
|
73
|
+
idFactory?: ProviderEventIdFactory;
|
|
74
|
+
maxPayloadBytes?: number;
|
|
75
|
+
shouldRedactPayload?: boolean;
|
|
76
|
+
redactionPatterns?: readonly RegExp[];
|
|
77
|
+
};
|
|
78
|
+
export type ProviderEventEmitterInput<TPayload extends Record<string, unknown> = Record<string, unknown>> = Omit<BuildProviderEventInput<TPayload>, "providerId" | "connectionId" | "serviceAccountId" | "session"> & {
|
|
79
|
+
providerId?: string;
|
|
80
|
+
connectionId?: string;
|
|
81
|
+
serviceAccountId?: string;
|
|
82
|
+
session?: ProviderEventSession;
|
|
83
|
+
};
|
|
84
|
+
export declare class ProviderEventValidationError extends Error {
|
|
85
|
+
constructor(message: string);
|
|
86
|
+
}
|
|
87
|
+
export declare function buildProviderEvent<TPayload extends Record<string, unknown> = Record<string, unknown>>(input: BuildProviderEventInput<TPayload>): ProviderEvent<TPayload>;
|
|
88
|
+
export declare function redactProviderEventPayload<TPayload>(payload: TPayload, extraPatterns?: readonly RegExp[]): TPayload;
|
|
89
|
+
export declare function isStaleProviderEvent(event: ProviderEvent, currentGeneration: number): boolean;
|
|
90
|
+
export declare class InMemoryProviderEventLog implements ProviderEventLog {
|
|
91
|
+
#private;
|
|
92
|
+
append(event: ProviderEvent): Promise<ProviderEventAppendResult>;
|
|
93
|
+
get(eventId: string): Promise<ProviderEvent | null>;
|
|
94
|
+
list(options?: ProviderEventListOptions): Promise<ProviderEvent[]>;
|
|
95
|
+
}
|
|
96
|
+
export declare class ProviderEventEmitter {
|
|
97
|
+
#private;
|
|
98
|
+
constructor(log: ProviderEventLog, defaults: ProviderEventEmitterDefaults);
|
|
99
|
+
emit<TPayload extends Record<string, unknown> = Record<string, unknown>>(input: ProviderEventEmitterInput<TPayload>): Promise<ProviderEventAppendResult>;
|
|
100
|
+
}
|
|
101
|
+
export declare function randomProviderEventId(): string;
|
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
2
|
+
const DEFAULT_MAX_PAYLOAD_BYTES = 256 * 1024;
|
|
3
|
+
const REDACTED_VALUE = "[REDACTED]";
|
|
4
|
+
const CREDENTIAL_KEY_PATTERN = /(?:token|password|secret|credential|cookie|authorization)/i;
|
|
5
|
+
export class ProviderEventValidationError extends Error {
|
|
6
|
+
constructor(message) {
|
|
7
|
+
super(message);
|
|
8
|
+
this.name = "ProviderEventValidationError";
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
export function buildProviderEvent(input) {
|
|
12
|
+
validatePayload(input.payload, input.maxPayloadBytes ?? DEFAULT_MAX_PAYLOAD_BYTES);
|
|
13
|
+
const observedAt = input.observedAt ?? input.clock?.().toISOString() ?? new Date().toISOString();
|
|
14
|
+
const payload = input.shouldRedactPayload === false
|
|
15
|
+
? input.payload
|
|
16
|
+
: redactProviderEventPayload(input.payload, input.redactionPatterns);
|
|
17
|
+
const event = {
|
|
18
|
+
eventId: input.eventId ?? buildEventId({ ...input, observedAt }),
|
|
19
|
+
providerId: input.providerId,
|
|
20
|
+
eventType: input.eventType,
|
|
21
|
+
observedAt,
|
|
22
|
+
payload,
|
|
23
|
+
session: input.session,
|
|
24
|
+
...(input.connectionId ? { connectionId: input.connectionId } : {}),
|
|
25
|
+
...(input.serviceAccountId ? { serviceAccountId: input.serviceAccountId } : {}),
|
|
26
|
+
...(input.subject ? { subject: input.subject } : {}),
|
|
27
|
+
...(input.occurredAt ? { occurredAt: input.occurredAt } : {}),
|
|
28
|
+
...(input.providerCursor ? { providerCursor: input.providerCursor } : {}),
|
|
29
|
+
...(input.dedupeKey ? { dedupeKey: input.dedupeKey } : {}),
|
|
30
|
+
...(input.rawRef ? { rawRef: input.rawRef } : {}),
|
|
31
|
+
};
|
|
32
|
+
validateProviderEvent(event, input.maxPayloadBytes);
|
|
33
|
+
return event;
|
|
34
|
+
}
|
|
35
|
+
export function redactProviderEventPayload(payload, extraPatterns = []) {
|
|
36
|
+
const cloned = structuredClone(payload);
|
|
37
|
+
redactValueInPlace(cloned, [CREDENTIAL_KEY_PATTERN, ...extraPatterns]);
|
|
38
|
+
return cloned;
|
|
39
|
+
}
|
|
40
|
+
export function isStaleProviderEvent(event, currentGeneration) {
|
|
41
|
+
return event.session.generation < currentGeneration;
|
|
42
|
+
}
|
|
43
|
+
export class InMemoryProviderEventLog {
|
|
44
|
+
#events = [];
|
|
45
|
+
#eventIds = new Set();
|
|
46
|
+
#dedupeKeys = new Set();
|
|
47
|
+
async append(event) {
|
|
48
|
+
validateProviderEvent(event);
|
|
49
|
+
if (this.#eventIds.has(event.eventId)) {
|
|
50
|
+
return { event, appended: false };
|
|
51
|
+
}
|
|
52
|
+
const dedupeTuple = event.dedupeKey ? buildDedupeTuple(event) : undefined;
|
|
53
|
+
if (dedupeTuple && this.#dedupeKeys.has(dedupeTuple)) {
|
|
54
|
+
return { event, appended: false };
|
|
55
|
+
}
|
|
56
|
+
this.#events.push(event);
|
|
57
|
+
this.#eventIds.add(event.eventId);
|
|
58
|
+
if (dedupeTuple)
|
|
59
|
+
this.#dedupeKeys.add(dedupeTuple);
|
|
60
|
+
return { event, appended: true };
|
|
61
|
+
}
|
|
62
|
+
async get(eventId) {
|
|
63
|
+
validateRequiredString("eventId", eventId);
|
|
64
|
+
return this.#events.find((event) => event.eventId === eventId) ?? null;
|
|
65
|
+
}
|
|
66
|
+
async list(options = {}) {
|
|
67
|
+
validateListOptions(options);
|
|
68
|
+
let events = this.#events;
|
|
69
|
+
if (options.providerId) {
|
|
70
|
+
events = events.filter((event) => event.providerId === options.providerId);
|
|
71
|
+
}
|
|
72
|
+
if (options.connectionId) {
|
|
73
|
+
events = events.filter((event) => event.connectionId === options.connectionId);
|
|
74
|
+
}
|
|
75
|
+
if (options.eventType) {
|
|
76
|
+
events = events.filter((event) => event.eventType === options.eventType);
|
|
77
|
+
}
|
|
78
|
+
if (options.subjectKind) {
|
|
79
|
+
events = events.filter((event) => event.subject?.kind === options.subjectKind);
|
|
80
|
+
}
|
|
81
|
+
if (options.subjectId) {
|
|
82
|
+
events = events.filter((event) => event.subject?.id === options.subjectId);
|
|
83
|
+
}
|
|
84
|
+
if (options.afterObservedAt) {
|
|
85
|
+
const afterMs = Date.parse(options.afterObservedAt);
|
|
86
|
+
events = events.filter((event) => Date.parse(event.observedAt) > afterMs);
|
|
87
|
+
}
|
|
88
|
+
return typeof options.limit === "number" ? events.slice(0, options.limit) : [...events];
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
export class ProviderEventEmitter {
|
|
92
|
+
#log;
|
|
93
|
+
#defaults;
|
|
94
|
+
constructor(log, defaults) {
|
|
95
|
+
validateRequiredString("providerId", defaults.providerId);
|
|
96
|
+
if (defaults.connectionId !== undefined) {
|
|
97
|
+
validateRequiredString("connectionId", defaults.connectionId);
|
|
98
|
+
}
|
|
99
|
+
if (defaults.serviceAccountId !== undefined) {
|
|
100
|
+
validateRequiredString("serviceAccountId", defaults.serviceAccountId);
|
|
101
|
+
}
|
|
102
|
+
validateSession(defaults.session);
|
|
103
|
+
this.#log = log;
|
|
104
|
+
this.#defaults = defaults;
|
|
105
|
+
}
|
|
106
|
+
emit(input) {
|
|
107
|
+
const event = buildProviderEvent({
|
|
108
|
+
...input,
|
|
109
|
+
providerId: input.providerId ?? this.#defaults.providerId,
|
|
110
|
+
connectionId: input.connectionId ?? this.#defaults.connectionId,
|
|
111
|
+
serviceAccountId: input.serviceAccountId ?? this.#defaults.serviceAccountId,
|
|
112
|
+
session: input.session ?? this.#defaults.session,
|
|
113
|
+
clock: input.clock ?? this.#defaults.clock,
|
|
114
|
+
idFactory: input.idFactory ?? this.#defaults.idFactory,
|
|
115
|
+
maxPayloadBytes: input.maxPayloadBytes ?? this.#defaults.maxPayloadBytes,
|
|
116
|
+
shouldRedactPayload: input.shouldRedactPayload ?? this.#defaults.shouldRedactPayload,
|
|
117
|
+
redactionPatterns: input.redactionPatterns ?? this.#defaults.redactionPatterns,
|
|
118
|
+
});
|
|
119
|
+
return this.#log.append(event);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
function buildEventId(input) {
|
|
123
|
+
if (input.dedupeKey) {
|
|
124
|
+
return deterministicProviderEventId([
|
|
125
|
+
input.providerId,
|
|
126
|
+
input.connectionId ?? "",
|
|
127
|
+
input.eventType,
|
|
128
|
+
input.dedupeKey,
|
|
129
|
+
]);
|
|
130
|
+
}
|
|
131
|
+
return (input.idFactory?.() ??
|
|
132
|
+
deterministicProviderEventId([
|
|
133
|
+
input.providerId,
|
|
134
|
+
input.connectionId ?? "",
|
|
135
|
+
input.eventType,
|
|
136
|
+
input.subject?.kind ?? "",
|
|
137
|
+
input.subject?.id ?? "",
|
|
138
|
+
input.observedAt,
|
|
139
|
+
]));
|
|
140
|
+
}
|
|
141
|
+
function deterministicProviderEventId(parts) {
|
|
142
|
+
return `pevt_${createHash("sha256").update(parts.join("\u001f")).digest("hex")}`;
|
|
143
|
+
}
|
|
144
|
+
function validateProviderEvent(event, maxPayloadBytes = DEFAULT_MAX_PAYLOAD_BYTES) {
|
|
145
|
+
validateRequiredString("eventId", event.eventId);
|
|
146
|
+
validateRequiredString("providerId", event.providerId);
|
|
147
|
+
if (event.connectionId !== undefined)
|
|
148
|
+
validateRequiredString("connectionId", event.connectionId);
|
|
149
|
+
if (event.serviceAccountId !== undefined) {
|
|
150
|
+
validateRequiredString("serviceAccountId", event.serviceAccountId);
|
|
151
|
+
}
|
|
152
|
+
validateRequiredString("eventType", event.eventType);
|
|
153
|
+
if (event.subject) {
|
|
154
|
+
validateRequiredString("subject.kind", event.subject.kind);
|
|
155
|
+
validateRequiredString("subject.id", event.subject.id);
|
|
156
|
+
}
|
|
157
|
+
if (event.occurredAt !== undefined) {
|
|
158
|
+
validateRequiredString("occurredAt", event.occurredAt);
|
|
159
|
+
validateTimestamp("occurredAt", event.occurredAt);
|
|
160
|
+
}
|
|
161
|
+
validateRequiredString("observedAt", event.observedAt);
|
|
162
|
+
validateTimestamp("observedAt", event.observedAt);
|
|
163
|
+
validateSession(event.session);
|
|
164
|
+
if (event.providerCursor !== undefined) {
|
|
165
|
+
validateRequiredString("providerCursor", event.providerCursor);
|
|
166
|
+
}
|
|
167
|
+
if (event.dedupeKey !== undefined) {
|
|
168
|
+
validateRequiredString("dedupeKey", event.dedupeKey);
|
|
169
|
+
}
|
|
170
|
+
if (event.rawRef !== undefined) {
|
|
171
|
+
validateRequiredString("rawRef", event.rawRef);
|
|
172
|
+
}
|
|
173
|
+
validatePayload(event.payload, maxPayloadBytes);
|
|
174
|
+
}
|
|
175
|
+
function validateSession(session) {
|
|
176
|
+
validateRequiredString("session.sessionKey", session?.sessionKey);
|
|
177
|
+
if (!Number.isInteger(session?.generation) || session.generation < 0) {
|
|
178
|
+
throw new ProviderEventValidationError("ProviderEvent session.generation must be a non-negative integer.");
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
function validateRequiredString(name, value) {
|
|
182
|
+
if (typeof value !== "string" || value.trim().length === 0) {
|
|
183
|
+
throw new ProviderEventValidationError(`ProviderEvent ${name} must be a non-empty string.`);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
function validateTimestamp(name, value) {
|
|
187
|
+
if (!Number.isFinite(Date.parse(value))) {
|
|
188
|
+
throw new ProviderEventValidationError(`ProviderEvent ${name} must be a parseable ISO timestamp string.`);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
function validatePayload(payload, maxPayloadBytes) {
|
|
192
|
+
if (typeof maxPayloadBytes !== "number" ||
|
|
193
|
+
!Number.isFinite(maxPayloadBytes) ||
|
|
194
|
+
maxPayloadBytes <= 0) {
|
|
195
|
+
throw new ProviderEventValidationError("ProviderEvent maxPayloadBytes must be a positive finite number.");
|
|
196
|
+
}
|
|
197
|
+
if (!isPlainRecord(payload)) {
|
|
198
|
+
throw new ProviderEventValidationError("ProviderEvent payload must be a JSON-serializable object.");
|
|
199
|
+
}
|
|
200
|
+
assertJsonValue(payload, "payload");
|
|
201
|
+
const encoded = JSON.stringify(payload);
|
|
202
|
+
if (encoded === undefined) {
|
|
203
|
+
throw new ProviderEventValidationError("ProviderEvent payload must be JSON-serializable.");
|
|
204
|
+
}
|
|
205
|
+
const payloadBytes = Buffer.byteLength(encoded, "utf8");
|
|
206
|
+
if (payloadBytes > maxPayloadBytes) {
|
|
207
|
+
throw new ProviderEventValidationError(`ProviderEvent payload exceeds max size of ${maxPayloadBytes} bytes.`);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
function assertJsonValue(value, path, seen = new WeakSet()) {
|
|
211
|
+
if (value === null)
|
|
212
|
+
return;
|
|
213
|
+
if (typeof value === "string" || typeof value === "boolean")
|
|
214
|
+
return;
|
|
215
|
+
if (typeof value === "number") {
|
|
216
|
+
if (Number.isFinite(value))
|
|
217
|
+
return;
|
|
218
|
+
throw new ProviderEventValidationError(`ProviderEvent ${path} must not contain non-finite numbers.`);
|
|
219
|
+
}
|
|
220
|
+
if (Array.isArray(value)) {
|
|
221
|
+
if (seen.has(value)) {
|
|
222
|
+
throw new ProviderEventValidationError(`ProviderEvent ${path} must not contain circular references.`);
|
|
223
|
+
}
|
|
224
|
+
seen.add(value);
|
|
225
|
+
for (const [index, entry] of value.entries()) {
|
|
226
|
+
assertJsonValue(entry, `${path}[${index}]`, seen);
|
|
227
|
+
}
|
|
228
|
+
seen.delete(value);
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
if (isPlainRecord(value)) {
|
|
232
|
+
if (seen.has(value)) {
|
|
233
|
+
throw new ProviderEventValidationError(`ProviderEvent ${path} must not contain circular references.`);
|
|
234
|
+
}
|
|
235
|
+
seen.add(value);
|
|
236
|
+
for (const [key, entry] of Object.entries(value)) {
|
|
237
|
+
assertJsonValue(entry, `${path}.${key}`, seen);
|
|
238
|
+
}
|
|
239
|
+
seen.delete(value);
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
throw new ProviderEventValidationError(`ProviderEvent ${path} must be JSON-serializable.`);
|
|
243
|
+
}
|
|
244
|
+
function redactValueInPlace(value, patterns) {
|
|
245
|
+
if (Array.isArray(value)) {
|
|
246
|
+
for (const entry of value)
|
|
247
|
+
redactValueInPlace(entry, patterns);
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
if (!isPlainRecord(value))
|
|
251
|
+
return;
|
|
252
|
+
for (const [key, entry] of Object.entries(value)) {
|
|
253
|
+
if (patterns.some((pattern) => matchesPattern(pattern, key)))
|
|
254
|
+
value[key] = REDACTED_VALUE;
|
|
255
|
+
else
|
|
256
|
+
redactValueInPlace(entry, patterns);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
function isPlainRecord(value) {
|
|
260
|
+
return (typeof value === "object" &&
|
|
261
|
+
value !== null &&
|
|
262
|
+
!Array.isArray(value) &&
|
|
263
|
+
Object.getPrototypeOf(value) === Object.prototype);
|
|
264
|
+
}
|
|
265
|
+
function buildDedupeTuple(event) {
|
|
266
|
+
return [
|
|
267
|
+
event.serviceAccountId ?? "",
|
|
268
|
+
event.providerId,
|
|
269
|
+
event.connectionId ?? "",
|
|
270
|
+
event.dedupeKey,
|
|
271
|
+
].join("\u001f");
|
|
272
|
+
}
|
|
273
|
+
function matchesPattern(pattern, value) {
|
|
274
|
+
pattern.lastIndex = 0;
|
|
275
|
+
const matched = pattern.test(value);
|
|
276
|
+
pattern.lastIndex = 0;
|
|
277
|
+
return matched;
|
|
278
|
+
}
|
|
279
|
+
function validateListOptions(options) {
|
|
280
|
+
if (options.afterObservedAt) {
|
|
281
|
+
validateTimestamp("afterObservedAt", options.afterObservedAt);
|
|
282
|
+
}
|
|
283
|
+
if (options.limit !== undefined && (!Number.isInteger(options.limit) || options.limit <= 0)) {
|
|
284
|
+
throw new ProviderEventValidationError("ProviderEvent list limit must be a positive integer.");
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
export function randomProviderEventId() {
|
|
288
|
+
return `pevt_${randomUUID().replaceAll("-", "")}`;
|
|
289
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
declare const SESSION_KEY_BRAND: unique symbol;
|
|
2
|
+
/** Canonical, opaque identity for a stateful provider session. */
|
|
3
|
+
export type SessionKey = string & {
|
|
4
|
+
readonly [SESSION_KEY_BRAND]: "SessionKey";
|
|
5
|
+
};
|
|
6
|
+
export interface SessionKeyParts {
|
|
7
|
+
readonly providerId: string;
|
|
8
|
+
readonly serviceAccountId: string;
|
|
9
|
+
readonly connectionId: string;
|
|
10
|
+
/** Optional extra session axes (e.g. device, mailbox, persona). Defaults to none. */
|
|
11
|
+
readonly dimensions?: Readonly<Record<string, string>>;
|
|
12
|
+
}
|
|
13
|
+
export declare function buildSessionKey(parts: SessionKeyParts): SessionKey;
|
|
14
|
+
export declare function parseSessionKey(key: string): SessionKeyParts;
|
|
15
|
+
export {};
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
const PREFIX = "stateful:v1";
|
|
2
|
+
const REQUIRED_DIMENSIONS = ["providerId", "serviceAccountId", "connectionId"];
|
|
3
|
+
const REQUIRED_DIMENSION_SET = new Set(REQUIRED_DIMENSIONS);
|
|
4
|
+
export function buildSessionKey(parts) {
|
|
5
|
+
validateRequiredPart(parts.providerId, "providerId");
|
|
6
|
+
validateRequiredPart(parts.serviceAccountId, "serviceAccountId");
|
|
7
|
+
validateRequiredPart(parts.connectionId, "connectionId");
|
|
8
|
+
const dimensions = Object.entries(parts.dimensions ?? {}).sort(([left], [right]) => left < right ? -1 : left > right ? 1 : 0);
|
|
9
|
+
for (const [name, value] of dimensions) {
|
|
10
|
+
validateDimension(name, value);
|
|
11
|
+
}
|
|
12
|
+
const segments = [
|
|
13
|
+
["providerId", parts.providerId],
|
|
14
|
+
["serviceAccountId", parts.serviceAccountId],
|
|
15
|
+
["connectionId", parts.connectionId],
|
|
16
|
+
...dimensions,
|
|
17
|
+
];
|
|
18
|
+
return `${PREFIX}/${segments
|
|
19
|
+
.map(([name, value]) => `${encodeURIComponent(name)}=${encodeURIComponent(value)}`)
|
|
20
|
+
.join("/")}`;
|
|
21
|
+
}
|
|
22
|
+
export function parseSessionKey(key) {
|
|
23
|
+
const prefix = `${PREFIX}/`;
|
|
24
|
+
if (!key.startsWith(prefix)) {
|
|
25
|
+
throw new Error(`Invalid session key: expected canonical prefix "${PREFIX}".`);
|
|
26
|
+
}
|
|
27
|
+
const parsed = new Map();
|
|
28
|
+
for (const segment of key.slice(prefix.length).split("/")) {
|
|
29
|
+
const separator = segment.indexOf("=");
|
|
30
|
+
if (separator <= 0) {
|
|
31
|
+
throw new Error(`Invalid session key segment "${segment}"; expected name=value.`);
|
|
32
|
+
}
|
|
33
|
+
const name = decodeSegment(segment.slice(0, separator), "dimension name");
|
|
34
|
+
const value = decodeSegment(segment.slice(separator + 1), `dimension "${name}"`);
|
|
35
|
+
if (parsed.has(name)) {
|
|
36
|
+
throw new Error(`Invalid session key: duplicate dimension "${name}".`);
|
|
37
|
+
}
|
|
38
|
+
if (name.trim().length === 0) {
|
|
39
|
+
throw new Error("Invalid session key: dimension name must not be empty.");
|
|
40
|
+
}
|
|
41
|
+
if (value.trim().length === 0) {
|
|
42
|
+
throw new Error(`Invalid session key: dimension "${name}" must not be empty.`);
|
|
43
|
+
}
|
|
44
|
+
parsed.set(name, value);
|
|
45
|
+
}
|
|
46
|
+
for (const required of REQUIRED_DIMENSIONS) {
|
|
47
|
+
if (!parsed.has(required)) {
|
|
48
|
+
throw new Error(`Invalid session key: missing required dimension "${required}".`);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
const dimensions = {};
|
|
52
|
+
for (const [name, value] of parsed) {
|
|
53
|
+
if (!REQUIRED_DIMENSION_SET.has(name))
|
|
54
|
+
dimensions[name] = value;
|
|
55
|
+
}
|
|
56
|
+
return {
|
|
57
|
+
providerId: parsed.get("providerId"),
|
|
58
|
+
serviceAccountId: parsed.get("serviceAccountId"),
|
|
59
|
+
connectionId: parsed.get("connectionId"),
|
|
60
|
+
dimensions,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
function validateRequiredPart(value, name) {
|
|
64
|
+
if (typeof value !== "string" || value.trim().length === 0) {
|
|
65
|
+
throw new Error(`Cannot build session key: required dimension "${name}" is missing or empty.`);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
function validateDimension(name, value) {
|
|
69
|
+
if (name.trim().length === 0) {
|
|
70
|
+
throw new Error("Cannot build session key: extra dimension name must not be empty.");
|
|
71
|
+
}
|
|
72
|
+
if (REQUIRED_DIMENSION_SET.has(name)) {
|
|
73
|
+
throw new Error(`Cannot build session key: extra dimension "${name}" duplicates a required dimension.`);
|
|
74
|
+
}
|
|
75
|
+
if (typeof value !== "string" || value.trim().length === 0) {
|
|
76
|
+
throw new Error(`Cannot build session key: extra dimension "${name}" is missing or empty.`);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
function decodeSegment(value, label) {
|
|
80
|
+
try {
|
|
81
|
+
return decodeURIComponent(value);
|
|
82
|
+
}
|
|
83
|
+
catch {
|
|
84
|
+
throw new Error(`Invalid session key: ${label} is not valid URI encoding.`);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { StatefulProviderSessionContext } from "./stateful-provider-adapter.js";
|
|
2
|
+
import type { StatefulOperationRequest } from "./stateful-provider-session-routing.js";
|
|
3
|
+
import type { ManagedSession, SessionOwnerRecord } from "./stateful-provider-session-runtime.js";
|
|
4
|
+
export declare function makeStatefulProviderSessionContext(providerId: string, owner: SessionOwnerRecord, request: StatefulOperationRequest, signal: AbortSignal): StatefulProviderSessionContext;
|
|
5
|
+
export declare function makeStatefulProviderCloseContext<TSession>(providerId: string, session: ManagedSession<TSession>): StatefulProviderSessionContext;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
export function makeStatefulProviderSessionContext(providerId, owner, request, signal) {
|
|
2
|
+
const ctx = {
|
|
3
|
+
sessionKey: owner.sessionKey,
|
|
4
|
+
providerId,
|
|
5
|
+
connectionId: request.connectionId,
|
|
6
|
+
serviceAccountId: request.serviceAccountId,
|
|
7
|
+
ownerPodId: owner.ownerPodId,
|
|
8
|
+
ownerEndpoint: owner.ownerEndpoint,
|
|
9
|
+
ownerStatus: owner.status,
|
|
10
|
+
generation: owner.generation,
|
|
11
|
+
signal,
|
|
12
|
+
requestId: request.requestId,
|
|
13
|
+
operationId: request.operationId,
|
|
14
|
+
...(request.runtimeContext !== undefined ? { runtimeContext: request.runtimeContext } : {}),
|
|
15
|
+
};
|
|
16
|
+
return withOptionalRequestMetadata(ctx, request);
|
|
17
|
+
}
|
|
18
|
+
export function makeStatefulProviderCloseContext(providerId, session) {
|
|
19
|
+
const identity = session.identity;
|
|
20
|
+
return {
|
|
21
|
+
sessionKey: session.sessionKey,
|
|
22
|
+
providerId,
|
|
23
|
+
...(identity
|
|
24
|
+
? {
|
|
25
|
+
connectionId: identity.connectionId,
|
|
26
|
+
serviceAccountId: identity.serviceAccountId,
|
|
27
|
+
ownerPodId: identity.ownerPodId,
|
|
28
|
+
ownerEndpoint: identity.ownerEndpoint,
|
|
29
|
+
ownerStatus: identity.ownerStatus,
|
|
30
|
+
}
|
|
31
|
+
: {}),
|
|
32
|
+
generation: session.generation,
|
|
33
|
+
signal: new AbortController().signal,
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
function withOptionalRequestMetadata(ctx, request) {
|
|
37
|
+
return {
|
|
38
|
+
...ctx,
|
|
39
|
+
...(request.idempotencyKey ? { idempotencyKey: request.idempotencyKey } : {}),
|
|
40
|
+
...(request.deadlineAt ? { deadlineAt: request.deadlineAt } : {}),
|
|
41
|
+
};
|
|
42
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { StatefulProviderMetricEmitter } from "./stateful-provider-observability.js";
|
|
2
|
+
import type { StatefulOperationRequest } from "./stateful-provider-session-routing.js";
|
|
3
|
+
import type { SessionOwnerRecord } from "./stateful-provider-session-runtime.js";
|
|
4
|
+
export declare function observeStatefulSessionOperationDuration(input: {
|
|
5
|
+
readonly metricEmitter: StatefulProviderMetricEmitter;
|
|
6
|
+
readonly request: StatefulOperationRequest;
|
|
7
|
+
readonly owner: SessionOwnerRecord;
|
|
8
|
+
readonly durationMs: number;
|
|
9
|
+
}): void;
|
|
10
|
+
export declare function emitStatefulSessionInvalidatedMetric(input: {
|
|
11
|
+
readonly metricEmitter: StatefulProviderMetricEmitter;
|
|
12
|
+
readonly request: StatefulOperationRequest;
|
|
13
|
+
readonly owner: SessionOwnerRecord;
|
|
14
|
+
readonly reason: string;
|
|
15
|
+
}): void;
|