Skip to content

Commit 6ec26a2

Browse files
committed
Updated the Readme and code examples
1 parent 85105af commit 6ec26a2

File tree

4 files changed

+67
-17
lines changed

4 files changed

+67
-17
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
strategy:
1515
matrix:
1616
os: [ubuntu-latest, windows-latest, macOS-latest]
17-
node: ["15", "14", "12", engines]
17+
node: ["16", "15", "14", engines]
1818
exclude:
1919
# On Windows, run tests with only the LTS environments.
2020
- os: windows-latest

README.md

Lines changed: 64 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,66 @@ A Blob implementation in Node.js, originally from [node-fetch](https://github.co
1313
npm install fetch-blob
1414
```
1515

16+
<details>
17+
<summary>Upgrading from 2x to 3x</summary>
18+
19+
Updating from 2 to 3 should be a breeze since there is not many changes to the blob specification.
20+
The major cause of a major release is coding standards.
21+
- internal WeakMaps was replaced with private fields
22+
- internal Buffer.from was replaced with TextEncoder/Decoder
23+
- internal buffers was replaced with Uint8Arrays
24+
- CommonJS was replaced with ESM
25+
- The node stream returned by calling `blob.stream()` was replaced with a simple generator function that yields Uint8Array (Breaking change)
26+
27+
The reasoning behind `Blob.prototype.stream()` is that node readable stream
28+
isn't spec compatible with whatwg stream and we didn't want to import a hole whatwg stream polyfill for node
29+
or browserify hole node-stream for browsers and picking any flavor over the other. So we decided to opted out
30+
of any stream and just implement the bear minium of what both streams have in common which is the asyncIterator
31+
that both yields Uint8Array. It would be redundant to convert anything to whatwg streams and than convert it back to
32+
node streams since you work inside of Node.
33+
It will probably stay like this until nodejs get native support for whatwg<sup>[1][https://github.com/nodejs/whatwg-stream]</sup> streams and whatwg stream add the node
34+
equivalent for `Readable.from(iterable)`<sup>[2](https://github.com/whatwg/streams/issues/1018)</sup>
35+
36+
But for now if you really want/need a Node Stream then you can do so using this transformation
37+
```js
38+
import {Readable} from 'stream'
39+
const stream = Readable.from(blob.stream())
40+
```
41+
But if you don't need it to be a stream then you can just use the asyncIterator part of it that both whatwg stream and node stream have in common
42+
```js
43+
for await (const chunk of blob.stream()) {
44+
console.log(chunk) // uInt8Array
45+
}
46+
```
47+
48+
All of this changes have made it dependency free of any core node modules, so it would be possible to just import it using http-import from a CDN without any bundling
49+
50+
</details>
51+
1652
## Usage
1753

1854
```js
19-
const Blob = require('fetch-blob');
20-
const fetch = require('node-fetch');
21-
22-
fetch('https://httpbin.org/post', {
23-
method: 'POST',
24-
body: new Blob(['Hello World'], { type: 'text/plain' })
25-
})
26-
.then(res => res.json());
27-
.then(json => console.log(json));
55+
// Ways to import
56+
// (note that it's dependency free ESM package so regular http-import from CDN works too)
57+
import Blob from 'fetch-blob';
58+
import {Blob} from 'fetch-blob';
59+
const {Blob} = await import('fetch-blob');
60+
61+
const blob = new Blob(['hello, world']);
62+
63+
// Ways to read the blob:
64+
65+
await blob.text()
66+
67+
await blob.arrayBuffer()
68+
69+
for await (let chunk of blob.stream()) { ... }
70+
71+
// turn the async iterator into a node stream
72+
stream.Readable.from(blob.stream())
73+
74+
// turn the async iterator into a whatwg stream (feature)
75+
globalThis.ReadableStream.from(blob.stream())
2876
```
2977

3078
### Blob part backed up by filesystem
@@ -35,13 +83,16 @@ npm install fetch-blob domexception
3583
```
3684

3785
```js
38-
const blobFrom = require('fetch-blob/from.js');
39-
const blob1 = blobFrom('./2-GiB-file.bin');
40-
const blob2 = blobFrom('./2-GiB-file.bin');
86+
// The default export is sync and use fs.stat to retrieve size & last modified
87+
import blobFromSync from 'fetch-blob/from.js'
88+
import {Blob, blobFrom, blobFromSync} 'fetch-blob/from.js'
89+
90+
const fsBlob1 = blobFromSync('./2-GiB-file.bin');
91+
const fsBlob2 = await blobFrom('./2-GiB-file.bin');
4192

4293
// Not a 4 GiB memory snapshot, just holds 3 references
4394
// points to where data is located on the disk
44-
const blob = new Blob([blob1, blob2]);
95+
const blob = new Blob([fsBlob1, fsBlob2, 'memory']);
4596
console.log(blob.size) // 4 GiB
4697
```
4798

from.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const from = (stat, path) => new Blob([new BlobDataItem({
2929
*
3030
* @private
3131
*/
32-
class BlobDataItem {
32+
export default class BlobDataItem {
3333
#path;
3434
#start;
3535

@@ -71,5 +71,4 @@ class BlobDataItem {
7171
}
7272
}
7373

74-
export default blobFromSync;
7574
export {Blob, blobFrom, blobFromSync};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "fetch-blob",
3-
"version": "3.0.0",
3+
"version": "3.0.0-rc.0",
44
"description": "A Blob implementation in Node.js, originally from node-fetch.",
55
"main": "index.js",
66
"type": "module",

0 commit comments

Comments
 (0)