@ocfl/ocfl-fs 0.3.0 → 1.0.0-beta.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/utils.js DELETED
@@ -1,62 +0,0 @@
1
- const fs = require('node:fs/promises');
2
- const path = require('node:path');
3
-
4
- /**
5
- * Wrapper for fs.opendir that reimplements the recursion and
6
- * returns just the file without the directory itself
7
- * fs.opendir has a bug that gives wrong result when recursive option is used
8
- * @param {string} dirPath
9
- * @param {OpenDirOptions} [options]
10
- */
11
- async function* opendir(dirPath, { encoding = 'utf8', bufferSize = 32, recursive = false } = {}) {
12
- /** @type Promise<{ name: string, path: string, size: number, lastModified: Date}>[] */
13
- let buffer = [];
14
- let dirp;
15
- const maxBuffer = bufferSize || 1;
16
- const dirQueue = [fs.opendir(dirPath, { encoding, bufferSize })];
17
- while (dirp = dirQueue.shift()) {
18
- const dir = await dirp;
19
- for await (const de of dir) {
20
- const cdp = path.join(dir.path, de.name);
21
- if (recursive && de.isDirectory()) {
22
- dirQueue.push(fs.opendir(cdp));
23
- } else {
24
- let f = fs.stat(cdp).then(stats => ({
25
- name: de.name,
26
- path: path.relative(dirPath, cdp),
27
- size: stats.size,
28
- lastModified: stats.mtime
29
- }));
30
- if (buffer.length < maxBuffer) {
31
- buffer.push(f);
32
- } else {
33
- yield buffer.shift();
34
- buffer.push(f);
35
- }
36
- }
37
- }
38
- }
39
- let file;
40
- while (file = buffer.shift()) {
41
- yield file;
42
- }
43
- }
44
-
45
- // (async function() {
46
- // var a = opendir('/home/alvinsw/.ssh', {recursive: true})
47
- // var b = 'a';
48
- // console.log(await a);
49
-
50
- // let c = 0;
51
- // for await (const f of await opendir('/home/alvinsw/uqlang', {recursive: true})) {
52
- // c++;
53
- // }
54
- // console.log(c);
55
- // c=0;
56
- // for await (const f of await fs.opendir('/home/alvinsw/uqlang', {recursive: true})) {
57
- // c++;
58
- // }
59
- // console.log(c);
60
- // })();
61
-
62
- module.exports = { opendir }