@ocfl/ocfl-fs 0.2.0 → 0.2.2

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/store.js CHANGED
@@ -1,9 +1,10 @@
1
1
  //@ts-check
2
2
  const path = require('path');
3
- const fs = require('fs');
4
- const { mkdir, copyFile, rename, unlink } = fs.promises;
3
+ const fs = require('node:fs');
4
+ const { Readable, Writable } = require('node:stream');
5
5
 
6
6
  const { OcflStore, OcflConstants } = require('@ocfl/ocfl');
7
+ const { opendir } = require('./utils.js');
7
8
  const { INVENTORY_NAME } = OcflConstants;
8
9
 
9
10
  /**
@@ -37,8 +38,8 @@ class OcflFsStore extends OcflStore {
37
38
  this.fs = config?.fs ?? fs;
38
39
  }
39
40
 
40
- async stat(filePath) {
41
- return this.fs.promises.stat(filePath);
41
+ async stat(filePath) {
42
+ return this.fs.promises.stat(filePath);
42
43
  }
43
44
 
44
45
  async createReadStream(filePath, options) {
@@ -46,11 +47,20 @@ class OcflFsStore extends OcflStore {
46
47
  return this.fs.createReadStream(filePath, options);
47
48
  }
48
49
 
49
- async createWriteStream(filePath, options) {
50
+ async createWriteStream(filePath, options) {
50
51
  await this.fs.promises.mkdir(path.dirname(filePath), { recursive: true });
51
52
  return this.fs.createWriteStream(filePath, options);
52
53
  }
53
54
 
55
+ async createReadable(filePath, options) {
56
+ return Readable.toWeb(this.fs.createReadStream(filePath, options));
57
+ }
58
+
59
+ async createWritable(filePath, options) {
60
+ await this.fs.promises.mkdir(path.dirname(filePath), { recursive: true });
61
+ return Writable.toWeb(this.fs.createWriteStream(filePath, options));
62
+ }
63
+
54
64
  async readFile(filePath, options) {
55
65
  return this.fs.promises.readFile(filePath, options);
56
66
  //if (err.code !== 'ENOENT') throw validation.createError(34, invPath);
@@ -67,11 +77,54 @@ class OcflFsStore extends OcflStore {
67
77
  }
68
78
 
69
79
  async opendir(filePath, options) {
70
- return this.fs.promises.opendir(filePath, options);
80
+ return this.fs.promises.opendir(filePath, options);
71
81
  }
72
82
 
73
83
  async readdir(filePath, options) {
74
- return this.fs.promises.readdir(filePath, options);
84
+ return this.fs.promises.readdir(filePath, options);
85
+ }
86
+
87
+ /**
88
+ * @param {string} dirPath
89
+ * @param {OpenDirOptions} [options]
90
+ * @return {Promise<AsyncIterableIterator<{name: string, path: string, size: number, lastModified: Date}>> }
91
+ */
92
+ async list(dirPath, { encoding = 'utf8', bufferSize = 32, recursive = false } = {}) {
93
+ const {opendir, stat} = this.fs.promises;
94
+ const maxBuffer = bufferSize || 1;
95
+ async function* generator() {
96
+ /** @type Promise<{ name: string, path: string, size: number, lastModified: Date}>[] */
97
+ let buffer = [];
98
+ let dirp;
99
+ const dirQueue = [opendir(dirPath, { encoding, bufferSize })];
100
+ while (dirp = dirQueue.shift()) {
101
+ const dir = await dirp;
102
+ for await (const de of dir) {
103
+ const cdp = path.join(dir.path, de.name);
104
+ if (recursive && de.isDirectory()) {
105
+ dirQueue.push(opendir(cdp, { encoding, bufferSize }));
106
+ } else {
107
+ let f = stat(cdp).then(stats => ({
108
+ name: de.name,
109
+ path: path.relative(dirPath, cdp),
110
+ size: stats.size,
111
+ lastModified: stats.mtime
112
+ }));
113
+ if (buffer.length < maxBuffer) {
114
+ buffer.push(f);
115
+ } else {
116
+ yield buffer.shift();
117
+ buffer.push(f);
118
+ }
119
+ }
120
+ }
121
+ }
122
+ let file;
123
+ while (file = buffer.shift()) {
124
+ yield file;
125
+ }
126
+ }
127
+ return generator();
75
128
  }
76
129
 
77
130
  async mkdir(filePath, options = { recursive: true }) {
package/lib/types.d.ts CHANGED
@@ -1,3 +1,5 @@
1
- type TestType = {
2
- name: string;
3
- };
1
+ interface OpenDirOptions {
2
+ encoding?: BufferEncoding | undefined;
3
+ bufferSize?: number | undefined;
4
+ recursive?: boolean | undefined;
5
+ }
package/lib/utils.js CHANGED
@@ -0,0 +1,62 @@
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 }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ocfl/ocfl-fs",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
4
4
  "description": "Oxford Common File Layout (OCFL) JS client library for filesystem storage",
5
5
  "main": "index.js",
6
6
  "exports": {
@@ -37,14 +37,15 @@
37
37
  "license": "GPL-3.0-or-later",
38
38
  "devDependencies": {
39
39
  "@types/fs-extra": "^9.0.13",
40
- "clean-jsdoc-theme": "^3.3.4",
41
- "jsdoc": "^3.6.10",
42
- "mocha": "^10.0.0",
40
+ "@types/node": "^18",
41
+ "clean-jsdoc-theme": "^4.3.0",
42
+ "jsdoc": "^4.0.3",
43
+ "mocha": "^10.7.3",
43
44
  "typedoc": "^0.22.15",
44
45
  "@ocfl/ocfl-tests": "*"
45
46
  },
46
47
  "dependencies": {
47
- "@ocfl/ocfl": "0.2.0"
48
+ "@ocfl/ocfl": "0.2.2"
48
49
  },
49
50
  "files": [
50
51
  "index.js",