-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpeer.js
50 lines (43 loc) · 1.09 KB
/
peer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const assert = require('assert');
const debug = require('debug')('swarm:peer');
class Peer {
constructor ({ urls = [], session }) {
this.address = '';
this.publicKey = '';
this.urls = urls;
this.apps = [];
this.session = session;
// Object.defineProperty(this, 'session', {
// enumerable: false,
// writable: true,
// configurable: true,
// value: session,
// });
}
get connected () {
return Boolean(this.session && this.session.handshaked);
}
get initiate () {
return Boolean(this.session && this.session.initiate);
}
async handshake () {
let advertisement = await this.session.handshake();
this.address = advertisement.address;
this.publicKey = advertisement.publicKey;
this.urls = advertisement.urls;
this.apps = advertisement.apps;
}
send ({ app, command, payload }) {
return this.session.write({ app, command, payload });
}
dump () {
let { address, publicKey, urls, apps } = this;
return {
address,
publicKey,
urls,
apps,
};
}
}
module.exports = { Peer };