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