Repository navigation
Expand file tree
/
Copy pathaddSymlinksToDirectoryCache.mjs
More file actions
247 lines (218 loc) · 7.82 KB
/
Copy pathaddSymlinksToDirectoryCache.mjs
File metadata and controls
247 lines (218 loc) · 7.82 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
import { readFile, writeFile } from 'node:fs/promises';
import { basename, dirname } from 'node:path';
import {
DOCS_DIR,
NODE_LATEST_FILE_NAME,
PROD_BUCKET,
RELEASE_DIR,
STATIC_FILE_SYMLINKS_PATH,
} from '../constants.mjs';
import { headR2File } from './r2.mjs';
import { CACHED_DIRECTORIES_PATH, DOCS_DIRECTORY_PATH } from '../constants.mjs';
import { listR2Directory } from '../../lib/listR2Directory.mjs';
/**
* Adds the `nodejs/release/node-latest.tar.gz` symlink to the directory cache
* @param {import('@aws-sdk/client-s3').S3Client} client
* @param {import('../../src/providers/provider').ReadDirectoryResult} releaseDirectory
* @param {import('./getLatestVersionMapping.mjs').LatestVersionMapping} latestVersionMap
* @param {string} latestVersion
*/
export async function addLatestTarSymlinkToCache(
client,
releaseDirectory,
latestVersionMap,
latestVersion
) {
const nodeLatestPath = `${RELEASE_DIR}${latestVersionMap[NODE_LATEST_FILE_NAME].replaceAll('latest', latestVersion)}`;
// Stat the file that `node-latest.tar.gz` points to so we can have accurate
// size & last modified info for the directory listing. Also acts as a safety
// check so this doesn't point to an invalid path without us noticing.
const nodeLatest = await headR2File(client, PROD_BUCKET, nodeLatestPath);
if (!nodeLatest) {
throw new TypeError(
`couldn't map node-latest.tar.gz: path '${nodeLatestPath}' doesn't exist in the '${PROD_BUCKET}'`
);
}
releaseDirectory.files.push({
name: NODE_LATEST_FILE_NAME,
lastModified: nodeLatest.lastModified,
size: nodeLatest.size,
});
}
/**
* Adds the `latest` and `latest-*` directory symlinks to the directory cache
* @param {import('../../src/providers/provider').ReadDirectoryResult} releaseDirectory
* @param {import('./getLatestVersionMapping.mjs').LatestVersionMapping} latestVersionMap
*/
export function addLatestDirectorySymlinksToCache(
releaseDirectory,
latestVersionMap
) {
releaseDirectory.subdirectories.push(
...Object.keys(latestVersionMap)
// We only want directories, remove the only file in the map
.filter(version => version !== NODE_LATEST_FILE_NAME)
.map(version => `${version}/`)
);
releaseDirectory.subdirectories.sort();
}
/**
* Manually override the contents of `nodejs/docs` to be a union of what's
* actually in that directory as well as the versions present in
* `nodejs/release/`.
* @param {import('../../src/providers/provider').ReadDirectoryResult} releaseDirectory
* @param {import('../../src/providers/provider').ReadDirectoryResult | undefined} docsDirectory
*/
export async function addVersionSymlinksToCachedDocsDirectory(
releaseDirectory,
docsDirectory
) {
if (!docsDirectory) {
return;
}
// Cache the current contents of the directory so we know which directories
// actually exist and which are symlinks
// TODO: look into a better/more general way of handling this
await writeFile(
DOCS_DIRECTORY_PATH,
JSON.stringify(
docsDirectory.subdirectories.map(subdirectory =>
// Trim trailing `/`
subdirectory.substring(0, subdirectory.length - 1)
),
null,
2
)
);
// We only want the `vX.X.X/` and `latest-X/` directories
const versionDirectories = releaseDirectory.subdirectories.filter(
subdirectory =>
subdirectory.startsWith('v') || subdirectory.startsWith('latest')
);
docsDirectory.subdirectories = Array.from(
new Set(docsDirectory.subdirectories.concat(versionDirectories))
).sort();
}
/**
* Add the static file symlinks to the directory cache
* @param {import('@aws-sdk/client-s3').S3Client} client
* @param {import('./r2.mjs').DirectoryListMapping} cachedDirectories
* @param {string} latestVersion
*/
export async function addStaticFileSymlinksToCache(
client,
cachedDirectories,
latestVersion
) {
// Keep list of directories we modify here to include in cachedDirectories.json
// TODO: remove when KV is considered stable
const directoriesIncludedInFileCache = new Set([RELEASE_DIR, DOCS_DIR]);
const fileSymlinksFile = await readFile(STATIC_FILE_SYMLINKS_PATH, 'utf8');
/**
* @type {Record<string, string>}
*/
const fileSymlinks = JSON.parse(fileSymlinksFile);
// These files are an exception for two reasons:
// 1. They're dynamic and point to the latest Node version
// 2. They're handled specially and are technically in the root directory,
// which we should never be listing thus don't need to add them to the
// directory cache.
// So, let's remove them then add them back once we're done handling the rest
// of the static file symlinks.
fileSymlinks['node-config-schema.json'] = undefined;
fileSymlinks['llms.txt'] = undefined;
// Add the symlinks to the directory cache
for (const symlink of Object.keys(fileSymlinks)) {
const sourceFilePath = fileSymlinks[symlink];
if (!sourceFilePath) {
// Hit a file we don't want to stat, ignore it
continue;
}
// Stat the source file so we can get its size, last modified
const sourceFile = await headR2File(
client,
PROD_BUCKET,
fileSymlinks[symlink],
cachedDirectories
);
if (!sourceFile) {
throw new TypeError(
`symlink '${symlink}' points to invalid file '${fileSymlinks[symlink]}'`
);
}
const directoryPath = `${dirname(symlink)}/`;
directoriesIncludedInFileCache.add(directoryPath);
let directory = cachedDirectories.get(directoryPath);
if (!directory) {
directory = await listR2Directory(client, PROD_BUCKET, directoryPath);
cachedDirectories.set(directoryPath, directory);
}
directory.files.push({
...sourceFile,
name: basename(symlink),
});
}
// Update the node-config-schema.json and llms.txt file symlinks to point to
// the latest version
fileSymlinks['node-config-schema.json'] =
`${RELEASE_DIR}${latestVersion}/docs/node-config-schema.json`;
fileSymlinks['llms.txt'] = `${RELEASE_DIR}${latestVersion}/docs/llms.txt`;
// Update file so it can be used in the worker
await writeFile(
STATIC_FILE_SYMLINKS_PATH,
JSON.stringify(fileSymlinks, null, 2)
);
// Update the cached directories file
// TODO: remove when KV is considered stable
const fileContents = {};
directoriesIncludedInFileCache.forEach(directory => {
fileContents[directory] = cachedDirectories.get(directory);
});
await writeFile(
CACHED_DIRECTORIES_PATH,
JSON.stringify(fileContents, null, 2)
);
}
/**
* R2 doesn't have symlinks, but we make heavy use of them. So, we need to add
* them into the cache ourselves.
*
* We use symlinks to point to both files and directories.
*
* The file symlinks are more or less static. There are only a few that are
* constantly updated with each release:
* - `nodejs/release/node-latest.tar.gz`
* - `node-config-schema.json`
* - `llms.txt`
*
* The directory symlinks are dynamic with a few exceptions for a select number
* of directories in `nodejs/docs/`
*
* @param {S3Client} client
* @param {import('./r2.mjs').DirectoryListMapping} cachedDirectories
* @param {import('./getLatestVersionMapping.mjs').LatestVersionMapping} latestVersionMap
*/
export async function addSymlinksToDirectoryCache(
client,
cachedDirectories,
latestVersionMap
) {
const releaseDirectory = cachedDirectories.get(RELEASE_DIR);
if (!releaseDirectory) {
console.warn(`Release directory not found at path ${RELEASE_DIR}`);
return;
}
const latestVersion = latestVersionMap['latest'];
await addLatestTarSymlinkToCache(
client,
releaseDirectory,
latestVersionMap,
latestVersion
);
addLatestDirectorySymlinksToCache(releaseDirectory, latestVersionMap);
await addVersionSymlinksToCachedDocsDirectory(
releaseDirectory,
cachedDirectories.get(DOCS_DIR)
);
await addStaticFileSymlinksToCache(client, cachedDirectories, latestVersion);
}