feat: initial version

This commit is contained in:
Derrick Hammer 2023-09-01 06:55:37 -04:00
parent 2fb2a46717
commit 904abe6abd
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
5 changed files with 19580 additions and 1 deletions

27
.presetterrc.json Normal file
View File

@ -0,0 +1,27 @@
{
"preset": [
"@lumeweb/presetter-kernel-module-preset"
],
"config": {
"official": true,
"vite": {
"resolve": {
"alias": {
"protomux": "@lumeweb/kernel-protomux-client",
"url": "whatwg-url"
}
},
"polyfill": {
"exclude": [
"fs",
"url"
]
}
},
"tsconfig": {
"compilerOptions": {
"module": "nodenext"
}
}
}
}

View File

@ -1,6 +1,6 @@
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:

19443
npm-shrinkwrap.json generated Normal file

File diff suppressed because it is too large Load Diff

26
package.json Normal file
View File

@ -0,0 +1,26 @@
{
"name": "@lumeweb/kernel-s5",
"version": "0.1.0",
"type": "module",
"readme": "ERROR: No README data found!",
"_id": "@lumeweb/kernel-s5@0.1.0",
"repository": {
"type": "git",
"url": "gitea@git.lumeweb.com:LumeWeb/kernel-s5.git"
},
"scripts": {
"prepare": "presetter bootstrap",
"build": "run build",
"semantic-release": "semantic-release"
},
"dependencies": {
"@lumeweb/kernel-protomux-client": "^0.0.2-develop.16",
"@lumeweb/kernel-swarm-client": "^0.1.0-develop.5",
"@lumeweb/libkernel": "^0.1.0-develop.36",
"@lumeweb/libs5-transport-hyper": "^0.1.0-develop.10",
"whatwg-url": "^13.0.0"
},
"devDependencies": {
"@lumeweb/presetter-kernel-module-preset": "^0.1.0-develop.44"
}
}

83
src/index.ts Normal file
View File

@ -0,0 +1,83 @@
import { concatBytes } from "@lumeweb/libkernel";
import {
addHandler,
defer,
getKey,
handlePresentKey as handlePresentKeyModule,
} from "@lumeweb/libkernel/module";
import type { ActiveQuery } from "@lumeweb/libkernel/module";
import { createClient as createSwarmClient } from "@lumeweb/kernel-swarm-client";
import Protomux from "@lumeweb/kernel-protomux-client";
import {
createKeyPair,
createNode,
NodeId,
S5NodeConfig,
} from "@lumeweb/libs5";
import { mkeyEd25519 } from "@lumeweb/libs5/lib/constants.js";
import { Level } from "level";
import HyperTransportPeer from "@lumeweb/libs5-transport-hyper";
const PROTOCOL = "lumeweb.service.s5";
const moduleReadyDefer = defer();
let swarm;
addHandler("presentKey", handlePresentKey);
addHandler("ready", ready);
async function handlePresentKey(aq: ActiveQuery) {
handlePresentKeyModule({
callerInput: {
key: aq.callerInput.rootKey,
},
} as ActiveQuery);
await setup();
moduleReadyDefer.resolve();
}
async function setup() {
swarm = createSwarmClient();
const db = new Level<string, Uint8Array>("s5");
await db.open();
let config = {
keyPair: createKeyPair(await getKey()),
db,
p2p: {
peers: {
initial: [],
},
},
} as S5NodeConfig;
swarm.join(PROTOCOL);
await swarm.start();
await swarm.ready();
const node = createNode(config);
await node.start();
swarm.on("connection", async (peer: any) => {
const muxer = Protomux.from(peer);
const s5peer = new HyperTransportPeer({
muxer,
peer,
protocol: PROTOCOL,
});
s5peer.id = new NodeId(
concatBytes(Uint8Array.from([mkeyEd25519]), peer.remotePublicKey),
);
await s5peer.init();
node.services.p2p.onNewPeer(s5peer, true);
});
}
async function ready() {
await moduleReadyDefer.promise;
}