@twin.org/api-server-fastify 0.0.2-next.1 → 0.0.2-next.3

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.
@@ -288,30 +288,33 @@ class FastifyWebServer {
288
288
  for (const socketRoute of socketRoutes) {
289
289
  const path = core.StringHelper.trimLeadingSlashes(core.StringHelper.trimTrailingSlashes(socketRoute.path));
290
290
  const pathParts = path.split("/");
291
+ const namespace = `/${pathParts[0]}`;
292
+ const topic = pathParts.slice(1).join("/");
291
293
  await this._loggingConnector?.log({
292
294
  level: "info",
293
295
  ts: Date.now(),
294
296
  source: this.CLASS_NAME,
295
297
  message: `${FastifyWebServer._CLASS_NAME_CAMEL_CASE}.socketRouteAdded`,
296
- data: { route: `/${path}` }
298
+ data: {
299
+ handshakePath: this._socketConfig.path,
300
+ namespace,
301
+ eventName: topic
302
+ }
297
303
  });
298
- const socketNamespace = io.of(`/${pathParts[0]}`);
299
- const topic = pathParts.slice(1).join("/");
304
+ const socketNamespace = io.of(namespace);
300
305
  socketNamespace.on("connection", async (socket) => {
301
- const httpServerRequest = {
306
+ const socketServerRequest = {
302
307
  method: web.HttpMethod.GET,
303
308
  url: socket.handshake.url,
304
309
  query: socket.handshake.query,
305
- headers: socket.handshake.headers
310
+ headers: socket.handshake.headers,
311
+ socketId: socket.id
306
312
  };
307
313
  // Pass the connected information on to any processors
308
314
  try {
309
- const processorState = {
310
- socketId: socket.id
311
- };
312
315
  for (const socketRouteProcessor of socketRouteProcessors) {
313
- if (core.Is.function(socketRouteProcessor.connected)) {
314
- await socketRouteProcessor.connected(httpServerRequest, socketRoute, processorState);
316
+ if (socketRouteProcessor.connected) {
317
+ await socketRouteProcessor.connected(socketServerRequest, socketRoute);
315
318
  }
316
319
  }
317
320
  }
@@ -323,13 +326,10 @@ class FastifyWebServer {
323
326
  }
324
327
  socket.on("disconnect", async () => {
325
328
  try {
326
- const processorState = {
327
- socketId: socket.id
328
- };
329
329
  // The socket disconnected so notify any processors
330
330
  for (const socketRouteProcessor of socketRouteProcessors) {
331
- if (core.Is.function(socketRouteProcessor.disconnected)) {
332
- await socketRouteProcessor.disconnected(httpServerRequest, socketRoute, processorState);
331
+ if (socketRouteProcessor.disconnected) {
332
+ await socketRouteProcessor.disconnected(socketServerRequest, socketRoute);
333
333
  }
334
334
  }
335
335
  }
@@ -387,17 +387,17 @@ class FastifyWebServer {
387
387
  async runProcessorsRest(restRouteProcessors, restRoute, httpServerRequest, httpResponse, httpRequestIdentity, processorState) {
388
388
  try {
389
389
  for (const routeProcessor of restRouteProcessors) {
390
- if (core.Is.function(routeProcessor.pre)) {
390
+ if (routeProcessor.pre) {
391
391
  await routeProcessor.pre(httpServerRequest, httpResponse, restRoute, httpRequestIdentity, processorState);
392
392
  }
393
393
  }
394
394
  for (const routeProcessor of restRouteProcessors) {
395
- if (core.Is.function(routeProcessor.process)) {
395
+ if (routeProcessor.process) {
396
396
  await routeProcessor.process(httpServerRequest, httpResponse, restRoute, httpRequestIdentity, processorState);
397
397
  }
398
398
  }
399
399
  for (const routeProcessor of restRouteProcessors) {
400
- if (core.Is.function(routeProcessor.post)) {
400
+ if (routeProcessor.post) {
401
401
  await routeProcessor.post(httpServerRequest, httpResponse, restRoute, httpRequestIdentity, processorState);
402
402
  }
403
403
  }
@@ -418,44 +418,44 @@ class FastifyWebServer {
418
418
  * @internal
419
419
  */
420
420
  async handleRequestSocket(socketRouteProcessors, socketRoute, socket, fullPath, emitTopic, request) {
421
- const httpServerRequest = {
421
+ const socketServerRequest = {
422
422
  method: web.HttpMethod.GET,
423
423
  url: fullPath,
424
424
  query: socket.handshake.query,
425
425
  headers: socket.handshake.headers,
426
- body: request.body
426
+ body: request.body,
427
+ socketId: socket.id
427
428
  };
428
429
  const httpResponse = {};
429
430
  const httpRequestIdentity = {};
430
- const processorState = {
431
- socketId: socket.id
432
- };
433
- delete httpServerRequest.query?.EIO;
434
- delete httpServerRequest.query?.transport;
435
- await this.runProcessorsSocket(socketRouteProcessors, socketRoute, httpServerRequest, httpResponse, httpRequestIdentity, processorState, emitTopic, async (topic, response) => {
431
+ const processorState = {};
432
+ delete socketServerRequest.query?.EIO;
433
+ delete socketServerRequest.query?.transport;
434
+ await this.runProcessorsSocket(socketRouteProcessors, socketRoute, socketServerRequest, httpResponse, httpRequestIdentity, processorState, emitTopic, async (topic, response) => {
436
435
  await socket.emit(topic, response);
437
436
  });
438
437
  }
439
438
  /**
440
439
  * Run the socket processors for the route.
440
+ * @param socketId The id of the socket.
441
441
  * @param socketRouteProcessors The processors to run.
442
442
  * @param socketRoute The route to process.
443
- * @param httpServerRequest The incoming request.
443
+ * @param socketServerRequest The incoming request.
444
444
  * @param httpResponse The outgoing response.
445
445
  * @param httpRequestIdentity The identity context for the request.
446
446
  * @param processorState The state handed through the processors.
447
447
  * @param requestTopic The topic of the request.
448
448
  * @internal
449
449
  */
450
- async runProcessorsSocket(socketRouteProcessors, socketRoute, httpServerRequest, httpResponse, httpRequestIdentity, processorState, requestTopic, responseEmitter) {
450
+ async runProcessorsSocket(socketRouteProcessors, socketRoute, socketServerRequest, httpResponse, httpRequestIdentity, processorState, requestTopic, responseEmitter) {
451
451
  // Custom emit method which will also call the post processors
452
452
  const postProcessEmit = async (topic, response, responseProcessorState) => {
453
453
  await responseEmitter(topic, response);
454
454
  try {
455
455
  // The post processors are called after the response has been emitted
456
456
  for (const postSocketRouteProcessor of socketRouteProcessors) {
457
- if (core.Is.function(postSocketRouteProcessor.post)) {
458
- await postSocketRouteProcessor.post(httpServerRequest, response, socketRoute, httpRequestIdentity, responseProcessorState);
457
+ if (postSocketRouteProcessor.post) {
458
+ await postSocketRouteProcessor.post(socketServerRequest, response, socketRoute, httpRequestIdentity, responseProcessorState);
459
459
  }
460
460
  }
461
461
  }
@@ -474,8 +474,8 @@ class FastifyWebServer {
474
474
  };
475
475
  try {
476
476
  for (const socketRouteProcessor of socketRouteProcessors) {
477
- if (core.Is.function(socketRouteProcessor.pre)) {
478
- await socketRouteProcessor.pre(httpServerRequest, httpResponse, socketRoute, httpRequestIdentity, processorState);
477
+ if (socketRouteProcessor.pre) {
478
+ await socketRouteProcessor.pre(socketServerRequest, httpResponse, socketRoute, httpRequestIdentity, processorState);
479
479
  }
480
480
  }
481
481
  // We always call all the processors regardless of any response set by a previous processor.
@@ -485,8 +485,8 @@ class FastifyWebServer {
485
485
  await postProcessEmit(requestTopic, httpResponse, processorState);
486
486
  }
487
487
  for (const socketRouteProcessor of socketRouteProcessors) {
488
- if (core.Is.function(socketRouteProcessor.process)) {
489
- await socketRouteProcessor.process(httpServerRequest, httpResponse, socketRoute, httpRequestIdentity, processorState, async (topic, processResponse) => {
488
+ if (socketRouteProcessor.process) {
489
+ await socketRouteProcessor.process(socketServerRequest, httpResponse, socketRoute, httpRequestIdentity, processorState, async (topic, processResponse) => {
490
490
  await postProcessEmit(topic, processResponse, processorState);
491
491
  });
492
492
  }
@@ -286,30 +286,33 @@ class FastifyWebServer {
286
286
  for (const socketRoute of socketRoutes) {
287
287
  const path = StringHelper.trimLeadingSlashes(StringHelper.trimTrailingSlashes(socketRoute.path));
288
288
  const pathParts = path.split("/");
289
+ const namespace = `/${pathParts[0]}`;
290
+ const topic = pathParts.slice(1).join("/");
289
291
  await this._loggingConnector?.log({
290
292
  level: "info",
291
293
  ts: Date.now(),
292
294
  source: this.CLASS_NAME,
293
295
  message: `${FastifyWebServer._CLASS_NAME_CAMEL_CASE}.socketRouteAdded`,
294
- data: { route: `/${path}` }
296
+ data: {
297
+ handshakePath: this._socketConfig.path,
298
+ namespace,
299
+ eventName: topic
300
+ }
295
301
  });
296
- const socketNamespace = io.of(`/${pathParts[0]}`);
297
- const topic = pathParts.slice(1).join("/");
302
+ const socketNamespace = io.of(namespace);
298
303
  socketNamespace.on("connection", async (socket) => {
299
- const httpServerRequest = {
304
+ const socketServerRequest = {
300
305
  method: HttpMethod.GET,
301
306
  url: socket.handshake.url,
302
307
  query: socket.handshake.query,
303
- headers: socket.handshake.headers
308
+ headers: socket.handshake.headers,
309
+ socketId: socket.id
304
310
  };
305
311
  // Pass the connected information on to any processors
306
312
  try {
307
- const processorState = {
308
- socketId: socket.id
309
- };
310
313
  for (const socketRouteProcessor of socketRouteProcessors) {
311
- if (Is.function(socketRouteProcessor.connected)) {
312
- await socketRouteProcessor.connected(httpServerRequest, socketRoute, processorState);
314
+ if (socketRouteProcessor.connected) {
315
+ await socketRouteProcessor.connected(socketServerRequest, socketRoute);
313
316
  }
314
317
  }
315
318
  }
@@ -321,13 +324,10 @@ class FastifyWebServer {
321
324
  }
322
325
  socket.on("disconnect", async () => {
323
326
  try {
324
- const processorState = {
325
- socketId: socket.id
326
- };
327
327
  // The socket disconnected so notify any processors
328
328
  for (const socketRouteProcessor of socketRouteProcessors) {
329
- if (Is.function(socketRouteProcessor.disconnected)) {
330
- await socketRouteProcessor.disconnected(httpServerRequest, socketRoute, processorState);
329
+ if (socketRouteProcessor.disconnected) {
330
+ await socketRouteProcessor.disconnected(socketServerRequest, socketRoute);
331
331
  }
332
332
  }
333
333
  }
@@ -385,17 +385,17 @@ class FastifyWebServer {
385
385
  async runProcessorsRest(restRouteProcessors, restRoute, httpServerRequest, httpResponse, httpRequestIdentity, processorState) {
386
386
  try {
387
387
  for (const routeProcessor of restRouteProcessors) {
388
- if (Is.function(routeProcessor.pre)) {
388
+ if (routeProcessor.pre) {
389
389
  await routeProcessor.pre(httpServerRequest, httpResponse, restRoute, httpRequestIdentity, processorState);
390
390
  }
391
391
  }
392
392
  for (const routeProcessor of restRouteProcessors) {
393
- if (Is.function(routeProcessor.process)) {
393
+ if (routeProcessor.process) {
394
394
  await routeProcessor.process(httpServerRequest, httpResponse, restRoute, httpRequestIdentity, processorState);
395
395
  }
396
396
  }
397
397
  for (const routeProcessor of restRouteProcessors) {
398
- if (Is.function(routeProcessor.post)) {
398
+ if (routeProcessor.post) {
399
399
  await routeProcessor.post(httpServerRequest, httpResponse, restRoute, httpRequestIdentity, processorState);
400
400
  }
401
401
  }
@@ -416,44 +416,44 @@ class FastifyWebServer {
416
416
  * @internal
417
417
  */
418
418
  async handleRequestSocket(socketRouteProcessors, socketRoute, socket, fullPath, emitTopic, request) {
419
- const httpServerRequest = {
419
+ const socketServerRequest = {
420
420
  method: HttpMethod.GET,
421
421
  url: fullPath,
422
422
  query: socket.handshake.query,
423
423
  headers: socket.handshake.headers,
424
- body: request.body
424
+ body: request.body,
425
+ socketId: socket.id
425
426
  };
426
427
  const httpResponse = {};
427
428
  const httpRequestIdentity = {};
428
- const processorState = {
429
- socketId: socket.id
430
- };
431
- delete httpServerRequest.query?.EIO;
432
- delete httpServerRequest.query?.transport;
433
- await this.runProcessorsSocket(socketRouteProcessors, socketRoute, httpServerRequest, httpResponse, httpRequestIdentity, processorState, emitTopic, async (topic, response) => {
429
+ const processorState = {};
430
+ delete socketServerRequest.query?.EIO;
431
+ delete socketServerRequest.query?.transport;
432
+ await this.runProcessorsSocket(socketRouteProcessors, socketRoute, socketServerRequest, httpResponse, httpRequestIdentity, processorState, emitTopic, async (topic, response) => {
434
433
  await socket.emit(topic, response);
435
434
  });
436
435
  }
437
436
  /**
438
437
  * Run the socket processors for the route.
438
+ * @param socketId The id of the socket.
439
439
  * @param socketRouteProcessors The processors to run.
440
440
  * @param socketRoute The route to process.
441
- * @param httpServerRequest The incoming request.
441
+ * @param socketServerRequest The incoming request.
442
442
  * @param httpResponse The outgoing response.
443
443
  * @param httpRequestIdentity The identity context for the request.
444
444
  * @param processorState The state handed through the processors.
445
445
  * @param requestTopic The topic of the request.
446
446
  * @internal
447
447
  */
448
- async runProcessorsSocket(socketRouteProcessors, socketRoute, httpServerRequest, httpResponse, httpRequestIdentity, processorState, requestTopic, responseEmitter) {
448
+ async runProcessorsSocket(socketRouteProcessors, socketRoute, socketServerRequest, httpResponse, httpRequestIdentity, processorState, requestTopic, responseEmitter) {
449
449
  // Custom emit method which will also call the post processors
450
450
  const postProcessEmit = async (topic, response, responseProcessorState) => {
451
451
  await responseEmitter(topic, response);
452
452
  try {
453
453
  // The post processors are called after the response has been emitted
454
454
  for (const postSocketRouteProcessor of socketRouteProcessors) {
455
- if (Is.function(postSocketRouteProcessor.post)) {
456
- await postSocketRouteProcessor.post(httpServerRequest, response, socketRoute, httpRequestIdentity, responseProcessorState);
455
+ if (postSocketRouteProcessor.post) {
456
+ await postSocketRouteProcessor.post(socketServerRequest, response, socketRoute, httpRequestIdentity, responseProcessorState);
457
457
  }
458
458
  }
459
459
  }
@@ -472,8 +472,8 @@ class FastifyWebServer {
472
472
  };
473
473
  try {
474
474
  for (const socketRouteProcessor of socketRouteProcessors) {
475
- if (Is.function(socketRouteProcessor.pre)) {
476
- await socketRouteProcessor.pre(httpServerRequest, httpResponse, socketRoute, httpRequestIdentity, processorState);
475
+ if (socketRouteProcessor.pre) {
476
+ await socketRouteProcessor.pre(socketServerRequest, httpResponse, socketRoute, httpRequestIdentity, processorState);
477
477
  }
478
478
  }
479
479
  // We always call all the processors regardless of any response set by a previous processor.
@@ -483,8 +483,8 @@ class FastifyWebServer {
483
483
  await postProcessEmit(requestTopic, httpResponse, processorState);
484
484
  }
485
485
  for (const socketRouteProcessor of socketRouteProcessors) {
486
- if (Is.function(socketRouteProcessor.process)) {
487
- await socketRouteProcessor.process(httpServerRequest, httpResponse, socketRoute, httpRequestIdentity, processorState, async (topic, processResponse) => {
486
+ if (socketRouteProcessor.process) {
487
+ await socketRouteProcessor.process(socketServerRequest, httpResponse, socketRoute, httpRequestIdentity, processorState, async (topic, processResponse) => {
488
488
  await postProcessEmit(topic, processResponse, processorState);
489
489
  });
490
490
  }
package/docs/changelog.md CHANGED
@@ -1,5 +1,37 @@
1
1
  # @twin.org/api-server-fastify - Changelog
2
2
 
3
+ ## [0.0.2-next.3](https://github.com/twinfoundation/api/compare/api-server-fastify-v0.0.2-next.2...api-server-fastify-v0.0.2-next.3) (2025-07-24)
4
+
5
+
6
+ ### Features
7
+
8
+ * add socket id, connect and disconnect ([20b0d0e](https://github.com/twinfoundation/api/commit/20b0d0ec279cab46141fee09de2c4a7087cdce16))
9
+
10
+
11
+ ### Dependencies
12
+
13
+ * The following workspace dependencies were updated
14
+ * dependencies
15
+ * @twin.org/api-core bumped from 0.0.2-next.2 to 0.0.2-next.3
16
+ * @twin.org/api-models bumped from 0.0.2-next.2 to 0.0.2-next.3
17
+ * @twin.org/api-processors bumped from 0.0.2-next.2 to 0.0.2-next.3
18
+
19
+ ## [0.0.2-next.2](https://github.com/twinfoundation/api/compare/api-server-fastify-v0.0.2-next.1...api-server-fastify-v0.0.2-next.2) (2025-07-17)
20
+
21
+
22
+ ### Features
23
+
24
+ * improve socket route logging ([b8d9519](https://github.com/twinfoundation/api/commit/b8d95199f838ac6ba9f45c30ef7c4e613201ff53))
25
+
26
+
27
+ ### Dependencies
28
+
29
+ * The following workspace dependencies were updated
30
+ * dependencies
31
+ * @twin.org/api-core bumped from 0.0.2-next.1 to 0.0.2-next.2
32
+ * @twin.org/api-models bumped from 0.0.2-next.1 to 0.0.2-next.2
33
+ * @twin.org/api-processors bumped from 0.0.2-next.1 to 0.0.2-next.2
34
+
3
35
  ## [0.0.2-next.1](https://github.com/twinfoundation/api/compare/api-server-fastify-v0.0.2-next.0...api-server-fastify-v0.0.2-next.1) (2025-07-08)
4
36
 
5
37
 
package/locales/en.json CHANGED
@@ -7,7 +7,7 @@
7
7
  "stopped": "The Web Server was stopped",
8
8
  "badRequest": "The web server could not handle the request",
9
9
  "restRouteAdded": "Added REST route \"{route}\" \"{method}\"",
10
- "socketRouteAdded": "Added socket route \"{route}\"",
10
+ "socketRouteAdded": "Added socket route: handshake path \"{handshakePath}\", namespace \"{namespace}\", event name \"{eventName}\"",
11
11
  "noRestProcessors": "You must configure at least one REST processor",
12
12
  "noSocketProcessors": "You must configure at least one socket processor",
13
13
  "postProcessorError": "There was a failure after in a post processor for route \"{route}\""
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@twin.org/api-server-fastify",
3
- "version": "0.0.2-next.1",
3
+ "version": "0.0.2-next.3",
4
4
  "description": "Use Fastify as the core web server for APIs",
5
5
  "repository": {
6
6
  "type": "git",
@@ -14,16 +14,16 @@
14
14
  "node": ">=20.0.0"
15
15
  },
16
16
  "dependencies": {
17
- "@fastify/compress": "8.0.3",
17
+ "@fastify/compress": "8.1.0",
18
18
  "@fastify/cors": "11.0.1",
19
- "@twin.org/api-core": "0.0.2-next.1",
20
- "@twin.org/api-models": "0.0.2-next.1",
21
- "@twin.org/api-processors": "0.0.2-next.1",
19
+ "@twin.org/api-core": "0.0.2-next.3",
20
+ "@twin.org/api-models": "0.0.2-next.3",
21
+ "@twin.org/api-processors": "0.0.2-next.3",
22
22
  "@twin.org/core": "next",
23
23
  "@twin.org/logging-models": "next",
24
24
  "@twin.org/nameof": "next",
25
25
  "@twin.org/web": "next",
26
- "fastify": "5.3.3",
26
+ "fastify": "5.4.0",
27
27
  "socket.io": "4.8.1"
28
28
  },
29
29
  "main": "./dist/cjs/index.cjs",