Skip to content

Commit 4bd2009

Browse files
khuezyfwang
andauthored
fix: response throwing error due to javascript bug (#117)
* fix: response throwing error due to javascript Typed Array prototype design fault * server: support ArrayBuffer response * Add comments --------- Co-authored-by: Frank <[email protected]>
1 parent e9226d8 commit 4bd2009

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

.changeset/polite-rice-learn.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"open-next": patch
3+
---
4+
5+
server: support ArrayBuffer response

packages/open-next/src/adapters/response.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,29 @@ const BODY = Symbol();
1111
const HEADERS = Symbol();
1212

1313
function getString(data) {
14+
// Note: use `ArrayBuffer.isView()` to check for Uint8Array. Using
15+
// `instanceof Uint8Array` returns false in some cases. For example,
16+
// when the buffer is created in middleware and passed to NextServer.
1417
if (Buffer.isBuffer(data)) {
1518
return data.toString("utf8");
19+
} else if (ArrayBuffer.isView(data)) {
20+
return Buffer.from(data).toString("utf8");
1621
} else if (typeof data === "string") {
1722
return data;
1823
} else {
19-
throw new Error(`response.write() of unexpected type: ${typeof data}`);
24+
throw new Error(`response.getString() of unexpected type: ${typeof data}`);
2025
}
2126
}
2227

2328
function addData(stream, data) {
2429
if (
2530
Buffer.isBuffer(data) ||
26-
typeof data === "string" ||
27-
data instanceof Uint8Array
31+
ArrayBuffer.isView(data) ||
32+
typeof data === "string"
2833
) {
2934
stream[BODY].push(Buffer.from(data));
3035
} else {
31-
throw new Error(`response.write() of unexpected type: ${typeof data}`);
36+
throw new Error(`response.addData() of unexpected type: ${typeof data}`);
3237
}
3338
}
3439

0 commit comments

Comments
 (0)