ssb-conn
Advanced tools
Comparing version 0.15.1 to 0.15.2-rc0
@@ -5,2 +5,4 @@ export declare class ConnScheduler { | ||
private readonly hasSsbDb; | ||
private pubDiscoveryPausable?; | ||
private intervalForUpdate?; | ||
private closed; | ||
@@ -7,0 +9,0 @@ private isLoadingHops; |
@@ -111,2 +111,3 @@ "use strict"; | ||
this.start = () => { | ||
var _a, _b; | ||
if (!this.closed) | ||
@@ -130,9 +131,18 @@ return; | ||
this.setupPubDiscovery(); | ||
(_a = this.pubDiscoveryPausable) === null || _a === void 0 ? void 0 : _a.resume(); | ||
this.setupLanDiscovery(); | ||
this.setupBluetoothDiscovery(); | ||
const int = setInterval(() => this.updateSoon(), 2e3); | ||
if (int.unref) | ||
int.unref(); | ||
onWakeup(() => this.ssb.conn.hub().reset()); | ||
onNetwork(() => this.ssb.conn.hub().reset()); | ||
this.intervalForUpdate = setInterval(() => this.updateSoon(), 2e3); | ||
if ((_b = this.intervalForUpdate) === null || _b === void 0 ? void 0 : _b.unref) | ||
this.intervalForUpdate.unref(); | ||
onWakeup(() => { | ||
if (this.closed) | ||
return; | ||
this.ssb.conn.hub().reset(); | ||
}); | ||
onNetwork(() => { | ||
if (this.closed) | ||
return; | ||
this.ssb.conn.hub().reset(); | ||
}); | ||
pull(this.ssb.conn.hub().listen(), pull.filter((ev) => ev.type === 'disconnected'), pull.drain(() => this.updateSoon(200))); | ||
@@ -142,4 +152,9 @@ this.updateSoon(); | ||
this.stop = () => { | ||
var _a, _b, _c; | ||
(_c = (_a = this.ssb.lan) === null || _a === void 0 ? void 0 : (_b = _a).stop) === null || _c === void 0 ? void 0 : _c.call(_b); | ||
var _a, _b, _c, _d; | ||
(_a = this.pubDiscoveryPausable) === null || _a === void 0 ? void 0 : _a.pause(); | ||
(_d = (_b = this.ssb.lan) === null || _b === void 0 ? void 0 : (_c = _b).stop) === null || _d === void 0 ? void 0 : _d.call(_c); | ||
if (this.intervalForUpdate) { | ||
clearInterval(this.intervalForUpdate); | ||
this.intervalForUpdate = void 0; | ||
} | ||
this.ssb.conn.hub().reset(); | ||
@@ -332,2 +347,4 @@ this.closed = true; | ||
updateNow() { | ||
if (this.closed) | ||
return; | ||
if (this.hasSsbDb && !this.ssb.ready()) | ||
@@ -357,3 +374,5 @@ return; | ||
markDefunct([addr, data]) { | ||
this.ssb.conn.db().replace(addr, { defunct: true, birth: data.birth }); | ||
this.ssb.conn | ||
.db() | ||
.replace(addr, { defunct: true, birth: data.birth, autoconnect: false }); | ||
} | ||
@@ -378,5 +397,6 @@ populateWithSeeds() { | ||
setTimeout(() => { | ||
var _a; | ||
const MAX_STAGED_PUBS = 3; | ||
const pausable = Pausable(); | ||
pull(this.ssb.messagesByType({ type: 'pub', live: true, keys: false }), pull.filter((msg) => !msg.sync), pull.asyncMap((x, cb) => setTimeout(() => cb(null, x), 250)), pull.filter((msg) => { var _a; return ((_a = msg.content) === null || _a === void 0 ? void 0 : _a.address) && Ref.isAddress(msg.content.address); }), pausable, pull.drain((msg) => { | ||
this.pubDiscoveryPausable = (_a = this.pubDiscoveryPausable, (_a !== null && _a !== void 0 ? _a : Pausable())); | ||
pull(this.ssb.messagesByType({ type: 'pub', live: true, keys: false }), pull.filter((msg) => !msg.sync), pull.asyncMap((x, cb) => setTimeout(() => cb(null, x), 250)), pull.filter((msg) => { var _a; return ((_a = msg.content) === null || _a === void 0 ? void 0 : _a.address) && Ref.isAddress(msg.content.address); }), this.pubDiscoveryPausable, pull.drain((msg) => { | ||
try { | ||
@@ -402,8 +422,11 @@ const address = Ref.toMultiServerAddress(msg.content.address); | ||
pull(this.ssb.conn.staging().liveEntries(), pull.drain((staged) => { | ||
var _a, _b; | ||
if (this.closed) | ||
return; | ||
const stagedPubs = staged.filter(([, data]) => data.type === 'pub'); | ||
if (stagedPubs.length >= MAX_STAGED_PUBS) { | ||
pausable.pause(); | ||
(_a = this.pubDiscoveryPausable) === null || _a === void 0 ? void 0 : _a.pause(); | ||
} | ||
else { | ||
pausable.resume(); | ||
(_b = this.pubDiscoveryPausable) === null || _b === void 0 ? void 0 : _b.resume(); | ||
} | ||
@@ -420,2 +443,4 @@ })); | ||
pull(this.ssb.bluetooth.nearbyScuttlebuttDevices(1000), pull.drain(({ discovered }) => { | ||
if (this.closed) | ||
return; | ||
for (const btPeer of discovered) { | ||
@@ -422,0 +447,0 @@ const address = `bt:${btPeer.remoteAddress.split(':').join('')}` + |
102
package.json
{ | ||
"name": "ssb-conn", | ||
"description": "SSB plugin for establishing and managing peer connections", | ||
"version": "0.15.1", | ||
"homepage": "https://github.com/staltz/ssb-conn", | ||
"main": "lib/index.js", | ||
"types": "lib/index.d.ts", | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/staltz/ssb-conn.git" | ||
}, | ||
"dependencies": { | ||
"debug": "~4.1.1", | ||
"has-network2": "0.0.1", | ||
"ip": "^1.1.5", | ||
"on-change-network-strict": "1.0.0", | ||
"on-wakeup": "^1.0.1", | ||
"pull-notify": "^0.1.1", | ||
"pull-pause": "~0.0.2", | ||
"pull-ping": "^2.0.3", | ||
"pull-stream": "^3.6.14", | ||
"secret-stack-decorators": "1.0.0", | ||
"ssb-conn-db": "~0.3.0", | ||
"ssb-conn-hub": "~0.2.7", | ||
"ssb-conn-query": "~0.4.5", | ||
"ssb-conn-staging": "~0.1.0", | ||
"ssb-ref": "^2.13.9", | ||
"ssb-typescript": "^1.5.0", | ||
"statistics": "^3.3.0", | ||
"zii": "~1.1.0" | ||
}, | ||
"peerDependencies": { | ||
"secret-stack": ">=6.2.0" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^13.5.0", | ||
"cont": "^1.0.3", | ||
"secret-stack": "^6.3.1", | ||
"ssb-caps": "~1.1.0", | ||
"ssb-lan": "~0.2.0", | ||
"ssb-server": "~15.2.0", | ||
"tape": "^4.13.0", | ||
"typescript": "3.7.5" | ||
}, | ||
"scripts": { | ||
"typescript": "tsc", | ||
"tape": "set -e; for t in test/*.js; do node $t; done", | ||
"test": "npm run typescript && npm run tape" | ||
}, | ||
"author": "Andre Staltz <contact@staltz.com> (http://staltz.com)", | ||
"license": "MIT" | ||
} | ||
"name": "ssb-conn", | ||
"description": "SSB plugin for establishing and managing peer connections", | ||
"version": "0.15.2-rc0", | ||
"homepage": "https://github.com/staltz/ssb-conn", | ||
"main": "lib/index.js", | ||
"types": "lib/index.d.ts", | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/staltz/ssb-conn.git" | ||
}, | ||
"dependencies": { | ||
"debug": "~4.1.1", | ||
"has-network2": "0.0.1", | ||
"ip": "^1.1.5", | ||
"on-change-network-strict": "1.0.0", | ||
"on-wakeup": "^1.0.1", | ||
"pull-notify": "^0.1.1", | ||
"pull-pause": "~0.0.2", | ||
"pull-ping": "^2.0.3", | ||
"pull-stream": "^3.6.14", | ||
"secret-stack-decorators": "1.0.0", | ||
"ssb-conn-db": "~0.3.0", | ||
"ssb-conn-hub": "~0.2.7", | ||
"ssb-conn-query": "~0.4.5", | ||
"ssb-conn-staging": "~0.1.0", | ||
"ssb-ref": "^2.13.9", | ||
"ssb-typescript": "^1.5.0", | ||
"statistics": "^3.3.0", | ||
"zii": "~1.1.0" | ||
}, | ||
"peerDependencies": { | ||
"secret-stack": ">=6.2.0" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^13.5.0", | ||
"cont": "^1.0.3", | ||
"secret-stack": "^6.3.1", | ||
"ssb-caps": "~1.1.0", | ||
"ssb-lan": "~0.2.0", | ||
"ssb-server": "~15.2.0", | ||
"tape": "^4.13.0", | ||
"typescript": "3.7.5" | ||
}, | ||
"scripts": { | ||
"typescript": "tsc", | ||
"tape": "set -e; for t in test/*.js; do node $t; done", | ||
"test": "npm run typescript && npm run tape" | ||
}, | ||
"author": "Andre Staltz <contact@staltz.com> (http://staltz.com)", | ||
"license": "MIT" | ||
} |
@@ -113,7 +113,11 @@ import ConnQuery = require('ssb-conn-query'); | ||
type Pausable = {pause: CallableFunction; resume: CallableFunction}; | ||
@plugin('1.0.0') | ||
export class ConnScheduler { | ||
private readonly ssb: {conn: CONN; [name: string]: any}; | ||
private readonly config: any; | ||
private readonly config: Record<string, any>; | ||
private readonly hasSsbDb: boolean; | ||
private pubDiscoveryPausable?: Pausable; | ||
private intervalForUpdate?: NodeJS.Timeout; | ||
private closed: boolean; | ||
@@ -365,2 +369,3 @@ private isLoadingHops: boolean; | ||
private updateNow() { | ||
if (this.closed) return; | ||
if (this.hasSsbDb && !this.ssb.ready()) return; | ||
@@ -415,3 +420,3 @@ if (this.isCurrentlyDownloading()) return; | ||
const MAX_STAGED_PUBS = 3; | ||
const pausable = Pausable(); | ||
this.pubDiscoveryPausable = this.pubDiscoveryPausable ?? Pausable(); | ||
@@ -427,3 +432,3 @@ pull( | ||
), | ||
pausable, | ||
this.pubDiscoveryPausable, | ||
pull.drain((msg: Msg<PubContent>['value']) => { | ||
@@ -453,7 +458,9 @@ try { | ||
pull.drain((staged: Array<any>) => { | ||
if (this.closed) return; | ||
const stagedPubs = staged.filter(([, data]) => data.type === 'pub'); | ||
if (stagedPubs.length >= MAX_STAGED_PUBS) { | ||
pausable.pause(); | ||
this.pubDiscoveryPausable?.pause(); | ||
} else { | ||
pausable.resume(); | ||
this.pubDiscoveryPausable?.resume(); | ||
} | ||
@@ -476,2 +483,4 @@ }), | ||
pull.drain(({discovered}: {discovered: Array<BTPeer>}) => { | ||
if (this.closed) return; | ||
for (const btPeer of discovered) { | ||
@@ -553,2 +562,3 @@ const address = | ||
this.setupPubDiscovery(); | ||
this.pubDiscoveryPausable?.resume(); | ||
this.setupLanDiscovery(); | ||
@@ -558,10 +568,16 @@ this.setupBluetoothDiscovery(); | ||
// Upon regular time intervals, attempt to make connections | ||
const int = setInterval(() => this.updateSoon(), 2e3); | ||
if (int.unref) int.unref(); | ||
this.intervalForUpdate = setInterval(() => this.updateSoon(), 2e3); | ||
if (this.intervalForUpdate?.unref) this.intervalForUpdate.unref(); | ||
// Upon wakeup, trigger hard reconnect | ||
onWakeup(() => this.ssb.conn.hub().reset()); | ||
onWakeup(() => { | ||
if (this.closed) return; | ||
this.ssb.conn.hub().reset(); | ||
}); | ||
// Upon network changes, trigger hard reconnect | ||
onNetwork(() => this.ssb.conn.hub().reset()); | ||
onNetwork(() => { | ||
if (this.closed) return; | ||
this.ssb.conn.hub().reset(); | ||
}); | ||
@@ -581,3 +597,8 @@ // Upon some disconnection, attempt to make connections | ||
public stop = () => { | ||
this.pubDiscoveryPausable?.pause(); | ||
this.ssb.lan?.stop?.(); | ||
if (this.intervalForUpdate) { | ||
clearInterval(this.intervalForUpdate); | ||
this.intervalForUpdate = void 0; | ||
} | ||
this.ssb.conn.hub().reset(); | ||
@@ -584,0 +605,0 @@ this.closed = true; |
140905
2523