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: force bodies which are in the middle of being read to be abortable #847

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

wheresrhys
Copy link
Owner

No description provided.

@mtenberge
Copy link

mtenberge commented Nov 1, 2024

I manually applied the changes in Router directly to my local node_modules/fetch-mock/dist/esm/Router.js, and can confirm that this solves the ReadableStream is locked errors for me.

@asrashley
Copy link

I created a temporary work-around using patch-package to modify the built code, as I was also finding my vitest tests failing due to an abort() call after the request had completed. To fix all of the test errors I needed to check both the request and the response bodies.

diff --git a/node_modules/fetch-mock/dist/esm/Router.js b/node_modules/fetch-mock/dist/esm/Router.js
index 599e79f..ef9ed5d 100644
--- a/node_modules/fetch-mock/dist/esm/Router.js
+++ b/node_modules/fetch-mock/dist/esm/Router.js
@@ -84,10 +84,19 @@ export default class Router {
                     const error = new DOMException('The operation was aborted.', 'AbortError');
                     const requestBody = request?.body || options?.body;
                     if (requestBody instanceof ReadableStream) {
-                        requestBody.cancel(error);
+                        if (requestBody.locked) {
+                                                       requestBody.getReader().cancel(error);
+                                               } else {
+                                                       requestBody.cancel(error);
+                                               }
                     }
-                    if (callLog?.response?.body) {
-                        callLog.response.body.cancel(error);
+                    const responseBody = callLog?.response?.body;
+                    if (responseBody) {
+                        if (responseBody instanceof ReadableStream && responseBody.locked) {
+                                                       responseBody.getReader().cancel(error);
+                        } else {
+                            responseBody.cancel(error);
+                        }
                     }
                     reject(error);
                 };

asrashley pushed a commit to asrashley/dash-live that referenced this pull request Jan 18, 2025
The fetch-mock library does not correctly handle the case of an abort
controller firing after a request has completed. This is a known issue
but not yet fixed upstream.

see wheresrhys/fetch-mock#847
asrashley pushed a commit to asrashley/dash-live that referenced this pull request Jan 22, 2025
The fetch-mock library does not correctly handle the case of an abort
controller firing after a request has completed. This is a known issue
but not yet fixed upstream.

see wheresrhys/fetch-mock#847
asrashley pushed a commit to asrashley/dash-live that referenced this pull request Jan 25, 2025
The fetch-mock library does not correctly handle the case of an abort
controller firing after a request has completed. This is a known issue
but not yet fixed upstream.

see wheresrhys/fetch-mock#847
asrashley pushed a commit to asrashley/dash-live that referenced this pull request Jan 26, 2025
The fetch-mock library does not correctly handle the case of an abort
controller firing after a request has completed. This is a known issue
but not yet fixed upstream.

see wheresrhys/fetch-mock#847
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.

3 participants