1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458 |
x40
x40
x40
x40
x40
x40
x40
x40
x40
x40
x40
x40
x114
x114
x114
x114
x114
x40
x40
x114
x338
x87405
x87405
x338
x114
x40
x40
x86906
x86906
x86906
x86906
x86906
x86906
x86947
x87164
x87164
x86906
x173742
x260359
x260359
x173731
x260556
x260578
x260578
x260545
x347372
x347380
x347380
x347438
x347438
x347380
x347348
x86906
x40
x169
x219
x56
x228
x57
x228
x51
x86657
x86657
x86657
x86657
x51
x40
x40
x48
x144
x48
x73
x73
x73
x73
x73
x48
x40
x51
x51
x51
x51
x51
x51
x51
x51
x40
x40
x40
x89
x89
x89
x89
x40
x114
x114
x40
x114
x114
x40 |
I
I
I
I
|
// Copyright 2018-2025 the Deno authors. MIT license.
import { toByteStream } from "@std/streams/unstable-to-byte-stream";
import { numberToArray } from "./_common.ts";
import { CborArrayEncoderStream } from "./array_encoder_stream.ts";
import { CborByteEncoderStream } from "./byte_encoder_stream.ts";
import { encodeCbor } from "./encode_cbor.ts";
import { CborMapEncoderStream } from "./map_encoder_stream.ts";
import { CborTag } from "./tag.ts";
import { CborTextEncoderStream } from "./text_encoder_stream.ts";
import type { CborStreamInput } from "./types.ts";
/**
* A {@link TransformStream} that encodes a
* {@link ReadableStream<CborStreamInput>} into CBOR format sequence.
* [RFC 8949 - Concise Binary Object Representation (CBOR)](https://datatracker.ietf.org/doc/html/rfc8949)
*
* @example Usage
* ```ts no-assert
* import { encodeBase64Url } from "@std/encoding";
* import {
* CborArrayDecodedStream,
* CborArrayEncoderStream,
* CborByteDecodedStream,
* CborByteEncoderStream,
* CborMapDecodedStream,
* CborMapEncoderStream,
* type CborStreamOutput,
* CborSequenceDecoderStream,
* CborSequenceEncoderStream,
* CborTag,
* CborTextDecodedStream,
* CborTextEncoderStream,
* } from "@std/cbor";
*
* const rawMessage = [
* undefined,
* null,
* true,
* false,
* 3.14,
* 5,
* 2n ** 32n,
* "Hello World",
* new Uint8Array(25),
* new Date(),
* new CborTag(33, encodeBase64Url(new Uint8Array(7))),
* ["cake", "carrot"],
* { a: 3, b: "d" },
* CborByteEncoderStream.from([new Uint8Array(7)]),
* CborTextEncoderStream.from(["Bye!"]),
* CborArrayEncoderStream.from([
* "Hey!",
* CborByteEncoderStream.from([new Uint8Array(18)]),
* ]),
* CborMapEncoderStream.from([
* ["a", 0],
* ["b", "potato"],
* ]),
* ];
*
* async function logValue(value: CborStreamOutput) {
* if (
* value instanceof CborByteDecodedStream ||
* value instanceof CborTextDecodedStream
* ) {
* for await (const x of value) console.log(x);
* } else if (value instanceof CborArrayDecodedStream) {
* for await (const x of value) logValue(x);
* } else if (value instanceof CborMapDecodedStream) {
* for await (const [k, v] of value) {
* console.log(k);
* logValue(v);
* }
* } else if (value instanceof CborTag) {
* console.log(value);
* logValue(value.tagContent);
* } else console.log(value);
* }
*
* for await (
* const value of ReadableStream.from(rawMessage)
* .pipeThrough(new CborSequenceEncoderStream())
* .pipeThrough(new CborSequenceDecoderStream())
* ) {
* logValue(value);
* }
* ```
*/
export class CborSequenceEncoderStream
implements TransformStream<CborStreamInput, Uint8Array> {
#readable: ReadableStream<Uint8Array>;
#writable: WritableStream<CborStreamInput>;
/**
* Constructs a new instance.
*/
constructor() {
const { readable, writable } = new TransformStream<
CborStreamInput,
CborStreamInput
>();
this.#readable = toByteStream(
ReadableStream.from(this.#encodeFromReadable(readable)),
);
this.#writable = writable;
}
async *#encodeFromReadable(
readable: ReadableStream<CborStreamInput>,
): AsyncGenerator<Uint8Array> {
for await (const x of readable) {
for await (const y of this.#encode(x)) {
yield y;
}
}
}
async *#encode(
x: CborStreamInput,
): AsyncGenerator<Uint8Array> {
if (
x instanceof CborByteEncoderStream ||
x instanceof CborTextEncoderStream ||
x instanceof CborArrayEncoderStream ||
x instanceof CborMapEncoderStream
) {
for await (const y of x.readable) {
yield y;
}
} else if (x instanceof Array) {
for await (const y of this.#encodeArray(x)) {
yield y;
}
} else if (x instanceof CborTag) {
for await (const y of this.#encodeTag(x)) {
yield y;
}
} else if (typeof x === "object" && x !== null) {
if (x instanceof Date || x instanceof Uint8Array) yield encodeCbor(x);
else {
for await (const y of this.#encodeObject(x)) {
yield y;
}
}
} else yield encodeCbor(x);
}
async *#encodeArray(x: CborStreamInput[]): AsyncGenerator<Uint8Array> {
if (x.length < 24) yield new Uint8Array([0b100_00000 + x.length]);
else if (x.length < 2 ** 8) yield new Uint8Array([0b100_11000, x.length]);
else if (x.length < 2 ** 16) {
yield new Uint8Array([0b100_11001, ...numberToArray(2, x.length)]);
} else if (x.length < 2 ** 32) {
yield new Uint8Array([0b100_11010, ...numberToArray(4, x.length)]);
} // Can safely assume `x.length < 2 ** 64` as JavaScript doesn't support an `Array` being that large.
else yield new Uint8Array([0b100_11011, ...numberToArray(8, x.length)]);
for (const y of x) {
for await (const z of this.#encode(y)) {
yield z;
}
}
}
async *#encodeObject(
x: { [k: string]: CborStreamInput },
): AsyncGenerator<Uint8Array> {
const len = Object.keys(x).length;
if (len < 24) yield new Uint8Array([0b101_00000 + len]);
else if (len < 2 ** 8) yield new Uint8Array([0b101_11000, len]);
else if (len < 2 ** 16) {
yield new Uint8Array([0b101_11001, ...numberToArray(2, len)]);
} else if (len < 2 ** 32) {
yield new Uint8Array([0b101_11010, ...numberToArray(4, len)]);
} // Can safely assume `len < 2 ** 64` as JavaScript doesn't support an `Object` being that Large.
else yield new Uint8Array([0b101_11011, ...numberToArray(8, len)]);
for (const [k, v] of Object.entries(x)) {
yield encodeCbor(k);
for await (const y of this.#encode(v)) {
yield y;
}
}
}
async *#encodeTag(x: CborTag<CborStreamInput>): AsyncGenerator<Uint8Array> {
const tagNumber = BigInt(x.tagNumber);
if (tagNumber < 0n) {
throw new RangeError(
`Cannot encode Tag Item: Tag Number (${x.tagNumber}) is less than zero`,
);
}
if (tagNumber > 2n ** 64n) {
throw new RangeError(
`Cannot encode Tag Item: Tag Number (${x.tagNumber}) exceeds 2 ** 64 - 1`,
);
}
const head = encodeCbor(tagNumber);
head[0]! += 0b110_00000;
yield head;
for await (const y of this.#encode(x.tagContent)) {
yield y;
}
}
/**
* Creates a {@link CborSequenceEncoderStream} instance from an iterable of
* {@link CborStreamInput} chunks.
*
* @example Usage
* ```ts no-assert
* import { encodeBase64Url } from "@std/encoding";
* import {
* CborArrayDecodedStream,
* CborArrayEncoderStream,
* CborByteDecodedStream,
* CborByteEncoderStream,
* CborMapDecodedStream,
* CborMapEncoderStream,
* type CborStreamOutput,
* CborSequenceDecoderStream,
* CborSequenceEncoderStream,
* CborTag,
* CborTextDecodedStream,
* CborTextEncoderStream,
* } from "@std/cbor";
*
* const rawMessage = [
* undefined,
* null,
* true,
* false,
* 3.14,
* 5,
* 2n ** 32n,
* "Hello World",
* new Uint8Array(25),
* new Date(),
* new CborTag(33, encodeBase64Url(new Uint8Array(7))),
* ["cake", "carrot"],
* { a: 3, b: "d" },
* CborByteEncoderStream.from([new Uint8Array(7)]),
* CborTextEncoderStream.from(["Bye!"]),
* CborArrayEncoderStream.from([
* "Hey!",
* CborByteEncoderStream.from([new Uint8Array(18)]),
* ]),
* CborMapEncoderStream.from([
* ["a", 0],
* ["b", "potato"],
* ]),
* ];
*
* async function logValue(value: CborStreamOutput) {
* if (
* value instanceof CborByteDecodedStream ||
* value instanceof CborTextDecodedStream
* ) {
* for await (const x of value) console.log(x);
* } else if (value instanceof CborArrayDecodedStream) {
* for await (const x of value) logValue(x);
* } else if (value instanceof CborMapDecodedStream) {
* for await (const [k, v] of value) {
* console.log(k);
* logValue(v);
* }
* } else if (value instanceof CborTag) {
* console.log(value);
* logValue(value.tagContent);
* } else console.log(value);
* }
*
* for await (
* const value of CborSequenceEncoderStream.from(rawMessage)
* .readable
* .pipeThrough(new CborSequenceDecoderStream())
* ) {
* logValue(value);
* }
* ```
*
* @param asyncIterable The value to encode of type
* {@link AsyncIterable<CborStreamInput>} or
* {@link Iterable<CborStreamInput>}.
* @returns A {@link CborSequenceEncoderStream} instance of the encoded data.
*/
static from(
asyncIterable: AsyncIterable<CborStreamInput> | Iterable<CborStreamInput>,
): CborSequenceEncoderStream {
const encoder = new CborSequenceEncoderStream();
ReadableStream.from(asyncIterable).pipeTo(encoder.writable);
return encoder;
}
/**
* The {@link ReadableStream<Uint8Array>} associated with the instance, which
* provides the encoded CBOR data as {@link Uint8Array} chunks.
*
* @example Usage
* ```ts no-assert
* import { encodeBase64Url } from "@std/encoding";
* import {
* CborArrayDecodedStream,
* CborArrayEncoderStream,
* CborByteDecodedStream,
* CborByteEncoderStream,
* CborMapDecodedStream,
* CborMapEncoderStream,
* type CborStreamOutput,
* CborSequenceDecoderStream,
* CborSequenceEncoderStream,
* CborTag,
* CborTextDecodedStream,
* CborTextEncoderStream,
* } from "@std/cbor";
*
* const rawMessage = [
* undefined,
* null,
* true,
* false,
* 3.14,
* 5,
* 2n ** 32n,
* "Hello World",
* new Uint8Array(25),
* new Date(),
* new CborTag(33, encodeBase64Url(new Uint8Array(7))),
* ["cake", "carrot"],
* { a: 3, b: "d" },
* CborByteEncoderStream.from([new Uint8Array(7)]),
* CborTextEncoderStream.from(["Bye!"]),
* CborArrayEncoderStream.from([
* "Hey!",
* CborByteEncoderStream.from([new Uint8Array(18)]),
* ]),
* CborMapEncoderStream.from([
* ["a", 0],
* ["b", "potato"],
* ]),
* ];
*
* async function logValue(value: CborStreamOutput) {
* if (
* value instanceof CborByteDecodedStream ||
* value instanceof CborTextDecodedStream
* ) {
* for await (const x of value) console.log(x);
* } else if (value instanceof CborArrayDecodedStream) {
* for await (const x of value) logValue(x);
* } else if (value instanceof CborMapDecodedStream) {
* for await (const [k, v] of value) {
* console.log(k);
* logValue(v);
* }
* } else if (value instanceof CborTag) {
* console.log(value);
* logValue(value.tagContent);
* } else console.log(value);
* }
*
* for await (
* const value of ReadableStream.from(rawMessage)
* .pipeThrough(new CborSequenceEncoderStream())
* .pipeThrough(new CborSequenceDecoderStream())
* ) {
* logValue(value);
* }
* ```
*
* @returns A {@link ReadableStream<Uint8Array>}.
*/
get readable(): ReadableStream<Uint8Array> {
return this.#readable;
}
/**
* The {@link WritableStream<CborStreamInput>} associated with the
* instance, which accepts {@link CborStreamInput} chunks to be encoded
* into CBOR format.
*
* @example Usage
* ```ts no-assert
* import { encodeBase64Url } from "@std/encoding";
* import {
* CborArrayDecodedStream,
* CborArrayEncoderStream,
* CborByteDecodedStream,
* CborByteEncoderStream,
* CborMapDecodedStream,
* CborMapEncoderStream,
* type CborStreamOutput,
* CborSequenceDecoderStream,
* CborSequenceEncoderStream,
* CborTag,
* CborTextDecodedStream,
* CborTextEncoderStream,
* } from "@std/cbor";
*
* const rawMessage = [
* undefined,
* null,
* true,
* false,
* 3.14,
* 5,
* 2n ** 32n,
* "Hello World",
* new Uint8Array(25),
* new Date(),
* new CborTag(33, encodeBase64Url(new Uint8Array(7))),
* ["cake", "carrot"],
* { a: 3, b: "d" },
* CborByteEncoderStream.from([new Uint8Array(7)]),
* CborTextEncoderStream.from(["Bye!"]),
* CborArrayEncoderStream.from([
* "Hey!",
* CborByteEncoderStream.from([new Uint8Array(18)]),
* ]),
* CborMapEncoderStream.from([
* ["a", 0],
* ["b", "potato"],
* ]),
* ];
*
* async function logValue(value: CborStreamOutput) {
* if (
* value instanceof CborByteDecodedStream ||
* value instanceof CborTextDecodedStream
* ) {
* for await (const x of value) console.log(x);
* } else if (value instanceof CborArrayDecodedStream) {
* for await (const x of value) logValue(x);
* } else if (value instanceof CborMapDecodedStream) {
* for await (const [k, v] of value) {
* console.log(k);
* logValue(v);
* }
* } else if (value instanceof CborTag) {
* console.log(value);
* logValue(value.tagContent);
* } else console.log(value);
* }
*
* for await (
* const value of ReadableStream.from(rawMessage)
* .pipeThrough(new CborSequenceEncoderStream())
* .pipeThrough(new CborSequenceDecoderStream())
* ) {
* logValue(value);
* }
* ```
*
* @returns A {@link WritableStream<CborStreamInput>}.
*/
get writable(): WritableStream<CborStreamInput> {
return this.#writable;
}
}
|