Skip to content

Commit

Permalink
Added new reading method to blob
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmywarting committed Apr 15, 2019
1 parent bee2ad8 commit 0ad136d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/blob.js
Expand Up @@ -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;

Expand Down
19 changes: 19 additions & 0 deletions test/test.js
Expand Up @@ -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`;

Expand Down

0 comments on commit 0ad136d

Please sign in to comment.