|
1
|
import path from "path" |
|
2
|
import { unstable_cache } from "next/cache" |
|
3
|
import { getCloudflareContext } from "@opennextjs/cloudflare" |
|
4
|
|
|
5
|
/** |
|
6
|
* Compares two version directory names, sorting the newest first. Falls back |
|
7
|
* to a string comparison for segments that aren't numeric. |
|
8
|
*/ |
|
9
|
function compareVersionsDesc(versionA: string, versionB: string): number { |
|
10
|
const segmentsA = versionA.split(".") |
|
11
|
const segmentsB = versionB.split(".") |
|
12
|
|
|
13
|
for (let i = 0; i < Math.max(segmentsA.length, segmentsB.length); i++) { |
|
14
|
const segmentA = segmentsA[i] ?? "" |
|
15
|
const segmentB = segmentsB[i] ?? "" |
|
16
|
|
|
17
|
if (segmentA === segmentB) { |
|
18
|
continue |
|
19
|
} |
|
20
|
|
|
21
|
const numberA = Number.parseInt(segmentA, 10) |
|
22
|
const numberB = Number.parseInt(segmentB, 10) |
|
23
|
|
|
24
|
if (Number.isNaN(numberA) || Number.isNaN(numberB)) { |
|
25
|
return segmentB.localeCompare(segmentA) |
|
26
|
} |
|
27
|
|
|
28
|
return numberB - numberA |
|
29
|
} |
|
30
|
|
|
31
|
return 0 |
|
32
|
} |
|
33
|
|
|
34
|
/** |
|
35
|
* Version directory names are interpolated into bucket keys and file system |
|
36
|
* paths, so anything that isn't a plain version is rejected before use. |
|
37
|
*/ |
|
38
|
const VERSION_PATTERN = /^\d+(?:\.\d+)*(?:-[a-zA-Z0-9.]+)?$/ |
|
39
|
|
|
40
|
export function isValidSpecVersion(version: string): boolean { |
|
41
|
return VERSION_PATTERN.test(version) |
|
42
|
} |
|
43
|
|
|
44
|
/** |
|
45
|
* Resolves the R2 bucket binding and the prefix that the archived spec keys |
|
46
|
* live under. Returns `null` outside Cloudflare (next dev / build) or when the |
|
47
|
* binding isn't configured, so callers can fall back to the file system. |
|
48
|
*/ |
|
49
|
type SpecsBucket = NonNullable<CloudflareEnv["SPECS_R2_BUCKET"]> |
|
50
|
|
|
51
|
async function getVersionsBucket(): Promise<{ |
|
52
|
bucket: SpecsBucket |
|
53
|
prefix: string |
|
54
|
} | null> { |
|
55
|
const r2Base = process.env.SPECS_R2_BASE_URL |
|
56
|
if (!r2Base) { |
|
57
|
return null |
|
58
|
} |
|
59
|
|
|
60
|
try { |
|
61
|
// async mode so this also works in statically rendered contexts |
|
62
|
const { env } = await getCloudflareContext({ async: true }) |
|
63
|
const bucket = env.SPECS_R2_BUCKET |
|
64
|
if (!bucket) { |
|
65
|
return null |
|
66
|
} |
|
67
|
|
|
68
|
// The public bucket URL serves the bucket root, so its pathname is the |
|
69
|
// prefix that the spec keys live under. |
|
70
|
const basePrefix = new URL(r2Base).pathname.replace(/^\/|\/$/g, "") |
|
71
|
|
|
72
|
return { |
|
73
|
bucket, |
|
74
|
prefix: `${basePrefix ? `${basePrefix}/` : ""}specs/versions/`, |
|
75
|
} |
|
76
|
} catch { |
|
77
|
return null |
|
78
|
} |
|
79
|
} |
|
80
|
|
|
81
|
/** |
|
82
|
* Lists the version directories under `specs/versions` in the R2 bucket using |
|
83
|
* the bucket binding. Returns `null` when the bucket isn't available. |
|
84
|
*/ |
|
85
|
async function listVersionsFromBinding(): Promise<string[] | null> { |
|
86
|
const resolved = await getVersionsBucket() |
|
87
|
if (!resolved) { |
|
88
|
return null |
|
89
|
} |
|
90
|
|
|
91
|
const { bucket, prefix } = resolved |
|
92
|
|
|
93
|
try { |
|
94
|
const versions: string[] = [] |
|
95
|
let cursor: string | undefined |
|
96
|
|
|
97
|
do { |
|
98
|
const listed = await bucket.list({ prefix, delimiter: "/", cursor }) |
|
99
|
versions.push( |
|
100
|
...listed.delimitedPrefixes.map((delimitedPrefix) => |
|
101
|
delimitedPrefix.slice(prefix.length).replace(/\/$/, "") |
|
102
|
) |
|
103
|
) |
|
104
|
cursor = listed.truncated ? listed.cursor : undefined |
|
105
|
} while (cursor) |
|
106
|
|
|
107
|
// An empty listing means the binding points at a bucket that doesn't hold |
|
108
|
// the specs, which is indistinguishable from "no versions". Treating it as |
|
109
|
// a miss lets the caller fall back instead of returning an empty list. |
|
110
|
return versions.length ? versions : null |
|
111
|
} catch { |
|
112
|
return null |
|
113
|
} |
|
114
|
} |
|
115
|
|
|
116
|
/** |
|
117
|
* Checks whether a single version is archived in the R2 bucket, without |
|
118
|
* listing the other versions. Returns `null` when the bucket isn't available |
|
119
|
* or holds no objects for the version, so callers can fall back. |
|
120
|
*/ |
|
121
|
async function versionExistsInBinding(version: string): Promise<true | null> { |
|
122
|
const resolved = await getVersionsBucket() |
|
123
|
if (!resolved) { |
|
124
|
return null |
|
125
|
} |
|
126
|
|
|
127
|
const { bucket, prefix } = resolved |
|
128
|
|
|
129
|
try { |
|
130
|
const listed = await bucket.list({ |
|
131
|
prefix: `${prefix}${version}/`, |
|
132
|
limit: 1, |
|
133
|
}) |
|
134
|
|
|
135
|
// An empty listing is indistinguishable from a bucket that doesn't hold |
|
136
|
// the specs, which is what the binding points at in local development. |
|
137
|
// Treating it as a miss lets the caller fall back to the file system. |
|
138
|
return listed.objects.length > 0 || listed.delimitedPrefixes.length > 0 |
|
139
|
? true |
|
140
|
: null |
|
141
|
} catch { |
|
142
|
return null |
|
143
|
} |
|
144
|
} |
|
145
|
|
|
146
|
/** |
|
147
|
* Checks whether a single version is archived in the file system. |
|
148
|
*/ |
|
149
|
async function versionExistsInFs(version: string): Promise<boolean> { |
|
150
|
const { stat } = await import("fs/promises") |
|
151
|
|
|
152
|
try { |
|
153
|
const stats = await stat( |
|
154
|
path.join(process.cwd(), "specs", "versions", version) |
|
155
|
) |
|
156
|
|
|
157
|
return stats.isDirectory() |
|
158
|
} catch { |
|
159
|
return false |
|
160
|
} |
|
161
|
} |
|
162
|
|
|
163
|
/** |
|
164
|
* Checks whether a version is archived under `specs/versions`, looking up only |
|
165
|
* that version instead of retrieving the full listing. |
|
166
|
*/ |
|
167
|
export async function specVersionExists(version: string): Promise<boolean> { |
|
168
|
if (!isValidSpecVersion(version)) { |
|
169
|
return false |
|
170
|
} |
|
171
|
|
|
172
|
return ( |
|
173
|
(await versionExistsInBinding(version)) ?? |
|
174
|
(await versionExistsInFs(version)) |
|
175
|
) |
|
176
|
} |
|
177
|
|
|
178
|
/** |
|
179
|
* Lists the version directories under `specs/versions` in the file system. |
|
180
|
*/ |
|
181
|
async function listVersionsFromFs(): Promise<string[]> { |
|
182
|
const { readdir } = await import("fs/promises") |
|
183
|
|
|
184
|
try { |
|
185
|
const entries = await readdir( |
|
186
|
path.join(process.cwd(), "specs", "versions"), |
|
187
|
{ |
|
188
|
withFileTypes: true, |
|
189
|
} |
|
190
|
) |
|
191
|
|
|
192
|
return entries |
|
193
|
.filter((entry) => entry.isDirectory()) |
|
194
|
.map(({ name }) => name) |
|
195
|
} catch { |
|
196
|
return [] |
|
197
|
} |
|
198
|
} |
|
199
|
|
|
200
|
/** |
|
201
|
* Retrieves the archived spec versions, sorted from newest to oldest. The |
|
202
|
* versions are read from R2 when it's configured, and from the local `specs` |
|
203
|
* directory otherwise. |
|
204
|
*/ |
|
205
|
export async function getSpecVersions(): Promise<string[]> { |
|
206
|
const versions = |
|
207
|
(await listVersionsFromBinding()) ?? (await listVersionsFromFs()) |
|
208
|
|
|
209
|
return versions.filter(Boolean).sort(compareVersionsDesc) |
|
210
|
} |
|
211
|
|
|
212
|
/** |
|
213
|
* The listing only changes when a version is archived, so it's memoized to |
|
214
|
* keep repeat requests from paging through the bucket again. |
|
215
|
*/ |
|
216
|
export const getCachedSpecVersions = unstable_cache( |
|
217
|
getSpecVersions, |
|
218
|
["api-ref-spec-versions"], |
|
219
|
{ |
|
220
|
revalidate: 3600, |
|
221
|
} |
|
222
|
) |