serverless-offline 8.7.0 → 9.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.
- package/README.md +91 -95
- package/package.json +41 -69
- package/src/ServerlessOffline.js +412 -0
- package/src/config/commandOptions.js +155 -0
- package/src/config/constants.js +22 -0
- package/{dist → src}/config/defaultOptions.js +8 -17
- package/src/config/index.js +4 -0
- package/src/config/supportedRuntimes.js +47 -0
- package/src/events/authCanExecuteResource.js +35 -0
- package/src/events/authFunctionNameExtractor.js +75 -0
- package/src/events/authMatchPolicyResource.js +71 -0
- package/src/events/authValidateContext.js +51 -0
- package/src/events/http/Endpoint.js +135 -0
- package/src/events/http/Http.js +50 -0
- package/src/events/http/HttpEventDefinition.js +20 -0
- package/src/events/http/HttpServer.js +1277 -0
- package/src/events/http/OfflineEndpoint.js +33 -0
- package/src/events/http/authJWTSettingsExtractor.js +70 -0
- package/src/events/http/createAuthScheme.js +176 -0
- package/src/events/http/createJWTAuthScheme.js +106 -0
- package/src/events/http/index.js +1 -0
- package/src/events/http/javaHelpers.js +102 -0
- package/src/events/http/lambda-events/LambdaIntegrationEvent.js +57 -0
- package/src/events/http/lambda-events/LambdaProxyIntegrationEvent.js +233 -0
- package/src/events/http/lambda-events/LambdaProxyIntegrationEventV2.js +190 -0
- package/src/events/http/lambda-events/VelocityContext.js +147 -0
- package/src/events/http/lambda-events/index.js +4 -0
- package/src/events/http/lambda-events/renderVelocityTemplateObject.js +93 -0
- package/{dist → src}/events/http/parseResources.js +73 -78
- package/src/events/http/payloadSchemaValidator.js +13 -0
- package/{dist → src}/events/http/templates/offline-default.req.vm +0 -0
- package/{dist → src}/events/http/templates/offline-default.res.vm +0 -0
- package/src/events/schedule/Schedule.js +131 -0
- package/src/events/schedule/ScheduleEvent.js +18 -0
- package/src/events/schedule/ScheduleEventDefinition.js +21 -0
- package/src/events/schedule/index.js +1 -0
- package/src/events/websocket/HttpServer.js +69 -0
- package/src/events/websocket/WebSocket.js +52 -0
- package/src/events/websocket/WebSocketClients.js +462 -0
- package/src/events/websocket/WebSocketEventDefinition.js +18 -0
- package/src/events/websocket/WebSocketServer.js +73 -0
- package/src/events/websocket/http-routes/_catchAll/catchAllRoute.js +16 -0
- package/src/events/websocket/http-routes/_catchAll/index.js +1 -0
- package/src/events/websocket/http-routes/connections/ConnectionsController.js +28 -0
- package/src/events/websocket/http-routes/connections/connectionsRoutes.js +70 -0
- package/src/events/websocket/http-routes/connections/index.js +1 -0
- package/src/events/websocket/http-routes/index.js +2 -0
- package/src/events/websocket/index.js +1 -0
- package/src/events/websocket/lambda-events/WebSocketAuthorizerEvent.js +65 -0
- package/src/events/websocket/lambda-events/WebSocketConnectEvent.js +68 -0
- package/src/events/websocket/lambda-events/WebSocketDisconnectEvent.js +31 -0
- package/src/events/websocket/lambda-events/WebSocketEvent.js +29 -0
- package/src/events/websocket/lambda-events/WebSocketRequestContext.js +67 -0
- package/src/events/websocket/lambda-events/index.js +4 -0
- package/src/index.js +12 -0
- package/src/lambda/HttpServer.js +108 -0
- package/src/lambda/Lambda.js +68 -0
- package/src/lambda/LambdaContext.js +33 -0
- package/src/lambda/LambdaFunction.js +308 -0
- package/src/lambda/LambdaFunctionPool.js +109 -0
- package/src/lambda/__tests__/LambdaContext.test.js +30 -0
- package/src/lambda/__tests__/LambdaFunction.test.js +196 -0
- package/src/lambda/__tests__/fixtures/Lambda/LambdaFunctionThatReturnsJSONObject.fixture.js +47 -0
- package/src/lambda/__tests__/fixtures/Lambda/LambdaFunctionThatReturnsNativeString.fixture.js +46 -0
- package/src/lambda/__tests__/fixtures/Lambda/package.json +3 -0
- package/src/lambda/__tests__/fixtures/lambdaFunction.fixture.js +145 -0
- package/src/lambda/__tests__/fixtures/package.json +3 -0
- package/src/lambda/__tests__/routes/invocations/InvocationsController.test.js +42 -0
- package/src/lambda/handler-runner/HandlerRunner.js +136 -0
- package/src/lambda/handler-runner/child-process-runner/ChildProcessRunner.js +72 -0
- package/src/lambda/handler-runner/child-process-runner/childProcessHelper.js +42 -0
- package/src/lambda/handler-runner/child-process-runner/index.js +1 -0
- package/src/lambda/handler-runner/docker-runner/DockerContainer.js +417 -0
- package/src/lambda/handler-runner/docker-runner/DockerImage.js +35 -0
- package/src/lambda/handler-runner/docker-runner/DockerRunner.js +63 -0
- package/src/lambda/handler-runner/docker-runner/index.js +1 -0
- package/src/lambda/handler-runner/go-runner/GoRunner.js +166 -0
- package/src/lambda/handler-runner/go-runner/index.js +1 -0
- package/src/lambda/handler-runner/in-process-runner/InProcessRunner.js +125 -0
- package/src/lambda/handler-runner/in-process-runner/index.js +1 -0
- package/src/lambda/handler-runner/index.js +1 -0
- package/src/lambda/handler-runner/java-runner/JavaRunner.js +114 -0
- package/src/lambda/handler-runner/java-runner/index.js +1 -0
- package/src/lambda/handler-runner/python-runner/PythonRunner.js +138 -0
- package/src/lambda/handler-runner/python-runner/index.js +1 -0
- package/{dist → src}/lambda/handler-runner/python-runner/invoke.py +0 -0
- package/src/lambda/handler-runner/ruby-runner/RubyRunner.js +107 -0
- package/src/lambda/handler-runner/ruby-runner/index.js +1 -0
- package/{dist → src}/lambda/handler-runner/ruby-runner/invoke.rb +0 -0
- package/src/lambda/handler-runner/worker-thread-runner/WorkerThreadRunner.js +70 -0
- package/src/lambda/handler-runner/worker-thread-runner/index.js +1 -0
- package/src/lambda/handler-runner/worker-thread-runner/workerThreadHelper.js +29 -0
- package/src/lambda/index.js +1 -0
- package/src/lambda/routes/index.js +2 -0
- package/src/lambda/routes/invocations/InvocationsController.js +102 -0
- package/src/lambda/routes/invocations/index.js +1 -0
- package/src/lambda/routes/invocations/invocationsRoute.js +77 -0
- package/src/lambda/routes/invoke-async/InvokeAsyncController.js +20 -0
- package/src/lambda/routes/invoke-async/index.js +1 -0
- package/src/lambda/routes/invoke-async/invokeAsyncRoute.js +33 -0
- package/src/utils/__tests__/createUniqueId.test.js +18 -0
- package/src/utils/__tests__/formatToClfTime.test.js +14 -0
- package/src/utils/__tests__/generateHapiPath.test.js +46 -0
- package/src/utils/__tests__/lowerCaseKeys.test.js +30 -0
- package/src/utils/__tests__/parseHeaders.test.js +13 -0
- package/src/utils/__tests__/parseMultiValueHeaders.test.js +24 -0
- package/src/utils/__tests__/parseMultiValueQueryStringParameters.test.js +159 -0
- package/src/utils/__tests__/parseQueryStringParameters.test.js +15 -0
- package/src/utils/__tests__/splitHandlerPathAndName.test.js +54 -0
- package/src/utils/__tests__/unflatten.test.js +32 -0
- package/src/utils/checkDockerDaemon.js +19 -0
- package/src/utils/checkGoVersion.js +16 -0
- package/src/utils/createApiKey.js +5 -0
- package/src/utils/createUniqueId.js +5 -0
- package/src/utils/detectExecutable.js +11 -0
- package/{dist → src}/utils/formatToClfTime.js +6 -14
- package/src/utils/generateHapiPath.js +26 -0
- package/src/utils/getHttpApiCorsConfig.js +28 -0
- package/src/utils/index.js +42 -0
- package/src/utils/jsonPath.js +13 -0
- package/src/utils/logRoutes.js +64 -0
- package/src/utils/lowerCaseKeys.js +6 -0
- package/src/utils/parseHeaders.js +14 -0
- package/src/utils/parseMultiValueHeaders.js +27 -0
- package/src/utils/parseMultiValueQueryStringParameters.js +31 -0
- package/src/utils/parseQueryStringParameters.js +15 -0
- package/src/utils/resolveJoins.js +29 -0
- package/src/utils/splitHandlerPathAndName.js +31 -0
- package/src/utils/unflatten.js +11 -0
- package/dist/ServerlessOffline.js +0 -507
- package/dist/checkEngine.js +0 -21
- package/dist/config/commandOptions.js +0 -149
- package/dist/config/constants.js +0 -30
- package/dist/config/index.js +0 -55
- package/dist/config/supportedRuntimes.js +0 -40
- package/dist/debugLog.js +0 -10
- package/dist/events/authCanExecuteResource.js +0 -35
- package/dist/events/authFunctionNameExtractor.js +0 -87
- package/dist/events/authMatchPolicyResource.js +0 -62
- package/dist/events/http/Endpoint.js +0 -171
- package/dist/events/http/Http.js +0 -77
- package/dist/events/http/HttpEventDefinition.js +0 -36
- package/dist/events/http/HttpServer.js +0 -1363
- package/dist/events/http/OfflineEndpoint.js +0 -40
- package/dist/events/http/authJWTSettingsExtractor.js +0 -76
- package/dist/events/http/authValidateContext.js +0 -48
- package/dist/events/http/createAuthScheme.js +0 -184
- package/dist/events/http/createJWTAuthScheme.js +0 -155
- package/dist/events/http/index.js +0 -15
- package/dist/events/http/javaHelpers.js +0 -99
- package/dist/events/http/lambda-events/LambdaIntegrationEvent.js +0 -85
- package/dist/events/http/lambda-events/LambdaProxyIntegrationEvent.js +0 -244
- package/dist/events/http/lambda-events/LambdaProxyIntegrationEventV2.js +0 -221
- package/dist/events/http/lambda-events/VelocityContext.js +0 -168
- package/dist/events/http/lambda-events/index.js +0 -39
- package/dist/events/http/lambda-events/renderVelocityTemplateObject.js +0 -108
- package/dist/events/http/payloadSchemaValidator.js +0 -13
- package/dist/events/schedule/Schedule.js +0 -182
- package/dist/events/schedule/ScheduleEvent.js +0 -27
- package/dist/events/schedule/ScheduleEventDefinition.js +0 -36
- package/dist/events/schedule/index.js +0 -15
- package/dist/events/websocket/HttpServer.js +0 -112
- package/dist/events/websocket/WebSocket.js +0 -78
- package/dist/events/websocket/WebSocketClients.js +0 -550
- package/dist/events/websocket/WebSocketEventDefinition.js +0 -32
- package/dist/events/websocket/WebSocketServer.js +0 -140
- package/dist/events/websocket/http-routes/_catchAll/catchAllRoute.js +0 -33
- package/dist/events/websocket/http-routes/_catchAll/index.js +0 -15
- package/dist/events/websocket/http-routes/connections/ConnectionsController.js +0 -45
- package/dist/events/websocket/http-routes/connections/connectionsRoutes.js +0 -95
- package/dist/events/websocket/http-routes/connections/index.js +0 -15
- package/dist/events/websocket/http-routes/index.js +0 -23
- package/dist/events/websocket/index.js +0 -15
- package/dist/events/websocket/lambda-events/WebSocketAuthorizerEvent.js +0 -99
- package/dist/events/websocket/lambda-events/WebSocketConnectEvent.js +0 -101
- package/dist/events/websocket/lambda-events/WebSocketDisconnectEvent.js +0 -47
- package/dist/events/websocket/lambda-events/WebSocketEvent.js +0 -54
- package/dist/events/websocket/lambda-events/WebSocketRequestContext.js +0 -98
- package/dist/events/websocket/lambda-events/index.js +0 -39
- package/dist/index.js +0 -19
- package/dist/lambda/HttpServer.js +0 -122
- package/dist/lambda/Lambda.js +0 -113
- package/dist/lambda/LambdaContext.js +0 -53
- package/dist/lambda/LambdaFunction.js +0 -391
- package/dist/lambda/LambdaFunctionPool.js +0 -127
- package/dist/lambda/handler-runner/HandlerRunner.js +0 -223
- package/dist/lambda/handler-runner/child-process-runner/ChildProcessRunner.js +0 -132
- package/dist/lambda/handler-runner/child-process-runner/childProcessHelper.js +0 -40
- package/dist/lambda/handler-runner/child-process-runner/index.js +0 -15
- package/dist/lambda/handler-runner/docker-runner/DockerContainer.js +0 -517
- package/dist/lambda/handler-runner/docker-runner/DockerImage.js +0 -67
- package/dist/lambda/handler-runner/docker-runner/DockerRunner.js +0 -74
- package/dist/lambda/handler-runner/docker-runner/index.js +0 -15
- package/dist/lambda/handler-runner/go-runner/GoRunner.js +0 -211
- package/dist/lambda/handler-runner/go-runner/index.js +0 -15
- package/dist/lambda/handler-runner/in-process-runner/InProcessRunner.js +0 -234
- package/dist/lambda/handler-runner/in-process-runner/index.js +0 -15
- package/dist/lambda/handler-runner/index.js +0 -15
- package/dist/lambda/handler-runner/java-runner/JavaRunner.js +0 -151
- package/dist/lambda/handler-runner/java-runner/index.js +0 -15
- package/dist/lambda/handler-runner/python-runner/PythonRunner.js +0 -180
- package/dist/lambda/handler-runner/python-runner/index.js +0 -15
- package/dist/lambda/handler-runner/ruby-runner/RubyRunner.js +0 -148
- package/dist/lambda/handler-runner/ruby-runner/index.js +0 -15
- package/dist/lambda/handler-runner/worker-thread-runner/WorkerThreadRunner.js +0 -94
- package/dist/lambda/handler-runner/worker-thread-runner/index.js +0 -15
- package/dist/lambda/handler-runner/worker-thread-runner/workerThreadHelper.js +0 -30
- package/dist/lambda/index.js +0 -15
- package/dist/lambda/routes/index.js +0 -23
- package/dist/lambda/routes/invocations/InvocationsController.js +0 -142
- package/dist/lambda/routes/invocations/index.js +0 -15
- package/dist/lambda/routes/invocations/invocationsRoute.js +0 -90
- package/dist/lambda/routes/invoke-async/InvokeAsyncController.js +0 -38
- package/dist/lambda/routes/invoke-async/index.js +0 -15
- package/dist/lambda/routes/invoke-async/invokeAsyncRoute.js +0 -43
- package/dist/main.js +0 -11
- package/dist/serverlessLog.js +0 -91
- package/dist/utils/checkDockerDaemon.js +0 -27
- package/dist/utils/checkGoVersion.js +0 -27
- package/dist/utils/createApiKey.js +0 -12
- package/dist/utils/createUniqueId.js +0 -14
- package/dist/utils/detectExecutable.js +0 -21
- package/dist/utils/generateHapiPath.js +0 -28
- package/dist/utils/getHttpApiCorsConfig.js +0 -44
- package/dist/utils/index.js +0 -158
- package/dist/utils/jsonPath.js +0 -21
- package/dist/utils/parseHeaders.js +0 -23
- package/dist/utils/parseMultiValueHeaders.js +0 -36
- package/dist/utils/parseMultiValueQueryStringParameters.js +0 -40
- package/dist/utils/parseQueryStringParameters.js +0 -26
- package/dist/utils/resolveJoins.js +0 -34
- package/dist/utils/satisfiesVersionRange.js +0 -20
- package/dist/utils/splitHandlerPathAndName.js +0 -41
- package/dist/utils/unflatten.js +0 -18
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { execa } from 'execa'
|
|
2
|
+
|
|
3
|
+
export default async function checkGoVersion() {
|
|
4
|
+
let goVersion
|
|
5
|
+
|
|
6
|
+
try {
|
|
7
|
+
const { stdout } = await execa('go', ['version'])
|
|
8
|
+
if (stdout.match(/go1.\d+/g)) {
|
|
9
|
+
goVersion = '1.x'
|
|
10
|
+
}
|
|
11
|
+
} catch {
|
|
12
|
+
// @ignore
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return goVersion
|
|
16
|
+
}
|
|
@@ -1,15 +1,8 @@
|
|
|
1
|
-
|
|
1
|
+
import { DateTime } from 'luxon'
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.default = formatToClfTime;
|
|
3
|
+
const { fromMillis } = DateTime
|
|
7
4
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
const {
|
|
11
|
-
fromMillis
|
|
12
|
-
} = _luxon.DateTime; // CLF -> Common Log Format
|
|
5
|
+
// CLF -> Common Log Format
|
|
13
6
|
// https://httpd.apache.org/docs/1.3/logs.html#common
|
|
14
7
|
// [day/month/year:hour:minute:second zone]
|
|
15
8
|
// day = 2*digit
|
|
@@ -19,7 +12,6 @@ const {
|
|
|
19
12
|
// minute = 2*digit
|
|
20
13
|
// second = 2*digit
|
|
21
14
|
// zone = (`+' | `-') 4*digit
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
}
|
|
15
|
+
export default function formatToClfTime(millis) {
|
|
16
|
+
return fromMillis(millis).toFormat('dd/MMM/yyyy:HH:mm:ss ZZZ')
|
|
17
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export default function generateHapiPath(path, options, serverless) {
|
|
2
|
+
// path must start with '/'
|
|
3
|
+
let hapiPath = path.startsWith('/') ? path : `/${path}`
|
|
4
|
+
|
|
5
|
+
if (!options.noPrependStageInUrl) {
|
|
6
|
+
const stage = options.stage || serverless.service.provider.stage
|
|
7
|
+
// prepend the stage to path
|
|
8
|
+
hapiPath = `/${stage}${hapiPath}`
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
if (options.prefix) {
|
|
12
|
+
hapiPath = `/${options.prefix}${hapiPath}`
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
if (
|
|
16
|
+
hapiPath !== '/' &&
|
|
17
|
+
hapiPath.endsWith('/') &&
|
|
18
|
+
(!options.noStripTrailingSlashInUrl || hapiPath.endsWith('+}/'))
|
|
19
|
+
) {
|
|
20
|
+
hapiPath = hapiPath.slice(0, -1)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
hapiPath = hapiPath.replace(/\+}/g, '*}')
|
|
24
|
+
|
|
25
|
+
return hapiPath
|
|
26
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { log } from '@serverless/utils/log.js'
|
|
2
|
+
|
|
3
|
+
export default function getHttpApiCorsConfig(httpApiCors) {
|
|
4
|
+
if (httpApiCors === true) {
|
|
5
|
+
// default values that should be set by serverless
|
|
6
|
+
// https://www.serverless.com/framework/docs/providers/aws/events/http-api/
|
|
7
|
+
const c = {
|
|
8
|
+
allowedHeaders: [
|
|
9
|
+
'Authorization',
|
|
10
|
+
'Content-Type',
|
|
11
|
+
'X-Amz-Date',
|
|
12
|
+
'X-Amz-Security-Token',
|
|
13
|
+
'X-Amz-User-Agent',
|
|
14
|
+
'X-Api-Key',
|
|
15
|
+
],
|
|
16
|
+
allowedMethods: ['DELETE', 'GET', 'OPTIONS', 'PATCH', 'POST', 'PUT'],
|
|
17
|
+
allowedOrigins: ['*'],
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
log.debug('Using CORS policy', c)
|
|
21
|
+
|
|
22
|
+
return c
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
log.debug('Using CORS policy', httpApiCors)
|
|
26
|
+
|
|
27
|
+
return httpApiCors
|
|
28
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
export { default as createApiKey } from './createApiKey.js'
|
|
2
|
+
export { default as createUniqueId } from './createUniqueId.js'
|
|
3
|
+
export { default as detectExecutable } from './detectExecutable.js'
|
|
4
|
+
export { default as formatToClfTime } from './formatToClfTime.js'
|
|
5
|
+
export { default as getHttpApiCorsConfig } from './getHttpApiCorsConfig.js'
|
|
6
|
+
export { default as jsonPath } from './jsonPath.js'
|
|
7
|
+
export { default as lowerCaseKeys } from './lowerCaseKeys.js'
|
|
8
|
+
export { default as parseHeaders } from './parseHeaders.js'
|
|
9
|
+
export { default as parseMultiValueHeaders } from './parseMultiValueHeaders.js'
|
|
10
|
+
export { default as parseMultiValueQueryStringParameters } from './parseMultiValueQueryStringParameters.js'
|
|
11
|
+
export { default as parseQueryStringParameters } from './parseQueryStringParameters.js'
|
|
12
|
+
export { default as splitHandlerPathAndName } from './splitHandlerPathAndName.js'
|
|
13
|
+
export { default as checkDockerDaemon } from './checkDockerDaemon.js'
|
|
14
|
+
export { default as checkGoVersion } from './checkGoVersion.js'
|
|
15
|
+
export { default as generateHapiPath } from './generateHapiPath.js'
|
|
16
|
+
// export { default as baseImage } from './baseImage.js'
|
|
17
|
+
|
|
18
|
+
const { isArray } = Array
|
|
19
|
+
const { keys } = Object
|
|
20
|
+
|
|
21
|
+
// Detect the toString encoding from the request headers content-type
|
|
22
|
+
// enhance if further content types need to be non utf8 encoded.
|
|
23
|
+
export function detectEncoding(request) {
|
|
24
|
+
const contentType = request.headers['content-type']
|
|
25
|
+
|
|
26
|
+
return typeof contentType === 'string' &&
|
|
27
|
+
contentType.includes('multipart/form-data')
|
|
28
|
+
? 'binary'
|
|
29
|
+
: 'utf8'
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function nullIfEmpty(obj) {
|
|
33
|
+
return obj && (keys(obj).length > 0 ? obj : null)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function isPlainObject(obj) {
|
|
37
|
+
return typeof obj === 'object' && !isArray(obj) && obj != null
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function toPlainOrEmptyObject(obj) {
|
|
41
|
+
return typeof obj === 'object' && !isArray(obj) ? obj : {}
|
|
42
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { JSONPath } from 'jsonpath-plus'
|
|
2
|
+
|
|
3
|
+
export default function jsonPath(json, path) {
|
|
4
|
+
// NOTE: JSONPath returns undefined if 'json' is e.g. null, undefined, string,
|
|
5
|
+
// number (anything other than JSON)
|
|
6
|
+
const [result] =
|
|
7
|
+
JSONPath({
|
|
8
|
+
json,
|
|
9
|
+
path,
|
|
10
|
+
}) || []
|
|
11
|
+
|
|
12
|
+
return result
|
|
13
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import boxen from 'boxen'
|
|
2
|
+
import chalk from 'chalk'
|
|
3
|
+
|
|
4
|
+
const { max } = Math
|
|
5
|
+
|
|
6
|
+
const dodgerblue = chalk.hex('#1e90ff')
|
|
7
|
+
const grey = chalk.hex('#808080')
|
|
8
|
+
const lime = chalk.hex('#00ff00')
|
|
9
|
+
const orange = chalk.hex('#ffa500')
|
|
10
|
+
const peachpuff = chalk.hex('#ffdab9')
|
|
11
|
+
const plum = chalk.hex('#dda0dd')
|
|
12
|
+
const red = chalk.hex('#ff0000')
|
|
13
|
+
const yellow = chalk.hex('#ffff00')
|
|
14
|
+
|
|
15
|
+
const colorMethodMapping = new Map([
|
|
16
|
+
['DELETE', red],
|
|
17
|
+
['GET', dodgerblue],
|
|
18
|
+
// ['HEAD', ...],
|
|
19
|
+
['PATCH', orange],
|
|
20
|
+
['POST', plum],
|
|
21
|
+
['PUT', dodgerblue],
|
|
22
|
+
])
|
|
23
|
+
|
|
24
|
+
// logs based on:
|
|
25
|
+
// https://github.com/serverless/serverless/blob/master/lib/classes/CLI.js
|
|
26
|
+
|
|
27
|
+
function logRoute(method, server, path, maxLength, dimPath = false) {
|
|
28
|
+
const methodColor = colorMethodMapping.get(method) ?? peachpuff
|
|
29
|
+
const methodFormatted = method.padEnd(maxLength, ' ')
|
|
30
|
+
|
|
31
|
+
return `${methodColor(methodFormatted)} ${yellow.dim('|')} ${grey.dim(
|
|
32
|
+
server,
|
|
33
|
+
)}${dimPath ? grey.dim(path) : lime(path)}`
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function getMaxHttpMethodNameLength(routeInfo) {
|
|
37
|
+
return max(...routeInfo.map(({ method }) => method.length))
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export default function logRoutes(routeInfo) {
|
|
41
|
+
const boxenOptions = {
|
|
42
|
+
borderColor: 'yellow',
|
|
43
|
+
dimBorder: true,
|
|
44
|
+
margin: 1,
|
|
45
|
+
padding: 1,
|
|
46
|
+
}
|
|
47
|
+
const maxLength = getMaxHttpMethodNameLength(routeInfo)
|
|
48
|
+
|
|
49
|
+
// eslint-disable-next-line no-console
|
|
50
|
+
console.log(
|
|
51
|
+
boxen(
|
|
52
|
+
routeInfo
|
|
53
|
+
.map(
|
|
54
|
+
({ method, path, server, invokePath }) =>
|
|
55
|
+
// eslint-disable-next-line prefer-template
|
|
56
|
+
logRoute(method, server, path, maxLength) +
|
|
57
|
+
'\n' +
|
|
58
|
+
logRoute('POST', server, invokePath, maxLength, true),
|
|
59
|
+
)
|
|
60
|
+
.join('\n'),
|
|
61
|
+
boxenOptions,
|
|
62
|
+
),
|
|
63
|
+
)
|
|
64
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import unflatten from './unflatten.js'
|
|
2
|
+
|
|
3
|
+
const { fromEntries } = Object
|
|
4
|
+
|
|
5
|
+
// (rawHeaders: Array<string>): { [string]: string }
|
|
6
|
+
export default function parseHeaders(rawHeaders) {
|
|
7
|
+
if (rawHeaders.length === 0) {
|
|
8
|
+
return null
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const unflattened = unflatten(rawHeaders, 2)
|
|
12
|
+
|
|
13
|
+
return fromEntries(unflattened)
|
|
14
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import unflatten from './unflatten.js'
|
|
2
|
+
|
|
3
|
+
const { fromEntries } = Object
|
|
4
|
+
|
|
5
|
+
// https://aws.amazon.com/blogs/compute/support-for-multi-value-parameters-in-amazon-api-gateway/
|
|
6
|
+
// (rawHeaders: Array<string>): { [string]: Array<string> }
|
|
7
|
+
export default function parseMultiValueHeaders(rawHeaders) {
|
|
8
|
+
if (rawHeaders.length === 0) {
|
|
9
|
+
return null
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const map = new Map()
|
|
13
|
+
const unflattened = unflatten(rawHeaders, 2)
|
|
14
|
+
|
|
15
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
16
|
+
for (const [key, value] of unflattened) {
|
|
17
|
+
const item = map.get(key)
|
|
18
|
+
|
|
19
|
+
if (item) {
|
|
20
|
+
item.push(value)
|
|
21
|
+
} else {
|
|
22
|
+
map.set(key, [value])
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return fromEntries(map)
|
|
27
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { BASE_URL_PLACEHOLDER } from '../config/index.js'
|
|
2
|
+
|
|
3
|
+
const { fromEntries } = Object
|
|
4
|
+
|
|
5
|
+
// https://aws.amazon.com/blogs/compute/support-for-multi-value-parameters-in-amazon-api-gateway/
|
|
6
|
+
// [ [ 'petType', 'dog' ], [ 'petType', 'fish' ] ]
|
|
7
|
+
// => { petType: [ 'dog', 'fish' ] },
|
|
8
|
+
export default function parseMultiValueQueryStringParameters(url) {
|
|
9
|
+
// dummy placeholder url for the WHATWG URL constructor
|
|
10
|
+
// https://github.com/nodejs/node/issues/12682
|
|
11
|
+
const { searchParams } = new URL(url, BASE_URL_PLACEHOLDER)
|
|
12
|
+
|
|
13
|
+
if (Array.from(searchParams).length === 0) {
|
|
14
|
+
return null
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const map = new Map()
|
|
18
|
+
|
|
19
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
20
|
+
for (const [key, value] of searchParams) {
|
|
21
|
+
const item = map.get(key)
|
|
22
|
+
|
|
23
|
+
if (item) {
|
|
24
|
+
item.push(value)
|
|
25
|
+
} else {
|
|
26
|
+
map.set(key, [value])
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return fromEntries(map)
|
|
31
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { BASE_URL_PLACEHOLDER } from '../config/index.js'
|
|
2
|
+
|
|
3
|
+
const { fromEntries } = Object
|
|
4
|
+
|
|
5
|
+
export default function parseQueryStringParameters(url) {
|
|
6
|
+
// dummy placeholder url for the WHATWG URL constructor
|
|
7
|
+
// https://github.com/nodejs/node/issues/12682
|
|
8
|
+
const { searchParams } = new URL(url, BASE_URL_PLACEHOLDER)
|
|
9
|
+
|
|
10
|
+
if (Array.from(searchParams).length === 0) {
|
|
11
|
+
return null
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
return fromEntries(searchParams)
|
|
15
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
const { entries } = Object
|
|
2
|
+
|
|
3
|
+
// Used to resolve Fn::Join in environment variables
|
|
4
|
+
export default function resolveJoins(environment) {
|
|
5
|
+
if (!environment) {
|
|
6
|
+
return undefined
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const newEnv = {}
|
|
10
|
+
|
|
11
|
+
entries(environment).forEach(([key, value]) => {
|
|
12
|
+
if (!value) {
|
|
13
|
+
return
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const joinArray = value['Fn::Join']
|
|
17
|
+
const isJoin = Boolean(joinArray)
|
|
18
|
+
|
|
19
|
+
if (isJoin) {
|
|
20
|
+
const separator = joinArray[0]
|
|
21
|
+
const joined = joinArray[1].join(separator)
|
|
22
|
+
newEnv[key] = joined
|
|
23
|
+
} else {
|
|
24
|
+
newEnv[key] = value
|
|
25
|
+
}
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
return newEnv
|
|
29
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// some-folder/src.index => some-folder/src
|
|
2
|
+
export default function splitHandlerPathAndName(handler) {
|
|
3
|
+
// Split handler into method name and path i.e. handler.run
|
|
4
|
+
// Support Ruby paths with namespace resolution operators e.g.
|
|
5
|
+
// ./src/somefolder/source.LambdaFunctions::Handler.process
|
|
6
|
+
// prepath: ./src/somefolder/
|
|
7
|
+
// postpath: source.LambdaFunctions::Handler.process
|
|
8
|
+
// filename: source
|
|
9
|
+
// path: ./src/somefolder/source
|
|
10
|
+
// name: LambdaFunctions::Handler.process
|
|
11
|
+
if (handler.match(/::/)) {
|
|
12
|
+
const prepathDelimiter = handler.lastIndexOf('/')
|
|
13
|
+
const prepath = handler.substr(0, prepathDelimiter + 1) // include '/' for path
|
|
14
|
+
const postpath = handler.substr(prepathDelimiter + 1)
|
|
15
|
+
const nameDelimiter = postpath.indexOf('.')
|
|
16
|
+
const filename = postpath.substr(0, nameDelimiter)
|
|
17
|
+
const path = prepath + filename
|
|
18
|
+
const name = postpath.substr(nameDelimiter + 1)
|
|
19
|
+
|
|
20
|
+
return [path, name]
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// Support nested paths i.e. ./src/somefolder/.handlers/handler.run
|
|
24
|
+
// path: ./src/somefoler/.handlers/handler
|
|
25
|
+
// name: run
|
|
26
|
+
const delimiter = handler.lastIndexOf('.')
|
|
27
|
+
const path = handler.substr(0, delimiter)
|
|
28
|
+
const name = handler.substr(delimiter + 1)
|
|
29
|
+
|
|
30
|
+
return [path, name]
|
|
31
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// [0, 1, 2, 3, 4 ,5] => [[0, 1], [2, 3], [4, 5]]
|
|
2
|
+
export default function unflatten(value, size) {
|
|
3
|
+
const unflattened = []
|
|
4
|
+
|
|
5
|
+
for (let i = 0; i < value.length; i += size) {
|
|
6
|
+
const slice = value.slice(i, i + size)
|
|
7
|
+
unflattened.push(slice)
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
return unflattened
|
|
11
|
+
}
|