Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
266 lines (237 loc) · 7.74 KB
/
Copy pathindex.js
File metadata and controls
266 lines (237 loc) · 7.74 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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/**
* @file Works out ranges of Node.js versions, named by `@nodevu/aliases`.
*
* @example
* ```js
* const ranges = require('@nodevu/ranges');
* ```
*/
const nodevu = require('@nodevu/core');
const aliases = require('@nodevu/aliases');
/**
* The empty ranges `generateRanges` copies and fills in, with the fields each
* one has. Spelling the model out takes less logic than building it
* dynamically, and shows its shape at a glance.
*
* @example
* ```js
* defaultRangesObject.eol; // { versions: [], newest: undefined, oldest: undefined }
* ```
*/
const defaultRangesObject = {
all: {
versions: [],
newestLts: undefined,
newestSecurity: undefined,
newest: undefined,
oldest: undefined,
},
alpha: {
versions: [],
newest: undefined,
oldest: undefined,
},
current: {
versions: [],
oldestSecurity: undefined,
newestSecurity: undefined,
newest: undefined,
oldest: undefined,
},
'lts/latest': {
versions: [],
oldestLts: undefined,
oldestSecurity: undefined,
newestLts: undefined,
newestSecurity: undefined,
newest: undefined,
oldest: undefined,
},
'lts/active': {
versions: [],
oldestLts: undefined,
oldestSecurity: undefined,
newestLts: undefined,
newestSecurity: undefined,
newest: undefined,
oldest: undefined,
},
'lts/maintenance': {
versions: [],
oldestLts: undefined,
oldestSecurity: undefined,
newestLts: undefined,
newestSecurity: undefined,
newest: undefined,
oldest: undefined,
},
eol: {
versions: [],
newest: undefined,
oldest: undefined,
},
};
/**
* A range of versions. The fields a range doesn't have, or has nothing to
* point to, are `undefined`. `versions`, `newest` and `oldest`, and the `all`
* range's `newestLts` and `newestSecurity`, have a `v`; the other LTS and
* security fields come from `@nodevu/core` without one.
*
* @public
* @example
* ```js
* const ranges = require('@nodevu/ranges');
*
* const { eol } = await ranges('eol');
*
* eol.versions.at(-1) === eol.oldest; // true
* ```
*
* @typedef {object} Range
* @property {string[]} versions The versions in the range, newest first.
* @property {string} [newest] The newest version in the range.
* @property {string} [oldest] The oldest version in the range.
* @property {string} [newestLts] The newest LTS release.
* @property {string} [oldestLts] The first LTS release of the oldest line.
* @property {string} [newestSecurity] The newest security release.
* @property {string} [oldestSecurity] The oldest security release of the
* oldest line.
*/
/**
* Works out ranges of Node.js versions, named by `@nodevu/aliases`.
*
* @param {import('@nodevu/aliases').Alias | readonly import('@nodevu/aliases').Alias[]} [filter]
* The ranges to work out. Defaults to `'all'`, every range.
* @param {import('@nodevu/core').Options} [options] Passed to `@nodevu/core`.
* @returns {Promise<Partial<Record<import('@nodevu/aliases').Alias, Range>>>}
* A key per range. A range with no releases is left out.
* @public
* @example
* ```js
* const ranges = require('@nodevu/ranges');
*
* const { eol } = await ranges(['eol']);
*
* eol.oldest; // 'v0.8.0'
* eol.newestSecurity; // undefined, `eol` has no security fields
* ```
*/
async function generateRanges(filter, options) {
let localFilter = filter;
// filter is untyped public input, so typeof is how it gets validated here
// oxlint-disable-next-line anti-slop/no-runtime-typeof
if (typeof localFilter === 'string') {
// check to make sure that the filter is valid. if it's not, yeet.
if (!aliases.includes(localFilter)) {
throw new Error(
`Unknown value passed as a filter. The passed value: '${localFilter}'.`,
);
}
} else if (Array.isArray(localFilter) === true) {
// check to make sure that the filter is valid. if it's not, yeet.
if (!localFilter.every((alias) => aliases.includes(alias))) {
throw new Error(
`At least one of the values passed as a filter is unknown. The passed value(s): '${localFilter.join(
"', '",
)}'`,
);
}
} else if (localFilter) {
throw new Error(
// names the wrong type in the error, nothing is narrowed
// oxlint-disable-next-line anti-slop/no-runtime-typeof
`Unknown value passed as a filter. The passed value: '${localFilter}', with a type of '${typeof localFilter}'. Make sure you are passing a string (or an Array of strings) with valid value(s).`,
);
} else {
localFilter = 'all';
}
// a string filter is checked as a one-item list, since `includes` on a
// string matches substrings
/** @type {readonly string[]} */
const requested = [localFilter].flat();
const wants = (/** @type {string} */ alias) =>
requested.includes(alias) || requested.includes('all');
// set up the data from @nodevu/core, passing options (like `fetch` and `now`) through
const data = await nodevu(options);
// deep clone the default object so we don't modify it.
//
// modifying the default object has some super wonky side effects
// on subsequent calls to this function that we don't want.
/** @type {Record<string, Range>} */
const ranges = JSON.parse(JSON.stringify(defaultRangesObject));
for (const line in data) {
const phase = data[line].support?.phases.current;
for (const key in data[line].releases) {
const release = data[line].releases[key];
// Alpha releases (`vN.0.0-alpha.X.Y.Z`) are pre-releases, so they only
// go into `alpha` and are kept out of every other range
if (release.alpha?.isAlpha) {
if (wants('alpha') && phase === 'alpha') {
ranges.alpha.versions.push(key);
}
continue;
}
// define `all` data, where the newest LTS and security releases are the
// first ones seen
if (wants('all')) {
ranges.all.versions.push(key);
if (release.lts.isLts) {
ranges.all.newestLts ??= key;
}
if (release.security.isSecurity) {
ranges.all.newestSecurity ??= key;
}
}
// the phase `start` means the line is Current
if (wants('current') && phase === 'start') {
ranges.current.versions.push(key);
// only the first Current line fills these in. checking the versions
// count rather than the fields, since a line without a security
// release leaves them undefined.
if (ranges.current.versions.length === 1) {
ranges.current.oldestSecurity = data[line].security.oldest;
ranges.current.newestSecurity = data[line].security.newest;
}
}
// lts/active is every release in lts/latest and lts/maintenance
// TODO: do we want to include all versions in the release line, even prior to it being minted LTS?
if (release.lts.isLts && (phase === 'lts' || phase === 'maintenance')) {
const phaseRange = phase === 'lts' ? 'lts/latest' : 'lts/maintenance';
for (const alias of [phaseRange, 'lts/active']) {
if (!wants(alias)) {
continue;
}
const range = ranges[alias];
const isFirstRelease = range.versions.push(key) === 1;
// the newest bounds come from the range's first line, and the oldest
// from its last. lts/latest only takes its first line's, same as
// 'current'.
if (isFirstRelease) {
range.newestSecurity = data[line].security.newest;
range.newestLts = data[line].support?.lts?.newest;
}
if (isFirstRelease || alias !== 'lts/latest') {
range.oldestSecurity = data[line].security.oldest;
range.oldestLts = data[line].support?.lts?.oldest;
}
}
}
if (wants('eol') && phase === 'end') {
ranges.eol.versions.push(key);
}
}
}
// define newest and oldest from each range's versions, and delete the
// ranges with none
for (const alias in ranges) {
const { versions } = ranges[alias];
if (versions.length === 0) {
delete ranges[alias];
continue;
}
ranges[alias].newest = versions[0];
ranges[alias].oldest = versions.at(-1);
}
return ranges;
}
module.exports = generateRanges;