@iebh/reflib 2.1.1 → 2.1.2
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.
- package/modules/json.js +0 -1
- package/package.json +1 -1
- package/shared/streamEmitter.js +46 -4
package/modules/json.js
CHANGED
package/package.json
CHANGED
package/shared/streamEmitter.js
CHANGED
|
@@ -11,8 +11,50 @@ import Emitter from '../shared/emitter.js';
|
|
|
11
11
|
* @emits error Emitted as `(Error)` on any read error
|
|
12
12
|
*/
|
|
13
13
|
export default function streamEmitter(inStream) {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
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
|
+
bytesRead: 0,
|
|
25
|
+
reader: inStream.getReader(),
|
|
26
|
+
textDecoder: new TextDecoder('utf-8'),
|
|
27
|
+
read() { // Read one chunk + trigger emitters
|
|
28
|
+
this.reader.read()
|
|
29
|
+
.then(({done, value}) => {
|
|
30
|
+
if (done) {
|
|
31
|
+
reader.emit('end');
|
|
32
|
+
|
|
33
|
+
if (this.pipeTarget)
|
|
34
|
+
this.pipeTarget.end();
|
|
35
|
+
} else if (value) {
|
|
36
|
+
let data = this.textDecoder.decode(value);
|
|
37
|
+
this.bytesRead += data.length;
|
|
38
|
+
reader.emit('data', data);
|
|
39
|
+
|
|
40
|
+
if (this.pipeTarget)
|
|
41
|
+
this.pipeTarget.write(data);
|
|
42
|
+
|
|
43
|
+
setTimeout(this.read.bind(this));
|
|
44
|
+
}
|
|
45
|
+
})
|
|
46
|
+
.catch(e => this.emit('error', e))
|
|
47
|
+
},
|
|
48
|
+
pipeTarget: null,
|
|
49
|
+
pipe(target) {
|
|
50
|
+
this.pipeTarget = target;
|
|
51
|
+
return this;
|
|
52
|
+
},
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
setTimeout(()=> reader.read());
|
|
56
|
+
return reader;
|
|
57
|
+
} else { // Assume Node native stream.Readable
|
|
58
|
+
return inStream;
|
|
59
|
+
}
|
|
18
60
|
}
|