From 75ef4604d87724bc369e06a6fa1ef72c0e8ae3e5 Mon Sep 17 00:00:00 2001
From: dumbmoron <log@riseup.net>
Date: Mon, 12 Aug 2024 16:47:11 +0000
Subject: [PATCH] web/vite: expose libav.js files directly

---
 pnpm-lock.yaml     |  3 ++
 web/package.json   |  1 +
 web/vite.config.ts | 80 ++++++++++++++++++++++++++++++++++++++--------
 3 files changed, 71 insertions(+), 13 deletions(-)

diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 58fdabb1..69bb84bc 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -149,6 +149,9 @@ importers:
       eslint:
         specifier: ^8.57.0
         version: 8.57.0
+      glob:
+        specifier: ^10.4.5
+        version: 10.4.5
       mdsvex:
         specifier: ^0.11.2
         version: 0.11.2(svelte@4.2.18)
diff --git a/web/package.json b/web/package.json
index 5f3e5d14..a87283b6 100644
--- a/web/package.json
+++ b/web/package.json
@@ -33,6 +33,7 @@
         "@types/node": "^20.14.10",
         "compare-versions": "^6.1.0",
         "eslint": "^8.57.0",
+        "glob": "^10.4.5",
         "mdsvex": "^0.11.2",
         "svelte": "^4.2.7",
         "svelte-check": "^3.6.0",
diff --git a/web/vite.config.ts b/web/vite.config.ts
index a9f2d49b..37a77956 100644
--- a/web/vite.config.ts
+++ b/web/vite.config.ts
@@ -1,22 +1,72 @@
+import { defineConfig, searchForWorkspaceRoot, type PluginOption } from "vite";
 import { sveltekit } from "@sveltejs/kit/vite";
-import { defineConfig, searchForWorkspaceRoot } from "vite";
-
 import basicSSL from "@vitejs/plugin-basic-ssl";
+import { glob } from "glob";
+import mime from "mime";
+
+import { cp, readdir, mkdir } from "node:fs/promises";
+import { createReadStream } from "node:fs";
+import { join, basename } from "node:path";
+
+const exposeLibAV: PluginOption = (() => {
+    const IMPUT_MODULE_DIR = join(__dirname, 'node_modules/@imput');
+    return {
+        name: "vite-libav.js",
+        configureServer(server) {
+            server.middlewares.use(async (req, res, next) => {
+                if (!req.url?.startsWith('/_libav/')) return next();
+
+                const filename = basename(req.url).split('?')[0];
+                if (!filename) return next();
+
+                const [ file ] = await glob(join(IMPUT_MODULE_DIR, '/**/dist/', filename));
+                if (!file) return next();
+
+                const fileType = mime.getType(filename);
+                if (!fileType) return next();
+
+                res.setHeader('Content-Type', fileType);
+                return createReadStream(file).pipe(res);
+            });
+        },
+        generateBundle: async (options) => {
+            if (!options.dir) {
+                return;
+            }
+
+            const assets = join(options.dir, '_libav');
+            await mkdir(assets, { recursive: true });
+
+            const modules = await readdir(IMPUT_MODULE_DIR).then(
+                modules => modules.filter(m => m.startsWith('libav.js'))
+            );
+
+            for (const module of modules) {
+                const distFolder = join(IMPUT_MODULE_DIR, module, 'dist/');
+                console.log(distFolder);
+                await cp(distFolder, assets, { recursive: true });
+            }
+        }
+    }
+})();
+
+const enableCOEP: PluginOption = {
+    name: "isolation",
+    configureServer(server) {
+        server.middlewares.use((_req, res, next) => {
+            res.setHeader("Cross-Origin-Opener-Policy", "same-origin");
+            res.setHeader("Cross-Origin-Embedder-Policy", "require-corp");
+            next();
+        })
+    }
+};
 
 export default defineConfig({
     plugins: [
         basicSSL(),
         sveltekit(),
-        {
-            name: "isolation",
-            configureServer(server) {
-                server.middlewares.use((_req, res, next) => {
-                    res.setHeader("Cross-Origin-Opener-Policy", "same-origin");
-                    res.setHeader("Cross-Origin-Embedder-Policy", "require-corp");
-                    next();
-                })
-            }
-        }
+        enableCOEP,
+        exposeLibAV
     ],
     build: {
         rollupOptions: {
@@ -33,6 +83,10 @@ export default defineConfig({
         }
     },
     server: {
+        headers: {
+            "Cross-Origin-Opener-Policy": "same-origin",
+            "Cross-Origin-Embedder-Policy": "require-corp"
+        },
         fs: {
             allow: [
                 searchForWorkspaceRoot(process.cwd())
@@ -41,6 +95,6 @@ export default defineConfig({
         proxy: {}
     },
     optimizeDeps: {
-        exclude: ["@imput/ffmpeg.wasm"]
+        exclude: [ "@imput/libav.js-remux-cli" ]
     },
 });