Skip to content

Commit

Permalink
updated dependencies
Browse files Browse the repository at this point in the history
use ESM to import defineConfig

export more types for Router
  • Loading branch information
Inqnuam committed Aug 10, 2023
1 parent a753ef9 commit e2e48b7
Show file tree
Hide file tree
Showing 13 changed files with 278 additions and 258 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ functions:
- http: ANY /visitor
```
| 200 + 200 executions | Time (in seconds) | Memory used (mb) | CPU (core) usage | last invoke response | info |
| 200 + 200 executions | Time (in seconds) | Memory used (mb) | CPU (core) usage | last invoke response | note |
| --------------------------------------------------------------------------------------- | ---------------------------------------- | ------------------------- | -------------------------- | -------------------- | ----------------------------------- |
| serverless-aws-lambda <br/> cmd: `serverless aws-lambda` | sequential: 0.644<br/> concurrent: 0.414 | idle: 125<br/> peak: 169 | idle: 0,1%<br/> peak: 15% | Visit count 400 | |
| serverless-offline + serverless-esbuild <br/> cmd: `serverless offline --reloadHandler` | sequential: 10.4<br/> concurrent: 2.8 | idle: 110<br/> peak: 3960 | idle: 0,1%<br/> peak: 537% | Visit count 1 | most of concurrent invocations fail |
Expand Down
2 changes: 1 addition & 1 deletion build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const compileDeclarations = () => {
console.log(error.output?.[1]?.toString());
}
};
const external = ["esbuild", "archiver", "serve-static", "@aws-sdk/eventstream-codec"];
const external = ["esbuild", "archiver", "serve-static", "@smithy/eventstream-codec"];
const watchPlugin = {
name: "watch-plugin",
setup: (build) => {
Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "serverless-aws-lambda",
"version": "4.6.0",
"version": "4.6.1",
"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,17 +58,17 @@
}
},
"dependencies": {
"@aws-sdk/eventstream-codec": "^3.347.0",
"@types/serverless": "^3.12.12",
"@smithy/eventstream-codec": "^2.0.2",
"@types/serverless": "^3.12.13",
"archiver": "^5.3.1",
"esbuild": "^0.18.4",
"esbuild": "^0.19.0",
"serve-static": "^1.15.0"
},
"devDependencies": {
"@types/archiver": "^5.3.2",
"@types/node": "^14.14.31",
"@types/serve-static": "^1.15.1",
"typescript": "^5.1.3"
"@types/serve-static": "^1.15.2",
"typescript": "^5.1.6"
},
"keywords": [
"aws",
Expand Down
40 changes: 27 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ const isLocalEnv = {
IS_LOCAL: true,
AWS_SAM_LOCAL: true,
};
const osRootPath = cwd.split(path.sep)[0];
const NsReg = new RegExp(`^.*:${osRootPath}`);
const isNamespacedPath = (p: string) => NsReg.test(p);

interface PluginUtils {
log: Function;
Expand Down Expand Up @@ -446,20 +449,31 @@ class ServerlessAwsLambda extends Daemon {
const outputNames = Object.keys(outputs)
.filter((x) => !x.endsWith(".map") && outputs[x].entryPoint)
.map((x) => {
const element = outputs[x];
if (element.entryPoint) {
const lastPointIndex = element.entryPoint.lastIndexOf(".");
const entryPoint = path.join(cwd, element.entryPoint.slice(0, lastPointIndex));
const esOutputPath = path.join(cwd, x);

const ext = path.extname(element.entryPoint);

return {
esOutputPath,
entryPoint,
ext,
};
const element = outputs[x] as Metafile["outputs"]["happy typescript"] & { entryPoint: string };

let actuelEntryPoint = element.entryPoint;
let ext = "";

if (isNamespacedPath(actuelEntryPoint)) {
actuelEntryPoint = actuelEntryPoint.split(":").slice(1).join(":");
}

if (!path.isAbsolute(actuelEntryPoint)) {
actuelEntryPoint = path.resolve(actuelEntryPoint);
}

const lastPointIndex = actuelEntryPoint.lastIndexOf(".");

if (lastPointIndex != -1) {
ext = path.extname(actuelEntryPoint);
actuelEntryPoint = actuelEntryPoint.slice(0, lastPointIndex);
}

return {
esOutputPath: path.join(cwd, x),
entryPoint: actuelEntryPoint,
ext,
};
});

this.#lambdas.forEach((x) => {
Expand Down
4 changes: 2 additions & 2 deletions src/lambda/express/request.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type HttpMethod = "GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "HEAD" | "OPTIONS" | "ANY";
type HttpMethod = "GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "HEAD" | "OPTIONS";

export interface RawAPIResponseContent {
cookies?: string[];
Expand All @@ -13,7 +13,7 @@ export interface RawResponseContent {
[key: string]: any;
}

interface KnownRequestProperties {
export interface KnownRequestProperties {
requestContext: { [key: string]: any };
httpMethod: HttpMethod;
queryStringParameters: { [key: string]: string };
Expand Down
3 changes: 2 additions & 1 deletion src/lambda/express/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export interface IResponse {
sendStatus: (code: number) => void;
send: (content?: string | Buffer) => void;
end: (rawContent: any) => void;
type(contentType: string): this;
json: (content: Stringifiable) => void;
set: (header: string | { [key: string]: string }, value?: string) => this;
setHeader: (header: string | { [key: string]: string }, value?: string) => this;
Expand Down Expand Up @@ -218,7 +219,7 @@ export class _Response implements IResponse {
let resContent = undefined;
if (Array.isArray(rawContent)) {
resContent = [...rawContent];
} else if (typeof rawContent == "object") {
} else if (rawContent && typeof rawContent == "object") {
resContent = { ...rawContent };
} else {
resContent = rawContent;
Expand Down
4 changes: 2 additions & 2 deletions src/lambda/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ const getMiddleware = (controllers: (RouteController | RouteMiddleware | Functio
return controllers[0]?.length < 4 ? controllers[0] : undefined;
}
};
const { IS_LOCAL, NODE_ENV } = process.env;
const isProd = NODE_ENV == "production";
const { IS_LOCAL } = process.env;
const isProd = process.env.NODE_ENV == "production";

class Route extends Function {
controllers: (RouteController | RouteMiddleware | Function)[] = [];
Expand Down
1 change: 1 addition & 0 deletions src/lib/esbuild/knownCjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const knownCjs: string[] = [
"@middy/*",
"@hapi/joi",
"@opensearch-project/opensearch",
"@smithy/*",
"axios",
"axios-retry",
"axios-cache-interceptor",
Expand Down
10 changes: 9 additions & 1 deletion src/lib/esbuild/mergeEsbuildConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { BuildOptions } from "esbuild";

export const mergeEsbuildConfig = (esBuildConfig: BuildOptions, customEsBuildConfig: BuildOptions) => {
if (Array.isArray(customEsBuildConfig.plugins)) {
esBuildConfig.plugins!.push(...customEsBuildConfig.plugins);
esBuildConfig.plugins!.unshift(...customEsBuildConfig.plugins);
}

if (Array.isArray(customEsBuildConfig.external)) {
Expand Down Expand Up @@ -203,5 +203,13 @@ export const mergeEsbuildConfig = (esBuildConfig: BuildOptions, customEsBuildCon
esBuildConfig.packages = customEsBuildConfig.packages;
}

if (customEsBuildConfig.logOverride && typeof customEsBuildConfig.logOverride == "object") {
if (esBuildConfig.logOverride) {
esBuildConfig.logOverride = { ...esBuildConfig.logOverride, ...customEsBuildConfig.logOverride };
} else {
esBuildConfig.logOverride = customEsBuildConfig.logOverride;
}
}

return esBuildConfig;
};
3 changes: 3 additions & 0 deletions src/lib/esbuild/parseCustomEsbuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,5 +198,8 @@ export const parseCustomEsbuild = (customConfig: BuildOptions) => {
customEsBuild.packages = customConfig.packages;
}

if (customConfig.logOverride && typeof customConfig.logOverride == "object") {
customEsBuild.logOverride = customConfig.logOverride;
}
return customEsBuild;
};
48 changes: 17 additions & 31 deletions src/lib/utils/readDefineConfig.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,36 @@
import path from "path";
import { log } from "./colorize";
import esbuild from "esbuild";
import { createRequire } from "node:module";
import vm from "vm";
import { fileURLToPath, pathToFileURL } from "url";
import { rm } from "fs/promises";

const jsExt = ["js", "mjs", "cjs", "ts", "cts", "mts"];

const readFromPath = async (sourcefile: string) => {
const { href } = pathToFileURL(sourcefile);
const filename = fileURLToPath(href);
const exports = {};
const context = {
require: createRequire(href),
exports,
module: {
exports,
},
__filename: filename,
__dirname: path.dirname(filename),
};
const tt = await esbuild.build({
write: false,
const dir = path.dirname(sourcefile);
const fname = path.basename(sourcefile, path.extname(sourcefile));
const outfile = `${dir}/__${fname}.mjs`;

await esbuild.build({
outfile,
entryPoints: [sourcefile],
bundle: true,
packages: "external",
platform: "node",
format: "cjs",
format: "esm",
target: "ES2018",
// supported: {
// "top-level-await": true, // TODO: esbuild ask for cjs + top-level-await support
// },
banner: {
js: `async (${Object.keys(context).join(",")})=>{`,
js: `import { createRequire } from 'module';const require = createRequire(import.meta.url);const __filename = "${outfile}";const __dirname = "${dir}";`,
},
footer: {
js: "\nreturn module}",
},
});
const fn = vm.runInThisContext(tt.outputFiles[0].text, {
filename: sourcefile,
});

const res = await fn(...Object.values(context));

return res.exports;
try {
const config = await import(outfile);
return config;
} catch (error) {
throw error;
} finally {
await rm(outfile);
}
};

export const readDefineConfig = async (config: string) => {
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/lambda/streamEncoder.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { ServerResponse } from "http";
import { EventStreamCodec } from "@aws-sdk/eventstream-codec";
import { EventStreamCodec } from "@smithy/eventstream-codec";

import type { MessageHeaders } from "@aws-sdk/eventstream-codec";
import type { MessageHeaders } from "@smithy/eventstream-codec";
import { CommonEventGenerator } from "./events/common";

interface ErrorMessage {
Expand Down
Loading

0 comments on commit e2e48b7

Please sign in to comment.