document-drive 0.0.16 → 0.0.18
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/package.json +2 -2
- package/src/storage/filesystem.ts +5 -35
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "document-drive",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.18",
|
|
4
4
|
"license": "AGPL-3.0-only",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "./src/index.ts",
|
|
@@ -30,7 +30,6 @@
|
|
|
30
30
|
"localforage": "^1.10.0"
|
|
31
31
|
},
|
|
32
32
|
"dependencies": {
|
|
33
|
-
"proper-lockfile": "^4.1.2",
|
|
34
33
|
"sanitize-filename": "^1.6.3"
|
|
35
34
|
},
|
|
36
35
|
"devDependencies": {
|
|
@@ -46,6 +45,7 @@
|
|
|
46
45
|
"localforage": "^1.10.0",
|
|
47
46
|
"prettier": "^3.1.0",
|
|
48
47
|
"prettier-plugin-organize-imports": "^3.2.4",
|
|
48
|
+
"tsx": "^4.6.2",
|
|
49
49
|
"typescript": "^5.3.2",
|
|
50
50
|
"vitest": "^0.34.6"
|
|
51
51
|
}
|
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import { DocumentDriveDocument } from 'document-model-libs/document-drive';
|
|
2
2
|
import { Document } from 'document-model/document';
|
|
3
3
|
import type { Dirent } from 'fs';
|
|
4
|
-
import { existsSync, mkdirSync } from 'fs';
|
|
4
|
+
import { existsSync, mkdirSync, writeFileSync } from 'fs';
|
|
5
5
|
import fs from 'fs/promises';
|
|
6
6
|
import path from 'path';
|
|
7
|
-
import { check, lock } from 'proper-lockfile';
|
|
8
7
|
import sanitize from 'sanitize-filename';
|
|
9
8
|
import { isDocumentDrive } from '../utils';
|
|
10
9
|
import { IDriveStorage } from './types';
|
|
@@ -22,28 +21,6 @@ function ensureDir(dir: string) {
|
|
|
22
21
|
}
|
|
23
22
|
}
|
|
24
23
|
|
|
25
|
-
async function lockFile(path: string, retries = 10) {
|
|
26
|
-
// eslint-disable-next-line no-constant-condition
|
|
27
|
-
if (retries > 0) {
|
|
28
|
-
try {
|
|
29
|
-
const locked = await check(path);
|
|
30
|
-
if (locked) {
|
|
31
|
-
throw new Error('File is locked');
|
|
32
|
-
}
|
|
33
|
-
} catch (error) {
|
|
34
|
-
await new Promise(resolve => setTimeout(resolve, 1000)); // Wait for 1 second before checking again
|
|
35
|
-
return lockFile(path, retries - 1);
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
return lock(path, {
|
|
40
|
-
fs,
|
|
41
|
-
retries: 3,
|
|
42
|
-
realpath: false,
|
|
43
|
-
onCompromised: () => {}
|
|
44
|
-
});
|
|
45
|
-
}
|
|
46
|
-
|
|
47
24
|
export class FilesystemStorage implements IDriveStorage {
|
|
48
25
|
private basePath: string;
|
|
49
26
|
private drivesPath: string;
|
|
@@ -108,17 +85,10 @@ export class FilesystemStorage implements IDriveStorage {
|
|
|
108
85
|
|
|
109
86
|
async saveDocument(drive: string, id: string, document: Document) {
|
|
110
87
|
const documentPath = this._buildDocumentPath(drive, id);
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
:
|
|
114
|
-
|
|
115
|
-
await ensureDir(path.dirname(documentPath));
|
|
116
|
-
return fs.writeFile(documentPath, JSON.stringify(document), {
|
|
117
|
-
encoding: 'utf-8'
|
|
118
|
-
});
|
|
119
|
-
} finally {
|
|
120
|
-
releaseLock?.();
|
|
121
|
-
}
|
|
88
|
+
await ensureDir(path.dirname(documentPath));
|
|
89
|
+
await writeFileSync(documentPath, JSON.stringify(document), {
|
|
90
|
+
encoding: 'utf-8'
|
|
91
|
+
});
|
|
122
92
|
}
|
|
123
93
|
|
|
124
94
|
async deleteDocument(drive: string, id: string) {
|