From ae1eecc89fec101743251c002fb378405c6e53ac Mon Sep 17 00:00:00 2001 From: jordy25519 Date: Fri, 6 Dec 2024 07:44:06 +0800 Subject: [PATCH 1/5] Shrink docker image (#27) * Shrink docker image * Add build deps for bigint-buffer --- Dockerfile | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/Dockerfile b/Dockerfile index 554c9c7..0ad676c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,8 +1,8 @@ -FROM public.ecr.aws/bitnami/node:18 -RUN apt-get install git -ENV NODE_ENV=production -RUN npm install -g typescript +FROM node:18 AS builder +RUN apt update -y && apt install git build-essential make python3 -y +RUN npm install -g typescript @vercel/ncc +ENV NODE_ENV=production WORKDIR /app COPY . . WORKDIR /app/drift-common/protocol/sdk @@ -14,7 +14,12 @@ RUN yarn build WORKDIR /app RUN yarn RUN yarn build +RUN ncc build lib/index.js -o dist +FROM node:18-alpine +WORKDIR /app +COPY --from=builder /app/dist/ dist/ +ENV NODE_ENV=production EXPOSE 9464 -CMD [ "yarn", "start" ] \ No newline at end of file +CMD ["node", "./dist/index.js"] \ No newline at end of file From 8eec5cc99c28749471906b6e9aa03a87dbb8bee8 Mon Sep 17 00:00:00 2001 From: jordy25519 Date: Fri, 6 Dec 2024 07:53:09 +0800 Subject: [PATCH 2/5] Revert "Shrink docker image (#27)" This reverts commit ae1eecc89fec101743251c002fb378405c6e53ac. --- Dockerfile | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/Dockerfile b/Dockerfile index 0ad676c..554c9c7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,8 +1,8 @@ -FROM node:18 AS builder -RUN apt update -y && apt install git build-essential make python3 -y -RUN npm install -g typescript @vercel/ncc - +FROM public.ecr.aws/bitnami/node:18 +RUN apt-get install git ENV NODE_ENV=production +RUN npm install -g typescript + WORKDIR /app COPY . . WORKDIR /app/drift-common/protocol/sdk @@ -14,12 +14,7 @@ RUN yarn build WORKDIR /app RUN yarn RUN yarn build -RUN ncc build lib/index.js -o dist -FROM node:18-alpine -WORKDIR /app -COPY --from=builder /app/dist/ dist/ -ENV NODE_ENV=production EXPOSE 9464 -CMD ["node", "./dist/index.js"] \ No newline at end of file +CMD [ "yarn", "start" ] \ No newline at end of file From 9f857656ed280e810e52e2c2c17cbbf98a17b0e6 Mon Sep 17 00:00:00 2001 From: jordy25519 Date: Wed, 11 Dec 2024 14:12:17 +0800 Subject: [PATCH 3/5] Chore/shrink docker image (#28) * shrink docker image (esbuild + multistage docker build) --- Dockerfile | 36 ++++++++++++++++++++++++------------ esbuild.config.js | 20 ++++++++++++++++++++ package.json | 11 +++++------ src/index.ts | 4 ---- 4 files changed, 49 insertions(+), 22 deletions(-) create mode 100644 esbuild.config.js diff --git a/Dockerfile b/Dockerfile index 554c9c7..7ce6c38 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,20 +1,32 @@ -FROM public.ecr.aws/bitnami/node:18 -RUN apt-get install git -ENV NODE_ENV=production -RUN npm install -g typescript +FROM node:18 AS builder WORKDIR /app -COPY . . + +# Install build dependencies +RUN apt update -y && apt install git build-essential make python3 -y +# Copy package files first to leverage cache +COPY package.json yarn.lock ./ +COPY drift-common/protocol/sdk/package.json ./drift-common/protocol/sdk/ +COPY drift-common/common-ts/package.json ./drift-common/common-ts/ + +ENV NODE_ENV=production + WORKDIR /app/drift-common/protocol/sdk -RUN yarn -RUN yarn build +COPY drift-common/protocol/sdk/ . +RUN yarn && yarn build + WORKDIR /app/drift-common/common-ts -RUN yarn -RUN yarn build +COPY drift-common/common-ts/ . +RUN yarn && yarn build + WORKDIR /app -RUN yarn -RUN yarn build +COPY . . +RUN yarn && yarn build +FROM node:18-alpine +COPY --from=builder /app/dist/ ./lib/ + +ENV NODE_ENV=production EXPOSE 9464 -CMD [ "yarn", "start" ] \ No newline at end of file +CMD ["node", "./lib/index.js"] \ No newline at end of file diff --git a/esbuild.config.js b/esbuild.config.js new file mode 100644 index 0000000..0ed88bc --- /dev/null +++ b/esbuild.config.js @@ -0,0 +1,20 @@ +const esbuild = require('esbuild'); + +const commonConfig = { + bundle: true, + platform: 'node', + target: 'node18', + sourcemap: true, + // minify: true, makes messy debug/error output + treeShaking: true, + legalComments: 'none', + mainFields: ['module', 'main'], + metafile: true, + format: 'cjs' +}; + +esbuild.build({ + ...commonConfig, + entryPoints: ['src/index.ts', 'src/wsConnectionManager.ts'], + outdir: 'dist', +}).catch(() => process.exit(1)); \ No newline at end of file diff --git a/package.json b/package.json index 2fdcaf8..02d5cb5 100644 --- a/package.json +++ b/package.json @@ -21,27 +21,26 @@ "start": "ts-node src/index.ts", "ws-manager": "ts-node src/wsConnectionManager.ts", "playground": "ts-node src/playground.ts", - "build": "yarn clean && tsc", + "build": "yarn clean && node esbuild.config.js", "clean": "rm -rf lib", "prettify": "prettier --check './src/**/*.ts'", - "prettify:fix": "prettier --write './src/**/*.ts'" + "prettify:fix": "prettier --write './src/**/*.ts'" }, "dependencies": { "@aws-sdk/client-ssm": "^3.535.0", "@aws-sdk/credential-provider-node": "^3.535.0", "@coral-xyz/anchor": "^0.29.0", - "@drift-labs/sdk": "file:./drift-common/protocol/sdk", + "@drift-labs/sdk": "file:./drift-common/protocol/sdk", "@drift/common": "file:./drift-common/common-ts", - "@grpc/grpc-js": "^1.8.0", + "@grpc/grpc-js": "^1.8.0", "@triton-one/yellowstone-grpc": "0.6.0", "buffer": "^6.0.3", "compression": "^1.7.4", "cors": "^2.8.5", "dotenv": "^16.4.5", + "esbuild": "^0.20.1", "express": "^4.19.1", - "ioredis": "^5.3.2", "prom-client": "^15.1.0", - "rxjs": "^7.8.1", "ws": "^8.16.0" } } diff --git a/src/index.ts b/src/index.ts index 18d6314..bc296a7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,15 +8,11 @@ import { DriftEnv, EventType, ResubOpts, - SwiftOrderRecord, Wallet, } from "@drift-labs/sdk"; import { ClientDuplexStream } from "@grpc/grpc-js"; import { Connection, Keypair } from "@solana/web3.js"; -import { fromEventPattern } from "rxjs"; import { parseLogsWithRaw } from "@drift-labs/sdk"; -import { SSMClient, GetParameterCommand } from "@aws-sdk/client-ssm"; // ES Modules import -import Redis from "ioredis"; import { bs58 } from "@coral-xyz/anchor/dist/cjs/utils/bytes"; import { getSerializerFromEventType } from "./utils/utils"; import { RedisClient } from "@drift/common/clients"; From 754866bf1fee22517858e38546cc3ade4dda0891 Mon Sep 17 00:00:00 2001 From: jordy25519 Date: Wed, 11 Dec 2024 17:21:03 +0800 Subject: [PATCH 4/5] use bun for build (#29) --- Dockerfile | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/Dockerfile b/Dockerfile index 7ce6c38..86ef793 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,26 +2,27 @@ FROM node:18 AS builder WORKDIR /app -# Install build dependencies -RUN apt update -y && apt install git build-essential make python3 -y # Copy package files first to leverage cache COPY package.json yarn.lock ./ COPY drift-common/protocol/sdk/package.json ./drift-common/protocol/sdk/ COPY drift-common/common-ts/package.json ./drift-common/common-ts/ +# Install build dependencies +RUN npm install -g bun + ENV NODE_ENV=production WORKDIR /app/drift-common/protocol/sdk COPY drift-common/protocol/sdk/ . -RUN yarn && yarn build +RUN bun install && bun run build WORKDIR /app/drift-common/common-ts COPY drift-common/common-ts/ . -RUN yarn && yarn build +RUN bun install && bun run build WORKDIR /app COPY . . -RUN yarn && yarn build +RUN bun install && bun run build FROM node:18-alpine COPY --from=builder /app/dist/ ./lib/ From 81d3edb17a4e4cea454b19e0322b235c9bbab37d Mon Sep 17 00:00:00 2001 From: jordy25519 Date: Mon, 16 Dec 2024 08:39:31 +0800 Subject: [PATCH 5/5] add missing docker image tags --- .github/workflows/mainnet-beta.yml | 2 +- .github/workflows/master.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/mainnet-beta.yml b/.github/workflows/mainnet-beta.yml index d2e6647..c697dc3 100644 --- a/.github/workflows/mainnet-beta.yml +++ b/.github/workflows/mainnet-beta.yml @@ -35,7 +35,7 @@ jobs: context: . push: true tags: | - ${{ env.ECR_REGISTRY }}/${{ env.ECR_REPOSITORY }}:${{ env.IMAGE_TAG }} + ${{ env.ECR_REGISTRY }}/${{ env.ECR_REPOSITORY }}:${{ env.IMAGE_TAG }}-${{ env.BRANCH_NAME }}-amd64 ${{ env.ECR_REGISTRY }}/${{ env.ECR_REPOSITORY }}:latest-${{ env.BRANCH_NAME }}-amd64 deploy: diff --git a/.github/workflows/master.yml b/.github/workflows/master.yml index 7a06e20..f16e3d6 100644 --- a/.github/workflows/master.yml +++ b/.github/workflows/master.yml @@ -36,7 +36,7 @@ jobs: context: . push: true tags: | - ${{ env.ECR_REGISTRY }}/${{ env.ECR_REPOSITORY }}:${{ env.IMAGE_TAG }} + ${{ env.ECR_REGISTRY }}/${{ env.ECR_REPOSITORY }}:${{ env.IMAGE_TAG }}-${{ env.BRANCH_NAME }}-amd64 ${{ env.ECR_REGISTRY }}/${{ env.ECR_REPOSITORY }}:latest-${{ env.BRANCH_NAME }}-amd64 deploy: