|
| 1 | +import { |
| 2 | + S3Client, |
| 3 | + GetObjectCommand, |
| 4 | + PutObjectCommand, |
| 5 | + PutObjectCommandInput, |
| 6 | + ListObjectsV2Command, |
| 7 | +} from "@aws-sdk/client-s3"; |
| 8 | +import path from "node:path"; |
| 9 | +import { error, awsLogger } from "./logger.js"; |
| 10 | +import { loadBuildId } from "./util.js"; |
| 11 | + |
| 12 | +interface CachedFetchValue { |
| 13 | + kind: "FETCH"; |
| 14 | + data: { |
| 15 | + headers: { [k: string]: string }; |
| 16 | + body: string; |
| 17 | + status?: number; |
| 18 | + }; |
| 19 | + revalidate: number; |
| 20 | +} |
| 21 | + |
| 22 | +interface CachedRedirectValue { |
| 23 | + kind: "REDIRECT"; |
| 24 | + props: Object; |
| 25 | +} |
| 26 | + |
| 27 | +interface CachedRouteValue { |
| 28 | + kind: "ROUTE"; |
| 29 | + // this needs to be a RenderResult so since renderResponse |
| 30 | + // expects that type instead of a string |
| 31 | + body: Buffer; |
| 32 | + status: number; |
| 33 | + headers: Record<string, undefined | string | string[]>; |
| 34 | +} |
| 35 | + |
| 36 | +interface CachedImageValue { |
| 37 | + kind: "IMAGE"; |
| 38 | + etag: string; |
| 39 | + buffer: Buffer; |
| 40 | + extension: string; |
| 41 | + isMiss?: boolean; |
| 42 | + isStale?: boolean; |
| 43 | +} |
| 44 | + |
| 45 | +interface IncrementalCachedPageValue { |
| 46 | + kind: "PAGE"; |
| 47 | + // this needs to be a string since the cache expects to store |
| 48 | + // the string value |
| 49 | + html: string; |
| 50 | + pageData: Object; |
| 51 | +} |
| 52 | + |
| 53 | +type IncrementalCacheValue = |
| 54 | + | CachedRedirectValue |
| 55 | + | IncrementalCachedPageValue |
| 56 | + | CachedImageValue |
| 57 | + | CachedFetchValue |
| 58 | + | CachedRouteValue; |
| 59 | + |
| 60 | +interface CacheHandlerContext { |
| 61 | + fs?: never; |
| 62 | + dev?: boolean; |
| 63 | + flushToDisk?: boolean; |
| 64 | + serverDistDir?: string; |
| 65 | + maxMemoryCacheSize?: number; |
| 66 | + _appDir: boolean; |
| 67 | + _requestHeaders: never; |
| 68 | + fetchCacheKeyPrefix?: string; |
| 69 | +} |
| 70 | + |
| 71 | +interface CacheHandlerValue { |
| 72 | + lastModified?: number; |
| 73 | + age?: number; |
| 74 | + cacheState?: string; |
| 75 | + value: IncrementalCacheValue | null; |
| 76 | +} |
| 77 | + |
| 78 | +type Extension = "json" | "html" | "rsc" | "body" | "meta" | "fetch"; |
| 79 | + |
| 80 | +// Expected environment variables |
| 81 | +const { CACHE_BUCKET_NAME, CACHE_BUCKET_KEY_PREFIX, CACHE_BUCKET_REGION } = |
| 82 | + process.env; |
| 83 | + |
| 84 | +export default class S3Cache { |
| 85 | + private client: S3Client; |
| 86 | + private buildId: string; |
| 87 | + |
| 88 | + constructor(_ctx: CacheHandlerContext) { |
| 89 | + this.client = new S3Client({ |
| 90 | + region: CACHE_BUCKET_REGION, |
| 91 | + logger: awsLogger, |
| 92 | + }); |
| 93 | + this.buildId = loadBuildId( |
| 94 | + path.dirname(_ctx.serverDistDir ?? ".next/server") |
| 95 | + ); |
| 96 | + } |
| 97 | + |
| 98 | + async get(key: string, fetchCache?: boolean) { |
| 99 | + return fetchCache ? this.getFetchCache(key) : this.getIncrementalCache(key); |
| 100 | + } |
| 101 | + |
| 102 | + async getFetchCache(key: string) { |
| 103 | + try { |
| 104 | + const { Body, LastModified } = await this.getS3Object(key, "fetch"); |
| 105 | + return { |
| 106 | + lastModified: LastModified?.getTime(), |
| 107 | + value: JSON.parse((await Body?.transformToString()) ?? "{}"), |
| 108 | + } as CacheHandlerValue; |
| 109 | + } catch (e) { |
| 110 | + error("Failed to get fetch cache", e); |
| 111 | + return null; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + async getIncrementalCache(key: string): Promise<CacheHandlerValue | null> { |
| 116 | + const { Contents } = await this.listS3Objects(key); |
| 117 | + const keys = (Contents ?? []).map(({ Key }) => Key); |
| 118 | + |
| 119 | + if (keys.includes(this.buildS3Key(key, "body"))) { |
| 120 | + try { |
| 121 | + const [{ Body, LastModified }, { Body: MetaBody }] = await Promise.all([ |
| 122 | + this.getS3Object(key, "body"), |
| 123 | + this.getS3Object(key, "meta"), |
| 124 | + ]); |
| 125 | + const body = await Body?.transformToByteArray(); |
| 126 | + const meta = JSON.parse((await MetaBody?.transformToString()) ?? "{}"); |
| 127 | + |
| 128 | + return { |
| 129 | + lastModified: LastModified?.getTime(), |
| 130 | + value: { |
| 131 | + kind: "ROUTE", |
| 132 | + body: Buffer.from(body ?? Buffer.alloc(0)), |
| 133 | + status: meta.status, |
| 134 | + headers: meta.headers, |
| 135 | + }, |
| 136 | + } as CacheHandlerValue; |
| 137 | + } catch (e) { |
| 138 | + error("Failed to get body cache", e); |
| 139 | + } |
| 140 | + return null; |
| 141 | + } |
| 142 | + |
| 143 | + if (keys.includes(this.buildS3Key(key, "html"))) { |
| 144 | + const isJson = keys.includes(this.buildS3Key(key, "json")); |
| 145 | + const isRsc = keys.includes(this.buildS3Key(key, "rsc")); |
| 146 | + if (!isJson && !isRsc) return null; |
| 147 | + |
| 148 | + try { |
| 149 | + const [{ Body, LastModified }, { Body: PageBody }] = await Promise.all([ |
| 150 | + this.getS3Object(key, "html"), |
| 151 | + this.getS3Object(key, isJson ? "json" : "rsc"), |
| 152 | + ]); |
| 153 | + |
| 154 | + return { |
| 155 | + lastModified: LastModified?.getTime(), |
| 156 | + value: { |
| 157 | + kind: "PAGE", |
| 158 | + html: (await Body?.transformToString()) ?? "", |
| 159 | + pageData: isJson |
| 160 | + ? JSON.parse((await PageBody?.transformToString()) ?? "{}") |
| 161 | + : await PageBody?.transformToString(), |
| 162 | + }, |
| 163 | + } as CacheHandlerValue; |
| 164 | + } catch (e) { |
| 165 | + error("Failed to get html cache", e); |
| 166 | + } |
| 167 | + return null; |
| 168 | + } |
| 169 | + return null; |
| 170 | + } |
| 171 | + |
| 172 | + async set(key: string, data?: IncrementalCacheValue): Promise<void> { |
| 173 | + if (data?.kind === "ROUTE") { |
| 174 | + const { body, status, headers } = data; |
| 175 | + await Promise.all([ |
| 176 | + this.putS3Object(key, "body", body), |
| 177 | + this.putS3Object(key, "meta", JSON.stringify({ status, headers })), |
| 178 | + ]); |
| 179 | + } else if (data?.kind === "PAGE") { |
| 180 | + const { html, pageData } = data; |
| 181 | + const isAppPath = typeof pageData === "string"; |
| 182 | + await Promise.all([ |
| 183 | + this.putS3Object(key, "html", html), |
| 184 | + this.putS3Object( |
| 185 | + key, |
| 186 | + isAppPath ? "rsc" : "json", |
| 187 | + isAppPath ? pageData : JSON.stringify(pageData) |
| 188 | + ), |
| 189 | + ]); |
| 190 | + } else if (data?.kind === "FETCH") { |
| 191 | + await this.putS3Object(key, "fetch", JSON.stringify(data)); |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + private buildS3Key(key: string, extension: Extension) { |
| 196 | + return path.posix.join( |
| 197 | + CACHE_BUCKET_KEY_PREFIX ?? "", |
| 198 | + extension === "fetch" ? "__fetch" : "", |
| 199 | + this.buildId, |
| 200 | + extension === "fetch" ? key : `${key}.${extension}` |
| 201 | + ); |
| 202 | + } |
| 203 | + |
| 204 | + private buildS3KeyPrefix(key: string) { |
| 205 | + return path.posix.join(CACHE_BUCKET_KEY_PREFIX ?? "", this.buildId, key); |
| 206 | + } |
| 207 | + |
| 208 | + private listS3Objects(key: string) { |
| 209 | + return this.client.send( |
| 210 | + new ListObjectsV2Command({ |
| 211 | + Bucket: CACHE_BUCKET_NAME, |
| 212 | + Prefix: this.buildS3KeyPrefix(key), |
| 213 | + }) |
| 214 | + ); |
| 215 | + } |
| 216 | + |
| 217 | + private getS3Object(key: string, extension: Extension) { |
| 218 | + return this.client.send( |
| 219 | + new GetObjectCommand({ |
| 220 | + Bucket: CACHE_BUCKET_NAME, |
| 221 | + Key: this.buildS3Key(key, extension), |
| 222 | + }) |
| 223 | + ); |
| 224 | + } |
| 225 | + |
| 226 | + private putS3Object( |
| 227 | + key: string, |
| 228 | + extension: Extension, |
| 229 | + value: PutObjectCommandInput["Body"] |
| 230 | + ) { |
| 231 | + return this.client.send( |
| 232 | + new PutObjectCommand({ |
| 233 | + Bucket: CACHE_BUCKET_NAME, |
| 234 | + Key: this.buildS3Key(key, extension), |
| 235 | + Body: value, |
| 236 | + }) |
| 237 | + ); |
| 238 | + } |
| 239 | +} |
0 commit comments