document-drive 0.0.14 → 0.0.16
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 +1 -1
- package/src/storage/filesystem.ts +27 -9
package/package.json
CHANGED
|
@@ -4,17 +4,11 @@ import type { Dirent } from 'fs';
|
|
|
4
4
|
import { existsSync, mkdirSync } from 'fs';
|
|
5
5
|
import fs from 'fs/promises';
|
|
6
6
|
import path from 'path';
|
|
7
|
-
import { lock } from 'proper-lockfile';
|
|
7
|
+
import { check, lock } from 'proper-lockfile';
|
|
8
8
|
import sanitize from 'sanitize-filename';
|
|
9
9
|
import { isDocumentDrive } from '../utils';
|
|
10
10
|
import { IDriveStorage } from './types';
|
|
11
11
|
|
|
12
|
-
function ensureDir(dir: string) {
|
|
13
|
-
if (!existsSync(dir)) {
|
|
14
|
-
mkdirSync(dir, { recursive: true });
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
|
|
18
12
|
type FSError = {
|
|
19
13
|
errno: number;
|
|
20
14
|
code: string;
|
|
@@ -22,8 +16,32 @@ type FSError = {
|
|
|
22
16
|
path: string;
|
|
23
17
|
};
|
|
24
18
|
|
|
25
|
-
function
|
|
26
|
-
|
|
19
|
+
function ensureDir(dir: string) {
|
|
20
|
+
if (!existsSync(dir)) {
|
|
21
|
+
mkdirSync(dir, { recursive: true });
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
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
|
+
});
|
|
27
45
|
}
|
|
28
46
|
|
|
29
47
|
export class FilesystemStorage implements IDriveStorage {
|