Skip to content

Commit 11f95f1

Browse files
authored
Merge pull request #217 from msgpack/gfx/avoid-using-typescript-specific-langauge-ext
avoid using typescript-specific language extensions (const enum)
2 parents b015e94 + 7966865 commit 11f95f1

File tree

2 files changed

+41
-15
lines changed

2 files changed

+41
-15
lines changed

src/Decoder.ts

+12-14
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@ import { createDataView, ensureUint8Array } from "./utils/typedArrays";
66
import { CachedKeyDecoder, KeyDecoder } from "./CachedKeyDecoder";
77
import { DecodeError } from "./DecodeError";
88

9-
const enum State {
10-
ARRAY,
11-
MAP_KEY,
12-
MAP_VALUE,
13-
}
9+
const STATE_ARRAY = "array";
10+
const STATE_MAP_KEY = "map_key";
11+
const STATE_MAP_VALUE = "map_value";
1412

1513
type MapKeyType = string | number;
1614

@@ -21,15 +19,15 @@ const isValidMapKeyType = (key: unknown): key is MapKeyType => {
2119
};
2220

2321
type StackMapState = {
24-
type: State.MAP_KEY | State.MAP_VALUE;
22+
type: typeof STATE_MAP_KEY | typeof STATE_MAP_VALUE;
2523
size: number;
2624
key: MapKeyType | null;
2725
readCount: number;
2826
map: Record<string, unknown>;
2927
};
3028

3129
type StackArrayState = {
32-
type: State.ARRAY;
30+
type: typeof STATE_ARRAY;
3331
size: number;
3432
array: Array<unknown>;
3533
position: number;
@@ -397,7 +395,7 @@ export class Decoder<ContextType = undefined> {
397395
while (stack.length > 0) {
398396
// arrays and maps
399397
const state = stack[stack.length - 1]!;
400-
if (state.type === State.ARRAY) {
398+
if (state.type === STATE_ARRAY) {
401399
state.array[state.position] = object;
402400
state.position++;
403401
if (state.position === state.size) {
@@ -406,7 +404,7 @@ export class Decoder<ContextType = undefined> {
406404
} else {
407405
continue DECODE;
408406
}
409-
} else if (state.type === State.MAP_KEY) {
407+
} else if (state.type === STATE_MAP_KEY) {
410408
if (!isValidMapKeyType(object)) {
411409
throw new DecodeError("The type of key must be string or number but " + typeof object);
412410
}
@@ -415,7 +413,7 @@ export class Decoder<ContextType = undefined> {
415413
}
416414

417415
state.key = object;
418-
state.type = State.MAP_VALUE;
416+
state.type = STATE_MAP_VALUE;
419417
continue DECODE;
420418
} else {
421419
// it must be `state.type === State.MAP_VALUE` here
@@ -428,7 +426,7 @@ export class Decoder<ContextType = undefined> {
428426
object = state.map;
429427
} else {
430428
state.key = null;
431-
state.type = State.MAP_KEY;
429+
state.type = STATE_MAP_KEY;
432430
continue DECODE;
433431
}
434432
}
@@ -475,7 +473,7 @@ export class Decoder<ContextType = undefined> {
475473
}
476474

477475
this.stack.push({
478-
type: State.MAP_KEY,
476+
type: STATE_MAP_KEY,
479477
size,
480478
key: null,
481479
readCount: 0,
@@ -489,7 +487,7 @@ export class Decoder<ContextType = undefined> {
489487
}
490488

491489
this.stack.push({
492-
type: State.ARRAY,
490+
type: STATE_ARRAY,
493491
size,
494492
array: new Array<unknown>(size),
495493
position: 0,
@@ -523,7 +521,7 @@ export class Decoder<ContextType = undefined> {
523521
private stateIsMapKey(): boolean {
524522
if (this.stack.length > 0) {
525523
const state = this.stack[this.stack.length - 1]!;
526-
return state.type === State.MAP_KEY;
524+
return state.type === STATE_MAP_KEY;
527525
}
528526
return false;
529527
}

test/edge-cases.test.ts

+29-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// kind of hand-written fuzzing data
22
// any errors should not break Encoder/Decoder instance states
33
import assert from "assert";
4-
import { encode, decodeAsync, decode, Encoder, Decoder, decodeMulti, decodeMultiStream } from "../src";
4+
import { encode, decodeAsync, decode, Encoder, Decoder, decodeMulti, decodeMultiStream, ExtensionCodec } from "../src";
55
import { DataViewIndexOutOfBoundsError } from "../src/Decoder";
66

77
function testEncoder(encoder: Encoder): void {
@@ -181,4 +181,32 @@ describe("edge cases", () => {
181181
assert.deepStrictEqual(results, []);
182182
});
183183
});
184+
185+
context("xxx", () => {
186+
it("xxx", () => {
187+
function encodeStatsJson(stats: any) {
188+
const extensionCodec = new ExtensionCodec();
189+
const filteredFields = ['name'];
190+
extensionCodec.register({
191+
type: 0,
192+
encode: (object: any) => {
193+
console.log(object);
194+
if (object instanceof Object && !(object instanceof Array)) {
195+
for (const field of filteredFields) {
196+
delete object[field];
197+
}
198+
}
199+
console.log(object);
200+
return null;
201+
},
202+
decode: () => null,
203+
});
204+
205+
const encoded = encode(stats, { extensionCodec });
206+
return encoded;
207+
}
208+
209+
console.log(encodeStatsJson({ id: "xxx", name: 'foo', bar: 'baz', xxx: { name: 1, b: 2 } }));
210+
});
211+
});
184212
});

0 commit comments

Comments
 (0)