-
Notifications
You must be signed in to change notification settings - Fork 165
Expand file tree
/
Copy pathcore.ts
More file actions
436 lines (402 loc) · 13.3 KB
/
Copy pathcore.ts
File metadata and controls
436 lines (402 loc) · 13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
import { createLogger } from "@photon-ai/otel";
import type {
InboundReply,
RawInboundEvent,
} from "@photon-ai/proto/photon/fusor/v1/inbound";
import type { ProviderMessageRecord } from "../platform/types";
import { officialProviderInstallHint } from "../utils/provider-packages";
import { errorAttrs } from "../utils/telemetry";
import { createFusorTokenProvider, type FusorTokenProvider } from "./auth";
import { FUSOR_MESSAGES_CHANNEL, isFusorEvent } from "./event";
import { type ParsedHttpRequest, parseHttpRequest } from "./parse";
import type { FusorMessagesReturn, FusorReply, FusorVerify } from "./types";
import {
type FusorWsSession,
isWsAuthError,
runFusorWsSession,
} from "./websocket";
const DEFAULT_FUSOR_WS_URL =
"wss://fusor-ws.spectrum.photon.codes/v1/subscribe";
const RECONNECT_BASE_MS = 1000;
const RECONNECT_MAX_MS = 30_000;
const log = createLogger("spectrum.fusor");
const errorText = (error: unknown): string =>
error instanceof Error ? error.message : String(error);
export interface RegisteredFusorHandler<TPayload = unknown> {
messages: (ctx: {
payload: TPayload;
respond: (reply: FusorReply) => void;
}) => FusorMessagesReturn | Promise<FusorMessagesReturn>;
// Route a `fusorEvent(channel, data)` to its custom event channel. Wired by
// the Spectrum bootstrap to the per-(platform, channel) queue.
pushEvent: (channel: string, data: unknown) => void;
pushMessage: (record: ProviderMessageRecord) => void;
verify: FusorVerify<TPayload>;
}
export interface FusorEventMetadata {
eventId: string;
platform: string;
}
function toReplyBytes(body: string | Uint8Array | undefined): Uint8Array {
if (body === undefined) {
return new Uint8Array(0);
}
if (typeof body === "string") {
return new TextEncoder().encode(body);
}
return body;
}
interface HandlerOutcome {
errorReason?: string;
ok: boolean;
reply?: FusorReply;
}
function combineReplies(outcomes: HandlerOutcome[]): InboundReply {
const successes = outcomes.filter((o) => o.ok);
if (successes.length === 0) {
const firstFailure = outcomes[0];
return {
eventId: "",
errorReason: firstFailure?.errorReason ?? "no handler succeeded",
status: 0,
headers: {},
body: new Uint8Array(0),
};
}
let status = 0;
const headers: Record<string, string> = {};
let body: Uint8Array = new Uint8Array(0);
for (const outcome of successes) {
const reply = outcome.reply;
if (!reply) {
continue;
}
if (reply.status !== undefined && reply.status > status) {
status = reply.status;
}
if (reply.headers) {
for (const [k, v] of Object.entries(reply.headers)) {
headers[k.toLowerCase()] = v;
}
}
const candidate = toReplyBytes(reply.body);
if (candidate.length > 0) {
body = candidate;
}
}
return {
eventId: "",
errorReason: "",
status,
headers,
body,
};
}
// Route a handler's return value. A bare record (or `fusorEvent("messages", …)`)
// goes to the message sink (`deliver`, which the webhook path overrides);
// `fusorEvent(channel, …)` goes to its per-channel queue via `pushEvent` —
// always, on both transports, since the webhook handler is messages-only.
function routeHandlerResult(
result: FusorMessagesReturn,
handler: RegisteredFusorHandler,
deliver: (record: ProviderMessageRecord) => void
): void {
if (result === undefined) {
return;
}
const items = Array.isArray(result) ? result : [result];
for (const item of items) {
if (!isFusorEvent(item)) {
deliver(item);
continue;
}
if (item.name === FUSOR_MESSAGES_CHANNEL) {
deliver(item.data as ProviderMessageRecord);
} else {
handler.pushEvent(item.name, item.data);
}
}
}
function runHandlerOnce<TPayload>(
handler: RegisteredFusorHandler<TPayload>,
parsedRequest: ParsedHttpRequest,
deliver: (record: ProviderMessageRecord) => void = handler.pushMessage
): Promise<HandlerOutcome> {
return (async () => {
try {
const payload = await handler.verify(parsedRequest);
let reply: FusorReply | undefined;
let respondCalled = false;
let returned = false;
const respond = (next: FusorReply): void => {
if (returned) {
log.warn("fusor.respond called after handler returned; ignoring");
return;
}
if (respondCalled) {
log.debug("fusor.respond called more than once; last call wins");
}
respondCalled = true;
reply = next;
};
const result = await handler.messages({ payload, respond });
returned = true;
routeHandlerResult(result, handler as RegisteredFusorHandler, deliver);
return { ok: true, reply };
} catch (error) {
return { ok: false, errorReason: errorText(error) };
}
})();
}
export interface FusorCoreOptions {
// Optional: only the streaming transport (start) needs cloud credentials to
// mint a token. The webhook path (processRequest) routes registered handlers
// without them, so a webhook-only Spectrum can construct a core with
// neither set.
projectId?: string;
projectSecret?: string;
/**
* fusor-fanout-websocket endpoint (`wss://…/v1/subscribe`) — the
* streaming transport. Defaults to the `SPECTRUM_FUSOR_WS_URL` env
* var, then the production endpoint.
*/
websocketEndpoint?: string;
}
export class FusorCore {
private readonly options: FusorCoreOptions;
private readonly websocketEndpoint: string;
private readonly handlers = new Map<string, RegisteredFusorHandler[]>();
private tokenProvider?: FusorTokenProvider;
private wsSession?: FusorWsSession;
private connectionLoop?: Promise<void>;
private started = false;
private stopped = false;
private stopResolve?: () => void;
private readonly stoppedPromise: Promise<void>;
// The reconnect backoff sleep, made cancelable so close() can wake it.
private reconnectTimer?: ReturnType<typeof setTimeout>;
private reconnectResolve?: () => void;
constructor(options: FusorCoreOptions) {
this.options = options;
this.websocketEndpoint =
options.websocketEndpoint ??
process.env.SPECTRUM_FUSOR_WS_URL ??
DEFAULT_FUSOR_WS_URL;
this.stoppedPromise = new Promise<void>((resolve) => {
this.stopResolve = resolve;
});
}
register<TPayload>(
platform: string,
handler: RegisteredFusorHandler<TPayload>
): void {
const list = this.handlers.get(platform) ?? [];
list.push(handler as RegisteredFusorHandler);
this.handlers.set(platform, list);
}
async start(): Promise<void> {
if (!(this.options.projectId && this.options.projectSecret)) {
throw new Error(
"fusor: streaming via spectrum.messages requires projectId and projectSecret"
);
}
// Idempotent: a second start() must not spin up a duplicate token provider,
// channel, or connection loop. The flag is set synchronously before the
// first await so concurrent calls are guarded too.
if (this.started) {
return;
}
this.started = true;
this.tokenProvider = await createFusorTokenProvider(
this.options.projectId,
this.options.projectSecret
);
this.connectionLoop = this.runConnectionLoop().catch((error) => {
log.error("fusor connection loop crashed", errorAttrs(error), error);
});
}
// Streaming transport: the fusor.v1.json WebSocket plane. A session that
// runs to a clean end reconnects immediately; an errored session backs
// off exponentially (reset on the next clean run).
private async runConnectionLoop(): Promise<void> {
let attempt = 0;
while (!this.stopped) {
const wsRan = await this.tryWebsocketOnce();
if (this.stopped) {
return;
}
if (wsRan) {
attempt = 0;
continue;
}
attempt += 1;
await this.backoffSleep(this.backoffMs(attempt));
}
}
// True when the stream ran to a clean end; false when it errored (the
// loop then backs off before reconnecting).
private async tryWebsocketOnce(): Promise<boolean> {
try {
await this.runWebsocketOnce();
return true;
} catch (error) {
// Drop a stale token on auth failure so the next attempt mints a
// fresh one instead of replaying the rejected token.
if (isWsAuthError(error)) {
this.tokenProvider?.invalidate();
}
if (!this.stopped) {
log.warn(
"fusor websocket stream errored; reconnecting",
errorAttrs(error),
error
);
}
return false;
}
}
private backoffMs(attempt: number): number {
return Math.min(RECONNECT_BASE_MS * 2 ** (attempt - 1), RECONNECT_MAX_MS);
}
// Cancelable sleep: close() clears the timer and resolves it so
// shutdown doesn't wait out the (up to 30s) backoff.
private async backoffSleep(backoff: number): Promise<void> {
await new Promise<void>((resolve) => {
this.reconnectResolve = resolve;
const timer = setTimeout(resolve, backoff);
timer.unref?.();
this.reconnectTimer = timer;
});
this.reconnectTimer = undefined;
this.reconnectResolve = undefined;
}
private async runWebsocketOnce(): Promise<void> {
if (!this.tokenProvider) {
throw new Error("fusor: token not initialized");
}
const token = await this.tokenProvider.getToken();
const session = runFusorWsSession({
url: this.websocketEndpoint,
token,
onEvent: async (event, sendReply) => {
if (this.stopped) {
return;
}
const reply = await this.processEvent(event);
// The server answers unexpected replies with typed notices —
// only reply when asked (sendReply is set iff replyExpected).
sendReply?.(reply);
},
});
this.wsSession = session;
try {
await session.done;
} finally {
this.wsSession = undefined;
}
}
private noHandlerReply(event: FusorEventMetadata): InboundReply {
// Reply shape stays wire-compatible; only the local log gets the install
// hint (since v5 the official providers are separate packages, so "no
// handler" is usually a missing install, not a routing bug).
const hint = officialProviderInstallHint(event.platform);
log.warn(
hint
? `fusor: no handler for platform — ${hint}`
: "fusor: no handler for platform",
{
"spectrum.fusor.platform": event.platform,
"spectrum.fusor.event_id": event.eventId,
}
);
return {
eventId: event.eventId,
errorReason: `no handler for platform ${event.platform}`,
status: 0,
headers: {},
body: new Uint8Array(0),
};
}
private async processParsedRequest(
event: FusorEventMetadata,
parsedRequest: ParsedHttpRequest,
handlers: RegisteredFusorHandler[],
deliver?: (record: ProviderMessageRecord) => void
): Promise<InboundReply> {
const outcomes = await Promise.all(
handlers.map((handler) => runHandlerOnce(handler, parsedRequest, deliver))
);
const combined = combineReplies(outcomes);
combined.eventId = event.eventId;
return combined;
}
// WebSocket events still carry protobuf/raw HTTP. Parse that transport shape,
// then hand the resulting request to the shared provider pipeline.
async processEvent(
event: RawInboundEvent,
deliver?: (record: ProviderMessageRecord) => void
): Promise<InboundReply> {
const handlers = this.handlers.get(event.platform) ?? [];
if (handlers.length === 0) {
return this.noHandlerReply(event);
}
let parsedRequest: ParsedHttpRequest;
try {
parsedRequest = parseHttpRequest(event.rawRequest);
} catch (error) {
const errorReason = errorText(error);
log.warn("fusor: failed to parse raw_request", {
"spectrum.fusor.platform": event.platform,
"spectrum.fusor.event_id": event.eventId,
...errorAttrs(error),
});
return {
eventId: event.eventId,
errorReason,
status: 0,
headers: {},
body: new Uint8Array(0),
};
}
return this.processParsedRequest(event, parsedRequest, handlers, deliver);
}
// HTTP JSON deliveries already provide method/path/headers plus the exact
// original body bytes, so they enter after raw HTTP parsing. Keeping this
// seam shared means WebSocket protobuf behavior remains unchanged.
async processRequest(
event: FusorEventMetadata,
parsedRequest: ParsedHttpRequest,
deliver?: (record: ProviderMessageRecord) => void
): Promise<InboundReply> {
const handlers = this.handlers.get(event.platform) ?? [];
if (handlers.length === 0) {
return this.noHandlerReply(event);
}
return this.processParsedRequest(event, parsedRequest, handlers, deliver);
}
async close(): Promise<void> {
if (this.stopped) {
return;
}
this.stopped = true;
this.wsSession?.close();
// Wake an in-progress reconnect backoff so the loop observes stopped and
// exits immediately instead of waiting out the timer.
if (this.reconnectTimer) {
clearTimeout(this.reconnectTimer);
this.reconnectTimer = undefined;
}
this.reconnectResolve?.();
this.reconnectResolve = undefined;
if (this.tokenProvider) {
await this.tokenProvider.dispose();
}
if (this.connectionLoop) {
await this.connectionLoop;
}
this.stopResolve?.();
}
async waitStopped(): Promise<void> {
return this.stoppedPromise;
}
}