serverless-offline 8.8.1 → 9.1.1

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 (234) hide show
  1. package/README.md +112 -44
  2. package/package.json +33 -57
  3. package/src/ServerlessOffline.js +409 -0
  4. package/src/config/commandOptions.js +159 -0
  5. package/src/config/constants.js +22 -0
  6. package/{dist → src}/config/defaultOptions.js +9 -17
  7. package/src/config/index.js +4 -0
  8. package/src/config/supportedRuntimes.js +47 -0
  9. package/src/events/authCanExecuteResource.js +35 -0
  10. package/src/events/authFunctionNameExtractor.js +75 -0
  11. package/src/events/authMatchPolicyResource.js +71 -0
  12. package/src/events/authValidateContext.js +51 -0
  13. package/src/events/http/Endpoint.js +135 -0
  14. package/src/events/http/Http.js +50 -0
  15. package/src/events/http/HttpEventDefinition.js +20 -0
  16. package/src/events/http/HttpServer.js +1242 -0
  17. package/src/events/http/OfflineEndpoint.js +33 -0
  18. package/src/events/http/authJWTSettingsExtractor.js +70 -0
  19. package/src/events/http/createAuthScheme.js +176 -0
  20. package/src/events/http/createJWTAuthScheme.js +106 -0
  21. package/src/events/http/index.js +1 -0
  22. package/src/events/http/javaHelpers.js +102 -0
  23. package/src/events/http/lambda-events/LambdaIntegrationEvent.js +57 -0
  24. package/src/events/http/lambda-events/LambdaProxyIntegrationEvent.js +233 -0
  25. package/src/events/http/lambda-events/LambdaProxyIntegrationEventV2.js +190 -0
  26. package/src/events/http/lambda-events/VelocityContext.js +147 -0
  27. package/src/events/http/lambda-events/index.js +4 -0
  28. package/src/events/http/lambda-events/renderVelocityTemplateObject.js +93 -0
  29. package/{dist → src}/events/http/parseResources.js +73 -78
  30. package/src/events/http/payloadSchemaValidator.js +13 -0
  31. package/{dist → src}/events/http/templates/offline-default.req.vm +0 -0
  32. package/{dist → src}/events/http/templates/offline-default.res.vm +0 -0
  33. package/src/events/schedule/Schedule.js +131 -0
  34. package/src/events/schedule/ScheduleEvent.js +18 -0
  35. package/src/events/schedule/ScheduleEventDefinition.js +21 -0
  36. package/src/events/schedule/index.js +1 -0
  37. package/src/events/websocket/HttpServer.js +69 -0
  38. package/src/events/websocket/WebSocket.js +52 -0
  39. package/src/events/websocket/WebSocketClients.js +462 -0
  40. package/src/events/websocket/WebSocketEventDefinition.js +18 -0
  41. package/src/events/websocket/WebSocketServer.js +73 -0
  42. package/src/events/websocket/http-routes/_catchAll/catchAllRoute.js +16 -0
  43. package/src/events/websocket/http-routes/_catchAll/index.js +1 -0
  44. package/src/events/websocket/http-routes/connections/ConnectionsController.js +28 -0
  45. package/src/events/websocket/http-routes/connections/connectionsRoutes.js +70 -0
  46. package/src/events/websocket/http-routes/connections/index.js +1 -0
  47. package/src/events/websocket/http-routes/index.js +2 -0
  48. package/src/events/websocket/index.js +1 -0
  49. package/src/events/websocket/lambda-events/WebSocketAuthorizerEvent.js +65 -0
  50. package/src/events/websocket/lambda-events/WebSocketConnectEvent.js +68 -0
  51. package/src/events/websocket/lambda-events/WebSocketDisconnectEvent.js +31 -0
  52. package/src/events/websocket/lambda-events/WebSocketEvent.js +29 -0
  53. package/src/events/websocket/lambda-events/WebSocketRequestContext.js +67 -0
  54. package/src/events/websocket/lambda-events/index.js +4 -0
  55. package/src/index.js +12 -0
  56. package/src/lambda/HttpServer.js +108 -0
  57. package/src/lambda/Lambda.js +68 -0
  58. package/src/lambda/LambdaContext.js +33 -0
  59. package/src/lambda/LambdaFunction.js +309 -0
  60. package/src/lambda/LambdaFunctionPool.js +109 -0
  61. package/src/lambda/__tests__/LambdaContext.test.js +30 -0
  62. package/src/lambda/__tests__/LambdaFunction.test.js +196 -0
  63. package/src/lambda/__tests__/fixtures/Lambda/LambdaFunctionThatReturnsJSONObject.fixture.js +47 -0
  64. package/src/lambda/__tests__/fixtures/Lambda/LambdaFunctionThatReturnsNativeString.fixture.js +46 -0
  65. package/src/lambda/__tests__/fixtures/Lambda/package.json +3 -0
  66. package/src/lambda/__tests__/fixtures/lambdaFunction.fixture.js +145 -0
  67. package/src/lambda/__tests__/fixtures/package.json +3 -0
  68. package/src/lambda/__tests__/routes/invocations/InvocationsController.test.js +42 -0
  69. package/src/lambda/handler-runner/HandlerRunner.js +136 -0
  70. package/src/lambda/handler-runner/child-process-runner/ChildProcessRunner.js +76 -0
  71. package/src/lambda/handler-runner/child-process-runner/childProcessHelper.js +42 -0
  72. package/src/lambda/handler-runner/child-process-runner/index.js +1 -0
  73. package/src/lambda/handler-runner/docker-runner/DockerContainer.js +419 -0
  74. package/src/lambda/handler-runner/docker-runner/DockerImage.js +35 -0
  75. package/src/lambda/handler-runner/docker-runner/DockerRunner.js +63 -0
  76. package/src/lambda/handler-runner/docker-runner/index.js +1 -0
  77. package/src/lambda/handler-runner/go-runner/GoRunner.js +172 -0
  78. package/src/lambda/handler-runner/go-runner/index.js +1 -0
  79. package/src/lambda/handler-runner/in-process-runner/InProcessRunner.js +126 -0
  80. package/src/lambda/handler-runner/in-process-runner/index.js +1 -0
  81. package/src/lambda/handler-runner/index.js +1 -0
  82. package/src/lambda/handler-runner/java-runner/JavaRunner.js +114 -0
  83. package/src/lambda/handler-runner/java-runner/index.js +1 -0
  84. package/src/lambda/handler-runner/python-runner/PythonRunner.js +138 -0
  85. package/src/lambda/handler-runner/python-runner/index.js +1 -0
  86. package/{dist → src}/lambda/handler-runner/python-runner/invoke.py +0 -0
  87. package/src/lambda/handler-runner/ruby-runner/RubyRunner.js +107 -0
  88. package/src/lambda/handler-runner/ruby-runner/index.js +1 -0
  89. package/{dist → src}/lambda/handler-runner/ruby-runner/invoke.rb +0 -0
  90. package/src/lambda/handler-runner/worker-thread-runner/WorkerThreadRunner.js +70 -0
  91. package/src/lambda/handler-runner/worker-thread-runner/index.js +1 -0
  92. package/src/lambda/handler-runner/worker-thread-runner/workerThreadHelper.js +29 -0
  93. package/src/lambda/index.js +1 -0
  94. package/src/lambda/routes/index.js +2 -0
  95. package/src/lambda/routes/invocations/InvocationsController.js +102 -0
  96. package/src/lambda/routes/invocations/index.js +1 -0
  97. package/src/lambda/routes/invocations/invocationsRoute.js +77 -0
  98. package/src/lambda/routes/invoke-async/InvokeAsyncController.js +20 -0
  99. package/src/lambda/routes/invoke-async/index.js +1 -0
  100. package/src/lambda/routes/invoke-async/invokeAsyncRoute.js +33 -0
  101. package/src/utils/__tests__/createUniqueId.test.js +18 -0
  102. package/src/utils/__tests__/formatToClfTime.test.js +14 -0
  103. package/src/utils/__tests__/generateHapiPath.test.js +46 -0
  104. package/src/utils/__tests__/lowerCaseKeys.test.js +30 -0
  105. package/src/utils/__tests__/parseHeaders.test.js +13 -0
  106. package/src/utils/__tests__/parseMultiValueHeaders.test.js +24 -0
  107. package/src/utils/__tests__/parseMultiValueQueryStringParameters.test.js +159 -0
  108. package/src/utils/__tests__/parseQueryStringParameters.test.js +15 -0
  109. package/src/utils/__tests__/splitHandlerPathAndName.test.js +54 -0
  110. package/src/utils/__tests__/unflatten.test.js +32 -0
  111. package/src/utils/checkDockerDaemon.js +19 -0
  112. package/src/utils/checkGoVersion.js +16 -0
  113. package/src/utils/createApiKey.js +5 -0
  114. package/src/utils/createUniqueId.js +5 -0
  115. package/src/utils/detectExecutable.js +11 -0
  116. package/{dist → src}/utils/formatToClfTime.js +6 -14
  117. package/src/utils/generateHapiPath.js +26 -0
  118. package/src/utils/getHttpApiCorsConfig.js +28 -0
  119. package/src/utils/index.js +42 -0
  120. package/src/utils/jsonPath.js +13 -0
  121. package/src/utils/logRoutes.js +64 -0
  122. package/src/utils/lowerCaseKeys.js +6 -0
  123. package/src/utils/parseHeaders.js +14 -0
  124. package/src/utils/parseMultiValueHeaders.js +27 -0
  125. package/src/utils/parseMultiValueQueryStringParameters.js +31 -0
  126. package/src/utils/parseQueryStringParameters.js +15 -0
  127. package/src/utils/splitHandlerPathAndName.js +31 -0
  128. package/src/utils/unflatten.js +11 -0
  129. package/CHANGELOG.md +0 -78
  130. package/dist/ServerlessOffline.js +0 -508
  131. package/dist/config/commandOptions.js +0 -149
  132. package/dist/config/constants.js +0 -30
  133. package/dist/config/index.js +0 -55
  134. package/dist/config/supportedRuntimes.js +0 -40
  135. package/dist/debugLog.js +0 -12
  136. package/dist/events/authCanExecuteResource.js +0 -35
  137. package/dist/events/authFunctionNameExtractor.js +0 -87
  138. package/dist/events/authMatchPolicyResource.js +0 -62
  139. package/dist/events/authValidateContext.js +0 -53
  140. package/dist/events/http/Endpoint.js +0 -173
  141. package/dist/events/http/Http.js +0 -77
  142. package/dist/events/http/HttpEventDefinition.js +0 -36
  143. package/dist/events/http/HttpServer.js +0 -1370
  144. package/dist/events/http/OfflineEndpoint.js +0 -38
  145. package/dist/events/http/authJWTSettingsExtractor.js +0 -76
  146. package/dist/events/http/createAuthScheme.js +0 -184
  147. package/dist/events/http/createJWTAuthScheme.js +0 -159
  148. package/dist/events/http/index.js +0 -15
  149. package/dist/events/http/javaHelpers.js +0 -99
  150. package/dist/events/http/lambda-events/LambdaIntegrationEvent.js +0 -87
  151. package/dist/events/http/lambda-events/LambdaProxyIntegrationEvent.js +0 -246
  152. package/dist/events/http/lambda-events/LambdaProxyIntegrationEventV2.js +0 -225
  153. package/dist/events/http/lambda-events/VelocityContext.js +0 -170
  154. package/dist/events/http/lambda-events/index.js +0 -39
  155. package/dist/events/http/lambda-events/renderVelocityTemplateObject.js +0 -111
  156. package/dist/events/http/payloadSchemaValidator.js +0 -13
  157. package/dist/events/schedule/Schedule.js +0 -183
  158. package/dist/events/schedule/ScheduleEvent.js +0 -27
  159. package/dist/events/schedule/ScheduleEventDefinition.js +0 -36
  160. package/dist/events/schedule/index.js +0 -15
  161. package/dist/events/websocket/HttpServer.js +0 -114
  162. package/dist/events/websocket/WebSocket.js +0 -78
  163. package/dist/events/websocket/WebSocketClients.js +0 -577
  164. package/dist/events/websocket/WebSocketEventDefinition.js +0 -32
  165. package/dist/events/websocket/WebSocketServer.js +0 -139
  166. package/dist/events/websocket/http-routes/_catchAll/catchAllRoute.js +0 -33
  167. package/dist/events/websocket/http-routes/_catchAll/index.js +0 -15
  168. package/dist/events/websocket/http-routes/connections/ConnectionsController.js +0 -45
  169. package/dist/events/websocket/http-routes/connections/connectionsRoutes.js +0 -95
  170. package/dist/events/websocket/http-routes/connections/index.js +0 -15
  171. package/dist/events/websocket/http-routes/index.js +0 -23
  172. package/dist/events/websocket/index.js +0 -15
  173. package/dist/events/websocket/lambda-events/WebSocketAuthorizerEvent.js +0 -99
  174. package/dist/events/websocket/lambda-events/WebSocketConnectEvent.js +0 -101
  175. package/dist/events/websocket/lambda-events/WebSocketDisconnectEvent.js +0 -47
  176. package/dist/events/websocket/lambda-events/WebSocketEvent.js +0 -54
  177. package/dist/events/websocket/lambda-events/WebSocketRequestContext.js +0 -98
  178. package/dist/events/websocket/lambda-events/index.js +0 -39
  179. package/dist/index.js +0 -15
  180. package/dist/lambda/HttpServer.js +0 -124
  181. package/dist/lambda/Lambda.js +0 -117
  182. package/dist/lambda/LambdaContext.js +0 -53
  183. package/dist/lambda/LambdaFunction.js +0 -390
  184. package/dist/lambda/LambdaFunctionPool.js +0 -127
  185. package/dist/lambda/handler-runner/HandlerRunner.js +0 -195
  186. package/dist/lambda/handler-runner/child-process-runner/ChildProcessRunner.js +0 -124
  187. package/dist/lambda/handler-runner/child-process-runner/childProcessHelper.js +0 -49
  188. package/dist/lambda/handler-runner/child-process-runner/index.js +0 -15
  189. package/dist/lambda/handler-runner/docker-runner/DockerContainer.js +0 -515
  190. package/dist/lambda/handler-runner/docker-runner/DockerImage.js +0 -67
  191. package/dist/lambda/handler-runner/docker-runner/DockerRunner.js +0 -74
  192. package/dist/lambda/handler-runner/docker-runner/index.js +0 -15
  193. package/dist/lambda/handler-runner/go-runner/GoRunner.js +0 -230
  194. package/dist/lambda/handler-runner/go-runner/index.js +0 -15
  195. package/dist/lambda/handler-runner/in-process-runner/InProcessRunner.js +0 -228
  196. package/dist/lambda/handler-runner/in-process-runner/index.js +0 -15
  197. package/dist/lambda/handler-runner/index.js +0 -15
  198. package/dist/lambda/handler-runner/java-runner/JavaRunner.js +0 -153
  199. package/dist/lambda/handler-runner/java-runner/index.js +0 -15
  200. package/dist/lambda/handler-runner/python-runner/PythonRunner.js +0 -185
  201. package/dist/lambda/handler-runner/python-runner/index.js +0 -15
  202. package/dist/lambda/handler-runner/ruby-runner/RubyRunner.js +0 -147
  203. package/dist/lambda/handler-runner/ruby-runner/index.js +0 -15
  204. package/dist/lambda/handler-runner/worker-thread-runner/WorkerThreadRunner.js +0 -92
  205. package/dist/lambda/handler-runner/worker-thread-runner/index.js +0 -15
  206. package/dist/lambda/handler-runner/worker-thread-runner/workerThreadHelper.js +0 -31
  207. package/dist/lambda/index.js +0 -15
  208. package/dist/lambda/routes/index.js +0 -23
  209. package/dist/lambda/routes/invocations/InvocationsController.js +0 -142
  210. package/dist/lambda/routes/invocations/index.js +0 -15
  211. package/dist/lambda/routes/invocations/invocationsRoute.js +0 -90
  212. package/dist/lambda/routes/invoke-async/InvokeAsyncController.js +0 -38
  213. package/dist/lambda/routes/invoke-async/index.js +0 -15
  214. package/dist/lambda/routes/invoke-async/invokeAsyncRoute.js +0 -43
  215. package/dist/main.js +0 -11
  216. package/dist/serverlessLog.js +0 -91
  217. package/dist/utils/checkDockerDaemon.js +0 -27
  218. package/dist/utils/checkGoVersion.js +0 -27
  219. package/dist/utils/createApiKey.js +0 -12
  220. package/dist/utils/createUniqueId.js +0 -14
  221. package/dist/utils/detectExecutable.js +0 -21
  222. package/dist/utils/generateHapiPath.js +0 -28
  223. package/dist/utils/getHttpApiCorsConfig.js +0 -40
  224. package/dist/utils/index.js +0 -165
  225. package/dist/utils/jsonPath.js +0 -21
  226. package/dist/utils/lowerCaseKeys.js +0 -14
  227. package/dist/utils/parseHeaders.js +0 -23
  228. package/dist/utils/parseMultiValueHeaders.js +0 -36
  229. package/dist/utils/parseMultiValueQueryStringParameters.js +0 -40
  230. package/dist/utils/parseQueryStringParameters.js +0 -26
  231. package/dist/utils/resolveJoins.js +0 -36
  232. package/dist/utils/satisfiesVersionRange.js +0 -20
  233. package/dist/utils/splitHandlerPathAndName.js +0 -37
  234. package/dist/utils/unflatten.js +0 -18
@@ -0,0 +1,409 @@
1
+ import process, { exit } from 'node:process'
2
+ import { log } from '@serverless/utils/log.js'
3
+ import chalk from 'chalk'
4
+ import {
5
+ commandOptions,
6
+ CUSTOM_OPTION,
7
+ defaultOptions,
8
+ SERVER_SHUTDOWN_TIMEOUT,
9
+ } from './config/index.js'
10
+
11
+ export default class ServerlessOffline {
12
+ #cliOptions = null
13
+
14
+ #http = null
15
+
16
+ #lambda = null
17
+
18
+ #options = null
19
+
20
+ #schedule = null
21
+
22
+ #serverless = null
23
+
24
+ #webSocket = null
25
+
26
+ constructor(serverless, cliOptions) {
27
+ this.#cliOptions = cliOptions
28
+ this.#serverless = serverless
29
+
30
+ this.commands = {
31
+ offline: {
32
+ // add start nested options
33
+ commands: {
34
+ functionsUpdated: {
35
+ lifecycleEvents: ['cleanup'],
36
+ type: 'entrypoint',
37
+ },
38
+ start: {
39
+ lifecycleEvents: ['init', 'ready', 'end'],
40
+ options: commandOptions,
41
+ usage:
42
+ 'Simulates API Gateway to call your lambda functions offline using backward compatible initialization.',
43
+ },
44
+ },
45
+ lifecycleEvents: ['start'],
46
+ options: commandOptions,
47
+ usage: 'Simulates API Gateway to call your lambda functions offline.',
48
+ },
49
+ }
50
+
51
+ this.hooks = {
52
+ 'offline:functionsUpdated:cleanup': this.#cleanupFunctions.bind(this),
53
+ 'offline:start': this.#startWithExplicitEnd.bind(this),
54
+ 'offline:start:end': this.end.bind(this),
55
+ 'offline:start:init': this.start.bind(this),
56
+ 'offline:start:ready': this.#ready.bind(this),
57
+ }
58
+ }
59
+
60
+ // Entry point for the plugin (sls offline) when running 'sls offline start'
61
+ async start() {
62
+ this.#mergeOptions()
63
+
64
+ const { httpEvents, lambdas, scheduleEvents, webSocketEvents } =
65
+ this.#getEvents()
66
+
67
+ // if (lambdas.length > 0) {
68
+ await this.#createLambda(lambdas)
69
+ // }
70
+
71
+ const eventModules = []
72
+
73
+ if (httpEvents.length > 0) {
74
+ eventModules.push(this.#createHttp(httpEvents))
75
+ }
76
+
77
+ if (!this.#options.disableScheduledEvents && scheduleEvents.length > 0) {
78
+ eventModules.push(this.#createSchedule(scheduleEvents))
79
+ }
80
+
81
+ if (webSocketEvents.length > 0) {
82
+ eventModules.push(this.#createWebSocket(webSocketEvents))
83
+ }
84
+
85
+ await Promise.all(eventModules)
86
+ }
87
+
88
+ async #ready() {
89
+ await this.#listenForTermination()
90
+ }
91
+
92
+ async end(skipExit) {
93
+ log.info('Halting offline server')
94
+
95
+ const eventModules = []
96
+
97
+ if (this.#lambda) {
98
+ eventModules.push(this.#lambda.cleanup())
99
+ eventModules.push(this.#lambda.stop(SERVER_SHUTDOWN_TIMEOUT))
100
+ }
101
+
102
+ if (this.#http) {
103
+ eventModules.push(this.#http.stop(SERVER_SHUTDOWN_TIMEOUT))
104
+ }
105
+
106
+ // if (this.#schedule) {
107
+ // eventModules.push(this.#schedule.stop())
108
+ // }
109
+
110
+ if (this.#webSocket) {
111
+ eventModules.push(this.#webSocket.stop(SERVER_SHUTDOWN_TIMEOUT))
112
+ }
113
+
114
+ await Promise.all(eventModules)
115
+
116
+ if (!skipExit) {
117
+ exit(0)
118
+ }
119
+ }
120
+
121
+ async #cleanupFunctions() {
122
+ if (this.#lambda) {
123
+ log.debug('Forcing cleanup of Lambda functions')
124
+ await this.#lambda.cleanup()
125
+ }
126
+ }
127
+
128
+ /**
129
+ * Entry point for the plugin (serverless offline) when running 'serverless offline'
130
+ * The call to this.end() would terminate the process before 'offline:start:end' could be consumed
131
+ * by downstream plugins. When running serverless offline that can be expected, but docs say that
132
+ * 'serverless offline start' will provide the init and end hooks for other plugins to consume
133
+ * */
134
+ async #startWithExplicitEnd() {
135
+ await this.start()
136
+ await this.#ready()
137
+ this.end()
138
+ }
139
+
140
+ async #listenForTermination() {
141
+ const command = await new Promise((resolve) => {
142
+ process
143
+ // SIGINT will be usually sent when user presses ctrl+c
144
+ .on('SIGINT', () => resolve('SIGINT'))
145
+ // SIGTERM is a default termination signal in many cases,
146
+ // for example when "killing" a subprocess spawned in node
147
+ // with child_process methods
148
+ .on('SIGTERM', () => resolve('SIGTERM'))
149
+ })
150
+
151
+ log.info(`Got ${command} signal. Offline Halting...`)
152
+ }
153
+
154
+ async #createLambda(lambdas, skipStart) {
155
+ const { default: Lambda } = await import('./lambda/index.js')
156
+
157
+ this.#lambda = new Lambda(this.#serverless, this.#options)
158
+
159
+ this.#lambda.create(lambdas)
160
+
161
+ if (!skipStart) {
162
+ await this.#lambda.start()
163
+ }
164
+ }
165
+
166
+ async #createHttp(events, skipStart) {
167
+ const { default: Http } = await import('./events/http/index.js')
168
+
169
+ this.#http = new Http(this.#serverless, this.#options, this.#lambda)
170
+
171
+ await this.#http.registerPlugins()
172
+
173
+ this.#http.create(events)
174
+
175
+ // HTTP Proxy defined in Resource
176
+ this.#http.createResourceRoutes()
177
+
178
+ // Not found handling
179
+ // we have to create the 404 routes last, otherwise we could have
180
+ // collisions with catch all routes, e.g. any (proxy+}
181
+ this.#http.create404Route()
182
+
183
+ if (!skipStart) {
184
+ await this.#http.start()
185
+ }
186
+ }
187
+
188
+ async #createSchedule(events) {
189
+ const { default: Schedule } = await import('./events/schedule/index.js')
190
+
191
+ this.#schedule = new Schedule(
192
+ this.#lambda,
193
+ this.#serverless.service.provider.region,
194
+ )
195
+
196
+ this.#schedule.create(events)
197
+ }
198
+
199
+ async #createWebSocket(events) {
200
+ const { default: WebSocket } = await import('./events/websocket/index.js')
201
+
202
+ this.#webSocket = new WebSocket(
203
+ this.#serverless,
204
+ this.#options,
205
+ this.#lambda,
206
+ )
207
+
208
+ this.#webSocket.create(events)
209
+
210
+ return this.#webSocket.start()
211
+ }
212
+
213
+ #mergeOptions() {
214
+ const {
215
+ service: { custom = {}, provider },
216
+ } = this.#serverless
217
+
218
+ const customOptions = custom[CUSTOM_OPTION]
219
+
220
+ // merge options
221
+ // order of Precedence: command line options, custom options, defaults.
222
+ this.#options = {
223
+ ...defaultOptions,
224
+ ...customOptions,
225
+ ...this.#cliOptions,
226
+ }
227
+
228
+ // Parse CORS options
229
+ this.#options.corsAllowHeaders = this.#options.corsAllowHeaders
230
+ .replace(/\s/g, '')
231
+ .split(',')
232
+ this.#options.corsAllowOrigin = this.#options.corsAllowOrigin
233
+ .replace(/\s/g, '')
234
+ .split(',')
235
+ this.#options.corsExposedHeaders = this.#options.corsExposedHeaders
236
+ .replace(/\s/g, '')
237
+ .split(',')
238
+
239
+ if (this.#options.corsDisallowCredentials) {
240
+ this.#options.corsAllowCredentials = false
241
+ }
242
+
243
+ this.#options.corsConfig = {
244
+ credentials: this.#options.corsAllowCredentials,
245
+ exposedHeaders: this.#options.corsExposedHeaders,
246
+ headers: this.#options.corsAllowHeaders,
247
+ origin: this.#options.corsAllowOrigin,
248
+ }
249
+
250
+ log.notice()
251
+ log.notice(
252
+ `Starting Offline at stage ${provider.stage} ${chalk.gray(
253
+ `(${provider.region})`,
254
+ )}`,
255
+ )
256
+ log.notice()
257
+ log.debug('options:', this.#options)
258
+ }
259
+
260
+ #getEvents() {
261
+ const { service } = this.#serverless
262
+
263
+ const httpEvents = []
264
+ const lambdas = []
265
+ const scheduleEvents = []
266
+ const webSocketEvents = []
267
+
268
+ const functionKeys = service.getAllFunctions()
269
+
270
+ let hasPrivateHttpEvent = false
271
+
272
+ functionKeys.forEach((functionKey) => {
273
+ const functionDefinition = service.getFunction(functionKey)
274
+
275
+ lambdas.push({ functionDefinition, functionKey })
276
+
277
+ const events = service.getAllEventsInFunction(functionKey) || []
278
+
279
+ events.forEach((event) => {
280
+ const { http, httpApi, schedule, websocket } = event
281
+
282
+ if ((http || httpApi) && functionDefinition.handler) {
283
+ const httpEvent = {
284
+ functionKey,
285
+ handler: functionDefinition.handler,
286
+ http: http || httpApi,
287
+ }
288
+
289
+ if (httpApi) {
290
+ // Ensure definitions for 'httpApi' events are objects so that they can be marked
291
+ // with an 'isHttpApi' property (they are handled differently to 'http' events)
292
+ if (typeof httpEvent.http === 'string') {
293
+ httpEvent.http = {
294
+ routeKey: httpEvent.http === '*' ? '$default' : httpEvent.http,
295
+ }
296
+ } else if (typeof httpEvent.http === 'object') {
297
+ if (!httpEvent.http.method) {
298
+ log.warning(
299
+ `Event definition is missing a method for function "${functionKey}"`,
300
+ )
301
+ httpEvent.http.method = ''
302
+ }
303
+ if (
304
+ httpEvent.http.method === '*' &&
305
+ httpEvent.http.path === '*'
306
+ ) {
307
+ httpEvent.http.routeKey = '$default'
308
+ } else {
309
+ const resolvedMethod =
310
+ httpEvent.http.method === '*'
311
+ ? 'ANY'
312
+ : httpEvent.http.method.toUpperCase()
313
+ httpEvent.http.routeKey = `${resolvedMethod} ${httpEvent.http.path}`
314
+ }
315
+ // Clear these properties to avoid confusion (they will be derived from the routeKey
316
+ // when needed later)
317
+ delete httpEvent.http.method
318
+ delete httpEvent.http.path
319
+ } else {
320
+ log.warning(
321
+ `Event definition must be a string or object but received ${typeof httpEvent.http} for function "${functionKey}"`,
322
+ )
323
+ httpEvent.http.routeKey = ''
324
+ }
325
+
326
+ httpEvent.http.isHttpApi = true
327
+ if (
328
+ functionDefinition.httpApi &&
329
+ functionDefinition.httpApi.payload
330
+ ) {
331
+ httpEvent.http.payload = functionDefinition.httpApi.payload
332
+ } else {
333
+ httpEvent.http.payload =
334
+ service.provider.httpApi && service.provider.httpApi.payload
335
+ ? service.provider.httpApi.payload
336
+ : '2.0'
337
+ }
338
+ }
339
+
340
+ if (http && http.private) {
341
+ hasPrivateHttpEvent = true
342
+ }
343
+
344
+ httpEvents.push(httpEvent)
345
+ }
346
+
347
+ if (schedule) {
348
+ scheduleEvents.push({
349
+ functionKey,
350
+ schedule,
351
+ })
352
+ }
353
+
354
+ if (websocket) {
355
+ webSocketEvents.push({
356
+ functionKey,
357
+ websocket,
358
+ })
359
+ }
360
+ })
361
+ })
362
+
363
+ // for simple API Key authentication model
364
+ if (hasPrivateHttpEvent) {
365
+ log.notice(`Key with token: ${this.#options.apiKey}`)
366
+
367
+ if (this.#options.noAuth) {
368
+ log.notice(
369
+ 'Authorizers are turned off. You do not need to use x-api-key header.',
370
+ )
371
+ } else {
372
+ log.notice('Remember to use x-api-key on the request headers')
373
+ }
374
+ }
375
+
376
+ return {
377
+ httpEvents,
378
+ lambdas,
379
+ scheduleEvents,
380
+ webSocketEvents,
381
+ }
382
+ }
383
+
384
+ // TODO FIXME
385
+ // TEMP quick fix to expose for testing, look for better solution
386
+ internals() {
387
+ return {
388
+ createHttp: (events, skipStart) => {
389
+ return this.#createHttp(events, skipStart)
390
+ },
391
+
392
+ createLambda: (lambdas, skipStart) => {
393
+ return this.#createLambda(lambdas, skipStart)
394
+ },
395
+
396
+ getApiGatewayServer: () => {
397
+ return this.#http.getServer()
398
+ },
399
+
400
+ getEvents: () => {
401
+ return this.#getEvents()
402
+ },
403
+
404
+ mergeOptions: () => {
405
+ this.#mergeOptions()
406
+ },
407
+ }
408
+ }
409
+ }
@@ -0,0 +1,159 @@
1
+ export default {
2
+ apiKey: {
3
+ type: 'string',
4
+ usage:
5
+ 'Defines the API key value to be used for endpoints marked as private. Defaults to a random hash.',
6
+ },
7
+ corsAllowHeaders: {
8
+ type: 'string',
9
+ usage:
10
+ 'Used to build the Access-Control-Allow-Headers header for CORS support.',
11
+ },
12
+ corsAllowOrigin: {
13
+ type: 'string',
14
+ usage:
15
+ 'Used to build the Access-Control-Allow-Origin header for CORS support.',
16
+ },
17
+ corsDisallowCredentials: {
18
+ type: 'boolean',
19
+ usage:
20
+ 'Used to override the Access-Control-Allow-Credentials default (which is true) to false.',
21
+ },
22
+ corsExposedHeaders: {
23
+ type: 'string',
24
+ usage:
25
+ 'Used to build the Access-Control-Exposed-Headers response header for CORS support',
26
+ },
27
+ disableCookieValidation: {
28
+ type: 'boolean',
29
+ usage: 'Used to disable cookie-validation on hapi.js-server',
30
+ },
31
+ disableScheduledEvents: {
32
+ type: 'boolean',
33
+ usage:
34
+ 'Disables all scheduled events. Overrides configurations in serverless.yml. Default: false',
35
+ },
36
+ dockerHost: {
37
+ type: 'string',
38
+ usage: 'The host name of Docker. Default: localhost',
39
+ },
40
+ dockerHostServicePath: {
41
+ type: 'string',
42
+ usage:
43
+ 'Defines service path which is used by SLS running inside Docker container',
44
+ },
45
+ dockerNetwork: {
46
+ type: 'string',
47
+ usage: 'The network that the Docker container will connect to',
48
+ },
49
+ dockerReadOnly: {
50
+ type: 'boolean',
51
+ usage: 'Marks if the docker code layer should be read only. Default: true',
52
+ },
53
+ enforceSecureCookies: {
54
+ type: 'boolean',
55
+ usage: 'Enforce secure cookies',
56
+ },
57
+ functionCleanupIdleTimeSeconds: {
58
+ type: 'string',
59
+ usage: 'Number of seconds until an idle function is eligible for cleanup',
60
+ },
61
+ hideStackTraces: {
62
+ type: 'boolean',
63
+ usage: 'Hide the stack trace on lambda failure. Default: false',
64
+ },
65
+ host: {
66
+ shortcut: 'o',
67
+ type: 'string',
68
+ usage: 'The host name to listen on. Default: localhost',
69
+ },
70
+ httpPort: {
71
+ type: 'string',
72
+ usage: 'HTTP port to listen on. Default: 3000',
73
+ },
74
+ httpsProtocol: {
75
+ shortcut: 'H',
76
+ type: 'string',
77
+ usage:
78
+ 'To enable HTTPS, specify directory (relative to your cwd, typically your project dir) for both cert.pem and key.pem files.',
79
+ },
80
+ ignoreJWTSignature: {
81
+ type: 'boolean',
82
+ usage:
83
+ "When using HttpApi with a JWT authorizer, don't check the signature of the JWT token. This should only be used for local development.",
84
+ },
85
+ lambdaPort: {
86
+ type: 'string',
87
+ usage: 'Lambda http port to listen on. Default: 3002',
88
+ },
89
+ layersDir: {
90
+ type: 'string',
91
+ usage:
92
+ 'The directory layers should be stored in. Default: {codeDir}/.serverless-offline/layers',
93
+ },
94
+ localEnvironment: {
95
+ type: 'boolean',
96
+ usage: 'Copy local environment variables. Default: false',
97
+ },
98
+ noAuth: {
99
+ type: 'boolean',
100
+ usage: 'Turns off all authorizers',
101
+ },
102
+ noPrependStageInUrl: {
103
+ type: 'boolean',
104
+ usage: "Don't prepend http routes with the stage.",
105
+ },
106
+ noStripTrailingSlashInUrl: {
107
+ type: 'boolean',
108
+ usage: "Don't strip trailing slash from http routes.",
109
+ },
110
+ noTimeout: {
111
+ shortcut: 't',
112
+ type: 'boolean',
113
+ usage: 'Disables the timeout feature.',
114
+ },
115
+ prefix: {
116
+ shortcut: 'p',
117
+ type: 'string',
118
+ usage:
119
+ 'Adds a prefix to every path, to send your requests to http://localhost:3000/prefix/[your_path] instead.',
120
+ },
121
+ printOutput: {
122
+ type: 'boolean',
123
+ usage: 'Outputs your lambda response to the terminal.',
124
+ },
125
+ reloadHandler: {
126
+ type: 'boolean',
127
+ usage: 'Reloads handler with each request.',
128
+ },
129
+ resourceRoutes: {
130
+ type: 'boolean',
131
+ usage: 'Turns on loading of your HTTP proxy settings from serverless.yml.',
132
+ },
133
+ useChildProcesses: {
134
+ type: 'boolean',
135
+ usage: 'Use separate node process to run handlers',
136
+ },
137
+ useDocker: {
138
+ type: 'boolean',
139
+ usage: 'Uses docker for node/python/ruby/provided',
140
+ },
141
+ useInProcess: {
142
+ type: 'boolean',
143
+ usage: "Run handlers in the same process as 'serverless-offline'",
144
+ },
145
+ webSocketHardTimeout: {
146
+ type: 'string',
147
+ usage:
148
+ 'Set WebSocket hard timeout in seconds to reproduce AWS limits (https://docs.aws.amazon.com/apigateway/latest/developerguide/limits.html#apigateway-execution-service-websocket-limits-table). Default: 7200 (2 hours)',
149
+ },
150
+ webSocketIdleTimeout: {
151
+ type: 'string',
152
+ usage:
153
+ 'Set WebSocket idle timeout in seconds to reproduce AWS limits (https://docs.aws.amazon.com/apigateway/latest/developerguide/limits.html#apigateway-execution-service-websocket-limits-table). Default: 600 (10 minutes)',
154
+ },
155
+ websocketPort: {
156
+ type: 'string',
157
+ usage: 'Websocket port to listen on. Default: 3001',
158
+ },
159
+ }
@@ -0,0 +1,22 @@
1
+ // dummy placeholder url for the WHATWG URL constructor
2
+ // https://github.com/nodejs/node/issues/12682
3
+ export const BASE_URL_PLACEHOLDER = 'http://example'
4
+
5
+ export const CUSTOM_OPTION = 'serverless-offline'
6
+
7
+ export const DEFAULT_LAMBDA_RUNTIME = 'nodejs14.x'
8
+
9
+ // https://docs.aws.amazon.com/lambda/latest/dg/limits.html
10
+ export const DEFAULT_LAMBDA_MEMORY_SIZE = 1024
11
+ // default function timeout in seconds
12
+ export const DEFAULT_LAMBDA_TIMEOUT = 6 // 6 seconds
13
+
14
+ // timeout for all connections to be closed
15
+ export const SERVER_SHUTDOWN_TIMEOUT = 5000
16
+
17
+ export const DEFAULT_WEBSOCKETS_API_ROUTE_SELECTION_EXPRESSION =
18
+ '$request.body.action'
19
+
20
+ export const DEFAULT_WEBSOCKETS_ROUTE = '$default'
21
+
22
+ export const DEFAULT_DOCKER_CONTAINER_PORT = 9001
@@ -1,17 +1,8 @@
1
- "use strict";
1
+ import { createApiKey } from '../utils/index.js'
2
2
 
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
-
8
- var _index = require("../utils/index.js");
9
-
10
- var _default = {
11
- allowCache: false,
12
- apiKey: (0, _index.createApiKey)(),
13
- corsAllowCredentials: true,
14
- // TODO no CLI option
3
+ export default {
4
+ apiKey: createApiKey(),
5
+ corsAllowCredentials: true, // TODO no CLI option
15
6
  corsAllowHeaders: 'accept,content-type,x-api-key,authorization',
16
7
  corsAllowOrigin: '*',
17
8
  corsExposedHeaders: 'WWW-Authenticate,Server-Authorization',
@@ -29,18 +20,19 @@ var _default = {
29
20
  httpsProtocol: '',
30
21
  lambdaPort: 3002,
31
22
  layersDir: null,
23
+ localEnvironment: false,
32
24
  noAuth: false,
33
25
  noPrependStageInUrl: false,
34
26
  noStripTrailingSlashInUrl: false,
35
27
  noTimeout: false,
36
28
  prefix: '',
37
29
  printOutput: false,
30
+ reloadHandler: false,
38
31
  resourceRoutes: false,
39
32
  useChildProcesses: false,
40
33
  useDocker: false,
41
- useWorkerThreads: false,
34
+ useInProcess: false,
42
35
  webSocketHardTimeout: 7200,
43
36
  webSocketIdleTimeout: 600,
44
- websocketPort: 3001
45
- };
46
- exports.default = _default;
37
+ websocketPort: 3001,
38
+ }
@@ -0,0 +1,4 @@
1
+ export { default as commandOptions } from './commandOptions.js'
2
+ export * from './constants.js'
3
+ export { default as defaultOptions } from './defaultOptions.js'
4
+ export * from './supportedRuntimes.js'
@@ -0,0 +1,47 @@
1
+ // native runtime support for AWS
2
+ // https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtimes.html
3
+
4
+ // .NET CORE
5
+ export const supportedDotnetcore = new Set([
6
+ // 'dotnet6',
7
+ // 'dotnetcore3.1',
8
+ ])
9
+
10
+ // GO
11
+ export const supportedGo = new Set(['go1.x'])
12
+
13
+ // JAVA
14
+ export const supportedJava = new Set(['java8', 'java8.al2', 'java11'])
15
+
16
+ // NODE.JS
17
+ export const supportedNodejs = new Set([
18
+ 'nodejs12.x',
19
+ 'nodejs14.x',
20
+ 'nodejs16.x',
21
+ ])
22
+
23
+ // PROVIDED
24
+ export const supportedProvided = new Set(['provided', 'provided.al2'])
25
+
26
+ // PYTHON
27
+ export const supportedPython = new Set([
28
+ 'python3.6',
29
+ 'python3.7',
30
+ 'python3.8',
31
+ 'python3.9',
32
+ ])
33
+
34
+ // RUBY
35
+ export const supportedRuby = new Set(['ruby2.7'])
36
+
37
+ // deprecated runtimes
38
+ // https://docs.aws.amazon.com/lambda/latest/dg/runtime-support-policy.html
39
+ export const supportedRuntimes = new Set([
40
+ ...supportedDotnetcore,
41
+ ...supportedGo,
42
+ ...supportedJava,
43
+ ...supportedNodejs,
44
+ ...supportedProvided,
45
+ ...supportedPython,
46
+ ...supportedRuby,
47
+ ])