Skip to content

Commit c5c2b52

Browse files
authored
fix(warning): Supress warnings when importing experimental web stream from NodeJS (#125)
* Suppress warnings when importing web stream * added node: prefix * updated changelog * apply `standard streams.cjs --fix`
1 parent 836709f commit c5c2b52

File tree

3 files changed

+43
-26
lines changed

3 files changed

+43
-26
lines changed

CHANGELOG.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ project adheres to [Semantic Versioning](http://semver.org/).
88
(Unreleased)
99
==================
1010
### Removed
11-
-
11+
-
1212
### Changed
13-
-
13+
-
1414
### Added
1515
-
1616
### Fixed
@@ -19,6 +19,8 @@ project adheres to [Semantic Versioning](http://semver.org/).
1919
- File name are now casted to string [#109]
2020
- Slicing in the middle of multiple parts added more bytes than what what it should have [#109]
2121
- Prefixed `stream/web` import with `node:` to allow easier static analysis detection of Node built-ins [#122]
22+
- Added `node:` prefix in `from.js` as well [#114]
23+
- Suppress warning when importing `stream/web` [#114]
2224

2325
## v3.1.2
2426

@@ -102,3 +104,4 @@ project adheres to [Semantic Versioning](http://semver.org/).
102104

103105
[#108]: https://github.com/node-fetch/fetch-blob/pull/108
104106
[#109]: https://github.com/node-fetch/fetch-blob/pull/109
107+
[#114]: https://github.com/node-fetch/fetch-blob/pull/114

from.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import {statSync, createReadStream, promises as fs} from 'fs';
2-
import {basename} from 'path';
3-
import {MessageChannel} from 'worker_threads';
1+
import {statSync, createReadStream, promises as fs} from 'node:fs';
2+
import {basename} from 'node:path';
3+
import {MessageChannel} from 'node:worker_threads';
44

55
import File from './file.js';
66
import Blob from './index.js';

streams.cjs

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,51 @@
11
/* c8 ignore start */
22
// 64 KiB (same size chrome slice theirs blob into Uint8array's)
3-
const POOL_SIZE = 65536;
3+
const POOL_SIZE = 65536
44

55
if (!globalThis.ReadableStream) {
6+
// `node:stream/web` got introduced in v16.5.0 as experimental
7+
// and it's preferred over the polyfilled version. So we also
8+
// suppress the warning that gets emitted by NodeJS for using it.
69
try {
7-
Object.assign(globalThis, require('node:stream/web'))
10+
const process = require('node:process')
11+
const { emitWarning } = process
12+
try {
13+
process.emitWarning = () => {}
14+
Object.assign(globalThis, require('node:stream/web'))
15+
process.emitWarning = emitWarning
16+
} catch (error) {
17+
process.emitWarning = emitWarning
18+
throw error
19+
}
820
} catch (error) {
9-
// TODO: Remove when only supporting node >= 16.5.0
21+
// fallback to polyfill implementation
1022
Object.assign(globalThis, require('web-streams-polyfill/dist/ponyfill.es2018.js'))
1123
}
1224
}
1325

1426
try {
15-
const {Blob} = require('buffer')
27+
// Don't use node: prefix for this, require+node: is not supported until node v14.14
28+
// Only `import()` can use prefix in 12.20 and later
29+
const { Blob } = require('buffer')
1630
if (Blob && !Blob.prototype.stream) {
17-
Blob.prototype.stream = function name(params) {
18-
let position = 0;
19-
const blob = this;
31+
Blob.prototype.stream = function name (params) {
32+
let position = 0
33+
const blob = this
2034

21-
return new ReadableStream({
22-
type: 'bytes',
23-
async pull(ctrl) {
24-
const chunk = blob.slice(position, Math.min(blob.size, position + POOL_SIZE));
25-
const buffer = await chunk.arrayBuffer();
26-
position += buffer.byteLength;
27-
ctrl.enqueue(new Uint8Array(buffer))
35+
return new ReadableStream({
36+
type: 'bytes',
37+
async pull (ctrl) {
38+
const chunk = blob.slice(position, Math.min(blob.size, position + POOL_SIZE))
39+
const buffer = await chunk.arrayBuffer()
40+
position += buffer.byteLength
41+
ctrl.enqueue(new Uint8Array(buffer))
2842

29-
if (position === blob.size) {
30-
ctrl.close()
31-
}
32-
}
33-
})
34-
}
35-
}
43+
if (position === blob.size) {
44+
ctrl.close()
45+
}
46+
}
47+
})
48+
}
49+
}
3650
} catch (error) {}
3751
/* c8 ignore end */

0 commit comments

Comments
 (0)