-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstreams.test.ts
39 lines (32 loc) · 975 Bytes
/
streams.test.ts
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
import test from "ava"
import { CBORValue, CBOREncoderStream, CBORDecoderStream } from "microcbor"
test("CBOREncoderStream / CBORDecoderStream", async (t) => {
const input: CBORValue[] = [
{ hello: "world" },
[1, 2, 3, { foo: NaN, bar: -Infinity, baz: Infinity }],
"test string",
42,
"jfkdlsfj ksdljfkldsafjkdlsajfklds ajfklads jfkldas fjklasd",
null,
true,
undefined,
new Uint8Array([1, 2, 3]),
]
// Create source stream
const reader = new ReadableStream<CBORValue>({
start(controller) {
input.forEach((value) => controller.enqueue(value))
controller.close()
},
})
// Create encoder and decoder
const encoder = new CBOREncoderStream()
const decoder = new CBORDecoderStream()
// Create output writer
const output: CBORValue[] = []
const writer = new WritableStream<CBORValue>({
write: (chunk) => void output.push(chunk),
})
await reader.pipeThrough(encoder).pipeThrough(decoder).pipeTo(writer)
t.deepEqual(input, output)
})