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

Error trying to deserialize Buffer() #181

Closed
PatrikElfstrom opened this issue Apr 22, 2022 · 6 comments
Closed

Error trying to deserialize Buffer() #181

PatrikElfstrom opened this issue Apr 22, 2022 · 6 comments

Comments

@PatrikElfstrom
Copy link

PatrikElfstrom commented Apr 22, 2022

I thought support for Buffer was added in #155 from #144
but I get Error: Trying to deserialize unknown typed array

The reason for using Buffer is that Prisma is using that for storing binary files.
https://www.prisma.io/docs/concepts/components/prisma-client/working-with-fields#working-with-bytes

Demo: https://runkit.com/patrikelfstrom/6262c0aac86357000827f8fe

Maybe there is better way of doing this? I have not really worked with typed arrays before.

@Skn0tt
Copy link
Collaborator

Skn0tt commented Apr 25, 2022

Interesting! Here's a list of supported types of typed arrays: https://github.com/blitz-js/superjson/pull/155/files#diff-df7446068188621a3fa66e02d4e5c9081902f1bb2cd9092e06c59271978257d3R200

I'm hesitant to add Buffer to that list since it's generally not available in the Browser, afaik - let me google that

@PatrikElfstrom
Copy link
Author

I guess it's not generally an issue since there is often a disconnect between frontend and backend types.

I'm using Next.js and Prisma with tRCP and superjson to handle dates and so right now to get it to work I have to create a Uint8array from the Buffer before superjson and casting it as a Buffer.
new Uint8Array(file.blob) as Buffer;

But since like you say Buffer is not supported in the browser maybe it's not in superjson's scope to convert the Buffer.
And the right way is maybe to get Prisma to support something else than Buffer.

@Skn0tt
Copy link
Collaborator

Skn0tt commented Apr 25, 2022

that sounds like a good idea, yeah. maybe Prisma already has an experimental feature for that? cc @janpio

@janpio
Copy link

janpio commented Apr 25, 2022

Not that I am aware of. We let users handle the type conversion on their side - ironically often via libraries like superjson of course. If another Client side type would be useful @PatrikElfstrom, please open a feature request at prisma/prisma.

@Trolley33
Copy link

I know this closed, but for anyone else finding this link and wanting to automatically serialize a buffer (for the use case described by Patrik) - the below code snippet worked for me using Prisma + tRPC + React.

superjson.registerCustom<Buffer, number[]>(
  {
    isApplicable: (v): v is Buffer => v instanceof Buffer,
    serialize: v => [...v],
    deserialize: v => Buffer.from(v)
  },
  "buffer"
);

I placed it in my utils/trps.ts and then export superjson from there to make sure it's registered for both transformers. The result on the client is actually a Uint8Array when console.logged - but it worked for my case.

@skagedal
Copy link

skagedal commented Mar 2, 2024

@Trolley33 Could perhaps show exactly how you write to export superjson? I seem to not be getting it right...

[edit] Oh, I got it to work instead by putting it in _app.ts as described here 👍

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

5 participants