@shetty4l/core 0.1.9 → 0.1.11
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/bin/version-bump +2 -0
- package/package.json +4 -3
- package/src/http.ts +5 -2
- package/src/signals.ts +10 -3
package/bin/version-bump
ADDED
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shetty4l/core",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.11",
|
|
4
4
|
"description": "Shared infrastructure primitives for Bun/TypeScript services",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
7
|
-
"url": "https://github.com/shetty4l/core"
|
|
7
|
+
"url": "git+https://github.com/shetty4l/core.git"
|
|
8
8
|
},
|
|
9
9
|
"type": "module",
|
|
10
10
|
"main": "src/index.ts",
|
|
@@ -20,13 +20,14 @@
|
|
|
20
20
|
},
|
|
21
21
|
"files": [
|
|
22
22
|
"src/",
|
|
23
|
+
"bin/",
|
|
23
24
|
"scripts/install-lib.sh"
|
|
24
25
|
],
|
|
25
26
|
"publishConfig": {
|
|
26
27
|
"registry": "https://registry.npmjs.org"
|
|
27
28
|
},
|
|
28
29
|
"bin": {
|
|
29
|
-
"version-bump": "./
|
|
30
|
+
"version-bump": "./bin/version-bump"
|
|
30
31
|
},
|
|
31
32
|
"scripts": {
|
|
32
33
|
"typecheck": "tsc --noEmit",
|
package/src/http.ts
CHANGED
|
@@ -64,6 +64,8 @@ export function healthResponse(
|
|
|
64
64
|
// --- Server ---
|
|
65
65
|
|
|
66
66
|
export interface ServerOpts {
|
|
67
|
+
/** Service name used as log prefix (e.g. "synapse"). */
|
|
68
|
+
name?: string;
|
|
67
69
|
/** Port to listen on. */
|
|
68
70
|
port: number;
|
|
69
71
|
/** Hostname to bind to. Defaults to "127.0.0.1". */
|
|
@@ -98,8 +100,9 @@ export interface HttpServer {
|
|
|
98
100
|
* 4. If onRequest returns null -> 404
|
|
99
101
|
*/
|
|
100
102
|
export function createServer(opts: ServerOpts): HttpServer {
|
|
101
|
-
const { port, host = "127.0.0.1", version, onRequest } = opts;
|
|
103
|
+
const { port, host = "127.0.0.1", version, onRequest, name } = opts;
|
|
102
104
|
const startTime = Date.now();
|
|
105
|
+
const prefix = name ? `${name}: ` : "";
|
|
103
106
|
|
|
104
107
|
const server = Bun.serve({
|
|
105
108
|
port,
|
|
@@ -122,7 +125,7 @@ export function createServer(opts: ServerOpts): HttpServer {
|
|
|
122
125
|
}
|
|
123
126
|
return result;
|
|
124
127
|
} catch (error) {
|
|
125
|
-
console.error(
|
|
128
|
+
console.error(`${prefix}HTTP request error:`, error);
|
|
126
129
|
return jsonError(
|
|
127
130
|
500,
|
|
128
131
|
error instanceof Error ? error.message : "Internal server error",
|
package/src/signals.ts
CHANGED
|
@@ -7,6 +7,8 @@
|
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
9
|
export interface ShutdownOpts {
|
|
10
|
+
/** Service name used as log prefix (e.g. "synapse"). */
|
|
11
|
+
name?: string;
|
|
10
12
|
/** Signals to handle. Defaults to ["SIGINT", "SIGTERM"]. */
|
|
11
13
|
signals?: string[];
|
|
12
14
|
/** Force exit after this many ms. No timeout if omitted. */
|
|
@@ -26,6 +28,7 @@ export function onShutdown(
|
|
|
26
28
|
): void {
|
|
27
29
|
const signals = opts?.signals ?? ["SIGINT", "SIGTERM"];
|
|
28
30
|
const timeoutMs = opts?.timeoutMs;
|
|
31
|
+
const prefix = opts?.name ? `${opts.name}: ` : "";
|
|
29
32
|
let shutting = false;
|
|
30
33
|
|
|
31
34
|
const handler = (signal: string) => {
|
|
@@ -33,12 +36,16 @@ export function onShutdown(
|
|
|
33
36
|
process.exit(1);
|
|
34
37
|
}
|
|
35
38
|
shutting = true;
|
|
36
|
-
console.
|
|
39
|
+
console.error(
|
|
40
|
+
`\n${prefix}${signal.toLowerCase()} received, shutting down...`,
|
|
41
|
+
);
|
|
37
42
|
|
|
38
43
|
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
39
44
|
if (timeoutMs !== undefined) {
|
|
40
45
|
timer = setTimeout(() => {
|
|
41
|
-
console.error(
|
|
46
|
+
console.error(
|
|
47
|
+
`${prefix}shutdown timed out after ${timeoutMs}ms, forcing exit`,
|
|
48
|
+
);
|
|
42
49
|
process.exit(1);
|
|
43
50
|
}, timeoutMs);
|
|
44
51
|
// Don't block the event loop from exiting
|
|
@@ -53,7 +60,7 @@ export function onShutdown(
|
|
|
53
60
|
process.exit(0);
|
|
54
61
|
})
|
|
55
62
|
.catch((err) => {
|
|
56
|
-
console.error(
|
|
63
|
+
console.error(`${prefix}shutdown cleanup error:`, err);
|
|
57
64
|
if (timer) clearTimeout(timer);
|
|
58
65
|
process.exit(1);
|
|
59
66
|
});
|