@dxos/lock-file 0.1.52-main.0e90c21

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 ADDED
@@ -0,0 +1,8 @@
1
+ MIT License
2
+ Copyright (c) 2022 DXOS
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5
+
6
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7
+
8
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,72 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // packages/common/lock-file/src/index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ LockFile: () => LockFile
24
+ });
25
+ module.exports = __toCommonJS(src_exports);
26
+
27
+ // packages/common/lock-file/src/lock-file.ts
28
+ var import_fs_ext = require("fs-ext");
29
+ var import_node_fs = require("node:fs");
30
+ var import_promises = require("node:fs/promises");
31
+ var LockFile = class {
32
+ static async acquire(filename) {
33
+ const handle = await (0, import_promises.open)(filename, import_node_fs.constants.O_CREAT);
34
+ await new Promise((resolve, reject) => {
35
+ (0, import_fs_ext.flock)(handle.fd, "exnb", (err) => {
36
+ if (err) {
37
+ reject(err);
38
+ return;
39
+ }
40
+ resolve();
41
+ });
42
+ });
43
+ return handle;
44
+ }
45
+ static async release(handle) {
46
+ await new Promise((resolve, reject) => {
47
+ (0, import_fs_ext.flock)(handle.fd, "un", (err) => {
48
+ if (err) {
49
+ reject(err);
50
+ return;
51
+ }
52
+ resolve();
53
+ });
54
+ });
55
+ await handle.close();
56
+ }
57
+ static async isLocked(filename) {
58
+ try {
59
+ const handle = await LockFile.acquire(filename);
60
+ await LockFile.release(handle);
61
+ await handle.close();
62
+ return false;
63
+ } catch (e) {
64
+ return true;
65
+ }
66
+ }
67
+ };
68
+ // Annotate the CommonJS export names for ESM import in node:
69
+ 0 && (module.exports = {
70
+ LockFile
71
+ });
72
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../../src/index.ts", "../../../src/lock-file.ts"],
4
+ "sourcesContent": ["//\n// Copyright 2023 DXOS.org\n//\n\nexport * from './lock-file';\n", "//\n// Copyright 2023 DXOS.org\n//\n\nimport { flock } from 'fs-ext';\nimport { constants } from 'node:fs';\nimport { open, FileHandle } from 'node:fs/promises';\n\nexport class LockFile {\n static async acquire(filename: string): Promise<FileHandle> {\n const handle = await open(filename, constants.O_CREAT);\n await new Promise<void>((resolve, reject) => {\n flock(handle.fd, 'exnb', (err) => {\n if (err) {\n reject(err);\n return;\n }\n resolve();\n });\n });\n return handle;\n }\n\n static async release(handle: FileHandle): Promise<void> {\n await new Promise<void>((resolve, reject) => {\n flock(handle.fd, 'un', (err) => {\n if (err) {\n reject(err);\n return;\n }\n resolve();\n });\n });\n await handle.close();\n }\n\n static async isLocked(filename: string): Promise<boolean> {\n try {\n const handle = await LockFile.acquire(filename);\n await LockFile.release(handle);\n await handle.close();\n\n return false;\n } catch (e) {\n return true;\n }\n }\n}\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;ACIA,oBAAsB;AACtB,qBAA0B;AAC1B,sBAAiC;AAE1B,IAAMA,WAAN,MAAMA;EACX,aAAaC,QAAQC,UAAuC;AAC1D,UAAMC,SAAS,UAAMC,sBAAKF,UAAUG,yBAAUC,OAAO;AACrD,UAAM,IAAIC,QAAc,CAACC,SAASC,WAAW;AAC3CC,+BAAMP,OAAOQ,IAAI,QAAQ,CAACC,QAAQ;AAChC,YAAIA,KAAK;AACPH,iBAAOG,GAAAA;AACP;QACF;AACAJ,gBAAAA;MACF,CAAA;IACF,CAAA;AACA,WAAOL;EACT;EAEA,aAAaU,QAAQV,QAAmC;AACtD,UAAM,IAAII,QAAc,CAACC,SAASC,WAAW;AAC3CC,+BAAMP,OAAOQ,IAAI,MAAM,CAACC,QAAQ;AAC9B,YAAIA,KAAK;AACPH,iBAAOG,GAAAA;AACP;QACF;AACAJ,gBAAAA;MACF,CAAA;IACF,CAAA;AACA,UAAML,OAAOW,MAAK;EACpB;EAEA,aAAaC,SAASb,UAAoC;AACxD,QAAI;AACF,YAAMC,SAAS,MAAMH,SAASC,QAAQC,QAAAA;AACtC,YAAMF,SAASa,QAAQV,MAAAA;AACvB,YAAMA,OAAOW,MAAK;AAElB,aAAO;IACT,SAASE,GAAP;AACA,aAAO;IACT;EACF;AACF;",
6
+ "names": ["LockFile", "acquire", "filename", "handle", "open", "constants", "O_CREAT", "Promise", "resolve", "reject", "flock", "fd", "err", "release", "close", "isLocked", "e"]
7
+ }
@@ -0,0 +1 @@
1
+ {"inputs":{"packages/common/lock-file/src/lock-file.ts":{"bytes":4071,"imports":[{"path":"fs-ext","kind":"import-statement","external":true},{"path":"node:fs","kind":"import-statement","external":true},{"path":"node:fs/promises","kind":"import-statement","external":true}]},"packages/common/lock-file/src/index.ts":{"bytes":377,"imports":[{"path":"packages/common/lock-file/src/lock-file.ts","kind":"import-statement","original":"./lock-file"}]}},"outputs":{"packages/common/lock-file/dist/lib/node/index.cjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":2241},"packages/common/lock-file/dist/lib/node/index.cjs":{"imports":[{"path":"fs-ext","kind":"require-call","external":true},{"path":"node:fs","kind":"require-call","external":true},{"path":"node:fs/promises","kind":"require-call","external":true}],"exports":[],"entryPoint":"packages/common/lock-file/src/index.ts","inputs":{"packages/common/lock-file/src/index.ts":{"bytesInOutput":121},"packages/common/lock-file/src/lock-file.ts":{"bytesInOutput":1032}},"bytes":2179}}}
@@ -0,0 +1,2 @@
1
+ export * from './lock-file';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AAIA,cAAc,aAAa,CAAC"}
@@ -0,0 +1,8 @@
1
+ /// <reference types="node" />
2
+ import { FileHandle } from 'node:fs/promises';
3
+ export declare class LockFile {
4
+ static acquire(filename: string): Promise<FileHandle>;
5
+ static release(handle: FileHandle): Promise<void>;
6
+ static isLocked(filename: string): Promise<boolean>;
7
+ }
8
+ //# sourceMappingURL=lock-file.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lock-file.d.ts","sourceRoot":"","sources":["../../../src/lock-file.ts"],"names":[],"mappings":";AAMA,OAAO,EAAQ,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAEpD,qBAAa,QAAQ;WACN,OAAO,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC;WAc9C,OAAO,CAAC,MAAM,EAAE,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC;WAa1C,QAAQ,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;CAW1D"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=lock-file.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lock-file.test.d.ts","sourceRoot":"","sources":["../../../src/lock-file.test.ts"],"names":[],"mappings":""}
package/package.json ADDED
@@ -0,0 +1,30 @@
1
+ {
2
+ "name": "@dxos/lock-file",
3
+ "version": "0.1.52-main.0e90c21",
4
+ "description": "Lock file .",
5
+ "homepage": "https://dxos.org",
6
+ "bugs": "https://github.com/dxos/dxos/issues",
7
+ "license": "MIT",
8
+ "author": "DXOS.org",
9
+ "main": "./dist/lib/node/index.cjs",
10
+ "types": "./dist/types/src/index.d.ts",
11
+ "files": [
12
+ "dist",
13
+ "src"
14
+ ],
15
+ "dependencies": {
16
+ "fs-ext": "^2.0.0",
17
+ "@dxos/async": "0.1.52-main.0e90c21",
18
+ "@dxos/log": "0.1.52-main.0e90c21",
19
+ "@dxos/node-std": "0.1.52-main.0e90c21"
20
+ },
21
+ "devDependencies": {
22
+ "@types/fs-ext": "^2.0.0",
23
+ "@types/node": "^18.11.9",
24
+ "ts-node": "10.9.1",
25
+ "typescript": "^5.0.4"
26
+ },
27
+ "publishConfig": {
28
+ "access": "public"
29
+ }
30
+ }
package/src/index.ts ADDED
@@ -0,0 +1,5 @@
1
+ //
2
+ // Copyright 2023 DXOS.org
3
+ //
4
+
5
+ export * from './lock-file';
@@ -0,0 +1,72 @@
1
+ //
2
+ // Copyright 2023 DXOS.org
3
+ //
4
+
5
+ import chai, { expect } from 'chai';
6
+ import chaiAsPromised from 'chai-as-promised';
7
+ import { spawn } from 'node:child_process';
8
+ import { join } from 'node:path';
9
+
10
+ import { sleep } from '@dxos/async';
11
+ import { describe, test } from '@dxos/test';
12
+
13
+ import { LockFile } from './lock-file';
14
+
15
+ chai.use(chaiAsPromised);
16
+
17
+ describe('LockFile', () => {
18
+ test('basic', async () => {
19
+ const filename = join('/tmp', `lock-${Math.random()}.lock`);
20
+
21
+ const handle = await LockFile.acquire(filename);
22
+ await expect(LockFile.acquire(filename)).to.be.rejected;
23
+ await LockFile.release(handle);
24
+
25
+ const handle2 = await LockFile.acquire(filename);
26
+ await LockFile.release(handle2);
27
+ });
28
+
29
+ test('released when process exists', async () => {
30
+ const filename = join('/tmp', `lock-${Math.random()}.lock`);
31
+ const processHandle = spawn('node', ['-e', `(${lockInProcess.toString()})(${JSON.stringify(filename)})`], {
32
+ stdio: 'inherit',
33
+ });
34
+
35
+ // Wait for process to start
36
+ await sleep(200);
37
+ await expect(LockFile.acquire(filename)).to.be.rejected;
38
+
39
+ processHandle.kill();
40
+
41
+ // Wait for process to be killed
42
+ await sleep(200);
43
+
44
+ const handle = await LockFile.acquire(filename);
45
+ await LockFile.release(handle);
46
+ });
47
+ });
48
+
49
+ // NOTE: Self-contained so when function.toString is called the code runs.
50
+ const lockInProcess = (filename: string) => {
51
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
52
+ const { open } = require('node:fs/promises');
53
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
54
+ const { constants } = require('node:fs');
55
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
56
+ const { flock } = require('fs-ext');
57
+
58
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
59
+ // @ts-ignore
60
+ open(filename, constants.O_CREAT).then((handle) => {
61
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
62
+ // @ts-ignore
63
+ flock(handle.fd, 'exnb', (err) => {
64
+ if (err) {
65
+ console.error(err);
66
+ return;
67
+ }
68
+ // Hang
69
+ setTimeout(() => {}, 1_000_000);
70
+ });
71
+ });
72
+ };
@@ -0,0 +1,48 @@
1
+ //
2
+ // Copyright 2023 DXOS.org
3
+ //
4
+
5
+ import { flock } from 'fs-ext';
6
+ import { constants } from 'node:fs';
7
+ import { open, FileHandle } from 'node:fs/promises';
8
+
9
+ export class LockFile {
10
+ 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', (err) => {
14
+ if (err) {
15
+ reject(err);
16
+ return;
17
+ }
18
+ resolve();
19
+ });
20
+ });
21
+ return handle;
22
+ }
23
+
24
+ static async release(handle: FileHandle): Promise<void> {
25
+ await new Promise<void>((resolve, reject) => {
26
+ flock(handle.fd, 'un', (err) => {
27
+ if (err) {
28
+ reject(err);
29
+ return;
30
+ }
31
+ resolve();
32
+ });
33
+ });
34
+ await handle.close();
35
+ }
36
+
37
+ static async isLocked(filename: string): Promise<boolean> {
38
+ try {
39
+ const handle = await LockFile.acquire(filename);
40
+ await LockFile.release(handle);
41
+ await handle.close();
42
+
43
+ return false;
44
+ } catch (e) {
45
+ return true;
46
+ }
47
+ }
48
+ }