@darksheep/express 2.1.1 → 2.1.2
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/CHANGELOG.md +25 -0
- package/README.md +108 -1
- package/express.d.ts +1 -1
- package/package.json +3 -3
- package/src/logger/filter.js +5 -1
- package/src/logger/middleware.js +25 -11
- package/types/content-type/middleware.d.ts +1 -1
- package/types/error-handler/middleware.d.ts +1 -1
- package/types/logger/filter.d.ts +2 -2
- package/types/logger/middleware.d.ts +3 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,30 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [2.1.2](https://github.com/DarkSheepSoftware/node-packages/compare/express-v2.1.1...express-v2.1.2) (2026-04-17)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### 🩹 Fixes
|
|
7
|
+
|
|
8
|
+
* **express:** make @darksheep/logger optional, always log errors ([#836](https://github.com/DarkSheepSoftware/node-packages/issues/836)) ([f29fef9](https://github.com/DarkSheepSoftware/node-packages/commit/f29fef9453b78ef03b6903bcc0b1ccd3af4e2baa))
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### 📚 Documentation
|
|
12
|
+
|
|
13
|
+
* Update the READMEs ([#824](https://github.com/DarkSheepSoftware/node-packages/issues/824)) ([9d282b7](https://github.com/DarkSheepSoftware/node-packages/commit/9d282b724fe056ec275578088bf495985e22c25f))
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
### Dependencies
|
|
17
|
+
|
|
18
|
+
* The following workspace dependencies were updated
|
|
19
|
+
* dependencies
|
|
20
|
+
* @darksheep/errors bumped from 1.2.0 to 1.2.1
|
|
21
|
+
* devDependencies
|
|
22
|
+
* @darksheep/json-schema bumped to 0.1.2
|
|
23
|
+
* @darksheep/logger bumped from 1.3.0 to 1.3.1
|
|
24
|
+
* peerDependencies
|
|
25
|
+
* @darksheep/json-schema bumped to 0.1.2
|
|
26
|
+
* @darksheep/logger bumped from >=1.0.10 to >=1.3.1
|
|
27
|
+
|
|
3
28
|
## [2.1.1](https://github.com/DarkSheepSoftware/node-packages/compare/express-v2.1.0...express-v2.1.1) (2025-12-04)
|
|
4
29
|
|
|
5
30
|
|
package/README.md
CHANGED
|
@@ -1,2 +1,109 @@
|
|
|
1
1
|
# @darksheep/express
|
|
2
|
-
|
|
2
|
+
Express middleware for the stuff you end up wiring up in every service.
|
|
3
|
+
|
|
4
|
+
## Installation
|
|
5
|
+
```sh
|
|
6
|
+
yarn add @darksheep/express express
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
## Example
|
|
10
|
+
```js
|
|
11
|
+
import express from 'express';
|
|
12
|
+
import {
|
|
13
|
+
createRequestIdMiddleware,
|
|
14
|
+
createRequestLoggerMiddleware,
|
|
15
|
+
errorHandlerMiddleware,
|
|
16
|
+
notFoundMiddleware,
|
|
17
|
+
HttpErrorHandler,
|
|
18
|
+
} from '@darksheep/express';
|
|
19
|
+
|
|
20
|
+
const app = express();
|
|
21
|
+
|
|
22
|
+
app.use(createRequestIdMiddleware());
|
|
23
|
+
app.use(createRequestLoggerMiddleware());
|
|
24
|
+
|
|
25
|
+
// your routes here
|
|
26
|
+
|
|
27
|
+
app.use(notFoundMiddleware);
|
|
28
|
+
app.errorHandlers = [ HttpErrorHandler ];
|
|
29
|
+
app.use(errorHandlerMiddleware);
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Detailed Docs
|
|
33
|
+
|
|
34
|
+
### Middleware
|
|
35
|
+
|
|
36
|
+
```js
|
|
37
|
+
/**
|
|
38
|
+
* Adds a unique request ID to req.id and the X-Request-Id response header.
|
|
39
|
+
*/
|
|
40
|
+
createRequestIdMiddleware(options?)
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Logs each request. Requires @darksheep/logger.
|
|
44
|
+
*/
|
|
45
|
+
createRequestLoggerMiddleware(options?)
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Returns 404 JSON for any unmatched route.
|
|
49
|
+
*/
|
|
50
|
+
notFoundMiddleware
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Catches errors, sets the status code from error.statusCode, and returns JSON.
|
|
54
|
+
* Checks app.errorHandlers to format the error body.
|
|
55
|
+
*/
|
|
56
|
+
errorHandlerMiddleware
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### Content type
|
|
60
|
+
|
|
61
|
+
`createContentTypeMiddleware` checks the incoming `Content-Type` header and either continues or throws `UnsupportedMediaType`:
|
|
62
|
+
|
|
63
|
+
```js
|
|
64
|
+
import { createContentTypeMiddleware } from '@darksheep/express';
|
|
65
|
+
|
|
66
|
+
// Only allow JSON bodies on this route
|
|
67
|
+
app.post('/data', createContentTypeMiddleware('application/json'), (req, res) => {
|
|
68
|
+
res.json({ ok: true });
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
// Allow multiple content types
|
|
72
|
+
app.post('/upload', createContentTypeMiddleware(['application/json', 'multipart/form-data']), handler);
|
|
73
|
+
|
|
74
|
+
// Pass false to skip to the next route instead of throwing
|
|
75
|
+
app.post('/maybe', createContentTypeMiddleware('application/json', false), jsonHandler);
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
`createContentTypeRouter` is a shorthand that builds a sub-router dispatching by `Content-Type`:
|
|
79
|
+
|
|
80
|
+
```js
|
|
81
|
+
import { createContentTypeRouter } from '@darksheep/express';
|
|
82
|
+
|
|
83
|
+
app.post('/data', createContentTypeRouter({
|
|
84
|
+
'application/json': jsonHandler,
|
|
85
|
+
'multipart/form-data': formHandler,
|
|
86
|
+
}));
|
|
87
|
+
// Throws UnsupportedMediaType for anything else
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### Error handlers
|
|
91
|
+
`app.errorHandlers` is an array of `{ test, format }` objects. The first matching handler formats the error response.
|
|
92
|
+
|
|
93
|
+
```js
|
|
94
|
+
/**
|
|
95
|
+
* Handles @darksheep/errors HttpError instances.
|
|
96
|
+
* Returns the error name and message.
|
|
97
|
+
*/
|
|
98
|
+
HttpErrorHandler
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Handles @darksheep/json-schema validation errors.
|
|
102
|
+
*/
|
|
103
|
+
SchemaErrorHandler
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Handles Zod validation errors.
|
|
107
|
+
*/
|
|
108
|
+
ZodErrorHandler
|
|
109
|
+
```
|
package/express.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@darksheep/express",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.2",
|
|
4
4
|
"description": "express utilites and other bits",
|
|
5
5
|
"license": "UNLICENCED",
|
|
6
6
|
"type": "module",
|
|
@@ -22,13 +22,13 @@
|
|
|
22
22
|
"test:watch": "node --watch --test src/**/*.test.js"
|
|
23
23
|
},
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@darksheep/errors": "1.2.
|
|
25
|
+
"@darksheep/errors": "1.2.1",
|
|
26
26
|
"@darksheep/prune": "1.1.0",
|
|
27
27
|
"on-finished": "2.4.1"
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
30
|
"@darksheep/json-schema": "",
|
|
31
|
-
"@darksheep/logger": "1.3.
|
|
31
|
+
"@darksheep/logger": "1.3.1",
|
|
32
32
|
"@types/express": "~5.0.0",
|
|
33
33
|
"@types/on-finished": "^2",
|
|
34
34
|
"express": "~5.2.0"
|
package/src/logger/filter.js
CHANGED
|
@@ -10,11 +10,15 @@ const fallbackFilters = [
|
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
|
-
* @param {string} requestPath - The path to test.
|
|
13
|
+
* @param {string | undefined | null} [requestPath] - The path to test.
|
|
14
14
|
* @param {Filter[]} [filters] - A list of paths to filter out.
|
|
15
15
|
* @returns {boolean}
|
|
16
16
|
*/
|
|
17
17
|
export function shouldFilterOutRequest(requestPath, filters = fallbackFilters) {
|
|
18
|
+
if (requestPath == null) {
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
|
|
18
22
|
for (const filter of filters) {
|
|
19
23
|
if (filter instanceof RegExp && filter.test(requestPath)) {
|
|
20
24
|
return true;
|
package/src/logger/middleware.js
CHANGED
|
@@ -1,18 +1,32 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @import { Socket } from 'node:net'
|
|
3
|
+
* @import { Logger } from '@darksheep/logger'
|
|
3
4
|
* @import { Handler } from '../../express.js'
|
|
4
5
|
* @import { Filter } from './filter.js'
|
|
5
6
|
*/
|
|
6
7
|
|
|
7
|
-
import { Logger } from '@darksheep/logger';
|
|
8
8
|
import { pruneShallow } from '@darksheep/prune';
|
|
9
9
|
import onFinished from 'on-finished';
|
|
10
10
|
|
|
11
11
|
import { shouldFilterOutRequest } from './filter.js';
|
|
12
12
|
|
|
13
|
+
async function getLogger() {
|
|
14
|
+
try {
|
|
15
|
+
const { Logger } = await import('@darksheep/logger');
|
|
16
|
+
|
|
17
|
+
return Logger;
|
|
18
|
+
} catch {
|
|
19
|
+
// optional peer dependency
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const Logger = await getLogger();
|
|
24
|
+
|
|
13
25
|
/** @type {WeakMap<Socket, number>} */
|
|
14
26
|
const sockets = new WeakMap();
|
|
15
27
|
|
|
28
|
+
const wrap = Logger?.wrap ?? ((_ctx, fn) => fn());
|
|
29
|
+
|
|
16
30
|
/**
|
|
17
31
|
* @param {Logger} logger - A callback that can be bound to the request.
|
|
18
32
|
* @param {Filter[]} [filters] - A list of requests to not log.
|
|
@@ -25,25 +39,25 @@ export function createRequestLoggerMiddleware(
|
|
|
25
39
|
return (request, response, next) => {
|
|
26
40
|
request.logger = logger;
|
|
27
41
|
|
|
28
|
-
|
|
42
|
+
wrap({
|
|
29
43
|
request: request,
|
|
30
44
|
trail: request.trail,
|
|
31
45
|
}, () => {
|
|
32
|
-
const [ requestPath ] = request.originalUrl.split('?', 1);
|
|
33
|
-
if (
|
|
34
|
-
requestPath == null ||
|
|
35
|
-
shouldFilterOutRequest(requestPath, filters)
|
|
36
|
-
) {
|
|
37
|
-
next();
|
|
38
|
-
return;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
46
|
const startBytesRead = sockets.get(request.socket) ?? 0;
|
|
42
47
|
const startBytesWritten = request.socket.bytesWritten;
|
|
43
48
|
|
|
44
49
|
const requestStartMs = Date.now();
|
|
45
50
|
onFinished(response, (error) => {
|
|
46
51
|
sockets.set(request.socket, request.socket.bytesRead);
|
|
52
|
+
const [ requestPath ] = request.originalUrl.split('?', 1);
|
|
53
|
+
|
|
54
|
+
if (
|
|
55
|
+
shouldFilterOutRequest(requestPath, filters) === true &&
|
|
56
|
+
response.statusCode < 400 &&
|
|
57
|
+
error == null
|
|
58
|
+
) {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
47
61
|
|
|
48
62
|
const message = `${request.method} ${requestPath} ${response.statusCode}`;
|
|
49
63
|
const context = pruneShallow({
|
|
@@ -4,5 +4,5 @@
|
|
|
4
4
|
* @returns {Handler}
|
|
5
5
|
*/
|
|
6
6
|
export function createContentTypeMiddleware(targetContentTypes: string | string[], shouldThrow?: boolean): Handler;
|
|
7
|
-
export function unsupportedMediaType(req: import("
|
|
7
|
+
export function unsupportedMediaType(req: import("express-serve-static-core").Request<import("express-serve-static-core").ParamsDictionary, any, any, import("qs").ParsedQs, Record<string, any>>, res: import("express-serve-static-core").Response<any, Record<string, any>, number>, next: import("express-serve-static-core").NextFunction): unknown;
|
|
8
8
|
import type { Handler } from '../../express.js';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export function errorHandlerMiddleware(err: any, req: import("
|
|
1
|
+
export function errorHandlerMiddleware(err: any, req: import("express-serve-static-core").Request<import("express-serve-static-core").ParamsDictionary, any, any, import("qs").ParsedQs, Record<string, any>>, res: import("express-serve-static-core").Response<any, Record<string, any>, number>, next: import("express-serve-static-core").NextFunction): unknown;
|
package/types/logger/filter.d.ts
CHANGED
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
* @typedef {string|RegExp} Filter
|
|
3
3
|
*/
|
|
4
4
|
/**
|
|
5
|
-
* @param {string} requestPath - The path to test.
|
|
5
|
+
* @param {string | undefined | null} [requestPath] - The path to test.
|
|
6
6
|
* @param {Filter[]} [filters] - A list of paths to filter out.
|
|
7
7
|
* @returns {boolean}
|
|
8
8
|
*/
|
|
9
|
-
export function shouldFilterOutRequest(requestPath
|
|
9
|
+
export function shouldFilterOutRequest(requestPath?: string | undefined | null, filters?: Filter[]): boolean;
|
|
10
10
|
export type Filter = string | RegExp;
|
|
@@ -4,6 +4,8 @@
|
|
|
4
4
|
* @returns {Handler}
|
|
5
5
|
*/
|
|
6
6
|
export function createRequestLoggerMiddleware(logger: Logger, filters?: Filter[]): Handler;
|
|
7
|
-
|
|
7
|
+
declare const Logger: typeof Logger | undefined;
|
|
8
|
+
import type { Logger } from '@darksheep/logger';
|
|
8
9
|
import type { Filter } from './filter.js';
|
|
9
10
|
import type { Handler } from '../../express.js';
|
|
11
|
+
export {};
|