@twin.org/api-server-fastify 0.0.3-next.4 → 0.0.3-next.41

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  # TWIN API Server Fastify
2
2
 
3
- Use [Fastify](https://fastify.dev/) as the core web server for APIs.
3
+ This package provides Fastify web server integration for exposing API routes with consistent runtime behaviour. It builds on the Fastify framework to offer a predictable hosting layer for API services.
4
4
 
5
5
  ## Installation
6
6
 
@@ -2,11 +2,11 @@
2
2
  // SPDX-License-Identifier: Apache-2.0.
3
3
  import FastifyCompress from "@fastify/compress";
4
4
  import FastifyCors from "@fastify/cors";
5
- import { HttpErrorHelper } from "@twin.org/api-models";
5
+ import { HttpContextIdKeys, HttpErrorHelper } from "@twin.org/api-models";
6
6
  import { JsonLdMimeTypeProcessor } from "@twin.org/api-processors";
7
7
  import { ContextIdStore } from "@twin.org/context";
8
- import { BaseError, ComponentFactory, GeneralError, Is, StringHelper } from "@twin.org/core";
9
- import { HeaderTypes, HttpMethod, HttpStatusCode } from "@twin.org/web";
8
+ import { BaseError, ComponentFactory, GeneralError, HealthStatus, Is, StringHelper } from "@twin.org/core";
9
+ import { HeaderTypes, HttpMethod, HttpStatusCode, HeaderHelper } from "@twin.org/web";
10
10
  import Fastify from "fastify";
11
11
  import FastifySocketIO from "./fastifySocketIo.js";
12
12
  /**
@@ -37,6 +37,11 @@ export class FastifyWebServer {
37
37
  * @internal
38
38
  */
39
39
  _logging;
40
+ /**
41
+ * The hosting component type.
42
+ * @internal
43
+ */
44
+ _hostingComponentType;
40
45
  /**
41
46
  * The options for the server.
42
47
  * @internal
@@ -74,6 +79,7 @@ export class FastifyWebServer {
74
79
  constructor(options) {
75
80
  this._loggingComponentType = options?.loggingComponentType;
76
81
  this._logging = ComponentFactory.getIfExists(options?.loggingComponentType ?? "logging");
82
+ this._hostingComponentType = options?.hostingComponentType;
77
83
  this._fastify = Fastify({
78
84
  routerOptions: {
79
85
  maxParamLength: 2000
@@ -94,6 +100,13 @@ export class FastifyWebServer {
94
100
  }
95
101
  this._includeErrorStack = options?.config?.includeErrorStack ?? false;
96
102
  }
103
+ /**
104
+ * Returns the class name of the component.
105
+ * @returns The class name of the component.
106
+ */
107
+ className() {
108
+ return FastifyWebServer.CLASS_NAME;
109
+ }
97
110
  /**
98
111
  * Get the web server instance.
99
112
  * @returns The web server instance.
@@ -238,6 +251,30 @@ export class FastifyWebServer {
238
251
  });
239
252
  }
240
253
  }
254
+ /**
255
+ * Perform a health check on the server by fetching its own root endpoint.
256
+ * @returns The health status of the server.
257
+ */
258
+ async health() {
259
+ let healthCheck;
260
+ if (this._fastify?.server?.listening) {
261
+ healthCheck = {
262
+ source: FastifyWebServer.CLASS_NAME,
263
+ status: HealthStatus.Ok,
264
+ description: "description",
265
+ message: "reachable"
266
+ };
267
+ }
268
+ else {
269
+ healthCheck = {
270
+ source: FastifyWebServer.CLASS_NAME,
271
+ status: HealthStatus.Error,
272
+ description: "description",
273
+ message: "unreachable"
274
+ };
275
+ }
276
+ return [healthCheck];
277
+ }
241
278
  /**
242
279
  * Add the REST routes to the server.
243
280
  * @param restRouteProcessors The processors for the incoming requests.
@@ -339,29 +376,47 @@ export class FastifyWebServer {
339
376
  * @param request The incoming request.
340
377
  * @param reply The outgoing response.
341
378
  * @param restRoute The REST route to handle.
379
+ * @returns The Fastify reply with the response.
342
380
  * @internal
343
381
  */
344
382
  async handleRequestRest(restRouteProcessors, request, reply, restRoute) {
383
+ const port = (request.port === 80 && request.protocol === "http") ||
384
+ (request.port === 443 && request.protocol === "https") ||
385
+ !Is.integer(request.port)
386
+ ? ""
387
+ : `:${request.port}`;
345
388
  const httpServerRequest = {
346
389
  method: request.method.toUpperCase(),
347
- url: `${request.protocol}://${request.hostname}${request.url}`,
390
+ url: `${request.protocol}://${request.hostname}${port}${request.url}`,
348
391
  body: request.body,
349
392
  query: request.query,
350
393
  pathParams: request.params,
351
394
  headers: request.headers
352
395
  };
353
396
  const httpResponse = {};
354
- const contextIds = {};
397
+ const contextIds = {
398
+ [HttpContextIdKeys.IpAddress]: HeaderHelper.extractClientIps(httpServerRequest.headers).join("|"),
399
+ [HttpContextIdKeys.UserAgent]: HeaderHelper.extractUserAgent(httpServerRequest.headers),
400
+ [HttpContextIdKeys.CorrelationId]: HeaderHelper.extractCorrelationId(httpServerRequest.headers)
401
+ };
355
402
  const processorState = restRoute?.processorData ?? {};
403
+ if (Is.object(httpServerRequest.pathParams)) {
404
+ for (const key of Object.keys(httpServerRequest.pathParams)) {
405
+ httpServerRequest.pathParams[key] = decodeURIComponent(httpServerRequest.pathParams[key]);
406
+ }
407
+ }
408
+ if (Is.object(httpServerRequest.query)) {
409
+ for (const key of Object.keys(httpServerRequest.query)) {
410
+ httpServerRequest.query[key] = decodeURIComponent(httpServerRequest.query[key]);
411
+ }
412
+ }
356
413
  await this.runProcessorsRest(restRouteProcessors, restRoute, httpServerRequest, httpResponse, contextIds, processorState);
357
414
  if (!Is.empty(httpResponse.headers)) {
358
415
  for (const header of Object.keys(httpResponse.headers)) {
359
416
  reply.header(header, httpResponse.headers[header]);
360
417
  }
361
418
  }
362
- return reply
363
- .status((httpResponse.statusCode ?? HttpStatusCode.ok))
364
- .send(httpResponse.body);
419
+ return reply.status(httpResponse.statusCode ?? HttpStatusCode.ok).send(httpResponse.body);
365
420
  }
366
421
  /**
367
422
  * Run the REST processors for the route.
@@ -373,34 +428,83 @@ export class FastifyWebServer {
373
428
  * @internal
374
429
  */
375
430
  async runProcessorsRest(restRouteProcessors, restRoute, httpServerRequest, httpResponse, contextIds, processorState) {
431
+ let hasPreError = false;
432
+ const filteredProcessors = this.filterRouteProcessors(restRoute, restRouteProcessors);
376
433
  try {
377
- const filteredProcessors = this.filterRouteProcessors(restRoute, restRouteProcessors);
378
- for (const routeProcessor of filteredProcessors) {
379
- const pre = routeProcessor.pre?.bind(routeProcessor);
380
- if (Is.function(pre)) {
381
- await pre(httpServerRequest, httpResponse, restRoute, contextIds, processorState, this._loggingComponentType);
382
- }
383
- }
384
- // Run the processors within an async context
385
- // so that any services can access the context ids
434
+ // Run inside ContextIdStore.run so pre-processors can do tenant-scoped storage lookups.
386
435
  await ContextIdStore.run(contextIds, async () => {
387
436
  for (const routeProcessor of filteredProcessors) {
388
- const process = routeProcessor.process?.bind(routeProcessor);
389
- if (Is.function(process)) {
390
- await process(httpServerRequest, httpResponse, restRoute, processorState, this._loggingComponentType);
437
+ const pre = routeProcessor.pre?.bind(routeProcessor);
438
+ if (Is.function(pre)) {
439
+ await pre(httpServerRequest, httpResponse, restRoute, contextIds, processorState, {
440
+ loggingComponentType: this._loggingComponentType,
441
+ hostingComponentType: this._hostingComponentType
442
+ });
391
443
  }
392
444
  }
393
445
  });
394
- for (const routeProcessor of filteredProcessors) {
395
- const post = routeProcessor.post?.bind(routeProcessor);
396
- if (Is.function(post)) {
397
- await post(httpServerRequest, httpResponse, restRoute, contextIds, processorState, this._loggingComponentType);
398
- }
399
- }
400
446
  }
401
447
  catch (err) {
402
448
  const { error, httpStatusCode } = HttpErrorHelper.processError(err, this._includeErrorStack);
403
449
  HttpErrorHelper.buildResponse(httpResponse, error, httpStatusCode);
450
+ hasPreError = true;
451
+ }
452
+ // A pre-processor may set an error response without throwing; treat that as halt.
453
+ if (!hasPreError &&
454
+ Is.integer(httpResponse.statusCode) &&
455
+ httpResponse.statusCode >= HttpStatusCode.badRequest) {
456
+ hasPreError = true;
457
+ }
458
+ // Don't run the main processing if there was an error in the pre processing
459
+ // As this is likely to perform tasks such as authentication which may have failed
460
+ if (!hasPreError) {
461
+ try {
462
+ // Run the processors within an async context
463
+ // so that any services can access the context ids
464
+ await ContextIdStore.run(contextIds, async () => {
465
+ for (const routeProcessor of filteredProcessors) {
466
+ const process = routeProcessor.process?.bind(routeProcessor);
467
+ if (Is.function(process)) {
468
+ await process(httpServerRequest, httpResponse, restRoute, processorState, {
469
+ loggingComponentType: this._loggingComponentType,
470
+ hostingComponentType: this._hostingComponentType
471
+ });
472
+ }
473
+ }
474
+ });
475
+ }
476
+ catch (err) {
477
+ const { error, httpStatusCode } = HttpErrorHelper.processError(err, this._includeErrorStack);
478
+ HttpErrorHelper.buildResponse(httpResponse, error, httpStatusCode);
479
+ }
480
+ }
481
+ try {
482
+ // Always run the post processors, even if there was an error earlier
483
+ // as they may perform cleanup tasks, or logging etc
484
+ await ContextIdStore.run(contextIds, async () => {
485
+ for (const routeProcessor of filteredProcessors) {
486
+ const post = routeProcessor.post?.bind(routeProcessor);
487
+ if (Is.function(post)) {
488
+ await post(httpServerRequest, httpResponse, restRoute, contextIds, processorState, {
489
+ loggingComponentType: this._loggingComponentType,
490
+ hostingComponentType: this._hostingComponentType
491
+ });
492
+ }
493
+ }
494
+ });
495
+ }
496
+ catch (err) {
497
+ // Just log post processor errors
498
+ await this._logging?.log({
499
+ level: "error",
500
+ ts: Date.now(),
501
+ source: FastifyWebServer.CLASS_NAME,
502
+ message: "postProcessorError",
503
+ error: BaseError.fromError(err),
504
+ data: {
505
+ route: restRoute?.path ?? ""
506
+ }
507
+ });
404
508
  }
405
509
  }
406
510
  /**
@@ -437,7 +541,7 @@ export class FastifyWebServer {
437
541
  * @param socket The socket to handle.
438
542
  * @param fullPath The full path of the socket route.
439
543
  * @param emitTopic The topic to emit the response on.
440
- * @param data The incoming data.
544
+ * @param request The incoming request.
441
545
  * @internal
442
546
  */
443
547
  async handleRequestSocket(socketRouteProcessors, socketRoute, socket, fullPath, emitTopic, request) {
@@ -460,7 +564,6 @@ export class FastifyWebServer {
460
564
  }
461
565
  /**
462
566
  * Run the socket processors for the route.
463
- * @param socketId The id of the socket.
464
567
  * @param socketRouteProcessors The processors to run.
465
568
  * @param socketRoute The route to process.
466
569
  * @param socketServerRequest The incoming request.
@@ -468,6 +571,7 @@ export class FastifyWebServer {
468
571
  * @param contextIds The context IDs of the request.
469
572
  * @param processorState The state handed through the processors.
470
573
  * @param requestTopic The topic of the request.
574
+ * @param responseEmitter The emitter to send the response on.
471
575
  * @internal
472
576
  */
473
577
  async runProcessorsSocket(socketRouteProcessors, socketRoute, socketServerRequest, httpResponse, contextIds, processorState, requestTopic, responseEmitter) {
@@ -480,7 +584,10 @@ export class FastifyWebServer {
480
584
  for (const postSocketRouteProcessor of filteredProcessors) {
481
585
  const post = postSocketRouteProcessor.post?.bind(postSocketRouteProcessor);
482
586
  if (Is.function(post)) {
483
- await post(socketServerRequest, response, socketRoute, contextIds, responseProcessorState, this._loggingComponentType);
587
+ await post(socketServerRequest, response, socketRoute, contextIds, responseProcessorState, {
588
+ loggingComponentType: this._loggingComponentType,
589
+ hostingComponentType: this._hostingComponentType
590
+ });
484
591
  }
485
592
  }
486
593
  }
@@ -501,7 +608,10 @@ export class FastifyWebServer {
501
608
  for (const socketRouteProcessor of filteredProcessors) {
502
609
  const pre = socketRouteProcessor.pre?.bind(socketRouteProcessor);
503
610
  if (Is.function(pre)) {
504
- await pre(socketServerRequest, httpResponse, socketRoute, contextIds, processorState, this._loggingComponentType);
611
+ await pre(socketServerRequest, httpResponse, socketRoute, contextIds, processorState, {
612
+ loggingComponentType: this._loggingComponentType,
613
+ hostingComponentType: this._hostingComponentType
614
+ });
505
615
  }
506
616
  }
507
617
  // We always call all the processors regardless of any response set by a previous processor.
@@ -1 +1 @@
1
- {"version":3,"file":"fastifyWebServer.js","sourceRoot":"","sources":["../../src/fastifyWebServer.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,eAAe,MAAM,mBAAmB,CAAC;AAChD,OAAO,WAAW,MAAM,eAAe,CAAC;AACxC,OAAO,EACN,eAAe,EAgBf,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,uBAAuB,EAAE,MAAM,0BAA0B,CAAC;AACnE,OAAO,EAAE,cAAc,EAAoB,MAAM,mBAAmB,CAAC;AACrE,OAAO,EACN,SAAS,EACT,gBAAgB,EAChB,YAAY,EAEZ,EAAE,EACF,YAAY,EACZ,MAAM,gBAAgB,C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U,CAAC,OAAO;SAClB,CAAC;QACF,MAAM,cAAc,GAAG;YACtB,6BAA6B;YAC7B,kBAAkB;YAClB,iBAAiB;YACjB,WAAW,CAAC,WAAW;YACvB,WAAW,CAAC,aAAa;YACzB,WAAW,CAAC,MAAM;SAClB,CAAC;QACF,MAAM,cAAc,GAAa,CAAC,WAAW,CAAC,kBAAkB,EAAE,WAAW,CAAC,QAAQ,CAAC,CAAC;QAExF,IAAI,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,cAAc,CAAC,EAAE,CAAC;YAC5C,cAAc,CAAC,IAAI,CAAC,GAAG,OAAO,CAAC,cAAc,CAAC,CAAC;QAChD,CAAC;QACD,IAAI,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,cAAc,CAAC,EAAE,CAAC;YAC5C,cAAc,CAAC,IAAI,CAAC,GAAG,OAAO,CAAC,cAAc,CAAC,CAAC;QAChD,CAAC;QAED,MAAM,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,WAAW,EAAE;YACzC,MAAM,EAAE,CAAC,MAAM,EAAE,QAAQ,EAAE,EAAE;gBAC5B,QAAQ,CAAC,IAAI,EAAE,iBAAiB,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,OAAO,CAAC,QAAQ,CAAC,MAAgB,CAAC,CAAC,CAAC;YAC/E,CAAC;YACD,OAAO;YACP,cAAc;YACd,cAAc;YACd,WAAW,EAAE,IAAI;SACjB,CAAC,CAAC;IACJ,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport FastifyCompress from \"@fastify/compress\";\nimport FastifyCors from \"@fastify/cors\";\nimport {\n\tHttpErrorHelper,\n\ttype IBaseRoute,\n\ttype IBaseRouteProcessor,\n\ttype IHttpRequest,\n\ttype IHttpRequestPathParams,\n\ttype IHttpRequestQuery,\n\ttype IHttpResponse,\n\ttype IHttpServerRequest,\n\ttype IMimeTypeProcessor,\n\ttype IRestRoute,\n\ttype IRestRouteProcessor,\n\ttype ISocketRoute,\n\ttype ISocketRouteProcessor,\n\ttype ISocketServerRequest,\n\ttype IWebServer,\n\ttype IWebServerOptions\n} from \"@twin.org/api-models\";\nimport { JsonLdMimeTypeProcessor } from \"@twin.org/api-processors\";\nimport { ContextIdStore, type IContextIds } from \"@twin.org/context\";\nimport {\n\tBaseError,\n\tComponentFactory,\n\tGeneralError,\n\ttype IError,\n\tIs,\n\tStringHelper\n} from \"@twin.org/core\";\nimport type { ILoggingComponent } from \"@twin.org/logging-models\";\nimport { nameof } from \"@twin.org/nameof\";\nimport { HeaderTypes, HttpMethod, HttpStatusCode, type IHttpHeaders } from \"@twin.org/web\";\nimport Fastify, {\n\ttype FastifyInstance,\n\ttype FastifyReply,\n\ttype FastifyRequest,\n\ttype FastifyServerOptions\n} from \"fastify\";\nimport type { Server, ServerOptions, Socket } from \"socket.io\";\nimport FastifySocketIO from \"./fastifySocketIo.js\";\nimport type { IFastifyWebServerConstructorOptions } from \"./models/IFastifyWebServerConstructorOptions.js\";\n\n/**\n * Implementation of the web server using Fastify.\n */\nexport class FastifyWebServer implements IWebServer<FastifyInstance> {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<FastifyWebServer>();\n\n\t/**\n\t * Default port for running the server.\n\t * @internal\n\t */\n\tprivate static readonly _DEFAULT_PORT: number = 3000;\n\n\t/**\n\t * Default host for running the server.\n\t * @internal\n\t */\n\tprivate static readonly _DEFAULT_HOST: string = \"localhost\";\n\n\t/**\n\t * The logging component type.\n\t * @internal\n\t */\n\tprivate readonly _loggingComponentType?: string;\n\n\t/**\n\t * The logging component.\n\t * @internal\n\t */\n\tprivate readonly _logging?: ILoggingComponent;\n\n\t/**\n\t * The options for the server.\n\t * @internal\n\t */\n\tprivate _options?: IWebServerOptions;\n\n\t/**\n\t * The Fastify instance.\n\t * @internal\n\t */\n\tprivate readonly _fastify: FastifyInstance;\n\n\t/**\n\t * The options for the socket server.\n\t * @internal\n\t */\n\tprivate readonly _socketConfig: Partial<ServerOptions>;\n\n\t/**\n\t * Whether the server has been started.\n\t * @internal\n\t */\n\tprivate _started: boolean;\n\n\t/**\n\t * The mime type processors.\n\t * @internal\n\t */\n\tprivate readonly _mimeTypeProcessors: IMimeTypeProcessor[];\n\n\t/**\n\t * Include the stack with errors.\n\t * @internal\n\t */\n\tprivate readonly _includeErrorStack: boolean;\n\n\t/**\n\t * Create a new instance of FastifyWebServer.\n\t * @param options The options for the server.\n\t */\n\tconstructor(options?: IFastifyWebServerConstructorOptions) {\n\t\tthis._loggingComponentType = options?.loggingComponentType;\n\t\tthis._logging = ComponentFactory.getIfExists(options?.loggingComponentType ?? \"logging\");\n\t\tthis._fastify = Fastify({\n\t\t\trouterOptions: {\n\t\t\t\tmaxParamLength: 2000\n\t\t\t},\n\t\t\t...options?.config?.web\n\t\t\t// Need this cast for now as maxParamLength has moved in to routerOptions\n\t\t\t// but the TS defs has not been updated yet\n\t\t} as unknown as FastifyServerOptions);\n\t\tthis._socketConfig = {\n\t\t\tpath: \"/socket\",\n\t\t\t...options?.config?.socket\n\t\t};\n\t\tthis._started = false;\n\n\t\tthis._mimeTypeProcessors = options?.mimeTypeProcessors ?? [];\n\n\t\tconst hasJsonLd = this._mimeTypeProcessors.find(\n\t\t\tprocessor => processor.className() === \"json-ld\"\n\t\t);\n\t\tif (!hasJsonLd) {\n\t\t\tthis._mimeTypeProcessors.push(new JsonLdMimeTypeProcessor());\n\t\t}\n\n\t\tthis._includeErrorStack = options?.config?.includeErrorStack ?? false;\n\t}\n\n\t/**\n\t * Get the web server instance.\n\t * @returns The web server instance.\n\t */\n\tpublic getInstance(): FastifyInstance {\n\t\treturn this._fastify;\n\t}\n\n\t/**\n\t * Build the server.\n\t * @param restRouteProcessors The processors for incoming requests over REST.\n\t * @param restRoutes The REST routes.\n\t * @param socketRouteProcessors The processors for incoming requests over Sockets.\n\t * @param socketRoutes The socket routes.\n\t * @param options Options for building the server.\n\t * @returns Nothing.\n\t */\n\tpublic async build(\n\t\trestRouteProcessors?: IRestRouteProcessor[],\n\t\trestRoutes?: IRestRoute[],\n\t\tsocketRouteProcessors?: ISocketRouteProcessor[],\n\t\tsocketRoutes?: ISocketRoute[],\n\t\toptions?: IWebServerOptions\n\t): Promise<void> {\n\t\tif (Is.arrayValue(restRoutes) && !Is.arrayValue(restRouteProcessors)) {\n\t\t\tthrow new GeneralError(FastifyWebServer.CLASS_NAME, \"noRestProcessors\");\n\t\t}\n\t\tif (Is.arrayValue(socketRoutes) && !Is.arrayValue(socketRouteProcessors)) {\n\t\t\tthrow new GeneralError(FastifyWebServer.CLASS_NAME, \"noSocketProcessors\");\n\t\t}\n\t\tawait this._logging?.log({\n\t\t\tlevel: \"info\",\n\t\t\tts: Date.now(),\n\t\t\tsource: FastifyWebServer.CLASS_NAME,\n\t\t\tmessage: \"building\"\n\t\t});\n\n\t\tthis._options = options;\n\n\t\tawait this._fastify.register(FastifyCompress);\n\n\t\tif (Is.arrayValue(socketRoutes)) {\n\t\t\tawait this._fastify.register(FastifySocketIO, this._socketConfig);\n\t\t}\n\n\t\tif (Is.arrayValue(this._mimeTypeProcessors)) {\n\t\t\tfor (const contentTypeHandler of this._mimeTypeProcessors) {\n\t\t\t\tthis._fastify.addContentTypeParser(\n\t\t\t\t\tcontentTypeHandler.getTypes(),\n\t\t\t\t\t{ parseAs: \"buffer\" },\n\t\t\t\t\t(request, body, done) => {\n\t\t\t\t\t\t// Fastify does not handle this method correctly if it is async\n\t\t\t\t\t\t// so we have to use the callback method\n\t\t\t\t\t\tcontentTypeHandler\n\t\t\t\t\t\t\t.handle(body as Buffer)\n\t\t\t\t\t\t\t// eslint-disable-next-line promise/prefer-await-to-then, promise/no-callback-in-promise\n\t\t\t\t\t\t\t.then(processed => done(null, processed))\n\t\t\t\t\t\t\t// eslint-disable-next-line promise/prefer-await-to-then, promise/no-callback-in-promise\n\t\t\t\t\t\t\t.catch(err => done(BaseError.fromError(err)));\n\t\t\t\t\t}\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\n\t\tawait this.initCors(options);\n\n\t\tthis._fastify.setNotFoundHandler({}, async (request, reply) =>\n\t\t\tthis.handleRequestRest(restRouteProcessors ?? [], request, reply)\n\t\t);\n\n\t\tthis._fastify.setErrorHandler(\n\t\t\tasync (\n\t\t\t\terror: Error & {\n\t\t\t\t\tcode?: number | string;\n\t\t\t\t\tstatusCode?: number | string;\n\t\t\t\t},\n\t\t\t\trequest,\n\t\t\t\treply\n\t\t\t) => {\n\t\t\t\t// If code property is set this is a fastify error\n\t\t\t\t// otherwise it's from our framework\n\t\t\t\tlet httpStatusCode: HttpStatusCode;\n\t\t\t\tlet err: IError;\n\t\t\t\tif (Is.number(error.code) || Is.string(error.code)) {\n\t\t\t\t\terr = {\n\t\t\t\t\t\tsource: FastifyWebServer.CLASS_NAME,\n\t\t\t\t\t\tname: error.name,\n\t\t\t\t\t\tmessage: `${error.code}: ${error.message}`\n\t\t\t\t\t};\n\t\t\t\t\thttpStatusCode = (error.statusCode as HttpStatusCode) ?? HttpStatusCode.badRequest;\n\t\t\t\t} else {\n\t\t\t\t\tconst errorAndCode = HttpErrorHelper.processError(error);\n\t\t\t\t\terr = errorAndCode.error;\n\t\t\t\t\thttpStatusCode = errorAndCode.httpStatusCode;\n\t\t\t\t}\n\n\t\t\t\tawait this._logging?.log({\n\t\t\t\t\tlevel: \"error\",\n\t\t\t\t\tts: Date.now(),\n\t\t\t\t\tsource: FastifyWebServer.CLASS_NAME,\n\t\t\t\t\tmessage: \"badRequest\",\n\t\t\t\t\terror: err\n\t\t\t\t});\n\n\t\t\t\treturn reply.status(httpStatusCode).send({\n\t\t\t\t\terror: err\n\t\t\t\t});\n\t\t\t}\n\t\t);\n\n\t\tawait this.addRoutesRest(restRouteProcessors, restRoutes);\n\t\tawait this.addRoutesSocket(socketRouteProcessors, socketRoutes);\n\t}\n\n\t/**\n\t * Start the server.\n\t * @returns Nothing.\n\t */\n\tpublic async start(): Promise<void> {\n\t\tconst host = this._options?.host ?? FastifyWebServer._DEFAULT_HOST;\n\t\tconst port = this._options?.port ?? FastifyWebServer._DEFAULT_PORT;\n\n\t\tawait this._logging?.log({\n\t\t\tlevel: \"info\",\n\t\t\tts: Date.now(),\n\t\t\tsource: FastifyWebServer.CLASS_NAME,\n\t\t\tmessage: \"starting\",\n\t\t\tdata: {\n\t\t\t\thost,\n\t\t\t\tport\n\t\t\t}\n\t\t});\n\n\t\tif (!this._started) {\n\t\t\ttry {\n\t\t\t\tawait this._fastify.listen({ port, host });\n\t\t\t\tconst addresses = this._fastify.addresses();\n\n\t\t\t\tconst protocol = Is.object(this._fastify.initialConfig.https) ? \"https://\" : \"http://\";\n\t\t\t\tawait this._logging?.log({\n\t\t\t\t\tlevel: \"info\",\n\t\t\t\t\tts: Date.now(),\n\t\t\t\t\tsource: FastifyWebServer.CLASS_NAME,\n\t\t\t\t\tmessage: \"started\",\n\t\t\t\t\tdata: {\n\t\t\t\t\t\taddresses: addresses\n\t\t\t\t\t\t\t.map(\n\t\t\t\t\t\t\t\ta =>\n\t\t\t\t\t\t\t\t\t`${protocol}${a.family === \"IPv6\" ? \"[\" : \"\"}${a.address}${a.family === \"IPv6\" ? \"]\" : \"\"}:${a.port}`\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t.join(\", \")\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t\tthis._started = true;\n\t\t\t} catch (err) {\n\t\t\t\tawait this._logging?.log({\n\t\t\t\t\tlevel: \"error\",\n\t\t\t\t\tts: Date.now(),\n\t\t\t\t\tsource: FastifyWebServer.CLASS_NAME,\n\t\t\t\t\tmessage: \"startFailed\",\n\t\t\t\t\terror: BaseError.fromError(err)\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Stop the server.\n\t * @returns Nothing.\n\t */\n\tpublic async stop(): Promise<void> {\n\t\tif (this._started) {\n\t\t\tthis._started = false;\n\n\t\t\tawait this._fastify.close();\n\n\t\t\tawait this._logging?.log({\n\t\t\t\tlevel: \"info\",\n\t\t\t\tts: Date.now(),\n\t\t\t\tsource: FastifyWebServer.CLASS_NAME,\n\t\t\t\tmessage: \"stopped\"\n\t\t\t});\n\t\t}\n\t}\n\n\t/**\n\t * Add the REST routes to the server.\n\t * @param restRouteProcessors The processors for the incoming requests.\n\t * @param restRoutes The REST routes to add.\n\t * @internal\n\t */\n\tprivate async addRoutesRest(\n\t\trestRouteProcessors?: IRestRouteProcessor[],\n\t\trestRoutes?: IRestRoute[]\n\t): Promise<void> {\n\t\tif (Is.arrayValue(restRouteProcessors) && Is.arrayValue(restRoutes)) {\n\t\t\tfor (const restRoute of restRoutes) {\n\t\t\t\tlet path = StringHelper.trimTrailingSlashes(restRoute.path);\n\t\t\t\tif (!path.startsWith(\"/\")) {\n\t\t\t\t\tpath = `/${path}`;\n\t\t\t\t}\n\t\t\t\tawait this._logging?.log({\n\t\t\t\t\tlevel: \"info\",\n\t\t\t\t\tts: Date.now(),\n\t\t\t\t\tsource: FastifyWebServer.CLASS_NAME,\n\t\t\t\t\tmessage: \"restRouteAdded\",\n\t\t\t\t\tdata: { route: path, method: restRoute.method }\n\t\t\t\t});\n\t\t\t\tconst method = restRoute.method.toLowerCase() as\n\t\t\t\t\t| \"get\"\n\t\t\t\t\t| \"post\"\n\t\t\t\t\t| \"put\"\n\t\t\t\t\t| \"patch\"\n\t\t\t\t\t| \"delete\"\n\t\t\t\t\t| \"options\"\n\t\t\t\t\t| \"head\";\n\n\t\t\t\tthis._fastify[method](path, async (request, reply) =>\n\t\t\t\t\tthis.handleRequestRest(restRouteProcessors, request, reply, restRoute)\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Add the socket routes to the server.\n\t * @param socketRouteProcessors The processors for the incoming requests.\n\t * @param socketRoutes The socket routes to add.\n\t * @internal\n\t */\n\tprivate async addRoutesSocket(\n\t\tsocketRouteProcessors?: ISocketRouteProcessor[],\n\t\tsocketRoutes?: ISocketRoute[]\n\t): Promise<void> {\n\t\tif (Is.arrayValue(socketRouteProcessors) && Is.arrayValue(socketRoutes)) {\n\t\t\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\t\t\tconst io: Server = (this._fastify as any).io;\n\n\t\t\tfor (const socketRoute of socketRoutes) {\n\t\t\t\tconst path = StringHelper.trimLeadingSlashes(\n\t\t\t\t\tStringHelper.trimTrailingSlashes(socketRoute.path)\n\t\t\t\t);\n\t\t\t\tconst pathParts = path.split(\"/\");\n\n\t\t\t\tconst namespace = `/${pathParts[0]}`;\n\t\t\t\tconst topic = pathParts.slice(1).join(\"/\");\n\n\t\t\t\tawait this._logging?.log({\n\t\t\t\t\tlevel: \"info\",\n\t\t\t\t\tts: Date.now(),\n\t\t\t\t\tsource: FastifyWebServer.CLASS_NAME,\n\t\t\t\t\tmessage: \"socketRouteAdded\",\n\t\t\t\t\tdata: {\n\t\t\t\t\t\thandshakePath: this._socketConfig.path,\n\t\t\t\t\t\tnamespace,\n\t\t\t\t\t\teventName: topic\n\t\t\t\t\t}\n\t\t\t\t});\n\n\t\t\t\tconst socketNamespace = io.of(namespace);\n\n\t\t\t\tsocketNamespace.on(\"connection\", async socket => {\n\t\t\t\t\tconst socketServerRequest: ISocketServerRequest = {\n\t\t\t\t\t\tmethod: HttpMethod.GET,\n\t\t\t\t\t\turl: socket.handshake.url,\n\t\t\t\t\t\tquery: socket.handshake.query as IHttpRequestQuery,\n\t\t\t\t\t\theaders: socket.handshake.headers as IHttpHeaders,\n\t\t\t\t\t\tsocketId: socket.id\n\t\t\t\t\t};\n\n\t\t\t\t\t// Pass the connected information on to any processors\n\t\t\t\t\ttry {\n\t\t\t\t\t\tfor (const socketRouteProcessor of socketRouteProcessors) {\n\t\t\t\t\t\t\tif (socketRouteProcessor.connected) {\n\t\t\t\t\t\t\t\tawait socketRouteProcessor.connected(\n\t\t\t\t\t\t\t\t\tsocketServerRequest,\n\t\t\t\t\t\t\t\t\tsocketRoute,\n\t\t\t\t\t\t\t\t\tthis._loggingComponentType\n\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t} catch (err) {\n\t\t\t\t\t\tconst { error, httpStatusCode } = HttpErrorHelper.processError(\n\t\t\t\t\t\t\terr,\n\t\t\t\t\t\t\tthis._includeErrorStack\n\t\t\t\t\t\t);\n\t\t\t\t\t\tconst response: IHttpResponse = {};\n\t\t\t\t\t\tHttpErrorHelper.buildResponse(response, error, httpStatusCode);\n\t\t\t\t\t\tsocket.emit(topic, response);\n\t\t\t\t\t}\n\n\t\t\t\t\tsocket.on(\"disconnect\", async () => {\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\t// The socket disconnected so notify any processors\n\t\t\t\t\t\t\tfor (const socketRouteProcessor of socketRouteProcessors) {\n\t\t\t\t\t\t\t\tif (socketRouteProcessor.disconnected) {\n\t\t\t\t\t\t\t\t\tawait socketRouteProcessor.disconnected(\n\t\t\t\t\t\t\t\t\t\tsocketServerRequest,\n\t\t\t\t\t\t\t\t\t\tsocketRoute,\n\t\t\t\t\t\t\t\t\t\tthis._loggingComponentType\n\t\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t} catch {\n\t\t\t\t\t\t\t// If something fails on a disconnect there is not much we can do with it\n\t\t\t\t\t\t}\n\t\t\t\t\t});\n\n\t\t\t\t\t// Handle any incoming messages\n\t\t\t\t\tsocket.on(topic, async data => {\n\t\t\t\t\t\tawait this.handleRequestSocket(\n\t\t\t\t\t\t\tsocketRouteProcessors,\n\t\t\t\t\t\t\tsocketRoute,\n\t\t\t\t\t\t\tsocket,\n\t\t\t\t\t\t\t`/${pathParts.join(\"/\")}`,\n\t\t\t\t\t\t\ttopic,\n\t\t\t\t\t\t\tdata\n\t\t\t\t\t\t);\n\t\t\t\t\t});\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Handle the incoming REST request.\n\t * @param restRouteProcessors The hooks to process the incoming requests.\n\t * @param request The incoming request.\n\t * @param reply The outgoing response.\n\t * @param restRoute The REST route to handle.\n\t * @internal\n\t */\n\tprivate async handleRequestRest(\n\t\trestRouteProcessors: IRestRouteProcessor[],\n\t\trequest: FastifyRequest,\n\t\treply: FastifyReply,\n\t\trestRoute?: IRestRoute\n\t): Promise<FastifyReply> {\n\t\tconst httpServerRequest: IHttpServerRequest = {\n\t\t\tmethod: request.method.toUpperCase() as HttpMethod,\n\t\t\turl: `${request.protocol}://${request.hostname}${request.url}`,\n\t\t\tbody: request.body,\n\t\t\tquery: request.query as IHttpRequestQuery,\n\t\t\tpathParams: request.params as IHttpRequestPathParams,\n\t\t\theaders: request.headers as IHttpHeaders\n\t\t};\n\t\tconst httpResponse: IHttpResponse = {};\n\t\tconst contextIds: IContextIds = {};\n\t\tconst processorState = restRoute?.processorData ?? {};\n\n\t\tawait this.runProcessorsRest(\n\t\t\trestRouteProcessors,\n\t\t\trestRoute,\n\t\t\thttpServerRequest,\n\t\t\thttpResponse,\n\t\t\tcontextIds,\n\t\t\tprocessorState\n\t\t);\n\n\t\tif (!Is.empty(httpResponse.headers)) {\n\t\t\tfor (const header of Object.keys(httpResponse.headers)) {\n\t\t\t\treply.header(header, httpResponse.headers[header]);\n\t\t\t}\n\t\t}\n\t\treturn reply\n\t\t\t.status((httpResponse.statusCode ?? HttpStatusCode.ok) as number)\n\t\t\t.send(httpResponse.body);\n\t}\n\n\t/**\n\t * Run the REST processors for the route.\n\t * @param restRouteProcessors The processors to run.\n\t * @param restRoute The route to process.\n\t * @param httpServerRequest The incoming request.\n\t * @param httpResponse The outgoing response.\n\t * @param contextIds The context IDs of the request.\n\t * @internal\n\t */\n\tprivate async runProcessorsRest(\n\t\trestRouteProcessors: IRestRouteProcessor[],\n\t\trestRoute: IRestRoute | undefined,\n\t\thttpServerRequest: IHttpServerRequest,\n\t\thttpResponse: IHttpResponse,\n\t\tcontextIds: IContextIds,\n\t\tprocessorState: {\n\t\t\t[id: string]: unknown;\n\t\t}\n\t): Promise<void> {\n\t\ttry {\n\t\t\tconst filteredProcessors = this.filterRouteProcessors(restRoute, restRouteProcessors);\n\n\t\t\tfor (const routeProcessor of filteredProcessors) {\n\t\t\t\tconst pre = routeProcessor.pre?.bind(routeProcessor);\n\t\t\t\tif (Is.function(pre)) {\n\t\t\t\t\tawait pre(\n\t\t\t\t\t\thttpServerRequest,\n\t\t\t\t\t\thttpResponse,\n\t\t\t\t\t\trestRoute,\n\t\t\t\t\t\tcontextIds,\n\t\t\t\t\t\tprocessorState,\n\t\t\t\t\t\tthis._loggingComponentType\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Run the processors within an async context\n\t\t\t// so that any services can access the context ids\n\t\t\tawait ContextIdStore.run(contextIds, async () => {\n\t\t\t\tfor (const routeProcessor of filteredProcessors) {\n\t\t\t\t\tconst process = routeProcessor.process?.bind(routeProcessor);\n\t\t\t\t\tif (Is.function(process)) {\n\t\t\t\t\t\tawait process(\n\t\t\t\t\t\t\thttpServerRequest,\n\t\t\t\t\t\t\thttpResponse,\n\t\t\t\t\t\t\trestRoute,\n\t\t\t\t\t\t\tprocessorState,\n\t\t\t\t\t\t\tthis._loggingComponentType\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t});\n\n\t\t\tfor (const routeProcessor of filteredProcessors) {\n\t\t\t\tconst post = routeProcessor.post?.bind(routeProcessor);\n\t\t\t\tif (Is.function(post)) {\n\t\t\t\t\tawait post(\n\t\t\t\t\t\thttpServerRequest,\n\t\t\t\t\t\thttpResponse,\n\t\t\t\t\t\trestRoute,\n\t\t\t\t\t\tcontextIds,\n\t\t\t\t\t\tprocessorState,\n\t\t\t\t\t\tthis._loggingComponentType\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t}\n\t\t} catch (err) {\n\t\t\tconst { error, httpStatusCode } = HttpErrorHelper.processError(err, this._includeErrorStack);\n\t\t\tHttpErrorHelper.buildResponse(httpResponse, error, httpStatusCode);\n\t\t}\n\t}\n\n\t/**\n\t * Filter the route processors based on the requested features.\n\t * @param route The route to process.\n\t * @param routeProcessors The processors to filter.\n\t * @returns The filtered list of route processor.\n\t * @internal\n\t */\n\tprivate filterRouteProcessors<T extends IBaseRouteProcessor>(\n\t\troute: IBaseRoute | undefined,\n\t\trouteProcessors: T[]\n\t): T[] {\n\t\tconst requestedFeatures = route?.processorFeatures ?? [];\n\n\t\tif (!Is.arrayValue(requestedFeatures)) {\n\t\t\t// If there are no requested features, we just return all the processors\n\t\t\treturn routeProcessors;\n\t\t}\n\n\t\t// Reduce the list of route processors to just those in the requested features list\n\t\tconst reducedProcessors = routeProcessors.filter(routeProcessor => {\n\t\t\t// Processors that do not define any features always get run\n\t\t\t// If the route processor has features defined, then we only run it\n\t\t\t// if the route has at least one of those features required\n\t\t\tlet runRouteProcessor = true;\n\t\t\tif (routeProcessor.features) {\n\t\t\t\tconst routeProcessorFeatures = routeProcessor.features();\n\t\t\t\trunRouteProcessor = routeProcessorFeatures.some(feature =>\n\t\t\t\t\trequestedFeatures.includes(feature)\n\t\t\t\t);\n\t\t\t}\n\t\t\treturn runRouteProcessor;\n\t\t});\n\n\t\treturn reducedProcessors;\n\t}\n\n\t/**\n\t * Handle the incoming socket request.\n\t * @param socketRouteProcessors The hooks to process the incoming requests.\n\t * @param socketRoute The socket route to handle.\n\t * @param socket The socket to handle.\n\t * @param fullPath The full path of the socket route.\n\t * @param emitTopic The topic to emit the response on.\n\t * @param data The incoming data.\n\t * @internal\n\t */\n\tprivate async handleRequestSocket(\n\t\tsocketRouteProcessors: ISocketRouteProcessor[],\n\t\tsocketRoute: ISocketRoute,\n\t\tsocket: Socket,\n\t\tfullPath: string,\n\t\temitTopic: string,\n\t\trequest: IHttpRequest\n\t): Promise<void> {\n\t\tconst socketServerRequest: ISocketServerRequest = {\n\t\t\tmethod: HttpMethod.GET,\n\t\t\turl: fullPath,\n\t\t\tquery: socket.handshake.query as IHttpRequestQuery,\n\t\t\theaders: socket.handshake.headers as IHttpHeaders,\n\t\t\tbody: request.body,\n\t\t\tsocketId: socket.id\n\t\t};\n\t\tconst httpResponse: IHttpResponse = {};\n\t\tconst contextIds: IContextIds = {};\n\t\tconst processorState = {};\n\n\t\tdelete socketServerRequest.query?.EIO;\n\t\tdelete socketServerRequest.query?.transport;\n\n\t\tawait this.runProcessorsSocket(\n\t\t\tsocketRouteProcessors,\n\t\t\tsocketRoute,\n\t\t\tsocketServerRequest,\n\t\t\thttpResponse,\n\t\t\tcontextIds,\n\t\t\tprocessorState,\n\t\t\temitTopic,\n\t\t\tasync (topic, response) => {\n\t\t\t\tsocket.emit(topic, response);\n\t\t\t}\n\t\t);\n\t}\n\n\t/**\n\t * Run the socket processors for the route.\n\t * @param socketId The id of the socket.\n\t * @param socketRouteProcessors The processors to run.\n\t * @param socketRoute The route to process.\n\t * @param socketServerRequest The incoming request.\n\t * @param httpResponse The outgoing response.\n\t * @param contextIds The context IDs of the request.\n\t * @param processorState The state handed through the processors.\n\t * @param requestTopic The topic of the request.\n\t * @internal\n\t */\n\tprivate async runProcessorsSocket(\n\t\tsocketRouteProcessors: ISocketRouteProcessor[],\n\t\tsocketRoute: ISocketRoute,\n\t\tsocketServerRequest: ISocketServerRequest,\n\t\thttpResponse: IHttpResponse,\n\t\tcontextIds: IContextIds,\n\t\tprocessorState: {\n\t\t\t[id: string]: unknown;\n\t\t},\n\t\trequestTopic: string,\n\t\tresponseEmitter: (topic: string, response: IHttpResponse) => Promise<void>\n\t): Promise<void> {\n\t\tconst filteredProcessors = this.filterRouteProcessors(socketRoute, socketRouteProcessors);\n\n\t\t// Custom emit method which will also call the post processors\n\t\tconst postProcessEmit = async (\n\t\t\ttopic: string,\n\t\t\tresponse: IHttpResponse,\n\t\t\tresponseProcessorState: {\n\t\t\t\t[id: string]: unknown;\n\t\t\t}\n\t\t): Promise<void> => {\n\t\t\tawait responseEmitter(topic, response);\n\n\t\t\ttry {\n\t\t\t\t// The post processors are called after the response has been emitted\n\t\t\t\tfor (const postSocketRouteProcessor of filteredProcessors) {\n\t\t\t\t\tconst post = postSocketRouteProcessor.post?.bind(postSocketRouteProcessor);\n\t\t\t\t\tif (Is.function(post)) {\n\t\t\t\t\t\tawait post(\n\t\t\t\t\t\t\tsocketServerRequest,\n\t\t\t\t\t\t\tresponse,\n\t\t\t\t\t\t\tsocketRoute,\n\t\t\t\t\t\t\tcontextIds,\n\t\t\t\t\t\t\tresponseProcessorState,\n\t\t\t\t\t\t\tthis._loggingComponentType\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} catch (err) {\n\t\t\t\tawait this._logging?.log({\n\t\t\t\t\tlevel: \"error\",\n\t\t\t\t\tts: Date.now(),\n\t\t\t\t\tsource: FastifyWebServer.CLASS_NAME,\n\t\t\t\t\tmessage: \"postProcessorError\",\n\t\t\t\t\terror: BaseError.fromError(err),\n\t\t\t\t\tdata: {\n\t\t\t\t\t\troute: socketRoute.path\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t}\n\t\t};\n\n\t\ttry {\n\t\t\tfor (const socketRouteProcessor of filteredProcessors) {\n\t\t\t\tconst pre = socketRouteProcessor.pre?.bind(socketRouteProcessor);\n\t\t\t\tif (Is.function(pre)) {\n\t\t\t\t\tawait pre(\n\t\t\t\t\t\tsocketServerRequest,\n\t\t\t\t\t\thttpResponse,\n\t\t\t\t\t\tsocketRoute,\n\t\t\t\t\t\tcontextIds,\n\t\t\t\t\t\tprocessorState,\n\t\t\t\t\t\tthis._loggingComponentType\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// We always call all the processors regardless of any response set by a previous processor.\n\t\t\t// But if a pre processor sets a status code, we will emit the response manually, as the pre\n\t\t\t// and post processors do not receive the emit method, they just populate the response object.\n\t\t\tif (!Is.empty(httpResponse.statusCode)) {\n\t\t\t\tawait postProcessEmit(requestTopic, httpResponse, processorState);\n\t\t\t}\n\n\t\t\tawait ContextIdStore.run(contextIds, async () => {\n\t\t\t\tfor (const socketRouteProcessor of filteredProcessors) {\n\t\t\t\t\tconst process = socketRouteProcessor.process?.bind(socketRouteProcessor);\n\t\t\t\t\tif (Is.function(process)) {\n\t\t\t\t\t\tawait process(\n\t\t\t\t\t\t\tsocketServerRequest,\n\t\t\t\t\t\t\thttpResponse,\n\t\t\t\t\t\t\tsocketRoute,\n\t\t\t\t\t\t\tprocessorState,\n\t\t\t\t\t\t\tasync (topic: string, processResponse: IHttpResponse) => {\n\t\t\t\t\t\t\t\tawait postProcessEmit(topic, processResponse, processorState);\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\tthis._loggingComponentType\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t});\n\n\t\t\t// If the processors set the status to any kind of error then we should emit this manually\n\t\t\tif (\n\t\t\t\tIs.integer(httpResponse.statusCode) &&\n\t\t\t\thttpResponse.statusCode >= HttpStatusCode.badRequest\n\t\t\t) {\n\t\t\t\tawait postProcessEmit(requestTopic, httpResponse, processorState);\n\t\t\t}\n\t\t} catch (err) {\n\t\t\t// Emit any unhandled errors manually\n\t\t\tconst { error, httpStatusCode } = HttpErrorHelper.processError(err, this._includeErrorStack);\n\t\t\tHttpErrorHelper.buildResponse(httpResponse, error, httpStatusCode);\n\t\t\tawait postProcessEmit(requestTopic, httpResponse, processorState);\n\t\t}\n\t}\n\n\t/**\n\t * Initialize the cors options.\n\t * @param options The web server options.\n\t * @internal\n\t */\n\tprivate async initCors(options?: IWebServerOptions): Promise<void> {\n\t\tlet origins: string[] = [\"*\"];\n\n\t\tif (Is.arrayValue(options?.corsOrigins)) {\n\t\t\torigins = options?.corsOrigins;\n\t\t} else if (Is.stringValue(options?.corsOrigins)) {\n\t\t\torigins = [options?.corsOrigins];\n\t\t}\n\n\t\tconst hasWildcardOrigin = origins.includes(\"*\");\n\n\t\tconst methods = options?.methods ?? [\n\t\t\tHttpMethod.GET,\n\t\t\tHttpMethod.PUT,\n\t\t\tHttpMethod.POST,\n\t\t\tHttpMethod.DELETE,\n\t\t\tHttpMethod.OPTIONS\n\t\t];\n\t\tconst allowedHeaders = [\n\t\t\t\"Access-Control-Allow-Origin\",\n\t\t\t\"Content-Encoding\",\n\t\t\t\"Accept-Encoding\",\n\t\t\tHeaderTypes.ContentType,\n\t\t\tHeaderTypes.Authorization,\n\t\t\tHeaderTypes.Accept\n\t\t];\n\t\tconst exposedHeaders: string[] = [HeaderTypes.ContentDisposition, HeaderTypes.Location];\n\n\t\tif (Is.arrayValue(options?.allowedHeaders)) {\n\t\t\tallowedHeaders.push(...options.allowedHeaders);\n\t\t}\n\t\tif (Is.arrayValue(options?.exposedHeaders)) {\n\t\t\texposedHeaders.push(...options.exposedHeaders);\n\t\t}\n\n\t\tawait this._fastify.register(FastifyCors, {\n\t\t\torigin: (origin, callback) => {\n\t\t\t\tcallback(null, hasWildcardOrigin ? true : origins.includes(origin as string));\n\t\t\t},\n\t\t\tmethods,\n\t\t\tallowedHeaders,\n\t\t\texposedHeaders,\n\t\t\tcredentials: true\n\t\t});\n\t}\n}\n"]}
1
+ {"version":3,"file":"fastifyWebServer.js","sourceRoot":"","sources":["../../src/fastifyWebServer.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,eAAe,MAAM,mBAAmB,CAAC;AAChD,OAAO,WAAW,MAAM,eAAe,CAAC;AACxC,OAAO,EACN,iBAAiB,EACjB,eAAe,EAgBf,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,uBAAuB,EAAE,MAAM,0BAA0B,CAAC;AACnE,OAAO,EAAE,cAAc,EAAoB,MAAM,mBAAmB,CAAC;AACrE,OAAO,EACN,SAAS,EACT,gBAAgB,EAChB,YAAY,EACZ,YAAY,EAGZ,EAAE,EACF,YAAY,EACZ,MAAM,gBAAgB,CAAC;AAGxB,OAAO,EACN,WAAW,EACX,UAAU,EACV,cAAc,EAEd,YAAY,EACZ,MAAM,eAAe,CA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CAAC,WAAW,CAAC,kBAAkB,EAAE,WAAW,CAAC,QAAQ,CAAC,CAAC;QAExF,IAAI,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,cAAc,CAAC,EAAE,CAAC;YAC5C,cAAc,CAAC,IAAI,CAAC,GAAG,OAAO,CAAC,cAAc,CAAC,CAAC;QAChD,CAAC;QACD,IAAI,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,cAAc,CAAC,EAAE,CAAC;YAC5C,cAAc,CAAC,IAAI,CAAC,GAAG,OAAO,CAAC,cAAc,CAAC,CAAC;QAChD,CAAC;QAED,MAAM,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,WAAW,EAAE;YACzC,MAAM,EAAE,CAAC,MAAM,EAAE,QAAQ,EAAE,EAAE;gBAC5B,QAAQ,CAAC,IAAI,EAAE,iBAAiB,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,OAAO,CAAC,QAAQ,CAAC,MAAgB,CAAC,CAAC,CAAC;YAC/E,CAAC;YACD,OAAO;YACP,cAAc;YACd,cAAc;YACd,WAAW,EAAE,IAAI;SACjB,CAAC,CAAC;IACJ,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport FastifyCompress from \"@fastify/compress\";\nimport FastifyCors from \"@fastify/cors\";\nimport {\n\tHttpContextIdKeys,\n\tHttpErrorHelper,\n\ttype IBaseRoute,\n\ttype IBaseRouteProcessor,\n\ttype IHttpRequest,\n\ttype IHttpRequestPathParams,\n\ttype IHttpRequestQuery,\n\ttype IHttpResponse,\n\ttype IHttpServerRequest,\n\ttype IMimeTypeProcessor,\n\ttype IRestRoute,\n\ttype IRestRouteProcessor,\n\ttype ISocketRoute,\n\ttype ISocketRouteProcessor,\n\ttype ISocketServerRequest,\n\ttype IWebServer,\n\ttype IWebServerOptions\n} from \"@twin.org/api-models\";\nimport { JsonLdMimeTypeProcessor } from \"@twin.org/api-processors\";\nimport { ContextIdStore, type IContextIds } from \"@twin.org/context\";\nimport {\n\tBaseError,\n\tComponentFactory,\n\tGeneralError,\n\tHealthStatus,\n\ttype IError,\n\ttype IHealth,\n\tIs,\n\tStringHelper\n} from \"@twin.org/core\";\nimport type { ILoggingComponent } from \"@twin.org/logging-models\";\nimport { nameof } from \"@twin.org/nameof\";\nimport {\n\tHeaderTypes,\n\tHttpMethod,\n\tHttpStatusCode,\n\ttype IHttpHeaders,\n\tHeaderHelper\n} from \"@twin.org/web\";\nimport Fastify, {\n\ttype FastifyInstance,\n\ttype FastifyReply,\n\ttype FastifyRequest,\n\ttype FastifyServerOptions\n} from \"fastify\";\nimport type { Server, ServerOptions, Socket } from \"socket.io\";\nimport FastifySocketIO from \"./fastifySocketIo.js\";\nimport type { IFastifyWebServerConstructorOptions } from \"./models/IFastifyWebServerConstructorOptions.js\";\n\n/**\n * Implementation of the web server using Fastify.\n */\nexport class FastifyWebServer implements IWebServer<FastifyInstance> {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<FastifyWebServer>();\n\n\t/**\n\t * Default port for running the server.\n\t * @internal\n\t */\n\tprivate static readonly _DEFAULT_PORT: number = 3000;\n\n\t/**\n\t * Default host for running the server.\n\t * @internal\n\t */\n\tprivate static readonly _DEFAULT_HOST: string = \"localhost\";\n\n\t/**\n\t * The logging component type.\n\t * @internal\n\t */\n\tprivate readonly _loggingComponentType?: string;\n\n\t/**\n\t * The logging component.\n\t * @internal\n\t */\n\tprivate readonly _logging?: ILoggingComponent;\n\n\t/**\n\t * The hosting component type.\n\t * @internal\n\t */\n\tprivate readonly _hostingComponentType?: string;\n\n\t/**\n\t * The options for the server.\n\t * @internal\n\t */\n\tprivate _options?: IWebServerOptions;\n\n\t/**\n\t * The Fastify instance.\n\t * @internal\n\t */\n\tprivate readonly _fastify: FastifyInstance;\n\n\t/**\n\t * The options for the socket server.\n\t * @internal\n\t */\n\tprivate readonly _socketConfig: Partial<ServerOptions>;\n\n\t/**\n\t * Whether the server has been started.\n\t * @internal\n\t */\n\tprivate _started: boolean;\n\n\t/**\n\t * The mime type processors.\n\t * @internal\n\t */\n\tprivate readonly _mimeTypeProcessors: IMimeTypeProcessor[];\n\n\t/**\n\t * Include the stack with errors.\n\t * @internal\n\t */\n\tprivate readonly _includeErrorStack: boolean;\n\n\t/**\n\t * Create a new instance of FastifyWebServer.\n\t * @param options The options for the server.\n\t */\n\tconstructor(options?: IFastifyWebServerConstructorOptions) {\n\t\tthis._loggingComponentType = options?.loggingComponentType;\n\t\tthis._logging = ComponentFactory.getIfExists(options?.loggingComponentType ?? \"logging\");\n\t\tthis._hostingComponentType = options?.hostingComponentType;\n\t\tthis._fastify = Fastify({\n\t\t\trouterOptions: {\n\t\t\t\tmaxParamLength: 2000\n\t\t\t},\n\t\t\t...options?.config?.web\n\t\t\t// Need this cast for now as maxParamLength has moved in to routerOptions\n\t\t\t// but the TS defs has not been updated yet\n\t\t} as unknown as FastifyServerOptions);\n\t\tthis._socketConfig = {\n\t\t\tpath: \"/socket\",\n\t\t\t...options?.config?.socket\n\t\t};\n\t\tthis._started = false;\n\n\t\tthis._mimeTypeProcessors = options?.mimeTypeProcessors ?? [];\n\n\t\tconst hasJsonLd = this._mimeTypeProcessors.find(\n\t\t\tprocessor => processor.className() === \"json-ld\"\n\t\t);\n\t\tif (!hasJsonLd) {\n\t\t\tthis._mimeTypeProcessors.push(new JsonLdMimeTypeProcessor());\n\t\t}\n\n\t\tthis._includeErrorStack = options?.config?.includeErrorStack ?? false;\n\t}\n\n\t/**\n\t * Returns the class name of the component.\n\t * @returns The class name of the component.\n\t */\n\tpublic className(): string {\n\t\treturn FastifyWebServer.CLASS_NAME;\n\t}\n\n\t/**\n\t * Get the web server instance.\n\t * @returns The web server instance.\n\t */\n\tpublic getInstance(): FastifyInstance {\n\t\treturn this._fastify;\n\t}\n\n\t/**\n\t * Build the server.\n\t * @param restRouteProcessors The processors for incoming requests over REST.\n\t * @param restRoutes The REST routes.\n\t * @param socketRouteProcessors The processors for incoming requests over Sockets.\n\t * @param socketRoutes The socket routes.\n\t * @param options Options for building the server.\n\t * @returns Nothing.\n\t */\n\tpublic async build(\n\t\trestRouteProcessors?: IRestRouteProcessor[],\n\t\trestRoutes?: IRestRoute[],\n\t\tsocketRouteProcessors?: ISocketRouteProcessor[],\n\t\tsocketRoutes?: ISocketRoute[],\n\t\toptions?: IWebServerOptions\n\t): Promise<void> {\n\t\tif (Is.arrayValue(restRoutes) && !Is.arrayValue(restRouteProcessors)) {\n\t\t\tthrow new GeneralError(FastifyWebServer.CLASS_NAME, \"noRestProcessors\");\n\t\t}\n\t\tif (Is.arrayValue(socketRoutes) && !Is.arrayValue(socketRouteProcessors)) {\n\t\t\tthrow new GeneralError(FastifyWebServer.CLASS_NAME, \"noSocketProcessors\");\n\t\t}\n\t\tawait this._logging?.log({\n\t\t\tlevel: \"info\",\n\t\t\tts: Date.now(),\n\t\t\tsource: FastifyWebServer.CLASS_NAME,\n\t\t\tmessage: \"building\"\n\t\t});\n\n\t\tthis._options = options;\n\n\t\tawait this._fastify.register(FastifyCompress);\n\n\t\tif (Is.arrayValue(socketRoutes)) {\n\t\t\tawait this._fastify.register(FastifySocketIO, this._socketConfig);\n\t\t}\n\n\t\tif (Is.arrayValue(this._mimeTypeProcessors)) {\n\t\t\tfor (const contentTypeHandler of this._mimeTypeProcessors) {\n\t\t\t\tthis._fastify.addContentTypeParser(\n\t\t\t\t\tcontentTypeHandler.getTypes(),\n\t\t\t\t\t{ parseAs: \"buffer\" },\n\t\t\t\t\t(request, body, done) => {\n\t\t\t\t\t\t// Fastify does not handle this method correctly if it is async\n\t\t\t\t\t\t// so we have to use the callback method\n\t\t\t\t\t\tcontentTypeHandler\n\t\t\t\t\t\t\t.handle(body as Buffer)\n\t\t\t\t\t\t\t// eslint-disable-next-line promise/prefer-await-to-then, promise/no-callback-in-promise\n\t\t\t\t\t\t\t.then(processed => done(null, processed))\n\t\t\t\t\t\t\t// eslint-disable-next-line promise/prefer-await-to-then, promise/no-callback-in-promise\n\t\t\t\t\t\t\t.catch(err => done(BaseError.fromError(err)));\n\t\t\t\t\t}\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\n\t\tawait this.initCors(options);\n\n\t\tthis._fastify.setNotFoundHandler({}, async (request, reply) =>\n\t\t\tthis.handleRequestRest(restRouteProcessors ?? [], request, reply)\n\t\t);\n\n\t\tthis._fastify.setErrorHandler(\n\t\t\tasync (\n\t\t\t\terror: Error & {\n\t\t\t\t\tcode?: number | string;\n\t\t\t\t\tstatusCode?: number | string;\n\t\t\t\t},\n\t\t\t\trequest,\n\t\t\t\treply\n\t\t\t) => {\n\t\t\t\t// If code property is set this is a fastify error\n\t\t\t\t// otherwise it's from our framework\n\t\t\t\tlet httpStatusCode: HttpStatusCode;\n\t\t\t\tlet err: IError;\n\t\t\t\tif (Is.number(error.code) || Is.string(error.code)) {\n\t\t\t\t\terr = {\n\t\t\t\t\t\tsource: FastifyWebServer.CLASS_NAME,\n\t\t\t\t\t\tname: error.name,\n\t\t\t\t\t\tmessage: `${error.code}: ${error.message}`\n\t\t\t\t\t};\n\t\t\t\t\thttpStatusCode = (error.statusCode as HttpStatusCode) ?? HttpStatusCode.badRequest;\n\t\t\t\t} else {\n\t\t\t\t\tconst errorAndCode = HttpErrorHelper.processError(error);\n\t\t\t\t\terr = errorAndCode.error;\n\t\t\t\t\thttpStatusCode = errorAndCode.httpStatusCode;\n\t\t\t\t}\n\n\t\t\t\tawait this._logging?.log({\n\t\t\t\t\tlevel: \"error\",\n\t\t\t\t\tts: Date.now(),\n\t\t\t\t\tsource: FastifyWebServer.CLASS_NAME,\n\t\t\t\t\tmessage: \"badRequest\",\n\t\t\t\t\terror: err\n\t\t\t\t});\n\n\t\t\t\treturn reply.status(httpStatusCode).send({\n\t\t\t\t\terror: err\n\t\t\t\t});\n\t\t\t}\n\t\t);\n\n\t\tawait this.addRoutesRest(restRouteProcessors, restRoutes);\n\t\tawait this.addRoutesSocket(socketRouteProcessors, socketRoutes);\n\t}\n\n\t/**\n\t * Start the server.\n\t * @returns Nothing.\n\t */\n\tpublic async start(): Promise<void> {\n\t\tconst host = this._options?.host ?? FastifyWebServer._DEFAULT_HOST;\n\t\tconst port = this._options?.port ?? FastifyWebServer._DEFAULT_PORT;\n\n\t\tawait this._logging?.log({\n\t\t\tlevel: \"info\",\n\t\t\tts: Date.now(),\n\t\t\tsource: FastifyWebServer.CLASS_NAME,\n\t\t\tmessage: \"starting\",\n\t\t\tdata: {\n\t\t\t\thost,\n\t\t\t\tport\n\t\t\t}\n\t\t});\n\n\t\tif (!this._started) {\n\t\t\ttry {\n\t\t\t\tawait this._fastify.listen({ port, host });\n\t\t\t\tconst addresses = this._fastify.addresses();\n\n\t\t\t\tconst protocol = Is.object(this._fastify.initialConfig.https) ? \"https://\" : \"http://\";\n\t\t\t\tawait this._logging?.log({\n\t\t\t\t\tlevel: \"info\",\n\t\t\t\t\tts: Date.now(),\n\t\t\t\t\tsource: FastifyWebServer.CLASS_NAME,\n\t\t\t\t\tmessage: \"started\",\n\t\t\t\t\tdata: {\n\t\t\t\t\t\taddresses: addresses\n\t\t\t\t\t\t\t.map(\n\t\t\t\t\t\t\t\ta =>\n\t\t\t\t\t\t\t\t\t`${protocol}${a.family === \"IPv6\" ? \"[\" : \"\"}${a.address}${a.family === \"IPv6\" ? \"]\" : \"\"}:${a.port}`\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t.join(\", \")\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t\tthis._started = true;\n\t\t\t} catch (err) {\n\t\t\t\tawait this._logging?.log({\n\t\t\t\t\tlevel: \"error\",\n\t\t\t\t\tts: Date.now(),\n\t\t\t\t\tsource: FastifyWebServer.CLASS_NAME,\n\t\t\t\t\tmessage: \"startFailed\",\n\t\t\t\t\terror: BaseError.fromError(err)\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Stop the server.\n\t * @returns Nothing.\n\t */\n\tpublic async stop(): Promise<void> {\n\t\tif (this._started) {\n\t\t\tthis._started = false;\n\n\t\t\tawait this._fastify.close();\n\n\t\t\tawait this._logging?.log({\n\t\t\t\tlevel: \"info\",\n\t\t\t\tts: Date.now(),\n\t\t\t\tsource: FastifyWebServer.CLASS_NAME,\n\t\t\t\tmessage: \"stopped\"\n\t\t\t});\n\t\t}\n\t}\n\n\t/**\n\t * Perform a health check on the server by fetching its own root endpoint.\n\t * @returns The health status of the server.\n\t */\n\tpublic async health(): Promise<IHealth[]> {\n\t\tlet healthCheck: IHealth | undefined;\n\t\tif (this._fastify?.server?.listening) {\n\t\t\thealthCheck = {\n\t\t\t\tsource: FastifyWebServer.CLASS_NAME,\n\t\t\t\tstatus: HealthStatus.Ok,\n\t\t\t\tdescription: \"description\",\n\t\t\t\tmessage: \"reachable\"\n\t\t\t};\n\t\t} else {\n\t\t\thealthCheck = {\n\t\t\t\tsource: FastifyWebServer.CLASS_NAME,\n\t\t\t\tstatus: HealthStatus.Error,\n\t\t\t\tdescription: \"description\",\n\t\t\t\tmessage: \"unreachable\"\n\t\t\t};\n\t\t}\n\n\t\treturn [healthCheck];\n\t}\n\n\t/**\n\t * Add the REST routes to the server.\n\t * @param restRouteProcessors The processors for the incoming requests.\n\t * @param restRoutes The REST routes to add.\n\t * @internal\n\t */\n\tprivate async addRoutesRest(\n\t\trestRouteProcessors?: IRestRouteProcessor[],\n\t\trestRoutes?: IRestRoute[]\n\t): Promise<void> {\n\t\tif (Is.arrayValue(restRouteProcessors) && Is.arrayValue(restRoutes)) {\n\t\t\tfor (const restRoute of restRoutes) {\n\t\t\t\tlet path = StringHelper.trimTrailingSlashes(restRoute.path);\n\t\t\t\tif (!path.startsWith(\"/\")) {\n\t\t\t\t\tpath = `/${path}`;\n\t\t\t\t}\n\t\t\t\tawait this._logging?.log({\n\t\t\t\t\tlevel: \"info\",\n\t\t\t\t\tts: Date.now(),\n\t\t\t\t\tsource: FastifyWebServer.CLASS_NAME,\n\t\t\t\t\tmessage: \"restRouteAdded\",\n\t\t\t\t\tdata: { route: path, method: restRoute.method }\n\t\t\t\t});\n\t\t\t\tconst method = restRoute.method.toLowerCase() as\n\t\t\t\t\t| \"get\"\n\t\t\t\t\t| \"post\"\n\t\t\t\t\t| \"put\"\n\t\t\t\t\t| \"patch\"\n\t\t\t\t\t| \"delete\"\n\t\t\t\t\t| \"options\"\n\t\t\t\t\t| \"head\";\n\n\t\t\t\tthis._fastify[method](path, async (request, reply) =>\n\t\t\t\t\tthis.handleRequestRest(restRouteProcessors, request, reply, restRoute)\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Add the socket routes to the server.\n\t * @param socketRouteProcessors The processors for the incoming requests.\n\t * @param socketRoutes The socket routes to add.\n\t * @internal\n\t */\n\tprivate async addRoutesSocket(\n\t\tsocketRouteProcessors?: ISocketRouteProcessor[],\n\t\tsocketRoutes?: ISocketRoute[]\n\t): Promise<void> {\n\t\tif (Is.arrayValue(socketRouteProcessors) && Is.arrayValue(socketRoutes)) {\n\t\t\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\t\t\tconst io: Server = (this._fastify as any).io;\n\n\t\t\tfor (const socketRoute of socketRoutes) {\n\t\t\t\tconst path = StringHelper.trimLeadingSlashes(\n\t\t\t\t\tStringHelper.trimTrailingSlashes(socketRoute.path)\n\t\t\t\t);\n\t\t\t\tconst pathParts = path.split(\"/\");\n\n\t\t\t\tconst namespace = `/${pathParts[0]}`;\n\t\t\t\tconst topic = pathParts.slice(1).join(\"/\");\n\n\t\t\t\tawait this._logging?.log({\n\t\t\t\t\tlevel: \"info\",\n\t\t\t\t\tts: Date.now(),\n\t\t\t\t\tsource: FastifyWebServer.CLASS_NAME,\n\t\t\t\t\tmessage: \"socketRouteAdded\",\n\t\t\t\t\tdata: {\n\t\t\t\t\t\thandshakePath: this._socketConfig.path,\n\t\t\t\t\t\tnamespace,\n\t\t\t\t\t\teventName: topic\n\t\t\t\t\t}\n\t\t\t\t});\n\n\t\t\t\tconst socketNamespace = io.of(namespace);\n\n\t\t\t\tsocketNamespace.on(\"connection\", async socket => {\n\t\t\t\t\tconst socketServerRequest: ISocketServerRequest = {\n\t\t\t\t\t\tmethod: HttpMethod.GET,\n\t\t\t\t\t\turl: socket.handshake.url,\n\t\t\t\t\t\tquery: socket.handshake.query as IHttpRequestQuery,\n\t\t\t\t\t\theaders: socket.handshake.headers as IHttpHeaders,\n\t\t\t\t\t\tsocketId: socket.id\n\t\t\t\t\t};\n\n\t\t\t\t\t// Pass the connected information on to any processors\n\t\t\t\t\ttry {\n\t\t\t\t\t\tfor (const socketRouteProcessor of socketRouteProcessors) {\n\t\t\t\t\t\t\tif (socketRouteProcessor.connected) {\n\t\t\t\t\t\t\t\tawait socketRouteProcessor.connected(\n\t\t\t\t\t\t\t\t\tsocketServerRequest,\n\t\t\t\t\t\t\t\t\tsocketRoute,\n\t\t\t\t\t\t\t\t\tthis._loggingComponentType\n\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t} catch (err) {\n\t\t\t\t\t\tconst { error, httpStatusCode } = HttpErrorHelper.processError(\n\t\t\t\t\t\t\terr,\n\t\t\t\t\t\t\tthis._includeErrorStack\n\t\t\t\t\t\t);\n\t\t\t\t\t\tconst response: IHttpResponse = {};\n\t\t\t\t\t\tHttpErrorHelper.buildResponse(response, error, httpStatusCode);\n\t\t\t\t\t\tsocket.emit(topic, response);\n\t\t\t\t\t}\n\n\t\t\t\t\tsocket.on(\"disconnect\", async () => {\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\t// The socket disconnected so notify any processors\n\t\t\t\t\t\t\tfor (const socketRouteProcessor of socketRouteProcessors) {\n\t\t\t\t\t\t\t\tif (socketRouteProcessor.disconnected) {\n\t\t\t\t\t\t\t\t\tawait socketRouteProcessor.disconnected(\n\t\t\t\t\t\t\t\t\t\tsocketServerRequest,\n\t\t\t\t\t\t\t\t\t\tsocketRoute,\n\t\t\t\t\t\t\t\t\t\tthis._loggingComponentType\n\t\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t} catch {\n\t\t\t\t\t\t\t// If something fails on a disconnect there is not much we can do with it\n\t\t\t\t\t\t}\n\t\t\t\t\t});\n\n\t\t\t\t\t// Handle any incoming messages\n\t\t\t\t\tsocket.on(topic, async data => {\n\t\t\t\t\t\tawait this.handleRequestSocket(\n\t\t\t\t\t\t\tsocketRouteProcessors,\n\t\t\t\t\t\t\tsocketRoute,\n\t\t\t\t\t\t\tsocket,\n\t\t\t\t\t\t\t`/${pathParts.join(\"/\")}`,\n\t\t\t\t\t\t\ttopic,\n\t\t\t\t\t\t\tdata\n\t\t\t\t\t\t);\n\t\t\t\t\t});\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Handle the incoming REST request.\n\t * @param restRouteProcessors The hooks to process the incoming requests.\n\t * @param request The incoming request.\n\t * @param reply The outgoing response.\n\t * @param restRoute The REST route to handle.\n\t * @returns The Fastify reply with the response.\n\t * @internal\n\t */\n\tprivate async handleRequestRest(\n\t\trestRouteProcessors: IRestRouteProcessor[],\n\t\trequest: FastifyRequest,\n\t\treply: FastifyReply,\n\t\trestRoute?: IRestRoute\n\t): Promise<FastifyReply> {\n\t\tconst port =\n\t\t\t(request.port === 80 && request.protocol === \"http\") ||\n\t\t\t(request.port === 443 && request.protocol === \"https\") ||\n\t\t\t!Is.integer(request.port)\n\t\t\t\t? \"\"\n\t\t\t\t: `:${request.port}`;\n\n\t\tconst httpServerRequest: IHttpServerRequest = {\n\t\t\tmethod: request.method.toUpperCase() as HttpMethod,\n\t\t\turl: `${request.protocol}://${request.hostname}${port}${request.url}`,\n\t\t\tbody: request.body,\n\t\t\tquery: request.query as IHttpRequestQuery,\n\t\t\tpathParams: request.params as IHttpRequestPathParams,\n\t\t\theaders: request.headers as IHttpHeaders\n\t\t};\n\t\tconst httpResponse: IHttpResponse = {};\n\t\tconst contextIds: IContextIds = {\n\t\t\t[HttpContextIdKeys.IpAddress]: HeaderHelper.extractClientIps(httpServerRequest.headers).join(\n\t\t\t\t\"|\"\n\t\t\t),\n\t\t\t[HttpContextIdKeys.UserAgent]: HeaderHelper.extractUserAgent(httpServerRequest.headers),\n\t\t\t[HttpContextIdKeys.CorrelationId]: HeaderHelper.extractCorrelationId(\n\t\t\t\thttpServerRequest.headers\n\t\t\t)\n\t\t};\n\t\tconst processorState = restRoute?.processorData ?? {};\n\n\t\tif (Is.object(httpServerRequest.pathParams)) {\n\t\t\tfor (const key of Object.keys(httpServerRequest.pathParams)) {\n\t\t\t\thttpServerRequest.pathParams[key] = decodeURIComponent(httpServerRequest.pathParams[key]);\n\t\t\t}\n\t\t}\n\t\tif (Is.object(httpServerRequest.query)) {\n\t\t\tfor (const key of Object.keys(httpServerRequest.query)) {\n\t\t\t\thttpServerRequest.query[key] = decodeURIComponent(httpServerRequest.query[key]);\n\t\t\t}\n\t\t}\n\n\t\tawait this.runProcessorsRest(\n\t\t\trestRouteProcessors,\n\t\t\trestRoute,\n\t\t\thttpServerRequest,\n\t\t\thttpResponse,\n\t\t\tcontextIds,\n\t\t\tprocessorState\n\t\t);\n\n\t\tif (!Is.empty(httpResponse.headers)) {\n\t\t\tfor (const header of Object.keys(httpResponse.headers)) {\n\t\t\t\treply.header(header, httpResponse.headers[header]);\n\t\t\t}\n\t\t}\n\t\treturn reply.status(httpResponse.statusCode ?? HttpStatusCode.ok).send(httpResponse.body);\n\t}\n\n\t/**\n\t * Run the REST processors for the route.\n\t * @param restRouteProcessors The processors to run.\n\t * @param restRoute The route to process.\n\t * @param httpServerRequest The incoming request.\n\t * @param httpResponse The outgoing response.\n\t * @param contextIds The context IDs of the request.\n\t * @internal\n\t */\n\tprivate async runProcessorsRest(\n\t\trestRouteProcessors: IRestRouteProcessor[],\n\t\trestRoute: IRestRoute | undefined,\n\t\thttpServerRequest: IHttpServerRequest,\n\t\thttpResponse: IHttpResponse,\n\t\tcontextIds: IContextIds,\n\t\tprocessorState: {\n\t\t\t[id: string]: unknown;\n\t\t}\n\t): Promise<void> {\n\t\tlet hasPreError = false;\n\t\tconst filteredProcessors = this.filterRouteProcessors(restRoute, restRouteProcessors);\n\n\t\ttry {\n\t\t\t// Run inside ContextIdStore.run so pre-processors can do tenant-scoped storage lookups.\n\t\t\tawait ContextIdStore.run(contextIds, async () => {\n\t\t\t\tfor (const routeProcessor of filteredProcessors) {\n\t\t\t\t\tconst pre = routeProcessor.pre?.bind(routeProcessor);\n\t\t\t\t\tif (Is.function(pre)) {\n\t\t\t\t\t\tawait pre(httpServerRequest, httpResponse, restRoute, contextIds, processorState, {\n\t\t\t\t\t\t\tloggingComponentType: this._loggingComponentType,\n\t\t\t\t\t\t\thostingComponentType: this._hostingComponentType\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t});\n\t\t} catch (err) {\n\t\t\tconst { error, httpStatusCode } = HttpErrorHelper.processError(err, this._includeErrorStack);\n\t\t\tHttpErrorHelper.buildResponse(httpResponse, error, httpStatusCode);\n\t\t\thasPreError = true;\n\t\t}\n\n\t\t// A pre-processor may set an error response without throwing; treat that as halt.\n\t\tif (\n\t\t\t!hasPreError &&\n\t\t\tIs.integer(httpResponse.statusCode) &&\n\t\t\thttpResponse.statusCode >= HttpStatusCode.badRequest\n\t\t) {\n\t\t\thasPreError = true;\n\t\t}\n\n\t\t// Don't run the main processing if there was an error in the pre processing\n\t\t// As this is likely to perform tasks such as authentication which may have failed\n\t\tif (!hasPreError) {\n\t\t\ttry {\n\t\t\t\t// Run the processors within an async context\n\t\t\t\t// so that any services can access the context ids\n\t\t\t\tawait ContextIdStore.run(contextIds, async () => {\n\t\t\t\t\tfor (const routeProcessor of filteredProcessors) {\n\t\t\t\t\t\tconst process = routeProcessor.process?.bind(routeProcessor);\n\t\t\t\t\t\tif (Is.function(process)) {\n\t\t\t\t\t\t\tawait process(httpServerRequest, httpResponse, restRoute, processorState, {\n\t\t\t\t\t\t\t\tloggingComponentType: this._loggingComponentType,\n\t\t\t\t\t\t\t\thostingComponentType: this._hostingComponentType\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t} catch (err) {\n\t\t\t\tconst { error, httpStatusCode } = HttpErrorHelper.processError(\n\t\t\t\t\terr,\n\t\t\t\t\tthis._includeErrorStack\n\t\t\t\t);\n\t\t\t\tHttpErrorHelper.buildResponse(httpResponse, error, httpStatusCode);\n\t\t\t}\n\t\t}\n\n\t\ttry {\n\t\t\t// Always run the post processors, even if there was an error earlier\n\t\t\t// as they may perform cleanup tasks, or logging etc\n\t\t\tawait ContextIdStore.run(contextIds, async () => {\n\t\t\t\tfor (const routeProcessor of filteredProcessors) {\n\t\t\t\t\tconst post = routeProcessor.post?.bind(routeProcessor);\n\t\t\t\t\tif (Is.function(post)) {\n\t\t\t\t\t\tawait post(httpServerRequest, httpResponse, restRoute, contextIds, processorState, {\n\t\t\t\t\t\t\tloggingComponentType: this._loggingComponentType,\n\t\t\t\t\t\t\thostingComponentType: this._hostingComponentType\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t});\n\t\t} catch (err) {\n\t\t\t// Just log post processor errors\n\t\t\tawait this._logging?.log({\n\t\t\t\tlevel: \"error\",\n\t\t\t\tts: Date.now(),\n\t\t\t\tsource: FastifyWebServer.CLASS_NAME,\n\t\t\t\tmessage: \"postProcessorError\",\n\t\t\t\terror: BaseError.fromError(err),\n\t\t\t\tdata: {\n\t\t\t\t\troute: restRoute?.path ?? \"\"\n\t\t\t\t}\n\t\t\t});\n\t\t}\n\t}\n\n\t/**\n\t * Filter the route processors based on the requested features.\n\t * @param route The route to process.\n\t * @param routeProcessors The processors to filter.\n\t * @returns The filtered list of route processor.\n\t * @internal\n\t */\n\tprivate filterRouteProcessors<T extends IBaseRouteProcessor>(\n\t\troute: IBaseRoute | undefined,\n\t\trouteProcessors: T[]\n\t): T[] {\n\t\tconst requestedFeatures = route?.processorFeatures ?? [];\n\n\t\tif (!Is.arrayValue(requestedFeatures)) {\n\t\t\t// If there are no requested features, we just return all the processors\n\t\t\treturn routeProcessors;\n\t\t}\n\n\t\t// Reduce the list of route processors to just those in the requested features list\n\t\tconst reducedProcessors = routeProcessors.filter(routeProcessor => {\n\t\t\t// Processors that do not define any features always get run\n\t\t\t// If the route processor has features defined, then we only run it\n\t\t\t// if the route has at least one of those features required\n\t\t\tlet runRouteProcessor = true;\n\t\t\tif (routeProcessor.features) {\n\t\t\t\tconst routeProcessorFeatures = routeProcessor.features();\n\t\t\t\trunRouteProcessor = routeProcessorFeatures.some(feature =>\n\t\t\t\t\trequestedFeatures.includes(feature)\n\t\t\t\t);\n\t\t\t}\n\t\t\treturn runRouteProcessor;\n\t\t});\n\n\t\treturn reducedProcessors;\n\t}\n\n\t/**\n\t * Handle the incoming socket request.\n\t * @param socketRouteProcessors The hooks to process the incoming requests.\n\t * @param socketRoute The socket route to handle.\n\t * @param socket The socket to handle.\n\t * @param fullPath The full path of the socket route.\n\t * @param emitTopic The topic to emit the response on.\n\t * @param request The incoming request.\n\t * @internal\n\t */\n\tprivate async handleRequestSocket(\n\t\tsocketRouteProcessors: ISocketRouteProcessor[],\n\t\tsocketRoute: ISocketRoute,\n\t\tsocket: Socket,\n\t\tfullPath: string,\n\t\temitTopic: string,\n\t\trequest: IHttpRequest\n\t): Promise<void> {\n\t\tconst socketServerRequest: ISocketServerRequest = {\n\t\t\tmethod: HttpMethod.GET,\n\t\t\turl: fullPath,\n\t\t\tquery: socket.handshake.query as IHttpRequestQuery,\n\t\t\theaders: socket.handshake.headers as IHttpHeaders,\n\t\t\tbody: request.body,\n\t\t\tsocketId: socket.id\n\t\t};\n\t\tconst httpResponse: IHttpResponse = {};\n\t\tconst contextIds: IContextIds = {};\n\t\tconst processorState = {};\n\n\t\tdelete socketServerRequest.query?.EIO;\n\t\tdelete socketServerRequest.query?.transport;\n\n\t\tawait this.runProcessorsSocket(\n\t\t\tsocketRouteProcessors,\n\t\t\tsocketRoute,\n\t\t\tsocketServerRequest,\n\t\t\thttpResponse,\n\t\t\tcontextIds,\n\t\t\tprocessorState,\n\t\t\temitTopic,\n\t\t\tasync (topic, response) => {\n\t\t\t\tsocket.emit(topic, response);\n\t\t\t}\n\t\t);\n\t}\n\n\t/**\n\t * Run the socket processors for the route.\n\t * @param socketRouteProcessors The processors to run.\n\t * @param socketRoute The route to process.\n\t * @param socketServerRequest The incoming request.\n\t * @param httpResponse The outgoing response.\n\t * @param contextIds The context IDs of the request.\n\t * @param processorState The state handed through the processors.\n\t * @param requestTopic The topic of the request.\n\t * @param responseEmitter The emitter to send the response on.\n\t * @internal\n\t */\n\tprivate async runProcessorsSocket(\n\t\tsocketRouteProcessors: ISocketRouteProcessor[],\n\t\tsocketRoute: ISocketRoute,\n\t\tsocketServerRequest: ISocketServerRequest,\n\t\thttpResponse: IHttpResponse,\n\t\tcontextIds: IContextIds,\n\t\tprocessorState: {\n\t\t\t[id: string]: unknown;\n\t\t},\n\t\trequestTopic: string,\n\t\tresponseEmitter: (topic: string, response: IHttpResponse) => Promise<void>\n\t): Promise<void> {\n\t\tconst filteredProcessors = this.filterRouteProcessors(socketRoute, socketRouteProcessors);\n\n\t\t// Custom emit method which will also call the post processors\n\t\tconst postProcessEmit = async (\n\t\t\ttopic: string,\n\t\t\tresponse: IHttpResponse,\n\t\t\tresponseProcessorState: {\n\t\t\t\t[id: string]: unknown;\n\t\t\t}\n\t\t): Promise<void> => {\n\t\t\tawait responseEmitter(topic, response);\n\n\t\t\ttry {\n\t\t\t\t// The post processors are called after the response has been emitted\n\t\t\t\tfor (const postSocketRouteProcessor of filteredProcessors) {\n\t\t\t\t\tconst post = postSocketRouteProcessor.post?.bind(postSocketRouteProcessor);\n\t\t\t\t\tif (Is.function(post)) {\n\t\t\t\t\t\tawait post(\n\t\t\t\t\t\t\tsocketServerRequest,\n\t\t\t\t\t\t\tresponse,\n\t\t\t\t\t\t\tsocketRoute,\n\t\t\t\t\t\t\tcontextIds,\n\t\t\t\t\t\t\tresponseProcessorState,\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tloggingComponentType: this._loggingComponentType,\n\t\t\t\t\t\t\t\thostingComponentType: this._hostingComponentType\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} catch (err) {\n\t\t\t\tawait this._logging?.log({\n\t\t\t\t\tlevel: \"error\",\n\t\t\t\t\tts: Date.now(),\n\t\t\t\t\tsource: FastifyWebServer.CLASS_NAME,\n\t\t\t\t\tmessage: \"postProcessorError\",\n\t\t\t\t\terror: BaseError.fromError(err),\n\t\t\t\t\tdata: {\n\t\t\t\t\t\troute: socketRoute.path\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t}\n\t\t};\n\n\t\ttry {\n\t\t\tfor (const socketRouteProcessor of filteredProcessors) {\n\t\t\t\tconst pre = socketRouteProcessor.pre?.bind(socketRouteProcessor);\n\t\t\t\tif (Is.function(pre)) {\n\t\t\t\t\tawait pre(socketServerRequest, httpResponse, socketRoute, contextIds, processorState, {\n\t\t\t\t\t\tloggingComponentType: this._loggingComponentType,\n\t\t\t\t\t\thostingComponentType: this._hostingComponentType\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// We always call all the processors regardless of any response set by a previous processor.\n\t\t\t// But if a pre processor sets a status code, we will emit the response manually, as the pre\n\t\t\t// and post processors do not receive the emit method, they just populate the response object.\n\t\t\tif (!Is.empty(httpResponse.statusCode)) {\n\t\t\t\tawait postProcessEmit(requestTopic, httpResponse, processorState);\n\t\t\t}\n\n\t\t\tawait ContextIdStore.run(contextIds, async () => {\n\t\t\t\tfor (const socketRouteProcessor of filteredProcessors) {\n\t\t\t\t\tconst process = socketRouteProcessor.process?.bind(socketRouteProcessor);\n\t\t\t\t\tif (Is.function(process)) {\n\t\t\t\t\t\tawait process(\n\t\t\t\t\t\t\tsocketServerRequest,\n\t\t\t\t\t\t\thttpResponse,\n\t\t\t\t\t\t\tsocketRoute,\n\t\t\t\t\t\t\tprocessorState,\n\t\t\t\t\t\t\tasync (topic: string, processResponse: IHttpResponse) => {\n\t\t\t\t\t\t\t\tawait postProcessEmit(topic, processResponse, processorState);\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\tthis._loggingComponentType\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t});\n\n\t\t\t// If the processors set the status to any kind of error then we should emit this manually\n\t\t\tif (\n\t\t\t\tIs.integer(httpResponse.statusCode) &&\n\t\t\t\thttpResponse.statusCode >= HttpStatusCode.badRequest\n\t\t\t) {\n\t\t\t\tawait postProcessEmit(requestTopic, httpResponse, processorState);\n\t\t\t}\n\t\t} catch (err) {\n\t\t\t// Emit any unhandled errors manually\n\t\t\tconst { error, httpStatusCode } = HttpErrorHelper.processError(err, this._includeErrorStack);\n\t\t\tHttpErrorHelper.buildResponse(httpResponse, error, httpStatusCode);\n\t\t\tawait postProcessEmit(requestTopic, httpResponse, processorState);\n\t\t}\n\t}\n\n\t/**\n\t * Initialize the cors options.\n\t * @param options The web server options.\n\t * @internal\n\t */\n\tprivate async initCors(options?: IWebServerOptions): Promise<void> {\n\t\tlet origins: string[] = [\"*\"];\n\n\t\tif (Is.arrayValue(options?.corsOrigins)) {\n\t\t\torigins = options?.corsOrigins;\n\t\t} else if (Is.stringValue(options?.corsOrigins)) {\n\t\t\torigins = [options?.corsOrigins];\n\t\t}\n\n\t\tconst hasWildcardOrigin = origins.includes(\"*\");\n\n\t\tconst methods = options?.methods ?? [\n\t\t\tHttpMethod.GET,\n\t\t\tHttpMethod.PUT,\n\t\t\tHttpMethod.POST,\n\t\t\tHttpMethod.DELETE,\n\t\t\tHttpMethod.OPTIONS\n\t\t];\n\t\tconst allowedHeaders = [\n\t\t\t\"Access-Control-Allow-Origin\",\n\t\t\t\"Content-Encoding\",\n\t\t\t\"Accept-Encoding\",\n\t\t\tHeaderTypes.ContentType,\n\t\t\tHeaderTypes.Authorization,\n\t\t\tHeaderTypes.Accept\n\t\t];\n\t\tconst exposedHeaders: string[] = [HeaderTypes.ContentDisposition, HeaderTypes.Location];\n\n\t\tif (Is.arrayValue(options?.allowedHeaders)) {\n\t\t\tallowedHeaders.push(...options.allowedHeaders);\n\t\t}\n\t\tif (Is.arrayValue(options?.exposedHeaders)) {\n\t\t\texposedHeaders.push(...options.exposedHeaders);\n\t\t}\n\n\t\tawait this._fastify.register(FastifyCors, {\n\t\t\torigin: (origin, callback) => {\n\t\t\t\tcallback(null, hasWildcardOrigin ? true : origins.includes(origin as string));\n\t\t\t},\n\t\t\tmethods,\n\t\t\tallowedHeaders,\n\t\t\texposedHeaders,\n\t\t\tcredentials: true\n\t\t});\n\t}\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"IFastifyWebServerConstructorOptions.js","sourceRoot":"","sources":["../../../src/models/IFastifyWebServerConstructorOptions.ts"],"names":[],"mappings":"","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport type { IMimeTypeProcessor } from \"@twin.org/api-models\";\nimport type { IFastifyWebServerConfig } from \"./IFastifyWebServerConfig.js\";\n\n/**\n * The options for the Fastify web server constructor.\n */\nexport interface IFastifyWebServerConstructorOptions {\n\t/**\n\t * The type of the logging component to use, if undefined, no logging will happen.\n\t */\n\tloggingComponentType?: string;\n\n\t/**\n\t * Additional configuration for the server.\n\t */\n\tconfig?: IFastifyWebServerConfig;\n\n\t/**\n\t * Additional MIME type processors.\n\t */\n\tmimeTypeProcessors?: IMimeTypeProcessor[];\n}\n"]}
1
+ {"version":3,"file":"IFastifyWebServerConstructorOptions.js","sourceRoot":"","sources":["../../../src/models/IFastifyWebServerConstructorOptions.ts"],"names":[],"mappings":"","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport type { IMimeTypeProcessor } from \"@twin.org/api-models\";\nimport type { IFastifyWebServerConfig } from \"./IFastifyWebServerConfig.js\";\n\n/**\n * The options for the Fastify web server constructor.\n */\nexport interface IFastifyWebServerConstructorOptions {\n\t/**\n\t * The type of the logging component to use, if undefined, no logging will happen.\n\t */\n\tloggingComponentType?: string;\n\n\t/**\n\t * The type of the hosting component to use.\n\t */\n\thostingComponentType?: string;\n\n\t/**\n\t * Additional configuration for the server.\n\t */\n\tconfig?: IFastifyWebServerConfig;\n\n\t/**\n\t * Additional MIME type processors.\n\t */\n\tmimeTypeProcessors?: IMimeTypeProcessor[];\n}\n"]}
@@ -1,4 +1,5 @@
1
1
  import { type IRestRoute, type IRestRouteProcessor, type ISocketRoute, type ISocketRouteProcessor, type IWebServer, type IWebServerOptions } from "@twin.org/api-models";
2
+ import { type IHealth } from "@twin.org/core";
2
3
  import { type FastifyInstance } from "fastify";
3
4
  import type { IFastifyWebServerConstructorOptions } from "./models/IFastifyWebServerConstructorOptions.js";
4
5
  /**
@@ -14,6 +15,11 @@ export declare class FastifyWebServer implements IWebServer<FastifyInstance> {
14
15
  * @param options The options for the server.
15
16
  */
16
17
  constructor(options?: IFastifyWebServerConstructorOptions);
18
+ /**
19
+ * Returns the class name of the component.
20
+ * @returns The class name of the component.
21
+ */
22
+ className(): string;
17
23
  /**
18
24
  * Get the web server instance.
19
25
  * @returns The web server instance.
@@ -39,4 +45,9 @@ export declare class FastifyWebServer implements IWebServer<FastifyInstance> {
39
45
  * @returns Nothing.
40
46
  */
41
47
  stop(): Promise<void>;
48
+ /**
49
+ * Perform a health check on the server by fetching its own root endpoint.
50
+ * @returns The health status of the server.
51
+ */
52
+ health(): Promise<IHealth[]>;
42
53
  }
@@ -8,6 +8,10 @@ export interface IFastifyWebServerConstructorOptions {
8
8
  * The type of the logging component to use, if undefined, no logging will happen.
9
9
  */
10
10
  loggingComponentType?: string;
11
+ /**
12
+ * The type of the hosting component to use.
13
+ */
14
+ hostingComponentType?: string;
11
15
  /**
12
16
  * Additional configuration for the server.
13
17
  */