-
|
Hello, I am using oRPC with Bun (latest version) and am trying to return a File instance gotten via Bun S3 and am running into issues. Here is the essential parts of the routes code: export const getFile = os
.route({
method: "GET",
path: "/file/{id}",
})
.input(z.object({ id: z.string() }))
.output(z.file())
.handler(async ({ input }) => {
const file = await Bun.S3.file(id)
return file
});The following error is logged to the console though when this code is ran with a known good file key/ID (and the route returns a 500): Any help in debugging this issue would be greatly appreciated. Thanks for this awesome library by the way. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
|
Hi @ethndotsh! I'm Dosu and I’m helping the orpc team. The error happens because Bun's File object isn't fully compatible with the Fetch API's Response constructor as used by oRPC—especially when ResponseInit options (like headers and status) are passed. oRPC's .output(z.file()) expects a WHATWG-compatible File or Blob, but Bun's File doesn't always meet those requirements, leading to the TypeError you saw. To work around this, you can convert the Bun File to a Blob or ArrayBuffer before returning it. For example: .handler(async ({ input }) => {
const bunFile = await Bun.S3.file(input.id);
const arrayBuffer = await bunFile.arrayBuffer();
// You can use the original file name and type if available
return new File([arrayBuffer], bunFile.name ?? "file", { type: bunFile.type ?? "application/octet-stream" });
});Also, make sure to return the File directly (not wrapped in an object) for .output(z.file())—this is required for oRPC to handle the response as a file download source. If you need more advanced streaming or want to return a native Response, you’d need to extend the handler logic or use a custom OpenAPIHandler as described in this discussion, but for most file downloads, converting to a standard File or Blob should resolve the issue. To reply, just mention @dosu. How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other |
Beta Was this translation helpful? Give feedback.
Hi @ethndotsh! I'm Dosu and I’m helping the orpc team.
The error happens because Bun's File object isn't fully compatible with the Fetch API's Response constructor as used by oRPC—especially when ResponseInit options (like headers and status) are passed. oRPC's .output(z.file()) expects a WHATWG-compatible File or Blob, but Bun's File doesn't always meet those requirements, leading to the TypeError you saw.
To work around this, you can convert the Bun File to a Blob or ArrayBuffer before returning it. For example: