Compare commits

...
Author SHA1 Message Date
Konstantin Vyatkin cd619c7694 lint 2020-06-11 00:17:58 -04:00
Konstantin Vyatkin bc6ca02caa import TextDecoder from util 2020-06-11 00:16:48 -04:00
Konstantin Vyatkin b098249bea update badge 2020-06-11 00:13:20 -04:00
Konstantin Vyatkin ecdabc9879 add GitHub Actions 2020-06-11 00:01:56 -04:00
Konstantin Vyatkin 71600df374 switch coverage to c8 2020-06-11 00:01:51 -04:00
Konstantin Vyatkin f9be7e7d7b add codecov partial line coverage 2020-06-10 23:52:25 -04:00
Konstantin Vyatkin fcf91af996 remove travis 2020-06-10 23:50:57 -04:00
Antoni Kepinski 38639ec4aa release 2.0.0 2020-06-10 21:09:47 +02:00
Antoni Kepinski 5608cef75c update changelog 2020-06-10 21:08:54 +02:00
Antoni Kepinski 62fc8eab22 1.0.7 2020-06-10 16:14:25 +02:00
Antoni Kepinski 1a3c424a07 update nyc 2020-06-10 16:13:07 +02:00
Antoni Kepinski f4fa7e5825 Update CHANGELOG.md 2020-06-10 16:12:41 +02:00
Konstantin Vyatkin a1fef8cd0b Create TypeScript declarations from JSDoc (#45) 2020-06-10 16:09:02 +02:00
dependabot-preview[bot]anddependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com> 1ec92a63b2 Bump xo from 0.30.0 to 0.32.0 (#39)
Bumps [xo](https://github.com/xojs/xo) from 0.30.0 to 0.32.0.
- [Release notes](https://github.com/xojs/xo/releases)
- [Commits](https://github.com/xojs/xo/compare/v0.30.0...v0.32.0)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>

Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>
2020-06-09 20:22:03 +02:00
Jimmy Wärting fd4f45e70f Operate on blob parts (byte sequence) (#44)
* operate on blob parts

* jsdoc

* specify parts type

* Back to 100% (discovered a small bug)
2020-06-07 23:42:42 +02:00
Richie Bendall 9dc07473c6 Update formatting 2020-06-08 01:12:25 +12:00
Jimmy Wärting 870be1e2d5 Use a WeakMap for private properties (#42)
* options.buffer isn't a valid option

* truly private internal properties

* 100% test coverage
2020-06-06 00:09:32 -04:00
12 changed files with 354 additions and 165 deletions
+6
View File
@@ -0,0 +1,6 @@
coverage:
round: up
precision: 2
parsers:
javascript:
enable_partials: yes
+56
View File
@@ -0,0 +1,56 @@
name: CI
on:
push:
branches: [master]
pull_request:
paths:
- "**.js"
- "package.json"
- ".github/workflows/ci.yml"
jobs:
test:
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macOS-latest]
node: ["14", "12", engines]
exclude:
# On Windows, run tests with only the LTS environments.
- os: windows-latest
node: engines
- os: windows-latest
node: "14"
# On macOS, run tests with only the LTS environments.
- os: macOS-latest
node: engines
- os: macOS-latest
node: "14"
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v2
- name: Get Node.JS version from package.json
if: matrix.node == 'engines'
id: get-version
run: echo ::set-output name=node::$(npx --q minimum-node-version)
- uses: actions/setup-node@v2-beta
if: matrix.node != 'engines'
with:
node-version: ${{ matrix.node }}
- uses: actions/setup-node@v2-beta
if: matrix.node == 'engines'
with:
node-version: ${{steps.get-version.outputs.node}}
- run: npm install
- run: npm run report -- --colors
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v1
with:
file: ./coverage/coverage-final.json
+20
View File
@@ -0,0 +1,20 @@
name: CI
on:
pull_request:
paths:
- "**.js"
- "**eslint**"
- "package.json"
- ".github/workflows/lint.yml"
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Use Node.js
uses: actions/setup-node@v2-beta
with:
node-version: 14
- run: npm install
- run: npm run lint
+1 -2
View File
@@ -62,5 +62,4 @@ typings/
# dotenv environment variables file
.env
# next.js build output
.next
index.d.ts
+2 -1
View File
@@ -1 +1,2 @@
package-lock=false
package-lock=false
save-exact=false
-13
View File
@@ -1,13 +0,0 @@
language: node_js
node_js:
- "lts/*"
- "node"
script:
- npm run lint
- npm run coverage
cache:
directories:
- node_modules
+11
View File
@@ -1,6 +1,17 @@
Changelog
=========
## v2.0.0
> Note: This release was previously published as `1.0.7`, but as it contains breaking changes, we renamed it to `2.0.0`.
- **Breaking:** minimum supported Node.js version is now 10.17.
- **Breaking:** `buffer` option has been removed.
- Enhance: create TypeScript declarations from JSDoc (#45).
- Enhance: operate on blob parts (byte sequence) (#44).
- Enhance: use a `WeakMap` for private properties (#42) .
- Other: update formatting.
## v1.0.6
- Enhance: use upstream Blob directly in typings (#38)
+5 -5
View File
@@ -1,7 +1,7 @@
# fetch-blob
[![npm version][npm-image]][npm-url]
[![build status][travis-image]][travis-url]
[![build status][ci-image]][ci-url]
[![coverage status][codecov-image]][codecov-url]
[![install size][install-size-image]][install-size-url]
@@ -10,7 +10,7 @@ A Blob implementation in Node.js, originally from [node-fetch](https://github.co
## Installation
```sh
$ npm install fetch-blob
npm install fetch-blob
```
## Usage
@@ -21,7 +21,7 @@ const fetch = require('node-fetch');
fetch('https://httpbin.org/post', {
method: 'POST',
body: new Blob(['hello'], { type: 'text/plain' })
body: new Blob(['Hello World'], { type: 'text/plain' })
})
.then(res => res.json());
.then(json => console.log(json));
@@ -31,8 +31,8 @@ See the [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/API/Blo
[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/node-fetch/fetch-blob
[travis-url]: https://travis-ci.org/node-fetch/fetch-blob
[ci-image]: https://github.com/node-fetch/fetch-blob/workflows/CI/badge.svg
[ci-url]: https://github.com/node-fetch/fetch-blob/actions
[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
Vendored
-4
View File
@@ -1,4 +0,0 @@
/// <reference lib="dom"/>
/* global Blob */
export = Blob;
+138 -85
View File
@@ -1,117 +1,170 @@
// Based on https://github.com/tmpvar/jsdom/blob/aa85b2abf07766ff7bf5c1f6daafb3726f2f2db5/lib/jsdom/living/blob.js
// (MIT licensed)
const {Readable} = require('stream');
const {Readable: ReadableStream} = require('stream');
/**
* @type {WeakMap<Blob, {type: string, size: number, parts: (Blob | Buffer)[] }>}
*/
const wm = new WeakMap();
const BUFFER = Symbol('buffer');
const TYPE = Symbol('type');
async function * read(parts) {
for (const part of parts) {
if ('stream' in part) {
yield * part.stream();
} else {
yield part;
}
}
}
/**
* @template T
* @param {T} object
* @returns {T is Blob}
*/
const isBlob = object => {
return (
typeof object === 'object' &&
typeof object.stream === 'function' &&
typeof object.constructor === 'function' &&
/^(Blob|File)$/.test(object[Symbol.toStringTag])
);
};
class Blob {
constructor(...args) {
this[TYPE] = '';
const blobParts = args[0];
const options = args[1];
const buffers = [];
/* eslint-disable-next-line no-unused-vars */
/**
* The Blob() constructor returns a new Blob object. The content
* of the blob consists of the concatenation of the values given
* in the parameter array.
*
* @param {(ArrayBufferLike | ArrayBufferView | Blob | Buffer | string)[]} blobParts
* @param {{ type?: string }} [options]
*/
constructor(blobParts = [], options = {type: ''}) {
let size = 0;
if (blobParts) {
blobParts.forEach(element => {
let buffer;
if (element instanceof Buffer) {
buffer = element;
} else if (ArrayBuffer.isView(element)) {
buffer = Buffer.from(element.buffer, element.byteOffset, element.byteLength);
} else if (element instanceof ArrayBuffer) {
buffer = Buffer.from(element);
} else if (element instanceof Blob) {
buffer = element[BUFFER];
} else {
buffer = Buffer.from(typeof element === 'string' ? element : String(element));
}
const parts = blobParts.map(element => {
let buffer;
if (Buffer.isBuffer(element)) {
buffer = element;
} else if (ArrayBuffer.isView(element)) {
buffer = Buffer.from(element.buffer, element.byteOffset, element.byteLength);
} else if (element instanceof ArrayBuffer) {
buffer = Buffer.from(element);
} else if (isBlob(element)) {
buffer = element;
} else {
buffer = Buffer.from(typeof element === 'string' ? element : String(element));
}
size += buffer.length;
buffers.push(buffer);
});
}
size += buffer.length || buffer.size || 0;
return buffer;
});
this[BUFFER] = Buffer.concat(buffers);
const type = 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;
}
wm.set(this, {
type: /[^\u0020-\u007E]/.test(type) ? '' : type,
size,
parts
});
}
/**
* The Blob interface's size property returns the
* size of the Blob in bytes.
*/
get size() {
return this[BUFFER].length;
return wm.get(this).size;
}
/**
* The type property of a Blob object returns the MIME type of the file.
*/
get type() {
return this[TYPE];
return wm.get(this).type;
}
text() {
return Promise.resolve(this[BUFFER].toString());
/**
* The text() method in the Blob interface returns a Promise
* that resolves with a string containing the contents of
* the blob, interpreted as UTF-8.
*
* @return {Promise<string>}
*/
async text() {
return Buffer.from(await this.arrayBuffer()).toString();
}
arrayBuffer() {
const buf = this[BUFFER];
const ab = buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength);
return Promise.resolve(ab);
/**
* The arrayBuffer() method in the Blob interface returns a
* Promise that resolves with the contents of the blob as
* binary data contained in an ArrayBuffer.
*
* @return {Promise<ArrayBuffer>}
*/
async arrayBuffer() {
const data = new Uint8Array(this.size);
let offset = 0;
for await (const chunk of this.stream()) {
data.set(chunk, offset);
offset += chunk.length;
}
return data.buffer;
}
/**
* The Blob interface's stream() method is difference from native
* and uses node streams instead of whatwg streams.
*
* @returns {Readable} Node readable stream
*/
stream() {
const readable = new ReadableStream();
readable._read = () => { };
readable.push(this[BUFFER]);
readable.push(null);
return readable;
return Readable.from(read(wm.get(this).parts));
}
toString() {
return '[object Blob]';
}
slice(...args) {
/**
* The Blob interface's slice() method creates and returns a
* new Blob object which contains data from a subset of the
* blob on which it's called.
*
* @param {number} [start]
* @param {number} [end]
* @param {string} [type]
*/
slice(start = 0, end = this.size, type = '') {
const {size} = this;
const start = args[0];
const end = args[1];
let relativeStart;
let relativeEnd;
if (start === undefined) {
relativeStart = 0;
} else if (start < 0) {
relativeStart = Math.max(size + start, 0);
} else {
relativeStart = Math.min(start, size);
}
if (end === undefined) {
relativeEnd = size;
} else if (end < 0) {
relativeEnd = Math.max(size + end, 0);
} else {
relativeEnd = Math.min(end, size);
}
let relativeStart = start < 0 ? Math.max(size + start, 0) : Math.min(start, size);
let relativeEnd = end < 0 ? Math.max(size + end, 0) : Math.min(end, size);
const span = Math.max(relativeEnd - relativeStart, 0);
const parts = wm.get(this).parts.values();
const blobParts = [];
let added = 0;
for (const part of parts) {
const size = ArrayBuffer.isView(part) ? part.byteLength : part.size;
if (relativeStart && size <= relativeStart) {
// Skip the beginning and change the relative
// start & end position as we skip the unwanted parts
relativeStart -= size;
relativeEnd -= size;
} else {
const chunk = part.slice(relativeStart, Math.min(size, relativeEnd));
blobParts.push(chunk);
added += size;
relativeStart = 0; // All next sequental parts should start at 0
// don't add the overflow to new blobParts
if (added >= span) {
break;
}
}
}
const blob = new Blob([], {type});
Object.assign(wm.get(blob), {size: span, parts: blobParts});
const buffer = this[BUFFER];
const slicedBuffer = buffer.slice(
relativeStart,
relativeStart + span
);
const blob = new Blob([], {type: args[2]});
blob[BUFFER] = slicedBuffer;
return blob;
}
}
+50 -47
View File
@@ -1,49 +1,52 @@
{
"name": "fetch-blob",
"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": {
"lint": "xo",
"test": "xo && ava",
"report": "nyc ava",
"coverage": "nyc --reporter json --reporter text ava && codecov -f coverage/coverage-final.json"
},
"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/node-fetch/fetch-blob/issues"
},
"homepage": "https://github.com/node-fetch/fetch-blob#readme",
"devDependencies": {
"ava": "^3.8.2",
"codecov": "^3.7.0",
"get-stream": "^5.1.0",
"node-fetch": "^2.6.0",
"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
}
}
]
}
"name": "fetch-blob",
"version": "2.0.0",
"description": "A Blob implementation in Node.js, originally from node-fetch.",
"main": "index.js",
"files": [
"index.js",
"index.d.ts"
],
"scripts": {
"lint": "xo",
"test": "xo && ava",
"report": "c8 --reporter json --reporter text ava",
"coverage": "c8 --reporter json --reporter text ava && codecov -f coverage/coverage-final.json",
"prepublishOnly": "tsc --declaration --emitDeclarationOnly --allowJs index.js"
},
"repository": "https://github.com/node-fetch/fetch-blob.git",
"keywords": [
"blob",
"node-fetch"
],
"engines": {
"node": "^10.17.0"
},
"author": "David Frank",
"license": "MIT",
"bugs": {
"url": "https://github.com/node-fetch/fetch-blob/issues"
},
"homepage": "https://github.com/node-fetch/fetch-blob#readme",
"devDependencies": {
"ava": "^3.8.2",
"c8": "^7.2.0",
"codecov": "^3.7.0",
"get-stream": "^5.1.0",
"node-fetch": "^2.6.0",
"typescript": "^3.9.5",
"xo": "^0.32.0"
},
"xo": {
"overrides": [
{
"files": "test.js",
"rules": {
"node/no-unsupported-features/es-syntax": 0,
"node/no-unsupported-features/node-builtins": 0
}
}
]
},
"dependencies": {}
}
+65 -8
View File
@@ -2,13 +2,34 @@ const test = require('ava');
const Blob = require('.');
const getStream = require('get-stream');
const {Response} = require('node-fetch');
const {TextDecoder} = require('util');
test('Blob ctor', t => {
test('new Blob()', t => {
const blob = new Blob(); // eslint-disable-line no-unused-vars
t.pass();
});
test('new Blob(parts)', t => {
const data = 'a=1';
const blob = new Blob([data]); // eslint-disable-line no-unused-vars
t.pass();
});
test('Blob ctor parts', async t => {
const parts = [
'a',
new Uint8Array([98]),
new Uint16Array([25699]),
new Uint8Array([101]).buffer,
Buffer.from('f'),
new Blob(['g']),
{}
];
const blob = new Blob(parts);
t.is(await blob.text(), 'abcdefg[object Object]');
});
test('Blob size', t => {
const data = 'a=1';
const blob = new Blob([data]);
@@ -16,12 +37,27 @@ test('Blob size', t => {
});
test('Blob type', t => {
const data = 'a=1';
const type = 'text/plain';
const blob = new Blob([data], {type});
const blob = new Blob([], {type});
t.is(blob.type, type);
});
test('Blob slice type', t => {
const type = 'text/plain';
const blob = new Blob().slice(0, 0, type);
t.is(blob.type, type);
});
test('invalid Blob type', t => {
const blob = new Blob([], {type: '\u001Ftext/plain'});
t.is(blob.type, '');
});
test('invalid Blob slice type', t => {
const blob = new Blob().slice(0, 0, '\u001Ftext/plain');
t.is(blob.type, '');
});
test('Blob text()', async t => {
const data = 'a=1';
const type = 'text/plain';
@@ -55,11 +91,32 @@ test('Blob toString()', t => {
});
test('Blob slice()', async t => {
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));
const data = 'abcdefgh';
const blob = new Blob([data]).slice();
t.is(await blob.text(), data);
});
test('Blob slice(0, 1)', async t => {
const data = 'abcdefgh';
const blob = new Blob([data]).slice(0, 1);
t.is(await blob.text(), 'a');
});
test('Blob slice(-1)', async t => {
const data = 'abcdefgh';
const blob = new Blob([data]).slice(-1);
t.is(await blob.text(), 'h');
});
test('Blob slice(0, -1)', async t => {
const data = 'abcdefgh';
const blob = new Blob([data]).slice(0, -1);
t.is(await blob.text(), 'abcdefg');
});
test('throw away unwanted parts', async t => {
const blob = new Blob(['a', 'b', 'c']).slice(1, 2);
t.is(await blob.text(), 'b');
});
test('Blob works with node-fetch Response.blob()', async t => {