You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+50-20
Original file line number
Diff line number
Diff line change
@@ -23,49 +23,78 @@ npm install fetch-blob
23
23
- internal buffers was replaced with Uint8Arrays
24
24
- CommonJS was replaced with ESM
25
25
- The node stream returned by calling `blob.stream()` was replaced with a simple generator function that yields Uint8Array (Breaking change)
26
+
(Read "Differences from other blobs" for more info.)
26
27
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
28
+
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
29
+
30
+
</details>
31
+
32
+
<details>
33
+
<summary>Differences from other Blobs</summary>
34
+
35
+
- Unlike NodeJS `buffer.Blob` (Added in: v15.7.0) and browser native Blob this polyfilled version can't be sent via PostMessage
36
+
- This blob version is more arbitrary, it can be constructed with blob parts that isn't a instance of itself
37
+
it has to look and behave as a blob to be accepted as a blob part.
38
+
- The benefit of this is that you can create other types of blobs that don't contain any internal data that has to be read in other ways, such as the `BlobDataItem` created in `from.js` that wraps a file path into a blob-like item and read lazily (nodejs plans to [implement this][fs-blobs] as well)
39
+
- The `blob.stream()` is the most noticeable differences. It returns a AsyncGeneratorFunction that yields Uint8Arrays
40
+
41
+
The reasoning behind `Blob.prototype.stream()` is that NodeJS readable stream
42
+
isn't spec compatible with whatwg streams and we didn't want to import the hole whatwg stream polyfill for node
43
+
or browserify NodeJS streams for the browsers and picking any flavor over the other. So we decided to opted out
30
44
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
45
+
that both yields Uint8Array. this is the most isomorphic way with the use of `for-await-of` loops.
46
+
It would be redundant to convert anything to whatwg streams and than convert it back to
32
47
node streams since you work inside of Node.
33
48
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
49
equivalent for `Readable.from(iterable)`<sup>[2](https://github.com/whatwg/streams/issues/1018)</sup>
35
50
36
-
But for now if you really want/need a Node Stream then you can do so using this transformation
51
+
But for now if you really need a Node Stream then you can do so using this transformation
37
52
```js
38
53
import {Readable} from'stream'
39
54
conststream=Readable.from(blob.stream())
40
55
```
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
56
+
But if you don't need it to be a stream then you can just use the asyncIterator part of it that is isomorphic.
42
57
```js
43
58
forawait (constchunkofblob.stream()) {
44
59
console.log(chunk) // uInt8Array
45
60
}
46
61
```
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
-
62
+
If you need to make some feature detection to fix this different behavior
63
+
```js
64
+
if (Blob.prototype.stream?.constructor?.name==='AsyncGeneratorFunction') {
65
+
// not spec compatible, monkey patch it...
66
+
// (Alternative you could extend the Blob and use super.stream())
0 commit comments