Skip to content

Commit

Permalink
support reading blob with stream (#608)
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmywarting authored and bitinn committed Apr 16, 2019
1 parent 0ad136d commit 432c9b0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
19 changes: 12 additions & 7 deletions src/blob.js
@@ -1,6 +1,8 @@
// Based on https://github.com/tmpvar/jsdom/blob/aa85b2abf07766ff7bf5c1f6daafb3726f2f2db5/lib/jsdom/living/blob.js
// (MIT licensed)

import { Readable } from 'stream';

export const BUFFER = Symbol('buffer');
const TYPE = Symbol('type');

Expand Down Expand Up @@ -57,13 +59,16 @@ export default class Blob {
const ab = buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength);
return Promise.resolve(ab);
}
// stream() {
// const readable = new Readable()
// readable._read = () => {}
// readable.push(this[BUFFER])
// readable.push(null)
// return readable || whatwg stream? not decided
// }
stream() {
const readable = new Readable();
readable._read = () => {};
readable.push(this[BUFFER]);
readable.push(null);
return readable;
}
toString() {
return '[object Blob]'
}
slice() {
const size = this.size;

Expand Down
17 changes: 13 additions & 4 deletions test/test.js
Expand Up @@ -1779,8 +1779,8 @@ describe('node-fetch', () => {
.then(blob => blob.text())
.then(body => {
expect(body).to.equal('hello');
})
})
});
});

it('should support reading blob as arrayBuffer', function() {
return new Response(`hello`)
Expand All @@ -1789,8 +1789,17 @@ describe('node-fetch', () => {
.then(ab => {
const str = String.fromCharCode.apply(null, new Uint8Array(ab));
expect(str).to.equal('hello');
})
})
});
});

it('should support reading blob as stream', function() {
return new Response(`hello`)
.blob()
.then(blob => streamToPromise(blob.stream(), data => {
const str = data.toString();
expect(str).to.equal('hello');
}));
});

it('should support blob round-trip', function() {
const url = `${base}hello`;
Expand Down

0 comments on commit 432c9b0

Please sign in to comment.