mezon-js 2.9.94 → 2.9.96

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/client.ts CHANGED
@@ -160,6 +160,7 @@ import {
160
160
 
161
161
  import { Session } from "./session";
162
162
  import { DefaultSocket, Socket } from "./socket";
163
+ import { safeJSONParse } from "./utils";
163
164
  import { WebSocketAdapter, WebSocketAdapterText } from "./web_socket_adapter";
164
165
 
165
166
  const DEFAULT_HOST = "127.0.0.1";
@@ -1346,7 +1347,7 @@ export class Client {
1346
1347
  timezone: u.timezone,
1347
1348
  update_time: u.update_time,
1348
1349
  username: u.username,
1349
- metadata: u.metadata ? JSON.parse(u.metadata) : undefined,
1350
+ metadata: u.metadata ? safeJSONParse(u.metadata) : undefined,
1350
1351
  });
1351
1352
  });
1352
1353
  return Promise.resolve(result);
@@ -1434,27 +1435,27 @@ export class Client {
1434
1435
  response.messages!.forEach((m) => {
1435
1436
  var content, reactions, mentions, attachments, references;
1436
1437
  try {
1437
- content = JSON.parse(m.content);
1438
+ content = safeJSONParse(m.content);
1438
1439
  } catch (e) {
1439
1440
  console.log("error parse content", e);
1440
1441
  }
1441
1442
  try {
1442
- reactions = JSON.parse(m.reactions || "[]");
1443
+ reactions = safeJSONParse(m.reactions || "[]");
1443
1444
  } catch (e) {
1444
1445
  console.log("error parse reactions", e);
1445
1446
  }
1446
1447
  try {
1447
- mentions = JSON.parse(m.mentions || "[]");
1448
+ mentions = safeJSONParse(m.mentions || "[]");
1448
1449
  } catch (e) {
1449
1450
  console.log("error parse mentions", e);
1450
1451
  }
1451
1452
  try {
1452
- attachments = JSON.parse(m.attachments || "[]");
1453
+ attachments = safeJSONParse(m.attachments || "[]");
1453
1454
  } catch (e) {
1454
1455
  console.log("error parse attachments", e);
1455
1456
  }
1456
1457
  try {
1457
- references = JSON.parse(m.references || "[]");
1458
+ references = safeJSONParse(m.references || "[]");
1458
1459
  } catch (e) {
1459
1460
  console.log("error parse references", e);
1460
1461
  }
@@ -1689,7 +1690,7 @@ export class Client {
1689
1690
  update_time: gu.user!.update_time,
1690
1691
  username: gu.user!.username,
1691
1692
  metadata: gu.user!.metadata
1692
- ? JSON.parse(gu.user!.metadata!)
1693
+ ? safeJSONParse(gu.user!.metadata!)
1693
1694
  : undefined,
1694
1695
  },
1695
1696
  role_id: gu!.role_id,
@@ -2208,7 +2209,7 @@ export class Client {
2208
2209
  username: f.user!.username,
2209
2210
  is_mobile: f.user?.is_mobile,
2210
2211
  metadata: f.user!.metadata
2211
- ? JSON.parse(f.user!.metadata!)
2212
+ ? safeJSONParse(f.user!.metadata!)
2212
2213
  : undefined,
2213
2214
  },
2214
2215
  state: f.state,
@@ -2253,7 +2254,7 @@ export class Client {
2253
2254
  persistent: n.persistent,
2254
2255
  sender_id: n.sender_id,
2255
2256
  subject: n.subject,
2256
- content: n.content ? JSON.parse(n.content) : undefined,
2257
+ content: n.content ? safeJSONParse(n.content) : undefined,
2257
2258
  });
2258
2259
  });
2259
2260
  return Promise.resolve(result);
@@ -2287,7 +2288,7 @@ export class Client {
2287
2288
  .then((response: ApiRpc) => {
2288
2289
  return Promise.resolve({
2289
2290
  id: response.id,
2290
- payload: !response.payload ? undefined : JSON.parse(response.payload),
2291
+ payload: !response.payload ? undefined : safeJSONParse(response.payload),
2291
2292
  });
2292
2293
  });
2293
2294
  }
@@ -2303,7 +2304,7 @@ export class Client {
2303
2304
  .then((response: ApiRpc) => {
2304
2305
  return Promise.resolve({
2305
2306
  id: response.id,
2306
- payload: !response.payload ? undefined : JSON.parse(response.payload),
2307
+ payload: !response.payload ? undefined : safeJSONParse(response.payload),
2307
2308
  });
2308
2309
  })
2309
2310
  .catch((err: any) => {
package/dist/index.d.ts CHANGED
@@ -18,6 +18,7 @@ export * from "./client";
18
18
  export * from "./session";
19
19
  export * from "./socket";
20
20
  export * from "./web_socket_adapter";
21
+ export * from "./utils";
21
22
  /**
22
23
  * Reexported due to duplicate definition of ChannelMessage in [Client]{@link ./client.ts} and [Session]{@link ./session.ts}
23
24
  */
@@ -61,7 +61,11 @@ __export(mezon_js_exports, {
61
61
  NotificationType: () => NotificationType,
62
62
  Session: () => Session,
63
63
  WebSocketAdapterText: () => WebSocketAdapterText,
64
- WebrtcSignalingType: () => WebrtcSignalingType
64
+ WebrtcSignalingType: () => WebrtcSignalingType,
65
+ b64DecodeUnicode: () => b64DecodeUnicode,
66
+ b64EncodeUnicode: () => b64EncodeUnicode,
67
+ buildFetchOptions: () => buildFetchOptions,
68
+ safeJSONParse: () => safeJSONParse
65
69
  });
66
70
  module.exports = __toCommonJS(mezon_js_exports);
67
71
 
@@ -640,6 +644,19 @@ var re_utob = /[\uD800-\uDBFF][\uDC00-\uDFFFF]|[^\x00-\x7F]/g;
640
644
  var utob = (u) => u.replace(re_utob, cb_utob);
641
645
  var _encode = _hasBuffer ? (s) => Buffer.from(s, "utf8").toString("base64") : _TE ? (s) => _fromUint8Array(_TE.encode(s)) : (s) => _btoa(utob(s));
642
646
  var encode = (src, urlsafe = false) => urlsafe ? _mkUriSafe(_encode(src)) : _encode(src);
647
+ var re_btou = /[\xC0-\xDF][\x80-\xBF]|[\xE0-\xEF][\x80-\xBF]{2}|[\xF0-\xF7][\x80-\xBF]{3}/g;
648
+ var cb_btou = (cccc) => {
649
+ switch (cccc.length) {
650
+ case 4:
651
+ var cp = (7 & cccc.charCodeAt(0)) << 18 | (63 & cccc.charCodeAt(1)) << 12 | (63 & cccc.charCodeAt(2)) << 6 | 63 & cccc.charCodeAt(3), offset = cp - 65536;
652
+ return _fromCC((offset >>> 10) + 55296) + _fromCC((offset & 1023) + 56320);
653
+ case 3:
654
+ return _fromCC((15 & cccc.charCodeAt(0)) << 12 | (63 & cccc.charCodeAt(1)) << 6 | 63 & cccc.charCodeAt(2));
655
+ default:
656
+ return _fromCC((31 & cccc.charCodeAt(0)) << 6 | 63 & cccc.charCodeAt(1));
657
+ }
658
+ };
659
+ var btou = (b) => b.replace(re_btou, cb_btou);
643
660
  var atobPolyfill = (asc) => {
644
661
  asc = asc.replace(/\s+/g, "");
645
662
  if (!b64re.test(asc))
@@ -653,6 +670,10 @@ var atobPolyfill = (asc) => {
653
670
  return bin;
654
671
  };
655
672
  var _atob = typeof atob === "function" ? (asc) => atob(_tidyB64(asc)) : _hasBuffer ? (asc) => Buffer.from(asc, "base64").toString("binary") : atobPolyfill;
673
+ var _toUint8Array = _hasBuffer ? (a) => _U8Afrom(Buffer.from(a, "base64")) : (a) => _U8Afrom(_atob(a).split("").map((c) => c.charCodeAt(0)));
674
+ var _decode = _hasBuffer ? (a) => Buffer.from(a, "base64").toString("utf8") : _TD ? (a) => _TD.decode(_toUint8Array(a)) : (a) => btou(_atob(a));
675
+ var _unURI = (a) => _tidyB64(a.replace(/[-_]/g, (m0) => m0 == "-" ? "+" : "/"));
676
+ var decode2 = (src) => _decode(_unURI(src));
656
677
 
657
678
  // utils.ts
658
679
  function buildFetchOptions(method, options, bodyJson) {
@@ -680,6 +701,40 @@ function buildFetchOptions(method, options, bodyJson) {
680
701
  }
681
702
  return fetchOptions;
682
703
  }
704
+ function b64EncodeUnicode(str) {
705
+ return encode(encodeURIComponent(str).replace(
706
+ /%([0-9A-F]{2})/g,
707
+ function toSolidBytes(_match, p1) {
708
+ return String.fromCharCode(Number("0x" + p1));
709
+ }
710
+ ));
711
+ }
712
+ function b64DecodeUnicode(str) {
713
+ return decodeURIComponent(decode2(str).split("").map(function(c) {
714
+ return "%" + ("00" + c.charCodeAt(0).toString(16)).slice(-2);
715
+ }).join(""));
716
+ }
717
+ function safeJSONParse(jsonString) {
718
+ try {
719
+ const unescapedJSON = jsonString.replace(/\\./g, (match) => {
720
+ switch (match) {
721
+ case '\\"':
722
+ return '"';
723
+ case "\\n":
724
+ return "\n";
725
+ case "\\t":
726
+ return " ";
727
+ default:
728
+ return match[1];
729
+ }
730
+ });
731
+ const parsedData = JSON.parse(unescapedJSON);
732
+ return parsedData;
733
+ } catch (error) {
734
+ console.error("Error parsing JSON:", error);
735
+ return null;
736
+ }
737
+ }
683
738
 
684
739
  // api.gen.ts
685
740
  var MezonApi = class {
@@ -6842,7 +6897,7 @@ var encode2 = function(arraybuffer) {
6842
6897
  }
6843
6898
  return base64;
6844
6899
  };
6845
- var decode2 = function(base64) {
6900
+ var decode3 = function(base64) {
6846
6901
  var bufferLength = base64.length * 0.75, len = base64.length, i, p = 0, encoded1, encoded2, encoded3, encoded4;
6847
6902
  if (base64[base64.length - 1] === "=") {
6848
6903
  bufferLength--;
@@ -6883,10 +6938,10 @@ var WebSocketAdapterText = class {
6883
6938
  set onMessage(value) {
6884
6939
  if (value) {
6885
6940
  this._socket.onmessage = (evt) => {
6886
- const message = JSON.parse(evt.data);
6941
+ const message = safeJSONParse(evt.data);
6887
6942
  if (message.party_data && message.party_data.data) {
6888
6943
  message.party_data.data = new Uint8Array(
6889
- decode2(message.party_data.data)
6944
+ decode3(message.party_data.data)
6890
6945
  );
6891
6946
  }
6892
6947
  value(message);
@@ -6990,7 +7045,7 @@ var _DefaultSocket = class _DefaultSocket {
6990
7045
  if (!message.cid) {
6991
7046
  if (message.notifications) {
6992
7047
  message.notifications.notifications.forEach((n) => {
6993
- n.content = n.content ? JSON.parse(n.content) : void 0;
7048
+ n.content = n.content ? safeJSONParse(n.content) : void 0;
6994
7049
  this.onnotification(n);
6995
7050
  });
6996
7051
  } else if (message.voice_started_event) {
@@ -7038,27 +7093,27 @@ var _DefaultSocket = class _DefaultSocket {
7038
7093
  } else if (message.channel_message) {
7039
7094
  var content, reactions, mentions, attachments, references;
7040
7095
  try {
7041
- content = JSON.parse(message.channel_message.content);
7096
+ content = safeJSONParse(message.channel_message.content);
7042
7097
  } catch (e2) {
7043
7098
  console.log("content is invalid", e2);
7044
7099
  }
7045
7100
  try {
7046
- reactions = JSON.parse(message.channel_message.reactions);
7101
+ reactions = safeJSONParse(message.channel_message.reactions);
7047
7102
  } catch (e2) {
7048
7103
  console.log("reactions is invalid", e2);
7049
7104
  }
7050
7105
  try {
7051
- mentions = JSON.parse(message.channel_message.mentions);
7106
+ mentions = safeJSONParse(message.channel_message.mentions);
7052
7107
  } catch (e2) {
7053
7108
  console.log("mentions is invalid", e2);
7054
7109
  }
7055
7110
  try {
7056
- attachments = JSON.parse(message.channel_message.attachments);
7111
+ attachments = safeJSONParse(message.channel_message.attachments);
7057
7112
  } catch (e2) {
7058
7113
  console.log("attachments is invalid", e2);
7059
7114
  }
7060
7115
  try {
7061
- references = JSON.parse(message.channel_message.references);
7116
+ references = safeJSONParse(message.channel_message.references);
7062
7117
  } catch (e2) {
7063
7118
  console.log("references is invalid", e2);
7064
7119
  }
@@ -8383,7 +8438,7 @@ var Client = class {
8383
8438
  timezone: u.timezone,
8384
8439
  update_time: u.update_time,
8385
8440
  username: u.username,
8386
- metadata: u.metadata ? JSON.parse(u.metadata) : void 0
8441
+ metadata: u.metadata ? safeJSONParse(u.metadata) : void 0
8387
8442
  });
8388
8443
  });
8389
8444
  return Promise.resolve(result);
@@ -8437,27 +8492,27 @@ var Client = class {
8437
8492
  response.messages.forEach((m) => {
8438
8493
  var content, reactions, mentions, attachments, references;
8439
8494
  try {
8440
- content = JSON.parse(m.content);
8495
+ content = safeJSONParse(m.content);
8441
8496
  } catch (e) {
8442
8497
  console.log("error parse content", e);
8443
8498
  }
8444
8499
  try {
8445
- reactions = JSON.parse(m.reactions || "[]");
8500
+ reactions = safeJSONParse(m.reactions || "[]");
8446
8501
  } catch (e) {
8447
8502
  console.log("error parse reactions", e);
8448
8503
  }
8449
8504
  try {
8450
- mentions = JSON.parse(m.mentions || "[]");
8505
+ mentions = safeJSONParse(m.mentions || "[]");
8451
8506
  } catch (e) {
8452
8507
  console.log("error parse mentions", e);
8453
8508
  }
8454
8509
  try {
8455
- attachments = JSON.parse(m.attachments || "[]");
8510
+ attachments = safeJSONParse(m.attachments || "[]");
8456
8511
  } catch (e) {
8457
8512
  console.log("error parse attachments", e);
8458
8513
  }
8459
8514
  try {
8460
- references = JSON.parse(m.references || "[]");
8515
+ references = safeJSONParse(m.references || "[]");
8461
8516
  } catch (e) {
8462
8517
  console.log("error parse references", e);
8463
8518
  }
@@ -8633,7 +8688,7 @@ var Client = class {
8633
8688
  timezone: gu.user.timezone,
8634
8689
  update_time: gu.user.update_time,
8635
8690
  username: gu.user.username,
8636
- metadata: gu.user.metadata ? JSON.parse(gu.user.metadata) : void 0
8691
+ metadata: gu.user.metadata ? safeJSONParse(gu.user.metadata) : void 0
8637
8692
  },
8638
8693
  role_id: gu.role_id,
8639
8694
  clan_nick: gu.clan_nick,
@@ -8938,7 +8993,7 @@ var Client = class {
8938
8993
  update_time: f.user.update_time,
8939
8994
  username: f.user.username,
8940
8995
  is_mobile: (_a = f.user) == null ? void 0 : _a.is_mobile,
8941
- metadata: f.user.metadata ? JSON.parse(f.user.metadata) : void 0
8996
+ metadata: f.user.metadata ? safeJSONParse(f.user.metadata) : void 0
8942
8997
  },
8943
8998
  state: f.state
8944
8999
  });
@@ -8969,7 +9024,7 @@ var Client = class {
8969
9024
  persistent: n.persistent,
8970
9025
  sender_id: n.sender_id,
8971
9026
  subject: n.subject,
8972
- content: n.content ? JSON.parse(n.content) : void 0
9027
+ content: n.content ? safeJSONParse(n.content) : void 0
8973
9028
  });
8974
9029
  });
8975
9030
  return Promise.resolve(result);
@@ -8991,7 +9046,7 @@ var Client = class {
8991
9046
  ).then((response) => {
8992
9047
  return Promise.resolve({
8993
9048
  id: response.id,
8994
- payload: !response.payload ? void 0 : JSON.parse(response.payload)
9049
+ payload: !response.payload ? void 0 : safeJSONParse(response.payload)
8995
9050
  });
8996
9051
  });
8997
9052
  });
@@ -9002,7 +9057,7 @@ var Client = class {
9002
9057
  return this.apiClient.rpcFunc2("", id, input && JSON.stringify(input) || "", httpKey).then((response) => {
9003
9058
  return Promise.resolve({
9004
9059
  id: response.id,
9005
- payload: !response.payload ? void 0 : JSON.parse(response.payload)
9060
+ payload: !response.payload ? void 0 : safeJSONParse(response.payload)
9006
9061
  });
9007
9062
  }).catch((err) => {
9008
9063
  throw err;
@@ -610,6 +610,19 @@ var re_utob = /[\uD800-\uDBFF][\uDC00-\uDFFFF]|[^\x00-\x7F]/g;
610
610
  var utob = (u) => u.replace(re_utob, cb_utob);
611
611
  var _encode = _hasBuffer ? (s) => Buffer.from(s, "utf8").toString("base64") : _TE ? (s) => _fromUint8Array(_TE.encode(s)) : (s) => _btoa(utob(s));
612
612
  var encode = (src, urlsafe = false) => urlsafe ? _mkUriSafe(_encode(src)) : _encode(src);
613
+ var re_btou = /[\xC0-\xDF][\x80-\xBF]|[\xE0-\xEF][\x80-\xBF]{2}|[\xF0-\xF7][\x80-\xBF]{3}/g;
614
+ var cb_btou = (cccc) => {
615
+ switch (cccc.length) {
616
+ case 4:
617
+ var cp = (7 & cccc.charCodeAt(0)) << 18 | (63 & cccc.charCodeAt(1)) << 12 | (63 & cccc.charCodeAt(2)) << 6 | 63 & cccc.charCodeAt(3), offset = cp - 65536;
618
+ return _fromCC((offset >>> 10) + 55296) + _fromCC((offset & 1023) + 56320);
619
+ case 3:
620
+ return _fromCC((15 & cccc.charCodeAt(0)) << 12 | (63 & cccc.charCodeAt(1)) << 6 | 63 & cccc.charCodeAt(2));
621
+ default:
622
+ return _fromCC((31 & cccc.charCodeAt(0)) << 6 | 63 & cccc.charCodeAt(1));
623
+ }
624
+ };
625
+ var btou = (b) => b.replace(re_btou, cb_btou);
613
626
  var atobPolyfill = (asc) => {
614
627
  asc = asc.replace(/\s+/g, "");
615
628
  if (!b64re.test(asc))
@@ -623,6 +636,10 @@ var atobPolyfill = (asc) => {
623
636
  return bin;
624
637
  };
625
638
  var _atob = typeof atob === "function" ? (asc) => atob(_tidyB64(asc)) : _hasBuffer ? (asc) => Buffer.from(asc, "base64").toString("binary") : atobPolyfill;
639
+ var _toUint8Array = _hasBuffer ? (a) => _U8Afrom(Buffer.from(a, "base64")) : (a) => _U8Afrom(_atob(a).split("").map((c) => c.charCodeAt(0)));
640
+ var _decode = _hasBuffer ? (a) => Buffer.from(a, "base64").toString("utf8") : _TD ? (a) => _TD.decode(_toUint8Array(a)) : (a) => btou(_atob(a));
641
+ var _unURI = (a) => _tidyB64(a.replace(/[-_]/g, (m0) => m0 == "-" ? "+" : "/"));
642
+ var decode2 = (src) => _decode(_unURI(src));
626
643
 
627
644
  // utils.ts
628
645
  function buildFetchOptions(method, options, bodyJson) {
@@ -650,6 +667,40 @@ function buildFetchOptions(method, options, bodyJson) {
650
667
  }
651
668
  return fetchOptions;
652
669
  }
670
+ function b64EncodeUnicode(str) {
671
+ return encode(encodeURIComponent(str).replace(
672
+ /%([0-9A-F]{2})/g,
673
+ function toSolidBytes(_match, p1) {
674
+ return String.fromCharCode(Number("0x" + p1));
675
+ }
676
+ ));
677
+ }
678
+ function b64DecodeUnicode(str) {
679
+ return decodeURIComponent(decode2(str).split("").map(function(c) {
680
+ return "%" + ("00" + c.charCodeAt(0).toString(16)).slice(-2);
681
+ }).join(""));
682
+ }
683
+ function safeJSONParse(jsonString) {
684
+ try {
685
+ const unescapedJSON = jsonString.replace(/\\./g, (match) => {
686
+ switch (match) {
687
+ case '\\"':
688
+ return '"';
689
+ case "\\n":
690
+ return "\n";
691
+ case "\\t":
692
+ return " ";
693
+ default:
694
+ return match[1];
695
+ }
696
+ });
697
+ const parsedData = JSON.parse(unescapedJSON);
698
+ return parsedData;
699
+ } catch (error) {
700
+ console.error("Error parsing JSON:", error);
701
+ return null;
702
+ }
703
+ }
653
704
 
654
705
  // api.gen.ts
655
706
  var MezonApi = class {
@@ -6812,7 +6863,7 @@ var encode2 = function(arraybuffer) {
6812
6863
  }
6813
6864
  return base64;
6814
6865
  };
6815
- var decode2 = function(base64) {
6866
+ var decode3 = function(base64) {
6816
6867
  var bufferLength = base64.length * 0.75, len = base64.length, i, p = 0, encoded1, encoded2, encoded3, encoded4;
6817
6868
  if (base64[base64.length - 1] === "=") {
6818
6869
  bufferLength--;
@@ -6853,10 +6904,10 @@ var WebSocketAdapterText = class {
6853
6904
  set onMessage(value) {
6854
6905
  if (value) {
6855
6906
  this._socket.onmessage = (evt) => {
6856
- const message = JSON.parse(evt.data);
6907
+ const message = safeJSONParse(evt.data);
6857
6908
  if (message.party_data && message.party_data.data) {
6858
6909
  message.party_data.data = new Uint8Array(
6859
- decode2(message.party_data.data)
6910
+ decode3(message.party_data.data)
6860
6911
  );
6861
6912
  }
6862
6913
  value(message);
@@ -6960,7 +7011,7 @@ var _DefaultSocket = class _DefaultSocket {
6960
7011
  if (!message.cid) {
6961
7012
  if (message.notifications) {
6962
7013
  message.notifications.notifications.forEach((n) => {
6963
- n.content = n.content ? JSON.parse(n.content) : void 0;
7014
+ n.content = n.content ? safeJSONParse(n.content) : void 0;
6964
7015
  this.onnotification(n);
6965
7016
  });
6966
7017
  } else if (message.voice_started_event) {
@@ -7008,27 +7059,27 @@ var _DefaultSocket = class _DefaultSocket {
7008
7059
  } else if (message.channel_message) {
7009
7060
  var content, reactions, mentions, attachments, references;
7010
7061
  try {
7011
- content = JSON.parse(message.channel_message.content);
7062
+ content = safeJSONParse(message.channel_message.content);
7012
7063
  } catch (e2) {
7013
7064
  console.log("content is invalid", e2);
7014
7065
  }
7015
7066
  try {
7016
- reactions = JSON.parse(message.channel_message.reactions);
7067
+ reactions = safeJSONParse(message.channel_message.reactions);
7017
7068
  } catch (e2) {
7018
7069
  console.log("reactions is invalid", e2);
7019
7070
  }
7020
7071
  try {
7021
- mentions = JSON.parse(message.channel_message.mentions);
7072
+ mentions = safeJSONParse(message.channel_message.mentions);
7022
7073
  } catch (e2) {
7023
7074
  console.log("mentions is invalid", e2);
7024
7075
  }
7025
7076
  try {
7026
- attachments = JSON.parse(message.channel_message.attachments);
7077
+ attachments = safeJSONParse(message.channel_message.attachments);
7027
7078
  } catch (e2) {
7028
7079
  console.log("attachments is invalid", e2);
7029
7080
  }
7030
7081
  try {
7031
- references = JSON.parse(message.channel_message.references);
7082
+ references = safeJSONParse(message.channel_message.references);
7032
7083
  } catch (e2) {
7033
7084
  console.log("references is invalid", e2);
7034
7085
  }
@@ -8353,7 +8404,7 @@ var Client = class {
8353
8404
  timezone: u.timezone,
8354
8405
  update_time: u.update_time,
8355
8406
  username: u.username,
8356
- metadata: u.metadata ? JSON.parse(u.metadata) : void 0
8407
+ metadata: u.metadata ? safeJSONParse(u.metadata) : void 0
8357
8408
  });
8358
8409
  });
8359
8410
  return Promise.resolve(result);
@@ -8407,27 +8458,27 @@ var Client = class {
8407
8458
  response.messages.forEach((m) => {
8408
8459
  var content, reactions, mentions, attachments, references;
8409
8460
  try {
8410
- content = JSON.parse(m.content);
8461
+ content = safeJSONParse(m.content);
8411
8462
  } catch (e) {
8412
8463
  console.log("error parse content", e);
8413
8464
  }
8414
8465
  try {
8415
- reactions = JSON.parse(m.reactions || "[]");
8466
+ reactions = safeJSONParse(m.reactions || "[]");
8416
8467
  } catch (e) {
8417
8468
  console.log("error parse reactions", e);
8418
8469
  }
8419
8470
  try {
8420
- mentions = JSON.parse(m.mentions || "[]");
8471
+ mentions = safeJSONParse(m.mentions || "[]");
8421
8472
  } catch (e) {
8422
8473
  console.log("error parse mentions", e);
8423
8474
  }
8424
8475
  try {
8425
- attachments = JSON.parse(m.attachments || "[]");
8476
+ attachments = safeJSONParse(m.attachments || "[]");
8426
8477
  } catch (e) {
8427
8478
  console.log("error parse attachments", e);
8428
8479
  }
8429
8480
  try {
8430
- references = JSON.parse(m.references || "[]");
8481
+ references = safeJSONParse(m.references || "[]");
8431
8482
  } catch (e) {
8432
8483
  console.log("error parse references", e);
8433
8484
  }
@@ -8603,7 +8654,7 @@ var Client = class {
8603
8654
  timezone: gu.user.timezone,
8604
8655
  update_time: gu.user.update_time,
8605
8656
  username: gu.user.username,
8606
- metadata: gu.user.metadata ? JSON.parse(gu.user.metadata) : void 0
8657
+ metadata: gu.user.metadata ? safeJSONParse(gu.user.metadata) : void 0
8607
8658
  },
8608
8659
  role_id: gu.role_id,
8609
8660
  clan_nick: gu.clan_nick,
@@ -8908,7 +8959,7 @@ var Client = class {
8908
8959
  update_time: f.user.update_time,
8909
8960
  username: f.user.username,
8910
8961
  is_mobile: (_a = f.user) == null ? void 0 : _a.is_mobile,
8911
- metadata: f.user.metadata ? JSON.parse(f.user.metadata) : void 0
8962
+ metadata: f.user.metadata ? safeJSONParse(f.user.metadata) : void 0
8912
8963
  },
8913
8964
  state: f.state
8914
8965
  });
@@ -8939,7 +8990,7 @@ var Client = class {
8939
8990
  persistent: n.persistent,
8940
8991
  sender_id: n.sender_id,
8941
8992
  subject: n.subject,
8942
- content: n.content ? JSON.parse(n.content) : void 0
8993
+ content: n.content ? safeJSONParse(n.content) : void 0
8943
8994
  });
8944
8995
  });
8945
8996
  return Promise.resolve(result);
@@ -8961,7 +9012,7 @@ var Client = class {
8961
9012
  ).then((response) => {
8962
9013
  return Promise.resolve({
8963
9014
  id: response.id,
8964
- payload: !response.payload ? void 0 : JSON.parse(response.payload)
9015
+ payload: !response.payload ? void 0 : safeJSONParse(response.payload)
8965
9016
  });
8966
9017
  });
8967
9018
  });
@@ -8972,7 +9023,7 @@ var Client = class {
8972
9023
  return this.apiClient.rpcFunc2("", id, input && JSON.stringify(input) || "", httpKey).then((response) => {
8973
9024
  return Promise.resolve({
8974
9025
  id: response.id,
8975
- payload: !response.payload ? void 0 : JSON.parse(response.payload)
9026
+ payload: !response.payload ? void 0 : safeJSONParse(response.payload)
8976
9027
  });
8977
9028
  }).catch((err) => {
8978
9029
  throw err;
@@ -10405,5 +10456,9 @@ export {
10405
10456
  NotificationType,
10406
10457
  Session,
10407
10458
  WebSocketAdapterText,
10408
- WebrtcSignalingType
10459
+ WebrtcSignalingType,
10460
+ b64DecodeUnicode,
10461
+ b64EncodeUnicode,
10462
+ buildFetchOptions,
10463
+ safeJSONParse
10409
10464
  };
package/dist/utils.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export declare function buildFetchOptions(method: string, options: any, bodyJson: string): any;
2
2
  export declare function b64EncodeUnicode(str: string): string;
3
3
  export declare function b64DecodeUnicode(str: string): string;
4
+ export declare function safeJSONParse(jsonString: string): any;
package/index.ts CHANGED
@@ -20,6 +20,7 @@ export * from "./client";
20
20
  export * from "./session";
21
21
  export * from "./socket";
22
22
  export * from "./web_socket_adapter";
23
+ export * from "./utils"
23
24
 
24
25
  /**
25
26
  * Reexported due to duplicate definition of ChannelMessage in [Client]{@link ./client.ts} and [Session]{@link ./session.ts}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mezon-js",
3
3
 
4
- "version": "2.9.94",
4
+ "version": "2.9.96",
5
5
 
6
6
  "scripts": {
7
7
  "build": "npx tsc && npx rollup -c --bundleConfigAsCjs && node build.mjs"
package/socket.ts CHANGED
@@ -32,6 +32,7 @@ import {
32
32
  import { Session } from "./session";
33
33
  import { ChannelMessage, Notification } from "./client";
34
34
  import { WebSocketAdapter, WebSocketAdapterText } from "./web_socket_adapter";
35
+ import { safeJSONParse } from "./utils";
35
36
 
36
37
  /** Stores function references for resolve/reject with a DOM Promise. */
37
38
  interface PromiseExecutor {
@@ -1364,7 +1365,7 @@ export class DefaultSocket implements Socket {
1364
1365
  if (!message.cid) {
1365
1366
  if (message.notifications) {
1366
1367
  message.notifications.notifications.forEach((n: ApiNotification) => {
1367
- n.content = n.content ? JSON.parse(n.content) : undefined;
1368
+ n.content = n.content ? safeJSONParse(n.content) : undefined;
1368
1369
  this.onnotification(n);
1369
1370
  });
1370
1371
  } else if (message.voice_started_event) {
@@ -1412,27 +1413,27 @@ export class DefaultSocket implements Socket {
1412
1413
  } else if (message.channel_message) {
1413
1414
  var content, reactions, mentions, attachments, references;
1414
1415
  try {
1415
- content = JSON.parse(message.channel_message.content);
1416
+ content = safeJSONParse(message.channel_message.content);
1416
1417
  } catch (e) {
1417
1418
  console.log("content is invalid", e);
1418
1419
  }
1419
1420
  try {
1420
- reactions = JSON.parse(message.channel_message.reactions);
1421
+ reactions = safeJSONParse(message.channel_message.reactions);
1421
1422
  } catch (e) {
1422
1423
  console.log("reactions is invalid", e);
1423
1424
  }
1424
1425
  try {
1425
- mentions = JSON.parse(message.channel_message.mentions);
1426
+ mentions = safeJSONParse(message.channel_message.mentions);
1426
1427
  } catch (e) {
1427
1428
  console.log("mentions is invalid", e);
1428
1429
  }
1429
1430
  try {
1430
- attachments = JSON.parse(message.channel_message.attachments);
1431
+ attachments = safeJSONParse(message.channel_message.attachments);
1431
1432
  } catch (e) {
1432
1433
  console.log("attachments is invalid", e);
1433
1434
  }
1434
1435
  try {
1435
- references = JSON.parse(message.channel_message.references);
1436
+ references = safeJSONParse(message.channel_message.references);
1436
1437
  } catch (e) {
1437
1438
  console.log("references is invalid", e);
1438
1439
  }
package/utils.ts CHANGED
@@ -47,3 +47,26 @@ export function b64DecodeUnicode(str: string) {
47
47
  return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
48
48
  }).join(''));
49
49
  }
50
+
51
+ export function safeJSONParse(jsonString: string) {
52
+ try {
53
+ // Step 1: Unescape JSON strings to handle double-escaped characters
54
+ const unescapedJSON = jsonString.replace(/\\./g, (match) => {
55
+ switch (match) {
56
+ case '\\"': return '"';
57
+ case '\\n': return '\n';
58
+ case '\\t': return '\t';
59
+ // Add more escape sequences as needed
60
+ default: return match[1]; // Remove the backslash
61
+ }
62
+ });
63
+
64
+ // Step 2: Parse the unescaped JSON
65
+ const parsedData = JSON.parse(unescapedJSON);
66
+
67
+ return parsedData;
68
+ } catch (error) {
69
+ console.error('Error parsing JSON:', error);
70
+ return null; // Handle the error gracefully or throw an exception if necessary
71
+ }
72
+ }
@@ -16,6 +16,7 @@
16
16
 
17
17
  import { decode, encode } from "base64-arraybuffer";
18
18
  import { btoa } from "js-base64";
19
+ import { safeJSONParse } from "./utils";
19
20
 
20
21
  /**
21
22
  * An interface used by Mezon's web socket to determine the payload protocol.
@@ -114,7 +115,7 @@ export class WebSocketAdapterText implements WebSocketAdapter {
114
115
  set onMessage(value: SocketMessageHandler | null) {
115
116
  if (value) {
116
117
  this._socket!.onmessage = (evt: MessageEvent) => {
117
- const message: any = JSON.parse(evt.data);
118
+ const message: any = safeJSONParse(evt.data);
118
119
  if (message.party_data && message.party_data.data) {
119
120
  message.party_data.data = new Uint8Array(
120
121
  decode(message.party_data.data)