Skip to content

Support for custom encoders #18

@zbjornson

Description

@zbjornson

msgpack-lite supports Custom Extension Types (Codecs). There's a project that I'd love to switch to this faster lib, but it uses a codec.

(Thanks for maintaining this fork btw!)

msgpack-lite's API for that is okay and maybe the best choice for an easy migration path, but you could also consider some class API like:

// impl
exports.ToMsgPackSym = const ToMsgPackSym = Symbol("notepack.tomsgpack");
// in `_encode`
if (typeof value[ToMsgPackSym] === "function") {
  bytes.push(value.constructor[MsgPackTypeSym], ...value[ToMsgPackSym](value));
}

// usage
class MyClass {
  static [notepack.MsgPackTypeSym] = 0x00;
  [notepack.ToMsgPackSym]() {
    return /*something*/;
  }
  [notepack.FromMsgPackSym](bytes) {
    return new MyClass(/*something*/);
  }
}
notepack.registerType(MyClass);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions