holon-sh 0.0.1-dev.12 → 0.0.1-dev.13
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/README.md +2 -30
- package/dist/ext/events.d.ts +15 -155
- package/dist/ext/events.d.ts.map +1 -1
- package/dist/ext/events.js +92 -217
- package/dist/ext/events.js.map +1 -1
- package/dist/ext/files.client.d.ts +1 -1
- package/dist/ext/files.client.d.ts.map +1 -1
- package/dist/ext/files.client.js +2 -2
- package/dist/ext/files.client.js.map +1 -1
- package/dist/protocol.js +1 -1
- package/dist/protocol.js.map +1 -1
- package/dist/runtime.d.ts.map +1 -1
- package/dist/runtime.js +17 -12
- package/dist/runtime.js.map +1 -1
- package/package.json +1 -2
- package/src/ext/events.ts +130 -327
- package/src/ext/files.client.ts +2 -2
- package/src/protocol.ts +1 -1
- package/src/runtime.ts +17 -13
- package/dist/ext/webhooks.d.ts +0 -18
- package/dist/ext/webhooks.d.ts.map +0 -1
- package/dist/ext/webhooks.js +0 -527
- package/dist/ext/webhooks.js.map +0 -1
- package/src/ext/webhooks.ts +0 -512
package/src/ext/events.ts
CHANGED
|
@@ -1,19 +1,14 @@
|
|
|
1
1
|
// The server side of MCP Events, as the Triggers & Events Working Group's design sketch defines it:
|
|
2
2
|
// https://github.com/modelcontextprotocol/experimental-ext-triggers-events/blob/main/docs/design-sketch-proposal.md
|
|
3
|
-
// The draft is experimental, so this lives under ext and may change with it.
|
|
4
|
-
//
|
|
5
|
-
// that reaches the event types through the internals below. Names follow the sketch, and types are the MCP SDK's own
|
|
6
|
-
// wherever it has them.
|
|
3
|
+
// The draft is experimental, so this lives under ext and may change with it. It serves the poll and push delivery
|
|
4
|
+
// modes. Names follow the sketch, and types are the MCP SDK's own wherever it has them.
|
|
7
5
|
import { ProtocolError, ProtocolErrorCode, SUBSCRIPTION_ID_META_KEY } from "@modelcontextprotocol/server";
|
|
8
6
|
import type { AuthInfo, JSONRPCErrorResponse, McpServer, Server, StandardSchemaWithJSON } from "@modelcontextprotocol/server";
|
|
9
|
-
import { webhooks, type WebhookOptions } from "./webhooks.js";
|
|
10
|
-
|
|
11
|
-
export type { WebhookOptions } from "./webhooks.js";
|
|
12
7
|
|
|
13
8
|
/** The request a subscription came in, as Holon admitted it: read its caller with getContext. */
|
|
14
9
|
export type SubscriptionContext = { authInfo?: AuthInfo | undefined } | undefined;
|
|
15
10
|
/** How a subscription receives its events. */
|
|
16
|
-
export type DeliveryModeName = "poll" | "push"
|
|
11
|
+
export type DeliveryModeName = "poll" | "push";
|
|
17
12
|
/** A JSON-RPC error, as a subscription that is terminated or fails is told it. */
|
|
18
13
|
export type EventError = JSONRPCErrorResponse["error"];
|
|
19
14
|
/** A subscription as it begins: who asked, for what, and by which delivery mode. */
|
|
@@ -39,27 +34,7 @@ export interface EventOccurrence<D = unknown> {
|
|
|
39
34
|
/** When it happened, in ISO 8601. */
|
|
40
35
|
timestamp: string;
|
|
41
36
|
data: D;
|
|
42
|
-
cursor?: string
|
|
43
|
-
_meta?: Record<string, unknown>;
|
|
44
|
-
}
|
|
45
|
-
/**
|
|
46
|
-
* What happened after a cursor, as a check function finds it: the sketch's EventResult. Each event is an
|
|
47
|
-
* EventOccurrence whose name is the type's, and whose `eventId` (the source's own, so a client drops one it has seen)
|
|
48
|
-
* and `timestamp` are made up when not given. An event's `cursor`, the position just after it, lets a batch be cut
|
|
49
|
-
* short; without it a batch is delivered whole.
|
|
50
|
-
*/
|
|
51
|
-
export interface EventResult<D> {
|
|
52
|
-
/**
|
|
53
|
-
* Empty when the cursor was null: a null cursor asks only for the current position. A source without positions
|
|
54
|
-
* answers with a null cursor, is asked with null each time, and answers each time with what is new.
|
|
55
|
-
*/
|
|
56
|
-
events: Array<Omit<EventOccurrence<D>, "name" | "eventId" | "timestamp" | "cursor"> & { eventId?: string; timestamp?: string; cursor?: string }>;
|
|
57
|
-
/** The position after these events, or null when the source cannot be resumed from a position. */
|
|
58
|
-
cursor: string | null;
|
|
59
|
-
/** Events between the cursor and these were skipped, for instance because the source no longer keeps them. */
|
|
60
|
-
truncated?: boolean;
|
|
61
|
-
/** More events are ready at once after this batch. */
|
|
62
|
-
hasMore?: boolean;
|
|
37
|
+
cursor?: string;
|
|
63
38
|
}
|
|
64
39
|
/** An event type: the arguments a subscription gives, and the data each occurrence carries. */
|
|
65
40
|
export interface EventDefinition<A, D> {
|
|
@@ -79,14 +54,7 @@ export interface EventDefinition<A, D> {
|
|
|
79
54
|
match?: (data: D, args: A, context: SubscriptionContext) => boolean;
|
|
80
55
|
/** The data a subscription with these arguments receives of an emitted event. Without it, the data as emitted. */
|
|
81
56
|
transform?: (data: D, args: A, context: SubscriptionContext) => unknown;
|
|
82
|
-
/**
|
|
83
|
-
* What happened after a cursor, for a source that keeps its own history: polls are answered by it, and streams ask
|
|
84
|
-
* it every second. The cursor is the source's own, so it outlives the process. A null cursor asks for the
|
|
85
|
-
* current position. The function filters and shapes for the arguments itself; `match` and `transform` apply only to
|
|
86
|
-
* what is emitted. Without it, emitted events are kept in memory and polls and streams are served from there.
|
|
87
|
-
*/
|
|
88
|
-
check?: (cursor: string | null, args: A, context: SubscriptionContext) => EventResult<D> | Promise<EventResult<D>>;
|
|
89
|
-
/** The delivery modes this event type offers. Every mode served unless given, except webhook for a type with a check function, which webhook delivery does not read. */
|
|
57
|
+
/** The delivery modes this event type offers: poll and push unless given. */
|
|
90
58
|
delivery?: DeliveryModeName[];
|
|
91
59
|
/**
|
|
92
60
|
* Whether the caller may subscribe with these arguments. A subscription is refused with Forbidden when it may not,
|
|
@@ -110,33 +78,29 @@ export interface EventDefinition<A, D> {
|
|
|
110
78
|
export interface Emitter<D> {
|
|
111
79
|
/**
|
|
112
80
|
* `eventId` lets a client drop an occurrence it has seen. Give the source's own id when it has one. With
|
|
113
|
-
* `subscriptionId`, the sketch's targeted emit, the occurrence goes to that subscription alone, whatever its
|
|
114
|
-
* other, so a subscription that reconnects or polls again from its cursor still has
|
|
115
|
-
*
|
|
116
|
-
* reconnecting streams learn of it from the check function, and one emitted to a subscription that does not get it
|
|
117
|
-
* on a stream is lost, so that subscription's next poll or stream is told of a gap.
|
|
81
|
+
* `subscriptionId`, the sketch's targeted emit, the occurrence goes to that subscription alone, whatever its
|
|
82
|
+
* arguments; it is kept like any other, so a subscription that reconnects or polls again from its cursor still has
|
|
83
|
+
* it, or is told of a gap.
|
|
118
84
|
*
|
|
119
85
|
* Emitting is synchronous. Occurrences emitted one after another in the same synchronous run reach each
|
|
120
86
|
* subscription in that order, with no other occurrence of this type between them, whether polled or pushed. So a
|
|
121
87
|
* group of changes that nothing may come between is either emitted in one run or sent as one occurrence holding a
|
|
122
88
|
* list. A subscription skips part of a group only when it is told of a gap: the group outgrew the events kept, or
|
|
123
|
-
* the subscription fell behind them.
|
|
124
|
-
* not between what is emitted and what the check function finds, which may arrive later or again.
|
|
89
|
+
* the subscription fell behind them.
|
|
125
90
|
*
|
|
126
91
|
* The data is serialised when it is emitted, so data that cannot be sent as JSON throws here, before anything is
|
|
127
92
|
* kept or sent.
|
|
128
93
|
*/
|
|
129
94
|
emit(data: D, options?: { eventId?: string; timestamp?: string; subscriptionId?: string }): void;
|
|
130
95
|
/**
|
|
131
|
-
* The position after the last occurrence emitted
|
|
132
|
-
*
|
|
133
|
-
* state and none before. A type with a check function has no such position here: its cursor is the source's own,
|
|
134
|
-
* so read the source's head instead. This throws for such a type.
|
|
96
|
+
* The position after the last occurrence emitted. Return it with the state a tool reads, in the same synchronous
|
|
97
|
+
* run, and a client that subscribes from it hears every change after that state and none before.
|
|
135
98
|
*/
|
|
136
99
|
cursor(): string;
|
|
137
100
|
/**
|
|
138
|
-
* Terminates one subscription, or all of this type's: a stream is sent
|
|
139
|
-
* `
|
|
101
|
+
* Terminates one subscription, or all of this type's: a stream is sent what was emitted to it before, then
|
|
102
|
+
* `notifications/events/terminated` with `error`, and closed. The next poll is answered with the error. Forbidden
|
|
103
|
+
* unless given, which ends delivery at once, since access is lost.
|
|
140
104
|
*/
|
|
141
105
|
terminate(subscriptionId?: string, error?: EventError): void;
|
|
142
106
|
/**
|
|
@@ -144,49 +108,31 @@ export interface Emitter<D> {
|
|
|
144
108
|
*/
|
|
145
109
|
remove(): void;
|
|
146
110
|
}
|
|
147
|
-
export interface EventsOptions {
|
|
148
|
-
/**
|
|
149
|
-
* The sketch's webhook_ttl, which turns webhook delivery on: the longest lifetime granted to a webhook
|
|
150
|
-
* subscription, in milliseconds. A client's suggestion is held to it, and every grant expires. Without it, webhook
|
|
151
|
-
* delivery is not offered.
|
|
152
|
-
*/
|
|
153
|
-
webhookTtl?: number;
|
|
154
|
-
/** For tests of webhook delivery. */
|
|
155
|
-
webhook?: WebhookOptions;
|
|
156
|
-
}
|
|
157
|
-
/** A delivery mode besides poll and push: webhook. It registers its requests on each server and is listed by events/list. Not for applications. */
|
|
158
|
-
export interface DeliveryMode {
|
|
159
|
-
readonly name: string;
|
|
160
|
-
serve(server: object, internals: Internals): void;
|
|
161
|
-
}
|
|
162
111
|
/** The event types of a process, served by each MCP server it creates. */
|
|
163
112
|
export interface Events {
|
|
164
113
|
/** Declares an event type. Emit its occurrences with the returned emitter. */
|
|
165
114
|
event<A, D>(name: string, definition: EventDefinition<A, D>): Emitter<D>;
|
|
166
115
|
/** Serves events/list, events/poll and events/stream on an McpServer or a Server, and declares the extension. */
|
|
167
116
|
serve(server: McpServer | Server): void;
|
|
168
|
-
/** What a delivery mode works with. Not for applications. */
|
|
169
|
-
readonly [INTERNALS]: Internals;
|
|
170
117
|
}
|
|
171
118
|
|
|
172
|
-
/**
|
|
173
|
-
export
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
/** Where delivery starts. */
|
|
180
|
-
start: string | null;
|
|
181
|
-
cursor: string | null;
|
|
182
|
-
truncated: boolean;
|
|
183
|
-
hasMore: boolean;
|
|
119
|
+
/** The identifier a server declares the Events extension under, in capabilities.extensions. */
|
|
120
|
+
export const EVENTS_EXTENSION = "io.modelcontextprotocol/events";
|
|
121
|
+
|
|
122
|
+
// The parts of an MCP server and of a request's context this module uses.
|
|
123
|
+
interface Served {
|
|
124
|
+
registerCapabilities(capabilities: object): void;
|
|
125
|
+
setRequestHandler(method: string, schemas: object, handler: (request: any, ctx: Context) => unknown): void;
|
|
184
126
|
}
|
|
185
|
-
|
|
186
|
-
|
|
127
|
+
interface Context {
|
|
128
|
+
mcpReq: { id: string | number; signal: AbortSignal; notify(notification: { method: string; params: object }): Promise<void> };
|
|
129
|
+
http?: SubscriptionContext & object;
|
|
130
|
+
}
|
|
131
|
+
/** An emitted event, kept at its sequence for polls and reconnecting streams. */
|
|
132
|
+
interface Kept { sequence: number; at: number; target: string | undefined; occurrence: EventOccurrence }
|
|
133
|
+
/** A subscription held by a push stream or a poll lease. */
|
|
134
|
+
interface Held {
|
|
187
135
|
readonly id: string;
|
|
188
|
-
readonly name: string;
|
|
189
|
-
readonly delivery: DeliveryModeName;
|
|
190
136
|
readonly arguments: unknown;
|
|
191
137
|
readonly context: SubscriptionContext;
|
|
192
138
|
/** Nothing is emitted to it alone: a poll lease shared by callers without an identity. */
|
|
@@ -196,58 +142,18 @@ export interface Held {
|
|
|
196
142
|
/** Says the hold is over. */
|
|
197
143
|
close(): void;
|
|
198
144
|
}
|
|
145
|
+
/** Who an emitted event is shaped for. */
|
|
146
|
+
type Reader = Pick<Held, "id" | "arguments" | "context" | "untargetable">;
|
|
199
147
|
/** The subscription a request asks for, checked. */
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
export interface EventTypeState {
|
|
148
|
+
interface Requested { id: string; lease: string; arguments: unknown; context: SubscriptionContext }
|
|
149
|
+
interface Type<A = any, D = any> {
|
|
203
150
|
readonly name: string;
|
|
204
|
-
readonly delivery: DeliveryModeName[];
|
|
205
|
-
readonly interval: number;
|
|
206
|
-
readonly check?: EventDefinition<unknown, unknown>["check"];
|
|
207
|
-
/** Whether the caller may hold the subscription, which a mode that holds subscriptions long asks again. */
|
|
208
|
-
authorize?(args: unknown, context: SubscriptionContext): boolean | Promise<boolean>;
|
|
209
|
-
/** Told each event as it is emitted: a push stream is one, and a delivery mode adds its own. */
|
|
210
|
-
readonly sinks: Set<(kept: Kept) => void>;
|
|
211
|
-
/** The subscriptions held by every mode, by id. */
|
|
212
|
-
readonly subscriptions: Map<string, { held: Set<Held>; ready: Promise<unknown> }>;
|
|
213
|
-
}
|
|
214
|
-
/** What a delivery mode works with. */
|
|
215
|
-
export interface Internals {
|
|
216
|
-
readonly types: Map<string, EventTypeState>;
|
|
217
|
-
/** Checks a request, names its event type and the subscription's id. */
|
|
218
|
-
readonly subscription: (request: Record<string, unknown>, context: SubscriptionContext, delivery?: DeliveryModeName) => Promise<{ type: EventTypeState; subscription: Requested }>;
|
|
219
|
-
/** Begins holding a subscription. */
|
|
220
|
-
readonly open: (type: EventTypeState, subscription: { id: string; delivery: DeliveryModeName; arguments: unknown; context: SubscriptionContext }, end: (error: EventError) => void) => Promise<Held>;
|
|
221
|
-
/** What a subscription is owed after a cursor. */
|
|
222
|
-
readonly read: (type: EventTypeState, subscription: Pick<Held, "id" | "arguments" | "context" | "untargetable">, cursor: string | null | undefined, options?: { maxAgeMs?: number; maxEvents?: number }) => Owed | Promise<Owed>;
|
|
223
|
-
/** What a subscription is owed of the events emitted. */
|
|
224
|
-
readonly since: (type: EventTypeState, subscription: Pick<Held, "id" | "arguments" | "context" | "untargetable">, cursor: string | null | undefined, maxAgeMs?: number, limit?: number) => Owed;
|
|
225
|
-
/** An emitted event as one subscription receives it, or nothing when it is not for it. */
|
|
226
|
-
readonly shape: (type: EventTypeState, kept: Kept, subscription: Pick<Held, "id" | "arguments" | "context" | "untargetable">, failed?: (error: EventError) => void) => EventOccurrence | undefined;
|
|
227
|
-
/** A type's current cursor. */
|
|
228
|
-
readonly position: (type: EventTypeState, sequence?: number) => string;
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
/** The identifier a server declares the Events extension under, in capabilities.extensions. */
|
|
232
|
-
export const EVENTS_EXTENSION = "io.modelcontextprotocol/events";
|
|
233
|
-
/** Reaches what a delivery mode needs of the events it serves. Not for applications. */
|
|
234
|
-
export const INTERNALS: unique symbol = Symbol.for("holon-sh.events.internals");
|
|
235
|
-
|
|
236
|
-
// The parts of an MCP server and of a request's context this module uses.
|
|
237
|
-
interface Served {
|
|
238
|
-
registerCapabilities(capabilities: object): void;
|
|
239
|
-
setRequestHandler(method: string, schemas: object, handler: (request: any, ctx: Context) => unknown): void;
|
|
240
|
-
}
|
|
241
|
-
interface Context {
|
|
242
|
-
mcpReq: { id: string | number; signal: AbortSignal; notify(notification: { method: string; params: object }): Promise<void> };
|
|
243
|
-
http?: SubscriptionContext & object;
|
|
244
|
-
}
|
|
245
|
-
interface Type<A = any, D = any> extends EventTypeState {
|
|
246
151
|
readonly order: number;
|
|
247
152
|
readonly description?: string | undefined;
|
|
248
153
|
readonly inputSchema: StandardSchemaWithJSON<unknown, A>;
|
|
249
154
|
readonly payloadSchema: StandardSchemaWithJSON;
|
|
250
155
|
readonly _meta?: Record<string, unknown> | undefined;
|
|
156
|
+
readonly delivery: DeliveryModeName[];
|
|
251
157
|
readonly match?: EventDefinition<A, D>["match"];
|
|
252
158
|
readonly transform?: EventDefinition<A, D>["transform"];
|
|
253
159
|
readonly authorize?: EventDefinition<A, D>["authorize"];
|
|
@@ -255,11 +161,13 @@ interface Type<A = any, D = any> extends EventTypeState {
|
|
|
255
161
|
readonly onUnsubscribe?: EventDefinition<A, D>["onUnsubscribe"];
|
|
256
162
|
sequence: number;
|
|
257
163
|
dropped: number;
|
|
258
|
-
kept:
|
|
259
|
-
/**
|
|
260
|
-
|
|
164
|
+
kept: Kept[];
|
|
165
|
+
/** Woken by each emitted event: one for each push stream. */
|
|
166
|
+
readonly sinks: Set<() => void>;
|
|
167
|
+
/** The subscriptions held by streams and poll leases, by id. */
|
|
168
|
+
readonly subscriptions: Map<string, { held: Set<Held>; ready: Promise<Subscription<A>> }>;
|
|
261
169
|
}
|
|
262
|
-
interface Lease { type: Type; opening
|
|
170
|
+
interface Lease { type: Type; opening: Promise<Held>; ended?: EventError; release?: () => void; timer?: ReturnType<typeof setTimeout> }
|
|
263
171
|
|
|
264
172
|
const INVALID_PARAMS = ProtocolErrorCode.InvalidParams;
|
|
265
173
|
const INTERNAL_ERROR = ProtocolErrorCode.InternalError;
|
|
@@ -294,7 +202,7 @@ const caller = (context: SubscriptionContext): string | null => {
|
|
|
294
202
|
};
|
|
295
203
|
const hex = (bytes: Uint8Array) => [...bytes].map(byte => byte.toString(16).padStart(2, "0")).join("");
|
|
296
204
|
// A subscription's id is the same for the same caller, event type and arguments, so it survives reconnects and poll
|
|
297
|
-
// leases
|
|
205
|
+
// leases. It is made with the global crypto, which needs no node:crypto at load.
|
|
298
206
|
const identify = async (who: string | null, name: string, args: unknown) => {
|
|
299
207
|
const digest = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(JSON.stringify([who, name, canonical(args)])));
|
|
300
208
|
return `sub_${hex(new Uint8Array(digest, 0, 16))}`;
|
|
@@ -305,11 +213,11 @@ const unique = () => `sub_${hex(crypto.getRandomValues(new Uint8Array(16)))}`;
|
|
|
305
213
|
const settle = (run: () => unknown) => { Promise.resolve().then(run).catch(() => {}); };
|
|
306
214
|
const integer = (value: unknown, minimum: number) => value === undefined || Number.isInteger(value) && (value as number) >= minimum;
|
|
307
215
|
|
|
308
|
-
|
|
309
|
-
// emit buffer.
|
|
216
|
+
const OFFERED: DeliveryModeName[] = ["poll", "push"];
|
|
217
|
+
// What each type keeps of what it emits, for polls and reconnecting streams: the sketch's emit buffer.
|
|
310
218
|
const KEPT_EVENTS = 10_000;
|
|
311
219
|
const KEPT_AGE = 10 * 60_000;
|
|
312
|
-
// How long a poll is told to wait before the next
|
|
220
|
+
// How long a poll is told to wait before the next.
|
|
313
221
|
const INTERVAL = 1000;
|
|
314
222
|
// How often a stream hears from the server when nothing happens, as the sketch recommends.
|
|
315
223
|
const HEARTBEAT = 30_000;
|
|
@@ -332,13 +240,10 @@ const STREAM_BUDGET = 2 ** 20;
|
|
|
332
240
|
* Event types a process emits and the MCP servers it creates serve, in the poll and push delivery modes of MCP Events,
|
|
333
241
|
* the draft extension of the Triggers & Events Working Group. Each type keeps the last ten thousand events it emits,
|
|
334
242
|
* for ten minutes, for polls and for streams that reconnect: those cursors last as long as the process, and after a
|
|
335
|
-
* restart a client is told that events were skipped. A
|
|
336
|
-
*
|
|
337
|
-
* the server every thirty seconds.
|
|
243
|
+
* restart a client is told that events were skipped. A poll is told to come back every second, and a stream hears
|
|
244
|
+
* from the server every thirty seconds.
|
|
338
245
|
*/
|
|
339
|
-
export function createEvents(
|
|
340
|
-
const modes = webhookTtl === undefined ? [] : [webhooks({ ...webhook, ttl: webhookTtl, heartbeat: HEARTBEAT })];
|
|
341
|
-
const offered = ["poll", "push", ...modes.map(mode => mode.name)];
|
|
246
|
+
export function createEvents(): Events {
|
|
342
247
|
// Cursors are positions in this process's memory. One from another process names nothing here. The name is made
|
|
343
248
|
// without node:crypto, which is not there when a server is evaluated for its contract.
|
|
344
249
|
const epoch = `${Date.now().toString(36)}${Math.random().toString(36).slice(2, 10)}`;
|
|
@@ -347,33 +252,23 @@ export function createEvents({ webhookTtl, webhook }: EventsOptions = {}): Event
|
|
|
347
252
|
let subscribed = 0;
|
|
348
253
|
const callers = new Map<string, number>();
|
|
349
254
|
|
|
350
|
-
const position = (type:
|
|
351
|
-
|
|
352
|
-
// An event emitted to one subscription that it could not be given is owed to it as a gap, on its next poll or
|
|
353
|
-
// stream. The ids owed one are as many as the events a type keeps.
|
|
354
|
-
const owe = (type: Type, id: string) => {
|
|
355
|
-
type.owed.delete(id);
|
|
356
|
-
type.owed.set(id, true);
|
|
357
|
-
if (type.owed.size > KEPT_EVENTS) type.owed.delete(type.owed.keys().next().value!);
|
|
358
|
-
};
|
|
255
|
+
const position = (type: Type, sequence = type.sequence) => `${epoch}.${sequence}`;
|
|
359
256
|
|
|
360
|
-
// What a subscription is owed after a cursor
|
|
361
|
-
//
|
|
362
|
-
// the events still kept starts from the oldest kept, and
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
const
|
|
366
|
-
|
|
367
|
-
const
|
|
368
|
-
const
|
|
369
|
-
|
|
370
|
-
const floor = maxAgeMs === undefined ? -Infinity : Date.now() - maxAgeMs;
|
|
371
|
-
const later = type.kept.filter(kept => kept.sequence > sequence);
|
|
257
|
+
// What a subscription is owed after a cursor, up to `until`: the events for it, in order, where delivery starts,
|
|
258
|
+
// and where it now stands. A null cursor starts where the subscription was opened, and so does one from another
|
|
259
|
+
// process, which says that events were skipped. One past the events still kept starts from the oldest kept, and
|
|
260
|
+
// says so too.
|
|
261
|
+
const since = (type: Type, subscription: Reader, cursor: string | null | undefined, { maxAgeMs, maxEvents: limit = Infinity, opened = type.sequence, until = type.sequence }: { maxAgeMs?: number; maxEvents?: number; opened?: number; until?: number } = {}) => {
|
|
262
|
+
const [process, text] = cursor?.split(".") ?? [];
|
|
263
|
+
const resumed = cursor != null && process === epoch && Number.isInteger(Number(text)) && Number(text) <= type.sequence;
|
|
264
|
+
const sequence = resumed ? Number(text) : opened;
|
|
265
|
+
const floor = maxAgeMs === undefined || !resumed ? -Infinity : Date.now() - maxAgeMs;
|
|
266
|
+
const later = type.kept.filter(kept => kept.sequence > sequence && kept.sequence <= until);
|
|
372
267
|
const recent = later.filter(kept => kept.at >= floor);
|
|
373
|
-
const truncated = sequence < type.dropped || recent.length < later.length;
|
|
268
|
+
const truncated = cursor != null && !resumed || sequence < type.dropped || recent.length < later.length;
|
|
374
269
|
const events: EventOccurrence[] = [];
|
|
375
270
|
const failures: EventError[] = [];
|
|
376
|
-
let reached = recent.length ? recent[0]!.sequence - 1 :
|
|
271
|
+
let reached = recent.length ? recent[0]!.sequence - 1 : until;
|
|
377
272
|
const start = position(type, reached);
|
|
378
273
|
for (const kept of recent) {
|
|
379
274
|
if (events.length === limit) return { events, failures, start, cursor: position(type, reached), truncated, hasMore: true };
|
|
@@ -381,82 +276,36 @@ export function createEvents({ webhookTtl, webhook }: EventsOptions = {}): Event
|
|
|
381
276
|
const event = shape(type, kept, subscription, error => failures.push(error));
|
|
382
277
|
if (event) events.push(event);
|
|
383
278
|
}
|
|
384
|
-
return { events, failures, start, cursor: position(type), truncated, hasMore: false };
|
|
385
|
-
};
|
|
386
|
-
|
|
387
|
-
// What a subscription is owed after a cursor from the type's check function. The cursor is the author's own. Each
|
|
388
|
-
// event is placed at its own cursor when it has one, so a batch can be cut short; otherwise at the cursor before
|
|
389
|
-
// the batch, which is safe to resume from. After a gap, delivery starts from the position the check reached.
|
|
390
|
-
const checked = async (type: Type, { arguments: args, context }: Pick<Held, "arguments" | "context">, cursor: string | null | undefined, maxAgeMs?: number, limit = Infinity): Promise<Owed> => {
|
|
391
|
-
const result = await type.check!(cursor ?? null, args, context);
|
|
392
|
-
const next = result?.cursor ?? null;
|
|
393
|
-
// A null cursor asks for the current position and nothing before it, except of a source that has no positions:
|
|
394
|
-
// each check of that is asked with null, and answers with what is new.
|
|
395
|
-
if (cursor == null && next !== null) return { events: [], start: next, cursor: next, truncated: false, hasMore: false };
|
|
396
|
-
const items = result?.events ?? [];
|
|
397
|
-
const floor = maxAgeMs === undefined || cursor == null ? -Infinity : Date.now() - maxAgeMs;
|
|
398
|
-
let truncated = next !== null && Boolean(result.truncated);
|
|
399
|
-
const events: EventOccurrence[] = [];
|
|
400
|
-
const failures: EventError[] = [];
|
|
401
|
-
let reached = cursor ?? null;
|
|
402
|
-
let cuttable = false;
|
|
403
|
-
const answer = (end: string | null, hasMore: boolean): Owed => ({ events, failures, start: truncated || !events.length ? end : cursor ?? null, cursor: end, truncated, hasMore });
|
|
404
|
-
for (const [index, item] of items.entries()) {
|
|
405
|
-
if (events.length === limit && cuttable) return answer(reached, true);
|
|
406
|
-
const after = index === items.length - 1 ? next : item.cursor ?? reached;
|
|
407
|
-
const { eventId = crypto.randomUUID(), timestamp = new Date().toISOString(), data, _meta } = item;
|
|
408
|
-
if (Date.parse(timestamp) < floor) truncated = next !== null;
|
|
409
|
-
else if (serialisable(type, eventId, { data, _meta }, error => failures.push(error))) events.push({ eventId, name: type.name, timestamp, data, ...(_meta && { _meta }), cursor: after });
|
|
410
|
-
reached = after;
|
|
411
|
-
cuttable = item.cursor != null;
|
|
412
|
-
}
|
|
413
|
-
return answer(next, Boolean(result.hasMore));
|
|
279
|
+
return { events, failures, start, cursor: position(type, until), truncated, hasMore: false };
|
|
414
280
|
};
|
|
415
281
|
|
|
416
|
-
// What a subscription is owed after a cursor, whichever way its type learns of events.
|
|
417
|
-
const read: Internals["read"] = (type, subscription, cursor, { maxAgeMs, maxEvents } = {}) =>
|
|
418
|
-
type.check ? checked(type as Type, subscription, cursor, maxAgeMs, maxEvents) : since(type, subscription, cursor, maxAgeMs, maxEvents);
|
|
419
|
-
|
|
420
282
|
// The occurrence a subscription receives, or nothing when it is not for it. An event emitted to one subscription
|
|
421
283
|
// is for that one alone, and its arguments are not asked.
|
|
422
284
|
// An event that cannot be shaped for a subscription, because `match` or `transform` throws or what `transform`
|
|
423
285
|
// returns cannot be sent as JSON, is skipped for that subscription alone, so its cursor moves past it. It is
|
|
424
286
|
// logged, and `failed` hears of it so a stream can say so once.
|
|
425
|
-
const shape
|
|
426
|
-
const type = state as Type;
|
|
287
|
+
const shape = (type: Type, { sequence, target, occurrence }: Kept, { id, untargetable, arguments: args, context }: Reader, failed: (error: EventError) => void): EventOccurrence | undefined => {
|
|
427
288
|
let data: unknown;
|
|
428
289
|
try {
|
|
429
290
|
if (target !== undefined ? untargetable || target !== id : type.match && !type.match(occurrence.data, args, context)) return undefined;
|
|
430
291
|
data = type.transform ? type.transform(occurrence.data, args, context) : occurrence.data;
|
|
431
292
|
if (type.transform) JSON.stringify(data);
|
|
432
|
-
} catch (error) {
|
|
433
|
-
|
|
293
|
+
} catch (error: any) {
|
|
294
|
+
const { eventId } = occurrence;
|
|
295
|
+
console.error(`holon-sh/ext/events: event ${eventId} of ${type.name} was skipped for a subscription:`, error);
|
|
296
|
+
failed({ code: INTERNAL_ERROR, message: `Event ${eventId} could not be delivered and was skipped`, data: { eventId, reason: String(error?.message ?? error) } });
|
|
434
297
|
return undefined;
|
|
435
298
|
}
|
|
436
|
-
return { ...occurrence, data,
|
|
437
|
-
};
|
|
438
|
-
const skipped = (type: Type, eventId: string, error: any, failed?: (error: EventError) => void) => {
|
|
439
|
-
console.error(`holon-sh/ext/events: event ${eventId} of ${type.name} was skipped for a subscription:`, error);
|
|
440
|
-
failed?.({ code: INTERNAL_ERROR, message: `Event ${eventId} could not be delivered and was skipped`, data: { eventId, reason: String(error?.message ?? error) } });
|
|
441
|
-
};
|
|
442
|
-
// What a check function found is skipped in the same way when it cannot be sent as JSON.
|
|
443
|
-
const serialisable = (type: Type, eventId: string, value: unknown, failed: (error: EventError) => void) => {
|
|
444
|
-
try {
|
|
445
|
-
JSON.stringify(value);
|
|
446
|
-
return true;
|
|
447
|
-
} catch (error) {
|
|
448
|
-
skipped(type, eventId, error, failed);
|
|
449
|
-
return false;
|
|
450
|
-
}
|
|
299
|
+
return { ...occurrence, data, cursor: position(type, sequence) };
|
|
451
300
|
};
|
|
452
301
|
|
|
453
302
|
// The event type and arguments a request names, checked as the sketch's errors require, in its order: the type,
|
|
454
303
|
// the delivery mode, the request's own fields, the arguments, and then whether the caller may subscribe. A caller
|
|
455
304
|
// refused loses what it holds of that subscription at once.
|
|
456
|
-
const subscription
|
|
305
|
+
const subscription = async ({ name, arguments: args = {}, cursor, maxAgeMs, maxEvents }: Record<string, unknown>, context: SubscriptionContext, delivery: DeliveryModeName): Promise<{ type: Type; subscription: Requested }> => {
|
|
457
306
|
const type = types.get(name as string);
|
|
458
307
|
if (!type) throw refusal(removed);
|
|
459
|
-
if (
|
|
308
|
+
if (!type.delivery.includes(delivery)) throw refusal({ code: UNSUPPORTED, message: "Unsupported", data: { feature: "deliveryMode", value: delivery } });
|
|
460
309
|
if (cursor != null && typeof cursor !== "string") throw new ProtocolError(INVALID_PARAMS, "cursor must be a string or null");
|
|
461
310
|
if (!integer(maxAgeMs, 0)) throw new ProtocolError(INVALID_PARAMS, "maxAgeMs must be a non-negative integer");
|
|
462
311
|
if (!integer(maxEvents, 1)) throw new ProtocolError(INVALID_PARAMS, "maxEvents must be a positive integer");
|
|
@@ -474,16 +323,15 @@ export function createEvents({ webhookTtl, webhook }: EventsOptions = {}): Event
|
|
|
474
323
|
return { type, subscription: { id: who === null ? unique() : lease, lease, arguments: checked.value, context } };
|
|
475
324
|
};
|
|
476
325
|
|
|
477
|
-
const endSubscription = (type:
|
|
326
|
+
const endSubscription = (type: Type, id: string, error: EventError) => {
|
|
478
327
|
for (const held of [...type.subscriptions.get(id)?.held ?? []]) held.end(error);
|
|
479
328
|
};
|
|
480
329
|
|
|
481
|
-
// Begins holding a subscription, by a stream
|
|
482
|
-
//
|
|
483
|
-
//
|
|
484
|
-
//
|
|
485
|
-
const open
|
|
486
|
-
const type = state as Type;
|
|
330
|
+
// Begins holding a subscription, by a stream or a poll lease: counted against the limits first, so a refused one
|
|
331
|
+
// provisions nothing. The type's onSubscribe is called when the first hold of an id begins, and its onUnsubscribe
|
|
332
|
+
// when the last ends, so two streams of one subscription set it up once. `end` is how the holder is told the app
|
|
333
|
+
// terminated it; `close` is how the holder says its hold is over.
|
|
334
|
+
const open = async (type: Type, { id, delivery, arguments: args, context }: { id: string; delivery: DeliveryModeName; arguments: unknown; context: SubscriptionContext }, end: (error: EventError) => void): Promise<Held> => {
|
|
487
335
|
if (types.get(type.name) !== type) throw refusal(removed);
|
|
488
336
|
// Callers without an identity cannot be told apart, so they count together as one.
|
|
489
337
|
const who = caller(context) ?? "anonymous";
|
|
@@ -501,7 +349,7 @@ export function createEvents({ webhookTtl, webhook }: EventsOptions = {}): Event
|
|
|
501
349
|
}
|
|
502
350
|
const current = entry;
|
|
503
351
|
let closed = false;
|
|
504
|
-
const held: Held = { id,
|
|
352
|
+
const held: Held = { id, arguments: args, context, end, close() {
|
|
505
353
|
if (closed) return;
|
|
506
354
|
closed = true;
|
|
507
355
|
subscribed--;
|
|
@@ -512,7 +360,7 @@ export function createEvents({ webhookTtl, webhook }: EventsOptions = {}): Event
|
|
|
512
360
|
if (current.held.size) return;
|
|
513
361
|
if (type.subscriptions.get(id) === current) type.subscriptions.delete(id);
|
|
514
362
|
// Only a subscription that was set up is told it ended.
|
|
515
|
-
current.ready.then(subscription => { if (type.onUnsubscribe) settle(() => type.onUnsubscribe!(subscription
|
|
363
|
+
current.ready.then(subscription => { if (type.onUnsubscribe) settle(() => type.onUnsubscribe!(subscription)); }, () => {});
|
|
516
364
|
} };
|
|
517
365
|
current.held.add(held);
|
|
518
366
|
try {
|
|
@@ -545,13 +393,12 @@ export function createEvents({ webhookTtl, webhook }: EventsOptions = {}): Event
|
|
|
545
393
|
lent = undefined;
|
|
546
394
|
}
|
|
547
395
|
if (!lent) {
|
|
548
|
-
const fresh: Lease = { type }
|
|
549
|
-
lent = fresh;
|
|
550
|
-
leases.set(key, fresh);
|
|
551
|
-
fresh.opening = open(type, { ...subscribed, id: key, delivery: "poll" }, error => {
|
|
396
|
+
const fresh: Lease = { type, opening: open(type, { ...subscribed, id: key, delivery: "poll" }, error => {
|
|
552
397
|
fresh.ended = error;
|
|
553
398
|
fresh.release?.();
|
|
554
|
-
});
|
|
399
|
+
}) };
|
|
400
|
+
lent = fresh;
|
|
401
|
+
leases.set(key, fresh);
|
|
555
402
|
fresh.opening.then(held => {
|
|
556
403
|
if (shared) held.untargetable = true;
|
|
557
404
|
fresh.release = () => held.close();
|
|
@@ -559,7 +406,7 @@ export function createEvents({ webhookTtl, webhook }: EventsOptions = {}): Event
|
|
|
559
406
|
}, () => { if (leases.get(key) === fresh) leases.delete(key); });
|
|
560
407
|
}
|
|
561
408
|
const current = lent;
|
|
562
|
-
const held = await current.opening
|
|
409
|
+
const held = await current.opening;
|
|
563
410
|
// Ended while it was being set up.
|
|
564
411
|
if (current.ended) {
|
|
565
412
|
leases.delete(key);
|
|
@@ -574,28 +421,18 @@ export function createEvents({ webhookTtl, webhook }: EventsOptions = {}): Event
|
|
|
574
421
|
return held;
|
|
575
422
|
};
|
|
576
423
|
|
|
577
|
-
// What a delivery mode works with: `subscription` checks a request, names its event type and the subscription's
|
|
578
|
-
// id, `open` begins holding a subscription, `read` gives a subscription what it is owed after a cursor (`since` and
|
|
579
|
-
// `shape` for emitted events alone), `position` is a type's cursor, a type's `sinks` hear what is emitted, and its
|
|
580
|
-
// `subscriptions` are the ones held by every mode, by id.
|
|
581
|
-
const internals: Internals = { types, subscription, open, read, since, shape, position };
|
|
582
|
-
|
|
583
424
|
return {
|
|
584
|
-
[INTERNALS]: internals,
|
|
585
425
|
/**
|
|
586
426
|
* Declares an event type and returns the function that emits it. `match` decides which subscriptions receive an
|
|
587
|
-
* event from their arguments, and `transform` shapes its data for each.
|
|
588
|
-
* cursor, for event types whose source keeps its own history.
|
|
427
|
+
* event from their arguments, and `transform` shapes its data for each.
|
|
589
428
|
*/
|
|
590
|
-
event<A, D>(name: string, { description, inputSchema, payloadSchema, _meta, match, transform,
|
|
429
|
+
event<A, D>(name: string, { description, inputSchema, payloadSchema, _meta, match, transform, delivery = OFFERED, authorize, onSubscribe, onUnsubscribe }: EventDefinition<A, D>): Emitter<D> {
|
|
591
430
|
if (types.has(name)) throw new Error(`Event ${name} is already declared.`);
|
|
592
|
-
const unknown = delivery.filter(mode => !
|
|
593
|
-
if (unknown.length || !delivery.length) throw new Error(`Event ${name} is delivered by ${delivery.join(", ") || "nothing"}, but only ${
|
|
594
|
-
// Webhook delivery sends what is emitted, and does not ask a check function.
|
|
595
|
-
if (check && delivery.includes("webhook")) throw new Error(`Event ${name} has a check function, which webhook delivery does not read.`);
|
|
431
|
+
const unknown = delivery.filter(mode => !OFFERED.includes(mode));
|
|
432
|
+
if (unknown.length || !delivery.length) throw new Error(`Event ${name} is delivered by ${delivery.join(", ") || "nothing"}, but only ${OFFERED.join(", ")} are served.`);
|
|
596
433
|
const type: Type<A, D> = {
|
|
597
|
-
name, order: ++declared, description, inputSchema, payloadSchema, _meta, match, transform,
|
|
598
|
-
sequence: 0, dropped: 0, kept: [], sinks: new Set(), subscriptions: new Map(),
|
|
434
|
+
name, order: ++declared, description, inputSchema, payloadSchema, _meta, delivery, match, transform, authorize, onSubscribe, onUnsubscribe,
|
|
435
|
+
sequence: 0, dropped: 0, kept: [], sinks: new Set(), subscriptions: new Map(),
|
|
599
436
|
};
|
|
600
437
|
types.set(name, type);
|
|
601
438
|
return {
|
|
@@ -603,21 +440,13 @@ export function createEvents({ webhookTtl, webhook }: EventsOptions = {}): Event
|
|
|
603
440
|
const occurrence = { eventId, name, timestamp, data };
|
|
604
441
|
// Data that cannot be sent as JSON is refused here, before anything is kept or sent.
|
|
605
442
|
JSON.stringify(occurrence);
|
|
606
|
-
// A type with a check function is asked for what polls are owed, so what it emits is pushed and not kept.
|
|
607
|
-
if (type.check) {
|
|
608
|
-
// An event for one subscription that has no stream to take it is lost.
|
|
609
|
-
if (target !== undefined && ![...type.subscriptions.get(target)?.held ?? []].some(held => held.delivery !== "poll")) owe(type, target);
|
|
610
|
-
for (const sink of type.sinks) sink({ target, occurrence });
|
|
611
|
-
return;
|
|
612
|
-
}
|
|
613
443
|
// An event for one subscription is kept like any other, so the subscription has it when it comes back.
|
|
614
444
|
const kept = { sequence: ++type.sequence, at: Date.now(), target, occurrence };
|
|
615
445
|
type.kept.push(kept);
|
|
616
446
|
while (type.kept.length > KEPT_EVENTS || type.kept[0]!.at < kept.at - KEPT_AGE) type.dropped = type.kept.shift()!.sequence;
|
|
617
|
-
for (const sink of type.sinks) sink(
|
|
447
|
+
for (const sink of type.sinks) sink();
|
|
618
448
|
},
|
|
619
449
|
cursor() {
|
|
620
|
-
if (type.check) throw new Error(`Event ${name} has a check function, whose cursor is its source's own.`);
|
|
621
450
|
return position(type);
|
|
622
451
|
},
|
|
623
452
|
terminate(id, error = forbidden) {
|
|
@@ -634,9 +463,8 @@ export function createEvents({ webhookTtl, webhook }: EventsOptions = {}): Event
|
|
|
634
463
|
|
|
635
464
|
/** Serves the declared event types on an MCP server: events/list, events/poll and events/stream. */
|
|
636
465
|
serve(mcp) {
|
|
637
|
-
const underlying: Server = "server" in mcp ? mcp.server : mcp;
|
|
638
466
|
// The custom methods are registered through the parts of the server this module uses.
|
|
639
|
-
const server =
|
|
467
|
+
const server = ("server" in mcp ? mcp.server : mcp) as unknown as Served;
|
|
640
468
|
// The sketch declares capabilities.events. The MCP SDK's client drops top-level capabilities it does not know,
|
|
641
469
|
// so the extension is declared under capabilities.extensions too, where SDK clients read it.
|
|
642
470
|
// Clients are never told the event types changed: over Holon's stateless HTTP, the notification reaches none.
|
|
@@ -664,33 +492,43 @@ export function createEvents({ webhookTtl, webhook }: EventsOptions = {}): Event
|
|
|
664
492
|
...(start + PAGE_SIZE < all.length && { nextCursor: String(page.at(-1)!.order) }),
|
|
665
493
|
};
|
|
666
494
|
});
|
|
667
|
-
for (const mode of modes) mode.serve(underlying, internals);
|
|
668
495
|
|
|
669
496
|
server.setRequestHandler("events/poll", { params }, async (request, ctx) => {
|
|
670
497
|
const { type, subscription: subscribed } = await subscription(request, ctx.http, "poll");
|
|
671
|
-
|
|
498
|
+
// Taken before the lease, which waits for onSubscribe. The sketch's poll from null returns no events and a
|
|
499
|
+
// fresh cursor: this one is where the subscription opened, so the next poll has what onSubscribe emitted.
|
|
500
|
+
const opened = type.sequence;
|
|
501
|
+
const held = await lease(type, subscribed);
|
|
672
502
|
// Each poll is answered for its own caller, with the lease's id.
|
|
673
|
-
const { events, start, failures,
|
|
674
|
-
const lost = !held.untargetable && (type as Type).owed.delete(held.id);
|
|
503
|
+
const { events, start, failures, ...rest } = since(type, { ...subscribed, id: held.id, untargetable: held.untargetable }, request.cursor, { maxAgeMs: request.maxAgeMs, maxEvents: request.maxEvents, opened, ...(request.cursor == null && { until: opened }) });
|
|
675
504
|
// A poll carries its cursor once, for the whole response.
|
|
676
|
-
return { events: events.map(({ cursor, ...event }) => event), ...rest,
|
|
505
|
+
return { events: events.map(({ cursor, ...event }) => event), ...rest, nextPollMs: INTERVAL };
|
|
677
506
|
});
|
|
678
507
|
|
|
679
508
|
server.setRequestHandler("events/stream", { params }, async (request, ctx) => {
|
|
680
|
-
const { type
|
|
681
|
-
const type = state as Type;
|
|
509
|
+
const { type, subscription: subscribed } = await subscription(request, ctx.http, "push");
|
|
682
510
|
const { signal } = ctx.mcpReq;
|
|
683
511
|
const meta = { [SUBSCRIPTION_ID_META_KEY]: ctx.mcpReq.id };
|
|
684
512
|
let done = false;
|
|
685
513
|
let ending: EventError | undefined;
|
|
514
|
+
// Set when the app terminates the stream: the last event emitted before, which the stream still sends.
|
|
515
|
+
let until: number | undefined;
|
|
686
516
|
let wake = () => {};
|
|
687
517
|
const finish = () => {
|
|
688
518
|
done = true;
|
|
689
519
|
wake();
|
|
690
520
|
};
|
|
521
|
+
// What is emitted from here on is the stream's, so it has what onSubscribe emits to it.
|
|
522
|
+
const opened = type.sequence;
|
|
691
523
|
const held = await open(type, { ...subscribed, delivery: "push" }, error => {
|
|
524
|
+
// Lost access ends delivery at once, even while what came before is still being sent. Otherwise only the
|
|
525
|
+
// first end counts, and the stream first sends what was emitted before it.
|
|
526
|
+
const lost = error.code === FORBIDDEN;
|
|
527
|
+
if (ending && !lost) return;
|
|
692
528
|
ending = error;
|
|
693
|
-
finish();
|
|
529
|
+
if (lost) return finish();
|
|
530
|
+
until = type.sequence;
|
|
531
|
+
wake();
|
|
694
532
|
});
|
|
695
533
|
// A client that left while the subscription was being set up is not waited for.
|
|
696
534
|
if (signal.aborted) {
|
|
@@ -715,19 +553,15 @@ export function createEvents({ webhookTtl, webhook }: EventsOptions = {}): Event
|
|
|
715
553
|
}
|
|
716
554
|
if ((sent += JSON.stringify(params).length) > STREAM_BUDGET) finish();
|
|
717
555
|
};
|
|
718
|
-
// The stream follows its cursor
|
|
719
|
-
//
|
|
720
|
-
//
|
|
556
|
+
// The stream follows its cursor, and emitted events wake it. What it is owed waits in the buffer, not here, so
|
|
557
|
+
// a slow client is not owed more memory than one batch; when what it is owed is no longer kept, it is told of
|
|
558
|
+
// the gap.
|
|
721
559
|
let cursor: string | null = request.cursor ?? null;
|
|
722
560
|
let behind = true;
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
if (type.check) {
|
|
728
|
-
pushed.push(kept);
|
|
729
|
-
if (pushed.length > KEPT_EVENTS) pushed.shift();
|
|
730
|
-
} else behind = true;
|
|
561
|
+
const sink = () => {
|
|
562
|
+
// Nothing emitted after the app terminated the stream is for it.
|
|
563
|
+
if (until !== undefined) return;
|
|
564
|
+
behind = true;
|
|
731
565
|
wake();
|
|
732
566
|
};
|
|
733
567
|
type.sinks.add(sink);
|
|
@@ -748,58 +582,29 @@ export function createEvents({ webhookTtl, webhook }: EventsOptions = {}): Event
|
|
|
748
582
|
}, HEARTBEAT);
|
|
749
583
|
let first = true;
|
|
750
584
|
try {
|
|
751
|
-
let due = 0;
|
|
752
585
|
while (!done) {
|
|
753
|
-
if (
|
|
586
|
+
if (behind) {
|
|
754
587
|
behind = false;
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
}
|
|
763
|
-
due = Date.now() + type.interval;
|
|
764
|
-
if (result) {
|
|
765
|
-
// A check function's gap is told after the events that follow it, at the position after them, since
|
|
766
|
-
// the position before them is not known and the client's cursor must not go back. The stream is
|
|
767
|
-
// confirmed before anything else is said on it.
|
|
768
|
-
const after = type.check && result.truncated && !first;
|
|
769
|
-
const truncated = result.truncated || first && type.owed.delete(held.id);
|
|
770
|
-
if (first || truncated && !after) await notify("active", { cursor: type.check && result.truncated ? cursor : result.start, truncated });
|
|
771
|
-
if (first) beat = heartbeats();
|
|
772
|
-
first = false;
|
|
773
|
-
for (const error of result.failures ?? []) await notify("error", { error });
|
|
774
|
-
for (const event of result.events) {
|
|
775
|
-
if (done) break;
|
|
776
|
-
await notify("event", event);
|
|
777
|
-
cursor = event.cursor ?? null;
|
|
778
|
-
}
|
|
588
|
+
const result = since(type, held, cursor, { ...(first && request.maxAgeMs !== undefined && { maxAgeMs: request.maxAgeMs }), opened, until });
|
|
589
|
+
// The stream is confirmed before anything else is said on it.
|
|
590
|
+
if (first || result.truncated) await notify("active", { cursor: result.start, truncated: result.truncated });
|
|
591
|
+
if (first) beat = heartbeats();
|
|
592
|
+
first = false;
|
|
593
|
+
for (const error of result.failures) await notify("error", { error });
|
|
594
|
+
for (const event of result.events) {
|
|
779
595
|
if (done) break;
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
if (result.hasMore) {
|
|
783
|
-
due = 0;
|
|
784
|
-
behind = true;
|
|
785
|
-
}
|
|
596
|
+
await notify("event", event);
|
|
597
|
+
cursor = event.cursor ?? null;
|
|
786
598
|
}
|
|
599
|
+
if (done) break;
|
|
600
|
+
cursor = result.cursor;
|
|
787
601
|
}
|
|
788
|
-
//
|
|
789
|
-
|
|
790
|
-
let failure: EventError | undefined;
|
|
791
|
-
const event = shape(type, pushed.shift()!, held, error => { failure = error; });
|
|
792
|
-
if (failure) await notify("error", { error: failure });
|
|
793
|
-
if (event) await notify("event", { ...event, cursor });
|
|
794
|
-
}
|
|
795
|
-
if (done) break;
|
|
796
|
-
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
602
|
+
// Terminated by the app, the stream ends once it has sent what was emitted before.
|
|
603
|
+
if (done || until !== undefined && !behind) break;
|
|
797
604
|
await new Promise<void>(resolve => {
|
|
798
605
|
wake = resolve;
|
|
799
|
-
if (done ||
|
|
800
|
-
else if (type.check) timer = setTimeout(resolve, Math.max(0, due - Date.now()));
|
|
606
|
+
if (done || behind) resolve();
|
|
801
607
|
});
|
|
802
|
-
clearTimeout(timer);
|
|
803
608
|
wake = () => {};
|
|
804
609
|
}
|
|
805
610
|
if (ending) {
|
|
@@ -809,8 +614,6 @@ export function createEvents({ webhookTtl, webhook }: EventsOptions = {}): Event
|
|
|
809
614
|
} finally {
|
|
810
615
|
clearInterval(beat);
|
|
811
616
|
type.sinks.delete(sink);
|
|
812
|
-
// What was emitted to this subscription alone and not sent is owed to it as a gap when it comes back.
|
|
813
|
-
if (held.id === subscribed.lease && pushed.some(kept => kept.target === held.id)) owe(type, held.id);
|
|
814
617
|
held.close();
|
|
815
618
|
}
|
|
816
619
|
// The final frame, sent when the server ends the stream; when the client did, nobody is listening.
|