Skip to content

Commit

Permalink
reduced package size
Browse files Browse the repository at this point in the history
improved parser for sns and sqs plugins

fixed apg request path with variable
  • Loading branch information
Inqnuam committed May 31, 2023
1 parent c77bb07 commit 9274fc9
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 177 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ yarn-error.log
.vscode/*
!.vscode/extensions.json
!.vscode/settings.json

/.DS_Store
**/.DS_Store
/express
Expand Down
4 changes: 3 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ src
!src/lib/runtime/runners/ruby/index.rb
build.mjs
TODO.md
tsconfig.json
tsconfig.json
resources/invokeError.png
resources/invokeSuccess.png
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ for more options see [advanced configuration](#advanced-configuration).

Lambda execution lifecycles.
Succefull execution:
![lambda success](resources/invokeSuccess.png)
![lambda success](https://github.com/Inqnuam/serverless-aws-lambda/blob/main/resources/invokeSuccess.png)
Failed execution:
![lambda error](resources/invokeError.png)
![lambda error](https://github.com/Inqnuam/serverless-aws-lambda/blob/main/resources/invokeError.png)

Offline server supports Application Load Balancer, API Gateway and Function URL endpoints (see [plugins](#plugins) for more triggers).
Appropriate `event` object is sent to the handler based on your lambda declaration.
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "serverless-aws-lambda",
"version": "4.5.7",
"version": "4.5.8",
"description": "AWS Application Load Balancer and API Gateway - Lambda dev tool for Serverless. Allows Express synthax in handlers. Supports packaging, local invoking and offline ALB, APG, S3, SNS, SQS, DynamoDB Stream server mocking.",
"author": "Inqnuam",
"license": "MIT",
Expand Down Expand Up @@ -58,7 +58,7 @@
}
},
"dependencies": {
"@aws-sdk/eventstream-codec": "^3.329.0",
"@aws-sdk/eventstream-codec": "^3.342.0",
"@types/serverless": "^3.12.11",
"archiver": "^5.3.1",
"esbuild": "^0.17.19",
Expand Down
19 changes: 13 additions & 6 deletions src/defineConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,14 @@ export interface Options {
staticPath?: string;
port?: number;
};
plugins?: SlsAwsLambdaPlugin[];
/**
* Only SlsAwsLambdaPlugin type objects are considered as valid plugins.
*
* Others are ignored.
*
* This allows conditionnally ( true ?? customPlugin) plugin import.
*/
plugins?: (SlsAwsLambdaPlugin | null | undefined | boolean)[];
}

let exiting = false;
Expand Down Expand Up @@ -125,7 +132,7 @@ function defineConfig(options: Options) {

if (options.plugins) {
options.plugins = options.plugins.filter((plugin, index) => {
if (!plugin) {
if (typeof plugin != "object" || !plugin || !("name" in plugin)) {
return false;
}
if (!plugin.name || !plugin.name.length || typeof plugin.name != "string") {
Expand Down Expand Up @@ -170,7 +177,7 @@ function defineConfig(options: Options) {
};
if (options.plugins) {
config.offline!.onReady = async (port, ip) => {
for (const plugin of options.plugins!) {
for (const plugin of options.plugins! as SlsAwsLambdaPlugin[]) {
if (plugin.offline?.onReady) {
try {
await plugin.offline.onReady!.call(self, port, ip);
Expand All @@ -183,7 +190,7 @@ function defineConfig(options: Options) {
};

config.buildCallback = async (result, isRebuild) => {
for (const plugin of options.plugins!) {
for (const plugin of options.plugins! as SlsAwsLambdaPlugin[]) {
if (plugin.buildCallback) {
try {
await plugin.buildCallback.call(self, result, isRebuild);
Expand All @@ -195,7 +202,7 @@ function defineConfig(options: Options) {
}
};

const pluginsRequests: OfflineConfig["request"] = options.plugins?.reduce((accum: OfflineConfig["request"], obj) => {
const pluginsRequests: OfflineConfig["request"] = (options.plugins as SlsAwsLambdaPlugin[]).reduce((accum: OfflineConfig["request"], obj) => {
if (obj.offline?.request?.length) {
accum!.push(...obj.offline.request);
}
Expand All @@ -208,7 +215,7 @@ function defineConfig(options: Options) {
return x;
});
}
for (const plugin of options.plugins!) {
for (const plugin of options.plugins! as SlsAwsLambdaPlugin[]) {
if (plugin.onExit) {
exitCallbacks.push(plugin.onExit.bind(self));
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/parseEvents/endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ export const parseEndpoints = (event: any, httpApiPayload: LambdaEndpoint["versi
}

let ApgPathPartMatch = parsendEvent.paths[0];
ApgPathPartMatch.replace("*", ".*")
ApgPathPartMatch = ApgPathPartMatch.replace("*", ".*")
.replace(/\{[\w.:-]+\+?\}/g, ".*")
.replace(/\//g, "\\/");

Expand Down
1 change: 1 addition & 0 deletions src/lib/utils/readDefineConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const readFromPath = async (sourcefile: string) => {
packages: "external",
platform: "node",
format: "cjs",
target: "ES2018",
// supported: {
// "top-level-await": true, // TODO: esbuild ask for cjs + top-level-await support
// },
Expand Down
8 changes: 4 additions & 4 deletions src/plugins/sns/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const parseSnsPublishBody = (encodedBody: string[]) => {
let MessageAttributes: any = {};
let entryMap: any = {};
for (const s of encodedBody) {
const [k, v] = s.split("=");
const [k, v, ...restValues] = s.split("=");
if (k.startsWith("MessageAttributes")) {
const [_, __, entryNumber, entryType, aux] = k.split(".");

Expand All @@ -21,11 +21,11 @@ export const parseSnsPublishBody = (encodedBody: string[]) => {
if (aux == "DataType") {
MessageAttributes[entryMap[entryNumber]].Type = v;
} else {
MessageAttributes[entryMap[entryNumber]].Value = v;
MessageAttributes[entryMap[entryNumber]].Value = [v, ...restValues].join("=");
}
}
} else {
body[k] = v;
body[k] = [v, ...restValues].join("=");
}
}
if (Object.keys(MessageAttributes).length) {
Expand Down Expand Up @@ -100,7 +100,7 @@ export const parseSnsPublishBatchBody = (encodedBody: string[]) => {
}
}

for (let v of memberMap.values()) {
for (const v of memberMap.values()) {
let Sns: any = {
Type: "Notification",
MessageId: randomUUID(),
Expand Down
22 changes: 11 additions & 11 deletions src/plugins/sqs/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@ export const parseSqsPublishBody = (encodedBody: string[]) => {

if (entryType == "Name") {
MessageAttribute[v] = { DataType: "", StringValue: "" };
entryMap[entryNumber] = v;
entryMap[entryNumber] = [v, ...restValues].join("=");
} else if (entryType == "Value") {
if (aux == "DataType") {
MessageAttribute[entryMap[entryNumber]].DataType = v;
} else {
MessageAttribute[entryMap[entryNumber]].StringValue = v;
MessageAttribute[entryMap[entryNumber]].StringValue = [v, ...restValues].join("=");
}
}
} else if (k.startsWith("MessageSystemAttribute")) {
const [_, entryNumber, entryType, aux] = k.split(".");

if (entryType == "Name") {
MessageSystemAttribute[v] = { DataType: "", StringValue: "" };
systemEntryMap[entryNumber] = v;
systemEntryMap[entryNumber] = [v, ...restValues].join("=");
} else if (entryType == "Value") {
if (aux == "DataType") {
MessageSystemAttribute[systemEntryMap[entryNumber]].DataType = v;
Expand All @@ -40,7 +40,7 @@ export const parseSqsPublishBody = (encodedBody: string[]) => {
}
}
} else {
body[k] = v;
body[k] = [v, ...restValues].join("=");
}
}
if (Object.keys(MessageAttribute).length) {
Expand Down Expand Up @@ -70,14 +70,14 @@ export const parseSqsPublishBatchBody = (encodedBody: string[]) => {
const foundMember = memberMap.get(memberNumber);
if (foundMember) {
if (SQS_BATCH_MSG_TOP_LEVEL_KEYS.includes(entryType)) {
foundMember.value[entryType] = v;
foundMember.value[entryType] = [v, ...restValues].join("=");
} else if (entryType == "MessageAttribute" || entryType == "MessageSystemAttribute") {
const attribName = foundMember[entryType][entryNumber];

if (attribName) {
foundMember.value[entryType][attribName][aux3 == "DataType" ? "DataType" : "StringValue"] = v;
foundMember.value[entryType][attribName][aux3 == "DataType" ? "DataType" : "StringValue"] = [v, ...restValues].join("=");
} else {
foundMember[entryType][entryNumber] = v;
foundMember[entryType][entryNumber] = [v, ...restValues].join("=");

if (foundMember.value[entryType]) {
foundMember.value[entryType][v] = {};
Expand All @@ -95,9 +95,9 @@ export const parseSqsPublishBatchBody = (encodedBody: string[]) => {
value: {},
};
if (entryType == "MessageBody" || entryType == "Id") {
content.value[entryType] = v;
content.value[entryType] = [v, ...restValues].join("=");
} else if (entryType == "MessageAttribute" || entryType == "MessageSystemAttribute") {
content[entryType][entryNumber] = v;
content[entryType][entryNumber] = [v, ...restValues].join("=");
content.value[entryType] = {
[v]: {},
};
Expand All @@ -106,11 +106,11 @@ export const parseSqsPublishBatchBody = (encodedBody: string[]) => {
memberMap.set(memberNumber, content);
}
} else {
request[k] = v;
request[k] = [v, ...restValues].join("=");
}
}

for (let v of memberMap.values()) {
for (const v of memberMap.values()) {
request.Entries.push(v.value);
}
} catch (error) {
Expand Down
Loading

0 comments on commit 9274fc9

Please sign in to comment.