Compare commits
2 Commits
46f7201bde
...
0a780414e2
Author | SHA1 | Date |
---|---|---|
|
0a780414e2 | |
|
e340a79d5c |
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:
|
||||
|
||||
|
|
|
@ -0,0 +1,12 @@
|
|||
/// <reference types="node" />
|
||||
export interface Peer {
|
||||
host: string;
|
||||
port: number;
|
||||
}
|
||||
export type PeerSource = (pubkey: Buffer) => Promise<boolean | Peer>;
|
||||
export declare class PeerDiscovery {
|
||||
private _sources;
|
||||
registerSource(name: string, source: PeerSource): boolean;
|
||||
removeSource(name: string): boolean;
|
||||
discover(pubkey: string | Buffer): Promise<Peer | boolean>;
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
"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;
|
||||
}
|
||||
async discover(pubkey) {
|
||||
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);
|
||||
if (result) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
exports.PeerDiscovery = PeerDiscovery;
|
||||
//# sourceMappingURL=index.js.map
|
|
@ -0,0 +1,14 @@
|
|||
{
|
||||
"name": "@lumeweb/peer-discovery",
|
||||
"version": "0.1.0",
|
||||
"main": "dist/index.js",
|
||||
"devDependencies": {
|
||||
"@types/b4a": "^1.6.0",
|
||||
"@types/node": "^18.11.17",
|
||||
"prettier": "^2.8.1",
|
||||
"typescript": "^4.9.4"
|
||||
},
|
||||
"dependencies": {
|
||||
"b4a": "^1.6.1"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,47 @@
|
|||
import b4a from "b4a";
|
||||
|
||||
export interface Peer {
|
||||
host: string;
|
||||
port: number;
|
||||
}
|
||||
|
||||
export type PeerSource = (pubkey: Buffer) => 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 async discover(pubkey: string | Buffer): 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);
|
||||
|
||||
if (result) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
|
@ -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