fetch-blob
Advanced tools
Comparing version 3.0.0-rc.0 to 3.0.0
@@ -7,3 +7,3 @@ Changelog | ||
- Switch to ESM | ||
- blob.stream() return a subset of whatwg stream which is the async iterable | ||
- blob.stream() return a subset of whatwg stream which is the async iterable part | ||
(it no longer return a node stream) | ||
@@ -15,4 +15,5 @@ - Reduced the dependency of Buffer by changing to global TextEncoder/Decoder (require node 11) | ||
It would require a more proper mime type parser - so we just made it loose. | ||
- index.js can now be imported by browser & deno since it no longer depends on any | ||
core node features (but why would you? other environment can benefit from it) | ||
- index.js and file.js can now be imported by browser & deno since it no longer depends on any | ||
core node features (but why would you?) | ||
- Implemented a File class | ||
@@ -19,0 +20,0 @@ ## v2.1.2 |
export default blobFromSync; | ||
/** | ||
* @param {string} path filepath on the disk | ||
* @returns {Blob} | ||
* @param {string} [type] mimetype to use | ||
*/ | ||
export function blobFromSync(path: string): Blob; | ||
export function blobFromSync(path: string, type?: string): Blob; | ||
import File from "./file.js"; | ||
import Blob from "./index.js"; | ||
/** | ||
* @param {string} path filepath on the disk | ||
* @returns {Promise<Blob>} | ||
* @param {string} [type] mimetype to use | ||
*/ | ||
export function blobFrom(path: string): Promise<Blob>; | ||
export { Blob }; | ||
export function blobFrom(path: string, type?: string): Promise<Blob>; | ||
/** | ||
* @param {string} path filepath on the disk | ||
* @param {string} [type] mimetype to use | ||
*/ | ||
export function fileFrom(path: string, type?: string): Promise<File>; | ||
/** | ||
* @param {string} path filepath on the disk | ||
* @param {string} [type] mimetype to use | ||
*/ | ||
export function fileFromSync(path: string, type?: string): File; | ||
export { File, Blob }; |
44
from.js
import {statSync, createReadStream} from 'fs'; | ||
import {stat} from 'fs/promises'; | ||
import DOMException from 'domexception'; | ||
import {basename} from 'path'; | ||
import File from './file.js'; | ||
import Blob from './index.js'; | ||
import {MessageChannel} from 'worker_threads'; | ||
const DOMException = globalThis.DOMException || (() => { | ||
const port = new MessageChannel().port1 | ||
const ab = new ArrayBuffer(0) | ||
try { port.postMessage(ab, [ab, ab]) } | ||
catch (err) { return err.constructor } | ||
})() | ||
/** | ||
* @param {string} path filepath on the disk | ||
* @returns {Blob} | ||
* @param {string} [type] mimetype to use | ||
*/ | ||
const blobFromSync = path => from(statSync(path), path); | ||
const blobFromSync = (path, type) => fromBlob(statSync(path), path, type); | ||
/** | ||
* @param {string} path filepath on the disk | ||
* @returns {Promise<Blob>} | ||
* @param {string} [type] mimetype to use | ||
*/ | ||
const blobFrom = path => stat(path).then(stat => from(stat, path)); | ||
const blobFrom = (path, type) => stat(path).then(stat => fromBlob(stat, path, type)); | ||
const from = (stat, path) => new Blob([new BlobDataItem({ | ||
/** | ||
* @param {string} path filepath on the disk | ||
* @param {string} [type] mimetype to use | ||
*/ | ||
const fileFrom = (path, type) => stat(path).then(stat => fromFile(stat, path, type)); | ||
/** | ||
* @param {string} path filepath on the disk | ||
* @param {string} [type] mimetype to use | ||
*/ | ||
const fileFromSync = (path, type) => fromFile(statSync(path), path, type); | ||
const fromBlob = (stat, path, type = '') => new Blob([new BlobDataItem({ | ||
path, | ||
@@ -23,4 +44,11 @@ size: stat.size, | ||
start: 0 | ||
})]); | ||
})], {type}); | ||
const fromFile = (stat, path, type = '') => new File([new BlobDataItem({ | ||
path, | ||
size: stat.size, | ||
lastModified: stat.mtimeMs, | ||
start: 0 | ||
})], basename(path), { type, lastModified: stat.mtimeMs }); | ||
/** | ||
@@ -76,2 +104,2 @@ * This is a blob backed up by a file on the disk | ||
export default blobFromSync; | ||
export {Blob, blobFrom, blobFromSync}; | ||
export {File, Blob, blobFrom, blobFromSync, fileFrom, fileFromSync}; |
@@ -176,3 +176,3 @@ // 64 KiB (same size chrome slice theirs blob into Uint8array's) | ||
blobParts.push(chunk); | ||
relativeStart = 0; // All next sequental parts should start at 0 | ||
relativeStart = 0; // All next sequential parts should start at 0 | ||
@@ -199,5 +199,3 @@ // don't add the overflow to new blobParts | ||
return ( | ||
object && | ||
typeof object === 'object' && | ||
typeof object.constructor === 'function' && | ||
typeof object?.constructor === 'function' && | ||
( | ||
@@ -204,0 +202,0 @@ typeof object.stream === 'function' || |
{ | ||
"name": "fetch-blob", | ||
"version": "3.0.0-rc.0", | ||
"description": "A Blob implementation in Node.js, originally from node-fetch.", | ||
"version": "3.0.0", | ||
"description": "Blob & File implementation in Node.js, originally from node-fetch.", | ||
"main": "index.js", | ||
@@ -9,2 +9,4 @@ "type": "module", | ||
"from.js", | ||
"file.js", | ||
"file.d.ts", | ||
"index.js", | ||
@@ -24,2 +26,3 @@ "index.d.ts", | ||
"blob", | ||
"file", | ||
"node-fetch" | ||
@@ -30,3 +33,3 @@ ], | ||
}, | ||
"author": "David Frank", | ||
"author": "Jimmy Wärting <jimmy@warting.se> (https://jimmy.warting.se)", | ||
"license": "MIT", | ||
@@ -39,3 +42,5 @@ "bugs": { | ||
"rules": { | ||
"unicorn/import-index": "off", | ||
"unicorn/prefer-node-protocol": "off", | ||
"unicorn/numeric-separators-style": "off", | ||
"unicorn/prefer-spread": "off", | ||
"import/extensions": [ | ||
@@ -59,16 +64,20 @@ "error", | ||
}, | ||
"peerDependenciesMeta": { | ||
"domexception": { | ||
"optional": true | ||
} | ||
}, | ||
"devDependencies": { | ||
"ava": "^3.15.0", | ||
"c8": "^7.7.1", | ||
"codecov": "^3.8.1", | ||
"domexception": "^2.0.1", | ||
"c8": "^7.7.2", | ||
"codecov": "^3.8.2", | ||
"node-fetch": "^3.0.0-beta.9", | ||
"typescript": "^4.2.4", | ||
"xo": "^0.38.2" | ||
} | ||
"typescript": "^4.3.2", | ||
"xo": "^0.40.1" | ||
}, | ||
"funding": [ | ||
{ | ||
"type": "github", | ||
"url": "https://github.com/sponsors/jimmywarting" | ||
}, | ||
{ | ||
"type": "paypal", | ||
"url": "https://paypal.me/jimmywarting" | ||
} | ||
] | ||
} |
@@ -89,3 +89,7 @@ # fetch-blob | ||
import Blob from 'fetch-blob' | ||
import File from 'fetch-blob/file.js' | ||
import {Blob} from 'fetch-blob' | ||
import {File} from 'fetch-blob/file.js' | ||
const {Blob} = await import('fetch-blob') | ||
@@ -109,24 +113,30 @@ | ||
### Blob part backed up by filesystem | ||
To use, install [domexception](https://github.com/jsdom/domexception). | ||
```sh | ||
npm install fetch-blob domexception | ||
``` | ||
`fetch-blob/from.js` comes packed with tools to convert any filepath into either a Blob or a File | ||
It will not read the content into memory. It will only stat the file for last modified date and file size. | ||
```js | ||
// The default export is sync and use fs.stat to retrieve size & last modified | ||
// The default export is sync and use fs.stat to retrieve size & last modified as a blob | ||
import blobFromSync from 'fetch-blob/from.js' | ||
import {Blob, blobFrom, blobFromSync} from 'fetch-blob/from.js' | ||
import {File, Blob, blobFrom, blobFromSync, fileFrom, fileFromSync} from 'fetch-blob/from.js' | ||
const fsBlob1 = blobFromSync('./2-GiB-file.bin') | ||
const fsBlob2 = await blobFrom('./2-GiB-file.bin') | ||
const fsFile = fileFromSync('./2-GiB-file.bin', 'application/octet-stream') | ||
const fsBlob = await blobFrom('./2-GiB-file.mp4') | ||
// Not a 4 GiB memory snapshot, just holds 3 references | ||
// Not a 4 GiB memory snapshot, just holds references | ||
// points to where data is located on the disk | ||
const blob = new Blob([fsBlob1, fsBlob2, 'memory']) | ||
console.log(blob.size) // 4 GiB | ||
const blob = new Blob([fsFile, fsBlob, 'memory', new Uint8Array(10)]) | ||
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. | ||
`blobFrom|blobFromSync|fileFrom|fileFromSync(path, [mimetype])` | ||
### Creating Blobs backed up by other async sources | ||
Our Blob & File class are more generic then any other polyfills in the way that it can accept any blob look-a-like item | ||
An example of this is that our blob implementation can be constructed with parts coming from [BlobDataItem](https://github.com/node-fetch/fetch-blob/blob/8ef89adad40d255a3bbd55cf38b88597c1cd5480/from.js#L32) (aka a filepath) or from [buffer.Blob](https://nodejs.org/api/buffer.html#buffer_new_buffer_blob_sources_options), It dose not have to implement all the methods - just enough that it can be read/understood by our Blob implementation. The minium requirements is that it has `Symbol.toStringTag`, `size`, `slice()` and either a `stream()` or a `arrayBuffer()` method. If you then wrap it in our Blob or File `new Blob([blobDataItem])` then you get all of the other methods that should be implemented in a blob or file | ||
An example of this could be to create a file or blob like item coming from a remote HTTP request. Or from a DataBase | ||
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 of how to use the Blob. | ||
[npm-image]: https://flat.badgen.net/npm/v/fetch-blob | ||
@@ -133,0 +143,0 @@ [npm-url]: https://www.npmjs.com/package/fetch-blob |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
24984
6
10
404
0
149