ftown-bridge 0.30.77 → 0.30.78

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.
@@ -1,3 +1,4 @@
1
+ import { isDashboardEventChannel } from './security/ledger/channels.js';
1
2
  import { PREVIEW_MAX_INPUTS } from './session-preview-source.js';
2
3
  import { applyRemoteChildPlan, CHILD_PLAN_PATH } from './remote-child-plan.js';
3
4
  import { runProjectBoardCommand } from './project-board-commands.js';
@@ -370,6 +371,55 @@ export class LocalApiServer extends EventEmitter {
370
371
  for (const wake of this.localEventWaiters)
371
372
  wake();
372
373
  }
374
+ /** Bounded replay for a local long poll or an authenticated dashboard event request. */
375
+ async readEventPage(after, waitMs, userId) {
376
+ if (after === this.localEventSequence)
377
+ await new Promise(resolve => {
378
+ const wake = () => { clearTimeout(timer); this.localEventWaiters.delete(wake); resolve(); };
379
+ const timer = setTimeout(wake, waitMs);
380
+ this.localEventWaiters.add(wake);
381
+ });
382
+ let reset = after > this.localEventSequence || after < (this.localEvents[0]?.cursor ?? this.localEventSequence + 1) - 1;
383
+ const events = [];
384
+ let bytes = 0;
385
+ let cursor = reset ? 0 : after;
386
+ // Leave room for the encrypted owner reply nested inside a gateway reply.
387
+ const limit = userId ? 112_000 : 250_000;
388
+ for (const event of this.localEvents) {
389
+ if (event.cursor <= cursor)
390
+ continue;
391
+ if (userId && !isDashboardEventChannel(event.channel, userId)) {
392
+ cursor = event.cursor;
393
+ continue;
394
+ }
395
+ const size = Buffer.byteLength(JSON.stringify(event));
396
+ if (size > limit) {
397
+ cursor = event.cursor;
398
+ reset = true;
399
+ continue;
400
+ }
401
+ if (bytes + size > limit)
402
+ break;
403
+ events.push(event);
404
+ bytes += size;
405
+ cursor = event.cursor;
406
+ }
407
+ return { events, cursor: this.localEvents.length ? cursor : this.localEventSequence, reset };
408
+ }
409
+ dashboardEventPosition() {
410
+ if (!this.security)
411
+ throw new Error('Security is starting');
412
+ return { cursor: this.localEventSequence, epoch: this.security.receiverEpoch };
413
+ }
414
+ async dashboardEvents(after, epoch) {
415
+ const security = this.security;
416
+ if (!security)
417
+ throw new Error('Security is starting');
418
+ if (epoch !== security.receiverEpoch)
419
+ return { events: [], cursor: this.localEventSequence, reset: true, epoch: security.receiverEpoch };
420
+ const page = await this.readEventPage(after, 4000, security.options.relayUserId);
421
+ return applicationJson({ ...page, epoch });
422
+ }
373
423
  async dispatchSecure(message, clientGone) {
374
424
  return sessionRequestPrincipal.run(message.principal, () => this.dispatchAuthorized(message, clientGone));
375
425
  }
@@ -564,32 +614,9 @@ export class LocalApiServer extends EventEmitter {
564
614
  if (!/^\d{1,16}$/.test(raw) || !Number.isSafeInteger(Number(raw)))
565
615
  throw new Error('Invalid event cursor');
566
616
  const after = Number(raw);
567
- if (after === this.localEventSequence)
568
- await new Promise(resolve => {
569
- const wake = () => { clearTimeout(timer); this.localEventWaiters.delete(wake); resolve(); };
570
- const timer = setTimeout(wake, 20000);
571
- this.localEventWaiters.add(wake);
572
- });
617
+ const page = await this.readEventPage(after, 20000);
573
618
  await this.security.revalidate(message.principal);
574
- let reset = after > this.localEventSequence || after < (this.localEvents[0]?.cursor ?? this.localEventSequence + 1) - 1;
575
- const events = this.localEvents.filter(event => reset || event.cursor > after);
576
- const page = [];
577
- let bytes = 0;
578
- let cursor = reset ? 0 : after;
579
- for (const event of events) {
580
- const size = Buffer.byteLength(JSON.stringify(event));
581
- if (size > 250000) {
582
- cursor = event.cursor;
583
- reset = true;
584
- continue;
585
- }
586
- if (bytes + size > 250000)
587
- break;
588
- page.push(event);
589
- bytes += size;
590
- cursor = event.cursor;
591
- }
592
- return applicationJson({ events: page, cursor: events.length ? cursor : this.localEventSequence, reset });
619
+ return applicationJson(page);
593
620
  }
594
621
  const input = message.application.data;
595
622
  const request = Readable.from([Buffer.from(JSON.stringify(input.body ?? {}), 'utf8')]);