Skip to content

Commit

Permalink
Example non-included SDK (#410)
Browse files Browse the repository at this point in the history
* Add non-included SDK sample

* remove comment
  • Loading branch information
richarddavison authored Jun 6, 2024
1 parent 716e40d commit e67efd7
Show file tree
Hide file tree
Showing 14 changed files with 1,607 additions and 966 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ run: export _EXIT_ITERATIONS = 1
run: export JS_MINIFY = 0
run: export RUST_LOG = llrt=trace
run: export _HANDLER = index.handler
run: | clean-js js
run:
cargo run -vv

run-ssr: export AWS_LAMBDA_RUNTIME_API = localhost:3000
Expand Down
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ LLRT can work with any bundler of your choice. Below are some configurations for

### ESBuild

esbuild index.js --platform=node --target=es2020 --format=esm --bundle --minify --external:@aws-sdk --external:@smithy --external:uuid
```shell
esbuild index.js --platform=node --target=es2020 --format=esm --bundle --minify --external:@aws-sdk --external:@smithy --external:uuid
```

### Rollup

Expand Down Expand Up @@ -194,7 +196,7 @@ export default {
## Using AWS SDK (v3) with LLRT

LLRT includes many AWS SDK clients and utils as part of the runtime, built into the executable. These SDK Clients have been specifically fine-tuned to offer best performance while not compromising on compatibility. LLRT replaces some JavaScript dependencies used by the AWS SDK by native ones such as Hash calculations and XML parsing.
V3 SDK packages not included in the list below have to be bundled with your source code while marking the following packages as external:
V3 SDK packages not included in the list below have to be bundled with your source code. For an example on how to use a non-included SDK, see [this example build script (buildExternalSdkFunction)](example/functions/build.mjs)

| Bundled AWS SDK packages |
| ----------------------------------------- |
Expand All @@ -219,7 +221,9 @@ V3 SDK packages not included in the list below have to be bundled with your sour
| @aws-sdk/lib-dynamodb |
| @aws-sdk/s3-request-presigner |
| @aws-sdk/util-dynamodb |
| @aws-sdk/util-user-agent-browser |
| @smithy |
| @aws-crypto |

> [!IMPORTANT]
> LLRT currently does not support returning streams from SDK responses. Use `response.Body.transformToString();` or `response.Body.transformToByteArray();` as shown below.
Expand Down
86 changes: 33 additions & 53 deletions build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,11 @@ const SDK_DATA = {
};

const ADDITIONAL_PACKAGES = [
"@aws-sdk/core",
"@aws-sdk/util-dynamodb",
"@aws-sdk/credential-providers",
"@aws-sdk/s3-request-presigner",
"@aws-sdk/util-user-agent-browser",
"@smithy/config-resolver",
"@smithy/core",
"@smithy/eventstream-codec",
Expand Down Expand Up @@ -140,6 +142,13 @@ const ADDITIONAL_PACKAGES = [
"@smithy/util-waiter",
];

const REPLACEMENT_PACKAGES = {
"@aws-crypto/sha1-browser": "shims/aws-crypto-sha1.js",
"@aws-crypto/sha256-browser": "shims/aws-crypto-sha256.js",
"@aws-crypto/crc32": "shims/aws-crypto-crc32.js",
"@aws-crypto/crc32c": "shims/aws-crypto-crc32c.js",
};

const SERVICE_ENDPOINT_BY_PACKAGE = {};
const CLIENTS_BY_SDK = {};
const SDKS_BY_SDK_PACKAGES = {};
Expand All @@ -154,7 +163,7 @@ Object.keys(SDK_DATA).forEach((sdk) => {
CLIENTS_BY_SDK[sdk] = clientName;
});

function runtimeConfigWrapper(config) {
function resolveDefaultsModeConfigWrapper(config) {
if (!config.credentials) {
config.credentials = {
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
Expand All @@ -165,7 +174,7 @@ function runtimeConfigWrapper(config) {
if (!config.region) {
config.region = process.env.AWS_REGION;
}
return getRuntimeConfig(config);
return resolveDefaultsModeConfig(config);
}

const awsJsonSharedCommand = (name, input, context, request) => {
Expand Down Expand Up @@ -202,9 +211,9 @@ function defaultEndpointResolver(endpointParams, context = {}) {

const WRAPPERS = [
{
name: "getRuntimeConfig",
filter: /runtimeConfig\.shared\.js$/,
wrapper: runtimeConfigWrapper,
name: "resolveDefaultsModeConfig",
filter: /resolveDefaultsModeConfig.js$/,
wrapper: resolveDefaultsModeConfigWrapper,
},
];

Expand Down Expand Up @@ -511,42 +520,6 @@ async function loadShims() {
]);
}

const PACKAGE_NAME_CACHE = {};
async function findPackageName(filePath) {
const firstDir = path.dirname(filePath);

if (PACKAGE_NAME_CACHE[firstDir]) {
return PACKAGE_NAME_CACHE[firstDir];
}

let currentDir = firstDir;
while (true) {
const packageJsonPath = path.join(currentDir, "package.json");

const packageJsonExists = await fs
.access(packageJsonPath)
.then(() => true)
.catch(() => false);

if (packageJsonExists) {
const packageJsonContent = await fs.readFile(packageJsonPath, "utf8");
const packageJson = JSON.parse(packageJsonContent);

if (packageJson && packageJson.name) {
PACKAGE_NAME_CACHE[firstDir] = packageJson.name;
return packageJson.name;
}
}

const parentDir = path.dirname(currentDir);
if (parentDir === currentDir) {
return null;
}

currentDir = parentDir;
}
}

async function buildLibrary() {
const defaultLibEsBuildOption = {
chunkNames: "llrt-[name]-runtime-[hash]",
Expand Down Expand Up @@ -604,18 +577,25 @@ async function buildSdks() {

const sdkEntryPoints = Object.fromEntries(sdkEntryList);

await esbuild.build({
entryPoints: sdkEntryPoints,
plugins: [AWS_SDK_PLUGIN, esbuildShimPlugin([[/^bowser$/]])],
alias: {
"@aws-sdk/util-utf8": "@aws-sdk/util-utf8-browser",
"fast-xml-parser": "xml",
"@smithy/md5-js": "crypto",
},
chunkNames: "llrt-[name]-sdk-[hash]",
metafile: true,
...ES_BUILD_OPTIONS,
});
await Promise.all([
esbuild.build({
entryPoints: sdkEntryPoints,
plugins: [AWS_SDK_PLUGIN, esbuildShimPlugin([[/^bowser$/]])],
alias: {
"@aws-sdk/util-utf8": "@aws-sdk/util-utf8-browser",
"fast-xml-parser": "xml",
"@smithy/md5-js": "crypto",
},
chunkNames: "llrt-[name]-sdk-[hash]",
metafile: true,
...ES_BUILD_OPTIONS,
}),
esbuild.build({
entryPoints: REPLACEMENT_PACKAGES,
...ES_BUILD_OPTIONS,
sourcemap: false,
}),
]);

//console.log(await esbuild.analyzeMetafile(result.metafile));
}
Expand Down
101 changes: 68 additions & 33 deletions example/functions/build.mjs
Original file line number Diff line number Diff line change
@@ -1,39 +1,74 @@
import esbuild from "esbuild";
import fs from "fs/promises";
import path from "path";

const OUTDIR = "build";
const OUT_JS_INDEX = `${OUTDIR}/index.mjs`;

await fs.rm(OUTDIR, { recursive: true, force: true });
await fs.mkdir(OUTDIR);
await fs.copyFile("src/react/index.html", `${OUTDIR}/index.html`);

const devMode = process.argv.slice(2)[0] == "--dev";

await esbuild.build({
entryPoints: {
index: "src/ssr.ts",
app: "src/react/index.tsx",
},
logLevel: "info",
...(!devMode && {
platform: "node",
}),
external: ["@aws-sdk"],
target: "es2020",
format: devMode ? "cjs" : "esm",
define: {
"process.env.NODE_ENV": JSON.stringify("production"),
},
loader: {
".svg": "file",
},
bundle: true,
outdir: OUTDIR,
});

await fs.rename(`${OUTDIR}/index.js`, OUT_JS_INDEX);
await fs.readFile(OUT_JS_INDEX).then((data) => {
const indexSource = `import { createRequire } from "module";\nconst require = createRequire(import.meta.url);\n${data.toString()}`;
return fs.writeFile(OUT_JS_INDEX, indexSource);
});

async function buildReact() {
const outbase = path.join(OUTDIR, "react");
const outfile = path.join(outbase, "index.mjs");

await fs.mkdir(outbase, { recursive: true });
await fs.copyFile("src/react/index.html", path.join(outbase, "index.html"));

const devMode = process.argv.slice(2)[0] == "--dev";

await esbuild.build({
entryPoints: {
index: "src/ssr.ts",
app: "src/react/index.tsx",
},
logLevel: "info",
...(!devMode && {
platform: "node",
}),
external: ["@aws-sdk"],
target: "es2020",
format: devMode ? "cjs" : "esm",
define: {
"process.env.NODE_ENV": JSON.stringify("production"),
},
loader: {
".svg": "file",
},
bundle: true,
outdir: outbase,
});

await fs.rename(path.join(outbase, "index.js"), outfile);
await fs.readFile(outfile).then((data) => {
const indexSource = `import { createRequire } from "module";\nconst require = createRequire(import.meta.url);\n${data.toString()}`;
return fs.writeFile(outfile, indexSource);
});
}

async function buildExternalSdkFunction() {
const outbase = path.join(OUTDIR, "external");
const outfile = path.join(outbase, "index.mjs");

await esbuild.build({
entryPoints: {
index: "src/non-included-sdk.mjs",
},
logLevel: "info",
platform: "browser",
target: "es2020",
format: "esm",
bundle: true,
minify: true,
sourcemap: false,
outfile,
external: [
"@smithy",
"@aws-sdk/core",
"@aws-sdk/util-user-agent-browser",
"@aws-crypto",
"bowser",
],
});
}

await buildReact();
await buildExternalSdkFunction();
1 change: 1 addition & 0 deletions example/functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
},
"dependencies": {
"@aws-sdk/client-dynamodb": "3.588.0",
"@aws-sdk/client-ec2": "^3.590.0",
"@aws-sdk/client-s3": "3.588.0",
"aws-sdk": "2.1632.0",
"esbuild-css-modules-plugin": "3.1.2",
Expand Down
File renamed without changes.
Loading

0 comments on commit e67efd7

Please sign in to comment.