*switch to protobuf-ts
This commit is contained in:
parent
5439ef3299
commit
9c7dc7dad8
|
@ -1,4 +1,5 @@
|
|||
// type=0
|
||||
syntax = "proto2";
|
||||
|
||||
message Packet {
|
||||
required bytes originId = 1;
|
||||
required uint32 messageNumber = 2;
|
||||
|
|
|
@ -10,6 +10,7 @@
|
|||
"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",
|
||||
|
@ -17,7 +18,6 @@
|
|||
"hyperswarm": "^4.3.5",
|
||||
"protoc": "^1.1.3",
|
||||
"sodium-universal": "^3.1.0",
|
||||
"tape": "^5.6.1",
|
||||
"ts-proto": "^1.131.2"
|
||||
"tape": "^5.6.1"
|
||||
}
|
||||
}
|
||||
|
|
10
src/index.ts
10
src/index.ts
|
@ -5,7 +5,7 @@ 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"
|
||||
|
@ -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;
|
||||
|
||||
// Ignore messages from ourselves
|
||||
|
@ -93,9 +93,9 @@ export default class DHTFlood extends EventEmitter {
|
|||
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)),
|
||||
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]),
|
||||
})
|
||||
|
|
205
src/messages.ts
205
src/messages.ts
|
@ -1,149 +1,102 @@
|
|||
/* 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);
|
||||
// @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*/ }
|
||||
]);
|
||||
}
|
||||
if (message.messageNumber !== 0) {
|
||||
writer.uint32(16).uint32(message.messageNumber);
|
||||
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;
|
||||
}
|
||||
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();
|
||||
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();
|
||||
|
|
Loading…
Reference in New Issue