@projectinvicta/nails 3.1.7 → 3.2.2
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/Nails.d.ts +13 -6
- package/dist/Nails.js +39 -56
- package/dist/Nails.js.map +1 -1
- package/dist/Server.d.ts +31 -0
- package/dist/Server.js +193 -0
- package/dist/Server.js.map +1 -0
- package/dist/types.d.ts +6 -0
- package/lib/Nails.ts +47 -50
- package/lib/Server.ts +216 -0
- package/lib/types.ts +12 -2
- package/package.json +2 -2
- package/templates/default/package-lock.json +2613 -5016
- package/templates/default/package.json +1 -1
- package/templates/mcp/package-lock.json +2830 -4758
- package/tests/healthz.spec.js +300 -0
- package/tests/nails.spec.js +5 -1
- package/tests/services/integration/config/db.ts +1 -1
- package/tests/services.integration.spec.js +5 -1
- package/vite.config.ts +1 -0
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import os from 'node:os';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { chai, describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
|
|
5
|
+
import { default as chaiHttp, request } from 'chai-http';
|
|
6
|
+
import Nails from '../lib/Nails.ts';
|
|
7
|
+
|
|
8
|
+
chai.use(chaiHttp);
|
|
9
|
+
|
|
10
|
+
const makeHealthzRequest = (app) => {
|
|
11
|
+
return new Promise((resolve, reject) => {
|
|
12
|
+
request.execute(app)
|
|
13
|
+
.get('/healthz')
|
|
14
|
+
.end((err, res) => {
|
|
15
|
+
if (err && !res) return reject(err);
|
|
16
|
+
resolve(res);
|
|
17
|
+
});
|
|
18
|
+
});
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const createBaseConfig = (runtime = {}, port = 3456) => {
|
|
22
|
+
const tmpRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'nails-healthz-app-'));
|
|
23
|
+
const controllersDir = path.join(tmpRoot, 'controllers');
|
|
24
|
+
const modelsDir = path.join(tmpRoot, 'models');
|
|
25
|
+
const viewsDir = path.join(tmpRoot, 'views');
|
|
26
|
+
const publicDir = path.join(tmpRoot, 'public');
|
|
27
|
+
fs.mkdirSync(controllersDir, { recursive: true });
|
|
28
|
+
fs.mkdirSync(modelsDir, { recursive: true });
|
|
29
|
+
fs.mkdirSync(viewsDir, { recursive: true });
|
|
30
|
+
fs.mkdirSync(publicDir, { recursive: true });
|
|
31
|
+
|
|
32
|
+
return {
|
|
33
|
+
tmpRoot,
|
|
34
|
+
config: {
|
|
35
|
+
service: {
|
|
36
|
+
APP_ROOT: tmpRoot,
|
|
37
|
+
PUBLIC_ROOT: publicDir,
|
|
38
|
+
CONTROLLERS_ROOT: controllersDir,
|
|
39
|
+
VIEWS_ROOT: viewsDir,
|
|
40
|
+
MODELS_ROOT: modelsDir,
|
|
41
|
+
SERVER_ROOT: tmpRoot,
|
|
42
|
+
ENABLE_HTTP: true,
|
|
43
|
+
HOST: '127.0.0.1',
|
|
44
|
+
PORT: port,
|
|
45
|
+
ENABLE_HTTPS: false,
|
|
46
|
+
SSL_PORT: 0,
|
|
47
|
+
PRIVATE_KEY: Buffer.alloc(0),
|
|
48
|
+
CERTIFICATE: Buffer.alloc(0),
|
|
49
|
+
},
|
|
50
|
+
routes: [
|
|
51
|
+
['get', '/:catchall', { controller: 'home' }],
|
|
52
|
+
],
|
|
53
|
+
mimes: {},
|
|
54
|
+
db: {
|
|
55
|
+
address: 'sqlite:///:memory:',
|
|
56
|
+
options: { logging: false },
|
|
57
|
+
},
|
|
58
|
+
runtime,
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
const waitForCondition = async (predicate, timeoutMs = 2000, intervalMs = 25) => {
|
|
64
|
+
const start = Date.now();
|
|
65
|
+
while (Date.now() - start < timeoutMs) {
|
|
66
|
+
if (await predicate()) return;
|
|
67
|
+
await new Promise((r) => setTimeout(r, intervalMs));
|
|
68
|
+
}
|
|
69
|
+
throw new Error('Timed out waiting for condition');
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
describe('RuntimeConfig, WATCH_FILE, and /healthz', () => {
|
|
73
|
+
let activeNails = null;
|
|
74
|
+
let cleanupDirs = [];
|
|
75
|
+
let nextPort = 34560;
|
|
76
|
+
|
|
77
|
+
beforeEach(() => {
|
|
78
|
+
activeNails = null;
|
|
79
|
+
cleanupDirs = [];
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
afterEach(async () => {
|
|
83
|
+
if (activeNails) {
|
|
84
|
+
await activeNails.server.stop();
|
|
85
|
+
if (activeNails.sequelize) {
|
|
86
|
+
await activeNails.sequelize.close();
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
for (const dir of cleanupDirs) {
|
|
90
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it('returns 200 on /healthz when healthy and not in lame duck mode (even with catchall route)', async () => {
|
|
95
|
+
const { tmpRoot, config } = createBaseConfig({}, nextPort++);
|
|
96
|
+
cleanupDirs.push(tmpRoot);
|
|
97
|
+
|
|
98
|
+
activeNails = new Nails(config);
|
|
99
|
+
await activeNails.startServer();
|
|
100
|
+
|
|
101
|
+
const res = await makeHealthzRequest(activeNails.application);
|
|
102
|
+
expect(res.status).toBe(200);
|
|
103
|
+
expect(res.body).toEqual({ status: 'ok', lameDuckMode: false });
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it('returns 503 on /healthz when static runtime.lameDuckMode is true', async () => {
|
|
107
|
+
const { tmpRoot, config } = createBaseConfig({ lameDuckMode: true }, nextPort++);
|
|
108
|
+
cleanupDirs.push(tmpRoot);
|
|
109
|
+
|
|
110
|
+
activeNails = new Nails(config);
|
|
111
|
+
await activeNails.startServer();
|
|
112
|
+
|
|
113
|
+
expect(activeNails.lameDuckMode).toBe(true);
|
|
114
|
+
const res = await makeHealthzRequest(activeNails.application);
|
|
115
|
+
expect(res.status).toBe(503);
|
|
116
|
+
expect(res.body).toEqual({ status: 'lame_duck', lameDuckMode: true });
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it('evaluates WATCH_FILE as JSON on startServer() and extracts lameDuckMode', async () => {
|
|
120
|
+
const { tmpRoot, config } = createBaseConfig({}, nextPort++);
|
|
121
|
+
cleanupDirs.push(tmpRoot);
|
|
122
|
+
|
|
123
|
+
const watchFile = path.join(tmpRoot, 'runtime.json');
|
|
124
|
+
fs.writeFileSync(watchFile, JSON.stringify({ lameDuckMode: true }));
|
|
125
|
+
config.runtime = { WATCH_FILE: watchFile, WATCH_FILE_DELAY: 50 };
|
|
126
|
+
|
|
127
|
+
activeNails = new Nails(config);
|
|
128
|
+
await activeNails.startServer();
|
|
129
|
+
|
|
130
|
+
expect(activeNails.lameDuckMode).toBe(true);
|
|
131
|
+
expect(activeNails.config.runtime.lameDuckMode).toBe(true);
|
|
132
|
+
|
|
133
|
+
const res = await makeHealthzRequest(activeNails.application);
|
|
134
|
+
expect(res.status).toBe(503);
|
|
135
|
+
expect(res.body).toEqual({ status: 'lame_duck', lameDuckMode: true });
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it('dynamically updates lameDuckMode and /healthz when WATCH_FILE changes', async () => {
|
|
139
|
+
const { tmpRoot, config } = createBaseConfig({}, nextPort++);
|
|
140
|
+
cleanupDirs.push(tmpRoot);
|
|
141
|
+
|
|
142
|
+
const watchFile = path.join(tmpRoot, 'runtime.json');
|
|
143
|
+
fs.writeFileSync(watchFile, JSON.stringify({ lameDuckMode: false }));
|
|
144
|
+
config.runtime = { WATCH_FILE: watchFile, WATCH_FILE_DELAY: 50 };
|
|
145
|
+
|
|
146
|
+
activeNails = new Nails(config);
|
|
147
|
+
await activeNails.startServer();
|
|
148
|
+
|
|
149
|
+
let res = await makeHealthzRequest(activeNails.application);
|
|
150
|
+
expect(res.status).toBe(200);
|
|
151
|
+
|
|
152
|
+
// Wait briefly so fs.watchFile's initial stat completes before modifying
|
|
153
|
+
await new Promise((r) => setTimeout(r, 100));
|
|
154
|
+
fs.writeFileSync(watchFile, JSON.stringify({ lameDuckMode: true }));
|
|
155
|
+
|
|
156
|
+
await waitForCondition(() => activeNails.lameDuckMode === true);
|
|
157
|
+
res = await makeHealthzRequest(activeNails.application);
|
|
158
|
+
expect(res.status).toBe(503);
|
|
159
|
+
|
|
160
|
+
// Toggle back to false
|
|
161
|
+
await new Promise((r) => setTimeout(r, 100));
|
|
162
|
+
fs.writeFileSync(watchFile, JSON.stringify({ lameDuckMode: false }));
|
|
163
|
+
|
|
164
|
+
await waitForCondition(() => activeNails.lameDuckMode === false);
|
|
165
|
+
res = await makeHealthzRequest(activeNails.application);
|
|
166
|
+
expect(res.status).toBe(200);
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
it('handles missing WATCH_FILE gracefully and reacts when created and deleted', async () => {
|
|
170
|
+
const { tmpRoot, config } = createBaseConfig({}, nextPort++);
|
|
171
|
+
cleanupDirs.push(tmpRoot);
|
|
172
|
+
|
|
173
|
+
const watchFile = path.join(tmpRoot, 'does-not-exist-yet.json');
|
|
174
|
+
config.runtime = { WATCH_FILE: watchFile, WATCH_FILE_DELAY: 50 };
|
|
175
|
+
|
|
176
|
+
activeNails = new Nails(config);
|
|
177
|
+
await activeNails.startServer();
|
|
178
|
+
|
|
179
|
+
let res = await makeHealthzRequest(activeNails.application);
|
|
180
|
+
expect(res.status).toBe(200);
|
|
181
|
+
|
|
182
|
+
// Create the file with lameDuckMode: true
|
|
183
|
+
await new Promise((r) => setTimeout(r, 100));
|
|
184
|
+
fs.writeFileSync(watchFile, JSON.stringify({ lameDuckMode: true }));
|
|
185
|
+
|
|
186
|
+
await waitForCondition(() => activeNails.lameDuckMode === true);
|
|
187
|
+
res = await makeHealthzRequest(activeNails.application);
|
|
188
|
+
expect(res.status).toBe(503);
|
|
189
|
+
|
|
190
|
+
// Delete the file -> returns to initial lameDuckMode (false)
|
|
191
|
+
await new Promise((r) => setTimeout(r, 100));
|
|
192
|
+
fs.unlinkSync(watchFile);
|
|
193
|
+
|
|
194
|
+
await waitForCondition(() => activeNails.lameDuckMode === false);
|
|
195
|
+
res = await makeHealthzRequest(activeNails.application);
|
|
196
|
+
expect(res.status).toBe(200);
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
it('preserves current lameDuckMode and does not crash on invalid JSON in WATCH_FILE', async () => {
|
|
200
|
+
const { tmpRoot, config } = createBaseConfig({}, nextPort++);
|
|
201
|
+
cleanupDirs.push(tmpRoot);
|
|
202
|
+
|
|
203
|
+
const watchFile = path.join(tmpRoot, 'runtime.json');
|
|
204
|
+
fs.writeFileSync(watchFile, JSON.stringify({ lameDuckMode: true }));
|
|
205
|
+
config.runtime = { WATCH_FILE: watchFile, WATCH_FILE_DELAY: 50 };
|
|
206
|
+
|
|
207
|
+
const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
|
|
208
|
+
try {
|
|
209
|
+
activeNails = new Nails(config);
|
|
210
|
+
await activeNails.startServer();
|
|
211
|
+
|
|
212
|
+
expect(activeNails.lameDuckMode).toBe(true);
|
|
213
|
+
|
|
214
|
+
fs.writeFileSync(watchFile, '{ invalid json');
|
|
215
|
+
activeNails.server.evaluateWatchFile();
|
|
216
|
+
|
|
217
|
+
expect(activeNails.lameDuckMode).toBe(true);
|
|
218
|
+
const res = await makeHealthzRequest(activeNails.application);
|
|
219
|
+
expect(res.status).toBe(503);
|
|
220
|
+
} finally {
|
|
221
|
+
errorSpy.mockRestore();
|
|
222
|
+
}
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
it('returns 503 on /healthz when service is marked unhealthy', async () => {
|
|
226
|
+
const { tmpRoot, config } = createBaseConfig({}, nextPort++);
|
|
227
|
+
cleanupDirs.push(tmpRoot);
|
|
228
|
+
|
|
229
|
+
activeNails = new Nails(config);
|
|
230
|
+
await activeNails.startServer();
|
|
231
|
+
|
|
232
|
+
activeNails.healthy = false;
|
|
233
|
+
const res = await makeHealthzRequest(activeNails.application);
|
|
234
|
+
expect(res.status).toBe(503);
|
|
235
|
+
expect(res.body).toEqual({ status: 'unhealthy', lameDuckMode: false });
|
|
236
|
+
|
|
237
|
+
activeNails.healthy = true;
|
|
238
|
+
const res2 = await makeHealthzRequest(activeNails.application);
|
|
239
|
+
expect(res2.status).toBe(200);
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
it('returns 503 on /healthz when database health check fails', async () => {
|
|
243
|
+
const { tmpRoot, config } = createBaseConfig({}, nextPort++);
|
|
244
|
+
cleanupDirs.push(tmpRoot);
|
|
245
|
+
|
|
246
|
+
activeNails = new Nails(config);
|
|
247
|
+
await activeNails.startServer();
|
|
248
|
+
|
|
249
|
+
const authSpy = vi.spyOn(activeNails.sequelize, 'authenticate')
|
|
250
|
+
.mockRejectedValueOnce(new Error('DB connection lost'));
|
|
251
|
+
const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
|
|
252
|
+
|
|
253
|
+
try {
|
|
254
|
+
const res = await makeHealthzRequest(activeNails.application);
|
|
255
|
+
expect(res.status).toBe(503);
|
|
256
|
+
expect(res.body).toEqual({ status: 'unhealthy', lameDuckMode: false });
|
|
257
|
+
} finally {
|
|
258
|
+
authSpy.mockRestore();
|
|
259
|
+
errorSpy.mockRestore();
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
const resHealthy = await makeHealthzRequest(activeNails.application);
|
|
263
|
+
expect(resHealthy.status).toBe(200);
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
it('returns 503 on /healthz before initialization completes', async () => {
|
|
267
|
+
const { tmpRoot, config } = createBaseConfig({}, nextPort++);
|
|
268
|
+
cleanupDirs.push(tmpRoot);
|
|
269
|
+
|
|
270
|
+
let releaseInit;
|
|
271
|
+
const gate = new Promise((resolve) => {
|
|
272
|
+
releaseInit = resolve;
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
const originalInitModels = Nails.prototype.initializeModels;
|
|
276
|
+
const initModelsSpy = vi.spyOn(Nails.prototype, 'initializeModels').mockImplementation(async function () {
|
|
277
|
+
await gate;
|
|
278
|
+
return originalInitModels.call(this);
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
try {
|
|
282
|
+
activeNails = new Nails(config);
|
|
283
|
+
await activeNails.server.start(activeNails.router);
|
|
284
|
+
|
|
285
|
+
// While configure() is blocked on gate, /healthz must return 503
|
|
286
|
+
const earlyRes = await makeHealthzRequest(activeNails.application);
|
|
287
|
+
expect(earlyRes.status).toBe(503);
|
|
288
|
+
expect(earlyRes.body).toEqual({ status: 'unhealthy', lameDuckMode: false });
|
|
289
|
+
|
|
290
|
+
releaseInit();
|
|
291
|
+
await activeNails.initialized;
|
|
292
|
+
|
|
293
|
+
const readyRes = await makeHealthzRequest(activeNails.application);
|
|
294
|
+
expect(readyRes.status).toBe(200);
|
|
295
|
+
expect(readyRes.body).toEqual({ status: 'ok', lameDuckMode: false });
|
|
296
|
+
} finally {
|
|
297
|
+
initModelsSpy.mockRestore();
|
|
298
|
+
}
|
|
299
|
+
});
|
|
300
|
+
});
|
package/tests/nails.spec.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// TODO test all initialization here
|
|
2
|
-
import { describe, beforeAll, it, test, expect, vi } from 'vitest';
|
|
2
|
+
import { describe, beforeAll, afterAll, it, test, expect, vi } from 'vitest';
|
|
3
3
|
import Dog from './services/integration/server/models/dog.js';
|
|
4
4
|
import Owner from './services/integration/server/models/owner.js';
|
|
5
5
|
|
|
@@ -9,6 +9,10 @@ beforeAll(async () => {
|
|
|
9
9
|
nails = (await import('./services/integration/server.js')).default;
|
|
10
10
|
});
|
|
11
11
|
|
|
12
|
+
afterAll(async () => {
|
|
13
|
+
await nails?.server?.stop();
|
|
14
|
+
});
|
|
15
|
+
|
|
12
16
|
test("finalize is called during model initialization", async () => {
|
|
13
17
|
const owner = await Owner.create({});
|
|
14
18
|
const dog = await owner.createDog({name: "Fido"});
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { chai, describe, it, expect, beforeAll } from 'vitest';
|
|
1
|
+
import { chai, describe, it, expect, beforeAll, afterAll } from 'vitest';
|
|
2
2
|
import {default as chaiHttp, request} from 'chai-http';
|
|
3
3
|
import WebSocket from 'ws';
|
|
4
4
|
import Owner from "./services/integration/server/models/owner.js";
|
|
@@ -27,6 +27,10 @@ describe("Integration: Sequelize", () => {
|
|
|
27
27
|
express_app = nails.application;
|
|
28
28
|
});
|
|
29
29
|
|
|
30
|
+
afterAll(async () => {
|
|
31
|
+
await nails?.server?.stop();
|
|
32
|
+
});
|
|
33
|
+
|
|
30
34
|
it("Can directly access models", async () => {
|
|
31
35
|
const owners = await Owner.findAll();
|
|
32
36
|
});
|