@iebh/reflib 2.0.5 → 2.1.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.
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2021 Institute for Evidence-Based Healthcare
3
+ Copyright (c) 2022 Bond University Institute for Evidence-Based Healthcare
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy of
6
6
  this software and associated documentation files (the "Software"), to deal in
package/LICENSES.md ADDED
@@ -0,0 +1,97 @@
1
+ # @iebh/reflib (MIT)
2
+
3
+ ## [JSONStream](https://github.com/dominictarr/JSONStream)
4
+ ```
5
+ Apache License, Version 2.0
6
+
7
+ Copyright (c) 2011 Dominic Tarr
8
+
9
+ Licensed under the Apache License, Version 2.0 (the "License");
10
+ you may not use this file except in compliance with the License.
11
+ You may obtain a copy of the License at
12
+
13
+ http://www.apache.org/licenses/LICENSE-2.0
14
+
15
+ Unless required by applicable law or agreed to in writing, software
16
+ distributed under the License is distributed on an "AS IS" BASIS,
17
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
+ See the License for the specific language governing permissions and
19
+ limitations under the License.
20
+
21
+ ```
22
+
23
+ ## [htmlparser2](https://github.com/fb55/htmlparser2)
24
+ ```
25
+ Copyright 2010, 2011, Chris Winberry <chris@winberry.net>. All rights reserved.
26
+ Permission is hereby granted, free of charge, to any person obtaining a copy
27
+ of this software and associated documentation files (the "Software"), to
28
+ deal in the Software without restriction, including without limitation the
29
+ rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
30
+ sell copies of the Software, and to permit persons to whom the Software is
31
+ furnished to do so, subject to the following conditions:
32
+
33
+ The above copyright notice and this permission notice shall be included in
34
+ all copies or substantial portions of the Software.
35
+
36
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
37
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
38
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
39
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
40
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
41
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
42
+ IN THE SOFTWARE.
43
+ ```
44
+
45
+ ## [mitt](https://github.com/developit/mitt)
46
+ ```
47
+ MIT License
48
+
49
+ Copyright (c) 2021 Jason Miller
50
+
51
+ Permission is hereby granted, free of charge, to any person obtaining a copy
52
+ of this software and associated documentation files (the "Software"), to deal
53
+ in the Software without restriction, including without limitation the rights
54
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
55
+ copies of the Software, and to permit persons to whom the Software is
56
+ furnished to do so, subject to the following conditions:
57
+
58
+ The above copyright notice and this permission notice shall be included in all
59
+ copies or substantial portions of the Software.
60
+
61
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
62
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
63
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
64
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
65
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
66
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
67
+ SOFTWARE.
68
+
69
+ ```
70
+
71
+ ## [vite-plugin-replace](https://github.com/leanupjs/leanup)
72
+ ```
73
+ MIT License
74
+
75
+ Copyright (c) 2021 leanupjs
76
+
77
+ Permission is hereby granted, free of charge, to any person obtaining a copy
78
+ of this software and associated documentation files (the "Software"), to deal
79
+ in the Software without restriction, including without limitation the rights
80
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
81
+ copies of the Software, and to permit persons to whom the Software is
82
+ furnished to do so, subject to the following conditions:
83
+
84
+ The above copyright notice and this permission notice shall be included in all
85
+ copies or substantial portions of the Software.
86
+
87
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
88
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
89
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
90
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
91
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
92
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
93
+ SOFTWARE.
94
+
95
+ ```
96
+
97
+
package/README.md CHANGED
@@ -4,8 +4,7 @@ Reference library processing for Node.
4
4
 
5
5
  This library provides various read/write functionality to process citation libraries and handle individual references (henceforth "Refs").
6
6
 
7
- **NOTE: THIS LIBRARY IS STILL UNDER CONSTRUCTION**
8
- **Please use [Version 1](https://github.com/hash-bang/Reflib-Node) ([NPM](https://www.npmjs.com/package/reflib)) until this message is removed**
7
+ This module forms part of the [Systematic Review Accelerator](https://sr-accelerator.com)
9
8
 
10
9
 
11
10
  Compatibility
@@ -193,6 +192,19 @@ reflib.readFile('./data/json/json1.json')
193
192
  .then(refs => /* Do something with Ref collection */)
194
193
  ```
195
194
 
195
+ An emitter is available to track progress while reading. Note that due to the chainable nature of promises the first return contains the `emitter` key only:
196
+
197
+ ```javascript
198
+ let reader = reflib.readFile('./data/json/json1.json');
199
+
200
+ reader.emitter
201
+ .on('progress', ({readBytes, totalSize, refsFound}) => /* Report progress somehow */);
202
+ .on('end', ({refsFound}) => /* Report progress somehow */);
203
+
204
+ reader
205
+ .then(refs => /* Do something with Ref collection */)
206
+ ```
207
+
196
208
  uploadFile(options)
197
209
  ===================
198
210
  Available: Browser
@@ -257,12 +269,3 @@ Credits
257
269
  =======
258
270
  Developed for the [Bond University Institute for Evidence-Based Healthcare](https://iebh.bond.edu.au).
259
271
  Please contact [the author](mailto:matt_carter@bond.edu.au) with any issues.
260
-
261
-
262
- TODO
263
- ====
264
- - [x] Basic parsing iterfaces
265
- - [ ] "progress" emitter for files
266
- - [x] `.uploadFile()` browser compatibility
267
- - [ ] `.downloadFile()` browser compatibility
268
- - [x] `setup()` functions per module to avoid things like map calculations unless the module is actually needed
package/lib/readFile.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import {createReadStream} from 'node:fs';
2
+ import Emitter from '../shared/emitter.js';
2
3
  import {stat} from 'node:fs/promises';
3
4
  import {identifyFormat} from './identifyFormat.js';
4
5
  import {readStream} from './readStream.js';
@@ -9,16 +10,26 @@ import {readStream} from './readStream.js';
9
10
  * @param {string} path The file path to parse
10
11
  * @param {Object} [options] Additional options to pass to the parser
11
12
  * @param {string} [options.module] The module to use if overriding from the file path
13
+ *
12
14
  * @returns {Promise<Array>} An eventual array of all references parsed from the file
15
+ * @property {EventEmitter} emitter An event emitter which will fire the below events
16
+ *
17
+ * @fires progress Emitted as `({readBytes: Number, totalSize: Number, refsFound: Number})`
18
+ * @fires end Emitted as `({refsFound: Number})` when the reading operation has completed
13
19
  */
14
20
  export function readFile(path, options) {
15
- let settings = {...options};
21
+ let settings = {
22
+ progressTotal: false,
23
+ ...options,
24
+ };
16
25
  let module = options?.module || identifyFormat(path)?.id;
17
26
  if (!module) throw new Error(`Unable to identify reference library format for file path "${path}"`);
18
27
 
19
- return stat(path)
28
+ let promiseEmitter = Promise.resolve()
29
+ .then(()=> stat(path))
20
30
  .then(stats => new Promise((resolve, reject) => {
21
- let refs = [];
31
+ let refs = []; // eslint-disable-line no-unused-vars
32
+
22
33
  readStream(
23
34
  module,
24
35
  createReadStream(path),
@@ -30,5 +41,23 @@ export function readFile(path, options) {
30
41
  .on('end', ()=> resolve(refs))
31
42
  .on('error', reject)
32
43
  .on('ref', ref => refs.push(ref))
44
+ .on('progress', readBytes => promiseEmitter.emitter.emit('progress', {
45
+ readBytes,
46
+ totalSize: stats.size,
47
+ refsFound: refs.length,
48
+ }))
33
49
  }))
50
+ .then(refs => {
51
+ promiseEmitter.emitter.emit('end', {refsFound: refs.length})
52
+ return refs;
53
+ })
54
+
55
+ // Extend our base promise with an emitter subkey
56
+ return Object.defineProperties(promiseEmitter, {
57
+ emitter: {
58
+ value: Emitter(),
59
+ enumerable: true,
60
+ writable: false,
61
+ },
62
+ });
34
63
  }
@@ -3,6 +3,7 @@ import Emitter from '../shared/emitter.js';
3
3
 
4
4
  // TODO: CF: Don't need to import both, it depends if we are on browser or node
5
5
  import * as htmlparser2 from "htmlparser2";
6
+ // FIXME: CF: Browsers freak out without pollyfills if this is imported
6
7
  import {WritableStream as XMLParser} from 'htmlparser2/lib/WritableStream';
7
8
 
8
9
  /**
@@ -108,6 +109,7 @@ export function readStream(stream) {
108
109
  if (typeof stream.pipe === 'function') {
109
110
  // We are on the node.js client
110
111
  let parser = new XMLParser(parserOptions);
112
+ stream.on('data', ()=> emitter.emit('progress', stream.bytesRead))
111
113
  stream.pipe(parser)
112
114
  return;
113
115
  }
@@ -14,6 +14,7 @@
14
14
  * @emits ref Emitted with a single ref object when found
15
15
  * @emits end Emitted when parsing has completed
16
16
  * @emits error Emitted when an error has been raised
17
+ * @emits progress Emitted as (bytesRead) when reading a stream
17
18
  */
18
19
  export function readStream(stream, options) {
19
20
  // Stub
package/modules/json.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import Emitter from '../shared/emitter.js';
2
+ // FIXME: CF: Browsers freak out without pollyfills if this is imported
2
3
  import JSONStream from 'JSONStream';
3
4
 
4
5
  /**
@@ -9,7 +10,8 @@ export function readStream(stream) {
9
10
  let emitter = Emitter();
10
11
 
11
12
  // Queue up the parser in the next tick (so we can return the emitter first)
12
- setTimeout(()=>
13
+ setTimeout(()=> {
14
+ stream.on('data', ()=> emitter.emit('progress', stream.bytesRead));
13
15
  stream.pipe(
14
16
  JSONStream.parse('*')
15
17
  .on('data', ref => emitter.emit('ref', {
@@ -19,7 +21,7 @@ export function readStream(stream) {
19
21
  .on('end', ()=> emitter.emit('end'))
20
22
  .on('error', emitter.emit.bind('error'))
21
23
  )
22
- );
24
+ });
23
25
 
24
26
  return emitter;
25
27
  }
@@ -122,6 +122,7 @@ export function readStream(stream, options) {
122
122
  setTimeout(()=> {
123
123
  stream
124
124
  .on('data', chunkBuffer => {
125
+ emitter.emit('progress', stream.bytesRead);
125
126
  buffer += chunkBuffer.toString(); // Append incomming data to the partial-buffer we're holding in memory
126
127
 
127
128
  let bufferCrop = 0; // How many bytes to shift off the front of the buffer based on the last full reference we saw, should end up at the last byte offset of buffer that is valid to shift-truncate to
package/modules/ris.js CHANGED
@@ -21,6 +21,7 @@ export function readStream(stream, options) {
21
21
  setTimeout(()=> {
22
22
  stream
23
23
  .on('data', chunkBuffer => {
24
+ emitter.emit('progress', stream.bytesRead);
24
25
  buffer += chunkBuffer.toString(); // Append incomming data to the partial-buffer we're holding in memory
25
26
 
26
27
  let bufferCrop = 0; // How many bytes to shift off the front of the buffer based on the last full reference we saw, should end up at the last byte offset of buffer that is valid to shift-truncate to
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@iebh/reflib",
3
- "version": "2.0.5",
3
+ "version": "2.1.1",
4
4
  "description": "Reference / Citation reference library utilities",
5
5
  "scripts": {
6
6
  "lint": "eslint lib modules shared test",
@@ -17,7 +17,10 @@
17
17
  "citations",
18
18
  "library"
19
19
  ],
20
- "author": "Matt Carter <m@ttcarter.com>",
20
+ "author": [
21
+ "Matt Carter <m@ttcarter.com> (https://github.com/hash-bang)",
22
+ "Connor Forbes <cforbes.software@gmail.com> (https://github.com/connorf25)"
23
+ ],
21
24
  "license": "MIT",
22
25
  "bugs": {
23
26
  "url": "https://github.com/IEBH/RefLib/issues"
@@ -40,13 +43,13 @@
40
43
  "eslint": "^8.15.0",
41
44
  "mocha": "^10.0.0",
42
45
  "mocha-logger": "^1.0.8",
43
- "temp": "^0.9.4"
46
+ "temp": "^0.9.4",
47
+ "vite-plugin-replace": "^0.1.1"
44
48
  },
45
49
  "dependencies": {
46
50
  "htmlparser2": "^8.0.1",
47
51
  "JSONStream": "^1.3.5",
48
- "mitt": "^3.0.0",
49
- "vite-plugin-replace": "^0.1.1"
52
+ "mitt": "^3.0.0"
50
53
  },
51
54
  "eslintConfig": {
52
55
  "extends": "eslint:recommended",