Skip to content

Commit 71edd3a

Browse files
committed
src: map file symlinks for old node versions
Signed-off-by: flakey5 <[email protected]>
1 parent 7769280 commit 71edd3a

File tree

5 files changed

+1451
-2
lines changed

5 files changed

+1451
-2
lines changed

scripts/build-r2-symlinks.mjs

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#!/usr/bin/env node
22
'use strict';
33

4-
import { join } from 'node:path';
5-
import { writeFile } from 'node:fs/promises';
4+
import { basename, dirname, join } from 'node:path';
5+
import { readFile, writeFile } from 'node:fs/promises';
66
import {
77
HeadObjectCommand,
88
ListObjectsV2Command,
@@ -35,6 +35,14 @@ const CACHED_DIRECTORIES_OUT = join(
3535
'cachedDirectories.json'
3636
);
3737

38+
const FILE_SYMLINKS = join(
39+
import.meta.dirname,
40+
'..',
41+
'src',
42+
'constants',
43+
'fileSymlinks.json'
44+
);
45+
3846
if (!process.env.CF_ACCESS_KEY_ID) {
3947
throw new TypeError('CF_ACCESS_KEY_ID missing');
4048
}
@@ -118,6 +126,38 @@ const cachedDirectories = {
118126
},
119127
};
120128

129+
// Some older versions of Node exist in `nodejs/release/` and have folders
130+
// with symlinks to them. For example, node-v0.1.100.tar.gz lives under
131+
// `nodejs/release/`, but there's also `nodejs/release/v0.1.100/node-v0.1.100.tar.gz`
132+
// which is just a symlink to it.
133+
// Let's add these to our cached directories.
134+
const fileSymlinks = JSON.parse(await readFile(FILE_SYMLINKS, 'utf8'));
135+
136+
for (const file of Object.keys(fileSymlinks)) {
137+
// Stat the actual file so we can get it's size, last modified
138+
const actualFile = await headFile(client, fileSymlinks[file]);
139+
140+
const directory = `${dirname(file)}/`;
141+
142+
if (directory in cachedDirectories) {
143+
// Directory was already cached, let's just append the file to the result
144+
cachedDirectories[directory].files.push({
145+
...actualFile,
146+
name: basename(file),
147+
});
148+
} else {
149+
// List the directory that the symlink is in so we can append the symlink to
150+
// what's actually there
151+
const contents = await listDirectory(client, directory);
152+
contents.files.push({
153+
...actualFile,
154+
name: basename(file),
155+
});
156+
157+
cachedDirectories[directory] = contents;
158+
}
159+
}
160+
121161
await writeFile(CACHED_DIRECTORIES_OUT, JSON.stringify(cachedDirectories));
122162

123163
/**

src/constants/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ Various constants used throughout the worker
88
- [cachedDirectories.json](./cachedDirectories.json) - Directories that have their listing result cached because they have symlinks in them. These are updated by the [build-r2-symlinks](../scripts/build-r2-symlinks.mjs) script.
99
- [docsDirectory.json](./docsDirectory.json) - The contents of `nodejs/docs/` in the `dist-prod` bucket. This is updated by the [build-r2-symlinks](../scripts/build-r2-symlinks.mjs) script.
1010
- [files.ts](./files.ts) - Constants related to files the worker serves.
11+
- [fileSymlinks.ts](./fileSymlinks.json) - Manually updated mapping of file symlinks that exist in the `dist-prod` bucket.
1112
- [latestVersions.json] - Map of `latest-*` directories to their actual latest versions. This is updated by the [build-r2-symlinks](../scripts/build-r2-symlinks.mjs) script.
1213
- [limits.ts](./limits.ts) - Hardcap limits that the worker shouldn't exceed.

0 commit comments

Comments
 (0)