@enclave-vm/stream 2.19.1 → 2.19.2

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/index.js CHANGED
@@ -23,11 +23,13 @@ var index_exports = {};
23
23
  __export(index_exports, {
24
24
  AesGcmError: () => AesGcmError,
25
25
  ConnectionState: () => ConnectionState,
26
+ DEFAULT_MAX_LINE_LENGTH: () => DEFAULT_MAX_LINE_LENGTH,
26
27
  DEFAULT_RECONNECTION_CONFIG: () => DEFAULT_RECONNECTION_CONFIG,
27
28
  EcdhError: () => EcdhError,
28
29
  EventBuffer: () => EventBuffer,
29
30
  HeartbeatMonitor: () => HeartbeatMonitor,
30
31
  HkdfError: () => HkdfError,
32
+ NdjsonLineTooLongError: () => NdjsonLineTooLongError,
31
33
  NdjsonStreamParser: () => NdjsonStreamParser,
32
34
  ReconnectionStateMachine: () => ReconnectionStateMachine,
33
35
  SequenceTracker: () => SequenceTracker,
@@ -43,6 +45,7 @@ __export(index_exports, {
43
45
  deriveSessionCryptoKeys: () => deriveSessionCryptoKeys,
44
46
  deriveSessionKeys: () => deriveSessionKeys,
45
47
  deriveSharedSecret: () => deriveSharedSecret,
48
+ encodeEnvelopeHeader: () => encodeEnvelopeHeader,
46
49
  encrypt: () => encrypt,
47
50
  encryptJson: () => encryptJson,
48
51
  exportPublicKey: () => exportPublicKey,
@@ -52,6 +55,7 @@ __export(index_exports, {
52
55
  generateNonce: () => generateNonce,
53
56
  importAesGcmKey: () => importAesGcmKey,
54
57
  importPublicKey: () => importPublicKey,
58
+ openEncryptedEnvelope: () => openEncryptedEnvelope,
55
59
  parseLine: () => parseLine,
56
60
  parseLines: () => parseLines,
57
61
  parseNdjsonStream: () => parseNdjsonStream,
@@ -66,6 +70,81 @@ __reExport(index_exports, require("@enclave-vm/types"), module.exports);
66
70
 
67
71
  // libs/stream/src/ndjson.ts
68
72
  var import_types = require("@enclave-vm/types");
73
+ var DEFAULT_MAX_LINE_LENGTH = 8 * 1024 * 1024;
74
+ var NdjsonLineTooLongError = class extends Error {
75
+ maxLineLength;
76
+ constructor(maxLineLength) {
77
+ super(`NDJSON line exceeds the maximum line length of ${maxLineLength} characters`);
78
+ this.name = "NdjsonLineTooLongError";
79
+ this.maxLineLength = maxLineLength;
80
+ }
81
+ };
82
+ function resolveMaxLineLength(value) {
83
+ if (value === void 0) return DEFAULT_MAX_LINE_LENGTH;
84
+ if (typeof value !== "number" || !Number.isSafeInteger(value) || value < 1) {
85
+ throw new RangeError("maxLineLength must be a positive integer");
86
+ }
87
+ return value;
88
+ }
89
+ var LineAssembler = class {
90
+ constructor(maxLineLength) {
91
+ this.maxLineLength = maxLineLength;
92
+ }
93
+ pieces = [];
94
+ length = 0;
95
+ skipping = false;
96
+ /**
97
+ * Add a chunk.
98
+ *
99
+ * @param onLine - Called with every line the chunk completes (without its line end)
100
+ * @param onTooLong - Called when a line passes the limit, with the start of that line.
101
+ * It may throw; the assembler is then already past the line.
102
+ * @param onSkipped - Called when the end of a dropped line is reached
103
+ */
104
+ push(chunk, onLine, onTooLong, onSkipped) {
105
+ let start = 0;
106
+ for (; ; ) {
107
+ const newline = chunk.indexOf("\n", start);
108
+ const end = newline === -1 ? chunk.length : newline;
109
+ if (!this.skipping) {
110
+ if (this.length + (end - start) > this.maxLineLength) {
111
+ const head = (this.pieces.length > 0 ? this.pieces[0] : chunk.slice(start, start + 100)).slice(0, 100);
112
+ this.pieces = [];
113
+ this.length = 0;
114
+ this.skipping = true;
115
+ onTooLong(head);
116
+ } else if (end > start) {
117
+ this.pieces.push(chunk.slice(start, end));
118
+ this.length += end - start;
119
+ }
120
+ }
121
+ if (newline === -1) return;
122
+ if (this.skipping) {
123
+ this.skipping = false;
124
+ onSkipped?.();
125
+ } else {
126
+ onLine(this.take());
127
+ }
128
+ start = newline + 1;
129
+ }
130
+ }
131
+ /**
132
+ * Remove and return the unfinished line ('' when there is none, or when it is being dropped).
133
+ */
134
+ take() {
135
+ const line = this.pieces.length === 1 ? this.pieces[0] : this.pieces.join("");
136
+ this.pieces = [];
137
+ this.length = 0;
138
+ this.skipping = false;
139
+ return line;
140
+ }
141
+ /**
142
+ * Whether an unfinished line with something other than whitespace is held.
143
+ */
144
+ hasContent() {
145
+ return this.pieces.some((piece) => piece.trim().length > 0);
146
+ }
147
+ };
69
148
  function serializeEvent(event) {
70
149
  return JSON.stringify(event);
71
150
  }
@@ -120,65 +199,57 @@ function parseLines(data) {
120
199
  return { events, errors };
121
200
  }
122
201
  var NdjsonStreamParser = class {
123
- buffer = "";
202
+ lines;
203
+ maxLineLength;
124
204
  onEvent;
125
205
  onError;
126
206
  lineNumber = 0;
127
207
  constructor(options) {
128
208
  this.onEvent = options.onEvent;
129
209
  this.onError = options.onError;
210
+ this.maxLineLength = resolveMaxLineLength(options.maxLineLength);
211
+ this.lines = new LineAssembler(this.maxLineLength);
130
212
  }
131
213
  /**
132
214
  * Feed a chunk of data into the parser.
133
215
  * Complete lines are parsed and emitted immediately.
216
+ * A line longer than `maxLineLength` is reported through `onError` once and skipped.
134
217
  */
135
218
  feed(chunk) {
136
- this.buffer += chunk;
137
- let newlineIndex;
138
- while ((newlineIndex = this.buffer.indexOf("\n")) !== -1) {
139
- const line = this.buffer.slice(0, newlineIndex);
140
- this.buffer = this.buffer.slice(newlineIndex + 1);
141
- this.lineNumber++;
142
- const trimmed = line.trim();
143
- if (trimmed.length === 0) continue;
144
- const result = parseLine(trimmed);
145
- if (result.success) {
146
- this.onEvent(result.data);
147
- } else {
219
+ this.lines.push(
220
+ chunk,
221
+ (line) => {
222
+ this.lineNumber++;
223
+ this.parse(line);
224
+ },
225
+ (head) => {
148
226
  this.onError({
149
- line: this.lineNumber,
150
- error: result.error,
151
- content: trimmed.length > 100 ? trimmed.slice(0, 100) + "..." : trimmed
227
+ line: this.lineNumber + 1,
228
+ error: `Line exceeds the maximum line length of ${this.maxLineLength} characters`,
229
+ content: head + "..."
152
230
  });
231
+ },
232
+ () => {
233
+ this.lineNumber++;
153
234
  }
154
- }
235
+ );
155
236
  }
156
237
  /**
157
238
  * Flush any remaining buffered data.
158
239
  * Should be called when the stream ends.
159
240
  */
160
241
  flush() {
161
- const trimmed = this.buffer.trim();
242
+ const trimmed = this.lines.take().trim();
162
243
  if (trimmed.length > 0) {
163
244
  this.lineNumber++;
164
- const result = parseLine(trimmed);
165
- if (result.success) {
166
- this.onEvent(result.data);
167
- } else {
168
- this.onError({
169
- line: this.lineNumber,
170
- error: result.error,
171
- content: trimmed.length > 100 ? trimmed.slice(0, 100) + "..." : trimmed
172
- });
173
- }
245
+ this.parse(trimmed);
174
246
  }
175
- this.buffer = "";
176
247
  }
177
248
  /**
178
249
  * Reset the parser state.
179
250
  */
180
251
  reset() {
181
- this.buffer = "";
252
+ this.lines.take();
182
253
  this.lineNumber = 0;
183
254
  }
184
255
  /**
@@ -191,34 +262,46 @@ var NdjsonStreamParser = class {
191
262
  * Check if there's pending data in the buffer.
192
263
  */
193
264
  hasPendingData() {
194
- return this.buffer.trim().length > 0;
265
+ return this.lines.hasContent();
266
+ }
267
+ parse(line) {
268
+ const trimmed = line.trim();
269
+ if (trimmed.length === 0) return;
270
+ const result = parseLine(trimmed);
271
+ if (result.success) {
272
+ this.onEvent(result.data);
273
+ } else {
274
+ this.onError({
275
+ line: this.lineNumber,
276
+ error: result.error,
277
+ content: trimmed.length > 100 ? trimmed.slice(0, 100) + "..." : trimmed
278
+ });
279
+ }
195
280
  }
196
281
  };
197
- function createNdjsonParseStream() {
198
- let buffer = "";
282
+ function createNdjsonParseStream(options = {}) {
283
+ const maxLineLength = resolveMaxLineLength(options.maxLineLength);
284
+ const lines = new LineAssembler(maxLineLength);
285
+ const enqueueLine = (line, controller) => {
286
+ const trimmed = line.trim();
287
+ if (trimmed.length === 0) return;
288
+ const result = parseLine(trimmed);
289
+ if (result.success) {
290
+ controller.enqueue(result.data);
291
+ }
292
+ };
199
293
  return new TransformStream({
200
294
  transform(chunk, controller) {
201
- buffer += chunk;
202
- let newlineIndex;
203
- while ((newlineIndex = buffer.indexOf("\n")) !== -1) {
204
- const line = buffer.slice(0, newlineIndex);
205
- buffer = buffer.slice(newlineIndex + 1);
206
- const trimmed = line.trim();
207
- if (trimmed.length === 0) continue;
208
- const result = parseLine(trimmed);
209
- if (result.success) {
210
- controller.enqueue(result.data);
295
+ lines.push(
296
+ chunk,
297
+ (line) => enqueueLine(line, controller),
298
+ () => {
299
+ throw new NdjsonLineTooLongError(maxLineLength);
211
300
  }
212
- }
301
+ );
213
302
  },
214
303
  flush(controller) {
215
- const trimmed = buffer.trim();
216
- if (trimmed.length > 0) {
217
- const result = parseLine(trimmed);
218
- if (result.success) {
219
- controller.enqueue(result.data);
220
- }
221
- }
304
+ enqueueLine(lines.take(), controller);
222
305
  }
223
306
  });
224
307
  }
@@ -229,15 +312,19 @@ function createNdjsonSerializeStream() {
229
312
  }
230
313
  });
231
314
  }
232
- async function* parseNdjsonStream(stream) {
315
+ function parseNdjsonStream(stream, options = {}) {
316
+ return readNdjsonStream(stream, resolveMaxLineLength(options.maxLineLength));
317
+ }
318
+ async function* readNdjsonStream(stream, maxLineLength) {
233
319
  const decoder = new TextDecoder();
234
320
  const reader = stream.getReader();
235
- let buffer = "";
321
+ const lines = new LineAssembler(maxLineLength);
322
+ const completed = [];
236
323
  try {
237
324
  while (true) {
238
325
  const { done, value } = await reader.read();
239
326
  if (done) {
240
- const trimmed = buffer.trim();
327
+ const trimmed = lines.take().trim();
241
328
  if (trimmed.length > 0) {
242
329
  const result = parseLine(trimmed);
243
330
  if (result.success) {
@@ -246,11 +333,20 @@ async function* parseNdjsonStream(stream) {
246
333
  }
247
334
  break;
248
335
  }
249
- buffer += decoder.decode(value, { stream: true });
250
- let newlineIndex;
251
- while ((newlineIndex = buffer.indexOf("\n")) !== -1) {
252
- const line = buffer.slice(0, newlineIndex);
253
- buffer = buffer.slice(newlineIndex + 1);
336
+ let tooLong;
337
+ try {
338
+ lines.push(
339
+ decoder.decode(value, { stream: true }),
340
+ (line) => completed.push(line),
341
+ () => {
342
+ throw new NdjsonLineTooLongError(maxLineLength);
343
+ }
344
+ );
345
+ } catch (error) {
346
+ if (!(error instanceof NdjsonLineTooLongError)) throw error;
347
+ tooLong = error;
348
+ }
349
+ for (const line of completed.splice(0)) {
254
350
  const trimmed = line.trim();
255
351
  if (trimmed.length === 0) continue;
256
352
  const result = parseLine(trimmed);
@@ -258,6 +354,10 @@ async function* parseNdjsonStream(stream) {
258
354
  yield result.data;
259
355
  }
260
356
  }
357
+ if (tooLong) {
358
+ await reader.cancel(tooLong).catch(() => void 0);
359
+ throw tooLong;
360
+ }
261
361
  }
262
362
  } finally {
263
363
  reader.releaseLock();
@@ -287,8 +387,9 @@ async function generateKeyPair(curve = import_types2.SupportedCurve.P256) {
287
387
  name: "ECDH",
288
388
  namedCurve: curve
289
389
  },
290
- true,
291
- // extractable for export
390
+ // The private key stays inside Web Crypto. A public key can be exported whatever this
391
+ // flag says, which is all the handshake needs.
392
+ false,
292
393
  ["deriveBits"]
293
394
  );
294
395
  return {
@@ -377,7 +478,20 @@ async function createServerHello(keyPair, keyId) {
377
478
  keyId
378
479
  };
379
480
  }
481
+ var HANDSHAKE_PROTOCOL_VERSIONS = [import_types2.PROTOCOL_VERSION, import_types2.PROTOCOL_VERSION_V1];
482
+ function assertHandshakeParameters(hello) {
483
+ if (!HANDSHAKE_PROTOCOL_VERSIONS.includes(hello.protocolVersion)) {
484
+ throw new EcdhError("Unsupported protocol version in handshake", import_types2.EncryptionErrorCode.HandshakeFailed);
485
+ }
486
+ if (!Object.values(import_types2.SupportedCurve).includes(hello.curve)) {
487
+ throw new EcdhError("Unsupported curve in handshake", import_types2.EncryptionErrorCode.UnsupportedAlgorithm);
488
+ }
489
+ }
380
490
  async function processClientHello(clientHello) {
491
+ assertHandshakeParameters(clientHello);
492
+ if (!Array.isArray(clientHello.supportedAlgorithms) || !clientHello.supportedAlgorithms.includes(import_types2.EncryptionAlgorithm.AES_GCM_256)) {
493
+ throw new EcdhError("No supported encryption algorithm in client hello", import_types2.EncryptionErrorCode.UnsupportedAlgorithm);
494
+ }
381
495
  const serverKeyPair = await generateKeyPair(clientHello.curve);
382
496
  const peerPublicKey = await importPublicKey(clientHello.clientEphemeralPubKeyB64, clientHello.curve);
383
497
  const keyId = `k_${crypto.randomUUID()}`;
@@ -390,6 +504,16 @@ async function processClientHello(clientHello) {
390
504
  };
391
505
  }
392
506
  async function processServerHello(serverHello) {
507
+ assertHandshakeParameters(serverHello);
508
+ if (serverHello.selectedAlgorithm !== import_types2.EncryptionAlgorithm.AES_GCM_256) {
509
+ throw new EcdhError("Unsupported encryption algorithm in server hello", import_types2.EncryptionErrorCode.UnsupportedAlgorithm);
510
+ }
511
+ if (serverHello.kdf !== import_types2.KeyDerivation.HKDF_SHA256) {
512
+ throw new EcdhError("Unsupported key derivation in server hello", import_types2.EncryptionErrorCode.UnsupportedAlgorithm);
513
+ }
514
+ if (typeof serverHello.keyId !== "string" || serverHello.keyId.length === 0) {
515
+ throw new EcdhError("Missing key id in server hello", import_types2.EncryptionErrorCode.HandshakeFailed);
516
+ }
393
517
  const peerPublicKey = await importPublicKey(serverHello.serverEphemeralPubKeyB64, serverHello.curve);
394
518
  return {
395
519
  peerPublicKey,
@@ -516,18 +640,54 @@ function generateCounterNonce(prefix, counter) {
516
640
  }
517
641
  const nonce = new Uint8Array(import_types4.AES_GCM_NONCE_SIZE);
518
642
  nonce.set(prefix);
519
- const counterValue = Number(counter & 0xffffffffn);
643
+ if (typeof counter !== "bigint" || counter < 0n || counter > 0xffffffffn) {
644
+ throw new AesGcmError("Nonce counter must be between 0 and 2^32 - 1", import_types4.EncryptionErrorCode.NonceReuse);
645
+ }
646
+ const counterValue = Number(counter);
520
647
  nonce[8] = counterValue >> 24 & 255;
521
648
  nonce[9] = counterValue >> 16 & 255;
522
649
  nonce[10] = counterValue >> 8 & 255;
523
650
  nonce[11] = counterValue & 255;
524
651
  return nonce;
525
652
  }
653
+ var BASE64_CHUNK_SIZE = 32768;
526
654
  function toBase64(bytes) {
527
- return btoa(String.fromCharCode(...bytes));
655
+ let binary = "";
656
+ for (let offset = 0; offset < bytes.length; offset += BASE64_CHUNK_SIZE) {
657
+ binary += String.fromCharCode(...bytes.subarray(offset, offset + BASE64_CHUNK_SIZE));
658
+ }
659
+ return btoa(binary);
528
660
  }
529
661
  function fromBase64(base64) {
530
- return Uint8Array.from(atob(base64), (c) => c.charCodeAt(0));
662
+ let binary;
663
+ try {
664
+ binary = atob(base64);
665
+ } catch {
666
+ throw new AesGcmError("Invalid base64 input", import_types4.EncryptionErrorCode.DecryptionFailed);
667
+ }
668
+ const bytes = new Uint8Array(binary.length);
669
+ for (let i = 0; i < binary.length; i++) {
670
+ bytes[i] = binary.charCodeAt(i);
671
+ }
672
+ return bytes;
673
+ }
674
+ function encodeEnvelopeHeader(header) {
675
+ const { protocolVersion, sessionId, seq, kid } = header;
676
+ if (!Number.isSafeInteger(protocolVersion) || typeof sessionId !== "string" || !Number.isSafeInteger(seq) || seq < 0 || typeof kid !== "string") {
677
+ throw new AesGcmError("Invalid envelope header", import_types4.EncryptionErrorCode.DecryptionFailed);
678
+ }
679
+ return new TextEncoder().encode(JSON.stringify(["enclavejs-envelope", protocolVersion, sessionId, seq, kid]));
680
+ }
681
+ function receivedEnvelopeHeader(envelope) {
682
+ if (typeof envelope !== "object" || envelope === null || typeof envelope.payload !== "object" || !envelope.payload) {
683
+ throw new AesGcmError("Invalid envelope", import_types4.EncryptionErrorCode.DecryptionFailed);
684
+ }
685
+ return encodeEnvelopeHeader({
686
+ protocolVersion: envelope.protocolVersion,
687
+ sessionId: envelope.sessionId,
688
+ seq: envelope.seq,
689
+ kid: envelope.payload.kid
690
+ });
531
691
  }
532
692
  async function encrypt(key, plaintext, nonce, additionalData) {
533
693
  const subtle = getSubtle3();
@@ -585,25 +745,26 @@ async function decrypt(key, ciphertext, nonce, additionalData) {
585
745
  );
586
746
  }
587
747
  }
588
- async function encryptJson(key, keyId, data, nonce) {
748
+ async function encryptJson(key, keyId, data, nonce, additionalData) {
589
749
  const actualNonce = nonce ?? generateNonce();
590
750
  const plaintext = new TextEncoder().encode(JSON.stringify(data));
591
- const ciphertext = await encrypt(key, plaintext, actualNonce);
751
+ const ciphertext = await encrypt(key, plaintext, actualNonce, additionalData);
592
752
  return {
593
753
  kid: keyId,
594
754
  nonceB64: toBase64(actualNonce),
595
755
  ciphertextB64: toBase64(ciphertext)
596
756
  };
597
757
  }
598
- async function decryptJson(key, payload) {
758
+ async function decryptJson(key, payload, additionalData) {
599
759
  const nonce = fromBase64(payload.nonceB64);
600
760
  const ciphertext = fromBase64(payload.ciphertextB64);
601
- const plaintext = await decrypt(key, ciphertext, nonce);
761
+ const plaintext = await decrypt(key, ciphertext, nonce, additionalData);
602
762
  const json = new TextDecoder().decode(plaintext);
603
763
  return JSON.parse(json);
604
764
  }
605
765
  async function createEncryptedEnvelope(key, keyId, sessionId, seq, innerEvent, nonce) {
606
- const payload = await encryptJson(key, keyId, innerEvent, nonce);
766
+ const header = encodeEnvelopeHeader({ protocolVersion: import_types4.PROTOCOL_VERSION, sessionId, seq, kid: keyId });
767
+ const payload = await encryptJson(key, keyId, innerEvent, nonce, header);
607
768
  return {
608
769
  protocolVersion: import_types4.PROTOCOL_VERSION,
609
770
  sessionId,
@@ -612,6 +773,9 @@ async function createEncryptedEnvelope(key, keyId, sessionId, seq, innerEvent, n
612
773
  payload
613
774
  };
614
775
  }
776
+ async function openEncryptedEnvelope(key, envelope) {
777
+ return decryptJson(key, envelope.payload, receivedEnvelopeHeader(envelope));
778
+ }
615
779
  var SessionEncryptionContext = class _SessionEncryptionContext {
616
780
  key;
617
781
  keyInfo;
@@ -637,22 +801,27 @@ var SessionEncryptionContext = class _SessionEncryptionContext {
637
801
  }
638
802
  /**
639
803
  * Encrypt data and increment the nonce counter.
804
+ *
805
+ * @param additionalData - Optional additional authenticated data
640
806
  */
641
- async encrypt(plaintext) {
807
+ async encrypt(plaintext, additionalData) {
642
808
  if (this.needsRotation()) {
643
809
  throw new AesGcmError("Key needs rotation: nonce limit reached", import_types4.EncryptionErrorCode.KeyExpired);
644
810
  }
645
811
  const nonce = generateCounterNonce(this.noncePrefix, this.nonceCounter);
646
812
  this.nonceCounter++;
647
- const ciphertext = await encrypt(this.key, plaintext, nonce);
813
+ const ciphertext = await encrypt(this.key, plaintext, nonce, additionalData);
648
814
  return { ciphertext, nonce };
649
815
  }
650
816
  /**
651
817
  * Encrypt JSON data.
818
+ *
819
+ * @param additionalData - Optional additional authenticated data; the same bytes must be
820
+ * given to `decryptJson`
652
821
  */
653
- async encryptJson(data) {
822
+ async encryptJson(data, additionalData) {
654
823
  const plaintext = new TextEncoder().encode(JSON.stringify(data));
655
- const { ciphertext, nonce } = await this.encrypt(plaintext);
824
+ const { ciphertext, nonce } = await this.encrypt(plaintext, additionalData);
656
825
  return {
657
826
  kid: this.keyId,
658
827
  nonceB64: toBase64(nonce),
@@ -661,9 +830,13 @@ var SessionEncryptionContext = class _SessionEncryptionContext {
661
830
  }
662
831
  /**
663
832
  * Create an encrypted envelope.
833
+ *
834
+ * The envelope's header (protocol version, session id, sequence number, key id) is
835
+ * authenticated together with the event: read the envelope with `openEnvelope`.
664
836
  */
665
837
  async createEnvelope(sessionId, seq, innerEvent) {
666
- const payload = await this.encryptJson(innerEvent);
838
+ const header = encodeEnvelopeHeader({ protocolVersion: import_types4.PROTOCOL_VERSION, sessionId, seq, kid: this.keyId });
839
+ const payload = await this.encryptJson(innerEvent, header);
667
840
  return {
668
841
  protocolVersion: import_types4.PROTOCOL_VERSION,
669
842
  sessionId,
@@ -673,21 +846,37 @@ var SessionEncryptionContext = class _SessionEncryptionContext {
673
846
  };
674
847
  }
675
848
  /**
676
- * Decrypt an encrypted envelope payload.
849
+ * Decrypt a payload made by `encrypt` or `encryptJson`.
850
+ * The payload of an envelope is read with `openEnvelope`.
851
+ *
852
+ * @param additionalData - The additional authenticated data given when encrypting, if any
677
853
  */
678
- async decrypt(payload) {
854
+ async decrypt(payload, additionalData) {
679
855
  const nonce = fromBase64(payload.nonceB64);
680
856
  const ciphertext = fromBase64(payload.ciphertextB64);
681
- return decrypt(this.key, ciphertext, nonce);
857
+ return decrypt(this.key, ciphertext, nonce, additionalData);
682
858
  }
683
859
  /**
684
- * Decrypt and parse JSON from an encrypted envelope payload.
860
+ * Decrypt and parse JSON from a payload made by `encryptJson`.
861
+ * The payload of an envelope is read with `openEnvelope`.
862
+ *
863
+ * @param additionalData - The additional authenticated data given when encrypting, if any
685
864
  */
686
- async decryptJson(payload) {
687
- const plaintext = await this.decrypt(payload);
865
+ async decryptJson(payload, additionalData) {
866
+ const plaintext = await this.decrypt(payload, additionalData);
688
867
  const json = new TextDecoder().decode(plaintext);
689
868
  return JSON.parse(json);
690
869
  }
870
+ /**
871
+ * Decrypt an encrypted envelope and parse the event inside it.
872
+ *
873
+ * Fails when the ciphertext or any header field of the envelope (protocol version, session
874
+ * id, sequence number, key id) is not what the sender encrypted. Whether a sequence number
875
+ * was seen before is for the caller to check.
876
+ */
877
+ async openEnvelope(envelope) {
878
+ return this.decryptJson(envelope.payload, receivedEnvelopeHeader(envelope));
879
+ }
691
880
  /**
692
881
  * Get the current nonce counter value.
693
882
  */
@@ -696,6 +885,7 @@ var SessionEncryptionContext = class _SessionEncryptionContext {
696
885
  }
697
886
  /**
698
887
  * Create session key info for serialization.
888
+ * The result holds the raw key bytes: treat it as the key itself.
699
889
  */
700
890
  toKeyInfo() {
701
891
  return {
@@ -1060,11 +1250,13 @@ var HeartbeatMonitor = class {
1060
1250
  0 && (module.exports = {
1061
1251
  AesGcmError,
1062
1252
  ConnectionState,
1253
+ DEFAULT_MAX_LINE_LENGTH,
1063
1254
  DEFAULT_RECONNECTION_CONFIG,
1064
1255
  EcdhError,
1065
1256
  EventBuffer,
1066
1257
  HeartbeatMonitor,
1067
1258
  HkdfError,
1259
+ NdjsonLineTooLongError,
1068
1260
  NdjsonStreamParser,
1069
1261
  ReconnectionStateMachine,
1070
1262
  SequenceTracker,
@@ -1080,6 +1272,7 @@ var HeartbeatMonitor = class {
1080
1272
  deriveSessionCryptoKeys,
1081
1273
  deriveSessionKeys,
1082
1274
  deriveSharedSecret,
1275
+ encodeEnvelopeHeader,
1083
1276
  encrypt,
1084
1277
  encryptJson,
1085
1278
  exportPublicKey,
@@ -1089,6 +1282,7 @@ var HeartbeatMonitor = class {
1089
1282
  generateNonce,
1090
1283
  importAesGcmKey,
1091
1284
  importPublicKey,
1285
+ openEncryptedEnvelope,
1092
1286
  parseLine,
1093
1287
  parseLines,
1094
1288
  parseNdjsonStream,
package/ndjson.d.ts CHANGED
@@ -16,6 +16,30 @@ export type ParseResult<T> = {
16
16
  error: string;
17
17
  line: string;
18
18
  };
19
+ /**
20
+ * Default maximum length of one NDJSON line, in characters (8 MiB).
21
+ * Above the largest event the protocol's default limits allow (a 5 MB tool result).
22
+ */
23
+ export declare const DEFAULT_MAX_LINE_LENGTH: number;
24
+ /**
25
+ * Options of the incremental parsers.
26
+ */
27
+ export interface NdjsonParseOptions {
28
+ /**
29
+ * Maximum length of one line, in characters (UTF-16 code units; for ASCII JSON, bytes).
30
+ * A parser never buffers more than this of a line that has not ended yet.
31
+ * A positive integer.
32
+ * @default DEFAULT_MAX_LINE_LENGTH
33
+ */
34
+ maxLineLength?: number;
35
+ }
36
+ /**
37
+ * A line of the stream is longer than the parser's `maxLineLength`.
38
+ */
39
+ export declare class NdjsonLineTooLongError extends Error {
40
+ readonly maxLineLength: number;
41
+ constructor(maxLineLength: number);
42
+ }
19
43
  /**
20
44
  * Serialize an event to NDJSON format (single line).
21
45
  */
@@ -47,7 +71,8 @@ export declare function parseLines(data: string): {
47
71
  * Handles partial lines across chunks.
48
72
  */
49
73
  export declare class NdjsonStreamParser {
50
- private buffer;
74
+ private readonly lines;
75
+ private readonly maxLineLength;
51
76
  private readonly onEvent;
52
77
  private readonly onError;
53
78
  private lineNumber;
@@ -58,10 +83,11 @@ export declare class NdjsonStreamParser {
58
83
  error: string;
59
84
  content: string;
60
85
  }) => void;
61
- });
86
+ } & NdjsonParseOptions);
62
87
  /**
63
88
  * Feed a chunk of data into the parser.
64
89
  * Complete lines are parsed and emitted immediately.
90
+ * A line longer than `maxLineLength` is reported through `onError` once and skipped.
65
91
  */
66
92
  feed(chunk: string): void;
67
93
  /**
@@ -81,17 +107,21 @@ export declare class NdjsonStreamParser {
81
107
  * Check if there's pending data in the buffer.
82
108
  */
83
109
  hasPendingData(): boolean;
110
+ private parse;
84
111
  }
85
112
  /**
86
113
  * Create a transform stream that parses NDJSON.
87
114
  * Works with browser fetch() streaming and Node.js streams.
115
+ * A line longer than `maxLineLength` ends the stream with an `NdjsonLineTooLongError`.
88
116
  */
89
- export declare function createNdjsonParseStream(): TransformStream<string, ParsedStreamEvent | ParsedEncryptedEnvelope>;
117
+ export declare function createNdjsonParseStream(options?: NdjsonParseOptions): TransformStream<string, ParsedStreamEvent | ParsedEncryptedEnvelope>;
90
118
  /**
91
119
  * Create a transform stream that serializes events to NDJSON.
92
120
  */
93
121
  export declare function createNdjsonSerializeStream(): TransformStream<MaybeEncrypted<StreamEvent>, string>;
94
122
  /**
95
123
  * Async generator that parses NDJSON from a ReadableStream.
124
+ * A line longer than `maxLineLength` ends the iteration with an `NdjsonLineTooLongError`
125
+ * and cancels the stream.
96
126
  */
97
- export declare function parseNdjsonStream(stream: ReadableStream<Uint8Array>): AsyncGenerator<ParsedStreamEvent | ParsedEncryptedEnvelope>;
127
+ export declare function parseNdjsonStream(stream: ReadableStream<Uint8Array>, options?: NdjsonParseOptions): AsyncGenerator<ParsedStreamEvent | ParsedEncryptedEnvelope>;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@enclave-vm/stream",
3
- "version": "2.19.1",
4
- "description": "Streaming protocol implementation for EnclaveJS runtime (NDJSON, encryption, reconnection)",
3
+ "version": "2.19.2",
4
+ "description": "Streaming protocol building blocks for EnclaveJS (NDJSON, reconnection helpers, encryption primitives)",
5
5
  "author": "AgentFront <info@agentfront.dev>",
6
6
  "homepage": "https://github.com/agentfront/enclave",
7
7
  "license": "Apache-2.0",
@@ -34,6 +34,6 @@
34
34
  }
35
35
  },
36
36
  "dependencies": {
37
- "@enclave-vm/types": "2.19.1"
37
+ "@enclave-vm/types": "2.19.2"
38
38
  }
39
39
  }