@@ -17,28 +17,29 @@ import * as express from 'express';
1717import * as http from 'http' ;
1818import * as onFinished from 'on-finished' ;
1919import { HandlerFunction , Request , Response } from './functions' ;
20- import { SignatureType } from './types' ;
2120import { setLatestRes } from './invoker' ;
2221import { legacyPubSubEventMiddleware } from './pubsub_middleware' ;
2322import { cloudEventToBackgroundEventMiddleware } from './middleware/cloud_event_to_background_event' ;
2423import { backgroundEventToCloudEventMiddleware } from './middleware/background_event_to_cloud_event' ;
24+ import { timeoutMiddleware } from './middleware/timeout' ;
2525import { wrapUserFunction } from './function_wrappers' ;
2626import { asyncLocalStorageMiddleware } from './async_local_storage' ;
2727import { executionContextMiddleware } from './execution_context' ;
2828import { errorHandler } from './logger' ;
29+ import { FrameworkOptions } from './options' ;
2930
3031/**
3132 * Creates and configures an Express application and returns an HTTP server
3233 * which will run it.
3334 * @param userFunction User's function.
34- * @param functionSignatureType Type of user's function signature .
35+ * @param options the configured Function Framework options .
3536 * @return HTTP server.
3637 */
3738export function getServer (
3839 userFunction : HandlerFunction ,
39- functionSignatureType : SignatureType ,
40- enableExecutionId : boolean
40+ options : FrameworkOptions
4141) : http . Server {
42+
4243 // App to use for function executions.
4344 const app = express ( ) ;
4445
@@ -89,7 +90,7 @@ export function getServer(
8990 } ;
9091
9192 // Apply middleware
92- if ( functionSignatureType !== 'typed' ) {
93+ if ( options . signatureType !== 'typed' ) {
9394 // If the function is not typed then JSON parsing can be done automatically, otherwise the
9495 // functions format must determine deserialization.
9596 app . use ( bodyParser . json ( cloudEventsBodySavingOptions ) ) ;
@@ -120,23 +121,23 @@ export function getServer(
120121 app . use ( asyncLocalStorageMiddleware ) ;
121122
122123 if (
123- functionSignatureType === 'event' ||
124- functionSignatureType === 'cloudevent'
124+ options . signatureType === 'event' ||
125+ options . signatureType === 'cloudevent'
125126 ) {
126127 // If a Pub/Sub subscription is configured to invoke a user's function directly, the request body
127128 // needs to be marshalled into the structure that wrapEventFunction expects. This unblocks local
128129 // development with the Pub/Sub emulator
129130 app . use ( legacyPubSubEventMiddleware ) ;
130131 }
131132
132- if ( functionSignatureType === 'event' ) {
133+ if ( options . signatureType === 'event' ) {
133134 app . use ( cloudEventToBackgroundEventMiddleware ) ;
134135 }
135- if ( functionSignatureType === 'cloudevent' ) {
136+ if ( options . signatureType === 'cloudevent' ) {
136137 app . use ( backgroundEventToCloudEventMiddleware ) ;
137138 }
138139
139- if ( functionSignatureType === 'http' ) {
140+ if ( options . signatureType === 'http' ) {
140141 app . use ( '/favicon.ico|/robots.txt' , ( req , res ) => {
141142 // Neither crawlers nor browsers attempting to pull the icon find the body
142143 // contents particularly useful, so we send nothing in the response body.
@@ -151,16 +152,18 @@ export function getServer(
151152 } ) ;
152153 }
153154
155+ app . use ( timeoutMiddleware ( options . timeoutMilliseconds ) ) ;
156+
154157 // Set up the routes for the user's function
155- const requestHandler = wrapUserFunction ( userFunction , functionSignatureType ) ;
156- if ( functionSignatureType === 'http' ) {
158+ const requestHandler = wrapUserFunction ( userFunction , options . signatureType ) ;
159+ if ( options . signatureType === 'http' ) {
157160 app . all ( '/*' , requestHandler ) ;
158161 } else {
159162 app . post ( '/*' , requestHandler ) ;
160163 }
161164
162165 // Error Handler
163- if ( enableExecutionId ) {
166+ if ( options . enableExecutionId ) {
164167 app . use ( errorHandler ) ;
165168 }
166169
0 commit comments