diff --git a/.changeset/polite-rice-learn.md b/.changeset/polite-rice-learn.md new file mode 100644 index 00000000..c606b0f5 --- /dev/null +++ b/.changeset/polite-rice-learn.md @@ -0,0 +1,5 @@ +--- +"open-next": patch +--- + +server: support ArrayBuffer response diff --git a/packages/open-next/src/adapters/response.ts b/packages/open-next/src/adapters/response.ts index 88d2c41c..22375058 100644 --- a/packages/open-next/src/adapters/response.ts +++ b/packages/open-next/src/adapters/response.ts @@ -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}`); } }