@iebh/reflib 2.8.0 → 2.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,61 +1,61 @@
1
- import Emitter from '../shared/emitter.js';
2
-
3
- /**
4
- * Wrapper for streams which transforms a given input into an emitter pattern
5
- * This is designed to let regular `node:stream.Readable` objects pass through without alteration but browser based stream objects get wrapped
6
- * @param {stream.Readable|ReadableStream} inStream The input stream to wrap
7
- * @returns {stream.Readable|Emitter} Either the unedited node compatible stream or an event emitter with the same behaviour
8
- *
9
- * @emits data Emitted as `(chunk)` on each data chunk
10
- * @emits end Emitted as `()` when the input stream has closed
11
- * @emits error Emitted as `(Error)` on any read error
12
- */
13
- export default function streamEmitter (inStream) {
14
- if (inStream.getReader) { // Assume browser compatible ReadableStream
15
- /**
16
- * MC's tiny ReadableStream -> stream.Readable / Emitter pattern
17
- * There is a non-zero chance that this is going to break at some future point
18
- * This is really just a shiv for browser functionality to replicate Stream-a-like emitter pattern
19
- * @date 2023-10-19
20
- * @author Matt Carter <m@ttcarter.com>
21
- */
22
- let reader = new Emitter();
23
- Object.assign(reader, {
24
- isBrowser: true, // Tells us we are in browser env
25
- bytesRead: 0,
26
- reader: inStream.getReader(),
27
- textDecoder: new TextDecoder('utf-8'),
28
- read() { // Read one chunk + trigger emitters
29
- this.reader.read()
30
- .then(({done, value}) => {
31
- if (done) {
32
- reader.emit('end');
33
-
34
- if (this.pipeTarget)
35
- this.pipeTarget.end();
36
- } else if (value) {
37
- let data = this.textDecoder.decode(value);
38
- this.bytesRead += data.length;
39
- reader.emit('data', data);
40
-
41
- if (this.pipeTarget)
42
- this.pipeTarget.write(data);
43
-
44
- setTimeout(this.read.bind(this));
45
- }
46
- })
47
- .catch(e => this.emit('error', e))
48
- },
49
- pipeTarget: null,
50
- pipe(target) {
51
- this.pipeTarget = target;
52
- return this;
53
- },
54
- });
55
-
56
- setTimeout(()=> reader.read());
57
- return reader;
58
- } else { // Assume Node native stream.Readable
59
- return inStream;
60
- }
61
- }
1
+ import Emitter from '../shared/emitter.js';
2
+
3
+ /**
4
+ * Wrapper for streams which transforms a given input into an emitter pattern
5
+ * This is designed to let regular `node:stream.Readable` objects pass through without alteration but browser based stream objects get wrapped
6
+ * @param {stream.Readable|ReadableStream} inStream The input stream to wrap
7
+ * @returns {stream.Readable|Emitter} Either the unedited node compatible stream or an event emitter with the same behaviour
8
+ *
9
+ * @emits data Emitted as `(chunk)` on each data chunk
10
+ * @emits end Emitted as `()` when the input stream has closed
11
+ * @emits error Emitted as `(Error)` on any read error
12
+ */
13
+ export default function streamEmitter (inStream) {
14
+ if (inStream.getReader) { // Assume browser compatible ReadableStream
15
+ /**
16
+ * MC's tiny ReadableStream -> stream.Readable / Emitter pattern
17
+ * There is a non-zero chance that this is going to break at some future point
18
+ * This is really just a shiv for browser functionality to replicate Stream-a-like emitter pattern
19
+ * @date 2023-10-19
20
+ * @author Matt Carter <m@ttcarter.com>
21
+ */
22
+ let reader = new Emitter();
23
+ Object.assign(reader, {
24
+ isBrowser: true, // Tells us we are in browser env
25
+ bytesRead: 0,
26
+ reader: inStream.getReader(),
27
+ textDecoder: new TextDecoder('utf-8'),
28
+ read() { // Read one chunk + trigger emitters
29
+ this.reader.read()
30
+ .then(({done, value}) => {
31
+ if (done) {
32
+ reader.emit('end');
33
+
34
+ if (this.pipeTarget)
35
+ this.pipeTarget.end();
36
+ } else if (value) {
37
+ let data = this.textDecoder.decode(value);
38
+ this.bytesRead += data.length;
39
+ reader.emit('data', data);
40
+
41
+ if (this.pipeTarget)
42
+ this.pipeTarget.write(data);
43
+
44
+ setTimeout(this.read.bind(this));
45
+ }
46
+ })
47
+ .catch(e => this.emit('error', e))
48
+ },
49
+ pipeTarget: null,
50
+ pipe(target) {
51
+ this.pipeTarget = target;
52
+ return this;
53
+ },
54
+ });
55
+
56
+ setTimeout(()=> reader.read());
57
+ return reader;
58
+ } else { // Assume Node native stream.Readable
59
+ return inStream;
60
+ }
61
+ }