Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: response throwing error due to javascript bug #117

Merged
merged 3 commits into from
May 18, 2023

Conversation

khuezy
Copy link
Contributor

@khuezy khuezy commented May 18, 2023

When using NextResponse in middleware, the Uint8Array instance type becomes corrupted in some way due to cross realm context????
eg:

return new NextResponse(JSON.stringify({})), {status: 200, headers: {'content-type': 'application/json'}})

The data instanceof Uint8Array returns false b/c the data traveled far across realms: middleware => server

per chatGPT:

The expression `uint8Array instanceof Uint8Array` can sometimes return `false` in JavaScript due to the way objects and prototypes work.

In JavaScript, the `instanceof` operator checks if an object is an instance of a specific constructor. It traverses the prototype chain of the object to determine if the object inherits from the specified constructor's prototype.

However, there are situations where the prototype chain can be modified or the object's constructor can be changed, which can lead to unexpected results when using the `instanceof` operator.

Here are a few possible reasons why `uint8Array instanceof Uint8Array` may return `false`:

1. Context or Cross-Realm Issues: If the `Uint8Array` constructor used to create the `uint8Array` object comes from a different JavaScript context or realm than the one where the `instanceof` check is performed, it can result in `false`. Each context or realm has its own set of built-in constructors, including `Uint8Array`, and they are not shared across different contexts.

2. Prototype Modifications: If the prototype of `Uint8Array` or any of its parent prototypes is modified, the `instanceof` check may no longer produce the expected result.

3. Shadowing: If the `Uint8Array` constructor or its prototype is shadowed or overridden by a different constructor or object, the `instanceof` check may not work as intended.

To ensure consistent and reliable type checking, it's generally recommended to use other methods such as `ArrayBuffer.isView()` or `ArrayBuffer.isView(uint8Array)` to specifically check if an object is a typed array, like `Uint8Array`. These methods are more reliable for type checking typed array objects.

Example:

```javascript
const isUint8Array = ArrayBuffer.isView(uint8Array) && uint8Array.constructor === Uint8Array;
console.log(isUint8Array); // true

In this example, we use ArrayBuffer.isView() to check if the uint8Array is a typed array, and then verify if its constructor matches Uint8Array.

By using specific methods for type checking, you can avoid potential issues with the instanceof operator.

@changeset-bot
Copy link

changeset-bot bot commented May 18, 2023

🦋 Changeset detected

Latest commit: 1e0ced2

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
open-next Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@vercel
Copy link

vercel bot commented May 18, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
open-next ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 18, 2023 10:01pm

@@ -11,24 +11,24 @@ const BODY = Symbol();
const HEADERS = Symbol();

function getString(data) {
if (Buffer.isBuffer(data)) {
if (Buffer.isBuffer(data) || ArrayBuffer.isView(data)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should ArrayBuffer be converted like this:

Buffer.from(data).toString("utf8");

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm when I do that, the result is empty.

@seed-deploy seed-deploy bot temporarily deployed to pr117 May 18, 2023 22:00 Inactive
@fwang fwang merged commit 4bd2009 into opennextjs:main May 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants