Skip to content

Optimisations breaking instanceof Response checks #154

@ezg27

Description

@ezg27

Hi there! It looks as though @hono/node-server overriding Web API globals is causing valid instanceof Response checks to fail in Node.js when they succeed in other applications, including the same Hono apps in other runtimes e.g. Bun, Deno, Cloudflare.

It appears to be related to the changes made in the following PR to reduce the overhead of Request and Response instance creation by replacing them with pseudo objects: #95.

Since those changes were released, attempting to do instanceof Response checks against a response returned from native fetch in Node.js will return false (pre v1.3.0 this would correctly return true). Just importing @hono/node-server is enough for the globals to be overridden, so the following is all that's needed to reproduce the issue (using Node v20):

import '@hono/node-server';

const response = await fetch('https://www.example.com');
console.log(response instanceof Response); // false

While i understand the motivation to try and reduce overhead in Node compared to other runtimes, I'm not sure if abstractions like this are the way to go when their implementations can leak into application code. However, i'm also not sure it'll be possible to achieve similar microbenchmark results as those reported in the above PR while still using the the native Request and Response.

For now i can work around the above issues by using patch-package to remove the imports/optimisations from ./request.js and ./response.js so that instanceof checks work as expected. However, if the aim is to keep the current overrides, would you be open to adding an option to disable them as part of the library (e.g. an optional disableOverrides that can be passed to serve)? I'd be happy to raise a PR for this, but will defer to you on the best way forward. At the very least i reckon it's worth documenting the overrides and these potential issues (the README currently still says It utilizes web standard APIs implemented in Node.js version 18 or higher.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions