msw icon indicating copy to clipboard operation
msw copied to clipboard

[undici] throw new RangeError('init["status"] must be in the range of 200 to 599, inclusive.');

Open Acetyld opened this issue 1 year ago • 2 comments

Prerequisites

Environment check

  • [X] I'm using the latest msw version
  • [X] I'm using Node.js version 18 or higher

Node.js version

v22.3.0

Reproduction repository

https://github.com/stichingsd-vitrion/msw_jest_undici_reproduction

Reproduction steps

  1. Clone project
  2. Run "yarn"
  3. Run "yarn test" once or twice

Current behavior

      at src/dummy.test.ts:4:33

 PASS  src/dummy.test.ts
  ✓ msw (124 ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        1.266 s, estimated 2 s
Ran all test suites.
node:internal/deps/undici/undici:9037
        throw new RangeError('init["status"] must be in the range of 200 to 599, inclusive.');
        ^

RangeError: init["status"] must be in the range of 200 to 599, inclusive.
    at initializeResponse (node:internal/deps/undici/undici:9037:15)
    at new Response (node:internal/deps/undici/undici:8829:9)
    at MockHttpSocket.onResponseStart (/Users/diongrendelman/Projects/verbleif/my-jest-project/node_modules/@mswjs/interceptors/src/interceptors/ClientRequest/MockHttpSocket.ts:547:22)
    at Object.read (/Users/diongrendelman/Projects/verbleif/my-jest-project/node_modules/@mswjs/interceptors/src/interceptors/ClientRequest/MockHttpSocket.ts:80:31)
    at MockHttpSocket.push (/Users/diongrendelman/Projects/verbleif/my-jest-project/node_modules/@mswjs/interceptors/src/interceptors/Socket/MockSocket.ts:56:18)
    at Socket.<anonymous> (/Users/diongrendelman/Projects/verbleif/my-jest-project/node_modules/@mswjs/interceptors/src/interceptors/ClientRequest/MockHttpSocket.ts:247:14)
    at Socket.emit (node:events:520:28)
    at addChunk (node:internal/streams/readable:559:12)
    at readableAddChunkPushByteMode (node:internal/streams/readable:510:3)
    at Socket.Readable.push (node:internal/streams/readable:390:5)
    at TCP.onStreamRead (node:internal/stream_base_commons:191:23)

Node.js v22.3.0
error Command failed with exit code 1.

Expected behavior

No errors recarding Undici

Acetyld avatar Oct 07 '24 14:10 Acetyld

Hi, @Acetyld. Thanks for reporting this.

I looked at the repo briefly, your usage seems to be okay. This may be a bug in MSW. I will explore this in more detail once I have a moment.

The error suggests that the underlying socket instance starts receiving a response whose status code is a non-configurable range (200 - 599):

/Users/diongrendelman/Projects/verbleif/my-jest-project/node_modules/@mswjs/interceptors/src/interceptors/ClientRequest/MockHttpSocket.ts:547:22

You can log out that response header received on that line (just go to the build *.js/*.mjs file, not the *.ts) and see what kind of response is that, and whether it gives you any hints why your test receives that response.

I'm quite curious what kind of response that may be.

kettanaito avatar Oct 08 '24 09:10 kettanaito

Cool, i tried looking int he builded js/mjs files but couldt reference it my self.

Acetyld avatar Oct 08 '24 11:10 Acetyld

Hi @kettanaito it all happens because of the websocket stuff image

I get a 201. Can i just disable the websocket stuff or something?

Acetyld avatar Oct 21 '24 16:10 Acetyld

Are you using WebSockets in tests? I wonder what triggers 101, to begin with. That has to be your tested code or your test environment.

kettanaito avatar Oct 21 '24 17:10 kettanaito

Are you using WebSockets in tests? I wonder what triggers 101, to begin with. That has to be your tested code or your test environment.

Yhea i created a full reproduction see https://github.com/stichingsd-vitrion/msw_jest_undici_reproduction

I do nothing special in my opinion. Do you also get it with repro? Would love to hear =)

Acetyld avatar Oct 21 '24 17:10 Acetyld

diff --git a/node_modules/@mswjs/interceptors/lib/node/chunk-FPLETXGA.js b/node_modules/@mswjs/interceptors/lib/node/chunk-FPLETXGA.js
index 648938e..85e8a13 100644
--- a/node_modules/@mswjs/interceptors/lib/node/chunk-FPLETXGA.js
+++ b/node_modules/@mswjs/interceptors/lib/node/chunk-FPLETXGA.js
@@ -348,6 +348,7 @@ var MockHttpSocket = class extends MockSocket {
         this.responseStream = new (0, _stream.Readable)({ read() {
         } });
       }
+      let newStatus = status > 200 ? status : 200
       const response = new Response(
         /**
          * @note The Fetch API response instance exposed to the consumer
@@ -358,7 +359,7 @@ var MockHttpSocket = class extends MockSocket {
          */
         canHaveBody ? _stream.Readable.toWeb(this.responseStream) : null,
         {
-          status,
+          status:newStatus,
           statusText,
           headers
         }
diff --git a/node_modules/@mswjs/interceptors/lib/node/chunk-MG3S53QP.mjs b/node_modules/@mswjs/interceptors/lib/node/chunk-MG3S53QP.mjs
index c6d2383..33a8411 100644
--- a/node_modules/@mswjs/interceptors/lib/node/chunk-MG3S53QP.mjs
+++ b/node_modules/@mswjs/interceptors/lib/node/chunk-MG3S53QP.mjs
@@ -348,6 +348,8 @@ var MockHttpSocket = class extends MockSocket {
         this.responseStream = new Readable({ read() {
         } });
       }
+      let newStatus = status > 200 ? status : 200;
+
       const response = new Response(
         /**
          * @note The Fetch API response instance exposed to the consumer
@@ -358,7 +360,7 @@ var MockHttpSocket = class extends MockSocket {
          */
         canHaveBody ? Readable.toWeb(this.responseStream) : null,
         {
-          status,
+          status:newStatus,
           statusText,
           headers
         }

For now ill use this as a patch-package

Acetyld avatar Oct 23 '24 10:10 Acetyld

same issue problem = console-ninja extenstion vscode console ninja using socket

wslp12 avatar Nov 04 '24 04:11 wslp12

Yeah, or e.g. Effect Devtools using socket

tsteckenborn avatar Nov 09 '24 12:11 tsteckenborn

@Acetyld, thanks for publishing a reproduction repo! That package patch though, I don't recommend doing that. You are disrupting the data transfer, and responses like 101 won't be received and handled properly by Node.js. Do that at your own risk.

I will take a look at your repo once I have a minute and track down the root cause for this.

From the looks of it, this brings us back to non-configurable response status codes. We do something around this already in Interceptors, perhaps just not in MockHttpSocket.

@wslp12 @tsteckenborn thanks for confirming this happens during WebSocket usage!

kettanaito avatar Nov 15 '24 10:11 kettanaito

Yup! I also have Console Ninja, so @wslp12 thanks for pointing that out 😉

Acetyld avatar Nov 15 '24 10:11 Acetyld

The fix is written in https://github.com/mswjs/interceptors/pull/677.

kettanaito avatar Nov 15 '24 11:11 kettanaito

Released: v2.6.5 🎉

This has been released in v2.6.5!

Make sure to always update to the latest version (npm i msw@latest) to get the newest features and bug fixes.


Predictable release automation by @ossjs/release.

kettanaito avatar Nov 16 '24 01:11 kettanaito