@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
package/lib/Server.ts
ADDED
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import https, { type Server as HttpsServer } from 'node:https';
|
|
3
|
+
import { type Server as HttpServer } from 'node:http';
|
|
4
|
+
|
|
5
|
+
import { type Request, type Response } from 'express';
|
|
6
|
+
import { type Application } from 'express-ws';
|
|
7
|
+
import expressApp from './application.js';
|
|
8
|
+
import type Router from './Router.js';
|
|
9
|
+
import { type Config, type RuntimeConfig } from './types.js';
|
|
10
|
+
|
|
11
|
+
const DEFAULT_WATCH_FILE_DELAY = 5000;
|
|
12
|
+
|
|
13
|
+
export type HealthCheckFn = () => boolean | Promise<boolean>;
|
|
14
|
+
|
|
15
|
+
export default class Server {
|
|
16
|
+
private static activeServerByApp = new WeakMap<Application, Server>();
|
|
17
|
+
|
|
18
|
+
public config: Config;
|
|
19
|
+
public application: Application;
|
|
20
|
+
public httpServer?: HttpServer;
|
|
21
|
+
public httpsServer?: HttpsServer;
|
|
22
|
+
public healthy: boolean = true;
|
|
23
|
+
public isInitialized: boolean = false;
|
|
24
|
+
|
|
25
|
+
private healthCheck?: HealthCheckFn;
|
|
26
|
+
private initialLameDuckMode: boolean = false;
|
|
27
|
+
private watchedFilePath?: string;
|
|
28
|
+
private watchFileListener?: (curr: fs.Stats, prev: fs.Stats) => void;
|
|
29
|
+
|
|
30
|
+
get lameDuckMode(): boolean {
|
|
31
|
+
return Boolean(this.config.runtime?.lameDuckMode);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
set lameDuckMode(value: boolean) {
|
|
35
|
+
if (!this.config.runtime) {
|
|
36
|
+
this.config.runtime = {};
|
|
37
|
+
}
|
|
38
|
+
this.config.runtime.lameDuckMode = value;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
constructor(
|
|
42
|
+
config: Config,
|
|
43
|
+
application: Application = expressApp,
|
|
44
|
+
healthCheck?: HealthCheckFn
|
|
45
|
+
) {
|
|
46
|
+
this.config = config;
|
|
47
|
+
if (!this.config.runtime) {
|
|
48
|
+
this.config.runtime = {};
|
|
49
|
+
}
|
|
50
|
+
this.initialLameDuckMode = Boolean(this.config.runtime.lameDuckMode);
|
|
51
|
+
this.application = application;
|
|
52
|
+
this.healthCheck = healthCheck;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
private registerHealthzEndpoint(): void {
|
|
56
|
+
const alreadyRegistered = Server.activeServerByApp.has(this.application);
|
|
57
|
+
Server.activeServerByApp.set(this.application, this);
|
|
58
|
+
if (!alreadyRegistered) {
|
|
59
|
+
const app = this.application;
|
|
60
|
+
app.get('/healthz', (req: Request, res: Response) => {
|
|
61
|
+
const activeServer = Server.activeServerByApp.get(app);
|
|
62
|
+
if (activeServer) {
|
|
63
|
+
void activeServer.handleHealthz(req, res);
|
|
64
|
+
} else {
|
|
65
|
+
res.status(503).json({ status: 'unhealthy', lameDuckMode: false });
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
async isHealthy(): Promise<boolean> {
|
|
72
|
+
if (!this.isInitialized || !this.healthy) {
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
if (this.healthCheck) {
|
|
76
|
+
try {
|
|
77
|
+
return Boolean(await this.healthCheck());
|
|
78
|
+
} catch (error) {
|
|
79
|
+
console.error('Health check failed:', error);
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return true;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
async handleHealthz(_req: Request, res: Response): Promise<void> {
|
|
87
|
+
try {
|
|
88
|
+
if (this.lameDuckMode) {
|
|
89
|
+
res.status(503).json({ status: 'lame_duck', lameDuckMode: true });
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
const healthy = await this.isHealthy();
|
|
93
|
+
if (!healthy) {
|
|
94
|
+
res.status(503).json({ status: 'unhealthy', lameDuckMode: false });
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
res.status(200).json({ status: 'ok', lameDuckMode: false });
|
|
98
|
+
} catch (error) {
|
|
99
|
+
console.error('Health check error:', error);
|
|
100
|
+
res.status(503).json({ status: 'unhealthy', lameDuckMode: this.lameDuckMode });
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
evaluateWatchFile(watchFile?: string): void {
|
|
105
|
+
const filePath = watchFile ?? this.config.runtime?.WATCH_FILE;
|
|
106
|
+
if (!filePath) return;
|
|
107
|
+
|
|
108
|
+
try {
|
|
109
|
+
const raw = fs.readFileSync(filePath, 'utf-8');
|
|
110
|
+
const parsed = JSON.parse(raw) as Partial<RuntimeConfig>;
|
|
111
|
+
if (typeof parsed !== 'object' || parsed === null || Array.isArray(parsed)) {
|
|
112
|
+
console.error(`Watch file must contain a JSON object: ${filePath}`);
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
this.lameDuckMode = 'lameDuckMode' in parsed
|
|
116
|
+
? Boolean(parsed.lameDuckMode)
|
|
117
|
+
: this.initialLameDuckMode;
|
|
118
|
+
} catch (error: any) {
|
|
119
|
+
if (error?.code === 'ENOENT') {
|
|
120
|
+
console.warn(`Watch file not found: ${filePath}`);
|
|
121
|
+
this.lameDuckMode = this.initialLameDuckMode;
|
|
122
|
+
} else {
|
|
123
|
+
console.error(`Failed to evaluate watch file ${filePath}:`, error);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
private initializeRuntimeWatchFile(): void {
|
|
129
|
+
this.stopWatchingRuntimeFile();
|
|
130
|
+
const watchFile = this.config.runtime?.WATCH_FILE;
|
|
131
|
+
if (!watchFile) return;
|
|
132
|
+
|
|
133
|
+
this.evaluateWatchFile(watchFile);
|
|
134
|
+
|
|
135
|
+
const delay = this.config.runtime?.WATCH_FILE_DELAY ?? DEFAULT_WATCH_FILE_DELAY;
|
|
136
|
+
const listener = () => {
|
|
137
|
+
this.evaluateWatchFile(watchFile);
|
|
138
|
+
};
|
|
139
|
+
this.watchedFilePath = watchFile;
|
|
140
|
+
this.watchFileListener = listener;
|
|
141
|
+
fs.watchFile(watchFile, { persistent: false, interval: delay }, listener);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
stopWatchingRuntimeFile(): void {
|
|
145
|
+
if (this.watchedFilePath && this.watchFileListener) {
|
|
146
|
+
fs.unwatchFile(this.watchedFilePath, this.watchFileListener);
|
|
147
|
+
this.watchedFilePath = undefined;
|
|
148
|
+
this.watchFileListener = undefined;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
async start(router: Router): Promise<void> {
|
|
153
|
+
this.registerHealthzEndpoint();
|
|
154
|
+
this.initializeRuntimeWatchFile();
|
|
155
|
+
// TODO: Use logging middleware.
|
|
156
|
+
// Use the router middleware.
|
|
157
|
+
this.application.use(router.expressRouter);
|
|
158
|
+
const host = this.config.service.HOST || 'localhost';
|
|
159
|
+
const port = this.config.service.PORT || 3000;
|
|
160
|
+
|
|
161
|
+
const startHttp = 'ENABLE_HTTP' in this.config.service && !!this.config.service.ENABLE_HTTP;
|
|
162
|
+
const startHttps = !!this.config.service.ENABLE_HTTPS;
|
|
163
|
+
|
|
164
|
+
if (!startHttp && !startHttps) {
|
|
165
|
+
console.error("Either ENABLE_HTTPS or ENABLE_HTTP must be set for nails to start");
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
let atLeastOneServerStarted = false;
|
|
169
|
+
return new Promise<void>((resolve) => {
|
|
170
|
+
if (atLeastOneServerStarted) return resolve();
|
|
171
|
+
const serverStartedCallback = () => {
|
|
172
|
+
console.log("Started");
|
|
173
|
+
atLeastOneServerStarted = true;
|
|
174
|
+
resolve();
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
if (startHttp) {
|
|
178
|
+
console.log("starting nails HTTP server. listening to ", host + ':' + port);
|
|
179
|
+
if (this.httpServer) {
|
|
180
|
+
console.warn("Attempted to recreate http server");
|
|
181
|
+
} else {
|
|
182
|
+
this.httpServer = this.application.listen(port, host, serverStartedCallback);
|
|
183
|
+
console.log("Done starting HTTP server");
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
if (startHttps) {
|
|
187
|
+
if (this.httpsServer) {
|
|
188
|
+
console.warn("Attempted to recreate HTTPS server");
|
|
189
|
+
} else {
|
|
190
|
+
console.log(`starting nails HTTPS server. Listening to ${host}:${this.config.service.SSL_PORT}`);
|
|
191
|
+
this.httpsServer = https.createServer({
|
|
192
|
+
key: this.config.service.PRIVATE_KEY,
|
|
193
|
+
cert: this.config.service.CERTIFICATE
|
|
194
|
+
}, this.application).listen(this.config.service.SSL_PORT, serverStartedCallback);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
// TODO: Set a startup timeout and reject the promise so it does not hang forever;
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
async stop(): Promise<void> {
|
|
202
|
+
this.stopWatchingRuntimeFile();
|
|
203
|
+
const closePromises: Promise<void>[] = [];
|
|
204
|
+
if (this.httpServer) {
|
|
205
|
+
const server = this.httpServer;
|
|
206
|
+
this.httpServer = undefined;
|
|
207
|
+
closePromises.push(new Promise((resolve) => server.close(() => resolve())));
|
|
208
|
+
}
|
|
209
|
+
if (this.httpsServer) {
|
|
210
|
+
const server = this.httpsServer;
|
|
211
|
+
this.httpsServer = undefined;
|
|
212
|
+
closePromises.push(new Promise((resolve) => server.close(() => resolve())));
|
|
213
|
+
}
|
|
214
|
+
await Promise.all(closePromises);
|
|
215
|
+
}
|
|
216
|
+
}
|
package/lib/types.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {type Options } from "sequelize";
|
|
1
|
+
import { type Options } from "sequelize";
|
|
2
2
|
import { type NextFunction, type Request, type Response } from "express";
|
|
3
3
|
|
|
4
4
|
export interface ControllerDoRouteParams {
|
|
@@ -33,7 +33,7 @@ type HttpMethod = 'get' | 'post' | 'put' | 'delete' | 'ws';
|
|
|
33
33
|
|
|
34
34
|
export type RouteDefinition = [HttpMethod, RouteMatcher, RouteOptions?];
|
|
35
35
|
|
|
36
|
-
export interface RoutesConfig extends Array<RouteDefinition> {}
|
|
36
|
+
export interface RoutesConfig extends Array<RouteDefinition> { }
|
|
37
37
|
|
|
38
38
|
interface MimesConfig {
|
|
39
39
|
[key: string]: any;
|
|
@@ -46,6 +46,15 @@ export interface DbConfig {
|
|
|
46
46
|
options?: Options;
|
|
47
47
|
}
|
|
48
48
|
|
|
49
|
+
export interface RuntimeConfig {
|
|
50
|
+
WATCH_FILE?: string;
|
|
51
|
+
WATCH_FILE_DELAY?: number;
|
|
52
|
+
lameDuckMode?: boolean;
|
|
53
|
+
// TODO(Stanton): allow features to be rolled out to specific users/roles
|
|
54
|
+
// May want to put this in its own config file so feature resolution can be done dynamically.
|
|
55
|
+
// features?: Map<string, boolean>;
|
|
56
|
+
}
|
|
57
|
+
|
|
49
58
|
interface AppConfig {
|
|
50
59
|
APP_ROOT: string;
|
|
51
60
|
PUBLIC_ROOT: string;
|
|
@@ -71,4 +80,5 @@ export interface Config {
|
|
|
71
80
|
routes: RoutesConfig;
|
|
72
81
|
mimes: MimesConfig;
|
|
73
82
|
db: DbConfig;
|
|
83
|
+
runtime?: RuntimeConfig;
|
|
74
84
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@projectinvicta/nails",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.2.2",
|
|
4
4
|
"description": "A node.js webserver scaffold",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "index.js",
|
|
@@ -41,7 +41,6 @@
|
|
|
41
41
|
"react": "*",
|
|
42
42
|
"react-dom": "^16.13.1",
|
|
43
43
|
"sequelize": "^6.37.7",
|
|
44
|
-
"showdown": "^2.1.0",
|
|
45
44
|
"sqlite3": "^6.0.1",
|
|
46
45
|
"ws": "*"
|
|
47
46
|
},
|
|
@@ -51,6 +50,7 @@
|
|
|
51
50
|
"@types/express-ws": "*",
|
|
52
51
|
"@types/node": "*",
|
|
53
52
|
"chai-http": "*",
|
|
53
|
+
"showdown": "^2.1.0",
|
|
54
54
|
"typescript": "^6.0.2",
|
|
55
55
|
"vite": "^8.0.1",
|
|
56
56
|
"vitest": "^4.1.0"
|