add dynamic

This commit is contained in:
CoolElectronics 2023-08-13 15:04:53 -04:00
parent ebe1f18b68
commit f5e0c4202e
No known key found for this signature in database
GPG key ID: F63593D168636C50
20 changed files with 1782 additions and 252 deletions

3
.gitmodules vendored
View file

@ -10,3 +10,6 @@
[submodule "vite-plugin-singlefile"]
path = vite-plugin-singlefile
url = https://github.com/CoolElectronics/vite-plugin-singlefile
[submodule "Dynamic"]
path = Dynamic
url = https://github.com/NebulaServices/Dynamic

1
Dynamic Submodule

@ -0,0 +1 @@
Subproject commit 63a735814868f0a2c0411b18b8d6e03e84007ea0

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,28 @@
self.__dynamic$config = {
prefix: '/service/',
encoding: 'xor',
mode: 'production', // development: zero caching, no minification, production: speed-oriented
logLevel: 0, // 0: none, 1: errors, 2: errors + warnings, 3: errors + warnings + info
bare: {
version: 2, // v3 is bad
path: '/bare/',
},
tab: {
title: 'Service',
icon: null,
ua: null,
},
assets: {
prefix: '/dynamic/',
files: {
handler: 'dynamic.handler.js',
client: 'dynamic.client.js',
worker: 'dynamic.worker.js',
config: 'dynamic.config.js',
inject: null,
}
},
block: [
]
};

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

80
frontend/public/sw.js Normal file
View file

@ -0,0 +1,80 @@
/*global UVServiceWorker,__uv$config*/
/*
* Stock service worker script.
* Users can provide their own sw.js if they need to extend the functionality of the service worker.
* Ideally, this will be registered under the scope in uv.config.js so it will not need to be modified.
* However, if a user changes the location of uv.bundle.js/uv.config.js or sw.js is not relative to them, they will need to modify this script locally.
*/
// import index from "sw-filemap"
// let filemap = JSON.parse(index);
let filemap = {};
importScripts('uv/uv.bundle.js');
importScripts('uv.config.js');
importScripts(__uv$config.sw || 'uv.sw.js');
importScripts('/dynamic/dynamic.config.js');
importScripts('/dynamic/dynamic.worker.js');
const dynamic = new Dynamic();
self.dynamic = dynamic;
const sw = new UVServiceWorker();
self.addEventListener('fetch', (event) => {
event.respondWith(
(async function () {
let url = new URL(event.request.url).pathname;
if (url == "/")
url = "/index.html";
if (filemap[url]) {
let contenttype = "text/plain";
if (url.includes(".js"))
contenttype = "application/javascript";
else if (url.includes(".html"))
contenttype = "text/html";
else if (url.includes(".css"))
contenttype = "text/css";
return new Response(filemap[url], {
headers: {
"content-type": contenttype
}
});
} else {
if (await dynamic.route(event)) {
return await dynamic.fetch(event);
}
if (event.request.url.startsWith(location.origin + "/uvsw/")) {
return await sw.fetch(event);
}
return await fetch(event.request)
}
})()
);
//
});
// self.addEventListener('fetch',
// event => {
// event.respondWith(
// (async function() {
// if (await dynamic.route(event)) {
// return await dynamic.fetch(event);
// }
// return await fetch(event.request);
// })()
// );
// }
// );

View file

@ -1,6 +1,6 @@
/*global Ultraviolet*/
self.__uv$config = {
prefix: '/service/',
prefix: '/uvsw/',
bare: '/bare/',
encodeUrl: Ultraviolet.codec.xor.encode,
decodeUrl: Ultraviolet.codec.xor.decode,

View file

@ -28,6 +28,8 @@
let ready = false;
let dynamic = false;
let url: string;
let proxyIframe: HTMLIFrameElement;
@ -126,7 +128,9 @@
function visitURL(url: string) {
if (!import.meta.env.VITE_ADRIFT_SINGLEFILE) {
let path = `${__uv$config.prefix}${__uv$config.encodeUrl(url)}`;
let path =
(dynamic && `/service/route?url=${url}`) ||
`${__uv$config.prefix}${__uv$config.encodeUrl(url)}`;
proxyIframe.src = path;
} else {
let bare = new BareClient();
@ -152,9 +156,15 @@
{#if ready}
<div class="container h-full w-full">
<div class="container">
<input bind:value={url} type="text" />
<button on:click={() => visitURL(url)}>Go!</button>
<div class="flex">
<div class="container">
<input bind:value={url} type="text" />
<button on:click={() => visitURL(url)}>Go!</button>
</div>
<div class="container">
<label>use dynamic?</label>
<input type="checkbox" bind:value={dynamic} />
</div>
</div>
<iframe class="h-full w-full" bind:this={proxyIframe} on:load={frameLoad} />
</div>
@ -180,6 +190,9 @@
padding: 0;
margin: 0;
}
.flex {
display: flex;
}
iframe {
outline: none;
border: none;

View file

@ -1,45 +0,0 @@
/*global UVServiceWorker,__uv$config*/
/*
* Stock service worker script.
* Users can provide their own sw.js if they need to extend the functionality of the service worker.
* Ideally, this will be registered under the scope in uv.config.js so it will not need to be modified.
* However, if a user changes the location of uv.bundle.js/uv.config.js or sw.js is not relative to them, they will need to modify this script locally.
*/
import index from "sw-filemap"
let filemap = JSON.parse(index);
importScripts('uv/uv.bundle.js');
importScripts('uv.config.js');
importScripts(__uv$config.sw || 'uv.sw.js');
const sw = new UVServiceWorker();
self.addEventListener('fetch', (event) => {
let url = new URL(event.request.url).pathname;
// console.log(url);
if (url == "/")
url = "/index.html";
if (filemap[url]) {
let contenttype = "text/plain";
if (url.includes(".js"))
contenttype = "application/javascript";
else if (url.includes(".html"))
contenttype = "text/html";
else if (url.includes(".css"))
contenttype = "text/css";
event.respondWith(new Response(filemap[url], {
headers: {
"content-type": contenttype
}
}));
} else {
event.respondWith(sw.fetch(event))
}
});

View file

@ -2,7 +2,7 @@ import { build } from 'esbuild';
import inlineImportPlugin from 'esbuild-plugin-inline-import';
import path from "path";
import fs from "fs/promises";
import _fs from "fs";
const transform = options => {
const { filter, namespace, transform } = Object.assign(
{
@ -19,6 +19,8 @@ const transform = options => {
return {
name: 'esbuild-sw-transformer',
setup(build) {
if (_fs.existsSync("./sw.js"))
fs.rm("./sw.js");
build.onResolve({ filter }, args => {
const realPath = args.path.replace(filter, '');
return {
@ -64,8 +66,9 @@ const transform = options => {
}
};
};
build({
entryPoints: ['../sw.js'],
entryPoints: ['../public/sw.js'],
bundle: true,
outfile: 'sw.js',
plugins: [

View file

@ -15,5 +15,10 @@ export default defineConfig({
target: "esnext",
outDir: "dist",
sourcemap: true,
rollupOptions: {
external: [
"./sw.js"
]
}
}
})

1712
pnpm-lock.yaml generated

File diff suppressed because it is too large Load diff

View file

@ -5,3 +5,5 @@ packages:
- client
- firebase-config
- corium
- Ultraviolet
- Dynamic

View file

@ -122,6 +122,7 @@ export class Client {
}
sendHTTPResponseChunk(seq: number, chunk: Buffer) {
if (!chunk.copy) return;
const buf = Buffer.alloc(2 + 1 + chunk.length);
let cursor = 0;
cursor = buf.writeUInt16BE(seq, cursor);