mezon-js-protobuf 1.8.74 → 1.8.76

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.ts CHANGED
@@ -1 +1,2 @@
1
1
  export * from "./web_socket_adapter_pb";
2
+ export * from "./utils"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mezon-js-protobuf",
3
- "version": "1.8.74",
3
+ "version": "1.8.76",
4
4
  "description": "Websocket adapter adding protocol buffer support to the Mezon Javascript client.",
5
5
  "main": "dist/mezon-js-protobuf.cjs.js",
6
6
  "module": "dist/mezon-js-protobuf.esm.mjs",
package/rtapi/realtime.ts CHANGED
@@ -794,9 +794,9 @@ export interface ChannelMessageRemove {
794
794
  /** */
795
795
  topic_id: string;
796
796
  /** Message mention */
797
- mentions: string;
797
+ mentions: Uint8Array;
798
798
  /** Message reference */
799
- references: string;
799
+ references: Uint8Array;
800
800
  }
801
801
 
802
802
  /** A set of joins and leaves on a particular channel. */
@@ -6663,8 +6663,8 @@ function createBaseChannelMessageRemove(): ChannelMessageRemove {
6663
6663
  is_public: false,
6664
6664
  has_attachment: false,
6665
6665
  topic_id: "",
6666
- mentions: "",
6667
- references: "",
6666
+ mentions: new Uint8Array(0),
6667
+ references: new Uint8Array(0),
6668
6668
  };
6669
6669
  }
6670
6670
 
@@ -6691,11 +6691,11 @@ export const ChannelMessageRemove = {
6691
6691
  if (message.topic_id !== "") {
6692
6692
  writer.uint32(58).string(message.topic_id);
6693
6693
  }
6694
- if (message.mentions !== "") {
6695
- writer.uint32(66).string(message.mentions);
6694
+ if (message.mentions.length !== 0) {
6695
+ writer.uint32(66).bytes(message.mentions);
6696
6696
  }
6697
- if (message.references !== "") {
6698
- writer.uint32(74).string(message.references);
6697
+ if (message.references.length !== 0) {
6698
+ writer.uint32(74).bytes(message.references);
6699
6699
  }
6700
6700
  return writer;
6701
6701
  },
@@ -6761,14 +6761,14 @@ export const ChannelMessageRemove = {
6761
6761
  break;
6762
6762
  }
6763
6763
 
6764
- message.mentions = reader.string();
6764
+ message.mentions = reader.bytes();
6765
6765
  continue;
6766
6766
  case 9:
6767
6767
  if (tag !== 74) {
6768
6768
  break;
6769
6769
  }
6770
6770
 
6771
- message.references = reader.string();
6771
+ message.references = reader.bytes();
6772
6772
  continue;
6773
6773
  }
6774
6774
  if ((tag & 7) === 4 || tag === 0) {
@@ -6788,8 +6788,8 @@ export const ChannelMessageRemove = {
6788
6788
  is_public: isSet(object.is_public) ? globalThis.Boolean(object.is_public) : false,
6789
6789
  has_attachment: isSet(object.has_attachment) ? globalThis.Boolean(object.has_attachment) : false,
6790
6790
  topic_id: isSet(object.topic_id) ? globalThis.String(object.topic_id) : "",
6791
- mentions: isSet(object.mentions) ? globalThis.String(object.mentions) : "",
6792
- references: isSet(object.references) ? globalThis.String(object.references) : "",
6791
+ mentions: isSet(object.mentions) ? bytesFromBase64(object.mentions) : new Uint8Array(0),
6792
+ references: isSet(object.references) ? bytesFromBase64(object.references) : new Uint8Array(0),
6793
6793
  };
6794
6794
  },
6795
6795
 
@@ -6816,11 +6816,11 @@ export const ChannelMessageRemove = {
6816
6816
  if (message.topic_id !== "") {
6817
6817
  obj.topic_id = message.topic_id;
6818
6818
  }
6819
- if (message.mentions !== "") {
6820
- obj.mentions = message.mentions;
6819
+ if (message.mentions.length !== 0) {
6820
+ obj.mentions = base64FromBytes(message.mentions);
6821
6821
  }
6822
- if (message.references !== "") {
6823
- obj.references = message.references;
6822
+ if (message.references.length !== 0) {
6823
+ obj.references = base64FromBytes(message.references);
6824
6824
  }
6825
6825
  return obj;
6826
6826
  },
@@ -6837,8 +6837,8 @@ export const ChannelMessageRemove = {
6837
6837
  message.is_public = object.is_public ?? false;
6838
6838
  message.has_attachment = object.has_attachment ?? false;
6839
6839
  message.topic_id = object.topic_id ?? "";
6840
- message.mentions = object.mentions ?? "";
6841
- message.references = object.references ?? "";
6840
+ message.mentions = object.mentions ?? new Uint8Array(0);
6841
+ message.references = object.references ?? new Uint8Array(0);
6842
6842
  return message;
6843
6843
  },
6844
6844
  };
@@ -16583,6 +16583,31 @@ export const FcmDataPayload = {
16583
16583
  },
16584
16584
  };
16585
16585
 
16586
+ function bytesFromBase64(b64: string): Uint8Array {
16587
+ if ((globalThis as any).Buffer) {
16588
+ return Uint8Array.from(globalThis.Buffer.from(b64, "base64"));
16589
+ } else {
16590
+ const bin = globalThis.atob(b64);
16591
+ const arr = new Uint8Array(bin.length);
16592
+ for (let i = 0; i < bin.length; ++i) {
16593
+ arr[i] = bin.charCodeAt(i);
16594
+ }
16595
+ return arr;
16596
+ }
16597
+ }
16598
+
16599
+ function base64FromBytes(arr: Uint8Array): string {
16600
+ if ((globalThis as any).Buffer) {
16601
+ return globalThis.Buffer.from(arr).toString("base64");
16602
+ } else {
16603
+ const bin: string[] = [];
16604
+ arr.forEach((byte) => {
16605
+ bin.push(globalThis.String.fromCharCode(byte));
16606
+ });
16607
+ return globalThis.btoa(bin.join(""));
16608
+ }
16609
+ }
16610
+
16586
16611
  type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined;
16587
16612
 
16588
16613
  export type DeepPartial<T> = T extends Builtin ? T
package/utils.ts ADDED
@@ -0,0 +1,33 @@
1
+ import * as tsproto from "./api/api";
2
+
3
+ export function decodeMentions(data: any) {
4
+ const buffer: ArrayBuffer = data;
5
+ const uintBuffer: Uint8Array = new Uint8Array(buffer);
6
+ const mentions = tsproto.MessageMentionList.decode(uintBuffer);
7
+
8
+ return mentions
9
+ }
10
+
11
+ export function decodeAttachments(data: any) {
12
+ const buffer: ArrayBuffer = data;
13
+ const uintBuffer: Uint8Array = new Uint8Array(buffer);
14
+ const attachments = tsproto.MessageAttachmentList.decode(uintBuffer);
15
+
16
+ return attachments
17
+ }
18
+
19
+ export function decodeRefs(data: any) {
20
+ const buffer: ArrayBuffer = data;
21
+ const uintBuffer: Uint8Array = new Uint8Array(buffer);
22
+ const refs = tsproto.MessageRefList.decode(uintBuffer);
23
+
24
+ return refs
25
+ }
26
+
27
+ export function decodeNotificationFcm(data: any) {
28
+ const buffer: ArrayBuffer = data;
29
+ const uintBuffer: Uint8Array = new Uint8Array(buffer);
30
+ const noti = tsproto.DirectFcmProto.decode(uintBuffer);
31
+
32
+ return noti
33
+ }
@@ -1,137 +1,123 @@
1
- /**
2
- * Copyright 2020 The Mezon Authors
3
- *
4
- * Licensed under the Apache License, Version 2.0 (the "License");
5
- * you may not use this file except in compliance with the License.
6
- * You may obtain a copy of the License at
7
- *
8
- * http://www.apache.org/licenses/LICENSE-2.0
9
- *
10
- * Unless required by applicable law or agreed to in writing, software
11
- * distributed under the License is distributed on an "AS IS" BASIS,
12
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- * See the License for the specific language governing permissions and
14
- * limitations under the License.
15
- */
16
- import {
17
- WebSocketAdapter,
18
- SocketCloseHandler,
19
- SocketErrorHandler,
20
- SocketMessageHandler,
21
- SocketOpenHandler,
22
- } from "../mezon-js/web_socket_adapter";
23
- import * as tsproto from "./rtapi/realtime";
24
-
25
- /**
26
- * A protocol buffer socket adapter that accepts and transmits payloads using the protobuf binary wire format.
27
- */
28
- export class WebSocketAdapterPb implements WebSocketAdapter {
29
- private _socket?: WebSocket;
30
-
31
- constructor() {
32
- }
33
-
34
- get onClose(): SocketCloseHandler | null {
35
- return this._socket!.onclose;
36
- }
37
-
38
- set onClose(value: SocketCloseHandler | null) {
39
- this._socket!.onclose = value;
40
- }
41
-
42
- get onError(): SocketErrorHandler | null {
43
- return this._socket!.onerror;
44
- }
45
-
46
- set onError(value: SocketErrorHandler | null) {
47
- this._socket!.onerror = value;
48
- }
49
-
50
- get onMessage(): SocketMessageHandler | null {
51
- return this._socket!.onmessage;
52
- }
53
-
54
- set onMessage(value: SocketMessageHandler | null) {
55
- if (value) {
56
- this._socket!.onmessage = (evt: MessageEvent) => {
57
- const buffer: ArrayBuffer = evt.data;
58
- const uintBuffer: Uint8Array = new Uint8Array(buffer);
59
- const envelope = tsproto.Envelope.decode(uintBuffer);
60
-
61
- if (envelope.channel_message) {
62
- if (envelope.channel_message.code == undefined) {
63
- //protobuf plugin does not default-initialize missing Int32Value fields
64
- envelope.channel_message.code = 0;
65
- }
66
- }
67
-
68
- value!(envelope);
69
- };
70
- }
71
- else {
72
- value = null;
73
- }
74
- }
75
-
76
- get onOpen(): SocketOpenHandler | null {
77
- return this._socket!.onopen;
78
- }
79
-
80
- set onOpen(value: SocketOpenHandler | null) {
81
- this._socket!.onopen = value;
82
- }
83
-
84
- isOpen(): boolean {
85
- return this._socket?.readyState == WebSocket.OPEN;
86
- }
87
-
88
- close() {
89
- this._socket?.close();
90
- this._socket = undefined;
91
- }
92
-
93
- connect(
94
- scheme: string,
95
- host: string,
96
- port: string,
97
- createStatus: boolean,
98
- token: string,
99
- platform: string,
100
- signal?: AbortSignal
101
- ): void {
102
- if (signal) {
103
- signal.addEventListener("abort", () => {
104
- this.close();
105
- });
106
- }
107
- const url = `${scheme}${host}:${port}/ws?lang=en&status=${encodeURIComponent(
108
- createStatus.toString()
109
- )}&token=${encodeURIComponent(
110
- token
111
- )}&format=protobuf&platform=${encodeURIComponent(platform)}`;
112
- this._socket = new WebSocket(url);
113
- this._socket.binaryType = "arraybuffer";
114
- }
115
-
116
- send(msg: any): void {
117
- if (msg.match_data_send) {
118
- let payload = msg.match_data_send.data;
119
- // can't send a string over protobuf
120
- if (typeof payload == "string") {
121
- msg.match_data_send.data = new TextEncoder().encode(payload);
122
- }
123
- } else if (msg.party_data_send) {
124
- let payload = msg.party_data_send.data;
125
- // can't send a string over protobuf
126
- if (typeof payload == "string") {
127
- msg.party_data_send.data = new TextEncoder().encode(payload);
128
- }
129
- }
130
-
131
- const envelopeWriter = tsproto.Envelope.encode(
132
- tsproto.Envelope.fromPartial(msg)
133
- );
134
- const encodedMsg = envelopeWriter.finish();
135
- this._socket!.send(encodedMsg);
136
- }
137
- }
1
+ /**
2
+ * Copyright 2020 The Mezon Authors
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ import {
17
+ WebSocketAdapter,
18
+ SocketCloseHandler,
19
+ SocketErrorHandler,
20
+ SocketMessageHandler,
21
+ SocketOpenHandler,
22
+ } from "../mezon-js/web_socket_adapter";
23
+ import * as tsproto from "./rtapi/realtime";
24
+
25
+ /**
26
+ * A protocol buffer socket adapter that accepts and transmits payloads using the protobuf binary wire format.
27
+ */
28
+ export class WebSocketAdapterPb implements WebSocketAdapter {
29
+ private _socket?: WebSocket;
30
+
31
+ constructor() {
32
+ }
33
+
34
+ get onClose(): SocketCloseHandler | null {
35
+ return this._socket!.onclose;
36
+ }
37
+
38
+ set onClose(value: SocketCloseHandler | null) {
39
+ this._socket!.onclose = value;
40
+ }
41
+
42
+ get onError(): SocketErrorHandler | null {
43
+ return this._socket!.onerror;
44
+ }
45
+
46
+ set onError(value: SocketErrorHandler | null) {
47
+ this._socket!.onerror = value;
48
+ }
49
+
50
+ get onMessage(): SocketMessageHandler | null {
51
+ return this._socket!.onmessage;
52
+ }
53
+
54
+ set onMessage(value: SocketMessageHandler | null) {
55
+ if (value) {
56
+ this._socket!.onmessage = (evt: MessageEvent) => {
57
+ const buffer: ArrayBuffer = evt.data;
58
+ const uintBuffer: Uint8Array = new Uint8Array(buffer);
59
+ const envelope = tsproto.Envelope.decode(uintBuffer);
60
+
61
+ if (envelope.channel_message) {
62
+ if (envelope.channel_message.code == undefined) {
63
+ //protobuf plugin does not default-initialize missing Int32Value fields
64
+ envelope.channel_message.code = 0;
65
+ }
66
+ }
67
+
68
+ value!(envelope);
69
+ };
70
+ }
71
+ else {
72
+ value = null;
73
+ }
74
+ }
75
+
76
+ get onOpen(): SocketOpenHandler | null {
77
+ return this._socket!.onopen;
78
+ }
79
+
80
+ set onOpen(value: SocketOpenHandler | null) {
81
+ this._socket!.onopen = value;
82
+ }
83
+
84
+ isOpen(): boolean {
85
+ return this._socket?.readyState == WebSocket.OPEN;
86
+ }
87
+
88
+ close() {
89
+ this._socket?.close();
90
+ this._socket = undefined;
91
+ }
92
+
93
+ connect(
94
+ scheme: string,
95
+ host: string,
96
+ port: string,
97
+ createStatus: boolean,
98
+ token: string,
99
+ platform: string,
100
+ signal?: AbortSignal
101
+ ): void {
102
+ if (signal) {
103
+ signal.addEventListener("abort", () => {
104
+ this.close();
105
+ });
106
+ }
107
+ const url = `${scheme}${host}:${port}/ws?lang=en&status=${encodeURIComponent(
108
+ createStatus.toString()
109
+ )}&token=${encodeURIComponent(
110
+ token
111
+ )}&format=protobuf&platform=${encodeURIComponent(platform)}`;
112
+ this._socket = new WebSocket(url);
113
+ this._socket.binaryType = "arraybuffer";
114
+ }
115
+
116
+ send(msg: any): void {
117
+ const envelopeWriter = tsproto.Envelope.encode(
118
+ tsproto.Envelope.fromPartial(msg)
119
+ );
120
+ const encodedMsg = envelopeWriter.finish();
121
+ this._socket!.send(encodedMsg);
122
+ }
123
+ }