@cumulusds/aws-apig-bypass 0.3.0 → 1.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 +1 -0
- package/lib/create-api-gateway-event.js +58 -0
- package/lib/create-client.js +23 -0
- package/lib/create-lambda-client.js +37 -0
- package/lib/json-stringify.js +20 -0
- package/lib/lambda-invoke.js +1 -0
- package/lib/parse-payload.js +27 -0
- package/lib/response.js +1 -0
- package/package.json +7 -4
- package/.babelrc.js +0 -15
- package/.eslintrc.js +0 -15
- package/.flowconfig +0 -13
- package/.yarn/plugins/@yarnpkg/plugin-version.cjs +0 -550
- package/.yarn/versions/fd5879cf.yml +0 -0
- package/doc/CreateAccessKey.png +0 -0
- package/doc/development.md +0 -244
- package/doc/prettier-watchers-pc.xml +0 -222
- package/doc/prettier-watchers-unix.xml +0 -222
- package/doc/webstorm-file-watcher-prettier.png +0 -0
- package/doc/webstorm-flow.png +0 -0
- package/flow-typed/npm/jest_v26.x.x.js +0 -1218
- package/prettier.config.js +0 -3
- package/test/unit/index.test.js +0 -145
- /package/{src/create-api-gateway-event.js → lib/create-api-gateway-event.js.flow} +0 -0
- /package/{src/create-client.js → lib/create-client.js.flow} +0 -0
- /package/{src/create-lambda-client.js → lib/create-lambda-client.js.flow} +0 -0
- /package/{src/index.js → lib/index.js.flow} +0 -0
- /package/{src/json-stringify.js → lib/json-stringify.js.flow} +0 -0
- /package/{src/lambda-invoke.js → lib/lambda-invoke.js.flow} +0 -0
- /package/{src/parse-payload.js → lib/parse-payload.js.flow} +0 -0
- /package/{src/response.js → lib/response.js.flow} +0 -0
package/README.md
CHANGED
|
@@ -7,6 +7,7 @@ We often build externally accessible REST endpoints using API Gateway and Lambda
|
|
|
7
7
|
This library provides an adapter for the Lambda SDK to make it easy to directly invoke the handler.
|
|
8
8
|
|
|
9
9
|
```flow js
|
|
10
|
+
import { createAPIGatewayEvent, createClient } from "@cumulusds/aws-apig-bypass";
|
|
10
11
|
import { Lambda } from "aws-sdk";
|
|
11
12
|
|
|
12
13
|
function getLunches(location: string, category: string): Promise<Array<Lunch>> {
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = createAPIGatewayEvent;
|
|
7
|
+
const defaultRequestContext = {
|
|
8
|
+
accountId: "",
|
|
9
|
+
apiId: "",
|
|
10
|
+
httpMethod: "",
|
|
11
|
+
identity: {
|
|
12
|
+
accessKey: null,
|
|
13
|
+
accountId: null,
|
|
14
|
+
apiKey: null,
|
|
15
|
+
caller: null,
|
|
16
|
+
cognitoAuthenticationProvider: null,
|
|
17
|
+
cognitoAuthenticationType: null,
|
|
18
|
+
cognitoIdentityId: null,
|
|
19
|
+
cognitoIdentityPoolId: null,
|
|
20
|
+
sourceIp: "",
|
|
21
|
+
user: null,
|
|
22
|
+
userAgent: null,
|
|
23
|
+
userArn: null
|
|
24
|
+
},
|
|
25
|
+
stage: "",
|
|
26
|
+
requestId: "",
|
|
27
|
+
resourceId: "",
|
|
28
|
+
resourcePath: ""
|
|
29
|
+
};
|
|
30
|
+
function createAPIGatewayEvent({
|
|
31
|
+
body = null,
|
|
32
|
+
headers = {},
|
|
33
|
+
multiValueHeaders = {},
|
|
34
|
+
httpMethod = "",
|
|
35
|
+
isBase64Encoded = false,
|
|
36
|
+
pathParameters = null,
|
|
37
|
+
path = "",
|
|
38
|
+
queryStringParameters = null,
|
|
39
|
+
multiValueQueryStringParameters = null,
|
|
40
|
+
stageVariables = null,
|
|
41
|
+
requestContext = defaultRequestContext,
|
|
42
|
+
resource = ""
|
|
43
|
+
} = {}) {
|
|
44
|
+
return {
|
|
45
|
+
body,
|
|
46
|
+
headers,
|
|
47
|
+
multiValueHeaders,
|
|
48
|
+
httpMethod,
|
|
49
|
+
isBase64Encoded,
|
|
50
|
+
path,
|
|
51
|
+
pathParameters,
|
|
52
|
+
queryStringParameters,
|
|
53
|
+
multiValueQueryStringParameters,
|
|
54
|
+
stageVariables,
|
|
55
|
+
requestContext,
|
|
56
|
+
resource
|
|
57
|
+
};
|
|
58
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = createClient;
|
|
7
|
+
var _createLambdaClient = _interopRequireDefault(require("./create-lambda-client"));
|
|
8
|
+
var _jsonStringify = _interopRequireDefault(require("./json-stringify"));
|
|
9
|
+
var _parsePayload = _interopRequireDefault(require("./parse-payload"));
|
|
10
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
11
|
+
/**
|
|
12
|
+
* Client that directly invokes an API Gateway handler, bypassing the gateway. The client handles packing and unpacking messages for invoking the handler.
|
|
13
|
+
*/
|
|
14
|
+
/**
|
|
15
|
+
* Create an APIGatewayHandlerClient. The client invokes an API Gateway handler, bypassing the gateway. It adapts the AWS SDK by packing and unpacking messages for invoking the handler.
|
|
16
|
+
*
|
|
17
|
+
* @param options
|
|
18
|
+
* @returns {APIGatewayHandlerClient<T>}
|
|
19
|
+
*/
|
|
20
|
+
function createClient(options) {
|
|
21
|
+
const client = (0, _createLambdaClient.default)(options);
|
|
22
|
+
return async event => (0, _parsePayload.default)(await client((0, _jsonStringify.default)(event)));
|
|
23
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = createLambdaClient;
|
|
7
|
+
exports.LambdaInvokeError = void 0;
|
|
8
|
+
class LambdaInvokeError extends Error {
|
|
9
|
+
constructor(response) {
|
|
10
|
+
var _response$FunctionErr;
|
|
11
|
+
super(["Invocation Failed", response.ExecutedVersion, response.StatusCode, response.FunctionError, response.Payload, response.LogResult != null ? Buffer.from(response.LogResult, "base64").toString("utf-8") : null].filter(x => x != null).join(": "));
|
|
12
|
+
this.code = (_response$FunctionErr = response.FunctionError) !== null && _response$FunctionErr !== void 0 ? _response$FunctionErr : "";
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* AWS Lambda SDK adapter for createClient.
|
|
18
|
+
*/
|
|
19
|
+
exports.LambdaInvokeError = LambdaInvokeError;
|
|
20
|
+
function createLambdaClient({
|
|
21
|
+
Lambda,
|
|
22
|
+
FunctionName
|
|
23
|
+
}) {
|
|
24
|
+
return async Payload => {
|
|
25
|
+
const invocationResponse = await Lambda.invoke({
|
|
26
|
+
FunctionName,
|
|
27
|
+
Payload
|
|
28
|
+
}).promise();
|
|
29
|
+
if (invocationResponse.StatusCode !== 200 || invocationResponse.FunctionError != null) {
|
|
30
|
+
throw new LambdaInvokeError(invocationResponse);
|
|
31
|
+
}
|
|
32
|
+
if (typeof invocationResponse.Payload !== "string") {
|
|
33
|
+
throw new Error(`AWS Lambda invocation API returned the wrong payload type '${typeof invocationResponse.Payload}'; expected 'string'`);
|
|
34
|
+
}
|
|
35
|
+
return invocationResponse.Payload;
|
|
36
|
+
};
|
|
37
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = jsonStringify;
|
|
7
|
+
/**
|
|
8
|
+
* Stringify to JSON, returning a string or else throwing an exception because of unserializable input.
|
|
9
|
+
*
|
|
10
|
+
* @param value
|
|
11
|
+
* @returns {string}
|
|
12
|
+
*/
|
|
13
|
+
// flowlint-next-line unclear-type:off
|
|
14
|
+
function jsonStringify(value) {
|
|
15
|
+
const result = JSON.stringify(value);
|
|
16
|
+
if (result == null) {
|
|
17
|
+
throw new Error("Cannot stringify value");
|
|
18
|
+
}
|
|
19
|
+
return result;
|
|
20
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = parsePayload;
|
|
7
|
+
/**
|
|
8
|
+
* Decode the handler response
|
|
9
|
+
*
|
|
10
|
+
* @param payload
|
|
11
|
+
* @param validate if given, the validate function is called on the parsed response body. The function should throw an exception if the response is invalid.
|
|
12
|
+
* @returns {{headers: {[p: string]: boolean|number|string}, data: *, status: number}}
|
|
13
|
+
*/
|
|
14
|
+
function parsePayload(payload, validate) {
|
|
15
|
+
var _proxyResult$headers;
|
|
16
|
+
const proxyResult = JSON.parse(payload);
|
|
17
|
+
if (proxyResult.isBase64Encoded === true) {
|
|
18
|
+
throw new Error("A base64 encoded response from API Gateway handler was received, but is not supported.");
|
|
19
|
+
}
|
|
20
|
+
const data = JSON.parse(proxyResult.body);
|
|
21
|
+
validate === null || validate === void 0 ? void 0 : validate(data);
|
|
22
|
+
return {
|
|
23
|
+
data,
|
|
24
|
+
status: proxyResult.statusCode,
|
|
25
|
+
headers: (_proxyResult$headers = proxyResult.headers) !== null && _proxyResult$headers !== void 0 ? _proxyResult$headers : {}
|
|
26
|
+
};
|
|
27
|
+
}
|
package/lib/response.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";
|
package/package.json
CHANGED
|
@@ -1,16 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cumulusds/aws-apig-bypass",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"description": "Client for API Gateway handlers, bypassing API Gateway by directly invoking the Lambda",
|
|
5
5
|
"repository": "https://github.com/CumulusDS/aws-apig-bypass",
|
|
6
6
|
"author": "Cumulus Digital Systems",
|
|
7
7
|
"license": "MIT",
|
|
8
8
|
"engines": {
|
|
9
|
-
"node": "^
|
|
9
|
+
"node": "^18.0.0 || ^20.0.0 || >=21.0.0"
|
|
10
10
|
},
|
|
11
11
|
"main": "lib/index.js",
|
|
12
|
-
"
|
|
13
|
-
"
|
|
12
|
+
"files": [
|
|
13
|
+
"README.md",
|
|
14
|
+
"lib/*",
|
|
15
|
+
"lib/dynamodb/**",
|
|
16
|
+
"lib/services/**",
|
|
14
17
|
"package.json"
|
|
15
18
|
],
|
|
16
19
|
"scripts": {
|
package/.babelrc.js
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
module.exports = {
|
|
2
|
-
presets: [
|
|
3
|
-
[
|
|
4
|
-
"@babel/preset-env",
|
|
5
|
-
{
|
|
6
|
-
targets: {
|
|
7
|
-
node: "10.15.0"
|
|
8
|
-
},
|
|
9
|
-
exclude: ["proposal-async-generator-functions"] // Do not transform async generator functions, since they are supported natively in Node 10 & 12.
|
|
10
|
-
}
|
|
11
|
-
],
|
|
12
|
-
"@babel/preset-flow"
|
|
13
|
-
],
|
|
14
|
-
plugins: ["@babel/plugin-proposal-nullish-coalescing-operator", "@babel/plugin-proposal-optional-chaining"]
|
|
15
|
-
};
|
package/.eslintrc.js
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
module.exports = {
|
|
2
|
-
extends: ["airbnb-base", "plugin:prettier/recommended", "plugin:flowtype/recommended"],
|
|
3
|
-
rules: {
|
|
4
|
-
"linebreak-style": "off",
|
|
5
|
-
"no-console": "off",
|
|
6
|
-
"no-restricted-syntax": "off"
|
|
7
|
-
},
|
|
8
|
-
plugins: ["jest", "flowtype"],
|
|
9
|
-
env: {
|
|
10
|
-
"jest/globals": true
|
|
11
|
-
},
|
|
12
|
-
parserOptions: {
|
|
13
|
-
ecmaVersion: 2020
|
|
14
|
-
}
|
|
15
|
-
};
|
package/.flowconfig
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
[ignore]
|
|
2
|
-
<PROJECT_ROOT>/node_modules/config-chain/test/broken.json
|
|
3
|
-
<PROJECT_ROOT>/node_modules/license-checker/tests/config/custom_format_broken.json
|
|
4
|
-
<PROJECT_ROOT>/lib
|
|
5
|
-
<PROJECT_ROOT>/node_modules/jest-resolve/node_modules/resolve/test/resolver/malformed_package_json/package.json
|
|
6
|
-
|
|
7
|
-
[libs]
|
|
8
|
-
node_modules/@cumulusds/flow-aws-sdk/index.js
|
|
9
|
-
node_modules/@cumulusds/flow-aws-sdk/clients
|
|
10
|
-
node_modules/@cumulusds/flow-aws-sdk/lib
|
|
11
|
-
|
|
12
|
-
[lints]
|
|
13
|
-
all=error
|