mirror of
https://github.com/MercuryWorkshop/adrift.git
synced 2025-05-12 22:00:02 -04:00
add dynamic
This commit is contained in:
parent
ebe1f18b68
commit
f5e0c4202e
20 changed files with 1782 additions and 252 deletions
3
.gitmodules
vendored
3
.gitmodules
vendored
|
@ -10,3 +10,6 @@
|
||||||
[submodule "vite-plugin-singlefile"]
|
[submodule "vite-plugin-singlefile"]
|
||||||
path = vite-plugin-singlefile
|
path = vite-plugin-singlefile
|
||||||
url = https://github.com/CoolElectronics/vite-plugin-singlefile
|
url = https://github.com/CoolElectronics/vite-plugin-singlefile
|
||||||
|
[submodule "Dynamic"]
|
||||||
|
path = Dynamic
|
||||||
|
url = https://github.com/NebulaServices/Dynamic
|
||||||
|
|
1
Dynamic
Submodule
1
Dynamic
Submodule
|
@ -0,0 +1 @@
|
||||||
|
Subproject commit 63a735814868f0a2c0411b18b8d6e03e84007ea0
|
30
frontend/public/dynamic/dynamic.client.js
Normal file
30
frontend/public/dynamic/dynamic.client.js
Normal file
File diff suppressed because one or more lines are too long
7
frontend/public/dynamic/dynamic.client.js.map
Normal file
7
frontend/public/dynamic/dynamic.client.js.map
Normal file
File diff suppressed because one or more lines are too long
28
frontend/public/dynamic/dynamic.config.js
Normal file
28
frontend/public/dynamic/dynamic.config.js
Normal 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: [
|
||||||
|
|
||||||
|
]
|
||||||
|
};
|
30
frontend/public/dynamic/dynamic.handler.js
Normal file
30
frontend/public/dynamic/dynamic.handler.js
Normal file
File diff suppressed because one or more lines are too long
7
frontend/public/dynamic/dynamic.handler.js.map
Normal file
7
frontend/public/dynamic/dynamic.handler.js.map
Normal file
File diff suppressed because one or more lines are too long
9
frontend/public/dynamic/dynamic.html.js
Normal file
9
frontend/public/dynamic/dynamic.html.js
Normal file
File diff suppressed because one or more lines are too long
7
frontend/public/dynamic/dynamic.html.js.map
Normal file
7
frontend/public/dynamic/dynamic.html.js.map
Normal file
File diff suppressed because one or more lines are too long
30
frontend/public/dynamic/dynamic.worker.js
Normal file
30
frontend/public/dynamic/dynamic.worker.js
Normal file
File diff suppressed because one or more lines are too long
7
frontend/public/dynamic/dynamic.worker.js.map
Normal file
7
frontend/public/dynamic/dynamic.worker.js.map
Normal file
File diff suppressed because one or more lines are too long
80
frontend/public/sw.js
Normal file
80
frontend/public/sw.js
Normal 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);
|
||||||
|
// })()
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
// );
|
|
@ -1,6 +1,6 @@
|
||||||
/*global Ultraviolet*/
|
/*global Ultraviolet*/
|
||||||
self.__uv$config = {
|
self.__uv$config = {
|
||||||
prefix: '/service/',
|
prefix: '/uvsw/',
|
||||||
bare: '/bare/',
|
bare: '/bare/',
|
||||||
encodeUrl: Ultraviolet.codec.xor.encode,
|
encodeUrl: Ultraviolet.codec.xor.encode,
|
||||||
decodeUrl: Ultraviolet.codec.xor.decode,
|
decodeUrl: Ultraviolet.codec.xor.decode,
|
||||||
|
|
|
@ -28,6 +28,8 @@
|
||||||
|
|
||||||
let ready = false;
|
let ready = false;
|
||||||
|
|
||||||
|
let dynamic = false;
|
||||||
|
|
||||||
let url: string;
|
let url: string;
|
||||||
let proxyIframe: HTMLIFrameElement;
|
let proxyIframe: HTMLIFrameElement;
|
||||||
|
|
||||||
|
@ -126,7 +128,9 @@
|
||||||
|
|
||||||
function visitURL(url: string) {
|
function visitURL(url: string) {
|
||||||
if (!import.meta.env.VITE_ADRIFT_SINGLEFILE) {
|
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;
|
proxyIframe.src = path;
|
||||||
} else {
|
} else {
|
||||||
let bare = new BareClient();
|
let bare = new BareClient();
|
||||||
|
@ -152,10 +156,16 @@
|
||||||
|
|
||||||
{#if ready}
|
{#if ready}
|
||||||
<div class="container h-full w-full">
|
<div class="container h-full w-full">
|
||||||
|
<div class="flex">
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<input bind:value={url} type="text" />
|
<input bind:value={url} type="text" />
|
||||||
<button on:click={() => visitURL(url)}>Go!</button>
|
<button on:click={() => visitURL(url)}>Go!</button>
|
||||||
</div>
|
</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} />
|
<iframe class="h-full w-full" bind:this={proxyIframe} on:load={frameLoad} />
|
||||||
</div>
|
</div>
|
||||||
{:else if !import.meta.env.VITE_ADRIFT_DEV}
|
{:else if !import.meta.env.VITE_ADRIFT_DEV}
|
||||||
|
@ -180,6 +190,9 @@
|
||||||
padding: 0;
|
padding: 0;
|
||||||
margin: 0;
|
margin: 0;
|
||||||
}
|
}
|
||||||
|
.flex {
|
||||||
|
display: flex;
|
||||||
|
}
|
||||||
iframe {
|
iframe {
|
||||||
outline: none;
|
outline: none;
|
||||||
border: none;
|
border: none;
|
||||||
|
|
|
@ -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))
|
|
||||||
}
|
|
||||||
});
|
|
|
@ -2,7 +2,7 @@ import { build } from 'esbuild';
|
||||||
import inlineImportPlugin from 'esbuild-plugin-inline-import';
|
import inlineImportPlugin from 'esbuild-plugin-inline-import';
|
||||||
import path from "path";
|
import path from "path";
|
||||||
import fs from "fs/promises";
|
import fs from "fs/promises";
|
||||||
|
import _fs from "fs";
|
||||||
const transform = options => {
|
const transform = options => {
|
||||||
const { filter, namespace, transform } = Object.assign(
|
const { filter, namespace, transform } = Object.assign(
|
||||||
{
|
{
|
||||||
|
@ -19,6 +19,8 @@ const transform = options => {
|
||||||
return {
|
return {
|
||||||
name: 'esbuild-sw-transformer',
|
name: 'esbuild-sw-transformer',
|
||||||
setup(build) {
|
setup(build) {
|
||||||
|
if (_fs.existsSync("./sw.js"))
|
||||||
|
fs.rm("./sw.js");
|
||||||
build.onResolve({ filter }, args => {
|
build.onResolve({ filter }, args => {
|
||||||
const realPath = args.path.replace(filter, '');
|
const realPath = args.path.replace(filter, '');
|
||||||
return {
|
return {
|
||||||
|
@ -64,8 +66,9 @@ const transform = options => {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
build({
|
build({
|
||||||
entryPoints: ['../sw.js'],
|
entryPoints: ['../public/sw.js'],
|
||||||
bundle: true,
|
bundle: true,
|
||||||
outfile: 'sw.js',
|
outfile: 'sw.js',
|
||||||
plugins: [
|
plugins: [
|
||||||
|
|
|
@ -15,5 +15,10 @@ export default defineConfig({
|
||||||
target: "esnext",
|
target: "esnext",
|
||||||
outDir: "dist",
|
outDir: "dist",
|
||||||
sourcemap: true,
|
sourcemap: true,
|
||||||
|
rollupOptions: {
|
||||||
|
external: [
|
||||||
|
"./sw.js"
|
||||||
|
]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
1712
pnpm-lock.yaml
generated
1712
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load diff
|
@ -5,3 +5,5 @@ packages:
|
||||||
- client
|
- client
|
||||||
- firebase-config
|
- firebase-config
|
||||||
- corium
|
- corium
|
||||||
|
- Ultraviolet
|
||||||
|
- Dynamic
|
||||||
|
|
|
@ -122,6 +122,7 @@ export class Client {
|
||||||
}
|
}
|
||||||
|
|
||||||
sendHTTPResponseChunk(seq: number, chunk: Buffer) {
|
sendHTTPResponseChunk(seq: number, chunk: Buffer) {
|
||||||
|
if (!chunk.copy) return;
|
||||||
const buf = Buffer.alloc(2 + 1 + chunk.length);
|
const buf = Buffer.alloc(2 + 1 + chunk.length);
|
||||||
let cursor = 0;
|
let cursor = 0;
|
||||||
cursor = buf.writeUInt16BE(seq, cursor);
|
cursor = buf.writeUInt16BE(seq, cursor);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue