Compare commits

..

No commits in common. "01a637a767adde83b57a72445f438c3555609012" and "5439ef3299d5e14778a564c5b58f9b6c5bae8c5c" have entirely different histories.

9 changed files with 404 additions and 365 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;IA2CjB,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;IAmCjB,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,48 +44,41 @@ 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.toBinary(messages_js_1.Packet.create(m))), preencode: (state, m) => compact_encoding_1.default.raw.preencode(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))), encode: (state, m) => compact_encoding_1.default.raw.encode(state, messages_js_1.Packet.encode(m).finish()),
decode: (state) => messages_js_1.Packet.fromBinary(compact_encoding_1.default.raw.decode(state)), decode: (state) => messages_js_1.Packet.decode(compact_encoding_1.default.raw.decode(state)),
}, },
onmessage: (msg) => this.handleMessage(msg, chan.messages[0]), onmessage: (msg) => this.handleMessage(msg, chan.messages[0]),
}); });
@ -104,7 +97,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,39 +1,38 @@
import type { BinaryWriteOptions } from "@protobuf-ts/runtime"; import * as _m0 from "protobufjs/minimal";
import type { IBinaryWriter } from "@protobuf-ts/runtime"; export declare const protobufPackage = "";
import type { BinaryReadOptions } from "@protobuf-ts/runtime"; /** type=0 */
import type { IBinaryReader } from "@protobuf-ts/runtime"; export interface Packet {
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;
} }
declare class Packet$Type extends MessageType<PacketType> { export declare const Packet: {
constructor(); encode(message: Packet, writer?: _m0.Writer): _m0.Writer;
create(value?: PartialMessage<PacketType>): PacketType; decode(input: _m0.Reader | Uint8Array, length?: number): Packet;
internalBinaryRead(reader: IBinaryReader, length: number, options: BinaryReadOptions, target?: PacketType): PacketType; fromJSON(object: any): Packet;
internalBinaryWrite(message: PacketType, writer: IBinaryWriter, options: BinaryWriteOptions): IBinaryWriter; toJSON(message: Packet): unknown;
} fromPartial<I extends {
/** originId?: Uint8Array | undefined;
* @generated MessageType for protobuf message Packet messageNumber?: number | undefined;
*/ ttl?: number | undefined;
export declare const Packet: Packet$Type; data?: Uint8Array | undefined;
} & {
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":"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"} {"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"}

197
dist/messages.js vendored
View File

@ -1,89 +1,144 @@
"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 = void 0; exports.Packet = exports.protobufPackage = void 0;
const runtime_1 = require("@protobuf-ts/runtime"); /* eslint-disable */
const runtime_2 = require("@protobuf-ts/runtime"); const _m0 = __importStar(require("protobufjs/minimal"));
const runtime_3 = require("@protobuf-ts/runtime"); exports.protobufPackage = "";
const runtime_4 = require("@protobuf-ts/runtime"); function createBasePacket() {
const runtime_5 = require("@protobuf-ts/runtime"); return { originId: new Uint8Array(), messageNumber: 0, ttl: 0, data: new Uint8Array() };
// @generated message type with reflection information, may provide speed optimized methods }
class Packet$Type extends runtime_5.MessageType { exports.Packet = {
constructor() { encode(message, writer = _m0.Writer.create()) {
super("Packet", [ if (message.originId.length !== 0) {
{ no: 1, name: "originId", kind: "scalar", T: 12 /*ScalarType.BYTES*/ }, writer.uint32(10).bytes(message.originId);
{ }
no: 2, if (message.messageNumber !== 0) {
name: "messageNumber", writer.uint32(16).uint32(message.messageNumber);
kind: "scalar", }
T: 13 /*ScalarType.UINT32*/, if (message.ttl !== 0) {
}, writer.uint32(24).uint32(message.ttl);
{ no: 3, name: "ttl", kind: "scalar", T: 13 /*ScalarType.UINT32*/ }, }
{ no: 4, name: "data", kind: "scalar", T: 12 /*ScalarType.BYTES*/ }, if (message.data.length !== 0) {
]); writer.uint32(34).bytes(message.data);
} }
create(value) { return writer;
const message = { },
originId: new Uint8Array(0), decode(input, length) {
messageNumber: 0, const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input);
ttl: 0, let end = length === undefined ? reader.len : reader.pos + length;
data: new Uint8Array(0), const message = createBasePacket();
};
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) {
let [fieldNo, wireType] = reader.tag(); const tag = reader.uint32();
switch (fieldNo) { switch (tag >>> 3) {
case /* bytes originId */ 1: case 1:
message.originId = reader.bytes(); message.originId = reader.bytes();
break; break;
case /* uint32 messageNumber */ 2: case 2:
message.messageNumber = reader.uint32(); message.messageNumber = reader.uint32();
break; break;
case /* uint32 ttl */ 3: case 3:
message.ttl = reader.uint32(); message.ttl = reader.uint32();
break; break;
case /* bytes data */ 4: case 4:
message.data = reader.bytes(); message.data = reader.bytes();
break; break;
default: default:
let u = options.readUnknownField; reader.skipType(tag & 7);
if (u === "throw") break;
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;
} }
internalBinaryWrite(message, writer, options) { if (typeof self !== "undefined") {
/* bytes originId = 1; */ return self;
if (message.originId.length) }
writer.tag(1, runtime_1.WireType.LengthDelimited).bytes(message.originId); if (typeof window !== "undefined") {
/* uint32 messageNumber = 2; */ return window;
if (message.messageNumber !== 0) }
writer.tag(2, runtime_1.WireType.Varint).uint32(message.messageNumber); if (typeof global !== "undefined") {
/* uint32 ttl = 3; */ return global;
if (message.ttl !== 0) }
writer.tag(3, runtime_1.WireType.Varint).uint32(message.ttl); throw "Unable to locate global object";
/* bytes data = 4; */ })();
if (message.data.length) function bytesFromBase64(b64) {
writer.tag(4, runtime_1.WireType.LengthDelimited).bytes(message.data); if (globalThis.Buffer) {
let u = options.writeUnknownFields; return Uint8Array.from(globalThis.Buffer.from(b64, "base64"));
if (u !== false) }
(u == true ? runtime_2.UnknownFieldHandler.onWrite : u)(this.typeName, message, writer); else {
return writer; const bin = globalThis.atob(b64);
const arr = new Uint8Array(bin.length);
for (let i = 0; i < bin.length; ++i) {
arr[i] = bin.charCodeAt(i);
}
return arr;
} }
} }
/** function base64FromBytes(arr) {
* @generated MessageType for protobuf message Packet if (globalThis.Buffer) {
*/ 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,5 +1,4 @@
syntax = "proto2"; // type=0
message Packet { message Packet {
required bytes originId = 1; required bytes originId = 1;
required uint32 messageNumber = 2; required uint32 messageNumber = 2;

View File

@ -10,15 +10,14 @@
"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,140 +5,121 @@ 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, PacketType } from "./messages.js"; import {Packet} 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;
private ttl: number; private ttl: number;
private messageNumber: number; private messageNumber: number;
private lru: LRU; private lru: LRU;
private swarm: any; private swarm: any;
constructor({ constructor({
lruSize = LRU_SIZE, lruSize = LRU_SIZE,
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 }: PacketType,
messenger: any
) {
const originIdBuf = b4a.from(originId) as Buffer;
// Ignore messages from ourselves
if (originIdBuf.equals(this.id))
return debug("Got message from self", originId, messageNumber);
// Ignore messages we've already seen
const key = originIdBuf.toString("hex") + messageNumber;
if (this.lru.get(key))
return debug(
"Got message that was already seen",
originId,
messageNumber
);
this.lru.set(key, true);
this.emit("message", data, originId, messageNumber);
if (ttl <= 0)
return debug("Got message at end of TTL", originId, messageNumber, ttl);
messenger.send({
originId,
messageNumber,
data,
ttl: ttl - 1,
});
}
private setupPeer(peer: any) {
const mux = Protomux.from(peer);
let chan: any;
const self = this;
if (!mux.opened({ protocol: PROTOCOL })) {
chan = mux.createChannel({
protocol: PROTOCOL,
async onopen() {
self.emit("peer-open", peer);
},
async ondestroy() {
self.emit("peer-remove", peer);
},
});
peer[FLOOD_SYMBOL] = chan;
} }
chan = peer[FLOOD_SYMBOL]; private handleMessage({originId, messageNumber, ttl, data}: Packet, messenger: any) {
const originIdBuf = b4a.from(originId) as Buffer;
// Ignore messages from ourselves
if (originIdBuf.equals(this.id)) return debug('Got message from self', originId, messageNumber)
// Ignore messages we've already seen
const key = originIdBuf.toString('hex') + messageNumber
if (this.lru.get(key)) return debug('Got message that was already seen', originId, messageNumber)
this.lru.set(key, true)
this.emit('message', data, originId, messageNumber)
if (ttl <= 0) return debug('Got message at end of TTL', originId, messageNumber, ttl)
messenger.send({
originId,
messageNumber,
data,
ttl: ttl - 1
});
if (!chan) {
throw new Error("could not find channel");
} }
if (!chan.messages.length) { private setupPeer(peer: any) {
chan.addMessage({ const mux = Protomux.from(peer);
encoding: { let chan: any;
preencode: (state: any, m: any) =>
c.raw.preencode(state, Packet.toBinary(Packet.create(m))), if (!mux.opened({protocol: PROTOCOL})) {
encode: (state: any, m: any) => chan = mux.createChannel({
c.raw.encode(state, Packet.toBinary(Packet.create(m))), protocol: PROTOCOL,
decode: (state: any) => Packet.fromBinary(c.raw.decode(state)), });
}, peer[FLOOD_SYMBOL] = chan;
onmessage: (msg: any) => this.handleMessage(msg, chan.messages[0]), }
});
chan = peer[FLOOD_SYMBOL];
if (!chan) {
throw new Error('could not find channel');
}
if (!chan.messages.length) {
chan.addMessage({
encoding: {
preencode: (state: any, m: any) => c.raw.preencode(state, Packet.encode(m).finish()),
encode: (state: any, m: any) => c.raw.encode(state, Packet.encode(m).finish()),
decode: (state: any) => Packet.decode(c.raw.decode(state)),
},
onmessage: (msg: any) => this.handleMessage(msg, chan.messages[0]),
})
}
if (!chan.opened) {
chan.open();
}
return chan.messages[0];
} }
if (!chan.opened) { broadcast(data: any, ttl = this.ttl) {
chan.open(); this.messageNumber++
const {id, messageNumber} = this
for (const peer of this.swarm.connections.values()) {
const message = this.setupPeer(peer);
message.send({
originId: id,
messageNumber,
ttl,
data
})
}
} }
return chan.messages[0];
}
broadcast(data: any, ttl = this.ttl) {
this.messageNumber++;
const { id, messageNumber } = this;
for (const peer of this.swarm.connections.values()) {
const message = this.setupPeer(peer);
message.send({
originId: id,
messageNumber,
ttl,
data,
});
}
}
} }

View File

@ -1,136 +1,149 @@
// @generated by protobuf-ts 2.8.1 /* eslint-disable */
// @generated from protobuf file "messages.proto" (syntax proto2) import * as _m0 from "protobufjs/minimal";
// tslint:disable
import type { BinaryWriteOptions } from "@protobuf-ts/runtime"; export const protobufPackage = "";
import type { IBinaryWriter } from "@protobuf-ts/runtime";
import { WireType } from "@protobuf-ts/runtime"; /** type=0 */
import type { BinaryReadOptions } from "@protobuf-ts/runtime"; export interface Packet {
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
class Packet$Type extends MessageType<PacketType> { function createBasePacket(): Packet {
constructor() { return { originId: new Uint8Array(), messageNumber: 0, ttl: 0, data: new Uint8Array() };
super("Packet", [ }
{ no: 1, name: "originId", kind: "scalar", T: 12 /*ScalarType.BYTES*/ },
{ export const Packet = {
no: 2, encode(message: Packet, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
name: "messageNumber", if (message.originId.length !== 0) {
kind: "scalar", writer.uint32(10).bytes(message.originId);
T: 13 /*ScalarType.UINT32*/, }
}, if (message.messageNumber !== 0) {
{ no: 3, name: "ttl", kind: "scalar", T: 13 /*ScalarType.UINT32*/ }, writer.uint32(16).uint32(message.messageNumber);
{ no: 4, name: "data", kind: "scalar", T: 12 /*ScalarType.BYTES*/ }, }
]); if (message.ttl !== 0) {
} writer.uint32(24).uint32(message.ttl);
create(value?: PartialMessage<PacketType>): PacketType { }
const message = { if (message.data.length !== 0) {
originId: new Uint8Array(0), writer.uint32(34).bytes(message.data);
messageNumber: 0, }
ttl: 0, return writer;
data: new Uint8Array(0), },
};
globalThis.Object.defineProperty(message, MESSAGE_TYPE, { decode(input: _m0.Reader | Uint8Array, length?: number): Packet {
enumerable: false, const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input);
value: this, let end = length === undefined ? reader.len : reader.pos + length;
}); const message = createBasePacket();
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) {
let [fieldNo, wireType] = reader.tag(); const tag = reader.uint32();
switch (fieldNo) { switch (tag >>> 3) {
case /* bytes originId */ 1: case 1:
message.originId = reader.bytes(); message.originId = reader.bytes();
break; break;
case /* uint32 messageNumber */ 2: case 2:
message.messageNumber = reader.uint32(); message.messageNumber = reader.uint32();
break; break;
case /* uint32 ttl */ 3: case 3:
message.ttl = reader.uint32(); message.ttl = reader.uint32();
break; break;
case /* bytes data */ 4: case 4:
message.data = reader.bytes(); message.data = reader.bytes();
break; break;
default: default:
let u = options.readUnknownField; reader.skipType(tag & 7);
if (u === "throw") break;
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;
} }
internalBinaryWrite( if (typeof self !== "undefined") {
message: PacketType, return self;
writer: IBinaryWriter, }
options: BinaryWriteOptions if (typeof window !== "undefined") {
): IBinaryWriter { return window;
/* bytes originId = 1; */ }
if (message.originId.length) if (typeof global !== "undefined") {
writer.tag(1, WireType.LengthDelimited).bytes(message.originId); return global;
/* uint32 messageNumber = 2; */ }
if (message.messageNumber !== 0) throw "Unable to locate global object";
writer.tag(2, WireType.Varint).uint32(message.messageNumber); })();
/* uint32 ttl = 3; */
if (message.ttl !== 0) writer.tag(3, WireType.Varint).uint32(message.ttl); function bytesFromBase64(b64: string): Uint8Array {
/* bytes data = 4; */ if (globalThis.Buffer) {
if (message.data.length) return Uint8Array.from(globalThis.Buffer.from(b64, "base64"));
writer.tag(4, WireType.LengthDelimited).bytes(message.data); } else {
let u = options.writeUnknownFields; const bin = globalThis.atob(b64);
if (u !== false) const arr = new Uint8Array(bin.length);
(u == true ? UnknownFieldHandler.onWrite : u)( for (let i = 0; i < bin.length; ++i) {
this.typeName, arr[i] = bin.charCodeAt(i);
message, }
writer return arr;
);
return writer;
} }
} }
/**
* @generated MessageType for protobuf message Packet function base64FromBytes(arr: Uint8Array): string {
*/ if (globalThis.Buffer) {
export const Packet = new Packet$Type(); return globalThis.Buffer.from(arr).toString("base64");
} 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;
}