@drunkcod/express-kit 0.0.18 → 0.0.20
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/lib/cjs/index.d.ts +8 -7
- package/lib/cjs/index.js +28 -35
- package/lib/cjs/loggable.js +6 -3
- package/lib/index.d.ts +8 -7
- package/lib/index.js +23 -31
- package/lib/loggable.js +6 -3
- package/package.json +12 -11
package/lib/cjs/index.d.ts
CHANGED
|
@@ -1,22 +1,23 @@
|
|
|
1
1
|
import type express from 'express';
|
|
2
2
|
import { Timespan } from './stopwatch.js';
|
|
3
|
-
export * from '
|
|
3
|
+
export * from 'packages/express-async/lib/index.js';
|
|
4
4
|
export * from './loggable.js';
|
|
5
5
|
export * from './stopwatch.js';
|
|
6
6
|
type AsyncFn<T> = () => Promise<T>;
|
|
7
7
|
type ExpressServer = ReturnType<express.Application['listen']>;
|
|
8
8
|
export type ErrorHandler = (error: Error, request: express.Request, response: express.Response, next: express.NextFunction) => void;
|
|
9
|
+
export type WithReqBody<T, ReqBody> = T extends express.Request<infer P, infer ResBody, unknown, infer Query, infer Locals> ? express.Request<P, ResBody, ReqBody, Query, Locals> : never;
|
|
9
10
|
export declare function onceAsync<T>(fn: AsyncFn<T>): AsyncFn<T>;
|
|
10
11
|
export declare function mergeCallsAsync<T>(fn: AsyncFn<T>): AsyncFn<T>;
|
|
11
12
|
interface Listener<T> {
|
|
12
13
|
listen(cb: () => void): T;
|
|
13
14
|
listen(port: number, cb: () => void): T;
|
|
14
15
|
}
|
|
15
|
-
export declare
|
|
16
|
+
export declare const listenAsync: <T>(server: Listener<T>, options?: {
|
|
16
17
|
port?: number;
|
|
17
|
-
})
|
|
18
|
-
export declare
|
|
18
|
+
}) => Promise<T>;
|
|
19
|
+
export declare const closeAsync: (server: {
|
|
19
20
|
close: (cb: (error?: Error) => void) => void;
|
|
20
|
-
})
|
|
21
|
-
export declare function registerShutdown<Server extends ExpressServer = ExpressServer>(server: Server, shutdown?: () => Promise<
|
|
22
|
-
export declare
|
|
21
|
+
}) => Promise<void>;
|
|
22
|
+
export declare function registerShutdown<Server extends ExpressServer = ExpressServer>(server: Server, shutdown?: () => Promise<unknown>): void;
|
|
23
|
+
export declare const requestTimingMiddleware: (onRequestFinish: (req: express.Request, duration: Timespan) => void) => (req: express.Request, res: express.Response, next: express.NextFunction) => void;
|
package/lib/cjs/index.js
CHANGED
|
@@ -14,14 +14,12 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.requestTimingMiddleware = exports.closeAsync = exports.listenAsync = void 0;
|
|
17
18
|
exports.onceAsync = onceAsync;
|
|
18
19
|
exports.mergeCallsAsync = mergeCallsAsync;
|
|
19
|
-
exports.listenAsync = listenAsync;
|
|
20
|
-
exports.closeAsync = closeAsync;
|
|
21
20
|
exports.registerShutdown = registerShutdown;
|
|
22
|
-
exports.requestTimingMiddleware = requestTimingMiddleware;
|
|
23
21
|
const stopwatch_js_1 = require("./stopwatch.js");
|
|
24
|
-
__exportStar(require("
|
|
22
|
+
__exportStar(require("packages/express-async/lib/index.js"), exports);
|
|
25
23
|
__exportStar(require("./loggable.js"), exports);
|
|
26
24
|
__exportStar(require("./stopwatch.js"), exports);
|
|
27
25
|
function onceAsync(fn) {
|
|
@@ -47,44 +45,39 @@ function mergeCallsAsync(fn) {
|
|
|
47
45
|
}
|
|
48
46
|
};
|
|
49
47
|
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
const r = server.listen(options.port, () => resolve(r));
|
|
55
|
-
}
|
|
56
|
-
else {
|
|
57
|
-
const r = server.listen(() => resolve(r));
|
|
58
|
-
}
|
|
48
|
+
const listenAsync = (server, options) => new Promise((resolve, reject) => {
|
|
49
|
+
try {
|
|
50
|
+
if (options?.port) {
|
|
51
|
+
const r = server.listen(options.port, () => resolve(r));
|
|
59
52
|
}
|
|
60
|
-
|
|
61
|
-
|
|
53
|
+
else {
|
|
54
|
+
const r = server.listen(() => resolve(r));
|
|
62
55
|
}
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
}
|
|
56
|
+
}
|
|
57
|
+
catch (err) {
|
|
58
|
+
reject(err);
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
exports.listenAsync = listenAsync;
|
|
62
|
+
const closeAsync = (server) => new Promise((resolve, reject) => server.close((err) => {
|
|
63
|
+
if (err)
|
|
64
|
+
reject(err);
|
|
65
|
+
else
|
|
66
|
+
resolve();
|
|
67
|
+
}));
|
|
68
|
+
exports.closeAsync = closeAsync;
|
|
75
69
|
function registerShutdown(server, shutdown) {
|
|
76
70
|
const onShutdown = onceAsync(async () => {
|
|
77
|
-
await closeAsync(server);
|
|
71
|
+
await (0, exports.closeAsync)(server);
|
|
78
72
|
if (shutdown)
|
|
79
73
|
await shutdown();
|
|
80
74
|
});
|
|
81
75
|
process.on('SIGINT', onShutdown);
|
|
82
76
|
process.on('SIGTERM', onShutdown);
|
|
83
77
|
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
}
|
|
78
|
+
const requestTimingMiddleware = (onRequestFinish) => (req, res, next) => {
|
|
79
|
+
const s = stopwatch_js_1.Stopwatch.startNew();
|
|
80
|
+
res.on('finish', () => onRequestFinish(req, s.elapsed));
|
|
81
|
+
next();
|
|
82
|
+
};
|
|
83
|
+
exports.requestTimingMiddleware = requestTimingMiddleware;
|
package/lib/cjs/loggable.js
CHANGED
|
@@ -26,18 +26,21 @@ function loggableStack(message, stopAt) {
|
|
|
26
26
|
message = '';
|
|
27
27
|
return `LoggableError${message}\n` + s.stack?.substring(6);
|
|
28
28
|
}
|
|
29
|
-
function asLoggableCause(cause) {
|
|
29
|
+
function asLoggableCause(cause, seen = new WeakSet()) {
|
|
30
30
|
if (cause == null)
|
|
31
31
|
return null;
|
|
32
32
|
if (typeof cause !== 'object')
|
|
33
33
|
return { message: cause };
|
|
34
|
+
if (seen.has(cause))
|
|
35
|
+
return { message: '[Circular Reference]' };
|
|
36
|
+
seen.add(cause);
|
|
34
37
|
if (hasOwnJSON(cause))
|
|
35
|
-
return asLoggableCause(cause.toJSON());
|
|
38
|
+
return asLoggableCause(cause.toJSON(), seen);
|
|
36
39
|
if (cause instanceof Error || (0, argis_1.hasOwn)(cause, 'cause')) {
|
|
37
40
|
const { message, stack, cause: innerCause, ...rest } = cause;
|
|
38
41
|
const r = { message };
|
|
39
42
|
if (innerCause) {
|
|
40
|
-
const loggableInner = asLoggableCause(innerCause);
|
|
43
|
+
const loggableInner = asLoggableCause(innerCause, seen);
|
|
41
44
|
r.cause = loggableInner;
|
|
42
45
|
r.message ??= loggableInner.message;
|
|
43
46
|
}
|
package/lib/index.d.ts
CHANGED
|
@@ -1,22 +1,23 @@
|
|
|
1
1
|
import type express from 'express';
|
|
2
2
|
import { Timespan } from './stopwatch.js';
|
|
3
|
-
export * from '
|
|
3
|
+
export * from 'packages/express-async/lib/index.js';
|
|
4
4
|
export * from './loggable.js';
|
|
5
5
|
export * from './stopwatch.js';
|
|
6
6
|
type AsyncFn<T> = () => Promise<T>;
|
|
7
7
|
type ExpressServer = ReturnType<express.Application['listen']>;
|
|
8
8
|
export type ErrorHandler = (error: Error, request: express.Request, response: express.Response, next: express.NextFunction) => void;
|
|
9
|
+
export type WithReqBody<T, ReqBody> = T extends express.Request<infer P, infer ResBody, unknown, infer Query, infer Locals> ? express.Request<P, ResBody, ReqBody, Query, Locals> : never;
|
|
9
10
|
export declare function onceAsync<T>(fn: AsyncFn<T>): AsyncFn<T>;
|
|
10
11
|
export declare function mergeCallsAsync<T>(fn: AsyncFn<T>): AsyncFn<T>;
|
|
11
12
|
interface Listener<T> {
|
|
12
13
|
listen(cb: () => void): T;
|
|
13
14
|
listen(port: number, cb: () => void): T;
|
|
14
15
|
}
|
|
15
|
-
export declare
|
|
16
|
+
export declare const listenAsync: <T>(server: Listener<T>, options?: {
|
|
16
17
|
port?: number;
|
|
17
|
-
})
|
|
18
|
-
export declare
|
|
18
|
+
}) => Promise<T>;
|
|
19
|
+
export declare const closeAsync: (server: {
|
|
19
20
|
close: (cb: (error?: Error) => void) => void;
|
|
20
|
-
})
|
|
21
|
-
export declare function registerShutdown<Server extends ExpressServer = ExpressServer>(server: Server, shutdown?: () => Promise<
|
|
22
|
-
export declare
|
|
21
|
+
}) => Promise<void>;
|
|
22
|
+
export declare function registerShutdown<Server extends ExpressServer = ExpressServer>(server: Server, shutdown?: () => Promise<unknown>): void;
|
|
23
|
+
export declare const requestTimingMiddleware: (onRequestFinish: (req: express.Request, duration: Timespan) => void) => (req: express.Request, res: express.Response, next: express.NextFunction) => void;
|
package/lib/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Stopwatch } from './stopwatch.js';
|
|
2
|
-
export * from '
|
|
2
|
+
export * from 'packages/express-async/lib/index.js';
|
|
3
3
|
export * from './loggable.js';
|
|
4
4
|
export * from './stopwatch.js';
|
|
5
5
|
export function onceAsync(fn) {
|
|
@@ -25,31 +25,25 @@ export function mergeCallsAsync(fn) {
|
|
|
25
25
|
}
|
|
26
26
|
};
|
|
27
27
|
}
|
|
28
|
-
export
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
const r = server.listen(options.port, () => resolve(r));
|
|
33
|
-
}
|
|
34
|
-
else {
|
|
35
|
-
const r = server.listen(() => resolve(r));
|
|
36
|
-
}
|
|
28
|
+
export const listenAsync = (server, options) => new Promise((resolve, reject) => {
|
|
29
|
+
try {
|
|
30
|
+
if (options?.port) {
|
|
31
|
+
const r = server.listen(options.port, () => resolve(r));
|
|
37
32
|
}
|
|
38
|
-
|
|
39
|
-
|
|
33
|
+
else {
|
|
34
|
+
const r = server.listen(() => resolve(r));
|
|
40
35
|
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
}
|
|
36
|
+
}
|
|
37
|
+
catch (err) {
|
|
38
|
+
reject(err);
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
export const closeAsync = (server) => new Promise((resolve, reject) => server.close((err) => {
|
|
42
|
+
if (err)
|
|
43
|
+
reject(err);
|
|
44
|
+
else
|
|
45
|
+
resolve();
|
|
46
|
+
}));
|
|
53
47
|
export function registerShutdown(server, shutdown) {
|
|
54
48
|
const onShutdown = onceAsync(async () => {
|
|
55
49
|
await closeAsync(server);
|
|
@@ -59,10 +53,8 @@ export function registerShutdown(server, shutdown) {
|
|
|
59
53
|
process.on('SIGINT', onShutdown);
|
|
60
54
|
process.on('SIGTERM', onShutdown);
|
|
61
55
|
}
|
|
62
|
-
export
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
};
|
|
68
|
-
}
|
|
56
|
+
export const requestTimingMiddleware = (onRequestFinish) => (req, res, next) => {
|
|
57
|
+
const s = Stopwatch.startNew();
|
|
58
|
+
res.on('finish', () => onRequestFinish(req, s.elapsed));
|
|
59
|
+
next();
|
|
60
|
+
};
|
package/lib/loggable.js
CHANGED
|
@@ -21,18 +21,21 @@ function loggableStack(message, stopAt) {
|
|
|
21
21
|
message = '';
|
|
22
22
|
return `LoggableError${message}\n` + s.stack?.substring(6);
|
|
23
23
|
}
|
|
24
|
-
function asLoggableCause(cause) {
|
|
24
|
+
function asLoggableCause(cause, seen = new WeakSet()) {
|
|
25
25
|
if (cause == null)
|
|
26
26
|
return null;
|
|
27
27
|
if (typeof cause !== 'object')
|
|
28
28
|
return { message: cause };
|
|
29
|
+
if (seen.has(cause))
|
|
30
|
+
return { message: '[Circular Reference]' };
|
|
31
|
+
seen.add(cause);
|
|
29
32
|
if (hasOwnJSON(cause))
|
|
30
|
-
return asLoggableCause(cause.toJSON());
|
|
33
|
+
return asLoggableCause(cause.toJSON(), seen);
|
|
31
34
|
if (cause instanceof Error || hasOwn(cause, 'cause')) {
|
|
32
35
|
const { message, stack, cause: innerCause, ...rest } = cause;
|
|
33
36
|
const r = { message };
|
|
34
37
|
if (innerCause) {
|
|
35
|
-
const loggableInner = asLoggableCause(innerCause);
|
|
38
|
+
const loggableInner = asLoggableCause(innerCause, seen);
|
|
36
39
|
r.cause = loggableInner;
|
|
37
40
|
r.message ??= loggableInner.message;
|
|
38
41
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@drunkcod/express-kit",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.20",
|
|
5
5
|
"description": "Express4 utility things",
|
|
6
6
|
"main": "lib/index.js",
|
|
7
7
|
"types": "lib/index.d.ts",
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
"compile": "tsc",
|
|
27
27
|
"cjs:compile": "tsc --module commonjs --outdir lib/cjs",
|
|
28
28
|
"cjs:fixup": "echo '{\"type\": \"commonjs\"}' > lib/cjs/package.json",
|
|
29
|
-
"test": "
|
|
29
|
+
"test": "node --experimental-vm-modules --disable-warning=ExperimentalWarning node_modules/jest/bin/jest.js",
|
|
30
30
|
"test:all": "npm-run-all \"test --workspaces --include-workspace-root\" --silent",
|
|
31
31
|
"build": "npm-run-all clean -p compile cjs:compile -s cjs:fixup --silent",
|
|
32
32
|
"build:all": "npm-run-all \"build --workspaces --include-workspace-root\" --silent"
|
|
@@ -37,20 +37,21 @@
|
|
|
37
37
|
"author": "Tobbe Gyllebring <tobbe@drunkcod.com>",
|
|
38
38
|
"license": "MIT",
|
|
39
39
|
"workspaces": [
|
|
40
|
-
"
|
|
40
|
+
"packages/*"
|
|
41
41
|
],
|
|
42
42
|
"dependencies": {
|
|
43
|
-
"@drunkcod/argis": "^0.0.
|
|
43
|
+
"@drunkcod/argis": "^0.0.14",
|
|
44
44
|
"@drunkcod/express-async": "^0.0.18"
|
|
45
45
|
},
|
|
46
46
|
"devDependencies": {
|
|
47
|
-
"@drunkcod/ts-jest-esm": "^0.0.
|
|
48
|
-
"@jest/globals": "^
|
|
49
|
-
"@types/node": "^
|
|
50
|
-
"jest": "^
|
|
47
|
+
"@drunkcod/ts-jest-esm": "^0.0.3",
|
|
48
|
+
"@jest/globals": "^30.2.0",
|
|
49
|
+
"@types/node": "^24.10.13",
|
|
50
|
+
"jest": "^30.2.0",
|
|
51
|
+
"jest-util": "^30.2.0",
|
|
51
52
|
"npm-run-all": "^4.1.5",
|
|
52
|
-
"rimraf": "^6.
|
|
53
|
-
"ts-jest": "^29.
|
|
54
|
-
"typescript": "^5.
|
|
53
|
+
"rimraf": "^6.1.3",
|
|
54
|
+
"ts-jest": "^29.4.6",
|
|
55
|
+
"typescript": "^5.9.3"
|
|
55
56
|
}
|
|
56
57
|
}
|