Compare commits
10
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5b2bab5365 | ||
|
|
b75fb395b4 | ||
|
|
3217da9bc7 | ||
|
|
ae6b565ae6 | ||
|
|
26ca0e39b4 | ||
|
|
99388a79bb | ||
|
|
cf379d10e0 | ||
|
|
1d1faebe18 | ||
|
|
903a4c1bb0 | ||
|
|
126b4ec697 |
@@ -47,6 +47,7 @@ jobs:
|
|||||||
node-version: ${{steps.get-version.outputs.node}}
|
node-version: ${{steps.get-version.outputs.node}}
|
||||||
|
|
||||||
- run: npm install
|
- run: npm install
|
||||||
|
- run: npm install domexception
|
||||||
|
|
||||||
- run: npm run report -- --colors
|
- run: npm run report -- --colors
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,14 @@
|
|||||||
Changelog
|
Changelog
|
||||||
=========
|
=========
|
||||||
|
|
||||||
|
## v2.1.0
|
||||||
|
- Fix: .slice has an implementation bug (#54).
|
||||||
|
- Added blob backed up by filesystem (#55)
|
||||||
|
|
||||||
|
## v2.0.1
|
||||||
|
|
||||||
|
- Fix: remove upper bound for node engine semver (#49).
|
||||||
|
|
||||||
## v2.0.0
|
## 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`.
|
> Note: This release was previously published as `1.0.7`, but as it contains breaking changes, we renamed it to `2.0.0`.
|
||||||
|
|||||||
@@ -27,6 +27,24 @@ fetch('https://httpbin.org/post', {
|
|||||||
.then(json => console.log(json));
|
.then(json => console.log(json));
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Blob part backed up by filesystem
|
||||||
|
To use, install [domexception](https://github.com/jsdom/domexception).
|
||||||
|
|
||||||
|
```sh
|
||||||
|
npm install fetch-blob domexception
|
||||||
|
```
|
||||||
|
|
||||||
|
```js
|
||||||
|
const blobFrom = require('fetch-blob/from.js');
|
||||||
|
const blob1 = blobFrom('./2-GiB-file.bin');
|
||||||
|
const blob2 = blobFrom('./2-GiB-file.bin');
|
||||||
|
|
||||||
|
// Not a 4 GiB memory snapshot, just holds 3 references
|
||||||
|
// points to where data is located on the disk
|
||||||
|
const blob = new Blob([blob1, blob2]);
|
||||||
|
console.log(blob.size) // 4 GiB
|
||||||
|
```
|
||||||
|
|
||||||
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.
|
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-image]: https://flat.badgen.net/npm/v/fetch-blob
|
||||||
|
|||||||
@@ -0,0 +1,57 @@
|
|||||||
|
const {statSync, createReadStream} = require('fs');
|
||||||
|
const Blob = require('.');
|
||||||
|
const DOMException = require('domexception');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {string} path filepath on the disk
|
||||||
|
* @returns {Blob}
|
||||||
|
*/
|
||||||
|
function blobFrom(path) {
|
||||||
|
const {size, mtime} = statSync(path);
|
||||||
|
const blob = new BlobDataItem({path, size, mtime});
|
||||||
|
|
||||||
|
return new Blob([blob]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This is a blob backed up by a file on the disk
|
||||||
|
* with minium requirement
|
||||||
|
*
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
class BlobDataItem {
|
||||||
|
constructor(options) {
|
||||||
|
this.size = options.size;
|
||||||
|
this.path = options.path;
|
||||||
|
this.start = options.start;
|
||||||
|
this.mtime = options.mtime;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Slicing arguments is validated and formated
|
||||||
|
// by Blob.prototype.slice
|
||||||
|
slice(start, end) {
|
||||||
|
return new BlobDataItem({
|
||||||
|
path: this.path,
|
||||||
|
start,
|
||||||
|
mtime: this.mtime,
|
||||||
|
size: end - start
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
stream() {
|
||||||
|
if (statSync(this.path).mtime > this.mtime) {
|
||||||
|
throw new DOMException('The requested file could not be read, typically due to permission problems that have occurred after a reference to a file was acquired.', 'NotReadableError');
|
||||||
|
}
|
||||||
|
|
||||||
|
return createReadStream(this.path, {
|
||||||
|
start: this.start,
|
||||||
|
end: this.start + this.size - 1
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
get [Symbol.toStringTag]() {
|
||||||
|
return 'Blob';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = blobFrom;
|
||||||
@@ -15,20 +15,6 @@ async function * read(parts) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @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 {
|
class Blob {
|
||||||
/**
|
/**
|
||||||
* The Blob() constructor returns a new Blob object. The content
|
* The Blob() constructor returns a new Blob object. The content
|
||||||
@@ -43,13 +29,13 @@ class Blob {
|
|||||||
|
|
||||||
const parts = blobParts.map(element => {
|
const parts = blobParts.map(element => {
|
||||||
let buffer;
|
let buffer;
|
||||||
if (Buffer.isBuffer(element)) {
|
if (element instanceof Buffer) {
|
||||||
buffer = element;
|
buffer = element;
|
||||||
} else if (ArrayBuffer.isView(element)) {
|
} else if (ArrayBuffer.isView(element)) {
|
||||||
buffer = Buffer.from(element.buffer, element.byteOffset, element.byteLength);
|
buffer = Buffer.from(element.buffer, element.byteOffset, element.byteLength);
|
||||||
} else if (element instanceof ArrayBuffer) {
|
} else if (element instanceof ArrayBuffer) {
|
||||||
buffer = Buffer.from(element);
|
buffer = Buffer.from(element);
|
||||||
} else if (isBlob(element)) {
|
} else if (element instanceof Blob) {
|
||||||
buffer = element;
|
buffer = element;
|
||||||
} else {
|
} else {
|
||||||
buffer = Buffer.from(typeof element === 'string' ? element : String(element));
|
buffer = Buffer.from(typeof element === 'string' ? element : String(element));
|
||||||
@@ -152,7 +138,7 @@ class Blob {
|
|||||||
} else {
|
} else {
|
||||||
const chunk = part.slice(relativeStart, Math.min(size, relativeEnd));
|
const chunk = part.slice(relativeStart, Math.min(size, relativeEnd));
|
||||||
blobParts.push(chunk);
|
blobParts.push(chunk);
|
||||||
added += size;
|
added += ArrayBuffer.isView(chunk) ? chunk.byteLength : chunk.size;
|
||||||
relativeStart = 0; // All next sequental parts should start at 0
|
relativeStart = 0; // All next sequental parts should start at 0
|
||||||
|
|
||||||
// don't add the overflow to new blobParts
|
// don't add the overflow to new blobParts
|
||||||
@@ -167,6 +153,20 @@ class Blob {
|
|||||||
|
|
||||||
return blob;
|
return blob;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get [Symbol.toStringTag]() {
|
||||||
|
return 'Blob';
|
||||||
|
}
|
||||||
|
|
||||||
|
static [Symbol.hasInstance](object) {
|
||||||
|
return (
|
||||||
|
typeof object === 'object' &&
|
||||||
|
typeof object.stream === 'function' &&
|
||||||
|
object.stream.length === 0 &&
|
||||||
|
typeof object.constructor === 'function' &&
|
||||||
|
/^(Blob|File)$/.test(object[Symbol.toStringTag])
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Object.defineProperties(Blob.prototype, {
|
Object.defineProperties(Blob.prototype, {
|
||||||
@@ -175,11 +175,4 @@ Object.defineProperties(Blob.prototype, {
|
|||||||
slice: {enumerable: true}
|
slice: {enumerable: true}
|
||||||
});
|
});
|
||||||
|
|
||||||
Object.defineProperty(Blob.prototype, Symbol.toStringTag, {
|
|
||||||
value: 'Blob',
|
|
||||||
writable: false,
|
|
||||||
enumerable: false,
|
|
||||||
configurable: true
|
|
||||||
});
|
|
||||||
|
|
||||||
module.exports = Blob;
|
module.exports = Blob;
|
||||||
|
|||||||
+6
-3
@@ -1,9 +1,10 @@
|
|||||||
{
|
{
|
||||||
"name": "fetch-blob",
|
"name": "fetch-blob",
|
||||||
"version": "2.0.0",
|
"version": "2.1.0",
|
||||||
"description": "A Blob implementation in Node.js, originally from node-fetch.",
|
"description": "A Blob implementation in Node.js, originally from node-fetch.",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"files": [
|
"files": [
|
||||||
|
"from.js",
|
||||||
"index.js",
|
"index.js",
|
||||||
"index.d.ts"
|
"index.d.ts"
|
||||||
],
|
],
|
||||||
@@ -20,7 +21,7 @@
|
|||||||
"node-fetch"
|
"node-fetch"
|
||||||
],
|
],
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": "^10.17.0"
|
"node": "^10.17.0 || >=12.3.0"
|
||||||
},
|
},
|
||||||
"author": "David Frank",
|
"author": "David Frank",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
@@ -48,5 +49,7 @@
|
|||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"dependencies": {}
|
"peerDependencies": {
|
||||||
|
"domexception": "^2.0.1"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
|
const fs = require('fs');
|
||||||
const test = require('ava');
|
const test = require('ava');
|
||||||
const Blob = require('.');
|
const Blob = require('.');
|
||||||
|
const blobFrom = require('./from');
|
||||||
const getStream = require('get-stream');
|
const getStream = require('get-stream');
|
||||||
const {Response} = require('node-fetch');
|
const {Response} = require('node-fetch');
|
||||||
const {TextDecoder} = require('util');
|
const {TextDecoder} = require('util');
|
||||||
@@ -114,6 +116,12 @@ test('Blob slice(0, -1)', async t => {
|
|||||||
t.is(await blob.text(), 'abcdefg');
|
t.is(await blob.text(), 'abcdefg');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('Blob(["hello ", "world"]).slice(5)', async t => {
|
||||||
|
const parts = ['hello ', 'world'];
|
||||||
|
const blob = new Blob(parts);
|
||||||
|
t.is(await blob.slice(5).text(), ' world');
|
||||||
|
});
|
||||||
|
|
||||||
test('throw away unwanted parts', async t => {
|
test('throw away unwanted parts', async t => {
|
||||||
const blob = new Blob(['a', 'b', 'c']).slice(1, 2);
|
const blob = new Blob(['a', 'b', 'c']).slice(1, 2);
|
||||||
t.is(await blob.text(), 'b');
|
t.is(await blob.text(), 'b');
|
||||||
@@ -136,3 +144,19 @@ test('Blob works with node-fetch Response.text()', async t => {
|
|||||||
const text = await response.text();
|
const text = await response.text();
|
||||||
t.is(text, data);
|
t.is(text, data);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('blob part backed up by filesystem', async t => {
|
||||||
|
const blob = blobFrom('./LICENSE');
|
||||||
|
t.is(await blob.slice(0, 3).text(), 'MIT');
|
||||||
|
t.is(await blob.slice(4, 11).text(), 'License');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Reading after modified should fail', async t => {
|
||||||
|
const blob = blobFrom('./LICENSE');
|
||||||
|
await new Promise(resolve => setTimeout(resolve, 100));
|
||||||
|
const now = new Date();
|
||||||
|
// Change modified time
|
||||||
|
fs.utimesSync('./LICENSE', now, now);
|
||||||
|
const error = await blob.text().catch(error => error);
|
||||||
|
t.is(error.name, 'NotReadableError');
|
||||||
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user