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`
This commit is contained in:
+5
-2
@@ -8,9 +8,9 @@ project adheres to [Semantic Versioning](http://semver.org/).
|
||||
(Unreleased)
|
||||
==================
|
||||
### Removed
|
||||
-
|
||||
-
|
||||
### Changed
|
||||
-
|
||||
-
|
||||
### Added
|
||||
-
|
||||
### Fixed
|
||||
@@ -19,6 +19,8 @@ project adheres to [Semantic Versioning](http://semver.org/).
|
||||
- File name are now casted to string [#109]
|
||||
- Slicing in the middle of multiple parts added more bytes than what what it should have [#109]
|
||||
- Prefixed `stream/web` import with `node:` to allow easier static analysis detection of Node built-ins [#122]
|
||||
- Added `node:` prefix in `from.js` as well [#114]
|
||||
- Suppress warning when importing `stream/web` [#114]
|
||||
|
||||
## v3.1.2
|
||||
|
||||
@@ -102,3 +104,4 @@ project adheres to [Semantic Versioning](http://semver.org/).
|
||||
|
||||
[#108]: https://github.com/node-fetch/fetch-blob/pull/108
|
||||
[#109]: https://github.com/node-fetch/fetch-blob/pull/109
|
||||
[#114]: https://github.com/node-fetch/fetch-blob/pull/114
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import {statSync, createReadStream, promises as fs} from 'fs';
|
||||
import {basename} from 'path';
|
||||
import {MessageChannel} from 'worker_threads';
|
||||
import {statSync, createReadStream, promises as fs} from 'node:fs';
|
||||
import {basename} from 'node:path';
|
||||
import {MessageChannel} from 'node:worker_threads';
|
||||
|
||||
import File from './file.js';
|
||||
import Blob from './index.js';
|
||||
|
||||
+35
-21
@@ -1,37 +1,51 @@
|
||||
/* c8 ignore start */
|
||||
// 64 KiB (same size chrome slice theirs blob into Uint8array's)
|
||||
const POOL_SIZE = 65536;
|
||||
const POOL_SIZE = 65536
|
||||
|
||||
if (!globalThis.ReadableStream) {
|
||||
// `node:stream/web` got introduced in v16.5.0 as experimental
|
||||
// and it's preferred over the polyfilled version. So we also
|
||||
// suppress the warning that gets emitted by NodeJS for using it.
|
||||
try {
|
||||
Object.assign(globalThis, require('node:stream/web'))
|
||||
const process = require('node:process')
|
||||
const { emitWarning } = process
|
||||
try {
|
||||
process.emitWarning = () => {}
|
||||
Object.assign(globalThis, require('node:stream/web'))
|
||||
process.emitWarning = emitWarning
|
||||
} catch (error) {
|
||||
process.emitWarning = emitWarning
|
||||
throw error
|
||||
}
|
||||
} catch (error) {
|
||||
// TODO: Remove when only supporting node >= 16.5.0
|
||||
// fallback to polyfill implementation
|
||||
Object.assign(globalThis, require('web-streams-polyfill/dist/ponyfill.es2018.js'))
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
const {Blob} = require('buffer')
|
||||
// Don't use node: prefix for this, require+node: is not supported until node v14.14
|
||||
// Only `import()` can use prefix in 12.20 and later
|
||||
const { Blob } = require('buffer')
|
||||
if (Blob && !Blob.prototype.stream) {
|
||||
Blob.prototype.stream = function name(params) {
|
||||
let position = 0;
|
||||
const blob = this;
|
||||
Blob.prototype.stream = function name (params) {
|
||||
let position = 0
|
||||
const blob = this
|
||||
|
||||
return new ReadableStream({
|
||||
type: 'bytes',
|
||||
async pull(ctrl) {
|
||||
const chunk = blob.slice(position, Math.min(blob.size, position + POOL_SIZE));
|
||||
const buffer = await chunk.arrayBuffer();
|
||||
position += buffer.byteLength;
|
||||
ctrl.enqueue(new Uint8Array(buffer))
|
||||
return new ReadableStream({
|
||||
type: 'bytes',
|
||||
async pull (ctrl) {
|
||||
const chunk = blob.slice(position, Math.min(blob.size, position + POOL_SIZE))
|
||||
const buffer = await chunk.arrayBuffer()
|
||||
position += buffer.byteLength
|
||||
ctrl.enqueue(new Uint8Array(buffer))
|
||||
|
||||
if (position === blob.size) {
|
||||
ctrl.close()
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
if (position === blob.size) {
|
||||
ctrl.close()
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
} catch (error) {}
|
||||
/* c8 ignore end */
|
||||
|
||||
Reference in New Issue
Block a user