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
const compact_encoding_1 = __importDefault(require("compact-encoding"));
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 TTL = 255;
const PROTOCOL = "lumeweb.flood";
@ -25,14 +25,14 @@ class DHTFlood extends events_1.default {
messageNumber;
lru;
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();
this.id = id;
this.ttl = ttl;
this.messageNumber = messageNumber;
this.lru = new lru_1.default(lruSize);
if (!swarm) {
throw new Error('swarm is required');
throw new Error("swarm is required");
}
this.swarm = swarm;
this.swarm.on("connection", (peer) => {
@ -44,41 +44,48 @@ class DHTFlood extends events_1.default {
const originIdBuf = b4a_1.default.from(originId);
// Ignore messages from ourselves
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
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);
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);
return debug("Got message at end of TTL", originId, messageNumber, ttl);
messenger.send({
originId,
messageNumber,
data,
ttl: ttl - 1
ttl: ttl - 1,
});
}
setupPeer(peer) {
const mux = protomux_1.default.from(peer);
let chan;
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];
if (!chan) {
throw new Error('could not find channel');
throw new Error("could not find channel");
}
if (!chan.messages.length) {
chan.addMessage({
encoding: {
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.encode(m).finish()),
decode: (state) => messages_js_1.Packet.decode(compact_encoding_1.default.raw.decode(state)),
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.toBinary(messages_js_1.Packet.create(m))),
decode: (state) => messages_js_1.Packet.fromBinary(compact_encoding_1.default.raw.decode(state)),
},
onmessage: (msg) => this.handleMessage(msg, chan.messages[0]),
});
@ -97,7 +104,7 @@ class DHTFlood extends events_1.default {
originId: id,
messageNumber,
ttl,
data
data,
});
}
}

63
dist/messages.d.ts vendored
View File

@ -1,38 +1,39 @@
import * as _m0 from "protobufjs/minimal";
export declare const protobufPackage = "";
/** type=0 */
export interface Packet {
import type { BinaryWriteOptions } from "@protobuf-ts/runtime";
import type { IBinaryWriter } from "@protobuf-ts/runtime";
import type { BinaryReadOptions } from "@protobuf-ts/runtime";
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;
/**
* @generated from protobuf field: uint32 messageNumber = 2;
*/
messageNumber: number;
/**
* @generated from protobuf field: uint32 ttl = 3;
*/
ttl: number;
/**
* @generated from protobuf field: bytes data = 4;
*/
data: Uint8Array;
}
export declare const Packet: {
encode(message: Packet, writer?: _m0.Writer): _m0.Writer;
decode(input: _m0.Reader | Uint8Array, length?: number): Packet;
fromJSON(object: any): Packet;
toJSON(message: Packet): unknown;
fromPartial<I extends {
originId?: Uint8Array | undefined;
messageNumber?: number | undefined;
ttl?: number | undefined;
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;
};
declare class Packet$Type extends MessageType<PacketType> {
constructor();
create(value?: PartialMessage<PacketType>): PacketType;
internalBinaryRead(reader: IBinaryReader, length: number, options: BinaryReadOptions, target?: PacketType): PacketType;
internalBinaryWrite(message: PacketType, writer: IBinaryWriter, options: BinaryWriteOptions): IBinaryWriter;
}
/**
* @generated MessageType for protobuf message Packet
*/
export declare const Packet: Packet$Type;
export {};
//# 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"}

197
dist/messages.js vendored
View File

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

View File

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

View File

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

View File

@ -5,121 +5,140 @@ import LRU from "lru";
import debug0 from "debug";
// @ts-ignore
import Protomux from "protomux";
import {Packet} from "./messages.js";
import { Packet, PacketType } from "./messages.js";
// @ts-ignore
import c from "compact-encoding"
import b4a from "b4a"
import c from "compact-encoding";
import b4a from "b4a";
const debug = debug0('dht-flood')
const debug = debug0("dht-flood");
const LRU_SIZE = 255
const TTL = 255
const PROTOCOL = "lumeweb.flood"
const LRU_SIZE = 255;
const TTL = 255;
const PROTOCOL = "lumeweb.flood";
const FLOOD_SYMBOL = Symbol.for(PROTOCOL)
const FLOOD_SYMBOL = Symbol.for(PROTOCOL);
export default class DHTFlood extends EventEmitter {
private id: Buffer;
private ttl: number;
private messageNumber: number;
private lru: LRU;
private swarm: any;
private id: Buffer;
private ttl: number;
private messageNumber: number;
private lru: LRU;
private swarm: any;
constructor({
lruSize = LRU_SIZE,
ttl = TTL,
messageNumber = 0,
id = crypto.randomBytes(32),
swarm = null
} = {}) {
super()
constructor({
lruSize = LRU_SIZE,
ttl = TTL,
messageNumber = 0,
id = crypto.randomBytes(32),
swarm = null,
} = {}) {
super();
this.id = id
this.ttl = ttl
this.messageNumber = messageNumber
this.lru = new LRU(lruSize)
if (!swarm) {
throw new Error('swarm is required');
}
this.swarm = swarm;
this.id = id;
this.ttl = ttl;
this.messageNumber = messageNumber;
this.lru = new LRU(lruSize);
if (!swarm) {
throw new Error("swarm is required");
}
this.swarm = swarm;
this.swarm.on("connection", (peer: any) => {
const mux = Protomux.from(peer);
mux.pair({protocol: PROTOCOL}, () => this.setupPeer(peer));
});
this.swarm.on("connection", (peer: any) => {
const mux = Protomux.from(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;
}
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
});
chan = peer[FLOOD_SYMBOL];
if (!chan) {
throw new Error("could not find channel");
}
private setupPeer(peer: any) {
const mux = Protomux.from(peer);
let chan: any;
if (!mux.opened({protocol: PROTOCOL})) {
chan = mux.createChannel({
protocol: PROTOCOL,
});
peer[FLOOD_SYMBOL] = chan;
}
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.messages.length) {
chan.addMessage({
encoding: {
preencode: (state: any, m: any) =>
c.raw.preencode(state, Packet.toBinary(Packet.create(m))),
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]),
});
}
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
})
}
if (!chan.opened) {
chan.open();
}
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,149 +1,136 @@
/* eslint-disable */
import * as _m0 from "protobufjs/minimal";
export const protobufPackage = "";
/** type=0 */
export interface Packet {
// @generated by protobuf-ts 2.8.1
// @generated from protobuf file "messages.proto" (syntax proto2)
// tslint:disable
import type { BinaryWriteOptions } from "@protobuf-ts/runtime";
import type { IBinaryWriter } from "@protobuf-ts/runtime";
import { WireType } from "@protobuf-ts/runtime";
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;
/**
* @generated from protobuf field: uint32 messageNumber = 2;
*/
messageNumber: number;
/**
* @generated from protobuf field: uint32 ttl = 3;
*/
ttl: number;
/**
* @generated from protobuf field: bytes data = 4;
*/
data: Uint8Array;
}
function createBasePacket(): Packet {
return { originId: new Uint8Array(), messageNumber: 0, ttl: 0, data: new Uint8Array() };
}
export const Packet = {
encode(message: Packet, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.originId.length !== 0) {
writer.uint32(10).bytes(message.originId);
}
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;
},
decode(input: _m0.Reader | Uint8Array, length?: number): Packet {
const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input);
let end = length === undefined ? reader.len : reader.pos + length;
const message = createBasePacket();
// @generated message type with reflection information, may provide speed optimized methods
class Packet$Type extends MessageType<PacketType> {
constructor() {
super("Packet", [
{ no: 1, name: "originId", kind: "scalar", T: 12 /*ScalarType.BYTES*/ },
{
no: 2,
name: "messageNumber",
kind: "scalar",
T: 13 /*ScalarType.UINT32*/,
},
{ no: 3, name: "ttl", kind: "scalar", T: 13 /*ScalarType.UINT32*/ },
{ no: 4, name: "data", kind: "scalar", T: 12 /*ScalarType.BYTES*/ },
]);
}
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) {
const tag = reader.uint32();
switch (tag >>> 3) {
case 1:
let [fieldNo, wireType] = reader.tag();
switch (fieldNo) {
case /* bytes originId */ 1:
message.originId = reader.bytes();
break;
case 2:
case /* uint32 messageNumber */ 2:
message.messageNumber = reader.uint32();
break;
case 3:
case /* uint32 ttl */ 3:
message.ttl = reader.uint32();
break;
case 4:
case /* bytes data */ 4:
message.data = reader.bytes();
break;
default:
reader.skipType(tag & 7);
break;
let u = options.readUnknownField;
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;
},
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") {
return self;
}
if (typeof window !== "undefined") {
return window;
}
if (typeof global !== "undefined") {
return global;
}
throw "Unable to locate global object";
})();
function bytesFromBase64(b64: string): Uint8Array {
if (globalThis.Buffer) {
return Uint8Array.from(globalThis.Buffer.from(b64, "base64"));
} else {
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;
internalBinaryWrite(
message: PacketType,
writer: IBinaryWriter,
options: BinaryWriteOptions
): IBinaryWriter {
/* bytes originId = 1; */
if (message.originId.length)
writer.tag(1, WireType.LengthDelimited).bytes(message.originId);
/* uint32 messageNumber = 2; */
if (message.messageNumber !== 0)
writer.tag(2, WireType.Varint).uint32(message.messageNumber);
/* uint32 ttl = 3; */
if (message.ttl !== 0) writer.tag(3, WireType.Varint).uint32(message.ttl);
/* bytes data = 4; */
if (message.data.length)
writer.tag(4, WireType.LengthDelimited).bytes(message.data);
let u = options.writeUnknownFields;
if (u !== false)
(u == true ? UnknownFieldHandler.onWrite : u)(
this.typeName,
message,
writer
);
return writer;
}
}
function base64FromBytes(arr: Uint8Array): string {
if (globalThis.Buffer) {
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;
}
/**
* @generated MessageType for protobuf message Packet
*/
export const Packet = new Packet$Type();