@iebh/reflib 2.5.5 → 2.5.7

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,85 +1,85 @@
1
- import * as EndNoteEnl from './endnoteEnl.js';
2
- import Emitter from '../shared/emitter.js';
3
- import {BlobReader as ZipBlobReader, ZipReader, Writer} from '@zip.js/zip.js';
4
-
5
- /**
6
- * Utility class to read a Zip file into a Uint8Array
7
- * This extends the deafult @zip.js/zip.js Writer class so its works without a crowbar
8
- */
9
- class BinaryStringWriter extends Writer {
10
- chunks = [];
11
-
12
- writeUint8Array(chunk) {
13
- this.chunks.push(chunk);
14
- }
15
-
16
- getData() {
17
- let buf = new Uint8Array(
18
- this.chunks.reduce((total, chunk) => total + chunk.length, 0)
19
- );
20
-
21
- let position = 0;
22
- this.chunks.forEach(chunk => {
23
- buf.set(chunk, position);
24
- position += chunk.length;
25
- });
26
-
27
- // Flush buffer to free up memory
28
- this.chunks = [];
29
-
30
- return buf;
31
- }
32
- }
33
-
34
-
35
- /**
36
- * Read an EndNote(@11+) / Zip+SQLite database EnlX returning an Emitter analogue
37
- *
38
- * Since EnlX files are really just a Zip file containing the SQLite database we're actually intrestesd in, most of this module
39
- * is handling the Zip container and handing the extracted buffer to the regular modules/endnoteEnl.js driver
40
- *
41
- * @see modules/inhterface.js
42
- *
43
- * @param {Stream} stream Stream primative to encapsulate
44
- *
45
- * @returns {Object} An Emitter analogue defined in `../shared/Emitter.js`
46
- */
47
- export function readStream(stream) {
48
- let emitter = Emitter();
49
-
50
- // Queue up the parser in the next tick (so we can return the emitter first)
51
- setTimeout(()=> {
52
- let chunks = []; // Gathered buffer chunks, used to make an arrayBuffer later
53
-
54
- stream
55
- .on('data', chunk => chunks.push(chunk))
56
- .on('error', e => emitter.emit('error', e))
57
- .on('end', ()=> {
58
- Promise.resolve()
59
- .then(()=> { // Parse chunks into a Blob
60
- let blob = new Blob(chunks);
61
-
62
- // Release chunks to free up memory
63
- chunks = [];
64
- return blob;
65
- })
66
- .then(blob => new ZipReader( // Create zipReader
67
- new ZipBlobReader(blob),
68
- ))
69
- .then(zip => zip.getEntries()) // Fetch files
70
- .then(files => files.find(f => f.filename == 'sdb/sdb.eni')) // Find the file we want
71
- .then(sdb => { // Extract that files stream
72
- let writer = new BinaryStringWriter();
73
- return sdb.getData(writer);
74
- })
75
- .then(buf => EndNoteEnl.readBuffer(buf, {
76
- onRef(ref) {
77
- emitter.emit('ref', ref);
78
- },
79
- }))
80
- .finally(()=> emitter.emit('end'))
81
- })
82
- });
83
-
84
- return emitter;
85
- }
1
+ import * as EndNoteEnl from './endnoteEnl.js';
2
+ import Emitter from '../shared/emitter.js';
3
+ import {BlobReader as ZipBlobReader, ZipReader, Writer} from '@zip.js/zip.js';
4
+
5
+ /**
6
+ * Utility class to read a Zip file into a Uint8Array
7
+ * This extends the deafult @zip.js/zip.js Writer class so its works without a crowbar
8
+ */
9
+ class BinaryStringWriter extends Writer {
10
+ chunks = [];
11
+
12
+ writeUint8Array(chunk) {
13
+ this.chunks.push(chunk);
14
+ }
15
+
16
+ getData() {
17
+ let buf = new Uint8Array(
18
+ this.chunks.reduce((total, chunk) => total + chunk.length, 0)
19
+ );
20
+
21
+ let position = 0;
22
+ this.chunks.forEach(chunk => {
23
+ buf.set(chunk, position);
24
+ position += chunk.length;
25
+ });
26
+
27
+ // Flush buffer to free up memory
28
+ this.chunks = [];
29
+
30
+ return buf;
31
+ }
32
+ }
33
+
34
+
35
+ /**
36
+ * Read an EndNote(@11+) / Zip+SQLite database EnlX returning an Emitter analogue
37
+ *
38
+ * Since EnlX files are really just a Zip file containing the SQLite database we're actually intrestesd in, most of this module
39
+ * is handling the Zip container and handing the extracted buffer to the regular modules/endnoteEnl.js driver
40
+ *
41
+ * @see modules/inhterface.js
42
+ *
43
+ * @param {Stream} stream Stream primative to encapsulate
44
+ *
45
+ * @returns {Object} An Emitter analogue defined in `../shared/Emitter.js`
46
+ */
47
+ export function readStream(stream) {
48
+ let emitter = Emitter();
49
+
50
+ // Queue up the parser in the next tick (so we can return the emitter first)
51
+ setTimeout(()=> {
52
+ let chunks = []; // Gathered buffer chunks, used to make an arrayBuffer later
53
+
54
+ stream
55
+ .on('data', chunk => chunks.push(chunk))
56
+ .on('error', e => emitter.emit('error', e))
57
+ .on('end', ()=> {
58
+ Promise.resolve()
59
+ .then(()=> { // Parse chunks into a Blob
60
+ let blob = new Blob(chunks);
61
+
62
+ // Release chunks to free up memory
63
+ chunks = [];
64
+ return blob;
65
+ })
66
+ .then(blob => new ZipReader( // Create zipReader
67
+ new ZipBlobReader(blob),
68
+ ))
69
+ .then(zip => zip.getEntries()) // Fetch files
70
+ .then(files => files.find(f => f.filename == 'sdb/sdb.eni')) // Find the file we want
71
+ .then(sdb => { // Extract that files stream
72
+ let writer = new BinaryStringWriter();
73
+ return sdb.getData(writer);
74
+ })
75
+ .then(buf => EndNoteEnl.readBuffer(buf, {
76
+ onRef(ref) {
77
+ emitter.emit('ref', ref);
78
+ },
79
+ }))
80
+ .finally(()=> emitter.emit('end'))
81
+ })
82
+ });
83
+
84
+ return emitter;
85
+ }