Skip to content

Commit

Permalink
fix: drop existing peer connections on new topic holepunchto/hyperswa…
Browse files Browse the repository at this point in the history
  • Loading branch information
Elias Rhouzlane authored and Elias Rhouzlane committed May 25, 2020
1 parent 41230eb commit e4d6e19
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 10 deletions.
7 changes: 7 additions & 0 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ import createSwarmHTTP from "./index.js";
const swarmHTTP = createSwarmHTTP();

function cli(command, ...args) {
process.stdout.write("> ")
process.stdin.on("data", data=> {
const input = data.toString();
swarmHTTP.join(input);
process.stdout.write(">")
})

const actions = {
[undefined]: () => swarmHTTP.client(),
server: () => swarmHTTP.server(args[0] || "."),
Expand Down
16 changes: 13 additions & 3 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,20 @@ const peerLink = ({ host, port }) => `http://${host}:${port}`;

export default (swarm) => () => {
const peers = new DictEmitter()
.on("add", (peer) => log("connection", peerLink(peer), peerLink(peer.interactive)))
.on("delete", (peer) => log("disconnection", peerLink(peer)));
.on("add", (peer) =>
log("connection", peer&& peerLink(peer), peer && peerLink(peer.interactive))
)
.on("delete", (peer) => log("disconnection", peer && peerLink(peer)));

swarm.on("connection", (_, { peer }) => {
swarm.on("connection", (_, details) => {
swarm.on("data", (data) => {
console.log(data.toString());
if (data.toString() === "close") {
socket.destroy();
details.destroy();
}
});
const { peer } = details;
if (peer) {
createInteractiveWebdav({
inject: `<script>window.webdav="${peerLink(peer)}"</script>`,
Expand Down
9 changes: 6 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ import crypto from "crypto";

import limited from "./utils/create-swarm.js";

export default () => {
const hash = (data) => crypto.createHash("sha256").update(data).digest();

export default (defaultTopic = crypto.randomBytes(20).toString("hex")) => {
const swarm = limited(hyperswarm());
const topic = crypto.createHash("sha256").update("the-commoners").digest();
swarm.join(topic, { lookup: true, announce: true });
swarm.join(hash(defaultTopic), { lookup: true, announce: true });

return {
join: (topic) => swarm.join(hash(topic), { lookup: true, announce: true }),
leave: (topic) => swarm.leave(hash(topic)),
client: (...args) =>
import("./client.js").then(({ default: f }) => f(swarm)(...args)),
server: (...args) =>
Expand Down
31 changes: 27 additions & 4 deletions src/utils/create-swarm.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,41 @@
const getId = (socket) => `${socket.localAddress}:${socket.localPort}`;

export default (swarm) => {
const callbacks = { connection: [], disconnection: [] };
const connections = {};
swarm.on("connection", (socket, details) => {
connections[socket.id] = { socket, details };
connections[getId(socket)] = { socket, details };
callbacks.connection.forEach((callback) => callback(socket, details));
});
swarm.on("disconnection", (socket, details) => {
delete connections[socket.id];
delete connections[getId(socket)];
callbacks.disconnection.forEach((callback) => callback(socket, details));
});

swarm.on("disconnection", (socket, details) => {
socket.destroy();
details.destroy();
});
let topic = null;
const gconnections = () => Object.freeze({ ...connections });
const leave = (oldTopic, fn) => {
Object.values(gconnections()).forEach(({ socket, details }) => {
socket.destroy();
details.destroy();
});
topic = null;
swarm.leave(oldTopic, fn);
};
const join = (newTopic, options) => {
if (topic) leave(topic);
topic = newTopic;
swarm.join(topic, options);
};

return {
join: (...args) => swarm.join(...args),
connections: Object.freeze({ ...connections }),
leave,
join,
connections: () => Object.freeze({ ...connections }),
on: (event, callback) => {
callbacks[event] = [...(callbacks[event] || []), callback];
if (event === "connection") {
Expand Down

0 comments on commit e4d6e19

Please sign in to comment.