@req-inspect/express 0.0.0-stage → 0.1.0
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/LICENSE +21 -0
- package/README.md +22 -2
- package/dist/index.cjs +130 -0
- package/dist/index.d.cts +7 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +110 -0
- package/package.json +59 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Luis
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,23 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @req-inspect/express
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Express middleware for [req-inspect](https://github.com/Hurishihara/req-inspect): a live request inspector dashboard.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm i @req-inspect/express
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```js
|
|
14
|
+
import express from 'express'
|
|
15
|
+
import { inspector } from '@req-inspect/express'
|
|
16
|
+
|
|
17
|
+
const app = express()
|
|
18
|
+
app.use(inspector()) // at the app root, before routes and body parsers
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Open `/_inspect` in your browser. Without `auth` it is only reachable from localhost.
|
|
22
|
+
|
|
23
|
+
See the [root README](https://github.com/Hurishihara/req-inspect#readme) for options, security and known limitations.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
default: () => index_default,
|
|
24
|
+
inspector: () => inspector
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(index_exports);
|
|
27
|
+
var import_core = require("@req-inspect/core");
|
|
28
|
+
function inspector(options = {}) {
|
|
29
|
+
const insp = (0, import_core.createInspector)(options);
|
|
30
|
+
const { enabled, maxBodyBytes: max } = insp.options;
|
|
31
|
+
return (req, res, next) => {
|
|
32
|
+
if (!enabled) return next();
|
|
33
|
+
const q = req.originalUrl.indexOf("?");
|
|
34
|
+
const pathname = q < 0 ? req.originalUrl : req.originalUrl.slice(0, q);
|
|
35
|
+
if (insp.isInspectorPath(pathname)) return insp.handle(req, res);
|
|
36
|
+
if (insp.shouldIgnore(pathname)) return next();
|
|
37
|
+
const startedAt = Date.now();
|
|
38
|
+
const t0 = performance.now();
|
|
39
|
+
const chunks = [];
|
|
40
|
+
let kept = 0;
|
|
41
|
+
let total = 0;
|
|
42
|
+
let done = false;
|
|
43
|
+
let snap;
|
|
44
|
+
const snapshot = () => {
|
|
45
|
+
snap ??= {
|
|
46
|
+
route: req.route ? `${req.baseUrl}${req.route.path}` : null,
|
|
47
|
+
params: { ...req.params }
|
|
48
|
+
};
|
|
49
|
+
};
|
|
50
|
+
const collect = (args) => {
|
|
51
|
+
try {
|
|
52
|
+
snapshot();
|
|
53
|
+
const chunk = args[0];
|
|
54
|
+
if (typeof chunk !== "string" && !(chunk instanceof Uint8Array)) return;
|
|
55
|
+
const enc = typeof args[1] === "string" ? args[1] : "utf8";
|
|
56
|
+
if (kept >= max) {
|
|
57
|
+
total += typeof chunk === "string" ? Buffer.byteLength(chunk, enc) : chunk.byteLength;
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
const room = max - kept;
|
|
61
|
+
let part;
|
|
62
|
+
if (typeof chunk === "string") {
|
|
63
|
+
const buf = Buffer.from(chunk, enc);
|
|
64
|
+
total += buf.length;
|
|
65
|
+
part = buf.length > room ? Buffer.from(buf.subarray(0, room)) : buf;
|
|
66
|
+
} else {
|
|
67
|
+
const bytes = chunk;
|
|
68
|
+
total += bytes.byteLength;
|
|
69
|
+
part = Buffer.from(bytes.subarray(0, Math.min(bytes.byteLength, room)));
|
|
70
|
+
}
|
|
71
|
+
chunks.push(part);
|
|
72
|
+
kept += part.length;
|
|
73
|
+
} catch (err) {
|
|
74
|
+
(0, import_core.warnOnce)("capture", err);
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
const origWrite = res.write;
|
|
78
|
+
const origEnd = res.end;
|
|
79
|
+
res.write = function(...args) {
|
|
80
|
+
collect(args);
|
|
81
|
+
return origWrite.apply(this, args);
|
|
82
|
+
};
|
|
83
|
+
res.end = function(...args) {
|
|
84
|
+
collect(args);
|
|
85
|
+
return origEnd.apply(this, args);
|
|
86
|
+
};
|
|
87
|
+
const finalize = (aborted) => {
|
|
88
|
+
if (done) return;
|
|
89
|
+
done = true;
|
|
90
|
+
try {
|
|
91
|
+
snapshot();
|
|
92
|
+
const reqBody = insp.captureRequestBody(req.body, req.headers["content-type"]);
|
|
93
|
+
const resBody = req.method === "HEAD" ? { kind: "none" } : (0, import_core.captureRawBody)(
|
|
94
|
+
chunks,
|
|
95
|
+
total,
|
|
96
|
+
res.getHeader("content-type"),
|
|
97
|
+
res.getHeader("content-encoding"),
|
|
98
|
+
max
|
|
99
|
+
);
|
|
100
|
+
const draft = {
|
|
101
|
+
startedAt,
|
|
102
|
+
durationMs: Math.round((performance.now() - t0) * 10) / 10,
|
|
103
|
+
method: req.method,
|
|
104
|
+
url: req.originalUrl,
|
|
105
|
+
route: snap.route,
|
|
106
|
+
status: res.statusCode,
|
|
107
|
+
...aborted ? { aborted: true } : {},
|
|
108
|
+
request: {
|
|
109
|
+
headers: (0, import_core.normalizeHeaders)(req.headers),
|
|
110
|
+
query: req.query,
|
|
111
|
+
params: snap.params,
|
|
112
|
+
body: reqBody.kind === "binary" && reqBody.size === 0 ? { kind: "none" } : reqBody
|
|
113
|
+
},
|
|
114
|
+
response: { headers: (0, import_core.normalizeHeaders)(res.getHeaders()), body: resBody }
|
|
115
|
+
};
|
|
116
|
+
insp.record(draft);
|
|
117
|
+
} catch (err) {
|
|
118
|
+
(0, import_core.warnOnce)("capture", err);
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
res.once("finish", () => finalize(false));
|
|
122
|
+
res.once("close", () => finalize(true));
|
|
123
|
+
next();
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
var index_default = inspector;
|
|
127
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
128
|
+
0 && (module.exports = {
|
|
129
|
+
inspector
|
|
130
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { RequestHandler } from 'express';
|
|
2
|
+
import { InspectorOptions } from '@req-inspect/core';
|
|
3
|
+
export { InspectorOptions } from '@req-inspect/core';
|
|
4
|
+
|
|
5
|
+
declare function inspector(options?: InspectorOptions): RequestHandler;
|
|
6
|
+
|
|
7
|
+
export { inspector as default, inspector };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { RequestHandler } from 'express';
|
|
2
|
+
import { InspectorOptions } from '@req-inspect/core';
|
|
3
|
+
export { InspectorOptions } from '@req-inspect/core';
|
|
4
|
+
|
|
5
|
+
declare function inspector(options?: InspectorOptions): RequestHandler;
|
|
6
|
+
|
|
7
|
+
export { inspector as default, inspector };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import {
|
|
3
|
+
captureRawBody,
|
|
4
|
+
createInspector,
|
|
5
|
+
normalizeHeaders,
|
|
6
|
+
warnOnce
|
|
7
|
+
} from "@req-inspect/core";
|
|
8
|
+
function inspector(options = {}) {
|
|
9
|
+
const insp = createInspector(options);
|
|
10
|
+
const { enabled, maxBodyBytes: max } = insp.options;
|
|
11
|
+
return (req, res, next) => {
|
|
12
|
+
if (!enabled) return next();
|
|
13
|
+
const q = req.originalUrl.indexOf("?");
|
|
14
|
+
const pathname = q < 0 ? req.originalUrl : req.originalUrl.slice(0, q);
|
|
15
|
+
if (insp.isInspectorPath(pathname)) return insp.handle(req, res);
|
|
16
|
+
if (insp.shouldIgnore(pathname)) return next();
|
|
17
|
+
const startedAt = Date.now();
|
|
18
|
+
const t0 = performance.now();
|
|
19
|
+
const chunks = [];
|
|
20
|
+
let kept = 0;
|
|
21
|
+
let total = 0;
|
|
22
|
+
let done = false;
|
|
23
|
+
let snap;
|
|
24
|
+
const snapshot = () => {
|
|
25
|
+
snap ??= {
|
|
26
|
+
route: req.route ? `${req.baseUrl}${req.route.path}` : null,
|
|
27
|
+
params: { ...req.params }
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
const collect = (args) => {
|
|
31
|
+
try {
|
|
32
|
+
snapshot();
|
|
33
|
+
const chunk = args[0];
|
|
34
|
+
if (typeof chunk !== "string" && !(chunk instanceof Uint8Array)) return;
|
|
35
|
+
const enc = typeof args[1] === "string" ? args[1] : "utf8";
|
|
36
|
+
if (kept >= max) {
|
|
37
|
+
total += typeof chunk === "string" ? Buffer.byteLength(chunk, enc) : chunk.byteLength;
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
const room = max - kept;
|
|
41
|
+
let part;
|
|
42
|
+
if (typeof chunk === "string") {
|
|
43
|
+
const buf = Buffer.from(chunk, enc);
|
|
44
|
+
total += buf.length;
|
|
45
|
+
part = buf.length > room ? Buffer.from(buf.subarray(0, room)) : buf;
|
|
46
|
+
} else {
|
|
47
|
+
const bytes = chunk;
|
|
48
|
+
total += bytes.byteLength;
|
|
49
|
+
part = Buffer.from(bytes.subarray(0, Math.min(bytes.byteLength, room)));
|
|
50
|
+
}
|
|
51
|
+
chunks.push(part);
|
|
52
|
+
kept += part.length;
|
|
53
|
+
} catch (err) {
|
|
54
|
+
warnOnce("capture", err);
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
const origWrite = res.write;
|
|
58
|
+
const origEnd = res.end;
|
|
59
|
+
res.write = function(...args) {
|
|
60
|
+
collect(args);
|
|
61
|
+
return origWrite.apply(this, args);
|
|
62
|
+
};
|
|
63
|
+
res.end = function(...args) {
|
|
64
|
+
collect(args);
|
|
65
|
+
return origEnd.apply(this, args);
|
|
66
|
+
};
|
|
67
|
+
const finalize = (aborted) => {
|
|
68
|
+
if (done) return;
|
|
69
|
+
done = true;
|
|
70
|
+
try {
|
|
71
|
+
snapshot();
|
|
72
|
+
const reqBody = insp.captureRequestBody(req.body, req.headers["content-type"]);
|
|
73
|
+
const resBody = req.method === "HEAD" ? { kind: "none" } : captureRawBody(
|
|
74
|
+
chunks,
|
|
75
|
+
total,
|
|
76
|
+
res.getHeader("content-type"),
|
|
77
|
+
res.getHeader("content-encoding"),
|
|
78
|
+
max
|
|
79
|
+
);
|
|
80
|
+
const draft = {
|
|
81
|
+
startedAt,
|
|
82
|
+
durationMs: Math.round((performance.now() - t0) * 10) / 10,
|
|
83
|
+
method: req.method,
|
|
84
|
+
url: req.originalUrl,
|
|
85
|
+
route: snap.route,
|
|
86
|
+
status: res.statusCode,
|
|
87
|
+
...aborted ? { aborted: true } : {},
|
|
88
|
+
request: {
|
|
89
|
+
headers: normalizeHeaders(req.headers),
|
|
90
|
+
query: req.query,
|
|
91
|
+
params: snap.params,
|
|
92
|
+
body: reqBody.kind === "binary" && reqBody.size === 0 ? { kind: "none" } : reqBody
|
|
93
|
+
},
|
|
94
|
+
response: { headers: normalizeHeaders(res.getHeaders()), body: resBody }
|
|
95
|
+
};
|
|
96
|
+
insp.record(draft);
|
|
97
|
+
} catch (err) {
|
|
98
|
+
warnOnce("capture", err);
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
res.once("finish", () => finalize(false));
|
|
102
|
+
res.once("close", () => finalize(true));
|
|
103
|
+
next();
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
var index_default = inspector;
|
|
107
|
+
export {
|
|
108
|
+
index_default as default,
|
|
109
|
+
inspector
|
|
110
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,61 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@req-inspect/express",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"engines": {
|
|
7
|
+
"node": ">=20"
|
|
8
|
+
},
|
|
9
|
+
"main": "./dist/index.cjs",
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"import": {
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"default": "./dist/index.js"
|
|
16
|
+
},
|
|
17
|
+
"require": {
|
|
18
|
+
"types": "./dist/index.d.cts",
|
|
19
|
+
"default": "./dist/index.cjs"
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"dist"
|
|
25
|
+
],
|
|
26
|
+
"scripts": {
|
|
27
|
+
"build": "tsup"
|
|
28
|
+
},
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"@req-inspect/core": "^0.1.0"
|
|
31
|
+
},
|
|
32
|
+
"peerDependencies": {
|
|
33
|
+
"express": "^4.18 || ^5"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@types/express": "^4.17.21",
|
|
37
|
+
"express4": "npm:express@^4.21",
|
|
38
|
+
"express5": "npm:express@^5.1"
|
|
39
|
+
},
|
|
40
|
+
"repository": {
|
|
41
|
+
"type": "git",
|
|
42
|
+
"url": "git+https://github.com/Hurishihara/req-inspect.git",
|
|
43
|
+
"directory": "packages/express"
|
|
44
|
+
},
|
|
45
|
+
"homepage": "https://github.com/Hurishihara/req-inspect#readme",
|
|
46
|
+
"bugs": {
|
|
47
|
+
"url": "https://github.com/Hurishihara/req-inspect/issues"
|
|
48
|
+
},
|
|
49
|
+
"keywords": [
|
|
50
|
+
"req-inspect",
|
|
51
|
+
"request",
|
|
52
|
+
"inspector",
|
|
53
|
+
"debug",
|
|
54
|
+
"express",
|
|
55
|
+
"middleware"
|
|
56
|
+
],
|
|
57
|
+
"publishConfig": {
|
|
58
|
+
"access": "public",
|
|
59
|
+
"provenance": true
|
|
60
|
+
}
|
|
61
|
+
}
|