|
20 | 20 |
|
21 | 21 | import snapshotVersions from "./melpa_snapshot_versions.json" with { type: "json" };
|
22 | 22 |
|
| 23 | +const today = process.env.TODAY ? new Date(process.env.TODAY) : new Date(); |
23 | 24 | const melpaSnapshotLeadingComp = "/melpa/snapshot" as const;
|
| 25 | +const melpaAgeLeadingComp = "/melpa/at-least-days-old" as const; |
24 | 26 | const snapshotVersionsRegexp = snapshotVersions.join("|");
|
| 27 | +// Snapshot versions from latest to oldest. |
| 28 | +const maxAge = 270 as const; // We support <= 270 days old. |
| 29 | + |
| 30 | +interface SnapshotVersionWithAge { |
| 31 | + version: string; |
| 32 | + ageRegexp: string; // age of the snapshot in days |
| 33 | +} |
| 34 | + |
| 35 | +/** Create a list of snapshots and their age regexps. Sorted from most recent to |
| 36 | + * least recent. */ |
| 37 | +function createSnapshotVersionWithAges(): SnapshotVersionWithAge[] { |
| 38 | + const sortedSnapshotVersions = snapshotVersions.toSorted().reverse(); |
| 39 | + const ageRegexps: SnapshotVersionWithAge[] = []; |
| 40 | + let lastSnapshotAge = 0; |
| 41 | + for (const snapshotVersion of sortedSnapshotVersions) { |
| 42 | + const snapshotDate = new Date(snapshotVersion); |
| 43 | + const ageOfSnapshot = Math.min( |
| 44 | + maxAge, |
| 45 | + Math.floor( |
| 46 | + (today.getTime() - snapshotDate.getTime()) / (1000 * 60 * 60 * 24), |
| 47 | + ), |
| 48 | + ); |
| 49 | + const days = [...Array(ageOfSnapshot - lastSnapshotAge).keys()].map((i) => |
| 50 | + (lastSnapshotAge + i + 1).toString(), |
| 51 | + ); |
| 52 | + ageRegexps.push({ ageRegexp: days.join("|"), version: snapshotVersion }); |
| 53 | + lastSnapshotAge = ageOfSnapshot; |
| 54 | + if (lastSnapshotAge >= maxAge) { |
| 55 | + break; |
| 56 | + } |
| 57 | + } |
| 58 | + return ageRegexps; |
| 59 | +} |
| 60 | + |
| 61 | +const snapshotVersionWithAges = createSnapshotVersionWithAges(); |
25 | 62 |
|
26 | 63 | const caddyfile = `
|
27 | 64 | {
|
@@ -66,6 +103,22 @@ For more information, please visit the Delpa homepage: https://delpa.org"
|
66 | 103 | close
|
67 | 104 | }
|
68 | 105 | }
|
| 106 | +
|
| 107 | + # /melpa/at-least-days-old/{number} |
| 108 | + route ${melpaAgeLeadingComp}/* { |
| 109 | +${snapshotVersionWithAges |
| 110 | + .map((snapshotVersionWithAge) => { |
| 111 | + const matcherName = `@valid-age-root-${snapshotVersionWithAge.version}`; |
| 112 | + return ` |
| 113 | + ${matcherName} path_regexp ^${melpaAgeLeadingComp}/(${snapshotVersionWithAge.ageRegexp})/*$ |
| 114 | + respond ${matcherName} 200 { |
| 115 | + body "{re.1} days old points to snapshot version ${snapshotVersionWithAge.version}." |
| 116 | + close |
| 117 | + } |
| 118 | + `; |
| 119 | + }) |
| 120 | + .join("\n")} |
| 121 | + } |
69 | 122 | }
|
70 | 123 | `;
|
71 | 124 |
|
|
0 commit comments