Compare commits
No commits in common. "develop" and "master" have entirely different histories.
|
@ -1,13 +0,0 @@
|
||||||
name: Build/Publish
|
|
||||||
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
branches:
|
|
||||||
- master
|
|
||||||
- develop
|
|
||||||
- develop-*
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
main:
|
|
||||||
uses: lumeweb/github-node-deploy-workflow/.github/workflows/main.yml@master
|
|
||||||
secrets: inherit
|
|
|
@ -1,5 +0,0 @@
|
||||||
{
|
|
||||||
"preset": [
|
|
||||||
"@lumeweb/node-library-preset"
|
|
||||||
]
|
|
||||||
}
|
|
13
CHANGELOG.md
13
CHANGELOG.md
|
@ -1,13 +0,0 @@
|
||||||
# [0.1.0-develop.2](https://git.lumeweb.com/LumeWeb/libpeerdiscovery/compare/v0.1.0-develop.1...v0.1.0-develop.2) (2023-07-12)
|
|
||||||
|
|
||||||
|
|
||||||
### Bug Fixes
|
|
||||||
|
|
||||||
* use try catch and use local logger function if there is an error ([6d98acc](https://git.lumeweb.com/LumeWeb/libpeerdiscovery/commit/6d98acce1dd04e5bd1795a372d74058129b10721))
|
|
||||||
|
|
||||||
# [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))
|
|
|
@ -0,0 +1,14 @@
|
||||||
|
/// <reference types="node" />
|
||||||
|
export interface Peer {
|
||||||
|
host: string;
|
||||||
|
port: number;
|
||||||
|
}
|
||||||
|
export type PeerSource = (pubkey: Buffer, options?: any) => Promise<boolean | Peer>;
|
||||||
|
export declare class PeerDiscovery {
|
||||||
|
private _sources;
|
||||||
|
registerSource(name: string, source: PeerSource): boolean;
|
||||||
|
removeSource(name: string): boolean;
|
||||||
|
removeAllSources(): void;
|
||||||
|
sourceExists(name: string): boolean;
|
||||||
|
discover(pubkey: string | Buffer, options?: {}): Promise<Peer | boolean>;
|
||||||
|
}
|
|
@ -0,0 +1,44 @@
|
||||||
|
"use strict";
|
||||||
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||||
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||||
|
};
|
||||||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
|
exports.PeerDiscovery = void 0;
|
||||||
|
const b4a_1 = __importDefault(require("b4a"));
|
||||||
|
class PeerDiscovery {
|
||||||
|
_sources = new Map();
|
||||||
|
registerSource(name, source) {
|
||||||
|
if (this._sources.has(name)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
this._sources.set(name, source);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
removeSource(name) {
|
||||||
|
if (!this._sources.has(name)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
this._sources.delete(name);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
removeAllSources() {
|
||||||
|
this._sources.clear();
|
||||||
|
}
|
||||||
|
sourceExists(name) {
|
||||||
|
return this._sources.has(name);
|
||||||
|
}
|
||||||
|
async discover(pubkey, options = {}) {
|
||||||
|
if (!b4a_1.default.isBuffer(pubkey)) {
|
||||||
|
pubkey = b4a_1.default.from(pubkey, "hex");
|
||||||
|
}
|
||||||
|
for (const source of this._sources.values()) {
|
||||||
|
const result = await source(pubkey, options);
|
||||||
|
if (result) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
exports.PeerDiscovery = PeerDiscovery;
|
||||||
|
//# sourceMappingURL=index.js.map
|
File diff suppressed because it is too large
Load Diff
25
package.json
25
package.json
|
@ -1,23 +1,14 @@
|
||||||
{
|
{
|
||||||
"name": "@lumeweb/libpeerdiscovery",
|
"name": "@lumeweb/peer-discovery",
|
||||||
"version": "0.1.0-develop.2",
|
"version": "0.1.0",
|
||||||
"type": "module",
|
"main": "dist/index.js",
|
||||||
"main": "lib/index.js",
|
|
||||||
"types": "lib/index.d.ts",
|
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@lumeweb/node-library-preset": "^0.2.5",
|
"@types/b4a": "^1.6.0",
|
||||||
"presetter": "*"
|
"@types/node": "^18.11.17",
|
||||||
},
|
"prettier": "^2.8.1",
|
||||||
"readme": "ERROR: No README data found!",
|
"typescript": "^4.9.4"
|
||||||
"scripts": {
|
|
||||||
"prepare": "presetter bootstrap",
|
|
||||||
"build": "run build",
|
|
||||||
"semantic-release": "semantic-release"
|
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"b4a": "^1.6.4"
|
"b4a": "^1.6.1"
|
||||||
},
|
|
||||||
"publishConfig": {
|
|
||||||
"access": "public"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,45 @@
|
||||||
|
lockfileVersion: 5.4
|
||||||
|
|
||||||
|
specifiers:
|
||||||
|
'@types/b4a': ^1.6.0
|
||||||
|
'@types/node': ^18.11.17
|
||||||
|
b4a: ^1.6.1
|
||||||
|
prettier: ^2.8.1
|
||||||
|
typescript: ^4.9.4
|
||||||
|
|
||||||
|
dependencies:
|
||||||
|
b4a: 1.6.1
|
||||||
|
|
||||||
|
devDependencies:
|
||||||
|
'@types/b4a': 1.6.0
|
||||||
|
'@types/node': 18.11.18
|
||||||
|
prettier: 2.8.3
|
||||||
|
typescript: 4.9.4
|
||||||
|
|
||||||
|
packages:
|
||||||
|
|
||||||
|
/@types/b4a/1.6.0:
|
||||||
|
resolution: {integrity: sha512-rYU2r5nSUPyKyufWijxgTjsFp2kLCwydj2TmKU4StJeGPHS/Fs5KHgP89DNF0jddyeAbN5mdjNDqIrjIHca60g==}
|
||||||
|
dependencies:
|
||||||
|
'@types/node': 18.11.18
|
||||||
|
dev: true
|
||||||
|
|
||||||
|
/@types/node/18.11.18:
|
||||||
|
resolution: {integrity: sha512-DHQpWGjyQKSHj3ebjFI/wRKcqQcdR+MoFBygntYOZytCqNfkd2ZC4ARDJ2DQqhjH5p85Nnd3jhUJIXrszFX/JA==}
|
||||||
|
dev: true
|
||||||
|
|
||||||
|
/b4a/1.6.1:
|
||||||
|
resolution: {integrity: sha512-AsKjNhz72yxteo/0EtQEiwkMUgk/tGmycXlbG4g3Ard2/ULtNLUykGOkeK0egmN27h0xMAhb76jYccW+XTBExA==}
|
||||||
|
dev: false
|
||||||
|
|
||||||
|
/prettier/2.8.3:
|
||||||
|
resolution: {integrity: sha512-tJ/oJ4amDihPoufT5sM0Z1SKEuKay8LfVAMlbbhnnkvt6BUserZylqo2PN+p9KeljLr0OHa2rXHU1T8reeoTrw==}
|
||||||
|
engines: {node: '>=10.13.0'}
|
||||||
|
hasBin: true
|
||||||
|
dev: true
|
||||||
|
|
||||||
|
/typescript/4.9.4:
|
||||||
|
resolution: {integrity: sha512-Uz+dTXYzxXXbsFpM86Wh3dKCxrQqUcVMxwU54orwlJjOpO3ao8L7j5lH+dWfTwgCwIuM9GQ2kvVotzYJMXTBZg==}
|
||||||
|
engines: {node: '>=4.2.0'}
|
||||||
|
hasBin: true
|
||||||
|
dev: true
|
20
src/index.ts
20
src/index.ts
|
@ -7,18 +7,12 @@ export interface Peer {
|
||||||
|
|
||||||
export type PeerSource = (
|
export type PeerSource = (
|
||||||
pubkey: Buffer,
|
pubkey: Buffer,
|
||||||
options?: any,
|
options?: any
|
||||||
) => Promise<boolean | Peer>;
|
) => Promise<boolean | Peer>;
|
||||||
|
|
||||||
export class PeerDiscovery {
|
export class PeerDiscovery {
|
||||||
private _sources: Map<string, PeerSource> = new Map<string, PeerSource>();
|
private _sources: Map<string, PeerSource> = new Map<string, PeerSource>();
|
||||||
|
|
||||||
private _logger = console.log;
|
|
||||||
|
|
||||||
set logger(value: (...data: any[]) => void) {
|
|
||||||
this._logger = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
public registerSource(name: string, source: PeerSource): boolean {
|
public registerSource(name: string, source: PeerSource): boolean {
|
||||||
if (this._sources.has(name)) {
|
if (this._sources.has(name)) {
|
||||||
return false;
|
return false;
|
||||||
|
@ -48,21 +42,17 @@ export class PeerDiscovery {
|
||||||
|
|
||||||
public async discover(
|
public async discover(
|
||||||
pubkey: string | Buffer,
|
pubkey: string | Buffer,
|
||||||
options = {},
|
options = {}
|
||||||
): Promise<Peer | boolean> {
|
): Promise<Peer | boolean> {
|
||||||
if (!b4a.isBuffer(pubkey)) {
|
if (!b4a.isBuffer(pubkey)) {
|
||||||
pubkey = b4a.from(pubkey, "hex") as Buffer;
|
pubkey = b4a.from(pubkey, "hex") as Buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const source of this._sources.values()) {
|
for (const source of this._sources.values()) {
|
||||||
try {
|
const result = await source(pubkey, options);
|
||||||
const result = await source(pubkey as Buffer, options);
|
|
||||||
|
|
||||||
if (result) {
|
if (result) {
|
||||||
return result;
|
return result;
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
this._logger(e);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,14 @@
|
||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"module": "commonjs",
|
||||||
|
"target": "esnext",
|
||||||
|
"sourceMap": true,
|
||||||
|
"esModuleInterop": true,
|
||||||
|
"outDir": "dist",
|
||||||
|
"declaration": true
|
||||||
|
},
|
||||||
|
"include": ["src"],
|
||||||
|
"exclude": [
|
||||||
|
"node_modules"
|
||||||
|
]
|
||||||
|
}
|
Loading…
Reference in New Issue