support reading blob with stream (#608)
This commit is contained in:
parent
0ad136d49f
commit
432c9b01ea
19
src/blob.js
19
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');
|
||||
|
||||
|
@ -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;
|
||||
|
||||
|
|
17
test/test.js
17
test/test.js
|
@ -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`)
|
||||
|
@ -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`;
|
||||
|
|
Loading…
Reference in New Issue