Compare commits
10 Commits
Author | SHA1 | Date |
---|---|---|
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 |
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,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
|
|
@ -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,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
|
|
@ -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, options);
|
||||
|
||||
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