*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]),
|
||||||
})
|
})
|
||||||
|
|
205
src/messages.ts
205
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";
|
||||||
|
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 = {
|
{ 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 (value !== undefined)
|
||||||
|
reflectionMergePartial<PacketType>(this, message, value);
|
||||||
|
return message;
|
||||||
}
|
}
|
||||||
if (message.ttl !== 0) {
|
internalBinaryRead(reader: IBinaryReader, length: number, options: BinaryReadOptions, target?: PacketType): PacketType {
|
||||||
writer.uint32(24).uint32(message.ttl);
|
let message = target ?? this.create(), end = reader.pos + length;
|
||||||
}
|
|
||||||
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();
|
|
||||||
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(message: PacketType, writer: IBinaryWriter, options: BinaryWriteOptions): IBinaryWriter {
|
||||||
return self;
|
/* bytes originId = 1; */
|
||||||
}
|
if (message.originId.length)
|
||||||
if (typeof window !== "undefined") {
|
writer.tag(1, WireType.LengthDelimited).bytes(message.originId);
|
||||||
return window;
|
/* uint32 messageNumber = 2; */
|
||||||
}
|
if (message.messageNumber !== 0)
|
||||||
if (typeof global !== "undefined") {
|
writer.tag(2, WireType.Varint).uint32(message.messageNumber);
|
||||||
return global;
|
/* uint32 ttl = 3; */
|
||||||
}
|
if (message.ttl !== 0)
|
||||||
throw "Unable to locate global object";
|
writer.tag(3, WireType.Varint).uint32(message.ttl);
|
||||||
})();
|
/* bytes data = 4; */
|
||||||
|
if (message.data.length)
|
||||||
function bytesFromBase64(b64: string): Uint8Array {
|
writer.tag(4, WireType.LengthDelimited).bytes(message.data);
|
||||||
if (globalThis.Buffer) {
|
let u = options.writeUnknownFields;
|
||||||
return Uint8Array.from(globalThis.Buffer.from(b64, "base64"));
|
if (u !== false)
|
||||||
} else {
|
(u == true ? 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: 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;
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue