Skip to content

Commit d39cb4a

Browse files
authored
Make streaming functionality optional (#172)
* Add readable-stream as optional dependency * Attempt to use readable-stream if available, falling back on native stream, and if neither are available, throwing an error when calling stream
1 parent b1cfdc4 commit d39cb4a

File tree

3 files changed

+171
-1
lines changed

3 files changed

+171
-1
lines changed

lib/stream.js

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
1-
const { Readable } = require("stream");
1+
// Attempt to use readable-stream if available, attempt to use the built-in stream module.
2+
let Readable;
3+
try {
4+
Readable = require("readable-stream").Readable;
5+
} catch (e) {
6+
try {
7+
Readable = require("stream").Readable;
8+
} catch (e) {
9+
Readable = null;
10+
}
11+
}
212

313
/**
414
* A server-sent event.
@@ -42,6 +52,12 @@ class Stream extends Readable {
4252
* @param {object} options The fetch options.
4353
*/
4454
constructor(url, options) {
55+
if (!Readable) {
56+
throw new Error(
57+
"Readable streams are not supported. Please use Node.js 18 or later, or install the readable-stream package."
58+
);
59+
}
60+
4561
super();
4662
this.url = url;
4763
this.options = options;

package-lock.json

+151
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
"lint": "biome lint .",
2020
"test": "jest"
2121
},
22+
"optionalDependencies": {
23+
"readable-stream": ">=4.0.0"
24+
},
2225
"devDependencies": {
2326
"@biomejs/biome": "^1.4.1",
2427
"@types/jest": "^29.5.3",

0 commit comments

Comments
 (0)