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

fetch-mock Response.body is not always a stream in Node.js #609

Closed
fluggo opened this issue May 15, 2021 · 3 comments
Closed

fetch-mock Response.body is not always a stream in Node.js #609

fluggo opened this issue May 15, 2021 · 3 comments

Comments

@fluggo
Copy link

fluggo commented May 15, 2021

WhatWG fetch API spec requires that Body.body is of type ReadableStream. node-fetch interprets the standard as requiring a Node.js Readable stream. fetch-mock returns a Buffer if anything other than a Readable is given to the Response object.

In Node:

const fetchMock = require('fetch-mock');
const fetch = fetchMock.sandbox();

fetch.get('http://www.www.com.com', 'woo woo woo');

fetch('http://www.www.com.com').then(resp => resp.body.pipe(process.stdout));

...causes:

(node:36650) UnhandledPromiseRejectionWarning: TypeError: resp.body.pipe is not a function
    at /Users/fluggo/software/fetchmocktest/test-stream.js:6:56

This appears to be a bug in the ResponseBuilder, I have a patch.

fluggo pushed a commit to fluggo/fetch-mock that referenced this issue May 15, 2021
The WhatWG fetch spec requires that Body.body (and therefore Response.body)
is always a readable stream, but it doesn't always happen that way in Node.
This seems to be because ResponseBuilder tries to fetch the stream module
from fetchMock, but it looks in the wrong place.

Fixes wheresrhys#609
fluggo pushed a commit to fluggo/fetch-mock that referenced this issue May 15, 2021
The WhatWG fetch spec requires that Body.body (and therefore Response.body)
is always a readable stream, but it doesn't always happen that way in Node.
This seems to be because ResponseBuilder tries to fetch the stream module
from fetchMock, but it looks in the wrong place.

Fixes wheresrhys#609
fluggo pushed a commit to fluggo/fetch-mock that referenced this issue May 15, 2021
The WhatWG fetch spec requires that Body.body (and therefore Response.body)
is always a readable stream, but it doesn't always happen that way in Node.
This seems to be because ResponseBuilder tries to fetch the stream module
from fetchMock, but it looks in the wrong place.

Fixes wheresrhys#609
@fluggo
Copy link
Author

fluggo commented May 15, 2021

Workaround:

const stream = require('stream');
const _baseResponseBuilder = require('fetch-mock/cjs/lib/response-builder');

require.cache[require.resolve('fetch-mock/cjs/lib/response-builder')] = {
  ..._baseResponseBuilder,
  exports: (options) => _baseResponseBuilder({ ...options, Stream: stream }),
};

const nodeFetch = require('node-fetch');
// and so forth

@jimmywarting
Copy link

in node-fetch@v3 the res.body is normalized into a node readable stream or null

@wheresrhys
Copy link
Owner

Should not be a problem any more in fetch-mock@10

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 a pull request may close this issue.

3 participants