Use chrome-aws-lambda for assetImage

Okay cool, this one worked! We use this special Chrome package with AWS Lambda support, and then we use normal Playright in dev, and then we exclude `playwright` from the deployment (even though it got auto-detected by `require("playwright")`) to just barely sneak in under the 50MB limit for this function. Phew!

The preview deploys for this seem to be, actually working? So that's exciting!
This commit is contained in:
Emi Matchu 2021-08-19 16:12:12 -07:00
parent 49abb92c03
commit adf70dc25f
4 changed files with 55 additions and 18 deletions

View file

@ -18,8 +18,6 @@ const beeline = require("honeycomb-beeline")({
disableInstrumentationOnLoad: true, disableInstrumentationOnLoad: true,
}); });
const playwright = require("playwright-aws-lambda");
// To render the image, we load the /internal/assetImage page in the web app, // To render the image, we load the /internal/assetImage page in the web app,
// a simple page specifically designed for this API endpoint! // a simple page specifically designed for this API endpoint!
const ASSET_IMAGE_PAGE_BASE_URL = process.env.VERCEL_URL const ASSET_IMAGE_PAGE_BASE_URL = process.env.VERCEL_URL
@ -36,7 +34,20 @@ const ASSET_IMAGE_PAGE_BASE_URL = process.env.VERCEL_URL
let BROWSER; let BROWSER;
async function getBrowser() { async function getBrowser() {
if (!BROWSER) { if (!BROWSER) {
BROWSER = await playwright.launchChromium({ headless: true }); if (process.env["NODE_ENV"] === "production") {
// In production, we use a special chrome-aws-lambda Chromium.
const chromium = require("chrome-aws-lambda");
const playwright = require("playwright-core");
BROWSER = await playwright.chromium.launch({
args: chromium.args,
executablePath: await chromium.executablePath,
headless: true,
});
} else {
// In development, we use the standard playwright Chromium.
const playwright = require("playwright");
BROWSER = await playwright.chromium.launch({ headless: true });
}
} }
return BROWSER; return BROWSER;
} }

View file

@ -27,6 +27,7 @@
"apollo-server-env": "^3.0.0", "apollo-server-env": "^3.0.0",
"aws-sdk": "^2.726.0", "aws-sdk": "^2.726.0",
"canvas": "^2.6.1", "canvas": "^2.6.1",
"chrome-aws-lambda": "^10.1.0",
"dataloader": "^2.0.0", "dataloader": "^2.0.0",
"dompurify": "^2.2.0", "dompurify": "^2.2.0",
"easeljs": "^1.0.2", "easeljs": "^1.0.2",
@ -41,7 +42,6 @@
"lru-cache": "^6.0.0", "lru-cache": "^6.0.0",
"mysql2": "^2.1.0", "mysql2": "^2.1.0",
"node-fetch": "^2.6.0", "node-fetch": "^2.6.0",
"playwright-aws-lambda": "^0.7.0",
"playwright-core": "^1.14.0", "playwright-core": "^1.14.0",
"react": "^17.0.1", "react": "^17.0.1",
"react-autosuggest": "^10.0.2", "react-autosuggest": "^10.0.2",
@ -138,6 +138,7 @@
"inquirer": "^7.3.3", "inquirer": "^7.3.3",
"jest-image-snapshot": "^4.3.0", "jest-image-snapshot": "^4.3.0",
"lint-staged": "^10.5.4", "lint-staged": "^10.5.4",
"playwright": "^1.14.0",
"prettier": "^2.0.5", "prettier": "^2.0.5",
"react-is": "^16.13.1", "react-is": "^16.13.1",
"ts-node": "^9.1.1", "ts-node": "^9.1.1",

View file

@ -22,5 +22,10 @@
"src": "/static/js/[^.]*\\.[^.]*\\.chunk.js", "src": "/static/js/[^.]*\\.[^.]*\\.chunk.js",
"status": 404 "status": 404
} }
] ],
"functions": {
"api/assetImage.js": {
"excludeFiles": "node_modules/playwright/**"
}
}
} }

View file

@ -7768,6 +7768,13 @@ chownr@^2.0.0:
resolved "https://registry.yarnpkg.com/chownr/-/chownr-2.0.0.tgz#15bfbe53d2eab4cf70f18a8cd68ebe5b3cb1dece" resolved "https://registry.yarnpkg.com/chownr/-/chownr-2.0.0.tgz#15bfbe53d2eab4cf70f18a8cd68ebe5b3cb1dece"
integrity sha512-bIomtDF5KGpdogkLd9VspvFzk9KfpyyGlS8YFVZl7TGPBHL5snIOnxeshwVgPteQ9b4Eydl+pVbIyE1DcvCWgQ== integrity sha512-bIomtDF5KGpdogkLd9VspvFzk9KfpyyGlS8YFVZl7TGPBHL5snIOnxeshwVgPteQ9b4Eydl+pVbIyE1DcvCWgQ==
chrome-aws-lambda@^10.1.0:
version "10.1.0"
resolved "https://registry.yarnpkg.com/chrome-aws-lambda/-/chrome-aws-lambda-10.1.0.tgz#ac43b4cdfc1fbb2275c62effada560858099501e"
integrity sha512-NZQVf+J4kqG4sVhRm3WNmOfzY0OtTSm+S8rg77pwePa9RCYHzhnzRs8YvNI6L9tALIW6RpmefWiPURt3vURXcw==
dependencies:
lambdafs "^2.0.3"
chrome-trace-event@^1.0.2: chrome-trace-event@^1.0.2:
version "1.0.2" version "1.0.2"
resolved "https://registry.yarnpkg.com/chrome-trace-event/-/chrome-trace-event-1.0.2.tgz#234090ee97c7d4ad1a2c4beae27505deffc608a4" resolved "https://registry.yarnpkg.com/chrome-trace-event/-/chrome-trace-event-1.0.2.tgz#234090ee97c7d4ad1a2c4beae27505deffc608a4"
@ -12930,12 +12937,12 @@ kleur@^3.0.3:
resolved "https://registry.yarnpkg.com/kleur/-/kleur-3.0.3.tgz#a79c9ecc86ee1ce3fa6206d1216c501f147fc07e" resolved "https://registry.yarnpkg.com/kleur/-/kleur-3.0.3.tgz#a79c9ecc86ee1ce3fa6206d1216c501f147fc07e"
integrity sha512-eTIzlVOSUR+JxdDFepEYcBMtZ9Qqdef+rnzWdRZuMbOywu5tO2w2N7rqjoANZ5k9vywhL6Br1VRjUIgTQx4E8w== integrity sha512-eTIzlVOSUR+JxdDFepEYcBMtZ9Qqdef+rnzWdRZuMbOywu5tO2w2N7rqjoANZ5k9vywhL6Br1VRjUIgTQx4E8w==
lambdafs@^1.3.0: lambdafs@^2.0.3:
version "1.3.0" version "2.0.3"
resolved "https://registry.yarnpkg.com/lambdafs/-/lambdafs-1.3.0.tgz#7e369cedc9a09623bb365fa99a1113c2ab2fc7ae" resolved "https://registry.yarnpkg.com/lambdafs/-/lambdafs-2.0.3.tgz#f2acc53cf4862d29f1df654c327864b4faeb1d4a"
integrity sha512-HqRPmEgtkTW4sCYDUjTEuTGkjCHuLvtZU8iM8GkhD7SpjW4AJJbBk86YU4K43sWGuW5Vmzp1lVCx4ab/kJsuBw== integrity sha512-5YWwZA/QKk09GdfcJ/ABVO+bpFoGlnTBa5jmyM8Kt9yIzl2lDDVBPLK+Aenq2UEcuDpqxXIYI5zLB7VZNepTrg==
dependencies: dependencies:
tar-fs "^2.0.0" tar-fs "^2.1.1"
language-subtag-registry@~0.3.2: language-subtag-registry@~0.3.2:
version "0.3.21" version "0.3.21"
@ -14886,13 +14893,6 @@ pkg-up@3.1.0, pkg-up@^3.1.0:
dependencies: dependencies:
find-up "^3.0.0" find-up "^3.0.0"
playwright-aws-lambda@^0.7.0:
version "0.7.0"
resolved "https://registry.yarnpkg.com/playwright-aws-lambda/-/playwright-aws-lambda-0.7.0.tgz#6bef962700dc0053ff306ffbc04a11f26a994810"
integrity sha512-vmcXA98LioRhcQg9v+xSLk/HHpkfuOyPQTFGx02rtUa00AEYuMQlNIEMSFn50Sm7R8cwQehyzI3FdKcWhYSAdA==
dependencies:
lambdafs "^1.3.0"
playwright-core@^1.14.0: playwright-core@^1.14.0:
version "1.14.0" version "1.14.0"
resolved "https://registry.yarnpkg.com/playwright-core/-/playwright-core-1.14.0.tgz#af51da7b201c11eeda780e2db3f05c8bca74c8be" resolved "https://registry.yarnpkg.com/playwright-core/-/playwright-core-1.14.0.tgz#af51da7b201c11eeda780e2db3f05c8bca74c8be"
@ -14913,6 +14913,26 @@ playwright-core@^1.14.0:
ws "^7.4.6" ws "^7.4.6"
yazl "^2.5.1" yazl "^2.5.1"
playwright@^1.14.0:
version "1.14.0"
resolved "https://registry.yarnpkg.com/playwright/-/playwright-1.14.0.tgz#18301b11f5278a446d36b5cf96f67db36ce2cd20"
integrity sha512-aR5oZ1iVsjQkGfYCjgYAmyMAVu0MQ0i8MgdnfdqDu9EVLfbnpuuFmTv/Rb7/Yjno1kOrDUP9+RyNC+zfG3wozA==
dependencies:
commander "^6.1.0"
debug "^4.1.1"
extract-zip "^2.0.1"
https-proxy-agent "^5.0.0"
jpeg-js "^0.4.2"
mime "^2.4.6"
pngjs "^5.0.0"
progress "^2.0.3"
proper-lockfile "^4.1.1"
proxy-from-env "^1.1.0"
rimraf "^3.0.2"
stack-utils "^2.0.3"
ws "^7.4.6"
yazl "^2.5.1"
please-upgrade-node@^3.2.0: please-upgrade-node@^3.2.0:
version "3.2.0" version "3.2.0"
resolved "https://registry.yarnpkg.com/please-upgrade-node/-/please-upgrade-node-3.2.0.tgz#aeddd3f994c933e4ad98b99d9a556efa0e2fe942" resolved "https://registry.yarnpkg.com/please-upgrade-node/-/please-upgrade-node-3.2.0.tgz#aeddd3f994c933e4ad98b99d9a556efa0e2fe942"
@ -18090,7 +18110,7 @@ tapable@^1.0.0, tapable@^1.1.3:
resolved "https://registry.yarnpkg.com/tapable/-/tapable-1.1.3.tgz#a1fccc06b58db61fd7a45da2da44f5f3a3e67ba2" resolved "https://registry.yarnpkg.com/tapable/-/tapable-1.1.3.tgz#a1fccc06b58db61fd7a45da2da44f5f3a3e67ba2"
integrity sha512-4WK/bYZmj8xLr+HUCODHGF1ZFzsYffasLUgEiMBY4fgtltdO6B4WJtlSbPaDTLpYTcGVwM2qLnFTICEcNxs3kA== integrity sha512-4WK/bYZmj8xLr+HUCODHGF1ZFzsYffasLUgEiMBY4fgtltdO6B4WJtlSbPaDTLpYTcGVwM2qLnFTICEcNxs3kA==
tar-fs@^2.0.0: tar-fs@^2.1.1:
version "2.1.1" version "2.1.1"
resolved "https://registry.yarnpkg.com/tar-fs/-/tar-fs-2.1.1.tgz#489a15ab85f1f0befabb370b7de4f9eb5cbe8784" resolved "https://registry.yarnpkg.com/tar-fs/-/tar-fs-2.1.1.tgz#489a15ab85f1f0befabb370b7de4f9eb5cbe8784"
integrity sha512-V0r2Y9scmbDRLCNex/+hYzvp/zyYjvFbHPNgVTKfQvVrb6guiE/fxP+XblDNR011utopbkex2nM4dHNV6GDsng== integrity sha512-V0r2Y9scmbDRLCNex/+hYzvp/zyYjvFbHPNgVTKfQvVrb6guiE/fxP+XblDNR011utopbkex2nM4dHNV6GDsng==