Skip to content

avoid using typescript-specific language extensions (const enum) #217

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 12 additions & 14 deletions src/Decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ import { createDataView, ensureUint8Array } from "./utils/typedArrays";
import { CachedKeyDecoder, KeyDecoder } from "./CachedKeyDecoder";
import { DecodeError } from "./DecodeError";

const enum State {
ARRAY,
MAP_KEY,
MAP_VALUE,
}
const STATE_ARRAY = "array";
const STATE_MAP_KEY = "map_key";
const STATE_MAP_VALUE = "map_value";

type MapKeyType = string | number;

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

type StackMapState = {
type: State.MAP_KEY | State.MAP_VALUE;
type: typeof STATE_MAP_KEY | typeof STATE_MAP_VALUE;
size: number;
key: MapKeyType | null;
readCount: number;
map: Record<string, unknown>;
};

type StackArrayState = {
type: State.ARRAY;
type: typeof STATE_ARRAY;
size: number;
array: Array<unknown>;
position: number;
Expand Down Expand Up @@ -397,7 +395,7 @@ export class Decoder<ContextType = undefined> {
while (stack.length > 0) {
// arrays and maps
const state = stack[stack.length - 1]!;
if (state.type === State.ARRAY) {
if (state.type === STATE_ARRAY) {
state.array[state.position] = object;
state.position++;
if (state.position === state.size) {
Expand All @@ -406,7 +404,7 @@ export class Decoder<ContextType = undefined> {
} else {
continue DECODE;
}
} else if (state.type === State.MAP_KEY) {
} else if (state.type === STATE_MAP_KEY) {
if (!isValidMapKeyType(object)) {
throw new DecodeError("The type of key must be string or number but " + typeof object);
}
Expand All @@ -415,7 +413,7 @@ export class Decoder<ContextType = undefined> {
}

state.key = object;
state.type = State.MAP_VALUE;
state.type = STATE_MAP_VALUE;
continue DECODE;
} else {
// it must be `state.type === State.MAP_VALUE` here
Expand All @@ -428,7 +426,7 @@ export class Decoder<ContextType = undefined> {
object = state.map;
} else {
state.key = null;
state.type = State.MAP_KEY;
state.type = STATE_MAP_KEY;
continue DECODE;
}
}
Expand Down Expand Up @@ -475,7 +473,7 @@ export class Decoder<ContextType = undefined> {
}

this.stack.push({
type: State.MAP_KEY,
type: STATE_MAP_KEY,
size,
key: null,
readCount: 0,
Expand All @@ -489,7 +487,7 @@ export class Decoder<ContextType = undefined> {
}

this.stack.push({
type: State.ARRAY,
type: STATE_ARRAY,
size,
array: new Array<unknown>(size),
position: 0,
Expand Down Expand Up @@ -523,7 +521,7 @@ export class Decoder<ContextType = undefined> {
private stateIsMapKey(): boolean {
if (this.stack.length > 0) {
const state = this.stack[this.stack.length - 1]!;
return state.type === State.MAP_KEY;
return state.type === STATE_MAP_KEY;
}
return false;
}
Expand Down
30 changes: 29 additions & 1 deletion test/edge-cases.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// kind of hand-written fuzzing data
// any errors should not break Encoder/Decoder instance states
import assert from "assert";
import { encode, decodeAsync, decode, Encoder, Decoder, decodeMulti, decodeMultiStream } from "../src";
import { encode, decodeAsync, decode, Encoder, Decoder, decodeMulti, decodeMultiStream, ExtensionCodec } from "../src";
import { DataViewIndexOutOfBoundsError } from "../src/Decoder";

function testEncoder(encoder: Encoder): void {
Expand Down Expand Up @@ -181,4 +181,32 @@ describe("edge cases", () => {
assert.deepStrictEqual(results, []);
});
});

context("xxx", () => {
it("xxx", () => {
function encodeStatsJson(stats: any) {
const extensionCodec = new ExtensionCodec();
const filteredFields = ['name'];
extensionCodec.register({
type: 0,
encode: (object: any) => {
console.log(object);
if (object instanceof Object && !(object instanceof Array)) {
for (const field of filteredFields) {
delete object[field];
}
}
console.log(object);
return null;
},
decode: () => null,
});

const encoded = encode(stats, { extensionCodec });
return encoded;
}

console.log(encodeStatsJson({ id: "xxx", name: 'foo', bar: 'baz', xxx: { name: 1, b: 2 } }));
});
});
});