@whook/gcp-functions 18.0.3 → 19.0.0

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.
Files changed (37) hide show
  1. package/README.md +19 -57
  2. package/dist/commands/testGCPFunctionRoute.d.ts +50 -0
  3. package/dist/commands/{testHTTPFunction.js → testGCPFunctionRoute.js} +44 -46
  4. package/dist/commands/testGCPFunctionRoute.js.map +1 -0
  5. package/dist/index.d.ts +8 -7
  6. package/dist/index.js +50 -54
  7. package/dist/index.js.map +1 -1
  8. package/dist/libs/utils.d.ts +1 -1
  9. package/dist/libs/utils.js +1 -1
  10. package/dist/libs/utils.js.map +1 -1
  11. package/dist/services/_autoload.d.ts +16 -12
  12. package/dist/services/_autoload.js +49 -63
  13. package/dist/services/_autoload.js.map +1 -1
  14. package/dist/wrappers/wrapRouteHandlerForGoogleHTTPFunction.d.ts +19 -0
  15. package/dist/wrappers/wrapRouteHandlerForGoogleHTTPFunction.js +307 -0
  16. package/dist/wrappers/wrapRouteHandlerForGoogleHTTPFunction.js.map +1 -0
  17. package/package.json +11 -10
  18. package/src/commands/{testHTTPFunction.ts → testGCPFunctionRoute.ts} +73 -75
  19. package/src/index.ts +100 -104
  20. package/src/libs/utils.ts +2 -2
  21. package/src/services/_autoload.ts +90 -117
  22. package/src/types.d.ts +8 -0
  23. package/src/wrappers/wrapRouteHandlerForGoogleHTTPFunction.ts +571 -0
  24. package/dist/commands/testHTTPFunction.d.ts +0 -13
  25. package/dist/commands/testHTTPFunction.js.map +0 -1
  26. package/dist/services/HANDLER.d.ts +0 -11
  27. package/dist/services/HANDLER.js +0 -21
  28. package/dist/services/HANDLER.js.map +0 -1
  29. package/dist/services/log.d.ts +0 -5
  30. package/dist/services/log.js +0 -4
  31. package/dist/services/log.js.map +0 -1
  32. package/dist/wrappers/wrapHandlerForGoogleHTTPFunction.d.ts +0 -23
  33. package/dist/wrappers/wrapHandlerForGoogleHTTPFunction.js +0 -272
  34. package/dist/wrappers/wrapHandlerForGoogleHTTPFunction.js.map +0 -1
  35. package/src/services/HANDLER.ts +0 -43
  36. package/src/services/log.ts +0 -7
  37. package/src/wrappers/wrapHandlerForGoogleHTTPFunction.ts +0 -482
@@ -1,482 +0,0 @@
1
- /* eslint-disable @typescript-eslint/no-explicit-any */
2
- import { YError, printStackTrace } from 'yerror';
3
- import {
4
- DEFAULT_DEBUG_NODE_ENVS,
5
- DEFAULT_BUFFER_LIMIT,
6
- DEFAULT_PARSERS,
7
- DEFAULT_STRINGIFYERS,
8
- DEFAULT_DECODERS,
9
- DEFAULT_ENCODERS,
10
- extractOperationSecurityParameters,
11
- castParameters,
12
- } from '@whook/http-router';
13
- import { autoService } from 'knifecycle';
14
- import Ajv from 'ajv';
15
- import addAJVFormats from 'ajv-formats';
16
- import bytes from 'bytes';
17
- import { YHTTPError } from 'yhttperror';
18
- import {
19
- prepareParametersValidators,
20
- prepareBodyValidator,
21
- applyValidators,
22
- filterHeaders,
23
- extractBodySpec,
24
- extractResponseSpec,
25
- checkResponseCharset,
26
- checkResponseMediaType,
27
- executeHandler,
28
- extractProduceableMediaTypes,
29
- extractConsumableMediaTypes,
30
- getBody,
31
- sendBody,
32
- } from '@whook/http-router';
33
- import { noop, identity, lowerCaseHeaders } from '@whook/whook';
34
- import stream from 'stream';
35
- import type { WhookQueryStringParser } from '@whook/http-router';
36
- import type {
37
- WhookRequest,
38
- WhookResponse,
39
- WhookHandler,
40
- WhookObfuscatorService,
41
- WhookOperation,
42
- WhookWrapper,
43
- WhookErrorHandler,
44
- } from '@whook/whook';
45
- import type { LogService } from 'common-services';
46
- import type { OpenAPIV3_1 } from 'openapi-types';
47
- import type { Readable } from 'stream';
48
- import type { AppEnvVars } from 'application-services';
49
- import type { DereferencedParameterObject } from '@whook/http-transaction';
50
-
51
- const SEARCH_SEPARATOR = '?';
52
- const PATH_SEPARATOR = '/';
53
-
54
- export type WhookWrapHTTPFunctionDependencies = {
55
- OPERATION_API: OpenAPIV3_1.Document;
56
- ENV: AppEnvVars;
57
- DEBUG_NODE_ENVS?: string[];
58
- DECODERS?: typeof DEFAULT_DECODERS;
59
- ENCODERS?: typeof DEFAULT_ENCODERS;
60
- PARSERS?: typeof DEFAULT_PARSERS;
61
- STRINGIFYERS?: typeof DEFAULT_STRINGIFYERS;
62
- QUERY_PARSER: WhookQueryStringParser;
63
- BUFFER_LIMIT?: string;
64
- obfuscator: WhookObfuscatorService;
65
- errorHandler: WhookErrorHandler;
66
- log?: LogService;
67
- };
68
-
69
- /**
70
- * Wrap an handler to make it work with GCP Functions.
71
- * @param {Object} services
72
- * The services the wrapper depends on
73
- * @param {Object} services.OPERATION_API
74
- * An OpenAPI definitition for that handler
75
- * @param {Object} services.ENV
76
- * The process environment
77
- * @param {Object} services.DEBUG_NODE_ENVS
78
- * The NODE_ENV values that trigger debugging
79
- * @param {Object} services.DECODERS
80
- * Request body decoders available
81
- * @param {Object} services.ENCODERS
82
- * Response body encoders available
83
- * @param {Object} services.PARSERS
84
- * Request body parsers available
85
- * @param {Object} services.STRINGIFYERS
86
- * Response body stringifyers available
87
- * @param {Object} services.BUFFER_LIMIT
88
- * The buffer size limit
89
- * @param {Object} services.QUERY_PARSER
90
- * The query parser to use
91
- * @param {Object} services.obfuscator
92
- * A service to hide sensible values
93
- * @param {Object} services.errorHandler
94
- * A service that changes any error to Whook response
95
- * @param {Object} [services.log=noop]
96
- * An optional logging service
97
- * @return {Promise<Object>}
98
- * A promise of an object containing the reshaped env vars.
99
- */
100
-
101
- async function initWrapHandlerForGoogleHTTPFunction<S extends WhookHandler>({
102
- OPERATION_API,
103
- ENV,
104
- DEBUG_NODE_ENVS = DEFAULT_DEBUG_NODE_ENVS,
105
- DECODERS = DEFAULT_DECODERS,
106
- ENCODERS = DEFAULT_ENCODERS,
107
- PARSERS = DEFAULT_PARSERS,
108
- STRINGIFYERS = DEFAULT_STRINGIFYERS,
109
- BUFFER_LIMIT = DEFAULT_BUFFER_LIMIT,
110
- QUERY_PARSER,
111
- obfuscator,
112
- errorHandler,
113
- log = noop,
114
- }: WhookWrapHTTPFunctionDependencies): Promise<WhookWrapper<S>> {
115
- log('debug', '📥 - Initializing the AWS Lambda cron wrapper.');
116
-
117
- const path = Object.keys(OPERATION_API.paths || {})[0];
118
- const pathObject = OPERATION_API.paths?.[path];
119
-
120
- if (typeof pathObject === 'undefined' || '$ref' in pathObject) {
121
- throw new YError('E_BAD_OPERATION', 'pathObject', pathObject);
122
- }
123
-
124
- const method = Object.keys(pathObject)[0];
125
- const operationObject = pathObject[method];
126
-
127
- if (typeof operationObject === 'undefined' || '$ref' in operationObject) {
128
- throw new YError('E_BAD_OPERATION', 'operationObject', operationObject);
129
- }
130
-
131
- const operation: WhookOperation = {
132
- path,
133
- method,
134
- ...operationObject,
135
- };
136
- const consumableCharsets = Object.keys(DECODERS);
137
- const produceableCharsets = Object.keys(ENCODERS);
138
- const consumableMediaTypes = extractConsumableMediaTypes(operation);
139
- const produceableMediaTypes = extractProduceableMediaTypes(operation);
140
- const ajv = new Ajv.default({
141
- verbose: DEBUG_NODE_ENVS.includes(ENV.NODE_ENV),
142
- strict: true,
143
- logger: {
144
- log: (...args: string[]) => log?.('debug', ...args),
145
- warn: (...args: string[]) => log?.('warning', ...args),
146
- error: (...args: string[]) => log?.('error', ...args),
147
- },
148
- });
149
- addAJVFormats.default(ajv);
150
- const ammendedParameters = extractOperationSecurityParameters(
151
- OPERATION_API,
152
- operation,
153
- );
154
- const validators = prepareParametersValidators(
155
- ajv,
156
- operation.operationId,
157
- ((operation.parameters || []) as DereferencedParameterObject[]).concat(
158
- ammendedParameters,
159
- ),
160
- );
161
- const bodyValidator = prepareBodyValidator(ajv, operation);
162
- const wrapper = async (handler: S): Promise<S> => {
163
- const wrappedHandler = handleForAWSHTTPFunction.bind(
164
- null,
165
- {
166
- DECODERS,
167
- ENCODERS,
168
- PARSERS,
169
- STRINGIFYERS,
170
- BUFFER_LIMIT,
171
- QUERY_PARSER,
172
- obfuscator,
173
- errorHandler,
174
- log,
175
- },
176
- {
177
- consumableMediaTypes,
178
- produceableMediaTypes,
179
- consumableCharsets,
180
- produceableCharsets,
181
- validators,
182
- bodyValidator,
183
- operation,
184
- },
185
- handler as any,
186
- );
187
-
188
- return wrappedHandler as unknown as S;
189
- };
190
-
191
- return wrapper;
192
- }
193
-
194
- async function handleForAWSHTTPFunction(
195
- {
196
- DECODERS,
197
- ENCODERS,
198
- PARSERS,
199
- STRINGIFYERS,
200
- BUFFER_LIMIT,
201
- QUERY_PARSER,
202
- obfuscator,
203
- errorHandler,
204
- log,
205
- }: Omit<
206
- Required<WhookWrapHTTPFunctionDependencies>,
207
- 'time' | 'OPERATION_API' | 'ENV' | 'DEBUG_NODE_ENVS'
208
- >,
209
- {
210
- consumableMediaTypes,
211
- produceableMediaTypes,
212
- consumableCharsets,
213
- produceableCharsets,
214
- validators,
215
- bodyValidator,
216
- operation,
217
- }: {
218
- consumableMediaTypes: string[];
219
- produceableMediaTypes: string[];
220
- consumableCharsets: string[];
221
- produceableCharsets: string[];
222
- validators: {
223
- [name: string]: Ajv.ValidateFunction<unknown>;
224
- };
225
- bodyValidator: (
226
- operation: WhookOperation,
227
- contentType: string,
228
- value: unknown,
229
- ) => void;
230
- operation: WhookOperation;
231
- },
232
- handler: WhookHandler,
233
- req,
234
- res,
235
- ) {
236
- const bufferLimit =
237
- bytes.parse(BUFFER_LIMIT) || (bytes.parse(DEFAULT_BUFFER_LIMIT) as number);
238
-
239
- log?.(
240
- 'info',
241
- 'GCP_FUNCTIONS_REQUEST',
242
- JSON.stringify({
243
- url: req.originalUrl,
244
- method: req.method,
245
- body: req.body,
246
- // body: obfuscateEventBody(obfuscator, req.body),
247
- headers: obfuscator.obfuscateSensibleHeaders(req.headers),
248
- }),
249
- );
250
-
251
- const request = await gcpfReqToRequest(req);
252
- let parameters;
253
- let response;
254
- let responseLog;
255
- let responseSpec;
256
-
257
- log?.(
258
- 'debug',
259
- 'REQUEST',
260
- JSON.stringify({
261
- ...request,
262
- body: request.body ? 'Stream' : undefined,
263
- headers: obfuscator.obfuscateSensibleHeaders(request.headers),
264
- }),
265
- );
266
-
267
- try {
268
- const bodySpec = extractBodySpec(
269
- request,
270
- consumableMediaTypes,
271
- consumableCharsets,
272
- );
273
-
274
- responseSpec = extractResponseSpec(
275
- operation,
276
- request,
277
- produceableMediaTypes,
278
- produceableCharsets,
279
- );
280
-
281
- try {
282
- const body = await getBody(
283
- {
284
- DECODERS,
285
- PARSERS,
286
- bufferLimit,
287
- },
288
- operation,
289
- request.body as Readable,
290
- bodySpec,
291
- );
292
- const path = request.url.split(SEARCH_SEPARATOR)[0];
293
- const parts = path.split(PATH_SEPARATOR).filter(identity);
294
- const search = request.url.substr(
295
- request.url.split(SEARCH_SEPARATOR)[0].length,
296
- );
297
-
298
- const pathParameters = (
299
- operation.path
300
- .split(PATH_SEPARATOR)
301
- .filter(identity)
302
- .map((part, index) => {
303
- const matches = /^\{([\d\w]+)\}$/i.exec(part);
304
-
305
- if (matches) {
306
- return {
307
- name: matches[1],
308
- value: parts[index],
309
- };
310
- }
311
- }) as Array<{ name: string; value: string }>
312
- )
313
- .filter(identity)
314
- .reduce(
315
- (accParameters, { name, value }) => ({
316
- ...accParameters,
317
- [name]: value,
318
- }),
319
- {},
320
- );
321
-
322
- // TODO: Update strictQS to handle OpenAPI 3
323
- const retroCompatibleQueryParameters = (operation.parameters || [])
324
- .filter((p) => p.in === 'query')
325
- .map((p) => ({ ...p, ...p.schema }));
326
-
327
- parameters = {
328
- ...pathParameters,
329
- ...QUERY_PARSER(retroCompatibleQueryParameters as any, search),
330
- ...filterHeaders(operation.parameters, request.headers),
331
- };
332
-
333
- parameters = {
334
- // TODO: Use the security of the operation to infer
335
- // authorization parameters, see:
336
- // https://github.com/nfroidure/whook/blob/06ccae93d1d52d97ff70fd5e19fa826bdabf3968/packages/whook-http-router/src/validation.js#L110
337
- authorization: parameters.authorization,
338
- ...castParameters(operation.parameters || [], parameters),
339
- };
340
-
341
- applyValidators(operation, validators, parameters);
342
-
343
- bodyValidator(operation, bodySpec.contentType, body);
344
-
345
- parameters = {
346
- ...parameters,
347
- ...('undefined' !== typeof body ? { body } : {}),
348
- };
349
- } catch (err) {
350
- throw YHTTPError.cast(err as Error, 400);
351
- }
352
-
353
- response = await executeHandler(operation, handler, parameters);
354
-
355
- if (response.body) {
356
- response.headers['content-type'] =
357
- response.headers['content-type'] || responseSpec.contentTypes[0];
358
- }
359
-
360
- // Check the stringifyer only when a schema is
361
- // specified and it is not a binary one
362
- const responseObject =
363
- operation.responses &&
364
- (operation.responses[response.status] as OpenAPIV3_1.ResponseObject);
365
- const responseSchema =
366
- responseObject &&
367
- responseObject.content &&
368
- responseObject.content[response.headers['content-type']] &&
369
- (responseObject.content[response.headers['content-type']]
370
- .schema as OpenAPIV3_1.SchemaObject);
371
- const responseHasSchema =
372
- responseSchema &&
373
- (responseSchema.type !== 'string' || responseSchema.format !== 'binary');
374
-
375
- if (responseHasSchema && !STRINGIFYERS[response.headers['content-type']]) {
376
- throw new YHTTPError(
377
- 500,
378
- 'E_STRINGIFYER_LACK',
379
- response.headers['content-type'],
380
- );
381
- }
382
- if (response.body) {
383
- checkResponseMediaType(request, responseSpec, produceableMediaTypes);
384
- checkResponseCharset(request, responseSpec, produceableCharsets);
385
- }
386
- responseLog = {
387
- type: 'success',
388
- status: response.status,
389
- };
390
- log?.('debug', JSON.stringify(responseLog));
391
- } catch (err) {
392
- response = await errorHandler('none', responseSpec, err as Error);
393
- responseLog = {
394
- type: 'error',
395
- code: (err as YError)?.code || 'E_UNEXPECTED',
396
- statusCode: response.status,
397
- params: (err as YError)?.params || [],
398
- stack: printStackTrace(err as Error),
399
- };
400
-
401
- log?.('error', JSON.stringify(responseLog));
402
-
403
- response = {
404
- ...response,
405
- headers: {
406
- ...response.headers,
407
- 'content-type': 'application/json',
408
- },
409
- };
410
- }
411
-
412
- log?.(
413
- 'debug',
414
- 'RESPONSE',
415
- JSON.stringify({
416
- ...response,
417
- body: obfuscateEventBody(obfuscator, response.body),
418
- headers: obfuscator.obfuscateSensibleHeaders(response.headers),
419
- }),
420
- );
421
-
422
- await pipeResponseInGCPFResponse(
423
- await sendBody(
424
- {
425
- ENCODERS,
426
- STRINGIFYERS,
427
- },
428
- response,
429
- ),
430
- res,
431
- );
432
- }
433
-
434
- async function gcpfReqToRequest(req): Promise<WhookRequest> {
435
- const request: WhookRequest = {
436
- method: req.method.toLowerCase(),
437
- headers: lowerCaseHeaders(req.headers || {}),
438
- url: req.originalUrl,
439
- };
440
-
441
- if (req.rawBody) {
442
- request.headers['content-length'] = req.rawBody.length.toString();
443
- const bodyStream = new stream.PassThrough();
444
-
445
- request.body = bodyStream;
446
- bodyStream.write(req.rawBody);
447
- bodyStream.end();
448
- }
449
-
450
- return request;
451
- }
452
-
453
- async function pipeResponseInGCPFResponse(
454
- response: WhookResponse,
455
- res,
456
- ): Promise<void> {
457
- Object.keys(response.headers || {}).forEach((headerName) => {
458
- res.set(headerName, response.headers?.[headerName]);
459
- });
460
- res.status(response.status);
461
-
462
- if (response.body) {
463
- (response.body as Readable).pipe(res);
464
- return;
465
- }
466
-
467
- res.end();
468
- }
469
-
470
- function obfuscateEventBody(obfuscator, rawBody) {
471
- if (typeof rawBody === 'string') {
472
- try {
473
- const jsonBody = JSON.parse(rawBody);
474
-
475
- return JSON.stringify(obfuscator.obfuscateSensibleProps(jsonBody));
476
- // eslint-disable-next-line
477
- } catch (err) {}
478
- }
479
- return rawBody;
480
- }
481
-
482
- export default autoService(initWrapHandlerForGoogleHTTPFunction);