@shetty4l/core 0.1.8 → 0.1.10
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/package.json +1 -1
- package/src/cli.ts +3 -3
- package/src/http.ts +5 -2
- package/src/signals.ts +10 -3
package/package.json
CHANGED
package/src/cli.ts
CHANGED
|
@@ -45,7 +45,7 @@ export function formatUptime(seconds: number): string {
|
|
|
45
45
|
export type CommandHandler = (
|
|
46
46
|
args: string[],
|
|
47
47
|
json: boolean,
|
|
48
|
-
) => void | Promise<void>;
|
|
48
|
+
) => void | number | Promise<void | number>;
|
|
49
49
|
|
|
50
50
|
export interface RunCliOpts {
|
|
51
51
|
/** Service name, used in error messages. */
|
|
@@ -104,6 +104,6 @@ export async function runCli(opts: RunCliOpts): Promise<void> {
|
|
|
104
104
|
process.exit(1);
|
|
105
105
|
}
|
|
106
106
|
|
|
107
|
-
await handler(args, json);
|
|
108
|
-
process.exit(
|
|
107
|
+
const exitCode = (await handler(args, json)) ?? 0;
|
|
108
|
+
process.exit(exitCode);
|
|
109
109
|
}
|
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
|
});
|