Compare commits

...

4 Commits

Author SHA1 Message Date
Derrick Hammer 01a637a767
*Update dist 2022-11-15 14:28:57 -05:00
Derrick Hammer aab5254ef2
*Add prettier 2022-11-15 14:28:28 -05:00
Derrick Hammer 8d57c62994
*Add peer-open and peer-remove events 2022-11-15 14:27:43 -05:00
Derrick Hammer 9c7dc7dad8
*switch to protobuf-ts 2022-11-15 14:22:11 -05:00
9 changed files with 365 additions and 404 deletions

2
dist/index.d.ts.map vendored
View File

@ -1 +1 @@
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;AAAA,OAAO,YAAY,MAAM,QAAQ,CAAC;AAoBlC,MAAM,CAAC,OAAO,OAAO,QAAS,SAAQ,YAAY;IAC9C,OAAO,CAAC,EAAE,CAAS;IACnB,OAAO,CAAC,GAAG,CAAS;IACpB,OAAO,CAAC,aAAa,CAAS;IAC9B,OAAO,CAAC,GAAG,CAAM;IACjB,OAAO,CAAC,KAAK,CAAM;gBAEP,EACI,OAAkB,EAClB,GAAS,EACT,aAAiB,EACjB,EAA2B,EAC3B,KAAY,EACf;;;;;;KAAK;IAkBlB,OAAO,CAAC,aAAa;IAwBrB,OAAO,CAAC,SAAS;IAmCjB,SAAS,CAAC,IAAI,EAAE,GAAG,EAAE,GAAG,SAAW;CActC"} {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;AAAA,OAAO,YAAY,MAAM,QAAQ,CAAC;AAoBlC,MAAM,CAAC,OAAO,OAAO,QAAS,SAAQ,YAAY;IAC9C,OAAO,CAAC,EAAE,CAAS;IACnB,OAAO,CAAC,GAAG,CAAS;IACpB,OAAO,CAAC,aAAa,CAAS;IAC9B,OAAO,CAAC,GAAG,CAAM;IACjB,OAAO,CAAC,KAAK,CAAM;gBAEP,EACI,OAAkB,EAClB,GAAS,EACT,aAAiB,EACjB,EAA2B,EAC3B,KAAY,EACf;;;;;;KAAK;IAkBlB,OAAO,CAAC,aAAa;IAwBrB,OAAO,CAAC,SAAS;IA2CjB,SAAS,CAAC,IAAI,EAAE,GAAG,EAAE,GAAG,SAAW;CActC"}

35
dist/index.js vendored
View File

@ -14,7 +14,7 @@ const messages_js_1 = require("./messages.js");
// @ts-ignore // @ts-ignore
const compact_encoding_1 = __importDefault(require("compact-encoding")); const compact_encoding_1 = __importDefault(require("compact-encoding"));
const b4a_1 = __importDefault(require("b4a")); const b4a_1 = __importDefault(require("b4a"));
const debug = (0, debug_1.default)('dht-flood'); const debug = (0, debug_1.default)("dht-flood");
const LRU_SIZE = 255; const LRU_SIZE = 255;
const TTL = 255; const TTL = 255;
const PROTOCOL = "lumeweb.flood"; const PROTOCOL = "lumeweb.flood";
@ -25,14 +25,14 @@ class DHTFlood extends events_1.default {
messageNumber; messageNumber;
lru; lru;
swarm; swarm;
constructor({ lruSize = LRU_SIZE, ttl = TTL, messageNumber = 0, id = crypto_1.default.randomBytes(32), swarm = null } = {}) { constructor({ lruSize = LRU_SIZE, ttl = TTL, messageNumber = 0, id = crypto_1.default.randomBytes(32), swarm = null, } = {}) {
super(); super();
this.id = id; this.id = id;
this.ttl = ttl; this.ttl = ttl;
this.messageNumber = messageNumber; this.messageNumber = messageNumber;
this.lru = new lru_1.default(lruSize); this.lru = new lru_1.default(lruSize);
if (!swarm) { if (!swarm) {
throw new Error('swarm is required'); throw new Error("swarm is required");
} }
this.swarm = swarm; this.swarm = swarm;
this.swarm.on("connection", (peer) => { this.swarm.on("connection", (peer) => {
@ -44,41 +44,48 @@ class DHTFlood extends events_1.default {
const originIdBuf = b4a_1.default.from(originId); const originIdBuf = b4a_1.default.from(originId);
// Ignore messages from ourselves // Ignore messages from ourselves
if (originIdBuf.equals(this.id)) if (originIdBuf.equals(this.id))
return debug('Got message from self', originId, messageNumber); return debug("Got message from self", originId, messageNumber);
// Ignore messages we've already seen // Ignore messages we've already seen
const key = originIdBuf.toString('hex') + messageNumber; const key = originIdBuf.toString("hex") + messageNumber;
if (this.lru.get(key)) if (this.lru.get(key))
return debug('Got message that was already seen', originId, messageNumber); return debug("Got message that was already seen", originId, messageNumber);
this.lru.set(key, true); this.lru.set(key, true);
this.emit('message', data, originId, messageNumber); this.emit("message", data, originId, messageNumber);
if (ttl <= 0) if (ttl <= 0)
return debug('Got message at end of TTL', originId, messageNumber, ttl); return debug("Got message at end of TTL", originId, messageNumber, ttl);
messenger.send({ messenger.send({
originId, originId,
messageNumber, messageNumber,
data, data,
ttl: ttl - 1 ttl: ttl - 1,
}); });
} }
setupPeer(peer) { setupPeer(peer) {
const mux = protomux_1.default.from(peer); const mux = protomux_1.default.from(peer);
let chan; let chan;
const self = this;
if (!mux.opened({ protocol: PROTOCOL })) { if (!mux.opened({ protocol: PROTOCOL })) {
chan = mux.createChannel({ chan = mux.createChannel({
protocol: PROTOCOL, protocol: PROTOCOL,
async onopen() {
self.emit("peer-open", peer);
},
async ondestroy() {
self.emit("peer-remove", peer);
},
}); });
peer[FLOOD_SYMBOL] = chan; peer[FLOOD_SYMBOL] = chan;
} }
chan = peer[FLOOD_SYMBOL]; chan = peer[FLOOD_SYMBOL];
if (!chan) { if (!chan) {
throw new Error('could not find channel'); throw new Error("could not find channel");
} }
if (!chan.messages.length) { if (!chan.messages.length) {
chan.addMessage({ chan.addMessage({
encoding: { encoding: {
preencode: (state, m) => compact_encoding_1.default.raw.preencode(state, messages_js_1.Packet.encode(m).finish()), preencode: (state, m) => compact_encoding_1.default.raw.preencode(state, messages_js_1.Packet.toBinary(messages_js_1.Packet.create(m))),
encode: (state, m) => compact_encoding_1.default.raw.encode(state, messages_js_1.Packet.encode(m).finish()), encode: (state, m) => compact_encoding_1.default.raw.encode(state, messages_js_1.Packet.toBinary(messages_js_1.Packet.create(m))),
decode: (state) => messages_js_1.Packet.decode(compact_encoding_1.default.raw.decode(state)), decode: (state) => messages_js_1.Packet.fromBinary(compact_encoding_1.default.raw.decode(state)),
}, },
onmessage: (msg) => this.handleMessage(msg, chan.messages[0]), onmessage: (msg) => this.handleMessage(msg, chan.messages[0]),
}); });
@ -97,7 +104,7 @@ class DHTFlood extends events_1.default {
originId: id, originId: id,
messageNumber, messageNumber,
ttl, ttl,
data data,
}); });
} }
} }

63
dist/messages.d.ts vendored
View File

@ -1,38 +1,39 @@
import * as _m0 from "protobufjs/minimal"; import type { BinaryWriteOptions } from "@protobuf-ts/runtime";
export declare const protobufPackage = ""; import type { IBinaryWriter } from "@protobuf-ts/runtime";
/** type=0 */ import type { BinaryReadOptions } from "@protobuf-ts/runtime";
export interface Packet { import type { IBinaryReader } from "@protobuf-ts/runtime";
import type { PartialMessage } from "@protobuf-ts/runtime";
import { MessageType } from "@protobuf-ts/runtime";
/**
* @generated from protobuf message Packet
*/
export interface PacketType {
/**
* @generated from protobuf field: bytes originId = 1;
*/
originId: Uint8Array; originId: Uint8Array;
/**
* @generated from protobuf field: uint32 messageNumber = 2;
*/
messageNumber: number; messageNumber: number;
/**
* @generated from protobuf field: uint32 ttl = 3;
*/
ttl: number; ttl: number;
/**
* @generated from protobuf field: bytes data = 4;
*/
data: Uint8Array; data: Uint8Array;
} }
export declare const Packet: { declare class Packet$Type extends MessageType<PacketType> {
encode(message: Packet, writer?: _m0.Writer): _m0.Writer; constructor();
decode(input: _m0.Reader | Uint8Array, length?: number): Packet; create(value?: PartialMessage<PacketType>): PacketType;
fromJSON(object: any): Packet; internalBinaryRead(reader: IBinaryReader, length: number, options: BinaryReadOptions, target?: PacketType): PacketType;
toJSON(message: Packet): unknown; internalBinaryWrite(message: PacketType, writer: IBinaryWriter, options: BinaryWriteOptions): IBinaryWriter;
fromPartial<I extends { }
originId?: Uint8Array | undefined; /**
messageNumber?: number | undefined; * @generated MessageType for protobuf message Packet
ttl?: number | undefined; */
data?: Uint8Array | undefined; export declare const Packet: Packet$Type;
} & {
originId?: Uint8Array | undefined;
messageNumber?: number | undefined;
ttl?: number | undefined;
data?: Uint8Array | undefined;
} & { [K in Exclude<keyof I, keyof Packet>]: never; }>(object: I): Packet;
};
declare type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined;
export declare type DeepPartial<T> = T extends Builtin ? T : T extends Array<infer U> ? Array<DeepPartial<U>> : T extends ReadonlyArray<infer U> ? ReadonlyArray<DeepPartial<U>> : T extends {} ? {
[K in keyof T]?: DeepPartial<T[K]>;
} : Partial<T>;
declare type KeysOfUnion<T> = T extends T ? keyof T : never;
export declare type Exact<P, I extends P> = P extends Builtin ? P : P & {
[K in keyof P]: Exact<P[K], I[K]>;
} & {
[K in Exclude<keyof I, KeysOfUnion<P>>]: never;
};
export {}; export {};
//# sourceMappingURL=messages.d.ts.map //# sourceMappingURL=messages.d.ts.map

View File

@ -1 +1 @@
{"version":3,"file":"messages.d.ts","sourceRoot":"","sources":["../src/messages.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,GAAG,MAAM,oBAAoB,CAAC;AAE1C,eAAO,MAAM,eAAe,KAAK,CAAC;AAElC,aAAa;AACb,MAAM,WAAW,MAAM;IACrB,QAAQ,EAAE,UAAU,CAAC;IACrB,aAAa,EAAE,MAAM,CAAC;IACtB,GAAG,EAAE,MAAM,CAAC;IACZ,IAAI,EAAE,UAAU,CAAC;CAClB;AAMD,eAAO,MAAM,MAAM;oBACD,MAAM,WAAU,IAAI,MAAM,GAAyB,IAAI,MAAM;kBAgB/D,IAAI,MAAM,GAAG,UAAU,WAAW,MAAM,GAAG,MAAM;qBA2B9C,GAAG,GAAG,MAAM;oBASb,MAAM,GAAG,OAAO;;;;;;;;;;;uEAWiC,MAAM;CAQxE,CAAC;AA8CF,aAAK,OAAO,GAAG,IAAI,GAAG,QAAQ,GAAG,UAAU,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,SAAS,CAAC;AAEpF,oBAAY,WAAW,CAAC,CAAC,IAAI,CAAC,SAAS,OAAO,GAAG,CAAC,GAC9C,CAAC,SAAS,KAAK,CAAC,MAAM,CAAC,CAAC,GAAG,KAAK,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,SAAS,aAAa,CAAC,MAAM,CAAC,CAAC,GAAG,aAAa,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,GACnH,CAAC,SAAS,EAAE,GAAG;KAAG,CAAC,IAAI,MAAM,CAAC,CAAC,CAAC,EAAE,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;CAAE,GACrD,OAAO,CAAC,CAAC,CAAC,CAAC;AAEf,aAAK,WAAW,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,GAAG,MAAM,CAAC,GAAG,KAAK,CAAC;AACpD,oBAAY,KAAK,CAAC,CAAC,EAAE,CAAC,SAAS,CAAC,IAAI,CAAC,SAAS,OAAO,GAAG,CAAC,GACrD,CAAC,GAAG;KAAG,CAAC,IAAI,MAAM,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;CAAE,GAAG;KAAG,CAAC,IAAI,OAAO,CAAC,MAAM,CAAC,EAAE,WAAW,CAAC,CAAC,CAAC,CAAC,GAAG,KAAK;CAAE,CAAC"} {"version":3,"file":"messages.d.ts","sourceRoot":"","sources":["../src/messages.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAC/D,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAE1D,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AAC9D,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAE1D,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAG3D,OAAO,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AACnD;;GAEG;AACH,MAAM,WAAW,UAAU;IACzB;;OAEG;IACH,QAAQ,EAAE,UAAU,CAAC;IACrB;;OAEG;IACH,aAAa,EAAE,MAAM,CAAC;IACtB;;OAEG;IACH,GAAG,EAAE,MAAM,CAAC;IACZ;;OAEG;IACH,IAAI,EAAE,UAAU,CAAC;CAClB;AAED,cAAM,WAAY,SAAQ,WAAW,CAAC,UAAU,CAAC;;IAc/C,MAAM,CAAC,KAAK,CAAC,EAAE,cAAc,CAAC,UAAU,CAAC,GAAG,UAAU;IAetD,kBAAkB,CAChB,MAAM,EAAE,aAAa,EACrB,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,iBAAiB,EAC1B,MAAM,CAAC,EAAE,UAAU,GAClB,UAAU;IAqCb,mBAAmB,CACjB,OAAO,EAAE,UAAU,EACnB,MAAM,EAAE,aAAa,EACrB,OAAO,EAAE,kBAAkB,GAC1B,aAAa;CAqBjB;AACD;;GAEG;AACH,eAAO,MAAM,MAAM,aAAoB,CAAC"}

195
dist/messages.js vendored
View File

@ -1,144 +1,89 @@
"use strict"; "use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
exports.Packet = exports.protobufPackage = void 0; exports.Packet = void 0;
/* eslint-disable */ const runtime_1 = require("@protobuf-ts/runtime");
const _m0 = __importStar(require("protobufjs/minimal")); const runtime_2 = require("@protobuf-ts/runtime");
exports.protobufPackage = ""; const runtime_3 = require("@protobuf-ts/runtime");
function createBasePacket() { const runtime_4 = require("@protobuf-ts/runtime");
return { originId: new Uint8Array(), messageNumber: 0, ttl: 0, data: new Uint8Array() }; const runtime_5 = require("@protobuf-ts/runtime");
} // @generated message type with reflection information, may provide speed optimized methods
exports.Packet = { class Packet$Type extends runtime_5.MessageType {
encode(message, writer = _m0.Writer.create()) { constructor() {
if (message.originId.length !== 0) { super("Packet", [
writer.uint32(10).bytes(message.originId); { no: 1, name: "originId", kind: "scalar", T: 12 /*ScalarType.BYTES*/ },
} {
if (message.messageNumber !== 0) { no: 2,
writer.uint32(16).uint32(message.messageNumber); name: "messageNumber",
} kind: "scalar",
if (message.ttl !== 0) { T: 13 /*ScalarType.UINT32*/,
writer.uint32(24).uint32(message.ttl);
}
if (message.data.length !== 0) {
writer.uint32(34).bytes(message.data);
}
return writer;
}, },
decode(input, length) { { no: 3, name: "ttl", kind: "scalar", T: 13 /*ScalarType.UINT32*/ },
const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input); { no: 4, name: "data", kind: "scalar", T: 12 /*ScalarType.BYTES*/ },
let end = length === undefined ? reader.len : reader.pos + length; ]);
const message = createBasePacket(); }
create(value) {
const message = {
originId: new Uint8Array(0),
messageNumber: 0,
ttl: 0,
data: new Uint8Array(0),
};
globalThis.Object.defineProperty(message, runtime_4.MESSAGE_TYPE, {
enumerable: false,
value: this,
});
if (value !== undefined)
(0, runtime_3.reflectionMergePartial)(this, message, value);
return message;
}
internalBinaryRead(reader, length, options, target) {
let message = target ?? this.create(), end = reader.pos + length;
while (reader.pos < end) { while (reader.pos < end) {
const tag = reader.uint32(); let [fieldNo, wireType] = reader.tag();
switch (tag >>> 3) { switch (fieldNo) {
case 1: case /* bytes originId */ 1:
message.originId = reader.bytes(); message.originId = reader.bytes();
break; break;
case 2: case /* uint32 messageNumber */ 2:
message.messageNumber = reader.uint32(); message.messageNumber = reader.uint32();
break; break;
case 3: case /* uint32 ttl */ 3:
message.ttl = reader.uint32(); message.ttl = reader.uint32();
break; break;
case 4: case /* bytes data */ 4:
message.data = reader.bytes(); message.data = reader.bytes();
break; break;
default: default:
reader.skipType(tag & 7); let u = options.readUnknownField;
break; if (u === "throw")
throw new globalThis.Error(`Unknown field ${fieldNo} (wire type ${wireType}) for ${this.typeName}`);
let d = reader.skip(wireType);
if (u !== false)
(u === true ? runtime_2.UnknownFieldHandler.onRead : u)(this.typeName, message, fieldNo, wireType, d);
} }
} }
return message; return message;
},
fromJSON(object) {
return {
originId: isSet(object.originId) ? bytesFromBase64(object.originId) : new Uint8Array(),
messageNumber: isSet(object.messageNumber) ? Number(object.messageNumber) : 0,
ttl: isSet(object.ttl) ? Number(object.ttl) : 0,
data: isSet(object.data) ? bytesFromBase64(object.data) : new Uint8Array(),
};
},
toJSON(message) {
const obj = {};
message.originId !== undefined &&
(obj.originId = base64FromBytes(message.originId !== undefined ? message.originId : new Uint8Array()));
message.messageNumber !== undefined && (obj.messageNumber = Math.round(message.messageNumber));
message.ttl !== undefined && (obj.ttl = Math.round(message.ttl));
message.data !== undefined &&
(obj.data = base64FromBytes(message.data !== undefined ? message.data : new Uint8Array()));
return obj;
},
fromPartial(object) {
const message = createBasePacket();
message.originId = object.originId ?? new Uint8Array();
message.messageNumber = object.messageNumber ?? 0;
message.ttl = object.ttl ?? 0;
message.data = object.data ?? new Uint8Array();
return message;
},
};
var globalThis = (() => {
if (typeof globalThis !== "undefined") {
return globalThis;
} }
if (typeof self !== "undefined") { internalBinaryWrite(message, writer, options) {
return self; /* bytes originId = 1; */
} if (message.originId.length)
if (typeof window !== "undefined") { writer.tag(1, runtime_1.WireType.LengthDelimited).bytes(message.originId);
return window; /* uint32 messageNumber = 2; */
} if (message.messageNumber !== 0)
if (typeof global !== "undefined") { writer.tag(2, runtime_1.WireType.Varint).uint32(message.messageNumber);
return global; /* uint32 ttl = 3; */
} if (message.ttl !== 0)
throw "Unable to locate global object"; writer.tag(3, runtime_1.WireType.Varint).uint32(message.ttl);
})(); /* bytes data = 4; */
function bytesFromBase64(b64) { if (message.data.length)
if (globalThis.Buffer) { writer.tag(4, runtime_1.WireType.LengthDelimited).bytes(message.data);
return Uint8Array.from(globalThis.Buffer.from(b64, "base64")); let u = options.writeUnknownFields;
} if (u !== false)
else { (u == true ? runtime_2.UnknownFieldHandler.onWrite : u)(this.typeName, message, writer);
const bin = globalThis.atob(b64); return writer;
const arr = new Uint8Array(bin.length);
for (let i = 0; i < bin.length; ++i) {
arr[i] = bin.charCodeAt(i);
}
return arr;
} }
} }
function base64FromBytes(arr) { /**
if (globalThis.Buffer) { * @generated MessageType for protobuf message Packet
return globalThis.Buffer.from(arr).toString("base64"); */
} exports.Packet = new Packet$Type();
else {
const bin = [];
arr.forEach((byte) => {
bin.push(String.fromCharCode(byte));
});
return globalThis.btoa(bin.join(""));
}
}
function isSet(value) {
return value !== null && value !== undefined;
}

View File

@ -1,4 +1,5 @@
// type=0 syntax = "proto2";
message Packet { message Packet {
required bytes originId = 1; required bytes originId = 1;
required uint32 messageNumber = 2; required uint32 messageNumber = 2;

View File

@ -10,14 +10,15 @@
"protomux-rpc": "^1.3.0" "protomux-rpc": "^1.3.0"
}, },
"devDependencies": { "devDependencies": {
"@protobuf-ts/plugin": "^2.8.1",
"@types/b4a": "^1.6.0", "@types/b4a": "^1.6.0",
"@types/debug": "^4.1.7", "@types/debug": "^4.1.7",
"b4a": "^1.6.1", "b4a": "^1.6.1",
"debug": "^4.3.4", "debug": "^4.3.4",
"hyperswarm": "^4.3.5", "hyperswarm": "^4.3.5",
"prettier": "^2.7.1",
"protoc": "^1.1.3", "protoc": "^1.1.3",
"sodium-universal": "^3.1.0", "sodium-universal": "^3.1.0",
"tape": "^5.6.1", "tape": "^5.6.1"
"ts-proto": "^1.131.2"
} }
} }

View File

@ -5,18 +5,18 @@ import LRU from "lru";
import debug0 from "debug"; import debug0 from "debug";
// @ts-ignore // @ts-ignore
import Protomux from "protomux"; import Protomux from "protomux";
import {Packet} from "./messages.js"; import { Packet, PacketType } from "./messages.js";
// @ts-ignore // @ts-ignore
import c from "compact-encoding" import c from "compact-encoding";
import b4a from "b4a" import b4a from "b4a";
const debug = debug0('dht-flood') const debug = debug0("dht-flood");
const LRU_SIZE = 255 const LRU_SIZE = 255;
const TTL = 255 const TTL = 255;
const PROTOCOL = "lumeweb.flood" const PROTOCOL = "lumeweb.flood";
const FLOOD_SYMBOL = Symbol.for(PROTOCOL) const FLOOD_SYMBOL = Symbol.for(PROTOCOL);
export default class DHTFlood extends EventEmitter { export default class DHTFlood extends EventEmitter {
private id: Buffer; private id: Buffer;
@ -30,56 +30,73 @@ export default class DHTFlood extends EventEmitter {
ttl = TTL, ttl = TTL,
messageNumber = 0, messageNumber = 0,
id = crypto.randomBytes(32), id = crypto.randomBytes(32),
swarm = null swarm = null,
} = {}) { } = {}) {
super() super();
this.id = id this.id = id;
this.ttl = ttl this.ttl = ttl;
this.messageNumber = messageNumber this.messageNumber = messageNumber;
this.lru = new LRU(lruSize) this.lru = new LRU(lruSize);
if (!swarm) { if (!swarm) {
throw new Error('swarm is required'); throw new Error("swarm is required");
} }
this.swarm = swarm; this.swarm = swarm;
this.swarm.on("connection", (peer: any) => { this.swarm.on("connection", (peer: any) => {
const mux = Protomux.from(peer); const mux = Protomux.from(peer);
mux.pair({protocol: PROTOCOL}, () => this.setupPeer(peer)); mux.pair({ protocol: PROTOCOL }, () => this.setupPeer(peer));
}); });
} }
private handleMessage({originId, messageNumber, ttl, data}: Packet, messenger: any) { private handleMessage(
{ originId, messageNumber, ttl, data }: PacketType,
messenger: any
) {
const originIdBuf = b4a.from(originId) as Buffer; const originIdBuf = b4a.from(originId) as Buffer;
// Ignore messages from ourselves // Ignore messages from ourselves
if (originIdBuf.equals(this.id)) return debug('Got message from self', originId, messageNumber) if (originIdBuf.equals(this.id))
return debug("Got message from self", originId, messageNumber);
// Ignore messages we've already seen // Ignore messages we've already seen
const key = originIdBuf.toString('hex') + messageNumber const key = originIdBuf.toString("hex") + messageNumber;
if (this.lru.get(key)) return debug('Got message that was already seen', originId, messageNumber) if (this.lru.get(key))
this.lru.set(key, true) return debug(
"Got message that was already seen",
originId,
messageNumber
);
this.lru.set(key, true);
this.emit('message', data, originId, messageNumber) this.emit("message", data, originId, messageNumber);
if (ttl <= 0) return debug('Got message at end of TTL', originId, messageNumber, ttl) if (ttl <= 0)
return debug("Got message at end of TTL", originId, messageNumber, ttl);
messenger.send({ messenger.send({
originId, originId,
messageNumber, messageNumber,
data, data,
ttl: ttl - 1 ttl: ttl - 1,
}); });
} }
private setupPeer(peer: any) { private setupPeer(peer: any) {
const mux = Protomux.from(peer); const mux = Protomux.from(peer);
let chan: any; let chan: any;
if (!mux.opened({protocol: PROTOCOL})) { const self = this;
if (!mux.opened({ protocol: PROTOCOL })) {
chan = mux.createChannel({ chan = mux.createChannel({
protocol: PROTOCOL, protocol: PROTOCOL,
async onopen() {
self.emit("peer-open", peer);
},
async ondestroy() {
self.emit("peer-remove", peer);
},
}); });
peer[FLOOD_SYMBOL] = chan; peer[FLOOD_SYMBOL] = chan;
} }
@ -87,18 +104,20 @@ export default class DHTFlood extends EventEmitter {
chan = peer[FLOOD_SYMBOL]; chan = peer[FLOOD_SYMBOL];
if (!chan) { if (!chan) {
throw new Error('could not find channel'); throw new Error("could not find channel");
} }
if (!chan.messages.length) { if (!chan.messages.length) {
chan.addMessage({ chan.addMessage({
encoding: { encoding: {
preencode: (state: any, m: any) => c.raw.preencode(state, Packet.encode(m).finish()), preencode: (state: any, m: any) =>
encode: (state: any, m: any) => c.raw.encode(state, Packet.encode(m).finish()), c.raw.preencode(state, Packet.toBinary(Packet.create(m))),
decode: (state: any) => Packet.decode(c.raw.decode(state)), encode: (state: any, m: any) =>
c.raw.encode(state, Packet.toBinary(Packet.create(m))),
decode: (state: any) => Packet.fromBinary(c.raw.decode(state)),
}, },
onmessage: (msg: any) => this.handleMessage(msg, chan.messages[0]), onmessage: (msg: any) => this.handleMessage(msg, chan.messages[0]),
}) });
} }
if (!chan.opened) { if (!chan.opened) {
@ -109,8 +128,8 @@ export default class DHTFlood extends EventEmitter {
} }
broadcast(data: any, ttl = this.ttl) { broadcast(data: any, ttl = this.ttl) {
this.messageNumber++ this.messageNumber++;
const {id, messageNumber} = this const { id, messageNumber } = this;
for (const peer of this.swarm.connections.values()) { for (const peer of this.swarm.connections.values()) {
const message = this.setupPeer(peer); const message = this.setupPeer(peer);
@ -118,8 +137,8 @@ export default class DHTFlood extends EventEmitter {
originId: id, originId: id,
messageNumber, messageNumber,
ttl, ttl,
data data,
}) });
} }
} }
} }

View File

@ -1,149 +1,136 @@
/* eslint-disable */ // @generated by protobuf-ts 2.8.1
import * as _m0 from "protobufjs/minimal"; // @generated from protobuf file "messages.proto" (syntax proto2)
// tslint:disable
export const protobufPackage = ""; import type { BinaryWriteOptions } from "@protobuf-ts/runtime";
import type { IBinaryWriter } from "@protobuf-ts/runtime";
/** type=0 */ import { WireType } from "@protobuf-ts/runtime";
export interface Packet { import type { BinaryReadOptions } from "@protobuf-ts/runtime";
import type { IBinaryReader } from "@protobuf-ts/runtime";
import { UnknownFieldHandler } from "@protobuf-ts/runtime";
import type { PartialMessage } from "@protobuf-ts/runtime";
import { reflectionMergePartial } from "@protobuf-ts/runtime";
import { MESSAGE_TYPE } from "@protobuf-ts/runtime";
import { MessageType } from "@protobuf-ts/runtime";
/**
* @generated from protobuf message Packet
*/
export interface PacketType {
/**
* @generated from protobuf field: bytes originId = 1;
*/
originId: Uint8Array; originId: Uint8Array;
/**
* @generated from protobuf field: uint32 messageNumber = 2;
*/
messageNumber: number; messageNumber: number;
/**
* @generated from protobuf field: uint32 ttl = 3;
*/
ttl: number; ttl: number;
/**
* @generated from protobuf field: bytes data = 4;
*/
data: Uint8Array; data: Uint8Array;
} }
// @generated message type with reflection information, may provide speed optimized methods
function createBasePacket(): Packet { class Packet$Type extends MessageType<PacketType> {
return { originId: new Uint8Array(), messageNumber: 0, ttl: 0, data: new Uint8Array() }; constructor() {
} super("Packet", [
{ no: 1, name: "originId", kind: "scalar", T: 12 /*ScalarType.BYTES*/ },
export const Packet = { {
encode(message: Packet, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer { no: 2,
if (message.originId.length !== 0) { name: "messageNumber",
writer.uint32(10).bytes(message.originId); kind: "scalar",
} T: 13 /*ScalarType.UINT32*/,
if (message.messageNumber !== 0) {
writer.uint32(16).uint32(message.messageNumber);
}
if (message.ttl !== 0) {
writer.uint32(24).uint32(message.ttl);
}
if (message.data.length !== 0) {
writer.uint32(34).bytes(message.data);
}
return writer;
}, },
{ no: 3, name: "ttl", kind: "scalar", T: 13 /*ScalarType.UINT32*/ },
decode(input: _m0.Reader | Uint8Array, length?: number): Packet { { no: 4, name: "data", kind: "scalar", T: 12 /*ScalarType.BYTES*/ },
const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input); ]);
let end = length === undefined ? reader.len : reader.pos + length; }
const message = createBasePacket(); create(value?: PartialMessage<PacketType>): PacketType {
const message = {
originId: new Uint8Array(0),
messageNumber: 0,
ttl: 0,
data: new Uint8Array(0),
};
globalThis.Object.defineProperty(message, MESSAGE_TYPE, {
enumerable: false,
value: this,
});
if (value !== undefined)
reflectionMergePartial<PacketType>(this, message, value);
return message;
}
internalBinaryRead(
reader: IBinaryReader,
length: number,
options: BinaryReadOptions,
target?: PacketType
): PacketType {
let message = target ?? this.create(),
end = reader.pos + length;
while (reader.pos < end) { while (reader.pos < end) {
const tag = reader.uint32(); let [fieldNo, wireType] = reader.tag();
switch (tag >>> 3) { switch (fieldNo) {
case 1: case /* bytes originId */ 1:
message.originId = reader.bytes(); message.originId = reader.bytes();
break; break;
case 2: case /* uint32 messageNumber */ 2:
message.messageNumber = reader.uint32(); message.messageNumber = reader.uint32();
break; break;
case 3: case /* uint32 ttl */ 3:
message.ttl = reader.uint32(); message.ttl = reader.uint32();
break; break;
case 4: case /* bytes data */ 4:
message.data = reader.bytes(); message.data = reader.bytes();
break; break;
default: default:
reader.skipType(tag & 7); let u = options.readUnknownField;
break; if (u === "throw")
throw new globalThis.Error(
`Unknown field ${fieldNo} (wire type ${wireType}) for ${this.typeName}`
);
let d = reader.skip(wireType);
if (u !== false)
(u === true ? UnknownFieldHandler.onRead : u)(
this.typeName,
message,
fieldNo,
wireType,
d
);
} }
} }
return message; return message;
},
fromJSON(object: any): Packet {
return {
originId: isSet(object.originId) ? bytesFromBase64(object.originId) : new Uint8Array(),
messageNumber: isSet(object.messageNumber) ? Number(object.messageNumber) : 0,
ttl: isSet(object.ttl) ? Number(object.ttl) : 0,
data: isSet(object.data) ? bytesFromBase64(object.data) : new Uint8Array(),
};
},
toJSON(message: Packet): unknown {
const obj: any = {};
message.originId !== undefined &&
(obj.originId = base64FromBytes(message.originId !== undefined ? message.originId : new Uint8Array()));
message.messageNumber !== undefined && (obj.messageNumber = Math.round(message.messageNumber));
message.ttl !== undefined && (obj.ttl = Math.round(message.ttl));
message.data !== undefined &&
(obj.data = base64FromBytes(message.data !== undefined ? message.data : new Uint8Array()));
return obj;
},
fromPartial<I extends Exact<DeepPartial<Packet>, I>>(object: I): Packet {
const message = createBasePacket();
message.originId = object.originId ?? new Uint8Array();
message.messageNumber = object.messageNumber ?? 0;
message.ttl = object.ttl ?? 0;
message.data = object.data ?? new Uint8Array();
return message;
},
};
declare var self: any | undefined;
declare var window: any | undefined;
declare var global: any | undefined;
var globalThis: any = (() => {
if (typeof globalThis !== "undefined") {
return globalThis;
} }
if (typeof self !== "undefined") { internalBinaryWrite(
return self; message: PacketType,
} writer: IBinaryWriter,
if (typeof window !== "undefined") { options: BinaryWriteOptions
return window; ): IBinaryWriter {
} /* bytes originId = 1; */
if (typeof global !== "undefined") { if (message.originId.length)
return global; writer.tag(1, WireType.LengthDelimited).bytes(message.originId);
} /* uint32 messageNumber = 2; */
throw "Unable to locate global object"; if (message.messageNumber !== 0)
})(); writer.tag(2, WireType.Varint).uint32(message.messageNumber);
/* uint32 ttl = 3; */
function bytesFromBase64(b64: string): Uint8Array { if (message.ttl !== 0) writer.tag(3, WireType.Varint).uint32(message.ttl);
if (globalThis.Buffer) { /* bytes data = 4; */
return Uint8Array.from(globalThis.Buffer.from(b64, "base64")); if (message.data.length)
} else { writer.tag(4, WireType.LengthDelimited).bytes(message.data);
const bin = globalThis.atob(b64); let u = options.writeUnknownFields;
const arr = new Uint8Array(bin.length); if (u !== false)
for (let i = 0; i < bin.length; ++i) { (u == true ? UnknownFieldHandler.onWrite : u)(
arr[i] = bin.charCodeAt(i); this.typeName,
} message,
return arr; writer
);
return writer;
} }
} }
/**
function base64FromBytes(arr: Uint8Array): string { * @generated MessageType for protobuf message Packet
if (globalThis.Buffer) { */
return globalThis.Buffer.from(arr).toString("base64"); export const Packet = new Packet$Type();
} else {
const bin: string[] = [];
arr.forEach((byte) => {
bin.push(String.fromCharCode(byte));
});
return globalThis.btoa(bin.join(""));
}
}
type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined;
export type DeepPartial<T> = T extends Builtin ? T
: T extends Array<infer U> ? Array<DeepPartial<U>> : T extends ReadonlyArray<infer U> ? ReadonlyArray<DeepPartial<U>>
: T extends {} ? { [K in keyof T]?: DeepPartial<T[K]> }
: Partial<T>;
type KeysOfUnion<T> = T extends T ? keyof T : never;
export type Exact<P, I extends P> = P extends Builtin ? P
: P & { [K in keyof P]: Exact<P[K], I[K]> } & { [K in Exclude<keyof I, KeysOfUnion<P>>]: never };
function isSet(value: any): boolean {
return value !== null && value !== undefined;
}