Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
960f7e528d | ||
|
|
582880a297 | ||
|
|
c1dd0851d8 | ||
|
|
bec5a3dceb | ||
|
|
6fabfce52f | ||
|
|
4a46def452 | ||
|
|
9a458af871 | ||
|
|
f7e60156a4 | ||
|
|
af2edbd01c | ||
|
|
f4ddd159a4 | ||
|
|
83559d1144 | ||
|
|
8bf6441343 | ||
|
|
1f5258cd79 | ||
|
|
f336a8d182 | ||
|
|
b09fdac531 | ||
|
|
b197ea6198 | ||
|
|
c9b6fdba06 |
@@ -1,3 +1,8 @@
|
||||
# Lock files
|
||||
|
||||
package-lock.json
|
||||
yarn.lock
|
||||
|
||||
# Logs
|
||||
logs
|
||||
*.log
|
||||
|
||||
+5
-3
@@ -1,11 +1,13 @@
|
||||
language: node_js
|
||||
|
||||
node_js:
|
||||
- "8"
|
||||
- "10"
|
||||
- "12"
|
||||
- "lts/*"
|
||||
- "node"
|
||||
|
||||
script:
|
||||
- npm run lint
|
||||
- npm run coverage
|
||||
|
||||
cache:
|
||||
directories:
|
||||
- node_modules
|
||||
|
||||
+13
-1
@@ -1,7 +1,19 @@
|
||||
|
||||
Changelog
|
||||
=========
|
||||
|
||||
## v1.0.6
|
||||
|
||||
- Enhance: use upstream Blob directly in typings (#38)
|
||||
- Other: update dependencies
|
||||
|
||||
## v1.0.5
|
||||
|
||||
- Other: no change to code, update dev dependency to address vulnerability reports
|
||||
|
||||
## v1.0.4
|
||||
|
||||
- Other: general code rewrite to pass linting, prepare for `node-fetch` release v3
|
||||
|
||||
## v1.0.3
|
||||
|
||||
- Fix: package.json export `blob.js` properly now
|
||||
|
||||
@@ -1,38 +1,39 @@
|
||||
|
||||
fetch-blob
|
||||
==========
|
||||
# fetch-blob
|
||||
|
||||
[![npm version][npm-image]][npm-url]
|
||||
[![build status][travis-image]][travis-url]
|
||||
[![coverage status][codecov-image]][codecov-url]
|
||||
[![install size][install-size-image]][install-size-url]
|
||||
|
||||
A Blob implementation on node.js, originally from node-fetch
|
||||
A Blob implementation in Node.js, originally from [node-fetch](https://github.com/node-fetch/node-fetch).
|
||||
|
||||
## Usage
|
||||
## Installation
|
||||
|
||||
```sh
|
||||
$ npm install fetch-blob
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
```js
|
||||
const Blob = require('fetch-blob');
|
||||
const fetch = require('node-fetch');
|
||||
|
||||
fetch('https://httpbin.org/post', {
|
||||
method: 'POST',
|
||||
body: new Blob(['hello'], { type: 'text/plain' })
|
||||
})
|
||||
.then(res => res.json()) // expecting a json response
|
||||
method: 'POST',
|
||||
body: new Blob(['hello'], { type: 'text/plain' })
|
||||
})
|
||||
.then(res => res.json());
|
||||
.then(json => console.log(json));
|
||||
```
|
||||
|
||||
See [MDN doc](https://developer.mozilla.org/en-US/docs/Web/API/Blob) and [Tests](https://github.com/bitinn/fetch-blob/blob/master/test.js) for more details.
|
||||
See the [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/API/Blob) and [tests](https://github.com/node-fetch/fetch-blob/blob/master/test.js) for more details.
|
||||
|
||||
[npm-image]: https://flat.badgen.net/npm/v/fetch-blob
|
||||
[npm-url]: https://www.npmjs.com/package/fetch-blob
|
||||
[travis-image]: https://flat.badgen.net/travis/bitinn/fetch-blob
|
||||
[travis-url]: https://travis-ci.org/bitinn/fetch-blob
|
||||
[codecov-image]: https://flat.badgen.net/codecov/c/github/bitinn/fetch-blob/master
|
||||
[codecov-url]: https://codecov.io/gh/bitinn/fetch-blob
|
||||
[travis-image]: https://flat.badgen.net/travis/node-fetch/fetch-blob
|
||||
[travis-url]: https://travis-ci.org/node-fetch/fetch-blob
|
||||
[codecov-image]: https://flat.badgen.net/codecov/c/github/node-fetch/fetch-blob/master
|
||||
[codecov-url]: https://codecov.io/gh/node-fetch/fetch-blob
|
||||
[install-size-image]: https://flat.badgen.net/packagephobia/install/fetch-blob
|
||||
[install-size-url]: https://packagephobia.now.sh/result?p=fetch-blob
|
||||
|
||||
Vendored
+4
@@ -0,0 +1,4 @@
|
||||
/// <reference lib="dom"/>
|
||||
|
||||
/* global Blob */
|
||||
export = Blob;
|
||||
+39
-29
@@ -1,28 +1,24 @@
|
||||
// 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)
|
||||
|
||||
const Stream = require('stream');
|
||||
const Readable = Stream.Readable;
|
||||
const {Readable: ReadableStream} = require('stream');
|
||||
|
||||
const BUFFER = Symbol('buffer');
|
||||
const TYPE = Symbol('type');
|
||||
|
||||
class Blob {
|
||||
constructor() {
|
||||
constructor(...args) {
|
||||
this[TYPE] = '';
|
||||
|
||||
const blobParts = arguments[0];
|
||||
const options = arguments[1];
|
||||
const blobParts = args[0];
|
||||
const options = args[1];
|
||||
|
||||
const buffers = [];
|
||||
/* eslint-disable-next-line no-unused-vars */
|
||||
let size = 0;
|
||||
|
||||
if (blobParts) {
|
||||
const a = blobParts;
|
||||
const length = Number(a.length);
|
||||
for (let i = 0; i < length; i++) {
|
||||
const element = a[i];
|
||||
blobParts.forEach(element => {
|
||||
let buffer;
|
||||
if (element instanceof Buffer) {
|
||||
buffer = element;
|
||||
@@ -35,50 +31,62 @@ class Blob {
|
||||
} else {
|
||||
buffer = Buffer.from(typeof element === 'string' ? element : String(element));
|
||||
}
|
||||
|
||||
size += buffer.length;
|
||||
buffers.push(buffer);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
this[BUFFER] = Buffer.concat(buffers);
|
||||
|
||||
let type = options &&
|
||||
options.type !== undefined &&
|
||||
String(options.type).toLowerCase();
|
||||
const type = options && options.type !== undefined && String(options.type).toLowerCase();
|
||||
if (type && !/[^\u0020-\u007E]/.test(type)) {
|
||||
this[TYPE] = type;
|
||||
}
|
||||
|
||||
if (options && Buffer.isBuffer(options.buffer)) {
|
||||
this[BUFFER] = options.buffer;
|
||||
}
|
||||
}
|
||||
|
||||
get size() {
|
||||
return this[BUFFER].length;
|
||||
}
|
||||
|
||||
get type() {
|
||||
return this[TYPE];
|
||||
}
|
||||
|
||||
text() {
|
||||
return Promise.resolve(this[BUFFER].toString())
|
||||
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 = () => {};
|
||||
const readable = new ReadableStream();
|
||||
readable._read = () => { };
|
||||
readable.push(this[BUFFER]);
|
||||
readable.push(null);
|
||||
return readable;
|
||||
}
|
||||
toString() {
|
||||
return '[object Blob]'
|
||||
}
|
||||
slice() {
|
||||
const size = this.size;
|
||||
|
||||
const start = arguments[0];
|
||||
const end = arguments[1];
|
||||
let relativeStart, relativeEnd;
|
||||
toString() {
|
||||
return '[object Blob]';
|
||||
}
|
||||
|
||||
slice(...args) {
|
||||
const {size} = this;
|
||||
|
||||
const start = args[0];
|
||||
const end = args[1];
|
||||
let relativeStart;
|
||||
let relativeEnd;
|
||||
|
||||
if (start === undefined) {
|
||||
relativeStart = 0;
|
||||
} else if (start < 0) {
|
||||
@@ -86,6 +94,7 @@ class Blob {
|
||||
} else {
|
||||
relativeStart = Math.min(start, size);
|
||||
}
|
||||
|
||||
if (end === undefined) {
|
||||
relativeEnd = size;
|
||||
} else if (end < 0) {
|
||||
@@ -93,6 +102,7 @@ class Blob {
|
||||
} else {
|
||||
relativeEnd = Math.min(end, size);
|
||||
}
|
||||
|
||||
const span = Math.max(relativeEnd - relativeStart, 0);
|
||||
|
||||
const buffer = this[BUFFER];
|
||||
@@ -100,16 +110,16 @@ class Blob {
|
||||
relativeStart,
|
||||
relativeStart + span
|
||||
);
|
||||
const blob = new Blob([], { type: arguments[2] });
|
||||
const blob = new Blob([], {type: args[2]});
|
||||
blob[BUFFER] = slicedBuffer;
|
||||
return blob;
|
||||
}
|
||||
}
|
||||
|
||||
Object.defineProperties(Blob.prototype, {
|
||||
size: { enumerable: true },
|
||||
type: { enumerable: true },
|
||||
slice: { enumerable: true }
|
||||
size: {enumerable: true},
|
||||
type: {enumerable: true},
|
||||
slice: {enumerable: true}
|
||||
});
|
||||
|
||||
Object.defineProperty(Blob.prototype, Symbol.toStringTag, {
|
||||
Generated
-5381
File diff suppressed because it is too large
Load Diff
+30
-13
@@ -1,32 +1,49 @@
|
||||
{
|
||||
"name": "fetch-blob",
|
||||
"version": "1.0.3",
|
||||
"description": "A Blob implementation on node.js, originally from node-fetch",
|
||||
"main": "blob.js",
|
||||
"version": "1.0.6",
|
||||
"description": "A Blob implementation in Node.js, originally from node-fetch.",
|
||||
"main": "index.js",
|
||||
"files": [
|
||||
"index.js",
|
||||
"index.d.ts"
|
||||
],
|
||||
"scripts": {
|
||||
"test": "ava",
|
||||
"lint": "xo",
|
||||
"test": "xo && ava",
|
||||
"report": "nyc ava",
|
||||
"coverage": "nyc --reporter json --reporter text ava && codecov -f coverage/coverage-final.json"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/bitinn/fetch-blob.git"
|
||||
},
|
||||
"repository": "https://github.com/node-fetch/fetch-blob.git",
|
||||
"keywords": [
|
||||
"blob",
|
||||
"node-fetch"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">=6"
|
||||
},
|
||||
"author": "David Frank",
|
||||
"license": "MIT",
|
||||
"bugs": {
|
||||
"url": "https://github.com/bitinn/fetch-blob/issues"
|
||||
"url": "https://github.com/node-fetch/fetch-blob/issues"
|
||||
},
|
||||
"homepage": "https://github.com/bitinn/fetch-blob#readme",
|
||||
"homepage": "https://github.com/node-fetch/fetch-blob#readme",
|
||||
"devDependencies": {
|
||||
"ava": "^1.4.1",
|
||||
"codecov": "^3.5.0",
|
||||
"ava": "^3.8.2",
|
||||
"codecov": "^3.7.0",
|
||||
"get-stream": "^5.1.0",
|
||||
"node-fetch": "^2.6.0",
|
||||
"nyc": "^14.1.1"
|
||||
"nyc": "^15.0.1",
|
||||
"xo": "^0.30.0"
|
||||
},
|
||||
"xo": {
|
||||
"overrides": [
|
||||
{
|
||||
"files": "test.js",
|
||||
"rules": {
|
||||
"node/no-unsupported-features/es-syntax": 0,
|
||||
"node/no-unsupported-features/node-builtins": 0
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,87 +1,81 @@
|
||||
|
||||
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;
|
||||
const test = require('ava');
|
||||
const Blob = require('.');
|
||||
const getStream = require('get-stream');
|
||||
const {Response} = require('node-fetch');
|
||||
|
||||
test('Blob ctor', t => {
|
||||
let data = 'a=1';
|
||||
let blob = new Blob([data]);
|
||||
const data = 'a=1';
|
||||
const blob = new Blob([data]); // eslint-disable-line no-unused-vars
|
||||
t.pass();
|
||||
});
|
||||
|
||||
test('Blob size', t => {
|
||||
let data = 'a=1';
|
||||
let blob = new Blob([data]);
|
||||
const data = 'a=1';
|
||||
const 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 });
|
||||
const data = 'a=1';
|
||||
const type = 'text/plain';
|
||||
const 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 });
|
||||
const data = 'a=1';
|
||||
const type = 'text/plain';
|
||||
const 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 });
|
||||
const data = 'a=1';
|
||||
const type = 'text/plain';
|
||||
const blob = new Blob([data], {type});
|
||||
|
||||
let decoder = new TextDecoder('utf-8');
|
||||
let buffer = await blob.arrayBuffer();
|
||||
const decoder = new TextDecoder('utf-8');
|
||||
const 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());
|
||||
const data = 'a=1';
|
||||
const type = 'text/plain';
|
||||
const blob = new Blob([data], {type});
|
||||
const 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 });
|
||||
const data = 'a=1';
|
||||
const type = 'text/plain';
|
||||
const 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);
|
||||
const data = 'a=1';
|
||||
const type = 'text/plain';
|
||||
const blob = new Blob([data], {type});
|
||||
const blob2 = blob.slice(0, 1);
|
||||
t.is(await blob2.text(), data.slice(0, 1));
|
||||
});
|
||||
|
||||
test('Blob works with node-fetch Response.blob()', 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();
|
||||
const data = 'a=1';
|
||||
const type = 'text/plain';
|
||||
const blob = new Blob([data], {type});
|
||||
const response = new Response(blob);
|
||||
const blob2 = await response.blob();
|
||||
t.is(await blob2.text(), data);
|
||||
});
|
||||
|
||||
test('Blob works with node-fetch Response.text()', async t => {
|
||||
let data = 'a=1';
|
||||
let type = 'text/plain';
|
||||
let blob = new Blob([data], { type });
|
||||
let res = new Response(blob);
|
||||
let text = await res.text();
|
||||
const data = 'a=1';
|
||||
const type = 'text/plain';
|
||||
const blob = new Blob([data], {type});
|
||||
const response = new Response(blob);
|
||||
const text = await response.text();
|
||||
t.is(text, data);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user