@twin.org/api-processors 0.0.2-next.8 → 0.0.3-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/es/data/restRouteProcessor.js +115 -0
- package/dist/es/data/restRouteProcessor.js.map +1 -0
- package/dist/es/data/socketRouteProcessor.js +126 -0
- package/dist/es/data/socketRouteProcessor.js.map +1 -0
- package/dist/es/identity/contextIdProcessor.js +68 -0
- package/dist/es/identity/contextIdProcessor.js.map +1 -0
- package/dist/es/identity/staticContextIdProcessor.js +59 -0
- package/dist/es/identity/staticContextIdProcessor.js.map +1 -0
- package/dist/es/index.js +19 -0
- package/dist/es/index.js.map +1 -0
- package/dist/es/logging/loggingProcessor.js +177 -0
- package/dist/es/logging/loggingProcessor.js.map +1 -0
- package/dist/es/mimeType/jsonLdMimeTypeProcessor.js +38 -0
- package/dist/es/mimeType/jsonLdMimeTypeProcessor.js.map +1 -0
- package/dist/es/mimeType/jwtMimeTypeProcessor.js +34 -0
- package/dist/es/mimeType/jwtMimeTypeProcessor.js.map +1 -0
- package/dist/es/models/IContextIdProcessorConfig.js +4 -0
- package/dist/es/models/IContextIdProcessorConfig.js.map +1 -0
- package/dist/es/models/IContextIdProcessorConstructorOptions.js +2 -0
- package/dist/es/models/IContextIdProcessorConstructorOptions.js.map +1 -0
- package/dist/es/models/ILoggingProcessorConfig.js +4 -0
- package/dist/es/models/ILoggingProcessorConfig.js.map +1 -0
- package/dist/es/models/ILoggingProcessorConstructorOptions.js +2 -0
- package/dist/es/models/ILoggingProcessorConstructorOptions.js.map +1 -0
- package/dist/es/models/IRestRouteProcessorConstructorOptions.js +2 -0
- package/dist/es/models/IRestRouteProcessorConstructorOptions.js.map +1 -0
- package/dist/es/models/IRouteProcessorConfig.js +4 -0
- package/dist/es/models/IRouteProcessorConfig.js.map +1 -0
- package/dist/es/models/ISocketRouteProcessorConstructorOptions.js +2 -0
- package/dist/es/models/ISocketRouteProcessorConstructorOptions.js.map +1 -0
- package/dist/es/models/IStaticContextIdProcessorConfig.js +4 -0
- package/dist/es/models/IStaticContextIdProcessorConfig.js.map +1 -0
- package/dist/es/models/IStaticContextIdProcessorConstructorOptions.js +2 -0
- package/dist/es/models/IStaticContextIdProcessorConstructorOptions.js.map +1 -0
- package/dist/types/data/restRouteProcessor.d.ts +9 -5
- package/dist/types/data/socketRouteProcessor.d.ts +10 -6
- package/dist/types/identity/contextIdProcessor.d.ts +39 -0
- package/dist/types/identity/staticContextIdProcessor.d.ts +33 -0
- package/dist/types/index.d.ts +16 -14
- package/dist/types/logging/loggingProcessor.d.ts +13 -7
- package/dist/types/mimeType/jsonLdMimeTypeProcessor.d.ts +6 -1
- package/dist/types/mimeType/jwtMimeTypeProcessor.d.ts +6 -1
- package/dist/types/models/IContextIdProcessorConfig.d.ts +13 -0
- package/dist/types/models/IContextIdProcessorConstructorOptions.d.ts +10 -0
- package/dist/types/models/ILoggingProcessorConstructorOptions.d.ts +1 -1
- package/dist/types/models/IRestRouteProcessorConstructorOptions.d.ts +1 -1
- package/dist/types/models/ISocketRouteProcessorConstructorOptions.d.ts +1 -1
- package/dist/types/models/IStaticContextIdProcessorConfig.d.ts +17 -0
- package/dist/types/models/IStaticContextIdProcessorConstructorOptions.d.ts +10 -0
- package/docs/changelog.md +96 -0
- package/docs/reference/classes/{NodeIdentityProcessor.md → ContextIdProcessor.md} +37 -19
- package/docs/reference/classes/JsonLdMimeTypeProcessor.md +17 -3
- package/docs/reference/classes/JwtMimeTypeProcessor.md +17 -3
- package/docs/reference/classes/LoggingProcessor.md +27 -13
- package/docs/reference/classes/RestRouteProcessor.md +19 -11
- package/docs/reference/classes/SocketRouteProcessor.md +22 -14
- package/docs/reference/classes/StaticContextIdProcessor.md +99 -0
- package/docs/reference/index.md +6 -4
- package/docs/reference/interfaces/IContextIdProcessorConfig.md +19 -0
- package/docs/reference/interfaces/IContextIdProcessorConstructorOptions.md +11 -0
- package/docs/reference/interfaces/IStaticContextIdProcessorConfig.md +27 -0
- package/docs/reference/interfaces/IStaticContextIdProcessorConstructorOptions.md +11 -0
- package/locales/en.json +0 -5
- package/package.json +20 -9
- package/dist/cjs/index.cjs +0 -527
- package/dist/esm/index.mjs +0 -519
- package/dist/types/identity/nodeIdentityProcessor.d.ts +0 -28
- package/dist/types/identity/staticUserIdentityProcessor.d.ts +0 -27
- package/dist/types/models/IStaticUserIdentityProcessorConfig.d.ts +0 -9
- package/dist/types/models/IStaticUserIdentityProcessorConstructorOptions.d.ts +0 -10
- package/docs/reference/classes/StaticUserIdentityProcessor.md +0 -85
- package/docs/reference/interfaces/IStaticUserIdentityProcessorConfig.md +0 -11
- package/docs/reference/interfaces/IStaticUserIdentityProcessorConstructorOptions.md +0 -11
package/dist/esm/index.mjs
DELETED
|
@@ -1,519 +0,0 @@
|
|
|
1
|
-
import { HttpErrorHelper } from '@twin.org/api-models';
|
|
2
|
-
import { Is, NotFoundError, Guards, ComponentFactory, ObjectHelper, Coerce, GeneralError, Converter } from '@twin.org/core';
|
|
3
|
-
import { HttpStatusCode, HeaderTypes, MimeTypes } from '@twin.org/web';
|
|
4
|
-
|
|
5
|
-
// Copyright 2024 IOTA Stiftung.
|
|
6
|
-
// SPDX-License-Identifier: Apache-2.0.
|
|
7
|
-
/**
|
|
8
|
-
* Process the REST request and hands it on to the route handler.
|
|
9
|
-
*/
|
|
10
|
-
class RestRouteProcessor {
|
|
11
|
-
/**
|
|
12
|
-
* Runtime name for the class.
|
|
13
|
-
*/
|
|
14
|
-
CLASS_NAME = "RestRouteProcessor";
|
|
15
|
-
/**
|
|
16
|
-
* Include the stack with errors.
|
|
17
|
-
* @internal
|
|
18
|
-
*/
|
|
19
|
-
_includeErrorStack;
|
|
20
|
-
/**
|
|
21
|
-
* Create a new instance of RouteProcessor.
|
|
22
|
-
* @param options Options for the processor.
|
|
23
|
-
*/
|
|
24
|
-
constructor(options) {
|
|
25
|
-
this._includeErrorStack = options?.config?.includeErrorStack ?? false;
|
|
26
|
-
}
|
|
27
|
-
/**
|
|
28
|
-
* Process the REST request for the specified route.
|
|
29
|
-
* @param request The incoming request.
|
|
30
|
-
* @param response The outgoing response.
|
|
31
|
-
* @param route The route to process.
|
|
32
|
-
* @param requestIdentity The identity context for the request.
|
|
33
|
-
* @param processorState The state handed through the processors.
|
|
34
|
-
* @param loggingComponentType The logging component type for the request.
|
|
35
|
-
*/
|
|
36
|
-
async process(request, response, route, requestIdentity, processorState, loggingComponentType) {
|
|
37
|
-
// Don't handle the route if another processor has already set the response
|
|
38
|
-
// status code e.g. from an auth processor
|
|
39
|
-
if (Is.empty(response.statusCode)) {
|
|
40
|
-
if (Is.empty(route)) {
|
|
41
|
-
HttpErrorHelper.buildResponse(response, {
|
|
42
|
-
name: NotFoundError.CLASS_NAME,
|
|
43
|
-
message: `${this.CLASS_NAME}.routeNotFound`,
|
|
44
|
-
properties: {
|
|
45
|
-
notFoundId: request.url
|
|
46
|
-
}
|
|
47
|
-
}, HttpStatusCode.notFound);
|
|
48
|
-
}
|
|
49
|
-
else {
|
|
50
|
-
try {
|
|
51
|
-
const req = {
|
|
52
|
-
pathParams: request.pathParams,
|
|
53
|
-
query: request.query,
|
|
54
|
-
body: request.body
|
|
55
|
-
};
|
|
56
|
-
const restRouteResponse = await route.handler({
|
|
57
|
-
...requestIdentity,
|
|
58
|
-
serverRequest: request,
|
|
59
|
-
processorState,
|
|
60
|
-
loggingComponentType
|
|
61
|
-
}, req);
|
|
62
|
-
let statusCode = restRouteResponse.statusCode ?? response.statusCode ?? HttpStatusCode.ok;
|
|
63
|
-
const headers = restRouteResponse?.headers ?? {};
|
|
64
|
-
if (Is.empty(restRouteResponse?.body)) {
|
|
65
|
-
// If there is no custom status code and the body is empty
|
|
66
|
-
// use the no content response and set the length to 0
|
|
67
|
-
headers[HeaderTypes.ContentLength] = "0";
|
|
68
|
-
// Only change to no content if the status code is ok
|
|
69
|
-
// This could be something like a created status code
|
|
70
|
-
// which is successful but has no content
|
|
71
|
-
if (statusCode === HttpStatusCode.ok) {
|
|
72
|
-
statusCode = HttpStatusCode.noContent;
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
else {
|
|
76
|
-
// Only set the content type if there is a body
|
|
77
|
-
// If there are custom response types for the route then use them
|
|
78
|
-
// instead of the default application/json
|
|
79
|
-
headers[HeaderTypes.ContentType] =
|
|
80
|
-
restRouteResponse?.attachment?.mimeType ??
|
|
81
|
-
restRouteResponse.headers?.[HeaderTypes.ContentType] ??
|
|
82
|
-
`${MimeTypes.Json}; charset=utf-8`;
|
|
83
|
-
// If there are filename or inline options set then add the content disposition
|
|
84
|
-
if (Is.stringValue(restRouteResponse?.attachment?.filename) ||
|
|
85
|
-
Is.boolean(restRouteResponse?.attachment?.inline)) {
|
|
86
|
-
let filename = "";
|
|
87
|
-
if (Is.stringValue(restRouteResponse?.attachment?.filename)) {
|
|
88
|
-
filename = `; filename="${restRouteResponse?.attachment?.filename}"`;
|
|
89
|
-
}
|
|
90
|
-
headers[HeaderTypes.ContentDisposition] =
|
|
91
|
-
`${restRouteResponse?.attachment?.inline ? "inline" : "attachment"}${filename}`;
|
|
92
|
-
}
|
|
93
|
-
// If this is a binary response then set the content length
|
|
94
|
-
if (Is.uint8Array(restRouteResponse?.body)) {
|
|
95
|
-
const contentLength = restRouteResponse.body.length;
|
|
96
|
-
headers[HeaderTypes.ContentLength] = contentLength.toString();
|
|
97
|
-
}
|
|
98
|
-
response.body = restRouteResponse?.body;
|
|
99
|
-
}
|
|
100
|
-
response.headers = headers;
|
|
101
|
-
response.statusCode = statusCode;
|
|
102
|
-
}
|
|
103
|
-
catch (err) {
|
|
104
|
-
const { error, httpStatusCode } = HttpErrorHelper.processError(err, this._includeErrorStack);
|
|
105
|
-
HttpErrorHelper.buildResponse(response, error, httpStatusCode);
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
// Copyright 2024 IOTA Stiftung.
|
|
113
|
-
// SPDX-License-Identifier: Apache-2.0.
|
|
114
|
-
/**
|
|
115
|
-
* Process the socket request and hands it on to the route handler.
|
|
116
|
-
*/
|
|
117
|
-
class SocketRouteProcessor {
|
|
118
|
-
/**
|
|
119
|
-
* Runtime name for the class.
|
|
120
|
-
*/
|
|
121
|
-
CLASS_NAME = "SocketRouteProcessor";
|
|
122
|
-
/**
|
|
123
|
-
* Include the stack with errors.
|
|
124
|
-
* @internal
|
|
125
|
-
*/
|
|
126
|
-
_includeErrorStack;
|
|
127
|
-
/**
|
|
128
|
-
* Create a new instance of SocketRouteProcessor.
|
|
129
|
-
* @param options Options for the processor.
|
|
130
|
-
*/
|
|
131
|
-
constructor(options) {
|
|
132
|
-
this._includeErrorStack = options?.config?.includeErrorStack ?? false;
|
|
133
|
-
}
|
|
134
|
-
/**
|
|
135
|
-
* Process the connected event.
|
|
136
|
-
* @param request The server request object containing the socket id and other parameters.
|
|
137
|
-
* @param route The route being requested, if a matching one was found.
|
|
138
|
-
* @param loggingComponentType The logging component type for the request.
|
|
139
|
-
* @returns Promise that resolves when the request is processed.
|
|
140
|
-
*/
|
|
141
|
-
async connected(request, route, loggingComponentType) {
|
|
142
|
-
if (route?.connected) {
|
|
143
|
-
try {
|
|
144
|
-
const socketRequestContext = {
|
|
145
|
-
socketId: request.socketId,
|
|
146
|
-
serverRequest: request,
|
|
147
|
-
processorState: {},
|
|
148
|
-
loggingComponentType
|
|
149
|
-
};
|
|
150
|
-
await route.connected(socketRequestContext);
|
|
151
|
-
}
|
|
152
|
-
catch { }
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
/**
|
|
156
|
-
* Process the disconnected event.
|
|
157
|
-
* @param request The server request object containing the socket id and other parameters.
|
|
158
|
-
* @param route The route being requested, if a matching one was found.
|
|
159
|
-
* @param loggingComponentType The logging component type for the request.
|
|
160
|
-
* @returns Promise that resolves when the request is processed.
|
|
161
|
-
*/
|
|
162
|
-
async disconnected(request, route, loggingComponentType) {
|
|
163
|
-
if (route?.disconnected) {
|
|
164
|
-
try {
|
|
165
|
-
const socketRequestContext = {
|
|
166
|
-
socketId: request.socketId,
|
|
167
|
-
serverRequest: request,
|
|
168
|
-
processorState: {},
|
|
169
|
-
loggingComponentType
|
|
170
|
-
};
|
|
171
|
-
await route.disconnected(socketRequestContext);
|
|
172
|
-
}
|
|
173
|
-
catch { }
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
/**
|
|
177
|
-
* Process the REST request for the specified route.
|
|
178
|
-
* @param request The incoming request.
|
|
179
|
-
* @param response The outgoing response.
|
|
180
|
-
* @param route The route to process.
|
|
181
|
-
* @param requestIdentity The identity context for the request.
|
|
182
|
-
* @param processorState The state handed through the processors.
|
|
183
|
-
* @param responseEmitter The function to emit a response.
|
|
184
|
-
* @param loggingComponentType The logging component type for the request.
|
|
185
|
-
*/
|
|
186
|
-
async process(request, response, route, requestIdentity, processorState, responseEmitter, loggingComponentType) {
|
|
187
|
-
// Don't handle the route if another processor has already set the response
|
|
188
|
-
// status code e.g. from an auth processor
|
|
189
|
-
if (Is.empty(response.statusCode)) {
|
|
190
|
-
if (Is.empty(route)) {
|
|
191
|
-
HttpErrorHelper.buildResponse(response, {
|
|
192
|
-
name: NotFoundError.CLASS_NAME,
|
|
193
|
-
message: `${this.CLASS_NAME}.routeNotFound`,
|
|
194
|
-
properties: {
|
|
195
|
-
notFoundId: request.url
|
|
196
|
-
}
|
|
197
|
-
}, HttpStatusCode.notFound);
|
|
198
|
-
}
|
|
199
|
-
else {
|
|
200
|
-
try {
|
|
201
|
-
const req = {
|
|
202
|
-
pathParams: request.pathParams,
|
|
203
|
-
query: request.query,
|
|
204
|
-
body: request.body
|
|
205
|
-
};
|
|
206
|
-
const socketRequestContext = {
|
|
207
|
-
...requestIdentity,
|
|
208
|
-
socketId: request.socketId,
|
|
209
|
-
serverRequest: request,
|
|
210
|
-
processorState,
|
|
211
|
-
loggingComponentType
|
|
212
|
-
};
|
|
213
|
-
await route.handler(socketRequestContext, req, async (topic, restRouteResponse) => {
|
|
214
|
-
response.headers = restRouteResponse?.headers;
|
|
215
|
-
response.body = restRouteResponse?.body;
|
|
216
|
-
response.statusCode =
|
|
217
|
-
restRouteResponse.statusCode ?? response.statusCode ?? HttpStatusCode.ok;
|
|
218
|
-
await responseEmitter(topic, response);
|
|
219
|
-
});
|
|
220
|
-
}
|
|
221
|
-
catch (err) {
|
|
222
|
-
const { error, httpStatusCode } = HttpErrorHelper.processError(err, this._includeErrorStack);
|
|
223
|
-
HttpErrorHelper.buildResponse(response, error, httpStatusCode);
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
/**
|
|
231
|
-
* Adds a node identity to the request identity.
|
|
232
|
-
*/
|
|
233
|
-
class NodeIdentityProcessor {
|
|
234
|
-
/**
|
|
235
|
-
* Runtime name for the class.
|
|
236
|
-
*/
|
|
237
|
-
CLASS_NAME = "NodeIdentityProcessor";
|
|
238
|
-
/**
|
|
239
|
-
* The node identity for request context.
|
|
240
|
-
* @internal
|
|
241
|
-
*/
|
|
242
|
-
_nodeIdentity;
|
|
243
|
-
/**
|
|
244
|
-
* The service needs to be started when the application is initialized.
|
|
245
|
-
* @param nodeIdentity The identity of the node.
|
|
246
|
-
* @param nodeLoggingComponentType The node logging component type.
|
|
247
|
-
* @returns Nothing.
|
|
248
|
-
*/
|
|
249
|
-
async start(nodeIdentity, nodeLoggingComponentType) {
|
|
250
|
-
Guards.string(this.CLASS_NAME, "nodeIdentity", nodeIdentity);
|
|
251
|
-
this._nodeIdentity = nodeIdentity;
|
|
252
|
-
}
|
|
253
|
-
/**
|
|
254
|
-
* Pre process the REST request for the specified route.
|
|
255
|
-
* @param request The incoming request.
|
|
256
|
-
* @param response The outgoing response.
|
|
257
|
-
* @param route The route to process.
|
|
258
|
-
* @param requestIdentity The identity context for the request.
|
|
259
|
-
* @param processorState The state handed through the processors.
|
|
260
|
-
*/
|
|
261
|
-
async pre(request, response, route, requestIdentity, processorState) {
|
|
262
|
-
requestIdentity.nodeIdentity = this._nodeIdentity;
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
/**
|
|
267
|
-
* Adds a static user identity to the request context.
|
|
268
|
-
*/
|
|
269
|
-
class StaticUserIdentityProcessor {
|
|
270
|
-
/**
|
|
271
|
-
* Runtime name for the class.
|
|
272
|
-
*/
|
|
273
|
-
CLASS_NAME = "StaticUserIdentityProcessor";
|
|
274
|
-
/**
|
|
275
|
-
* The fixed identity for request context.
|
|
276
|
-
* @internal
|
|
277
|
-
*/
|
|
278
|
-
_userIdentity;
|
|
279
|
-
/**
|
|
280
|
-
* Create a new instance of StaticIdentityProcessor.
|
|
281
|
-
* @param options Options for the processor.
|
|
282
|
-
*/
|
|
283
|
-
constructor(options) {
|
|
284
|
-
Guards.object(this.CLASS_NAME, "options", options);
|
|
285
|
-
Guards.object(this.CLASS_NAME, "options.config", options.config);
|
|
286
|
-
Guards.stringValue(this.CLASS_NAME, "options.config.userIdentity", options.config.userIdentity);
|
|
287
|
-
this._userIdentity = options.config.userIdentity;
|
|
288
|
-
}
|
|
289
|
-
/**
|
|
290
|
-
* Pre process the REST request for the specified route.
|
|
291
|
-
* @param request The incoming request.
|
|
292
|
-
* @param response The outgoing response.
|
|
293
|
-
* @param route The route to process.
|
|
294
|
-
* @param requestIdentity The identity context for the request.
|
|
295
|
-
* @param processorState The state handed through the processors.
|
|
296
|
-
*/
|
|
297
|
-
async pre(request, response, route, requestIdentity, processorState) {
|
|
298
|
-
if (!Is.empty(route) && !(route.skipAuth ?? false)) {
|
|
299
|
-
requestIdentity.userIdentity = this._userIdentity;
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
/**
|
|
305
|
-
* Process the REST request and log its information.
|
|
306
|
-
*/
|
|
307
|
-
class LoggingProcessor {
|
|
308
|
-
/**
|
|
309
|
-
* Runtime name for the class.
|
|
310
|
-
*/
|
|
311
|
-
CLASS_NAME = "LoggingProcessor";
|
|
312
|
-
/**
|
|
313
|
-
* The component for logging the information.
|
|
314
|
-
* @internal
|
|
315
|
-
*/
|
|
316
|
-
_logging;
|
|
317
|
-
/**
|
|
318
|
-
* Include the body objects when logging the information.
|
|
319
|
-
* @internal
|
|
320
|
-
*/
|
|
321
|
-
_includeBody;
|
|
322
|
-
/**
|
|
323
|
-
* Show the full base64 content for data, default to abbreviate.
|
|
324
|
-
* @internal
|
|
325
|
-
*/
|
|
326
|
-
_fullBase64;
|
|
327
|
-
/**
|
|
328
|
-
* List of property names to obfuscate, defaults to "password".
|
|
329
|
-
* @internal
|
|
330
|
-
*/
|
|
331
|
-
_obfuscateProperties;
|
|
332
|
-
/**
|
|
333
|
-
* Create a new instance of LoggingProcessor.
|
|
334
|
-
* @param options Options for the processor.
|
|
335
|
-
*/
|
|
336
|
-
constructor(options) {
|
|
337
|
-
this._logging = ComponentFactory.getIfExists(options?.loggingComponentType ?? "logging");
|
|
338
|
-
this._includeBody = options?.config?.includeBody ?? false;
|
|
339
|
-
this._fullBase64 = options?.config?.fullBase64 ?? false;
|
|
340
|
-
this._obfuscateProperties = options?.config?.obfuscateProperties ?? ["password"];
|
|
341
|
-
}
|
|
342
|
-
/**
|
|
343
|
-
* Pre process the REST request for the specified route.
|
|
344
|
-
* @param request The incoming request.
|
|
345
|
-
* @param response The outgoing response.
|
|
346
|
-
* @param route The route to process.
|
|
347
|
-
* @param requestIdentity The identity context for the request.
|
|
348
|
-
* @param processorState The state handed through the processors.
|
|
349
|
-
*/
|
|
350
|
-
async pre(request, response, route, requestIdentity, processorState) {
|
|
351
|
-
const now = process.hrtime.bigint();
|
|
352
|
-
processorState.requestStart = now;
|
|
353
|
-
const contentType = request.headers?.[HeaderTypes.ContentType];
|
|
354
|
-
const isJson = Is.stringValue(contentType)
|
|
355
|
-
? contentType.includes(MimeTypes.Json) || contentType.includes(MimeTypes.JsonLd)
|
|
356
|
-
: false;
|
|
357
|
-
let requestUrl = "";
|
|
358
|
-
if (Is.stringValue(request.url)) {
|
|
359
|
-
// Socket paths do not have a prefix so just use the whole url.
|
|
360
|
-
if (request.url.startsWith("http")) {
|
|
361
|
-
requestUrl = new URL(request.url).pathname;
|
|
362
|
-
}
|
|
363
|
-
else {
|
|
364
|
-
requestUrl = request.url;
|
|
365
|
-
}
|
|
366
|
-
}
|
|
367
|
-
await this._logging?.log({
|
|
368
|
-
level: "info",
|
|
369
|
-
source: this.CLASS_NAME,
|
|
370
|
-
ts: Date.now(),
|
|
371
|
-
message: `===> ${request.method} ${requestUrl}`,
|
|
372
|
-
data: this._includeBody && isJson
|
|
373
|
-
? this.processJson("body", ObjectHelper.clone(request?.body))
|
|
374
|
-
: undefined
|
|
375
|
-
});
|
|
376
|
-
}
|
|
377
|
-
/**
|
|
378
|
-
* Post process the REST request for the specified route.
|
|
379
|
-
* @param request The incoming request.
|
|
380
|
-
* @param response The outgoing response.
|
|
381
|
-
* @param route The route to process.
|
|
382
|
-
* @param requestIdentity The identity context for the request.
|
|
383
|
-
* @param processorState The state handed through the processors.
|
|
384
|
-
*/
|
|
385
|
-
async post(request, response, route, requestIdentity, processorState) {
|
|
386
|
-
let data;
|
|
387
|
-
if (this._includeBody) {
|
|
388
|
-
const contentType = response.headers?.[HeaderTypes.ContentType];
|
|
389
|
-
const isJson = Is.stringValue(contentType)
|
|
390
|
-
? contentType.includes(MimeTypes.Json) || contentType.includes(MimeTypes.JsonLd)
|
|
391
|
-
: false;
|
|
392
|
-
const contentLength = response.headers?.[HeaderTypes.ContentLength];
|
|
393
|
-
if (isJson) {
|
|
394
|
-
data = {
|
|
395
|
-
body: this.processJson("body", ObjectHelper.clone(response.body))
|
|
396
|
-
};
|
|
397
|
-
}
|
|
398
|
-
else {
|
|
399
|
-
const dataParts = {};
|
|
400
|
-
if (Is.stringValue(contentType)) {
|
|
401
|
-
dataParts["Content Type"] = contentType;
|
|
402
|
-
}
|
|
403
|
-
if (Is.stringValue(contentLength)) {
|
|
404
|
-
dataParts["Content Length"] = contentLength;
|
|
405
|
-
}
|
|
406
|
-
if (Object.keys(dataParts).length > 0) {
|
|
407
|
-
data = {
|
|
408
|
-
headers: dataParts
|
|
409
|
-
};
|
|
410
|
-
}
|
|
411
|
-
}
|
|
412
|
-
}
|
|
413
|
-
const now = process.hrtime.bigint();
|
|
414
|
-
const start = Coerce.bigint(processorState.requestStart) ?? now;
|
|
415
|
-
const elapsed = now - start;
|
|
416
|
-
const elapsedMicroSeconds = Math.floor(Number(elapsed) / 1000);
|
|
417
|
-
let requestUrl = "";
|
|
418
|
-
if (Is.stringValue(request.url)) {
|
|
419
|
-
// Socket paths do not have a prefix so just use the whole url.
|
|
420
|
-
if (request.url.startsWith("http")) {
|
|
421
|
-
requestUrl = new URL(request.url).pathname;
|
|
422
|
-
}
|
|
423
|
-
else {
|
|
424
|
-
requestUrl = request.url;
|
|
425
|
-
}
|
|
426
|
-
}
|
|
427
|
-
await this._logging?.log({
|
|
428
|
-
level: Is.number(response.statusCode) && response.statusCode >= HttpStatusCode.badRequest
|
|
429
|
-
? "error"
|
|
430
|
-
: "info",
|
|
431
|
-
source: this.CLASS_NAME,
|
|
432
|
-
ts: Date.now(),
|
|
433
|
-
message: `<=== ${response.statusCode ?? ""} ${request.method} ${requestUrl} duration: ${elapsedMicroSeconds}µs`,
|
|
434
|
-
data
|
|
435
|
-
});
|
|
436
|
-
}
|
|
437
|
-
/**
|
|
438
|
-
* Process the JSON.
|
|
439
|
-
* @param propValue The property to process.
|
|
440
|
-
* @returns The processed property.
|
|
441
|
-
* @internal
|
|
442
|
-
*/
|
|
443
|
-
processJson(propName, propValue) {
|
|
444
|
-
if (Is.array(propValue)) {
|
|
445
|
-
for (let i = 0; i < propValue.length; i++) {
|
|
446
|
-
propValue[i] = this.processJson(`${i}`, propValue[i]);
|
|
447
|
-
}
|
|
448
|
-
}
|
|
449
|
-
else if (Is.object(propValue)) {
|
|
450
|
-
for (const key of Object.keys(propValue)) {
|
|
451
|
-
propValue[key] = this.processJson(key, propValue[key]);
|
|
452
|
-
}
|
|
453
|
-
}
|
|
454
|
-
else if (!this._fullBase64 && Is.stringBase64(propValue) && propValue.length > 256) {
|
|
455
|
-
propValue = "<base64>";
|
|
456
|
-
}
|
|
457
|
-
else if (Is.string(propValue) &&
|
|
458
|
-
this._obfuscateProperties.some(op => new RegExp(op).test(propName))) {
|
|
459
|
-
propValue = "**************";
|
|
460
|
-
}
|
|
461
|
-
return propValue;
|
|
462
|
-
}
|
|
463
|
-
}
|
|
464
|
-
|
|
465
|
-
/**
|
|
466
|
-
* Process the JSON-LD mime type.
|
|
467
|
-
*/
|
|
468
|
-
class JsonLdMimeTypeProcessor {
|
|
469
|
-
/**
|
|
470
|
-
* Runtime name for the class.
|
|
471
|
-
*/
|
|
472
|
-
CLASS_NAME = "JsonLdMimeTypeProcessor";
|
|
473
|
-
/**
|
|
474
|
-
* Get the MIME types that this handler can handle.
|
|
475
|
-
* @returns The MIME types that this handler can handle.
|
|
476
|
-
*/
|
|
477
|
-
getTypes() {
|
|
478
|
-
return [MimeTypes.JsonLd];
|
|
479
|
-
}
|
|
480
|
-
/**
|
|
481
|
-
* Handle content.
|
|
482
|
-
* @param body The body to process.
|
|
483
|
-
* @returns The processed body.
|
|
484
|
-
*/
|
|
485
|
-
async handle(body) {
|
|
486
|
-
const json = ObjectHelper.fromBytes(body);
|
|
487
|
-
if (Is.empty(json) || Is.empty(json["@context"])) {
|
|
488
|
-
throw new GeneralError(this.CLASS_NAME, "invalidJsonLd");
|
|
489
|
-
}
|
|
490
|
-
return json;
|
|
491
|
-
}
|
|
492
|
-
}
|
|
493
|
-
|
|
494
|
-
/**
|
|
495
|
-
* Process the JWT mime type.
|
|
496
|
-
*/
|
|
497
|
-
class JwtMimeTypeProcessor {
|
|
498
|
-
/**
|
|
499
|
-
* Runtime name for the class.
|
|
500
|
-
*/
|
|
501
|
-
CLASS_NAME = "JwtMimeTypeProcessor";
|
|
502
|
-
/**
|
|
503
|
-
* Get the MIME types that this handler can handle.
|
|
504
|
-
* @returns The MIME types that this handler can handle.
|
|
505
|
-
*/
|
|
506
|
-
getTypes() {
|
|
507
|
-
return [MimeTypes.Jwt];
|
|
508
|
-
}
|
|
509
|
-
/**
|
|
510
|
-
* Handle content.
|
|
511
|
-
* @param body The body to process.
|
|
512
|
-
* @returns The processed body.
|
|
513
|
-
*/
|
|
514
|
-
async handle(body) {
|
|
515
|
-
return Converter.bytesToUtf8(body);
|
|
516
|
-
}
|
|
517
|
-
}
|
|
518
|
-
|
|
519
|
-
export { JsonLdMimeTypeProcessor, JwtMimeTypeProcessor, LoggingProcessor, NodeIdentityProcessor, RestRouteProcessor, SocketRouteProcessor, StaticUserIdentityProcessor };
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import type { IBaseRoute, IBaseRouteProcessor, IHttpRequestIdentity, IHttpResponse, IHttpServerRequest } from "@twin.org/api-models";
|
|
2
|
-
/**
|
|
3
|
-
* Adds a node identity to the request identity.
|
|
4
|
-
*/
|
|
5
|
-
export declare class NodeIdentityProcessor implements IBaseRouteProcessor {
|
|
6
|
-
/**
|
|
7
|
-
* Runtime name for the class.
|
|
8
|
-
*/
|
|
9
|
-
readonly CLASS_NAME: string;
|
|
10
|
-
/**
|
|
11
|
-
* The service needs to be started when the application is initialized.
|
|
12
|
-
* @param nodeIdentity The identity of the node.
|
|
13
|
-
* @param nodeLoggingComponentType The node logging component type.
|
|
14
|
-
* @returns Nothing.
|
|
15
|
-
*/
|
|
16
|
-
start(nodeIdentity: string, nodeLoggingComponentType?: string): Promise<void>;
|
|
17
|
-
/**
|
|
18
|
-
* Pre process the REST request for the specified route.
|
|
19
|
-
* @param request The incoming request.
|
|
20
|
-
* @param response The outgoing response.
|
|
21
|
-
* @param route The route to process.
|
|
22
|
-
* @param requestIdentity The identity context for the request.
|
|
23
|
-
* @param processorState The state handed through the processors.
|
|
24
|
-
*/
|
|
25
|
-
pre(request: IHttpServerRequest, response: IHttpResponse, route: IBaseRoute | undefined, requestIdentity: IHttpRequestIdentity, processorState: {
|
|
26
|
-
[id: string]: unknown;
|
|
27
|
-
}): Promise<void>;
|
|
28
|
-
}
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import type { IBaseRoute, IBaseRouteProcessor, IHttpRequestIdentity, IHttpResponse, IHttpServerRequest } from "@twin.org/api-models";
|
|
2
|
-
import type { IStaticUserIdentityProcessorConstructorOptions } from "../models/IStaticUserIdentityProcessorConstructorOptions";
|
|
3
|
-
/**
|
|
4
|
-
* Adds a static user identity to the request context.
|
|
5
|
-
*/
|
|
6
|
-
export declare class StaticUserIdentityProcessor implements IBaseRouteProcessor {
|
|
7
|
-
/**
|
|
8
|
-
* Runtime name for the class.
|
|
9
|
-
*/
|
|
10
|
-
readonly CLASS_NAME: string;
|
|
11
|
-
/**
|
|
12
|
-
* Create a new instance of StaticIdentityProcessor.
|
|
13
|
-
* @param options Options for the processor.
|
|
14
|
-
*/
|
|
15
|
-
constructor(options: IStaticUserIdentityProcessorConstructorOptions);
|
|
16
|
-
/**
|
|
17
|
-
* Pre process the REST request for the specified route.
|
|
18
|
-
* @param request The incoming request.
|
|
19
|
-
* @param response The outgoing response.
|
|
20
|
-
* @param route The route to process.
|
|
21
|
-
* @param requestIdentity The identity context for the request.
|
|
22
|
-
* @param processorState The state handed through the processors.
|
|
23
|
-
*/
|
|
24
|
-
pre(request: IHttpServerRequest, response: IHttpResponse, route: IBaseRoute | undefined, requestIdentity: IHttpRequestIdentity, processorState: {
|
|
25
|
-
[id: string]: unknown;
|
|
26
|
-
}): Promise<void>;
|
|
27
|
-
}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import type { IStaticUserIdentityProcessorConfig } from "./IStaticUserIdentityProcessorConfig";
|
|
2
|
-
/**
|
|
3
|
-
* Options for the StaticUserIdentityProcessor constructor.
|
|
4
|
-
*/
|
|
5
|
-
export interface IStaticUserIdentityProcessorConstructorOptions {
|
|
6
|
-
/**
|
|
7
|
-
* The configuration for the processor.
|
|
8
|
-
*/
|
|
9
|
-
config: IStaticUserIdentityProcessorConfig;
|
|
10
|
-
}
|
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
# Class: StaticUserIdentityProcessor
|
|
2
|
-
|
|
3
|
-
Adds a static user identity to the request context.
|
|
4
|
-
|
|
5
|
-
## Implements
|
|
6
|
-
|
|
7
|
-
- `IBaseRouteProcessor`
|
|
8
|
-
|
|
9
|
-
## Constructors
|
|
10
|
-
|
|
11
|
-
### Constructor
|
|
12
|
-
|
|
13
|
-
> **new StaticUserIdentityProcessor**(`options`): `StaticUserIdentityProcessor`
|
|
14
|
-
|
|
15
|
-
Create a new instance of StaticIdentityProcessor.
|
|
16
|
-
|
|
17
|
-
#### Parameters
|
|
18
|
-
|
|
19
|
-
##### options
|
|
20
|
-
|
|
21
|
-
[`IStaticUserIdentityProcessorConstructorOptions`](../interfaces/IStaticUserIdentityProcessorConstructorOptions.md)
|
|
22
|
-
|
|
23
|
-
Options for the processor.
|
|
24
|
-
|
|
25
|
-
#### Returns
|
|
26
|
-
|
|
27
|
-
`StaticUserIdentityProcessor`
|
|
28
|
-
|
|
29
|
-
## Properties
|
|
30
|
-
|
|
31
|
-
### CLASS\_NAME
|
|
32
|
-
|
|
33
|
-
> `readonly` **CLASS\_NAME**: `string`
|
|
34
|
-
|
|
35
|
-
Runtime name for the class.
|
|
36
|
-
|
|
37
|
-
#### Implementation of
|
|
38
|
-
|
|
39
|
-
`IBaseRouteProcessor.CLASS_NAME`
|
|
40
|
-
|
|
41
|
-
## Methods
|
|
42
|
-
|
|
43
|
-
### pre()
|
|
44
|
-
|
|
45
|
-
> **pre**(`request`, `response`, `route`, `requestIdentity`, `processorState`): `Promise`\<`void`\>
|
|
46
|
-
|
|
47
|
-
Pre process the REST request for the specified route.
|
|
48
|
-
|
|
49
|
-
#### Parameters
|
|
50
|
-
|
|
51
|
-
##### request
|
|
52
|
-
|
|
53
|
-
`IHttpServerRequest`
|
|
54
|
-
|
|
55
|
-
The incoming request.
|
|
56
|
-
|
|
57
|
-
##### response
|
|
58
|
-
|
|
59
|
-
`IHttpResponse`
|
|
60
|
-
|
|
61
|
-
The outgoing response.
|
|
62
|
-
|
|
63
|
-
##### route
|
|
64
|
-
|
|
65
|
-
The route to process.
|
|
66
|
-
|
|
67
|
-
`undefined` | `IBaseRoute`
|
|
68
|
-
|
|
69
|
-
##### requestIdentity
|
|
70
|
-
|
|
71
|
-
`IHttpRequestIdentity`
|
|
72
|
-
|
|
73
|
-
The identity context for the request.
|
|
74
|
-
|
|
75
|
-
##### processorState
|
|
76
|
-
|
|
77
|
-
The state handed through the processors.
|
|
78
|
-
|
|
79
|
-
#### Returns
|
|
80
|
-
|
|
81
|
-
`Promise`\<`void`\>
|
|
82
|
-
|
|
83
|
-
#### Implementation of
|
|
84
|
-
|
|
85
|
-
`IBaseRouteProcessor.pre`
|