document-drive 0.0.11 → 0.0.13

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.11",
3
+ "version": "0.0.13",
4
4
  "license": "AGPL-3.0-only",
5
5
  "type": "module",
6
6
  "module": "./src/index.ts",
@@ -30,9 +30,11 @@
30
30
  "localforage": "^1.10.0"
31
31
  },
32
32
  "dependencies": {
33
+ "proper-lockfile": "^4.1.2",
33
34
  "sanitize-filename": "^1.6.3"
34
35
  },
35
36
  "devDependencies": {
37
+ "@types/proper-lockfile": "^4.1.4",
36
38
  "@typescript-eslint/eslint-plugin": "^6.12.0",
37
39
  "@typescript-eslint/parser": "^6.12.0",
38
40
  "@vitest/coverage-v8": "^0.34.6",
@@ -3,8 +3,8 @@ import { Document } from 'document-model/document';
3
3
  import type { Dirent } from 'fs';
4
4
  import { existsSync, mkdirSync } from 'fs';
5
5
  import fs from 'fs/promises';
6
-
7
6
  import path from 'path';
7
+ import { lock } from 'proper-lockfile';
8
8
  import sanitize from 'sanitize-filename';
9
9
  import { isDocumentDrive } from '../utils';
10
10
  import { IDriveStorage } from './types';
@@ -22,6 +22,10 @@ type FSError = {
22
22
  path: string;
23
23
  };
24
24
 
25
+ function lockFile(path: string) {
26
+ return lock(path, { fs, retries: 3, realpath: false });
27
+ }
28
+
25
29
  export class FilesystemStorage implements IDriveStorage {
26
30
  private basePath: string;
27
31
  private drivesPath: string;
@@ -86,12 +90,15 @@ export class FilesystemStorage implements IDriveStorage {
86
90
 
87
91
  async saveDocument(drive: string, id: string, document: Document) {
88
92
  const documentPath = this._buildDocumentPath(drive, id);
89
- await ensureDir(path.dirname(documentPath));
90
- return fs.writeFile(
91
- this._buildDocumentPath(drive, id),
92
- JSON.stringify(document),
93
- { encoding: 'utf-8' }
94
- );
93
+ const releaseLock = await lockFile(documentPath);
94
+ try {
95
+ await ensureDir(path.dirname(documentPath));
96
+ return fs.writeFile(documentPath, JSON.stringify(document), {
97
+ encoding: 'utf-8'
98
+ });
99
+ } finally {
100
+ releaseLock();
101
+ }
95
102
  }
96
103
 
97
104
  async deleteDocument(drive: string, id: string) {