@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 CHANGED
@@ -1,5 +1,4 @@
1
1
  import Emitter from '../shared/emitter.js';
2
- // FIXME: CF: Browsers freak out without pollyfills if this is imported
3
2
  import JSONStream from 'JSONStream';
4
3
 
5
4
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@iebh/reflib",
3
- "version": "2.1.1",
3
+ "version": "2.1.2",
4
4
  "description": "Reference / Citation reference library utilities",
5
5
  "scripts": {
6
6
  "lint": "eslint lib modules shared test",
@@ -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
- // FIXME: Need to examine inStream and multiplex
15
- // inStream.pipeTo - a browser stream - passthru
16
- // !inStream.pipeTo - probably Node stream - need to glue pipeTo as a promiseable
17
- return 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
+ 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
  }