-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcodesandbox-cache.js
More file actions
361 lines (330 loc) · 11.5 KB
/
Copy pathcodesandbox-cache.js
File metadata and controls
361 lines (330 loc) · 11.5 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
// Cache helpers for /codesandbox-vm and /codesandbox-browser.
//
// Wraps the cold-path GitHub + npm calls in a CompositeCacheStore-backed cache
// so repeat requests skip redundant network I/O. Also exposes an in-memory
// coalescer that collapses concurrent identical requests into a single upstream
// fetch — useful when the cache is cold and N tabs hit the service at once.
//
// All cache writes are best-effort: a failure to read or write the cache must
// never break the request. The store is the same CompositeCacheStore created
// by ./changelog-prs/cache.js (Redis primary, file fallback).
import { Buffer } from "node:buffer";
import { fetchFiles as fetchFilesUncached } from "./github.js";
import { getVersion as getVersionUncached } from "./version.js";
import { validateExampleDirExistsInRepo as validateExampleDirExistsInRepoUncached } from "./validate-query-params.js";
const KEY_PREFIX = "csb-vm:";
// TTLs (seconds)
const EXISTS_TTL_SEC = 10 * 60;
const VERSION_LATEST_TTL_SEC = 60;
const VERSION_BRANCH_TTL_SEC = 5 * 60;
const VERSION_SHA_TTL_SEC = 30 * 24 * 60 * 60;
const TIP_SHA_TTL_SEC = 60;
const FILES_TTL_SEC = 30 * 24 * 60 * 60;
const SANDBOX_ID_TTL_SEC = 24 * 60 * 60;
// Redis values larger than this go to file-only storage (mirrors changelog-prs
// behavior for bulk PR payloads). Some example dirs have multi-MB lockfiles
// which we do NOT want to ship to a managed Redis with a small value cap.
const FILES_REDIS_MAX_BYTES = 5_000_000;
const slug = (v) => (v === undefined || v === null || v === "" ? "-" : String(v));
export function buildSandboxCacheKeys({
exampleDir,
exampleBranch,
handsontableVersion,
handsontableBranch,
handsontableSha,
pkgPrNew,
resolvedVersion,
}) {
const flavor = pkgPrNew ? "pp" : "reg";
const raw = `${KEY_PREFIX}sb-raw:v1/${slug(exampleDir)}/${slug(exampleBranch)}/${slug(handsontableVersion)}/${slug(handsontableBranch)}/${slug(handsontableSha)}/${flavor}`;
const resolved = resolvedVersion
? `${KEY_PREFIX}sb:v1/${slug(exampleDir)}/${slug(exampleBranch)}/${slug(resolvedVersion)}/${flavor}`
: null;
return { raw, resolved };
}
async function getJson(cache, key) {
if (!cache) return null;
try {
const raw = await cache.get(key);
if (raw == null) return null;
return JSON.parse(raw);
} catch {
return null;
}
}
function safeStringify(value) {
try {
return JSON.stringify(value);
} catch {
return null;
}
}
function putJsonAsync(cache, key, value, ttlSec, opts) {
if (!cache) return;
const body = safeStringify(value);
if (body == null) return;
setImmediate(() => {
cache.set(key, body, ttlSec, opts).catch((err) => {
console.warn(`[csb-vm] cache put failed ${key}:`, err?.message || err);
});
});
}
// Sandbox ID cache ----------------------------------------------------------
export async function readCachedSandboxId(cache, keys, opts = {}) {
if (!cache) return null;
if (opts.bypass) {
console.log(`[csb-cache] sandbox-id BYPASS (nocache)`);
return null;
}
for (const k of [keys.raw, keys.resolved]) {
if (!k) continue;
const start = Date.now();
try {
const raw = await cache.get(k);
const elapsed = Date.now() - start;
if (raw) {
const parsed = JSON.parse(raw);
if (parsed && typeof parsed.id === "string" && parsed.id) {
console.log(`[csb-cache] sandbox-id HIT ${k} -> ${parsed.id} (${elapsed}ms)`);
return parsed.id;
}
}
console.log(`[csb-cache] sandbox-id MISS ${k} (${elapsed}ms)`);
} catch (err) {
console.warn(
`[csb-cache] sandbox-id GET error ${k} after ${Date.now() - start}ms: ${err?.message || err}`,
);
}
}
return null;
}
export function writeCachedSandboxId(cache, keys, id) {
if (!cache || !id) return;
const payload = { id, ts: Date.now() };
if (keys.raw) putJsonAsync(cache, keys.raw, payload, SANDBOX_ID_TTL_SEC);
if (keys.resolved) putJsonAsync(cache, keys.resolved, payload, SANDBOX_ID_TTL_SEC);
}
export function invalidateCachedSandboxId(cache, keys) {
if (!cache) return;
for (const k of [keys.raw, keys.resolved]) {
if (!k) continue;
cache.del(k).catch(() => {});
}
}
// validateExampleDirExistsInRepo wrapper ------------------------------------
export async function cachedValidateExampleDirExistsInRepo(
cache,
octokit,
exampleDir,
exampleBranch,
opts = {},
) {
const key = `${KEY_PREFIX}exists:v1/${slug(exampleDir)}/${slug(exampleBranch)}`;
if (opts.bypass) {
console.log(`[csb-cache] exists BYPASS ${exampleDir}@${exampleBranch || "default"}`);
}
const cached = opts.bypass ? null : await getJson(cache, key);
if (cached?.ok === true) {
console.log(`[csb-cache] exists HIT ${exampleDir}@${exampleBranch || "default"}`);
return { ok: true };
}
console.log(
`[csb-cache] exists MISS ${exampleDir}@${exampleBranch || "default"} — calling GitHub …`,
);
const start = Date.now();
const fresh = await validateExampleDirExistsInRepoUncached(
octokit,
exampleDir,
exampleBranch,
);
console.log(
`[csb-cache] exists GitHub call ${Date.now() - start}ms ok=${fresh.ok}`,
);
if (fresh.ok) {
putJsonAsync(cache, key, { ok: true }, EXISTS_TTL_SEC);
}
return fresh;
}
// getVersion wrapper --------------------------------------------------------
export async function cachedGetVersion(
cache,
octokit,
handsontableBranch,
handsontableVersion,
handsontableSha,
opts = {},
) {
if (handsontableVersion) {
console.log(`[csb-cache] version PASSTHROUGH ${handsontableVersion}`);
return handsontableVersion;
}
let ttl;
if (handsontableSha) {
ttl = VERSION_SHA_TTL_SEC;
} else if (handsontableBranch) {
ttl = VERSION_BRANCH_TTL_SEC;
} else {
ttl = VERSION_LATEST_TTL_SEC;
}
const key = `${KEY_PREFIX}ver:v1/${slug(handsontableBranch)}/${slug(handsontableSha)}`;
if (opts.bypass) {
console.log(
`[csb-cache] version BYPASS branch=${handsontableBranch || "-"} sha=${handsontableSha || "-"}`,
);
}
const cached = opts.bypass ? null : await getJson(cache, key);
if (cached && typeof cached.v === "string" && cached.v) {
console.log(
`[csb-cache] version HIT branch=${handsontableBranch || "-"} sha=${handsontableSha || "-"} -> ${cached.v}`,
);
return cached.v;
}
console.log(
`[csb-cache] version MISS branch=${handsontableBranch || "-"} sha=${handsontableSha || "-"} — resolving …`,
);
const start = Date.now();
const v = await getVersionUncached(
octokit,
handsontableBranch,
handsontableVersion,
handsontableSha,
);
console.log(`[csb-cache] version resolved ${Date.now() - start}ms -> ${v}`);
if (typeof v === "string" && v && v !== "latest") {
putJsonAsync(cache, key, { v }, ttl);
}
return v;
}
// fetchFiles wrapper --------------------------------------------------------
//
// Strategy: resolve the ref to a tip SHA (cheap — single API call, also briefly
// cached) and key the file payload by that SHA so the cached blob is immutable.
// On hit we skip the recursive getContent + per-file getBlob avalanche entirely.
async function resolveTipShaCached(cache, octokit, owner, repo, ref, opts = {}) {
const key = `${KEY_PREFIX}tip:v1/${slug(owner)}/${slug(repo)}/${slug(ref)}`;
if (opts.bypass) {
console.log(
`[csb-cache] tip-sha BYPASS ${owner}/${repo}@${ref || "HEAD"}`,
);
}
const cached = opts.bypass ? null : await getJson(cache, key);
if (cached && typeof cached.sha === "string" && cached.sha) {
console.log(`[csb-cache] tip-sha HIT ${owner}/${repo}@${ref || "HEAD"} = ${cached.sha.slice(0, 7)}`);
return cached.sha;
}
const start = Date.now();
try {
const params = { owner, repo, ref: ref || "HEAD" };
const { data } = await octokit.rest.repos.getCommit(params);
const sha = data?.sha;
if (typeof sha === "string" && sha) {
console.log(
`[csb-cache] tip-sha MISS ${owner}/${repo}@${ref || "HEAD"} = ${sha.slice(0, 7)} (${Date.now() - start}ms)`,
);
putJsonAsync(cache, key, { sha }, TIP_SHA_TTL_SEC);
return sha;
}
} catch (err) {
console.warn(
`[csb-cache] tip-sha ERROR ${owner}/${repo}@${ref || "HEAD"} after ${Date.now() - start}ms: ${err?.message || err}`,
);
}
return null;
}
export async function cachedFetchFiles(
cache,
octokit,
owner,
repo,
directory,
options = {},
opts = {},
) {
const ref = options.ref;
const tipShaStart = Date.now();
const tipSha = await resolveTipShaCached(cache, octokit, owner, repo, ref, opts);
console.log(
`[csb-cache] resolveTipSha total ${Date.now() - tipShaStart}ms (sha=${tipSha ? tipSha.slice(0, 7) : "none"})`,
);
if (tipSha) {
const filesKey = `${KEY_PREFIX}files:v1/${slug(owner)}/${slug(repo)}/${slug(directory)}/${tipSha}`;
if (opts.bypass) {
console.log(`[csb-cache] files BYPASS ${directory}@${tipSha.slice(0, 7)}`);
}
const cacheReadStart = Date.now();
const cached = opts.bypass ? null : await getJson(cache, filesKey);
console.log(
`[csb-cache] files cache get ${Date.now() - cacheReadStart}ms hit=${!!cached}`,
);
if (cached && Array.isArray(cached.files)) {
console.log(
`[csb-cache] files HIT ${directory}@${tipSha.slice(0, 7)} (${cached.files.length} files)`,
);
return cached.files.map((f) => ({
path: f.path,
text: f.text || "",
contents: f.text ? Buffer.from(f.text, "utf-8") : null,
}));
}
console.log(
`[csb-cache] files MISS ${directory}@${tipSha.slice(0, 7)} — fetching from GitHub …`,
);
const fetchStart = Date.now();
const fresh = await fetchFilesUncached(octokit, owner, repo, directory, {
...options,
ref: tipSha,
});
console.log(
`[csb-cache] fetchFilesUncached returned ${fresh.length} files in ${Date.now() - fetchStart}ms`,
);
const serializable = fresh.map((f) => ({ path: f.path, text: f.text || "" }));
const body = safeStringify({ files: serializable });
if (body != null) {
const preferFile = body.length > FILES_REDIS_MAX_BYTES;
console.log(
`[csb-cache] files cache put ${(body.length / 1024).toFixed(1)}KB preferFile=${preferFile}`,
);
setImmediate(() => {
cache
?.set(filesKey, body, FILES_TTL_SEC, { preferFile })
.catch((err) => {
console.warn(
`[csb-vm] files cache put failed ${filesKey}:`,
err?.message || err,
);
});
});
}
return fresh;
}
console.warn(
`[csb-cache] tip SHA unavailable — falling back to uncached recursive fetch (slow!)`,
);
const fallbackStart = Date.now();
const fresh = await fetchFilesUncached(octokit, owner, repo, directory, options);
console.log(
`[csb-cache] uncached fetchFiles ${fresh.length} files in ${Date.now() - fallbackStart}ms`,
);
return fresh;
}
// In-flight coalescer ------------------------------------------------------
const inflight = new Map();
/**
* Run `fn` exclusively per `key` within this process. Concurrent callers with
* the same key share the same Promise. Useful to avoid creating duplicate
* sandboxes when N requests hit a cold cache simultaneously.
*
* @template T
* @param {string} key
* @param {() => Promise<T>} fn
* @returns {Promise<T>}
*/
export function coalesce(key, fn) {
const existing = inflight.get(key);
if (existing) return existing;
const promise = (async () => fn())().finally(() => {
if (inflight.get(key) === promise) inflight.delete(key);
});
inflight.set(key, promise);
return promise;
}