forked from msgpack/msgpack-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecodeAsync.test.ts
138 lines (120 loc) · 4.04 KB
/
decodeAsync.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
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
import assert from "assert";
import { encode, decodeAsync } from "../src/index.ts";
describe("decodeAsync", () => {
function wrapWithNoisyBuffer(byte: number) {
return Uint8Array.from([0x01, byte, 0x02]).subarray(1, 2);
}
it("decodes nil", async () => {
const createStream = async function* () {
yield wrapWithNoisyBuffer(0xc0); // nil
};
const object = await decodeAsync(createStream());
assert.deepStrictEqual(object, null);
});
it("decodes fixarray [nil]", async () => {
const createStream = async function* () {
yield wrapWithNoisyBuffer(0x91); // fixarray size=1
yield [0xc0]; // nil
};
const object = await decodeAsync(createStream());
assert.deepStrictEqual(object, [null]);
});
it("decodes fixmap {'foo': 'bar'}", async () => {
const createStream = async function* () {
yield [0x81]; // fixmap size=1
yield encode("foo");
yield encode("bar");
};
const object = await decodeAsync(createStream());
assert.deepStrictEqual(object, { "foo": "bar" });
});
it("decodes fixmap {'[1, 2]': 'baz'} with custom map key converter", async () => {
const createStream = async function* () {
yield [0x81]; // fixmap size=1
yield encode([1, 2]);
yield encode("baz");
};
const object = await decodeAsync(createStream(), {
mapKeyConverter: (key) => JSON.stringify(key),
});
const key = JSON.stringify([1, 2]);
assert.deepStrictEqual(object, { [key]: "baz" });
});
it("decodes multi-byte integer byte-by-byte", async () => {
const createStream = async function* () {
yield [0xcd]; // uint 16
yield [0x12];
yield [0x34];
};
const object = await decodeAsync(createStream());
assert.deepStrictEqual(object, 0x1234);
});
it("decodes fixstr byte-by-byte", async () => {
const createStream = async function* () {
yield [0xa3]; // fixstr size=3
yield [0x66]; // "f"
yield [0x6f]; // "o"
yield [0x6f]; // "o"
};
const object = await decodeAsync(createStream());
assert.deepStrictEqual(object, "foo");
});
it("decodes binary byte-by-byte", async () => {
const createStream = async function* () {
yield [0xc4]; // bin 8
yield [0x03]; // bin size=3
yield [0x66]; // "f"
yield [0x6f]; // "o"
yield [0x6f]; // "o"
};
const object = await decodeAsync(createStream());
assert.deepStrictEqual(object, Uint8Array.from([0x66, 0x6f, 0x6f]));
});
it("decodes binary with noisy buffer", async () => {
const createStream = async function* () {
yield wrapWithNoisyBuffer(0xc5); // bin 16
yield [0x00];
yield [0x00]; // bin size=0
};
const object = await decodeAsync(createStream());
assert.deepStrictEqual(object, new Uint8Array(0));
});
it("decodes mixed object byte-by-byte", async () => {
const object = {
nil: null,
true: true,
false: false,
int: -42,
uint64: Number.MAX_SAFE_INTEGER,
int64: Number.MIN_SAFE_INTEGER,
float: Math.PI,
string: "Hello, world!",
longString: "Hello, world!\n".repeat(100),
binary: Uint8Array.from([0xf1, 0xf2, 0xf3]),
array: [1000, 2000, 3000],
map: { foo: 1, bar: 2, baz: 3 },
timestampExt: new Date(),
map0: {},
array0: [],
str0: "",
bin0: Uint8Array.from([]),
};
const createStream = async function* () {
for (const byte of encode(object)) {
yield [byte];
}
};
assert.deepStrictEqual(await decodeAsync(createStream()), object);
});
it("decodes BufferSource", async () => {
// https://developer.mozilla.org/en-US/docs/Web/API/BufferSource
const createStream = async function* () {
yield [0x81] as ArrayLike<number>; // fixmap size=1
yield encode("foo") as BufferSource;
yield encode("bar") as BufferSource;
};
// createStream() returns AsyncGenerator<ArrayLike<number> | BufferSource, ...>
const object = await decodeAsync(createStream());
assert.deepStrictEqual(object, { "foo": "bar" });
});
});