2021-03-10 16:22:49 +11:00
|
|
|
// Ensure that this class matches the proto interface while
|
2021-03-22 15:34:13 +11:00
|
|
|
import { Reader } from 'protobufjs/minimal';
|
2021-03-10 16:22:49 +11:00
|
|
|
|
2021-03-22 15:34:13 +11:00
|
|
|
// Protecting the user from protobuf oddities
|
2021-05-03 16:26:02 +10:00
|
|
|
import * as proto from '../proto/waku/v2/message';
|
2021-03-10 16:22:49 +11:00
|
|
|
|
2021-04-07 11:04:30 +10:00
|
|
|
export const DEFAULT_CONTENT_TOPIC = '/waku/2/default-content/proto';
|
2021-03-22 15:34:13 +11:00
|
|
|
const DEFAULT_VERSION = 0;
|
2021-03-10 16:22:49 +11:00
|
|
|
|
2021-04-01 11:18:35 +11:00
|
|
|
export class WakuMessage {
|
2021-05-03 16:26:02 +10:00
|
|
|
public constructor(public proto: proto.WakuMessage) {}
|
2021-04-07 11:04:30 +10:00
|
|
|
|
2021-03-12 14:23:21 +11:00
|
|
|
/**
|
2021-03-31 10:43:29 +11:00
|
|
|
* Create Message with a utf-8 string as payload
|
2021-05-03 16:26:02 +10:00
|
|
|
* @param utf8
|
|
|
|
|
* @param contentTopic
|
2021-04-01 11:18:35 +11:00
|
|
|
* @returns {WakuMessage}
|
2021-03-12 14:23:21 +11:00
|
|
|
*/
|
2021-05-03 16:26:02 +10:00
|
|
|
static fromUtf8String(
|
|
|
|
|
utf8: string,
|
|
|
|
|
contentTopic: string = DEFAULT_CONTENT_TOPIC
|
|
|
|
|
): WakuMessage {
|
|
|
|
|
const payload = Buffer.from(utf8, 'utf-8');
|
|
|
|
|
return new WakuMessage({
|
|
|
|
|
payload,
|
|
|
|
|
version: DEFAULT_VERSION,
|
|
|
|
|
contentTopic,
|
|
|
|
|
});
|
2021-03-31 10:43:29 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create Message with a byte array as payload
|
|
|
|
|
* @param payload
|
2021-04-07 11:04:30 +10:00
|
|
|
* @param contentTopic
|
2021-04-01 11:18:35 +11:00
|
|
|
* @returns {WakuMessage}
|
2021-03-31 10:43:29 +11:00
|
|
|
*/
|
2021-04-07 11:04:30 +10:00
|
|
|
static fromBytes(
|
|
|
|
|
payload: Uint8Array,
|
|
|
|
|
contentTopic: string = DEFAULT_CONTENT_TOPIC
|
|
|
|
|
): WakuMessage {
|
2021-05-03 16:26:02 +10:00
|
|
|
return new WakuMessage({
|
|
|
|
|
payload,
|
|
|
|
|
version: DEFAULT_VERSION,
|
|
|
|
|
contentTopic,
|
|
|
|
|
});
|
2021-03-10 16:22:49 +11:00
|
|
|
}
|
|
|
|
|
|
2021-04-01 11:18:35 +11:00
|
|
|
static decode(bytes: Uint8Array): WakuMessage {
|
2021-05-03 16:26:02 +10:00
|
|
|
const wakuMsg = proto.WakuMessage.decode(Reader.create(bytes));
|
|
|
|
|
return new WakuMessage(wakuMsg);
|
2021-03-10 16:22:49 +11:00
|
|
|
}
|
|
|
|
|
|
2021-05-03 16:26:02 +10:00
|
|
|
encode(): Uint8Array {
|
|
|
|
|
return proto.WakuMessage.encode(this.proto).finish();
|
2021-03-10 16:22:49 +11:00
|
|
|
}
|
|
|
|
|
|
2021-05-03 16:26:02 +10:00
|
|
|
get payloadAsUtf8(): string {
|
|
|
|
|
if (!this.proto.payload) {
|
2021-03-24 16:31:54 +11:00
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-03 16:26:02 +10:00
|
|
|
return Array.from(this.proto.payload)
|
2021-03-24 16:31:54 +11:00
|
|
|
.map((char) => {
|
|
|
|
|
return String.fromCharCode(char);
|
|
|
|
|
})
|
|
|
|
|
.join('');
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-03 16:26:02 +10:00
|
|
|
get payload(): Uint8Array | undefined {
|
|
|
|
|
return this.proto.payload;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get contentTopic(): string | undefined {
|
|
|
|
|
return this.proto.contentTopic;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get version(): number | undefined {
|
|
|
|
|
return this.proto.version;
|
2021-03-10 16:22:49 +11:00
|
|
|
}
|
|
|
|
|
}
|