Compare commits

...

12 Commits

Author SHA1 Message Date
semantic-release-bot d3a991d998
chore(release): 0.1.0-develop.1 [skip ci]
# [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](79f8d105c5))
2023-07-12 05:42:00 -04:00
Derrick Hammer 79f8d105c5
feat: initial release 2023-06-27 23:46:18 -04:00
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
8 changed files with 18857 additions and 3 deletions

13
.github/workflows/ci.yml vendored Normal file
View File

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

5
.presetterrc.json Normal file
View File

@ -0,0 +1,5 @@
{
"preset": [
"@lumeweb/node-library-preset"
]
}

6
CHANGELOG.md Normal file
View File

@ -0,0 +1,6 @@
# [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))

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:

View File

@ -1,2 +1 @@
# peer-discovery
# libpeerdiscovery

18747
npm-shrinkwrap.json generated Normal file

File diff suppressed because it is too large Load Diff

23
package.json Normal file
View File

@ -0,0 +1,23 @@
{
"name": "@lumeweb/libpeerdiscovery",
"version": "0.1.0-develop.1",
"type": "module",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"devDependencies": {
"@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.4"
},
"publishConfig": {
"access": "public"
}
}

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 as Buffer, options);
if (result) {
return result;
}
}
return false;
}
}