Compare commits
4 Commits
Author | SHA1 | Date |
---|---|---|
semantic-release-bot | 6b7930fc35 | |
Derrick Hammer | 6d98acce1d | |
semantic-release-bot | d3a991d998 | |
Derrick Hammer | 79f8d105c5 |
|
@ -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,13 @@
|
|||
# [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))
|
|
@ -1,14 +0,0 @@
|
|||
/// <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>;
|
||||
}
|
|
@ -1,44 +0,0 @@
|
|||
"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,14 +1,23 @@
|
|||
{
|
||||
"name": "@lumeweb/peer-discovery",
|
||||
"version": "0.1.0",
|
||||
"main": "dist/index.js",
|
||||
"name": "@lumeweb/libpeerdiscovery",
|
||||
"version": "0.1.0-develop.2",
|
||||
"type": "module",
|
||||
"main": "lib/index.js",
|
||||
"types": "lib/index.d.ts",
|
||||
"devDependencies": {
|
||||
"@types/b4a": "^1.6.0",
|
||||
"@types/node": "^18.11.17",
|
||||
"prettier": "^2.8.1",
|
||||
"typescript": "^4.9.4"
|
||||
"@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.1"
|
||||
"b4a": "^1.6.4"
|
||||
},
|
||||
"publishConfig": {
|
||||
"access": "public"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,45 +0,0 @@
|
|||
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
|
16
src/index.ts
16
src/index.ts
|
@ -7,12 +7,18 @@ export interface Peer {
|
|||
|
||||
export type PeerSource = (
|
||||
pubkey: Buffer,
|
||||
options?: any
|
||||
options?: any,
|
||||
) => Promise<boolean | Peer>;
|
||||
|
||||
export class PeerDiscovery {
|
||||
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 {
|
||||
if (this._sources.has(name)) {
|
||||
return false;
|
||||
|
@ -42,18 +48,22 @@ export class PeerDiscovery {
|
|||
|
||||
public async discover(
|
||||
pubkey: string | Buffer,
|
||||
options = {}
|
||||
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, options);
|
||||
try {
|
||||
const result = await source(pubkey as Buffer, options);
|
||||
|
||||
if (result) {
|
||||
return result;
|
||||
}
|
||||
} catch (e) {
|
||||
this._logger(e);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
|
|
|
@ -1,14 +0,0 @@
|
|||
{
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"target": "esnext",
|
||||
"sourceMap": true,
|
||||
"esModuleInterop": true,
|
||||
"outDir": "dist",
|
||||
"declaration": true
|
||||
},
|
||||
"include": ["src"],
|
||||
"exclude": [
|
||||
"node_modules"
|
||||
]
|
||||
}
|
Loading…
Reference in New Issue