Skip to content

v0.12.0

Compare
Choose a tag to compare
@lxsmnsyc lxsmnsyc released this 19 Oct 06:13
· 258 commits to main since this release
  • Add the Plugin API
    • This feature allows custom serialization/deserialization API in seroval. The feature required massive restructuring in the core, such as moving the whole parser, serializer and deserializer code from functions to class-based, which will allow deduping and a lot more. It's required so that the underlying methods can be exposed for the plugin methods.
    • This resolves #17
    • This resolves #14
    • Example:
    import {
      createPlugin,
      serialize,
      type SerovalNode,
    } from 'seroval';
    
    const BufferPlugin = createPlugin<Buffer, SerovalNode>({
      tag: 'Buffer',
      test(value) {
        return value instanceof Buffer;
      },
      parse: {
        sync(value, ctx) {
          return ctx.parse(value.toString('base64'));
        },
        async async(value, ctx) {
          return ctx.parse(value.toString('base64'));
        },
        stream(value, ctx) {
          return ctx.parse(value.toString('base64'));
        },
      },
      serialize(node, ctx) {
        return `Buffer.from(${ctx.serialize(node)}, "base64")`;
      },
      deserialize(node, ctx) {
        return Buffer.from(ctx.deserialize(node) as string, 'base64');
      },
      isIterable() {
        return true;
      },
    });
    
    const serializedJS = serialize(
      Buffer.from('Hello World', 'utf-8'),
      {
        plugins: [BufferPlugin],
      },
    );
  • Fixes #27
  • Fixes #28