*switch to protobuf-ts
This commit is contained in:
parent
5439ef3299
commit
9c7dc7dad8
|
@ -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;
|
||||||
|
|
|
@ -10,6 +10,7 @@
|
||||||
"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",
|
||||||
|
@ -17,7 +18,6 @@
|
||||||
"hyperswarm": "^4.3.5",
|
"hyperswarm": "^4.3.5",
|
||||||
"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"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
10
src/index.ts
10
src/index.ts
|
@ -5,7 +5,7 @@ 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"
|
||||||
|
@ -49,7 +49,7 @@ export default class DHTFlood extends EventEmitter {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
||||||
|
@ -93,9 +93,9 @@ export default class DHTFlood extends EventEmitter {
|
||||||
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) => c.raw.preencode(state, Packet.toBinary(Packet.create(m))),
|
||||||
encode: (state: any, m: any) => c.raw.encode(state, Packet.encode(m).finish()),
|
encode: (state: any, m: any) => c.raw.encode(state, Packet.toBinary(Packet.create(m))),
|
||||||
decode: (state: any) => Packet.decode(c.raw.decode(state)),
|
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]),
|
||||||
})
|
})
|
||||||
|
|
247
src/messages.ts
247
src/messages.ts
|
@ -1,149 +1,102 @@
|
||||||
/* 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";
|
||||||
originId: Uint8Array;
|
import type { IBinaryReader } from "@protobuf-ts/runtime";
|
||||||
messageNumber: number;
|
import { UnknownFieldHandler } from "@protobuf-ts/runtime";
|
||||||
ttl: number;
|
import type { PartialMessage } from "@protobuf-ts/runtime";
|
||||||
data: Uint8Array;
|
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;
|
||||||
}
|
}
|
||||||
|
// @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 = {
|
{ no: 2, name: "messageNumber", kind: "scalar", T: 13 /*ScalarType.UINT32*/ },
|
||||||
encode(message: Packet, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
|
{ no: 3, name: "ttl", kind: "scalar", T: 13 /*ScalarType.UINT32*/ },
|
||||||
if (message.originId.length !== 0) {
|
{ no: 4, name: "data", kind: "scalar", T: 12 /*ScalarType.BYTES*/ }
|
||||||
writer.uint32(10).bytes(message.originId);
|
]);
|
||||||
}
|
}
|
||||||
if (message.messageNumber !== 0) {
|
create(value?: PartialMessage<PacketType>): PacketType {
|
||||||
writer.uint32(16).uint32(message.messageNumber);
|
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 (message.ttl !== 0) {
|
if (value !== undefined)
|
||||||
writer.uint32(24).uint32(message.ttl);
|
reflectionMergePartial<PacketType>(this, message, value);
|
||||||
}
|
return message;
|
||||||
if (message.data.length !== 0) {
|
}
|
||||||
writer.uint32(34).bytes(message.data);
|
internalBinaryRead(reader: IBinaryReader, length: number, options: BinaryReadOptions, target?: PacketType): PacketType {
|
||||||
}
|
let message = target ?? this.create(), end = reader.pos + length;
|
||||||
return writer;
|
while (reader.pos < end) {
|
||||||
},
|
let [fieldNo, wireType] = reader.tag();
|
||||||
|
switch (fieldNo) {
|
||||||
decode(input: _m0.Reader | Uint8Array, length?: number): Packet {
|
case /* bytes originId */ 1:
|
||||||
const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input);
|
message.originId = reader.bytes();
|
||||||
let end = length === undefined ? reader.len : reader.pos + length;
|
break;
|
||||||
const message = createBasePacket();
|
case /* uint32 messageNumber */ 2:
|
||||||
while (reader.pos < end) {
|
message.messageNumber = reader.uint32();
|
||||||
const tag = reader.uint32();
|
break;
|
||||||
switch (tag >>> 3) {
|
case /* uint32 ttl */ 3:
|
||||||
case 1:
|
message.ttl = reader.uint32();
|
||||||
message.originId = reader.bytes();
|
break;
|
||||||
break;
|
case /* bytes data */ 4:
|
||||||
case 2:
|
message.data = reader.bytes();
|
||||||
message.messageNumber = reader.uint32();
|
break;
|
||||||
break;
|
default:
|
||||||
case 3:
|
let u = options.readUnknownField;
|
||||||
message.ttl = reader.uint32();
|
if (u === "throw")
|
||||||
break;
|
throw new globalThis.Error(`Unknown field ${fieldNo} (wire type ${wireType}) for ${this.typeName}`);
|
||||||
case 4:
|
let d = reader.skip(wireType);
|
||||||
message.data = reader.bytes();
|
if (u !== false)
|
||||||
break;
|
(u === true ? UnknownFieldHandler.onRead : u)(this.typeName, message, fieldNo, wireType, d);
|
||||||
default:
|
}
|
||||||
reader.skipType(tag & 7);
|
}
|
||||||
break;
|
return message;
|
||||||
}
|
}
|
||||||
}
|
internalBinaryWrite(message: PacketType, writer: IBinaryWriter, options: BinaryWriteOptions): IBinaryWriter {
|
||||||
return message;
|
/* bytes originId = 1; */
|
||||||
},
|
if (message.originId.length)
|
||||||
|
writer.tag(1, WireType.LengthDelimited).bytes(message.originId);
|
||||||
fromJSON(object: any): Packet {
|
/* uint32 messageNumber = 2; */
|
||||||
return {
|
if (message.messageNumber !== 0)
|
||||||
originId: isSet(object.originId) ? bytesFromBase64(object.originId) : new Uint8Array(),
|
writer.tag(2, WireType.Varint).uint32(message.messageNumber);
|
||||||
messageNumber: isSet(object.messageNumber) ? Number(object.messageNumber) : 0,
|
/* uint32 ttl = 3; */
|
||||||
ttl: isSet(object.ttl) ? Number(object.ttl) : 0,
|
if (message.ttl !== 0)
|
||||||
data: isSet(object.data) ? bytesFromBase64(object.data) : new Uint8Array(),
|
writer.tag(3, WireType.Varint).uint32(message.ttl);
|
||||||
};
|
/* bytes data = 4; */
|
||||||
},
|
if (message.data.length)
|
||||||
|
writer.tag(4, WireType.LengthDelimited).bytes(message.data);
|
||||||
toJSON(message: Packet): unknown {
|
let u = options.writeUnknownFields;
|
||||||
const obj: any = {};
|
if (u !== false)
|
||||||
message.originId !== undefined &&
|
(u == true ? UnknownFieldHandler.onWrite : u)(this.typeName, message, writer);
|
||||||
(obj.originId = base64FromBytes(message.originId !== undefined ? message.originId : new Uint8Array()));
|
return writer;
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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();
|
||||||
|
|
Loading…
Reference in New Issue