@twin.org/api-server-fastify 0.0.2-next.2 → 0.0.2-next.5
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/dist/cjs/index.cjs
CHANGED
|
@@ -5,7 +5,6 @@ var FastifyCors = require('@fastify/cors');
|
|
|
5
5
|
var apiModels = require('@twin.org/api-models');
|
|
6
6
|
var apiProcessors = require('@twin.org/api-processors');
|
|
7
7
|
var core = require('@twin.org/core');
|
|
8
|
-
var loggingModels = require('@twin.org/logging-models');
|
|
9
8
|
var web = require('@twin.org/web');
|
|
10
9
|
var Fastify = require('fastify');
|
|
11
10
|
var fp = require('fastify-plugin');
|
|
@@ -48,10 +47,15 @@ class FastifyWebServer {
|
|
|
48
47
|
*/
|
|
49
48
|
CLASS_NAME = "FastifyWebServer";
|
|
50
49
|
/**
|
|
51
|
-
* The logging
|
|
50
|
+
* The logging component type.
|
|
52
51
|
* @internal
|
|
53
52
|
*/
|
|
54
|
-
|
|
53
|
+
_loggingComponentType;
|
|
54
|
+
/**
|
|
55
|
+
* The logging component.
|
|
56
|
+
* @internal
|
|
57
|
+
*/
|
|
58
|
+
_loggingComponent;
|
|
55
59
|
/**
|
|
56
60
|
* The options for the server.
|
|
57
61
|
* @internal
|
|
@@ -87,8 +91,9 @@ class FastifyWebServer {
|
|
|
87
91
|
* @param options The options for the server.
|
|
88
92
|
*/
|
|
89
93
|
constructor(options) {
|
|
90
|
-
this.
|
|
91
|
-
|
|
94
|
+
this._loggingComponentType = options?.loggingComponentType;
|
|
95
|
+
this._loggingComponent = core.Is.stringValue(options?.loggingComponentType)
|
|
96
|
+
? core.ComponentFactory.get(options.loggingComponentType)
|
|
92
97
|
: undefined;
|
|
93
98
|
this._fastify = Fastify({
|
|
94
99
|
maxParamLength: 2000,
|
|
@@ -129,7 +134,7 @@ class FastifyWebServer {
|
|
|
129
134
|
if (core.Is.arrayValue(socketRoutes) && !core.Is.arrayValue(socketRouteProcessors)) {
|
|
130
135
|
throw new core.GeneralError(this.CLASS_NAME, "noSocketProcessors");
|
|
131
136
|
}
|
|
132
|
-
await this.
|
|
137
|
+
await this._loggingComponent?.log({
|
|
133
138
|
level: "info",
|
|
134
139
|
ts: Date.now(),
|
|
135
140
|
source: this.CLASS_NAME,
|
|
@@ -174,7 +179,7 @@ class FastifyWebServer {
|
|
|
174
179
|
err = errorAndCode.error;
|
|
175
180
|
httpStatusCode = errorAndCode.httpStatusCode;
|
|
176
181
|
}
|
|
177
|
-
await this.
|
|
182
|
+
await this._loggingComponent?.log({
|
|
178
183
|
level: "error",
|
|
179
184
|
ts: Date.now(),
|
|
180
185
|
source: this.CLASS_NAME,
|
|
@@ -195,7 +200,7 @@ class FastifyWebServer {
|
|
|
195
200
|
async start() {
|
|
196
201
|
const host = this._options?.host ?? FastifyWebServer._DEFAULT_HOST;
|
|
197
202
|
const port = this._options?.port ?? FastifyWebServer._DEFAULT_PORT;
|
|
198
|
-
await this.
|
|
203
|
+
await this._loggingComponent?.log({
|
|
199
204
|
level: "info",
|
|
200
205
|
ts: Date.now(),
|
|
201
206
|
source: this.CLASS_NAME,
|
|
@@ -210,7 +215,7 @@ class FastifyWebServer {
|
|
|
210
215
|
await this._fastify.listen({ port, host });
|
|
211
216
|
const addresses = this._fastify.addresses();
|
|
212
217
|
const protocol = core.Is.object(this._fastify.initialConfig.https) ? "https://" : "http://";
|
|
213
|
-
await this.
|
|
218
|
+
await this._loggingComponent?.log({
|
|
214
219
|
level: "info",
|
|
215
220
|
ts: Date.now(),
|
|
216
221
|
source: this.CLASS_NAME,
|
|
@@ -224,7 +229,7 @@ class FastifyWebServer {
|
|
|
224
229
|
this._started = true;
|
|
225
230
|
}
|
|
226
231
|
catch (err) {
|
|
227
|
-
await this.
|
|
232
|
+
await this._loggingComponent?.log({
|
|
228
233
|
level: "error",
|
|
229
234
|
ts: Date.now(),
|
|
230
235
|
source: this.CLASS_NAME,
|
|
@@ -242,7 +247,7 @@ class FastifyWebServer {
|
|
|
242
247
|
if (this._started) {
|
|
243
248
|
this._started = false;
|
|
244
249
|
await this._fastify.close();
|
|
245
|
-
await this.
|
|
250
|
+
await this._loggingComponent?.log({
|
|
246
251
|
level: "info",
|
|
247
252
|
ts: Date.now(),
|
|
248
253
|
source: this.CLASS_NAME,
|
|
@@ -263,7 +268,7 @@ class FastifyWebServer {
|
|
|
263
268
|
if (!path.startsWith("/")) {
|
|
264
269
|
path = `/${path}`;
|
|
265
270
|
}
|
|
266
|
-
await this.
|
|
271
|
+
await this._loggingComponent?.log({
|
|
267
272
|
level: "info",
|
|
268
273
|
ts: Date.now(),
|
|
269
274
|
source: this.CLASS_NAME,
|
|
@@ -290,7 +295,7 @@ class FastifyWebServer {
|
|
|
290
295
|
const pathParts = path.split("/");
|
|
291
296
|
const namespace = `/${pathParts[0]}`;
|
|
292
297
|
const topic = pathParts.slice(1).join("/");
|
|
293
|
-
await this.
|
|
298
|
+
await this._loggingComponent?.log({
|
|
294
299
|
level: "info",
|
|
295
300
|
ts: Date.now(),
|
|
296
301
|
source: this.CLASS_NAME,
|
|
@@ -303,20 +308,18 @@ class FastifyWebServer {
|
|
|
303
308
|
});
|
|
304
309
|
const socketNamespace = io.of(namespace);
|
|
305
310
|
socketNamespace.on("connection", async (socket) => {
|
|
306
|
-
const
|
|
311
|
+
const socketServerRequest = {
|
|
307
312
|
method: web.HttpMethod.GET,
|
|
308
313
|
url: socket.handshake.url,
|
|
309
314
|
query: socket.handshake.query,
|
|
310
|
-
headers: socket.handshake.headers
|
|
315
|
+
headers: socket.handshake.headers,
|
|
316
|
+
socketId: socket.id
|
|
311
317
|
};
|
|
312
318
|
// Pass the connected information on to any processors
|
|
313
319
|
try {
|
|
314
|
-
const processorState = {
|
|
315
|
-
socketId: socket.id
|
|
316
|
-
};
|
|
317
320
|
for (const socketRouteProcessor of socketRouteProcessors) {
|
|
318
|
-
if (
|
|
319
|
-
await socketRouteProcessor.connected(
|
|
321
|
+
if (socketRouteProcessor.connected) {
|
|
322
|
+
await socketRouteProcessor.connected(socketServerRequest, socketRoute, this._loggingComponentType);
|
|
320
323
|
}
|
|
321
324
|
}
|
|
322
325
|
}
|
|
@@ -328,13 +331,10 @@ class FastifyWebServer {
|
|
|
328
331
|
}
|
|
329
332
|
socket.on("disconnect", async () => {
|
|
330
333
|
try {
|
|
331
|
-
const processorState = {
|
|
332
|
-
socketId: socket.id
|
|
333
|
-
};
|
|
334
334
|
// The socket disconnected so notify any processors
|
|
335
335
|
for (const socketRouteProcessor of socketRouteProcessors) {
|
|
336
|
-
if (
|
|
337
|
-
await socketRouteProcessor.disconnected(
|
|
336
|
+
if (socketRouteProcessor.disconnected) {
|
|
337
|
+
await socketRouteProcessor.disconnected(socketServerRequest, socketRoute, this._loggingComponentType);
|
|
338
338
|
}
|
|
339
339
|
}
|
|
340
340
|
}
|
|
@@ -392,18 +392,18 @@ class FastifyWebServer {
|
|
|
392
392
|
async runProcessorsRest(restRouteProcessors, restRoute, httpServerRequest, httpResponse, httpRequestIdentity, processorState) {
|
|
393
393
|
try {
|
|
394
394
|
for (const routeProcessor of restRouteProcessors) {
|
|
395
|
-
if (
|
|
396
|
-
await routeProcessor.pre(httpServerRequest, httpResponse, restRoute, httpRequestIdentity, processorState);
|
|
395
|
+
if (routeProcessor.pre) {
|
|
396
|
+
await routeProcessor.pre(httpServerRequest, httpResponse, restRoute, httpRequestIdentity, processorState, this._loggingComponentType);
|
|
397
397
|
}
|
|
398
398
|
}
|
|
399
399
|
for (const routeProcessor of restRouteProcessors) {
|
|
400
|
-
if (
|
|
401
|
-
await routeProcessor.process(httpServerRequest, httpResponse, restRoute, httpRequestIdentity, processorState);
|
|
400
|
+
if (routeProcessor.process) {
|
|
401
|
+
await routeProcessor.process(httpServerRequest, httpResponse, restRoute, httpRequestIdentity, processorState, this._loggingComponentType);
|
|
402
402
|
}
|
|
403
403
|
}
|
|
404
404
|
for (const routeProcessor of restRouteProcessors) {
|
|
405
|
-
if (
|
|
406
|
-
await routeProcessor.post(httpServerRequest, httpResponse, restRoute, httpRequestIdentity, processorState);
|
|
405
|
+
if (routeProcessor.post) {
|
|
406
|
+
await routeProcessor.post(httpServerRequest, httpResponse, restRoute, httpRequestIdentity, processorState, this._loggingComponentType);
|
|
407
407
|
}
|
|
408
408
|
}
|
|
409
409
|
}
|
|
@@ -423,49 +423,49 @@ class FastifyWebServer {
|
|
|
423
423
|
* @internal
|
|
424
424
|
*/
|
|
425
425
|
async handleRequestSocket(socketRouteProcessors, socketRoute, socket, fullPath, emitTopic, request) {
|
|
426
|
-
const
|
|
426
|
+
const socketServerRequest = {
|
|
427
427
|
method: web.HttpMethod.GET,
|
|
428
428
|
url: fullPath,
|
|
429
429
|
query: socket.handshake.query,
|
|
430
430
|
headers: socket.handshake.headers,
|
|
431
|
-
body: request.body
|
|
431
|
+
body: request.body,
|
|
432
|
+
socketId: socket.id
|
|
432
433
|
};
|
|
433
434
|
const httpResponse = {};
|
|
434
435
|
const httpRequestIdentity = {};
|
|
435
|
-
const processorState = {
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
delete httpServerRequest.query?.transport;
|
|
440
|
-
await this.runProcessorsSocket(socketRouteProcessors, socketRoute, httpServerRequest, httpResponse, httpRequestIdentity, processorState, emitTopic, async (topic, response) => {
|
|
436
|
+
const processorState = {};
|
|
437
|
+
delete socketServerRequest.query?.EIO;
|
|
438
|
+
delete socketServerRequest.query?.transport;
|
|
439
|
+
await this.runProcessorsSocket(socketRouteProcessors, socketRoute, socketServerRequest, httpResponse, httpRequestIdentity, processorState, emitTopic, async (topic, response) => {
|
|
441
440
|
await socket.emit(topic, response);
|
|
442
441
|
});
|
|
443
442
|
}
|
|
444
443
|
/**
|
|
445
444
|
* Run the socket processors for the route.
|
|
445
|
+
* @param socketId The id of the socket.
|
|
446
446
|
* @param socketRouteProcessors The processors to run.
|
|
447
447
|
* @param socketRoute The route to process.
|
|
448
|
-
* @param
|
|
448
|
+
* @param socketServerRequest The incoming request.
|
|
449
449
|
* @param httpResponse The outgoing response.
|
|
450
450
|
* @param httpRequestIdentity The identity context for the request.
|
|
451
451
|
* @param processorState The state handed through the processors.
|
|
452
452
|
* @param requestTopic The topic of the request.
|
|
453
453
|
* @internal
|
|
454
454
|
*/
|
|
455
|
-
async runProcessorsSocket(socketRouteProcessors, socketRoute,
|
|
455
|
+
async runProcessorsSocket(socketRouteProcessors, socketRoute, socketServerRequest, httpResponse, httpRequestIdentity, processorState, requestTopic, responseEmitter) {
|
|
456
456
|
// Custom emit method which will also call the post processors
|
|
457
457
|
const postProcessEmit = async (topic, response, responseProcessorState) => {
|
|
458
458
|
await responseEmitter(topic, response);
|
|
459
459
|
try {
|
|
460
460
|
// The post processors are called after the response has been emitted
|
|
461
461
|
for (const postSocketRouteProcessor of socketRouteProcessors) {
|
|
462
|
-
if (
|
|
463
|
-
await postSocketRouteProcessor.post(
|
|
462
|
+
if (postSocketRouteProcessor.post) {
|
|
463
|
+
await postSocketRouteProcessor.post(socketServerRequest, response, socketRoute, httpRequestIdentity, responseProcessorState, this._loggingComponentType);
|
|
464
464
|
}
|
|
465
465
|
}
|
|
466
466
|
}
|
|
467
467
|
catch (err) {
|
|
468
|
-
this.
|
|
468
|
+
this._loggingComponent?.log({
|
|
469
469
|
level: "error",
|
|
470
470
|
ts: Date.now(),
|
|
471
471
|
source: this.CLASS_NAME,
|
|
@@ -479,8 +479,8 @@ class FastifyWebServer {
|
|
|
479
479
|
};
|
|
480
480
|
try {
|
|
481
481
|
for (const socketRouteProcessor of socketRouteProcessors) {
|
|
482
|
-
if (
|
|
483
|
-
await socketRouteProcessor.pre(
|
|
482
|
+
if (socketRouteProcessor.pre) {
|
|
483
|
+
await socketRouteProcessor.pre(socketServerRequest, httpResponse, socketRoute, httpRequestIdentity, processorState, this._loggingComponentType);
|
|
484
484
|
}
|
|
485
485
|
}
|
|
486
486
|
// We always call all the processors regardless of any response set by a previous processor.
|
|
@@ -490,10 +490,10 @@ class FastifyWebServer {
|
|
|
490
490
|
await postProcessEmit(requestTopic, httpResponse, processorState);
|
|
491
491
|
}
|
|
492
492
|
for (const socketRouteProcessor of socketRouteProcessors) {
|
|
493
|
-
if (
|
|
494
|
-
await socketRouteProcessor.process(
|
|
493
|
+
if (socketRouteProcessor.process) {
|
|
494
|
+
await socketRouteProcessor.process(socketServerRequest, httpResponse, socketRoute, httpRequestIdentity, processorState, async (topic, processResponse) => {
|
|
495
495
|
await postProcessEmit(topic, processResponse, processorState);
|
|
496
|
-
});
|
|
496
|
+
}, this._loggingComponentType);
|
|
497
497
|
}
|
|
498
498
|
}
|
|
499
499
|
// If the processors set the status to any kind of error then we should emit this manually
|
package/dist/esm/index.mjs
CHANGED
|
@@ -2,8 +2,7 @@ import FastifyCompress from '@fastify/compress';
|
|
|
2
2
|
import FastifyCors from '@fastify/cors';
|
|
3
3
|
import { HttpErrorHelper } from '@twin.org/api-models';
|
|
4
4
|
import { JsonLdMimeTypeProcessor } from '@twin.org/api-processors';
|
|
5
|
-
import { StringHelper, Is, GeneralError, BaseError } from '@twin.org/core';
|
|
6
|
-
import { LoggingConnectorFactory } from '@twin.org/logging-models';
|
|
5
|
+
import { StringHelper, ComponentFactory, Is, GeneralError, BaseError } from '@twin.org/core';
|
|
7
6
|
import { HttpStatusCode, HttpMethod, HeaderTypes } from '@twin.org/web';
|
|
8
7
|
import Fastify from 'fastify';
|
|
9
8
|
import fp from 'fastify-plugin';
|
|
@@ -46,10 +45,15 @@ class FastifyWebServer {
|
|
|
46
45
|
*/
|
|
47
46
|
CLASS_NAME = "FastifyWebServer";
|
|
48
47
|
/**
|
|
49
|
-
* The logging
|
|
48
|
+
* The logging component type.
|
|
50
49
|
* @internal
|
|
51
50
|
*/
|
|
52
|
-
|
|
51
|
+
_loggingComponentType;
|
|
52
|
+
/**
|
|
53
|
+
* The logging component.
|
|
54
|
+
* @internal
|
|
55
|
+
*/
|
|
56
|
+
_loggingComponent;
|
|
53
57
|
/**
|
|
54
58
|
* The options for the server.
|
|
55
59
|
* @internal
|
|
@@ -85,8 +89,9 @@ class FastifyWebServer {
|
|
|
85
89
|
* @param options The options for the server.
|
|
86
90
|
*/
|
|
87
91
|
constructor(options) {
|
|
88
|
-
this.
|
|
89
|
-
|
|
92
|
+
this._loggingComponentType = options?.loggingComponentType;
|
|
93
|
+
this._loggingComponent = Is.stringValue(options?.loggingComponentType)
|
|
94
|
+
? ComponentFactory.get(options.loggingComponentType)
|
|
90
95
|
: undefined;
|
|
91
96
|
this._fastify = Fastify({
|
|
92
97
|
maxParamLength: 2000,
|
|
@@ -127,7 +132,7 @@ class FastifyWebServer {
|
|
|
127
132
|
if (Is.arrayValue(socketRoutes) && !Is.arrayValue(socketRouteProcessors)) {
|
|
128
133
|
throw new GeneralError(this.CLASS_NAME, "noSocketProcessors");
|
|
129
134
|
}
|
|
130
|
-
await this.
|
|
135
|
+
await this._loggingComponent?.log({
|
|
131
136
|
level: "info",
|
|
132
137
|
ts: Date.now(),
|
|
133
138
|
source: this.CLASS_NAME,
|
|
@@ -172,7 +177,7 @@ class FastifyWebServer {
|
|
|
172
177
|
err = errorAndCode.error;
|
|
173
178
|
httpStatusCode = errorAndCode.httpStatusCode;
|
|
174
179
|
}
|
|
175
|
-
await this.
|
|
180
|
+
await this._loggingComponent?.log({
|
|
176
181
|
level: "error",
|
|
177
182
|
ts: Date.now(),
|
|
178
183
|
source: this.CLASS_NAME,
|
|
@@ -193,7 +198,7 @@ class FastifyWebServer {
|
|
|
193
198
|
async start() {
|
|
194
199
|
const host = this._options?.host ?? FastifyWebServer._DEFAULT_HOST;
|
|
195
200
|
const port = this._options?.port ?? FastifyWebServer._DEFAULT_PORT;
|
|
196
|
-
await this.
|
|
201
|
+
await this._loggingComponent?.log({
|
|
197
202
|
level: "info",
|
|
198
203
|
ts: Date.now(),
|
|
199
204
|
source: this.CLASS_NAME,
|
|
@@ -208,7 +213,7 @@ class FastifyWebServer {
|
|
|
208
213
|
await this._fastify.listen({ port, host });
|
|
209
214
|
const addresses = this._fastify.addresses();
|
|
210
215
|
const protocol = Is.object(this._fastify.initialConfig.https) ? "https://" : "http://";
|
|
211
|
-
await this.
|
|
216
|
+
await this._loggingComponent?.log({
|
|
212
217
|
level: "info",
|
|
213
218
|
ts: Date.now(),
|
|
214
219
|
source: this.CLASS_NAME,
|
|
@@ -222,7 +227,7 @@ class FastifyWebServer {
|
|
|
222
227
|
this._started = true;
|
|
223
228
|
}
|
|
224
229
|
catch (err) {
|
|
225
|
-
await this.
|
|
230
|
+
await this._loggingComponent?.log({
|
|
226
231
|
level: "error",
|
|
227
232
|
ts: Date.now(),
|
|
228
233
|
source: this.CLASS_NAME,
|
|
@@ -240,7 +245,7 @@ class FastifyWebServer {
|
|
|
240
245
|
if (this._started) {
|
|
241
246
|
this._started = false;
|
|
242
247
|
await this._fastify.close();
|
|
243
|
-
await this.
|
|
248
|
+
await this._loggingComponent?.log({
|
|
244
249
|
level: "info",
|
|
245
250
|
ts: Date.now(),
|
|
246
251
|
source: this.CLASS_NAME,
|
|
@@ -261,7 +266,7 @@ class FastifyWebServer {
|
|
|
261
266
|
if (!path.startsWith("/")) {
|
|
262
267
|
path = `/${path}`;
|
|
263
268
|
}
|
|
264
|
-
await this.
|
|
269
|
+
await this._loggingComponent?.log({
|
|
265
270
|
level: "info",
|
|
266
271
|
ts: Date.now(),
|
|
267
272
|
source: this.CLASS_NAME,
|
|
@@ -288,7 +293,7 @@ class FastifyWebServer {
|
|
|
288
293
|
const pathParts = path.split("/");
|
|
289
294
|
const namespace = `/${pathParts[0]}`;
|
|
290
295
|
const topic = pathParts.slice(1).join("/");
|
|
291
|
-
await this.
|
|
296
|
+
await this._loggingComponent?.log({
|
|
292
297
|
level: "info",
|
|
293
298
|
ts: Date.now(),
|
|
294
299
|
source: this.CLASS_NAME,
|
|
@@ -301,20 +306,18 @@ class FastifyWebServer {
|
|
|
301
306
|
});
|
|
302
307
|
const socketNamespace = io.of(namespace);
|
|
303
308
|
socketNamespace.on("connection", async (socket) => {
|
|
304
|
-
const
|
|
309
|
+
const socketServerRequest = {
|
|
305
310
|
method: HttpMethod.GET,
|
|
306
311
|
url: socket.handshake.url,
|
|
307
312
|
query: socket.handshake.query,
|
|
308
|
-
headers: socket.handshake.headers
|
|
313
|
+
headers: socket.handshake.headers,
|
|
314
|
+
socketId: socket.id
|
|
309
315
|
};
|
|
310
316
|
// Pass the connected information on to any processors
|
|
311
317
|
try {
|
|
312
|
-
const processorState = {
|
|
313
|
-
socketId: socket.id
|
|
314
|
-
};
|
|
315
318
|
for (const socketRouteProcessor of socketRouteProcessors) {
|
|
316
|
-
if (
|
|
317
|
-
await socketRouteProcessor.connected(
|
|
319
|
+
if (socketRouteProcessor.connected) {
|
|
320
|
+
await socketRouteProcessor.connected(socketServerRequest, socketRoute, this._loggingComponentType);
|
|
318
321
|
}
|
|
319
322
|
}
|
|
320
323
|
}
|
|
@@ -326,13 +329,10 @@ class FastifyWebServer {
|
|
|
326
329
|
}
|
|
327
330
|
socket.on("disconnect", async () => {
|
|
328
331
|
try {
|
|
329
|
-
const processorState = {
|
|
330
|
-
socketId: socket.id
|
|
331
|
-
};
|
|
332
332
|
// The socket disconnected so notify any processors
|
|
333
333
|
for (const socketRouteProcessor of socketRouteProcessors) {
|
|
334
|
-
if (
|
|
335
|
-
await socketRouteProcessor.disconnected(
|
|
334
|
+
if (socketRouteProcessor.disconnected) {
|
|
335
|
+
await socketRouteProcessor.disconnected(socketServerRequest, socketRoute, this._loggingComponentType);
|
|
336
336
|
}
|
|
337
337
|
}
|
|
338
338
|
}
|
|
@@ -390,18 +390,18 @@ class FastifyWebServer {
|
|
|
390
390
|
async runProcessorsRest(restRouteProcessors, restRoute, httpServerRequest, httpResponse, httpRequestIdentity, processorState) {
|
|
391
391
|
try {
|
|
392
392
|
for (const routeProcessor of restRouteProcessors) {
|
|
393
|
-
if (
|
|
394
|
-
await routeProcessor.pre(httpServerRequest, httpResponse, restRoute, httpRequestIdentity, processorState);
|
|
393
|
+
if (routeProcessor.pre) {
|
|
394
|
+
await routeProcessor.pre(httpServerRequest, httpResponse, restRoute, httpRequestIdentity, processorState, this._loggingComponentType);
|
|
395
395
|
}
|
|
396
396
|
}
|
|
397
397
|
for (const routeProcessor of restRouteProcessors) {
|
|
398
|
-
if (
|
|
399
|
-
await routeProcessor.process(httpServerRequest, httpResponse, restRoute, httpRequestIdentity, processorState);
|
|
398
|
+
if (routeProcessor.process) {
|
|
399
|
+
await routeProcessor.process(httpServerRequest, httpResponse, restRoute, httpRequestIdentity, processorState, this._loggingComponentType);
|
|
400
400
|
}
|
|
401
401
|
}
|
|
402
402
|
for (const routeProcessor of restRouteProcessors) {
|
|
403
|
-
if (
|
|
404
|
-
await routeProcessor.post(httpServerRequest, httpResponse, restRoute, httpRequestIdentity, processorState);
|
|
403
|
+
if (routeProcessor.post) {
|
|
404
|
+
await routeProcessor.post(httpServerRequest, httpResponse, restRoute, httpRequestIdentity, processorState, this._loggingComponentType);
|
|
405
405
|
}
|
|
406
406
|
}
|
|
407
407
|
}
|
|
@@ -421,49 +421,49 @@ class FastifyWebServer {
|
|
|
421
421
|
* @internal
|
|
422
422
|
*/
|
|
423
423
|
async handleRequestSocket(socketRouteProcessors, socketRoute, socket, fullPath, emitTopic, request) {
|
|
424
|
-
const
|
|
424
|
+
const socketServerRequest = {
|
|
425
425
|
method: HttpMethod.GET,
|
|
426
426
|
url: fullPath,
|
|
427
427
|
query: socket.handshake.query,
|
|
428
428
|
headers: socket.handshake.headers,
|
|
429
|
-
body: request.body
|
|
429
|
+
body: request.body,
|
|
430
|
+
socketId: socket.id
|
|
430
431
|
};
|
|
431
432
|
const httpResponse = {};
|
|
432
433
|
const httpRequestIdentity = {};
|
|
433
|
-
const processorState = {
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
delete httpServerRequest.query?.transport;
|
|
438
|
-
await this.runProcessorsSocket(socketRouteProcessors, socketRoute, httpServerRequest, httpResponse, httpRequestIdentity, processorState, emitTopic, async (topic, response) => {
|
|
434
|
+
const processorState = {};
|
|
435
|
+
delete socketServerRequest.query?.EIO;
|
|
436
|
+
delete socketServerRequest.query?.transport;
|
|
437
|
+
await this.runProcessorsSocket(socketRouteProcessors, socketRoute, socketServerRequest, httpResponse, httpRequestIdentity, processorState, emitTopic, async (topic, response) => {
|
|
439
438
|
await socket.emit(topic, response);
|
|
440
439
|
});
|
|
441
440
|
}
|
|
442
441
|
/**
|
|
443
442
|
* Run the socket processors for the route.
|
|
443
|
+
* @param socketId The id of the socket.
|
|
444
444
|
* @param socketRouteProcessors The processors to run.
|
|
445
445
|
* @param socketRoute The route to process.
|
|
446
|
-
* @param
|
|
446
|
+
* @param socketServerRequest The incoming request.
|
|
447
447
|
* @param httpResponse The outgoing response.
|
|
448
448
|
* @param httpRequestIdentity The identity context for the request.
|
|
449
449
|
* @param processorState The state handed through the processors.
|
|
450
450
|
* @param requestTopic The topic of the request.
|
|
451
451
|
* @internal
|
|
452
452
|
*/
|
|
453
|
-
async runProcessorsSocket(socketRouteProcessors, socketRoute,
|
|
453
|
+
async runProcessorsSocket(socketRouteProcessors, socketRoute, socketServerRequest, httpResponse, httpRequestIdentity, processorState, requestTopic, responseEmitter) {
|
|
454
454
|
// Custom emit method which will also call the post processors
|
|
455
455
|
const postProcessEmit = async (topic, response, responseProcessorState) => {
|
|
456
456
|
await responseEmitter(topic, response);
|
|
457
457
|
try {
|
|
458
458
|
// The post processors are called after the response has been emitted
|
|
459
459
|
for (const postSocketRouteProcessor of socketRouteProcessors) {
|
|
460
|
-
if (
|
|
461
|
-
await postSocketRouteProcessor.post(
|
|
460
|
+
if (postSocketRouteProcessor.post) {
|
|
461
|
+
await postSocketRouteProcessor.post(socketServerRequest, response, socketRoute, httpRequestIdentity, responseProcessorState, this._loggingComponentType);
|
|
462
462
|
}
|
|
463
463
|
}
|
|
464
464
|
}
|
|
465
465
|
catch (err) {
|
|
466
|
-
this.
|
|
466
|
+
this._loggingComponent?.log({
|
|
467
467
|
level: "error",
|
|
468
468
|
ts: Date.now(),
|
|
469
469
|
source: this.CLASS_NAME,
|
|
@@ -477,8 +477,8 @@ class FastifyWebServer {
|
|
|
477
477
|
};
|
|
478
478
|
try {
|
|
479
479
|
for (const socketRouteProcessor of socketRouteProcessors) {
|
|
480
|
-
if (
|
|
481
|
-
await socketRouteProcessor.pre(
|
|
480
|
+
if (socketRouteProcessor.pre) {
|
|
481
|
+
await socketRouteProcessor.pre(socketServerRequest, httpResponse, socketRoute, httpRequestIdentity, processorState, this._loggingComponentType);
|
|
482
482
|
}
|
|
483
483
|
}
|
|
484
484
|
// We always call all the processors regardless of any response set by a previous processor.
|
|
@@ -488,10 +488,10 @@ class FastifyWebServer {
|
|
|
488
488
|
await postProcessEmit(requestTopic, httpResponse, processorState);
|
|
489
489
|
}
|
|
490
490
|
for (const socketRouteProcessor of socketRouteProcessors) {
|
|
491
|
-
if (
|
|
492
|
-
await socketRouteProcessor.process(
|
|
491
|
+
if (socketRouteProcessor.process) {
|
|
492
|
+
await socketRouteProcessor.process(socketServerRequest, httpResponse, socketRoute, httpRequestIdentity, processorState, async (topic, processResponse) => {
|
|
493
493
|
await postProcessEmit(topic, processResponse, processorState);
|
|
494
|
-
});
|
|
494
|
+
}, this._loggingComponentType);
|
|
495
495
|
}
|
|
496
496
|
}
|
|
497
497
|
// If the processors set the status to any kind of error then we should emit this manually
|
|
@@ -5,9 +5,9 @@ import type { IFastifyWebServerConfig } from "./IFastifyWebServerConfig";
|
|
|
5
5
|
*/
|
|
6
6
|
export interface IFastifyWebServerConstructorOptions {
|
|
7
7
|
/**
|
|
8
|
-
* The type of the logging
|
|
8
|
+
* The type of the logging component to use, if undefined, no logging will happen.
|
|
9
9
|
*/
|
|
10
|
-
|
|
10
|
+
loggingComponentType?: string;
|
|
11
11
|
/**
|
|
12
12
|
* Additional configuration for the server.
|
|
13
13
|
*/
|
package/docs/changelog.md
CHANGED
|
@@ -1,5 +1,58 @@
|
|
|
1
1
|
# @twin.org/api-server-fastify - Changelog
|
|
2
2
|
|
|
3
|
+
## [0.0.2-next.5](https://github.com/twinfoundation/api/compare/api-server-fastify-v0.0.2-next.4...api-server-fastify-v0.0.2-next.5) (2025-07-25)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Features
|
|
7
|
+
|
|
8
|
+
* add json-ld mime type processor and auth admin component ([8861791](https://github.com/twinfoundation/api/commit/88617916e23bfbca023dbae1976fe421983a02ff))
|
|
9
|
+
* add logging component type to request contexts ([210de1b](https://github.com/twinfoundation/api/commit/210de1b9e1c91079b59a2b90ddd57569668d647d))
|
|
10
|
+
* add socket id, connect and disconnect ([20b0d0e](https://github.com/twinfoundation/api/commit/20b0d0ec279cab46141fee09de2c4a7087cdce16))
|
|
11
|
+
* improve socket route logging ([b8d9519](https://github.com/twinfoundation/api/commit/b8d95199f838ac6ba9f45c30ef7c4e613201ff53))
|
|
12
|
+
* update dependencies ([1171dc4](https://github.com/twinfoundation/api/commit/1171dc416a9481737f6a640e3cf30145768f37e9))
|
|
13
|
+
* use shared store mechanism ([#19](https://github.com/twinfoundation/api/issues/19)) ([32116df](https://github.com/twinfoundation/api/commit/32116df3b4380a30137f5056f242a5c99afa2df9))
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
### Dependencies
|
|
17
|
+
|
|
18
|
+
* The following workspace dependencies were updated
|
|
19
|
+
* dependencies
|
|
20
|
+
* @twin.org/api-core bumped from 0.0.2-next.4 to 0.0.2-next.5
|
|
21
|
+
* @twin.org/api-models bumped from 0.0.2-next.4 to 0.0.2-next.5
|
|
22
|
+
* @twin.org/api-processors bumped from 0.0.2-next.4 to 0.0.2-next.5
|
|
23
|
+
|
|
24
|
+
## [0.0.2-next.4](https://github.com/twinfoundation/api/compare/api-server-fastify-v0.0.2-next.3...api-server-fastify-v0.0.2-next.4) (2025-07-25)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
### Features
|
|
28
|
+
|
|
29
|
+
* add logging component type to request contexts ([210de1b](https://github.com/twinfoundation/api/commit/210de1b9e1c91079b59a2b90ddd57569668d647d))
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
### Dependencies
|
|
33
|
+
|
|
34
|
+
* The following workspace dependencies were updated
|
|
35
|
+
* dependencies
|
|
36
|
+
* @twin.org/api-core bumped from 0.0.2-next.3 to 0.0.2-next.4
|
|
37
|
+
* @twin.org/api-models bumped from 0.0.2-next.3 to 0.0.2-next.4
|
|
38
|
+
* @twin.org/api-processors bumped from 0.0.2-next.3 to 0.0.2-next.4
|
|
39
|
+
|
|
40
|
+
## [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)
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
### Features
|
|
44
|
+
|
|
45
|
+
* add socket id, connect and disconnect ([20b0d0e](https://github.com/twinfoundation/api/commit/20b0d0ec279cab46141fee09de2c4a7087cdce16))
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
### Dependencies
|
|
49
|
+
|
|
50
|
+
* The following workspace dependencies were updated
|
|
51
|
+
* dependencies
|
|
52
|
+
* @twin.org/api-core bumped from 0.0.2-next.2 to 0.0.2-next.3
|
|
53
|
+
* @twin.org/api-models bumped from 0.0.2-next.2 to 0.0.2-next.3
|
|
54
|
+
* @twin.org/api-processors bumped from 0.0.2-next.2 to 0.0.2-next.3
|
|
55
|
+
|
|
3
56
|
## [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)
|
|
4
57
|
|
|
5
58
|
|
|
@@ -4,11 +4,11 @@ The options for the Fastify web server constructor.
|
|
|
4
4
|
|
|
5
5
|
## Properties
|
|
6
6
|
|
|
7
|
-
###
|
|
7
|
+
### loggingComponentType?
|
|
8
8
|
|
|
9
|
-
> `optional` **
|
|
9
|
+
> `optional` **loggingComponentType**: `string`
|
|
10
10
|
|
|
11
|
-
The type of the logging
|
|
11
|
+
The type of the logging component to use, if undefined, no logging will happen.
|
|
12
12
|
|
|
13
13
|
***
|
|
14
14
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@twin.org/api-server-fastify",
|
|
3
|
-
"version": "0.0.2-next.
|
|
3
|
+
"version": "0.0.2-next.5",
|
|
4
4
|
"description": "Use Fastify as the core web server for APIs",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -16,9 +16,9 @@
|
|
|
16
16
|
"dependencies": {
|
|
17
17
|
"@fastify/compress": "8.1.0",
|
|
18
18
|
"@fastify/cors": "11.0.1",
|
|
19
|
-
"@twin.org/api-core": "0.0.2-next.
|
|
20
|
-
"@twin.org/api-models": "0.0.2-next.
|
|
21
|
-
"@twin.org/api-processors": "0.0.2-next.
|
|
19
|
+
"@twin.org/api-core": "0.0.2-next.5",
|
|
20
|
+
"@twin.org/api-models": "0.0.2-next.5",
|
|
21
|
+
"@twin.org/api-processors": "0.0.2-next.5",
|
|
22
22
|
"@twin.org/core": "next",
|
|
23
23
|
"@twin.org/logging-models": "next",
|
|
24
24
|
"@twin.org/nameof": "next",
|