@openchamber/web 1.14.0 → 1.15.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.
- package/bin/cli.test.js +8 -0
- package/bin/lib/DOCUMENTATION.md +3 -0
- package/bin/lib/cli-args.js +10 -0
- package/bin/lib/commands-connect-url.js +169 -22
- package/dist/assets/{DiagramView-CIaZCGUT.js → DiagramView-HR2xZxJ8.js} +1 -1
- package/dist/assets/MarkdownRendererImpl-sm01Q7R7.js +11 -0
- package/dist/assets/{MultiRunWindow-Bsbr3i8y.js → MultiRunWindow-DlFUaNQK.js} +1 -1
- package/dist/assets/OnboardingScreen-DVRz82mJ.js +1 -0
- package/dist/assets/{SessionSwitcherDropdown-CLm6tF5n.js → SessionSwitcherDropdown-DvoOalMY.js} +1 -1
- package/dist/assets/SettingsWindow-CkInZcj7.js +1 -0
- package/dist/assets/{TerminalView-DF7-jlam.js → TerminalView-D_9BAjtv.js} +3 -3
- package/dist/assets/es-cjUissX4.js +15 -0
- package/dist/assets/fr-DqbXnTdK.js +13 -0
- package/dist/assets/index-Dg0nmvQU.css +1 -0
- package/dist/assets/index-ltrmNGAf.js +2 -0
- package/dist/assets/ja-Cetyq6Eg.js +15 -0
- package/dist/assets/ko-DtunWisP.js +15 -0
- package/dist/assets/main-BC0m8s1v.js +236 -0
- package/dist/assets/main-DzNa8r0N.js +2 -0
- package/dist/assets/miniChat-BRYDE-Se.js +2 -0
- package/dist/assets/mobile-C3QJVA97.js +2 -0
- package/dist/assets/pl-DctGLgve.js +15 -0
- package/dist/assets/pt-BR-CXPhf78S.js +15 -0
- package/dist/assets/renderElectronMiniChatApp-CVFbDC5A.js +2 -0
- package/dist/assets/renderMobileApp-wkBjh5WR.js +16 -0
- package/dist/assets/runtimeEndpointReset-C42u8PvG.js +167 -0
- package/dist/assets/uk-Bdrc-kqF.js +15 -0
- package/dist/assets/{useAppFontEffects-DTO332li.js → useAppFontEffects-DzBg8hrC.js} +132 -116
- package/dist/assets/{vendor-.bun-C6iL44L9.js → vendor-.bun-BE757pvp.js} +1 -1
- package/dist/assets/zh-CN-CeCVKrHP.js +15 -0
- package/dist/assets/zh-TW-CoqiQING.js +15 -0
- package/dist/index.html +4 -4
- package/dist/mini-chat.html +4 -4
- package/dist/mobile.html +4 -4
- package/package.json +2 -2
- package/server/index.js +157 -6
- package/server/lib/client-auth/pairing.js +308 -0
- package/server/lib/client-auth/pairing.test.js +142 -0
- package/server/lib/client-auth/remote-clients.js +65 -3
- package/server/lib/notifications/apns-runtime.js +9 -16
- package/server/lib/opencode/bootstrap-runtime.js +10 -0
- package/server/lib/opencode/core-routes.js +247 -6
- package/server/lib/opencode/core-routes.test.js +248 -9
- package/server/lib/opencode/feature-routes-runtime.js +10 -0
- package/server/lib/opencode/models-metadata.js +61 -0
- package/server/lib/opencode/network-runtime.js +14 -1
- package/server/lib/opencode/network-runtime.test.js +29 -9
- package/server/lib/opencode/openchamber-routes.js +7 -40
- package/server/lib/opencode/proxy.js +47 -6
- package/server/lib/opencode/settings-helpers.js +16 -0
- package/server/lib/opencode/settings-helpers.test.js +11 -0
- package/server/lib/opencode/settings-normalization-runtime.js +2 -0
- package/server/lib/opencode/shutdown-runtime.js +2 -0
- package/server/lib/package-manager.js +18 -5
- package/server/lib/relay/DOCUMENTATION.md +78 -0
- package/server/lib/relay/cross-compat.test.js +135 -0
- package/server/lib/relay/e2ee.js +341 -0
- package/server/lib/relay/e2ee.test.js +124 -0
- package/server/lib/relay/host-client.js +389 -0
- package/server/lib/relay/host-client.test.js +280 -0
- package/server/lib/relay/identity.js +73 -0
- package/server/lib/relay/identity.test.js +78 -0
- package/server/lib/relay/service.js +247 -0
- package/server/lib/relay/signing-key.js +50 -0
- package/server/lib/relay/tunnel-codec.js +373 -0
- package/server/lib/relay/tunnel-codec.test.js +58 -0
- package/server/lib/relay/tunnel-host.js +462 -0
- package/server/lib/session-assist/DOCUMENTATION.md +66 -0
- package/server/lib/session-assist/runtime.js +394 -0
- package/server/lib/small-model/DOCUMENTATION.md +78 -0
- package/server/lib/small-model/call.js +380 -0
- package/server/lib/small-model/catalog.js +13 -0
- package/server/lib/small-model/index.js +167 -0
- package/server/lib/small-model/resolve.js +131 -0
- package/server/lib/small-model/resolve.test.js +197 -0
- package/server/lib/small-model/routes.js +44 -0
- package/server/lib/ui-auth/DOCUMENTATION.md +6 -0
- package/server/lib/ui-auth/ui-auth.js +10 -0
- package/server/opencode-proxy.test.js +38 -0
- package/dist/assets/MarkdownRendererImpl-DWgOeIpO.js +0 -7
- package/dist/assets/OnboardingScreen-3IAKkM5k.js +0 -1
- package/dist/assets/SettingsWindow-K-ErjehP.js +0 -1
- package/dist/assets/es-DQIFCjC2.js +0 -15
- package/dist/assets/fr-CJRav0LS.js +0 -13
- package/dist/assets/index-5eUDDB6w.js +0 -2
- package/dist/assets/index-CgpE7F8T.css +0 -1
- package/dist/assets/ja-CjAaqvHO.js +0 -15
- package/dist/assets/ko-BEbFxvrd.js +0 -15
- package/dist/assets/main-C2put7uQ.js +0 -2
- package/dist/assets/main-DuQEoz31.js +0 -214
- package/dist/assets/miniChat-o3AAQ-8r.js +0 -2
- package/dist/assets/mobile-X0421aWz.js +0 -2
- package/dist/assets/pl-D4kzai5T.js +0 -15
- package/dist/assets/pt-BR-9Sk3esQZ.js +0 -15
- package/dist/assets/renderElectronMiniChatApp-CdDj0ms1.js +0 -2
- package/dist/assets/renderMobileApp-cZ_HZKSe.js +0 -16
- package/dist/assets/runtimeEndpointReset-BYLVW9e7.js +0 -167
- package/dist/assets/uk-DW_dTfoO.js +0 -15
- package/dist/assets/zh-CN-r2_euV7U.js +0 -15
- package/dist/assets/zh-TW-BdPx3NWh.js +0 -15
|
@@ -0,0 +1,373 @@
|
|
|
1
|
+
// Tunnel mux frame codec (Layer 3 of the protocol spec). Pure functions, no I/O.
|
|
2
|
+
// JS mirror of packages/ui/src/lib/relay/tunnel-codec.ts (+ the Layer 3
|
|
3
|
+
// constants from protocol.ts) — MUST stay byte-compatible with those modules.
|
|
4
|
+
// Frame layout: [1 byte frameType (high bit = fragment-continues)][4 byte BE streamId][payload].
|
|
5
|
+
// Client-initiated streams use odd streamIds starting at 1; even ids are reserved.
|
|
6
|
+
// Spec: .opencode/plans/private-relay/01-protocol-spec.md (Layer 3).
|
|
7
|
+
|
|
8
|
+
import { MAX_PLAINTEXT_FRAME_BYTES } from './e2ee.js';
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
export const TUNNEL_FRAME_HEADER_BYTES = 5;
|
|
12
|
+
export const TUNNEL_FRAGMENT_FLAG = 0x80;
|
|
13
|
+
|
|
14
|
+
// Batch envelope container (mirror of protocol.ts). Only used when both peers
|
|
15
|
+
// negotiated `batch`. Reserve the per-frame envelope overhead from the payload
|
|
16
|
+
// budget so any single frame still fits one 64 KiB encrypted plaintext.
|
|
17
|
+
export const BATCH_CONTAINER_TAG_SINGLE = 0x00;
|
|
18
|
+
export const BATCH_CONTAINER_TAG_BATCH = 0x01;
|
|
19
|
+
export const BATCH_FRAME_LENGTH_BYTES = 4;
|
|
20
|
+
export const BATCH_ENVELOPE_RESERVED_BYTES = 1 + BATCH_FRAME_LENGTH_BYTES;
|
|
21
|
+
export const MAX_TUNNEL_PAYLOAD_BYTES =
|
|
22
|
+
MAX_PLAINTEXT_FRAME_BYTES - TUNNEL_FRAME_HEADER_BYTES - BATCH_ENVELOPE_RESERVED_BYTES;
|
|
23
|
+
|
|
24
|
+
export const TunnelFrameType = {
|
|
25
|
+
HttpRequest: 1,
|
|
26
|
+
HttpBody: 2,
|
|
27
|
+
HttpResponse: 3,
|
|
28
|
+
StreamEnd: 4,
|
|
29
|
+
StreamAbort: 5,
|
|
30
|
+
WsOpen: 6,
|
|
31
|
+
WsOpened: 7,
|
|
32
|
+
WsText: 8,
|
|
33
|
+
WsBinary: 9,
|
|
34
|
+
WsClose: 10,
|
|
35
|
+
Ping: 11,
|
|
36
|
+
Pong: 12,
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const TUNNEL_FRAME_TYPE_VALUES = new Set(Object.values(TunnelFrameType));
|
|
40
|
+
|
|
41
|
+
/** @param {number} value */
|
|
42
|
+
export const isTunnelFrameType = (value) => TUNNEL_FRAME_TYPE_VALUES.has(value);
|
|
43
|
+
|
|
44
|
+
const MAX_STREAM_ID = 0xffffffff;
|
|
45
|
+
|
|
46
|
+
export class TunnelCodecError extends Error {
|
|
47
|
+
constructor(message) {
|
|
48
|
+
super(message);
|
|
49
|
+
this.name = 'TunnelCodecError';
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* @param {number} frameType
|
|
55
|
+
* @param {number} streamId
|
|
56
|
+
* @param {Uint8Array} payload
|
|
57
|
+
* @param {boolean} [hasMoreFragments]
|
|
58
|
+
*/
|
|
59
|
+
export const encodeTunnelFrame = (frameType, streamId, payload, hasMoreFragments = false) => {
|
|
60
|
+
if (!Number.isInteger(streamId) || streamId < 0 || streamId > MAX_STREAM_ID) {
|
|
61
|
+
throw new TunnelCodecError('invalid stream id');
|
|
62
|
+
}
|
|
63
|
+
if (payload.length > MAX_TUNNEL_PAYLOAD_BYTES) {
|
|
64
|
+
throw new TunnelCodecError('tunnel payload exceeds maximum size');
|
|
65
|
+
}
|
|
66
|
+
const frame = new Uint8Array(TUNNEL_FRAME_HEADER_BYTES + payload.length);
|
|
67
|
+
frame[0] = hasMoreFragments ? frameType | TUNNEL_FRAGMENT_FLAG : frameType;
|
|
68
|
+
frame[1] = (streamId >>> 24) & 0xff;
|
|
69
|
+
frame[2] = (streamId >>> 16) & 0xff;
|
|
70
|
+
frame[3] = (streamId >>> 8) & 0xff;
|
|
71
|
+
frame[4] = streamId & 0xff;
|
|
72
|
+
frame.set(payload, TUNNEL_FRAME_HEADER_BYTES);
|
|
73
|
+
return frame;
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* @param {Uint8Array} frame
|
|
78
|
+
* @returns {{ frameType: number, streamId: number, payload: Uint8Array, hasMoreFragments: boolean }}
|
|
79
|
+
*/
|
|
80
|
+
export const decodeTunnelFrame = (frame) => {
|
|
81
|
+
if (frame.length < TUNNEL_FRAME_HEADER_BYTES) {
|
|
82
|
+
throw new TunnelCodecError('tunnel frame too short');
|
|
83
|
+
}
|
|
84
|
+
const rawType = frame[0];
|
|
85
|
+
const hasMoreFragments = (rawType & TUNNEL_FRAGMENT_FLAG) !== 0;
|
|
86
|
+
const frameType = rawType & ~TUNNEL_FRAGMENT_FLAG;
|
|
87
|
+
if (!isTunnelFrameType(frameType)) {
|
|
88
|
+
throw new TunnelCodecError(`unknown tunnel frame type ${frameType}`);
|
|
89
|
+
}
|
|
90
|
+
const streamId = ((frame[1] << 24) | (frame[2] << 16) | (frame[3] << 8) | frame[4]) >>> 0;
|
|
91
|
+
return {
|
|
92
|
+
frameType,
|
|
93
|
+
streamId,
|
|
94
|
+
payload: frame.slice(TUNNEL_FRAME_HEADER_BYTES),
|
|
95
|
+
hasMoreFragments,
|
|
96
|
+
};
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
const textEncoder = new TextEncoder();
|
|
100
|
+
const textDecoder = new TextDecoder();
|
|
101
|
+
|
|
102
|
+
/** @param {unknown} value */
|
|
103
|
+
export const encodeJsonPayload = (value) => textEncoder.encode(JSON.stringify(value));
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* @param {Uint8Array} payload
|
|
107
|
+
* @param {(parsed: unknown) => boolean} validate
|
|
108
|
+
*/
|
|
109
|
+
export const decodeJsonPayload = (payload, validate) => {
|
|
110
|
+
let parsed;
|
|
111
|
+
try {
|
|
112
|
+
parsed = JSON.parse(textDecoder.decode(payload));
|
|
113
|
+
} catch {
|
|
114
|
+
throw new TunnelCodecError('malformed JSON tunnel payload');
|
|
115
|
+
}
|
|
116
|
+
if (!validate(parsed)) {
|
|
117
|
+
throw new TunnelCodecError('unexpected JSON tunnel payload shape');
|
|
118
|
+
}
|
|
119
|
+
return parsed;
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Split a body/message into payload-sized chunks. Empty input yields one empty chunk.
|
|
124
|
+
* @param {Uint8Array} bytes
|
|
125
|
+
* @param {number} [chunkSize]
|
|
126
|
+
*/
|
|
127
|
+
export const chunkPayload = (bytes, chunkSize = MAX_TUNNEL_PAYLOAD_BYTES) => {
|
|
128
|
+
if (chunkSize <= 0 || chunkSize > MAX_TUNNEL_PAYLOAD_BYTES) {
|
|
129
|
+
throw new TunnelCodecError('invalid chunk size');
|
|
130
|
+
}
|
|
131
|
+
if (bytes.length === 0) return [new Uint8Array(0)];
|
|
132
|
+
const chunks = [];
|
|
133
|
+
for (let offset = 0; offset < bytes.length; offset += chunkSize) {
|
|
134
|
+
chunks.push(bytes.slice(offset, offset + chunkSize));
|
|
135
|
+
}
|
|
136
|
+
return chunks;
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Encode one logical message as one or more frames, setting the fragment flag
|
|
141
|
+
* on all but the last. Used for WS messages that exceed the frame budget.
|
|
142
|
+
* @param {number} frameType
|
|
143
|
+
* @param {number} streamId
|
|
144
|
+
* @param {Uint8Array} payload
|
|
145
|
+
*/
|
|
146
|
+
export const encodeFragmentedMessage = (frameType, streamId, payload) => {
|
|
147
|
+
const chunks = chunkPayload(payload);
|
|
148
|
+
return chunks.map((chunk, index) => encodeTunnelFrame(frameType, streamId, chunk, index < chunks.length - 1));
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Reassembles fragmented messages per (streamId, frameType). Bounded to protect memory.
|
|
153
|
+
* @param {number} [maxMessageBytes]
|
|
154
|
+
*/
|
|
155
|
+
export const createFragmentAssembler = (maxMessageBytes = 16 * 1024 * 1024) => {
|
|
156
|
+
const pending = new Map();
|
|
157
|
+
return {
|
|
158
|
+
/**
|
|
159
|
+
* Returns the complete message payload once all fragments arrived, or null
|
|
160
|
+
* while more fragments are expected.
|
|
161
|
+
* @param {{ frameType: number, streamId: number, payload: Uint8Array, hasMoreFragments: boolean }} frame
|
|
162
|
+
*/
|
|
163
|
+
push(frame) {
|
|
164
|
+
const key = `${frame.streamId}:${frame.frameType}`;
|
|
165
|
+
const entry = pending.get(key);
|
|
166
|
+
if (!frame.hasMoreFragments && !entry) {
|
|
167
|
+
return frame.payload;
|
|
168
|
+
}
|
|
169
|
+
const chunks = entry?.chunks ?? [];
|
|
170
|
+
const totalBytes = (entry?.totalBytes ?? 0) + frame.payload.length;
|
|
171
|
+
if (totalBytes > maxMessageBytes) {
|
|
172
|
+
pending.delete(key);
|
|
173
|
+
throw new TunnelCodecError('fragmented message exceeds maximum size');
|
|
174
|
+
}
|
|
175
|
+
chunks.push(frame.payload);
|
|
176
|
+
if (frame.hasMoreFragments) {
|
|
177
|
+
pending.set(key, { chunks, totalBytes });
|
|
178
|
+
return null;
|
|
179
|
+
}
|
|
180
|
+
pending.delete(key);
|
|
181
|
+
const message = new Uint8Array(totalBytes);
|
|
182
|
+
let offset = 0;
|
|
183
|
+
for (const chunk of chunks) {
|
|
184
|
+
message.set(chunk, offset);
|
|
185
|
+
offset += chunk.length;
|
|
186
|
+
}
|
|
187
|
+
return message;
|
|
188
|
+
},
|
|
189
|
+
/** @param {number} streamId */
|
|
190
|
+
dropStream(streamId) {
|
|
191
|
+
for (const key of pending.keys()) {
|
|
192
|
+
if (key.startsWith(`${streamId}:`)) pending.delete(key);
|
|
193
|
+
}
|
|
194
|
+
},
|
|
195
|
+
};
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Batch envelope encoder (mirror of tunnel-codec.ts encodeFrameBatch). Only used
|
|
200
|
+
* when both peers negotiated `batch`. One encrypted WS message still equals one
|
|
201
|
+
* encrypt() call — this only changes how many tunnel frames it carries.
|
|
202
|
+
* @param {Uint8Array[]} frames
|
|
203
|
+
* @returns {Uint8Array}
|
|
204
|
+
*/
|
|
205
|
+
export const encodeFrameBatch = (frames) => {
|
|
206
|
+
if (frames.length === 0) {
|
|
207
|
+
throw new TunnelCodecError('cannot encode an empty frame batch');
|
|
208
|
+
}
|
|
209
|
+
if (frames.length === 1) {
|
|
210
|
+
const frame = frames[0];
|
|
211
|
+
const out = new Uint8Array(1 + frame.length);
|
|
212
|
+
out[0] = BATCH_CONTAINER_TAG_SINGLE;
|
|
213
|
+
out.set(frame, 1);
|
|
214
|
+
if (out.length > MAX_PLAINTEXT_FRAME_BYTES) {
|
|
215
|
+
throw new TunnelCodecError('frame batch exceeds maximum plaintext size');
|
|
216
|
+
}
|
|
217
|
+
return out;
|
|
218
|
+
}
|
|
219
|
+
let total = 1;
|
|
220
|
+
for (const frame of frames) total += BATCH_FRAME_LENGTH_BYTES + frame.length;
|
|
221
|
+
if (total > MAX_PLAINTEXT_FRAME_BYTES) {
|
|
222
|
+
throw new TunnelCodecError('frame batch exceeds maximum plaintext size');
|
|
223
|
+
}
|
|
224
|
+
const out = new Uint8Array(total);
|
|
225
|
+
out[0] = BATCH_CONTAINER_TAG_BATCH;
|
|
226
|
+
let offset = 1;
|
|
227
|
+
for (const frame of frames) {
|
|
228
|
+
out[offset] = (frame.length >>> 24) & 0xff;
|
|
229
|
+
out[offset + 1] = (frame.length >>> 16) & 0xff;
|
|
230
|
+
out[offset + 2] = (frame.length >>> 8) & 0xff;
|
|
231
|
+
out[offset + 3] = frame.length & 0xff;
|
|
232
|
+
offset += BATCH_FRAME_LENGTH_BYTES;
|
|
233
|
+
out.set(frame, offset);
|
|
234
|
+
offset += frame.length;
|
|
235
|
+
}
|
|
236
|
+
return out;
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Decodes a batch-envelope plaintext into its ordered tunnel frames.
|
|
241
|
+
* @param {Uint8Array} plaintext
|
|
242
|
+
* @returns {Uint8Array[]}
|
|
243
|
+
*/
|
|
244
|
+
export const decodeFrameBatch = (plaintext) => {
|
|
245
|
+
if (plaintext.length < 1) {
|
|
246
|
+
throw new TunnelCodecError('empty batch plaintext');
|
|
247
|
+
}
|
|
248
|
+
const tag = plaintext[0];
|
|
249
|
+
if (tag === BATCH_CONTAINER_TAG_SINGLE) {
|
|
250
|
+
return [plaintext.slice(1)];
|
|
251
|
+
}
|
|
252
|
+
if (tag !== BATCH_CONTAINER_TAG_BATCH) {
|
|
253
|
+
throw new TunnelCodecError(`unknown batch container tag ${tag}`);
|
|
254
|
+
}
|
|
255
|
+
const frames = [];
|
|
256
|
+
let offset = 1;
|
|
257
|
+
while (offset < plaintext.length) {
|
|
258
|
+
if (offset + BATCH_FRAME_LENGTH_BYTES > plaintext.length) {
|
|
259
|
+
throw new TunnelCodecError('truncated batch frame length');
|
|
260
|
+
}
|
|
261
|
+
const length =
|
|
262
|
+
((plaintext[offset] << 24)
|
|
263
|
+
| (plaintext[offset + 1] << 16)
|
|
264
|
+
| (plaintext[offset + 2] << 8)
|
|
265
|
+
| plaintext[offset + 3]) >>> 0;
|
|
266
|
+
offset += BATCH_FRAME_LENGTH_BYTES;
|
|
267
|
+
if (offset + length > plaintext.length) {
|
|
268
|
+
throw new TunnelCodecError('truncated batch frame body');
|
|
269
|
+
}
|
|
270
|
+
frames.push(plaintext.slice(offset, offset + length));
|
|
271
|
+
offset += length;
|
|
272
|
+
}
|
|
273
|
+
if (frames.length === 0) {
|
|
274
|
+
throw new TunnelCodecError('empty frame batch');
|
|
275
|
+
}
|
|
276
|
+
return frames;
|
|
277
|
+
};
|
|
278
|
+
|
|
279
|
+
// Only high-volume body/stream data is buffered; setup/teardown/keepalive frames
|
|
280
|
+
// flush immediately so TTFT, terminal echo, and liveness stay snappy.
|
|
281
|
+
const BUFFERED_FRAME_TYPES = new Set([
|
|
282
|
+
TunnelFrameType.HttpBody,
|
|
283
|
+
TunnelFrameType.WsText,
|
|
284
|
+
TunnelFrameType.WsBinary,
|
|
285
|
+
]);
|
|
286
|
+
|
|
287
|
+
// See the TS mirror (tunnel-codec.ts) for the 150ms rationale: the chat render pipeline's
|
|
288
|
+
// 100ms input throttle + ~64ms paced-reveal smoothing make a 150ms batch window invisible.
|
|
289
|
+
export const DEFAULT_BATCH_WINDOW_MS = 150;
|
|
290
|
+
export const DEFAULT_BATCH_MAX_BYTES = 24 * 1024;
|
|
291
|
+
export const DEFAULT_BATCH_MAX_FRAMES = 32;
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* Outbound batching buffer (mirror of tunnel-codec.ts createOutboundFrameBatcher).
|
|
295
|
+
* @param {{
|
|
296
|
+
* windowMs?: number,
|
|
297
|
+
* maxBatchBytes?: number,
|
|
298
|
+
* maxBatchFrames?: number,
|
|
299
|
+
* sendBatch: (plaintext: Uint8Array) => void,
|
|
300
|
+
* now?: () => number,
|
|
301
|
+
* setTimer?: (fn: () => void, ms: number) => any,
|
|
302
|
+
* clearTimer?: (handle: any) => void,
|
|
303
|
+
* }} options
|
|
304
|
+
*/
|
|
305
|
+
export const createOutboundFrameBatcher = (options) => {
|
|
306
|
+
const windowMs = options.windowMs ?? DEFAULT_BATCH_WINDOW_MS;
|
|
307
|
+
const maxBatchBytes = options.maxBatchBytes ?? DEFAULT_BATCH_MAX_BYTES;
|
|
308
|
+
const maxBatchFrames = options.maxBatchFrames ?? DEFAULT_BATCH_MAX_FRAMES;
|
|
309
|
+
const now = options.now ?? (() => Date.now());
|
|
310
|
+
const setTimer = options.setTimer ?? ((fn, ms) => setTimeout(fn, ms));
|
|
311
|
+
const clearTimer = options.clearTimer ?? ((handle) => clearTimeout(handle));
|
|
312
|
+
|
|
313
|
+
let buffer = [];
|
|
314
|
+
let bufferedBytes = 0;
|
|
315
|
+
let timer = null;
|
|
316
|
+
let lastFlushAt = 0;
|
|
317
|
+
let disposed = false;
|
|
318
|
+
|
|
319
|
+
const clearPendingTimer = () => {
|
|
320
|
+
if (timer !== null) {
|
|
321
|
+
clearTimer(timer);
|
|
322
|
+
timer = null;
|
|
323
|
+
}
|
|
324
|
+
};
|
|
325
|
+
|
|
326
|
+
const flush = () => {
|
|
327
|
+
clearPendingTimer();
|
|
328
|
+
if (buffer.length === 0) return;
|
|
329
|
+
const frames = buffer;
|
|
330
|
+
buffer = [];
|
|
331
|
+
bufferedBytes = 0;
|
|
332
|
+
lastFlushAt = now();
|
|
333
|
+
options.sendBatch(encodeFrameBatch(frames));
|
|
334
|
+
};
|
|
335
|
+
|
|
336
|
+
const enqueue = (frame) => {
|
|
337
|
+
if (disposed) return;
|
|
338
|
+
const frameType = frame[0] & ~TUNNEL_FRAGMENT_FLAG;
|
|
339
|
+
if (!BUFFERED_FRAME_TYPES.has(frameType)) {
|
|
340
|
+
buffer.push(frame);
|
|
341
|
+
flush();
|
|
342
|
+
return;
|
|
343
|
+
}
|
|
344
|
+
const at = now();
|
|
345
|
+
if (buffer.length === 0 && at - lastFlushAt >= windowMs) {
|
|
346
|
+
buffer.push(frame);
|
|
347
|
+
flush();
|
|
348
|
+
return;
|
|
349
|
+
}
|
|
350
|
+
const frameCost = BATCH_FRAME_LENGTH_BYTES + frame.length;
|
|
351
|
+
if (buffer.length > 0 && 1 + bufferedBytes + frameCost > MAX_PLAINTEXT_FRAME_BYTES) {
|
|
352
|
+
flush();
|
|
353
|
+
}
|
|
354
|
+
buffer.push(frame);
|
|
355
|
+
bufferedBytes += frameCost;
|
|
356
|
+
if (bufferedBytes >= maxBatchBytes || buffer.length >= maxBatchFrames) {
|
|
357
|
+
flush();
|
|
358
|
+
return;
|
|
359
|
+
}
|
|
360
|
+
if (timer === null) timer = setTimer(flush, windowMs);
|
|
361
|
+
};
|
|
362
|
+
|
|
363
|
+
return {
|
|
364
|
+
enqueue,
|
|
365
|
+
flush,
|
|
366
|
+
dispose() {
|
|
367
|
+
disposed = true;
|
|
368
|
+
clearPendingTimer();
|
|
369
|
+
buffer = [];
|
|
370
|
+
bufferedBytes = 0;
|
|
371
|
+
},
|
|
372
|
+
};
|
|
373
|
+
};
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { describe, expect, it } from 'bun:test';
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
TunnelCodecError,
|
|
5
|
+
TunnelFrameType,
|
|
6
|
+
createFragmentAssembler,
|
|
7
|
+
decodeTunnelFrame,
|
|
8
|
+
encodeFragmentedMessage,
|
|
9
|
+
encodeTunnelFrame,
|
|
10
|
+
MAX_TUNNEL_PAYLOAD_BYTES,
|
|
11
|
+
} from './tunnel-codec.js';
|
|
12
|
+
|
|
13
|
+
describe('relay tunnel codec', () => {
|
|
14
|
+
it('round-trips a frame', () => {
|
|
15
|
+
const payload = new TextEncoder().encode('hello tunnel');
|
|
16
|
+
const frame = encodeTunnelFrame(TunnelFrameType.HttpRequest, 7, payload);
|
|
17
|
+
const decoded = decodeTunnelFrame(frame);
|
|
18
|
+
expect(decoded.frameType).toBe(TunnelFrameType.HttpRequest);
|
|
19
|
+
expect(decoded.streamId).toBe(7);
|
|
20
|
+
expect(decoded.hasMoreFragments).toBe(false);
|
|
21
|
+
expect(new TextDecoder().decode(decoded.payload)).toBe('hello tunnel');
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it('preserves large stream ids without sign issues', () => {
|
|
25
|
+
const frame = encodeTunnelFrame(TunnelFrameType.HttpBody, 0xfffffffd, new Uint8Array(0));
|
|
26
|
+
expect(decodeTunnelFrame(frame).streamId).toBe(0xfffffffd);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it('rejects truncated and unknown frames', () => {
|
|
30
|
+
expect(() => decodeTunnelFrame(new Uint8Array([1, 2]))).toThrow(TunnelCodecError);
|
|
31
|
+
expect(() => decodeTunnelFrame(new Uint8Array([99, 0, 0, 0, 1]))).toThrow(TunnelCodecError);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it('fragments and reassembles oversized messages', () => {
|
|
35
|
+
const big = new Uint8Array(MAX_TUNNEL_PAYLOAD_BYTES * 2 + 10);
|
|
36
|
+
for (let i = 0; i < big.length; i += 1) big[i] = i & 0xff;
|
|
37
|
+
const frames = encodeFragmentedMessage(TunnelFrameType.WsBinary, 3, big);
|
|
38
|
+
expect(frames.length).toBe(3);
|
|
39
|
+
|
|
40
|
+
const assembler = createFragmentAssembler();
|
|
41
|
+
let result = null;
|
|
42
|
+
for (const frame of frames) {
|
|
43
|
+
result = assembler.push(decodeTunnelFrame(frame));
|
|
44
|
+
}
|
|
45
|
+
expect(result).not.toBeNull();
|
|
46
|
+
expect(Array.from(result)).toEqual(Array.from(big));
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it('bounds fragment reassembly memory', () => {
|
|
50
|
+
const assembler = createFragmentAssembler(MAX_TUNNEL_PAYLOAD_BYTES + 1);
|
|
51
|
+
const chunk = new Uint8Array(MAX_TUNNEL_PAYLOAD_BYTES);
|
|
52
|
+
// First fragment fits, second pushes past the cap.
|
|
53
|
+
assembler.push({ frameType: TunnelFrameType.WsText, streamId: 1, payload: chunk, hasMoreFragments: true });
|
|
54
|
+
expect(() =>
|
|
55
|
+
assembler.push({ frameType: TunnelFrameType.WsText, streamId: 1, payload: chunk, hasMoreFragments: true }),
|
|
56
|
+
).toThrow(TunnelCodecError);
|
|
57
|
+
});
|
|
58
|
+
});
|