@@ -2,16 +2,16 @@ import assert from "assert";
2
2
import { encode , decode } from "../src" ;
3
3
import type { DecoderOptions } from "../src" ;
4
4
5
- describe ( "decode with useRawBinaryStrings specified" , ( ) => {
6
- const options = { useRawBinaryStrings : true } satisfies DecoderOptions ;
5
+ describe ( "decode with rawBinaryStringValues specified" , ( ) => {
6
+ const options = { rawBinaryStringValues : true } satisfies DecoderOptions ;
7
7
8
- it ( "decodes string as binary" , ( ) => {
8
+ it ( "decodes string values as binary" , ( ) => {
9
9
const actual = decode ( encode ( "foo" ) , options ) ;
10
10
const expected = Uint8Array . from ( [ 0x66 , 0x6f , 0x6f ] ) ;
11
11
assert . deepStrictEqual ( actual , expected ) ;
12
12
} ) ;
13
13
14
- it ( "decodes invalid UTF-8 string as binary" , ( ) => {
14
+ it ( "decodes invalid UTF-8 string values as binary" , ( ) => {
15
15
const invalidUtf8String = Uint8Array . from ( [
16
16
61 , 180 , 118 , 220 , 39 , 166 , 43 , 68 , 219 , 116 , 105 , 84 , 121 , 46 , 122 , 136 , 233 , 221 , 15 , 174 , 247 , 19 , 50 , 176 ,
17
17
184 , 221 , 66 , 188 , 171 , 36 , 135 , 121 ,
@@ -25,18 +25,12 @@ describe("decode with useRawBinaryStrings specified", () => {
25
25
assert . deepStrictEqual ( actual , invalidUtf8String ) ;
26
26
} ) ;
27
27
28
- it ( "decodes object keys as strings" , ( ) => {
28
+ it ( "decodes map string keys as strings" , ( ) => {
29
29
const actual = decode ( encode ( { key : "foo" } ) , options ) ;
30
30
const expected = { key : Uint8Array . from ( [ 0x66 , 0x6f , 0x6f ] ) } ;
31
31
assert . deepStrictEqual ( actual , expected ) ;
32
32
} ) ;
33
33
34
- it ( "decodes map keys as binary when useMap is enabled" , ( ) => {
35
- const actual = decode ( encode ( { key : "foo" } ) , { ...options , useMap : true } ) ;
36
- const expected = new Map ( [ [ Uint8Array . from ( [ 0x6b , 0x65 , 0x79 ] ) , Uint8Array . from ( [ 0x66 , 0x6f , 0x6f ] ) ] ] ) ;
37
- assert . deepStrictEqual ( actual , expected ) ;
38
- } ) ;
39
-
40
34
it ( "ignores maxStrLength" , ( ) => {
41
35
const lengthLimitedOptions = { ...options , maxStrLength : 1 } satisfies DecoderOptions ;
42
36
@@ -53,3 +47,86 @@ describe("decode with useRawBinaryStrings specified", () => {
53
47
} , / m a x l e n g t h e x c e e d e d / i) ;
54
48
} ) ;
55
49
} ) ;
50
+
51
+ describe ( "decode with rawBinaryStringKeys specified" , ( ) => {
52
+ const options = { rawBinaryStringKeys : true , useMap : true } satisfies DecoderOptions ;
53
+
54
+ it ( "errors if useMap is not enabled" , ( ) => {
55
+ assert . throws ( ( ) => {
56
+ decode ( encode ( { key : "foo" } ) , { rawBinaryStringKeys : true } ) ;
57
+ } , new Error ( "rawBinaryStringKeys is only supported when useMap is true" ) ) ;
58
+ } ) ;
59
+
60
+ it ( "decodes map string keys as binary" , ( ) => {
61
+ const actual = decode ( encode ( { key : "foo" } ) , options ) ;
62
+ const expected = new Map ( [ [ Uint8Array . from ( [ 0x6b , 0x65 , 0x79 ] ) , "foo" ] ] ) ;
63
+ assert . deepStrictEqual ( actual , expected ) ;
64
+ } ) ;
65
+
66
+ it ( "decodes invalid UTF-8 string keys as binary" , ( ) => {
67
+ const invalidUtf8String = Uint8Array . from ( [
68
+ 61 , 180 , 118 , 220 , 39 , 166 , 43 , 68 , 219 , 116 , 105 , 84 , 121 , 46 , 122 , 136 , 233 , 221 , 15 , 174 , 247 , 19 , 50 , 176 ,
69
+ 184 , 221 , 66 , 188 , 171 , 36 , 135 , 121 ,
70
+ ] ) ;
71
+ const encodedMap = Uint8Array . from ( [
72
+ 129 , 217 , 32 , 61 , 180 , 118 , 220 , 39 , 166 , 43 , 68 , 219 , 116 , 105 , 84 , 121 , 46 , 122 , 136 , 233 , 221 , 15 , 174 , 247 ,
73
+ 19 , 50 , 176 , 184 , 221 , 66 , 188 , 171 , 36 , 135 , 121 , 163 , 97 , 98 , 99 ,
74
+ ] ) ;
75
+ const actual = decode ( encodedMap , options ) ;
76
+ const expected = new Map ( [ [ invalidUtf8String , "abc" ] ] ) ;
77
+ assert . deepStrictEqual ( actual , expected ) ;
78
+ } ) ;
79
+
80
+ it ( "decodes string values as strings" , ( ) => {
81
+ const actual = decode ( encode ( "foo" ) , options ) ;
82
+ const expected = "foo" ;
83
+ assert . deepStrictEqual ( actual , expected ) ;
84
+ } ) ;
85
+
86
+ it ( "ignores maxStrLength" , ( ) => {
87
+ const lengthLimitedOptions = { ...options , maxStrLength : 1 } satisfies DecoderOptions ;
88
+
89
+ const actual = decode ( encode ( { foo : 1 } ) , lengthLimitedOptions ) ;
90
+ const expected = new Map ( [ [ Uint8Array . from ( [ 0x66 , 0x6f , 0x6f ] ) , 1 ] ] ) ;
91
+ assert . deepStrictEqual ( actual , expected ) ;
92
+ } ) ;
93
+
94
+ it ( "respects maxBinLength" , ( ) => {
95
+ const lengthLimitedOptions = { ...options , maxBinLength : 1 } satisfies DecoderOptions ;
96
+
97
+ assert . throws ( ( ) => {
98
+ decode ( encode ( { foo : 1 } ) , lengthLimitedOptions ) ;
99
+ } , / m a x l e n g t h e x c e e d e d / i) ;
100
+ } ) ;
101
+ } ) ;
102
+
103
+ describe ( "decode with rawBinaryStringKeys and rawBinaryStringValues" , ( ) => {
104
+ const options = { rawBinaryStringValues : true , rawBinaryStringKeys : true , useMap : true } satisfies DecoderOptions ;
105
+
106
+ it ( "errors if useMap is not enabled" , ( ) => {
107
+ assert . throws ( ( ) => {
108
+ decode ( encode ( { key : "foo" } ) , { rawBinaryStringKeys : true , rawBinaryStringValues : true } ) ;
109
+ } , new Error ( "rawBinaryStringKeys is only supported when useMap is true" ) ) ;
110
+ } ) ;
111
+
112
+ it ( "decodes map string keys and values as binary" , ( ) => {
113
+ const actual = decode ( encode ( { key : "foo" } ) , options ) ;
114
+ const expected = new Map ( [ [ Uint8Array . from ( [ 0x6b , 0x65 , 0x79 ] ) , Uint8Array . from ( [ 0x66 , 0x6f , 0x6f ] ) ] ] ) ;
115
+ assert . deepStrictEqual ( actual , expected ) ;
116
+ } ) ;
117
+
118
+ it ( "decodes invalid UTF-8 string keys and values as binary" , ( ) => {
119
+ const invalidUtf8String = Uint8Array . from ( [
120
+ 61 , 180 , 118 , 220 , 39 , 166 , 43 , 68 , 219 , 116 , 105 , 84 , 121 , 46 , 122 , 136 , 233 , 221 , 15 , 174 , 247 , 19 , 50 , 176 ,
121
+ 184 , 221 , 66 , 188 , 171 , 36 , 135 , 121 ,
122
+ ] ) ;
123
+ const encodedMap = Uint8Array . from ( [
124
+ 129 , 217 , 32 , 61 , 180 , 118 , 220 , 39 , 166 , 43 , 68 , 219 , 116 , 105 , 84 , 121 , 46 , 122 , 136 , 233 , 221 , 15 , 174 , 247 ,
125
+ 19 , 50 , 176 , 184 , 221 , 66 , 188 , 171 , 36 , 135 , 121 , 217 , 32 , 61 , 180 , 118 , 220 , 39 , 166 , 43 , 68 , 219 , 116 , 105 , 84 ,
126
+ 121 , 46 , 122 , 136 , 233 , 221 , 15 , 174 , 247 , 19 , 50 , 176 , 184 , 221 , 66 , 188 , 171 , 36 , 135 , 121 ,
127
+ ] ) ;
128
+ const actual = decode ( encodedMap , options ) ;
129
+ const expected = new Map ( [ [ invalidUtf8String , invalidUtf8String ] ] ) ;
130
+ assert . deepStrictEqual ( actual , expected ) ;
131
+ } ) ;
132
+ } ) ;
0 commit comments