Compare commits
12 Commits
v0.0.1
...
v0.1.0-dev
Author | SHA1 | Date |
---|---|---|
semantic-release-bot | d3a991d998 | |
Derrick Hammer | 79f8d105c5 | |
Derrick Hammer | d2bd926275 | |
Derrick Hammer | abdb430c27 | |
Derrick Hammer | 038555ac62 | |
Derrick Hammer | b6469a6a45 | |
Derrick Hammer | ae6609f0b0 | |
Derrick Hammer | 0a9197315c | |
Derrick Hammer | e2ed9ed184 | |
Derrick Hammer | f278892f76 | |
Derrick Hammer | 0a780414e2 | |
Derrick Hammer | e340a79d5c |
|
@ -0,0 +1,13 @@
|
|||
name: Build/Publish
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- master
|
||||
- develop
|
||||
- develop-*
|
||||
|
||||
jobs:
|
||||
main:
|
||||
uses: lumeweb/github-node-deploy-workflow/.github/workflows/main.yml@master
|
||||
secrets: inherit
|
|
@ -0,0 +1,5 @@
|
|||
{
|
||||
"preset": [
|
||||
"@lumeweb/node-library-preset"
|
||||
]
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
# [0.1.0-develop.1](https://git.lumeweb.com/LumeWeb/libpeerdiscovery/compare/v0.0.1...v0.1.0-develop.1) (2023-07-12)
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* initial release ([79f8d10](https://git.lumeweb.com/LumeWeb/libpeerdiscovery/commit/79f8d105c56ed0f11ca92f645db6ba5cca4a8ca7))
|
2
LICENSE
2
LICENSE
|
@ -1,6 +1,6 @@
|
|||
MIT License
|
||||
|
||||
Copyright (c) <year> <copyright holders>
|
||||
Copyright (c) 2022 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:
|
||||
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,23 @@
|
|||
{
|
||||
"name": "@lumeweb/libpeerdiscovery",
|
||||
"version": "0.1.0-develop.1",
|
||||
"type": "module",
|
||||
"main": "lib/index.js",
|
||||
"types": "lib/index.d.ts",
|
||||
"devDependencies": {
|
||||
"@lumeweb/node-library-preset": "^0.2.5",
|
||||
"presetter": "*"
|
||||
},
|
||||
"readme": "ERROR: No README data found!",
|
||||
"scripts": {
|
||||
"prepare": "presetter bootstrap",
|
||||
"build": "run build",
|
||||
"semantic-release": "semantic-release"
|
||||
},
|
||||
"dependencies": {
|
||||
"b4a": "^1.6.4"
|
||||
},
|
||||
"publishConfig": {
|
||||
"access": "public"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,61 @@
|
|||
import b4a from "b4a";
|
||||
|
||||
export interface Peer {
|
||||
host: string;
|
||||
port: number;
|
||||
}
|
||||
|
||||
export type PeerSource = (
|
||||
pubkey: Buffer,
|
||||
options?: any,
|
||||
) => Promise<boolean | Peer>;
|
||||
|
||||
export class PeerDiscovery {
|
||||
private _sources: Map<string, PeerSource> = new Map<string, PeerSource>();
|
||||
|
||||
public registerSource(name: string, source: PeerSource): boolean {
|
||||
if (this._sources.has(name)) {
|
||||
return false;
|
||||
}
|
||||
this._sources.set(name, source);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public removeSource(name: string): boolean {
|
||||
if (!this._sources.has(name)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
this._sources.delete(name);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public removeAllSources(): void {
|
||||
this._sources.clear();
|
||||
}
|
||||
|
||||
public sourceExists(name: string): boolean {
|
||||
return this._sources.has(name);
|
||||
}
|
||||
|
||||
public async discover(
|
||||
pubkey: string | Buffer,
|
||||
options = {},
|
||||
): Promise<Peer | boolean> {
|
||||
if (!b4a.isBuffer(pubkey)) {
|
||||
pubkey = b4a.from(pubkey, "hex") as Buffer;
|
||||
}
|
||||
|
||||
for (const source of this._sources.values()) {
|
||||
const result = await source(pubkey as Buffer, options);
|
||||
|
||||
if (result) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue