#!/usr/bin/env node // Copies the chat renderer's third-party assets into app/renderer/vendor with a provenance record. // The packages are installed OUTSIDE this repository so they never become runtime dependencies: // npm install --prefix katex@0.16.47 mermaid@11.17.2 emojibase-data@18.0.0 \ // @codemirror/state@6.7.6 @codemirror/view@6.43.15 @codemirror/commands@6.11.1 // npm install --prefix --save-dev esbuild@0.28.2 // node scripts/vendor-chat-libs.mjs /node_modules // Versions match the installed official TeamSpeak 6 client where it bundles the same library // (KaTeX 0.16.47 and Mermaid 11.17.2 in html/client_ui/main.js). Emoji shortcodes come from the // emojibase preset, which covers every shortcode the official emoji table uses. The official composer // is a CodeMirror 6 view (main.js @3520977); the chat editor uses the CodeMirror core packages only, // bundled by esbuild (a build tool, not redistributed) into one offline ES module. import fs from 'node:fs'; import path from 'node:path'; import crypto from 'node:crypto'; import { createRequire } from 'node:module'; import { fileURLToPath } from 'node:url'; const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..'); const OUT = path.join(ROOT, 'app', 'renderer', 'vendor'); const PINNED = { katex: '0.16.47', mermaid: '11.17.2', 'emojibase-data': '18.0.0', '@codemirror/state': '6.7.6', '@codemirror/view': '6.43.15', '@codemirror/commands': '6.11.1', esbuild: '0.28.2', }; const nodeModules = process.argv[2]; if (!nodeModules || !fs.existsSync(nodeModules)) { console.error('usage: node scripts/vendor-chat-libs.mjs '); process.exit(2); } function pkg(name) { const dir = path.join(nodeModules, name); const meta = JSON.parse(fs.readFileSync(path.join(dir, 'package.json'), 'utf8')); if (meta.version !== PINNED[name]) throw new Error(`${name} ${meta.version} found, ${PINNED[name]} required`); return { dir, meta }; } const sha256 = (buf) => crypto.createHash('sha256').update(buf).digest('hex'); const provenance = { generatedBy: 'scripts/vendor-chat-libs.mjs', packages: [] }; function copy(name, files, transform = null) { const { dir, meta } = pkg(name); const target = path.join(OUT, name === 'emojibase-data' ? 'emoji' : name); fs.rmSync(target, { recursive: true, force: true }); fs.mkdirSync(target, { recursive: true }); const record = { name, version: meta.version, license: meta.license, homepage: meta.homepage ?? null, files: [] }; for (const [from, to] of files) { const buf = fs.readFileSync(path.join(dir, from)); fs.mkdirSync(path.dirname(path.join(target, to)), { recursive: true }); fs.writeFileSync(path.join(target, to), buf); record.files.push({ path: path.posix.join(path.basename(target), to), from, sha256: sha256(buf) }); } if (transform) { for (const [to, content] of transform(dir)) { fs.writeFileSync(path.join(target, to), content); record.files.push({ path: path.posix.join(path.basename(target), to), generated: true, sha256: sha256(Buffer.from(content)) }); } } provenance.packages.push(record); } const katexFonts = fs.readdirSync(path.join(nodeModules, 'katex', 'dist', 'fonts')).filter((f) => f.endsWith('.woff2')); copy('katex', [ ['dist/katex.min.js', 'katex.min.js'], ['dist/katex.min.css', 'katex.min.css'], ['LICENSE', 'LICENSE'], ...katexFonts.map((f) => [`dist/fonts/${f}`, `fonts/${f}`]), ]); copy('mermaid', [ ['dist/mermaid.min.js', 'mermaid.min.js'], ['LICENSE', 'LICENSE'], ]); copy('emojibase-data', [['LICENSE', 'LICENSE']], (dir) => { const compact = JSON.parse(fs.readFileSync(path.join(dir, 'en', 'compact.json'), 'utf8')); const shortcodes = JSON.parse(fs.readFileSync(path.join(dir, 'en', 'shortcodes', 'emojibase.json'), 'utf8')); const unicodeByHex = new Map(); const visit = (e) => { unicodeByHex.set(e.hexcode, e.unicode); for (const skin of e.skins ?? []) visit(skin); }; compact.forEach(visit); const map = {}; for (const [hex, codes] of Object.entries(shortcodes)) { const unicode = unicodeByHex.get(hex); if (!unicode) continue; for (const code of Array.isArray(codes) ? codes : [codes]) map[code] ??= unicode; } const body = `// Generated by scripts/vendor-chat-libs.mjs from emojibase-data ${PINNED['emojibase-data']} (MIT):\n` + '// en/shortcodes/emojibase.json joined with en/compact.json. Do not edit.\n' + `export default ${JSON.stringify(map)};\n`; return [['shortcodes.js', body], ['package.json', '{\n "type": "module"\n}\n']]; }); // Only the symbols chat-editor.js uses; esbuild tree-shakes the rest. The output is deterministic for the // pinned inputs, so regenerating it with the same lock reproduces the recorded hash. const CODEMIRROR_EXPORTS = { '@codemirror/state': ['Annotation', 'Compartment', 'EditorSelection', 'EditorState', 'StateField'], '@codemirror/view': ['Decoration', 'EditorView', 'WidgetType', 'keymap', 'placeholder'], '@codemirror/commands': ['history', 'historyKeymap', 'insertNewline', 'standardKeymap'], }; { for (const name of [...Object.keys(CODEMIRROR_EXPORTS), 'esbuild']) pkg(name); const workspace = path.resolve(nodeModules, '..'); const esbuild = createRequire(path.join(workspace, 'package.json'))('esbuild'); const entry = Object.entries(CODEMIRROR_EXPORTS).map(([name, symbols]) => `export { ${symbols.join(', ')} } from '${name}';`).join('\n'); const result = await esbuild.build({ stdin: { contents: entry, resolveDir: workspace, sourcefile: 'codemirror-entry.js', loader: 'js' }, bundle: true, format: 'esm', platform: 'browser', target: 'chrome120', write: false, metafile: true, legalComments: 'inline', charset: 'utf8', logLevel: 'error', absWorkingDir: workspace, }); // Every package that contributed bytes to the bundle (after tree-shaking), with its own license text. const bundled = new Map(); const [output] = Object.values(result.metafile.outputs); for (const [input, { bytesInOutput }] of Object.entries(output.inputs)) { const match = /^.*node_modules\/((?:@[^/]+\/)?[^/]+)\//.exec(input.replace(/\\/g, '/')); if (match && bytesInOutput > 0) bundled.set(match[1], path.join(workspace, ...match[0].split('/'))); } const target = path.join(OUT, 'codemirror'); fs.rmSync(target, { recursive: true, force: true }); fs.mkdirSync(path.join(target, 'licenses'), { recursive: true }); const record = { name: 'codemirror', bundler: `esbuild@${PINNED.esbuild}`, exports: CODEMIRROR_EXPORTS, patches: ['style-mod: use constructed stylesheets on Document as well as ShadowRoot; no inline style element under the application CSP'], bundled: [], files: [] }; for (const [name, dir] of [...bundled].sort(([a], [b]) => a.localeCompare(b))) { const meta = JSON.parse(fs.readFileSync(path.join(dir, 'package.json'), 'utf8')); const license = fs.readdirSync(dir).find((f) => /^licen[cs]e(\..*)?$/i.test(f)); if (!license) throw new Error(`${name} has no license file`); const buf = fs.readFileSync(path.join(dir, license)); const to = `licenses/${name.replace('/', '__')}.LICENSE`; fs.writeFileSync(path.join(target, to), buf); record.bundled.push({ name, version: meta.version, license: meta.license }); record.files.push({ path: `codemirror/${to}`, from: `${name}/${license}`, sha256: sha256(buf) }); } const header = `// Generated by scripts/vendor-chat-libs.mjs with esbuild ${PINNED.esbuild}. Do not edit.\n` + `// Bundles ${record.bundled.map((p) => `${p.name}@${p.version} (${p.license})`).join(', ')}.\n` + '// License texts: vendor/codemirror/licenses/.\n'; // Chromium supports constructed sheets on Document too. style-mod otherwise inserts a