trigger.dev icon indicating copy to clipboard operation
trigger.dev copied to clipboard

[TRI-4135] Add support for Blob in payloads and outputs.

Open matt-aitken opened this issue 1 year ago • 1 comments

We use SuperJSON so you can use types beyond regular JSON in your payloads/outputs. It would be great to add Blob to this.

The source code is here: https://github.com/triggerdotdev/trigger.dev/blob/f7bf7bc2687e984655436592dd65a154e0c56dc5/packages/core/src/v3/utils/ioSerialization.ts#L419

TRI-4135

matt-aitken avatar Dec 02 '24 15:12 matt-aitken

@matt-aitken here's what I tried for adding Blob to SuperJSON

superjson.registerCustom<Blob, number[]>(
    {
      isApplicable: (v): v is Blob => typeof Blob === "function" && v instanceof Blob,
      serialize: async (v) => {
        const arrayBuffer = await v.arrayBuffer();
        return [...(new Uint8Array(arrayBuffer))];
      },
      deserialize: (v) => {
        const u8Array = new Uint8Array(v);
        const arrayBuffer = u8Array.buffer.slice(u8Array.byteOffset, u8Array.byteOffset + u8Array.byteLength);
        return new Blob([arrayBuffer]);
      },
    },
    "blob"
  );

but unfortunately serialize function cannot return promise and Blob cannot be serialized synchronously. Would you consider other approach for this?

chandraguptgosavi avatar Jan 12 '25 19:01 chandraguptgosavi