@isoloop/protocol 0.0.0-next.69da74b → 0.0.0-next.b8ffdcf

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -13,13 +13,20 @@ export type Envelope<T extends string, P> = {
13
13
  id?: string;
14
14
  /** Id of the message this replies to. */
15
15
  re?: string;
16
+ /**
17
+ * The pool member a message is for (host → worker) or from (worker → host),
18
+ * on a pooled worker's isolate port (M2c, ADR 0001). Absent on an own
19
+ * worker's port, and on messages about the whole worker (`beat`).
20
+ */
21
+ isolate?: string;
16
22
  p: P;
17
23
  };
18
24
  /** Any envelope, before its type is known. */
19
25
  export type AnyEnvelope = Envelope<string, unknown>;
20
26
  /**
21
27
  * True for a well-formed envelope this build can read: `v` is a supported
22
- * version, `t` is a string, `id`/`re` are absent or strings, and `p` is present.
28
+ * version, `t` is a string, `id`/`re`/`isolate` are absent or strings, and `p`
29
+ * is present.
23
30
  * Says nothing about whether `t` is known; see the per-port guards.
24
31
  */
25
32
  export declare function isEnvelope(x: unknown): x is AnyEnvelope;
@@ -92,8 +99,12 @@ export type Mods = {
92
99
  meta: boolean;
93
100
  shift: boolean;
94
101
  };
95
- /** Event types the host forwards (design §4.6), plus `resize` for transferred canvases (§4.7). */
96
- export type EventType = "click" | "dblclick" | "input" | "change" | "keydown" | "keyup" | "submit" | "focusin" | "focusout" | "pointerdown" | "pointerup" | "pointermove" | "resize";
102
+ /**
103
+ * Event types the host forwards (design §4.6), plus `resize` for transferred
104
+ * canvases (§4.7). `pointermove` and `scroll` are opt-in per node and come at
105
+ * most once a frame.
106
+ */
107
+ export type EventType = "click" | "dblclick" | "input" | "change" | "keydown" | "keyup" | "submit" | "focusin" | "focusout" | "pointerdown" | "pointerup" | "pointermove" | "scroll" | "resize";
97
108
  /** A DOM event as it reaches an isolate (design §4.6). Node ids are the isolate's own. */
98
109
  export type SerializedEvent = {
99
110
  type: EventType;
@@ -111,6 +122,9 @@ export type SerializedEvent = {
111
122
  /** For `resize`. */
112
123
  width?: number;
113
124
  height?: number;
125
+ /** For `scroll`: the scrolled element's offsets (M3b). */
126
+ scrollTop?: number;
127
+ scrollLeft?: number;
114
128
  };
115
129
  /** Why the host asks an isolate to shut down. `suspend` keeps its offsets (M2, ADR 0001). */
116
130
  export type ShutdownReason = "stop" | "restart" | "suspend";
@@ -146,6 +160,8 @@ export type InitPayload = {
146
160
  * subscriptions start from `committed` instead, so it replays what it missed.
147
161
  */
148
162
  wake?: boolean;
163
+ /** `KernelConfig.bus.commitIntervalMs` (ADR 0016, decision 1). Default 100. */
164
+ commitIntervalMs?: number;
149
165
  };
150
166
  /** What a beat says about whether the isolate could be suspended (ADR 0001). */
151
167
  export type IdleReport = {
@@ -185,6 +201,7 @@ export type IsolateToHost = Envelope<"ready", {
185
201
  lagMs: number;
186
202
  heapBytes?: number;
187
203
  idle?: IdleReport;
204
+ members?: Record<string, IdleReport>;
188
205
  }> | Envelope<"error", ErrorPayload> | Envelope<"bye", Record<string, never>>;
189
206
  export declare const HOST_TO_ISOLATE: MessageTypes<HostToIsolate>;
190
207
  export declare const ISOLATE_TO_HOST: MessageTypes<IsolateToHost>;
@@ -232,9 +249,11 @@ export type IsolateToBus = Envelope<"publish", {
232
249
  group?: string;
233
250
  }> | Envelope<"unsubscribe", {
234
251
  topic: string;
235
- }> | Envelope<"commit", {
236
- topic: string;
237
- offset: number;
252
+ }> | Envelope<"commits", {
253
+ offsets: {
254
+ topic: string;
255
+ offset: number;
256
+ }[];
238
257
  }> | Envelope<"request", {
239
258
  topic: string;
240
259
  key?: string;
@@ -261,6 +280,44 @@ export declare const ISOLATE_TO_BUS: MessageTypes<IsolateToBus>;
261
280
  export declare const BUS_TO_ISOLATE: MessageTypes<BusToIsolate>;
262
281
  export declare const isIsolateToBus: (x: unknown) => x is IsolateToBus;
263
282
  export declare const isBusToIsolate: (x: unknown) => x is BusToIsolate;
283
+ /** `KernelConfig.bus.commitIntervalMs` when unset (ADR 0016, decision 1, point 7). */
284
+ export declare const DEFAULT_COMMIT_INTERVAL_MS = 100;
285
+ /** The highest allowed interval: keeps the redelivery window after a crash under a second. */
286
+ export declare const MAX_COMMIT_INTERVAL_MS = 1e3;
287
+ /** A subscription's credit at the broker: records delivered and not yet committed. */
288
+ export declare const DEFAULT_CREDIT = 1e3;
289
+ /** Why an interval is invalid, or `undefined` when it's an integer from 0 to 1,000. */
290
+ export declare function commitIntervalProblem(ms: unknown): string | undefined;
291
+ export type CommitOffsets = {
292
+ topic: string;
293
+ offset: number;
294
+ }[];
295
+ export type CommitterOptions = {
296
+ /** Sends one `commits` message. Called with at least one entry. */
297
+ send: (offsets: CommitOffsets) => void;
298
+ /** 0: at the end of each task that settled a record. Default 100. */
299
+ intervalMs?: number;
300
+ /** The broker's credit per subscription; a subscription sends early at half of it. Default 1,000. */
301
+ credit?: number;
302
+ };
303
+ export type Committer = {
304
+ /** `records` records of `topic`, up to `offset`, are settled. Default one. */
305
+ settle(topic: string, offset: number, records?: number): void;
306
+ /** Sends what's pending now: an unsubscribe, and a new port after a bus restart. */
307
+ flush(): void;
308
+ /**
309
+ * Returns what's pending and clears it, without sending: for a sender that
310
+ * must wait for the broker's answer (a shutdown, before `bye`).
311
+ */
312
+ take(): CommitOffsets | undefined;
313
+ /** Forgets a topic's pending offset (an unsubscribe or a lost memory topic). */
314
+ drop(topic: string): void;
315
+ /** Settled offsets not yet sent, by topic. For tests and reports. */
316
+ pending(): ReadonlyMap<string, number>;
317
+ /** Stops the timer without sending. */
318
+ dispose(): void;
319
+ };
320
+ export declare function createCommitter(o: CommitterOptions): Committer;
264
321
  /**
265
322
  * A rejected bus call (`kernel.publish`, `ctx.bus.publish`, `request`,
266
323
  * `reply`): the broker's error code, and the schema issues for `invalid`.
package/dist/index.js CHANGED
@@ -8,11 +8,12 @@ const isRecord = (x) => typeof x === "object" && x !== null && !Array.isArray(x)
8
8
  const isOptionalString = (x) => x === void 0 || typeof x === "string";
9
9
  /**
10
10
  * True for a well-formed envelope this build can read: `v` is a supported
11
- * version, `t` is a string, `id`/`re` are absent or strings, and `p` is present.
11
+ * version, `t` is a string, `id`/`re`/`isolate` are absent or strings, and `p`
12
+ * is present.
12
13
  * Says nothing about whether `t` is known; see the per-port guards.
13
14
  */
14
15
  function isEnvelope(x) {
15
- return isRecord(x) && x["v"] === PROTOCOL.v && typeof x["t"] === "string" && "p" in x && isOptionalString(x["id"]) && isOptionalString(x["re"]);
16
+ return isRecord(x) && x["v"] === PROTOCOL.v && typeof x["t"] === "string" && "p" in x && isOptionalString(x["id"]) && isOptionalString(x["re"]) && isOptionalString(x["isolate"]);
16
17
  }
17
18
  /**
18
19
  * Builds a guard for one port's union from its message types. Each side drops
@@ -107,7 +108,7 @@ const ISOLATE_TO_BUS = {
107
108
  publish: true,
108
109
  subscribe: true,
109
110
  unsubscribe: true,
110
- commit: true,
111
+ commits: true,
111
112
  request: true,
112
113
  reply: true
113
114
  };
@@ -118,6 +119,81 @@ const BUS_TO_ISOLATE = {
118
119
  };
119
120
  const isIsolateToBus = envelopeGuard(ISOLATE_TO_BUS);
120
121
  const isBusToIsolate = envelopeGuard(BUS_TO_ISOLATE);
122
+ // src/commits.ts
123
+ /** `KernelConfig.bus.commitIntervalMs` when unset (ADR 0016, decision 1, point 7). */
124
+ const DEFAULT_COMMIT_INTERVAL_MS = 100;
125
+ /** The highest allowed interval: keeps the redelivery window after a crash under a second. */
126
+ const MAX_COMMIT_INTERVAL_MS = 1e3;
127
+ /** A subscription's credit at the broker: records delivered and not yet committed. */
128
+ const DEFAULT_CREDIT = 1e3;
129
+ /** Why an interval is invalid, or `undefined` when it's an integer from 0 to 1,000. */
130
+ function commitIntervalProblem(ms) {
131
+ if (typeof ms === "number" && Number.isInteger(ms) && ms >= 0 && ms <= 1e3) return void 0;
132
+ return `bus.commitIntervalMs must be an integer from 0 to ${MAX_COMMIT_INTERVAL_MS}, got ${String(ms)}`;
133
+ }
134
+ function createCommitter(o) {
135
+ const intervalMs = o.intervalMs ?? 100;
136
+ const half = Math.max(1, Math.floor((o.credit ?? 1e3) / 2));
137
+ /** Highest settled offset per topic, not yet sent. */
138
+ const offsets = /* @__PURE__ */ new Map();
139
+ /** Records settled per topic since its last send: the early send at half the credit. */
140
+ const counts = /* @__PURE__ */ new Map();
141
+ let timer;
142
+ let queued = false;
143
+ const take = () => {
144
+ if (timer !== void 0) {
145
+ clearTimeout(timer);
146
+ timer = void 0;
147
+ }
148
+ queued = false;
149
+ if (offsets.size === 0) return void 0;
150
+ const out = [];
151
+ for (const [topic, offset] of offsets) out.push({
152
+ topic,
153
+ offset
154
+ });
155
+ offsets.clear();
156
+ counts.clear();
157
+ return out;
158
+ };
159
+ const flush = () => {
160
+ const out = take();
161
+ if (out) o.send(out);
162
+ };
163
+ const schedule = () => {
164
+ if (intervalMs === 0) {
165
+ if (queued) return;
166
+ queued = true;
167
+ queueMicrotask(() => {
168
+ if (queued) flush();
169
+ });
170
+ return;
171
+ }
172
+ timer ??= setTimeout(flush, intervalMs);
173
+ };
174
+ return {
175
+ settle(topic, offset, records = 1) {
176
+ const prev = offsets.get(topic);
177
+ if (prev === void 0 || offset > prev) offsets.set(topic, offset);
178
+ const n = (counts.get(topic) ?? 0) + records;
179
+ counts.set(topic, n);
180
+ if (n >= half) flush();
181
+ else schedule();
182
+ },
183
+ flush,
184
+ take,
185
+ drop(topic) {
186
+ offsets.delete(topic);
187
+ counts.delete(topic);
188
+ },
189
+ pending: () => offsets,
190
+ dispose() {
191
+ if (timer !== void 0) clearTimeout(timer);
192
+ timer = void 0;
193
+ queued = false;
194
+ }
195
+ };
196
+ }
121
197
  // src/bus-error.ts
122
198
  /**
123
199
  * A rejected bus call (`kernel.publish`, `ctx.bus.publish`, `request`,
@@ -252,4 +328,4 @@ function compareVersions(a, b) {
252
328
  const [bm, bn] = parse(b);
253
329
  return am - bm || an - bn;
254
330
  }
255
- export { BUS_TO_HOST, BUS_TO_ISOLATE, BusError, ERROR_CODES, HOST_TO_BUS, HOST_TO_ISOLATE, ISOLATE_TO_BUS, ISOLATE_TO_HOST, PROTOCOL, ROUTE_KEY, SYSTEM_TOPICS, SYSTEM_TOPIC_PREFIX, assertNever, checks, compareVersions, envelopeGuard, isBusToHost, isBusToIsolate, isEnvelope, isErrorCode, isHostToBus, isHostToIsolate, isIsolateToBus, isIsolateToHost, isSystemTopic, majorMinor, matchAny, matchTopic, routeTopic, systemSchema, systemTopic, topicConfigOf };
331
+ export { BUS_TO_HOST, BUS_TO_ISOLATE, BusError, DEFAULT_COMMIT_INTERVAL_MS, DEFAULT_CREDIT, ERROR_CODES, HOST_TO_BUS, HOST_TO_ISOLATE, ISOLATE_TO_BUS, ISOLATE_TO_HOST, MAX_COMMIT_INTERVAL_MS, PROTOCOL, ROUTE_KEY, SYSTEM_TOPICS, SYSTEM_TOPIC_PREFIX, assertNever, checks, commitIntervalProblem, compareVersions, createCommitter, envelopeGuard, isBusToHost, isBusToIsolate, isEnvelope, isErrorCode, isHostToBus, isHostToIsolate, isIsolateToBus, isIsolateToHost, isSystemTopic, majorMinor, matchAny, matchTopic, routeTopic, systemSchema, systemTopic, topicConfigOf };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@isoloop/protocol",
3
- "version": "0.0.0-next.69da74b",
3
+ "version": "0.0.0-next.b8ffdcf",
4
4
  "description": "Wire protocol types and guards shared by every Isoloop package.",
5
5
  "license": "MIT",
6
6
  "type": "module",