@floegence/flowersec-core 2.5.3 → 3.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 (119) hide show
  1. package/README.md +46 -18
  2. package/dist/browser/connectSessionV3.d.ts +13 -0
  3. package/dist/browser/connectSessionV3.js +75 -0
  4. package/dist/browser/index.d.ts +3 -2
  5. package/dist/browser/index.js +2 -1
  6. package/dist/browser/v2.d.ts +3 -0
  7. package/dist/browser/v2.js +2 -0
  8. package/dist/facade.d.ts +15 -8
  9. package/dist/facade.js +5 -4
  10. package/dist/framing/jsonframe.d.ts +1 -1
  11. package/dist/framing/jsonframe.js +3 -1
  12. package/dist/interop/proxyServerPeer.js +1 -1
  13. package/dist/interop/serverParityPeer.js +8 -1
  14. package/dist/node/acceptor.d.ts +10 -0
  15. package/dist/node/acceptor.js +53 -7
  16. package/dist/node/acceptorV3.d.ts +57 -0
  17. package/dist/node/acceptorV3.js +227 -0
  18. package/dist/node/connectSessionV3.d.ts +21 -0
  19. package/dist/node/connectSessionV3.js +73 -0
  20. package/dist/node/index.d.ts +15 -10
  21. package/dist/node/index.js +6 -5
  22. package/dist/node/nativeTransportAddon.d.ts +39 -1
  23. package/dist/node/nativeTransportAddon.js +29 -5
  24. package/dist/node/rawQuicAdapterV3.d.ts +10 -0
  25. package/dist/node/rawQuicAdapterV3.js +76 -0
  26. package/dist/node/rawQuicServerV3.d.ts +24 -0
  27. package/dist/node/rawQuicServerV3.js +34 -0
  28. package/dist/node/runtimeAuthorizationV3.d.ts +29 -0
  29. package/dist/node/runtimeAuthorizationV3.js +126 -0
  30. package/dist/node/tunnelRuntimeV3.d.ts +57 -0
  31. package/dist/node/tunnelRuntimeV3.js +857 -0
  32. package/dist/node/v2.d.ts +11 -0
  33. package/dist/node/v2.js +6 -0
  34. package/dist/node/webSocketServerV3.d.ts +27 -0
  35. package/dist/node/webSocketServerV3.js +219 -0
  36. package/dist/proxy/runtime.js +39 -6
  37. package/dist/proxy/serviceWorker.d.ts +2 -0
  38. package/dist/proxy/serviceWorker.js +63 -7
  39. package/dist/proxy/windowBridge.d.ts +1 -0
  40. package/dist/proxy/windowBridge.js +93 -33
  41. package/dist/proxy/wsPatch.d.ts +1 -0
  42. package/dist/proxy/wsPatch.js +39 -6
  43. package/dist/public/streamHandlers.d.ts +3 -0
  44. package/dist/public/streamHandlers.js +6 -1
  45. package/dist/rpc/client.js +9 -7
  46. package/dist/rpc/server.js +44 -20
  47. package/dist/rpc/validate.d.ts +1 -0
  48. package/dist/rpc/validate.js +18 -6
  49. package/dist/v2/index.d.ts +14 -0
  50. package/dist/v2/index.js +7 -0
  51. package/dist/v2/protocol.js +23 -32
  52. package/dist/v2/publicSession.js +4 -0
  53. package/dist/v2/session.d.ts +5 -0
  54. package/dist/v2/session.js +94 -35
  55. package/dist/v3/admissionError.d.ts +4 -0
  56. package/dist/v3/admissionError.js +8 -0
  57. package/dist/v3/artifact.d.ts +145 -0
  58. package/dist/v3/artifact.js +1162 -0
  59. package/dist/v3/artifactLease.d.ts +17 -0
  60. package/dist/v3/artifactLease.js +109 -0
  61. package/dist/v3/browserRuntime.d.ts +37 -0
  62. package/dist/v3/browserRuntime.js +197 -0
  63. package/dist/v3/browserSessionRuntime.d.ts +2 -0
  64. package/dist/v3/browserSessionRuntime.js +21 -0
  65. package/dist/v3/capability.d.ts +32 -0
  66. package/dist/v3/capability.js +345 -0
  67. package/dist/v3/carrier.d.ts +84 -0
  68. package/dist/v3/carrier.js +411 -0
  69. package/dist/v3/connectionController.d.ts +87 -0
  70. package/dist/v3/connectionController.js +805 -0
  71. package/dist/v3/contract.d.ts +65 -0
  72. package/dist/v3/contract.js +3 -0
  73. package/dist/v3/controller.d.ts +43 -0
  74. package/dist/v3/controller.js +303 -0
  75. package/dist/v3/handshake.d.ts +81 -0
  76. package/dist/v3/handshake.js +471 -0
  77. package/dist/v3/jcs.d.ts +6 -0
  78. package/dist/v3/jcs.js +69 -0
  79. package/dist/v3/jsonPreflight.d.ts +1 -0
  80. package/dist/v3/jsonPreflight.js +251 -0
  81. package/dist/v3/nodeRuntime.d.ts +40 -0
  82. package/dist/v3/nodeRuntime.js +315 -0
  83. package/dist/v3/nodeSessionRuntime.d.ts +2 -0
  84. package/dist/v3/nodeSessionRuntime.js +13 -0
  85. package/dist/v3/protocol.d.ts +99 -0
  86. package/dist/v3/protocol.js +773 -0
  87. package/dist/v3/publicApi.d.ts +12 -0
  88. package/dist/v3/publicApi.js +46 -0
  89. package/dist/v3/publicSession.d.ts +1 -0
  90. package/dist/v3/publicSession.js +281 -0
  91. package/dist/v3/retryDisposition.d.ts +16 -0
  92. package/dist/v3/retryDisposition.js +30 -0
  93. package/dist/v3/runtimeAdapters.d.ts +9 -0
  94. package/dist/v3/runtimeAdapters.js +109 -0
  95. package/dist/v3/security.d.ts +34 -0
  96. package/dist/v3/security.js +100 -0
  97. package/dist/v3/serverAdmission.d.ts +38 -0
  98. package/dist/v3/serverAdmission.js +223 -0
  99. package/dist/v3/session.d.ts +318 -0
  100. package/dist/v3/session.js +2289 -0
  101. package/dist/v3/sessionConnector.d.ts +37 -0
  102. package/dist/v3/sessionConnector.js +440 -0
  103. package/dist/v3/streamLifetimeLedger.d.ts +31 -0
  104. package/dist/v3/streamLifetimeLedger.js +131 -0
  105. package/dist/v3/streamMetadata.d.ts +1 -0
  106. package/dist/v3/streamMetadata.js +7 -0
  107. package/dist/v3/transportConstants.d.ts +56 -0
  108. package/dist/v3/transportConstants.js +59 -0
  109. package/dist/v3/unreliableMessage.d.ts +44 -0
  110. package/dist/v3/unreliableMessage.js +284 -0
  111. package/dist/v3/webSocketCarrier.d.ts +31 -0
  112. package/dist/v3/webSocketCarrier.js +370 -0
  113. package/dist/v3/webTransportCarrier.d.ts +27 -0
  114. package/dist/v3/webTransportCarrier.js +534 -0
  115. package/dist/yamux/stream.d.ts +3 -0
  116. package/dist/yamux/stream.js +17 -4
  117. package/package.json +4 -4
  118. package/sbom/cyclonedx.json +6 -6
  119. package/sbom/spdx.json +11 -11
@@ -0,0 +1,370 @@
1
+ import { YamuxSession } from "../yamux/session.js";
2
+ import { CarrierError } from "./carrier.js";
3
+ export function createWebSocketCarrierSessionV3(transport, options) {
4
+ return new WebSocketYamuxCarrierSession(transport, options);
5
+ }
6
+ export function createServerWebSocketCarrierSessionV3(transport, options) {
7
+ requireCapacity(options.inboundBidirectionalStreamCapacity);
8
+ const carrier = new ServerWebSocketAdmissionCarrier(transport, options);
9
+ serverAdmissionCarriers.set(carrier, carrier);
10
+ return carrier;
11
+ }
12
+ const serverAdmissionCarriers = new WeakMap();
13
+ export function configureServerWebSocketCarrierRoleV3(carrier, client) {
14
+ const server = serverAdmissionCarriers.get(carrier);
15
+ if (server === undefined)
16
+ throw new CarrierError("closed", "WebSocket carrier is not a server admission carrier");
17
+ server.configureRole(client);
18
+ }
19
+ class ServerWebSocketAdmissionCarrier {
20
+ transport;
21
+ options;
22
+ kind = "websocket";
23
+ path;
24
+ inboundBidirectionalStreamCapacity;
25
+ unreliableDatagrams = undefined;
26
+ admissionAvailable = true;
27
+ active;
28
+ closed = false;
29
+ client = false;
30
+ constructor(transport, options) {
31
+ this.transport = transport;
32
+ this.options = options;
33
+ this.path = options.path;
34
+ this.inboundBidirectionalStreamCapacity = options.inboundBidirectionalStreamCapacity;
35
+ }
36
+ async openStream(options = {}) {
37
+ return await this.activate().openStream(options);
38
+ }
39
+ async acceptStream(options = {}) {
40
+ throwIfAborted(options.signal);
41
+ if (this.closed)
42
+ throw new CarrierError("closed", "WebSocket carrier is closed");
43
+ if (this.admissionAvailable) {
44
+ this.admissionAvailable = false;
45
+ return new WebSocketAdmissionStream(this.transport);
46
+ }
47
+ return await this.activate().acceptStream(options);
48
+ }
49
+ async close() {
50
+ if (this.closed)
51
+ return;
52
+ this.closed = true;
53
+ if (this.active === undefined)
54
+ this.transport.close();
55
+ else
56
+ await this.active.close();
57
+ }
58
+ abort() {
59
+ if (this.closed)
60
+ return;
61
+ this.closed = true;
62
+ if (this.active === undefined)
63
+ this.transport.close();
64
+ else
65
+ this.active.abort();
66
+ }
67
+ async waitTermination() {
68
+ if (this.closed && this.active === undefined)
69
+ return;
70
+ await this.activate().waitTermination();
71
+ }
72
+ configureRole(client) {
73
+ if (this.closed || this.active !== undefined)
74
+ throw new CarrierError("closed", "WebSocket carrier is already active");
75
+ this.client = client;
76
+ }
77
+ activate() {
78
+ if (this.closed)
79
+ throw new CarrierError("closed", "WebSocket carrier is closed");
80
+ if (this.admissionAvailable) {
81
+ throw new CarrierError("closed", "WebSocket admission has not completed");
82
+ }
83
+ this.active ??= createWebSocketCarrierSessionV3(this.transport, {
84
+ path: this.options.path,
85
+ client: this.client,
86
+ inboundBidirectionalStreamCapacity: this.options.inboundBidirectionalStreamCapacity,
87
+ ...(this.options.resourcePolicy === undefined ? {} : { resourcePolicy: this.options.resourcePolicy }),
88
+ });
89
+ return this.active;
90
+ }
91
+ }
92
+ class WebSocketAdmissionStream {
93
+ transport;
94
+ readComplete = false;
95
+ writeComplete = false;
96
+ outbound = [];
97
+ constructor(transport) {
98
+ this.transport = transport;
99
+ }
100
+ async read(options = {}) {
101
+ if (this.readComplete)
102
+ return null;
103
+ this.readComplete = true;
104
+ return await this.transport.readBinary(options);
105
+ }
106
+ async write(data, options = {}) {
107
+ throwIfAborted(options.signal);
108
+ if (this.writeComplete)
109
+ throw new CarrierError("write_closed", "admission response is closed");
110
+ this.outbound.push(data.slice());
111
+ return data.length;
112
+ }
113
+ async closeWrite() {
114
+ if (this.writeComplete)
115
+ return;
116
+ this.writeComplete = true;
117
+ const length = this.outbound.reduce((total, chunk) => total + chunk.length, 0);
118
+ const response = new Uint8Array(length);
119
+ let offset = 0;
120
+ for (const chunk of this.outbound) {
121
+ response.set(chunk, offset);
122
+ offset += chunk.length;
123
+ }
124
+ this.outbound.length = 0;
125
+ await this.transport.writeBinary(response);
126
+ }
127
+ async stopSending() {
128
+ this.transport.close();
129
+ }
130
+ async reset() {
131
+ this.transport.close();
132
+ }
133
+ abort() {
134
+ this.transport.close();
135
+ }
136
+ }
137
+ class WebSocketYamuxCarrierSession {
138
+ transport;
139
+ kind = "websocket";
140
+ path;
141
+ inboundBidirectionalStreamCapacity;
142
+ unreliableDatagrams = undefined;
143
+ incoming = new IncomingStreamQueue();
144
+ yamux;
145
+ termination = deferred();
146
+ terminalError;
147
+ closed = false;
148
+ constructor(transport, options) {
149
+ this.transport = transport;
150
+ requireCapacity(options.inboundBidirectionalStreamCapacity);
151
+ this.path = options.path;
152
+ this.inboundBidirectionalStreamCapacity = options.inboundBidirectionalStreamCapacity;
153
+ const duplex = {
154
+ read: async () => await transport.readBinary(),
155
+ write: async (chunk) => await transport.writeBinary(chunk),
156
+ close: () => transport.close(),
157
+ };
158
+ const policy = options.resourcePolicy ?? {};
159
+ this.yamux = new YamuxSession(duplex, {
160
+ client: options.client,
161
+ limits: {
162
+ maxActiveStreams: Math.max(policy.maxConcurrentStreams ?? this.inboundBidirectionalStreamCapacity, this.inboundBidirectionalStreamCapacity),
163
+ maxInboundStreams: this.inboundBidirectionalStreamCapacity,
164
+ ...(policy.maxFrameBytes === undefined ? {} : { maxFrameBytes: policy.maxFrameBytes }),
165
+ ...(policy.preferredWriteBytes === undefined ? {} : { preferredOutboundFrameBytes: policy.preferredWriteBytes }),
166
+ ...(policy.maxStreamWriteQueueBytes === undefined ? {} : { maxStreamWriteQueueBytes: policy.maxStreamWriteQueueBytes }),
167
+ ...(policy.maxStreamReceiveBytes === undefined ? {} : { maxStreamReceiveBytes: policy.maxStreamReceiveBytes }),
168
+ ...(policy.maxSessionReceiveBytes === undefined ? {} : { maxSessionReceiveBytes: policy.maxSessionReceiveBytes }),
169
+ },
170
+ onIncomingStream: (stream) => this.incoming.push(new YamuxCarrierStreamAdapter(stream)),
171
+ onTerminal: (error) => {
172
+ const failure = error instanceof CarrierError
173
+ ? error
174
+ : new CarrierError("closed", "WebSocket carrier terminated", error);
175
+ this.closed = true;
176
+ this.terminalError = failure;
177
+ this.incoming.fail(failure);
178
+ this.termination.resolve();
179
+ },
180
+ });
181
+ }
182
+ async openStream(options = {}) {
183
+ this.assertOpen();
184
+ try {
185
+ return new YamuxCarrierStreamAdapter(await this.yamux.openStream(options));
186
+ }
187
+ catch (error) {
188
+ throw error instanceof CarrierError ? error : new CarrierError("closed", "WebSocket stream open failed", error);
189
+ }
190
+ }
191
+ async acceptStream(options = {}) {
192
+ this.assertOpen();
193
+ return await this.incoming.shift(options.signal);
194
+ }
195
+ async close() {
196
+ if (this.closed)
197
+ return;
198
+ await this.transport.flush().catch(() => undefined);
199
+ this.closeLocally();
200
+ }
201
+ abort() {
202
+ this.closeLocally();
203
+ }
204
+ async waitTermination() {
205
+ await this.termination.promise;
206
+ if (this.terminalError !== undefined)
207
+ throw this.terminalError;
208
+ }
209
+ assertOpen() {
210
+ if (this.terminalError !== undefined)
211
+ throw this.terminalError;
212
+ if (this.closed)
213
+ throw new CarrierError("closed", "WebSocket carrier is closed");
214
+ }
215
+ closeLocally() {
216
+ if (this.closed)
217
+ return;
218
+ this.closed = true;
219
+ const failure = new CarrierError("closed", "WebSocket carrier is closed");
220
+ this.incoming.fail(failure);
221
+ this.yamux.close();
222
+ this.termination.resolve();
223
+ }
224
+ }
225
+ class YamuxCarrierStreamAdapter {
226
+ stream;
227
+ constructor(stream) {
228
+ this.stream = stream;
229
+ }
230
+ async read(options = {}) {
231
+ try {
232
+ return await abortable(this.stream.read(), options.signal, () => this.stream.abort());
233
+ }
234
+ catch (error) {
235
+ throw error instanceof CarrierError ? error : new CarrierError("reset", "WebSocket stream read failed", error);
236
+ }
237
+ }
238
+ async write(data, options = {}) {
239
+ throwIfAborted(options.signal);
240
+ try {
241
+ await abortable(this.stream.write(data), options.signal, () => this.stream.abort());
242
+ }
243
+ catch (error) {
244
+ throw error instanceof CarrierError ? error : new CarrierError("reset", "WebSocket stream write failed", error);
245
+ }
246
+ return data.length;
247
+ }
248
+ async closeWrite() {
249
+ await this.stream.close();
250
+ }
251
+ async reset() {
252
+ await this.stream.reset();
253
+ }
254
+ async stopSending() {
255
+ throw new CarrierError("stop_sending_unavailable", "WebSocket/Yamux cannot stop only the receive side of a bidirectional stream");
256
+ }
257
+ abort(error) {
258
+ this.stream.abort(error);
259
+ }
260
+ }
261
+ class IncomingStreamQueue {
262
+ values = [];
263
+ waiters = new Set();
264
+ terminalError;
265
+ push(value) {
266
+ if (this.terminalError !== undefined) {
267
+ value.abort(this.terminalError);
268
+ return;
269
+ }
270
+ const waiter = this.waiters.values().next().value;
271
+ if (waiter === undefined)
272
+ this.values.push(value);
273
+ else {
274
+ this.waiters.delete(waiter);
275
+ waiter.deliver(value);
276
+ }
277
+ }
278
+ shift(signal) {
279
+ throwIfAborted(signal);
280
+ if (this.terminalError !== undefined)
281
+ return Promise.reject(this.terminalError);
282
+ const value = this.values.shift();
283
+ if (value !== undefined)
284
+ return Promise.resolve(value);
285
+ return new Promise((resolve, reject) => {
286
+ let settled = false;
287
+ const cleanup = () => {
288
+ this.waiters.delete(waiter);
289
+ signal?.removeEventListener("abort", abort);
290
+ };
291
+ const waiter = {
292
+ deliver: (stream) => {
293
+ if (settled)
294
+ return;
295
+ settled = true;
296
+ cleanup();
297
+ resolve(stream);
298
+ },
299
+ fail: (error) => {
300
+ if (settled)
301
+ return;
302
+ settled = true;
303
+ cleanup();
304
+ reject(error);
305
+ },
306
+ };
307
+ const abort = () => waiter.fail(new CarrierError("aborted", "carrier operation aborted"));
308
+ this.waiters.add(waiter);
309
+ signal?.addEventListener("abort", abort, { once: true });
310
+ if (signal?.aborted === true)
311
+ abort();
312
+ });
313
+ }
314
+ fail(error) {
315
+ if (this.terminalError !== undefined)
316
+ return;
317
+ this.terminalError = error;
318
+ for (const stream of this.values)
319
+ stream.abort(error);
320
+ this.values.length = 0;
321
+ for (const waiter of this.waiters)
322
+ waiter.fail(error);
323
+ this.waiters.clear();
324
+ }
325
+ }
326
+ function deferred() {
327
+ let resolve;
328
+ const promise = new Promise((resolvePromise) => { resolve = resolvePromise; });
329
+ return { promise, resolve };
330
+ }
331
+ function requireCapacity(value) {
332
+ if (!Number.isInteger(value) || value < 3 || value > 130) {
333
+ throw new RangeError("inboundBidirectionalStreamCapacity must be an integer from 3 to 130");
334
+ }
335
+ }
336
+ function throwIfAborted(signal) {
337
+ if (signal?.aborted === true)
338
+ throw new CarrierError("aborted", "carrier operation aborted");
339
+ }
340
+ async function abortable(promise, signal, onAbort) {
341
+ if (signal === undefined)
342
+ return await promise;
343
+ throwIfAborted(signal);
344
+ return await new Promise((resolve, reject) => {
345
+ let settled = false;
346
+ const cleanup = () => signal.removeEventListener("abort", abort);
347
+ const abort = () => {
348
+ if (settled)
349
+ return;
350
+ settled = true;
351
+ cleanup();
352
+ onAbort();
353
+ reject(new CarrierError("aborted", "carrier operation aborted"));
354
+ };
355
+ signal.addEventListener("abort", abort, { once: true });
356
+ void promise.then((value) => {
357
+ if (settled)
358
+ return;
359
+ settled = true;
360
+ cleanup();
361
+ resolve(value);
362
+ }, (error) => {
363
+ if (settled)
364
+ return;
365
+ settled = true;
366
+ cleanup();
367
+ reject(error);
368
+ });
369
+ });
370
+ }
@@ -0,0 +1,27 @@
1
+ import { type NativeCarrierSessionV3 } from "./carrier.js";
2
+ import type { PathKind } from "./contract.js";
3
+ export type WebTransportBidirectionalLikeV3 = Readonly<{
4
+ readable: ReadableStream<Uint8Array>;
5
+ writable: WritableStream<Uint8Array>;
6
+ }>;
7
+ export type WebTransportDatagramsLikeV3 = Readonly<{
8
+ readable: ReadableStream<Uint8Array>;
9
+ writable: WritableStream<Uint8Array>;
10
+ maxDatagramSize?: number;
11
+ }>;
12
+ export type WebTransportSessionLikeV3 = Readonly<{
13
+ closed: Promise<unknown>;
14
+ incomingBidirectionalStreams: ReadableStream<WebTransportBidirectionalLikeV3>;
15
+ datagrams: WebTransportDatagramsLikeV3;
16
+ createBidirectionalStream(): Promise<WebTransportBidirectionalLikeV3>;
17
+ close(info?: Readonly<{
18
+ closeCode: number;
19
+ reason: string;
20
+ }>): void;
21
+ }>;
22
+ export declare function adaptWebTransportCarrierSessionV3(native: WebTransportSessionLikeV3, options: Readonly<{
23
+ path: PathKind;
24
+ inboundBidirectionalStreamCapacity: number;
25
+ datagramMaxSize?: number;
26
+ }>): NativeCarrierSessionV3;
27
+ export declare function hasWebTransportConstructorSurfaceV3(value: unknown): boolean;