document-drive 0.0.10 → 0.0.12

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.10",
3
+ "version": "0.0.12",
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 });
27
+ }
28
+
25
29
  export class FilesystemStorage implements IDriveStorage {
26
30
  private basePath: string;
27
31
  private drivesPath: string;
@@ -78,19 +82,23 @@ export class FilesystemStorage implements IDriveStorage {
78
82
  { encoding: 'utf-8' }
79
83
  );
80
84
  return JSON.parse(content);
81
- } catch {
85
+ } catch (error) {
86
+ console.error(error);
82
87
  throw new Error(`Document with id ${id} not found`);
83
88
  }
84
89
  }
85
90
 
86
91
  async saveDocument(drive: string, id: string, document: Document) {
87
92
  const documentPath = this._buildDocumentPath(drive, id);
88
- await ensureDir(path.dirname(documentPath));
89
- return fs.writeFile(
90
- this._buildDocumentPath(drive, id),
91
- JSON.stringify(document),
92
- { encoding: 'utf-8' }
93
- );
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
+ }
94
102
  }
95
103
 
96
104
  async deleteDocument(drive: string, id: string) {
@@ -120,8 +128,8 @@ export class FilesystemStorage implements IDriveStorage {
120
128
  let document: Document;
121
129
  try {
122
130
  document = await this.getDocument(FilesystemStorage.DRIVES_DIR, id);
123
- } catch (error) {
124
- throw new Error(`Drive with id ${id} not found: ${error}`);
131
+ } catch {
132
+ throw new Error(`Drive with id ${id} not found`);
125
133
  }
126
134
  if (isDocumentDrive(document)) {
127
135
  return document;