FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
msgpack-javascript/test/ExtensionCodec.test.ts at main · msgpack/msgpack-javascript · GitHub
msgpack
/
msgpack-javascript
Public
Notifications
You must be signed in to change notification settings
Fork
180
Star
1.6k
Code
Issues
19
Pull requests
5
Discussions
Actions
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
msgpack-javascript
/
test
/
ExtensionCodec.test.ts
Copy path
More file actions
More file actions
Latest commit
History
History
History
243 lines (226 loc) · 7.19 KB
Breadcrumbs
msgpack-javascript
/
test
/
ExtensionCodec.test.ts
Copy path
File metadata and controls
243 lines (226 loc) · 7.19 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
import
assert
from
"assert"
;
import
util
from
"util"
;
import
{
encode
,
decode
,
ExtensionCodec
,
decodeAsync
}
from
"../src/index.ts"
;
describe
(
"ExtensionCodec"
,
(
)
=>
{
context
(
"timestamp"
,
(
)
=>
{
const
extensionCodec
=
ExtensionCodec
.
defaultCodec
;
it
(
"encodes and decodes a date without milliseconds (timestamp 32)"
,
(
)
=>
{
const
date
=
new
Date
(
1556633024000
)
;
const
encoded
=
encode
(
date
,
{
extensionCodec
}
)
;
assert
.
deepStrictEqual
(
decode
(
encoded
,
{
extensionCodec
}
)
,
date
,
`date:
${
date
.
toISOString
(
)
}
, encoded:
${
util
.
inspect
(
encoded
)
}
`
,
)
;
}
)
;
it
(
"encodes and decodes a date with milliseconds (timestamp 64)"
,
(
)
=>
{
const
date
=
new
Date
(
1556633024123
)
;
const
encoded
=
encode
(
date
,
{
extensionCodec
}
)
;
assert
.
deepStrictEqual
(
decode
(
encoded
,
{
extensionCodec
}
)
,
date
,
`date:
${
date
.
toISOString
(
)
}
, encoded:
${
util
.
inspect
(
encoded
)
}
`
,
)
;
}
)
;
it
(
"encodes and decodes a future date (timestamp 96)"
,
(
)
=>
{
const
date
=
new
Date
(
0x400000000
*
1000
)
;
const
encoded
=
encode
(
date
,
{
extensionCodec
}
)
;
assert
.
deepStrictEqual
(
decode
(
encoded
,
{
extensionCodec
}
)
,
date
,
`date:
${
date
.
toISOString
(
)
}
, encoded:
${
util
.
inspect
(
encoded
)
}
`
,
)
;
}
)
;
}
)
;
context
(
"custom extensions"
,
(
)
=>
{
const
extensionCodec
=
new
ExtensionCodec
(
)
;
// Set<T>
extensionCodec
.
register
(
{
type
:
0
,
encode
:
(
object
:
unknown
)
:
Uint8Array
|
null
=>
{
if
(
object
instanceof
Set
)
{
return
encode
(
[
...
object
]
)
;
}
else
{
return
null
;
}
}
,
decode
:
(
data
:
Uint8Array
)
=>
{
const
array
=
decode
(
data
)
as
Array
<
unknown
>
;
return
new
Set
(
array
)
;
}
,
}
)
;
// Map<T>
extensionCodec
.
register
(
{
type
:
1
,
encode
:
(
object
:
unknown
)
:
Uint8Array
|
null
=>
{
if
(
object
instanceof
Map
)
{
return
encode
(
[
...
object
]
)
;
}
else
{
return
null
;
}
}
,
decode
:
(
data
:
Uint8Array
)
=>
{
const
array
=
decode
(
data
)
as
Array
<
[
unknown
,
unknown
]
>
;
return
new
Map
(
array
)
;
}
,
}
)
;
it
(
"encodes and decodes custom data types (synchronously)"
,
(
)
=>
{
const
set
=
new
Set
(
[
1
,
2
,
3
]
)
;
const
map
=
new
Map
(
[
[
"foo"
,
"bar"
]
,
[
"bar"
,
"baz"
]
,
]
)
;
const
encoded
=
encode
(
[
set
,
map
]
,
{
extensionCodec
}
)
;
assert
.
deepStrictEqual
(
decode
(
encoded
,
{
extensionCodec
}
)
,
[
set
,
map
]
)
;
}
)
;
it
(
"encodes and decodes custom data types (asynchronously)"
,
async
(
)
=>
{
const
set
=
new
Set
(
[
1
,
2
,
3
]
)
;
const
map
=
new
Map
(
[
[
"foo"
,
"bar"
]
,
[
"bar"
,
"baz"
]
,
]
)
;
const
encoded
=
encode
(
[
set
,
map
]
,
{
extensionCodec
}
)
;
const
createStream
=
async
function
*
(
)
{
yield
encoded
;
}
;
assert
.
deepStrictEqual
(
await
decodeAsync
(
createStream
(
)
,
{
extensionCodec
}
)
,
[
set
,
map
]
)
;
}
)
;
}
)
;
context
(
"custom extensions with custom context"
,
(
)
=>
{
class
Context
{
public
ctxVal
:
number
;
public
expectations
:
Array
<
any
>
=
[
]
;
constructor
(
ctxVal
:
number
)
{
this
.
ctxVal
=
ctxVal
;
}
public
hasVisited
(
val
:
any
)
{
this
.
expectations
.
push
(
val
)
;
}
}
const
extensionCodec
=
new
ExtensionCodec
<
Context
>
(
)
;
class
Magic
<
T
>
{
public
val
:
T
;
constructor
(
val
:
T
)
{
this
.
val
=
val
;
}
}
// Magic
extensionCodec
.
register
(
{
type
:
0
,
encode
:
(
object
:
unknown
,
context
)
:
Uint8Array
|
null
=>
{
if
(
object
instanceof
Magic
)
{
context
.
hasVisited
(
{
encoding
:
object
.
val
}
)
;
return
encode
(
{
magic
:
object
.
val
,
ctx
:
context
.
ctxVal
}
,
{
extensionCodec
,
context
}
)
;
}
else
{
return
null
;
}
}
,
decode
:
(
data
:
Uint8Array
,
extType
,
context
)
=>
{
const
decoded
=
decode
(
data
,
{
extensionCodec
,
context
}
)
as
{
magic
:
number
}
;
context
.
hasVisited
(
{
decoding
:
decoded
.
magic
,
ctx
:
context
.
ctxVal
}
)
;
return
new
Magic
(
decoded
.
magic
)
;
}
,
}
)
;
it
(
"encodes and decodes custom data types (synchronously)"
,
(
)
=>
{
const
context
=
new
Context
(
42
)
;
const
magic1
=
new
Magic
(
17
)
;
const
magic2
=
new
Magic
(
{
foo
:
new
Magic
(
"inner"
)
}
)
;
const
test
=
[
magic1
,
magic2
]
;
const
encoded
=
encode
(
test
,
{
extensionCodec
,
context
}
)
;
assert
.
deepStrictEqual
(
decode
(
encoded
,
{
extensionCodec
,
context
}
)
,
test
)
;
assert
.
deepStrictEqual
(
context
.
expectations
,
[
{
encoding
:
magic1
.
val
,
}
,
{
encoding
:
magic2
.
val
,
}
,
{
encoding
:
magic2
.
val
.
foo
.
val
,
}
,
{
ctx
:
42
,
decoding
:
magic1
.
val
,
}
,
{
ctx
:
42
,
decoding
:
magic2
.
val
.
foo
.
val
,
}
,
{
ctx
:
42
,
decoding
:
magic2
.
val
,
}
,
]
)
;
}
)
;
it
(
"encodes and decodes custom data types (asynchronously)"
,
async
(
)
=>
{
const
context
=
new
Context
(
42
)
;
const
magic1
=
new
Magic
(
17
)
;
const
magic2
=
new
Magic
(
{
foo
:
new
Magic
(
"inner"
)
}
)
;
const
test
=
[
magic1
,
magic2
]
;
const
encoded
=
encode
(
test
,
{
extensionCodec
,
context
}
)
;
const
createStream
=
async
function
*
(
)
{
yield
encoded
;
}
;
assert
.
deepStrictEqual
(
await
decodeAsync
(
createStream
(
)
,
{
extensionCodec
,
context
}
)
,
test
)
;
assert
.
deepStrictEqual
(
context
.
expectations
,
[
{
encoding
:
magic1
.
val
,
}
,
{
encoding
:
magic2
.
val
,
}
,
{
encoding
:
magic2
.
val
.
foo
.
val
,
}
,
{
ctx
:
42
,
decoding
:
magic1
.
val
,
}
,
{
ctx
:
42
,
decoding
:
magic2
.
val
.
foo
.
val
,
}
,
{
ctx
:
42
,
decoding
:
magic2
.
val
,
}
,
]
)
;
}
)
;
}
)
;
context
(
"custom extensions with alignment"
,
(
)
=>
{
const
extensionCodec
=
new
ExtensionCodec
(
)
;
extensionCodec
.
register
(
{
type
:
0x01
,
encode
:
(
object
:
unknown
)
=>
{
if
(
object
instanceof
Float32Array
)
{
return
(
pos
:
number
)
=>
{
const
bpe
=
Float32Array
.
BYTES_PER_ELEMENT
;
const
padding
=
1
+
(
(
bpe
-
(
(
pos
+
1
)
%
bpe
)
)
%
bpe
)
;
const
data
=
new
Uint8Array
(
object
.
buffer
)
;
const
result
=
new
Uint8Array
(
padding
+
data
.
length
)
;
result
[
0
]
=
padding
;
result
.
set
(
data
,
padding
)
;
return
result
;
}
;
}
return
null
;
}
,
decode
:
(
data
:
Uint8Array
)
=>
{
const
padding
=
data
[
0
]
!
;
const
bpe
=
Float32Array
.
BYTES_PER_ELEMENT
;
const
offset
=
data
.
byteOffset
+
padding
;
const
length
=
data
.
byteLength
-
padding
;
return
new
Float32Array
(
data
.
buffer
,
offset
,
length
/
bpe
)
;
}
,
}
)
;
it
(
"encodes and decodes Float32Array type with zero-copy"
,
(
)
=>
{
const
data
=
{
position
:
new
Float32Array
(
[
1.1
,
2.2
,
3.3
,
4.4
,
5.5
]
)
,
}
;
const
encoded
=
encode
(
data
,
{
extensionCodec
}
)
;
const
decoded
=
decode
(
encoded
,
{
extensionCodec
}
)
;
assert
.
deepStrictEqual
(
decoded
,
data
)
;
assert
.
strictEqual
(
decoded
.
position
.
buffer
,
encoded
.
buffer
)
;
}
)
;
}
)
;
}
)
;
Back
|
FazBrowse Home
|
New Git URL