@xirconsss/zero-mock 1.2.0 → 1.2.1
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/dist/cleanup.js +61 -0
- package/dist/errors.js +7 -16
- package/dist/store/jsonStore.js +1 -1
- package/package.json +1 -1
package/dist/cleanup.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.registerLock = registerLock;
|
|
7
|
+
exports.releaseLock = releaseLock;
|
|
8
|
+
exports.registerCleanupTask = registerCleanupTask;
|
|
9
|
+
const fs_1 = __importDefault(require("fs"));
|
|
10
|
+
const errors_1 = require("./errors");
|
|
11
|
+
const activeLocks = new Set();
|
|
12
|
+
const cleanupTasks = new Set();
|
|
13
|
+
function registerLock(lockFilePath) {
|
|
14
|
+
activeLocks.add(lockFilePath);
|
|
15
|
+
}
|
|
16
|
+
function releaseLock(lockFilePath) {
|
|
17
|
+
activeLocks.delete(lockFilePath);
|
|
18
|
+
try {
|
|
19
|
+
if (fs_1.default.existsSync(lockFilePath)) {
|
|
20
|
+
fs_1.default.unlinkSync(lockFilePath);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
catch (_) {
|
|
24
|
+
// Ignore cleanup errors
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
function registerCleanupTask(task) {
|
|
28
|
+
cleanupTasks.add(task);
|
|
29
|
+
}
|
|
30
|
+
function runCleanup() {
|
|
31
|
+
for (const lockFilePath of activeLocks) {
|
|
32
|
+
try {
|
|
33
|
+
if (fs_1.default.existsSync(lockFilePath)) {
|
|
34
|
+
fs_1.default.unlinkSync(lockFilePath);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
catch (_) {
|
|
38
|
+
// Ignore cleanup errors
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
activeLocks.clear();
|
|
42
|
+
for (const task of cleanupTasks) {
|
|
43
|
+
try {
|
|
44
|
+
task();
|
|
45
|
+
}
|
|
46
|
+
catch (_) {
|
|
47
|
+
// Ignore cleanup task errors
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
cleanupTasks.clear();
|
|
51
|
+
}
|
|
52
|
+
// Module load time registry
|
|
53
|
+
process.on('exit', runCleanup);
|
|
54
|
+
process.on('SIGINT', () => { runCleanup(); process.exit(0); });
|
|
55
|
+
process.on('SIGTERM', () => { runCleanup(); process.exit(0); });
|
|
56
|
+
process.on('SIGHUP', () => { runCleanup(); process.exit(0); });
|
|
57
|
+
process.on('uncaughtException', (err) => {
|
|
58
|
+
console.error(err);
|
|
59
|
+
runCleanup();
|
|
60
|
+
(0, errors_1.printFatal)('SERVER_CRASH', err.message);
|
|
61
|
+
});
|
package/dist/errors.js
CHANGED
|
@@ -10,6 +10,9 @@ exports.acquireLock = acquireLock;
|
|
|
10
10
|
const picocolors_1 = __importDefault(require("picocolors"));
|
|
11
11
|
const fs_1 = __importDefault(require("fs"));
|
|
12
12
|
const path_1 = __importDefault(require("path"));
|
|
13
|
+
const os_1 = __importDefault(require("os"));
|
|
14
|
+
const crypto_1 = __importDefault(require("crypto"));
|
|
15
|
+
const cleanup_1 = require("./cleanup");
|
|
13
16
|
const ERROR_MESSAGES = {
|
|
14
17
|
FILE_NOT_FOUND: { msg: 'Target JSON file not found.', hint: 'Check if the file path is correct.' },
|
|
15
18
|
FILE_NOT_JSON: { msg: 'Target file is not a JSON file.', hint: 'Ensure the file ends with .json.' },
|
|
@@ -68,7 +71,9 @@ function validateConfig(file, port) {
|
|
|
68
71
|
}
|
|
69
72
|
}
|
|
70
73
|
function acquireLock(file) {
|
|
71
|
-
const
|
|
74
|
+
const absoluteDbPath = path_1.default.resolve(file);
|
|
75
|
+
const hash = crypto_1.default.createHash('md5').update(absoluteDbPath).digest('hex');
|
|
76
|
+
const lockFilePath = path_1.default.join(os_1.default.tmpdir(), `zero-mock-${hash}.lock`);
|
|
72
77
|
if (fs_1.default.existsSync(lockFilePath)) {
|
|
73
78
|
try {
|
|
74
79
|
const lockData = fs_1.default.readFileSync(lockFilePath, 'utf8');
|
|
@@ -110,19 +115,5 @@ function acquireLock(file) {
|
|
|
110
115
|
printFatal('LOCK_WRITE_FAILED', e.message);
|
|
111
116
|
}
|
|
112
117
|
}
|
|
113
|
-
|
|
114
|
-
try {
|
|
115
|
-
if (fs_1.default.existsSync(lockFilePath)) {
|
|
116
|
-
fs_1.default.unlinkSync(lockFilePath);
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
catch (_) { /* ignore cleanup errors */ }
|
|
120
|
-
};
|
|
121
|
-
process.on('exit', cleanup);
|
|
122
|
-
process.on('SIGINT', () => { cleanup(); process.exit(130); });
|
|
123
|
-
process.on('SIGTERM', () => { cleanup(); process.exit(143); });
|
|
124
|
-
process.on('uncaughtException', (err) => {
|
|
125
|
-
cleanup();
|
|
126
|
-
printFatal('SERVER_CRASH', err.message);
|
|
127
|
-
});
|
|
118
|
+
(0, cleanup_1.registerLock)(lockFilePath);
|
|
128
119
|
}
|
package/dist/store/jsonStore.js
CHANGED
|
@@ -73,7 +73,7 @@ class JsonStoreImpl {
|
|
|
73
73
|
const run = async () => {
|
|
74
74
|
const target = this.backingPath;
|
|
75
75
|
const dir = (0, path_1.dirname)(target);
|
|
76
|
-
const tmp = (0, path_1.join)(dir, `.zero-mock-${(0, crypto_1.randomBytes)(8).toString("hex")}.
|
|
76
|
+
const tmp = (0, path_1.join)(dir, `.zero-mock-tmp-${(0, crypto_1.randomBytes)(8).toString("hex")}.json`);
|
|
77
77
|
const payload = `${JSON.stringify(this.data, null, 2)}\n`;
|
|
78
78
|
try {
|
|
79
79
|
await (0, promises_1.writeFile)(tmp, payload, "utf8");
|