@sentry/core
Version:
Base implementation for all Sentry JavaScript SDKs
27 lines (26 loc) • 1.2 kB
JavaScript
// src/build-time-plugins/stringifyForCodeInjection.ts
const UNSAFE_CHARS = /[<>\u2028\u2029]/g;
const ESCAPED_CHARS = {
"<": "\\u003c",
">": "\\u003e",
"\u2028": "\\u2028",
"\u2029": "\\u2029"
};
/**
* A drop-in replacement for `JSON.stringify`, for values that go into generated JavaScript code.
*
* `JSON.stringify` output is a valid JavaScript expression, but it does not escape `<` and `>`, so
* a value with `<\/script>` or `<!--` breaks out of a `<script>` element when the code is inlined in
* HTML. It also does not escape U+2028 and U+2029, which end a line in JavaScript before ES2019.
* These characters only occur inside JSON strings, where their `\u` escapes have the same value.
*
* Use this every time `JSON.stringify` output is put into code, for example in a bundler plugin.
*
* @internal Only meant for Sentry-internal SDK usage.
* @hidden
*/
function stringifyForCodeInjection(value, replacer, space) {
return (typeof replacer === "function" ? JSON.stringify(value, replacer, space) : JSON.stringify(value, replacer, space))?.replace(UNSAFE_CHARS, (char) => ESCAPED_CHARS[char]);
}
export { stringifyForCodeInjection };
//# sourceMappingURL=stringifyForCodeInjection.js.map