Skip to content

Commit 47f14d3

Browse files
committed
refactor(cbor): inline enum values directly
verbatimModuleSyntax gimps the const-enums transformation that i was expecting, so thanks for that
1 parent a34283d commit 47f14d3

File tree

1 file changed

+22
-17
lines changed

1 file changed

+22
-17
lines changed

packages/utilities/cbor/lib/decode.ts

+22-17
Original file line numberDiff line numberDiff line change
@@ -107,24 +107,29 @@ const readCid = (state: State, length: number): CidLink => {
107107
return new CidLinkWrapper(slice);
108108
};
109109

110-
const enum ContainerType {
111-
MAP,
112-
ARRAY,
113-
}
114-
115110
type Container =
116111
| {
117-
t: ContainerType.MAP;
112+
/** map type */
113+
t: 0;
114+
/** container value */
118115
c: Record<string, unknown>;
116+
/** held key (as we decode the value) */
119117
k: string | null;
118+
/** remaining elements (key + value) */
120119
r: number;
120+
/** next container in stack */
121121
n: Container | null;
122122
}
123123
| {
124-
t: ContainerType.ARRAY;
124+
/** array type */
125+
t: 1;
126+
/** container value */
125127
c: any[];
128+
/** held key (not used) */
126129
k: null;
130+
/** remaining elements (values) */
127131
r: number;
132+
/** next container in stack */
128133
n: Container | null;
129134
};
130135

@@ -169,7 +174,7 @@ export const decodeFirst = (buf: Uint8Array): [value: any, remainder: Uint8Array
169174
value = arr;
170175

171176
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 };
173178
continue jump;
174179
}
175180

@@ -181,7 +186,7 @@ export const decodeFirst = (buf: Uint8Array): [value: any, remainder: Uint8Array
181186

182187
if (arg > 0) {
183188
// `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 };
185190
continue jump;
186191
}
187192

@@ -236,14 +241,7 @@ export const decodeFirst = (buf: Uint8Array): [value: any, remainder: Uint8Array
236241

237242
while (stack !== null) {
238243
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: {
247245
const obj = stack.c;
248246
const key = stack.k;
249247

@@ -265,6 +263,13 @@ export const decodeFirst = (buf: Uint8Array): [value: any, remainder: Uint8Array
265263

266264
break;
267265
}
266+
case 1: {
267+
const arr = stack.c;
268+
const index = arr.length - stack.r;
269+
270+
arr[index] = value;
271+
break;
272+
}
268273
}
269274

270275
if (--stack.r !== 0) {

0 commit comments

Comments
 (0)