-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
197 lines (167 loc) · 6.1 KB
/
Copy pathindex.js
File metadata and controls
197 lines (167 loc) · 6.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
const fs = require("fs");
const crypto = require("crypto");
const puppeteer = require("puppeteer-core");
const chromium = require("@sparticuz/chromium");
const {
S3Client,
GetObjectCommand,
PutObjectCommand,
} = require("@aws-sdk/client-s3");
const imagePath = "/tmp/screenshot.jpg";
const baseUrl = "https://exercism.org";
// Generating an image costs a few seconds of headless Chrome at 2GB, so we only
// ever want to pay for it once per distinct URL. CDN edge caches can't give us
// that on their own: they're per-PoP, they evict the long tail (most images are
// fetched a handful of times ever), and a flood of distinct URLs misses them
// entirely. Writing through to S3 makes the cost a function of how many images
// exist rather than how many times they're requested.
const bucket = process.env.IMAGE_BUCKET || "exercism-v3-assets";
const keyPrefix = process.env.IMAGE_KEY_PREFIX || "generated-images";
const s3 = new S3Client({});
// These must stay comfortably under the Lambda's 20s timeout. puppeteer
// defaults both to 30s, which is longer, so before this a render that hung
// burned the full 20s at 2GB rather than failing fast.
const navigationTimeout = parseInt(process.env.NAVIGATION_TIMEOUT_MS || "6000", 10);
const selectorTimeout = parseInt(process.env.SELECTOR_TIMEOUT_MS || "6000", 10);
const legacyMaxAge = 86400;
const solutionRegex = /^\/tracks\/(?<track_slug>.+?)\/exercises\/(?<exercise_slug>.+?)\/solutions\/(?<user_handle>.+?)(?:-\d{10})?\.jpg$/;
const profileRegex = /^\/profiles\/(?<user_handle>.+?)(?:-\d{10})?\.jpg$/;
function rawPathToScreenshotData(rawPath) {
const solutionMatch = solutionRegex.exec(rawPath);
if (solutionMatch) {
const { track_slug, exercise_slug, user_handle } = solutionMatch.groups;
return {
url: `${baseUrl}/images/solutions/${track_slug}/${exercise_slug}/${user_handle}`,
imageSelector: "#image-content",
waitForSelector: "#image-content .c-code-pane",
};
}
const profileMatch = profileRegex.exec(rawPath);
if (profileMatch) {
const { user_handle } = profileMatch.groups;
return {
url: `${baseUrl}/images/profiles/${user_handle}`,
imageSelector: "#image-content",
waitForSelector: "#image-content #contributions-chart",
};
}
throw new Error(`Could not map raw path '${rawPath}' to image URL.`);
}
// URLs ending in -${timestamp}.jpg address a version that will never change, so
// they can be cached forever. Legacy URLs without one address mutable content.
function cacheMetadata(rawPath) {
const match = rawPath.match(/-(\d{10})\.\w+$/);
const isTimestamped = !!match;
return {
isTimestamped,
cacheControl: isTimestamped
? "public, max-age=31536000, immutable"
: `public, max-age=${legacyMaxAge}`,
lastModified: isTimestamped
? new Date(parseInt(match[1], 10) * 1000).toUTCString()
: new Date().toUTCString(),
};
}
function s3Key(rawPath) {
return `${keyPrefix}${rawPath}`;
}
function imageResponse(imageBuffer, { cacheControl, lastModified }) {
const etag = crypto.createHash("md5").update(imageBuffer).digest("hex");
return {
statusCode: 200,
body: imageBuffer.toString("base64"),
headers: {
"Content-Type": "image/jpg",
"Cache-Control": cacheControl,
"Last-Modified": lastModified,
"Etag": `W/"${etag}"`,
},
isBase64Encoded: true,
};
}
// A cache problem should never stop us serving an image, so every failure here
// falls through to generating one.
async function fetchFromS3(key, { isTimestamped }) {
try {
const object = await s3.send(
new GetObjectCommand({ Bucket: bucket, Key: key })
);
// Legacy URLs point at content that can change, so a stored copy is only
// good for as long as we'd have let a CDN hold onto it.
if (!isTimestamped) {
const age = (Date.now() - object.LastModified.getTime()) / 1000;
if (age > legacyMaxAge) return null;
}
return Buffer.from(await object.Body.transformToByteArray());
} catch (err) {
if (err.name !== "NoSuchKey" && err.name !== "NotFound") {
console.error(`Failed reading ${key} from S3: ${err.message}`);
}
return null;
}
}
async function writeToS3(key, imageBuffer, cacheControl) {
try {
await s3.send(
new PutObjectCommand({
Bucket: bucket,
Key: key,
Body: imageBuffer,
ContentType: "image/jpg",
CacheControl: cacheControl,
})
);
} catch (err) {
console.error(`Failed writing ${key} to S3: ${err.message}`);
}
}
async function generateImage({ url, imageSelector, waitForSelector }) {
const browser = await puppeteer.launch({
executablePath: await chromium.executablePath(),
headless: chromium.headless,
ignoreHTTPSErrors: true,
defaultViewport: { ...chromium.defaultViewport, deviceScaleFactor: 2 },
args: [
...chromium.args,
"--hide-scrollbars",
"--disable-web-security",
"--high-dpi-support=1",
],
});
// Now that a render can fail rather than take the whole container down with
// it, the browser has to be closed on the way out or it leaks into the next
// warm invocation.
try {
const page = await browser.newPage();
page.setDefaultNavigationTimeout(navigationTimeout);
await page.goto(url);
await page.waitForSelector(waitForSelector, { timeout: selectorTimeout });
const image = await page.$(imageSelector);
await image.screenshot({
path: imagePath,
type: "jpeg",
quality: 80,
});
return fs.readFileSync(imagePath);
} finally {
await browser.close();
}
}
exports.handler = async (event) => {
try {
const screenshotData = rawPathToScreenshotData(event.rawPath);
const metadata = cacheMetadata(event.rawPath);
const key = s3Key(event.rawPath);
const cached = await fetchFromS3(key, metadata);
if (cached) return imageResponse(cached, metadata);
const imageBuffer = await generateImage(screenshotData);
await writeToS3(key, imageBuffer, metadata.cacheControl);
return imageResponse(imageBuffer, metadata);
} catch (err) {
console.error(err);
return {
statusCode: 500,
body: err.message,
};
}
};