@blockspark/chat-widget 1.0.15 → 1.0.17
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/{core/stateManager.esm.js → ChatWidget-DKfLes8T.js} +450 -4
- package/dist/ChatWidget-DKfLes8T.js.map +1 -0
- package/dist/ChatWidget-lvHzA07s.cjs +2 -0
- package/dist/ChatWidget-lvHzA07s.cjs.map +1 -0
- package/dist/index.cjs.js +1 -1
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +1357 -7
- package/dist/index.esm.js.map +1 -1
- package/dist/nuxt.cjs.js +1 -1
- package/dist/nuxt.esm.js +4 -5
- package/dist/nuxt.esm.js.map +1 -1
- package/dist/sanitize-D904jROs.cjs +4 -0
- package/dist/sanitize-D904jROs.cjs.map +1 -0
- package/dist/sanitize-O18C3eqP.js +2948 -0
- package/dist/sanitize-O18C3eqP.js.map +1 -0
- package/dist/services/chatService.d.ts.map +1 -1
- package/dist/utils/frameworkDetector.d.ts.map +1 -1
- package/dist/utils/sanitize.d.ts.map +1 -1
- package/dist/vue.cjs.js +1 -1
- package/dist/vue.esm.js +4 -5
- package/dist/vue.esm.js.map +1 -1
- package/package.json +14 -49
- package/dist/_virtual/_plugin-vue_export-helper.cjs.js +0 -2
- package/dist/_virtual/_plugin-vue_export-helper.cjs.js.map +0 -1
- package/dist/_virtual/_plugin-vue_export-helper.esm.js +0 -11
- package/dist/_virtual/_plugin-vue_export-helper.esm.js.map +0 -1
- package/dist/components/ChatWidget.cjs.js +0 -2
- package/dist/components/ChatWidget.cjs.js.map +0 -1
- package/dist/components/ChatWidget.esm.js +0 -1129
- package/dist/components/ChatWidget.esm.js.map +0 -1
- package/dist/components/ChatWidget.vue.cjs.js +0 -2
- package/dist/components/ChatWidget.vue.cjs.js.map +0 -1
- package/dist/components/ChatWidget.vue.cjs2.js +0 -2
- package/dist/components/ChatWidget.vue.cjs2.js.map +0 -1
- package/dist/components/ChatWidget.vue.esm.js +0 -8
- package/dist/components/ChatWidget.vue.esm.js.map +0 -1
- package/dist/components/ChatWidget.vue.esm2.js +0 -374
- package/dist/components/ChatWidget.vue.esm2.js.map +0 -1
- package/dist/composables/useChatWidget.cjs.js +0 -2
- package/dist/composables/useChatWidget.cjs.js.map +0 -1
- package/dist/composables/useChatWidget.esm.js +0 -75
- package/dist/composables/useChatWidget.esm.js.map +0 -1
- package/dist/core/stateManager.cjs.js +0 -2
- package/dist/core/stateManager.cjs.js.map +0 -1
- package/dist/core/stateManager.esm.js.map +0 -1
- package/dist/entry/vanilla.cjs.js +0 -2
- package/dist/entry/vanilla.cjs.js.map +0 -1
- package/dist/entry/vanilla.esm.js +0 -50
- package/dist/entry/vanilla.esm.js.map +0 -1
- package/dist/hooks/useChatMode.cjs.js +0 -2
- package/dist/hooks/useChatMode.cjs.js.map +0 -1
- package/dist/hooks/useChatMode.esm.js +0 -61
- package/dist/hooks/useChatMode.esm.js.map +0 -1
- package/dist/node_modules/jose/dist/browser/jws/compact/sign.cjs.js +0 -2
- package/dist/node_modules/jose/dist/browser/jws/compact/sign.cjs.js.map +0 -1
- package/dist/node_modules/jose/dist/browser/jws/compact/sign.esm.js +0 -21
- package/dist/node_modules/jose/dist/browser/jws/compact/sign.esm.js.map +0 -1
- package/dist/node_modules/jose/dist/browser/jws/flattened/sign.cjs.js +0 -2
- package/dist/node_modules/jose/dist/browser/jws/flattened/sign.cjs.js.map +0 -1
- package/dist/node_modules/jose/dist/browser/jws/flattened/sign.esm.js +0 -84
- package/dist/node_modules/jose/dist/browser/jws/flattened/sign.esm.js.map +0 -1
- package/dist/node_modules/jose/dist/browser/jwt/produce.cjs.js +0 -2
- package/dist/node_modules/jose/dist/browser/jwt/produce.cjs.js.map +0 -1
- package/dist/node_modules/jose/dist/browser/jwt/produce.esm.js +0 -72
- package/dist/node_modules/jose/dist/browser/jwt/produce.esm.js.map +0 -1
- package/dist/node_modules/jose/dist/browser/jwt/sign.cjs.js +0 -2
- package/dist/node_modules/jose/dist/browser/jwt/sign.cjs.js.map +0 -1
- package/dist/node_modules/jose/dist/browser/jwt/sign.esm.js +0 -22
- package/dist/node_modules/jose/dist/browser/jwt/sign.esm.js.map +0 -1
- package/dist/node_modules/jose/dist/browser/key/import.cjs.js +0 -2
- package/dist/node_modules/jose/dist/browser/key/import.cjs.js.map +0 -1
- package/dist/node_modules/jose/dist/browser/key/import.esm.js +0 -11
- package/dist/node_modules/jose/dist/browser/key/import.esm.js.map +0 -1
- package/dist/node_modules/jose/dist/browser/lib/buffer_utils.cjs.js +0 -2
- package/dist/node_modules/jose/dist/browser/lib/buffer_utils.cjs.js.map +0 -1
- package/dist/node_modules/jose/dist/browser/lib/buffer_utils.esm.js +0 -18
- package/dist/node_modules/jose/dist/browser/lib/buffer_utils.esm.js.map +0 -1
- package/dist/node_modules/jose/dist/browser/lib/check_key_type.cjs.js +0 -2
- package/dist/node_modules/jose/dist/browser/lib/check_key_type.cjs.js.map +0 -1
- package/dist/node_modules/jose/dist/browser/lib/check_key_type.esm.js +0 -77
- package/dist/node_modules/jose/dist/browser/lib/check_key_type.esm.js.map +0 -1
- package/dist/node_modules/jose/dist/browser/lib/crypto_key.cjs.js +0 -2
- package/dist/node_modules/jose/dist/browser/lib/crypto_key.cjs.js.map +0 -1
- package/dist/node_modules/jose/dist/browser/lib/crypto_key.esm.js +0 -101
- package/dist/node_modules/jose/dist/browser/lib/crypto_key.esm.js.map +0 -1
- package/dist/node_modules/jose/dist/browser/lib/epoch.cjs.js +0 -2
- package/dist/node_modules/jose/dist/browser/lib/epoch.cjs.js.map +0 -1
- package/dist/node_modules/jose/dist/browser/lib/epoch.esm.js +0 -5
- package/dist/node_modules/jose/dist/browser/lib/epoch.esm.js.map +0 -1
- package/dist/node_modules/jose/dist/browser/lib/invalid_key_input.cjs.js +0 -2
- package/dist/node_modules/jose/dist/browser/lib/invalid_key_input.cjs.js.map +0 -1
- package/dist/node_modules/jose/dist/browser/lib/invalid_key_input.esm.js +0 -32
- package/dist/node_modules/jose/dist/browser/lib/invalid_key_input.esm.js.map +0 -1
- package/dist/node_modules/jose/dist/browser/lib/is_disjoint.cjs.js +0 -2
- package/dist/node_modules/jose/dist/browser/lib/is_disjoint.cjs.js.map +0 -1
- package/dist/node_modules/jose/dist/browser/lib/is_disjoint.esm.js +0 -25
- package/dist/node_modules/jose/dist/browser/lib/is_disjoint.esm.js.map +0 -1
- package/dist/node_modules/jose/dist/browser/lib/is_jwk.cjs.js +0 -2
- package/dist/node_modules/jose/dist/browser/lib/is_jwk.cjs.js.map +0 -1
- package/dist/node_modules/jose/dist/browser/lib/is_jwk.esm.js +0 -20
- package/dist/node_modules/jose/dist/browser/lib/is_jwk.esm.js.map +0 -1
- package/dist/node_modules/jose/dist/browser/lib/is_object.cjs.js +0 -2
- package/dist/node_modules/jose/dist/browser/lib/is_object.cjs.js.map +0 -1
- package/dist/node_modules/jose/dist/browser/lib/is_object.esm.js +0 -20
- package/dist/node_modules/jose/dist/browser/lib/is_object.esm.js.map +0 -1
- package/dist/node_modules/jose/dist/browser/lib/secs.cjs.js +0 -2
- package/dist/node_modules/jose/dist/browser/lib/secs.cjs.js.map +0 -1
- package/dist/node_modules/jose/dist/browser/lib/secs.esm.js +0 -59
- package/dist/node_modules/jose/dist/browser/lib/secs.esm.js.map +0 -1
- package/dist/node_modules/jose/dist/browser/lib/validate_crit.cjs.js +0 -2
- package/dist/node_modules/jose/dist/browser/lib/validate_crit.cjs.js.map +0 -1
- package/dist/node_modules/jose/dist/browser/lib/validate_crit.esm.js +0 -34
- package/dist/node_modules/jose/dist/browser/lib/validate_crit.esm.js.map +0 -1
- package/dist/node_modules/jose/dist/browser/runtime/asn1.cjs.js +0 -2
- package/dist/node_modules/jose/dist/browser/runtime/asn1.cjs.js.map +0 -1
- package/dist/node_modules/jose/dist/browser/runtime/asn1.esm.js +0 -103
- package/dist/node_modules/jose/dist/browser/runtime/asn1.esm.js.map +0 -1
- package/dist/node_modules/jose/dist/browser/runtime/base64url.cjs.js +0 -2
- package/dist/node_modules/jose/dist/browser/runtime/base64url.cjs.js.map +0 -1
- package/dist/node_modules/jose/dist/browser/runtime/base64url.esm.js +0 -43
- package/dist/node_modules/jose/dist/browser/runtime/base64url.esm.js.map +0 -1
- package/dist/node_modules/jose/dist/browser/runtime/check_key_length.cjs.js +0 -2
- package/dist/node_modules/jose/dist/browser/runtime/check_key_length.cjs.js.map +0 -1
- package/dist/node_modules/jose/dist/browser/runtime/check_key_length.esm.js +0 -12
- package/dist/node_modules/jose/dist/browser/runtime/check_key_length.esm.js.map +0 -1
- package/dist/node_modules/jose/dist/browser/runtime/get_sign_verify_key.cjs.js +0 -2
- package/dist/node_modules/jose/dist/browser/runtime/get_sign_verify_key.cjs.js.map +0 -1
- package/dist/node_modules/jose/dist/browser/runtime/get_sign_verify_key.esm.js +0 -25
- package/dist/node_modules/jose/dist/browser/runtime/get_sign_verify_key.esm.js.map +0 -1
- package/dist/node_modules/jose/dist/browser/runtime/is_key_like.cjs.js +0 -2
- package/dist/node_modules/jose/dist/browser/runtime/is_key_like.cjs.js.map +0 -1
- package/dist/node_modules/jose/dist/browser/runtime/is_key_like.esm.js +0 -13
- package/dist/node_modules/jose/dist/browser/runtime/is_key_like.esm.js.map +0 -1
- package/dist/node_modules/jose/dist/browser/runtime/jwk_to_key.cjs.js +0 -2
- package/dist/node_modules/jose/dist/browser/runtime/jwk_to_key.cjs.js.map +0 -1
- package/dist/node_modules/jose/dist/browser/runtime/jwk_to_key.esm.js +0 -107
- package/dist/node_modules/jose/dist/browser/runtime/jwk_to_key.esm.js.map +0 -1
- package/dist/node_modules/jose/dist/browser/runtime/normalize_key.cjs.js +0 -2
- package/dist/node_modules/jose/dist/browser/runtime/normalize_key.cjs.js.map +0 -1
- package/dist/node_modules/jose/dist/browser/runtime/normalize_key.esm.js +0 -71
- package/dist/node_modules/jose/dist/browser/runtime/normalize_key.esm.js.map +0 -1
- package/dist/node_modules/jose/dist/browser/runtime/sign.cjs.js +0 -2
- package/dist/node_modules/jose/dist/browser/runtime/sign.cjs.js.map +0 -1
- package/dist/node_modules/jose/dist/browser/runtime/sign.esm.js +0 -14
- package/dist/node_modules/jose/dist/browser/runtime/sign.esm.js.map +0 -1
- package/dist/node_modules/jose/dist/browser/runtime/subtle_dsa.cjs.js +0 -2
- package/dist/node_modules/jose/dist/browser/runtime/subtle_dsa.cjs.js.map +0 -1
- package/dist/node_modules/jose/dist/browser/runtime/subtle_dsa.esm.js +0 -32
- package/dist/node_modules/jose/dist/browser/runtime/subtle_dsa.esm.js.map +0 -1
- package/dist/node_modules/jose/dist/browser/runtime/webcrypto.cjs.js +0 -2
- package/dist/node_modules/jose/dist/browser/runtime/webcrypto.cjs.js.map +0 -1
- package/dist/node_modules/jose/dist/browser/runtime/webcrypto.esm.js +0 -7
- package/dist/node_modules/jose/dist/browser/runtime/webcrypto.esm.js.map +0 -1
- package/dist/node_modules/jose/dist/browser/util/errors.cjs.js +0 -2
- package/dist/node_modules/jose/dist/browser/util/errors.cjs.js.map +0 -1
- package/dist/node_modules/jose/dist/browser/util/errors.esm.js +0 -131
- package/dist/node_modules/jose/dist/browser/util/errors.esm.js.map +0 -1
- package/dist/node_modules/react-dom/client.cjs.js +0 -2
- package/dist/node_modules/react-dom/client.cjs.js.map +0 -1
- package/dist/node_modules/react-dom/client.esm.js +0 -21
- package/dist/node_modules/react-dom/client.esm.js.map +0 -1
- package/dist/services/chatService.cjs.js +0 -2
- package/dist/services/chatService.cjs.js.map +0 -1
- package/dist/services/chatService.esm.js +0 -482
- package/dist/services/chatService.esm.js.map +0 -1
- package/dist/services/dialogflowClient.cjs.js +0 -2
- package/dist/services/dialogflowClient.cjs.js.map +0 -1
- package/dist/services/dialogflowClient.esm.js +0 -282
- package/dist/services/dialogflowClient.esm.js.map +0 -1
- package/dist/services/sessionManager.cjs.js +0 -2
- package/dist/services/sessionManager.cjs.js.map +0 -1
- package/dist/services/sessionManager.esm.js +0 -48
- package/dist/services/sessionManager.esm.js.map +0 -1
- package/dist/utils/frameworkDetector.cjs.js +0 -2
- package/dist/utils/frameworkDetector.cjs.js.map +0 -1
- package/dist/utils/frameworkDetector.esm.js +0 -125
- package/dist/utils/frameworkDetector.esm.js.map +0 -1
- package/dist/utils/sanitize.cjs.js +0 -2
- package/dist/utils/sanitize.cjs.js.map +0 -1
- package/dist/utils/sanitize.esm.js +0 -52
- package/dist/utils/sanitize.esm.js.map +0 -1
|
@@ -0,0 +1,2948 @@
|
|
|
1
|
+
const crypto$1 = crypto;
|
|
2
|
+
const isCryptoKey = (key) => key instanceof CryptoKey;
|
|
3
|
+
const encoder = new TextEncoder();
|
|
4
|
+
const decoder = new TextDecoder();
|
|
5
|
+
function concat(...buffers) {
|
|
6
|
+
const size = buffers.reduce((acc, { length }) => acc + length, 0);
|
|
7
|
+
const buf = new Uint8Array(size);
|
|
8
|
+
let i = 0;
|
|
9
|
+
for (const buffer of buffers) {
|
|
10
|
+
buf.set(buffer, i);
|
|
11
|
+
i += buffer.length;
|
|
12
|
+
}
|
|
13
|
+
return buf;
|
|
14
|
+
}
|
|
15
|
+
const encodeBase64 = (input) => {
|
|
16
|
+
let unencoded = input;
|
|
17
|
+
if (typeof unencoded === "string") {
|
|
18
|
+
unencoded = encoder.encode(unencoded);
|
|
19
|
+
}
|
|
20
|
+
const CHUNK_SIZE = 32768;
|
|
21
|
+
const arr = [];
|
|
22
|
+
for (let i = 0; i < unencoded.length; i += CHUNK_SIZE) {
|
|
23
|
+
arr.push(String.fromCharCode.apply(null, unencoded.subarray(i, i + CHUNK_SIZE)));
|
|
24
|
+
}
|
|
25
|
+
return btoa(arr.join(""));
|
|
26
|
+
};
|
|
27
|
+
const encode = (input) => {
|
|
28
|
+
return encodeBase64(input).replace(/=/g, "").replace(/\+/g, "-").replace(/\//g, "_");
|
|
29
|
+
};
|
|
30
|
+
const decodeBase64 = (encoded) => {
|
|
31
|
+
const binary = atob(encoded);
|
|
32
|
+
const bytes = new Uint8Array(binary.length);
|
|
33
|
+
for (let i = 0; i < binary.length; i++) {
|
|
34
|
+
bytes[i] = binary.charCodeAt(i);
|
|
35
|
+
}
|
|
36
|
+
return bytes;
|
|
37
|
+
};
|
|
38
|
+
const decode = (input) => {
|
|
39
|
+
let encoded = input;
|
|
40
|
+
if (encoded instanceof Uint8Array) {
|
|
41
|
+
encoded = decoder.decode(encoded);
|
|
42
|
+
}
|
|
43
|
+
encoded = encoded.replace(/-/g, "+").replace(/_/g, "/").replace(/\s/g, "");
|
|
44
|
+
try {
|
|
45
|
+
return decodeBase64(encoded);
|
|
46
|
+
} catch {
|
|
47
|
+
throw new TypeError("The input to be decoded is not correctly encoded.");
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
class JOSEError extends Error {
|
|
51
|
+
constructor(message2, options) {
|
|
52
|
+
super(message2, options);
|
|
53
|
+
this.code = "ERR_JOSE_GENERIC";
|
|
54
|
+
this.name = this.constructor.name;
|
|
55
|
+
Error.captureStackTrace?.(this, this.constructor);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
JOSEError.code = "ERR_JOSE_GENERIC";
|
|
59
|
+
class JWTClaimValidationFailed extends JOSEError {
|
|
60
|
+
constructor(message2, payload, claim = "unspecified", reason = "unspecified") {
|
|
61
|
+
super(message2, { cause: { claim, reason, payload } });
|
|
62
|
+
this.code = "ERR_JWT_CLAIM_VALIDATION_FAILED";
|
|
63
|
+
this.claim = claim;
|
|
64
|
+
this.reason = reason;
|
|
65
|
+
this.payload = payload;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
JWTClaimValidationFailed.code = "ERR_JWT_CLAIM_VALIDATION_FAILED";
|
|
69
|
+
class JWTExpired extends JOSEError {
|
|
70
|
+
constructor(message2, payload, claim = "unspecified", reason = "unspecified") {
|
|
71
|
+
super(message2, { cause: { claim, reason, payload } });
|
|
72
|
+
this.code = "ERR_JWT_EXPIRED";
|
|
73
|
+
this.claim = claim;
|
|
74
|
+
this.reason = reason;
|
|
75
|
+
this.payload = payload;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
JWTExpired.code = "ERR_JWT_EXPIRED";
|
|
79
|
+
class JOSEAlgNotAllowed extends JOSEError {
|
|
80
|
+
constructor() {
|
|
81
|
+
super(...arguments);
|
|
82
|
+
this.code = "ERR_JOSE_ALG_NOT_ALLOWED";
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
JOSEAlgNotAllowed.code = "ERR_JOSE_ALG_NOT_ALLOWED";
|
|
86
|
+
class JOSENotSupported extends JOSEError {
|
|
87
|
+
constructor() {
|
|
88
|
+
super(...arguments);
|
|
89
|
+
this.code = "ERR_JOSE_NOT_SUPPORTED";
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
JOSENotSupported.code = "ERR_JOSE_NOT_SUPPORTED";
|
|
93
|
+
class JWEDecryptionFailed extends JOSEError {
|
|
94
|
+
constructor(message2 = "decryption operation failed", options) {
|
|
95
|
+
super(message2, options);
|
|
96
|
+
this.code = "ERR_JWE_DECRYPTION_FAILED";
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
JWEDecryptionFailed.code = "ERR_JWE_DECRYPTION_FAILED";
|
|
100
|
+
class JWEInvalid extends JOSEError {
|
|
101
|
+
constructor() {
|
|
102
|
+
super(...arguments);
|
|
103
|
+
this.code = "ERR_JWE_INVALID";
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
JWEInvalid.code = "ERR_JWE_INVALID";
|
|
107
|
+
class JWSInvalid extends JOSEError {
|
|
108
|
+
constructor() {
|
|
109
|
+
super(...arguments);
|
|
110
|
+
this.code = "ERR_JWS_INVALID";
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
JWSInvalid.code = "ERR_JWS_INVALID";
|
|
114
|
+
class JWTInvalid extends JOSEError {
|
|
115
|
+
constructor() {
|
|
116
|
+
super(...arguments);
|
|
117
|
+
this.code = "ERR_JWT_INVALID";
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
JWTInvalid.code = "ERR_JWT_INVALID";
|
|
121
|
+
class JWKInvalid extends JOSEError {
|
|
122
|
+
constructor() {
|
|
123
|
+
super(...arguments);
|
|
124
|
+
this.code = "ERR_JWK_INVALID";
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
JWKInvalid.code = "ERR_JWK_INVALID";
|
|
128
|
+
class JWKSInvalid extends JOSEError {
|
|
129
|
+
constructor() {
|
|
130
|
+
super(...arguments);
|
|
131
|
+
this.code = "ERR_JWKS_INVALID";
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
JWKSInvalid.code = "ERR_JWKS_INVALID";
|
|
135
|
+
class JWKSNoMatchingKey extends JOSEError {
|
|
136
|
+
constructor(message2 = "no applicable key found in the JSON Web Key Set", options) {
|
|
137
|
+
super(message2, options);
|
|
138
|
+
this.code = "ERR_JWKS_NO_MATCHING_KEY";
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
JWKSNoMatchingKey.code = "ERR_JWKS_NO_MATCHING_KEY";
|
|
142
|
+
class JWKSMultipleMatchingKeys extends JOSEError {
|
|
143
|
+
constructor(message2 = "multiple matching keys found in the JSON Web Key Set", options) {
|
|
144
|
+
super(message2, options);
|
|
145
|
+
this.code = "ERR_JWKS_MULTIPLE_MATCHING_KEYS";
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
JWKSMultipleMatchingKeys.code = "ERR_JWKS_MULTIPLE_MATCHING_KEYS";
|
|
149
|
+
class JWKSTimeout extends JOSEError {
|
|
150
|
+
constructor(message2 = "request timed out", options) {
|
|
151
|
+
super(message2, options);
|
|
152
|
+
this.code = "ERR_JWKS_TIMEOUT";
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
JWKSTimeout.code = "ERR_JWKS_TIMEOUT";
|
|
156
|
+
class JWSSignatureVerificationFailed extends JOSEError {
|
|
157
|
+
constructor(message2 = "signature verification failed", options) {
|
|
158
|
+
super(message2, options);
|
|
159
|
+
this.code = "ERR_JWS_SIGNATURE_VERIFICATION_FAILED";
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
JWSSignatureVerificationFailed.code = "ERR_JWS_SIGNATURE_VERIFICATION_FAILED";
|
|
163
|
+
function unusable(name, prop = "algorithm.name") {
|
|
164
|
+
return new TypeError(`CryptoKey does not support this operation, its ${prop} must be ${name}`);
|
|
165
|
+
}
|
|
166
|
+
function isAlgorithm(algorithm, name) {
|
|
167
|
+
return algorithm.name === name;
|
|
168
|
+
}
|
|
169
|
+
function getHashLength(hash) {
|
|
170
|
+
return parseInt(hash.name.slice(4), 10);
|
|
171
|
+
}
|
|
172
|
+
function getNamedCurve$1(alg) {
|
|
173
|
+
switch (alg) {
|
|
174
|
+
case "ES256":
|
|
175
|
+
return "P-256";
|
|
176
|
+
case "ES384":
|
|
177
|
+
return "P-384";
|
|
178
|
+
case "ES512":
|
|
179
|
+
return "P-521";
|
|
180
|
+
default:
|
|
181
|
+
throw new Error("unreachable");
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
function checkUsage(key, usages) {
|
|
185
|
+
if (usages.length && !usages.some((expected) => key.usages.includes(expected))) {
|
|
186
|
+
let msg = "CryptoKey does not support this operation, its usages must include ";
|
|
187
|
+
if (usages.length > 2) {
|
|
188
|
+
const last = usages.pop();
|
|
189
|
+
msg += `one of ${usages.join(", ")}, or ${last}.`;
|
|
190
|
+
} else if (usages.length === 2) {
|
|
191
|
+
msg += `one of ${usages[0]} or ${usages[1]}.`;
|
|
192
|
+
} else {
|
|
193
|
+
msg += `${usages[0]}.`;
|
|
194
|
+
}
|
|
195
|
+
throw new TypeError(msg);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
function checkSigCryptoKey(key, alg, ...usages) {
|
|
199
|
+
switch (alg) {
|
|
200
|
+
case "HS256":
|
|
201
|
+
case "HS384":
|
|
202
|
+
case "HS512": {
|
|
203
|
+
if (!isAlgorithm(key.algorithm, "HMAC"))
|
|
204
|
+
throw unusable("HMAC");
|
|
205
|
+
const expected = parseInt(alg.slice(2), 10);
|
|
206
|
+
const actual = getHashLength(key.algorithm.hash);
|
|
207
|
+
if (actual !== expected)
|
|
208
|
+
throw unusable(`SHA-${expected}`, "algorithm.hash");
|
|
209
|
+
break;
|
|
210
|
+
}
|
|
211
|
+
case "RS256":
|
|
212
|
+
case "RS384":
|
|
213
|
+
case "RS512": {
|
|
214
|
+
if (!isAlgorithm(key.algorithm, "RSASSA-PKCS1-v1_5"))
|
|
215
|
+
throw unusable("RSASSA-PKCS1-v1_5");
|
|
216
|
+
const expected = parseInt(alg.slice(2), 10);
|
|
217
|
+
const actual = getHashLength(key.algorithm.hash);
|
|
218
|
+
if (actual !== expected)
|
|
219
|
+
throw unusable(`SHA-${expected}`, "algorithm.hash");
|
|
220
|
+
break;
|
|
221
|
+
}
|
|
222
|
+
case "PS256":
|
|
223
|
+
case "PS384":
|
|
224
|
+
case "PS512": {
|
|
225
|
+
if (!isAlgorithm(key.algorithm, "RSA-PSS"))
|
|
226
|
+
throw unusable("RSA-PSS");
|
|
227
|
+
const expected = parseInt(alg.slice(2), 10);
|
|
228
|
+
const actual = getHashLength(key.algorithm.hash);
|
|
229
|
+
if (actual !== expected)
|
|
230
|
+
throw unusable(`SHA-${expected}`, "algorithm.hash");
|
|
231
|
+
break;
|
|
232
|
+
}
|
|
233
|
+
case "EdDSA": {
|
|
234
|
+
if (key.algorithm.name !== "Ed25519" && key.algorithm.name !== "Ed448") {
|
|
235
|
+
throw unusable("Ed25519 or Ed448");
|
|
236
|
+
}
|
|
237
|
+
break;
|
|
238
|
+
}
|
|
239
|
+
case "Ed25519": {
|
|
240
|
+
if (!isAlgorithm(key.algorithm, "Ed25519"))
|
|
241
|
+
throw unusable("Ed25519");
|
|
242
|
+
break;
|
|
243
|
+
}
|
|
244
|
+
case "ES256":
|
|
245
|
+
case "ES384":
|
|
246
|
+
case "ES512": {
|
|
247
|
+
if (!isAlgorithm(key.algorithm, "ECDSA"))
|
|
248
|
+
throw unusable("ECDSA");
|
|
249
|
+
const expected = getNamedCurve$1(alg);
|
|
250
|
+
const actual = key.algorithm.namedCurve;
|
|
251
|
+
if (actual !== expected)
|
|
252
|
+
throw unusable(expected, "algorithm.namedCurve");
|
|
253
|
+
break;
|
|
254
|
+
}
|
|
255
|
+
default:
|
|
256
|
+
throw new TypeError("CryptoKey does not support this operation");
|
|
257
|
+
}
|
|
258
|
+
checkUsage(key, usages);
|
|
259
|
+
}
|
|
260
|
+
function message(msg, actual, ...types2) {
|
|
261
|
+
types2 = types2.filter(Boolean);
|
|
262
|
+
if (types2.length > 2) {
|
|
263
|
+
const last = types2.pop();
|
|
264
|
+
msg += `one of type ${types2.join(", ")}, or ${last}.`;
|
|
265
|
+
} else if (types2.length === 2) {
|
|
266
|
+
msg += `one of type ${types2[0]} or ${types2[1]}.`;
|
|
267
|
+
} else {
|
|
268
|
+
msg += `of type ${types2[0]}.`;
|
|
269
|
+
}
|
|
270
|
+
if (actual == null) {
|
|
271
|
+
msg += ` Received ${actual}`;
|
|
272
|
+
} else if (typeof actual === "function" && actual.name) {
|
|
273
|
+
msg += ` Received function ${actual.name}`;
|
|
274
|
+
} else if (typeof actual === "object" && actual != null) {
|
|
275
|
+
if (actual.constructor?.name) {
|
|
276
|
+
msg += ` Received an instance of ${actual.constructor.name}`;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
return msg;
|
|
280
|
+
}
|
|
281
|
+
const invalidKeyInput = (actual, ...types2) => {
|
|
282
|
+
return message("Key must be ", actual, ...types2);
|
|
283
|
+
};
|
|
284
|
+
function withAlg(alg, actual, ...types2) {
|
|
285
|
+
return message(`Key for the ${alg} algorithm must be `, actual, ...types2);
|
|
286
|
+
}
|
|
287
|
+
const isKeyLike = (key) => {
|
|
288
|
+
if (isCryptoKey(key)) {
|
|
289
|
+
return true;
|
|
290
|
+
}
|
|
291
|
+
return key?.[Symbol.toStringTag] === "KeyObject";
|
|
292
|
+
};
|
|
293
|
+
const types = ["CryptoKey"];
|
|
294
|
+
const isDisjoint = (...headers) => {
|
|
295
|
+
const sources = headers.filter(Boolean);
|
|
296
|
+
if (sources.length === 0 || sources.length === 1) {
|
|
297
|
+
return true;
|
|
298
|
+
}
|
|
299
|
+
let acc;
|
|
300
|
+
for (const header of sources) {
|
|
301
|
+
const parameters = Object.keys(header);
|
|
302
|
+
if (!acc || acc.size === 0) {
|
|
303
|
+
acc = new Set(parameters);
|
|
304
|
+
continue;
|
|
305
|
+
}
|
|
306
|
+
for (const parameter of parameters) {
|
|
307
|
+
if (acc.has(parameter)) {
|
|
308
|
+
return false;
|
|
309
|
+
}
|
|
310
|
+
acc.add(parameter);
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
return true;
|
|
314
|
+
};
|
|
315
|
+
function isObjectLike(value) {
|
|
316
|
+
return typeof value === "object" && value !== null;
|
|
317
|
+
}
|
|
318
|
+
function isObject(input) {
|
|
319
|
+
if (!isObjectLike(input) || Object.prototype.toString.call(input) !== "[object Object]") {
|
|
320
|
+
return false;
|
|
321
|
+
}
|
|
322
|
+
if (Object.getPrototypeOf(input) === null) {
|
|
323
|
+
return true;
|
|
324
|
+
}
|
|
325
|
+
let proto = input;
|
|
326
|
+
while (Object.getPrototypeOf(proto) !== null) {
|
|
327
|
+
proto = Object.getPrototypeOf(proto);
|
|
328
|
+
}
|
|
329
|
+
return Object.getPrototypeOf(input) === proto;
|
|
330
|
+
}
|
|
331
|
+
const checkKeyLength = (alg, key) => {
|
|
332
|
+
if (alg.startsWith("RS") || alg.startsWith("PS")) {
|
|
333
|
+
const { modulusLength } = key.algorithm;
|
|
334
|
+
if (typeof modulusLength !== "number" || modulusLength < 2048) {
|
|
335
|
+
throw new TypeError(`${alg} requires key modulusLength to be 2048 bits or larger`);
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
};
|
|
339
|
+
function isJWK(key) {
|
|
340
|
+
return isObject(key) && typeof key.kty === "string";
|
|
341
|
+
}
|
|
342
|
+
function isPrivateJWK(key) {
|
|
343
|
+
return key.kty !== "oct" && typeof key.d === "string";
|
|
344
|
+
}
|
|
345
|
+
function isPublicJWK(key) {
|
|
346
|
+
return key.kty !== "oct" && typeof key.d === "undefined";
|
|
347
|
+
}
|
|
348
|
+
function isSecretJWK(key) {
|
|
349
|
+
return isJWK(key) && key.kty === "oct" && typeof key.k === "string";
|
|
350
|
+
}
|
|
351
|
+
function subtleMapping(jwk) {
|
|
352
|
+
let algorithm;
|
|
353
|
+
let keyUsages;
|
|
354
|
+
switch (jwk.kty) {
|
|
355
|
+
case "RSA": {
|
|
356
|
+
switch (jwk.alg) {
|
|
357
|
+
case "PS256":
|
|
358
|
+
case "PS384":
|
|
359
|
+
case "PS512":
|
|
360
|
+
algorithm = { name: "RSA-PSS", hash: `SHA-${jwk.alg.slice(-3)}` };
|
|
361
|
+
keyUsages = jwk.d ? ["sign"] : ["verify"];
|
|
362
|
+
break;
|
|
363
|
+
case "RS256":
|
|
364
|
+
case "RS384":
|
|
365
|
+
case "RS512":
|
|
366
|
+
algorithm = { name: "RSASSA-PKCS1-v1_5", hash: `SHA-${jwk.alg.slice(-3)}` };
|
|
367
|
+
keyUsages = jwk.d ? ["sign"] : ["verify"];
|
|
368
|
+
break;
|
|
369
|
+
case "RSA-OAEP":
|
|
370
|
+
case "RSA-OAEP-256":
|
|
371
|
+
case "RSA-OAEP-384":
|
|
372
|
+
case "RSA-OAEP-512":
|
|
373
|
+
algorithm = {
|
|
374
|
+
name: "RSA-OAEP",
|
|
375
|
+
hash: `SHA-${parseInt(jwk.alg.slice(-3), 10) || 1}`
|
|
376
|
+
};
|
|
377
|
+
keyUsages = jwk.d ? ["decrypt", "unwrapKey"] : ["encrypt", "wrapKey"];
|
|
378
|
+
break;
|
|
379
|
+
default:
|
|
380
|
+
throw new JOSENotSupported('Invalid or unsupported JWK "alg" (Algorithm) Parameter value');
|
|
381
|
+
}
|
|
382
|
+
break;
|
|
383
|
+
}
|
|
384
|
+
case "EC": {
|
|
385
|
+
switch (jwk.alg) {
|
|
386
|
+
case "ES256":
|
|
387
|
+
algorithm = { name: "ECDSA", namedCurve: "P-256" };
|
|
388
|
+
keyUsages = jwk.d ? ["sign"] : ["verify"];
|
|
389
|
+
break;
|
|
390
|
+
case "ES384":
|
|
391
|
+
algorithm = { name: "ECDSA", namedCurve: "P-384" };
|
|
392
|
+
keyUsages = jwk.d ? ["sign"] : ["verify"];
|
|
393
|
+
break;
|
|
394
|
+
case "ES512":
|
|
395
|
+
algorithm = { name: "ECDSA", namedCurve: "P-521" };
|
|
396
|
+
keyUsages = jwk.d ? ["sign"] : ["verify"];
|
|
397
|
+
break;
|
|
398
|
+
case "ECDH-ES":
|
|
399
|
+
case "ECDH-ES+A128KW":
|
|
400
|
+
case "ECDH-ES+A192KW":
|
|
401
|
+
case "ECDH-ES+A256KW":
|
|
402
|
+
algorithm = { name: "ECDH", namedCurve: jwk.crv };
|
|
403
|
+
keyUsages = jwk.d ? ["deriveBits"] : [];
|
|
404
|
+
break;
|
|
405
|
+
default:
|
|
406
|
+
throw new JOSENotSupported('Invalid or unsupported JWK "alg" (Algorithm) Parameter value');
|
|
407
|
+
}
|
|
408
|
+
break;
|
|
409
|
+
}
|
|
410
|
+
case "OKP": {
|
|
411
|
+
switch (jwk.alg) {
|
|
412
|
+
case "Ed25519":
|
|
413
|
+
algorithm = { name: "Ed25519" };
|
|
414
|
+
keyUsages = jwk.d ? ["sign"] : ["verify"];
|
|
415
|
+
break;
|
|
416
|
+
case "EdDSA":
|
|
417
|
+
algorithm = { name: jwk.crv };
|
|
418
|
+
keyUsages = jwk.d ? ["sign"] : ["verify"];
|
|
419
|
+
break;
|
|
420
|
+
case "ECDH-ES":
|
|
421
|
+
case "ECDH-ES+A128KW":
|
|
422
|
+
case "ECDH-ES+A192KW":
|
|
423
|
+
case "ECDH-ES+A256KW":
|
|
424
|
+
algorithm = { name: jwk.crv };
|
|
425
|
+
keyUsages = jwk.d ? ["deriveBits"] : [];
|
|
426
|
+
break;
|
|
427
|
+
default:
|
|
428
|
+
throw new JOSENotSupported('Invalid or unsupported JWK "alg" (Algorithm) Parameter value');
|
|
429
|
+
}
|
|
430
|
+
break;
|
|
431
|
+
}
|
|
432
|
+
default:
|
|
433
|
+
throw new JOSENotSupported('Invalid or unsupported JWK "kty" (Key Type) Parameter value');
|
|
434
|
+
}
|
|
435
|
+
return { algorithm, keyUsages };
|
|
436
|
+
}
|
|
437
|
+
const parse = async (jwk) => {
|
|
438
|
+
if (!jwk.alg) {
|
|
439
|
+
throw new TypeError('"alg" argument is required when "jwk.alg" is not present');
|
|
440
|
+
}
|
|
441
|
+
const { algorithm, keyUsages } = subtleMapping(jwk);
|
|
442
|
+
const rest = [
|
|
443
|
+
algorithm,
|
|
444
|
+
jwk.ext ?? false,
|
|
445
|
+
jwk.key_ops ?? keyUsages
|
|
446
|
+
];
|
|
447
|
+
const keyData = { ...jwk };
|
|
448
|
+
delete keyData.alg;
|
|
449
|
+
delete keyData.use;
|
|
450
|
+
return crypto$1.subtle.importKey("jwk", keyData, ...rest);
|
|
451
|
+
};
|
|
452
|
+
const exportKeyValue = (k) => decode(k);
|
|
453
|
+
let privCache;
|
|
454
|
+
let pubCache;
|
|
455
|
+
const isKeyObject = (key) => {
|
|
456
|
+
return key?.[Symbol.toStringTag] === "KeyObject";
|
|
457
|
+
};
|
|
458
|
+
const importAndCache = async (cache, key, jwk, alg, freeze2 = false) => {
|
|
459
|
+
let cached = cache.get(key);
|
|
460
|
+
if (cached?.[alg]) {
|
|
461
|
+
return cached[alg];
|
|
462
|
+
}
|
|
463
|
+
const cryptoKey = await parse({ ...jwk, alg });
|
|
464
|
+
if (freeze2)
|
|
465
|
+
Object.freeze(key);
|
|
466
|
+
if (!cached) {
|
|
467
|
+
cache.set(key, { [alg]: cryptoKey });
|
|
468
|
+
} else {
|
|
469
|
+
cached[alg] = cryptoKey;
|
|
470
|
+
}
|
|
471
|
+
return cryptoKey;
|
|
472
|
+
};
|
|
473
|
+
const normalizePublicKey = (key, alg) => {
|
|
474
|
+
if (isKeyObject(key)) {
|
|
475
|
+
let jwk = key.export({ format: "jwk" });
|
|
476
|
+
delete jwk.d;
|
|
477
|
+
delete jwk.dp;
|
|
478
|
+
delete jwk.dq;
|
|
479
|
+
delete jwk.p;
|
|
480
|
+
delete jwk.q;
|
|
481
|
+
delete jwk.qi;
|
|
482
|
+
if (jwk.k) {
|
|
483
|
+
return exportKeyValue(jwk.k);
|
|
484
|
+
}
|
|
485
|
+
pubCache || (pubCache = /* @__PURE__ */ new WeakMap());
|
|
486
|
+
return importAndCache(pubCache, key, jwk, alg);
|
|
487
|
+
}
|
|
488
|
+
if (isJWK(key)) {
|
|
489
|
+
if (key.k)
|
|
490
|
+
return decode(key.k);
|
|
491
|
+
pubCache || (pubCache = /* @__PURE__ */ new WeakMap());
|
|
492
|
+
const cryptoKey = importAndCache(pubCache, key, key, alg, true);
|
|
493
|
+
return cryptoKey;
|
|
494
|
+
}
|
|
495
|
+
return key;
|
|
496
|
+
};
|
|
497
|
+
const normalizePrivateKey = (key, alg) => {
|
|
498
|
+
if (isKeyObject(key)) {
|
|
499
|
+
let jwk = key.export({ format: "jwk" });
|
|
500
|
+
if (jwk.k) {
|
|
501
|
+
return exportKeyValue(jwk.k);
|
|
502
|
+
}
|
|
503
|
+
privCache || (privCache = /* @__PURE__ */ new WeakMap());
|
|
504
|
+
return importAndCache(privCache, key, jwk, alg);
|
|
505
|
+
}
|
|
506
|
+
if (isJWK(key)) {
|
|
507
|
+
if (key.k)
|
|
508
|
+
return decode(key.k);
|
|
509
|
+
privCache || (privCache = /* @__PURE__ */ new WeakMap());
|
|
510
|
+
const cryptoKey = importAndCache(privCache, key, key, alg, true);
|
|
511
|
+
return cryptoKey;
|
|
512
|
+
}
|
|
513
|
+
return key;
|
|
514
|
+
};
|
|
515
|
+
const normalize = { normalizePublicKey, normalizePrivateKey };
|
|
516
|
+
const findOid = (keyData, oid, from = 0) => {
|
|
517
|
+
if (from === 0) {
|
|
518
|
+
oid.unshift(oid.length);
|
|
519
|
+
oid.unshift(6);
|
|
520
|
+
}
|
|
521
|
+
const i = keyData.indexOf(oid[0], from);
|
|
522
|
+
if (i === -1)
|
|
523
|
+
return false;
|
|
524
|
+
const sub = keyData.subarray(i, i + oid.length);
|
|
525
|
+
if (sub.length !== oid.length)
|
|
526
|
+
return false;
|
|
527
|
+
return sub.every((value, index) => value === oid[index]) || findOid(keyData, oid, i + 1);
|
|
528
|
+
};
|
|
529
|
+
const getNamedCurve = (keyData) => {
|
|
530
|
+
switch (true) {
|
|
531
|
+
case findOid(keyData, [42, 134, 72, 206, 61, 3, 1, 7]):
|
|
532
|
+
return "P-256";
|
|
533
|
+
case findOid(keyData, [43, 129, 4, 0, 34]):
|
|
534
|
+
return "P-384";
|
|
535
|
+
case findOid(keyData, [43, 129, 4, 0, 35]):
|
|
536
|
+
return "P-521";
|
|
537
|
+
case findOid(keyData, [43, 101, 110]):
|
|
538
|
+
return "X25519";
|
|
539
|
+
case findOid(keyData, [43, 101, 111]):
|
|
540
|
+
return "X448";
|
|
541
|
+
case findOid(keyData, [43, 101, 112]):
|
|
542
|
+
return "Ed25519";
|
|
543
|
+
case findOid(keyData, [43, 101, 113]):
|
|
544
|
+
return "Ed448";
|
|
545
|
+
default:
|
|
546
|
+
throw new JOSENotSupported("Invalid or unsupported EC Key Curve or OKP Key Sub Type");
|
|
547
|
+
}
|
|
548
|
+
};
|
|
549
|
+
const genericImport = async (replace, keyFormat, pem, alg, options) => {
|
|
550
|
+
let algorithm;
|
|
551
|
+
let keyUsages;
|
|
552
|
+
const keyData = new Uint8Array(atob(pem.replace(replace, "")).split("").map((c) => c.charCodeAt(0)));
|
|
553
|
+
switch (alg) {
|
|
554
|
+
case "PS256":
|
|
555
|
+
case "PS384":
|
|
556
|
+
case "PS512":
|
|
557
|
+
algorithm = { name: "RSA-PSS", hash: `SHA-${alg.slice(-3)}` };
|
|
558
|
+
keyUsages = ["sign"];
|
|
559
|
+
break;
|
|
560
|
+
case "RS256":
|
|
561
|
+
case "RS384":
|
|
562
|
+
case "RS512":
|
|
563
|
+
algorithm = { name: "RSASSA-PKCS1-v1_5", hash: `SHA-${alg.slice(-3)}` };
|
|
564
|
+
keyUsages = ["sign"];
|
|
565
|
+
break;
|
|
566
|
+
case "RSA-OAEP":
|
|
567
|
+
case "RSA-OAEP-256":
|
|
568
|
+
case "RSA-OAEP-384":
|
|
569
|
+
case "RSA-OAEP-512":
|
|
570
|
+
algorithm = {
|
|
571
|
+
name: "RSA-OAEP",
|
|
572
|
+
hash: `SHA-${parseInt(alg.slice(-3), 10) || 1}`
|
|
573
|
+
};
|
|
574
|
+
keyUsages = ["decrypt", "unwrapKey"];
|
|
575
|
+
break;
|
|
576
|
+
case "ES256":
|
|
577
|
+
algorithm = { name: "ECDSA", namedCurve: "P-256" };
|
|
578
|
+
keyUsages = ["sign"];
|
|
579
|
+
break;
|
|
580
|
+
case "ES384":
|
|
581
|
+
algorithm = { name: "ECDSA", namedCurve: "P-384" };
|
|
582
|
+
keyUsages = ["sign"];
|
|
583
|
+
break;
|
|
584
|
+
case "ES512":
|
|
585
|
+
algorithm = { name: "ECDSA", namedCurve: "P-521" };
|
|
586
|
+
keyUsages = ["sign"];
|
|
587
|
+
break;
|
|
588
|
+
case "ECDH-ES":
|
|
589
|
+
case "ECDH-ES+A128KW":
|
|
590
|
+
case "ECDH-ES+A192KW":
|
|
591
|
+
case "ECDH-ES+A256KW": {
|
|
592
|
+
const namedCurve = getNamedCurve(keyData);
|
|
593
|
+
algorithm = namedCurve.startsWith("P-") ? { name: "ECDH", namedCurve } : { name: namedCurve };
|
|
594
|
+
keyUsages = ["deriveBits"];
|
|
595
|
+
break;
|
|
596
|
+
}
|
|
597
|
+
case "Ed25519":
|
|
598
|
+
algorithm = { name: "Ed25519" };
|
|
599
|
+
keyUsages = ["sign"];
|
|
600
|
+
break;
|
|
601
|
+
case "EdDSA":
|
|
602
|
+
algorithm = { name: getNamedCurve(keyData) };
|
|
603
|
+
keyUsages = ["sign"];
|
|
604
|
+
break;
|
|
605
|
+
default:
|
|
606
|
+
throw new JOSENotSupported('Invalid or unsupported "alg" (Algorithm) value');
|
|
607
|
+
}
|
|
608
|
+
return crypto$1.subtle.importKey(keyFormat, keyData, algorithm, false, keyUsages);
|
|
609
|
+
};
|
|
610
|
+
const fromPKCS8 = (pem, alg, options) => {
|
|
611
|
+
return genericImport(/(?:-----(?:BEGIN|END) PRIVATE KEY-----|\s)/g, "pkcs8", pem, alg);
|
|
612
|
+
};
|
|
613
|
+
async function importPKCS8(pkcs8, alg, options) {
|
|
614
|
+
if (typeof pkcs8 !== "string" || pkcs8.indexOf("-----BEGIN PRIVATE KEY-----") !== 0) {
|
|
615
|
+
throw new TypeError('"pkcs8" must be PKCS#8 formatted string');
|
|
616
|
+
}
|
|
617
|
+
return fromPKCS8(pkcs8, alg);
|
|
618
|
+
}
|
|
619
|
+
const tag = (key) => key?.[Symbol.toStringTag];
|
|
620
|
+
const jwkMatchesOp = (alg, key, usage) => {
|
|
621
|
+
if (key.use !== void 0 && key.use !== "sig") {
|
|
622
|
+
throw new TypeError("Invalid key for this operation, when present its use must be sig");
|
|
623
|
+
}
|
|
624
|
+
if (key.key_ops !== void 0 && key.key_ops.includes?.(usage) !== true) {
|
|
625
|
+
throw new TypeError(`Invalid key for this operation, when present its key_ops must include ${usage}`);
|
|
626
|
+
}
|
|
627
|
+
if (key.alg !== void 0 && key.alg !== alg) {
|
|
628
|
+
throw new TypeError(`Invalid key for this operation, when present its alg must be ${alg}`);
|
|
629
|
+
}
|
|
630
|
+
return true;
|
|
631
|
+
};
|
|
632
|
+
const symmetricTypeCheck = (alg, key, usage, allowJwk) => {
|
|
633
|
+
if (key instanceof Uint8Array)
|
|
634
|
+
return;
|
|
635
|
+
if (allowJwk && isJWK(key)) {
|
|
636
|
+
if (isSecretJWK(key) && jwkMatchesOp(alg, key, usage))
|
|
637
|
+
return;
|
|
638
|
+
throw new TypeError(`JSON Web Key for symmetric algorithms must have JWK "kty" (Key Type) equal to "oct" and the JWK "k" (Key Value) present`);
|
|
639
|
+
}
|
|
640
|
+
if (!isKeyLike(key)) {
|
|
641
|
+
throw new TypeError(withAlg(alg, key, ...types, "Uint8Array", allowJwk ? "JSON Web Key" : null));
|
|
642
|
+
}
|
|
643
|
+
if (key.type !== "secret") {
|
|
644
|
+
throw new TypeError(`${tag(key)} instances for symmetric algorithms must be of type "secret"`);
|
|
645
|
+
}
|
|
646
|
+
};
|
|
647
|
+
const asymmetricTypeCheck = (alg, key, usage, allowJwk) => {
|
|
648
|
+
if (allowJwk && isJWK(key)) {
|
|
649
|
+
switch (usage) {
|
|
650
|
+
case "sign":
|
|
651
|
+
if (isPrivateJWK(key) && jwkMatchesOp(alg, key, usage))
|
|
652
|
+
return;
|
|
653
|
+
throw new TypeError(`JSON Web Key for this operation be a private JWK`);
|
|
654
|
+
case "verify":
|
|
655
|
+
if (isPublicJWK(key) && jwkMatchesOp(alg, key, usage))
|
|
656
|
+
return;
|
|
657
|
+
throw new TypeError(`JSON Web Key for this operation be a public JWK`);
|
|
658
|
+
}
|
|
659
|
+
}
|
|
660
|
+
if (!isKeyLike(key)) {
|
|
661
|
+
throw new TypeError(withAlg(alg, key, ...types, allowJwk ? "JSON Web Key" : null));
|
|
662
|
+
}
|
|
663
|
+
if (key.type === "secret") {
|
|
664
|
+
throw new TypeError(`${tag(key)} instances for asymmetric algorithms must not be of type "secret"`);
|
|
665
|
+
}
|
|
666
|
+
if (usage === "sign" && key.type === "public") {
|
|
667
|
+
throw new TypeError(`${tag(key)} instances for asymmetric algorithm signing must be of type "private"`);
|
|
668
|
+
}
|
|
669
|
+
if (usage === "decrypt" && key.type === "public") {
|
|
670
|
+
throw new TypeError(`${tag(key)} instances for asymmetric algorithm decryption must be of type "private"`);
|
|
671
|
+
}
|
|
672
|
+
if (key.algorithm && usage === "verify" && key.type === "private") {
|
|
673
|
+
throw new TypeError(`${tag(key)} instances for asymmetric algorithm verifying must be of type "public"`);
|
|
674
|
+
}
|
|
675
|
+
if (key.algorithm && usage === "encrypt" && key.type === "private") {
|
|
676
|
+
throw new TypeError(`${tag(key)} instances for asymmetric algorithm encryption must be of type "public"`);
|
|
677
|
+
}
|
|
678
|
+
};
|
|
679
|
+
function checkKeyType(allowJwk, alg, key, usage) {
|
|
680
|
+
const symmetric = alg.startsWith("HS") || alg === "dir" || alg.startsWith("PBES2") || /^A\d{3}(?:GCM)?KW$/.test(alg);
|
|
681
|
+
if (symmetric) {
|
|
682
|
+
symmetricTypeCheck(alg, key, usage, allowJwk);
|
|
683
|
+
} else {
|
|
684
|
+
asymmetricTypeCheck(alg, key, usage, allowJwk);
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
checkKeyType.bind(void 0, false);
|
|
688
|
+
const checkKeyTypeWithJwk = checkKeyType.bind(void 0, true);
|
|
689
|
+
function validateCrit(Err, recognizedDefault, recognizedOption, protectedHeader, joseHeader) {
|
|
690
|
+
if (joseHeader.crit !== void 0 && protectedHeader?.crit === void 0) {
|
|
691
|
+
throw new Err('"crit" (Critical) Header Parameter MUST be integrity protected');
|
|
692
|
+
}
|
|
693
|
+
if (!protectedHeader || protectedHeader.crit === void 0) {
|
|
694
|
+
return /* @__PURE__ */ new Set();
|
|
695
|
+
}
|
|
696
|
+
if (!Array.isArray(protectedHeader.crit) || protectedHeader.crit.length === 0 || protectedHeader.crit.some((input) => typeof input !== "string" || input.length === 0)) {
|
|
697
|
+
throw new Err('"crit" (Critical) Header Parameter MUST be an array of non-empty strings when present');
|
|
698
|
+
}
|
|
699
|
+
let recognized;
|
|
700
|
+
if (recognizedOption !== void 0) {
|
|
701
|
+
recognized = new Map([...Object.entries(recognizedOption), ...recognizedDefault.entries()]);
|
|
702
|
+
} else {
|
|
703
|
+
recognized = recognizedDefault;
|
|
704
|
+
}
|
|
705
|
+
for (const parameter of protectedHeader.crit) {
|
|
706
|
+
if (!recognized.has(parameter)) {
|
|
707
|
+
throw new JOSENotSupported(`Extension Header Parameter "${parameter}" is not recognized`);
|
|
708
|
+
}
|
|
709
|
+
if (joseHeader[parameter] === void 0) {
|
|
710
|
+
throw new Err(`Extension Header Parameter "${parameter}" is missing`);
|
|
711
|
+
}
|
|
712
|
+
if (recognized.get(parameter) && protectedHeader[parameter] === void 0) {
|
|
713
|
+
throw new Err(`Extension Header Parameter "${parameter}" MUST be integrity protected`);
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
return new Set(protectedHeader.crit);
|
|
717
|
+
}
|
|
718
|
+
function subtleDsa(alg, algorithm) {
|
|
719
|
+
const hash = `SHA-${alg.slice(-3)}`;
|
|
720
|
+
switch (alg) {
|
|
721
|
+
case "HS256":
|
|
722
|
+
case "HS384":
|
|
723
|
+
case "HS512":
|
|
724
|
+
return { hash, name: "HMAC" };
|
|
725
|
+
case "PS256":
|
|
726
|
+
case "PS384":
|
|
727
|
+
case "PS512":
|
|
728
|
+
return { hash, name: "RSA-PSS", saltLength: alg.slice(-3) >> 3 };
|
|
729
|
+
case "RS256":
|
|
730
|
+
case "RS384":
|
|
731
|
+
case "RS512":
|
|
732
|
+
return { hash, name: "RSASSA-PKCS1-v1_5" };
|
|
733
|
+
case "ES256":
|
|
734
|
+
case "ES384":
|
|
735
|
+
case "ES512":
|
|
736
|
+
return { hash, name: "ECDSA", namedCurve: algorithm.namedCurve };
|
|
737
|
+
case "Ed25519":
|
|
738
|
+
return { name: "Ed25519" };
|
|
739
|
+
case "EdDSA":
|
|
740
|
+
return { name: algorithm.name };
|
|
741
|
+
default:
|
|
742
|
+
throw new JOSENotSupported(`alg ${alg} is not supported either by JOSE or your javascript runtime`);
|
|
743
|
+
}
|
|
744
|
+
}
|
|
745
|
+
async function getCryptoKey(alg, key, usage) {
|
|
746
|
+
{
|
|
747
|
+
key = await normalize.normalizePrivateKey(key, alg);
|
|
748
|
+
}
|
|
749
|
+
if (isCryptoKey(key)) {
|
|
750
|
+
checkSigCryptoKey(key, alg, usage);
|
|
751
|
+
return key;
|
|
752
|
+
}
|
|
753
|
+
if (key instanceof Uint8Array) {
|
|
754
|
+
if (!alg.startsWith("HS")) {
|
|
755
|
+
throw new TypeError(invalidKeyInput(key, ...types));
|
|
756
|
+
}
|
|
757
|
+
return crypto$1.subtle.importKey("raw", key, { hash: `SHA-${alg.slice(-3)}`, name: "HMAC" }, false, [usage]);
|
|
758
|
+
}
|
|
759
|
+
throw new TypeError(invalidKeyInput(key, ...types, "Uint8Array", "JSON Web Key"));
|
|
760
|
+
}
|
|
761
|
+
const epoch = (date) => Math.floor(date.getTime() / 1e3);
|
|
762
|
+
const minute = 60;
|
|
763
|
+
const hour = minute * 60;
|
|
764
|
+
const day = hour * 24;
|
|
765
|
+
const week = day * 7;
|
|
766
|
+
const year = day * 365.25;
|
|
767
|
+
const REGEX = /^(\+|\-)? ?(\d+|\d+\.\d+) ?(seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|weeks?|w|years?|yrs?|y)(?: (ago|from now))?$/i;
|
|
768
|
+
const secs = (str) => {
|
|
769
|
+
const matched = REGEX.exec(str);
|
|
770
|
+
if (!matched || matched[4] && matched[1]) {
|
|
771
|
+
throw new TypeError("Invalid time period format");
|
|
772
|
+
}
|
|
773
|
+
const value = parseFloat(matched[2]);
|
|
774
|
+
const unit = matched[3].toLowerCase();
|
|
775
|
+
let numericDate;
|
|
776
|
+
switch (unit) {
|
|
777
|
+
case "sec":
|
|
778
|
+
case "secs":
|
|
779
|
+
case "second":
|
|
780
|
+
case "seconds":
|
|
781
|
+
case "s":
|
|
782
|
+
numericDate = Math.round(value);
|
|
783
|
+
break;
|
|
784
|
+
case "minute":
|
|
785
|
+
case "minutes":
|
|
786
|
+
case "min":
|
|
787
|
+
case "mins":
|
|
788
|
+
case "m":
|
|
789
|
+
numericDate = Math.round(value * minute);
|
|
790
|
+
break;
|
|
791
|
+
case "hour":
|
|
792
|
+
case "hours":
|
|
793
|
+
case "hr":
|
|
794
|
+
case "hrs":
|
|
795
|
+
case "h":
|
|
796
|
+
numericDate = Math.round(value * hour);
|
|
797
|
+
break;
|
|
798
|
+
case "day":
|
|
799
|
+
case "days":
|
|
800
|
+
case "d":
|
|
801
|
+
numericDate = Math.round(value * day);
|
|
802
|
+
break;
|
|
803
|
+
case "week":
|
|
804
|
+
case "weeks":
|
|
805
|
+
case "w":
|
|
806
|
+
numericDate = Math.round(value * week);
|
|
807
|
+
break;
|
|
808
|
+
default:
|
|
809
|
+
numericDate = Math.round(value * year);
|
|
810
|
+
break;
|
|
811
|
+
}
|
|
812
|
+
if (matched[1] === "-" || matched[4] === "ago") {
|
|
813
|
+
return -numericDate;
|
|
814
|
+
}
|
|
815
|
+
return numericDate;
|
|
816
|
+
};
|
|
817
|
+
const sign = async (alg, key, data) => {
|
|
818
|
+
const cryptoKey = await getCryptoKey(alg, key, "sign");
|
|
819
|
+
checkKeyLength(alg, cryptoKey);
|
|
820
|
+
const signature = await crypto$1.subtle.sign(subtleDsa(alg, cryptoKey.algorithm), cryptoKey, data);
|
|
821
|
+
return new Uint8Array(signature);
|
|
822
|
+
};
|
|
823
|
+
class FlattenedSign {
|
|
824
|
+
constructor(payload) {
|
|
825
|
+
if (!(payload instanceof Uint8Array)) {
|
|
826
|
+
throw new TypeError("payload must be an instance of Uint8Array");
|
|
827
|
+
}
|
|
828
|
+
this._payload = payload;
|
|
829
|
+
}
|
|
830
|
+
setProtectedHeader(protectedHeader) {
|
|
831
|
+
if (this._protectedHeader) {
|
|
832
|
+
throw new TypeError("setProtectedHeader can only be called once");
|
|
833
|
+
}
|
|
834
|
+
this._protectedHeader = protectedHeader;
|
|
835
|
+
return this;
|
|
836
|
+
}
|
|
837
|
+
setUnprotectedHeader(unprotectedHeader) {
|
|
838
|
+
if (this._unprotectedHeader) {
|
|
839
|
+
throw new TypeError("setUnprotectedHeader can only be called once");
|
|
840
|
+
}
|
|
841
|
+
this._unprotectedHeader = unprotectedHeader;
|
|
842
|
+
return this;
|
|
843
|
+
}
|
|
844
|
+
async sign(key, options) {
|
|
845
|
+
if (!this._protectedHeader && !this._unprotectedHeader) {
|
|
846
|
+
throw new JWSInvalid("either setProtectedHeader or setUnprotectedHeader must be called before #sign()");
|
|
847
|
+
}
|
|
848
|
+
if (!isDisjoint(this._protectedHeader, this._unprotectedHeader)) {
|
|
849
|
+
throw new JWSInvalid("JWS Protected and JWS Unprotected Header Parameter names must be disjoint");
|
|
850
|
+
}
|
|
851
|
+
const joseHeader = {
|
|
852
|
+
...this._protectedHeader,
|
|
853
|
+
...this._unprotectedHeader
|
|
854
|
+
};
|
|
855
|
+
const extensions = validateCrit(JWSInvalid, /* @__PURE__ */ new Map([["b64", true]]), options?.crit, this._protectedHeader, joseHeader);
|
|
856
|
+
let b64 = true;
|
|
857
|
+
if (extensions.has("b64")) {
|
|
858
|
+
b64 = this._protectedHeader.b64;
|
|
859
|
+
if (typeof b64 !== "boolean") {
|
|
860
|
+
throw new JWSInvalid('The "b64" (base64url-encode payload) Header Parameter must be a boolean');
|
|
861
|
+
}
|
|
862
|
+
}
|
|
863
|
+
const { alg } = joseHeader;
|
|
864
|
+
if (typeof alg !== "string" || !alg) {
|
|
865
|
+
throw new JWSInvalid('JWS "alg" (Algorithm) Header Parameter missing or invalid');
|
|
866
|
+
}
|
|
867
|
+
checkKeyTypeWithJwk(alg, key, "sign");
|
|
868
|
+
let payload = this._payload;
|
|
869
|
+
if (b64) {
|
|
870
|
+
payload = encoder.encode(encode(payload));
|
|
871
|
+
}
|
|
872
|
+
let protectedHeader;
|
|
873
|
+
if (this._protectedHeader) {
|
|
874
|
+
protectedHeader = encoder.encode(encode(JSON.stringify(this._protectedHeader)));
|
|
875
|
+
} else {
|
|
876
|
+
protectedHeader = encoder.encode("");
|
|
877
|
+
}
|
|
878
|
+
const data = concat(protectedHeader, encoder.encode("."), payload);
|
|
879
|
+
const signature = await sign(alg, key, data);
|
|
880
|
+
const jws = {
|
|
881
|
+
signature: encode(signature),
|
|
882
|
+
payload: ""
|
|
883
|
+
};
|
|
884
|
+
if (b64) {
|
|
885
|
+
jws.payload = decoder.decode(payload);
|
|
886
|
+
}
|
|
887
|
+
if (this._unprotectedHeader) {
|
|
888
|
+
jws.header = this._unprotectedHeader;
|
|
889
|
+
}
|
|
890
|
+
if (this._protectedHeader) {
|
|
891
|
+
jws.protected = decoder.decode(protectedHeader);
|
|
892
|
+
}
|
|
893
|
+
return jws;
|
|
894
|
+
}
|
|
895
|
+
}
|
|
896
|
+
class CompactSign {
|
|
897
|
+
constructor(payload) {
|
|
898
|
+
this._flattened = new FlattenedSign(payload);
|
|
899
|
+
}
|
|
900
|
+
setProtectedHeader(protectedHeader) {
|
|
901
|
+
this._flattened.setProtectedHeader(protectedHeader);
|
|
902
|
+
return this;
|
|
903
|
+
}
|
|
904
|
+
async sign(key, options) {
|
|
905
|
+
const jws = await this._flattened.sign(key, options);
|
|
906
|
+
if (jws.payload === void 0) {
|
|
907
|
+
throw new TypeError("use the flattened module for creating JWS with b64: false");
|
|
908
|
+
}
|
|
909
|
+
return `${jws.protected}.${jws.payload}.${jws.signature}`;
|
|
910
|
+
}
|
|
911
|
+
}
|
|
912
|
+
function validateInput(label, input) {
|
|
913
|
+
if (!Number.isFinite(input)) {
|
|
914
|
+
throw new TypeError(`Invalid ${label} input`);
|
|
915
|
+
}
|
|
916
|
+
return input;
|
|
917
|
+
}
|
|
918
|
+
class ProduceJWT {
|
|
919
|
+
constructor(payload = {}) {
|
|
920
|
+
if (!isObject(payload)) {
|
|
921
|
+
throw new TypeError("JWT Claims Set MUST be an object");
|
|
922
|
+
}
|
|
923
|
+
this._payload = payload;
|
|
924
|
+
}
|
|
925
|
+
setIssuer(issuer) {
|
|
926
|
+
this._payload = { ...this._payload, iss: issuer };
|
|
927
|
+
return this;
|
|
928
|
+
}
|
|
929
|
+
setSubject(subject) {
|
|
930
|
+
this._payload = { ...this._payload, sub: subject };
|
|
931
|
+
return this;
|
|
932
|
+
}
|
|
933
|
+
setAudience(audience) {
|
|
934
|
+
this._payload = { ...this._payload, aud: audience };
|
|
935
|
+
return this;
|
|
936
|
+
}
|
|
937
|
+
setJti(jwtId) {
|
|
938
|
+
this._payload = { ...this._payload, jti: jwtId };
|
|
939
|
+
return this;
|
|
940
|
+
}
|
|
941
|
+
setNotBefore(input) {
|
|
942
|
+
if (typeof input === "number") {
|
|
943
|
+
this._payload = { ...this._payload, nbf: validateInput("setNotBefore", input) };
|
|
944
|
+
} else if (input instanceof Date) {
|
|
945
|
+
this._payload = { ...this._payload, nbf: validateInput("setNotBefore", epoch(input)) };
|
|
946
|
+
} else {
|
|
947
|
+
this._payload = { ...this._payload, nbf: epoch(/* @__PURE__ */ new Date()) + secs(input) };
|
|
948
|
+
}
|
|
949
|
+
return this;
|
|
950
|
+
}
|
|
951
|
+
setExpirationTime(input) {
|
|
952
|
+
if (typeof input === "number") {
|
|
953
|
+
this._payload = { ...this._payload, exp: validateInput("setExpirationTime", input) };
|
|
954
|
+
} else if (input instanceof Date) {
|
|
955
|
+
this._payload = { ...this._payload, exp: validateInput("setExpirationTime", epoch(input)) };
|
|
956
|
+
} else {
|
|
957
|
+
this._payload = { ...this._payload, exp: epoch(/* @__PURE__ */ new Date()) + secs(input) };
|
|
958
|
+
}
|
|
959
|
+
return this;
|
|
960
|
+
}
|
|
961
|
+
setIssuedAt(input) {
|
|
962
|
+
if (typeof input === "undefined") {
|
|
963
|
+
this._payload = { ...this._payload, iat: epoch(/* @__PURE__ */ new Date()) };
|
|
964
|
+
} else if (input instanceof Date) {
|
|
965
|
+
this._payload = { ...this._payload, iat: validateInput("setIssuedAt", epoch(input)) };
|
|
966
|
+
} else if (typeof input === "string") {
|
|
967
|
+
this._payload = {
|
|
968
|
+
...this._payload,
|
|
969
|
+
iat: validateInput("setIssuedAt", epoch(/* @__PURE__ */ new Date()) + secs(input))
|
|
970
|
+
};
|
|
971
|
+
} else {
|
|
972
|
+
this._payload = { ...this._payload, iat: validateInput("setIssuedAt", input) };
|
|
973
|
+
}
|
|
974
|
+
return this;
|
|
975
|
+
}
|
|
976
|
+
}
|
|
977
|
+
class SignJWT extends ProduceJWT {
|
|
978
|
+
setProtectedHeader(protectedHeader) {
|
|
979
|
+
this._protectedHeader = protectedHeader;
|
|
980
|
+
return this;
|
|
981
|
+
}
|
|
982
|
+
async sign(key, options) {
|
|
983
|
+
const sig = new CompactSign(encoder.encode(JSON.stringify(this._payload)));
|
|
984
|
+
sig.setProtectedHeader(this._protectedHeader);
|
|
985
|
+
if (Array.isArray(this._protectedHeader?.crit) && this._protectedHeader.crit.includes("b64") && this._protectedHeader.b64 === false) {
|
|
986
|
+
throw new JWTInvalid("JWTs MUST NOT use unencoded payload");
|
|
987
|
+
}
|
|
988
|
+
return sig.sign(key, options);
|
|
989
|
+
}
|
|
990
|
+
}
|
|
991
|
+
async function getAccessTokenFromServiceAccount(serviceAccountKey) {
|
|
992
|
+
const now = Math.floor(Date.now() / 1e3);
|
|
993
|
+
let privateKeyPem = serviceAccountKey.private_key;
|
|
994
|
+
if (privateKeyPem) {
|
|
995
|
+
privateKeyPem = privateKeyPem.trim();
|
|
996
|
+
if (!privateKeyPem.includes("-----BEGIN")) {
|
|
997
|
+
const keyContent = privateKeyPem.replace(/\s/g, "");
|
|
998
|
+
const formattedKey = keyContent.match(/.{1,64}/g)?.join("\n") || keyContent;
|
|
999
|
+
privateKeyPem = `-----BEGIN PRIVATE KEY-----
|
|
1000
|
+
${formattedKey}
|
|
1001
|
+
-----END PRIVATE KEY-----`;
|
|
1002
|
+
} else {
|
|
1003
|
+
privateKeyPem = privateKeyPem.replace(/\r\n/g, "\n").replace(/\r/g, "\n");
|
|
1004
|
+
if (privateKeyPem.includes("BEGIN RSA PRIVATE KEY")) {
|
|
1005
|
+
throw new Error(
|
|
1006
|
+
"Private key is in PKCS#1 format (RSA PRIVATE KEY). Please download a new service account key from Google Cloud Console. The key should be in PKCS#8 format (PRIVATE KEY)."
|
|
1007
|
+
);
|
|
1008
|
+
}
|
|
1009
|
+
const keyMatch = privateKeyPem.match(/-----BEGIN PRIVATE KEY-----\n?([\s\S]*?)\n?-----END PRIVATE KEY-----/);
|
|
1010
|
+
if (keyMatch) {
|
|
1011
|
+
const keyContent = keyMatch[1].replace(/\s/g, "");
|
|
1012
|
+
if (!keyContent.includes("\n") || keyContent.length > 64) {
|
|
1013
|
+
const formattedKey = keyContent.match(/.{1,64}/g)?.join("\n") || keyContent;
|
|
1014
|
+
privateKeyPem = `-----BEGIN PRIVATE KEY-----
|
|
1015
|
+
${formattedKey}
|
|
1016
|
+
-----END PRIVATE KEY-----`;
|
|
1017
|
+
}
|
|
1018
|
+
}
|
|
1019
|
+
}
|
|
1020
|
+
} else {
|
|
1021
|
+
throw new Error("Private key is missing from service account key");
|
|
1022
|
+
}
|
|
1023
|
+
try {
|
|
1024
|
+
const privateKey = await importPKCS8(privateKeyPem, "RS256");
|
|
1025
|
+
const jwt = await new SignJWT({
|
|
1026
|
+
scope: "https://www.googleapis.com/auth/cloud-platform"
|
|
1027
|
+
}).setProtectedHeader({ alg: "RS256" }).setIssuedAt(now).setExpirationTime(now + 3600).setIssuer(serviceAccountKey.client_email).setSubject(serviceAccountKey.client_email).setAudience("https://oauth2.googleapis.com/token").sign(privateKey);
|
|
1028
|
+
const response = await fetch("https://oauth2.googleapis.com/token", {
|
|
1029
|
+
method: "POST",
|
|
1030
|
+
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
|
1031
|
+
body: new URLSearchParams({
|
|
1032
|
+
grant_type: "urn:ietf:params:oauth:grant-type:jwt-bearer",
|
|
1033
|
+
assertion: jwt
|
|
1034
|
+
})
|
|
1035
|
+
});
|
|
1036
|
+
if (!response.ok) {
|
|
1037
|
+
const error = await response.json();
|
|
1038
|
+
throw new Error(error.error_description || "Failed to get access token");
|
|
1039
|
+
}
|
|
1040
|
+
const data = await response.json();
|
|
1041
|
+
return data.access_token;
|
|
1042
|
+
} catch (error) {
|
|
1043
|
+
if (error.message && error.message.includes("pkcs8")) {
|
|
1044
|
+
throw new Error(
|
|
1045
|
+
"Invalid private key format. The service account key must be in PKCS#8 format. Please ensure your service account key JSON file has a properly formatted private_key field. If you downloaded the key from Google Cloud Console, it should already be in the correct format."
|
|
1046
|
+
);
|
|
1047
|
+
}
|
|
1048
|
+
throw error;
|
|
1049
|
+
}
|
|
1050
|
+
}
|
|
1051
|
+
async function getAccessToken(config) {
|
|
1052
|
+
if (config.accessToken) {
|
|
1053
|
+
return config.accessToken;
|
|
1054
|
+
}
|
|
1055
|
+
if (!config.serviceAccountKey) {
|
|
1056
|
+
throw new Error("Either serviceAccountKey or accessToken must be provided");
|
|
1057
|
+
}
|
|
1058
|
+
return await getAccessTokenFromServiceAccount(config.serviceAccountKey);
|
|
1059
|
+
}
|
|
1060
|
+
function createSessionPath(projectId, location, agentId, sessionId) {
|
|
1061
|
+
return `projects/${projectId}/locations/${location}/agents/${agentId}/sessions/${sessionId}`;
|
|
1062
|
+
}
|
|
1063
|
+
function extractRichContent(responseMessages) {
|
|
1064
|
+
let richContent = null;
|
|
1065
|
+
for (const msg of responseMessages) {
|
|
1066
|
+
if (msg.payload) {
|
|
1067
|
+
if (msg.payload.richContent) {
|
|
1068
|
+
richContent = msg.payload.richContent;
|
|
1069
|
+
break;
|
|
1070
|
+
}
|
|
1071
|
+
if (msg.payload.fields && msg.payload.fields.richContent) {
|
|
1072
|
+
const richContentValue = msg.payload.fields.richContent;
|
|
1073
|
+
if (richContentValue.listValue && richContentValue.listValue.values) {
|
|
1074
|
+
richContent = richContentValue.listValue.values.map((v) => {
|
|
1075
|
+
if (v.listValue && v.listValue.values) {
|
|
1076
|
+
return v.listValue.values.map((item) => {
|
|
1077
|
+
if (item.structValue && item.structValue.fields) {
|
|
1078
|
+
const fields = item.structValue.fields;
|
|
1079
|
+
if (fields.type && fields.options) {
|
|
1080
|
+
return {
|
|
1081
|
+
type: fields.type.stringValue || fields.type,
|
|
1082
|
+
options: fields.options.listValue ? fields.options.listValue.values.map((opt) => ({
|
|
1083
|
+
text: opt.structValue?.fields?.text?.stringValue || "",
|
|
1084
|
+
payload: opt.structValue?.fields?.payload?.stringValue || ""
|
|
1085
|
+
})) : []
|
|
1086
|
+
};
|
|
1087
|
+
}
|
|
1088
|
+
}
|
|
1089
|
+
return item;
|
|
1090
|
+
});
|
|
1091
|
+
}
|
|
1092
|
+
return v;
|
|
1093
|
+
});
|
|
1094
|
+
} else if (typeof richContentValue === "object" && !richContentValue.listValue) {
|
|
1095
|
+
richContent = richContentValue;
|
|
1096
|
+
}
|
|
1097
|
+
break;
|
|
1098
|
+
}
|
|
1099
|
+
}
|
|
1100
|
+
}
|
|
1101
|
+
return richContent;
|
|
1102
|
+
}
|
|
1103
|
+
async function createDialogflowSession(config) {
|
|
1104
|
+
try {
|
|
1105
|
+
const accessToken = await getAccessToken(config);
|
|
1106
|
+
const sessionId = `session_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
|
|
1107
|
+
const location = config.dfLocation.split(" ")[0].trim();
|
|
1108
|
+
const sessionPath = createSessionPath(
|
|
1109
|
+
config.dfProjectId,
|
|
1110
|
+
location,
|
|
1111
|
+
config.dfAgentId,
|
|
1112
|
+
sessionId
|
|
1113
|
+
);
|
|
1114
|
+
const apiEndpoint = `https://${location}-dialogflow.googleapis.com/v3/${sessionPath}:detectIntent`;
|
|
1115
|
+
const request = {
|
|
1116
|
+
queryInput: {
|
|
1117
|
+
text: {
|
|
1118
|
+
text: "hello"
|
|
1119
|
+
},
|
|
1120
|
+
languageCode: config.languageCode || "en"
|
|
1121
|
+
}
|
|
1122
|
+
};
|
|
1123
|
+
const response = await fetch(apiEndpoint, {
|
|
1124
|
+
method: "POST",
|
|
1125
|
+
headers: {
|
|
1126
|
+
"Authorization": `Bearer ${accessToken}`,
|
|
1127
|
+
"Content-Type": "application/json"
|
|
1128
|
+
},
|
|
1129
|
+
body: JSON.stringify(request)
|
|
1130
|
+
});
|
|
1131
|
+
if (!response.ok) {
|
|
1132
|
+
const error = await response.json().catch(() => ({}));
|
|
1133
|
+
throw new Error(error.error?.message || `HTTP error! status: ${response.status}`);
|
|
1134
|
+
}
|
|
1135
|
+
const data = await response.json();
|
|
1136
|
+
let welcomeMessage = "Hello! I'm BlockSpark AI Assistant. How can I help you today?";
|
|
1137
|
+
let richContent = null;
|
|
1138
|
+
if (data.queryResult?.responseMessages) {
|
|
1139
|
+
const textMessages = data.queryResult.responseMessages.filter((msg) => msg.text).map((msg) => msg.text.text.join(" "));
|
|
1140
|
+
if (textMessages.length > 0) {
|
|
1141
|
+
welcomeMessage = textMessages.join(" ");
|
|
1142
|
+
}
|
|
1143
|
+
richContent = extractRichContent(data.queryResult.responseMessages);
|
|
1144
|
+
} else if (data.queryResult?.fulfillmentText) {
|
|
1145
|
+
welcomeMessage = data.queryResult.fulfillmentText;
|
|
1146
|
+
}
|
|
1147
|
+
return {
|
|
1148
|
+
session_id: sessionId,
|
|
1149
|
+
message: welcomeMessage,
|
|
1150
|
+
...richContent && { richContent }
|
|
1151
|
+
};
|
|
1152
|
+
} catch (error) {
|
|
1153
|
+
console.error("Error creating Dialogflow session:", error);
|
|
1154
|
+
const errorMessage = error.message || "Failed to create session";
|
|
1155
|
+
if (errorMessage.includes("401") || errorMessage.includes("Unauthorized")) {
|
|
1156
|
+
throw new Error("Authentication failed. Please check your service account key or access token.");
|
|
1157
|
+
} else if (errorMessage.includes("403") || errorMessage.includes("Forbidden")) {
|
|
1158
|
+
throw new Error("Access forbidden. Please check your Dialogflow API permissions.");
|
|
1159
|
+
} else if (errorMessage.includes("404") || errorMessage.includes("Not Found")) {
|
|
1160
|
+
throw new Error("Dialogflow agent not found. Please check your project ID, location, and agent ID.");
|
|
1161
|
+
}
|
|
1162
|
+
throw new Error(errorMessage);
|
|
1163
|
+
}
|
|
1164
|
+
}
|
|
1165
|
+
async function sendDialogflowMessage(message2, sessionId, config) {
|
|
1166
|
+
try {
|
|
1167
|
+
const accessToken = await getAccessToken(config);
|
|
1168
|
+
const location = config.dfLocation.split(" ")[0].trim();
|
|
1169
|
+
const sessionPath = createSessionPath(
|
|
1170
|
+
config.dfProjectId,
|
|
1171
|
+
location,
|
|
1172
|
+
config.dfAgentId,
|
|
1173
|
+
sessionId
|
|
1174
|
+
);
|
|
1175
|
+
const apiEndpoint = `https://${location}-dialogflow.googleapis.com/v3/${sessionPath}:detectIntent`;
|
|
1176
|
+
const request = {
|
|
1177
|
+
queryInput: {
|
|
1178
|
+
text: {
|
|
1179
|
+
text: message2.trim()
|
|
1180
|
+
},
|
|
1181
|
+
languageCode: config.languageCode || "en"
|
|
1182
|
+
}
|
|
1183
|
+
};
|
|
1184
|
+
const response = await fetch(apiEndpoint, {
|
|
1185
|
+
method: "POST",
|
|
1186
|
+
headers: {
|
|
1187
|
+
"Authorization": `Bearer ${accessToken}`,
|
|
1188
|
+
"Content-Type": "application/json"
|
|
1189
|
+
},
|
|
1190
|
+
body: JSON.stringify(request)
|
|
1191
|
+
});
|
|
1192
|
+
if (!response.ok) {
|
|
1193
|
+
const errorText = await response.text();
|
|
1194
|
+
let errorData = {};
|
|
1195
|
+
try {
|
|
1196
|
+
errorData = JSON.parse(errorText);
|
|
1197
|
+
} catch {
|
|
1198
|
+
errorData = { message: errorText || `HTTP ${response.status}` };
|
|
1199
|
+
}
|
|
1200
|
+
const errorMessage = errorData.error?.message || errorData.message || `HTTP error! status: ${response.status}`;
|
|
1201
|
+
console.error("Dialogflow API Error (sendMessage):", {
|
|
1202
|
+
status: response.status,
|
|
1203
|
+
statusText: response.statusText,
|
|
1204
|
+
error: errorData,
|
|
1205
|
+
endpoint: apiEndpoint
|
|
1206
|
+
});
|
|
1207
|
+
throw new Error(errorMessage);
|
|
1208
|
+
}
|
|
1209
|
+
const data = await response.json();
|
|
1210
|
+
let responseText = "I'm sorry, I didn't understand that. Could you please rephrase?";
|
|
1211
|
+
let richContent = null;
|
|
1212
|
+
let handoff = false;
|
|
1213
|
+
if (data.queryResult?.parameters?.fields?.handoff?.boolValue === true) {
|
|
1214
|
+
handoff = true;
|
|
1215
|
+
} else if (data.queryResult?.responseMessages) {
|
|
1216
|
+
for (const msg of data.queryResult.responseMessages) {
|
|
1217
|
+
if (msg.payload) {
|
|
1218
|
+
if (typeof msg.payload === "object") {
|
|
1219
|
+
if (msg.payload.handoff === true) {
|
|
1220
|
+
handoff = true;
|
|
1221
|
+
break;
|
|
1222
|
+
}
|
|
1223
|
+
if (msg.payload.fields?.handoff?.boolValue === true) {
|
|
1224
|
+
handoff = true;
|
|
1225
|
+
break;
|
|
1226
|
+
}
|
|
1227
|
+
}
|
|
1228
|
+
}
|
|
1229
|
+
}
|
|
1230
|
+
}
|
|
1231
|
+
if (data.handoff === true) {
|
|
1232
|
+
handoff = true;
|
|
1233
|
+
}
|
|
1234
|
+
if (data.queryResult?.responseMessages) {
|
|
1235
|
+
const textMessages = data.queryResult.responseMessages.filter((msg) => msg.text).map((msg) => msg.text.text.join(" "));
|
|
1236
|
+
if (textMessages.length > 0) {
|
|
1237
|
+
responseText = textMessages.join(" ");
|
|
1238
|
+
}
|
|
1239
|
+
richContent = extractRichContent(data.queryResult.responseMessages);
|
|
1240
|
+
} else if (data.queryResult?.fulfillmentText) {
|
|
1241
|
+
responseText = data.queryResult.fulfillmentText;
|
|
1242
|
+
}
|
|
1243
|
+
return {
|
|
1244
|
+
response: responseText,
|
|
1245
|
+
session_id: sessionId,
|
|
1246
|
+
source: "dialogflow",
|
|
1247
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
1248
|
+
...richContent && { richContent },
|
|
1249
|
+
...handoff && { handoff: true }
|
|
1250
|
+
};
|
|
1251
|
+
} catch (error) {
|
|
1252
|
+
console.error("Error sending message to Dialogflow:", error);
|
|
1253
|
+
const errorMessage = error.message || "Failed to send message";
|
|
1254
|
+
if (errorMessage.includes("401") || errorMessage.includes("Unauthorized")) {
|
|
1255
|
+
throw new Error("Authentication failed. Please check your service account key or access token.");
|
|
1256
|
+
} else if (errorMessage.includes("403") || errorMessage.includes("Forbidden")) {
|
|
1257
|
+
throw new Error("Access forbidden. Please check your Dialogflow API permissions.");
|
|
1258
|
+
} else if (errorMessage.includes("404") || errorMessage.includes("Not Found")) {
|
|
1259
|
+
throw new Error("Dialogflow agent not found. Please check your project ID, location, and agent ID.");
|
|
1260
|
+
} else if (errorMessage.includes("CORS")) {
|
|
1261
|
+
throw new Error("CORS error. Dialogflow API may not allow browser requests. Consider using a backend proxy.");
|
|
1262
|
+
}
|
|
1263
|
+
throw new Error(errorMessage);
|
|
1264
|
+
}
|
|
1265
|
+
}
|
|
1266
|
+
const SESSION_KEY = "chat_session_id";
|
|
1267
|
+
class ChatSessionManager {
|
|
1268
|
+
constructor() {
|
|
1269
|
+
this.sessionId = null;
|
|
1270
|
+
this.sessionId = this.loadSessionId();
|
|
1271
|
+
}
|
|
1272
|
+
loadSessionId() {
|
|
1273
|
+
if (typeof localStorage !== "undefined") {
|
|
1274
|
+
return localStorage.getItem(SESSION_KEY);
|
|
1275
|
+
}
|
|
1276
|
+
return null;
|
|
1277
|
+
}
|
|
1278
|
+
saveSessionId(sessionId) {
|
|
1279
|
+
this.sessionId = sessionId;
|
|
1280
|
+
if (typeof localStorage !== "undefined") {
|
|
1281
|
+
if (sessionId) {
|
|
1282
|
+
localStorage.setItem(SESSION_KEY, sessionId);
|
|
1283
|
+
} else {
|
|
1284
|
+
localStorage.removeItem(SESSION_KEY);
|
|
1285
|
+
}
|
|
1286
|
+
}
|
|
1287
|
+
}
|
|
1288
|
+
updateSessionFromResponse(responseData) {
|
|
1289
|
+
const sid = responseData?.data?.session_id || responseData?.session_id;
|
|
1290
|
+
if (sid && typeof sid === "string") {
|
|
1291
|
+
this.saveSessionId(sid);
|
|
1292
|
+
}
|
|
1293
|
+
}
|
|
1294
|
+
getSessionId() {
|
|
1295
|
+
return this.sessionId || "";
|
|
1296
|
+
}
|
|
1297
|
+
getSessionHeader() {
|
|
1298
|
+
return {
|
|
1299
|
+
"X-Session-ID": this.getSessionId()
|
|
1300
|
+
};
|
|
1301
|
+
}
|
|
1302
|
+
}
|
|
1303
|
+
let sessionManagerInstance = null;
|
|
1304
|
+
function getSessionManager() {
|
|
1305
|
+
if (!sessionManagerInstance) {
|
|
1306
|
+
sessionManagerInstance = new ChatSessionManager();
|
|
1307
|
+
}
|
|
1308
|
+
return sessionManagerInstance;
|
|
1309
|
+
}
|
|
1310
|
+
class ChatResolvedError extends Error {
|
|
1311
|
+
constructor(message2 = "chat_resolved") {
|
|
1312
|
+
super(message2);
|
|
1313
|
+
this.reason = "chat_resolved";
|
|
1314
|
+
this.name = "ChatResolvedError";
|
|
1315
|
+
}
|
|
1316
|
+
}
|
|
1317
|
+
const DEFAULT_BASE_URL = typeof process !== "undefined" && process.env?.REACT_APP_BACKEND_BASE_URL;
|
|
1318
|
+
const DEFAULT_WS_URL = typeof process !== "undefined" && process.env?.REACT_APP_BACKEND_WS_URL;
|
|
1319
|
+
class ChatService {
|
|
1320
|
+
constructor(config = {}) {
|
|
1321
|
+
this.ws = null;
|
|
1322
|
+
this.wsReconnectAttempts = 0;
|
|
1323
|
+
this.maxReconnectAttempts = 5;
|
|
1324
|
+
this.reconnectTimeout = null;
|
|
1325
|
+
this.pingInterval = null;
|
|
1326
|
+
this.currentChatId = null;
|
|
1327
|
+
this.currentSessionId = null;
|
|
1328
|
+
this.messageHandlers = /* @__PURE__ */ new Set();
|
|
1329
|
+
this.connectionHandlers = /* @__PURE__ */ new Set();
|
|
1330
|
+
this.baseUrl = config.baseUrl || DEFAULT_BASE_URL;
|
|
1331
|
+
this.wsUrl = config.wsUrl || DEFAULT_WS_URL;
|
|
1332
|
+
this.debug = config.debug || false;
|
|
1333
|
+
this.sessionManager = getSessionManager();
|
|
1334
|
+
}
|
|
1335
|
+
/**
|
|
1336
|
+
* Start a new support chat session
|
|
1337
|
+
* @param dialogflowSessionId - Optional: Dialogflow session ID
|
|
1338
|
+
* @param customerName - Optional: Customer name
|
|
1339
|
+
* @param customerEmail - Optional: Customer email
|
|
1340
|
+
* @param customerMobile - Optional: Customer mobile number
|
|
1341
|
+
*/
|
|
1342
|
+
async startSupportChat(dialogflowSessionId, customerName, customerEmail, customerMobile) {
|
|
1343
|
+
try {
|
|
1344
|
+
const headers = {
|
|
1345
|
+
"Content-Type": "application/json",
|
|
1346
|
+
...this.sessionManager.getSessionHeader()
|
|
1347
|
+
};
|
|
1348
|
+
if (dialogflowSessionId) {
|
|
1349
|
+
headers["X-Session-ID"] = dialogflowSessionId;
|
|
1350
|
+
}
|
|
1351
|
+
const body = {};
|
|
1352
|
+
if (customerName) body.name = customerName;
|
|
1353
|
+
if (customerEmail) body.email = customerEmail;
|
|
1354
|
+
if (customerMobile) body.mobile = customerMobile;
|
|
1355
|
+
const response = await fetch(`${this.baseUrl}/api/support/chat/start`, {
|
|
1356
|
+
method: "POST",
|
|
1357
|
+
headers,
|
|
1358
|
+
body: JSON.stringify(body)
|
|
1359
|
+
});
|
|
1360
|
+
if (!response.ok) {
|
|
1361
|
+
const contentType2 = response.headers.get("content-type");
|
|
1362
|
+
let error = {};
|
|
1363
|
+
if (contentType2 && contentType2.includes("application/json")) {
|
|
1364
|
+
try {
|
|
1365
|
+
error = await response.json();
|
|
1366
|
+
} catch {
|
|
1367
|
+
error = {};
|
|
1368
|
+
}
|
|
1369
|
+
} else {
|
|
1370
|
+
const text2 = await response.text();
|
|
1371
|
+
throw new Error(
|
|
1372
|
+
`API endpoint returned HTML instead of JSON. Status: ${response.status}. This usually means the endpoint doesn't exist or the server is misconfigured. URL: ${this.baseUrl}/api/support/chat/start`
|
|
1373
|
+
);
|
|
1374
|
+
}
|
|
1375
|
+
throw new Error(
|
|
1376
|
+
error.message || `HTTP error! status: ${response.status}`
|
|
1377
|
+
);
|
|
1378
|
+
}
|
|
1379
|
+
const contentType = response.headers.get("content-type");
|
|
1380
|
+
if (!contentType || !contentType.includes("application/json")) {
|
|
1381
|
+
const text2 = await response.text();
|
|
1382
|
+
throw new Error(
|
|
1383
|
+
`API endpoint returned non-JSON response. Status: ${response.status}. Content-Type: ${contentType || "unknown"}. This usually means the endpoint doesn't exist or the server is misconfigured. URL: ${this.baseUrl}/api/support/chat/start`
|
|
1384
|
+
);
|
|
1385
|
+
}
|
|
1386
|
+
const data = await response.json();
|
|
1387
|
+
this.sessionManager.updateSessionFromResponse(data);
|
|
1388
|
+
if (data.status && data.data) {
|
|
1389
|
+
return {
|
|
1390
|
+
chat_id: data.data.chat_id,
|
|
1391
|
+
session_id: data.data.session_id
|
|
1392
|
+
};
|
|
1393
|
+
}
|
|
1394
|
+
if (data.chat_id && data.session_id) {
|
|
1395
|
+
return {
|
|
1396
|
+
chat_id: data.chat_id,
|
|
1397
|
+
session_id: data.session_id
|
|
1398
|
+
};
|
|
1399
|
+
}
|
|
1400
|
+
throw new Error("Invalid response format from chat start endpoint");
|
|
1401
|
+
} catch (error) {
|
|
1402
|
+
console.error("Error starting support chat:", error);
|
|
1403
|
+
throw new Error(
|
|
1404
|
+
error.message || "Failed to start support chat session"
|
|
1405
|
+
);
|
|
1406
|
+
}
|
|
1407
|
+
}
|
|
1408
|
+
/**
|
|
1409
|
+
* Request handoff to human agent
|
|
1410
|
+
* @param chatId - Chat ID (must be valid, not undefined)
|
|
1411
|
+
* @param sessionId - Session ID
|
|
1412
|
+
* @param reason - Optional: Reason for handoff
|
|
1413
|
+
* @param dialogflowSessionId - Optional: Dialogflow session ID
|
|
1414
|
+
* @param customerName - Optional: Customer name
|
|
1415
|
+
* @param customerEmail - Optional: Customer email
|
|
1416
|
+
* @param customerMobile - Optional: Customer mobile number
|
|
1417
|
+
*/
|
|
1418
|
+
async requestHandoff(chatId, sessionId, reason, dialogflowSessionId, customerName, customerEmail, customerMobile) {
|
|
1419
|
+
if (!chatId || chatId === "undefined" || chatId === "null") {
|
|
1420
|
+
throw new Error("Invalid chat_id. Chat must be initialized first.");
|
|
1421
|
+
}
|
|
1422
|
+
try {
|
|
1423
|
+
const headers = {
|
|
1424
|
+
"Content-Type": "application/json",
|
|
1425
|
+
...this.sessionManager.getSessionHeader()
|
|
1426
|
+
};
|
|
1427
|
+
if (sessionId) {
|
|
1428
|
+
headers["X-Session-ID"] = sessionId;
|
|
1429
|
+
} else if (dialogflowSessionId) {
|
|
1430
|
+
headers["X-Session-ID"] = dialogflowSessionId;
|
|
1431
|
+
}
|
|
1432
|
+
const body = {};
|
|
1433
|
+
if (reason) body.reason = reason;
|
|
1434
|
+
if (customerName) body.name = customerName;
|
|
1435
|
+
if (customerEmail) body.email = customerEmail;
|
|
1436
|
+
if (customerMobile) body.mobile = customerMobile;
|
|
1437
|
+
const response = await fetch(
|
|
1438
|
+
`${this.baseUrl}/api/support/chat/${chatId}/handoff`,
|
|
1439
|
+
{
|
|
1440
|
+
method: "POST",
|
|
1441
|
+
headers,
|
|
1442
|
+
body: JSON.stringify(body)
|
|
1443
|
+
}
|
|
1444
|
+
);
|
|
1445
|
+
const contentType = response.headers.get("content-type");
|
|
1446
|
+
if (response.status === 400 || response.status === 404) {
|
|
1447
|
+
let error = {};
|
|
1448
|
+
if (contentType && contentType.includes("application/json")) {
|
|
1449
|
+
try {
|
|
1450
|
+
error = await response.json();
|
|
1451
|
+
} catch {
|
|
1452
|
+
error = {};
|
|
1453
|
+
}
|
|
1454
|
+
} else {
|
|
1455
|
+
const text2 = await response.text();
|
|
1456
|
+
throw new Error(
|
|
1457
|
+
`API endpoint returned HTML instead of JSON. Status: ${response.status}. This usually means the chat_id is invalid or the endpoint doesn't exist. URL: ${this.baseUrl}/api/support/chat/${chatId}/handoff`
|
|
1458
|
+
);
|
|
1459
|
+
}
|
|
1460
|
+
throw new Error(
|
|
1461
|
+
error.message || "Invalid chat_id. Chat may have expired."
|
|
1462
|
+
);
|
|
1463
|
+
}
|
|
1464
|
+
if (!response.ok) {
|
|
1465
|
+
let error = {};
|
|
1466
|
+
if (contentType && contentType.includes("application/json")) {
|
|
1467
|
+
try {
|
|
1468
|
+
error = await response.json();
|
|
1469
|
+
} catch {
|
|
1470
|
+
error = {};
|
|
1471
|
+
}
|
|
1472
|
+
} else {
|
|
1473
|
+
const text2 = await response.text();
|
|
1474
|
+
throw new Error(
|
|
1475
|
+
`API endpoint returned HTML instead of JSON. Status: ${response.status}. This usually means the endpoint doesn't exist or the server is misconfigured. URL: ${this.baseUrl}/api/support/chat/${chatId}/handoff`
|
|
1476
|
+
);
|
|
1477
|
+
}
|
|
1478
|
+
throw new Error(
|
|
1479
|
+
error.message || `HTTP error! status: ${response.status}`
|
|
1480
|
+
);
|
|
1481
|
+
}
|
|
1482
|
+
if (!contentType || !contentType.includes("application/json")) {
|
|
1483
|
+
const text2 = await response.text();
|
|
1484
|
+
throw new Error(
|
|
1485
|
+
`API endpoint returned non-JSON response. Status: ${response.status}. Content-Type: ${contentType || "unknown"}. This usually means the endpoint doesn't exist or the server is misconfigured. URL: ${this.baseUrl}/api/support/chat/${chatId}/handoff`
|
|
1486
|
+
);
|
|
1487
|
+
}
|
|
1488
|
+
const data = await response.json();
|
|
1489
|
+
this.sessionManager.updateSessionFromResponse(data);
|
|
1490
|
+
if (data.status) {
|
|
1491
|
+
return {
|
|
1492
|
+
success: true,
|
|
1493
|
+
message: data.message || data.data?.message
|
|
1494
|
+
};
|
|
1495
|
+
}
|
|
1496
|
+
return {
|
|
1497
|
+
success: true,
|
|
1498
|
+
message: data.message
|
|
1499
|
+
};
|
|
1500
|
+
} catch (error) {
|
|
1501
|
+
console.error("Error requesting handoff:", error);
|
|
1502
|
+
throw error;
|
|
1503
|
+
}
|
|
1504
|
+
}
|
|
1505
|
+
/**
|
|
1506
|
+
* Send message to agent via REST API
|
|
1507
|
+
*/
|
|
1508
|
+
async sendMessageToAgent(chatId, sessionId, message2) {
|
|
1509
|
+
try {
|
|
1510
|
+
const headers = {
|
|
1511
|
+
"Content-Type": "application/json",
|
|
1512
|
+
...this.sessionManager.getSessionHeader()
|
|
1513
|
+
};
|
|
1514
|
+
if (sessionId) {
|
|
1515
|
+
headers["X-Session-ID"] = sessionId;
|
|
1516
|
+
}
|
|
1517
|
+
const response = await fetch(
|
|
1518
|
+
`${this.baseUrl}/api/support/chat/${chatId}/message`,
|
|
1519
|
+
{
|
|
1520
|
+
method: "POST",
|
|
1521
|
+
headers,
|
|
1522
|
+
body: JSON.stringify({
|
|
1523
|
+
content: message2
|
|
1524
|
+
})
|
|
1525
|
+
}
|
|
1526
|
+
);
|
|
1527
|
+
const contentType = response.headers.get("content-type");
|
|
1528
|
+
if (response.status === 401 || response.status === 404) {
|
|
1529
|
+
let error = {};
|
|
1530
|
+
if (contentType && contentType.includes("application/json")) {
|
|
1531
|
+
try {
|
|
1532
|
+
error = await response.json();
|
|
1533
|
+
} catch {
|
|
1534
|
+
error = {};
|
|
1535
|
+
}
|
|
1536
|
+
} else {
|
|
1537
|
+
const text2 = await response.text();
|
|
1538
|
+
throw new Error(
|
|
1539
|
+
`API endpoint returned HTML instead of JSON. Status: ${response.status}. This usually means the chat_id is invalid or the endpoint doesn't exist. URL: ${this.baseUrl}/api/support/chat/${chatId}/message`
|
|
1540
|
+
);
|
|
1541
|
+
}
|
|
1542
|
+
throw new Error(
|
|
1543
|
+
error.message || "Chat not found or unauthorized"
|
|
1544
|
+
);
|
|
1545
|
+
}
|
|
1546
|
+
if (!response.ok) {
|
|
1547
|
+
let error = {};
|
|
1548
|
+
if (contentType && contentType.includes("application/json")) {
|
|
1549
|
+
try {
|
|
1550
|
+
error = await response.json();
|
|
1551
|
+
} catch {
|
|
1552
|
+
error = {};
|
|
1553
|
+
}
|
|
1554
|
+
} else {
|
|
1555
|
+
const text2 = await response.text();
|
|
1556
|
+
throw new Error(
|
|
1557
|
+
`API endpoint returned HTML instead of JSON. Status: ${response.status}. This usually means the endpoint doesn't exist or the server is misconfigured. URL: ${this.baseUrl}/api/support/chat/${chatId}/message`
|
|
1558
|
+
);
|
|
1559
|
+
}
|
|
1560
|
+
throw new Error(
|
|
1561
|
+
error.message || `HTTP error! status: ${response.status}`
|
|
1562
|
+
);
|
|
1563
|
+
}
|
|
1564
|
+
if (!contentType || !contentType.includes("application/json")) {
|
|
1565
|
+
const text2 = await response.text();
|
|
1566
|
+
throw new Error(
|
|
1567
|
+
`API endpoint returned non-JSON response. Status: ${response.status}. Content-Type: ${contentType || "unknown"}. This usually means the endpoint doesn't exist or the server is misconfigured. URL: ${this.baseUrl}/api/support/chat/${chatId}/message`
|
|
1568
|
+
);
|
|
1569
|
+
}
|
|
1570
|
+
const data = await response.json();
|
|
1571
|
+
this.sessionManager.updateSessionFromResponse(data);
|
|
1572
|
+
if (data?.ignored === true && data?.reason === "chat_resolved") {
|
|
1573
|
+
throw new ChatResolvedError();
|
|
1574
|
+
}
|
|
1575
|
+
return {
|
|
1576
|
+
id: data.id,
|
|
1577
|
+
sender_type: "customer",
|
|
1578
|
+
content: message2,
|
|
1579
|
+
timestamp: data.timestamp || (/* @__PURE__ */ new Date()).toISOString()
|
|
1580
|
+
};
|
|
1581
|
+
} catch (error) {
|
|
1582
|
+
if (error instanceof ChatResolvedError || error?.name === "ChatResolvedError") {
|
|
1583
|
+
throw error;
|
|
1584
|
+
}
|
|
1585
|
+
console.error("Error sending message to agent:", error);
|
|
1586
|
+
throw new Error(error.message || "Failed to send message to agent");
|
|
1587
|
+
}
|
|
1588
|
+
}
|
|
1589
|
+
/**
|
|
1590
|
+
* Ensure chat is initialized - helper method
|
|
1591
|
+
* Returns existing chat_id if valid, otherwise initializes new chat
|
|
1592
|
+
* @param existingChatId - Existing chat ID to validate
|
|
1593
|
+
* @param existingSessionId - Existing session ID
|
|
1594
|
+
* @param dialogflowSessionId - Optional: Dialogflow session ID
|
|
1595
|
+
* @param customerName - Optional: Customer name
|
|
1596
|
+
* @param customerEmail - Optional: Customer email
|
|
1597
|
+
* @param customerMobile - Optional: Customer mobile number
|
|
1598
|
+
* @returns Promise with chat_id and session_id
|
|
1599
|
+
*/
|
|
1600
|
+
async ensureChatInitialized(existingChatId, existingSessionId, dialogflowSessionId, customerName, customerEmail, customerMobile) {
|
|
1601
|
+
if (existingChatId && existingChatId !== "undefined" && existingChatId !== "null" && existingSessionId) {
|
|
1602
|
+
return {
|
|
1603
|
+
chat_id: existingChatId,
|
|
1604
|
+
session_id: existingSessionId
|
|
1605
|
+
};
|
|
1606
|
+
}
|
|
1607
|
+
return await this.startSupportChat(
|
|
1608
|
+
dialogflowSessionId || null,
|
|
1609
|
+
customerName || null,
|
|
1610
|
+
customerEmail || null,
|
|
1611
|
+
customerMobile || null
|
|
1612
|
+
);
|
|
1613
|
+
}
|
|
1614
|
+
/**
|
|
1615
|
+
* Load message history
|
|
1616
|
+
*/
|
|
1617
|
+
async loadMessageHistory(chatId, sessionId) {
|
|
1618
|
+
try {
|
|
1619
|
+
const headers = {
|
|
1620
|
+
...this.sessionManager.getSessionHeader()
|
|
1621
|
+
};
|
|
1622
|
+
if (sessionId) {
|
|
1623
|
+
headers["X-Session-ID"] = sessionId;
|
|
1624
|
+
}
|
|
1625
|
+
const response = await fetch(
|
|
1626
|
+
`${this.baseUrl}/api/support/chat/${chatId}/messages`,
|
|
1627
|
+
{
|
|
1628
|
+
method: "GET",
|
|
1629
|
+
headers
|
|
1630
|
+
}
|
|
1631
|
+
);
|
|
1632
|
+
const contentType = response.headers.get("content-type");
|
|
1633
|
+
if (response.status === 401 || response.status === 404) {
|
|
1634
|
+
let error = {};
|
|
1635
|
+
if (contentType && contentType.includes("application/json")) {
|
|
1636
|
+
try {
|
|
1637
|
+
error = await response.json();
|
|
1638
|
+
} catch {
|
|
1639
|
+
error = {};
|
|
1640
|
+
}
|
|
1641
|
+
} else {
|
|
1642
|
+
const text2 = await response.text();
|
|
1643
|
+
throw new Error(
|
|
1644
|
+
`API endpoint returned HTML instead of JSON. Status: ${response.status}. This usually means the chat_id is invalid or the endpoint doesn't exist. URL: ${this.baseUrl}/api/support/chat/${chatId}/messages`
|
|
1645
|
+
);
|
|
1646
|
+
}
|
|
1647
|
+
throw new Error(
|
|
1648
|
+
error.message || "Chat not found or unauthorized"
|
|
1649
|
+
);
|
|
1650
|
+
}
|
|
1651
|
+
if (!response.ok) {
|
|
1652
|
+
let error = {};
|
|
1653
|
+
if (contentType && contentType.includes("application/json")) {
|
|
1654
|
+
try {
|
|
1655
|
+
error = await response.json();
|
|
1656
|
+
} catch {
|
|
1657
|
+
error = {};
|
|
1658
|
+
}
|
|
1659
|
+
} else {
|
|
1660
|
+
const text2 = await response.text();
|
|
1661
|
+
throw new Error(
|
|
1662
|
+
`API endpoint returned HTML instead of JSON. Status: ${response.status}. This usually means the endpoint doesn't exist or the server is misconfigured. URL: ${this.baseUrl}/api/support/chat/${chatId}/messages`
|
|
1663
|
+
);
|
|
1664
|
+
}
|
|
1665
|
+
throw new Error(
|
|
1666
|
+
error.message || `HTTP error! status: ${response.status}`
|
|
1667
|
+
);
|
|
1668
|
+
}
|
|
1669
|
+
if (!contentType || !contentType.includes("application/json")) {
|
|
1670
|
+
const text2 = await response.text();
|
|
1671
|
+
throw new Error(
|
|
1672
|
+
`API endpoint returned non-JSON response. Status: ${response.status}. Content-Type: ${contentType || "unknown"}. This usually means the endpoint doesn't exist or the server is misconfigured. URL: ${this.baseUrl}/api/support/chat/${chatId}/messages`
|
|
1673
|
+
);
|
|
1674
|
+
}
|
|
1675
|
+
const data = await response.json();
|
|
1676
|
+
this.sessionManager.updateSessionFromResponse(data);
|
|
1677
|
+
return data.messages || [];
|
|
1678
|
+
} catch (error) {
|
|
1679
|
+
console.error("Error loading message history:", error);
|
|
1680
|
+
throw new Error(error.message || "Failed to load message history");
|
|
1681
|
+
}
|
|
1682
|
+
}
|
|
1683
|
+
/**
|
|
1684
|
+
* Connect to WebSocket
|
|
1685
|
+
*/
|
|
1686
|
+
connectWebSocket(chatId, sessionId, onMessage, onConnectionChange, onClose) {
|
|
1687
|
+
if (this.reconnectTimeout) {
|
|
1688
|
+
clearTimeout(this.reconnectTimeout);
|
|
1689
|
+
this.reconnectTimeout = null;
|
|
1690
|
+
}
|
|
1691
|
+
if (this.ws && this.ws.readyState === WebSocket.OPEN) {
|
|
1692
|
+
if (this.debug) {
|
|
1693
|
+
console.log("WebSocket already connected");
|
|
1694
|
+
}
|
|
1695
|
+
return;
|
|
1696
|
+
}
|
|
1697
|
+
this.currentChatId = chatId;
|
|
1698
|
+
this.currentSessionId = sessionId;
|
|
1699
|
+
if (onMessage) {
|
|
1700
|
+
this.messageHandlers.add(onMessage);
|
|
1701
|
+
}
|
|
1702
|
+
if (onConnectionChange) {
|
|
1703
|
+
this.connectionHandlers.add(onConnectionChange);
|
|
1704
|
+
}
|
|
1705
|
+
const sessionIdToUse = sessionId || this.sessionManager.getSessionId() || "";
|
|
1706
|
+
const wsUrl = `${this.wsUrl}/ws/support/${chatId}?X-Session-ID=${encodeURIComponent(sessionIdToUse)}`;
|
|
1707
|
+
try {
|
|
1708
|
+
this.ws = new WebSocket(wsUrl);
|
|
1709
|
+
this.ws.onopen = () => {
|
|
1710
|
+
if (this.debug) {
|
|
1711
|
+
console.log("WebSocket connected to:", wsUrl);
|
|
1712
|
+
}
|
|
1713
|
+
console.log("✅ Customer WebSocket connected:", { chatId, sessionId, wsUrl });
|
|
1714
|
+
this.wsReconnectAttempts = 0;
|
|
1715
|
+
this.connectionHandlers.forEach((handler) => handler(true));
|
|
1716
|
+
this.startPingInterval();
|
|
1717
|
+
};
|
|
1718
|
+
this.ws.onmessage = (event) => {
|
|
1719
|
+
try {
|
|
1720
|
+
const message2 = JSON.parse(event.data);
|
|
1721
|
+
if (this.debug) {
|
|
1722
|
+
console.log("WebSocket raw message received:", event.data);
|
|
1723
|
+
console.log("WebSocket parsed message:", message2);
|
|
1724
|
+
}
|
|
1725
|
+
if (message2.session_id) {
|
|
1726
|
+
this.sessionManager.updateSessionFromResponse({ session_id: message2.session_id });
|
|
1727
|
+
}
|
|
1728
|
+
if (message2.type === "agent_accepted" || message2.type === "chat_resolved" || message2.type === "chat_ended") {
|
|
1729
|
+
console.log("🔔 Received notification message:", {
|
|
1730
|
+
type: message2.type,
|
|
1731
|
+
chat_id: message2.chat_id,
|
|
1732
|
+
timestamp: message2.timestamp,
|
|
1733
|
+
to_agent: message2.to_agent,
|
|
1734
|
+
to_agent_id: message2.to_agent_id
|
|
1735
|
+
});
|
|
1736
|
+
}
|
|
1737
|
+
if (message2.type === "pong") {
|
|
1738
|
+
return;
|
|
1739
|
+
}
|
|
1740
|
+
if (this.debug && message2.type === "message") {
|
|
1741
|
+
console.log("Processing message type:", {
|
|
1742
|
+
type: message2.type,
|
|
1743
|
+
sender_type: message2.sender_type,
|
|
1744
|
+
content: message2.content?.substring(0, 50) + "...",
|
|
1745
|
+
id: message2.id
|
|
1746
|
+
});
|
|
1747
|
+
}
|
|
1748
|
+
this.messageHandlers.forEach((handler) => handler(message2));
|
|
1749
|
+
} catch (error) {
|
|
1750
|
+
console.error("Error parsing WebSocket message:", error);
|
|
1751
|
+
if (this.debug) {
|
|
1752
|
+
console.error("Raw message data:", event.data);
|
|
1753
|
+
}
|
|
1754
|
+
}
|
|
1755
|
+
};
|
|
1756
|
+
this.ws.onerror = (error) => {
|
|
1757
|
+
console.error("❌ WebSocket error:", error);
|
|
1758
|
+
this.connectionHandlers.forEach((handler) => handler(false));
|
|
1759
|
+
};
|
|
1760
|
+
this.ws.onclose = (event) => {
|
|
1761
|
+
if (this.debug) {
|
|
1762
|
+
console.log("WebSocket closed:", event.code, event.reason);
|
|
1763
|
+
}
|
|
1764
|
+
console.log("🔌 Customer WebSocket closed:", { code: event.code, reason: event.reason, chatId });
|
|
1765
|
+
this.stopPingInterval();
|
|
1766
|
+
this.connectionHandlers.forEach((handler) => handler(false));
|
|
1767
|
+
this.ws = null;
|
|
1768
|
+
onClose?.(event);
|
|
1769
|
+
if (event.code === 4e3) {
|
|
1770
|
+
this.wsReconnectAttempts = this.maxReconnectAttempts;
|
|
1771
|
+
this.currentChatId = null;
|
|
1772
|
+
this.currentSessionId = null;
|
|
1773
|
+
return;
|
|
1774
|
+
}
|
|
1775
|
+
if (this.wsReconnectAttempts < this.maxReconnectAttempts && this.currentChatId && this.currentSessionId) {
|
|
1776
|
+
this.wsReconnectAttempts++;
|
|
1777
|
+
const delay = Math.min(1e3 * Math.pow(2, this.wsReconnectAttempts), 3e4);
|
|
1778
|
+
if (this.debug) {
|
|
1779
|
+
console.log(`Reconnecting in ${delay}ms (attempt ${this.wsReconnectAttempts}/${this.maxReconnectAttempts})`);
|
|
1780
|
+
}
|
|
1781
|
+
this.reconnectTimeout = setTimeout(() => {
|
|
1782
|
+
if (this.currentChatId && this.currentSessionId) {
|
|
1783
|
+
this.connectWebSocket(this.currentChatId, this.currentSessionId, onMessage, onConnectionChange, onClose);
|
|
1784
|
+
}
|
|
1785
|
+
}, delay);
|
|
1786
|
+
} else if (this.debug) {
|
|
1787
|
+
console.error("Max reconnection attempts reached");
|
|
1788
|
+
}
|
|
1789
|
+
};
|
|
1790
|
+
} catch (error) {
|
|
1791
|
+
console.error("Error creating WebSocket connection:", error);
|
|
1792
|
+
this.connectionHandlers.forEach((handler) => handler(false));
|
|
1793
|
+
}
|
|
1794
|
+
}
|
|
1795
|
+
/**
|
|
1796
|
+
* Send message via WebSocket
|
|
1797
|
+
*/
|
|
1798
|
+
sendMessageViaWebSocket(message2) {
|
|
1799
|
+
if (!this.ws || this.ws.readyState !== WebSocket.OPEN) {
|
|
1800
|
+
return false;
|
|
1801
|
+
}
|
|
1802
|
+
try {
|
|
1803
|
+
this.ws.send(
|
|
1804
|
+
JSON.stringify({
|
|
1805
|
+
type: "message",
|
|
1806
|
+
content: message2
|
|
1807
|
+
})
|
|
1808
|
+
);
|
|
1809
|
+
return true;
|
|
1810
|
+
} catch (error) {
|
|
1811
|
+
console.error("Error sending message via WebSocket:", error);
|
|
1812
|
+
return false;
|
|
1813
|
+
}
|
|
1814
|
+
}
|
|
1815
|
+
/**
|
|
1816
|
+
* Send typing indicator via WebSocket
|
|
1817
|
+
*/
|
|
1818
|
+
sendTypingIndicator(type) {
|
|
1819
|
+
if (!this.ws || this.ws.readyState !== WebSocket.OPEN) {
|
|
1820
|
+
return false;
|
|
1821
|
+
}
|
|
1822
|
+
try {
|
|
1823
|
+
this.ws.send(
|
|
1824
|
+
JSON.stringify({
|
|
1825
|
+
type
|
|
1826
|
+
})
|
|
1827
|
+
);
|
|
1828
|
+
return true;
|
|
1829
|
+
} catch (error) {
|
|
1830
|
+
console.error(`Error sending ${type} via WebSocket:`, error);
|
|
1831
|
+
return false;
|
|
1832
|
+
}
|
|
1833
|
+
}
|
|
1834
|
+
/**
|
|
1835
|
+
* Start ping interval to keep connection alive
|
|
1836
|
+
*/
|
|
1837
|
+
startPingInterval() {
|
|
1838
|
+
this.stopPingInterval();
|
|
1839
|
+
this.pingInterval = setInterval(() => {
|
|
1840
|
+
if (this.ws && this.ws.readyState === WebSocket.OPEN) {
|
|
1841
|
+
try {
|
|
1842
|
+
this.ws.send(JSON.stringify({ type: "ping" }));
|
|
1843
|
+
} catch (error) {
|
|
1844
|
+
console.error("Error sending ping:", error);
|
|
1845
|
+
}
|
|
1846
|
+
}
|
|
1847
|
+
}, 3e4);
|
|
1848
|
+
}
|
|
1849
|
+
/**
|
|
1850
|
+
* Stop ping interval
|
|
1851
|
+
*/
|
|
1852
|
+
stopPingInterval() {
|
|
1853
|
+
if (this.pingInterval) {
|
|
1854
|
+
clearInterval(this.pingInterval);
|
|
1855
|
+
this.pingInterval = null;
|
|
1856
|
+
}
|
|
1857
|
+
}
|
|
1858
|
+
/**
|
|
1859
|
+
* Disconnect WebSocket
|
|
1860
|
+
*/
|
|
1861
|
+
disconnectWebSocket() {
|
|
1862
|
+
if (this.reconnectTimeout) {
|
|
1863
|
+
clearTimeout(this.reconnectTimeout);
|
|
1864
|
+
this.reconnectTimeout = null;
|
|
1865
|
+
}
|
|
1866
|
+
this.stopPingInterval();
|
|
1867
|
+
if (this.ws) {
|
|
1868
|
+
this.ws.close();
|
|
1869
|
+
this.ws = null;
|
|
1870
|
+
}
|
|
1871
|
+
this.messageHandlers.clear();
|
|
1872
|
+
this.connectionHandlers.clear();
|
|
1873
|
+
this.wsReconnectAttempts = 0;
|
|
1874
|
+
this.currentChatId = null;
|
|
1875
|
+
this.currentSessionId = null;
|
|
1876
|
+
}
|
|
1877
|
+
/**
|
|
1878
|
+
* Check if WebSocket is connected
|
|
1879
|
+
*/
|
|
1880
|
+
isWebSocketConnected() {
|
|
1881
|
+
return this.ws !== null && this.ws.readyState === WebSocket.OPEN;
|
|
1882
|
+
}
|
|
1883
|
+
/**
|
|
1884
|
+
* Remove message handler
|
|
1885
|
+
*/
|
|
1886
|
+
removeMessageHandler(handler) {
|
|
1887
|
+
this.messageHandlers.delete(handler);
|
|
1888
|
+
}
|
|
1889
|
+
/**
|
|
1890
|
+
* Remove connection handler
|
|
1891
|
+
*/
|
|
1892
|
+
removeConnectionHandler(handler) {
|
|
1893
|
+
this.connectionHandlers.delete(handler);
|
|
1894
|
+
}
|
|
1895
|
+
}
|
|
1896
|
+
function createChatService(config) {
|
|
1897
|
+
return new ChatService(config);
|
|
1898
|
+
}
|
|
1899
|
+
/*! @license DOMPurify 3.3.1 | (c) Cure53 and other contributors | Released under the Apache license 2.0 and Mozilla Public License 2.0 | github.com/cure53/DOMPurify/blob/3.3.1/LICENSE */
|
|
1900
|
+
const {
|
|
1901
|
+
entries,
|
|
1902
|
+
setPrototypeOf,
|
|
1903
|
+
isFrozen,
|
|
1904
|
+
getPrototypeOf,
|
|
1905
|
+
getOwnPropertyDescriptor
|
|
1906
|
+
} = Object;
|
|
1907
|
+
let {
|
|
1908
|
+
freeze,
|
|
1909
|
+
seal,
|
|
1910
|
+
create
|
|
1911
|
+
} = Object;
|
|
1912
|
+
let {
|
|
1913
|
+
apply,
|
|
1914
|
+
construct
|
|
1915
|
+
} = typeof Reflect !== "undefined" && Reflect;
|
|
1916
|
+
if (!freeze) {
|
|
1917
|
+
freeze = function freeze2(x) {
|
|
1918
|
+
return x;
|
|
1919
|
+
};
|
|
1920
|
+
}
|
|
1921
|
+
if (!seal) {
|
|
1922
|
+
seal = function seal2(x) {
|
|
1923
|
+
return x;
|
|
1924
|
+
};
|
|
1925
|
+
}
|
|
1926
|
+
if (!apply) {
|
|
1927
|
+
apply = function apply2(func, thisArg) {
|
|
1928
|
+
for (var _len = arguments.length, args = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
|
|
1929
|
+
args[_key - 2] = arguments[_key];
|
|
1930
|
+
}
|
|
1931
|
+
return func.apply(thisArg, args);
|
|
1932
|
+
};
|
|
1933
|
+
}
|
|
1934
|
+
if (!construct) {
|
|
1935
|
+
construct = function construct2(Func) {
|
|
1936
|
+
for (var _len2 = arguments.length, args = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
|
|
1937
|
+
args[_key2 - 1] = arguments[_key2];
|
|
1938
|
+
}
|
|
1939
|
+
return new Func(...args);
|
|
1940
|
+
};
|
|
1941
|
+
}
|
|
1942
|
+
const arrayForEach = unapply(Array.prototype.forEach);
|
|
1943
|
+
const arrayLastIndexOf = unapply(Array.prototype.lastIndexOf);
|
|
1944
|
+
const arrayPop = unapply(Array.prototype.pop);
|
|
1945
|
+
const arrayPush = unapply(Array.prototype.push);
|
|
1946
|
+
const arraySplice = unapply(Array.prototype.splice);
|
|
1947
|
+
const stringToLowerCase = unapply(String.prototype.toLowerCase);
|
|
1948
|
+
const stringToString = unapply(String.prototype.toString);
|
|
1949
|
+
const stringMatch = unapply(String.prototype.match);
|
|
1950
|
+
const stringReplace = unapply(String.prototype.replace);
|
|
1951
|
+
const stringIndexOf = unapply(String.prototype.indexOf);
|
|
1952
|
+
const stringTrim = unapply(String.prototype.trim);
|
|
1953
|
+
const objectHasOwnProperty = unapply(Object.prototype.hasOwnProperty);
|
|
1954
|
+
const regExpTest = unapply(RegExp.prototype.test);
|
|
1955
|
+
const typeErrorCreate = unconstruct(TypeError);
|
|
1956
|
+
function unapply(func) {
|
|
1957
|
+
return function(thisArg) {
|
|
1958
|
+
if (thisArg instanceof RegExp) {
|
|
1959
|
+
thisArg.lastIndex = 0;
|
|
1960
|
+
}
|
|
1961
|
+
for (var _len3 = arguments.length, args = new Array(_len3 > 1 ? _len3 - 1 : 0), _key3 = 1; _key3 < _len3; _key3++) {
|
|
1962
|
+
args[_key3 - 1] = arguments[_key3];
|
|
1963
|
+
}
|
|
1964
|
+
return apply(func, thisArg, args);
|
|
1965
|
+
};
|
|
1966
|
+
}
|
|
1967
|
+
function unconstruct(Func) {
|
|
1968
|
+
return function() {
|
|
1969
|
+
for (var _len4 = arguments.length, args = new Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
|
|
1970
|
+
args[_key4] = arguments[_key4];
|
|
1971
|
+
}
|
|
1972
|
+
return construct(Func, args);
|
|
1973
|
+
};
|
|
1974
|
+
}
|
|
1975
|
+
function addToSet(set, array) {
|
|
1976
|
+
let transformCaseFunc = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : stringToLowerCase;
|
|
1977
|
+
if (setPrototypeOf) {
|
|
1978
|
+
setPrototypeOf(set, null);
|
|
1979
|
+
}
|
|
1980
|
+
let l = array.length;
|
|
1981
|
+
while (l--) {
|
|
1982
|
+
let element = array[l];
|
|
1983
|
+
if (typeof element === "string") {
|
|
1984
|
+
const lcElement = transformCaseFunc(element);
|
|
1985
|
+
if (lcElement !== element) {
|
|
1986
|
+
if (!isFrozen(array)) {
|
|
1987
|
+
array[l] = lcElement;
|
|
1988
|
+
}
|
|
1989
|
+
element = lcElement;
|
|
1990
|
+
}
|
|
1991
|
+
}
|
|
1992
|
+
set[element] = true;
|
|
1993
|
+
}
|
|
1994
|
+
return set;
|
|
1995
|
+
}
|
|
1996
|
+
function cleanArray(array) {
|
|
1997
|
+
for (let index = 0; index < array.length; index++) {
|
|
1998
|
+
const isPropertyExist = objectHasOwnProperty(array, index);
|
|
1999
|
+
if (!isPropertyExist) {
|
|
2000
|
+
array[index] = null;
|
|
2001
|
+
}
|
|
2002
|
+
}
|
|
2003
|
+
return array;
|
|
2004
|
+
}
|
|
2005
|
+
function clone(object) {
|
|
2006
|
+
const newObject = create(null);
|
|
2007
|
+
for (const [property, value] of entries(object)) {
|
|
2008
|
+
const isPropertyExist = objectHasOwnProperty(object, property);
|
|
2009
|
+
if (isPropertyExist) {
|
|
2010
|
+
if (Array.isArray(value)) {
|
|
2011
|
+
newObject[property] = cleanArray(value);
|
|
2012
|
+
} else if (value && typeof value === "object" && value.constructor === Object) {
|
|
2013
|
+
newObject[property] = clone(value);
|
|
2014
|
+
} else {
|
|
2015
|
+
newObject[property] = value;
|
|
2016
|
+
}
|
|
2017
|
+
}
|
|
2018
|
+
}
|
|
2019
|
+
return newObject;
|
|
2020
|
+
}
|
|
2021
|
+
function lookupGetter(object, prop) {
|
|
2022
|
+
while (object !== null) {
|
|
2023
|
+
const desc = getOwnPropertyDescriptor(object, prop);
|
|
2024
|
+
if (desc) {
|
|
2025
|
+
if (desc.get) {
|
|
2026
|
+
return unapply(desc.get);
|
|
2027
|
+
}
|
|
2028
|
+
if (typeof desc.value === "function") {
|
|
2029
|
+
return unapply(desc.value);
|
|
2030
|
+
}
|
|
2031
|
+
}
|
|
2032
|
+
object = getPrototypeOf(object);
|
|
2033
|
+
}
|
|
2034
|
+
function fallbackValue() {
|
|
2035
|
+
return null;
|
|
2036
|
+
}
|
|
2037
|
+
return fallbackValue;
|
|
2038
|
+
}
|
|
2039
|
+
const html$1 = freeze(["a", "abbr", "acronym", "address", "area", "article", "aside", "audio", "b", "bdi", "bdo", "big", "blink", "blockquote", "body", "br", "button", "canvas", "caption", "center", "cite", "code", "col", "colgroup", "content", "data", "datalist", "dd", "decorator", "del", "details", "dfn", "dialog", "dir", "div", "dl", "dt", "element", "em", "fieldset", "figcaption", "figure", "font", "footer", "form", "h1", "h2", "h3", "h4", "h5", "h6", "head", "header", "hgroup", "hr", "html", "i", "img", "input", "ins", "kbd", "label", "legend", "li", "main", "map", "mark", "marquee", "menu", "menuitem", "meter", "nav", "nobr", "ol", "optgroup", "option", "output", "p", "picture", "pre", "progress", "q", "rp", "rt", "ruby", "s", "samp", "search", "section", "select", "shadow", "slot", "small", "source", "spacer", "span", "strike", "strong", "style", "sub", "summary", "sup", "table", "tbody", "td", "template", "textarea", "tfoot", "th", "thead", "time", "tr", "track", "tt", "u", "ul", "var", "video", "wbr"]);
|
|
2040
|
+
const svg$1 = freeze(["svg", "a", "altglyph", "altglyphdef", "altglyphitem", "animatecolor", "animatemotion", "animatetransform", "circle", "clippath", "defs", "desc", "ellipse", "enterkeyhint", "exportparts", "filter", "font", "g", "glyph", "glyphref", "hkern", "image", "inputmode", "line", "lineargradient", "marker", "mask", "metadata", "mpath", "part", "path", "pattern", "polygon", "polyline", "radialgradient", "rect", "stop", "style", "switch", "symbol", "text", "textpath", "title", "tref", "tspan", "view", "vkern"]);
|
|
2041
|
+
const svgFilters = freeze(["feBlend", "feColorMatrix", "feComponentTransfer", "feComposite", "feConvolveMatrix", "feDiffuseLighting", "feDisplacementMap", "feDistantLight", "feDropShadow", "feFlood", "feFuncA", "feFuncB", "feFuncG", "feFuncR", "feGaussianBlur", "feImage", "feMerge", "feMergeNode", "feMorphology", "feOffset", "fePointLight", "feSpecularLighting", "feSpotLight", "feTile", "feTurbulence"]);
|
|
2042
|
+
const svgDisallowed = freeze(["animate", "color-profile", "cursor", "discard", "font-face", "font-face-format", "font-face-name", "font-face-src", "font-face-uri", "foreignobject", "hatch", "hatchpath", "mesh", "meshgradient", "meshpatch", "meshrow", "missing-glyph", "script", "set", "solidcolor", "unknown", "use"]);
|
|
2043
|
+
const mathMl$1 = freeze(["math", "menclose", "merror", "mfenced", "mfrac", "mglyph", "mi", "mlabeledtr", "mmultiscripts", "mn", "mo", "mover", "mpadded", "mphantom", "mroot", "mrow", "ms", "mspace", "msqrt", "mstyle", "msub", "msup", "msubsup", "mtable", "mtd", "mtext", "mtr", "munder", "munderover", "mprescripts"]);
|
|
2044
|
+
const mathMlDisallowed = freeze(["maction", "maligngroup", "malignmark", "mlongdiv", "mscarries", "mscarry", "msgroup", "mstack", "msline", "msrow", "semantics", "annotation", "annotation-xml", "mprescripts", "none"]);
|
|
2045
|
+
const text = freeze(["#text"]);
|
|
2046
|
+
const html = freeze(["accept", "action", "align", "alt", "autocapitalize", "autocomplete", "autopictureinpicture", "autoplay", "background", "bgcolor", "border", "capture", "cellpadding", "cellspacing", "checked", "cite", "class", "clear", "color", "cols", "colspan", "controls", "controlslist", "coords", "crossorigin", "datetime", "decoding", "default", "dir", "disabled", "disablepictureinpicture", "disableremoteplayback", "download", "draggable", "enctype", "enterkeyhint", "exportparts", "face", "for", "headers", "height", "hidden", "high", "href", "hreflang", "id", "inert", "inputmode", "integrity", "ismap", "kind", "label", "lang", "list", "loading", "loop", "low", "max", "maxlength", "media", "method", "min", "minlength", "multiple", "muted", "name", "nonce", "noshade", "novalidate", "nowrap", "open", "optimum", "part", "pattern", "placeholder", "playsinline", "popover", "popovertarget", "popovertargetaction", "poster", "preload", "pubdate", "radiogroup", "readonly", "rel", "required", "rev", "reversed", "role", "rows", "rowspan", "spellcheck", "scope", "selected", "shape", "size", "sizes", "slot", "span", "srclang", "start", "src", "srcset", "step", "style", "summary", "tabindex", "title", "translate", "type", "usemap", "valign", "value", "width", "wrap", "xmlns", "slot"]);
|
|
2047
|
+
const svg = freeze(["accent-height", "accumulate", "additive", "alignment-baseline", "amplitude", "ascent", "attributename", "attributetype", "azimuth", "basefrequency", "baseline-shift", "begin", "bias", "by", "class", "clip", "clippathunits", "clip-path", "clip-rule", "color", "color-interpolation", "color-interpolation-filters", "color-profile", "color-rendering", "cx", "cy", "d", "dx", "dy", "diffuseconstant", "direction", "display", "divisor", "dur", "edgemode", "elevation", "end", "exponent", "fill", "fill-opacity", "fill-rule", "filter", "filterunits", "flood-color", "flood-opacity", "font-family", "font-size", "font-size-adjust", "font-stretch", "font-style", "font-variant", "font-weight", "fx", "fy", "g1", "g2", "glyph-name", "glyphref", "gradientunits", "gradienttransform", "height", "href", "id", "image-rendering", "in", "in2", "intercept", "k", "k1", "k2", "k3", "k4", "kerning", "keypoints", "keysplines", "keytimes", "lang", "lengthadjust", "letter-spacing", "kernelmatrix", "kernelunitlength", "lighting-color", "local", "marker-end", "marker-mid", "marker-start", "markerheight", "markerunits", "markerwidth", "maskcontentunits", "maskunits", "max", "mask", "mask-type", "media", "method", "mode", "min", "name", "numoctaves", "offset", "operator", "opacity", "order", "orient", "orientation", "origin", "overflow", "paint-order", "path", "pathlength", "patterncontentunits", "patterntransform", "patternunits", "points", "preservealpha", "preserveaspectratio", "primitiveunits", "r", "rx", "ry", "radius", "refx", "refy", "repeatcount", "repeatdur", "restart", "result", "rotate", "scale", "seed", "shape-rendering", "slope", "specularconstant", "specularexponent", "spreadmethod", "startoffset", "stddeviation", "stitchtiles", "stop-color", "stop-opacity", "stroke-dasharray", "stroke-dashoffset", "stroke-linecap", "stroke-linejoin", "stroke-miterlimit", "stroke-opacity", "stroke", "stroke-width", "style", "surfacescale", "systemlanguage", "tabindex", "tablevalues", "targetx", "targety", "transform", "transform-origin", "text-anchor", "text-decoration", "text-rendering", "textlength", "type", "u1", "u2", "unicode", "values", "viewbox", "visibility", "version", "vert-adv-y", "vert-origin-x", "vert-origin-y", "width", "word-spacing", "wrap", "writing-mode", "xchannelselector", "ychannelselector", "x", "x1", "x2", "xmlns", "y", "y1", "y2", "z", "zoomandpan"]);
|
|
2048
|
+
const mathMl = freeze(["accent", "accentunder", "align", "bevelled", "close", "columnsalign", "columnlines", "columnspan", "denomalign", "depth", "dir", "display", "displaystyle", "encoding", "fence", "frame", "height", "href", "id", "largeop", "length", "linethickness", "lspace", "lquote", "mathbackground", "mathcolor", "mathsize", "mathvariant", "maxsize", "minsize", "movablelimits", "notation", "numalign", "open", "rowalign", "rowlines", "rowspacing", "rowspan", "rspace", "rquote", "scriptlevel", "scriptminsize", "scriptsizemultiplier", "selection", "separator", "separators", "stretchy", "subscriptshift", "supscriptshift", "symmetric", "voffset", "width", "xmlns"]);
|
|
2049
|
+
const xml = freeze(["xlink:href", "xml:id", "xlink:title", "xml:space", "xmlns:xlink"]);
|
|
2050
|
+
const MUSTACHE_EXPR = seal(/\{\{[\w\W]*|[\w\W]*\}\}/gm);
|
|
2051
|
+
const ERB_EXPR = seal(/<%[\w\W]*|[\w\W]*%>/gm);
|
|
2052
|
+
const TMPLIT_EXPR = seal(/\$\{[\w\W]*/gm);
|
|
2053
|
+
const DATA_ATTR = seal(/^data-[\-\w.\u00B7-\uFFFF]+$/);
|
|
2054
|
+
const ARIA_ATTR = seal(/^aria-[\-\w]+$/);
|
|
2055
|
+
const IS_ALLOWED_URI = seal(
|
|
2056
|
+
/^(?:(?:(?:f|ht)tps?|mailto|tel|callto|sms|cid|xmpp|matrix):|[^a-z]|[a-z+.\-]+(?:[^a-z+.\-:]|$))/i
|
|
2057
|
+
// eslint-disable-line no-useless-escape
|
|
2058
|
+
);
|
|
2059
|
+
const IS_SCRIPT_OR_DATA = seal(/^(?:\w+script|data):/i);
|
|
2060
|
+
const ATTR_WHITESPACE = seal(
|
|
2061
|
+
/[\u0000-\u0020\u00A0\u1680\u180E\u2000-\u2029\u205F\u3000]/g
|
|
2062
|
+
// eslint-disable-line no-control-regex
|
|
2063
|
+
);
|
|
2064
|
+
const DOCTYPE_NAME = seal(/^html$/i);
|
|
2065
|
+
const CUSTOM_ELEMENT = seal(/^[a-z][.\w]*(-[.\w]+)+$/i);
|
|
2066
|
+
var EXPRESSIONS = /* @__PURE__ */ Object.freeze({
|
|
2067
|
+
__proto__: null,
|
|
2068
|
+
ARIA_ATTR,
|
|
2069
|
+
ATTR_WHITESPACE,
|
|
2070
|
+
CUSTOM_ELEMENT,
|
|
2071
|
+
DATA_ATTR,
|
|
2072
|
+
DOCTYPE_NAME,
|
|
2073
|
+
ERB_EXPR,
|
|
2074
|
+
IS_ALLOWED_URI,
|
|
2075
|
+
IS_SCRIPT_OR_DATA,
|
|
2076
|
+
MUSTACHE_EXPR,
|
|
2077
|
+
TMPLIT_EXPR
|
|
2078
|
+
});
|
|
2079
|
+
const NODE_TYPE = {
|
|
2080
|
+
element: 1,
|
|
2081
|
+
text: 3,
|
|
2082
|
+
// Deprecated
|
|
2083
|
+
progressingInstruction: 7,
|
|
2084
|
+
comment: 8,
|
|
2085
|
+
document: 9
|
|
2086
|
+
};
|
|
2087
|
+
const getGlobal = function getGlobal2() {
|
|
2088
|
+
return typeof window === "undefined" ? null : window;
|
|
2089
|
+
};
|
|
2090
|
+
const _createTrustedTypesPolicy = function _createTrustedTypesPolicy2(trustedTypes, purifyHostElement) {
|
|
2091
|
+
if (typeof trustedTypes !== "object" || typeof trustedTypes.createPolicy !== "function") {
|
|
2092
|
+
return null;
|
|
2093
|
+
}
|
|
2094
|
+
let suffix = null;
|
|
2095
|
+
const ATTR_NAME = "data-tt-policy-suffix";
|
|
2096
|
+
if (purifyHostElement && purifyHostElement.hasAttribute(ATTR_NAME)) {
|
|
2097
|
+
suffix = purifyHostElement.getAttribute(ATTR_NAME);
|
|
2098
|
+
}
|
|
2099
|
+
const policyName = "dompurify" + (suffix ? "#" + suffix : "");
|
|
2100
|
+
try {
|
|
2101
|
+
return trustedTypes.createPolicy(policyName, {
|
|
2102
|
+
createHTML(html2) {
|
|
2103
|
+
return html2;
|
|
2104
|
+
},
|
|
2105
|
+
createScriptURL(scriptUrl) {
|
|
2106
|
+
return scriptUrl;
|
|
2107
|
+
}
|
|
2108
|
+
});
|
|
2109
|
+
} catch (_) {
|
|
2110
|
+
console.warn("TrustedTypes policy " + policyName + " could not be created.");
|
|
2111
|
+
return null;
|
|
2112
|
+
}
|
|
2113
|
+
};
|
|
2114
|
+
const _createHooksMap = function _createHooksMap2() {
|
|
2115
|
+
return {
|
|
2116
|
+
afterSanitizeAttributes: [],
|
|
2117
|
+
afterSanitizeElements: [],
|
|
2118
|
+
afterSanitizeShadowDOM: [],
|
|
2119
|
+
beforeSanitizeAttributes: [],
|
|
2120
|
+
beforeSanitizeElements: [],
|
|
2121
|
+
beforeSanitizeShadowDOM: [],
|
|
2122
|
+
uponSanitizeAttribute: [],
|
|
2123
|
+
uponSanitizeElement: [],
|
|
2124
|
+
uponSanitizeShadowNode: []
|
|
2125
|
+
};
|
|
2126
|
+
};
|
|
2127
|
+
function createDOMPurify() {
|
|
2128
|
+
let window2 = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : getGlobal();
|
|
2129
|
+
const DOMPurify = (root) => createDOMPurify(root);
|
|
2130
|
+
DOMPurify.version = "3.3.1";
|
|
2131
|
+
DOMPurify.removed = [];
|
|
2132
|
+
if (!window2 || !window2.document || window2.document.nodeType !== NODE_TYPE.document || !window2.Element) {
|
|
2133
|
+
DOMPurify.isSupported = false;
|
|
2134
|
+
return DOMPurify;
|
|
2135
|
+
}
|
|
2136
|
+
let {
|
|
2137
|
+
document
|
|
2138
|
+
} = window2;
|
|
2139
|
+
const originalDocument = document;
|
|
2140
|
+
const currentScript = originalDocument.currentScript;
|
|
2141
|
+
const {
|
|
2142
|
+
DocumentFragment,
|
|
2143
|
+
HTMLTemplateElement,
|
|
2144
|
+
Node,
|
|
2145
|
+
Element,
|
|
2146
|
+
NodeFilter,
|
|
2147
|
+
NamedNodeMap = window2.NamedNodeMap || window2.MozNamedAttrMap,
|
|
2148
|
+
HTMLFormElement,
|
|
2149
|
+
DOMParser,
|
|
2150
|
+
trustedTypes
|
|
2151
|
+
} = window2;
|
|
2152
|
+
const ElementPrototype = Element.prototype;
|
|
2153
|
+
const cloneNode = lookupGetter(ElementPrototype, "cloneNode");
|
|
2154
|
+
const remove = lookupGetter(ElementPrototype, "remove");
|
|
2155
|
+
const getNextSibling = lookupGetter(ElementPrototype, "nextSibling");
|
|
2156
|
+
const getChildNodes = lookupGetter(ElementPrototype, "childNodes");
|
|
2157
|
+
const getParentNode = lookupGetter(ElementPrototype, "parentNode");
|
|
2158
|
+
if (typeof HTMLTemplateElement === "function") {
|
|
2159
|
+
const template = document.createElement("template");
|
|
2160
|
+
if (template.content && template.content.ownerDocument) {
|
|
2161
|
+
document = template.content.ownerDocument;
|
|
2162
|
+
}
|
|
2163
|
+
}
|
|
2164
|
+
let trustedTypesPolicy;
|
|
2165
|
+
let emptyHTML = "";
|
|
2166
|
+
const {
|
|
2167
|
+
implementation,
|
|
2168
|
+
createNodeIterator,
|
|
2169
|
+
createDocumentFragment,
|
|
2170
|
+
getElementsByTagName
|
|
2171
|
+
} = document;
|
|
2172
|
+
const {
|
|
2173
|
+
importNode
|
|
2174
|
+
} = originalDocument;
|
|
2175
|
+
let hooks = _createHooksMap();
|
|
2176
|
+
DOMPurify.isSupported = typeof entries === "function" && typeof getParentNode === "function" && implementation && implementation.createHTMLDocument !== void 0;
|
|
2177
|
+
const {
|
|
2178
|
+
MUSTACHE_EXPR: MUSTACHE_EXPR2,
|
|
2179
|
+
ERB_EXPR: ERB_EXPR2,
|
|
2180
|
+
TMPLIT_EXPR: TMPLIT_EXPR2,
|
|
2181
|
+
DATA_ATTR: DATA_ATTR2,
|
|
2182
|
+
ARIA_ATTR: ARIA_ATTR2,
|
|
2183
|
+
IS_SCRIPT_OR_DATA: IS_SCRIPT_OR_DATA2,
|
|
2184
|
+
ATTR_WHITESPACE: ATTR_WHITESPACE2,
|
|
2185
|
+
CUSTOM_ELEMENT: CUSTOM_ELEMENT2
|
|
2186
|
+
} = EXPRESSIONS;
|
|
2187
|
+
let {
|
|
2188
|
+
IS_ALLOWED_URI: IS_ALLOWED_URI$1
|
|
2189
|
+
} = EXPRESSIONS;
|
|
2190
|
+
let ALLOWED_TAGS = null;
|
|
2191
|
+
const DEFAULT_ALLOWED_TAGS = addToSet({}, [...html$1, ...svg$1, ...svgFilters, ...mathMl$1, ...text]);
|
|
2192
|
+
let ALLOWED_ATTR = null;
|
|
2193
|
+
const DEFAULT_ALLOWED_ATTR = addToSet({}, [...html, ...svg, ...mathMl, ...xml]);
|
|
2194
|
+
let CUSTOM_ELEMENT_HANDLING = Object.seal(create(null, {
|
|
2195
|
+
tagNameCheck: {
|
|
2196
|
+
writable: true,
|
|
2197
|
+
configurable: false,
|
|
2198
|
+
enumerable: true,
|
|
2199
|
+
value: null
|
|
2200
|
+
},
|
|
2201
|
+
attributeNameCheck: {
|
|
2202
|
+
writable: true,
|
|
2203
|
+
configurable: false,
|
|
2204
|
+
enumerable: true,
|
|
2205
|
+
value: null
|
|
2206
|
+
},
|
|
2207
|
+
allowCustomizedBuiltInElements: {
|
|
2208
|
+
writable: true,
|
|
2209
|
+
configurable: false,
|
|
2210
|
+
enumerable: true,
|
|
2211
|
+
value: false
|
|
2212
|
+
}
|
|
2213
|
+
}));
|
|
2214
|
+
let FORBID_TAGS = null;
|
|
2215
|
+
let FORBID_ATTR = null;
|
|
2216
|
+
const EXTRA_ELEMENT_HANDLING = Object.seal(create(null, {
|
|
2217
|
+
tagCheck: {
|
|
2218
|
+
writable: true,
|
|
2219
|
+
configurable: false,
|
|
2220
|
+
enumerable: true,
|
|
2221
|
+
value: null
|
|
2222
|
+
},
|
|
2223
|
+
attributeCheck: {
|
|
2224
|
+
writable: true,
|
|
2225
|
+
configurable: false,
|
|
2226
|
+
enumerable: true,
|
|
2227
|
+
value: null
|
|
2228
|
+
}
|
|
2229
|
+
}));
|
|
2230
|
+
let ALLOW_ARIA_ATTR = true;
|
|
2231
|
+
let ALLOW_DATA_ATTR = true;
|
|
2232
|
+
let ALLOW_UNKNOWN_PROTOCOLS = false;
|
|
2233
|
+
let ALLOW_SELF_CLOSE_IN_ATTR = true;
|
|
2234
|
+
let SAFE_FOR_TEMPLATES = false;
|
|
2235
|
+
let SAFE_FOR_XML = true;
|
|
2236
|
+
let WHOLE_DOCUMENT = false;
|
|
2237
|
+
let SET_CONFIG = false;
|
|
2238
|
+
let FORCE_BODY = false;
|
|
2239
|
+
let RETURN_DOM = false;
|
|
2240
|
+
let RETURN_DOM_FRAGMENT = false;
|
|
2241
|
+
let RETURN_TRUSTED_TYPE = false;
|
|
2242
|
+
let SANITIZE_DOM = true;
|
|
2243
|
+
let SANITIZE_NAMED_PROPS = false;
|
|
2244
|
+
const SANITIZE_NAMED_PROPS_PREFIX = "user-content-";
|
|
2245
|
+
let KEEP_CONTENT = true;
|
|
2246
|
+
let IN_PLACE = false;
|
|
2247
|
+
let USE_PROFILES = {};
|
|
2248
|
+
let FORBID_CONTENTS = null;
|
|
2249
|
+
const DEFAULT_FORBID_CONTENTS = addToSet({}, ["annotation-xml", "audio", "colgroup", "desc", "foreignobject", "head", "iframe", "math", "mi", "mn", "mo", "ms", "mtext", "noembed", "noframes", "noscript", "plaintext", "script", "style", "svg", "template", "thead", "title", "video", "xmp"]);
|
|
2250
|
+
let DATA_URI_TAGS = null;
|
|
2251
|
+
const DEFAULT_DATA_URI_TAGS = addToSet({}, ["audio", "video", "img", "source", "image", "track"]);
|
|
2252
|
+
let URI_SAFE_ATTRIBUTES = null;
|
|
2253
|
+
const DEFAULT_URI_SAFE_ATTRIBUTES = addToSet({}, ["alt", "class", "for", "id", "label", "name", "pattern", "placeholder", "role", "summary", "title", "value", "style", "xmlns"]);
|
|
2254
|
+
const MATHML_NAMESPACE = "http://www.w3.org/1998/Math/MathML";
|
|
2255
|
+
const SVG_NAMESPACE = "http://www.w3.org/2000/svg";
|
|
2256
|
+
const HTML_NAMESPACE = "http://www.w3.org/1999/xhtml";
|
|
2257
|
+
let NAMESPACE = HTML_NAMESPACE;
|
|
2258
|
+
let IS_EMPTY_INPUT = false;
|
|
2259
|
+
let ALLOWED_NAMESPACES = null;
|
|
2260
|
+
const DEFAULT_ALLOWED_NAMESPACES = addToSet({}, [MATHML_NAMESPACE, SVG_NAMESPACE, HTML_NAMESPACE], stringToString);
|
|
2261
|
+
let MATHML_TEXT_INTEGRATION_POINTS = addToSet({}, ["mi", "mo", "mn", "ms", "mtext"]);
|
|
2262
|
+
let HTML_INTEGRATION_POINTS = addToSet({}, ["annotation-xml"]);
|
|
2263
|
+
const COMMON_SVG_AND_HTML_ELEMENTS = addToSet({}, ["title", "style", "font", "a", "script"]);
|
|
2264
|
+
let PARSER_MEDIA_TYPE = null;
|
|
2265
|
+
const SUPPORTED_PARSER_MEDIA_TYPES = ["application/xhtml+xml", "text/html"];
|
|
2266
|
+
const DEFAULT_PARSER_MEDIA_TYPE = "text/html";
|
|
2267
|
+
let transformCaseFunc = null;
|
|
2268
|
+
let CONFIG = null;
|
|
2269
|
+
const formElement = document.createElement("form");
|
|
2270
|
+
const isRegexOrFunction = function isRegexOrFunction2(testValue) {
|
|
2271
|
+
return testValue instanceof RegExp || testValue instanceof Function;
|
|
2272
|
+
};
|
|
2273
|
+
const _parseConfig = function _parseConfig2() {
|
|
2274
|
+
let cfg = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {};
|
|
2275
|
+
if (CONFIG && CONFIG === cfg) {
|
|
2276
|
+
return;
|
|
2277
|
+
}
|
|
2278
|
+
if (!cfg || typeof cfg !== "object") {
|
|
2279
|
+
cfg = {};
|
|
2280
|
+
}
|
|
2281
|
+
cfg = clone(cfg);
|
|
2282
|
+
PARSER_MEDIA_TYPE = // eslint-disable-next-line unicorn/prefer-includes
|
|
2283
|
+
SUPPORTED_PARSER_MEDIA_TYPES.indexOf(cfg.PARSER_MEDIA_TYPE) === -1 ? DEFAULT_PARSER_MEDIA_TYPE : cfg.PARSER_MEDIA_TYPE;
|
|
2284
|
+
transformCaseFunc = PARSER_MEDIA_TYPE === "application/xhtml+xml" ? stringToString : stringToLowerCase;
|
|
2285
|
+
ALLOWED_TAGS = objectHasOwnProperty(cfg, "ALLOWED_TAGS") ? addToSet({}, cfg.ALLOWED_TAGS, transformCaseFunc) : DEFAULT_ALLOWED_TAGS;
|
|
2286
|
+
ALLOWED_ATTR = objectHasOwnProperty(cfg, "ALLOWED_ATTR") ? addToSet({}, cfg.ALLOWED_ATTR, transformCaseFunc) : DEFAULT_ALLOWED_ATTR;
|
|
2287
|
+
ALLOWED_NAMESPACES = objectHasOwnProperty(cfg, "ALLOWED_NAMESPACES") ? addToSet({}, cfg.ALLOWED_NAMESPACES, stringToString) : DEFAULT_ALLOWED_NAMESPACES;
|
|
2288
|
+
URI_SAFE_ATTRIBUTES = objectHasOwnProperty(cfg, "ADD_URI_SAFE_ATTR") ? addToSet(clone(DEFAULT_URI_SAFE_ATTRIBUTES), cfg.ADD_URI_SAFE_ATTR, transformCaseFunc) : DEFAULT_URI_SAFE_ATTRIBUTES;
|
|
2289
|
+
DATA_URI_TAGS = objectHasOwnProperty(cfg, "ADD_DATA_URI_TAGS") ? addToSet(clone(DEFAULT_DATA_URI_TAGS), cfg.ADD_DATA_URI_TAGS, transformCaseFunc) : DEFAULT_DATA_URI_TAGS;
|
|
2290
|
+
FORBID_CONTENTS = objectHasOwnProperty(cfg, "FORBID_CONTENTS") ? addToSet({}, cfg.FORBID_CONTENTS, transformCaseFunc) : DEFAULT_FORBID_CONTENTS;
|
|
2291
|
+
FORBID_TAGS = objectHasOwnProperty(cfg, "FORBID_TAGS") ? addToSet({}, cfg.FORBID_TAGS, transformCaseFunc) : clone({});
|
|
2292
|
+
FORBID_ATTR = objectHasOwnProperty(cfg, "FORBID_ATTR") ? addToSet({}, cfg.FORBID_ATTR, transformCaseFunc) : clone({});
|
|
2293
|
+
USE_PROFILES = objectHasOwnProperty(cfg, "USE_PROFILES") ? cfg.USE_PROFILES : false;
|
|
2294
|
+
ALLOW_ARIA_ATTR = cfg.ALLOW_ARIA_ATTR !== false;
|
|
2295
|
+
ALLOW_DATA_ATTR = cfg.ALLOW_DATA_ATTR !== false;
|
|
2296
|
+
ALLOW_UNKNOWN_PROTOCOLS = cfg.ALLOW_UNKNOWN_PROTOCOLS || false;
|
|
2297
|
+
ALLOW_SELF_CLOSE_IN_ATTR = cfg.ALLOW_SELF_CLOSE_IN_ATTR !== false;
|
|
2298
|
+
SAFE_FOR_TEMPLATES = cfg.SAFE_FOR_TEMPLATES || false;
|
|
2299
|
+
SAFE_FOR_XML = cfg.SAFE_FOR_XML !== false;
|
|
2300
|
+
WHOLE_DOCUMENT = cfg.WHOLE_DOCUMENT || false;
|
|
2301
|
+
RETURN_DOM = cfg.RETURN_DOM || false;
|
|
2302
|
+
RETURN_DOM_FRAGMENT = cfg.RETURN_DOM_FRAGMENT || false;
|
|
2303
|
+
RETURN_TRUSTED_TYPE = cfg.RETURN_TRUSTED_TYPE || false;
|
|
2304
|
+
FORCE_BODY = cfg.FORCE_BODY || false;
|
|
2305
|
+
SANITIZE_DOM = cfg.SANITIZE_DOM !== false;
|
|
2306
|
+
SANITIZE_NAMED_PROPS = cfg.SANITIZE_NAMED_PROPS || false;
|
|
2307
|
+
KEEP_CONTENT = cfg.KEEP_CONTENT !== false;
|
|
2308
|
+
IN_PLACE = cfg.IN_PLACE || false;
|
|
2309
|
+
IS_ALLOWED_URI$1 = cfg.ALLOWED_URI_REGEXP || IS_ALLOWED_URI;
|
|
2310
|
+
NAMESPACE = cfg.NAMESPACE || HTML_NAMESPACE;
|
|
2311
|
+
MATHML_TEXT_INTEGRATION_POINTS = cfg.MATHML_TEXT_INTEGRATION_POINTS || MATHML_TEXT_INTEGRATION_POINTS;
|
|
2312
|
+
HTML_INTEGRATION_POINTS = cfg.HTML_INTEGRATION_POINTS || HTML_INTEGRATION_POINTS;
|
|
2313
|
+
CUSTOM_ELEMENT_HANDLING = cfg.CUSTOM_ELEMENT_HANDLING || {};
|
|
2314
|
+
if (cfg.CUSTOM_ELEMENT_HANDLING && isRegexOrFunction(cfg.CUSTOM_ELEMENT_HANDLING.tagNameCheck)) {
|
|
2315
|
+
CUSTOM_ELEMENT_HANDLING.tagNameCheck = cfg.CUSTOM_ELEMENT_HANDLING.tagNameCheck;
|
|
2316
|
+
}
|
|
2317
|
+
if (cfg.CUSTOM_ELEMENT_HANDLING && isRegexOrFunction(cfg.CUSTOM_ELEMENT_HANDLING.attributeNameCheck)) {
|
|
2318
|
+
CUSTOM_ELEMENT_HANDLING.attributeNameCheck = cfg.CUSTOM_ELEMENT_HANDLING.attributeNameCheck;
|
|
2319
|
+
}
|
|
2320
|
+
if (cfg.CUSTOM_ELEMENT_HANDLING && typeof cfg.CUSTOM_ELEMENT_HANDLING.allowCustomizedBuiltInElements === "boolean") {
|
|
2321
|
+
CUSTOM_ELEMENT_HANDLING.allowCustomizedBuiltInElements = cfg.CUSTOM_ELEMENT_HANDLING.allowCustomizedBuiltInElements;
|
|
2322
|
+
}
|
|
2323
|
+
if (SAFE_FOR_TEMPLATES) {
|
|
2324
|
+
ALLOW_DATA_ATTR = false;
|
|
2325
|
+
}
|
|
2326
|
+
if (RETURN_DOM_FRAGMENT) {
|
|
2327
|
+
RETURN_DOM = true;
|
|
2328
|
+
}
|
|
2329
|
+
if (USE_PROFILES) {
|
|
2330
|
+
ALLOWED_TAGS = addToSet({}, text);
|
|
2331
|
+
ALLOWED_ATTR = [];
|
|
2332
|
+
if (USE_PROFILES.html === true) {
|
|
2333
|
+
addToSet(ALLOWED_TAGS, html$1);
|
|
2334
|
+
addToSet(ALLOWED_ATTR, html);
|
|
2335
|
+
}
|
|
2336
|
+
if (USE_PROFILES.svg === true) {
|
|
2337
|
+
addToSet(ALLOWED_TAGS, svg$1);
|
|
2338
|
+
addToSet(ALLOWED_ATTR, svg);
|
|
2339
|
+
addToSet(ALLOWED_ATTR, xml);
|
|
2340
|
+
}
|
|
2341
|
+
if (USE_PROFILES.svgFilters === true) {
|
|
2342
|
+
addToSet(ALLOWED_TAGS, svgFilters);
|
|
2343
|
+
addToSet(ALLOWED_ATTR, svg);
|
|
2344
|
+
addToSet(ALLOWED_ATTR, xml);
|
|
2345
|
+
}
|
|
2346
|
+
if (USE_PROFILES.mathMl === true) {
|
|
2347
|
+
addToSet(ALLOWED_TAGS, mathMl$1);
|
|
2348
|
+
addToSet(ALLOWED_ATTR, mathMl);
|
|
2349
|
+
addToSet(ALLOWED_ATTR, xml);
|
|
2350
|
+
}
|
|
2351
|
+
}
|
|
2352
|
+
if (cfg.ADD_TAGS) {
|
|
2353
|
+
if (typeof cfg.ADD_TAGS === "function") {
|
|
2354
|
+
EXTRA_ELEMENT_HANDLING.tagCheck = cfg.ADD_TAGS;
|
|
2355
|
+
} else {
|
|
2356
|
+
if (ALLOWED_TAGS === DEFAULT_ALLOWED_TAGS) {
|
|
2357
|
+
ALLOWED_TAGS = clone(ALLOWED_TAGS);
|
|
2358
|
+
}
|
|
2359
|
+
addToSet(ALLOWED_TAGS, cfg.ADD_TAGS, transformCaseFunc);
|
|
2360
|
+
}
|
|
2361
|
+
}
|
|
2362
|
+
if (cfg.ADD_ATTR) {
|
|
2363
|
+
if (typeof cfg.ADD_ATTR === "function") {
|
|
2364
|
+
EXTRA_ELEMENT_HANDLING.attributeCheck = cfg.ADD_ATTR;
|
|
2365
|
+
} else {
|
|
2366
|
+
if (ALLOWED_ATTR === DEFAULT_ALLOWED_ATTR) {
|
|
2367
|
+
ALLOWED_ATTR = clone(ALLOWED_ATTR);
|
|
2368
|
+
}
|
|
2369
|
+
addToSet(ALLOWED_ATTR, cfg.ADD_ATTR, transformCaseFunc);
|
|
2370
|
+
}
|
|
2371
|
+
}
|
|
2372
|
+
if (cfg.ADD_URI_SAFE_ATTR) {
|
|
2373
|
+
addToSet(URI_SAFE_ATTRIBUTES, cfg.ADD_URI_SAFE_ATTR, transformCaseFunc);
|
|
2374
|
+
}
|
|
2375
|
+
if (cfg.FORBID_CONTENTS) {
|
|
2376
|
+
if (FORBID_CONTENTS === DEFAULT_FORBID_CONTENTS) {
|
|
2377
|
+
FORBID_CONTENTS = clone(FORBID_CONTENTS);
|
|
2378
|
+
}
|
|
2379
|
+
addToSet(FORBID_CONTENTS, cfg.FORBID_CONTENTS, transformCaseFunc);
|
|
2380
|
+
}
|
|
2381
|
+
if (cfg.ADD_FORBID_CONTENTS) {
|
|
2382
|
+
if (FORBID_CONTENTS === DEFAULT_FORBID_CONTENTS) {
|
|
2383
|
+
FORBID_CONTENTS = clone(FORBID_CONTENTS);
|
|
2384
|
+
}
|
|
2385
|
+
addToSet(FORBID_CONTENTS, cfg.ADD_FORBID_CONTENTS, transformCaseFunc);
|
|
2386
|
+
}
|
|
2387
|
+
if (KEEP_CONTENT) {
|
|
2388
|
+
ALLOWED_TAGS["#text"] = true;
|
|
2389
|
+
}
|
|
2390
|
+
if (WHOLE_DOCUMENT) {
|
|
2391
|
+
addToSet(ALLOWED_TAGS, ["html", "head", "body"]);
|
|
2392
|
+
}
|
|
2393
|
+
if (ALLOWED_TAGS.table) {
|
|
2394
|
+
addToSet(ALLOWED_TAGS, ["tbody"]);
|
|
2395
|
+
delete FORBID_TAGS.tbody;
|
|
2396
|
+
}
|
|
2397
|
+
if (cfg.TRUSTED_TYPES_POLICY) {
|
|
2398
|
+
if (typeof cfg.TRUSTED_TYPES_POLICY.createHTML !== "function") {
|
|
2399
|
+
throw typeErrorCreate('TRUSTED_TYPES_POLICY configuration option must provide a "createHTML" hook.');
|
|
2400
|
+
}
|
|
2401
|
+
if (typeof cfg.TRUSTED_TYPES_POLICY.createScriptURL !== "function") {
|
|
2402
|
+
throw typeErrorCreate('TRUSTED_TYPES_POLICY configuration option must provide a "createScriptURL" hook.');
|
|
2403
|
+
}
|
|
2404
|
+
trustedTypesPolicy = cfg.TRUSTED_TYPES_POLICY;
|
|
2405
|
+
emptyHTML = trustedTypesPolicy.createHTML("");
|
|
2406
|
+
} else {
|
|
2407
|
+
if (trustedTypesPolicy === void 0) {
|
|
2408
|
+
trustedTypesPolicy = _createTrustedTypesPolicy(trustedTypes, currentScript);
|
|
2409
|
+
}
|
|
2410
|
+
if (trustedTypesPolicy !== null && typeof emptyHTML === "string") {
|
|
2411
|
+
emptyHTML = trustedTypesPolicy.createHTML("");
|
|
2412
|
+
}
|
|
2413
|
+
}
|
|
2414
|
+
if (freeze) {
|
|
2415
|
+
freeze(cfg);
|
|
2416
|
+
}
|
|
2417
|
+
CONFIG = cfg;
|
|
2418
|
+
};
|
|
2419
|
+
const ALL_SVG_TAGS = addToSet({}, [...svg$1, ...svgFilters, ...svgDisallowed]);
|
|
2420
|
+
const ALL_MATHML_TAGS = addToSet({}, [...mathMl$1, ...mathMlDisallowed]);
|
|
2421
|
+
const _checkValidNamespace = function _checkValidNamespace2(element) {
|
|
2422
|
+
let parent = getParentNode(element);
|
|
2423
|
+
if (!parent || !parent.tagName) {
|
|
2424
|
+
parent = {
|
|
2425
|
+
namespaceURI: NAMESPACE,
|
|
2426
|
+
tagName: "template"
|
|
2427
|
+
};
|
|
2428
|
+
}
|
|
2429
|
+
const tagName = stringToLowerCase(element.tagName);
|
|
2430
|
+
const parentTagName = stringToLowerCase(parent.tagName);
|
|
2431
|
+
if (!ALLOWED_NAMESPACES[element.namespaceURI]) {
|
|
2432
|
+
return false;
|
|
2433
|
+
}
|
|
2434
|
+
if (element.namespaceURI === SVG_NAMESPACE) {
|
|
2435
|
+
if (parent.namespaceURI === HTML_NAMESPACE) {
|
|
2436
|
+
return tagName === "svg";
|
|
2437
|
+
}
|
|
2438
|
+
if (parent.namespaceURI === MATHML_NAMESPACE) {
|
|
2439
|
+
return tagName === "svg" && (parentTagName === "annotation-xml" || MATHML_TEXT_INTEGRATION_POINTS[parentTagName]);
|
|
2440
|
+
}
|
|
2441
|
+
return Boolean(ALL_SVG_TAGS[tagName]);
|
|
2442
|
+
}
|
|
2443
|
+
if (element.namespaceURI === MATHML_NAMESPACE) {
|
|
2444
|
+
if (parent.namespaceURI === HTML_NAMESPACE) {
|
|
2445
|
+
return tagName === "math";
|
|
2446
|
+
}
|
|
2447
|
+
if (parent.namespaceURI === SVG_NAMESPACE) {
|
|
2448
|
+
return tagName === "math" && HTML_INTEGRATION_POINTS[parentTagName];
|
|
2449
|
+
}
|
|
2450
|
+
return Boolean(ALL_MATHML_TAGS[tagName]);
|
|
2451
|
+
}
|
|
2452
|
+
if (element.namespaceURI === HTML_NAMESPACE) {
|
|
2453
|
+
if (parent.namespaceURI === SVG_NAMESPACE && !HTML_INTEGRATION_POINTS[parentTagName]) {
|
|
2454
|
+
return false;
|
|
2455
|
+
}
|
|
2456
|
+
if (parent.namespaceURI === MATHML_NAMESPACE && !MATHML_TEXT_INTEGRATION_POINTS[parentTagName]) {
|
|
2457
|
+
return false;
|
|
2458
|
+
}
|
|
2459
|
+
return !ALL_MATHML_TAGS[tagName] && (COMMON_SVG_AND_HTML_ELEMENTS[tagName] || !ALL_SVG_TAGS[tagName]);
|
|
2460
|
+
}
|
|
2461
|
+
if (PARSER_MEDIA_TYPE === "application/xhtml+xml" && ALLOWED_NAMESPACES[element.namespaceURI]) {
|
|
2462
|
+
return true;
|
|
2463
|
+
}
|
|
2464
|
+
return false;
|
|
2465
|
+
};
|
|
2466
|
+
const _forceRemove = function _forceRemove2(node) {
|
|
2467
|
+
arrayPush(DOMPurify.removed, {
|
|
2468
|
+
element: node
|
|
2469
|
+
});
|
|
2470
|
+
try {
|
|
2471
|
+
getParentNode(node).removeChild(node);
|
|
2472
|
+
} catch (_) {
|
|
2473
|
+
remove(node);
|
|
2474
|
+
}
|
|
2475
|
+
};
|
|
2476
|
+
const _removeAttribute = function _removeAttribute2(name, element) {
|
|
2477
|
+
try {
|
|
2478
|
+
arrayPush(DOMPurify.removed, {
|
|
2479
|
+
attribute: element.getAttributeNode(name),
|
|
2480
|
+
from: element
|
|
2481
|
+
});
|
|
2482
|
+
} catch (_) {
|
|
2483
|
+
arrayPush(DOMPurify.removed, {
|
|
2484
|
+
attribute: null,
|
|
2485
|
+
from: element
|
|
2486
|
+
});
|
|
2487
|
+
}
|
|
2488
|
+
element.removeAttribute(name);
|
|
2489
|
+
if (name === "is") {
|
|
2490
|
+
if (RETURN_DOM || RETURN_DOM_FRAGMENT) {
|
|
2491
|
+
try {
|
|
2492
|
+
_forceRemove(element);
|
|
2493
|
+
} catch (_) {
|
|
2494
|
+
}
|
|
2495
|
+
} else {
|
|
2496
|
+
try {
|
|
2497
|
+
element.setAttribute(name, "");
|
|
2498
|
+
} catch (_) {
|
|
2499
|
+
}
|
|
2500
|
+
}
|
|
2501
|
+
}
|
|
2502
|
+
};
|
|
2503
|
+
const _initDocument = function _initDocument2(dirty) {
|
|
2504
|
+
let doc = null;
|
|
2505
|
+
let leadingWhitespace = null;
|
|
2506
|
+
if (FORCE_BODY) {
|
|
2507
|
+
dirty = "<remove></remove>" + dirty;
|
|
2508
|
+
} else {
|
|
2509
|
+
const matches = stringMatch(dirty, /^[\r\n\t ]+/);
|
|
2510
|
+
leadingWhitespace = matches && matches[0];
|
|
2511
|
+
}
|
|
2512
|
+
if (PARSER_MEDIA_TYPE === "application/xhtml+xml" && NAMESPACE === HTML_NAMESPACE) {
|
|
2513
|
+
dirty = '<html xmlns="http://www.w3.org/1999/xhtml"><head></head><body>' + dirty + "</body></html>";
|
|
2514
|
+
}
|
|
2515
|
+
const dirtyPayload = trustedTypesPolicy ? trustedTypesPolicy.createHTML(dirty) : dirty;
|
|
2516
|
+
if (NAMESPACE === HTML_NAMESPACE) {
|
|
2517
|
+
try {
|
|
2518
|
+
doc = new DOMParser().parseFromString(dirtyPayload, PARSER_MEDIA_TYPE);
|
|
2519
|
+
} catch (_) {
|
|
2520
|
+
}
|
|
2521
|
+
}
|
|
2522
|
+
if (!doc || !doc.documentElement) {
|
|
2523
|
+
doc = implementation.createDocument(NAMESPACE, "template", null);
|
|
2524
|
+
try {
|
|
2525
|
+
doc.documentElement.innerHTML = IS_EMPTY_INPUT ? emptyHTML : dirtyPayload;
|
|
2526
|
+
} catch (_) {
|
|
2527
|
+
}
|
|
2528
|
+
}
|
|
2529
|
+
const body = doc.body || doc.documentElement;
|
|
2530
|
+
if (dirty && leadingWhitespace) {
|
|
2531
|
+
body.insertBefore(document.createTextNode(leadingWhitespace), body.childNodes[0] || null);
|
|
2532
|
+
}
|
|
2533
|
+
if (NAMESPACE === HTML_NAMESPACE) {
|
|
2534
|
+
return getElementsByTagName.call(doc, WHOLE_DOCUMENT ? "html" : "body")[0];
|
|
2535
|
+
}
|
|
2536
|
+
return WHOLE_DOCUMENT ? doc.documentElement : body;
|
|
2537
|
+
};
|
|
2538
|
+
const _createNodeIterator = function _createNodeIterator2(root) {
|
|
2539
|
+
return createNodeIterator.call(
|
|
2540
|
+
root.ownerDocument || root,
|
|
2541
|
+
root,
|
|
2542
|
+
// eslint-disable-next-line no-bitwise
|
|
2543
|
+
NodeFilter.SHOW_ELEMENT | NodeFilter.SHOW_COMMENT | NodeFilter.SHOW_TEXT | NodeFilter.SHOW_PROCESSING_INSTRUCTION | NodeFilter.SHOW_CDATA_SECTION,
|
|
2544
|
+
null
|
|
2545
|
+
);
|
|
2546
|
+
};
|
|
2547
|
+
const _isClobbered = function _isClobbered2(element) {
|
|
2548
|
+
return element instanceof HTMLFormElement && (typeof element.nodeName !== "string" || typeof element.textContent !== "string" || typeof element.removeChild !== "function" || !(element.attributes instanceof NamedNodeMap) || typeof element.removeAttribute !== "function" || typeof element.setAttribute !== "function" || typeof element.namespaceURI !== "string" || typeof element.insertBefore !== "function" || typeof element.hasChildNodes !== "function");
|
|
2549
|
+
};
|
|
2550
|
+
const _isNode = function _isNode2(value) {
|
|
2551
|
+
return typeof Node === "function" && value instanceof Node;
|
|
2552
|
+
};
|
|
2553
|
+
function _executeHooks(hooks2, currentNode, data) {
|
|
2554
|
+
arrayForEach(hooks2, (hook) => {
|
|
2555
|
+
hook.call(DOMPurify, currentNode, data, CONFIG);
|
|
2556
|
+
});
|
|
2557
|
+
}
|
|
2558
|
+
const _sanitizeElements = function _sanitizeElements2(currentNode) {
|
|
2559
|
+
let content = null;
|
|
2560
|
+
_executeHooks(hooks.beforeSanitizeElements, currentNode, null);
|
|
2561
|
+
if (_isClobbered(currentNode)) {
|
|
2562
|
+
_forceRemove(currentNode);
|
|
2563
|
+
return true;
|
|
2564
|
+
}
|
|
2565
|
+
const tagName = transformCaseFunc(currentNode.nodeName);
|
|
2566
|
+
_executeHooks(hooks.uponSanitizeElement, currentNode, {
|
|
2567
|
+
tagName,
|
|
2568
|
+
allowedTags: ALLOWED_TAGS
|
|
2569
|
+
});
|
|
2570
|
+
if (SAFE_FOR_XML && currentNode.hasChildNodes() && !_isNode(currentNode.firstElementChild) && regExpTest(/<[/\w!]/g, currentNode.innerHTML) && regExpTest(/<[/\w!]/g, currentNode.textContent)) {
|
|
2571
|
+
_forceRemove(currentNode);
|
|
2572
|
+
return true;
|
|
2573
|
+
}
|
|
2574
|
+
if (currentNode.nodeType === NODE_TYPE.progressingInstruction) {
|
|
2575
|
+
_forceRemove(currentNode);
|
|
2576
|
+
return true;
|
|
2577
|
+
}
|
|
2578
|
+
if (SAFE_FOR_XML && currentNode.nodeType === NODE_TYPE.comment && regExpTest(/<[/\w]/g, currentNode.data)) {
|
|
2579
|
+
_forceRemove(currentNode);
|
|
2580
|
+
return true;
|
|
2581
|
+
}
|
|
2582
|
+
if (!(EXTRA_ELEMENT_HANDLING.tagCheck instanceof Function && EXTRA_ELEMENT_HANDLING.tagCheck(tagName)) && (!ALLOWED_TAGS[tagName] || FORBID_TAGS[tagName])) {
|
|
2583
|
+
if (!FORBID_TAGS[tagName] && _isBasicCustomElement(tagName)) {
|
|
2584
|
+
if (CUSTOM_ELEMENT_HANDLING.tagNameCheck instanceof RegExp && regExpTest(CUSTOM_ELEMENT_HANDLING.tagNameCheck, tagName)) {
|
|
2585
|
+
return false;
|
|
2586
|
+
}
|
|
2587
|
+
if (CUSTOM_ELEMENT_HANDLING.tagNameCheck instanceof Function && CUSTOM_ELEMENT_HANDLING.tagNameCheck(tagName)) {
|
|
2588
|
+
return false;
|
|
2589
|
+
}
|
|
2590
|
+
}
|
|
2591
|
+
if (KEEP_CONTENT && !FORBID_CONTENTS[tagName]) {
|
|
2592
|
+
const parentNode = getParentNode(currentNode) || currentNode.parentNode;
|
|
2593
|
+
const childNodes = getChildNodes(currentNode) || currentNode.childNodes;
|
|
2594
|
+
if (childNodes && parentNode) {
|
|
2595
|
+
const childCount = childNodes.length;
|
|
2596
|
+
for (let i = childCount - 1; i >= 0; --i) {
|
|
2597
|
+
const childClone = cloneNode(childNodes[i], true);
|
|
2598
|
+
childClone.__removalCount = (currentNode.__removalCount || 0) + 1;
|
|
2599
|
+
parentNode.insertBefore(childClone, getNextSibling(currentNode));
|
|
2600
|
+
}
|
|
2601
|
+
}
|
|
2602
|
+
}
|
|
2603
|
+
_forceRemove(currentNode);
|
|
2604
|
+
return true;
|
|
2605
|
+
}
|
|
2606
|
+
if (currentNode instanceof Element && !_checkValidNamespace(currentNode)) {
|
|
2607
|
+
_forceRemove(currentNode);
|
|
2608
|
+
return true;
|
|
2609
|
+
}
|
|
2610
|
+
if ((tagName === "noscript" || tagName === "noembed" || tagName === "noframes") && regExpTest(/<\/no(script|embed|frames)/i, currentNode.innerHTML)) {
|
|
2611
|
+
_forceRemove(currentNode);
|
|
2612
|
+
return true;
|
|
2613
|
+
}
|
|
2614
|
+
if (SAFE_FOR_TEMPLATES && currentNode.nodeType === NODE_TYPE.text) {
|
|
2615
|
+
content = currentNode.textContent;
|
|
2616
|
+
arrayForEach([MUSTACHE_EXPR2, ERB_EXPR2, TMPLIT_EXPR2], (expr) => {
|
|
2617
|
+
content = stringReplace(content, expr, " ");
|
|
2618
|
+
});
|
|
2619
|
+
if (currentNode.textContent !== content) {
|
|
2620
|
+
arrayPush(DOMPurify.removed, {
|
|
2621
|
+
element: currentNode.cloneNode()
|
|
2622
|
+
});
|
|
2623
|
+
currentNode.textContent = content;
|
|
2624
|
+
}
|
|
2625
|
+
}
|
|
2626
|
+
_executeHooks(hooks.afterSanitizeElements, currentNode, null);
|
|
2627
|
+
return false;
|
|
2628
|
+
};
|
|
2629
|
+
const _isValidAttribute = function _isValidAttribute2(lcTag, lcName, value) {
|
|
2630
|
+
if (SANITIZE_DOM && (lcName === "id" || lcName === "name") && (value in document || value in formElement)) {
|
|
2631
|
+
return false;
|
|
2632
|
+
}
|
|
2633
|
+
if (ALLOW_DATA_ATTR && !FORBID_ATTR[lcName] && regExpTest(DATA_ATTR2, lcName)) ;
|
|
2634
|
+
else if (ALLOW_ARIA_ATTR && regExpTest(ARIA_ATTR2, lcName)) ;
|
|
2635
|
+
else if (EXTRA_ELEMENT_HANDLING.attributeCheck instanceof Function && EXTRA_ELEMENT_HANDLING.attributeCheck(lcName, lcTag)) ;
|
|
2636
|
+
else if (!ALLOWED_ATTR[lcName] || FORBID_ATTR[lcName]) {
|
|
2637
|
+
if (
|
|
2638
|
+
// First condition does a very basic check if a) it's basically a valid custom element tagname AND
|
|
2639
|
+
// b) if the tagName passes whatever the user has configured for CUSTOM_ELEMENT_HANDLING.tagNameCheck
|
|
2640
|
+
// and c) if the attribute name passes whatever the user has configured for CUSTOM_ELEMENT_HANDLING.attributeNameCheck
|
|
2641
|
+
_isBasicCustomElement(lcTag) && (CUSTOM_ELEMENT_HANDLING.tagNameCheck instanceof RegExp && regExpTest(CUSTOM_ELEMENT_HANDLING.tagNameCheck, lcTag) || CUSTOM_ELEMENT_HANDLING.tagNameCheck instanceof Function && CUSTOM_ELEMENT_HANDLING.tagNameCheck(lcTag)) && (CUSTOM_ELEMENT_HANDLING.attributeNameCheck instanceof RegExp && regExpTest(CUSTOM_ELEMENT_HANDLING.attributeNameCheck, lcName) || CUSTOM_ELEMENT_HANDLING.attributeNameCheck instanceof Function && CUSTOM_ELEMENT_HANDLING.attributeNameCheck(lcName, lcTag)) || // Alternative, second condition checks if it's an `is`-attribute, AND
|
|
2642
|
+
// the value passes whatever the user has configured for CUSTOM_ELEMENT_HANDLING.tagNameCheck
|
|
2643
|
+
lcName === "is" && CUSTOM_ELEMENT_HANDLING.allowCustomizedBuiltInElements && (CUSTOM_ELEMENT_HANDLING.tagNameCheck instanceof RegExp && regExpTest(CUSTOM_ELEMENT_HANDLING.tagNameCheck, value) || CUSTOM_ELEMENT_HANDLING.tagNameCheck instanceof Function && CUSTOM_ELEMENT_HANDLING.tagNameCheck(value))
|
|
2644
|
+
) ;
|
|
2645
|
+
else {
|
|
2646
|
+
return false;
|
|
2647
|
+
}
|
|
2648
|
+
} else if (URI_SAFE_ATTRIBUTES[lcName]) ;
|
|
2649
|
+
else if (regExpTest(IS_ALLOWED_URI$1, stringReplace(value, ATTR_WHITESPACE2, ""))) ;
|
|
2650
|
+
else if ((lcName === "src" || lcName === "xlink:href" || lcName === "href") && lcTag !== "script" && stringIndexOf(value, "data:") === 0 && DATA_URI_TAGS[lcTag]) ;
|
|
2651
|
+
else if (ALLOW_UNKNOWN_PROTOCOLS && !regExpTest(IS_SCRIPT_OR_DATA2, stringReplace(value, ATTR_WHITESPACE2, ""))) ;
|
|
2652
|
+
else if (value) {
|
|
2653
|
+
return false;
|
|
2654
|
+
} else ;
|
|
2655
|
+
return true;
|
|
2656
|
+
};
|
|
2657
|
+
const _isBasicCustomElement = function _isBasicCustomElement2(tagName) {
|
|
2658
|
+
return tagName !== "annotation-xml" && stringMatch(tagName, CUSTOM_ELEMENT2);
|
|
2659
|
+
};
|
|
2660
|
+
const _sanitizeAttributes = function _sanitizeAttributes2(currentNode) {
|
|
2661
|
+
_executeHooks(hooks.beforeSanitizeAttributes, currentNode, null);
|
|
2662
|
+
const {
|
|
2663
|
+
attributes
|
|
2664
|
+
} = currentNode;
|
|
2665
|
+
if (!attributes || _isClobbered(currentNode)) {
|
|
2666
|
+
return;
|
|
2667
|
+
}
|
|
2668
|
+
const hookEvent = {
|
|
2669
|
+
attrName: "",
|
|
2670
|
+
attrValue: "",
|
|
2671
|
+
keepAttr: true,
|
|
2672
|
+
allowedAttributes: ALLOWED_ATTR,
|
|
2673
|
+
forceKeepAttr: void 0
|
|
2674
|
+
};
|
|
2675
|
+
let l = attributes.length;
|
|
2676
|
+
while (l--) {
|
|
2677
|
+
const attr = attributes[l];
|
|
2678
|
+
const {
|
|
2679
|
+
name,
|
|
2680
|
+
namespaceURI,
|
|
2681
|
+
value: attrValue
|
|
2682
|
+
} = attr;
|
|
2683
|
+
const lcName = transformCaseFunc(name);
|
|
2684
|
+
const initValue = attrValue;
|
|
2685
|
+
let value = name === "value" ? initValue : stringTrim(initValue);
|
|
2686
|
+
hookEvent.attrName = lcName;
|
|
2687
|
+
hookEvent.attrValue = value;
|
|
2688
|
+
hookEvent.keepAttr = true;
|
|
2689
|
+
hookEvent.forceKeepAttr = void 0;
|
|
2690
|
+
_executeHooks(hooks.uponSanitizeAttribute, currentNode, hookEvent);
|
|
2691
|
+
value = hookEvent.attrValue;
|
|
2692
|
+
if (SANITIZE_NAMED_PROPS && (lcName === "id" || lcName === "name")) {
|
|
2693
|
+
_removeAttribute(name, currentNode);
|
|
2694
|
+
value = SANITIZE_NAMED_PROPS_PREFIX + value;
|
|
2695
|
+
}
|
|
2696
|
+
if (SAFE_FOR_XML && regExpTest(/((--!?|])>)|<\/(style|title|textarea)/i, value)) {
|
|
2697
|
+
_removeAttribute(name, currentNode);
|
|
2698
|
+
continue;
|
|
2699
|
+
}
|
|
2700
|
+
if (lcName === "attributename" && stringMatch(value, "href")) {
|
|
2701
|
+
_removeAttribute(name, currentNode);
|
|
2702
|
+
continue;
|
|
2703
|
+
}
|
|
2704
|
+
if (hookEvent.forceKeepAttr) {
|
|
2705
|
+
continue;
|
|
2706
|
+
}
|
|
2707
|
+
if (!hookEvent.keepAttr) {
|
|
2708
|
+
_removeAttribute(name, currentNode);
|
|
2709
|
+
continue;
|
|
2710
|
+
}
|
|
2711
|
+
if (!ALLOW_SELF_CLOSE_IN_ATTR && regExpTest(/\/>/i, value)) {
|
|
2712
|
+
_removeAttribute(name, currentNode);
|
|
2713
|
+
continue;
|
|
2714
|
+
}
|
|
2715
|
+
if (SAFE_FOR_TEMPLATES) {
|
|
2716
|
+
arrayForEach([MUSTACHE_EXPR2, ERB_EXPR2, TMPLIT_EXPR2], (expr) => {
|
|
2717
|
+
value = stringReplace(value, expr, " ");
|
|
2718
|
+
});
|
|
2719
|
+
}
|
|
2720
|
+
const lcTag = transformCaseFunc(currentNode.nodeName);
|
|
2721
|
+
if (!_isValidAttribute(lcTag, lcName, value)) {
|
|
2722
|
+
_removeAttribute(name, currentNode);
|
|
2723
|
+
continue;
|
|
2724
|
+
}
|
|
2725
|
+
if (trustedTypesPolicy && typeof trustedTypes === "object" && typeof trustedTypes.getAttributeType === "function") {
|
|
2726
|
+
if (namespaceURI) ;
|
|
2727
|
+
else {
|
|
2728
|
+
switch (trustedTypes.getAttributeType(lcTag, lcName)) {
|
|
2729
|
+
case "TrustedHTML": {
|
|
2730
|
+
value = trustedTypesPolicy.createHTML(value);
|
|
2731
|
+
break;
|
|
2732
|
+
}
|
|
2733
|
+
case "TrustedScriptURL": {
|
|
2734
|
+
value = trustedTypesPolicy.createScriptURL(value);
|
|
2735
|
+
break;
|
|
2736
|
+
}
|
|
2737
|
+
}
|
|
2738
|
+
}
|
|
2739
|
+
}
|
|
2740
|
+
if (value !== initValue) {
|
|
2741
|
+
try {
|
|
2742
|
+
if (namespaceURI) {
|
|
2743
|
+
currentNode.setAttributeNS(namespaceURI, name, value);
|
|
2744
|
+
} else {
|
|
2745
|
+
currentNode.setAttribute(name, value);
|
|
2746
|
+
}
|
|
2747
|
+
if (_isClobbered(currentNode)) {
|
|
2748
|
+
_forceRemove(currentNode);
|
|
2749
|
+
} else {
|
|
2750
|
+
arrayPop(DOMPurify.removed);
|
|
2751
|
+
}
|
|
2752
|
+
} catch (_) {
|
|
2753
|
+
_removeAttribute(name, currentNode);
|
|
2754
|
+
}
|
|
2755
|
+
}
|
|
2756
|
+
}
|
|
2757
|
+
_executeHooks(hooks.afterSanitizeAttributes, currentNode, null);
|
|
2758
|
+
};
|
|
2759
|
+
const _sanitizeShadowDOM = function _sanitizeShadowDOM2(fragment) {
|
|
2760
|
+
let shadowNode = null;
|
|
2761
|
+
const shadowIterator = _createNodeIterator(fragment);
|
|
2762
|
+
_executeHooks(hooks.beforeSanitizeShadowDOM, fragment, null);
|
|
2763
|
+
while (shadowNode = shadowIterator.nextNode()) {
|
|
2764
|
+
_executeHooks(hooks.uponSanitizeShadowNode, shadowNode, null);
|
|
2765
|
+
_sanitizeElements(shadowNode);
|
|
2766
|
+
_sanitizeAttributes(shadowNode);
|
|
2767
|
+
if (shadowNode.content instanceof DocumentFragment) {
|
|
2768
|
+
_sanitizeShadowDOM2(shadowNode.content);
|
|
2769
|
+
}
|
|
2770
|
+
}
|
|
2771
|
+
_executeHooks(hooks.afterSanitizeShadowDOM, fragment, null);
|
|
2772
|
+
};
|
|
2773
|
+
DOMPurify.sanitize = function(dirty) {
|
|
2774
|
+
let cfg = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
|
|
2775
|
+
let body = null;
|
|
2776
|
+
let importedNode = null;
|
|
2777
|
+
let currentNode = null;
|
|
2778
|
+
let returnNode = null;
|
|
2779
|
+
IS_EMPTY_INPUT = !dirty;
|
|
2780
|
+
if (IS_EMPTY_INPUT) {
|
|
2781
|
+
dirty = "<!-->";
|
|
2782
|
+
}
|
|
2783
|
+
if (typeof dirty !== "string" && !_isNode(dirty)) {
|
|
2784
|
+
if (typeof dirty.toString === "function") {
|
|
2785
|
+
dirty = dirty.toString();
|
|
2786
|
+
if (typeof dirty !== "string") {
|
|
2787
|
+
throw typeErrorCreate("dirty is not a string, aborting");
|
|
2788
|
+
}
|
|
2789
|
+
} else {
|
|
2790
|
+
throw typeErrorCreate("toString is not a function");
|
|
2791
|
+
}
|
|
2792
|
+
}
|
|
2793
|
+
if (!DOMPurify.isSupported) {
|
|
2794
|
+
return dirty;
|
|
2795
|
+
}
|
|
2796
|
+
if (!SET_CONFIG) {
|
|
2797
|
+
_parseConfig(cfg);
|
|
2798
|
+
}
|
|
2799
|
+
DOMPurify.removed = [];
|
|
2800
|
+
if (typeof dirty === "string") {
|
|
2801
|
+
IN_PLACE = false;
|
|
2802
|
+
}
|
|
2803
|
+
if (IN_PLACE) {
|
|
2804
|
+
if (dirty.nodeName) {
|
|
2805
|
+
const tagName = transformCaseFunc(dirty.nodeName);
|
|
2806
|
+
if (!ALLOWED_TAGS[tagName] || FORBID_TAGS[tagName]) {
|
|
2807
|
+
throw typeErrorCreate("root node is forbidden and cannot be sanitized in-place");
|
|
2808
|
+
}
|
|
2809
|
+
}
|
|
2810
|
+
} else if (dirty instanceof Node) {
|
|
2811
|
+
body = _initDocument("<!---->");
|
|
2812
|
+
importedNode = body.ownerDocument.importNode(dirty, true);
|
|
2813
|
+
if (importedNode.nodeType === NODE_TYPE.element && importedNode.nodeName === "BODY") {
|
|
2814
|
+
body = importedNode;
|
|
2815
|
+
} else if (importedNode.nodeName === "HTML") {
|
|
2816
|
+
body = importedNode;
|
|
2817
|
+
} else {
|
|
2818
|
+
body.appendChild(importedNode);
|
|
2819
|
+
}
|
|
2820
|
+
} else {
|
|
2821
|
+
if (!RETURN_DOM && !SAFE_FOR_TEMPLATES && !WHOLE_DOCUMENT && // eslint-disable-next-line unicorn/prefer-includes
|
|
2822
|
+
dirty.indexOf("<") === -1) {
|
|
2823
|
+
return trustedTypesPolicy && RETURN_TRUSTED_TYPE ? trustedTypesPolicy.createHTML(dirty) : dirty;
|
|
2824
|
+
}
|
|
2825
|
+
body = _initDocument(dirty);
|
|
2826
|
+
if (!body) {
|
|
2827
|
+
return RETURN_DOM ? null : RETURN_TRUSTED_TYPE ? emptyHTML : "";
|
|
2828
|
+
}
|
|
2829
|
+
}
|
|
2830
|
+
if (body && FORCE_BODY) {
|
|
2831
|
+
_forceRemove(body.firstChild);
|
|
2832
|
+
}
|
|
2833
|
+
const nodeIterator = _createNodeIterator(IN_PLACE ? dirty : body);
|
|
2834
|
+
while (currentNode = nodeIterator.nextNode()) {
|
|
2835
|
+
_sanitizeElements(currentNode);
|
|
2836
|
+
_sanitizeAttributes(currentNode);
|
|
2837
|
+
if (currentNode.content instanceof DocumentFragment) {
|
|
2838
|
+
_sanitizeShadowDOM(currentNode.content);
|
|
2839
|
+
}
|
|
2840
|
+
}
|
|
2841
|
+
if (IN_PLACE) {
|
|
2842
|
+
return dirty;
|
|
2843
|
+
}
|
|
2844
|
+
if (RETURN_DOM) {
|
|
2845
|
+
if (RETURN_DOM_FRAGMENT) {
|
|
2846
|
+
returnNode = createDocumentFragment.call(body.ownerDocument);
|
|
2847
|
+
while (body.firstChild) {
|
|
2848
|
+
returnNode.appendChild(body.firstChild);
|
|
2849
|
+
}
|
|
2850
|
+
} else {
|
|
2851
|
+
returnNode = body;
|
|
2852
|
+
}
|
|
2853
|
+
if (ALLOWED_ATTR.shadowroot || ALLOWED_ATTR.shadowrootmode) {
|
|
2854
|
+
returnNode = importNode.call(originalDocument, returnNode, true);
|
|
2855
|
+
}
|
|
2856
|
+
return returnNode;
|
|
2857
|
+
}
|
|
2858
|
+
let serializedHTML = WHOLE_DOCUMENT ? body.outerHTML : body.innerHTML;
|
|
2859
|
+
if (WHOLE_DOCUMENT && ALLOWED_TAGS["!doctype"] && body.ownerDocument && body.ownerDocument.doctype && body.ownerDocument.doctype.name && regExpTest(DOCTYPE_NAME, body.ownerDocument.doctype.name)) {
|
|
2860
|
+
serializedHTML = "<!DOCTYPE " + body.ownerDocument.doctype.name + ">\n" + serializedHTML;
|
|
2861
|
+
}
|
|
2862
|
+
if (SAFE_FOR_TEMPLATES) {
|
|
2863
|
+
arrayForEach([MUSTACHE_EXPR2, ERB_EXPR2, TMPLIT_EXPR2], (expr) => {
|
|
2864
|
+
serializedHTML = stringReplace(serializedHTML, expr, " ");
|
|
2865
|
+
});
|
|
2866
|
+
}
|
|
2867
|
+
return trustedTypesPolicy && RETURN_TRUSTED_TYPE ? trustedTypesPolicy.createHTML(serializedHTML) : serializedHTML;
|
|
2868
|
+
};
|
|
2869
|
+
DOMPurify.setConfig = function() {
|
|
2870
|
+
let cfg = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {};
|
|
2871
|
+
_parseConfig(cfg);
|
|
2872
|
+
SET_CONFIG = true;
|
|
2873
|
+
};
|
|
2874
|
+
DOMPurify.clearConfig = function() {
|
|
2875
|
+
CONFIG = null;
|
|
2876
|
+
SET_CONFIG = false;
|
|
2877
|
+
};
|
|
2878
|
+
DOMPurify.isValidAttribute = function(tag2, attr, value) {
|
|
2879
|
+
if (!CONFIG) {
|
|
2880
|
+
_parseConfig({});
|
|
2881
|
+
}
|
|
2882
|
+
const lcTag = transformCaseFunc(tag2);
|
|
2883
|
+
const lcName = transformCaseFunc(attr);
|
|
2884
|
+
return _isValidAttribute(lcTag, lcName, value);
|
|
2885
|
+
};
|
|
2886
|
+
DOMPurify.addHook = function(entryPoint, hookFunction) {
|
|
2887
|
+
if (typeof hookFunction !== "function") {
|
|
2888
|
+
return;
|
|
2889
|
+
}
|
|
2890
|
+
arrayPush(hooks[entryPoint], hookFunction);
|
|
2891
|
+
};
|
|
2892
|
+
DOMPurify.removeHook = function(entryPoint, hookFunction) {
|
|
2893
|
+
if (hookFunction !== void 0) {
|
|
2894
|
+
const index = arrayLastIndexOf(hooks[entryPoint], hookFunction);
|
|
2895
|
+
return index === -1 ? void 0 : arraySplice(hooks[entryPoint], index, 1)[0];
|
|
2896
|
+
}
|
|
2897
|
+
return arrayPop(hooks[entryPoint]);
|
|
2898
|
+
};
|
|
2899
|
+
DOMPurify.removeHooks = function(entryPoint) {
|
|
2900
|
+
hooks[entryPoint] = [];
|
|
2901
|
+
};
|
|
2902
|
+
DOMPurify.removeAllHooks = function() {
|
|
2903
|
+
hooks = _createHooksMap();
|
|
2904
|
+
};
|
|
2905
|
+
return DOMPurify;
|
|
2906
|
+
}
|
|
2907
|
+
var purify = createDOMPurify();
|
|
2908
|
+
function sanitizeHtml(html2, options) {
|
|
2909
|
+
const defaultOptions = {
|
|
2910
|
+
ALLOWED_TAGS: ["a", "b", "strong", "i", "em", "u", "br", "p", "span"],
|
|
2911
|
+
ALLOWED_ATTR: ["href", "target", "rel"],
|
|
2912
|
+
ALLOW_DATA_ATTR: false
|
|
2913
|
+
};
|
|
2914
|
+
const config = {
|
|
2915
|
+
...defaultOptions,
|
|
2916
|
+
...options?.allowedTags && { ALLOWED_TAGS: options.allowedTags },
|
|
2917
|
+
...options?.allowedAttributes && {
|
|
2918
|
+
ALLOWED_ATTR: Object.keys(options.allowedAttributes).flatMap(
|
|
2919
|
+
(key) => options.allowedAttributes[key]
|
|
2920
|
+
)
|
|
2921
|
+
}
|
|
2922
|
+
};
|
|
2923
|
+
const result = purify.sanitize(html2, config);
|
|
2924
|
+
return typeof result === "string" ? result : String(result);
|
|
2925
|
+
}
|
|
2926
|
+
function linkifyText(text2) {
|
|
2927
|
+
const urlRegex = /(https?:\/\/[^\s]+)/g;
|
|
2928
|
+
const linked = text2.replace(
|
|
2929
|
+
urlRegex,
|
|
2930
|
+
'<a href="$1" target="_blank" rel="noopener noreferrer">$1</a>'
|
|
2931
|
+
);
|
|
2932
|
+
return sanitizeHtml(linked, {
|
|
2933
|
+
allowedTags: ["a"],
|
|
2934
|
+
allowedAttributes: { a: ["href", "target", "rel"] }
|
|
2935
|
+
});
|
|
2936
|
+
}
|
|
2937
|
+
function safeLinkifyText(text2) {
|
|
2938
|
+
return linkifyText(text2);
|
|
2939
|
+
}
|
|
2940
|
+
export {
|
|
2941
|
+
ChatResolvedError as C,
|
|
2942
|
+
createDialogflowSession as a,
|
|
2943
|
+
safeLinkifyText as b,
|
|
2944
|
+
createChatService as c,
|
|
2945
|
+
linkifyText as l,
|
|
2946
|
+
sendDialogflowMessage as s
|
|
2947
|
+
};
|
|
2948
|
+
//# sourceMappingURL=sanitize-O18C3eqP.js.map
|