@dxos/lock-file 0.8.4-main.ead640a → 0.8.4-main.f466a3d56e
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/LICENSE +102 -5
- package/README.md +1 -1
- package/dist/lib/node-esm/index.mjs +5 -20
- package/dist/lib/node-esm/index.mjs.map +3 -3
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/src/lock-file.d.ts.map +1 -1
- package/dist/types/src/sys.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +8 -7
- package/src/lock-file.node.test.ts +2 -3
- package/src/locker-subprocess.ts +2 -0
- package/src/sys.ts +1 -2
package/LICENSE
CHANGED
|
@@ -1,8 +1,105 @@
|
|
|
1
|
-
|
|
2
|
-
Copyright (c) 2022 DXOS
|
|
1
|
+
# Functional Source License, Version 1.1, ALv2 Future License
|
|
3
2
|
|
|
4
|
-
|
|
3
|
+
## Abbreviation
|
|
5
4
|
|
|
6
|
-
|
|
5
|
+
FSL-1.1-Apache-2.0
|
|
7
6
|
|
|
8
|
-
|
|
7
|
+
## Notice
|
|
8
|
+
|
|
9
|
+
Copyright 2026 DXOS
|
|
10
|
+
|
|
11
|
+
## Terms and Conditions
|
|
12
|
+
|
|
13
|
+
### Licensor ("We")
|
|
14
|
+
|
|
15
|
+
The party offering the Software under these Terms and Conditions.
|
|
16
|
+
|
|
17
|
+
### The Software
|
|
18
|
+
|
|
19
|
+
The "Software" is each version of the software that we make available under
|
|
20
|
+
these Terms and Conditions, as indicated by our inclusion of these Terms and
|
|
21
|
+
Conditions with the Software.
|
|
22
|
+
|
|
23
|
+
### License Grant
|
|
24
|
+
|
|
25
|
+
Subject to your compliance with this License Grant and the Patents,
|
|
26
|
+
Redistribution and Trademark clauses below, we hereby grant you the right to
|
|
27
|
+
use, copy, modify, create derivative works, publicly perform, publicly display
|
|
28
|
+
and redistribute the Software for any Permitted Purpose identified below.
|
|
29
|
+
|
|
30
|
+
### Permitted Purpose
|
|
31
|
+
|
|
32
|
+
A Permitted Purpose is any purpose other than a Competing Use. A Competing Use
|
|
33
|
+
means making the Software available to others in a commercial product or
|
|
34
|
+
service that:
|
|
35
|
+
|
|
36
|
+
1. substitutes for the Software;
|
|
37
|
+
|
|
38
|
+
2. substitutes for any other product or service we offer using the Software
|
|
39
|
+
that exists as of the date we make the Software available; or
|
|
40
|
+
|
|
41
|
+
3. offers the same or substantially similar functionality as the Software.
|
|
42
|
+
|
|
43
|
+
Permitted Purposes specifically include using the Software:
|
|
44
|
+
|
|
45
|
+
1. for your internal use and access;
|
|
46
|
+
|
|
47
|
+
2. for non-commercial education;
|
|
48
|
+
|
|
49
|
+
3. for non-commercial research; and
|
|
50
|
+
|
|
51
|
+
4. in connection with professional services that you provide to a licensee
|
|
52
|
+
using the Software in accordance with these Terms and Conditions.
|
|
53
|
+
|
|
54
|
+
### Patents
|
|
55
|
+
|
|
56
|
+
To the extent your use for a Permitted Purpose would necessarily infringe our
|
|
57
|
+
patents, the license grant above includes a license under our patents. If you
|
|
58
|
+
make a claim against any party that the Software infringes or contributes to
|
|
59
|
+
the infringement of any patent, then your patent license to the Software ends
|
|
60
|
+
immediately.
|
|
61
|
+
|
|
62
|
+
### Redistribution
|
|
63
|
+
|
|
64
|
+
The Terms and Conditions apply to all copies, modifications and derivatives of
|
|
65
|
+
the Software.
|
|
66
|
+
|
|
67
|
+
If you redistribute any copies, modifications or derivatives of the Software,
|
|
68
|
+
you must include a copy of or a link to these Terms and Conditions and not
|
|
69
|
+
remove any copyright notices provided in or with the Software.
|
|
70
|
+
|
|
71
|
+
### Disclaimer
|
|
72
|
+
|
|
73
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTIES OF ANY KIND, EXPRESS OR
|
|
74
|
+
IMPLIED, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR
|
|
75
|
+
PURPOSE, MERCHANTABILITY, TITLE OR NON-INFRINGEMENT.
|
|
76
|
+
|
|
77
|
+
IN NO EVENT WILL WE HAVE ANY LIABILITY TO YOU ARISING OUT OF OR RELATED TO THE
|
|
78
|
+
SOFTWARE, INCLUDING INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES,
|
|
79
|
+
EVEN IF WE HAVE BEEN INFORMED OF THEIR POSSIBILITY IN ADVANCE.
|
|
80
|
+
|
|
81
|
+
### Trademarks
|
|
82
|
+
|
|
83
|
+
Except for displaying the License Details and identifying us as the origin of
|
|
84
|
+
the Software, you have no right under these Terms and Conditions to use our
|
|
85
|
+
trademarks, trade names, service marks or product names.
|
|
86
|
+
|
|
87
|
+
## Grant of Future License
|
|
88
|
+
|
|
89
|
+
We hereby irrevocably grant you an additional license to use the Software under
|
|
90
|
+
the Apache License, Version 2.0 that is effective on the second anniversary of
|
|
91
|
+
the date we make the Software available. On or after that date, you may use the
|
|
92
|
+
Software under the Apache License, Version 2.0, in which case the following
|
|
93
|
+
will apply:
|
|
94
|
+
|
|
95
|
+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
|
|
96
|
+
this file except in compliance with the License.
|
|
97
|
+
|
|
98
|
+
You may obtain a copy of the License at
|
|
99
|
+
|
|
100
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
101
|
+
|
|
102
|
+
Unless required by applicable law or agreed to in writing, software distributed
|
|
103
|
+
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
104
|
+
CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
|
105
|
+
specific language governing permissions and limitations under the License.
|
package/README.md
CHANGED
|
@@ -18,4 +18,4 @@ pnpm i @dxos/lock-file
|
|
|
18
18
|
|
|
19
19
|
Your ideas, issues, and code are most welcome. Please take a look at our [community code of conduct](https://github.com/dxos/dxos/blob/main/CODE_OF_CONDUCT.md), the [issue guide](https://github.com/dxos/dxos/blob/main/CONTRIBUTING.md#submitting-issues), and the [PR contribution guide](https://github.com/dxos/dxos/blob/main/CONTRIBUTING.md#submitting-prs).
|
|
20
20
|
|
|
21
|
-
License: [
|
|
21
|
+
License: [FSL-1.1-Apache-2.0](./LICENSE) Copyright 2022 © DXOS
|
|
@@ -6,26 +6,17 @@ import { constants, open } from "node:fs/promises";
|
|
|
6
6
|
|
|
7
7
|
// src/sys.ts
|
|
8
8
|
import { platform } from "node:os";
|
|
9
|
-
function _define_property(obj, key, value) {
|
|
10
|
-
if (key in obj) {
|
|
11
|
-
Object.defineProperty(obj, key, {
|
|
12
|
-
value,
|
|
13
|
-
enumerable: true,
|
|
14
|
-
configurable: true,
|
|
15
|
-
writable: true
|
|
16
|
-
});
|
|
17
|
-
} else {
|
|
18
|
-
obj[key] = value;
|
|
19
|
-
}
|
|
20
|
-
return obj;
|
|
21
|
-
}
|
|
22
9
|
var LOCK_SH = 1;
|
|
23
10
|
var LOCK_EX = 2;
|
|
24
11
|
var LOCK_NB = 4;
|
|
25
12
|
var LOCK_UN = 8;
|
|
26
13
|
var LockfileSys = class {
|
|
14
|
+
_init = null;
|
|
15
|
+
_koffi = null;
|
|
16
|
+
_libc = null;
|
|
17
|
+
_flockNative = null;
|
|
27
18
|
async init() {
|
|
28
|
-
await (this._init
|
|
19
|
+
await (this._init ??= this._runInit());
|
|
29
20
|
}
|
|
30
21
|
async _runInit() {
|
|
31
22
|
this._koffi = await import("koffi");
|
|
@@ -82,12 +73,6 @@ var LockfileSys = class {
|
|
|
82
73
|
throw new Error(`flock failed: ${errorMessage}`);
|
|
83
74
|
}
|
|
84
75
|
}
|
|
85
|
-
constructor() {
|
|
86
|
-
_define_property(this, "_init", null);
|
|
87
|
-
_define_property(this, "_koffi", null);
|
|
88
|
-
_define_property(this, "_libc", null);
|
|
89
|
-
_define_property(this, "_flockNative", null);
|
|
90
|
-
}
|
|
91
76
|
};
|
|
92
77
|
|
|
93
78
|
// src/lock-file.ts
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/lock-file.ts", "../../../src/sys.ts"],
|
|
4
|
-
"sourcesContent": ["//\n// Copyright 2023 DXOS.org\n//\n\nimport { existsSync } from 'node:fs';\nimport { type FileHandle, constants, open } from 'node:fs/promises';\n\nimport { LockfileSys } from './sys';\n\nconst sys = new LockfileSys();\n\nexport class LockFile {\n static async acquire(filename: string): Promise<FileHandle> {\n await sys.init();\n\n const handle = await open(filename, constants.O_CREAT | constants.O_RDWR);\n\n try {\n // Try to acquire exclusive non-blocking lock\n sys.flock(handle.fd, 'exnb');\n return handle;\n } catch (err) {\n // Close the file handle if we can't acquire the lock\n await handle.close();\n throw err;\n }\n }\n\n static async release(handle: FileHandle): Promise<void> {\n try {\n // Release the lock\n sys.flock(handle.fd, 'un');\n } finally {\n await handle.close();\n }\n }\n\n static async isLocked(filename: string): Promise<boolean> {\n if (!existsSync(filename)) {\n return false;\n }\n try {\n const handle = await LockFile.acquire(filename);\n await LockFile.release(handle);\n return false;\n } catch (e) {\n return true;\n }\n }\n}\n", "//\n// Copyright 2025 DXOS.org\n//\n\nimport
|
|
5
|
-
"mappings": ";;;AAIA,SAASA,kBAAkB;AAC3B,SAA0BC,WAAWC,YAAY;;;
|
|
6
|
-
"names": ["existsSync", "constants", "open", "platform", "LOCK_SH", "LOCK_EX", "LOCK_NB", "LOCK_UN", "LockfileSys", "
|
|
4
|
+
"sourcesContent": ["//\n// Copyright 2023 DXOS.org\n//\n\nimport { existsSync } from 'node:fs';\nimport { type FileHandle, constants, open } from 'node:fs/promises';\n\nimport { LockfileSys } from './sys';\n\nconst sys = new LockfileSys();\n\nexport class LockFile {\n static async acquire(filename: string): Promise<FileHandle> {\n await sys.init();\n\n const handle = await open(filename, constants.O_CREAT | constants.O_RDWR);\n\n try {\n // Try to acquire exclusive non-blocking lock\n sys.flock(handle.fd, 'exnb');\n return handle;\n } catch (err) {\n // Close the file handle if we can't acquire the lock\n await handle.close();\n throw err;\n }\n }\n\n static async release(handle: FileHandle): Promise<void> {\n try {\n // Release the lock\n sys.flock(handle.fd, 'un');\n } finally {\n await handle.close();\n }\n }\n\n static async isLocked(filename: string): Promise<boolean> {\n if (!existsSync(filename)) {\n return false;\n }\n try {\n const handle = await LockFile.acquire(filename);\n await LockFile.release(handle);\n return false;\n } catch (e) {\n return true;\n }\n }\n}\n", "//\n// Copyright 2025 DXOS.org\n//\n\nimport type * as koffi from 'koffi';\nimport { platform } from 'node:os';\n\n// flock constants\nconst LOCK_SH = 1; // Shared lock\nconst LOCK_EX = 2; // Exclusive lock\nconst LOCK_NB = 4; // Non-blocking\nconst LOCK_UN = 8; // Unlock\n\nexport class LockfileSys {\n private _init: Promise<void> | null = null;\n\n private _koffi: typeof koffi | null = null;\n private _libc: koffi.IKoffiLib | null = null;\n private _flockNative: koffi.KoffiFunction | null = null;\n\n async init() {\n await (this._init ??= this._runInit());\n }\n\n private async _runInit() {\n this._koffi = await import('koffi');\n switch (platform()) {\n case 'darwin':\n this._libc = this._koffi.load('libc.dylib');\n break;\n case 'linux':\n this._libc = this._koffi.load('libc.so.6');\n break;\n default:\n throw new Error(`Unsupported platform: ${platform()}`);\n }\n this._flockNative = this._libc.func('flock', 'int', ['int', 'int']);\n }\n\n flock(fd: number, operation: string) {\n if (!this._flockNative) {\n throw new Error('flock not initialized');\n }\n let op = 0;\n\n switch (operation) {\n case 'ex':\n op = LOCK_EX;\n break;\n case 'exnb':\n op = LOCK_EX | LOCK_NB;\n break;\n case 'sh':\n op = LOCK_SH;\n break;\n case 'shnb':\n op = LOCK_SH | LOCK_NB;\n break;\n case 'un':\n op = LOCK_UN;\n break;\n default:\n throw new Error(`Invalid flock operation: ${operation}`);\n }\n\n const result = this._flockNative!(fd, op);\n\n if (result !== 0) {\n // Get the errno to provide a more meaningful error\n const errno = this._koffi!.errno();\n const errorMessages: { [key: number]: string } = {\n 11: 'Resource temporarily unavailable (EAGAIN/EWOULDBLOCK)',\n 13: 'Permission denied (EACCES)',\n 22: 'Invalid argument (EINVAL)',\n 9: 'Bad file descriptor (EBADF)',\n 35: 'Resource temporarily unavailable (EAGAIN on macOS)',\n };\n\n const errorMessage = errorMessages[errno] || `Unknown error (errno: ${errno})`;\n throw new Error(`flock failed: ${errorMessage}`);\n }\n }\n}\n"],
|
|
5
|
+
"mappings": ";;;AAIA,SAASA,kBAAkB;AAC3B,SAA0BC,WAAWC,YAAY;;;ACAjD,SAASC,gBAAgB;AAGzB,IAAMC,UAAU;AAChB,IAAMC,UAAU;AAChB,IAAMC,UAAU;AAChB,IAAMC,UAAU;AAET,IAAMC,cAAN,MAAMA;EACHC,QAA8B;EAE9BC,SAA8B;EAC9BC,QAAgC;EAChCC,eAA2C;EAEnD,MAAMC,OAAO;AACX,WAAO,KAAKJ,UAAU,KAAKK,SAAQ;EACrC;EAEA,MAAcA,WAAW;AACvB,SAAKJ,SAAS,MAAM,OAAO,OAAA;AAC3B,YAAQP,SAAAA,GAAAA;MACN,KAAK;AACH,aAAKQ,QAAQ,KAAKD,OAAOK,KAAK,YAAA;AAC9B;MACF,KAAK;AACH,aAAKJ,QAAQ,KAAKD,OAAOK,KAAK,WAAA;AAC9B;MACF;AACE,cAAM,IAAIC,MAAM,yBAAyBb,SAAAA,CAAAA,EAAY;IACzD;AACA,SAAKS,eAAe,KAAKD,MAAMM,KAAK,SAAS,OAAO;MAAC;MAAO;KAAM;EACpE;EAEAC,MAAMC,IAAYC,WAAmB;AACnC,QAAI,CAAC,KAAKR,cAAc;AACtB,YAAM,IAAII,MAAM,uBAAA;IAClB;AACA,QAAIK,KAAK;AAET,YAAQD,WAAAA;MACN,KAAK;AACHC,aAAKhB;AACL;MACF,KAAK;AACHgB,aAAKhB,UAAUC;AACf;MACF,KAAK;AACHe,aAAKjB;AACL;MACF,KAAK;AACHiB,aAAKjB,UAAUE;AACf;MACF,KAAK;AACHe,aAAKd;AACL;MACF;AACE,cAAM,IAAIS,MAAM,4BAA4BI,SAAAA,EAAW;IAC3D;AAEA,UAAME,SAAS,KAAKV,aAAcO,IAAIE,EAAAA;AAEtC,QAAIC,WAAW,GAAG;AAEhB,YAAMC,QAAQ,KAAKb,OAAQa,MAAK;AAChC,YAAMC,gBAA2C;QAC/C,IAAI;QACJ,IAAI;QACJ,IAAI;QACJ,GAAG;QACH,IAAI;MACN;AAEA,YAAMC,eAAeD,cAAcD,KAAAA,KAAU,yBAAyBA,KAAAA;AACtE,YAAM,IAAIP,MAAM,iBAAiBS,YAAAA,EAAc;IACjD;EACF;AACF;;;ADzEA,IAAMC,MAAM,IAAIC,YAAAA;AAET,IAAMC,WAAN,MAAMA,UAAAA;EACX,aAAaC,QAAQC,UAAuC;AAC1D,UAAMJ,IAAIK,KAAI;AAEd,UAAMC,SAAS,MAAMC,KAAKH,UAAUI,UAAUC,UAAUD,UAAUE,MAAM;AAExE,QAAI;AAEFV,UAAIW,MAAML,OAAOM,IAAI,MAAA;AACrB,aAAON;IACT,SAASO,KAAK;AAEZ,YAAMP,OAAOQ,MAAK;AAClB,YAAMD;IACR;EACF;EAEA,aAAaE,QAAQT,QAAmC;AACtD,QAAI;AAEFN,UAAIW,MAAML,OAAOM,IAAI,IAAA;IACvB,UAAA;AACE,YAAMN,OAAOQ,MAAK;IACpB;EACF;EAEA,aAAaE,SAASZ,UAAoC;AACxD,QAAI,CAACa,WAAWb,QAAAA,GAAW;AACzB,aAAO;IACT;AACA,QAAI;AACF,YAAME,SAAS,MAAMJ,UAASC,QAAQC,QAAAA;AACtC,YAAMF,UAASa,QAAQT,MAAAA;AACvB,aAAO;IACT,SAASY,GAAG;AACV,aAAO;IACT;EACF;AACF;",
|
|
6
|
+
"names": ["existsSync", "constants", "open", "platform", "LOCK_SH", "LOCK_EX", "LOCK_NB", "LOCK_UN", "LockfileSys", "_init", "_koffi", "_libc", "_flockNative", "init", "_runInit", "load", "Error", "func", "flock", "fd", "operation", "op", "result", "errno", "errorMessages", "errorMessage", "sys", "LockfileSys", "LockFile", "acquire", "filename", "init", "handle", "open", "constants", "O_CREAT", "O_RDWR", "flock", "fd", "err", "close", "release", "isLocked", "existsSync", "e"]
|
|
7
7
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"inputs":{"src/sys.ts":{"bytes":
|
|
1
|
+
{"inputs":{"src/sys.ts":{"bytes":7682,"imports":[{"path":"node:os","kind":"import-statement","external":true},{"path":"koffi","kind":"dynamic-import","external":true}],"format":"esm"},"src/lock-file.ts":{"bytes":4187,"imports":[{"path":"node:fs","kind":"import-statement","external":true},{"path":"node:fs/promises","kind":"import-statement","external":true},{"path":"src/sys.ts","kind":"import-statement","original":"./sys"}],"format":"esm"},"src/index.ts":{"bytes":377,"imports":[{"path":"src/lock-file.ts","kind":"import-statement","original":"./lock-file"}],"format":"esm"}},"outputs":{"dist/lib/node-esm/index.mjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":5838},"dist/lib/node-esm/index.mjs":{"imports":[{"path":"node:fs","kind":"import-statement","external":true},{"path":"node:fs/promises","kind":"import-statement","external":true},{"path":"node:os","kind":"import-statement","external":true},{"path":"koffi","kind":"dynamic-import","external":true}],"exports":["LockFile"],"entryPoint":"src/index.ts","inputs":{"src/lock-file.ts":{"bytesInOutput":831},"src/sys.ts":{"bytesInOutput":1806},"src/index.ts":{"bytesInOutput":0}},"bytes":2844}}}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"lock-file.d.ts","sourceRoot":"","sources":["../../../src/lock-file.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,KAAK,UAAU,EAAmB,MAAM,kBAAkB,CAAC;AAMpE,qBAAa,QAAQ;
|
|
1
|
+
{"version":3,"file":"lock-file.d.ts","sourceRoot":"","sources":["../../../src/lock-file.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,KAAK,UAAU,EAAmB,MAAM,kBAAkB,CAAC;AAMpE,qBAAa,QAAQ;IACnB,OAAa,OAAO,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC,CAc1D;IAED,OAAa,OAAO,CAAC,MAAM,EAAE,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC,CAOtD;IAED,OAAa,QAAQ,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAWxD;CACF"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sys.d.ts","sourceRoot":"","sources":["../../../src/sys.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"sys.d.ts","sourceRoot":"","sources":["../../../src/sys.ts"],"names":[],"mappings":"AAaA,qBAAa,WAAW;IACtB,OAAO,CAAC,KAAK,CAA8B;IAE3C,OAAO,CAAC,MAAM,CAA6B;IAC3C,OAAO,CAAC,KAAK,CAAgC;IAC7C,OAAO,CAAC,YAAY,CAAoC;IAElD,IAAI,kBAET;YAEa,QAAQ;IAetB,KAAK,CAAC,EAAE,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,QA0ClC;CACF"}
|