/** * The global the HTML shell hands to the client for answers the server already * has when it serves the document. Merged rather than replaced, so several * injections compose in any order. */ const BOOTSTRAP_GLOBAL = 'window.__LIBRECHAT_CONFIG__'; export interface BootstrapInjection { /** Attribute marking this injection's script, so serving one shell through * two injectors — or the same one twice — does not stack it. */ sentinel: string; values: Record; } /** * Writes server-known values into the shell's `` (or the top of `` * in a document without one), ahead of the app's own scripts so the first * render reads them. The caller stamps the CSP nonce afterwards. */ export const injectBootstrapConfig = ( html: string, { sentinel, values }: BootstrapInjection, ): string => { if (html.includes(sentinel)) { return html; } /** A `<` in the payload could close the inline script; it never has to be raw. */ const payload = JSON.stringify(values).replace(/${BOOTSTRAP_GLOBAL}=Object.assign({},${BOOTSTRAP_GLOBAL},${payload});`; /* Replacements are functions: a `$&`, `$1` or `$'` inside a value is data, and a replacement string would have `String.replace` expand it instead. */ if (html.includes('')) { return html.replace('', () => `${script}`); } return html.replace( /]*)>/i, (_match, attributes: string) => `${script}`, ); };