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

Added ability to use Node 18 blobs #1664

Open
wants to merge 5 commits into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/body.js
Expand Up @@ -5,7 +5,7 @@
* Body interface provides common methods for Request and Response
*/

import Stream from 'stream';
import Stream, { Readable } from 'stream';

import Blob, { BUFFER } from './blob.js';
import FetchError from './fetch-error.js';
Expand Down Expand Up @@ -504,7 +504,16 @@ export function writeToStream(dest, instance) {
// body is null
dest.end();
} else if (isBlob(body)) {
body.stream().pipe(dest);
const bodyStream = body.stream();
if(bodyStream.constructor.name === 'ReadableStream') {
if('fromWeb' in Readable) {
Readable.fromWeb(body.stream()).pipe(dest);
maneesht marked this conversation as resolved.
Show resolved Hide resolved
} else {
dest.end();
}
} else {
body.stream().pipe(dest);
maneesht marked this conversation as resolved.
Show resolved Hide resolved
}
} else if (Buffer.isBuffer(body)) {
// body is buffer
dest.write(body);
Expand Down
28 changes: 28 additions & 0 deletions test/test.js
Expand Up @@ -15,6 +15,7 @@ import URLSearchParams_Polyfill from '@ungap/url-search-params';
import { URL } from 'whatwg-url';
import { AbortController } from 'abortcontroller-polyfill/dist/abortcontroller';
import AbortController2 from 'abort-controller';
import { Blob as NodeBlob} from 'buffer';

const { spawn } = require('child_process');
const http = require('http');
Expand Down Expand Up @@ -54,6 +55,7 @@ import ResponseOrig from '../src/response.js';
import Body, { getTotalBytes, extractContentType } from '../src/body.js';
import Blob from '../src/blob.js';
import zlib from "zlib";
import { Readable } from 'stream';

const supportToString = ({
[Symbol.toStringTag]: 'z'
Expand Down Expand Up @@ -1378,6 +1380,32 @@ describe('node-fetch', () => {
});
});

it('should allow POST request with node blob body with type', function() {
const url = `${base}inspect`;
const buffer = Buffer.from('a=1', 'utf-8');
const opts = {
method: 'POST',
body: new NodeBlob([buffer], {
type: 'text/plain;charset=UTF-8'
})
};
return fetch(url, opts).then(res => {
return res.json();
}).then(res => {
expect(res.method).to.equal('POST');
expect(res.body).to.equal('a=1');
expect(res.headers['transfer-encoding']).to.be.undefined;
expect(res.headers['content-type']).to.equal('text/plain;charset=utf-8');
expect(res.headers['content-length']).to.equal('3');
}, error => {
if('fromWeb' in Readable) {
expect.fail(`Unexpected error: ${error.message}`);
} else {
expect(error.message).to.contain('socket hang up');
}
});
});

it('should allow POST request with blob body with type', function() {
const url = `${base}inspect`;
const opts = {
Expand Down