Skip to content

Commit

Permalink
fix: response throwing error due to javascript bug (#117)
Browse files Browse the repository at this point in the history
* fix: response throwing error due to javascript Typed Array prototype design fault

* server: support ArrayBuffer response

* Add comments

---------

Co-authored-by: Frank <wangfanjie@gmail.com>
  • Loading branch information
khuezy and fwang authored May 18, 2023
1 parent e9226d8 commit 4bd2009
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/polite-rice-learn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"open-next": patch
---

server: support ArrayBuffer response
13 changes: 9 additions & 4 deletions packages/open-next/src/adapters/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,29 @@ const BODY = Symbol();
const HEADERS = Symbol();

function getString(data) {
// Note: use `ArrayBuffer.isView()` to check for Uint8Array. Using
// `instanceof Uint8Array` returns false in some cases. For example,
// when the buffer is created in middleware and passed to NextServer.
if (Buffer.isBuffer(data)) {
return data.toString("utf8");
} else if (ArrayBuffer.isView(data)) {
return Buffer.from(data).toString("utf8");
} else if (typeof data === "string") {
return data;
} else {
throw new Error(`response.write() of unexpected type: ${typeof data}`);
throw new Error(`response.getString() of unexpected type: ${typeof data}`);
}
}

function addData(stream, data) {
if (
Buffer.isBuffer(data) ||
typeof data === "string" ||
data instanceof Uint8Array
ArrayBuffer.isView(data) ||
typeof data === "string"
) {
stream[BODY].push(Buffer.from(data));
} else {
throw new Error(`response.write() of unexpected type: ${typeof data}`);
throw new Error(`response.addData() of unexpected type: ${typeof data}`);
}
}

Expand Down

1 comment on commit 4bd2009

@vercel
Copy link

@vercel vercel bot commented on 4bd2009 May 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

open-next – ./

open-next.vercel.app
open-next-sst-dev.vercel.app
open-next-git-main-sst-dev.vercel.app

Please sign in to comment.