mezon-js 2.7.1

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/tsconfig.json ADDED
@@ -0,0 +1,10 @@
1
+ {
2
+ "extends": "../../tsconfig.base.json",
3
+ "include": [
4
+ "./*",
5
+ ],
6
+ "compilerOptions": {
7
+ "outDir": "dist"
8
+ },
9
+ "declarationDir": "dist"
10
+ }
package/utils.ts ADDED
@@ -0,0 +1,49 @@
1
+ import {encode, decode} from "js-base64"
2
+
3
+ export function buildFetchOptions(method: string, options: any, bodyJson: string) {
4
+ const fetchOptions = {...{ method: method }, ...options};
5
+ fetchOptions.headers = {...options.headers};
6
+
7
+ if (typeof XMLHttpRequest !== "undefined") {
8
+ const descriptor = Object.getOwnPropertyDescriptor(XMLHttpRequest.prototype, "withCredentials");
9
+
10
+ // in Cocos Creator, XMLHttpRequest.withCredentials is not writable, so make the fetch
11
+ // polyfill avoid writing to it.
12
+ if (!descriptor?.set) {
13
+ fetchOptions.credentials = 'cocos-ignore'; // string value is arbitrary, cannot be 'omit' or 'include
14
+ }
15
+ }
16
+
17
+ if(!Object.keys(fetchOptions.headers).includes("Accept")) {
18
+ fetchOptions.headers["Accept"] = "application/json";
19
+ }
20
+
21
+ if(!Object.keys(fetchOptions.headers).includes("Content-Type")) {
22
+ fetchOptions.headers["Content-Type"] = "application/json";
23
+ }
24
+
25
+ Object.keys(fetchOptions.headers).forEach((key: string) => {
26
+ if (!fetchOptions.headers[key]) {
27
+ delete fetchOptions.headers[key];
28
+ }
29
+ });
30
+
31
+ if (bodyJson) {
32
+ fetchOptions.body = bodyJson;
33
+ }
34
+
35
+ return fetchOptions;
36
+ }
37
+
38
+ export function b64EncodeUnicode(str:string) {
39
+ return encode(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g,
40
+ function toSolidBytes(_match:string, p1) {
41
+ return String.fromCharCode(Number('0x' + p1));
42
+ }));
43
+ }
44
+
45
+ export function b64DecodeUnicode(str: string) {
46
+ return decodeURIComponent(decode(str).split('').map(function(c) {
47
+ return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
48
+ }).join(''));
49
+ }
@@ -0,0 +1,159 @@
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
+
17
+ import { decode, encode } from "base64-arraybuffer";
18
+ import { btoa } from "js-base64"
19
+
20
+ /**
21
+ * An interface used by Mezon's web socket to determine the payload protocol.
22
+ */
23
+ export interface WebSocketAdapter {
24
+
25
+ /**
26
+ * Dispatched when the web socket closes.
27
+ */
28
+ onClose : SocketCloseHandler | null;
29
+
30
+ /**
31
+ * Dispatched when the web socket receives an error.
32
+ */
33
+ onError : SocketErrorHandler | null;
34
+
35
+ /**
36
+ * Dispatched when the web socket receives a normal message.
37
+ */
38
+ onMessage : SocketMessageHandler | null;
39
+
40
+ /**
41
+ * Dispatched when the web socket opens.
42
+ */
43
+ onOpen : SocketOpenHandler | null;
44
+
45
+ isOpen(): boolean;
46
+ close() : void;
47
+ connect(scheme: string, host: string, port : string, createStatus: boolean, token : string) : void;
48
+ send(message: any) : void;
49
+ }
50
+
51
+ /**
52
+ * SocketCloseHandler defines a lambda that handles WebSocket close events.
53
+ */
54
+ export interface SocketCloseHandler {
55
+ (this : WebSocket, evt: CloseEvent): void;
56
+ }
57
+
58
+ /**
59
+ * SocketErrorHandler defines a lambda that handles responses from the server via WebSocket
60
+ * that indicate an error.
61
+ */
62
+ export interface SocketErrorHandler {
63
+ (this : WebSocket, evt: Event): void;
64
+ }
65
+
66
+ /**
67
+ * SocketMessageHandler defines a lambda that handles valid WebSocket messages.
68
+ */
69
+ export interface SocketMessageHandler {
70
+ (message: any): void;
71
+ }
72
+
73
+ /**
74
+ * SocketOpenHandler defines a lambda that handles WebSocket open events.
75
+ */
76
+ export interface SocketOpenHandler {
77
+ (this : WebSocket, evt : Event) : void
78
+ }
79
+
80
+ /**
81
+ * A text-based socket adapter that accepts and transmits payloads over UTF-8.
82
+ */
83
+ export class WebSocketAdapterText implements WebSocketAdapter {
84
+ private _socket?: WebSocket;
85
+
86
+ get onClose(): SocketCloseHandler | null {
87
+ return this._socket!.onclose;
88
+ }
89
+
90
+ set onClose(value: SocketCloseHandler | null) {
91
+ this._socket!.onclose = value;
92
+ }
93
+
94
+ get onError(): SocketErrorHandler | null {
95
+ return this._socket!.onerror;
96
+ }
97
+
98
+ set onError(value: SocketErrorHandler | null) {
99
+ this._socket!.onerror = value;
100
+ }
101
+
102
+ get onMessage(): SocketMessageHandler | null {
103
+ return this._socket!.onmessage;
104
+ }
105
+
106
+ set onMessage(value: SocketMessageHandler | null) {
107
+ if (value) {
108
+ this._socket!.onmessage = (evt: MessageEvent) => {
109
+ const message: any = JSON.parse(evt.data);
110
+
111
+ if (message.party_data && message.party_data.data) {
112
+ message.party_data.data = new Uint8Array(decode(message.party_data.data));
113
+ }
114
+
115
+ value!(message);
116
+ };
117
+ }
118
+ else {
119
+ value = null;
120
+ }
121
+ }
122
+
123
+ get onOpen(): SocketOpenHandler | null {
124
+ return this._socket!.onopen;
125
+ }
126
+
127
+ set onOpen(value: SocketOpenHandler | null) {
128
+ this._socket!.onopen = value;
129
+ }
130
+
131
+ isOpen(): boolean {
132
+ return this._socket?.readyState == WebSocket.OPEN;
133
+ }
134
+
135
+ connect(scheme: string, host: string, port: string, createStatus: boolean, token: string): void {
136
+ const url = `${scheme}${host}:${port}/ws?lang=en&status=${encodeURIComponent(createStatus.toString())}&token=${encodeURIComponent(token)}`;
137
+ this._socket = new WebSocket(url);
138
+ }
139
+
140
+ close() {
141
+ this._socket!.close();
142
+ this._socket = undefined;
143
+ }
144
+
145
+ send(msg: any): void {
146
+ if (msg.party_data_send) {
147
+ // according to protobuf docs, int64 is encoded to JSON as string.
148
+ msg.party_data_send.op_code = msg.party_data_send.op_code.toString();
149
+ let payload = msg.party_data_send.data;
150
+ if (payload && payload instanceof Uint8Array) {
151
+ msg.party_data_send.data = encode(payload.buffer);
152
+ } else if (payload) { // it's a string
153
+ msg.party_data_send.data = btoa(payload);
154
+ }
155
+ }
156
+
157
+ this._socket!.send(JSON.stringify(msg));
158
+ }
159
+ }