Compare commits

...

10 Commits

Author SHA1 Message Date
Derrick Hammer d2bd926275
*Switch to pnpm 2023-01-18 03:08:21 -05:00
Derrick Hammer abdb430c27
*Fix LICENSE 2023-01-15 19:08:33 -05:00
Derrick Hammer 038555ac62
*Update dist 2023-01-15 03:50:17 -05:00
Derrick Hammer b6469a6a45
*Add sourceExists 2023-01-15 03:49:26 -05:00
Derrick Hammer ae6609f0b0
*Update dist 2023-01-15 03:36:25 -05:00
Derrick Hammer 0a9197315c
*Add method to clear registered sources 2023-01-15 03:35:40 -05:00
Derrick Hammer e2ed9ed184
*Update dist 2023-01-15 02:58:50 -05:00
Derrick Hammer f278892f76
*Add optional options object to peer discovery 2023-01-15 02:58:08 -05:00
Derrick Hammer 0a780414e2
*Add dist 2022-12-20 07:28:01 -05:00
Derrick Hammer e340a79d5c
*Initial version 2022-12-20 07:27:13 -05:00
7 changed files with 193 additions and 1 deletions

View File

@ -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:

14
dist/index.d.ts vendored Normal file
View File

@ -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>;
}

44
dist/index.js vendored Normal file
View File

@ -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

14
package.json Normal file
View File

@ -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"
}
}

45
pnpm-lock.yaml Normal file
View File

@ -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

61
src/index.ts Normal file
View File

@ -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;
}
}

14
tsconfig.json Normal file
View File

@ -0,0 +1,14 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "esnext",
"sourceMap": true,
"esModuleInterop": true,
"outDir": "dist",
"declaration": true
},
"include": ["src"],
"exclude": [
"node_modules"
]
}