Skip to content

adding support for nonstandard map key in the decoder #266

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
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
25 changes: 14 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,17 +145,20 @@ NodeJS `Buffer` is also acceptable because it is a subclass of `Uint8Array`.

#### `DecoderOptions`

| Name | Type | Default |
| -------------- | -------------- | ----------------------------- |
| extensionCodec | ExtensionCodec | `ExtensionCodec.defaultCodec` |
| context | user-defined | - |
| useBigInt64 | boolean | false |
| rawStrings | boolean | false |
| maxStrLength | number | `4_294_967_295` (UINT32_MAX) |
| maxBinLength | number | `4_294_967_295` (UINT32_MAX) |
| maxArrayLength | number | `4_294_967_295` (UINT32_MAX) |
| maxMapLength | number | `4_294_967_295` (UINT32_MAX) |
| maxExtLength | number | `4_294_967_295` (UINT32_MAX) |
| Name | Type | Default |
| --------------- | ------------------- | ---------------------------------------------- |
| extensionCodec | ExtensionCodec | `ExtensionCodec.defaultCodec` |
| context | user-defined | - |
| useBigInt64 | boolean | false |
| rawStrings | boolean | false |
| maxStrLength | number | `4_294_967_295` (UINT32_MAX) |
| maxBinLength | number | `4_294_967_295` (UINT32_MAX) |
| maxArrayLength | number | `4_294_967_295` (UINT32_MAX) |
| maxMapLength | number | `4_294_967_295` (UINT32_MAX) |
| maxExtLength | number | `4_294_967_295` (UINT32_MAX) |
| mapKeyConverter | MapKeyConverterType | throw exception if key is not string or number |

`MapKeyConverterType` is defined as `(key: unknown) => string | number`.

To skip UTF-8 decoding of strings, `rawStrings` can be set to `true`. In this case, strings are decoded into `Uint8Array`.

Expand Down
21 changes: 15 additions & 6 deletions src/Decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ export type DecoderOptions<ContextType = undefined> = Readonly<
* `null` is a special value to disable the use of the key decoder at all.
*/
keyDecoder: KeyDecoder | null;

/**
* A function to convert decoded map key to a valid JS key type.
*
* Defaults to a function that throws an error if the key is not a string or a number.
*/
mapKeyConverter: (key: unknown) => MapKeyType;
}>
> &
ContextOf<ContextType>;
Expand All @@ -78,8 +85,11 @@ const STATE_MAP_VALUE = "map_value";

type MapKeyType = string | number;

const isValidMapKeyType = (key: unknown): key is MapKeyType => {
return typeof key === "string" || typeof key === "number";
const mapKeyConverter = (key: unknown): MapKeyType => {
if (typeof key === "string" || typeof key === "number") {
return key;
}
throw new DecodeError("The type of key must be string or number but " + typeof key);
};

type StackMapState = {
Expand Down Expand Up @@ -213,6 +223,7 @@ export class Decoder<ContextType = undefined> {
private readonly maxMapLength: number;
private readonly maxExtLength: number;
private readonly keyDecoder: KeyDecoder | null;
private readonly mapKeyConverter: (key: unknown) => MapKeyType;

private totalPos = 0;
private pos = 0;
Expand All @@ -236,6 +247,7 @@ export class Decoder<ContextType = undefined> {
this.maxMapLength = options?.maxMapLength ?? UINT32_MAX;
this.maxExtLength = options?.maxExtLength ?? UINT32_MAX;
this.keyDecoder = options?.keyDecoder !== undefined ? options.keyDecoder : sharedCachedKeyDecoder;
this.mapKeyConverter = options?.mapKeyConverter ?? mapKeyConverter;
}

private clone(): Decoder<ContextType> {
Expand Down Expand Up @@ -631,14 +643,11 @@ export class Decoder<ContextType = undefined> {
continue DECODE;
}
} 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);
}
if (object === "__proto__") {
throw new DecodeError("The key __proto__ is not allowed");
}

state.key = object;
state.key = this.mapKeyConverter(object);
state.type = STATE_MAP_VALUE;
continue DECODE;
} else {
Expand Down
15 changes: 15 additions & 0 deletions test/decodeAsync.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,21 @@ describe("decodeAsync", () => {
assert.deepStrictEqual(object, { "foo": "bar" });
});

it("decodes fixmap {'[1, 2]': 'baz'} with custom map key converter", async () => {
const createStream = async function* () {
yield [0x81]; // fixmap size=1
yield encode([1, 2]);
yield encode("baz");
};

const object = await decodeAsync(createStream(), {
mapKeyConverter: (key) => JSON.stringify(key),
});

const key = JSON.stringify([1, 2]);
assert.deepStrictEqual(object, { [key]: "baz" });
});

it("decodes multi-byte integer byte-by-byte", async () => {
const createStream = async function* () {
yield [0xcd]; // uint 16
Expand Down
Loading