@@ -107,24 +107,29 @@ const readCid = (state: State, length: number): CidLink => {
107
107
return new CidLinkWrapper ( slice ) ;
108
108
} ;
109
109
110
- const enum ContainerType {
111
- MAP ,
112
- ARRAY ,
113
- }
114
-
115
110
type Container =
116
111
| {
117
- t : ContainerType . MAP ;
112
+ /** map type */
113
+ t : 0 ;
114
+ /** container value */
118
115
c : Record < string , unknown > ;
116
+ /** held key (as we decode the value) */
119
117
k : string | null ;
118
+ /** remaining elements (key + value) */
120
119
r : number ;
120
+ /** next container in stack */
121
121
n : Container | null ;
122
122
}
123
123
| {
124
- t : ContainerType . ARRAY ;
124
+ /** array type */
125
+ t : 1 ;
126
+ /** container value */
125
127
c : any [ ] ;
128
+ /** held key (not used) */
126
129
k : null ;
130
+ /** remaining elements (values) */
127
131
r : number ;
132
+ /** next container in stack */
128
133
n : Container | null ;
129
134
} ;
130
135
@@ -169,7 +174,7 @@ export const decodeFirst = (buf: Uint8Array): [value: any, remainder: Uint8Array
169
174
value = arr ;
170
175
171
176
if ( arg > 0 ) {
172
- stack = { t : ContainerType . ARRAY , c : arr , k : null , r : arg , n : stack } ;
177
+ stack = { t : 1 , c : arr , k : null , r : arg , n : stack } ;
173
178
continue jump;
174
179
}
175
180
@@ -181,7 +186,7 @@ export const decodeFirst = (buf: Uint8Array): [value: any, remainder: Uint8Array
181
186
182
187
if ( arg > 0 ) {
183
188
// `arg * 2` because we're reading both keys and values
184
- stack = { t : ContainerType . MAP , c : obj , k : null , r : arg * 2 , n : stack } ;
189
+ stack = { t : 0 , c : obj , k : null , r : arg * 2 , n : stack } ;
185
190
continue jump;
186
191
}
187
192
@@ -236,14 +241,7 @@ export const decodeFirst = (buf: Uint8Array): [value: any, remainder: Uint8Array
236
241
237
242
while ( stack !== null ) {
238
243
switch ( stack . t ) {
239
- case ContainerType . ARRAY : {
240
- const arr = stack . c ;
241
- const index = arr . length - stack . r ;
242
-
243
- arr [ index ] = value ;
244
- break ;
245
- }
246
- case ContainerType . MAP : {
244
+ case 0 : {
247
245
const obj = stack . c ;
248
246
const key = stack . k ;
249
247
@@ -265,6 +263,13 @@ export const decodeFirst = (buf: Uint8Array): [value: any, remainder: Uint8Array
265
263
266
264
break ;
267
265
}
266
+ case 1 : {
267
+ const arr = stack . c ;
268
+ const index = arr . length - stack . r ;
269
+
270
+ arr [ index ] = value ;
271
+ break ;
272
+ }
268
273
}
269
274
270
275
if ( -- stack . r !== 0 ) {
0 commit comments