request-scope-api 1.0.23 → 1.0.24
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/README.md +37 -0
- package/dist/cjs/config.js +12 -5
- package/dist/cjs/config.js.map +1 -1
- package/dist/cjs/index.js +5 -6
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/middleware.js +19 -17
- package/dist/cjs/middleware.js.map +1 -1
- package/dist/esm/config.js +12 -5
- package/dist/esm/index.js +5 -6
- package/dist/esm/middleware.js +19 -17
- package/dist/types/index.d.ts +1 -1
- package/dist/types/types.d.ts +2 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -21,6 +21,7 @@ import { setup } from 'request-scope-api';
|
|
|
21
21
|
const app = express();
|
|
22
22
|
|
|
23
23
|
setup(app, {
|
|
24
|
+
mode: 'development', // or 'production' to disable tracking
|
|
24
25
|
storage: {
|
|
25
26
|
type: 'mongodb',
|
|
26
27
|
uri: 'mongodb://localhost:27017/myapp'
|
|
@@ -39,6 +40,7 @@ import { setup } from 'request-scope-api';
|
|
|
39
40
|
const app = express();
|
|
40
41
|
|
|
41
42
|
setup(app, {
|
|
43
|
+
mode: 'development', // or 'production' to disable tracking
|
|
42
44
|
storage: {
|
|
43
45
|
type: 'mysql',
|
|
44
46
|
host: 'localhost',
|
|
@@ -64,6 +66,7 @@ import { setup } from 'request-scope-api';
|
|
|
64
66
|
const app = express();
|
|
65
67
|
|
|
66
68
|
setup(app, {
|
|
69
|
+
mode: 'development', // or 'production' to disable tracking
|
|
67
70
|
storage: {
|
|
68
71
|
type: 'postgresql',
|
|
69
72
|
host: 'localhost',
|
|
@@ -88,6 +91,40 @@ app.listen(3000);
|
|
|
88
91
|
- **Performance Metrics**: Records response time and body sizes
|
|
89
92
|
- **Dashboard UI**: Web interface for browsing and filtering requests
|
|
90
93
|
- **Basic Auth**: Optional authentication for dashboard access
|
|
94
|
+
- **Production Mode**: Completely disables tracking in production for security and performance
|
|
95
|
+
|
|
96
|
+
## Mode Configuration
|
|
97
|
+
|
|
98
|
+
The package supports two modes: `development` and `production`.
|
|
99
|
+
|
|
100
|
+
### Development Mode (Default)
|
|
101
|
+
- Enables all request tracking, logging, and data collection
|
|
102
|
+
- Runs retention scheduler and background processing
|
|
103
|
+
- Dashboard is accessible
|
|
104
|
+
- Ideal for local development and testing
|
|
105
|
+
|
|
106
|
+
### Production Mode
|
|
107
|
+
- Completely disables request tracking and data collection
|
|
108
|
+
- No database connections are established
|
|
109
|
+
- No background services or retention jobs run
|
|
110
|
+
- Dashboard is disabled
|
|
111
|
+
- Acts as a no-op middleware for security and performance
|
|
112
|
+
|
|
113
|
+
```javascript
|
|
114
|
+
// Development mode (default)
|
|
115
|
+
setup(app, {
|
|
116
|
+
mode: 'development',
|
|
117
|
+
storage: { /* ... */ }
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
// Production mode - tracking disabled
|
|
121
|
+
setup(app, {
|
|
122
|
+
mode: 'production',
|
|
123
|
+
storage: { /* ... */ }
|
|
124
|
+
});
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
**Note:** In production mode, the package does not establish any database connections and has zero performance impact on your application.
|
|
91
128
|
|
|
92
129
|
## Sensitive Field Masking
|
|
93
130
|
|
package/dist/cjs/config.js
CHANGED
|
@@ -44,6 +44,9 @@ const DEFAULT_SENSITIVE_FIELDS = [
|
|
|
44
44
|
* storage.ssl → false
|
|
45
45
|
*/
|
|
46
46
|
function applyDefaults(config) {
|
|
47
|
+
if (config.mode === undefined) {
|
|
48
|
+
config.mode = 'development';
|
|
49
|
+
}
|
|
47
50
|
if (config.retentionDays === undefined) {
|
|
48
51
|
config.retentionDays = 30;
|
|
49
52
|
}
|
|
@@ -75,25 +78,29 @@ function applyDefaults(config) {
|
|
|
75
78
|
* 4. `retentionDays` (when provided) must be a positive integer in [1, 365].
|
|
76
79
|
*/
|
|
77
80
|
function validateConfig(config) {
|
|
78
|
-
const { storage, retentionDays } = config;
|
|
79
|
-
// 1. Validate
|
|
81
|
+
const { storage, retentionDays, mode } = config;
|
|
82
|
+
// 1. Validate mode
|
|
83
|
+
if (mode !== undefined && mode !== 'development' && mode !== 'production') {
|
|
84
|
+
throw new Error(`Invalid mode: '${mode}'. Supported values are: development, production`);
|
|
85
|
+
}
|
|
86
|
+
// 2. Validate storage.type
|
|
80
87
|
if (!SUPPORTED_STORAGE_TYPES.includes(storage.type)) {
|
|
81
88
|
throw new Error(`Invalid storage type: '${storage.type}'. Supported values are: mongodb, mysql, postgresql`);
|
|
82
89
|
}
|
|
83
|
-
//
|
|
90
|
+
// 3. MongoDB: uri is required
|
|
84
91
|
if (storage.type === 'mongodb') {
|
|
85
92
|
if (!storage.uri) {
|
|
86
93
|
throw new Error("storage.uri is required when storage.type is 'mongodb'");
|
|
87
94
|
}
|
|
88
95
|
}
|
|
89
|
-
//
|
|
96
|
+
// 4. MySQL / PostgreSQL: all SQL required fields must be present
|
|
90
97
|
if (storage.type === 'mysql' || storage.type === 'postgresql') {
|
|
91
98
|
const missing = SQL_REQUIRED_FIELDS.filter((field) => storage[field] === undefined || storage[field] === null);
|
|
92
99
|
if (missing.length > 0) {
|
|
93
100
|
throw new Error(`Missing required storage fields: ${missing.join(', ')}`);
|
|
94
101
|
}
|
|
95
102
|
}
|
|
96
|
-
//
|
|
103
|
+
// 5. retentionDays: must be a positive integer in [1, 365] when provided
|
|
97
104
|
if (retentionDays !== undefined) {
|
|
98
105
|
const isValidInteger = typeof retentionDays === 'number' &&
|
|
99
106
|
Number.isInteger(retentionDays) &&
|
package/dist/cjs/config.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.js","sourceRoot":"","sources":["../../src/config.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;AA0CH,
|
|
1
|
+
{"version":3,"file":"config.js","sourceRoot":"","sources":["../../src/config.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;AA0CH,sCA0BC;AAgBD,wCAiDC;AAaD,wD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AAc,GAClB,OAAO,aAAa,KAAK,QAAQ;YACjC,MAAM,CAAC,SAAS,CAAC,aAAa,CAAC;YAC/B,aAAa,IAAI,CAAC;YAClB,aAAa,IAAI,GAAG,CAAC;QAEvB,IAAI,CAAC,cAAc,EAAE,CAAC;YACpB,MAAM,IAAI,KAAK,CACb,yEAAyE,aAAa,EAAE,CACzF,CAAC;QACJ,CAAC;IACH,CAAC;AACH,CAAC;AAED,8EAA8E;AAC9E,yBAAyB;AACzB,8EAA8E;AAE9E;;;;;;GAMG;AACH,SAAgB,sBAAsB,CAAC,MAA0B;IAC/D,MAAM,KAAK,GAAa;QACtB,GAAG,wBAAwB;QAC3B,GAAG,CAAC,MAAM,CAAC,UAAU,IAAI,EAAE,CAAC;KAC7B,CAAC;IACF,OAAO,IAAI,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,WAAW,EAAE,CAAC,CAAC,CAAC;AACpD,CAAC"}
|
package/dist/cjs/index.js
CHANGED
|
@@ -42,14 +42,13 @@ exports.default = middleware_js_1.requestscope;
|
|
|
42
42
|
* @param adapter - Optional storage adapter instance
|
|
43
43
|
* @returns Express Router
|
|
44
44
|
*/
|
|
45
|
-
function dashboard(config, adapter) {
|
|
46
|
-
// Check
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
process.stderr.write('[RequestScope] Dashboard disabled in production environment (NODE_ENV=production)\n');
|
|
45
|
+
function dashboard(config, adapter, mode = 'development') {
|
|
46
|
+
// Check mode - if production, return disabled router
|
|
47
|
+
if (mode === 'production') {
|
|
48
|
+
process.stderr.write('[RequestScope] Dashboard disabled in production mode\n');
|
|
50
49
|
const router = express_1.default.Router();
|
|
51
50
|
router.use((req, res, next) => {
|
|
52
|
-
res.status(404).send('RequestScope dashboard is disabled in production
|
|
51
|
+
res.status(404).send('RequestScope dashboard is disabled in production mode');
|
|
53
52
|
});
|
|
54
53
|
return router;
|
|
55
54
|
}
|
package/dist/cjs/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;GASG;;;;;;AAwCH,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;GASG;;;;;;AAwCH,8BAYC;AAmCD,4BAEC;AAtFD,sDAA8B;AAC9B,mDAA8F;AA+DrF,6FA/DwC,4BAAY,OA+DxC;AANZ,sFAzDsD,qBAAK,OAyDtD;AAxDd,qDAA8D;AAC9D,+DAAwD;AAWxD,8EAA8E;AAC9E,0BAA0B;AAC1B,8EAA8E;AAE9E;;;;;GAKG;AACH,kBAAe,4BAAsB,CAAC;AAEtC,8EAA8E;AAC9E,2BAA2B;AAC3B,8EAA8E;AAE9E;;;;;;GAMG;AACH,SAAgB,SAAS,CAAC,MAAwB,EAAE,OAAwB,EAAE,OAAqC,aAAa;IAC9H,qDAAqD;IACrD,IAAI,IAAI,KAAK,YAAY,EAAE,CAAC;QAC1B,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,wDAAwD,CAAC,CAAC;QAC/E,MAAM,MAAM,GAAG,iBAAO,CAAC,MAAM,EAAE,CAAC;QAChC,MAAM,CAAC,GAAG,CAAC,CAAC,GAAQ,EAAE,GAAQ,EAAE,IAAS,EAAE,EAAE;YAC3C,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,uDAAuD,CAAC,CAAC;QAChF,CAAC,CAAC,CAAC;QACH,OAAO,MAAM,CAAC;IAChB,CAAC;IAED,OAAO,IAAA,iCAAqB,EAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AAChD,CAAC;AAED,6EAA6E;AAC5E,4BAA8B,CAAC,SAAS,GAAG,SAAS,CAAC;AActD,8EAA8E;AAC9E,oBAAoB;AACpB,8EAA8E;AAE9E;;;;;;;;;;;;;GAaG;AACI,KAAK,UAAU,QAAQ,CAAC,OAA2D;IACxF,MAAM,qCAAe,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;AAC1C,CAAC;AAgBD,8EAA8E;AAC9E,gCAAgC;AAChC,8EAA8E;AAE9E,iDAAyE;AAAhE,uHAAA,YAAY,OAA0B"}
|
package/dist/cjs/middleware.js
CHANGED
|
@@ -50,21 +50,22 @@ const ALWAYS_IGNORED_PATHS = ['/favicon.ico', '/requestscope/api', '/requestscop
|
|
|
50
50
|
* @returns Express RequestHandler middleware
|
|
51
51
|
*/
|
|
52
52
|
function requestscope(config, adapter) {
|
|
53
|
-
// Check if running in production environment
|
|
54
|
-
const nodeEnv = process.env.NODE_ENV;
|
|
55
|
-
if (nodeEnv === 'production') {
|
|
56
|
-
process.stderr.write('[RequestScope] Package disabled in production environment (NODE_ENV=production)\n');
|
|
57
|
-
return (req, res, next) => next();
|
|
58
|
-
}
|
|
59
53
|
// 1. Validate and apply defaults synchronously
|
|
60
54
|
(0, config_js_1.validateConfig)(config);
|
|
61
55
|
(0, config_js_1.applyDefaults)(config);
|
|
56
|
+
// 2. Check mode - if production, act as no-op
|
|
57
|
+
if (config.mode === 'production') {
|
|
58
|
+
process.stderr.write('[RequestScope] Running in production mode - request tracking disabled\n');
|
|
59
|
+
return (req, res, next) => next();
|
|
60
|
+
}
|
|
61
|
+
// 3. Log development mode
|
|
62
|
+
process.stderr.write('[RequestScope] Running in development mode - request tracking enabled\n');
|
|
62
63
|
const sensitiveFields = (0, config_js_1.buildSensitiveFieldSet)(config);
|
|
63
|
-
//
|
|
64
|
+
// 4. Get or create singleton adapter instance
|
|
64
65
|
if (!adapter) {
|
|
65
66
|
adapter = adapter_singleton_js_1.adapterSingleton.getOrCreate(config.storage);
|
|
66
67
|
}
|
|
67
|
-
//
|
|
68
|
+
// 5. Initialize adapter asynchronously (log errors, don't throw)
|
|
68
69
|
// Only initialize if not already initialized
|
|
69
70
|
void adapter.initialize().then(() => {
|
|
70
71
|
adapter_singleton_js_1.adapterSingleton.markInitialized(adapter, config.storage);
|
|
@@ -72,12 +73,12 @@ function requestscope(config, adapter) {
|
|
|
72
73
|
const message = err instanceof Error ? err.message : String(err);
|
|
73
74
|
process.stderr.write(`[RequestScope] Failed to initialize storage adapter: ${message}\n`);
|
|
74
75
|
});
|
|
75
|
-
//
|
|
76
|
+
// 6. Create and start AsyncQueue and RetentionScheduler
|
|
76
77
|
const queue = new queue_js_1.AsyncQueue(adapter);
|
|
77
78
|
const retentionDays = config.retentionDays ?? 30;
|
|
78
79
|
const retentionScheduler = new retention_js_1.RetentionScheduler(adapter, retentionDays);
|
|
79
80
|
retentionScheduler.start();
|
|
80
|
-
//
|
|
81
|
+
// 7. Register resources with shutdown manager
|
|
81
82
|
shutdown_manager_js_1.shutdownManager.registerResources({
|
|
82
83
|
queue: {
|
|
83
84
|
drain: (timeoutMs) => queue.drain(timeoutMs),
|
|
@@ -87,7 +88,7 @@ function requestscope(config, adapter) {
|
|
|
87
88
|
stop: () => retentionScheduler.stop(),
|
|
88
89
|
},
|
|
89
90
|
});
|
|
90
|
-
//
|
|
91
|
+
// 8. Return the request handler middleware
|
|
91
92
|
return async (req, res, next) => {
|
|
92
93
|
// Check ignore list
|
|
93
94
|
const ignoreList = [...ALWAYS_IGNORED_PATHS, ...(config.ignore ?? [])];
|
|
@@ -131,15 +132,16 @@ function requestscope(config, adapter) {
|
|
|
131
132
|
* @param config - RequestScope configuration object
|
|
132
133
|
*/
|
|
133
134
|
function setup(app, config) {
|
|
134
|
-
// Check if running in production environment
|
|
135
|
-
const nodeEnv = process.env.NODE_ENV;
|
|
136
|
-
if (nodeEnv === 'production') {
|
|
137
|
-
process.stderr.write('[RequestScope] Package disabled in production environment (NODE_ENV=production)\n');
|
|
138
|
-
return;
|
|
139
|
-
}
|
|
140
135
|
// Validate and apply defaults
|
|
141
136
|
(0, config_js_1.validateConfig)(config);
|
|
142
137
|
(0, config_js_1.applyDefaults)(config);
|
|
138
|
+
// Check mode - if production, act as no-op
|
|
139
|
+
if (config.mode === 'production') {
|
|
140
|
+
process.stderr.write('[RequestScope] Running in production mode - request tracking disabled\n');
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
// Log development mode
|
|
144
|
+
process.stderr.write('[RequestScope] Running in development mode - request tracking enabled\n');
|
|
143
145
|
// Get or create singleton adapter instance
|
|
144
146
|
const adapter = adapter_singleton_js_1.adapterSingleton.getOrCreate(config.storage);
|
|
145
147
|
// Initialize adapter asynchronously
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"middleware.js","sourceRoot":"","sources":["../../src/middleware.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;GAWG;;AA+CH,
|
|
1
|
+
{"version":3,"file":"middleware.js","sourceRoot":"","sources":["../../src/middleware.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;GAWG;;AA+CH,oCA0FC;AAiBD,sBAmCC;AAsBD,oCAgBC;AA/ND,2CAAoF;AACpF,yCAAwC;AACxC,iDAAoD;AACpD,6CAMsB;AACtB,qDAA8D;AAC9D,iEAA0D;AAC1D,+DAAwD;AAExD,MAAM,oBAAoB,GAAG,CAAC,cAAc,EAAE,mBAAmB,EAAE,sBAAsB,CAAC,CAAC;AAE3F,8EAA8E;AAC9E,mBAAmB;AACnB,8EAA8E;AAE9E;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,SAAgB,YAAY,CAAC,MAA0B,EAAE,OAAwB;IAC/E,+CAA+C;IAC/C,IAAA,0BAAc,EAAC,MAAM,CAAC,CAAC;IACvB,IAAA,yBAAa,EAAC,MAAM,CAAC,CAAC;IAEtB,8CAA8C;IAC9C,IAAI,MAAM,CAAC,IAAI,KAAK,YAAY,EAAE,CAAC;QACjC,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,yEAAyE,CAAC,CAAC;QAChG,OAAO,CAAC,GAAY,EAAE,GAAa,EAAE,IAAkB,EAAE,EAAE,CAAC,IAAI,EAAE,CAAC;IACrE,CAAC;IAED,0BAA0B;IA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CAAC,GAAG,CAAC,qBAAqB,EAAE,OAAO,CAAC,CAAC;IAExC,mGAAmG;IACnG,GAAG,CAAC,GAAG,CAAC,YAAY,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;IAEvC,mCAAmC;IACnC,GAAG,CAAC,GAAG,CAAC,eAAe,EAAE,IAAA,iCAAqB,EAAC,MAAM,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC,CAAC;AAC7E,CAAC;AAED,8EAA8E;AAC9E,mBAAmB;AACnB,8EAA8E;AAE9E;;;;;;;;;;;;;;;GAeG;AACH,SAAgB,YAAY,CAC1B,GAAU,EACV,GAAY,EACZ,GAAa,EACb,IAAkB;IAElB,MAAM,SAAS,GAAc;QAC3B,OAAO,EAAE,GAAG,CAAC,OAAO;QACpB,KAAK,EAAE,GAAG,CAAC,KAAK,IAAI,EAAE;QACtB,UAAU,EAAG,GAAW,CAAC,UAAU,IAAI,GAAG;KAC3C,CAAC;IAED,GAAqD,CAAC,8BAAiB,CAAC,GAAG,SAAS,CAAC;IAEtF,gDAAgD;IAChD,IAAI,CAAC,GAAG,CAAC,CAAC;AACZ,CAAC"}
|
package/dist/esm/config.js
CHANGED
|
@@ -39,6 +39,9 @@ const DEFAULT_SENSITIVE_FIELDS = [
|
|
|
39
39
|
* storage.ssl → false
|
|
40
40
|
*/
|
|
41
41
|
export function applyDefaults(config) {
|
|
42
|
+
if (config.mode === undefined) {
|
|
43
|
+
config.mode = 'development';
|
|
44
|
+
}
|
|
42
45
|
if (config.retentionDays === undefined) {
|
|
43
46
|
config.retentionDays = 30;
|
|
44
47
|
}
|
|
@@ -70,25 +73,29 @@ export function applyDefaults(config) {
|
|
|
70
73
|
* 4. `retentionDays` (when provided) must be a positive integer in [1, 365].
|
|
71
74
|
*/
|
|
72
75
|
export function validateConfig(config) {
|
|
73
|
-
const { storage, retentionDays } = config;
|
|
74
|
-
// 1. Validate
|
|
76
|
+
const { storage, retentionDays, mode } = config;
|
|
77
|
+
// 1. Validate mode
|
|
78
|
+
if (mode !== undefined && mode !== 'development' && mode !== 'production') {
|
|
79
|
+
throw new Error(`Invalid mode: '${mode}'. Supported values are: development, production`);
|
|
80
|
+
}
|
|
81
|
+
// 2. Validate storage.type
|
|
75
82
|
if (!SUPPORTED_STORAGE_TYPES.includes(storage.type)) {
|
|
76
83
|
throw new Error(`Invalid storage type: '${storage.type}'. Supported values are: mongodb, mysql, postgresql`);
|
|
77
84
|
}
|
|
78
|
-
//
|
|
85
|
+
// 3. MongoDB: uri is required
|
|
79
86
|
if (storage.type === 'mongodb') {
|
|
80
87
|
if (!storage.uri) {
|
|
81
88
|
throw new Error("storage.uri is required when storage.type is 'mongodb'");
|
|
82
89
|
}
|
|
83
90
|
}
|
|
84
|
-
//
|
|
91
|
+
// 4. MySQL / PostgreSQL: all SQL required fields must be present
|
|
85
92
|
if (storage.type === 'mysql' || storage.type === 'postgresql') {
|
|
86
93
|
const missing = SQL_REQUIRED_FIELDS.filter((field) => storage[field] === undefined || storage[field] === null);
|
|
87
94
|
if (missing.length > 0) {
|
|
88
95
|
throw new Error(`Missing required storage fields: ${missing.join(', ')}`);
|
|
89
96
|
}
|
|
90
97
|
}
|
|
91
|
-
//
|
|
98
|
+
// 5. retentionDays: must be a positive integer in [1, 365] when provided
|
|
92
99
|
if (retentionDays !== undefined) {
|
|
93
100
|
const isValidInteger = typeof retentionDays === 'number' &&
|
|
94
101
|
Number.isInteger(retentionDays) &&
|
package/dist/esm/index.js
CHANGED
|
@@ -32,14 +32,13 @@ export default requestscopeMiddleware;
|
|
|
32
32
|
* @param adapter - Optional storage adapter instance
|
|
33
33
|
* @returns Express Router
|
|
34
34
|
*/
|
|
35
|
-
export function dashboard(config, adapter) {
|
|
36
|
-
// Check
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
process.stderr.write('[RequestScope] Dashboard disabled in production environment (NODE_ENV=production)\n');
|
|
35
|
+
export function dashboard(config, adapter, mode = 'development') {
|
|
36
|
+
// Check mode - if production, return disabled router
|
|
37
|
+
if (mode === 'production') {
|
|
38
|
+
process.stderr.write('[RequestScope] Dashboard disabled in production mode\n');
|
|
40
39
|
const router = express.Router();
|
|
41
40
|
router.use((req, res, next) => {
|
|
42
|
-
res.status(404).send('RequestScope dashboard is disabled in production
|
|
41
|
+
res.status(404).send('RequestScope dashboard is disabled in production mode');
|
|
43
42
|
});
|
|
44
43
|
return router;
|
|
45
44
|
}
|
package/dist/esm/middleware.js
CHANGED
|
@@ -45,21 +45,22 @@ const ALWAYS_IGNORED_PATHS = ['/favicon.ico', '/requestscope/api', '/requestscop
|
|
|
45
45
|
* @returns Express RequestHandler middleware
|
|
46
46
|
*/
|
|
47
47
|
export function requestscope(config, adapter) {
|
|
48
|
-
// Check if running in production environment
|
|
49
|
-
const nodeEnv = process.env.NODE_ENV;
|
|
50
|
-
if (nodeEnv === 'production') {
|
|
51
|
-
process.stderr.write('[RequestScope] Package disabled in production environment (NODE_ENV=production)\n');
|
|
52
|
-
return (req, res, next) => next();
|
|
53
|
-
}
|
|
54
48
|
// 1. Validate and apply defaults synchronously
|
|
55
49
|
validateConfig(config);
|
|
56
50
|
applyDefaults(config);
|
|
51
|
+
// 2. Check mode - if production, act as no-op
|
|
52
|
+
if (config.mode === 'production') {
|
|
53
|
+
process.stderr.write('[RequestScope] Running in production mode - request tracking disabled\n');
|
|
54
|
+
return (req, res, next) => next();
|
|
55
|
+
}
|
|
56
|
+
// 3. Log development mode
|
|
57
|
+
process.stderr.write('[RequestScope] Running in development mode - request tracking enabled\n');
|
|
57
58
|
const sensitiveFields = buildSensitiveFieldSet(config);
|
|
58
|
-
//
|
|
59
|
+
// 4. Get or create singleton adapter instance
|
|
59
60
|
if (!adapter) {
|
|
60
61
|
adapter = adapterSingleton.getOrCreate(config.storage);
|
|
61
62
|
}
|
|
62
|
-
//
|
|
63
|
+
// 5. Initialize adapter asynchronously (log errors, don't throw)
|
|
63
64
|
// Only initialize if not already initialized
|
|
64
65
|
void adapter.initialize().then(() => {
|
|
65
66
|
adapterSingleton.markInitialized(adapter, config.storage);
|
|
@@ -67,12 +68,12 @@ export function requestscope(config, adapter) {
|
|
|
67
68
|
const message = err instanceof Error ? err.message : String(err);
|
|
68
69
|
process.stderr.write(`[RequestScope] Failed to initialize storage adapter: ${message}\n`);
|
|
69
70
|
});
|
|
70
|
-
//
|
|
71
|
+
// 6. Create and start AsyncQueue and RetentionScheduler
|
|
71
72
|
const queue = new AsyncQueue(adapter);
|
|
72
73
|
const retentionDays = config.retentionDays ?? 30;
|
|
73
74
|
const retentionScheduler = new RetentionScheduler(adapter, retentionDays);
|
|
74
75
|
retentionScheduler.start();
|
|
75
|
-
//
|
|
76
|
+
// 7. Register resources with shutdown manager
|
|
76
77
|
shutdownManager.registerResources({
|
|
77
78
|
queue: {
|
|
78
79
|
drain: (timeoutMs) => queue.drain(timeoutMs),
|
|
@@ -82,7 +83,7 @@ export function requestscope(config, adapter) {
|
|
|
82
83
|
stop: () => retentionScheduler.stop(),
|
|
83
84
|
},
|
|
84
85
|
});
|
|
85
|
-
//
|
|
86
|
+
// 8. Return the request handler middleware
|
|
86
87
|
return async (req, res, next) => {
|
|
87
88
|
// Check ignore list
|
|
88
89
|
const ignoreList = [...ALWAYS_IGNORED_PATHS, ...(config.ignore ?? [])];
|
|
@@ -126,15 +127,16 @@ export function requestscope(config, adapter) {
|
|
|
126
127
|
* @param config - RequestScope configuration object
|
|
127
128
|
*/
|
|
128
129
|
export function setup(app, config) {
|
|
129
|
-
// Check if running in production environment
|
|
130
|
-
const nodeEnv = process.env.NODE_ENV;
|
|
131
|
-
if (nodeEnv === 'production') {
|
|
132
|
-
process.stderr.write('[RequestScope] Package disabled in production environment (NODE_ENV=production)\n');
|
|
133
|
-
return;
|
|
134
|
-
}
|
|
135
130
|
// Validate and apply defaults
|
|
136
131
|
validateConfig(config);
|
|
137
132
|
applyDefaults(config);
|
|
133
|
+
// Check mode - if production, act as no-op
|
|
134
|
+
if (config.mode === 'production') {
|
|
135
|
+
process.stderr.write('[RequestScope] Running in production mode - request tracking disabled\n');
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
// Log development mode
|
|
139
|
+
process.stderr.write('[RequestScope] Running in development mode - request tracking enabled\n');
|
|
138
140
|
// Get or create singleton adapter instance
|
|
139
141
|
const adapter = adapterSingleton.getOrCreate(config.storage);
|
|
140
142
|
// Initialize adapter asynchronously
|
package/dist/types/index.d.ts
CHANGED
|
@@ -25,7 +25,7 @@ export default requestscopeMiddleware;
|
|
|
25
25
|
* @param adapter - Optional storage adapter instance
|
|
26
26
|
* @returns Express Router
|
|
27
27
|
*/
|
|
28
|
-
export declare function dashboard(config?: DashboardConfig, adapter?: StorageAdapter): express.Router;
|
|
28
|
+
export declare function dashboard(config?: DashboardConfig, adapter?: StorageAdapter, mode?: 'development' | 'production'): express.Router;
|
|
29
29
|
export { setup };
|
|
30
30
|
export { errorHandler };
|
|
31
31
|
/**
|
package/dist/types/types.d.ts
CHANGED
|
@@ -69,6 +69,8 @@ export interface DashboardConfig {
|
|
|
69
69
|
}
|
|
70
70
|
export interface RequestScopeConfig {
|
|
71
71
|
storage: StorageConfig;
|
|
72
|
+
/** Package mode: 'development' enables all features, 'production' disables all tracking. Default: 'development' */
|
|
73
|
+
mode?: 'development' | 'production';
|
|
72
74
|
/** Number of days to retain records. Range: 1–365. Default: 30 */
|
|
73
75
|
retentionDays?: number;
|
|
74
76
|
/** Exact URL paths to skip (no capture). Default: [] */
|