@iebh/reflib 2.1.4 → 2.2.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/browser.js +21 -3
- package/lib/default.js +19 -2
- package/lib/downloadFile.js +94 -0
- package/lib/uploadFile.js +3 -2
- package/lib/writeFile.js +3 -0
- package/package.json +1 -1
package/lib/browser.js
CHANGED
|
@@ -1,9 +1,27 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {downloadFile} from './downloadFile.js';
|
|
2
2
|
import {formats} from './formats.js';
|
|
3
3
|
import {getModule} from './getModule.js';
|
|
4
|
+
import {identifyFormat} from './identifyFormat.js';
|
|
4
5
|
import {readStream} from './readStream.js';
|
|
5
6
|
import {uploadFile} from './uploadFile.js';
|
|
6
7
|
import {writeStream} from './writeStream.js';
|
|
7
8
|
|
|
8
|
-
export {
|
|
9
|
-
|
|
9
|
+
export {
|
|
10
|
+
downloadFile,
|
|
11
|
+
formats,
|
|
12
|
+
getModule,
|
|
13
|
+
identifyFormat,
|
|
14
|
+
readStream,
|
|
15
|
+
uploadFile,
|
|
16
|
+
writeStream,
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export default {
|
|
20
|
+
downloadFile,
|
|
21
|
+
formats,
|
|
22
|
+
getModule,
|
|
23
|
+
identifyFormat,
|
|
24
|
+
readStream,
|
|
25
|
+
uploadFile,
|
|
26
|
+
writeStream,
|
|
27
|
+
};
|
package/lib/default.js
CHANGED
|
@@ -6,5 +6,22 @@ import {readFile} from './readFile.js';
|
|
|
6
6
|
import {writeStream} from './writeStream.js';
|
|
7
7
|
import {writeFile} from './writeFile.js';
|
|
8
8
|
|
|
9
|
-
export {
|
|
10
|
-
|
|
9
|
+
export {
|
|
10
|
+
formats,
|
|
11
|
+
getModule,
|
|
12
|
+
identifyFormat,
|
|
13
|
+
readFile,
|
|
14
|
+
readStream,
|
|
15
|
+
writeFile,
|
|
16
|
+
writeStream,
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export default {
|
|
20
|
+
formats,
|
|
21
|
+
getModule,
|
|
22
|
+
identifyFormat,
|
|
23
|
+
readFile,
|
|
24
|
+
readStream,
|
|
25
|
+
writeFile,
|
|
26
|
+
writeStream,
|
|
27
|
+
};
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import {identifyFormat} from './identifyFormat.js';
|
|
2
|
+
import {writeStream} from './writeStream.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Prompt the user for a file to save as and Write a given array of citations
|
|
6
|
+
*
|
|
7
|
+
* @param {Array<Ref>} Collection of references to write
|
|
8
|
+
*
|
|
9
|
+
* @param {Object} [options] Additional options when prompting the user
|
|
10
|
+
* @param {String} [options.filename='References.xml'] The filename to download
|
|
11
|
+
* @param {Object} [options.writeStreamOptions] Additional options for the subsequent `writeStream` internal call
|
|
12
|
+
* @param {Object} [options.promptDownload=true] Prompt the user to save the file, if falsy this returns the Blob file content instead of asking the user where to save it
|
|
13
|
+
*
|
|
14
|
+
* @returns {Promise|Promise<Blob>} A promise which will resolve when the file download has completed or (if `!promptDownload`) with the blob contents
|
|
15
|
+
*/
|
|
16
|
+
export function downloadFile(refs, options) {
|
|
17
|
+
let settings = {
|
|
18
|
+
filename: 'References.xml',
|
|
19
|
+
writeStreamOptions: {},
|
|
20
|
+
promptDownload: true,
|
|
21
|
+
...options,
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
return Promise.resolve()
|
|
25
|
+
// Identify module from filename {{{
|
|
26
|
+
.then(()=> {
|
|
27
|
+
let module = identifyFormat(settings.filename);
|
|
28
|
+
if (!module) throw new Error(`Unsupported Reflib filename "${settings.filename}"`);
|
|
29
|
+
return module;
|
|
30
|
+
})
|
|
31
|
+
// }}}
|
|
32
|
+
// Create stream {{{
|
|
33
|
+
.then(async (module) => {
|
|
34
|
+
let blobData = [];
|
|
35
|
+
let writableStream = new WritableStream({
|
|
36
|
+
write(chunk) {
|
|
37
|
+
blobData.push(chunk);
|
|
38
|
+
},
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
let writer = writableStream.getWriter();
|
|
42
|
+
|
|
43
|
+
// Map end->close to keep browser compatibility with Node
|
|
44
|
+
writer.end = async (cb) => {
|
|
45
|
+
await writer.close();
|
|
46
|
+
cb();
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
let streamer = await writeStream(module.id, writer, settings.writeStreamOptions);
|
|
50
|
+
|
|
51
|
+
return {blobData, streamer};
|
|
52
|
+
})
|
|
53
|
+
// }}}
|
|
54
|
+
// Flush all references into the stream {{{
|
|
55
|
+
.then(async ({blobData, streamer}) => {
|
|
56
|
+
// Start stream
|
|
57
|
+
await streamer.start();
|
|
58
|
+
|
|
59
|
+
// Write all references as a promise chain
|
|
60
|
+
await refs.reduce((promiseChain, ref) =>
|
|
61
|
+
promiseChain.then(()=>
|
|
62
|
+
streamer.write(ref)
|
|
63
|
+
)
|
|
64
|
+
, Promise.resolve());
|
|
65
|
+
|
|
66
|
+
// End stream
|
|
67
|
+
await streamer.end();
|
|
68
|
+
|
|
69
|
+
return blobData;
|
|
70
|
+
})
|
|
71
|
+
// }}}
|
|
72
|
+
// Convert blobData array to a Blob {{{
|
|
73
|
+
.then(blobData => {
|
|
74
|
+
return new Blob(blobData);
|
|
75
|
+
})
|
|
76
|
+
// }}}
|
|
77
|
+
// Download Blob
|
|
78
|
+
.then(blob => {
|
|
79
|
+
if (!settings.promptDownload) return blob;
|
|
80
|
+
|
|
81
|
+
let url = URL.createObjectURL(blob);
|
|
82
|
+
let aEl = document.createElement('a');
|
|
83
|
+
aEl.href = url;
|
|
84
|
+
aEl.download = settings.filename;
|
|
85
|
+
document.body.appendChild(aEl);
|
|
86
|
+
aEl.click();
|
|
87
|
+
|
|
88
|
+
// Clean up DOM
|
|
89
|
+
document.body.removeChild(aEl);
|
|
90
|
+
URL.revokeObjectURL(url);
|
|
91
|
+
// }}}
|
|
92
|
+
})
|
|
93
|
+
// }}}
|
|
94
|
+
}
|
package/lib/uploadFile.js
CHANGED
|
@@ -8,7 +8,7 @@ import StreamEmitter from '../shared/streamEmitter.js';
|
|
|
8
8
|
* @param {Object} [options] Additional options when prompting the user
|
|
9
9
|
* @param {File} [options.file] The File object to process, omitting this will prompt the user to select a file
|
|
10
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)` when processing the file
|
|
11
|
+
* @param {function} [options.onProgress] Function called as `(position, totalSize, refCount)` when processing the file
|
|
12
12
|
* @param {function} [options.onEnd] Async function called as `()` when the read stage has completed
|
|
13
13
|
* @param {*} [options.*] Additional settings to pass to `readStream()`
|
|
14
14
|
* @returns {Promise} A promise which will resolve with an array of extracted citations
|
|
@@ -17,6 +17,7 @@ export function uploadFile(options) {
|
|
|
17
17
|
let settings = {...options};
|
|
18
18
|
|
|
19
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 {{{
|
|
20
21
|
return new Promise(resolve => {
|
|
21
22
|
// Create hidden layer we will use to wrap the actual file upload input box
|
|
22
23
|
let fileWrapper = document.createElement('div');
|
|
@@ -40,6 +41,7 @@ export function uploadFile(options) {
|
|
|
40
41
|
fileWrapper.appendChild(uploader);
|
|
41
42
|
uploader.dispatchEvent(new MouseEvent('click'));
|
|
42
43
|
});
|
|
44
|
+
// }}}
|
|
43
45
|
} else { // Read the File object and return an emitter
|
|
44
46
|
if (!(settings.file instanceof File)) throw new Error('Expected "file" setting to uploadFile() to be a File type');
|
|
45
47
|
let identifiedType = identifyFormat(settings.file.name);
|
|
@@ -49,7 +51,6 @@ export function uploadFile(options) {
|
|
|
49
51
|
return Promise.resolve()
|
|
50
52
|
.then(()=> settings.onStart && settings.onStart(settings.file))
|
|
51
53
|
.then(()=> new Promise((resolve, reject) => {
|
|
52
|
-
|
|
53
54
|
let streamer = readStream(
|
|
54
55
|
identifiedType.id,
|
|
55
56
|
StreamEmitter(settings.file.stream()),
|
package/lib/writeFile.js
CHANGED
|
@@ -4,9 +4,12 @@ import {writeStream} from './writeStream.js';
|
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Write a file to disk via the appropriate module
|
|
7
|
+
*
|
|
7
8
|
* @param {string} path The file path to write, the module to use will be determined from this
|
|
9
|
+
* @param {Array<Ref>} Collection of references to write
|
|
8
10
|
* @param {Object} [options] Additional options to pass to the file writer
|
|
9
11
|
* @param {string} [options.module] The module to use if overriding from the file path
|
|
12
|
+
*
|
|
10
13
|
* @returns {Promise} A promise which resolves when the operation has completed
|
|
11
14
|
*/
|
|
12
15
|
export function writeFile(path, refs, options) {
|