@twin.org/api-processors 0.0.1-next.9 → 0.0.2-next.1
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 +246 -30
- package/dist/esm/index.mjs +244 -31
- package/dist/types/data/{routeProcessor.d.ts → restRouteProcessor.d.ts} +8 -8
- package/dist/types/data/socketRouteProcessor.d.ts +32 -0
- package/dist/types/identity/nodeIdentityProcessor.d.ts +7 -3
- package/dist/types/identity/staticUserIdentityProcessor.d.ts +9 -9
- package/dist/types/index.d.ts +10 -4
- package/dist/types/logging/loggingProcessor.d.ts +11 -13
- package/dist/types/mimeType/jsonLdMimeTypeProcessor.d.ts +25 -0
- package/dist/types/mimeType/jwtMimeTypeProcessor.d.ts +25 -0
- package/dist/types/models/ILoggingProcessorConfig.d.ts +17 -0
- package/dist/types/models/ILoggingProcessorConstructorOptions.d.ts +15 -0
- package/dist/types/models/IRestRouteProcessorConstructorOptions.d.ts +10 -0
- package/dist/types/models/ISocketRouteProcessorConstructorOptions.d.ts +10 -0
- package/dist/types/models/IStaticUserIdentityProcessorConstructorOptions.d.ts +10 -0
- package/docs/changelog.md +101 -1
- package/docs/reference/classes/JsonLdMimeTypeProcessor.md +81 -0
- package/docs/reference/classes/JwtMimeTypeProcessor.md +81 -0
- package/docs/reference/classes/LoggingProcessor.md +43 -27
- package/docs/reference/classes/NodeIdentityProcessor.md +35 -15
- package/docs/reference/classes/{RouteProcessor.md → RestRouteProcessor.md} +30 -18
- package/docs/reference/classes/SocketRouteProcessor.md +99 -0
- package/docs/reference/classes/StaticUserIdentityProcessor.md +29 -17
- package/docs/reference/index.md +9 -3
- package/docs/reference/interfaces/ILoggingProcessorConfig.md +27 -0
- package/docs/reference/interfaces/ILoggingProcessorConstructorOptions.md +23 -0
- package/docs/reference/interfaces/IRestRouteProcessorConstructorOptions.md +11 -0
- package/docs/reference/interfaces/ISocketRouteProcessorConstructorOptions.md +11 -0
- package/docs/reference/interfaces/IStaticUserIdentityProcessorConstructorOptions.md +11 -0
- package/locales/en.json +8 -2
- package/package.json +4 -6
- package/dist/types/models/IRequestLoggingProcessorConfig.d.ts +0 -9
- package/dist/types/models/IResponseLoggingProcessorConfig.d.ts +0 -9
- package/docs/reference/interfaces/IRequestLoggingProcessorConfig.md +0 -11
- package/docs/reference/interfaces/IResponseLoggingProcessorConfig.md +0 -11
package/dist/esm/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { HttpErrorHelper } from '@twin.org/api-models';
|
|
2
|
-
import { Is, NotFoundError, Guards, Coerce } from '@twin.org/core';
|
|
2
|
+
import { Is, NotFoundError, Guards, ObjectHelper, Coerce, GeneralError, Converter } from '@twin.org/core';
|
|
3
3
|
import { HttpStatusCode, HeaderTypes, MimeTypes } from '@twin.org/web';
|
|
4
4
|
import { LoggingConnectorFactory } from '@twin.org/logging-models';
|
|
5
5
|
|
|
@@ -8,11 +8,15 @@ import { LoggingConnectorFactory } from '@twin.org/logging-models';
|
|
|
8
8
|
/**
|
|
9
9
|
* Process the REST request and hands it on to the route handler.
|
|
10
10
|
*/
|
|
11
|
-
class
|
|
11
|
+
class RestRouteProcessor {
|
|
12
|
+
/**
|
|
13
|
+
* The namespace supported by the processor.
|
|
14
|
+
*/
|
|
15
|
+
static NAMESPACE = "rest-route";
|
|
12
16
|
/**
|
|
13
17
|
* Runtime name for the class.
|
|
14
18
|
*/
|
|
15
|
-
CLASS_NAME = "
|
|
19
|
+
CLASS_NAME = "RestRouteProcessor";
|
|
16
20
|
/**
|
|
17
21
|
* Include the stack with errors.
|
|
18
22
|
* @internal
|
|
@@ -21,8 +25,6 @@ class RouteProcessor {
|
|
|
21
25
|
/**
|
|
22
26
|
* Create a new instance of RouteProcessor.
|
|
23
27
|
* @param options Options for the processor.
|
|
24
|
-
* @param options.config The configuration for the processor.
|
|
25
|
-
* @returns Promise that resolves when the processor is initialized.
|
|
26
28
|
*/
|
|
27
29
|
constructor(options) {
|
|
28
30
|
this._includeErrorStack = options?.config?.includeErrorStack ?? false;
|
|
@@ -110,42 +112,78 @@ class RouteProcessor {
|
|
|
110
112
|
}
|
|
111
113
|
}
|
|
112
114
|
|
|
115
|
+
// Copyright 2024 IOTA Stiftung.
|
|
116
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
113
117
|
/**
|
|
114
|
-
*
|
|
118
|
+
* Process the socket request and hands it on to the route handler.
|
|
115
119
|
*/
|
|
116
|
-
class
|
|
120
|
+
class SocketRouteProcessor {
|
|
121
|
+
/**
|
|
122
|
+
* The namespace supported by the processor.
|
|
123
|
+
*/
|
|
124
|
+
static NAMESPACE = "socket-route";
|
|
117
125
|
/**
|
|
118
126
|
* Runtime name for the class.
|
|
119
127
|
*/
|
|
120
|
-
CLASS_NAME = "
|
|
128
|
+
CLASS_NAME = "SocketRouteProcessor";
|
|
121
129
|
/**
|
|
122
|
-
*
|
|
130
|
+
* Include the stack with errors.
|
|
123
131
|
* @internal
|
|
124
132
|
*/
|
|
125
|
-
|
|
133
|
+
_includeErrorStack;
|
|
126
134
|
/**
|
|
127
|
-
* Create a new instance of
|
|
135
|
+
* Create a new instance of SocketRouteProcessor.
|
|
128
136
|
* @param options Options for the processor.
|
|
129
|
-
* @param options.config The configuration for the processor.
|
|
130
|
-
* @returns Promise that resolves when the processor is initialized.
|
|
131
137
|
*/
|
|
132
138
|
constructor(options) {
|
|
133
|
-
|
|
134
|
-
Guards.object(this.CLASS_NAME, "options.config", options.config);
|
|
135
|
-
Guards.stringValue(this.CLASS_NAME, "options.config.userIdentity", options.config.userIdentity);
|
|
136
|
-
this._userIdentity = options.config.userIdentity;
|
|
139
|
+
this._includeErrorStack = options?.config?.includeErrorStack ?? false;
|
|
137
140
|
}
|
|
138
141
|
/**
|
|
139
|
-
*
|
|
142
|
+
* Process the REST request for the specified route.
|
|
140
143
|
* @param request The incoming request.
|
|
141
144
|
* @param response The outgoing response.
|
|
142
145
|
* @param route The route to process.
|
|
143
146
|
* @param requestIdentity The identity context for the request.
|
|
144
147
|
* @param processorState The state handed through the processors.
|
|
148
|
+
* @param responseEmitter The function to emit a response.
|
|
145
149
|
*/
|
|
146
|
-
async
|
|
147
|
-
|
|
148
|
-
|
|
150
|
+
async process(request, response, route, requestIdentity, processorState, responseEmitter) {
|
|
151
|
+
// Don't handle the route if another processor has already set the response
|
|
152
|
+
// status code e.g. from an auth processor
|
|
153
|
+
if (Is.empty(response.statusCode)) {
|
|
154
|
+
if (Is.empty(route)) {
|
|
155
|
+
HttpErrorHelper.buildResponse(response, {
|
|
156
|
+
name: NotFoundError.CLASS_NAME,
|
|
157
|
+
message: `${this.CLASS_NAME}.routeNotFound`,
|
|
158
|
+
properties: {
|
|
159
|
+
notFoundId: request.url
|
|
160
|
+
}
|
|
161
|
+
}, HttpStatusCode.notFound);
|
|
162
|
+
}
|
|
163
|
+
else {
|
|
164
|
+
try {
|
|
165
|
+
const req = {
|
|
166
|
+
pathParams: request.pathParams,
|
|
167
|
+
query: request.query,
|
|
168
|
+
body: request.body
|
|
169
|
+
};
|
|
170
|
+
await route.handler({
|
|
171
|
+
...requestIdentity,
|
|
172
|
+
serverRequest: request,
|
|
173
|
+
processorState
|
|
174
|
+
}, req, async (topic, restRouteResponse) => {
|
|
175
|
+
response.headers = restRouteResponse?.headers;
|
|
176
|
+
response.body = restRouteResponse?.body;
|
|
177
|
+
response.statusCode =
|
|
178
|
+
restRouteResponse.statusCode ?? response.statusCode ?? HttpStatusCode.ok;
|
|
179
|
+
await responseEmitter(topic, response);
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
catch (err) {
|
|
183
|
+
const { error, httpStatusCode } = HttpErrorHelper.processError(err, this._includeErrorStack);
|
|
184
|
+
HttpErrorHelper.buildResponse(response, error, httpStatusCode);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
149
187
|
}
|
|
150
188
|
}
|
|
151
189
|
}
|
|
@@ -154,6 +192,10 @@ class StaticUserIdentityProcessor {
|
|
|
154
192
|
* Adds a node identity to the request identity.
|
|
155
193
|
*/
|
|
156
194
|
class NodeIdentityProcessor {
|
|
195
|
+
/**
|
|
196
|
+
* The namespace supported by the processor.
|
|
197
|
+
*/
|
|
198
|
+
static NAMESPACE = "node-identity";
|
|
157
199
|
/**
|
|
158
200
|
* Runtime name for the class.
|
|
159
201
|
*/
|
|
@@ -186,10 +228,56 @@ class NodeIdentityProcessor {
|
|
|
186
228
|
}
|
|
187
229
|
}
|
|
188
230
|
|
|
231
|
+
/**
|
|
232
|
+
* Adds a static user identity to the request context.
|
|
233
|
+
*/
|
|
234
|
+
class StaticUserIdentityProcessor {
|
|
235
|
+
/**
|
|
236
|
+
* The namespace supported by the processor.
|
|
237
|
+
*/
|
|
238
|
+
static NAMESPACE = "static-user-identity";
|
|
239
|
+
/**
|
|
240
|
+
* Runtime name for the class.
|
|
241
|
+
*/
|
|
242
|
+
CLASS_NAME = "StaticUserIdentityProcessor";
|
|
243
|
+
/**
|
|
244
|
+
* The fixed identity for request context.
|
|
245
|
+
* @internal
|
|
246
|
+
*/
|
|
247
|
+
_userIdentity;
|
|
248
|
+
/**
|
|
249
|
+
* Create a new instance of StaticIdentityProcessor.
|
|
250
|
+
* @param options Options for the processor.
|
|
251
|
+
*/
|
|
252
|
+
constructor(options) {
|
|
253
|
+
Guards.object(this.CLASS_NAME, "options", options);
|
|
254
|
+
Guards.object(this.CLASS_NAME, "options.config", options.config);
|
|
255
|
+
Guards.stringValue(this.CLASS_NAME, "options.config.userIdentity", options.config.userIdentity);
|
|
256
|
+
this._userIdentity = options.config.userIdentity;
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* Pre process the REST request for the specified route.
|
|
260
|
+
* @param request The incoming request.
|
|
261
|
+
* @param response The outgoing response.
|
|
262
|
+
* @param route The route to process.
|
|
263
|
+
* @param requestIdentity The identity context for the request.
|
|
264
|
+
* @param processorState The state handed through the processors.
|
|
265
|
+
*/
|
|
266
|
+
async pre(request, response, route, requestIdentity, processorState) {
|
|
267
|
+
if (!Is.empty(route) && !(route.skipAuth ?? false)) {
|
|
268
|
+
requestIdentity.userIdentity = this._userIdentity;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
189
273
|
/**
|
|
190
274
|
* Process the REST request and log its information.
|
|
191
275
|
*/
|
|
192
276
|
class LoggingProcessor {
|
|
277
|
+
/**
|
|
278
|
+
* The namespace supported by the processor.
|
|
279
|
+
*/
|
|
280
|
+
static NAMESPACE = "logging";
|
|
193
281
|
/**
|
|
194
282
|
* Runtime name for the class.
|
|
195
283
|
*/
|
|
@@ -205,15 +293,24 @@ class LoggingProcessor {
|
|
|
205
293
|
*/
|
|
206
294
|
_includeBody;
|
|
207
295
|
/**
|
|
208
|
-
*
|
|
296
|
+
* Show the full base64 content for data, default to abbreviate.
|
|
297
|
+
* @internal
|
|
298
|
+
*/
|
|
299
|
+
_fullBase64;
|
|
300
|
+
/**
|
|
301
|
+
* List of property names to obfuscate, defaults to "password".
|
|
302
|
+
* @internal
|
|
303
|
+
*/
|
|
304
|
+
_obfuscateProperties;
|
|
305
|
+
/**
|
|
306
|
+
* Create a new instance of LoggingProcessor.
|
|
209
307
|
* @param options Options for the processor.
|
|
210
|
-
* @param options.loggingConnectorType The type for the logging connector, defaults to "logging".
|
|
211
|
-
* @param options.config The configuration for the processor.
|
|
212
|
-
* @returns Promise that resolves when the processor is initialized.
|
|
213
308
|
*/
|
|
214
309
|
constructor(options) {
|
|
215
310
|
this._loggingConnector = LoggingConnectorFactory.get(options?.loggingConnectorType ?? "logging");
|
|
216
311
|
this._includeBody = options?.config?.includeBody ?? false;
|
|
312
|
+
this._fullBase64 = options?.config?.fullBase64 ?? false;
|
|
313
|
+
this._obfuscateProperties = options?.config?.obfuscateProperties ?? ["password"];
|
|
217
314
|
}
|
|
218
315
|
/**
|
|
219
316
|
* Pre process the REST request for the specified route.
|
|
@@ -226,12 +323,28 @@ class LoggingProcessor {
|
|
|
226
323
|
async pre(request, response, route, requestIdentity, processorState) {
|
|
227
324
|
const now = process.hrtime.bigint();
|
|
228
325
|
processorState.requestStart = now;
|
|
326
|
+
const contentType = request.headers?.[HeaderTypes.ContentType];
|
|
327
|
+
const isJson = Is.stringValue(contentType)
|
|
328
|
+
? contentType.includes(MimeTypes.Json) || contentType.includes(MimeTypes.JsonLd)
|
|
329
|
+
: false;
|
|
330
|
+
let requestUrl = "";
|
|
331
|
+
if (Is.stringValue(request.url)) {
|
|
332
|
+
// Socket paths do not have a prefix so just use the whole url.
|
|
333
|
+
if (request.url.startsWith("http")) {
|
|
334
|
+
requestUrl = new URL(request.url).pathname;
|
|
335
|
+
}
|
|
336
|
+
else {
|
|
337
|
+
requestUrl = request.url;
|
|
338
|
+
}
|
|
339
|
+
}
|
|
229
340
|
await this._loggingConnector.log({
|
|
230
341
|
level: "info",
|
|
231
342
|
source: this.CLASS_NAME,
|
|
232
343
|
ts: Date.now(),
|
|
233
|
-
message: `===> ${request.method} ${
|
|
234
|
-
data: this._includeBody
|
|
344
|
+
message: `===> ${request.method} ${requestUrl}`,
|
|
345
|
+
data: this._includeBody && isJson
|
|
346
|
+
? this.processJson("body", ObjectHelper.clone(request?.body))
|
|
347
|
+
: undefined
|
|
235
348
|
});
|
|
236
349
|
}
|
|
237
350
|
/**
|
|
@@ -246,11 +359,13 @@ class LoggingProcessor {
|
|
|
246
359
|
let data;
|
|
247
360
|
if (this._includeBody) {
|
|
248
361
|
const contentType = response.headers?.[HeaderTypes.ContentType];
|
|
249
|
-
const isJson = contentType
|
|
362
|
+
const isJson = Is.stringValue(contentType)
|
|
363
|
+
? contentType.includes(MimeTypes.Json) || contentType.includes(MimeTypes.JsonLd)
|
|
364
|
+
: false;
|
|
250
365
|
const contentLength = response.headers?.[HeaderTypes.ContentLength];
|
|
251
366
|
if (isJson) {
|
|
252
367
|
data = {
|
|
253
|
-
body: response.body
|
|
368
|
+
body: this.processJson("body", ObjectHelper.clone(response.body))
|
|
254
369
|
};
|
|
255
370
|
}
|
|
256
371
|
else {
|
|
@@ -272,16 +387,114 @@ class LoggingProcessor {
|
|
|
272
387
|
const start = Coerce.bigint(processorState.requestStart) ?? now;
|
|
273
388
|
const elapsed = now - start;
|
|
274
389
|
const elapsedMicroSeconds = Math.floor(Number(elapsed) / 1000);
|
|
390
|
+
let requestUrl = "";
|
|
391
|
+
if (Is.stringValue(request.url)) {
|
|
392
|
+
// Socket paths do not have a prefix so just use the whole url.
|
|
393
|
+
if (request.url.startsWith("http")) {
|
|
394
|
+
requestUrl = new URL(request.url).pathname;
|
|
395
|
+
}
|
|
396
|
+
else {
|
|
397
|
+
requestUrl = request.url;
|
|
398
|
+
}
|
|
399
|
+
}
|
|
275
400
|
await this._loggingConnector.log({
|
|
276
401
|
level: Is.number(response.statusCode) && response.statusCode >= HttpStatusCode.badRequest
|
|
277
402
|
? "error"
|
|
278
403
|
: "info",
|
|
279
404
|
source: this.CLASS_NAME,
|
|
280
405
|
ts: Date.now(),
|
|
281
|
-
message: `<=== ${response.statusCode ?? ""} ${request.method} ${
|
|
406
|
+
message: `<=== ${response.statusCode ?? ""} ${request.method} ${requestUrl} duration: ${elapsedMicroSeconds}µs`,
|
|
282
407
|
data
|
|
283
408
|
});
|
|
284
409
|
}
|
|
410
|
+
/**
|
|
411
|
+
* Process the JSON.
|
|
412
|
+
* @param propValue The property to process.
|
|
413
|
+
* @returns The processed property.
|
|
414
|
+
* @internal
|
|
415
|
+
*/
|
|
416
|
+
processJson(propName, propValue) {
|
|
417
|
+
if (Is.array(propValue)) {
|
|
418
|
+
for (let i = 0; i < propValue.length; i++) {
|
|
419
|
+
propValue[i] = this.processJson(`${i}`, propValue[i]);
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
else if (Is.object(propValue)) {
|
|
423
|
+
for (const key of Object.keys(propValue)) {
|
|
424
|
+
propValue[key] = this.processJson(key, propValue[key]);
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
else if (!this._fullBase64 && Is.stringBase64(propValue) && propValue.length > 256) {
|
|
428
|
+
propValue = "<base64>";
|
|
429
|
+
}
|
|
430
|
+
else if (Is.string(propValue) &&
|
|
431
|
+
this._obfuscateProperties.some(op => new RegExp(op).test(propName))) {
|
|
432
|
+
propValue = "**************";
|
|
433
|
+
}
|
|
434
|
+
return propValue;
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
/**
|
|
439
|
+
* Process the JSON-LD mime type.
|
|
440
|
+
*/
|
|
441
|
+
class JsonLdMimeTypeProcessor {
|
|
442
|
+
/**
|
|
443
|
+
* The namespace supported by the processor.
|
|
444
|
+
*/
|
|
445
|
+
static NAMESPACE = "json-ld";
|
|
446
|
+
/**
|
|
447
|
+
* Runtime name for the class.
|
|
448
|
+
*/
|
|
449
|
+
CLASS_NAME = "JsonLdMimeTypeProcessor";
|
|
450
|
+
/**
|
|
451
|
+
* Get the MIME types that this handler can handle.
|
|
452
|
+
* @returns The MIME types that this handler can handle.
|
|
453
|
+
*/
|
|
454
|
+
getTypes() {
|
|
455
|
+
return [MimeTypes.JsonLd];
|
|
456
|
+
}
|
|
457
|
+
/**
|
|
458
|
+
* Handle content.
|
|
459
|
+
* @param body The body to process.
|
|
460
|
+
* @returns The processed body.
|
|
461
|
+
*/
|
|
462
|
+
async handle(body) {
|
|
463
|
+
const json = ObjectHelper.fromBytes(body);
|
|
464
|
+
if (Is.empty(json) || Is.empty(json["@context"])) {
|
|
465
|
+
throw new GeneralError(this.CLASS_NAME, "invalidJsonLd");
|
|
466
|
+
}
|
|
467
|
+
return json;
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
/**
|
|
472
|
+
* Process the JWT mime type.
|
|
473
|
+
*/
|
|
474
|
+
class JwtMimeTypeProcessor {
|
|
475
|
+
/**
|
|
476
|
+
* The namespace supported by the processor.
|
|
477
|
+
*/
|
|
478
|
+
static NAMESPACE = "jwt";
|
|
479
|
+
/**
|
|
480
|
+
* Runtime name for the class.
|
|
481
|
+
*/
|
|
482
|
+
CLASS_NAME = "JwtMimeTypeProcessor";
|
|
483
|
+
/**
|
|
484
|
+
* Get the MIME types that this handler can handle.
|
|
485
|
+
* @returns The MIME types that this handler can handle.
|
|
486
|
+
*/
|
|
487
|
+
getTypes() {
|
|
488
|
+
return [MimeTypes.Jwt];
|
|
489
|
+
}
|
|
490
|
+
/**
|
|
491
|
+
* Handle content.
|
|
492
|
+
* @param body The body to process.
|
|
493
|
+
* @returns The processed body.
|
|
494
|
+
*/
|
|
495
|
+
async handle(body) {
|
|
496
|
+
return Converter.bytesToUtf8(body);
|
|
497
|
+
}
|
|
285
498
|
}
|
|
286
499
|
|
|
287
|
-
export { LoggingProcessor, NodeIdentityProcessor,
|
|
500
|
+
export { JsonLdMimeTypeProcessor, JwtMimeTypeProcessor, LoggingProcessor, NodeIdentityProcessor, RestRouteProcessor, SocketRouteProcessor, StaticUserIdentityProcessor };
|
|
@@ -1,9 +1,13 @@
|
|
|
1
|
-
import { type IHttpRequestIdentity, type IHttpResponse, type
|
|
2
|
-
import type {
|
|
1
|
+
import { type IHttpRequestIdentity, type IHttpResponse, type IRestRouteProcessor, type IHttpServerRequest, type IRestRoute } from "@twin.org/api-models";
|
|
2
|
+
import type { IRestRouteProcessorConstructorOptions } from "../models/IRestRouteProcessorConstructorOptions";
|
|
3
3
|
/**
|
|
4
4
|
* Process the REST request and hands it on to the route handler.
|
|
5
5
|
*/
|
|
6
|
-
export declare class
|
|
6
|
+
export declare class RestRouteProcessor implements IRestRouteProcessor {
|
|
7
|
+
/**
|
|
8
|
+
* The namespace supported by the processor.
|
|
9
|
+
*/
|
|
10
|
+
static readonly NAMESPACE: string;
|
|
7
11
|
/**
|
|
8
12
|
* Runtime name for the class.
|
|
9
13
|
*/
|
|
@@ -11,12 +15,8 @@ export declare class RouteProcessor implements IHttpRestRouteProcessor {
|
|
|
11
15
|
/**
|
|
12
16
|
* Create a new instance of RouteProcessor.
|
|
13
17
|
* @param options Options for the processor.
|
|
14
|
-
* @param options.config The configuration for the processor.
|
|
15
|
-
* @returns Promise that resolves when the processor is initialized.
|
|
16
18
|
*/
|
|
17
|
-
constructor(options?:
|
|
18
|
-
config?: IRouteProcessorConfig;
|
|
19
|
-
});
|
|
19
|
+
constructor(options?: IRestRouteProcessorConstructorOptions);
|
|
20
20
|
/**
|
|
21
21
|
* Process the REST request for the specified route.
|
|
22
22
|
* @param request The incoming request.
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { type IHttpRequestIdentity, type IHttpResponse, type IHttpServerRequest, type ISocketRoute, type ISocketRouteProcessor } from "@twin.org/api-models";
|
|
2
|
+
import type { ISocketRouteProcessorConstructorOptions } from "../models/ISocketRouteProcessorConstructorOptions";
|
|
3
|
+
/**
|
|
4
|
+
* Process the socket request and hands it on to the route handler.
|
|
5
|
+
*/
|
|
6
|
+
export declare class SocketRouteProcessor implements ISocketRouteProcessor {
|
|
7
|
+
/**
|
|
8
|
+
* The namespace supported by the processor.
|
|
9
|
+
*/
|
|
10
|
+
static readonly NAMESPACE: string;
|
|
11
|
+
/**
|
|
12
|
+
* Runtime name for the class.
|
|
13
|
+
*/
|
|
14
|
+
readonly CLASS_NAME: string;
|
|
15
|
+
/**
|
|
16
|
+
* Create a new instance of SocketRouteProcessor.
|
|
17
|
+
* @param options Options for the processor.
|
|
18
|
+
*/
|
|
19
|
+
constructor(options?: ISocketRouteProcessorConstructorOptions);
|
|
20
|
+
/**
|
|
21
|
+
* Process the REST request for the specified route.
|
|
22
|
+
* @param request The incoming request.
|
|
23
|
+
* @param response The outgoing response.
|
|
24
|
+
* @param route The route to process.
|
|
25
|
+
* @param requestIdentity The identity context for the request.
|
|
26
|
+
* @param processorState The state handed through the processors.
|
|
27
|
+
* @param responseEmitter The function to emit a response.
|
|
28
|
+
*/
|
|
29
|
+
process(request: IHttpServerRequest, response: IHttpResponse, route: ISocketRoute | undefined, requestIdentity: IHttpRequestIdentity, processorState: {
|
|
30
|
+
[id: string]: unknown;
|
|
31
|
+
}, responseEmitter: (topic: string, response: IHttpResponse) => Promise<void>): Promise<void>;
|
|
32
|
+
}
|
|
@@ -1,8 +1,12 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { IBaseRoute, IBaseRouteProcessor, IHttpRequestIdentity, IHttpResponse, IHttpServerRequest } from "@twin.org/api-models";
|
|
2
2
|
/**
|
|
3
3
|
* Adds a node identity to the request identity.
|
|
4
4
|
*/
|
|
5
|
-
export declare class NodeIdentityProcessor implements
|
|
5
|
+
export declare class NodeIdentityProcessor implements IBaseRouteProcessor {
|
|
6
|
+
/**
|
|
7
|
+
* The namespace supported by the processor.
|
|
8
|
+
*/
|
|
9
|
+
static readonly NAMESPACE: string;
|
|
6
10
|
/**
|
|
7
11
|
* Runtime name for the class.
|
|
8
12
|
*/
|
|
@@ -22,7 +26,7 @@ export declare class NodeIdentityProcessor implements IHttpRestRouteProcessor {
|
|
|
22
26
|
* @param requestIdentity The identity context for the request.
|
|
23
27
|
* @param processorState The state handed through the processors.
|
|
24
28
|
*/
|
|
25
|
-
pre(request: IHttpServerRequest, response: IHttpResponse, route:
|
|
29
|
+
pre(request: IHttpServerRequest, response: IHttpResponse, route: IBaseRoute | undefined, requestIdentity: IHttpRequestIdentity, processorState: {
|
|
26
30
|
[id: string]: unknown;
|
|
27
31
|
}): Promise<void>;
|
|
28
32
|
}
|
|
@@ -1,9 +1,13 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type {
|
|
1
|
+
import type { IBaseRoute, IBaseRouteProcessor, IHttpRequestIdentity, IHttpResponse, IHttpServerRequest } from "@twin.org/api-models";
|
|
2
|
+
import type { IStaticUserIdentityProcessorConstructorOptions } from "../models/IStaticUserIdentityProcessorConstructorOptions";
|
|
3
3
|
/**
|
|
4
4
|
* Adds a static user identity to the request context.
|
|
5
5
|
*/
|
|
6
|
-
export declare class StaticUserIdentityProcessor implements
|
|
6
|
+
export declare class StaticUserIdentityProcessor implements IBaseRouteProcessor {
|
|
7
|
+
/**
|
|
8
|
+
* The namespace supported by the processor.
|
|
9
|
+
*/
|
|
10
|
+
static readonly NAMESPACE: string;
|
|
7
11
|
/**
|
|
8
12
|
* Runtime name for the class.
|
|
9
13
|
*/
|
|
@@ -11,12 +15,8 @@ export declare class StaticUserIdentityProcessor implements IHttpRestRouteProces
|
|
|
11
15
|
/**
|
|
12
16
|
* Create a new instance of StaticIdentityProcessor.
|
|
13
17
|
* @param options Options for the processor.
|
|
14
|
-
* @param options.config The configuration for the processor.
|
|
15
|
-
* @returns Promise that resolves when the processor is initialized.
|
|
16
18
|
*/
|
|
17
|
-
constructor(options:
|
|
18
|
-
config: IStaticUserIdentityProcessorConfig;
|
|
19
|
-
});
|
|
19
|
+
constructor(options: IStaticUserIdentityProcessorConstructorOptions);
|
|
20
20
|
/**
|
|
21
21
|
* Pre process the REST request for the specified route.
|
|
22
22
|
* @param request The incoming request.
|
|
@@ -25,7 +25,7 @@ export declare class StaticUserIdentityProcessor implements IHttpRestRouteProces
|
|
|
25
25
|
* @param requestIdentity The identity context for the request.
|
|
26
26
|
* @param processorState The state handed through the processors.
|
|
27
27
|
*/
|
|
28
|
-
pre(request: IHttpServerRequest, response: IHttpResponse, route:
|
|
28
|
+
pre(request: IHttpServerRequest, response: IHttpResponse, route: IBaseRoute | undefined, requestIdentity: IHttpRequestIdentity, processorState: {
|
|
29
29
|
[id: string]: unknown;
|
|
30
30
|
}): Promise<void>;
|
|
31
31
|
}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,8 +1,14 @@
|
|
|
1
|
-
export * from "./data/
|
|
2
|
-
export * from "./
|
|
1
|
+
export * from "./data/restRouteProcessor";
|
|
2
|
+
export * from "./data/socketRouteProcessor";
|
|
3
3
|
export * from "./identity/nodeIdentityProcessor";
|
|
4
|
+
export * from "./identity/staticUserIdentityProcessor";
|
|
4
5
|
export * from "./logging/loggingProcessor";
|
|
5
|
-
export * from "./
|
|
6
|
-
export * from "./
|
|
6
|
+
export * from "./mimeType/jsonLdMimeTypeProcessor";
|
|
7
|
+
export * from "./mimeType/jwtMimeTypeProcessor";
|
|
8
|
+
export * from "./models/ILoggingProcessorConfig";
|
|
9
|
+
export * from "./models/ILoggingProcessorConstructorOptions";
|
|
10
|
+
export * from "./models/IRestRouteProcessorConstructorOptions";
|
|
7
11
|
export * from "./models/IRouteProcessorConfig";
|
|
12
|
+
export * from "./models/ISocketRouteProcessorConstructorOptions";
|
|
8
13
|
export * from "./models/IStaticUserIdentityProcessorConfig";
|
|
14
|
+
export * from "./models/IStaticUserIdentityProcessorConstructorOptions";
|
|
@@ -1,24 +1,22 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type {
|
|
1
|
+
import type { IBaseRoute, IBaseRouteProcessor, IHttpRequestIdentity, IHttpResponse, IHttpServerRequest } from "@twin.org/api-models";
|
|
2
|
+
import type { ILoggingProcessorConstructorOptions } from "../models/ILoggingProcessorConstructorOptions";
|
|
3
3
|
/**
|
|
4
4
|
* Process the REST request and log its information.
|
|
5
5
|
*/
|
|
6
|
-
export declare class LoggingProcessor implements
|
|
6
|
+
export declare class LoggingProcessor implements IBaseRouteProcessor {
|
|
7
|
+
/**
|
|
8
|
+
* The namespace supported by the processor.
|
|
9
|
+
*/
|
|
10
|
+
static readonly NAMESPACE: string;
|
|
7
11
|
/**
|
|
8
12
|
* Runtime name for the class.
|
|
9
13
|
*/
|
|
10
14
|
readonly CLASS_NAME: string;
|
|
11
15
|
/**
|
|
12
|
-
* Create a new instance of
|
|
16
|
+
* Create a new instance of LoggingProcessor.
|
|
13
17
|
* @param options Options for the processor.
|
|
14
|
-
* @param options.loggingConnectorType The type for the logging connector, defaults to "logging".
|
|
15
|
-
* @param options.config The configuration for the processor.
|
|
16
|
-
* @returns Promise that resolves when the processor is initialized.
|
|
17
18
|
*/
|
|
18
|
-
constructor(options?:
|
|
19
|
-
loggingConnectorType?: string;
|
|
20
|
-
config?: IRequestLoggingProcessorConfig;
|
|
21
|
-
});
|
|
19
|
+
constructor(options?: ILoggingProcessorConstructorOptions);
|
|
22
20
|
/**
|
|
23
21
|
* Pre process the REST request for the specified route.
|
|
24
22
|
* @param request The incoming request.
|
|
@@ -27,7 +25,7 @@ export declare class LoggingProcessor implements IHttpRestRouteProcessor {
|
|
|
27
25
|
* @param requestIdentity The identity context for the request.
|
|
28
26
|
* @param processorState The state handed through the processors.
|
|
29
27
|
*/
|
|
30
|
-
pre(request: IHttpServerRequest, response: IHttpResponse, route:
|
|
28
|
+
pre(request: IHttpServerRequest, response: IHttpResponse, route: IBaseRoute | undefined, requestIdentity: IHttpRequestIdentity, processorState: {
|
|
31
29
|
[id: string]: unknown;
|
|
32
30
|
}): Promise<void>;
|
|
33
31
|
/**
|
|
@@ -38,7 +36,7 @@ export declare class LoggingProcessor implements IHttpRestRouteProcessor {
|
|
|
38
36
|
* @param requestIdentity The identity context for the request.
|
|
39
37
|
* @param processorState The state handed through the processors.
|
|
40
38
|
*/
|
|
41
|
-
post(request: IHttpServerRequest, response: IHttpResponse, route:
|
|
39
|
+
post(request: IHttpServerRequest, response: IHttpResponse, route: IBaseRoute | undefined, requestIdentity: IHttpRequestIdentity, processorState: {
|
|
42
40
|
[id: string]: unknown;
|
|
43
41
|
}): Promise<void>;
|
|
44
42
|
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { IMimeTypeProcessor } from "@twin.org/api-models";
|
|
2
|
+
/**
|
|
3
|
+
* Process the JSON-LD mime type.
|
|
4
|
+
*/
|
|
5
|
+
export declare class JsonLdMimeTypeProcessor implements IMimeTypeProcessor {
|
|
6
|
+
/**
|
|
7
|
+
* The namespace supported by the processor.
|
|
8
|
+
*/
|
|
9
|
+
static readonly NAMESPACE: string;
|
|
10
|
+
/**
|
|
11
|
+
* Runtime name for the class.
|
|
12
|
+
*/
|
|
13
|
+
readonly CLASS_NAME: string;
|
|
14
|
+
/**
|
|
15
|
+
* Get the MIME types that this handler can handle.
|
|
16
|
+
* @returns The MIME types that this handler can handle.
|
|
17
|
+
*/
|
|
18
|
+
getTypes(): string[];
|
|
19
|
+
/**
|
|
20
|
+
* Handle content.
|
|
21
|
+
* @param body The body to process.
|
|
22
|
+
* @returns The processed body.
|
|
23
|
+
*/
|
|
24
|
+
handle(body: Uint8Array): Promise<unknown>;
|
|
25
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { IMimeTypeProcessor } from "@twin.org/api-models";
|
|
2
|
+
/**
|
|
3
|
+
* Process the JWT mime type.
|
|
4
|
+
*/
|
|
5
|
+
export declare class JwtMimeTypeProcessor implements IMimeTypeProcessor {
|
|
6
|
+
/**
|
|
7
|
+
* The namespace supported by the processor.
|
|
8
|
+
*/
|
|
9
|
+
static readonly NAMESPACE: string;
|
|
10
|
+
/**
|
|
11
|
+
* Runtime name for the class.
|
|
12
|
+
*/
|
|
13
|
+
readonly CLASS_NAME: string;
|
|
14
|
+
/**
|
|
15
|
+
* Get the MIME types that this handler can handle.
|
|
16
|
+
* @returns The MIME types that this handler can handle.
|
|
17
|
+
*/
|
|
18
|
+
getTypes(): string[];
|
|
19
|
+
/**
|
|
20
|
+
* Handle content.
|
|
21
|
+
* @param body The body to process.
|
|
22
|
+
* @returns The processed body.
|
|
23
|
+
*/
|
|
24
|
+
handle(body: Uint8Array): Promise<unknown>;
|
|
25
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Configuration for the request logging processor.
|
|
3
|
+
*/
|
|
4
|
+
export interface ILoggingProcessorConfig {
|
|
5
|
+
/**
|
|
6
|
+
* Include the body objects when logging the information.
|
|
7
|
+
*/
|
|
8
|
+
includeBody?: boolean;
|
|
9
|
+
/**
|
|
10
|
+
* Show the full base64 content for data, default to abbreviate.
|
|
11
|
+
*/
|
|
12
|
+
fullBase64?: boolean;
|
|
13
|
+
/**
|
|
14
|
+
* List of property names to obfuscate, can be regex, defaults to "password".
|
|
15
|
+
*/
|
|
16
|
+
obfuscateProperties?: string[];
|
|
17
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { ILoggingProcessorConfig } from "./ILoggingProcessorConfig";
|
|
2
|
+
/**
|
|
3
|
+
* Options for the LoggingProcessor constructor.
|
|
4
|
+
*/
|
|
5
|
+
export interface ILoggingProcessorConstructorOptions {
|
|
6
|
+
/**
|
|
7
|
+
* The type for the logging connector.
|
|
8
|
+
* @default logging
|
|
9
|
+
*/
|
|
10
|
+
loggingConnectorType?: string;
|
|
11
|
+
/**
|
|
12
|
+
*
|
|
13
|
+
*/
|
|
14
|
+
config?: ILoggingProcessorConfig;
|
|
15
|
+
}
|