@webpieces/http-server 0.2.12 → 0.2.13

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.
@@ -1,16 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.WebpiecesServer = void 0;
4
- const tslib_1 = require("tslib");
5
- const express_1 = tslib_1.__importDefault(require("express"));
6
4
  const inversify_1 = require("inversify");
7
5
  const binding_decorators_1 = require("@inversifyjs/binding-decorators");
8
- const core_meta_1 = require("@webpieces/core-meta");
9
- const http_filters_1 = require("@webpieces/http-filters");
10
- const http_routing_1 = require("@webpieces/http-routing");
11
- const RouteBuilderImpl_1 = require("./RouteBuilderImpl");
12
- const FilterMatcher_1 = require("./FilterMatcher");
13
- const core_util_1 = require("@webpieces/core-util");
6
+ const WebpiecesCoreServer_1 = require("./WebpiecesCoreServer");
14
7
  /**
15
8
  * WebpiecesServer - Main bootstrap class for WebPieces applications.
16
9
  *
@@ -45,310 +38,34 @@ const core_util_1 = require("@webpieces/core-util");
45
38
  */
46
39
  class WebpiecesServer {
47
40
  constructor(meta) {
48
- /**
49
- * Routes registry: Maps "METHOD:path" -> RegisteredRoute
50
- * Example: "POST:/search/item" -> { method: "POST", path: "/search/item", handler: ... }
51
- *
52
- * We use unknown instead of any for type safety - each route has its own return type,
53
- * but we can't have different generic types in the same Map.
54
- */
55
- this.routes = new Map();
56
- /**
57
- * Registered filters with their definitions.
58
- * Used by FilterMatcher to match filters to routes based on filepath patterns.
59
- */
60
- this.filterRegistry = [];
61
- this.initialized = false;
62
- this.port = 8080;
63
41
  this.meta = meta;
64
42
  // Create WebPieces container for framework-level bindings
65
43
  this.webpiecesContainer = new inversify_1.Container();
66
- // Create application container as a child of WebPieces container
67
- // This allows app container to access framework bindings
68
- this.appContainer = new inversify_1.Container({ parent: this.webpiecesContainer });
44
+ // Load buildProviderModule to auto-scan for @provideSingleton decorators
45
+ // This registers framework classes (WebpiecesCoreServer, RouteBuilderImpl)
46
+ this.webpiecesContainer.load((0, binding_decorators_1.buildProviderModule)());
47
+ // Resolve WebpiecesCoreServer from DI container (proper DI - no 'new'!)
48
+ this.coreService = this.webpiecesContainer.get(WebpiecesCoreServer_1.WebpiecesCoreServer);
69
49
  }
70
50
  /**
71
51
  * Initialize the server (DI container, routes, filters).
72
52
  * This is called automatically by start() or can be called manually for testing.
73
53
  */
74
54
  initialize() {
75
- if (this.initialized) {
76
- return;
77
- }
78
- // 1. Load DI modules
79
- this.loadDIModules();
80
- // 2. Register routes and filters
81
- this.registerRoutes();
82
- this.initialized = true;
83
- }
84
- /**
85
- * Load DI modules from WebAppMeta.
86
- *
87
- * Currently, all user modules are loaded into the application container.
88
- * In the future, we could separate:
89
- * - WebPieces framework modules -> webpiecesContainer
90
- * - Application modules -> appContainer
91
- *
92
- * For now, everything goes into appContainer which has access to webpiecesContainer.
93
- */
94
- loadDIModules() {
95
- const modules = this.meta.getDIModules();
96
- // Load buildProviderModule to auto-scan for @provideSingleton decorators
97
- this.appContainer.load((0, binding_decorators_1.buildProviderModule)());
98
- // Load all modules into application container
99
- // (webpiecesContainer is currently empty, reserved for future framework bindings)
100
- for (const module of modules) {
101
- this.appContainer.load(module);
102
- }
103
- }
104
- /**
105
- * Register routes from WebAppMeta.
106
- *
107
- * Creates an explicit RouteBuilderImpl instead of an anonymous object.
108
- * This improves:
109
- * - Traceability: Can Cmd+Click on addRoute to see implementation
110
- * - Debugging: Explicit class shows up in stack traces
111
- * - Understanding: Clear class name vs anonymous object
112
- */
113
- registerRoutes() {
114
- const routeConfigs = this.meta.getRoutes();
115
- // Create explicit RouteBuilder implementation
116
- // Filters are resolved from appContainer (which has access to platformContainer too)
117
- const routeBuilder = new RouteBuilderImpl_1.RouteBuilderImpl(this.routes, this.filterRegistry, this.appContainer);
118
- // Configure routes using the explicit RouteBuilder
119
- for (const routeConfig of routeConfigs) {
120
- routeConfig.configure(routeBuilder);
121
- }
55
+ this.coreService.initialize(this.webpiecesContainer, this.meta);
122
56
  }
123
57
  /**
124
58
  * Start the HTTP server with Express.
125
59
  */
126
60
  start(port = 8080) {
127
- this.port = port;
128
61
  this.initialize();
129
- // Create Express app
130
- this.app = (0, express_1.default)();
131
- // Layer 1: Global Error Handler (OUTERMOST - runs FIRST)
132
- // Wraps all subsequent middleware with try-catch
133
- // IMPORTANT: Use async/await to catch BOTH synchronous throws AND async rejections
134
- this.app.use(async (req, res, next) => {
135
- console.log('🔴 [Layer 1: GlobalErrorHandler] Request START:', req.method, req.path);
136
- try {
137
- // await next() catches BOTH:
138
- // 1. Synchronous throws from next() itself
139
- // 2. Rejected promises from downstream async middleware
140
- await next();
141
- console.log('🔴 [Layer 1: GlobalErrorHandler] Request END (success):', req.method, req.path);
142
- }
143
- catch (err) {
144
- const error = (0, core_util_1.toError)(err);
145
- console.error('🔴 [Layer 1: GlobalErrorHandler] Caught unhandled error:', error);
146
- if (!res.headersSent) {
147
- // Return HTML error page (not JSON - JsonFilter handles JSON errors)
148
- res.status(500).send(`
149
- <!DOCTYPE html>
150
- <html>
151
- <head><title>Server Error</title></head>
152
- <body>
153
- <h1>You hit a server error</h1>
154
- <p>An unexpected error occurred while processing your request.</p>
155
- <pre>${error.message}</pre>
156
- </body>
157
- </html>
158
- `);
159
- }
160
- console.log('🔴 [Layer 1: GlobalErrorHandler] Request END (error):', req.method, req.path);
161
- }
162
- });
163
- // Layer 2: Log Next Layer (runs SECOND)
164
- this.app.use((req, res, next) => {
165
- console.log('🟡 [Layer 2: LogNextLayer] Before next() -', req.method, req.path);
166
- next();
167
- console.log('🟡 [Layer 2: LogNextLayer] After next() -', req.method, req.path);
168
- });
169
- // Layer 3+: Standard Express middleware
170
- this.app.use(express_1.default.json());
171
- this.app.use(express_1.default.urlencoded({ extended: true }));
172
- // Register routes (these become the innermost handlers)
173
- this.registerExpressRoutes();
174
- // Start listening
175
- this.server = this.app.listen(this.port, () => {
176
- console.log(`[WebpiecesServer] Server listening on http://localhost:${this.port}`);
177
- console.log(`[WebpiecesServer] Registered ${this.routes.size} routes`);
178
- console.log(`[WebpiecesServer] Registered ${this.filterRegistry.length} filters`);
179
- });
180
- }
181
- /**
182
- * Handle an incoming HTTP request through the filter chain and controller.
183
- * This is the main request processing logic.
184
- *
185
- * NO try-catch here - errors are handled by:
186
- * 1. JsonFilter - catches and returns JSON error responses
187
- * 2. GlobalErrorHandler middleware - catches any unhandled errors and returns HTML 500
188
- *
189
- * @param req - Express request
190
- * @param res - Express response
191
- * @param route - The registered route to execute
192
- * @param matchingFilters - Filters that apply to this route
193
- * @param key - Route key (method:path)
194
- */
195
- async handleRequest(req, res, route, matchingFilters, key) {
196
- // Create method metadata
197
- const meta = new http_filters_1.MethodMeta(route.method, route.path, key, [req.body], new core_meta_1.RouteRequest(req.body, req.query, req.params, req.headers), undefined, new Map());
198
- // Create filter chain with matched filters
199
- const filterChain = new http_filters_1.FilterChain(matchingFilters);
200
- // Execute the filter chain
201
- // Errors thrown here are caught by JsonFilter or bubble to GlobalErrorHandler
202
- const action = await filterChain.execute(meta, async () => {
203
- // Create typed route context
204
- // Use appContainer which has access to both app and framework bindings
205
- const routeContext = new core_meta_1.RouteContext(this.appContainer, [req.body], meta.request);
206
- // Final handler: invoke the controller method via route handler
207
- const result = await route.handler.execute(routeContext);
208
- // Wrap result in a JSON action
209
- return (0, http_filters_1.jsonAction)(result);
210
- });
211
- // Send response
212
- if (action.type === 'json') {
213
- res.json(action.data);
214
- }
215
- else if (action.type === 'error') {
216
- res.status(500).json({ error: action.data });
217
- }
218
- }
219
- /**
220
- * Register all routes with Express.
221
- */
222
- registerExpressRoutes() {
223
- if (!this.app) {
224
- throw new Error('Express app not initialized');
225
- }
226
- for (const [key, route] of this.routes.entries()) {
227
- this.setupRoute(key, route);
228
- }
229
- }
230
- /**
231
- * Setup a single route with Express.
232
- * Finds matching filters, creates handler, and registers with Express.
233
- *
234
- * @param key - Route key (method:path)
235
- * @param route - The registered route definition
236
- */
237
- setupRoute(key, route) {
238
- if (!this.app) {
239
- throw new Error('Express app not initialized');
240
- }
241
- const method = route.method.toLowerCase();
242
- const path = route.path;
243
- console.log(`[WebpiecesServer] Registering route: ${method.toUpperCase()} ${path}`);
244
- // Find matching filters for this route
245
- const matchingFilters = FilterMatcher_1.FilterMatcher.findMatchingFilters(route.controllerFilepath, this.filterRegistry);
246
- // Create Express route handler - delegates to handleRequest
247
- const handler = async (req, res, next) => {
248
- await this.handleRequest(req, res, route, matchingFilters, key);
249
- };
250
- // Register with Express
251
- switch (method) {
252
- case 'get':
253
- this.app.get(path, handler);
254
- break;
255
- case 'post':
256
- this.app.post(path, handler);
257
- break;
258
- case 'put':
259
- this.app.put(path, handler);
260
- break;
261
- case 'delete':
262
- this.app.delete(path, handler);
263
- break;
264
- case 'patch':
265
- this.app.patch(path, handler);
266
- break;
267
- default:
268
- console.warn(`[WebpiecesServer] Unknown HTTP method: ${method}`);
269
- }
62
+ this.coreService.start(port);
270
63
  }
271
64
  /**
272
65
  * Stop the HTTP server.
273
66
  */
274
67
  stop() {
275
- if (this.server) {
276
- this.server.close(() => {
277
- console.log('[WebpiecesServer] Server stopped');
278
- });
279
- }
280
- }
281
- /**
282
- * Create an API client proxy for testing (no HTTP).
283
- *
284
- * This creates a proxy object that implements the API interface
285
- * and routes method calls through the full filter chain to the controller.
286
- *
287
- * @param apiMetaClass - The API interface class with decorators
288
- * @returns Proxy object implementing the API interface
289
- */
290
- createApiClient(apiMetaClass) {
291
- this.initialize();
292
- // Get routes from the API metadata
293
- const routes = (0, http_routing_1.getRoutes)(apiMetaClass);
294
- // Create a proxy object
295
- const proxy = {};
296
- for (const route of routes) {
297
- const methodName = route.methodName;
298
- // Create a function that routes through the filter chain
299
- proxy[methodName] = async (...args) => {
300
- return this.invokeRoute(route, args);
301
- };
302
- }
303
- return proxy;
304
- }
305
- /**
306
- * Invoke a route through the filter chain.
307
- */
308
- async invokeRoute(route, args) {
309
- // Find the registered route
310
- const key = `${route.httpMethod}:${route.path}`;
311
- const registeredRoute = this.routes.get(key);
312
- if (!registeredRoute) {
313
- throw new Error(`Route not found: ${key}`);
314
- }
315
- // Find matching filters for this route
316
- const matchingFilters = FilterMatcher_1.FilterMatcher.findMatchingFilters(registeredRoute.controllerFilepath, this.filterRegistry);
317
- // Create method metadata
318
- const meta = new http_filters_1.MethodMeta(route.httpMethod, route.path, route.methodName, [...args], new core_meta_1.RouteRequest(args[0]), // Assume first arg is the request body
319
- undefined, new Map());
320
- // Create filter chain with matched filters
321
- const filterChain = new http_filters_1.FilterChain(matchingFilters);
322
- // Execute the filter chain
323
- const action = await filterChain.execute(meta, async () => {
324
- // Create typed route context
325
- // Use appContainer which has access to both app and framework bindings
326
- const routeContext = new core_meta_1.RouteContext(this.appContainer, meta.params, meta.request);
327
- // Final handler: invoke the controller method via route handler
328
- const result = await registeredRoute.handler.execute(routeContext);
329
- // Wrap result in a JSON action
330
- return (0, http_filters_1.jsonAction)(result);
331
- });
332
- // Return the data from the action
333
- if (action.type === 'error') {
334
- throw new Error(JSON.stringify(action.data));
335
- }
336
- return action.data;
337
- }
338
- /**
339
- * Get the application DI container (for testing).
340
- * Returns appContainer which has access to both app and framework bindings.
341
- */
342
- getContainer() {
343
- this.initialize();
344
- return this.appContainer;
345
- }
346
- /**
347
- * Get the WebPieces framework container (for advanced testing/debugging).
348
- */
349
- getWebpiecesContainer() {
350
- this.initialize();
351
- return this.webpiecesContainer;
68
+ this.coreService.stop();
352
69
  }
353
70
  }
354
71
  exports.WebpiecesServer = WebpiecesServer;
@@ -1 +1 @@
1
- {"version":3,"file":"WebpiecesServer.js","sourceRoot":"","sources":["../../../../../packages/http/http-server/src/WebpiecesServer.ts"],"names":[],"mappings":";;;;AAAA,8DAA4E;AAC5E,yCAAsC;AACtC,wEAAsE;AACtE,oDAAgG;AAChG,0DAAsF;AACtF,0DAAmE;AACnE,yDAAuE;AACvE,mDAAgD;AAChD,oDAA+C;AAE/C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH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AK,OAAO,EAAE,CAAC;YAC5B,MAAM,IAAI,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC;QAC/C,CAAC;QAED,OAAO,MAAM,CAAC,IAAI,CAAC;IACrB,CAAC;IAED;;;OAGG;IACH,YAAY;QACV,IAAI,CAAC,UAAU,EAAE,CAAC;QAClB,OAAO,IAAI,CAAC,YAAY,CAAC;IAC3B,CAAC;IAED;;OAEG;IACH,qBAAqB;QACnB,IAAI,CAAC,UAAU,EAAE,CAAC;QAClB,OAAO,IAAI,CAAC,kBAAkB,CAAC;IACjC,CAAC;CAGF;AApaD,0CAoaC","sourcesContent":["import express, { Express, Request, Response, NextFunction } from 'express';\nimport { Container } from 'inversify';\nimport { buildProviderModule } from '@inversifyjs/binding-decorators';\nimport { WebAppMeta, RouteContext, RouteRequest, FilterDefinition } from '@webpieces/core-meta';\nimport { FilterChain, Filter, MethodMeta, jsonAction } from '@webpieces/http-filters';\nimport { getRoutes, RouteMetadata } from '@webpieces/http-routing';\nimport { RouteBuilderImpl, RegisteredRoute } from './RouteBuilderImpl';\nimport { FilterMatcher } from './FilterMatcher';\nimport { toError } from '@webpieces/core-util';\n\n/**\n * WebpiecesServer - Main bootstrap class for WebPieces applications.\n *\n * This class uses a two-container pattern similar to Java WebPieces:\n * 1. webpiecesContainer: Core WebPieces framework bindings\n * 2. appContainer: User's application bindings (child of webpiecesContainer)\n *\n * This separation allows:\n * - Clean separation of concerns\n * - Better testability\n * - Ability to override framework bindings in tests\n *\n * The server:\n * 1. Initializes both DI containers from WebAppMeta.getDIModules()\n * 2. Registers routes using explicit RouteBuilderImpl\n * 3. Creates filter chains\n * 4. Supports both HTTP server mode and testing mode (no HTTP)\n *\n * Usage for testing (no HTTP):\n * ```typescript\n * const server = new WebpiecesServer(new ProdServerMeta());\n * server.initialize();\n * const saveApi = server.createApiClient<SaveApi>(SaveApiPrototype);\n * const response = await saveApi.save(request);\n * ```\n *\n * Usage for production (HTTP server):\n * ```typescript\n * const server = new WebpiecesServer(new ProdServerMeta());\n * server.start(); // Starts Express server\n * ```\n */\nexport class WebpiecesServer {\n private meta: WebAppMeta;\n\n /**\n * WebPieces container: Core WebPieces framework bindings.\n * This includes framework-level services like filters, routing infrastructure,\n * logging, metrics, etc. Similar to Java WebPieces platform container.\n */\n private webpiecesContainer: Container;\n\n /**\n * Application container: User's application bindings.\n * This is a child container of webpiecesContainer, so it can access\n * framework bindings while keeping app bindings separate.\n */\n private appContainer: Container;\n\n /**\n * Routes registry: Maps \"METHOD:path\" -> RegisteredRoute\n * Example: \"POST:/search/item\" -> { method: \"POST\", path: \"/search/item\", handler: ... }\n *\n * We use unknown instead of any for type safety - each route has its own return type,\n * but we can't have different generic types in the same Map.\n */\n private routes: Map<string, RegisteredRoute<unknown>> = new Map();\n\n /**\n * Registered filters with their definitions.\n * Used by FilterMatcher to match filters to routes based on filepath patterns.\n */\n private filterRegistry: Array<{ filter: Filter; definition: FilterDefinition }> = [];\n\n private initialized = false;\n private app?: Express;\n private server?: any;\n private port: number = 8080;\n\n constructor(meta: WebAppMeta) {\n this.meta = meta;\n\n // Create WebPieces container for framework-level bindings\n this.webpiecesContainer = new Container();\n\n // Create application container as a child of WebPieces container\n // This allows app container to access framework bindings\n this.appContainer = new Container({ parent: this.webpiecesContainer });\n }\n\n /**\n * Initialize the server (DI container, routes, filters).\n * This is called automatically by start() or can be called manually for testing.\n */\n initialize(): void {\n if (this.initialized) {\n return;\n }\n\n // 1. Load DI modules\n this.loadDIModules();\n\n // 2. Register routes and filters\n this.registerRoutes();\n\n this.initialized = true;\n }\n\n /**\n * Load DI modules from WebAppMeta.\n *\n * Currently, all user modules are loaded into the application container.\n * In the future, we could separate:\n * - WebPieces framework modules -> webpiecesContainer\n * - Application modules -> appContainer\n *\n * For now, everything goes into appContainer which has access to webpiecesContainer.\n */\n private loadDIModules(): void {\n const modules = this.meta.getDIModules();\n\n // Load buildProviderModule to auto-scan for @provideSingleton decorators\n this.appContainer.load(buildProviderModule());\n\n // Load all modules into application container\n // (webpiecesContainer is currently empty, reserved for future framework bindings)\n for (const module of modules) {\n this.appContainer.load(module);\n }\n }\n\n /**\n * Register routes from WebAppMeta.\n *\n * Creates an explicit RouteBuilderImpl instead of an anonymous object.\n * This improves:\n * - Traceability: Can Cmd+Click on addRoute to see implementation\n * - Debugging: Explicit class shows up in stack traces\n * - Understanding: Clear class name vs anonymous object\n */\n private registerRoutes(): void {\n const routeConfigs = this.meta.getRoutes();\n\n // Create explicit RouteBuilder implementation\n // Filters are resolved from appContainer (which has access to platformContainer too)\n const routeBuilder = new RouteBuilderImpl(\n this.routes,\n this.filterRegistry,\n this.appContainer\n );\n\n // Configure routes using the explicit RouteBuilder\n for (const routeConfig of routeConfigs) {\n routeConfig.configure(routeBuilder);\n }\n }\n\n /**\n * Start the HTTP server with Express.\n */\n start(port: number = 8080): void {\n this.port = port;\n this.initialize();\n\n // Create Express app\n this.app = express();\n\n // Layer 1: Global Error Handler (OUTERMOST - runs FIRST)\n // Wraps all subsequent middleware with try-catch\n // IMPORTANT: Use async/await to catch BOTH synchronous throws AND async rejections\n this.app.use(async (req: Request, res: Response, next: NextFunction) => {\n console.log('🔴 [Layer 1: GlobalErrorHandler] Request START:', req.method, req.path);\n\n try {\n // await next() catches BOTH:\n // 1. Synchronous throws from next() itself\n // 2. Rejected promises from downstream async middleware\n await next();\n console.log('🔴 [Layer 1: GlobalErrorHandler] Request END (success):', req.method, req.path);\n } catch (err: any) {\n const error = toError(err);\n console.error('🔴 [Layer 1: GlobalErrorHandler] Caught unhandled error:', error);\n if (!res.headersSent) {\n // Return HTML error page (not JSON - JsonFilter handles JSON errors)\n res.status(500).send(`\n <!DOCTYPE html>\n <html>\n <head><title>Server Error</title></head>\n <body>\n <h1>You hit a server error</h1>\n <p>An unexpected error occurred while processing your request.</p>\n <pre>${error.message}</pre>\n </body>\n </html>\n `);\n }\n console.log('🔴 [Layer 1: GlobalErrorHandler] Request END (error):', req.method, req.path);\n }\n });\n\n // Layer 2: Log Next Layer (runs SECOND)\n this.app.use((req: Request, res: Response, next: NextFunction) => {\n console.log('🟡 [Layer 2: LogNextLayer] Before next() -', req.method, req.path);\n next();\n console.log('🟡 [Layer 2: LogNextLayer] After next() -', req.method, req.path);\n });\n\n // Layer 3+: Standard Express middleware\n this.app.use(express.json());\n this.app.use(express.urlencoded({ extended: true }));\n\n // Register routes (these become the innermost handlers)\n this.registerExpressRoutes();\n\n // Start listening\n this.server = this.app.listen(this.port, () => {\n console.log(`[WebpiecesServer] Server listening on http://localhost:${this.port}`);\n console.log(`[WebpiecesServer] Registered ${this.routes.size} routes`);\n console.log(`[WebpiecesServer] Registered ${this.filterRegistry.length} filters`);\n });\n }\n\n /**\n * Handle an incoming HTTP request through the filter chain and controller.\n * This is the main request processing logic.\n *\n * NO try-catch here - errors are handled by:\n * 1. JsonFilter - catches and returns JSON error responses\n * 2. GlobalErrorHandler middleware - catches any unhandled errors and returns HTML 500\n *\n * @param req - Express request\n * @param res - Express response\n * @param route - The registered route to execute\n * @param matchingFilters - Filters that apply to this route\n * @param key - Route key (method:path)\n */\n private async handleRequest(\n req: Request,\n res: Response,\n route: RegisteredRoute<unknown>,\n matchingFilters: Filter[],\n key: string\n ): Promise<void> {\n // Create method metadata\n const meta = new MethodMeta(\n route.method,\n route.path,\n key,\n [req.body],\n new RouteRequest(req.body, req.query, req.params, req.headers),\n undefined,\n new Map()\n );\n\n // Create filter chain with matched filters\n const filterChain = new FilterChain(matchingFilters);\n\n // Execute the filter chain\n // Errors thrown here are caught by JsonFilter or bubble to GlobalErrorHandler\n const action = await filterChain.execute(meta, async () => {\n // Create typed route context\n // Use appContainer which has access to both app and framework bindings\n const routeContext = new RouteContext(\n this.appContainer,\n [req.body],\n meta.request\n );\n\n // Final handler: invoke the controller method via route handler\n const result = await route.handler.execute(routeContext);\n\n // Wrap result in a JSON action\n return jsonAction(result);\n });\n\n // Send response\n if (action.type === 'json') {\n res.json(action.data);\n } else if (action.type === 'error') {\n res.status(500).json({ error: action.data });\n }\n }\n\n /**\n * Register all routes with Express.\n */\n private registerExpressRoutes(): void {\n if (!this.app) {\n throw new Error('Express app not initialized');\n }\n\n for (const [key, route] of this.routes.entries()) {\n this.setupRoute(key, route);\n }\n }\n\n /**\n * Setup a single route with Express.\n * Finds matching filters, creates handler, and registers with Express.\n *\n * @param key - Route key (method:path)\n * @param route - The registered route definition\n */\n private setupRoute(key: string, route: RegisteredRoute<unknown>): void {\n if (!this.app) {\n throw new Error('Express app not initialized');\n }\n\n const method = route.method.toLowerCase();\n const path = route.path;\n\n console.log(`[WebpiecesServer] Registering route: ${method.toUpperCase()} ${path}`);\n\n // Find matching filters for this route\n const matchingFilters = FilterMatcher.findMatchingFilters(\n route.controllerFilepath,\n this.filterRegistry\n );\n\n // Create Express route handler - delegates to handleRequest\n const handler = async (req: Request, res: Response, next: NextFunction) => {\n await this.handleRequest(req, res, route, matchingFilters, key);\n };\n\n // Register with Express\n switch (method) {\n case 'get':\n this.app.get(path, handler);\n break;\n case 'post':\n this.app.post(path, handler);\n break;\n case 'put':\n this.app.put(path, handler);\n break;\n case 'delete':\n this.app.delete(path, handler);\n break;\n case 'patch':\n this.app.patch(path, handler);\n break;\n default:\n console.warn(`[WebpiecesServer] Unknown HTTP method: ${method}`);\n }\n }\n\n /**\n * Stop the HTTP server.\n */\n stop(): void {\n if (this.server) {\n this.server.close(() => {\n console.log('[WebpiecesServer] Server stopped');\n });\n }\n }\n\n /**\n * Create an API client proxy for testing (no HTTP).\n *\n * This creates a proxy object that implements the API interface\n * and routes method calls through the full filter chain to the controller.\n *\n * @param apiMetaClass - The API interface class with decorators\n * @returns Proxy object implementing the API interface\n */\n createApiClient<T>(apiMetaClass: any): T {\n this.initialize();\n\n // Get routes from the API metadata\n const routes = getRoutes(apiMetaClass);\n\n // Create a proxy object\n const proxy: any = {};\n\n for (const route of routes) {\n const methodName = route.methodName;\n\n // Create a function that routes through the filter chain\n proxy[methodName] = async (...args: any[]) => {\n return this.invokeRoute(route, args);\n };\n }\n\n return proxy as T;\n }\n\n /**\n * Invoke a route through the filter chain.\n */\n private async invokeRoute(route: RouteMetadata, args: any[]): Promise<any> {\n // Find the registered route\n const key = `${route.httpMethod}:${route.path}`;\n const registeredRoute = this.routes.get(key);\n\n if (!registeredRoute) {\n throw new Error(`Route not found: ${key}`);\n }\n\n // Find matching filters for this route\n const matchingFilters = FilterMatcher.findMatchingFilters(\n registeredRoute.controllerFilepath,\n this.filterRegistry\n );\n\n // Create method metadata\n const meta = new MethodMeta(\n route.httpMethod,\n route.path,\n route.methodName,\n [...args],\n new RouteRequest(args[0]), // Assume first arg is the request body\n undefined,\n new Map()\n );\n\n // Create filter chain with matched filters\n const filterChain = new FilterChain(matchingFilters);\n\n // Execute the filter chain\n const action = await filterChain.execute(meta, async () => {\n // Create typed route context\n // Use appContainer which has access to both app and framework bindings\n const routeContext = new RouteContext(\n this.appContainer,\n meta.params,\n meta.request\n );\n\n // Final handler: invoke the controller method via route handler\n const result = await registeredRoute.handler.execute(routeContext);\n\n // Wrap result in a JSON action\n return jsonAction(result);\n });\n\n // Return the data from the action\n if (action.type === 'error') {\n throw new Error(JSON.stringify(action.data));\n }\n\n return action.data;\n }\n\n /**\n * Get the application DI container (for testing).\n * Returns appContainer which has access to both app and framework bindings.\n */\n getContainer(): Container {\n this.initialize();\n return this.appContainer;\n }\n\n /**\n * Get the WebPieces framework container (for advanced testing/debugging).\n */\n getWebpiecesContainer(): Container {\n this.initialize();\n return this.webpiecesContainer;\n }\n\n\n}\n"]}
1
+ {"version":3,"file":"WebpiecesServer.js","sourceRoot":"","sources":["../../../../../packages/http/http-server/src/WebpiecesServer.ts"],"names":[],"mappings":";;;AAAA,yCAAsC;AACtC,wEAAsE;AAEtE,+DAA4D;AAE5D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,MAAa,eAAe;IAY1B,YAAY,IAAgB;QAC1B,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QAEjB,0DAA0D;QAC1D,IAAI,CAAC,kBAAkB,GAAG,IAAI,qBAAS,EAAE,CAAC;QAE1C,yEAAyE;QACzE,2EAA2E;QAC3E,IAAI,CAAC,kBAAkB,CAAC,IAAI,CAAC,IAAA,wCAAmB,GAAE,CAAC,CAAC;QAEpD,wEAAwE;QACxE,IAAI,CAAC,WAAW,GAAG,IAAI,CAAC,kBAAkB,CAAC,GAAG,CAAC,yCAAmB,CAAC,CAAC;IACtE,CAAC;IAED;;;OAGG;IACH,UAAU;QACR,IAAI,CAAC,WAAW,CAAC,UAAU,CAAC,IAAI,CAAC,kBAAkB,EAAE,IAAI,CAAC,IAAI,CAAC,CAAC;IAClE,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,OAAe,IAAI;QACvB,IAAI,CAAC,UAAU,EAAE,CAAC;QAClB,IAAI,CAAC,WAAW,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IAC/B,CAAC;IAED;;OAEG;IACH,IAAI;QACF,IAAI,CAAC,WAAW,CAAC,IAAI,EAAE,CAAC;IAC1B,CAAC;CAEF;AAjDD,0CAiDC","sourcesContent":["import { Container } from 'inversify';\nimport { buildProviderModule } from '@inversifyjs/binding-decorators';\nimport { WebAppMeta } from '@webpieces/core-meta';\nimport { WebpiecesCoreServer } from \"./WebpiecesCoreServer\";\n\n/**\n * WebpiecesServer - Main bootstrap class for WebPieces applications.\n *\n * This class uses a two-container pattern similar to Java WebPieces:\n * 1. webpiecesContainer: Core WebPieces framework bindings\n * 2. appContainer: User's application bindings (child of webpiecesContainer)\n *\n * This separation allows:\n * - Clean separation of concerns\n * - Better testability\n * - Ability to override framework bindings in tests\n *\n * The server:\n * 1. Initializes both DI containers from WebAppMeta.getDIModules()\n * 2. Registers routes using explicit RouteBuilderImpl\n * 3. Creates filter chains\n * 4. Supports both HTTP server mode and testing mode (no HTTP)\n *\n * Usage for testing (no HTTP):\n * ```typescript\n * const server = new WebpiecesServer(new ProdServerMeta());\n * server.initialize();\n * const saveApi = server.createApiClient<SaveApi>(SaveApiPrototype);\n * const response = await saveApi.save(request);\n * ```\n *\n * Usage for production (HTTP server):\n * ```typescript\n * const server = new WebpiecesServer(new ProdServerMeta());\n * server.start(); // Starts Express server\n * ```\n */\nexport class WebpiecesServer {\n private meta: WebAppMeta;\n\n /**\n * WebPieces container: Core WebPieces framework bindings.\n * This includes framework-level services like filters, routing infrastructure,\n * logging, metrics, etc. Similar to Java WebPieces platform container.\n */\n private webpiecesContainer: Container;\n\n private coreService: WebpiecesCoreServer;\n\n constructor(meta: WebAppMeta) {\n this.meta = meta;\n\n // Create WebPieces container for framework-level bindings\n this.webpiecesContainer = new Container();\n\n // Load buildProviderModule to auto-scan for @provideSingleton decorators\n // This registers framework classes (WebpiecesCoreServer, RouteBuilderImpl)\n this.webpiecesContainer.load(buildProviderModule());\n\n // Resolve WebpiecesCoreServer from DI container (proper DI - no 'new'!)\n this.coreService = this.webpiecesContainer.get(WebpiecesCoreServer);\n }\n\n /**\n * Initialize the server (DI container, routes, filters).\n * This is called automatically by start() or can be called manually for testing.\n */\n initialize(): void {\n this.coreService.initialize(this.webpiecesContainer, this.meta);\n }\n\n /**\n * Start the HTTP server with Express.\n */\n start(port: number = 8080): void {\n this.initialize();\n this.coreService.start(port);\n }\n\n /**\n * Stop the HTTP server.\n */\n stop(): void {\n this.coreService.stop();\n }\n\n}\n"]}
@@ -0,0 +1,12 @@
1
+ import { Filter, WpResponse, Service } from '@webpieces/http-filters';
2
+ import { MethodMeta } from '../MethodMeta';
3
+ /**
4
+ * ContextFilter - Sets up AsyncLocalStorage context for each request.
5
+ * Priority: 140 (executes first)
6
+ *
7
+ * This filter ensures that all subsequent filters and the controller
8
+ * execute within a context that can store request-scoped data.
9
+ */
10
+ export declare class ContextFilter extends Filter<MethodMeta, WpResponse<unknown>> {
11
+ filter(meta: MethodMeta, nextFilter: Service<MethodMeta, WpResponse<unknown>>): Promise<WpResponse<unknown>>;
12
+ }
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ContextFilter = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const inversify_1 = require("inversify");
6
+ const http_routing_1 = require("@webpieces/http-routing");
7
+ const core_context_1 = require("@webpieces/core-context");
8
+ const http_filters_1 = require("@webpieces/http-filters");
9
+ /**
10
+ * ContextFilter - Sets up AsyncLocalStorage context for each request.
11
+ * Priority: 140 (executes first)
12
+ *
13
+ * This filter ensures that all subsequent filters and the controller
14
+ * execute within a context that can store request-scoped data.
15
+ */
16
+ let ContextFilter = class ContextFilter extends http_filters_1.Filter {
17
+ async filter(meta, nextFilter) {
18
+ // Run the rest of the filter chain within a new context
19
+ return core_context_1.RequestContext.run(async () => {
20
+ // Store request metadata in context for other filters to access
21
+ core_context_1.RequestContext.put('METHOD_META', meta);
22
+ core_context_1.RequestContext.put('REQUEST_PATH', meta.path);
23
+ core_context_1.RequestContext.put('HTTP_METHOD', meta.httpMethod);
24
+ return await nextFilter.invoke(meta);
25
+ //RequestContext is auto cleared when done.
26
+ });
27
+ }
28
+ };
29
+ exports.ContextFilter = ContextFilter;
30
+ exports.ContextFilter = ContextFilter = tslib_1.__decorate([
31
+ (0, http_routing_1.provideSingleton)(),
32
+ (0, inversify_1.injectable)()
33
+ ], ContextFilter);
34
+ //# sourceMappingURL=ContextFilter.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ContextFilter.js","sourceRoot":"","sources":["../../../../../../packages/http/http-server/src/filters/ContextFilter.ts"],"names":[],"mappings":";;;;AAAA,yCAAuC;AACvC,0DAA2D;AAC3D,0DAAyD;AACzD,0DAAsE;AAGtE;;;;;;GAMG;AAGI,IAAM,aAAa,GAAnB,MAAM,aAAc,SAAQ,qBAAuC;IAExE,KAAK,CAAC,MAAM,CACV,IAAgB,EAChB,UAAoD;QAEpD,wDAAwD;QACxD,OAAO,6BAAc,CAAC,GAAG,CAAC,KAAK,IAAI,EAAE;YACnC,gEAAgE;YAChE,6BAAc,CAAC,GAAG,CAAC,aAAa,EAAE,IAAI,CAAC,CAAC;YACxC,6BAAc,CAAC,GAAG,CAAC,cAAc,EAAE,IAAI,CAAC,IAAI,CAAC,CAAC;YAC9C,6BAAc,CAAC,GAAG,CAAC,aAAa,EAAE,IAAI,CAAC,UAAU,CAAC,CAAC;YAEnD,OAAO,MAAM,UAAU,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;YACrC,2CAA2C;QAC7C,CAAC,CAAC,CAAC;IACL,CAAC;CACF,CAAA;AAjBY,sCAAa;wBAAb,aAAa;IAFzB,IAAA,+BAAgB,GAAE;IAClB,IAAA,sBAAU,GAAE;GACA,aAAa,CAiBzB","sourcesContent":["import { injectable } from 'inversify';\nimport { provideSingleton } from '@webpieces/http-routing';\nimport { RequestContext } from '@webpieces/core-context';\nimport { Filter, WpResponse, Service } from '@webpieces/http-filters';\nimport { MethodMeta } from '../MethodMeta';\n\n/**\n * ContextFilter - Sets up AsyncLocalStorage context for each request.\n * Priority: 140 (executes first)\n *\n * This filter ensures that all subsequent filters and the controller\n * execute within a context that can store request-scoped data.\n */\n@provideSingleton()\n@injectable()\nexport class ContextFilter extends Filter<MethodMeta, WpResponse<unknown>> {\n\n async filter(\n meta: MethodMeta,\n nextFilter: Service<MethodMeta, WpResponse<unknown>>\n ): Promise<WpResponse<unknown>> {\n // Run the rest of the filter chain within a new context\n return RequestContext.run(async () => {\n // Store request metadata in context for other filters to access\n RequestContext.put('METHOD_META', meta);\n RequestContext.put('REQUEST_PATH', meta.path);\n RequestContext.put('HTTP_METHOD', meta.httpMethod);\n\n return await nextFilter.invoke(meta);\n //RequestContext is auto cleared when done.\n });\n }\n}\n"]}
@@ -0,0 +1,62 @@
1
+ import { Filter, WpResponse, Service } from '@webpieces/http-filters';
2
+ import { MethodMeta } from '../MethodMeta';
3
+ /**
4
+ * DI tokens for JsonFilter.
5
+ */
6
+ export declare const FILTER_TYPES: {
7
+ JsonFilterConfig: symbol;
8
+ };
9
+ /**
10
+ * Configuration for JsonFilter.
11
+ * Register this in your DI container to customize JsonFilter behavior.
12
+ */
13
+ export declare class JsonFilterConfig {
14
+ }
15
+ /**
16
+ * JsonFilter - Handles JSON serialization/deserialization and writes response to HTTP body.
17
+ *
18
+ * Similar to Java WebPieces JacksonCatchAllFilter.
19
+ *
20
+ * Flow:
21
+ * 1. Log request
22
+ * 2. Deserialize request body to DTO and set on meta.requestDto
23
+ * 3. Call next filter/controller
24
+ * 4. Get response (WpResponse)
25
+ * 5. Write response to Express response
26
+ * 6. On ANY exception, send 500
27
+ */
28
+ export declare class JsonFilter extends Filter<MethodMeta, WpResponse<unknown>> {
29
+ private config;
30
+ constructor(config: JsonFilterConfig);
31
+ filter(meta: MethodMeta, nextFilter: Service<MethodMeta, WpResponse<unknown>>): Promise<WpResponse<unknown>>;
32
+ /**
33
+ * Deserialize request body to DTO and set on meta.requestDto.
34
+ */
35
+ private deserializeRequest;
36
+ /**
37
+ * Write WpResponse to HTTP response body as JSON.
38
+ */
39
+ private writeResponse;
40
+ /**
41
+ * Log the incoming request.
42
+ */
43
+ private logRequest;
44
+ /**
45
+ * Log the outgoing response.
46
+ */
47
+ private logResponse;
48
+ }
49
+ /**
50
+ * Exception thrown when validation fails.
51
+ */
52
+ export declare class ValidationException extends Error {
53
+ violations: string[];
54
+ constructor(violations: string[]);
55
+ }
56
+ /**
57
+ * HTTP exception with status code.
58
+ */
59
+ export declare class HttpException extends Error {
60
+ statusCode: number;
61
+ constructor(message: string, statusCode: number);
62
+ }
@@ -0,0 +1,146 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.HttpException = exports.ValidationException = exports.JsonFilter = exports.JsonFilterConfig = exports.FILTER_TYPES = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const inversify_1 = require("inversify");
6
+ const http_routing_1 = require("@webpieces/http-routing");
7
+ const http_filters_1 = require("@webpieces/http-filters");
8
+ const core_util_1 = require("@webpieces/core-util");
9
+ /**
10
+ * DI tokens for JsonFilter.
11
+ */
12
+ exports.FILTER_TYPES = {
13
+ JsonFilterConfig: Symbol.for('JsonFilterConfig'),
14
+ };
15
+ /**
16
+ * Configuration for JsonFilter.
17
+ * Register this in your DI container to customize JsonFilter behavior.
18
+ */
19
+ let JsonFilterConfig = class JsonFilterConfig {
20
+ };
21
+ exports.JsonFilterConfig = JsonFilterConfig;
22
+ exports.JsonFilterConfig = JsonFilterConfig = tslib_1.__decorate([
23
+ (0, inversify_1.injectable)()
24
+ ], JsonFilterConfig);
25
+ /**
26
+ * JsonFilter - Handles JSON serialization/deserialization and writes response to HTTP body.
27
+ *
28
+ * Similar to Java WebPieces JacksonCatchAllFilter.
29
+ *
30
+ * Flow:
31
+ * 1. Log request
32
+ * 2. Deserialize request body to DTO and set on meta.requestDto
33
+ * 3. Call next filter/controller
34
+ * 4. Get response (WpResponse)
35
+ * 5. Write response to Express response
36
+ * 6. On ANY exception, send 500
37
+ */
38
+ let JsonFilter = class JsonFilter extends http_filters_1.Filter {
39
+ constructor(config) {
40
+ super();
41
+ this.config = config;
42
+ }
43
+ async filter(meta, nextFilter) {
44
+ // Get Express Request/Response from routeRequest
45
+ const expressRequest = meta.routeRequest.request;
46
+ const expressResponse = meta.routeRequest.response;
47
+ try {
48
+ // 1. Log request
49
+ this.logRequest(meta, expressRequest);
50
+ // 2. Deserialize request body to DTO
51
+ this.deserializeRequest(meta, expressRequest);
52
+ // 3. Call next filter/controller
53
+ const responseWrapper = await nextFilter.invoke(meta);
54
+ // 4. Log response
55
+ this.logResponse(responseWrapper);
56
+ // 5. Write response to Express response
57
+ this.writeResponse(expressResponse, responseWrapper);
58
+ return responseWrapper;
59
+ }
60
+ catch (err) {
61
+ const error = (0, core_util_1.toError)(err);
62
+ // 6. On ANY exception, send 500
63
+ console.error('[JsonFilter] Error:', error);
64
+ const errorResponse = new http_filters_1.WpResponse({ error: 'Internal server error' }, 500);
65
+ this.writeResponse(expressResponse, errorResponse);
66
+ return errorResponse;
67
+ }
68
+ }
69
+ /**
70
+ * Deserialize request body to DTO and set on meta.requestDto.
71
+ */
72
+ deserializeRequest(meta, expressRequest) {
73
+ if (expressRequest.body) {
74
+ // Set the deserialized body as requestDto
75
+ meta.requestDto = expressRequest.body;
76
+ }
77
+ }
78
+ /**
79
+ * Write WpResponse to HTTP response body as JSON.
80
+ */
81
+ writeResponse(expressResponse, responseWrapper) {
82
+ // Set status code
83
+ expressResponse.status(responseWrapper.statusCode);
84
+ // Set headers from wrapper
85
+ responseWrapper.headers.forEach((value, name) => {
86
+ expressResponse.setHeader(name, value);
87
+ });
88
+ // Set content type to JSON
89
+ expressResponse.setHeader('Content-Type', 'application/json');
90
+ // Serialize and write response body
91
+ if (responseWrapper.response !== undefined) {
92
+ expressResponse.json(responseWrapper.response);
93
+ }
94
+ else {
95
+ expressResponse.end();
96
+ }
97
+ }
98
+ /**
99
+ * Log the incoming request.
100
+ */
101
+ logRequest(meta, expressRequest) {
102
+ console.log(`[JsonFilter] ${meta.httpMethod} ${meta.path}`);
103
+ if (expressRequest.body) {
104
+ console.log('[JsonFilter] Request body:', JSON.stringify(expressRequest.body, null, 2));
105
+ }
106
+ }
107
+ /**
108
+ * Log the outgoing response.
109
+ */
110
+ logResponse(responseWrapper) {
111
+ console.log(`[JsonFilter] Response: ${responseWrapper.statusCode}`);
112
+ if (responseWrapper.response) {
113
+ console.log('[JsonFilter] Response body:', JSON.stringify(responseWrapper.response, null, 2));
114
+ }
115
+ }
116
+ };
117
+ exports.JsonFilter = JsonFilter;
118
+ exports.JsonFilter = JsonFilter = tslib_1.__decorate([
119
+ (0, http_routing_1.provideSingleton)(),
120
+ (0, inversify_1.injectable)(),
121
+ tslib_1.__param(0, (0, inversify_1.inject)(exports.FILTER_TYPES.JsonFilterConfig)),
122
+ tslib_1.__metadata("design:paramtypes", [JsonFilterConfig])
123
+ ], JsonFilter);
124
+ /**
125
+ * Exception thrown when validation fails.
126
+ */
127
+ class ValidationException extends Error {
128
+ constructor(violations) {
129
+ super('Validation failed');
130
+ this.violations = violations;
131
+ this.name = 'ValidationException';
132
+ }
133
+ }
134
+ exports.ValidationException = ValidationException;
135
+ /**
136
+ * HTTP exception with status code.
137
+ */
138
+ class HttpException extends Error {
139
+ constructor(message, statusCode) {
140
+ super(message);
141
+ this.statusCode = statusCode;
142
+ this.name = 'HttpException';
143
+ }
144
+ }
145
+ exports.HttpException = HttpException;
146
+ //# sourceMappingURL=JsonFilter.js.map