-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathreleases.js
More file actions
372 lines (328 loc) · 12 KB
/
releases.js
File metadata and controls
372 lines (328 loc) · 12 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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
// releases.js - Dynamic release loading from GitHub API for WLED Web Installer
//
// Fetches available WLED releases from the GitHub Releases API and dynamically
// populates the version dropdown. Generates esp-web-tools manifests on-the-fly
// as blob URLs, so the existing setManifest()/handleCheckbox() logic in script.js
// works unchanged.
//
// Falls back to the static <option> elements already in index.htm if the API
// request fails (e.g. rate-limited, offline, network error).
(function () {
'use strict';
// ---------------------------------------------------------------------------
// Configuration
// ---------------------------------------------------------------------------
const GITHUB_RELEASES_URL = 'https://api.github.com/repos/wled/WLED/releases';
const CORS_PROXY = 'https://proxy.corsfix.com/?';
const CACHE_KEY = 'wled_releases_cache';
const CACHE_TTL = 5 * 60 * 1000; // 5 minutes
const MAX_STABLE_RELEASES = 8; // limit dropdown length
const MAX_BETA_RELEASES = 2; // only show the two most recent beta releases
// Base URL for locally-hosted bootloader / partition-table files.
// These are chip-specific and shared across WLED versions.
const bootBase = new URL('bin/boot/', window.location.href).href;
// ---------------------------------------------------------------------------
// Bootloader / partition configuration per chip family
// ---------------------------------------------------------------------------
// Each entry describes the boot-stage parts that must be flashed before the
// WLED firmware binary. The firmware is always the last part.
const CHIP_CONFIG = {
'ESP32': {
chipFamily: 'ESP32',
bootParts: [
{ path: bootBase + 'esp32_bootloader_v4.bin', offset: 0 }
],
firmwareOffset: 65536
},
'ESP32-C3': {
chipFamily: 'ESP32-C3',
bootParts: [
{ path: bootBase + 'esp32-c3_bootloader_v2.bin', offset: 0 }
],
firmwareOffset: 65536
},
'ESP32-S2': {
chipFamily: 'ESP32-S2',
bootParts: [
{ path: bootBase + 'bootloader_s2.bin', offset: 4096 },
{ path: bootBase + 'partitions_s2_4m.bin', offset: 32768 }
],
firmwareOffset: 65536
},
'ESP32-S3': {
chipFamily: 'ESP32-S3',
bootParts: [
{ path: bootBase + 'bootloader_s3.bin', offset: 0 },
{ path: bootBase + 'partitions_s3_8m.bin', offset: 32768 }
],
firmwareOffset: 65536
},
'ESP8266': {
chipFamily: 'ESP8266',
bootParts: [],
firmwareOffset: 0
}
};
// ---------------------------------------------------------------------------
// Variant definitions
// ---------------------------------------------------------------------------
// Each variant maps chip families to the asset-name suffix used in GitHub
// release assets. Only chips that have a matching asset will be included in
// the generated manifest; missing assets cause the variant's radio button to
// be disabled automatically (existing handleCheckbox logic).
const VARIANTS = {
normal: {
'ESP32': '_ESP32.bin',
'ESP32-C3': '_ESP32-C3.bin',
'ESP32-S2': '_ESP32-S2.bin',
'ESP32-S3': '_ESP32-S3_8MB_opi.bin',
'ESP8266': '_ESP8266.bin'
},
ethernet: {
'ESP32': '_ESP32_Ethernet.bin',
'ESP8266': '_ESP8266.bin'
},
audio: {
'ESP32': '_ESP32_audioreactive.bin'
},
test: {
'ESP8266': '_ESP8266_160.bin'
},
v4: {
'ESP32': '_ESP32_V4.bin'
},
debug: {
'ESP32': '_ESP32_DEBUG.bin'
}
};
// Maps variant names to the data-* attribute names expected by script.js
const VARIANT_DATA_ATTRS = {
normal: 'manifest',
ethernet: 'ethernet',
audio: 'audio',
test: 'test',
v4: 'v4',
debug: 'debug'
};
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
/** Find a release asset whose name ends with `suffix` (ignore .gz files). */
function findAsset(assets, suffix) {
return assets.find(function (a) {
return a.name.endsWith(suffix) && !a.name.endsWith('.gz');
}) || null;
}
/** Extract the WLED version string from asset filenames (for nightly). */
function extractVersionFromAssets(assets) {
for (let i = 0; i < assets.length; i++) {
const m = assets[i].name.match(/^WLED_(.+?)_(ESP\d|ESP8)/);
if (m) return m[1];
}
return 'unknown';
}
/** Human-readable version for the dropdown. */
function getDisplayVersion(release) {
if (release.tag_name === 'nightly') {
return extractVersionFromAssets(release.assets) + ' Nightly';
}
return release.tag_name.replace(/^v/, '');
}
/** Version string embedded in the manifest JSON. */
function getManifestVersion(release, variantName) {
let ver;
if (release.tag_name === 'nightly') {
ver = extractVersionFromAssets(release.assets);
} else {
ver = release.tag_name.replace(/^v/, '');
}
if (variantName !== 'normal') {
ver += ' ' + variantName;
}
return ver;
}
// ---------------------------------------------------------------------------
// Manifest generation
// ---------------------------------------------------------------------------
/**
* Build an esp-web-tools manifest object for the given release + variant.
* Returns null if no matching assets are found for this variant.
*/
function generateManifest(release, variantName) {
const chipSuffixes = VARIANTS[variantName];
const version = getManifestVersion(release, variantName);
const builds = [];
for (const chip in chipSuffixes) {
const suffix = chipSuffixes[chip];
const asset = findAsset(release.assets, suffix);
if (!asset) continue;
const config = CHIP_CONFIG[chip];
if (!config) continue;
const parts = config.bootParts.map(function (bp) {
return { path: bp.path, offset: bp.offset };
});
parts.push({
path: CORS_PROXY + asset.browser_download_url,
offset: config.firmwareOffset
});
builds.push({ chipFamily: config.chipFamily, parts: parts });
}
if (builds.length === 0) return null;
return {
name: 'WLED',
version: version,
home_assistant_domain: 'wled',
new_install_prompt_erase: true,
builds: builds
};
}
/** Create a blob:// URL from a manifest object so esp-web-tools can fetch it. */
function createManifestUrl(manifest) {
const blob = new Blob([JSON.stringify(manifest)], { type: 'application/json' });
return URL.createObjectURL(blob);
}
// ---------------------------------------------------------------------------
// Dropdown population
// ---------------------------------------------------------------------------
function categorize(release) {
if (release.tag_name === 'nightly') return 'nightly';
if (release.prerelease) return 'beta';
return 'release';
}
/**
* Create a single <option> element for a release. All variant manifests are
* pre-generated as blob URLs and stored in data-* attributes so that the
* existing setManifest() / handleCheckbox() code works without changes.
*/
function createOption(release) {
const opt = document.createElement('option');
opt.textContent = getDisplayVersion(release);
opt.dataset.dynamic = 'true'; // mark as dynamically generated
let hasPlain = false;
for (const variant in VARIANT_DATA_ATTRS) {
const manifest = generateManifest(release, variant);
if (manifest) {
opt.dataset[VARIANT_DATA_ATTRS[variant]] = createManifestUrl(manifest);
if (variant === 'normal') hasPlain = true;
}
}
// Every release must at least have the plain/normal variant
return hasPlain ? opt : null;
}
/** Replace the <select> contents with dynamically generated options. */
function populateDropdown(releases) {
const sel = document.getElementById('ver');
// Group by category
const groups = { release: [], beta: [], nightly: [] };
releases.forEach(function (r) {
if (r.draft || !r.assets || r.assets.length === 0) return;
groups[categorize(r)].push(r);
});
// Limit the number of stable releases shown
if (groups.release.length > MAX_STABLE_RELEASES) {
groups.release = groups.release.slice(0, MAX_STABLE_RELEASES);
}
// Limit the number of beta releases shown (only the two most recent)
if (groups.beta.length > MAX_BETA_RELEASES) {
groups.beta = groups.beta.slice(0, MAX_BETA_RELEASES);
}
// Build option groups
const fragment = document.createDocumentFragment();
if (groups.release.length > 0) {
const grp = document.createElement('optgroup');
grp.label = 'Release';
groups.release.forEach(function (r) {
const opt = createOption(r);
if (opt) grp.appendChild(opt);
});
if (grp.children.length > 0) fragment.appendChild(grp);
}
if (groups.beta.length > 0) {
const grp = document.createElement('optgroup');
grp.label = 'Beta';
groups.beta.forEach(function (r) {
const opt = createOption(r);
if (opt) grp.appendChild(opt);
});
if (grp.children.length > 0) fragment.appendChild(grp);
}
if (groups.nightly.length > 0) {
const grp = document.createElement('optgroup');
grp.label = 'Nightly';
groups.nightly.forEach(function (r) {
const opt = createOption(r);
if (opt) grp.appendChild(opt);
});
if (grp.children.length > 0) fragment.appendChild(grp);
}
// Only replace contents if we actually produced options
if (fragment.children.length > 0) {
sel.innerHTML = '';
sel.appendChild(fragment);
}
}
// ---------------------------------------------------------------------------
// Caching (sessionStorage, 5-minute TTL)
// ---------------------------------------------------------------------------
function getCachedReleases() {
try {
const raw = sessionStorage.getItem(CACHE_KEY);
if (!raw) return null;
const data = JSON.parse(raw);
if (Date.now() - data.timestamp < CACHE_TTL) return data.releases;
} catch (e) {
console.warn('Failed to read releases cache:', e);
}
return null;
}
function cacheReleases(releases) {
try {
sessionStorage.setItem(CACHE_KEY, JSON.stringify({
timestamp: Date.now(),
releases: releases
}));
} catch (e) {
console.warn('Failed to write releases cache:', e);
}
}
// ---------------------------------------------------------------------------
// Entry point
// ---------------------------------------------------------------------------
/**
* Safely call resetCheckboxes() and setManifest() from script.js.
* These are defined in script.js which loads before releases.js, but we add
* defensive checks for robustness.
*/
function applySelection() {
if (typeof resetCheckboxes === 'function') resetCheckboxes();
if (typeof setManifest === 'function') setManifest();
}
/**
* Fetch releases and populate the dropdown. On failure the existing static
* <option> elements in the HTML remain untouched, so the installer still
* works (just with the hardcoded version list).
*/
window.loadReleases = function loadReleases() {
const cached = getCachedReleases();
if (cached) {
populateDropdown(cached);
applySelection();
return;
}
fetch(GITHUB_RELEASES_URL + '?per_page=30')
.then(function (res) {
if (!res.ok) throw new Error('GitHub API responded with ' + res.status);
return res.json();
})
.then(function (releases) {
cacheReleases(releases);
populateDropdown(releases);
applySelection();
})
.catch(function (err) {
console.warn('Failed to load releases from GitHub API – using static fallback.', err);
// Static options remain in place; setManifest() was already called
// by checkSupported() during page load, so no action needed.
});
};
})();