ftown-bridge 0.30.76 → 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';
@@ -18,6 +19,8 @@ import { EventEmitter } from 'node:events';
18
19
  import { timingSafeEqual } from 'node:crypto';
19
20
  import { Readable } from 'node:stream';
20
21
  import { applicationJson } from './security/bridge/security.js';
22
+ import { parseSecureEnvelope } from './security/crypto.js';
23
+ import { PEER_BROWSER_PACKET, peerRouteChannel } from './protocol/peer-routing.js';
21
24
  import { executeSecurePacket } from './security/bridge/execute.js';
22
25
  import { observeHttpDelivery, scopedUpdateObserver } from './update-effects.js';
23
26
  import { nativeWake } from './security/bridge/native-wake.js';
@@ -368,6 +371,55 @@ export class LocalApiServer extends EventEmitter {
368
371
  for (const wake of this.localEventWaiters)
369
372
  wake();
370
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
+ }
371
423
  async dispatchSecure(message, clientGone) {
372
424
  return sessionRequestPrincipal.run(message.principal, () => this.dispatchAuthorized(message, clientGone));
373
425
  }
@@ -383,6 +435,20 @@ export class LocalApiServer extends EventEmitter {
383
435
  if (route.kind !== 'http')
384
436
  throw new Error('Invalid application route');
385
437
  const routePath = route.path.split('?')[0];
438
+ if (routePath === PEER_BROWSER_PACKET) {
439
+ const gateway = message.principal.endpoint.kind === 'bridge' &&
440
+ this.security.peerBridges().find(peer => peer.endpoint.id === message.principal.endpoint.id);
441
+ if (!gateway || route.method !== 'POST')
442
+ return { status: 403, body: { error: 'permission_denied' } };
443
+ const body = message.application.data?.body;
444
+ const packet = parseSecureEnvelope(body?.packet);
445
+ if (packet.purpose !== 'hello' && packet.purpose !== 'application')
446
+ return { status: 400, body: { error: 'invalid_packet' } };
447
+ const bridgeId = this.security.bridgeId;
448
+ const context = { transport: 'peer-hop', bridgeId, pairId: gateway.bridgeId, channel: peerRouteChannel(gateway.bridgeId, bridgeId) };
449
+ const answer = await executeSecurePacket(this.security, packet, context, inner => this.dispatchSecure(inner));
450
+ return { status: 200, body: applicationJson({ packet: answer }) };
451
+ }
386
452
  if (routePath === '/api/team-questions') {
387
453
  const owner = message.principal.endpoint.kind === 'bridge'
388
454
  && this.security.peerBridges().find(peer => peer.endpoint.id === message.principal.endpoint.id)?.bridgeId;
@@ -548,32 +614,9 @@ export class LocalApiServer extends EventEmitter {
548
614
  if (!/^\d{1,16}$/.test(raw) || !Number.isSafeInteger(Number(raw)))
549
615
  throw new Error('Invalid event cursor');
550
616
  const after = Number(raw);
551
- if (after === this.localEventSequence)
552
- await new Promise(resolve => {
553
- const wake = () => { clearTimeout(timer); this.localEventWaiters.delete(wake); resolve(); };
554
- const timer = setTimeout(wake, 20000);
555
- this.localEventWaiters.add(wake);
556
- });
617
+ const page = await this.readEventPage(after, 20000);
557
618
  await this.security.revalidate(message.principal);
558
- let reset = after > this.localEventSequence || after < (this.localEvents[0]?.cursor ?? this.localEventSequence + 1) - 1;
559
- const events = this.localEvents.filter(event => reset || event.cursor > after);
560
- const page = [];
561
- let bytes = 0;
562
- let cursor = reset ? 0 : after;
563
- for (const event of events) {
564
- const size = Buffer.byteLength(JSON.stringify(event));
565
- if (size > 250000) {
566
- cursor = event.cursor;
567
- reset = true;
568
- continue;
569
- }
570
- if (bytes + size > 250000)
571
- break;
572
- page.push(event);
573
- bytes += size;
574
- cursor = event.cursor;
575
- }
576
- return applicationJson({ events: page, cursor: events.length ? cursor : this.localEventSequence, reset });
619
+ return applicationJson(page);
577
620
  }
578
621
  const input = message.application.data;
579
622
  const request = Readable.from([Buffer.from(JSON.stringify(input.body ?? {}), 'utf8')]);