mirror of
https://github.com/logos-messaging/js-waku.git
synced 2026-01-04 06:43:12 +00:00
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:
parent
a89f2700a2
commit
704f2770d1
@ -3,7 +3,7 @@
|
|||||||
"parser": "@typescript-eslint/parser",
|
"parser": "@typescript-eslint/parser",
|
||||||
"parserOptions": { "project": "./tsconfig.json" },
|
"parserOptions": { "project": "./tsconfig.json" },
|
||||||
"env": { "es6": true },
|
"env": { "es6": true },
|
||||||
"ignorePatterns": ["node_modules", "build", "coverage", "gen"],
|
"ignorePatterns": ["node_modules", "build", "coverage", "proto"],
|
||||||
"plugins": ["import", "eslint-comments", "functional"],
|
"plugins": ["import", "eslint-comments", "functional"],
|
||||||
"extends": [
|
"extends": [
|
||||||
"eslint:recommended",
|
"eslint:recommended",
|
||||||
|
|||||||
1
.gitignore
vendored
1
.gitignore
vendored
@ -8,3 +8,4 @@ src/gen
|
|||||||
coverage
|
coverage
|
||||||
*.log
|
*.log
|
||||||
yarn.lock
|
yarn.lock
|
||||||
|
src/proto/**/*.ts
|
||||||
|
|||||||
@ -1,11 +1,6 @@
|
|||||||
version: v1beta1
|
version: v1beta1
|
||||||
|
|
||||||
plugins:
|
plugins:
|
||||||
- name: ts
|
- name: ts_proto
|
||||||
out: src/gen/proto
|
out: ./src/proto
|
||||||
opt: grpc_js
|
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
2516
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -57,6 +57,7 @@
|
|||||||
"libp2p-tcp": "^0.15.3",
|
"libp2p-tcp": "^0.15.3",
|
||||||
"multiaddr": "^8.1.2",
|
"multiaddr": "^8.1.2",
|
||||||
"prompt-sync": "^4.2.0",
|
"prompt-sync": "^4.2.0",
|
||||||
|
"ts-proto": "^1.74.0",
|
||||||
"yarg": "^1.0.8"
|
"yarg": "^1.0.8"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
@ -83,7 +84,6 @@
|
|||||||
"eslint-plugin-import": "^2.22.0",
|
"eslint-plugin-import": "^2.22.0",
|
||||||
"fast-check": "^2.14.0",
|
"fast-check": "^2.14.0",
|
||||||
"gh-pages": "^3.1.0",
|
"gh-pages": "^3.1.0",
|
||||||
"grpc_tools_node_protoc_ts": "^5.1.3",
|
|
||||||
"mocha": "^8.3.2",
|
"mocha": "^8.3.2",
|
||||||
"npm-run-all": "^4.1.5",
|
"npm-run-all": "^4.1.5",
|
||||||
"nyc": "^15.1.0",
|
"nyc": "^15.1.0",
|
||||||
|
|||||||
@ -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
|
// Ensure that this class matches the proto interface while
|
||||||
|
import { Reader } from 'protobufjs/minimal';
|
||||||
|
|
||||||
// Protecting the user from protobuf oddities
|
// 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 {
|
export class Message {
|
||||||
public payload: Uint8Array;
|
private constructor(
|
||||||
public contentTopic: number;
|
public payload?: Uint8Array,
|
||||||
public version: number;
|
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create Message from utf-8 string
|
* Create Message from utf-8 string
|
||||||
@ -31,39 +20,33 @@ export class Message {
|
|||||||
* @returns {Message}
|
* @returns {Message}
|
||||||
*/
|
*/
|
||||||
static fromUtf8String(message: string): Message {
|
static fromUtf8String(message: string): Message {
|
||||||
const wakuMsg = new WakuMessage();
|
const payload = Buffer.from(message, 'utf-8');
|
||||||
|
return new Message(payload, DEFAULT_CONTENT_TOPIC, DEFAULT_VERSION);
|
||||||
// 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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static fromBinary(message: Uint8Array): Message {
|
static fromBinary(bytes: Uint8Array): Message {
|
||||||
const wakuMsg = WakuMessage.deserializeBinary(message);
|
const wakuMsg = WakuMessage.decode(Reader.create(bytes));
|
||||||
return new Message(wakuMsg);
|
return new Message(wakuMsg.payload, wakuMsg.contentTopic, wakuMsg.version);
|
||||||
}
|
}
|
||||||
|
|
||||||
toBinary(): Uint8Array {
|
toBinary(): Uint8Array {
|
||||||
return this.protobuf.serializeBinary();
|
return WakuMessage.encode({
|
||||||
|
payload: this.payload,
|
||||||
|
version: this.version,
|
||||||
|
contentTopic: this.contentTopic,
|
||||||
|
}).finish();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Purely for tests purposes.
|
// Purely for tests purposes.
|
||||||
// We do consider protobuf field when checking equality
|
// We do consider protobuf field when checking equality
|
||||||
// As the content is held by the other fields.
|
// As the content is held by the other fields.
|
||||||
// TODO: Consider using WakuMessage.equals
|
|
||||||
isEqualTo(other: Message) {
|
isEqualTo(other: Message) {
|
||||||
|
const payloadsAreEqual =
|
||||||
|
this.payload && other.payload
|
||||||
|
? Buffer.compare(this.payload, other.payload) === 0
|
||||||
|
: !(this.payload || other.payload);
|
||||||
return (
|
return (
|
||||||
Buffer.compare(this.payload, other.payload) === 0 &&
|
payloadsAreEqual &&
|
||||||
this.contentTopic === other.contentTopic &&
|
this.contentTopic === other.contentTopic &&
|
||||||
this.version === other.version
|
this.version === other.version
|
||||||
);
|
);
|
||||||
|
|||||||
@ -109,7 +109,7 @@ describe('Waku Relay', () => {
|
|||||||
expect(msgs[0].version).to.equal(message.version);
|
expect(msgs[0].version).to.equal(message.version);
|
||||||
|
|
||||||
const payload = Buffer.from(msgs[0].payload);
|
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 () => {
|
it('Nim publishes to js', async () => {
|
||||||
@ -132,8 +132,8 @@ describe('Waku Relay', () => {
|
|||||||
expect(receivedMsg.contentTopic).to.eq(message.contentTopic);
|
expect(receivedMsg.contentTopic).to.eq(message.contentTopic);
|
||||||
expect(receivedMsg.version).to.eq(message.version);
|
expect(receivedMsg.version).to.eq(message.version);
|
||||||
|
|
||||||
const payload = Buffer.from(receivedMsg.payload);
|
const payload = Buffer.from(receivedMsg.payload!);
|
||||||
expect(Buffer.compare(payload, message.payload)).to.eq(0);
|
expect(Buffer.compare(payload, message.payload!)).to.eq(0);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@ -108,6 +108,10 @@ export class NimWaku {
|
|||||||
async sendMessage(message: Message) {
|
async sendMessage(message: Message) {
|
||||||
this.checkProcess();
|
this.checkProcess();
|
||||||
|
|
||||||
|
if (!message.payload) {
|
||||||
|
throw 'Attempting to send empty message';
|
||||||
|
}
|
||||||
|
|
||||||
const rpcMessage = {
|
const rpcMessage = {
|
||||||
payload: bufToHex(message.payload),
|
payload: bufToHex(message.payload),
|
||||||
contentTopic: message.contentTopic,
|
contentTopic: message.contentTopic,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user