@yz-social/kdht 0.1.1 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/portals/node.js +6 -0
- package/spec/bots.js +6 -0
- package/spec/node.html +18 -13
- package/spec/portal.js +1 -1
- package/transports/webrtc.js +1 -0
package/package.json
CHANGED
package/portals/node.js
CHANGED
|
@@ -28,4 +28,10 @@ export async function setup({baseURL, externalBaseURL = '', verbose, fixedSpacin
|
|
|
28
28
|
const bootstrap = joinURL && await contact.ensureRemoteContact(bootstrapName, joinURL);
|
|
29
29
|
process.send(contact.sname); // Report in to server as available for others to bootstrap through.
|
|
30
30
|
if (bootstrap) await contact.join(bootstrap);
|
|
31
|
+
process.on('SIGINT', async () => {
|
|
32
|
+
console.log(process.title, 'Shutdown for Ctrl+C');
|
|
33
|
+
await contact.disconnect();
|
|
34
|
+
process.exit(0);
|
|
35
|
+
});
|
|
36
|
+
return contact;
|
|
31
37
|
}
|
package/spec/bots.js
CHANGED
|
@@ -62,6 +62,12 @@ let bootstrapName = await contact.fetchBootstrap(argv.baseURL);
|
|
|
62
62
|
let bootstrapContact = await contact.ensureRemoteContact(bootstrapName, argv.baseURL);
|
|
63
63
|
await contact.join(bootstrapContact);
|
|
64
64
|
|
|
65
|
+
process.on('SIGINT', async () => {
|
|
66
|
+
console.log(process.title, 'Shutdown for Ctrl+C');
|
|
67
|
+
await contact.disconnect();
|
|
68
|
+
process.exit(0);
|
|
69
|
+
});
|
|
70
|
+
|
|
65
71
|
while (argv.thrash) {
|
|
66
72
|
await Node.delay(contact.host.fuzzyInterval(Node.refreshTimeIntervalMS));
|
|
67
73
|
const old = contact;
|
package/spec/node.html
CHANGED
|
@@ -19,29 +19,34 @@
|
|
|
19
19
|
import { v4 as uuidv4 } from 'uuid';
|
|
20
20
|
import { WebContact } from '@yz-social/kdht';
|
|
21
21
|
Object.assign(globalThis, {uuidv4, WebContact });
|
|
22
|
+
const bootstrapBase = new URL('/kdht', origin).href; // Must be a string
|
|
23
|
+
let contact;
|
|
22
24
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
25
|
+
async function connect() { // Connect or reconnect to the network.
|
|
26
|
+
const name = uuidv4();
|
|
27
|
+
contact = globalThis.contact = await WebContact.create({name});
|
|
28
|
+
const bootstrapName = await contact.fetchBootstrap(bootstrapBase);
|
|
29
|
+
console.log('connecting to', bootstrapName);
|
|
30
|
+
const bootstrapContact = globalThis.bootstrapContact = await contact.ensureRemoteContact(bootstrapName, bootstrapBase);
|
|
31
|
+
await contact.join(bootstrapContact);
|
|
32
|
+
update.onclick();
|
|
27
33
|
}
|
|
28
|
-
const contact = globalThis.contact = await WebContact.create({name});
|
|
29
|
-
const bootstrapBase = new URL('/kdht', origin).href; // Must be a string
|
|
30
|
-
const bootstrapName = await contact.fetchBootstrap(bootstrapBase);
|
|
31
|
-
console.log('connecting to', bootstrapName);
|
|
32
|
-
const bootstrapContact = globalThis.bootstrapContact = await contact.ensureRemoteContact(bootstrapName, bootstrapBase);
|
|
33
|
-
await contact.join(bootstrapContact);
|
|
34
|
-
console.log('connected');
|
|
35
34
|
|
|
36
35
|
update.onclick = () => {
|
|
37
36
|
const report = contact.node.report(null);
|
|
38
|
-
console.log({report, display});
|
|
39
37
|
display.textContent = report;
|
|
40
38
|
};
|
|
41
|
-
|
|
39
|
+
await connect();
|
|
42
40
|
|
|
43
41
|
write.onclick = () => contact.storeValue(key.value, writeValue.value);
|
|
44
42
|
read.onclick = () => contact.node.locateValue(key.value).then(value => readValue.value = value);
|
|
43
|
+
document.addEventListener("visibilitychange", () => {
|
|
44
|
+
if (document.hidden) {
|
|
45
|
+
contact.disconnect();
|
|
46
|
+
} else {
|
|
47
|
+
connect();
|
|
48
|
+
}
|
|
49
|
+
});
|
|
45
50
|
</script>
|
|
46
51
|
</body>
|
|
47
52
|
</html>
|
package/spec/portal.js
CHANGED
|
@@ -115,5 +115,5 @@ if (cluster.isPrimary) { // Parent process with portal webserver through which c
|
|
|
115
115
|
} else { // A portal node through which client's can connect.
|
|
116
116
|
const portalNode = await import('../portals/node.js');
|
|
117
117
|
const {baseURL, externalBaseURL, fixedSpacing, variableSpacing, verbose} = argv;
|
|
118
|
-
portalNode.setup({baseURL, externalBaseURL, fixedSpacing, variableSpacing, verbose});
|
|
118
|
+
await portalNode.setup({baseURL, externalBaseURL, fixedSpacing, variableSpacing, verbose});
|
|
119
119
|
}
|
package/transports/webrtc.js
CHANGED
|
@@ -209,6 +209,7 @@ export class WebContact extends Contact { // Our wrapper for the means of contac
|
|
|
209
209
|
// If we find that in our inFlight tags, then the message is a response.
|
|
210
210
|
if (dataString === '"bye"') { // Special messsage that the other side is disconnecting, so we can clean up early.
|
|
211
211
|
this.webrtc.close();
|
|
212
|
+
this.host.xlog('removing disconnected contact', this.sname);
|
|
212
213
|
await this.host.removeContact(this); // TODO: Make sure we're not invoking this in maxTransports cases.
|
|
213
214
|
return;
|
|
214
215
|
}
|