Use ts-proto

This allows the generation of ts files which makes it easier
to handle with test frameworks than just d.ts files
This commit is contained in:
Franck Royer 2021-03-22 15:34:13 +11:00
parent a89f2700a2
commit 704f2770d1
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
8 changed files with 149 additions and 2462 deletions

View File

@ -3,7 +3,7 @@
"parser": "@typescript-eslint/parser",
"parserOptions": { "project": "./tsconfig.json" },
"env": { "es6": true },
"ignorePatterns": ["node_modules", "build", "coverage", "gen"],
"ignorePatterns": ["node_modules", "build", "coverage", "proto"],
"plugins": ["import", "eslint-comments", "functional"],
"extends": [
"eslint:recommended",

1
.gitignore vendored
View File

@ -8,3 +8,4 @@ src/gen
coverage
*.log
yarn.lock
src/proto/**/*.ts

View File

@ -1,11 +1,6 @@
version: v1beta1
plugins:
- name: ts
out: src/gen/proto
- name: ts_proto
out: ./src/proto
opt: grpc_js
# protoc 3.13 our above is needed as the schema is v3 with optional fields
- name: js
out: build/main/gen/proto
opt: import_style=commonjs,binary

2516
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -57,6 +57,7 @@
"libp2p-tcp": "^0.15.3",
"multiaddr": "^8.1.2",
"prompt-sync": "^4.2.0",
"ts-proto": "^1.74.0",
"yarg": "^1.0.8"
},
"devDependencies": {
@ -83,7 +84,6 @@
"eslint-plugin-import": "^2.22.0",
"fast-check": "^2.14.0",
"gh-pages": "^3.1.0",
"grpc_tools_node_protoc_ts": "^5.1.3",
"mocha": "^8.3.2",
"npm-run-all": "^4.1.5",
"nyc": "^15.1.0",

View File

@ -1,29 +1,18 @@
/// <reference types="../gen/proto/waku/v2/waku_pb" />
import { WakuMessage } from '../gen/proto/waku/v2/waku_pb';
// Ensure that this class matches the proto interface while
import { Reader } from 'protobufjs/minimal';
// Protecting the user from protobuf oddities
import { WakuMessage } from '../proto/waku/v2/waku';
const DEFAULT_CONTENT_TOPIC = 1;
const DEFAULT_VERSION = 0;
export class Message {
public payload: Uint8Array;
public contentTopic: number;
public version: number;
private constructor(public protobuf: WakuMessage) {
this.protobuf = protobuf;
const msg = protobuf.toObject();
// Let's make is easier to avoid mistakes and only store in Uint8Array format
let payload;
if (typeof msg.payload === 'string') {
payload = Buffer.from(msg.payload, 'base64');
} else {
payload = msg.payload;
}
this.payload = payload;
this.contentTopic = msg.contentTopic;
this.version = msg.version;
}
private constructor(
public payload?: Uint8Array,
public contentTopic?: number,
public version?: number
) {}
/**
* Create Message from utf-8 string
@ -31,39 +20,33 @@ export class Message {
* @returns {Message}
*/
static fromUtf8String(message: string): Message {
const wakuMsg = new WakuMessage();
// Only Version 0 is implemented in Waku 2.
// 0: payload SHOULD be either plain or that encryption is done at a separate layer outside of Waku.
wakuMsg.setVersion(0);
// This is the content topic commonly used at this time
wakuMsg.setContentTopic(1);
const buf = Buffer.from(message, 'utf-8');
// Only accepts Uint8Array or base64 string
wakuMsg.setPayload(buf);
return new Message(wakuMsg);
const payload = Buffer.from(message, 'utf-8');
return new Message(payload, DEFAULT_CONTENT_TOPIC, DEFAULT_VERSION);
}
static fromBinary(message: Uint8Array): Message {
const wakuMsg = WakuMessage.deserializeBinary(message);
return new Message(wakuMsg);
static fromBinary(bytes: Uint8Array): Message {
const wakuMsg = WakuMessage.decode(Reader.create(bytes));
return new Message(wakuMsg.payload, wakuMsg.contentTopic, wakuMsg.version);
}
toBinary(): Uint8Array {
return this.protobuf.serializeBinary();
return WakuMessage.encode({
payload: this.payload,
version: this.version,
contentTopic: this.contentTopic,
}).finish();
}
// Purely for tests purposes.
// We do consider protobuf field when checking equality
// As the content is held by the other fields.
// TODO: Consider using WakuMessage.equals
isEqualTo(other: Message) {
const payloadsAreEqual =
this.payload && other.payload
? Buffer.compare(this.payload, other.payload) === 0
: !(this.payload || other.payload);
return (
Buffer.compare(this.payload, other.payload) === 0 &&
payloadsAreEqual &&
this.contentTopic === other.contentTopic &&
this.version === other.version
);

View File

@ -109,7 +109,7 @@ describe('Waku Relay', () => {
expect(msgs[0].version).to.equal(message.version);
const payload = Buffer.from(msgs[0].payload);
expect(Buffer.compare(payload, message.payload)).to.equal(0);
expect(Buffer.compare(payload, message.payload!)).to.equal(0);
});
it('Nim publishes to js', async () => {
@ -132,8 +132,8 @@ describe('Waku Relay', () => {
expect(receivedMsg.contentTopic).to.eq(message.contentTopic);
expect(receivedMsg.version).to.eq(message.version);
const payload = Buffer.from(receivedMsg.payload);
expect(Buffer.compare(payload, message.payload)).to.eq(0);
const payload = Buffer.from(receivedMsg.payload!);
expect(Buffer.compare(payload, message.payload!)).to.eq(0);
});
});
});

View File

@ -108,6 +108,10 @@ export class NimWaku {
async sendMessage(message: Message) {
this.checkProcess();
if (!message.payload) {
throw 'Attempting to send empty message';
}
const rpcMessage = {
payload: bufToHex(message.payload),
contentTopic: message.contentTopic,