From 811cf94c234dd2cb419441d59ce82ce48edcbb74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jimmy=20Wa=CC=88rting?= Date: Thu, 28 Oct 2021 13:25:54 +0200 Subject: [PATCH 1/4] Suppress warnings when importing web stream --- streams.cjs | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/streams.cjs b/streams.cjs index fdfcc08..abd9a99 100644 --- a/streams.cjs +++ b/streams.cjs @@ -3,16 +3,30 @@ const POOL_SIZE = 65536; if (!globalThis.ReadableStream) { - try { - Object.assign(globalThis, require('node:stream/web')) - } catch (error) { - // TODO: Remove when only supporting node >= 16.5.0 + // `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 { + 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) { + // 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; From 09bcfbe23cada739336e7e0b65e8c6bc8c34c7e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jimmy=20Wa=CC=88rting?= Date: Thu, 28 Oct 2021 13:26:41 +0200 Subject: [PATCH 2/4] added node: prefix --- from.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/from.js b/from.js index e31fe92..02e938e 100644 --- a/from.js +++ b/from.js @@ -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'; From d3504313e58162d73bac3be5847502f394f5c86e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jimmy=20Wa=CC=88rting?= Date: Thu, 28 Oct 2021 13:27:18 +0200 Subject: [PATCH 3/4] updated changelog --- CHANGELOG.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4a70805..a444841 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 From 7e4a06344ae8dbbaa61dd08c2bafbb0cc87e0063 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jimmy=20Wa=CC=88rting?= Date: Thu, 28 Oct 2021 13:30:10 +0200 Subject: [PATCH 4/4] apply `standard streams.cjs --fix` --- streams.cjs | 72 ++++++++++++++++++++++++++--------------------------- 1 file changed, 36 insertions(+), 36 deletions(-) diff --git a/streams.cjs b/streams.cjs index abd9a99..f760959 100644 --- a/streams.cjs +++ b/streams.cjs @@ -1,51 +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 { - 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) { - // fallback to polyfill implementation + // `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 { + 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) { + // fallback to polyfill implementation Object.assign(globalThis, require('web-streams-polyfill/dist/ponyfill.es2018.js')) } } try { - // 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 + // 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 */