-
Notifications
You must be signed in to change notification settings - Fork 88
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
Comments
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 |
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. But since like you say Buffer is not supported in the browser maybe it's not in superjson's scope to convert the Buffer. |
that sounds like a good idea, yeah. maybe Prisma already has an experimental feature for that? cc @janpio |
Not that I am aware of. We let users handle the type conversion on their side - ironically often via libraries like |
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. |
@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 |
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.
The text was updated successfully, but these errors were encountered: