*Update dist

This commit is contained in:
Derrick Hammer 2022-11-15 14:28:57 -05:00
parent aab5254ef2
commit 01a637a767
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
4 changed files with 125 additions and 172 deletions

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,CA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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;
}