@isoloop/protocol 0.0.0-next.62ef495 → 0.0.0-next.d028bc1

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
@@ -160,6 +160,8 @@ export type InitPayload = {
160
160
  * subscriptions start from `committed` instead, so it replays what it missed.
161
161
  */
162
162
  wake?: boolean;
163
+ /** `KernelConfig.bus.commitIntervalMs` (ADR 0016, decision 1). Default 100. */
164
+ commitIntervalMs?: number;
163
165
  };
164
166
  /** What a beat says about whether the isolate could be suspended (ADR 0001). */
165
167
  export type IdleReport = {
@@ -247,9 +249,11 @@ export type IsolateToBus = Envelope<"publish", {
247
249
  group?: string;
248
250
  }> | Envelope<"unsubscribe", {
249
251
  topic: string;
250
- }> | Envelope<"commit", {
251
- topic: string;
252
- offset: number;
252
+ }> | Envelope<"commits", {
253
+ offsets: {
254
+ topic: string;
255
+ offset: number;
256
+ }[];
253
257
  }> | Envelope<"request", {
254
258
  topic: string;
255
259
  key?: string;
@@ -276,6 +280,44 @@ export declare const ISOLATE_TO_BUS: MessageTypes<IsolateToBus>;
276
280
  export declare const BUS_TO_ISOLATE: MessageTypes<BusToIsolate>;
277
281
  export declare const isIsolateToBus: (x: unknown) => x is IsolateToBus;
278
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;
279
321
  /**
280
322
  * A rejected bus call (`kernel.publish`, `ctx.bus.publish`, `request`,
281
323
  * `reply`): the broker's error code, and the schema issues for `invalid`.
package/dist/index.js CHANGED
@@ -108,7 +108,7 @@ const ISOLATE_TO_BUS = {
108
108
  publish: true,
109
109
  subscribe: true,
110
110
  unsubscribe: true,
111
- commit: true,
111
+ commits: true,
112
112
  request: true,
113
113
  reply: true
114
114
  };
@@ -119,6 +119,81 @@ const BUS_TO_ISOLATE = {
119
119
  };
120
120
  const isIsolateToBus = envelopeGuard(ISOLATE_TO_BUS);
121
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
+ }
122
197
  // src/bus-error.ts
123
198
  /**
124
199
  * A rejected bus call (`kernel.publish`, `ctx.bus.publish`, `request`,
@@ -253,4 +328,4 @@ function compareVersions(a, b) {
253
328
  const [bm, bn] = parse(b);
254
329
  return am - bm || an - bn;
255
330
  }
256
- 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.62ef495",
3
+ "version": "0.0.0-next.d028bc1",
4
4
  "description": "Wire protocol types and guards shared by every Isoloop package.",
5
5
  "license": "MIT",
6
6
  "type": "module",