@sjawhar/pi-envoy 16.2.0 → 16.2.1

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/dispatch.js CHANGED
@@ -22847,7 +22847,7 @@ function createEnvoyClient(config) {
22847
22847
  session_id: input.sessionID,
22848
22848
  subjects: input.subjects.map((cursor) => cursor.after === undefined ? { subject: cursor.subject } : { subject: cursor.subject, after: cursor.after }),
22849
22849
  ...input.maxMessages === undefined ? {} : { max_messages: input.maxMessages }
22850
- }, { timeoutMs: REPLAY_TIMEOUT_MS, retry: false })))
22850
+ }, { timeoutMs: input.timeoutMs ?? REPLAY_TIMEOUT_MS, retry: false })))
22851
22851
  };
22852
22852
  }
22853
22853
  function waitForRetry() {
package/dist/envoy.js CHANGED
@@ -34993,7 +34993,7 @@ function createEnvoyClient(config) {
34993
34993
  session_id: input.sessionID,
34994
34994
  subjects: input.subjects.map((cursor) => cursor.after === undefined ? { subject: cursor.subject } : { subject: cursor.subject, after: cursor.after }),
34995
34995
  ...input.maxMessages === undefined ? {} : { max_messages: input.maxMessages }
34996
- }, { timeoutMs: REPLAY_TIMEOUT_MS, retry: false })))
34996
+ }, { timeoutMs: input.timeoutMs ?? REPLAY_TIMEOUT_MS, retry: false })))
34997
34997
  };
34998
34998
  }
34999
34999
  function waitForRetry() {
@@ -35021,6 +35021,7 @@ var REPLAY_PAGES_PER_SWEEP = 20;
35021
35021
  var REPLAY_FAILURES_BEFORE_NOTICE = 3;
35022
35022
  var REPLAY_STATE_REFRESH_MS = 60 * 60 * 1000;
35023
35023
  var REPLAY_MAX_SUBJECTS = 32;
35024
+ var START_CURSORS_TIMEOUT_MS = 2000;
35024
35025
  var ROLE_FORWARD_KEY_PREFIX = "envoy.role.forward.";
35025
35026
  var ReplayLedgerEntrySchema = object({
35026
35027
  event_id: string2().min(1).optional(),
@@ -35029,7 +35030,8 @@ var ReplayLedgerEntrySchema = object({
35029
35030
  });
35030
35031
  var ReplayCursorSchema = object({
35031
35032
  since: number2().int().nonnegative().optional(),
35032
- after: number2().int().nonnegative().optional()
35033
+ after: number2().int().nonnegative().optional(),
35034
+ from_start: number2().int().nonnegative().optional()
35033
35035
  });
35034
35036
  var ReplayPendingNoticeSchema = _enum2([
35035
35037
  "retention_gap",
@@ -35109,7 +35111,8 @@ function createReplayEngine(options) {
35109
35111
  const cursor = cursors.get(subject) ?? {};
35110
35112
  subjects[subject] = {
35111
35113
  ...cursor.since === undefined ? {} : { since: cursor.since },
35112
- ...!afters || cursor.after === undefined ? {} : { after: cursor.after }
35114
+ ...!afters || cursor.after === undefined ? {} : { after: cursor.after },
35115
+ ...cursor.from_start === undefined ? {} : { from_start: cursor.from_start }
35113
35116
  };
35114
35117
  }
35115
35118
  return JSON.stringify({
@@ -35315,7 +35318,10 @@ function createReplayEngine(options) {
35315
35318
  syncFollowed();
35316
35319
  const readings = new Map;
35317
35320
  for (const [subject, cursor] of cursors) {
35318
- readings.set(subject, { read: cursor.after, more: true });
35321
+ readings.set(subject, {
35322
+ read: cursor.from_start === undefined ? cursor.after : 0,
35323
+ more: true
35324
+ });
35319
35325
  }
35320
35326
  const buffer = new Map;
35321
35327
  const holes = new Map;
@@ -35406,6 +35412,18 @@ function createReplayEngine(options) {
35406
35412
  }
35407
35413
  for (const [, reading] of asking)
35408
35414
  reading.more = false;
35415
+ for (const result of response.subjects) {
35416
+ const reading = readings.get(result.subject);
35417
+ const markedAt = cursors.get(result.subject)?.from_start;
35418
+ if (reading === undefined || markedAt === undefined || result.refused !== undefined) {
35419
+ continue;
35420
+ }
35421
+ const start = Math.max(0, stream.first_seq - 1);
35422
+ cursors.set(result.subject, { since: start, after: start });
35423
+ cursorStarts.set(result.subject, now());
35424
+ if (now() - markedAt < DELIVERY_DUPLICATE_WINDOW_MS)
35425
+ reading.read = start;
35426
+ }
35409
35427
  let gapped = false;
35410
35428
  for (const result of response.subjects) {
35411
35429
  const reading = readings.get(result.subject);
@@ -35521,8 +35539,65 @@ function createReplayEngine(options) {
35521
35539
  });
35522
35540
  return rerun;
35523
35541
  };
35542
+ const started = (cursor) => cursor?.after !== undefined || cursor?.from_start !== undefined;
35543
+ const startOnce = async () => {
35544
+ const binding = bind();
35545
+ if (binding === undefined)
35546
+ return;
35547
+ const { sessionID, alive } = binding;
35548
+ const unstarted = [...new Set(host.followedSubjects())].filter((subject) => !started(cursors.get(subject)));
35549
+ let changed = false;
35550
+ for (let index = 0;index < unstarted.length; index += REPLAY_MAX_SUBJECTS) {
35551
+ let response;
35552
+ try {
35553
+ response = await transport.replay({
35554
+ sessionID,
35555
+ subjects: unstarted.slice(index, index + REPLAY_MAX_SUBJECTS).map((subject) => ({ subject })),
35556
+ timeoutMs: START_CURSORS_TIMEOUT_MS
35557
+ });
35558
+ } catch (error) {
35559
+ if (!alive())
35560
+ return;
35561
+ const own = `${AGENT_TOPIC_PREFIX}${sessionID}`;
35562
+ const marked = unstarted.includes(own) && !started(cursors.get(own));
35563
+ if (marked) {
35564
+ cursors.set(own, { from_start: now() });
35565
+ changed = true;
35566
+ }
35567
+ log(marked ? `replay: could not read the stream's position for ${own} before registering while the session's NATS connection is down; its first sweep once the connection is back reads it from the stream's oldest message, so what is stored for it meanwhile is shown` : "replay: could not read the stream's position before registering while the session's NATS connection is down; the first sweep starts the cursors instead", error);
35568
+ break;
35569
+ }
35570
+ if (!alive())
35571
+ return;
35572
+ const { stream } = response;
35573
+ if (streamReplaced(stream))
35574
+ break;
35575
+ streamID = stream.id ?? streamID;
35576
+ streamCreated = stream.created;
35577
+ const at = now();
35578
+ for (const result of response.subjects) {
35579
+ if (result.refused !== undefined || started(cursors.get(result.subject)))
35580
+ continue;
35581
+ cursors.set(result.subject, { since: result.next_after, after: result.next_after });
35582
+ cursorStarts.set(result.subject, at);
35583
+ changed = true;
35584
+ }
35585
+ }
35586
+ if (changed)
35587
+ await flush(alive, true);
35588
+ };
35589
+ const startCursors = async () => {
35590
+ while (running !== undefined)
35591
+ await running;
35592
+ running = startOnce().catch((error) => log("replay: starting the cursors failed", error)).finally(() => {
35593
+ running = undefined;
35594
+ });
35595
+ await running;
35596
+ };
35524
35597
  return {
35525
35598
  sweep,
35599
+ startCursors,
35600
+ startPending: () => [...cursors.values()].some((cursor) => cursor.from_start !== undefined),
35526
35601
  setConsumerMode(on) {
35527
35602
  consumerMode = on;
35528
35603
  },
@@ -38437,7 +38512,7 @@ var import_nats2 = __toESM(require_mod4(), 1);
38437
38512
 
38438
38513
  // package.json
38439
38514
  var name = "@sjawhar/pi-envoy";
38440
- var version2 = "16.2.0";
38515
+ var version2 = "16.2.1";
38441
38516
  var package_default = {
38442
38517
  name,
38443
38518
  version: version2,
@@ -45455,7 +45530,7 @@ ${REPLAYED_BTW_NOTE}`, frame.envelope, "inert");
45455
45530
  };
45456
45531
  const sendRegistration = (send) => {
45457
45532
  const registeringFor = sessionID;
45458
- const asking = relay?.sessionID === registeringFor && relay.subscription !== undefined ? relay : undefined;
45533
+ const asking = relay?.sessionID === registeringFor && relay.subscription !== undefined && replay?.startPending() !== true ? relay : undefined;
45459
45534
  const input = registrationInput(asking);
45460
45535
  return send(input).then((result) => {
45461
45536
  takeDeliveryAnswer(registeringFor, input.topics, asking, result);
@@ -45662,6 +45737,8 @@ ${REPLAYED_BTW_NOTE}`, frame.envelope, "inert");
45662
45737
  }
45663
45738
  }
45664
45739
  handle.check();
45740
+ if (inboundFailure !== undefined && replay !== undefined)
45741
+ await replay.startCursors();
45665
45742
  const registration = await sendRegistration(handle.register).then(() => {
45666
45743
  return;
45667
45744
  }, (error) => ({ error }));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sjawhar/pi-envoy",
3
- "version": "16.2.0",
3
+ "version": "16.2.1",
4
4
  "license": "Apache-2.0",
5
5
  "type": "module",
6
6
  "omp": {