Compare commits

...

10 Commits

6 changed files with 21681 additions and 0 deletions

18
.github/workflows/ci.yml vendored Normal file
View File

@ -0,0 +1,18 @@
name: Build/Publish
on:
push:
branches:
- master
- develop
- develop-*
workflow_dispatch:
inputs:
debug_enabled:
description: Debug
type: boolean
default: false
jobs:
main:
uses: lumeweb/github-node-deploy-workflow/.github/workflows/main.yml@master
secrets: inherit

8
.presetterrc.json Normal file
View File

@ -0,0 +1,8 @@
{
"preset": [
"@lumeweb/presetter-kernel-module-preset"
],
"config": {
"official": true
}
}

1
CHANGELOG.md Normal file
View File

@ -0,0 +1 @@
## [0.0.2-develop.1](https://git.lumeweb.com/LumeWeb/kernel-peer-discovery/compare/v0.0.1...v0.0.2-develop.1) (2023-06-30)

21532
npm-shrinkwrap.json generated Normal file

File diff suppressed because it is too large Load Diff

24
package.json Normal file
View File

@ -0,0 +1,24 @@
{
"name": "@lumeweb/kernel-peer-discovery",
"version": "0.0.2-develop.1",
"type": "module",
"repository": {
"type": "git",
"url": "gitea@git.lumeweb.com:LumeWeb/kernel-peer-discovery.git"
},
"scripts": {
"prepare": "presetter bootstrap",
"build": "run build",
"semantic-release": "semantic-release"
},
"readme": "ERROR: No README data found!",
"devDependencies": {
"@lumeweb/presetter-kernel-module-preset": "^0.1.0-develop.20",
"@rollup/plugin-typescript": "^11.1.2",
"presetter": "*"
},
"dependencies": {
"@lumeweb/libkernel": "^0.1.0-develop.6",
"@lumeweb/libpeerdiscovery": "^0.1.0-develop.1"
}
}

98
src/index.ts Normal file
View File

@ -0,0 +1,98 @@
import { addHandler, handleMessage } from "@lumeweb/libkernel/module";
import type { ActiveQuery } from "@lumeweb/libkernel/module";
import { PeerDiscovery } from "@lumeweb/libpeerdiscovery";
import type { PeerSource, Peer } from "@lumeweb/libpeerdiscovery";
import { callModule, logErr } from "@lumeweb/libkernel";
onmessage = handleMessage;
const discovery = new PeerDiscovery();
function wrapSourceModule(module: string): PeerSource {
return async (pubkey: Buffer, options = {}): Promise<boolean | Peer> => {
const [ret, err] = await callModule(module, "discover", {
pubkey,
options,
});
if (err) {
logErr(err);
return false;
}
return ret as Peer;
};
}
async function handleRegisterSource(aq: ActiveQuery): Promise<void> {
let [name, error] = await callModule(aq.domain, "name");
if (error) {
aq.reject(error);
return;
}
if (discovery.sourceExists(name)) {
aq.reject(`Source ${name} already exists`);
return;
}
discovery.registerSource(name, wrapSourceModule(aq.domain));
aq.respond();
}
function handleRemoveSource(aq: ActiveQuery): void {
if (!("name" in aq.callerInput)) {
aq.reject(`missing name`);
return;
}
aq.respond(discovery.removeSource(aq.callerInput.name));
}
function handleRemoveAllSources(aq: ActiveQuery): void {
discovery.removeAllSources();
aq.respond();
}
function handleSourceExists(aq: ActiveQuery): void {
if (!("name" in aq.callerInput)) {
aq.reject(`missing name`);
return;
}
aq.respond(discovery.sourceExists(aq.callerInput.name));
}
async function handleDiscover(aq: ActiveQuery) {
if (!("pubkey" in aq.callerInput)) {
aq.reject(`missing pubkey`);
return;
}
if (aq.callerInput.pubkey.length !== 32) {
aq.reject("pubkey must be 32 bytes");
return;
}
if (
"options" in aq.callerInput &&
typeof aq.callerInput.options !== "object"
) {
aq.reject(`options must be an object`);
return;
}
const ret = await discovery.discover(
aq.callerInput.pubkey,
aq.callerInput.options,
);
aq.respond(ret);
}
addHandler("register", handleRegisterSource);
addHandler("remove", handleRemoveSource);
addHandler("removeAll", handleRemoveAllSources);
addHandler("exists", handleSourceExists);
addHandler("discover", handleDiscover);