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

Wrap blobs into a node stream #1092

Closed
Closed
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
5 changes: 3 additions & 2 deletions src/body.js
Expand Up @@ -14,6 +14,7 @@ import {FetchError} from './errors/fetch-error.js';
import {FetchBaseError} from './errors/base.js';
import {formDataIterator, getBoundary, getFormDataLength} from './utils/form-data.js';
import {isBlob, isURLSearchParameters, isFormData} from './utils/is.js';
import {blobToNodeStream} from './utils/blob-to-stream.js';

const INTERNALS = Symbol('Body internals');

Expand Down Expand Up @@ -177,7 +178,7 @@ async function consumeBody(data) {

// Body is blob
if (isBlob(body)) {
body = body.stream();
body = blobToNodeStream(body);
}

// Body is buffer
Expand Down Expand Up @@ -371,7 +372,7 @@ export const writeToStream = (dest, {body}) => {
dest.end();
} else if (isBlob(body)) {
// Body is Blob
body.stream().pipe(dest);
blobToNodeStream(body).pipe(dest);
} else if (Buffer.isBuffer(body)) {
// Body is buffer
dest.write(body);
Expand Down
20 changes: 20 additions & 0 deletions src/utils/blob-to-stream.js
@@ -0,0 +1,20 @@
import {Readable} from 'stream';

/* c8 ignore start */
async function * read(blob) {
let position = 0;
while (position !== blob.size) {
const chunk = blob.slice(position, Math.min(blob.size, position + 524288));
jimmywarting marked this conversation as resolved.
Show resolved Hide resolved
// eslint-disable-next-line no-await-in-loop
const buffer = await chunk.arrayBuffer();
position += buffer.byteLength;
yield new Uint8Array(buffer);
}
}
/* c8 ignore end */

export function blobToNodeStream(blob) {
return Readable.from(blob.stream ? blob.stream() : read(blob), {
objectMode: false
});
}
8 changes: 6 additions & 2 deletions src/utils/is.js
Expand Up @@ -38,9 +38,13 @@ export const isBlob = object => {
typeof object === 'object' &&
typeof object.arrayBuffer === 'function' &&
typeof object.type === 'string' &&
typeof object.stream === 'function' &&
// typeof object.stream === 'function' &&
typeof object.constructor === 'function' &&
/^(Blob|File)$/.test(object[NAME])
(
/* c8 ignore next 2 */
/^(Blob|File)$/.test(object[NAME]) ||
/^(Blob|File)$/.test(object.constructor.name)
)
);
};

Expand Down
10 changes: 10 additions & 0 deletions test/response.js
Expand Up @@ -3,6 +3,7 @@ import * as stream from 'stream';
import {TextEncoder} from 'util';
import chai from 'chai';
import Blob from 'fetch-blob';
import buffer from 'buffer';
import {Response} from '../src/index.js';
import TestServer from './utils/server.js';

Expand Down Expand Up @@ -173,6 +174,15 @@ describe('Response', () => {
});
});

if (buffer.Blob) {
it('should support Buffer.Blob as body', () => {
const res = new Response(new buffer.Blob(['a=1']));
return res.text().then(result => {
expect(result).to.equal('a=1');
});
});
}

it('should support Uint8Array as body', () => {
const encoder = new TextEncoder();
const res = new Response(encoder.encode('a=1'));
Expand Down