mezon-js 2.9.94 → 2.9.95
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 +12 -11
- package/dist/mezon-js.cjs.js +39 -18
- package/dist/mezon-js.esm.mjs +39 -18
- package/dist/utils.d.ts +1 -0
- package/package.json +1 -1
- package/socket.ts +7 -6
- package/utils.ts +23 -0
- package/web_socket_adapter.ts +2 -1
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 ?
|
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 =
|
1438
|
+
content = safeJSONParse(m.content);
|
1438
1439
|
} catch (e) {
|
1439
1440
|
console.log("error parse content", e);
|
1440
1441
|
}
|
1441
1442
|
try {
|
1442
|
-
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 =
|
1448
|
+
mentions = safeJSONParse(m.mentions || "[]");
|
1448
1449
|
} catch (e) {
|
1449
1450
|
console.log("error parse mentions", e);
|
1450
1451
|
}
|
1451
1452
|
try {
|
1452
|
-
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 =
|
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
|
-
?
|
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
|
-
?
|
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 ?
|
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 :
|
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 :
|
2307
|
+
payload: !response.payload ? undefined : safeJSONParse(response.payload),
|
2307
2308
|
});
|
2308
2309
|
})
|
2309
2310
|
.catch((err: any) => {
|
package/dist/mezon-js.cjs.js
CHANGED
@@ -680,6 +680,27 @@ function buildFetchOptions(method, options, bodyJson) {
|
|
680
680
|
}
|
681
681
|
return fetchOptions;
|
682
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
|
+
}
|
683
704
|
|
684
705
|
// api.gen.ts
|
685
706
|
var MezonApi = class {
|
@@ -6883,7 +6904,7 @@ var WebSocketAdapterText = class {
|
|
6883
6904
|
set onMessage(value) {
|
6884
6905
|
if (value) {
|
6885
6906
|
this._socket.onmessage = (evt) => {
|
6886
|
-
const message =
|
6907
|
+
const message = safeJSONParse(evt.data);
|
6887
6908
|
if (message.party_data && message.party_data.data) {
|
6888
6909
|
message.party_data.data = new Uint8Array(
|
6889
6910
|
decode2(message.party_data.data)
|
@@ -6990,7 +7011,7 @@ var _DefaultSocket = class _DefaultSocket {
|
|
6990
7011
|
if (!message.cid) {
|
6991
7012
|
if (message.notifications) {
|
6992
7013
|
message.notifications.notifications.forEach((n) => {
|
6993
|
-
n.content = n.content ?
|
7014
|
+
n.content = n.content ? safeJSONParse(n.content) : void 0;
|
6994
7015
|
this.onnotification(n);
|
6995
7016
|
});
|
6996
7017
|
} else if (message.voice_started_event) {
|
@@ -7038,27 +7059,27 @@ var _DefaultSocket = class _DefaultSocket {
|
|
7038
7059
|
} else if (message.channel_message) {
|
7039
7060
|
var content, reactions, mentions, attachments, references;
|
7040
7061
|
try {
|
7041
|
-
content =
|
7062
|
+
content = safeJSONParse(message.channel_message.content);
|
7042
7063
|
} catch (e2) {
|
7043
7064
|
console.log("content is invalid", e2);
|
7044
7065
|
}
|
7045
7066
|
try {
|
7046
|
-
reactions =
|
7067
|
+
reactions = safeJSONParse(message.channel_message.reactions);
|
7047
7068
|
} catch (e2) {
|
7048
7069
|
console.log("reactions is invalid", e2);
|
7049
7070
|
}
|
7050
7071
|
try {
|
7051
|
-
mentions =
|
7072
|
+
mentions = safeJSONParse(message.channel_message.mentions);
|
7052
7073
|
} catch (e2) {
|
7053
7074
|
console.log("mentions is invalid", e2);
|
7054
7075
|
}
|
7055
7076
|
try {
|
7056
|
-
attachments =
|
7077
|
+
attachments = safeJSONParse(message.channel_message.attachments);
|
7057
7078
|
} catch (e2) {
|
7058
7079
|
console.log("attachments is invalid", e2);
|
7059
7080
|
}
|
7060
7081
|
try {
|
7061
|
-
references =
|
7082
|
+
references = safeJSONParse(message.channel_message.references);
|
7062
7083
|
} catch (e2) {
|
7063
7084
|
console.log("references is invalid", e2);
|
7064
7085
|
}
|
@@ -8383,7 +8404,7 @@ var Client = class {
|
|
8383
8404
|
timezone: u.timezone,
|
8384
8405
|
update_time: u.update_time,
|
8385
8406
|
username: u.username,
|
8386
|
-
metadata: u.metadata ?
|
8407
|
+
metadata: u.metadata ? safeJSONParse(u.metadata) : void 0
|
8387
8408
|
});
|
8388
8409
|
});
|
8389
8410
|
return Promise.resolve(result);
|
@@ -8437,27 +8458,27 @@ var Client = class {
|
|
8437
8458
|
response.messages.forEach((m) => {
|
8438
8459
|
var content, reactions, mentions, attachments, references;
|
8439
8460
|
try {
|
8440
|
-
content =
|
8461
|
+
content = safeJSONParse(m.content);
|
8441
8462
|
} catch (e) {
|
8442
8463
|
console.log("error parse content", e);
|
8443
8464
|
}
|
8444
8465
|
try {
|
8445
|
-
reactions =
|
8466
|
+
reactions = safeJSONParse(m.reactions || "[]");
|
8446
8467
|
} catch (e) {
|
8447
8468
|
console.log("error parse reactions", e);
|
8448
8469
|
}
|
8449
8470
|
try {
|
8450
|
-
mentions =
|
8471
|
+
mentions = safeJSONParse(m.mentions || "[]");
|
8451
8472
|
} catch (e) {
|
8452
8473
|
console.log("error parse mentions", e);
|
8453
8474
|
}
|
8454
8475
|
try {
|
8455
|
-
attachments =
|
8476
|
+
attachments = safeJSONParse(m.attachments || "[]");
|
8456
8477
|
} catch (e) {
|
8457
8478
|
console.log("error parse attachments", e);
|
8458
8479
|
}
|
8459
8480
|
try {
|
8460
|
-
references =
|
8481
|
+
references = safeJSONParse(m.references || "[]");
|
8461
8482
|
} catch (e) {
|
8462
8483
|
console.log("error parse references", e);
|
8463
8484
|
}
|
@@ -8633,7 +8654,7 @@ var Client = class {
|
|
8633
8654
|
timezone: gu.user.timezone,
|
8634
8655
|
update_time: gu.user.update_time,
|
8635
8656
|
username: gu.user.username,
|
8636
|
-
metadata: gu.user.metadata ?
|
8657
|
+
metadata: gu.user.metadata ? safeJSONParse(gu.user.metadata) : void 0
|
8637
8658
|
},
|
8638
8659
|
role_id: gu.role_id,
|
8639
8660
|
clan_nick: gu.clan_nick,
|
@@ -8938,7 +8959,7 @@ var Client = class {
|
|
8938
8959
|
update_time: f.user.update_time,
|
8939
8960
|
username: f.user.username,
|
8940
8961
|
is_mobile: (_a = f.user) == null ? void 0 : _a.is_mobile,
|
8941
|
-
metadata: f.user.metadata ?
|
8962
|
+
metadata: f.user.metadata ? safeJSONParse(f.user.metadata) : void 0
|
8942
8963
|
},
|
8943
8964
|
state: f.state
|
8944
8965
|
});
|
@@ -8969,7 +8990,7 @@ var Client = class {
|
|
8969
8990
|
persistent: n.persistent,
|
8970
8991
|
sender_id: n.sender_id,
|
8971
8992
|
subject: n.subject,
|
8972
|
-
content: n.content ?
|
8993
|
+
content: n.content ? safeJSONParse(n.content) : void 0
|
8973
8994
|
});
|
8974
8995
|
});
|
8975
8996
|
return Promise.resolve(result);
|
@@ -8991,7 +9012,7 @@ var Client = class {
|
|
8991
9012
|
).then((response) => {
|
8992
9013
|
return Promise.resolve({
|
8993
9014
|
id: response.id,
|
8994
|
-
payload: !response.payload ? void 0 :
|
9015
|
+
payload: !response.payload ? void 0 : safeJSONParse(response.payload)
|
8995
9016
|
});
|
8996
9017
|
});
|
8997
9018
|
});
|
@@ -9002,7 +9023,7 @@ var Client = class {
|
|
9002
9023
|
return this.apiClient.rpcFunc2("", id, input && JSON.stringify(input) || "", httpKey).then((response) => {
|
9003
9024
|
return Promise.resolve({
|
9004
9025
|
id: response.id,
|
9005
|
-
payload: !response.payload ? void 0 :
|
9026
|
+
payload: !response.payload ? void 0 : safeJSONParse(response.payload)
|
9006
9027
|
});
|
9007
9028
|
}).catch((err) => {
|
9008
9029
|
throw err;
|
package/dist/mezon-js.esm.mjs
CHANGED
@@ -650,6 +650,27 @@ function buildFetchOptions(method, options, bodyJson) {
|
|
650
650
|
}
|
651
651
|
return fetchOptions;
|
652
652
|
}
|
653
|
+
function safeJSONParse(jsonString) {
|
654
|
+
try {
|
655
|
+
const unescapedJSON = jsonString.replace(/\\./g, (match) => {
|
656
|
+
switch (match) {
|
657
|
+
case '\\"':
|
658
|
+
return '"';
|
659
|
+
case "\\n":
|
660
|
+
return "\n";
|
661
|
+
case "\\t":
|
662
|
+
return " ";
|
663
|
+
default:
|
664
|
+
return match[1];
|
665
|
+
}
|
666
|
+
});
|
667
|
+
const parsedData = JSON.parse(unescapedJSON);
|
668
|
+
return parsedData;
|
669
|
+
} catch (error) {
|
670
|
+
console.error("Error parsing JSON:", error);
|
671
|
+
return null;
|
672
|
+
}
|
673
|
+
}
|
653
674
|
|
654
675
|
// api.gen.ts
|
655
676
|
var MezonApi = class {
|
@@ -6853,7 +6874,7 @@ var WebSocketAdapterText = class {
|
|
6853
6874
|
set onMessage(value) {
|
6854
6875
|
if (value) {
|
6855
6876
|
this._socket.onmessage = (evt) => {
|
6856
|
-
const message =
|
6877
|
+
const message = safeJSONParse(evt.data);
|
6857
6878
|
if (message.party_data && message.party_data.data) {
|
6858
6879
|
message.party_data.data = new Uint8Array(
|
6859
6880
|
decode2(message.party_data.data)
|
@@ -6960,7 +6981,7 @@ var _DefaultSocket = class _DefaultSocket {
|
|
6960
6981
|
if (!message.cid) {
|
6961
6982
|
if (message.notifications) {
|
6962
6983
|
message.notifications.notifications.forEach((n) => {
|
6963
|
-
n.content = n.content ?
|
6984
|
+
n.content = n.content ? safeJSONParse(n.content) : void 0;
|
6964
6985
|
this.onnotification(n);
|
6965
6986
|
});
|
6966
6987
|
} else if (message.voice_started_event) {
|
@@ -7008,27 +7029,27 @@ var _DefaultSocket = class _DefaultSocket {
|
|
7008
7029
|
} else if (message.channel_message) {
|
7009
7030
|
var content, reactions, mentions, attachments, references;
|
7010
7031
|
try {
|
7011
|
-
content =
|
7032
|
+
content = safeJSONParse(message.channel_message.content);
|
7012
7033
|
} catch (e2) {
|
7013
7034
|
console.log("content is invalid", e2);
|
7014
7035
|
}
|
7015
7036
|
try {
|
7016
|
-
reactions =
|
7037
|
+
reactions = safeJSONParse(message.channel_message.reactions);
|
7017
7038
|
} catch (e2) {
|
7018
7039
|
console.log("reactions is invalid", e2);
|
7019
7040
|
}
|
7020
7041
|
try {
|
7021
|
-
mentions =
|
7042
|
+
mentions = safeJSONParse(message.channel_message.mentions);
|
7022
7043
|
} catch (e2) {
|
7023
7044
|
console.log("mentions is invalid", e2);
|
7024
7045
|
}
|
7025
7046
|
try {
|
7026
|
-
attachments =
|
7047
|
+
attachments = safeJSONParse(message.channel_message.attachments);
|
7027
7048
|
} catch (e2) {
|
7028
7049
|
console.log("attachments is invalid", e2);
|
7029
7050
|
}
|
7030
7051
|
try {
|
7031
|
-
references =
|
7052
|
+
references = safeJSONParse(message.channel_message.references);
|
7032
7053
|
} catch (e2) {
|
7033
7054
|
console.log("references is invalid", e2);
|
7034
7055
|
}
|
@@ -8353,7 +8374,7 @@ var Client = class {
|
|
8353
8374
|
timezone: u.timezone,
|
8354
8375
|
update_time: u.update_time,
|
8355
8376
|
username: u.username,
|
8356
|
-
metadata: u.metadata ?
|
8377
|
+
metadata: u.metadata ? safeJSONParse(u.metadata) : void 0
|
8357
8378
|
});
|
8358
8379
|
});
|
8359
8380
|
return Promise.resolve(result);
|
@@ -8407,27 +8428,27 @@ var Client = class {
|
|
8407
8428
|
response.messages.forEach((m) => {
|
8408
8429
|
var content, reactions, mentions, attachments, references;
|
8409
8430
|
try {
|
8410
|
-
content =
|
8431
|
+
content = safeJSONParse(m.content);
|
8411
8432
|
} catch (e) {
|
8412
8433
|
console.log("error parse content", e);
|
8413
8434
|
}
|
8414
8435
|
try {
|
8415
|
-
reactions =
|
8436
|
+
reactions = safeJSONParse(m.reactions || "[]");
|
8416
8437
|
} catch (e) {
|
8417
8438
|
console.log("error parse reactions", e);
|
8418
8439
|
}
|
8419
8440
|
try {
|
8420
|
-
mentions =
|
8441
|
+
mentions = safeJSONParse(m.mentions || "[]");
|
8421
8442
|
} catch (e) {
|
8422
8443
|
console.log("error parse mentions", e);
|
8423
8444
|
}
|
8424
8445
|
try {
|
8425
|
-
attachments =
|
8446
|
+
attachments = safeJSONParse(m.attachments || "[]");
|
8426
8447
|
} catch (e) {
|
8427
8448
|
console.log("error parse attachments", e);
|
8428
8449
|
}
|
8429
8450
|
try {
|
8430
|
-
references =
|
8451
|
+
references = safeJSONParse(m.references || "[]");
|
8431
8452
|
} catch (e) {
|
8432
8453
|
console.log("error parse references", e);
|
8433
8454
|
}
|
@@ -8603,7 +8624,7 @@ var Client = class {
|
|
8603
8624
|
timezone: gu.user.timezone,
|
8604
8625
|
update_time: gu.user.update_time,
|
8605
8626
|
username: gu.user.username,
|
8606
|
-
metadata: gu.user.metadata ?
|
8627
|
+
metadata: gu.user.metadata ? safeJSONParse(gu.user.metadata) : void 0
|
8607
8628
|
},
|
8608
8629
|
role_id: gu.role_id,
|
8609
8630
|
clan_nick: gu.clan_nick,
|
@@ -8908,7 +8929,7 @@ var Client = class {
|
|
8908
8929
|
update_time: f.user.update_time,
|
8909
8930
|
username: f.user.username,
|
8910
8931
|
is_mobile: (_a = f.user) == null ? void 0 : _a.is_mobile,
|
8911
|
-
metadata: f.user.metadata ?
|
8932
|
+
metadata: f.user.metadata ? safeJSONParse(f.user.metadata) : void 0
|
8912
8933
|
},
|
8913
8934
|
state: f.state
|
8914
8935
|
});
|
@@ -8939,7 +8960,7 @@ var Client = class {
|
|
8939
8960
|
persistent: n.persistent,
|
8940
8961
|
sender_id: n.sender_id,
|
8941
8962
|
subject: n.subject,
|
8942
|
-
content: n.content ?
|
8963
|
+
content: n.content ? safeJSONParse(n.content) : void 0
|
8943
8964
|
});
|
8944
8965
|
});
|
8945
8966
|
return Promise.resolve(result);
|
@@ -8961,7 +8982,7 @@ var Client = class {
|
|
8961
8982
|
).then((response) => {
|
8962
8983
|
return Promise.resolve({
|
8963
8984
|
id: response.id,
|
8964
|
-
payload: !response.payload ? void 0 :
|
8985
|
+
payload: !response.payload ? void 0 : safeJSONParse(response.payload)
|
8965
8986
|
});
|
8966
8987
|
});
|
8967
8988
|
});
|
@@ -8972,7 +8993,7 @@ var Client = class {
|
|
8972
8993
|
return this.apiClient.rpcFunc2("", id, input && JSON.stringify(input) || "", httpKey).then((response) => {
|
8973
8994
|
return Promise.resolve({
|
8974
8995
|
id: response.id,
|
8975
|
-
payload: !response.payload ? void 0 :
|
8996
|
+
payload: !response.payload ? void 0 : safeJSONParse(response.payload)
|
8976
8997
|
});
|
8977
8998
|
}).catch((err) => {
|
8978
8999
|
throw err;
|
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/package.json
CHANGED
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 ?
|
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 =
|
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 =
|
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 =
|
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 =
|
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 =
|
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
|
+
}
|
package/web_socket_adapter.ts
CHANGED
@@ -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 =
|
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)
|