Added new reading method to blob

This commit is contained in:
Jimmy Wärting 2019-04-15 22:46:11 +02:00
parent bee2ad8db7
commit 0ad136d49f
2 changed files with 34 additions and 0 deletions

View File

@ -49,6 +49,21 @@ export default class Blob {
get type() {
return this[TYPE];
}
text() {
return Promise.resolve(this[BUFFER].toString())
}
arrayBuffer() {
const buf = this[BUFFER];
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
// }
slice() {
const size = this.size;

View File

@ -1773,6 +1773,25 @@ describe('node-fetch', () => {
});
});
it('should support reading blob as text', function() {
return new Response(`hello`)
.blob()
.then(blob => blob.text())
.then(body => {
expect(body).to.equal('hello');
})
})
it('should support reading blob as arrayBuffer', function() {
return new Response(`hello`)
.blob()
.then(blob => blob.arrayBuffer())
.then(ab => {
const str = String.fromCharCode.apply(null, new Uint8Array(ab));
expect(str).to.equal('hello');
})
})
it('should support blob round-trip', function() {
const url = `${base}hello`;