Skip to content
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

Encoder / Decoder classes dont work with new ExtensionCodec() for handling Set and Map in typescript #236

Open
noahehall opened this issue Nov 19, 2023 · 0 comments

Comments

@noahehall
Copy link

noahehall commented Nov 19, 2023

issue:

how do we use ExtensionCodec with En/Decoder classes? they dont accept an options 2nd param

even if we pass the extensionCodec in the constructor, and continue as normal, encoding fails

import {
  Encoder,
  Decoder,
  encode,
  decode,
  ExtensionCodec,
} from "@msgpack/msgpack";

// Encoder && Decoder doesnt work with extensioncodec
const extensionCodec = new ExtensionCodec();
const decoder = new Decoder({ extensionCodec });
const encoder = new Encoder({ extensionCodec });

// Set<T>
const SET_EXT_TYPE = 0; // Any in 0-127
extensionCodec.register({
  type: SET_EXT_TYPE,
  encode: (object: unknown): Uint8Array | null => {
    if (object instanceof Set) {
      // return encoder.encode([...object], { extensionCodec }); // <-- when using encode fn
      return encoder.encode([...object]);
    } else {
      return null;
    }
  },
  decode: (data: Uint8Array) => {
    // const array = decoder.decode(data, { extensionCodec }) as Array<unknown>; // <-- when using decode fn
    const array = decoder.decode(data) as Array<unknown>;
    return new Set(array);
  },
});

// Map<T>
const MAP_EXT_TYPE = 1; // Any in 0-127
extensionCodec.register({
  type: MAP_EXT_TYPE,
  encode: (object: unknown): Uint8Array => {
    if (object instanceof Map) {
      //  return encoder.encode([...object], { extensionCodec }); // <---- when using encode fn
      return encoder.encode([...object]);
    } else {
      return null;
    }
  },
  decode: (data: Uint8Array) => {
    //  const array = decoder.decode(data, { extensionCodec }) as Array< // <--- when using decode fn
    const array = decoder.decode(data) as Array<
      [unknown, unknown]
    >;
    return new Map(array);
  },
});
  • example log of error when using En/Decoder classes
 original Map(2) {
  "nirvai": Map(8) {
    "updated_at": 2023-11-19T13:43:27.000Z,
    "callsign": "nirvai",
    "created_at": 2023-11-19T13:43:27.000Z,
    "id": "15796774249725404537",
    "label": "player",
    "type": "org",
    "created_by": "15796774249725404537",
    "email": "[email protected]",
  },
  "knoa": Map(8) {
    "updated_at": 2023-11-19T13:43:27.000Z,
    "callsign": "knoa",
    "created_at": 2023-11-19T13:43:27.000Z,
    "id": "11806400024551464037",
    "label": "player",
    "type": "human",
    "created_by": "15796774249725404537",
    "email": "noah@ogobar",
  },
}
RangeError: Extra 475 of 630 byte(s) found at buffer[155]
  --> GET /v1/players 500 8ms
@noahehall noahehall changed the title Encoder / Decoder factories dont work with new ExtensionCodec() for handling Set and Map in typescript Encoder / Decoder classes dont work with new ExtensionCodec() for handling Set and Map in typescript Nov 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant