@dxos/lock-file 0.8.4-main.67995b8 → 0.8.4-main.a4bbb77

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": "@dxos/lock-file",
3
- "version": "0.8.4-main.67995b8",
3
+ "version": "0.8.4-main.a4bbb77",
4
4
  "description": "Lock file .",
5
5
  "homepage": "https://dxos.org",
6
6
  "bugs": "https://github.com/dxos/dxos/issues",
@@ -10,9 +10,9 @@
10
10
  "type": "module",
11
11
  "exports": {
12
12
  ".": {
13
+ "source": "./src/index.ts",
13
14
  "types": "./dist/types/src/index.d.ts",
14
- "node": "./dist/lib/node-esm/index.mjs",
15
- "source": "./src/index.ts"
15
+ "node": "./dist/lib/node-esm/index.mjs"
16
16
  }
17
17
  },
18
18
  "types": "dist/types/src/index.d.ts",
@@ -24,13 +24,11 @@
24
24
  "src"
25
25
  ],
26
26
  "dependencies": {
27
- "fs-ext": "^2.0.0",
28
- "@dxos/async": "0.8.4-main.67995b8",
29
- "@dxos/node-std": "0.8.4-main.67995b8"
30
- },
31
- "devDependencies": {
32
- "@types/fs-ext": "^2.0.0"
27
+ "koffi": "^2.8.0",
28
+ "@dxos/async": "0.8.4-main.a4bbb77",
29
+ "@dxos/node-std": "0.8.4-main.a4bbb77"
33
30
  },
31
+ "devDependencies": {},
34
32
  "publishConfig": {
35
33
  "access": "public"
36
34
  }
@@ -4,18 +4,27 @@
4
4
 
5
5
  import { spawn } from 'node:child_process';
6
6
  import { existsSync, mkdirSync, unlinkSync } from 'node:fs';
7
+ import { rm } from 'node:fs/promises';
8
+ import { tmpdir } from 'node:os';
7
9
  import { join } from 'node:path';
8
- import { beforeAll, describe, expect, onTestFinished, test } from 'vitest';
10
+
11
+ import { afterAll, beforeAll, describe, expect, onTestFinished, test } from 'vitest';
9
12
 
10
13
  import { Trigger } from '@dxos/async';
11
14
 
12
15
  import { LockFile } from './lock-file';
13
16
 
14
17
  const TEST_DIR = '/tmp/dxos/testing/lock-file';
18
+ const TEMP_TEST_DIR = join(tmpdir(), 'lock-file-test-' + Date.now());
15
19
 
16
20
  describe('LockFile', () => {
17
21
  beforeAll(() => {
18
22
  mkdirSync(TEST_DIR, { recursive: true });
23
+ mkdirSync(TEMP_TEST_DIR, { recursive: true });
24
+ });
25
+
26
+ afterAll(async () => {
27
+ await rm(TEMP_TEST_DIR, { recursive: true, force: true });
19
28
  });
20
29
 
21
30
  test('basic', async () => {
@@ -38,16 +47,21 @@ describe('LockFile', () => {
38
47
  });
39
48
 
40
49
  const trigger = new Trigger();
41
- const processHandle = spawn('node', ['-e', `(${lockInProcess.toString()})(${JSON.stringify(filename)})`], {
50
+ const processHandle = spawn('vite-node', [new URL('./locker-subprocess.ts', import.meta.url).pathname, filename], {
42
51
  stdio: 'pipe',
43
52
  });
44
53
 
45
54
  {
46
55
  // Wait for process to start
47
-
48
56
  processHandle.stdout.on('data', (data: Uint8Array) => {
49
- expect(data.toString().trim()).to.equal('locked');
50
- trigger.wake();
57
+ process.stdout.write(data);
58
+ if (data.toString().trim().startsWith('#')) {
59
+ expect(data.toString().trim()).to.equal('# locked');
60
+ trigger.wake();
61
+ }
62
+ });
63
+ processHandle.on('exit', (code) => {
64
+ trigger.throw(new Error(`Process exited pre with code ${code}`));
51
65
  });
52
66
  }
53
67
 
@@ -56,7 +70,50 @@ describe('LockFile', () => {
56
70
  await expect(LockFile.acquire(filename)).rejects.toBeInstanceOf(Error);
57
71
 
58
72
  processHandle.stdin.write('close');
59
- processHandle.kill();
73
+
74
+ // Wait for process to be killed
75
+ await expect
76
+ .poll(async () => {
77
+ return await LockFile.isLocked(filename);
78
+ })
79
+ .toBe(false);
80
+
81
+ const handle = await LockFile.acquire(filename);
82
+ await LockFile.release(handle);
83
+ });
84
+
85
+ test('released when process killed with SIGKILL', { timeout: 10_000 }, async () => {
86
+ const filename = join(TEST_DIR, `lock-${Math.random()}.lock`);
87
+ onTestFinished(() => {
88
+ if (existsSync(filename)) {
89
+ unlinkSync(filename);
90
+ }
91
+ });
92
+
93
+ const trigger = new Trigger();
94
+ const processHandle = spawn('vite-node', [new URL('./locker-subprocess.ts', import.meta.url).pathname, filename], {
95
+ stdio: 'pipe',
96
+ });
97
+
98
+ {
99
+ // Wait for process to start
100
+ processHandle.stdout.on('data', (data: Uint8Array) => {
101
+ process.stdout.write(data);
102
+ if (data.toString().trim().startsWith('#')) {
103
+ expect(data.toString().trim()).to.equal('# locked');
104
+ trigger.wake();
105
+ }
106
+ });
107
+ processHandle.on('exit', (code) => {
108
+ trigger.throw(new Error(`Process exited pre with code ${code}`));
109
+ });
110
+ }
111
+
112
+ await trigger.wait({ timeout: 5_000 });
113
+
114
+ await expect(LockFile.acquire(filename)).rejects.toBeInstanceOf(Error);
115
+
116
+ processHandle.kill('SIGKILL');
60
117
 
61
118
  // Wait for process to be killed
62
119
  await expect
@@ -80,44 +137,55 @@ describe('LockFile', () => {
80
137
  expect(await LockFile.isLocked(filename)).to.be.false;
81
138
  }
82
139
  });
83
- });
84
140
 
85
- // NOTE: Self-contained so when function.toString is called the code runs.
86
- const lockInProcess = (filename: string) => {
87
- // eslint-disable-next-line @typescript-eslint/no-require-imports
88
- const { open } = require('node:fs/promises');
89
- // eslint-disable-next-line @typescript-eslint/no-require-imports
90
- const { constants } = require('node:fs');
91
- // eslint-disable-next-line @typescript-eslint/no-require-imports
92
- const { flock } = require('fs-ext');
93
-
94
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
95
- // @ts-ignore
96
- let fileHandle;
97
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
98
- // @ts-ignore
99
- open(filename, constants.O_CREAT).then((handle) => {
100
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
101
- // @ts-ignore
102
- flock(handle.fd, 'exnb', (err) => {
103
- if (err) {
104
- handle.close();
105
- console.error(err);
106
- return;
107
- }
108
- fileHandle = handle;
109
- console.log('locked');
110
- // Hang
111
- setTimeout(() => {}, 1_000_000);
112
- });
141
+ // New tests merged from lock-file.test.ts
142
+ test('should acquire and release lock', async () => {
143
+ const lockFile = join(TEMP_TEST_DIR, 'test.lock');
144
+ const handle = await LockFile.acquire(lockFile);
145
+ expect(handle).toBeDefined();
146
+ expect(handle.fd).toBeGreaterThan(0);
147
+
148
+ // Lock should be held
149
+ await expect(LockFile.isLocked(lockFile)).resolves.toBe(true);
150
+
151
+ await LockFile.release(handle);
152
+
153
+ // Lock should be released
154
+ await expect(LockFile.isLocked(lockFile)).resolves.toBe(false);
113
155
  });
114
156
 
115
- // Close file handle on stdin close.
116
- process.stdin.on('data', (data) => {
117
- if (data.toString().trim() === 'close') {
118
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
119
- // @ts-ignore
120
- fileHandle.close();
121
- }
157
+ test('should fail to acquire lock when already locked', async () => {
158
+ const lockFile = join(TEMP_TEST_DIR, 'test2.lock');
159
+ const handle = await LockFile.acquire(lockFile);
160
+
161
+ // Try to acquire again - should fail
162
+ await expect(LockFile.acquire(lockFile)).rejects.toThrow('flock failed');
163
+
164
+ await LockFile.release(handle);
122
165
  });
123
- };
166
+
167
+ test('isLocked should return false for non-existent file', async () => {
168
+ const nonExistent = join(TEMP_TEST_DIR, 'non-existent.lock');
169
+ await expect(LockFile.isLocked(nonExistent)).resolves.toBe(false);
170
+ });
171
+
172
+ test('should handle concurrent lock attempts', async () => {
173
+ const lockFile = join(TEMP_TEST_DIR, 'test3.lock');
174
+ const handle = await LockFile.acquire(lockFile);
175
+
176
+ // Multiple attempts to acquire should all fail
177
+ const attempts = Array(5)
178
+ .fill(0)
179
+ .map(() => LockFile.acquire(lockFile).catch((err) => err));
180
+
181
+ const results = await Promise.all(attempts);
182
+
183
+ // All attempts should have failed
184
+ results.forEach((result) => {
185
+ expect(result).toBeInstanceOf(Error);
186
+ expect(result.message).toContain('flock failed');
187
+ });
188
+
189
+ await LockFile.release(handle);
190
+ });
191
+ });
package/src/lock-file.ts CHANGED
@@ -2,37 +2,37 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
5
- import { flock } from 'fs-ext';
6
5
  import { existsSync } from 'node:fs';
7
- import { open, type FileHandle, constants } from 'node:fs/promises';
6
+ import { type FileHandle, constants, open } from 'node:fs/promises';
7
+
8
+ import { LockfileSys } from './sys';
9
+
10
+ const sys = new LockfileSys();
8
11
 
9
12
  export class LockFile {
10
13
  static async acquire(filename: string): Promise<FileHandle> {
11
- const handle = await open(filename, constants.O_CREAT);
12
- await new Promise<void>((resolve, reject) => {
13
- flock(handle.fd, 'exnb', async (err) => {
14
- if (err) {
15
- reject(err);
16
- await handle.close();
17
- return;
18
- }
19
- resolve();
20
- });
21
- });
22
- return handle;
14
+ await sys.init();
15
+
16
+ const handle = await open(filename, constants.O_CREAT | constants.O_RDWR);
17
+
18
+ try {
19
+ // Try to acquire exclusive non-blocking lock
20
+ sys.flock(handle.fd, 'exnb');
21
+ return handle;
22
+ } catch (err) {
23
+ // Close the file handle if we can't acquire the lock
24
+ await handle.close();
25
+ throw err;
26
+ }
23
27
  }
24
28
 
25
29
  static async release(handle: FileHandle): Promise<void> {
26
- await new Promise<void>((resolve, reject) => {
27
- flock(handle.fd, 'un', (err) => {
28
- if (err) {
29
- reject(err);
30
- return;
31
- }
32
- resolve();
33
- });
34
- });
35
- await handle.close();
30
+ try {
31
+ // Release the lock
32
+ sys.flock(handle.fd, 'un');
33
+ } finally {
34
+ await handle.close();
35
+ }
36
36
  }
37
37
 
38
38
  static async isLocked(filename: string): Promise<boolean> {
@@ -42,7 +42,6 @@ export class LockFile {
42
42
  try {
43
43
  const handle = await LockFile.acquire(filename);
44
44
  await LockFile.release(handle);
45
-
46
45
  return false;
47
46
  } catch (e) {
48
47
  return true;
@@ -0,0 +1,22 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import { LockFile } from './lock-file';
6
+
7
+ const filename = process.argv[2];
8
+
9
+ console.log('will lock');
10
+
11
+ const handle = await LockFile.acquire(filename);
12
+ // parents looks for # symbol in the output to know when the lock is acquired
13
+ console.log('# locked');
14
+
15
+ // Close file handle on stdin close.
16
+ process.stdin.on('data', async (data) => {
17
+ if (data.toString().trim() === 'close') {
18
+ console.log('will unlock');
19
+ await LockFile.release(handle);
20
+ console.log('unlocked');
21
+ }
22
+ });
package/src/sys.ts ADDED
@@ -0,0 +1,84 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import { platform } from 'node:os';
6
+
7
+ import type * as koffi from 'koffi';
8
+
9
+ // flock constants
10
+ const LOCK_SH = 1; // Shared lock
11
+ const LOCK_EX = 2; // Exclusive lock
12
+ const LOCK_NB = 4; // Non-blocking
13
+ const LOCK_UN = 8; // Unlock
14
+
15
+ export class LockfileSys {
16
+ private _init: Promise<void> | null = null;
17
+
18
+ private _koffi: typeof koffi | null = null;
19
+ private _libc: koffi.IKoffiLib | null = null;
20
+ private _flockNative: koffi.KoffiFunction | null = null;
21
+
22
+ async init() {
23
+ await (this._init ??= this._runInit());
24
+ }
25
+
26
+ private async _runInit() {
27
+ this._koffi = await import('koffi');
28
+ switch (platform()) {
29
+ case 'darwin':
30
+ this._libc = this._koffi.load('libc.dylib');
31
+ break;
32
+ case 'linux':
33
+ this._libc = this._koffi.load('libc.so.6');
34
+ break;
35
+ default:
36
+ throw new Error(`Unsupported platform: ${platform()}`);
37
+ }
38
+ this._flockNative = this._libc.func('flock', 'int', ['int', 'int']);
39
+ }
40
+
41
+ flock(fd: number, operation: string) {
42
+ if (!this._flockNative) {
43
+ throw new Error('flock not initialized');
44
+ }
45
+ let op = 0;
46
+
47
+ switch (operation) {
48
+ case 'ex':
49
+ op = LOCK_EX;
50
+ break;
51
+ case 'exnb':
52
+ op = LOCK_EX | LOCK_NB;
53
+ break;
54
+ case 'sh':
55
+ op = LOCK_SH;
56
+ break;
57
+ case 'shnb':
58
+ op = LOCK_SH | LOCK_NB;
59
+ break;
60
+ case 'un':
61
+ op = LOCK_UN;
62
+ break;
63
+ default:
64
+ throw new Error(`Invalid flock operation: ${operation}`);
65
+ }
66
+
67
+ const result = this._flockNative!(fd, op);
68
+
69
+ if (result !== 0) {
70
+ // Get the errno to provide a more meaningful error
71
+ const errno = this._koffi!.errno();
72
+ const errorMessages: { [key: number]: string } = {
73
+ 11: 'Resource temporarily unavailable (EAGAIN/EWOULDBLOCK)',
74
+ 13: 'Permission denied (EACCES)',
75
+ 22: 'Invalid argument (EINVAL)',
76
+ 9: 'Bad file descriptor (EBADF)',
77
+ 35: 'Resource temporarily unavailable (EAGAIN on macOS)',
78
+ };
79
+
80
+ const errorMessage = errorMessages[errno] || `Unknown error (errno: ${errno})`;
81
+ throw new Error(`flock failed: ${errorMessage}`);
82
+ }
83
+ }
84
+ }