Added new reading method to blob
This commit is contained in:
parent
bee2ad8db7
commit
0ad136d49f
15
src/blob.js
15
src/blob.js
|
@ -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;
|
||||
|
||||
|
|
19
test/test.js
19
test/test.js
|
@ -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`;
|
||||
|
||||
|
|
Loading…
Reference in New Issue