Skip to content

Commit c52129d

Browse files
committed
add cache generation to pages action
1 parent 4a88545 commit c52129d

File tree

7 files changed

+72
-14
lines changed

7 files changed

+72
-14
lines changed

.github/workflows/pages.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ jobs:
3636
- name: Build
3737
run: bun run build
3838

39+
- name: Generate cache
40+
run: bun run buildCache.js dist/cache.json
41+
3942
- name: Upload Pages artifact
4043
if: github.event_name == 'push'
4144
uses: actions/upload-pages-artifact@v3

buildCache.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import puppeteer from "puppeteer";
2+
3+
const server = Bun.serve({
4+
async fetch (req) {
5+
const path = new URL(req.url).pathname.replace("/convert/", "") || "index.html";
6+
return new Response(Bun.file(`${__dirname}/dist/${path}`));
7+
},
8+
port: 8080
9+
});
10+
11+
const browser = await puppeteer.launch({
12+
headless: "new",
13+
args: ["--no-sandbox", "--disable-setuid-sandbox"]
14+
});
15+
16+
const page = await browser.newPage();
17+
18+
await Promise.all([
19+
new Promise(resolve => {
20+
page.on("console", msg => {
21+
const text = msg.text();
22+
if (text === "Built initial format list.") resolve();
23+
});
24+
}),
25+
page.goto("http://localhost:8080/convert/index.html")
26+
]);
27+
28+
const cacheJSON = await page.evaluate(() => {
29+
return window.printSupportedFormatCache();
30+
});
31+
const outputPath = process.argv[2] || "cache.json";
32+
await Bun.write(outputPath, cacheJSON);
33+
34+
await browser.close();
35+
server.stop();

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"preview": "vite preview"
1010
},
1111
"devDependencies": {
12+
"puppeteer": "^24.36.0",
1213
"typescript": "~5.9.3",
1314
"vite": "^7.2.4"
1415
},

src/global.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import type { FileFormat } from "./FormatHandler.js";
2+
3+
declare global {
4+
interface Window {
5+
supportedFormatCache: Map<string, FileFormat[]>;
6+
printSupportedFormatCache: () => string;
7+
}
8+
}
9+
10+
export { };

src/handlers/FFmpeg.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class FFmpegHandler implements FormatHandler {
6767
} catch (e) {
6868
if (!e || (
6969
typeof e === "string"
70-
&& e.includes("index out of bounds")
70+
&& e.includes("out of bounds")
7171
&& attempts > 1
7272
)) {
7373
await this.reloadFFmpeg();

src/main.ts

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,11 @@ function hidePopup () {
143143

144144
const allOptions: Array<{ format: FileFormat, handler: FormatHandler }> = [];
145145

146-
let supportedFormatCache: Map<string, FileFormat[]> = new Map();
146+
window.supportedFormatCache = new Map();
147147

148-
function printSupportedFormatCache () {
148+
window.printSupportedFormatCache = () => {
149149
const entries = [];
150-
for (const entry of supportedFormatCache) {
150+
for (const entry of window.supportedFormatCache) {
151151
entries.push(entry);
152152
}
153153
return JSON.stringify(entries, null, 2);
@@ -160,16 +160,15 @@ async function buildOptionList () {
160160
ui.outputList.innerHTML = "";
161161

162162
for (const handler of handlers) {
163-
if (!supportedFormatCache.has(handler.name)) {
163+
if (!window.supportedFormatCache.has(handler.name)) {
164164
console.warn(`Cache miss for formats of handler "${handler.name}".`);
165165
await handler.init();
166166
if (handler.supportedFormats) {
167-
supportedFormatCache.set(handler.name, handler.supportedFormats);
168-
console.log("Updated supported format cache:");
169-
console.log(printSupportedFormatCache());
167+
window.supportedFormatCache.set(handler.name, handler.supportedFormats);
168+
console.info(`Updated supported format cache for "${handler.name}".`);
170169
}
171170
}
172-
const supportedFormats = supportedFormatCache.get(handler.name);
171+
const supportedFormats = window.supportedFormatCache.get(handler.name);
173172
if (!supportedFormats) {
174173
console.warn(`Handler "${handler.name}" doesn't support any formats.`);
175174
continue;
@@ -242,9 +241,15 @@ async function buildOptionList () {
242241
(async () => {
243242
try {
244243
const cacheJSON = await fetch("cache.json").then(r => r.json());
245-
supportedFormatCache = new Map(cacheJSON);
244+
window.supportedFormatCache = new Map(cacheJSON);
245+
} catch {
246+
console.warn(
247+
"Missing supported format precache.\n\n" +
248+
"Consider saving the output of printSupportedFormatCache() to cache.json."
249+
);
246250
} finally {
247-
buildOptionList();
251+
await buildOptionList();
252+
console.log("Built initial format list.");
248253
}
249254
})();
250255

@@ -271,7 +276,7 @@ async function attemptConvertPath (files: FileData[], path: ConvertPathNode[]) {
271276
if (!handler.ready) {
272277
await handler.init();
273278
if (handler.supportedFormats) {
274-
supportedFormatCache.set(handler.name, handler.supportedFormats);
279+
window.supportedFormatCache.set(handler.name, handler.supportedFormats);
275280
}
276281
}
277282
files = await handler.doConvert(files, path[i].format, path[i + 1].format);
@@ -302,7 +307,7 @@ async function buildConvertPath (
302307

303308
// Get handlers that support *taking in* the previous node's format
304309
const validHandlers = handlers.filter(handler => (
305-
supportedFormatCache.get(handler.name)?.some(format => (
310+
window.supportedFormatCache.get(handler.name)?.some(format => (
306311
format.mime === previous.format.mime &&
307312
format.from
308313
))
@@ -328,7 +333,7 @@ async function buildConvertPath (
328333

329334
// Look for untested mime types among valid handlers and add to queue
330335
for (const handler of validHandlers) {
331-
const supportedFormats = supportedFormatCache.get(handler.name);
336+
const supportedFormats = window.supportedFormatCache.get(handler.name);
332337
if (!supportedFormats) continue;
333338
for (const format of supportedFormats) {
334339
if (!format.to) continue;

vite.config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ export default defineConfig({
1111
plugins: [
1212
viteStaticCopy({
1313
targets: [
14+
{
15+
src: "favicon.ico",
16+
dest: "assets"
17+
},
1418
{
1519
src: "node_modules/@ffmpeg/core/dist/esm/ffmpeg-core.*",
1620
dest: "wasm"

0 commit comments

Comments
 (0)