feat: initial version

This commit is contained in:
Derrick Hammer 2023-09-19 21:53:55 -04:00
parent 57597207bd
commit 8eb2bdfd01
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
6 changed files with 19549 additions and 1 deletions

17
.presetterrc.json Normal file
View File

@ -0,0 +1,17 @@
{
"preset": [
"@lumeweb/presetter-kernel-module-preset"
],
"config": {
"tsconfig": {
"compilerOptions": {
"module": "nodenext"
}
}
},
"scripts": {
"build": "cross-env NODE_ENV=production run-s clean build:typescript:* build:vite build:html",
"build:vite": "vite build && shx mv lib/*.cjs lib/worker.js",
"build:html": "shx cp src/index.html lib/index.html"
}
}

View File

@ -1,6 +1,6 @@
MIT License MIT License
Copyright (c) 2023 LumeWeb Copyright (c) 2023 Hammer Technologies LLC
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

19419
npm-shrinkwrap.json generated Normal file

File diff suppressed because it is too large Load Diff

22
package.json Normal file
View File

@ -0,0 +1,22 @@
{
"name": "@lumeweb/hosted-kernel-worker",
"version": "0.1.0",
"type": "module",
"readme": "ERROR: No README data found!",
"_id": "@lumeweb/hosted-kernel-worker@0.1.0",
"scripts": {
"prepare": "presetter bootstrap",
"build": "run build",
"semantic-release": "semantic-release"
},
"dependencies": {
"@lumeweb/libkernel": "^0.1.0-develop.63",
"@lumeweb/libs5": "^0.1.0-develop.60",
"multiformats": "^12.1.1"
},
"devDependencies": {
"@lumeweb/presetter-kernel-module-preset": "0.1.0-develop.44",
"multiformats": "^12.1.1",
"presetter": "*"
}
}

13
src/index.html Normal file
View File

@ -0,0 +1,13 @@
<!doctype html>
<html class="no-js" lang="">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<script type="text/javascript" src="./worker.js"></script>
</body>
</html>

77
src/index.ts Normal file
View File

@ -0,0 +1,77 @@
import { CID } from "@lumeweb/libs5";
import { blake3 } from "@noble/hashes/blake3";
import { equalBytes } from "@noble/ciphers/utils";
import { defer } from "@lumeweb/libkernel/module";
document.title = "Hosted Lume Kernel Worker";
let header = document.createElement("h1");
header.textContent =
"Something went wrong! You should not be visiting this page, this page should only be accessed via an invisible iframe.";
document.body.appendChild(header);
const initDefer = defer<Uint8Array>();
let worker: Worker;
function boot() {
let workerCID = window.location.hostname.split(".")[0];
let cid = CID.decode(workerCID);
initDefer.promise.then((module) => {
if (!equalBytes(blake3(module), cid.hash.hashBytes)) {
sendError("invalid module");
return;
}
const moduleUrl = URL.createObjectURL(new Blob([module]));
worker = new Worker(moduleUrl);
worker.onmessage = function (event: MessageEvent) {
sendMessage("workerMessage", event.data);
};
worker.onerror = function (event: ErrorEvent) {
sendError(event.message, event.error);
};
sendMessage("workerInited", {});
});
}
function sendMessage(method: string, data: any) {
window.parent.postMessage(
{
method,
data,
},
"https://kernel.lumeweb.com",
);
}
function sendError(message: string, error = null) {
sendMessage("workerError", {
data: {
message,
error,
},
});
}
onmessage = function (event: MessageEvent) {
if (window === event.source) {
return;
}
if (event.origin !== "https://kernel.lumeweb.com") {
return;
}
if (event.data.method === "workerInit") {
initDefer.resolve(event.data.module);
return;
}
worker?.postMessage(event.data);
};
boot();