[Bug?]: Buffer is converted to an array-like object on the client-side
Duplicates
- [x] I have searched the existing issues
Latest version
- [x] I have tested the latest version
Current behavior 😯
{0: 102, 1: 240, 2: 142, 3: 156, 4: 217, 5: 251, 6: 19, 7: 196, 8: 113, 9: 88, 10: 46, 11: 27, 12: 246, 13: 206, 14: 74, 15: 43}
Expected behavior 🤔
[102,240,142,156,217,251,19,196,113,88,46,27,246,206,74,43]
Steps to reproduce 🕹
import { createAsync, query } from "@solidjs/router";
import { randomBytes } from "node:crypto";
const getData = query(async () => {
"use server";
return randomBytes(16);
}, "data");
export default function Home() {
const data = createAsync(() => getData());
console.log(data());
return (
<>
<div>Hello World</div>
</>
);
}
go to chrome console and you'll see
Context 🔦
this didn't happen before solidstart started using tanstack server functions
Your environment 🌎
this issue should probably be moved to @tanstack/server-functions-plugin instead
Could you maybe PR a failing test for this?
this issue should probably be moved to @tanstack/server-functions-plugin instead
this is a seroval behavior so this is fine. (@tanstack/server-functions-plugin doens't define the serializer, we do)
Now for why this happens. The reason Buffer instances aren't serialized into an array is because Buffers aren't really arrays, Buffers are typed arrays (specifically, Uint8Array). Although interestingly enough, it's not serialized as a Uint8Array because seroval doesn't check for class inheritance, so the serialization now falls back to plain object serialization (which is why it is an object in the log). I'm honestly not sure if I should change the behavior or not, because it impacts the perf badly.
this issue should probably be moved to @tanstack/server-functions-plugin instead
this is a seroval behavior so this is fine. (
@tanstack/server-functions-plugindoens't define the serializer, we do)Now for why this happens. The reason
Bufferinstances aren't serialized into an array is because Buffers aren't really arrays, Buffers are typed arrays (specifically,Uint8Array). Although interestingly enough, it's not serialized as aUint8Arraybecause seroval doesn't check for class inheritance, so the serialization now falls back to plain object serialization (which is why it is an object in the log). I'm honestly not sure if I should change the behavior or not, because it impacts the perf badly.
it should definitely be a Uint8Array
I'll leave that open-ended when we expose the Plugin API. This will be no-fix in seroval
no-fix
what does no-fix mean?
it means I won't change the current behavior in seroval. Once the Plugin API is exposed in SolidStart, I'll let users decide how to serialize Buffers
seroval issue