@@ -26,9 +26,9 @@ import * as express from 'express';
26
26
import * as http from 'http' ;
27
27
import * as onFinished from 'on-finished' ;
28
28
29
- // HTTP header field that is added to Worker response to signalize problems with
30
- // executing the client function.
31
- const FUNCTION_STATUS_HEADER_FIELD = 'X-Google-Status ';
29
+ import { FUNCTION_STATUS_HEADER_FIELD } from './types' ;
30
+ import { logAndSendError } from './logger' ;
31
+ import { isBinaryCloudEvent } from './cloudevents ';
32
32
33
33
/**
34
34
* The Cloud Functions context object for the event.
@@ -267,34 +267,6 @@ function sendResponse(result: any, err: Error | null, res: express.Response) {
267
267
}
268
268
}
269
269
270
- /**
271
- * Logs an error message and sends back an error response to the incoming
272
- * request.
273
- * @param err Error to be logged and sent.
274
- * @param res Express response object.
275
- * @param callback A function to be called synchronously.
276
- */
277
- function logAndSendError (
278
- // tslint:disable-next-line:no-any
279
- err : Error | any ,
280
- res : express . Response | null ,
281
- callback ?: Function
282
- ) {
283
- console . error ( err . stack || err ) ;
284
-
285
- // If user function has already sent response headers, the response with
286
- // error message cannot be sent. This check is done inside the callback,
287
- // right before sending the response, to make sure that no concurrent
288
- // execution sends the response between the check and 'send' call below.
289
- if ( res && ! res . headersSent ) {
290
- res . set ( FUNCTION_STATUS_HEADER_FIELD , 'crash' ) ;
291
- res . send ( err . message || err ) ;
292
- }
293
- if ( callback ) {
294
- callback ( ) ;
295
- }
296
- }
297
-
298
270
// Set limit to a value larger than 32MB, which is maximum limit of higher level
299
271
// layers anyway.
300
272
const requestLimit = '1024mb' ;
@@ -315,31 +287,6 @@ function rawBodySaver(
315
287
req . rawBody = buf ;
316
288
}
317
289
318
- const defaultBodySavingOptions = {
319
- limit : requestLimit ,
320
- verify : rawBodySaver ,
321
- } ;
322
-
323
- const cloudEventsBodySavingOptions = {
324
- type : 'application/cloudevents+json' ,
325
- limit : requestLimit ,
326
- verify : rawBodySaver ,
327
- } ;
328
-
329
- // The parser will process ALL content types so must come last.
330
- const rawBodySavingOptions = {
331
- limit : requestLimit ,
332
- verify : rawBodySaver ,
333
- type : '*/*' ,
334
- } ;
335
-
336
- // Use extended query string parsing for URL-encoded bodies.
337
- const urlEncodedOptions = {
338
- limit : requestLimit ,
339
- verify : rawBodySaver ,
340
- extended : true ,
341
- } ;
342
-
343
290
/**
344
291
* Wraps the provided function into an Express handler function with additional
345
292
* instrumentation logic.
@@ -366,25 +313,6 @@ function makeHttpHandler(execute: HttpFunction): express.RequestHandler {
366
313
} ;
367
314
}
368
315
369
- /**
370
- * Checks whether the incoming request is a CloudEvents event in binary content
371
- * mode. This is verified by checking the presence of required headers.
372
- *
373
- * @link https://github.com/cloudevents/spec/blob/master/http-transport-binding.md#31-binary-content-mode
374
- *
375
- * @param req Express request object.
376
- * @return True if the request is a CloudEvents event in binary content mode,
377
- * false otherwise.
378
- */
379
- function isBinaryCloudEvent ( req : express . Request ) : boolean {
380
- return ! ! (
381
- req . header ( 'ce-type' ) &&
382
- req . header ( 'ce-specversion' ) &&
383
- req . header ( 'ce-source' ) &&
384
- req . header ( 'ce-id' )
385
- ) ;
386
- }
387
-
388
316
/**
389
317
* Returns a CloudEvents context from the given CloudEvents request. Context
390
318
* attributes are retrieved from request headers.
@@ -567,6 +495,30 @@ export function getServer(
567
495
next ( ) ;
568
496
} ) ;
569
497
498
+ const defaultBodySavingOptions = {
499
+ limit : requestLimit ,
500
+ verify : rawBodySaver ,
501
+ } ;
502
+ const cloudEventsBodySavingOptions = {
503
+ type : 'application/cloudevents+json' ,
504
+ limit : requestLimit ,
505
+ verify : rawBodySaver ,
506
+ } ;
507
+
508
+ // The parser will process ALL content types so must come last.
509
+ const rawBodySavingOptions = {
510
+ limit : requestLimit ,
511
+ verify : rawBodySaver ,
512
+ type : '*/*' ,
513
+ } ;
514
+
515
+ // Use extended query string parsing for URL-encoded bodies.
516
+ const urlEncodedOptions = {
517
+ limit : requestLimit ,
518
+ verify : rawBodySaver ,
519
+ extended : true ,
520
+ } ;
521
+
570
522
app . use ( bodyParser . json ( cloudEventsBodySavingOptions ) ) ;
571
523
app . use ( bodyParser . json ( defaultBodySavingOptions ) ) ;
572
524
app . use ( bodyParser . text ( defaultBodySavingOptions ) ) ;
0 commit comments