@onlineapps/service-wrapper 2.1.60 → 2.1.61
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/package.json +1 -1
- package/src/index.js +29 -0
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -67,6 +67,35 @@ async function bootstrap(serviceRoot, options = {}) {
|
|
|
67
67
|
});
|
|
68
68
|
app.use(accountContextMw);
|
|
69
69
|
|
|
70
|
+
// Express routes are processed in registration order.
|
|
71
|
+
// Services usually register routes inside src/app BEFORE bootstrap runs, so app.use() here would be too late.
|
|
72
|
+
// We must enforce account context BEFORE routes: move our middleware to the beginning of the router stack.
|
|
73
|
+
// Fail-fast if we cannot guarantee correct order.
|
|
74
|
+
if (!app || !app._router || !Array.isArray(app._router.stack) || app._router.stack.length === 0) {
|
|
75
|
+
throw new Error(
|
|
76
|
+
`[service-wrapper][AccountContext] Cannot enforce account context - Express router stack not available. ` +
|
|
77
|
+
`Fix: ensure the service exports an Express app instance with registered routes before bootstrap.`
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const stack = app._router.stack;
|
|
82
|
+
const lastLayer = stack[stack.length - 1];
|
|
83
|
+
const isOurMiddleware =
|
|
84
|
+
lastLayer &&
|
|
85
|
+
lastLayer.handle &&
|
|
86
|
+
typeof lastLayer.handle === 'function' &&
|
|
87
|
+
lastLayer.handle.name === accountContextMw.name;
|
|
88
|
+
|
|
89
|
+
if (!isOurMiddleware) {
|
|
90
|
+
throw new Error(
|
|
91
|
+
`[service-wrapper][AccountContext] Cannot enforce account context - middleware placement is not deterministic. ` +
|
|
92
|
+
`Fix: ensure bootstrap registers account context middleware before routes, or update service to export app factory.`
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
stack.pop();
|
|
97
|
+
stack.unshift(lastLayer);
|
|
98
|
+
|
|
70
99
|
// 1. Start HTTP server
|
|
71
100
|
const PORT = config.service.port;
|
|
72
101
|
const server = app.listen(PORT, () => {
|