test blob

This commit is contained in:
David Frank
2019-05-16 15:41:22 +08:00
parent 544a1d3546
commit 6134add4ee
4 changed files with 4767 additions and 13 deletions
+14 -11
View File
@@ -1,15 +1,14 @@
// Based on https://github.com/tmpvar/jsdom/blob/aa85b2abf07766ff7bf5c1f6daafb3726f2f2db5/lib/jsdom/living/blob.js
// Also based on https://github.com/bitinn/node-fetch/blob/v2.5.0/src/blob.js
// (MIT licensed)
import Stream from 'stream';
// fix for "Readable" isn't a named export issue
const Stream = require('stream');
const Readable = Stream.Readable;
export const BUFFER = Symbol('buffer');
const BUFFER = Symbol('buffer');
const TYPE = Symbol('type');
export default class Blob {
class Blob {
constructor() {
this[TYPE] = '';
@@ -43,7 +42,9 @@ export default class Blob {
this[BUFFER] = Buffer.concat(buffers);
let type = options && options.type !== undefined && String(options.type).toLowerCase();
let type = options &&
options.type !== undefined &&
String(options.type).toLowerCase();
if (type && !/[^\u0020-\u007E]/.test(type)) {
this[TYPE] = type;
}
@@ -63,11 +64,11 @@ export default class Blob {
return Promise.resolve(ab);
}
stream() {
const readable = new Readable();
readable._read = () => {};
readable.push(this[BUFFER]);
readable.push(null);
return readable;
const readable = new Readable();
readable._read = () => {};
readable.push(this[BUFFER]);
readable.push(null);
return readable;
}
toString() {
return '[object Blob]'
@@ -117,3 +118,5 @@ Object.defineProperty(Blob.prototype, Symbol.toStringTag, {
enumerable: false,
configurable: true
});
module.exports = Blob;
+4668
View File
File diff suppressed because it is too large Load Diff
+7 -2
View File
@@ -4,7 +4,7 @@
"description": "A Blob implementation on node.js, originally from node-fetch",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "ava"
},
"repository": {
"type": "git",
@@ -19,5 +19,10 @@
"bugs": {
"url": "https://github.com/bitinn/fetch-blob/issues"
},
"homepage": "https://github.com/bitinn/fetch-blob#readme"
"homepage": "https://github.com/bitinn/fetch-blob#readme",
"devDependencies": {
"ava": "^1.4.1",
"get-stream": "^5.1.0",
"node-fetch": "^2.6.0"
}
}
+78
View File
@@ -0,0 +1,78 @@
import test from 'ava';
import Blob from './blob';
import util from 'util';
import stream from 'stream';
import getStream from 'get-stream';
import { Response } from 'node-fetch';
const TextDecoder = util.TextDecoder;
const Readable = stream.Readable;
test('Blob ctor', t => {
let data = 'a=1';
let blob = new Blob([data]);
t.pass();
});
test('Blob size', t => {
let data = 'a=1';
let blob = new Blob([data]);
t.is(blob.size, data.length);
});
test('Blob type', t => {
let data = 'a=1';
let type = 'text/plain';
let blob = new Blob([data], { type });
t.is(blob.type, type);
});
test('Blob text()', async t => {
let data = 'a=1';
let type = 'text/plain';
let blob = new Blob([data], { type });
t.is(await blob.text(), data);
});
test('Blob arrayBuffer()', async t => {
let data = 'a=1';
let type = 'text/plain';
let blob = new Blob([data], { type });
let decoder = new TextDecoder('utf-8');
let buffer = await blob.arrayBuffer();
t.is(decoder.decode(buffer), data);
});
test('Blob stream()', async t => {
let data = 'a=1';
let type = 'text/plain';
let blob = new Blob([data], { type });
let result = await getStream(blob.stream());
t.is(result, data);
});
test('Blob toString()', t => {
let data = 'a=1';
let type = 'text/plain';
let blob = new Blob([data], { type });
t.is(blob.toString(), '[object Blob]');
});
test('Blob slice()', async t => {
let data = 'a=1';
let type = 'text/plain';
let blob = new Blob([data], { type });
let blob2 = blob.slice(0, 1);
t.is(await blob2.text(), data.slice(0, 1));
});
test('Blob works with node-fetch Response', async t => {
let data = 'a=1';
let type = 'text/plain';
let blob = new Blob([data], { type });
let res = new Response(blob);
let blob2 = await res.blob();
t.is(await blob2.text(), data);
});