@drunkcod/express-kit 0.0.5
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/index.d.ts +6 -0
- package/lib/index.js +47 -0
- package/lib/loggable.d.ts +2 -0
- package/lib/loggable.js +40 -0
- package/package.json +34 -0
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type express from 'express';
|
|
2
|
+
export * from '@drunkcod/express-async';
|
|
3
|
+
export * from './loggable';
|
|
4
|
+
export declare function onceAsync<T>(fn: () => Promise<T>): () => Promise<T>;
|
|
5
|
+
type ExpressServer = ReturnType<express.Application['listen']>;
|
|
6
|
+
export declare function registerShutdown<Server extends ExpressServer = ExpressServer>(server: Server, shutdown?: () => Promise<void>): void;
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.onceAsync = onceAsync;
|
|
18
|
+
exports.registerShutdown = registerShutdown;
|
|
19
|
+
__exportStar(require("@drunkcod/express-async"), exports);
|
|
20
|
+
__exportStar(require("./loggable"), exports);
|
|
21
|
+
function onceAsync(fn) {
|
|
22
|
+
let p;
|
|
23
|
+
return () => {
|
|
24
|
+
if (p)
|
|
25
|
+
return p;
|
|
26
|
+
p = fn();
|
|
27
|
+
fn = () => p;
|
|
28
|
+
return p;
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
const closeAsync = (server) => (new Promise((resolve, reject) => {
|
|
32
|
+
server.close((err) => {
|
|
33
|
+
if (err)
|
|
34
|
+
reject(err);
|
|
35
|
+
else
|
|
36
|
+
resolve();
|
|
37
|
+
});
|
|
38
|
+
}));
|
|
39
|
+
function registerShutdown(server, shutdown) {
|
|
40
|
+
shutdown = onceAsync(async () => {
|
|
41
|
+
await closeAsync(server);
|
|
42
|
+
if (shutdown)
|
|
43
|
+
await shutdown();
|
|
44
|
+
});
|
|
45
|
+
process.on('SIGINT', shutdown);
|
|
46
|
+
process.on('SIGTERM', shutdown);
|
|
47
|
+
}
|
package/lib/loggable.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.at = at;
|
|
4
|
+
exports.asLoggableError = asLoggableError;
|
|
5
|
+
const argis_1 = require("@drunkcod/argis");
|
|
6
|
+
function at(message) {
|
|
7
|
+
const old = Error.stackTraceLimit;
|
|
8
|
+
try {
|
|
9
|
+
const r = { stack: '' };
|
|
10
|
+
Error.stackTraceLimit = 1;
|
|
11
|
+
Error.captureStackTrace(r, at);
|
|
12
|
+
message = message ? `${message} ` : '';
|
|
13
|
+
return message + r.stack.substring(10);
|
|
14
|
+
}
|
|
15
|
+
finally {
|
|
16
|
+
Error.stackTraceLimit = old;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
function asLoggableCause(cause) {
|
|
20
|
+
if (cause == null || typeof cause !== 'object')
|
|
21
|
+
return cause;
|
|
22
|
+
if (cause instanceof Error) {
|
|
23
|
+
const { message, stack, cause: innerCause, ...rest } = cause;
|
|
24
|
+
return innerCause
|
|
25
|
+
? { message, stack, cause: asLoggableCause(innerCause), ...rest }
|
|
26
|
+
: { message, stack, ...rest };
|
|
27
|
+
}
|
|
28
|
+
if ((0, argis_1.hasOwn)(cause, 'cause')) {
|
|
29
|
+
const { cause: innerCause, ...rest } = cause;
|
|
30
|
+
return { ...rest, cause: asLoggableCause(innerCause) };
|
|
31
|
+
}
|
|
32
|
+
return { ...cause };
|
|
33
|
+
}
|
|
34
|
+
function asLoggableError(error) {
|
|
35
|
+
if (error instanceof Error)
|
|
36
|
+
return asLoggableCause(error);
|
|
37
|
+
const r = (error && typeof error === 'object') ? asLoggableCause(error) : { message: error };
|
|
38
|
+
Error.captureStackTrace(r, asLoggableError);
|
|
39
|
+
return Object.defineProperty(r, 'stack', { enumerable: true });
|
|
40
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@drunkcod/express-kit",
|
|
3
|
+
"version": "0.0.5",
|
|
4
|
+
"description": "Express4 utility things",
|
|
5
|
+
"main": "lib/index.js",
|
|
6
|
+
"types": "lib/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"lib/**/*",
|
|
9
|
+
"!lib/**/*.spec.*"
|
|
10
|
+
],
|
|
11
|
+
"scripts": {
|
|
12
|
+
"test": "jest",
|
|
13
|
+
"build": "rimraf lib && tsc"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"express"
|
|
17
|
+
],
|
|
18
|
+
"author": "Tobbe Gyllebring",
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"workspaces": [
|
|
21
|
+
"express-async"
|
|
22
|
+
],
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@drunkcod/argis": "^0.0.2"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@jest/globals": "^29.7.0",
|
|
28
|
+
"@types/node": "^22.5.4",
|
|
29
|
+
"jest": "^29.7.0",
|
|
30
|
+
"rimraf": "^5.0.10",
|
|
31
|
+
"ts-jest": "^29.2.5",
|
|
32
|
+
"typescript": "^5.5.4"
|
|
33
|
+
}
|
|
34
|
+
}
|