document-drive 0.0.16 → 0.0.17

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "document-drive",
3
- "version": "0.0.16",
3
+ "version": "0.0.17",
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
  }
@@ -4,7 +4,6 @@ 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 { 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,16 +85,15 @@ 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
- const releaseLock = existsSync(documentPath)
112
- ? await lockFile(documentPath)
113
- : undefined;
88
+ let handle: fs.FileHandle | undefined = undefined;
114
89
  try {
115
90
  await ensureDir(path.dirname(documentPath));
116
- return fs.writeFile(documentPath, JSON.stringify(document), {
91
+ handle = await fs.open(documentPath, 'w');
92
+ await fs.writeFile(handle, JSON.stringify(document), {
117
93
  encoding: 'utf-8'
118
94
  });
119
95
  } finally {
120
- releaseLock?.();
96
+ await handle?.close();
121
97
  }
122
98
  }
123
99