-
Notifications
You must be signed in to change notification settings - Fork 670
/
Copy pathconfigure.js
121 lines (110 loc) · 4.04 KB
/
configure.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
const util = require('util')
const logger = require('./logger')
const { setCurrentInvoke } = require('./current-invoke')
const { getEventSource } = require('./event-sources')
const { getEventSourceNameBasedOnEvent } = require('./event-sources/utils')
const { getFramework } = require('./frameworks')
const makeResolver = require('./make-resolver')
const { forwardRequestToNodeServer, respondToEventSourceWithError } = require('./transport')
const { DEFAULT_BINARY_ENCODINGS, DEFAULT_BINARY_CONTENT_TYPES } = require('./constants')
function getDefaultBinarySettings (deprecatedBinaryMimeTypes) {
return {
contentTypes: deprecatedBinaryMimeTypes || DEFAULT_BINARY_CONTENT_TYPES,
contentEncodings: DEFAULT_BINARY_ENCODINGS
}
}
function configure ({
app: configureApp,
logSettings,
log: configureLog = logger(logSettings),
framework: configureFramework = getFramework({ app: configureApp, log: configureLog }),
binaryMimeTypes: configureBinaryMimeTypes,
binarySettings: configureBinarySettings,
resolutionMode: configureResolutionMode = 'PROMISE',
eventSourceName: configureEventSourceName,
eventSource: configureEventFns,
eventSourceRoutes: configureEventSourceRoutes,
respondWithErrors: configureRespondWithErrors = process.env.NODE_ENV === 'development'
} = {}) {
function proxy ({
app = configureApp,
framework = configureFramework,
event = {},
context = {},
callback = null,
resolutionMode = configureResolutionMode,
eventSourceName = configureEventSourceName || getEventSourceNameBasedOnEvent({ event }),
binaryMimeTypes = configureBinaryMimeTypes,
binarySettings = configureBinarySettings || getDefaultBinarySettings(binaryMimeTypes),
eventSource = configureEventFns || getEventSource({ eventSourceName }),
eventSourceRoutes = configureEventSourceRoutes || {},
log = configureLog,
respondWithErrors = configureRespondWithErrors
}) {
log.debug('SERVERLESS_EXPRESS:PROXY', () => ({
event: util.inspect(event, { depth: null }),
context: util.inspect(context, { depth: null }),
resolutionMode,
eventSourceName,
binarySettings,
respondWithErrors
}))
if (binaryMimeTypes) {
console.warn('[DEPRECATION NOTICE] { binaryMimeTypes: [] } is deprecated. base64 encoding is now automatically determined based on response content-type and content-encoding. If you need to manually set binary content types, instead, use { binarySettings: { contentTypes: [] } }')
}
setCurrentInvoke({ event, context })
return new Promise((resolve, reject) => {
const promise = {
resolve,
reject
}
const resolver = makeResolver({
context,
callback,
promise,
resolutionMode
})
try {
forwardRequestToNodeServer({
app,
framework,
event,
context,
resolver,
eventSourceName,
binarySettings,
eventSource,
eventSourceRoutes,
log
})
} catch (error) {
respondToEventSourceWithError({
error,
resolver,
log,
respondWithErrors,
eventSourceName,
eventSource
})
}
})
}
function handler (event, context, callback) {
return proxy({
event,
context,
callback
})
}
handler.handler = (...params) => {
console.warn('[DEPRECATION NOTICE] You\'re using the deprecated `serverlessExpress({...}).handler({...})` method. This will be removed in a future version of @codegenie/serverless-express. Instead, simply return `serverlessExpress({...})` as your handler.')
return handler(...params)
}
handler.proxy = (...params) => {
console.warn('[DEPRECATION NOTICE] You\'re using the deprecated `serverlessExpress({...}).proxy({...})` method. This will be removed in a future version of @codegenie/serverless-express. Instead, simply return `serverlessExpress({...})` as your handler.')
return proxy(...params)
}
handler.log = configureLog
return handler
}
module.exports = configure