@naman_deep_singh/server-utils 1.0.2 → 1.0.4
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/server.js +12 -12
- package/dist/shutdown.d.ts +1 -1
- package/dist/shutdown.js +15 -8
- package/dist/types.d.ts +2 -0
- package/package.json +3 -3
- package/src/server.ts +12 -12
- package/src/shutdown.ts +17 -8
- package/src/types.ts +2 -0
package/dist/server.js
CHANGED
|
@@ -39,7 +39,7 @@ class ExpressServer {
|
|
|
39
39
|
this.app.use(cors(corsOptions));
|
|
40
40
|
}
|
|
41
41
|
catch (error) {
|
|
42
|
-
console.warn(
|
|
42
|
+
console.warn(`${this.config.name}: CORS middleware not available. Install cors package.`);
|
|
43
43
|
}
|
|
44
44
|
}
|
|
45
45
|
// Apply Helmet if enabled
|
|
@@ -49,7 +49,7 @@ class ExpressServer {
|
|
|
49
49
|
this.app.use(helmet());
|
|
50
50
|
}
|
|
51
51
|
catch (error) {
|
|
52
|
-
console.warn(
|
|
52
|
+
console.warn(`${this.config.name}: Helmet middleware not available. Install helmet package.`);
|
|
53
53
|
}
|
|
54
54
|
}
|
|
55
55
|
// Apply JSON parser if enabled
|
|
@@ -146,11 +146,11 @@ class ExpressServer {
|
|
|
146
146
|
});
|
|
147
147
|
this.grpcServer.bindAsync(`0.0.0.0:${port}`, grpc.ServerCredentials.createInsecure(), () => {
|
|
148
148
|
this.grpcServer.start();
|
|
149
|
-
console.log(`🔗 gRPC server running on port ${port}`);
|
|
149
|
+
console.log(`🔗 ${this.config.name} gRPC server running on port ${port}`);
|
|
150
150
|
});
|
|
151
151
|
}
|
|
152
152
|
catch (error) {
|
|
153
|
-
console.warn(
|
|
153
|
+
console.warn(`${this.config.name}: gRPC not available. Install @grpc/grpc-js to use gRPC features.`);
|
|
154
154
|
}
|
|
155
155
|
}
|
|
156
156
|
}
|
|
@@ -161,10 +161,10 @@ class ExpressServer {
|
|
|
161
161
|
const jayson = require('jayson');
|
|
162
162
|
const rpcServer = jayson.server(this.rpcMethods);
|
|
163
163
|
this.app.use(path, rpcServer.middleware());
|
|
164
|
-
console.log(`📡 JSON-RPC server mounted on ${path}`);
|
|
164
|
+
console.log(`📡 ${this.config.name} JSON-RPC server mounted on ${path}`);
|
|
165
165
|
}
|
|
166
166
|
catch (error) {
|
|
167
|
-
console.warn(
|
|
167
|
+
console.warn(`${this.config.name}: JSON-RPC not available. Install jayson to use RPC features.`);
|
|
168
168
|
}
|
|
169
169
|
}
|
|
170
170
|
addWebhook(config) {
|
|
@@ -195,11 +195,11 @@ class ExpressServer {
|
|
|
195
195
|
res.status(500).json({ error: 'Webhook processing failed' });
|
|
196
196
|
}
|
|
197
197
|
});
|
|
198
|
-
console.log(`🪝
|
|
198
|
+
console.log(`🪝 ${this.config.name} webhook registered at ${config.path}${config.secret ? ' (with signature verification)' : ''}`);
|
|
199
199
|
}
|
|
200
200
|
addSocketIO(config = {}) {
|
|
201
201
|
if (!this.server) {
|
|
202
|
-
throw new Error(
|
|
202
|
+
throw new Error(`${this.config.name}: Server must be started before adding Socket.IO`);
|
|
203
203
|
}
|
|
204
204
|
try {
|
|
205
205
|
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
@@ -218,25 +218,25 @@ class ExpressServer {
|
|
|
218
218
|
// Handle connections
|
|
219
219
|
io.on('connection', (socket) => {
|
|
220
220
|
const typedSocket = socket;
|
|
221
|
-
console.log(`🔌 Socket connected
|
|
221
|
+
console.log(`🔌 ${this.config.name}: Socket connected [${typedSocket.id}]`);
|
|
222
222
|
// Call user-defined connection handler
|
|
223
223
|
if (config.onConnection) {
|
|
224
224
|
config.onConnection(socket);
|
|
225
225
|
}
|
|
226
226
|
// Handle disconnection
|
|
227
227
|
typedSocket.on('disconnect', (reason) => {
|
|
228
|
-
console.log(`🔌 Socket disconnected
|
|
228
|
+
console.log(`🔌 ${this.config.name}: Socket disconnected [${typedSocket.id}] - ${reason}`);
|
|
229
229
|
// Call user-defined disconnection handler
|
|
230
230
|
if (config.onDisconnection) {
|
|
231
231
|
config.onDisconnection(socket, reason);
|
|
232
232
|
}
|
|
233
233
|
});
|
|
234
234
|
});
|
|
235
|
-
console.log(`🔌 Socket.IO server attached${config.path ? ` at ${config.path}` : ''}`);
|
|
235
|
+
console.log(`🔌 ${this.config.name} Socket.IO server attached${config.path ? ` at ${config.path}` : ''}${config.cors ? ' (CORS enabled)' : ''}`);
|
|
236
236
|
return io;
|
|
237
237
|
}
|
|
238
238
|
catch (error) {
|
|
239
|
-
console.warn(
|
|
239
|
+
console.warn(`${this.config.name}: Socket.IO not available. Install socket.io to use WebSocket features.`);
|
|
240
240
|
return null;
|
|
241
241
|
}
|
|
242
242
|
}
|
package/dist/shutdown.d.ts
CHANGED
|
@@ -2,4 +2,4 @@ import { Server } from 'http';
|
|
|
2
2
|
import { GracefulShutdownConfig, ServerPlugin } from './types';
|
|
3
3
|
export declare function createGracefulShutdown(server: Server, config?: GracefulShutdownConfig): void;
|
|
4
4
|
export declare function withGracefulShutdown(config?: GracefulShutdownConfig): ServerPlugin;
|
|
5
|
-
export declare function startServerWithShutdown(app: any, port: number, shutdownConfig?: GracefulShutdownConfig): Server;
|
|
5
|
+
export declare function startServerWithShutdown(app: any, port: number, shutdownConfig?: GracefulShutdownConfig, serverName?: string, serverVersion?: string): Server;
|
package/dist/shutdown.js
CHANGED
|
@@ -4,11 +4,12 @@ exports.createGracefulShutdown = createGracefulShutdown;
|
|
|
4
4
|
exports.withGracefulShutdown = withGracefulShutdown;
|
|
5
5
|
exports.startServerWithShutdown = startServerWithShutdown;
|
|
6
6
|
function createGracefulShutdown(server, config = {}) {
|
|
7
|
-
const { timeout = 10000, onShutdown } = config;
|
|
7
|
+
const { timeout = 10000, onShutdown, serverName, serverVersion } = config;
|
|
8
|
+
const nameVersion = serverName && serverVersion ? `${serverName} v${serverVersion}` : 'Server';
|
|
8
9
|
const shutdown = async (signal) => {
|
|
9
|
-
console.log(`🛑
|
|
10
|
+
console.log(`🛑 ${nameVersion} received ${signal}, shutting down gracefully...`);
|
|
10
11
|
const shutdownTimer = setTimeout(() => {
|
|
11
|
-
console.log(
|
|
12
|
+
console.log(`⏰ ${nameVersion} shutdown timeout reached, forcing exit`);
|
|
12
13
|
process.exit(1);
|
|
13
14
|
}, timeout);
|
|
14
15
|
try {
|
|
@@ -19,13 +20,13 @@ function createGracefulShutdown(server, config = {}) {
|
|
|
19
20
|
// Close server
|
|
20
21
|
server.close(() => {
|
|
21
22
|
clearTimeout(shutdownTimer);
|
|
22
|
-
console.log(
|
|
23
|
+
console.log(`👋 ${nameVersion} closed. Exiting now.`);
|
|
23
24
|
process.exit(0);
|
|
24
25
|
});
|
|
25
26
|
}
|
|
26
27
|
catch (error) {
|
|
27
28
|
clearTimeout(shutdownTimer);
|
|
28
|
-
console.error(
|
|
29
|
+
console.error(`❌ ${nameVersion} error during shutdown:`, error);
|
|
29
30
|
process.exit(1);
|
|
30
31
|
}
|
|
31
32
|
};
|
|
@@ -39,12 +40,18 @@ function withGracefulShutdown(config = {}) {
|
|
|
39
40
|
app.__gracefulShutdownConfig = config;
|
|
40
41
|
};
|
|
41
42
|
}
|
|
42
|
-
function startServerWithShutdown(app, port, shutdownConfig = {}) {
|
|
43
|
+
function startServerWithShutdown(app, port, shutdownConfig = {}, serverName, serverVersion) {
|
|
43
44
|
const server = app.listen(port, () => {
|
|
44
|
-
|
|
45
|
+
const nameVersion = serverName && serverVersion ? `${serverName} v${serverVersion}` : 'Server';
|
|
46
|
+
console.log(`🚀 ${nameVersion} running on http://localhost:${port}`);
|
|
45
47
|
});
|
|
46
48
|
// Apply graceful shutdown from stored config or provided config
|
|
47
49
|
const config = app.__gracefulShutdownConfig || shutdownConfig;
|
|
48
|
-
|
|
50
|
+
const enhancedConfig = {
|
|
51
|
+
...config,
|
|
52
|
+
serverName,
|
|
53
|
+
serverVersion
|
|
54
|
+
};
|
|
55
|
+
createGracefulShutdown(server, enhancedConfig);
|
|
49
56
|
return server;
|
|
50
57
|
}
|
package/dist/types.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@naman_deep_singh/server-utils",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.4",
|
|
4
4
|
"description": "Extensible server utilities for Express.js microservices with TypeScript",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -18,10 +18,10 @@
|
|
|
18
18
|
"license": "ISC",
|
|
19
19
|
"packageManager": "pnpm@10.20.0",
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"express": "^
|
|
21
|
+
"express": "^5.1.0"
|
|
22
22
|
},
|
|
23
23
|
"devDependencies": {
|
|
24
|
-
"@types/express": "^
|
|
24
|
+
"@types/express": "^5.0.5",
|
|
25
25
|
"typescript": "^5.9.3"
|
|
26
26
|
}
|
|
27
27
|
}
|
package/src/server.ts
CHANGED
|
@@ -101,7 +101,7 @@ export class ExpressServer implements ServerInstance {
|
|
|
101
101
|
const corsOptions = typeof this.config.cors === 'object' ? this.config.cors : undefined;
|
|
102
102
|
this.app.use(cors(corsOptions));
|
|
103
103
|
} catch (error) {
|
|
104
|
-
console.warn(
|
|
104
|
+
console.warn(`${this.config.name}: CORS middleware not available. Install cors package.`);
|
|
105
105
|
}
|
|
106
106
|
}
|
|
107
107
|
|
|
@@ -111,7 +111,7 @@ export class ExpressServer implements ServerInstance {
|
|
|
111
111
|
const helmet = require('helmet');
|
|
112
112
|
this.app.use(helmet());
|
|
113
113
|
} catch (error) {
|
|
114
|
-
console.warn(
|
|
114
|
+
console.warn(`${this.config.name}: Helmet middleware not available. Install helmet package.`);
|
|
115
115
|
}
|
|
116
116
|
}
|
|
117
117
|
|
|
@@ -236,11 +236,11 @@ export class ExpressServer implements ServerInstance {
|
|
|
236
236
|
grpc.ServerCredentials.createInsecure(),
|
|
237
237
|
() => {
|
|
238
238
|
this.grpcServer!.start();
|
|
239
|
-
console.log(`🔗 gRPC server running on port ${port}`);
|
|
239
|
+
console.log(`🔗 ${this.config.name} gRPC server running on port ${port}`);
|
|
240
240
|
}
|
|
241
241
|
);
|
|
242
242
|
} catch (error: unknown) {
|
|
243
|
-
console.warn(
|
|
243
|
+
console.warn(`${this.config.name}: gRPC not available. Install @grpc/grpc-js to use gRPC features.`);
|
|
244
244
|
}
|
|
245
245
|
}
|
|
246
246
|
}
|
|
@@ -257,9 +257,9 @@ export class ExpressServer implements ServerInstance {
|
|
|
257
257
|
};
|
|
258
258
|
const rpcServer = jayson.server(this.rpcMethods);
|
|
259
259
|
this.app.use(path, rpcServer.middleware());
|
|
260
|
-
console.log(`📡 JSON-RPC server mounted on ${path}`);
|
|
260
|
+
console.log(`📡 ${this.config.name} JSON-RPC server mounted on ${path}`);
|
|
261
261
|
} catch (error: unknown) {
|
|
262
|
-
console.warn(
|
|
262
|
+
console.warn(`${this.config.name}: JSON-RPC not available. Install jayson to use RPC features.`);
|
|
263
263
|
}
|
|
264
264
|
}
|
|
265
265
|
|
|
@@ -295,12 +295,12 @@ export class ExpressServer implements ServerInstance {
|
|
|
295
295
|
}
|
|
296
296
|
});
|
|
297
297
|
|
|
298
|
-
console.log(`🪝
|
|
298
|
+
console.log(`🪝 ${this.config.name} webhook registered at ${config.path}${config.secret ? ' (with signature verification)' : ''}`);
|
|
299
299
|
}
|
|
300
300
|
|
|
301
301
|
addSocketIO(config: SocketIOConfig = {}): unknown {
|
|
302
302
|
if (!this.server) {
|
|
303
|
-
throw new Error(
|
|
303
|
+
throw new Error(`${this.config.name}: Server must be started before adding Socket.IO`);
|
|
304
304
|
}
|
|
305
305
|
|
|
306
306
|
try {
|
|
@@ -336,7 +336,7 @@ export class ExpressServer implements ServerInstance {
|
|
|
336
336
|
// Handle connections
|
|
337
337
|
io.on('connection', (socket: unknown) => {
|
|
338
338
|
const typedSocket = socket as SocketInstance;
|
|
339
|
-
console.log(`🔌 Socket connected
|
|
339
|
+
console.log(`🔌 ${this.config.name}: Socket connected [${typedSocket.id}]`);
|
|
340
340
|
|
|
341
341
|
// Call user-defined connection handler
|
|
342
342
|
if (config.onConnection) {
|
|
@@ -345,7 +345,7 @@ export class ExpressServer implements ServerInstance {
|
|
|
345
345
|
|
|
346
346
|
// Handle disconnection
|
|
347
347
|
typedSocket.on('disconnect', (reason) => {
|
|
348
|
-
console.log(`🔌 Socket disconnected
|
|
348
|
+
console.log(`🔌 ${this.config.name}: Socket disconnected [${typedSocket.id}] - ${reason}`);
|
|
349
349
|
|
|
350
350
|
// Call user-defined disconnection handler
|
|
351
351
|
if (config.onDisconnection) {
|
|
@@ -354,10 +354,10 @@ export class ExpressServer implements ServerInstance {
|
|
|
354
354
|
});
|
|
355
355
|
});
|
|
356
356
|
|
|
357
|
-
console.log(`🔌 Socket.IO server attached${config.path ? ` at ${config.path}` : ''}`);
|
|
357
|
+
console.log(`🔌 ${this.config.name} Socket.IO server attached${config.path ? ` at ${config.path}` : ''}${config.cors ? ' (CORS enabled)' : ''}`);
|
|
358
358
|
return io;
|
|
359
359
|
} catch (error: unknown) {
|
|
360
|
-
console.warn(
|
|
360
|
+
console.warn(`${this.config.name}: Socket.IO not available. Install socket.io to use WebSocket features.`);
|
|
361
361
|
return null;
|
|
362
362
|
}
|
|
363
363
|
}
|
package/src/shutdown.ts
CHANGED
|
@@ -2,13 +2,14 @@ import { Server } from 'http';
|
|
|
2
2
|
import { GracefulShutdownConfig, ServerPlugin } from './types';
|
|
3
3
|
|
|
4
4
|
export function createGracefulShutdown(server: Server, config: GracefulShutdownConfig = {}): void {
|
|
5
|
-
const { timeout = 10000, onShutdown } = config;
|
|
5
|
+
const { timeout = 10000, onShutdown, serverName, serverVersion } = config;
|
|
6
|
+
const nameVersion = serverName && serverVersion ? `${serverName} v${serverVersion}` : 'Server';
|
|
6
7
|
|
|
7
8
|
const shutdown = async (signal: string) => {
|
|
8
|
-
console.log(`🛑
|
|
9
|
+
console.log(`🛑 ${nameVersion} received ${signal}, shutting down gracefully...`);
|
|
9
10
|
|
|
10
11
|
const shutdownTimer = setTimeout(() => {
|
|
11
|
-
console.log(
|
|
12
|
+
console.log(`⏰ ${nameVersion} shutdown timeout reached, forcing exit`);
|
|
12
13
|
process.exit(1);
|
|
13
14
|
}, timeout);
|
|
14
15
|
|
|
@@ -21,12 +22,12 @@ export function createGracefulShutdown(server: Server, config: GracefulShutdownC
|
|
|
21
22
|
// Close server
|
|
22
23
|
server.close(() => {
|
|
23
24
|
clearTimeout(shutdownTimer);
|
|
24
|
-
console.log(
|
|
25
|
+
console.log(`👋 ${nameVersion} closed. Exiting now.`);
|
|
25
26
|
process.exit(0);
|
|
26
27
|
});
|
|
27
28
|
} catch (error) {
|
|
28
29
|
clearTimeout(shutdownTimer);
|
|
29
|
-
console.error(
|
|
30
|
+
console.error(`❌ ${nameVersion} error during shutdown:`, error);
|
|
30
31
|
process.exit(1);
|
|
31
32
|
}
|
|
32
33
|
};
|
|
@@ -46,15 +47,23 @@ export function withGracefulShutdown(config: GracefulShutdownConfig = {}): Serve
|
|
|
46
47
|
export function startServerWithShutdown(
|
|
47
48
|
app: any,
|
|
48
49
|
port: number,
|
|
49
|
-
shutdownConfig: GracefulShutdownConfig = {}
|
|
50
|
+
shutdownConfig: GracefulShutdownConfig = {},
|
|
51
|
+
serverName?: string,
|
|
52
|
+
serverVersion?: string
|
|
50
53
|
): Server {
|
|
51
54
|
const server = app.listen(port, () => {
|
|
52
|
-
|
|
55
|
+
const nameVersion = serverName && serverVersion ? `${serverName} v${serverVersion}` : 'Server';
|
|
56
|
+
console.log(`🚀 ${nameVersion} running on http://localhost:${port}`);
|
|
53
57
|
});
|
|
54
58
|
|
|
55
59
|
// Apply graceful shutdown from stored config or provided config
|
|
56
60
|
const config = app.__gracefulShutdownConfig || shutdownConfig;
|
|
57
|
-
|
|
61
|
+
const enhancedConfig = {
|
|
62
|
+
...config,
|
|
63
|
+
serverName,
|
|
64
|
+
serverVersion
|
|
65
|
+
};
|
|
66
|
+
createGracefulShutdown(server, enhancedConfig);
|
|
58
67
|
|
|
59
68
|
return server;
|
|
60
69
|
}
|