@iebh/reflib 2.7.2 → 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.
package/lib/readFile.js CHANGED
@@ -1,63 +1,63 @@
1
- import {createReadStream} from 'node:fs';
2
- import Emitter from '../shared/emitter.js';
3
- import {stat} from 'node:fs/promises';
4
- import {identifyFormat} from './identifyFormat.js';
5
- import {readStream} from './readStream.js';
6
-
7
- /**
8
- * Parse a file directly from a path
9
- * This function is a warpper around the readStream handler + some Promise magic
10
- * @param {string} path The file path to parse
11
- * @param {Object} [options] Additional options to pass to the parser
12
- * @param {string} [options.module] The module to use if overriding from the file path
13
- *
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
- * @emits progress Emitted as `({readBytes: Number, totalSize: Number, refsFound: Number})`
18
- * @emits end Emitted as `({refsFound: Number})` when the reading operation has completed
19
- */
20
- export function readFile(path, options) {
21
- let settings = {
22
- progressTotal: false,
23
- ...options,
24
- };
25
- let module = options?.module || identifyFormat(path)?.id;
26
- if (!module) throw new Error(`Unable to identify reference library format for file path "${path}"`);
27
-
28
- let promiseEmitter = Promise.resolve()
29
- .then(()=> stat(path))
30
- .then(stats => new Promise((resolve, reject) => {
31
- let refs = [];
32
-
33
- readStream(
34
- module,
35
- createReadStream(path),
36
- {
37
- ...settings,
38
- size: stats.size,
39
- },
40
- )
41
- .on('end', ()=> resolve(refs))
42
- .on('error', reject)
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
- }))
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
- });
63
- }
1
+ import {createReadStream} from 'node:fs';
2
+ import Emitter from '../shared/emitter.js';
3
+ import {stat} from 'node:fs/promises';
4
+ import {identifyFormat} from './identifyFormat.js';
5
+ import {readStream} from './readStream.js';
6
+
7
+ /**
8
+ * Parse a file directly from a path
9
+ * This function is a warpper around the readStream handler + some Promise magic
10
+ * @param {string} path The file path to parse
11
+ * @param {Object} [options] Additional options to pass to the parser
12
+ * @param {string} [options.module] The module to use if overriding from the file path
13
+ *
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
+ * @emits progress Emitted as `({readBytes: Number, totalSize: Number, refsFound: Number})`
18
+ * @emits end Emitted as `({refsFound: Number})` when the reading operation has completed
19
+ */
20
+ export function readFile(path, options) {
21
+ let settings = {
22
+ progressTotal: false,
23
+ ...options,
24
+ };
25
+ let module = options?.module || identifyFormat(path)?.id;
26
+ if (!module) throw new Error(`Unable to identify reference library format for file path "${path}"`);
27
+
28
+ let promiseEmitter = Promise.resolve()
29
+ .then(()=> stat(path))
30
+ .then(stats => new Promise((resolve, reject) => {
31
+ let refs = [];
32
+
33
+ readStream(
34
+ module,
35
+ createReadStream(path),
36
+ {
37
+ ...settings,
38
+ size: stats.size,
39
+ },
40
+ )
41
+ .on('end', ()=> resolve(refs))
42
+ .on('error', reject)
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
+ }))
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
+ });
63
+ }
package/lib/readStream.js CHANGED
@@ -1,21 +1,21 @@
1
- import {getModule} from './getModule.js';
2
-
3
- /**
4
- * Parse an input stream via a given format ID
5
- * This function is really just a multiplexor around each modules `readStream` export
6
- * @param {string} module The module ID as per `lib/formats.js`
7
- * @param {Stream.Readable} stream Input stream to parse
8
- * @param {Object} [options] Additional options to pass to the parser
9
- * @param {number} [options.size] Size of the input stream, if omitted `progress` events are not emitted
10
- * @returns {EventEmitter} An Event-Emitter compatible object which will fire various events while parsing
11
- *
12
- * @emits ref Emitted with an extracted reference object during parse
13
- * @emits end Emitted when the parsing has completed
14
- * @emits error Emitted with an Error object if any occured
15
- */
16
- export function readStream(module, stream, options) {
17
- if (!module) throw new Error('No module provided to parse with');
18
- if (!stream) throw new Error('No stream provided to parse');
19
-
20
- return getModule(module).readStream(stream, options);
21
- }
1
+ import {getModule} from './getModule.js';
2
+
3
+ /**
4
+ * Parse an input stream via a given format ID
5
+ * This function is really just a multiplexor around each modules `readStream` export
6
+ * @param {string} module The module ID as per `lib/formats.js`
7
+ * @param {Stream.Readable} stream Input stream to parse
8
+ * @param {Object} [options] Additional options to pass to the parser
9
+ * @param {number} [options.size] Size of the input stream, if omitted `progress` events are not emitted
10
+ * @returns {EventEmitter} An Event-Emitter compatible object which will fire various events while parsing
11
+ *
12
+ * @emits ref Emitted with an extracted reference object during parse
13
+ * @emits end Emitted when the parsing has completed
14
+ * @emits error Emitted with an Error object if any occured
15
+ */
16
+ export function readStream(module, stream, options) {
17
+ if (!module) throw new Error('No module provided to parse with');
18
+ if (!stream) throw new Error('No stream provided to parse');
19
+
20
+ return getModule(module).readStream(stream, options);
21
+ }
package/lib/uploadFile.js CHANGED
@@ -1,71 +1,71 @@
1
- import {formats} from './formats.js';
2
- import {identifyFormat} from './identifyFormat.js';
3
- import {readStream} from './readStream.js';
4
- import StreamEmitter from '../shared/streamEmitter.js';
5
-
6
- /**
7
- * Prompt the user for a file then read it as a Reflib event emitter
8
- * @param {Object} [options] Additional options when prompting the user
9
- * @param {File} [options.file] The File object to process, omitting this will prompt the user to select a file
10
- * @param {function} [options.onStart] Async function called as `(File)` when starting the read stage
11
- * @param {function} [options.onProgress] Function called as `(position, totalSize, refCount)` when processing the file
12
- * @param {function} [options.onEnd] Async function called as `()` when the read stage has completed
13
- * @param {...*} [options...] Additional settings to pass to `readStream()`
14
- * @returns {Promise} A promise which will resolve with an array of extracted citations
15
- */
16
- export function uploadFile(options) {
17
- let settings = {...options};
18
-
19
- if (!settings.file) { // No file provided - prompt the user via the DOM
20
- // Horrible kludge to create an upload element in the DOM + interact with it {{{
21
- return new Promise(resolve => {
22
- // Create hidden layer we will use to wrap the actual file upload input box
23
- let fileWrapper = document.createElement('div');
24
- fileWrapper.style.display = 'none';
25
- document.body.appendChild(fileWrapper);
26
-
27
- // Create upload input
28
- let uploader = document.createElement('input');
29
- uploader.type = 'file';
30
- uploader.accept = Object.values(formats) // Allow only uploading supported file extensions
31
- .flatMap(f => f.ext)
32
- .join(',');
33
-
34
- uploader.addEventListener('change', e => {
35
- document.body.removeChild(fileWrapper);
36
- resolve(uploadFile({
37
- file: e.target.files[0],
38
- ...options,
39
- }));
40
- });
41
- fileWrapper.appendChild(uploader);
42
- uploader.dispatchEvent(new MouseEvent('click'));
43
- });
44
- // }}}
45
- } else { // Read the File object and return an emitter
46
- if (!(settings.file instanceof File)) throw new Error('Expected "file" setting to uploadFile() to be a File type');
47
- let identifiedType = identifyFormat(settings.file.name);
48
- if (!identifiedType) throw new Error(`Unidenfified file format from filename "${settings.file.name}"`);
49
-
50
- let refs = [];
51
- return Promise.resolve()
52
- .then(()=> settings.onStart && settings.onStart(settings.file))
53
- .then(()=> new Promise((resolve, reject) => {
54
- let streamer = readStream(
55
- identifiedType.id,
56
- StreamEmitter(settings.file.stream()),
57
- {
58
- ...settings,
59
- size: settings.file.size,
60
- },
61
- )
62
- .on('end', ()=> resolve(refs))
63
- .on('error', e => reject(e))
64
- .on('ref', ref => refs.push(ref))
65
-
66
- if (settings.onProgress) streamer.on('progress', settings.onProgress);
67
- }))
68
- .then(()=> settings.onEnd && settings.onEnd())
69
- .then(()=> refs)
70
- }
71
- }
1
+ import {formats} from './formats.js';
2
+ import {identifyFormat} from './identifyFormat.js';
3
+ import {readStream} from './readStream.js';
4
+ import StreamEmitter from '../shared/streamEmitter.js';
5
+
6
+ /**
7
+ * Prompt the user for a file then read it as a Reflib event emitter
8
+ * @param {Object} [options] Additional options when prompting the user
9
+ * @param {File} [options.file] The File object to process, omitting this will prompt the user to select a file
10
+ * @param {function} [options.onStart] Async function called as `(File)` when starting the read stage
11
+ * @param {function} [options.onProgress] Function called as `(position, totalSize, refCount)` when processing the file
12
+ * @param {function} [options.onEnd] Async function called as `()` when the read stage has completed
13
+ * @param {...*} [options...] Additional settings to pass to `readStream()`
14
+ * @returns {Promise} A promise which will resolve with an array of extracted citations
15
+ */
16
+ export function uploadFile(options) {
17
+ let settings = {...options};
18
+
19
+ if (!settings.file) { // No file provided - prompt the user via the DOM
20
+ // Horrible kludge to create an upload element in the DOM + interact with it {{{
21
+ return new Promise(resolve => {
22
+ // Create hidden layer we will use to wrap the actual file upload input box
23
+ let fileWrapper = document.createElement('div');
24
+ fileWrapper.style.display = 'none';
25
+ document.body.appendChild(fileWrapper);
26
+
27
+ // Create upload input
28
+ let uploader = document.createElement('input');
29
+ uploader.type = 'file';
30
+ uploader.accept = Object.values(formats) // Allow only uploading supported file extensions
31
+ .flatMap(f => f.ext)
32
+ .join(',');
33
+
34
+ uploader.addEventListener('change', e => {
35
+ document.body.removeChild(fileWrapper);
36
+ resolve(uploadFile({
37
+ file: e.target.files[0],
38
+ ...options,
39
+ }));
40
+ });
41
+ fileWrapper.appendChild(uploader);
42
+ uploader.dispatchEvent(new MouseEvent('click'));
43
+ });
44
+ // }}}
45
+ } else { // Read the File object and return an emitter
46
+ if (!(settings.file instanceof File)) throw new Error('Expected "file" setting to uploadFile() to be a File type');
47
+ let identifiedType = identifyFormat(settings.file.name);
48
+ if (!identifiedType) throw new Error(`Unidenfified file format from filename "${settings.file.name}"`);
49
+
50
+ let refs = [];
51
+ return Promise.resolve()
52
+ .then(()=> settings.onStart && settings.onStart(settings.file))
53
+ .then(()=> new Promise((resolve, reject) => {
54
+ let streamer = readStream(
55
+ identifiedType.id,
56
+ StreamEmitter(settings.file.stream()),
57
+ {
58
+ ...settings,
59
+ size: settings.file.size,
60
+ },
61
+ )
62
+ .on('end', ()=> resolve(refs))
63
+ .on('error', e => reject(e))
64
+ .on('ref', ref => refs.push(ref))
65
+
66
+ if (settings.onProgress) streamer.on('progress', settings.onProgress);
67
+ }))
68
+ .then(()=> settings.onEnd && settings.onEnd())
69
+ .then(()=> refs)
70
+ }
71
+ }
package/lib/writeFile.js CHANGED
@@ -1,32 +1,32 @@
1
- import {createWriteStream} from 'node:fs';
2
- import {finished as streamFinished} from 'node:stream/promises';
3
- import {identifyFormat} from './identifyFormat.js';
4
- import {writeStream} from './writeStream.js';
5
-
6
- /**
7
- * Write a file to disk via the appropriate module
8
- *
9
- * @param {string} path The file path to write, the module to use will be determined from this
10
- * @param {Array<ReflibRef>} refs Collection of references to write
11
- * @param {Object} [options] Additional options to pass to the file writer
12
- * @param {string} [options.module] The module to use if overriding from the file path
13
- *
14
- * @returns {Promise} A promise which resolves when the operation has completed
15
- */
16
- export function writeFile(path, refs, options) {
17
- let format = options?.module || identifyFormat(path)?.id;
18
- if (!format) throw new Error(`Unable to identify reference library format when saving file "${path}"`);
19
-
20
- let fileStream = createWriteStream(path);
21
- let writer = writeStream(format, fileStream, options);
22
-
23
- return Promise.resolve()
24
- .then(()=> writer.start())
25
- .then(()=> refs.reduce((chain, ref, refIndex, refs) => chain // Write all refs as a series of promises
26
- .then(()=> writer.write(ref))
27
- .then(()=> refIndex < refs.length && writer.middle && writer.middle(ref))
28
- , Promise.resolve()))
29
- .then(()=> writer.end())
30
- .then(()=> fileStream.close())
31
- .then(()=> streamFinished(fileStream))
32
- }
1
+ import {createWriteStream} from 'node:fs';
2
+ import {finished as streamFinished} from 'node:stream/promises';
3
+ import {identifyFormat} from './identifyFormat.js';
4
+ import {writeStream} from './writeStream.js';
5
+
6
+ /**
7
+ * Write a file to disk via the appropriate module
8
+ *
9
+ * @param {string} path The file path to write, the module to use will be determined from this
10
+ * @param {Array<ReflibRef>} refs Collection of references to write
11
+ * @param {Object} [options] Additional options to pass to the file writer
12
+ * @param {string} [options.module] The module to use if overriding from the file path
13
+ *
14
+ * @returns {Promise} A promise which resolves when the operation has completed
15
+ */
16
+ export function writeFile(path, refs, options) {
17
+ let format = options?.module || identifyFormat(path)?.id;
18
+ if (!format) throw new Error(`Unable to identify reference library format when saving file "${path}"`);
19
+
20
+ let fileStream = createWriteStream(path);
21
+ let writer = writeStream(format, fileStream, options);
22
+
23
+ return Promise.resolve()
24
+ .then(()=> writer.start())
25
+ .then(()=> refs.reduce((chain, ref, refIndex, refs) => chain // Write all refs as a series of promises
26
+ .then(()=> writer.write(ref))
27
+ .then(()=> refIndex < refs.length && writer.middle && writer.middle(ref))
28
+ , Promise.resolve()))
29
+ .then(()=> writer.end())
30
+ .then(()=> fileStream.close())
31
+ .then(()=> streamFinished(fileStream))
32
+ }
@@ -1,16 +1,16 @@
1
- import {getModule} from './getModule.js';
2
-
3
- /**
4
- * Write an output stream via a given format ID
5
- * This function is really just a multiplexor around each modules `writeStream` export
6
- * @param {string} module The module ID as per `lib/formats.js`
7
- * @param {Stream.Writable} stream Output stream to write to
8
- * @param {Object} [options] Additional options to pass to the stream writer
9
- * @returns {Object} An object composed of `{start, write, end}` functions
10
- */
11
- export function writeStream(module, stream, options) {
12
- if (!module) throw new Error('No module provided to parse with');
13
- if (!stream) throw new Error('No stream provided to parse');
14
-
15
- return getModule(module).writeStream(stream, options);
16
- }
1
+ import {getModule} from './getModule.js';
2
+
3
+ /**
4
+ * Write an output stream via a given format ID
5
+ * This function is really just a multiplexor around each modules `writeStream` export
6
+ * @param {string} module The module ID as per `lib/formats.js`
7
+ * @param {Stream.Writable} stream Output stream to write to
8
+ * @param {Object} [options] Additional options to pass to the stream writer
9
+ * @returns {Object} An object composed of `{start, write, end}` functions
10
+ */
11
+ export function writeStream(module, stream, options) {
12
+ if (!module) throw new Error('No module provided to parse with');
13
+ if (!stream) throw new Error('No stream provided to parse');
14
+
15
+ return getModule(module).writeStream(stream, options);
16
+ }