@floegence/flowersec-core 7.4.0 → 8.0.0

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.
Files changed (42) hide show
  1. package/THIRD_PARTY_NOTICES.md +6 -5
  2. package/dist/browser/connectSessionV3.d.ts +5 -2
  3. package/dist/browser/connectSessionV3.js +8 -1
  4. package/dist/node/acceptorV3.d.ts +1 -0
  5. package/dist/node/acceptorV3.js +4 -2
  6. package/dist/node/connectSessionV3.d.ts +5 -1
  7. package/dist/node/connectSessionV3.js +4 -0
  8. package/dist/node/tunnelRuntimeV3.js +3 -4
  9. package/dist/node/webSocketServerV3.js +1 -1
  10. package/dist/proxy/index.d.ts +1 -1
  11. package/dist/proxy/index.js +1 -1
  12. package/dist/proxy/integration.d.ts +12 -0
  13. package/dist/proxy/integration.js +33 -0
  14. package/dist/proxy/runtime.d.ts +2 -1
  15. package/dist/proxy/runtime.js +13 -5
  16. package/dist/proxy/sessionRotation.d.ts +26 -0
  17. package/dist/proxy/sessionRotation.js +131 -0
  18. package/dist/public/contract.d.ts +1 -0
  19. package/dist/v3/artifact.js +1 -1
  20. package/dist/v3/artifactLease.d.ts +2 -1
  21. package/dist/v3/artifactLease.js +7 -2
  22. package/dist/v3/connectionController.d.ts +15 -3
  23. package/dist/v3/connectionController.js +86 -17
  24. package/dist/v3/contract.d.ts +3 -0
  25. package/dist/v3/handshake.d.ts +9 -1
  26. package/dist/v3/handshake.js +53 -5
  27. package/dist/v3/protocol.d.ts +2 -1
  28. package/dist/v3/protocol.js +3 -1
  29. package/dist/v3/publicSession.d.ts +5 -1
  30. package/dist/v3/publicSession.js +7 -0
  31. package/dist/v3/security.d.ts +4 -1
  32. package/dist/v3/security.js +3 -0
  33. package/dist/v3/serverAdmission.d.ts +2 -0
  34. package/dist/v3/serverAdmission.js +1 -0
  35. package/dist/v3/session.d.ts +8 -1
  36. package/dist/v3/session.js +59 -9
  37. package/dist/v3/sessionConnector.d.ts +5 -1
  38. package/dist/v3/sessionConnector.js +7 -4
  39. package/dist/v3/unreliableMessage.js +1 -1
  40. package/package.json +4 -3
  41. package/sbom/cyclonedx.json +68 -31
  42. package/sbom/spdx.json +75 -38
@@ -3,14 +3,15 @@
3
3
  This file is generated from the canonical Flowersec source dependency inventory for flowersec-ts.
4
4
  Do not edit it manually. License decisions are reviewed by the repository source license policy.
5
5
 
6
- - @floegence/flowersec-node-native 7.4.0 (Declared: MIT; selected: MIT; source: https://github.com/floegence/flowersec.git)
7
- - @floegence/flowersec-node-native-darwin-arm64 7.4.0 (Declared: MIT; selected: MIT; source: https://github.com/floegence/flowersec.git)
8
- - @floegence/flowersec-node-native-darwin-x64 7.4.0 (Declared: MIT; selected: MIT; source: https://github.com/floegence/flowersec.git)
9
- - @floegence/flowersec-node-native-linux-arm64-gnu 7.4.0 (Declared: MIT; selected: MIT; source: https://github.com/floegence/flowersec.git)
10
- - @floegence/flowersec-node-native-linux-x64-gnu 7.4.0 (Declared: MIT; selected: MIT; source: https://github.com/floegence/flowersec.git)
6
+ - @floegence/flowersec-node-native 8.0.0 (Declared: MIT; selected: MIT; source: https://github.com/floegence/flowersec.git)
7
+ - @floegence/flowersec-node-native-darwin-arm64 8.0.0 (Declared: MIT; selected: MIT; source: https://github.com/floegence/flowersec.git)
8
+ - @floegence/flowersec-node-native-darwin-x64 8.0.0 (Declared: MIT; selected: MIT; source: https://github.com/floegence/flowersec.git)
9
+ - @floegence/flowersec-node-native-linux-arm64-gnu 8.0.0 (Declared: MIT; selected: MIT; source: https://github.com/floegence/flowersec.git)
10
+ - @floegence/flowersec-node-native-linux-x64-gnu 8.0.0 (Declared: MIT; selected: MIT; source: https://github.com/floegence/flowersec.git)
11
11
  - @noble/ciphers 2.4.0 (Declared: MIT; selected: MIT; source: https://registry.npmjs.org/@noble/ciphers/-/ciphers-2.4.0.tgz)
12
12
  - @noble/curves 2.4.0 (Declared: MIT; selected: MIT; source: https://registry.npmjs.org/@noble/curves/-/curves-2.4.0.tgz)
13
13
  - @noble/hashes 2.4.0 (Declared: MIT; selected: MIT; source: https://registry.npmjs.org/@noble/hashes/-/hashes-2.4.0.tgz)
14
+ - @noble/post-quantum 0.7.1 (Declared: MIT; selected: MIT; source: https://registry.npmjs.org/@noble/post-quantum/-/post-quantum-0.7.1.tgz)
14
15
  - @types/node 26.4.0 (Declared: MIT; selected: MIT; source: https://registry.npmjs.org/@types/node/-/node-26.4.0.tgz)
15
16
  - punycode 2.3.1 (Declared: MIT; selected: MIT; source: https://registry.npmjs.org/punycode/-/punycode-2.3.1.tgz)
16
17
  - tr46 7.0.0 (Declared: MIT; selected: MIT; source: https://registry.npmjs.org/tr46/-/tr46-7.0.0.tgz)
@@ -1,7 +1,7 @@
1
1
  import type { Session } from "../public/contract.js";
2
2
  import { RPCHandlers, type SessionHandlerBinding } from "../public/sessionHandlers.js";
3
- import { type ArtifactLeaseV3 } from "../v3/artifactLease.js";
4
- import { type ArtifactSourceV3, type ConnectionControllerV3 } from "../v3/connectionController.js";
3
+ import { type ArtifactLeaseV3, type KeyExchangeMode } from "../v3/artifactLease.js";
4
+ import { type ArtifactSourceV3, type ConnectionControllerV3, type RotationDriver } from "../v3/connectionController.js";
5
5
  import { type PrivateLoopbackArtifactLeaseV1, type PrivateLoopbackArtifactSourceV1 } from "./privateLoopbackV1.js";
6
6
  import { type HTTPDirectArtifactLeaseV1, type HTTPDirectArtifactSourceV1 } from "./httpDirectV1.js";
7
7
  type RPCOptions = Readonly<{
@@ -14,6 +14,8 @@ type ControllerRPCOptions = Omit<RPCOptions, "rpcHandlers"> & Readonly<{
14
14
  }>;
15
15
  type TunnelOptions = Readonly<{
16
16
  tunnelIngressURL?: string;
17
+ keyExchange?: KeyExchangeMode;
18
+ enableHybridKeyExchange?: boolean;
17
19
  }>;
18
20
  export type SessionOptionsV3 = RPCOptions & TunnelOptions & Readonly<{
19
21
  signal?: AbortSignal;
@@ -35,6 +37,7 @@ export type PrivateLoopbackConnectionControllerOptionsV1 = ControllerRPCOptions
35
37
  }>;
36
38
  export declare function connectV3(lease: ArtifactLeaseV3, options?: SessionOptionsV3): Promise<Session>;
37
39
  export declare function createConnectionControllerV3(source: ArtifactSourceV3, options?: ConnectionControllerOptionsV3): Promise<ConnectionControllerV3<Session>>;
40
+ export declare function createBrowserController(source: ArtifactSourceV3, options: ConnectionControllerOptionsV3, rotation?: RotationDriver<Session>): Promise<ConnectionControllerV3<Session>>;
38
41
  export type HTTPDirectSessionOptionsV1 = PrivateLoopbackSessionOptionsV1;
39
42
  export type HTTPDirectConnectionControllerOptionsV1 = PrivateLoopbackConnectionControllerOptionsV1;
40
43
  export declare function connectPrivateLoopbackV1(lease: PrivateLoopbackArtifactLeaseV1, options: PrivateLoopbackSessionOptionsV1): Promise<Session>;
@@ -18,16 +18,21 @@ export async function connectV3(lease, options = {}) {
18
18
  });
19
19
  }
20
20
  export async function createConnectionControllerV3(source, options = {}) {
21
+ return createBrowserController(source, options);
22
+ }
23
+ export async function createBrowserController(source, options, rotation) {
21
24
  requireControllerHandlers(options);
22
25
  const registry = await BrowserRuntimeCapabilityRegistryV3.create();
23
26
  const runtime = browserRuntime(registry, options);
24
27
  const coreOptions = {
28
+ keyExchange: options.keyExchange ?? "classic",
29
+ connectTimeoutMs: options.connectTimeoutMs ?? SDK_DEFAULTS.transport.connectTimeoutMs,
25
30
  ...(runtime.tunnelIngressURL === undefined ? {} : { tunnelIngressURL: runtime.tunnelIngressURL }),
26
31
  capabilitySnapshot: runtime.capabilitySnapshot,
27
32
  projectSessionFailure,
28
33
  ...(options.maximumAttempts === undefined ? {} : { maximumAttempts: options.maximumAttempts }),
29
34
  };
30
- return createCoreControllerV3(source, async (context) => await attemptClaimedArtifactLeaseV3(context, runtime), coreOptions);
35
+ return createCoreControllerV3(source, async (context) => await attemptClaimedArtifactLeaseV3(context, runtime), coreOptions, rotation);
31
36
  }
32
37
  const privateProfile = {
33
38
  validateOrigin: validatePrivateLoopbackOriginV1,
@@ -170,6 +175,8 @@ function browserRuntime(registry, options) {
170
175
  capabilitySnapshot: () => registry.snapshot(),
171
176
  connectTimeoutMilliseconds,
172
177
  protocolRuntime: browserSessionRuntimeV3,
178
+ keyExchange: options.keyExchange ?? "classic",
179
+ enableHybridKeyExchange: options.enableHybridKeyExchange === true,
173
180
  ...(options.tunnelIngressURL === undefined ? {} : { tunnelIngressURL: options.tunnelIngressURL }),
174
181
  ...rpcOptions(options),
175
182
  dial: async (candidate, artifact, attemptNow, capability, signal) => {
@@ -29,6 +29,7 @@ export type AcceptorListenerV3 = (NodeWebTransportListenerConfig & Readonly<{
29
29
  }>;
30
30
  }>;
31
31
  export type AcceptorOptionsV3 = Readonly<{
32
+ enableHybridKeyExchange?: boolean;
32
33
  listeners: readonly AcceptorListenerV3[];
33
34
  maxInboundStreams: number;
34
35
  /** Bounds admissions in progress and authenticated sessions awaiting accept. */
@@ -117,6 +117,7 @@ async function admit(state, carrier, ingress, providers) {
117
117
  };
118
118
  const internal = await acceptCarrierSessionV3(carrier, authorize, {
119
119
  runtime: nodeSessionRuntimeV3,
120
+ enableHybridKeyExchange: state.enableHybridKeyExchange,
120
121
  maxRpcFrameBytes: state.maxRpcFrameBytes,
121
122
  maxConcurrentRpcRequests: state.maxConcurrentRpcRequests,
122
123
  admissionReasons: state.admissionReasons,
@@ -172,7 +173,7 @@ export async function createAcceptorV3(options) {
172
173
  const running = listener.carrier === "websocket"
173
174
  ? await startNodeWebSocketListenerV3({
174
175
  ...listener,
175
- maxPendingSessions: Math.min(options.maxPendingSessions ?? 1024, 1024),
176
+ maxPendingSessions: options.maxPendingSessions ?? 1024,
176
177
  ...(options.admissionTimeoutMs === undefined ? {} : { pendingSessionTimeoutMs: options.admissionTimeoutMs }),
177
178
  inboundBidirectionalStreamCapacity: options.maxInboundStreams + 2,
178
179
  })
@@ -191,6 +192,7 @@ export async function createAcceptorV3(options) {
191
192
  }
192
193
  const acceptor = new AcceptorV3();
193
194
  const state = {
195
+ enableHybridKeyExchange: options.enableHybridKeyExchange === true,
194
196
  listeners: Object.freeze(listeners),
195
197
  authorize: options.authorize,
196
198
  ...(options.resolveHandlers === undefined ? {} : { resolveHandlers: options.resolveHandlers }),
@@ -278,7 +280,7 @@ async function listen(state, listener, ingress) {
278
280
  function validateOptions(options) {
279
281
  validateWebTransportListeners(options.listeners);
280
282
  if (options.listeners.length === 0 ||
281
- (options.maxPendingSessions !== undefined && (!Number.isSafeInteger(options.maxPendingSessions) || options.maxPendingSessions < 1 || options.maxPendingSessions > 65536)) ||
283
+ (options.maxPendingSessions !== undefined && (!Number.isSafeInteger(options.maxPendingSessions) || options.maxPendingSessions < 1)) ||
282
284
  !Number.isSafeInteger(options.maxInboundStreams) || options.maxInboundStreams < 1 ||
283
285
  options.maxInboundStreams > 128 || typeof options.authorize !== "function" ||
284
286
  (options.admissionTimeoutMs !== undefined &&
@@ -1,10 +1,12 @@
1
1
  import type { NodeConnectionPath } from "./connectionPath.js";
2
2
  import type { Session } from "../public/contract.js";
3
- import type { ArtifactLeaseV3 } from "../v3/artifactLease.js";
3
+ import type { ArtifactLeaseV3, KeyExchangeMode } from "../v3/artifactLease.js";
4
4
  import { type ArtifactSourceV3, type ConnectionControllerV3 } from "../v3/connectionController.js";
5
5
  import { RPCHandlers, type SessionHandlerBinding } from "./acceptor.js";
6
6
  export type NodeTLSRootsV3 = string | Uint8Array | readonly (string | Uint8Array)[];
7
7
  export type SessionOptionsV3 = Readonly<{
8
+ keyExchange?: KeyExchangeMode;
9
+ enableHybridKeyExchange?: boolean;
8
10
  webTransport?: boolean;
9
11
  origin?: string;
10
12
  roots?: NodeTLSRootsV3;
@@ -18,6 +20,8 @@ export type SessionOptionsV3 = Readonly<{
18
20
  tunnelIngressURL?: string;
19
21
  }>;
20
22
  export type ConnectionControllerOptionsV3 = Readonly<{
23
+ keyExchange?: KeyExchangeMode;
24
+ enableHybridKeyExchange?: boolean;
21
25
  webTransport?: boolean;
22
26
  origin?: string;
23
27
  roots?: NodeTLSRootsV3;
@@ -21,6 +21,8 @@ export function createConnectionControllerV3(source, options) {
21
21
  }
22
22
  const runtime = nodeRuntime(options);
23
23
  const coreOptions = {
24
+ keyExchange: options.keyExchange ?? "classic",
25
+ connectTimeoutMs: options.connectTimeoutMs ?? SDK_DEFAULTS.transport.connectTimeoutMs,
24
26
  ...(runtime.tunnelIngressURL === undefined ? {} : { tunnelIngressURL: runtime.tunnelIngressURL }),
25
27
  capabilitySnapshot: runtime.capabilitySnapshot,
26
28
  projectSessionFailure,
@@ -59,6 +61,8 @@ function nodeRuntime(options) {
59
61
  (candidate.carrier === "raw_quic" || options.origin !== undefined),
60
62
  connectTimeoutMilliseconds,
61
63
  protocolRuntime: nodeSessionRuntimeV3,
64
+ keyExchange: options.keyExchange ?? "classic",
65
+ enableHybridKeyExchange: options.enableHybridKeyExchange === true,
62
66
  maxRpcFrameBytes: rpcFrameLimit(options.maxRpcFrameBytes),
63
67
  maxConcurrentRpcRequests: rpcConcurrency(options.maxConcurrentRpcRequests),
64
68
  ...(options.tunnelIngressURL === undefined ? {} : { tunnelIngressURL: options.tunnelIngressURL }),
@@ -13,7 +13,6 @@ const DEFAULT_ADMISSION_TIMEOUT_MS = 10_000;
13
13
  const DEFAULT_CLEANUP_TIMEOUT_MS = 2_000;
14
14
  const DEFAULT_MAX_CONCURRENT_ADMISSIONS = 1_024;
15
15
  const DEFAULT_MAX_PENDING_LEGS = 1_024;
16
- const DEFAULT_MAX_ACTIVE_PAIRS = 1_024;
17
16
  const DEFAULT_MAX_CONCURRENT_STREAMS = 128;
18
17
  const ID = /^[A-Za-z0-9._~-]{1,128}$/u;
19
18
  const BUILT_IN_ADMISSION_REASONS = [
@@ -645,7 +644,7 @@ function validateOptions(options) {
645
644
  validateWebTransportListeners(options.listeners);
646
645
  const limits = {
647
646
  maxPendingLegs: options.maxPendingLegs ?? DEFAULT_MAX_PENDING_LEGS,
648
- maxActivePairs: options.maxActivePairs ?? DEFAULT_MAX_ACTIVE_PAIRS,
647
+ maxActivePairs: options.maxActivePairs ?? Number.MAX_SAFE_INTEGER,
649
648
  maxConcurrentStreams: options.maxConcurrentStreams ?? DEFAULT_MAX_CONCURRENT_STREAMS,
650
649
  maxConcurrentAdmissions: options.maxConcurrentAdmissions ?? DEFAULT_MAX_CONCURRENT_ADMISSIONS,
651
650
  pairTimeoutMs: options.pairTimeoutMs ?? DEFAULT_PAIR_TIMEOUT_MS,
@@ -656,9 +655,9 @@ function validateOptions(options) {
656
655
  !Number.isSafeInteger(options.maxInboundStreams) ||
657
656
  options.maxInboundStreams < 1 || options.maxInboundStreams > 128 || typeof options.authorize !== "function" ||
658
657
  (options.release !== undefined && typeof options.release !== "function") ||
659
- !boundedInteger(limits.maxPendingLegs, 1, 65_536) || !boundedInteger(limits.maxActivePairs, 1, 65_536) ||
658
+ !boundedInteger(limits.maxPendingLegs, 1, Number.MAX_SAFE_INTEGER) || !boundedInteger(limits.maxActivePairs, 1, Number.MAX_SAFE_INTEGER) ||
660
659
  !boundedInteger(limits.maxConcurrentStreams, 1, 128) ||
661
- !boundedInteger(limits.maxConcurrentAdmissions, 1, 65_536) ||
660
+ !boundedInteger(limits.maxConcurrentAdmissions, 1, Number.MAX_SAFE_INTEGER) ||
662
661
  !boundedInteger(limits.pairTimeoutMs, 1, 60_000) ||
663
662
  !boundedInteger(limits.admissionTimeoutMs, 1, 60_000) ||
664
663
  !boundedInteger(limits.cleanupTimeoutMs, 1, 60_000)) {
@@ -143,7 +143,7 @@ function validateOptions(options) {
143
143
  options.allowedOrigins.some((origin) => !validOrigin(origin)) ||
144
144
  !Number.isInteger(options.inboundBidirectionalStreamCapacity) ||
145
145
  options.inboundBidirectionalStreamCapacity < 3 || options.inboundBidirectionalStreamCapacity > 130 ||
146
- (options.maxPendingSessions !== undefined && (!Number.isSafeInteger(options.maxPendingSessions) || options.maxPendingSessions < 1 || options.maxPendingSessions > 1_024)) ||
146
+ (options.maxPendingSessions !== undefined && (!Number.isSafeInteger(options.maxPendingSessions) || options.maxPendingSessions < 1)) ||
147
147
  (options.pendingSessionTimeoutMs !== undefined && (!Number.isSafeInteger(options.pendingSessionTimeoutMs) || options.pendingSessionTimeoutMs < 1 || options.pendingSessionTimeoutMs > 600_000)) ||
148
148
  (options.cleanupTimeoutMs !== undefined && (!Number.isSafeInteger(options.cleanupTimeoutMs) || options.cleanupTimeoutMs < 1 || options.cleanupTimeoutMs > 600_000)) ||
149
149
  options.tls.certificate.length === 0 || options.tls.privateKey.length === 0) {
@@ -12,6 +12,6 @@ export type { ProxyAppServiceWorkerControlOptions, ProxyAppWindowHandle, ProxyCo
12
12
  export { installWebSocketPatch } from "./wsPatch.js";
13
13
  export type { WebSocketPatchOptions } from "./wsPatch.js";
14
14
  export { disableUpstreamServiceWorkerRegister } from "./disableUpstreamServiceWorkerRegister.js";
15
- export { connectProxyBrowser, connectProxyControllerBrowser, } from "./integration.js";
15
+ export { connectProxyBrowser, connectRotatingProxyBrowser, connectProxyControllerBrowser, } from "./integration.js";
16
16
  export type { ProxyBrowserConnectOptions, ProxyBrowserHandle, ProxyControllerBrowserConnectOptions, ProxyControllerBrowserHandle, } from "./integration.js";
17
17
  export type { ProxyFetchRequest, ProxyHeader, ProxyRuntime, ProxyRuntimeControllerBridgeScope, ProxyRuntimeHTTPScope, ProxyRuntimeLimits, ProxyRuntimeOptions, ProxyRequestInit, ProxyRuntimePathPolicy, ProxyRuntimeScopeLimits, ProxyRuntimeScope, ProxyRuntimeServiceWorkerScope, } from "./types.js";
@@ -6,4 +6,4 @@ export { createServiceWorkerControllerGuard } from "./controllerGuard.js";
6
6
  export { registerProxyAppWindow, registerProxyAppWindowWithServiceWorkerControl, registerProxyAppWindowWithServiceWorkerRuntime, registerProxyControllerWindow, } from "./windowBridge.js";
7
7
  export { installWebSocketPatch } from "./wsPatch.js";
8
8
  export { disableUpstreamServiceWorkerRegister } from "./disableUpstreamServiceWorkerRegister.js";
9
- export { connectProxyBrowser, connectProxyControllerBrowser, } from "./integration.js";
9
+ export { connectProxyBrowser, connectRotatingProxyBrowser, connectProxyControllerBrowser, } from "./integration.js";
@@ -1,4 +1,6 @@
1
1
  import { type SessionOptionsV3 } from "../browser/connectSessionV3.js";
2
+ import { type ConnectionControllerOptionsV3 } from "../browser/connectSessionV3.js";
3
+ import type { ArtifactSourceV3 } from "../v3/connectionController.js";
2
4
  import { type ArtifactLeaseV3 } from "../v3/artifactLease.js";
3
5
  import type { Session } from "../public/contract.js";
4
6
  import type { ProxyRuntime, ProxyRuntimeOptions } from "./types.js";
@@ -19,6 +21,16 @@ export type ProxyBrowserHandle = Readonly<{
19
21
  runtime: ProxyRuntime;
20
22
  dispose(): Promise<void>;
21
23
  }>;
24
+ export declare function connectRotatingProxyBrowser(source: ArtifactSourceV3, options?: Omit<ProxyBrowserConnectOptions, "connect"> & Readonly<{
25
+ connect?: ConnectionControllerOptionsV3;
26
+ minimumRemaining?: number;
27
+ setupBudgetMs?: number;
28
+ drainTimeoutMs?: number;
29
+ signal?: AbortSignal;
30
+ }>): Promise<Readonly<{
31
+ runtime: ProxyRuntime;
32
+ dispose(): Promise<void>;
33
+ }>>;
22
34
  export declare function connectProxyBrowser(lease: ArtifactLeaseV3, options?: ProxyBrowserConnectOptions): Promise<ProxyBrowserHandle>;
23
35
  export type ProxyControllerBrowserConnectOptions = ProxyBrowserConnectOptions & Readonly<{
24
36
  controller: Omit<RegisterProxyControllerWindowOptions, "runtime">;
@@ -1,4 +1,6 @@
1
1
  import { connectV3 } from "../browser/connectSessionV3.js";
2
+ import { createBrowserController } from "../browser/connectSessionV3.js";
3
+ import { DirectProxyRotation } from "./sessionRotation.js";
2
4
  import { leaseScopeV3 } from "../v3/artifactLease.js";
3
5
  import { createScopedProxyRuntime } from "./runtime.js";
4
6
  import { assertProxyRuntimeScope, PROXY_RUNTIME_SCOPE } from "./scope.js";
@@ -12,6 +14,37 @@ function proxyAuthority(lease) {
12
14
  throw new TypeError("unsupported proxy scope version");
13
15
  return assertProxyRuntimeScope(entry.payload);
14
16
  }
17
+ export async function connectRotatingProxyBrowser(source, options = {}) {
18
+ const rotation = new DirectProxyRotation(options.minimumRemaining ?? 4096, options.setupBudgetMs ?? 10_000, options.drainTimeoutMs ?? 60_000);
19
+ const controller = await createBrowserController(source, options.connect ?? {}, rotation);
20
+ let runtime;
21
+ const dispose = async () => {
22
+ options.signal?.removeEventListener("abort", cancel);
23
+ runtime?.dispose();
24
+ await controller.close();
25
+ };
26
+ const cancel = () => { void dispose(); };
27
+ try {
28
+ options.signal?.throwIfAborted();
29
+ options.signal?.addEventListener("abort", cancel, { once: true });
30
+ controller.start();
31
+ const session = await controller.waitForSession();
32
+ const authority = rotation.authority;
33
+ const worker = options.serviceWorker;
34
+ if (worker && authority && (authority.mode !== "service_worker" || worker.scriptUrl !== authority.serviceWorker.scriptUrl || worker.scope !== authority.serviceWorker.scope)) {
35
+ throw new TypeError("service worker does not match the proxy scope");
36
+ }
37
+ if (worker)
38
+ await registerServiceWorkerAndEnsureControl(worker);
39
+ options.signal?.throwIfAborted();
40
+ runtime = createScopedProxyRuntime({ ...options.runtime, session }, authority, rotation);
41
+ return Object.freeze({ runtime, dispose });
42
+ }
43
+ catch (error) {
44
+ await dispose();
45
+ throw error;
46
+ }
47
+ }
15
48
  export async function connectProxyBrowser(lease, options = {}) {
16
49
  const authority = proxyAuthority(lease);
17
50
  if (authority && options.serviceWorker && (authority.mode !== "service_worker" ||
@@ -1,6 +1,7 @@
1
+ import type { DirectProxyRotation } from "./sessionRotation.js";
1
2
  import type { ProxyRuntime, ProxyRuntimeOptions, ProxyRuntimeScope } from "./types.js";
2
3
  export declare function createProxyRuntime(options: ProxyRuntimeOptions): ProxyRuntime;
3
- export declare function createScopedProxyRuntime(options: ProxyRuntimeOptions, authority?: ProxyRuntimeScope): ProxyRuntime;
4
+ export declare function createScopedProxyRuntime(options: ProxyRuntimeOptions, authority?: ProxyRuntimeScope, rotation?: DirectProxyRotation): ProxyRuntime;
4
5
  export type EnsureServiceWorkerRuntimeRegisteredOptions = Readonly<{
5
6
  timeoutMs?: number;
6
7
  runtimeRegistrationToken?: string;
@@ -242,7 +242,7 @@ function publicFailure(error) {
242
242
  export function createProxyRuntime(options) {
243
243
  return createScopedProxyRuntime(options);
244
244
  }
245
- export function createScopedProxyRuntime(options, authority) {
245
+ export function createScopedProxyRuntime(options, authority, rotation) {
246
246
  if (authority) {
247
247
  const grantedHeaders = authority.http?.extraRequestHeaders ?? [];
248
248
  const limits = Object.fromEntries(Object.entries(authority.limits ?? {}).map(([key, ceiling]) => {
@@ -291,6 +291,7 @@ export function createScopedProxyRuntime(options, authority) {
291
291
  let activeEvents = 0;
292
292
  const active = new Set();
293
293
  const lifetime = new AbortController();
294
+ const streamOwner = rotation ?? options.session;
294
295
  const register = () => void ensureServiceWorkerRuntimeRegistered({
295
296
  timeoutMs: 2_000,
296
297
  ...(runtimeRegistrationToken === undefined ? {} : { runtimeRegistrationToken }),
@@ -373,7 +374,7 @@ export function createScopedProxyRuntime(options, authority) {
373
374
  arm(remaining);
374
375
  release = await admission.acquire(body.length, controller.signal, eventRequested);
375
376
  controller.signal.throwIfAborted();
376
- stream = await options.session.openStream(PROXY_HTTP_STREAM_KIND, {
377
+ stream = await streamOwner.openStream(PROXY_HTTP_STREAM_KIND, {
377
378
  signal: controller.signal,
378
379
  metadata: createStreamMetadata({ protocol: "flowersec.proxy.http", version: 2 }),
379
380
  });
@@ -546,7 +547,7 @@ export function createScopedProxyRuntime(options, authority) {
546
547
  void stream?.reset().catch(() => undefined);
547
548
  };
548
549
  const establish = async () => {
549
- const stream = await options.session.openStream(PROXY_WEBSOCKET_STREAM_KIND, {
550
+ const stream = await streamOwner.openStream(PROXY_WEBSOCKET_STREAM_KIND, {
550
551
  signal,
551
552
  metadata: createStreamMetadata({ protocol: "flowersec.proxy.websocket", version: 2 }),
552
553
  });
@@ -615,8 +616,15 @@ export function createScopedProxyRuntime(options, authority) {
615
616
  serviceWorker.addEventListener("controllerchange", register);
616
617
  register();
617
618
  }
618
- void options.session.waitTermination({ signal: lifetime.signal }).then(terminate, () => { if (!disposed)
619
- terminate(); });
619
+ if (rotation) {
620
+ rotation.lifetime.signal.addEventListener("abort", terminate, { once: true, signal: lifetime.signal });
621
+ if (rotation.lifetime.signal.aborted)
622
+ terminate();
623
+ }
624
+ else {
625
+ void options.session.waitTermination({ signal: lifetime.signal }).then(terminate, () => { if (!disposed)
626
+ terminate(); });
627
+ }
620
628
  return runtime;
621
629
  }
622
630
  export async function ensureServiceWorkerRuntimeRegistered(options = {}) {
@@ -0,0 +1,26 @@
1
+ import { type ByteStream, type Session } from "../public/contract.js";
2
+ import type { ArtifactV3 } from "../v3/artifact.js";
3
+ import { streamBudgetOwner } from "../v3/publicSession.js";
4
+ import type { RotationDriver } from "../v3/connectionController.js";
5
+ import type { ProxyRuntimeScope } from "./types.js";
6
+ export declare class DirectProxyRotation implements RotationDriver<Session> {
7
+ private readonly minimum;
8
+ private readonly setupMs;
9
+ private readonly drainMs;
10
+ private readonly budget;
11
+ readonly lifetime: AbortController;
12
+ authority: ProxyRuntimeScope | undefined;
13
+ private binding;
14
+ private previousToken;
15
+ private current;
16
+ private draining;
17
+ private shutdown;
18
+ constructor(minimum: number, setupMs: number, drainMs: number, budget?: typeof streamBudgetOwner);
19
+ accept(artifact: ArtifactV3): void;
20
+ publish(session: Session): Promise<void>;
21
+ canServe(session: Session): boolean;
22
+ openStream(...args: Parameters<Session["openStream"]>): Promise<ByteStream>;
23
+ private retire;
24
+ slot(): Promise<void>;
25
+ stop(): Promise<void>;
26
+ }
@@ -0,0 +1,131 @@
1
+ import { SessionError } from "../public/contract.js";
2
+ import { canonicalizeJCSV3 } from "../v3/jcs.js";
3
+ import { streamBudgetOwner } from "../v3/publicSession.js";
4
+ import { assertProxyRuntimeScope, PROXY_RUNTIME_SCOPE } from "./scope.js";
5
+ export class DirectProxyRotation {
6
+ minimum;
7
+ setupMs;
8
+ drainMs;
9
+ budget;
10
+ lifetime = new AbortController();
11
+ authority;
12
+ binding;
13
+ previousToken;
14
+ current;
15
+ draining = Promise.resolve();
16
+ shutdown;
17
+ constructor(minimum, setupMs, drainMs, budget = streamBudgetOwner) {
18
+ this.minimum = minimum;
19
+ this.setupMs = setupMs;
20
+ this.drainMs = drainMs;
21
+ this.budget = budget;
22
+ if (!Number.isSafeInteger(minimum) || minimum < 1 || minimum >= 1_048_576 ||
23
+ !Number.isSafeInteger(setupMs) || setupMs < 1 || !Number.isSafeInteger(drainMs) || drainMs < 1 || drainMs > 2_147_483_647) {
24
+ throw new TypeError("invalid proxy session rotation budget");
25
+ }
26
+ }
27
+ accept(artifact) {
28
+ const path = artifact.path;
29
+ if (path.kind !== "direct")
30
+ throw new TypeError("rotating browser proxy requires a direct grant");
31
+ const scope = artifact.scoped.find(entry => entry.scope === PROXY_RUNTIME_SCOPE.name);
32
+ if (scope && scope.scope_version !== PROXY_RUNTIME_SCOPE.version)
33
+ throw new TypeError("unsupported proxy scope version");
34
+ const authority = scope && assertProxyRuntimeScope(scope.payload);
35
+ const binding = canonicalizeJCSV3([artifact.session.channel_id, path.listener_audience, artifact.scoped]);
36
+ if ((this.binding !== undefined && this.binding !== binding) || this.previousToken === path.routing_token) {
37
+ throw new TypeError("incompatible proxy rotation grant");
38
+ }
39
+ this.binding = binding;
40
+ this.authority = authority;
41
+ this.previousToken = path.routing_token;
42
+ }
43
+ publish(session) {
44
+ const old = this.current;
45
+ this.current = { session, references: 0 };
46
+ if (old)
47
+ this.draining = this.retire(old);
48
+ return this.budget(session).watch(this.minimum, this.setupMs);
49
+ }
50
+ canServe(session) {
51
+ return this.current?.session === session && this.budget(session).available();
52
+ }
53
+ async openStream(...args) {
54
+ const generation = this.current;
55
+ if (this.lifetime.signal.aborted || !generation)
56
+ throw new SessionError("closed");
57
+ if (!this.canServe(generation.session))
58
+ throw new SessionError("resource_exhausted");
59
+ generation.references++;
60
+ let released = false;
61
+ const release = () => {
62
+ if (released)
63
+ return;
64
+ released = true;
65
+ if (--generation.references === 0)
66
+ generation.finish?.();
67
+ };
68
+ try {
69
+ const stream = await generation.session.openStream(...args);
70
+ let eof = false, fin = false;
71
+ return {
72
+ kind: stream.kind,
73
+ get terminalError() { return stream.terminalError; },
74
+ async read(options) {
75
+ const value = await stream.read(options);
76
+ if (value === null) {
77
+ eof = true;
78
+ if (fin)
79
+ release();
80
+ }
81
+ return value;
82
+ },
83
+ write: (bytes, options) => stream.write(bytes, options),
84
+ async closeWrite() { await stream.closeWrite(); fin = true; if (eof)
85
+ release(); },
86
+ async reset() { try {
87
+ await stream.reset();
88
+ }
89
+ finally {
90
+ release();
91
+ } },
92
+ async close() { try {
93
+ await stream.close();
94
+ }
95
+ finally {
96
+ release();
97
+ } },
98
+ };
99
+ }
100
+ catch (error) {
101
+ release();
102
+ throw error;
103
+ }
104
+ }
105
+ async retire(generation) {
106
+ if (generation.references > 0 && !this.lifetime.signal.aborted) {
107
+ await new Promise(resolve => {
108
+ const timer = setTimeout(finish, this.drainMs);
109
+ const lifetime = this.lifetime.signal;
110
+ function finish() {
111
+ clearTimeout(timer);
112
+ lifetime.removeEventListener("abort", finish);
113
+ delete generation.finish;
114
+ resolve();
115
+ }
116
+ generation.finish = finish;
117
+ lifetime.addEventListener("abort", finish, { once: true });
118
+ });
119
+ }
120
+ await generation.session.close().catch(() => undefined);
121
+ }
122
+ slot() { return this.draining; }
123
+ stop() {
124
+ if (this.shutdown)
125
+ return this.shutdown;
126
+ this.lifetime.abort();
127
+ this.shutdown = Promise.allSettled([this.draining, this.current?.session.close()]).then(() => undefined);
128
+ this.current = undefined;
129
+ return this.shutdown;
130
+ }
131
+ }
@@ -67,6 +67,7 @@ export type SessionTermination = Readonly<{
67
67
  peerCloseReceived?: true;
68
68
  }>;
69
69
  export interface Session {
70
+ readonly usesHybridKeyExchange?: boolean;
70
71
  readonly rpc: RpcPeer;
71
72
  readonly unreliableMessages?: UnreliableMessageChannel;
72
73
  openStream(kind: string, options?: StreamOpenOptions): Promise<ByteStream>;
@@ -372,7 +372,7 @@ function validateSession(session) {
372
372
  let previous = 0;
373
373
  const suites = new Set();
374
374
  for (const suite of session.allowed_suites) {
375
- if ((suite !== 1 && suite !== 2) || suite <= previous || suites.has(suite)) {
375
+ if ((suite !== 1 && suite !== 2 && !(suite === 3 && session.allowed_suites.length === 1)) || suite <= previous || suites.has(suite)) {
376
376
  throw invalidArtifact("allowed suites");
377
377
  }
378
378
  previous = suite;
@@ -19,7 +19,8 @@ export declare function leaseScopeV3(lease: ArtifactLeaseV3, name: string): Read
19
19
  payload: Readonly<Record<string, unknown>>;
20
20
  }> | undefined;
21
21
  /** Local routing mistakes must not claim, retire or spend the invitation. */
22
- export declare function claimConnectionLeaseV3(lease: ArtifactLeaseV3, ingress?: string): ClaimedArtifactLeaseV3;
22
+ export type KeyExchangeMode = "classic" | "hybridRequired" | "hybridPreferred";
23
+ export declare function claimConnectionLeaseV3(lease: ArtifactLeaseV3, ingress?: string, mode?: KeyExchangeMode): ClaimedArtifactLeaseV3;
23
24
  export declare function claimedArtifactV3(claim: ClaimedArtifactLeaseV3): ArtifactV3;
24
25
  export declare function commitArtifactLeaseSpendV3(claim: ClaimedArtifactLeaseV3, signal?: AbortSignal): Promise<void>;
25
26
  export declare function retireArtifactLeaseV3(claim: ClaimedArtifactLeaseV3): Promise<void>;
@@ -1,5 +1,6 @@
1
1
  import { canonicalizeCandidatesV3 } from "./artifact.js";
2
2
  import { selectTunnelIngress } from "./tunnelIngress.js";
3
+ import { ConnectErrorV3 } from "./security.js";
3
4
  export class ArtifactLeaseV3Error extends Error {
4
5
  code = "already_consumed";
5
6
  constructor() {
@@ -54,12 +55,16 @@ export function leaseScopeV3(lease, name) {
54
55
  throw new ArtifactLeaseV3Error();
55
56
  return state.artifact.scoped.find(entry => entry.scope === name);
56
57
  }
57
- /** Local routing mistakes must not claim, retire or spend the invitation. */
58
- export function claimConnectionLeaseV3(lease, ingress) {
58
+ export function claimConnectionLeaseV3(lease, ingress, mode = "classic") {
59
59
  const state = leaseStates.get(lease);
60
60
  if (state === undefined || state.status !== "idle")
61
61
  throw new ArtifactLeaseV3Error();
62
62
  const path = state.artifact?.path;
63
+ if (mode !== "classic" && mode !== "hybridRequired" && mode !== "hybridPreferred")
64
+ throw new ConnectErrorV3("artifact_invalid", { kind: "terminal" });
65
+ if (path?.kind === "tunnel" && path.role === 2 ? mode !== "classic" : (state.artifact?.session?.default_suite === 3) !== (mode !== "classic")) {
66
+ throw new ConnectErrorV3("artifact_invalid", { kind: "terminal" });
67
+ }
63
68
  if (path?.kind === "tunnel" || (path?.kind === "direct" && ingress !== undefined)) {
64
69
  selectTunnelIngress(path.kind, canonicalizeCandidatesV3(path.kind, path.candidates).candidates, ingress);
65
70
  }
@@ -1,5 +1,5 @@
1
1
  import { type ArtifactV3, type CanonicalArtifactCandidateV3 } from "./artifact.js";
2
- import { type ArtifactLeaseV3, type ClaimedArtifactLeaseV3 } from "./artifactLease.js";
2
+ import { type ArtifactLeaseV3, type ClaimedArtifactLeaseV3, type KeyExchangeMode } from "./artifactLease.js";
3
3
  import { type CandidateFailureV3, type ControllerClockV3 } from "./controller.js";
4
4
  import { ConnectErrorV3, type RetryDispositionV3 } from "./security.js";
5
5
  import { type RuntimeCapabilityDescriptorV3 } from "./capability.js";
@@ -12,6 +12,9 @@ export type ArtifactSourceResultV3 = Readonly<{
12
12
  disposition: RetryDispositionV3;
13
13
  }>;
14
14
  export type ArtifactSourceV3 = Readonly<{
15
+ acquireForKeyExchange?(mode: "classic" | "hybridRequired", options: Readonly<{
16
+ signal: AbortSignal;
17
+ }>): Promise<ArtifactSourceResultV3>;
15
18
  acquire(options: Readonly<{
16
19
  signal: AbortSignal;
17
20
  }>): Promise<ArtifactSourceResultV3>;
@@ -69,7 +72,16 @@ export type ConnectionDiagnosticV3 = Readonly<{
69
72
  }>;
70
73
  /** Produces a stable diagnostic value without retaining a Session or transport detail. */
71
74
  export declare function connectionDiagnosticV3(snapshot: ConnectionControllerSnapshotV3): ConnectionDiagnosticV3;
75
+ export interface RotationDriver<Session> {
76
+ accept(artifact: ArtifactV3): void;
77
+ publish(session: Session): Promise<void>;
78
+ canServe(session: Session): boolean;
79
+ slot(): Promise<void>;
80
+ stop(): Promise<void>;
81
+ }
72
82
  export type ConnectionControllerOptionsV3 = Readonly<{
83
+ keyExchange?: KeyExchangeMode;
84
+ connectTimeoutMs?: number;
73
85
  tunnelIngressURL?: string;
74
86
  maximumAttempts?: number;
75
87
  clock?: ControllerClockV3;
@@ -86,7 +98,7 @@ export declare class ConnectionControllerV3Error extends Error {
86
98
  }
87
99
  export declare class ConnectionControllerV3<Session extends ManagedSessionV3 = ManagedSessionV3> {
88
100
  #private;
89
- constructor(source: ArtifactSourceV3, connector: LeaseConnectorV3<Session>, options: ConnectionControllerOptionsV3);
101
+ constructor(source: ArtifactSourceV3, connector: LeaseConnectorV3<Session>, options: ConnectionControllerOptionsV3, rotation?: RotationDriver<Session>);
90
102
  get state(): ConnectionControllerStateV3;
91
103
  get currentSession(): Session | undefined;
92
104
  get failure(): ConnectionControllerFailureV3 | undefined;
@@ -98,4 +110,4 @@ export declare class ConnectionControllerV3<Session extends ManagedSessionV3 = M
98
110
  }>): Promise<Session>;
99
111
  close(): Promise<void>;
100
112
  }
101
- export declare function createConnectionControllerV3<Session extends ManagedSessionV3>(source: ArtifactSourceV3, connector: LeaseConnectorV3<Session>, options: ConnectionControllerOptionsV3): ConnectionControllerV3<Session>;
113
+ export declare function createConnectionControllerV3<Session extends ManagedSessionV3>(source: ArtifactSourceV3, connector: LeaseConnectorV3<Session>, options: ConnectionControllerOptionsV3, rotation?: RotationDriver<Session>): ConnectionControllerV3<Session>;