mezon-light-sdk 1.0.2 → 1.0.3

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/dist/utils.js ADDED
@@ -0,0 +1,110 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.decodeAttachments = exports.safeJSONParse = exports.buildFetchOptions = void 0;
27
+ const tsproto = __importStar(require("./proto/api"));
28
+ function buildFetchOptions(method, options, bodyJson) {
29
+ const fetchOptions = { ...{ method: method }, ...options };
30
+ fetchOptions.headers = { ...options.headers };
31
+ if (typeof XMLHttpRequest !== "undefined") {
32
+ const descriptor = Object.getOwnPropertyDescriptor(XMLHttpRequest.prototype, "withCredentials");
33
+ // in Cocos Creator, XMLHttpRequest.withCredentials is not writable, so make the fetch
34
+ // polyfill avoid writing to it.
35
+ if (!descriptor?.set) {
36
+ fetchOptions.credentials = "cocos-ignore"; // string value is arbitrary, cannot be 'omit' or 'include
37
+ }
38
+ }
39
+ //fetchOptions.headers["connect-protocol-version"] = "1";
40
+ if (!Object.keys(fetchOptions.headers).includes("Accept")) {
41
+ fetchOptions.headers["Accept"] = "application/proto";
42
+ }
43
+ if (!Object.keys(fetchOptions.headers).includes("Content-Type")) {
44
+ fetchOptions.headers["Content-Type"] = "application/proto";
45
+ }
46
+ Object.keys(fetchOptions.headers).forEach((key) => {
47
+ if (!fetchOptions.headers[key]) {
48
+ delete fetchOptions.headers[key];
49
+ }
50
+ });
51
+ if (bodyJson) {
52
+ fetchOptions.body = bodyJson;
53
+ }
54
+ return fetchOptions;
55
+ }
56
+ exports.buildFetchOptions = buildFetchOptions;
57
+ function safeJSONParse(raw) {
58
+ if (raw === null || raw === undefined)
59
+ return { t: raw };
60
+ let jsonStr;
61
+ if (raw instanceof Uint8Array) {
62
+ jsonStr = new TextDecoder().decode(raw);
63
+ }
64
+ else if (typeof raw === "string") {
65
+ jsonStr = raw;
66
+ }
67
+ else {
68
+ return typeof raw === "object" ? raw : { t: raw };
69
+ }
70
+ if (!jsonStr || jsonStr === "" || jsonStr === "[]") {
71
+ return { t: jsonStr };
72
+ }
73
+ try {
74
+ return JSON.parse(jsonStr);
75
+ }
76
+ catch (error) {
77
+ try {
78
+ const fixedJsonStr = jsonStr.replace(/\n/g, "\\n").replace(/\r/g, "\\r");
79
+ return JSON.parse(fixedJsonStr);
80
+ }
81
+ catch (e) {
82
+ console.error("JSON Parse failed completely:", { original: jsonStr, error: e });
83
+ return { t: jsonStr };
84
+ }
85
+ }
86
+ }
87
+ exports.safeJSONParse = safeJSONParse;
88
+ const isEmpty = (data) => {
89
+ return !data || data === null || data === undefined || data === "" || data === "[]";
90
+ };
91
+ function decodeAttachments(data) {
92
+ if (isEmpty(data))
93
+ return;
94
+ // 91 is '[' (JSON Array) | 123 is '{' (JSON Object)
95
+ const firstByte = data[0];
96
+ const isJson = firstByte === 91 || firstByte === 123;
97
+ if (isJson) {
98
+ return safeJSONParse(data);
99
+ }
100
+ try {
101
+ const buffer = data;
102
+ const uintBuffer = new Uint8Array(buffer);
103
+ const attachments = tsproto.MessageAttachmentList.decode(uintBuffer);
104
+ return attachments;
105
+ }
106
+ catch (error) {
107
+ return safeJSONParse(data);
108
+ }
109
+ }
110
+ exports.decodeAttachments = decodeAttachments;
@@ -0,0 +1,69 @@
1
+ /**
2
+ * An interface used by Mezon's web socket to determine the payload protocol.
3
+ */
4
+ export interface WebSocketAdapter {
5
+ /**
6
+ * Dispatched when the web socket closes.
7
+ */
8
+ onClose: SocketCloseHandler | null;
9
+ /**
10
+ * Dispatched when the web socket receives an error.
11
+ */
12
+ onError: SocketErrorHandler | null;
13
+ /**
14
+ * Dispatched when the web socket receives a normal message.
15
+ */
16
+ onMessage: SocketMessageHandler | null;
17
+ /**
18
+ * Dispatched when the web socket opens.
19
+ */
20
+ onOpen: SocketOpenHandler | null;
21
+ isOpen(): boolean;
22
+ close(): void;
23
+ connect(scheme: string, host: string, port: string, createStatus: boolean, token: string, platform: string, signal?: AbortSignal): void;
24
+ send(message: any): void;
25
+ }
26
+ /**
27
+ * SocketCloseHandler defines a lambda that handles WebSocket close events.
28
+ */
29
+ export interface SocketCloseHandler {
30
+ (this: WebSocket, evt: CloseEvent): void;
31
+ }
32
+ /**
33
+ * SocketErrorHandler defines a lambda that handles responses from the server via WebSocket
34
+ * that indicate an error.
35
+ */
36
+ export interface SocketErrorHandler {
37
+ (this: WebSocket, evt: Event): void;
38
+ }
39
+ /**
40
+ * SocketMessageHandler defines a lambda that handles valid WebSocket messages.
41
+ */
42
+ export interface SocketMessageHandler {
43
+ (message: any): void;
44
+ }
45
+ /**
46
+ * SocketOpenHandler defines a lambda that handles WebSocket open events.
47
+ */
48
+ export interface SocketOpenHandler {
49
+ (this: WebSocket, evt: Event): void;
50
+ }
51
+ /**
52
+ * A protocol buffer socket adapter that accepts and transmits payloads using the protobuf binary wire format.
53
+ */
54
+ export declare class WebSocketAdapterPb implements WebSocketAdapter {
55
+ private _socket?;
56
+ constructor();
57
+ get onClose(): SocketCloseHandler | null;
58
+ set onClose(value: SocketCloseHandler | null);
59
+ get onError(): SocketErrorHandler | null;
60
+ set onError(value: SocketErrorHandler | null);
61
+ get onMessage(): SocketMessageHandler | null;
62
+ set onMessage(value: SocketMessageHandler | null);
63
+ get onOpen(): SocketOpenHandler | null;
64
+ set onOpen(value: SocketOpenHandler | null);
65
+ isOpen(): boolean;
66
+ close(): void;
67
+ connect(scheme: string, host: string, port: string, createStatus: boolean, token: string, platform: string, signal?: AbortSignal): void;
68
+ send(msg: any): void;
69
+ }
@@ -0,0 +1,111 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.WebSocketAdapterPb = void 0;
27
+ /**
28
+ * Copyright 2020 The Mezon Authors
29
+ *
30
+ * Licensed under the Apache License, Version 2.0 (the "License");
31
+ * you may not use this file except in compliance with the License.
32
+ * You may obtain a copy of the License at
33
+ *
34
+ * http://www.apache.org/licenses/LICENSE-2.0
35
+ *
36
+ * Unless required by applicable law or agreed to in writing, software
37
+ * distributed under the License is distributed on an "AS IS" BASIS,
38
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
39
+ * See the License for the specific language governing permissions and
40
+ * limitations under the License.
41
+ */
42
+ const tsproto = __importStar(require("./proto/realtime"));
43
+ /**
44
+ * A protocol buffer socket adapter that accepts and transmits payloads using the protobuf binary wire format.
45
+ */
46
+ class WebSocketAdapterPb {
47
+ constructor() { }
48
+ get onClose() {
49
+ return this._socket.onclose;
50
+ }
51
+ set onClose(value) {
52
+ this._socket.onclose = value;
53
+ }
54
+ get onError() {
55
+ return this._socket.onerror;
56
+ }
57
+ set onError(value) {
58
+ this._socket.onerror = value;
59
+ }
60
+ get onMessage() {
61
+ return this._socket.onmessage;
62
+ }
63
+ set onMessage(value) {
64
+ if (value) {
65
+ this._socket.onmessage = (evt) => {
66
+ const buffer = evt.data;
67
+ const uintBuffer = new Uint8Array(buffer);
68
+ const envelope = tsproto.Envelope.decode(uintBuffer);
69
+ if (envelope.channel_message) {
70
+ if (envelope.channel_message.code == undefined) {
71
+ //protobuf plugin does not default-initialize missing Int32Value fields
72
+ envelope.channel_message.code = 0;
73
+ }
74
+ }
75
+ value(envelope);
76
+ };
77
+ }
78
+ else {
79
+ value = null;
80
+ }
81
+ }
82
+ get onOpen() {
83
+ return this._socket.onopen;
84
+ }
85
+ set onOpen(value) {
86
+ this._socket.onopen = value;
87
+ }
88
+ isOpen() {
89
+ return this._socket?.readyState == WebSocket.OPEN;
90
+ }
91
+ close() {
92
+ this._socket?.close();
93
+ this._socket = undefined;
94
+ }
95
+ connect(scheme, host, port, createStatus, token, platform, signal) {
96
+ if (signal) {
97
+ signal.addEventListener("abort", () => {
98
+ this.close();
99
+ });
100
+ }
101
+ const url = `${scheme}${host}:${port}/ws?lang=en&status=${encodeURIComponent(createStatus.toString())}&token=${encodeURIComponent(token)}&format=protobuf&platform=${encodeURIComponent(platform)}`;
102
+ this._socket = new WebSocket(url);
103
+ this._socket.binaryType = "arraybuffer";
104
+ }
105
+ send(msg) {
106
+ const envelopeWriter = tsproto.Envelope.encode(tsproto.Envelope.fromPartial(msg));
107
+ const encodedMsg = envelopeWriter.finish();
108
+ this._socket.send(encodedMsg);
109
+ }
110
+ }
111
+ exports.WebSocketAdapterPb = WebSocketAdapterPb;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mezon-light-sdk",
3
- "version": "1.0.2",
4
- "description": "Lightweigth SDK for mezon chat.",
3
+ "version": "1.0.3",
4
+ "description": "lightweight SDK for mezon chat.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "files": [
@@ -12,8 +12,7 @@
12
12
  },
13
13
  "dependencies": {
14
14
  "js-base64": "^3.7.8",
15
- "mezon-js": "^2.13.58",
16
- "mezon-js-protobuf": "^1.8.66"
15
+ "protobufjs": "^8.0.0"
17
16
  },
18
17
  "author": "mezonai",
19
18
  "license": "MIT",
package/dist/message.d.ts DELETED
@@ -1,19 +0,0 @@
1
- export interface ChannelMessage {
2
- clan_id: string;
3
- channel_id: string;
4
- message_id: string;
5
- code: number;
6
- sender_id: string;
7
- username: string;
8
- avatar: string;
9
- content: any;
10
- create_time: string;
11
- update_time: string;
12
- display_name: string;
13
- mentions: any[];
14
- attachments: any[];
15
- references: any[];
16
- create_time_seconds: number;
17
- update_time_seconds: number;
18
- hide_editted: boolean;
19
- }
File without changes