@databrainhq/mcp-server 0.3.0-rc.13 → 0.3.0-rc.14
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/.mcp.json +6 -1
- package/dist/apps/server.d.ts +102 -389
- package/dist/apps/server.js +5 -5
- package/dist/apps/stdio.d.ts +1 -2
- package/dist/apps/stdio.js +6 -6
- package/dist/{bundle-TDQXWIPM.js → bundle-QJNCUMNY.js} +2 -2
- package/dist/{chunk-Q5EYDKSA.js → chunk-2F7V6RZR.js} +511 -107
- package/dist/chunk-2F7V6RZR.js.map +1 -0
- package/dist/{chunk-AEQTEE5C.js → chunk-AWICHHDU.js} +153 -61
- package/dist/chunk-AWICHHDU.js.map +1 -0
- package/dist/{chunk-X44C5GW2.js → chunk-CEGHG5U3.js} +59 -18
- package/dist/chunk-CEGHG5U3.js.map +1 -0
- package/dist/{chunk-233VGFHY.js → chunk-NIC5MAGC.js} +317 -147
- package/dist/chunk-NIC5MAGC.js.map +1 -0
- package/dist/{chunk-DTFYHLYN.js → chunk-S65KA3Z7.js} +26 -11
- package/dist/chunk-S65KA3Z7.js.map +1 -0
- package/dist/{chunk-PSJVYD4W.js → chunk-UYFFFAYV.js} +11722 -10560
- package/dist/chunk-UYFFFAYV.js.map +1 -0
- package/dist/{chunk-HCYZN7I3.js → chunk-VQ5G5AZV.js} +10 -7
- package/dist/chunk-VQ5G5AZV.js.map +1 -0
- package/dist/cli.d.ts +16 -15
- package/dist/cli.js +134 -36
- package/dist/cli.js.map +1 -1
- package/dist/custom-chart/custom-runtime.js +6 -6
- package/dist/custom-chart/template/chart.ts +116 -28
- package/dist/custom-chart/template/icon.svg +1 -1
- package/dist/custom-chart/template/manifest.json +66 -12
- package/dist/custom-chart/template/sample-data.json +21 -3
- package/dist/index.d.ts +1 -2
- package/dist/index.js +20 -10
- package/dist/index.js.map +1 -1
- package/dist/{main-VEKNKWL5.js → main-QCBVJSSR.js} +348 -155
- package/dist/main-QCBVJSSR.js.map +1 -0
- package/dist/{test-only-LJGOY2QA.js → test-only-6474NMTR.js} +12 -8
- package/dist/test-only-6474NMTR.js.map +1 -0
- package/package.json +20 -9
- package/dist/chunk-233VGFHY.js.map +0 -1
- package/dist/chunk-AEQTEE5C.js.map +0 -1
- package/dist/chunk-DTFYHLYN.js.map +0 -1
- package/dist/chunk-HCYZN7I3.js.map +0 -1
- package/dist/chunk-PSJVYD4W.js.map +0 -1
- package/dist/chunk-Q5EYDKSA.js.map +0 -1
- package/dist/chunk-X44C5GW2.js.map +0 -1
- package/dist/main-VEKNKWL5.js.map +0 -1
- package/dist/test-only-LJGOY2QA.js.map +0 -1
- /package/dist/{bundle-TDQXWIPM.js.map → bundle-QJNCUMNY.js.map} +0 -0
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/custom-chart/bundle.ts","../src/custom-chart/assets.ts","../src/custom-chart/sanitize-svg.ts","../src/custom-chart/types.ts","../src/custom-chart/icon.ts","../src/custom-chart/manifest.ts","../src/custom-chart/zip.ts"],"sourcesContent":["import { createHash } from 'node:crypto';\nimport { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';\nimport { join, resolve, sep } from 'node:path';\nimport { build as esbuild, type Plugin } from 'esbuild';\nimport { readRuntimeShim } from './assets.js';\nimport { sanitizeIcon, validateIcon } from './icon.js';\nimport { sortKeys, validateManifest } from './manifest.js';\nimport {\n MAX_BUNDLE_CSS_BYTES,\n MAX_BUNDLE_JS_BYTES,\n type CustomChartBundle,\n type CustomChartManifest,\n type Problem,\n} from './types.js';\nimport { writeBundleZip } from './zip.js';\n\nconst utf8Bytes = (text: string): number => Buffer.byteLength(text, 'utf8');\n\n/**\n * Content hash exactly as the backend computes it (brief section 5 and\n * FINDINGS be-core 2): sha256 hex over\n * JSON.stringify({ manifest, bundleJs, bundleCss, iconSvg }) in that key\n * order, the manifest keys sorted recursively (arrays keep their order) and\n * bundleCss always present, as \"\" when there is none.\n */\nexport function contentHash(bundle: CustomChartBundle): string {\n const payload = JSON.stringify({\n manifest: sortKeys(bundle.manifest),\n bundleJs: bundle.bundleJs,\n bundleCss: bundle.bundleCss ?? '',\n iconSvg: bundle.iconSvg,\n });\n return createHash('sha256').update(payload, 'utf8').digest('hex');\n}\n\n/** Bytes the upload carries: the four artifacts as UTF-8. */\nexport function bundleSizeBytes(bundle: CustomChartBundle): number {\n return (\n utf8Bytes(JSON.stringify(bundle.manifest)) +\n utf8Bytes(bundle.bundleJs) +\n (bundle.bundleCss ? utf8Bytes(bundle.bundleCss) : 0) +\n utf8Bytes(bundle.iconSvg)\n );\n}\n\nexport interface ValidateBundleInput {\n manifest: unknown;\n bundleJs?: string;\n bundleCss?: string;\n iconSvg?: string;\n}\n\n/**\n * The client side of the upload validation (the server repeats it). Manifest\n * rules first, then the bundle sizes, then the icon.\n */\nexport function validateBundle(input: ValidateBundleInput): Problem[] {\n const problems = validateManifest(input.manifest);\n if (input.bundleJs !== undefined) {\n if (typeof input.bundleJs !== 'string') {\n problems.push({ code: 'bundle.js.type', path: '/bundleJs', message: 'bundleJs must be a string.' });\n } else if (input.bundleJs.trim().length === 0) {\n problems.push({ code: 'bundle.js.empty', path: '/bundleJs', message: 'bundleJs must contain JavaScript.' });\n } else if (utf8Bytes(input.bundleJs) > MAX_BUNDLE_JS_BYTES) {\n problems.push({\n code: 'bundle.js.size',\n path: '/bundleJs',\n message: `bundleJs must be at most ${MAX_BUNDLE_JS_BYTES} bytes (2 MiB) as UTF-8. Drop or shrink a dependency.`,\n });\n }\n }\n if (input.bundleCss !== undefined) {\n if (typeof input.bundleCss !== 'string') {\n problems.push({ code: 'bundle.css.type', path: '/bundleCss', message: 'bundleCss must be a string when present.' });\n } else if (utf8Bytes(input.bundleCss) > MAX_BUNDLE_CSS_BYTES) {\n problems.push({\n code: 'bundle.css.size',\n path: '/bundleCss',\n message: `bundleCss must be at most ${MAX_BUNDLE_CSS_BYTES} bytes (256 KiB).`,\n });\n }\n }\n if (input.iconSvg !== undefined) problems.push(...validateIcon(input.iconSvg, '/iconSvg'));\n return problems;\n}\n\nexport interface ChartProject {\n dir: string;\n manifest: unknown;\n manifestText?: string;\n chartEntry?: string;\n css?: string;\n icon?: string;\n sampleData?: unknown;\n problems: Problem[];\n}\n\n/** Read a chart project folder. Missing or unparsable files become problems. */\nexport function readProject(dirInput: string): ChartProject {\n const dir = resolve(dirInput);\n const problems: Problem[] = [];\n const project: ChartProject = { dir, manifest: undefined, problems };\n\n const manifestPath = join(dir, 'manifest.json');\n if (!existsSync(manifestPath)) {\n problems.push({ code: 'project.manifest.missing', path: '/manifest.json', message: `No manifest.json in ${dir}.` });\n } else {\n project.manifestText = readFileSync(manifestPath, 'utf8');\n try {\n project.manifest = JSON.parse(project.manifestText);\n } catch (err) {\n problems.push({\n code: 'project.manifest.parse',\n path: '/manifest.json',\n message: `manifest.json is not valid JSON: ${(err as Error).message}`,\n });\n }\n }\n\n const entry = ['chart.ts', 'chart.js'].map((name) => join(dir, name)).find((path) => existsSync(path));\n if (!entry) {\n problems.push({ code: 'project.chart.missing', path: '/chart.ts', message: `No chart.ts in ${dir}. It must export render(ctx).` });\n } else {\n project.chartEntry = entry;\n }\n\n const cssPath = join(dir, 'chart.css');\n if (existsSync(cssPath)) project.css = readFileSync(cssPath, 'utf8');\n\n const iconPath = join(dir, 'icon.svg');\n if (!existsSync(iconPath)) {\n problems.push({ code: 'bundle.icon.required', path: '/icon.svg', message: `No icon.svg in ${dir}.` });\n } else {\n project.icon = readFileSync(iconPath, 'utf8');\n }\n\n const samplePath = join(dir, 'sample-data.json');\n if (existsSync(samplePath)) {\n try {\n project.sampleData = JSON.parse(readFileSync(samplePath, 'utf8'));\n } catch (err) {\n problems.push({\n code: 'project.sampleData.parse',\n path: '/sample-data.json',\n message: `sample-data.json is not valid JSON: ${(err as Error).message}`,\n });\n }\n }\n return project;\n}\n\n/** validate = read the folder, check the manifest and the icon. No esbuild. */\nexport function validateProject(dir: string): { ok: boolean; problems: Problem[]; project: ChartProject } {\n const project = readProject(dir);\n const problems = [...project.problems];\n if (project.manifest !== undefined) problems.push(...validateManifest(project.manifest));\n // The build strips the XML declaration and comments before upload, so check that form.\n if (project.icon !== undefined) problems.push(...validateIcon(sanitizeIcon(project.icon)));\n if (project.css !== undefined && utf8Bytes(project.css) > MAX_BUNDLE_CSS_BYTES) {\n problems.push({ code: 'bundle.css.size', path: '/chart.css', message: `chart.css must be at most ${MAX_BUNDLE_CSS_BYTES} bytes.` });\n }\n return { ok: problems.length === 0, problems, project };\n}\n\nexport interface BuildOutputs extends CustomChartBundle {\n manifest: CustomChartManifest;\n contentHash: string;\n sizeBytes: number;\n}\n\nexport interface BuildResult {\n ok: boolean;\n problems: Problem[];\n outputs?: BuildOutputs;\n /** Absolute paths written under dist/, in write order. Empty when write is false. */\n written: string[];\n}\n\nexport const DIST_FILES = ['index.js', 'manifest.json', 'icon.svg', 'index.css', 'bundle.zip'] as const;\n\n/**\n * Every import must resolve to a file inside the chart project. A relative\n * path that climbs out (`../../.config/x.json`) would be inlined into the\n * bundle and uploaded with it; the build refuses it with a problem instead.\n */\nfunction insideProjectOnly(dir: string): Plugin {\n const root = dir.endsWith(sep) ? dir : `${dir}${sep}`;\n return {\n name: 'databrain-inside-project-only',\n setup(build) {\n build.onResolve({ filter: /.*/ }, async (args) => {\n if (args.kind === 'entry-point' || args.pluginData?.databrainChecked) return undefined;\n const resolved = await build.resolve(args.path, {\n kind: args.kind,\n resolveDir: args.resolveDir,\n importer: args.importer,\n pluginData: { databrainChecked: true },\n });\n if (resolved.errors.length > 0) return { errors: resolved.errors };\n if (resolved.external || resolved.path.startsWith(root)) return resolved;\n return {\n errors: [\n {\n text: `\"${args.path}\" is outside the chart project. Only files inside ${dir} can be imported.`,\n },\n ],\n };\n });\n },\n };\n}\n\n/**\n * Compile chart.ts with esbuild to one IIFE, prepend the runtime shim and\n * the slot id list the shim reads, enforce the 2 MiB gate and write dist/.\n * See brief section 4 and contract/runtime-shim.js.\n */\nexport async function buildProject(dir: string, options: { write?: boolean } = {}): Promise<BuildResult> {\n const validated = validateProject(dir);\n const { project } = validated;\n const problems = [...validated.problems];\n if (!project.chartEntry || project.manifest === undefined || problems.length > 0) {\n return { ok: false, problems, written: [] };\n }\n const manifest = project.manifest as CustomChartManifest;\n\n // Bundled for the browser with no externals: every import is resolved\n // inside the project or it is a build error (a node builtin like \"fs\" does\n // not resolve on the browser platform), so dist/index.js never carries a\n // dangling require. The entry must be an ES module that exports render():\n // the shim reads globalThis.__dbnCustomChart.render, and a CommonJS\n // module.exports or a missing export would only fail later, as a blank tile.\n const common = {\n plugins: [insideProjectOnly(project.dir)],\n entryPoints: [project.chartEntry],\n absWorkingDir: project.dir,\n bundle: true as const,\n target: 'es2020',\n platform: 'browser' as const,\n write: false as const,\n legalComments: 'none' as const,\n logLevel: 'silent' as const,\n charset: 'utf8' as const,\n };\n let moduleJs: string;\n try {\n const shape = await esbuild({ ...common, format: 'esm', metafile: true });\n const exported = Object.values(shape.metafile.outputs)[0]?.exports ?? [];\n if (!exported.includes('render')) {\n problems.push({\n code: 'bundle.build.noRender',\n path: '/chart.ts',\n message:\n 'chart.ts must be an ES module that exports a function named render: `export function render(ctx) { ... }`. module.exports and default exports are not read.',\n });\n return { ok: false, problems, written: [] };\n }\n const result = await esbuild({\n ...common,\n format: 'iife',\n globalName: '__dbnCustomChart',\n minify: true,\n });\n moduleJs = result.outputFiles.map((file) => file.text).join('\\n');\n } catch (err) {\n const errors = (err as { errors?: { text: string; location?: { file?: string; line?: number } | null }[] }).errors;\n if (Array.isArray(errors) && errors.length > 0) {\n for (const error of errors) {\n const where = error.location ? `${error.location.file ?? 'chart.ts'}:${error.location.line ?? 0}` : 'chart.ts';\n problems.push({ code: 'bundle.build.failed', path: `/${where}`, message: error.text });\n }\n } else {\n problems.push({ code: 'bundle.build.failed', path: '/chart.ts', message: (err as Error).message });\n }\n return { ok: false, problems, written: [] };\n }\n\n // Shim first (byte identical to contract/runtime-shim.js), then the slot ids\n // so every manifest slot is present on ctx.slots, then the chart module.\n // chart.css is NOT inlined: the host injects bundleCss into the frame\n // (brief section 6.5); dist/index.css carries it for provenance and upload.\n const shim = readRuntimeShim();\n const slotIds = manifest.slots.map((slot) => slot.id);\n const globals = `globalThis.__dbnCustomChartSlots=${JSON.stringify(slotIds)};\\n`;\n const bundleJs = `${shim.trimEnd()}\\n${globals}${moduleJs.trimEnd()}\\nglobalThis.__dbnCustomChart=__dbnCustomChart;\\n`;\n const iconSvg = sanitizeIcon(project.icon as string);\n const bundleCss = project.css;\n\n problems.push(...validateBundle({ manifest, bundleJs, bundleCss, iconSvg }));\n if (problems.length > 0) return { ok: false, problems, written: [] };\n\n const bundle: CustomChartBundle = { manifest, bundleJs, bundleCss, iconSvg };\n const outputs: BuildOutputs = {\n ...bundle,\n contentHash: contentHash(bundle),\n sizeBytes: bundleSizeBytes(bundle),\n };\n\n const written: string[] = [];\n if (options.write !== false) {\n const dist = join(project.dir, 'dist');\n mkdirSync(dist, { recursive: true });\n const manifestJson = JSON.stringify(manifest, null, 2) + '\\n';\n const write = (name: string, content: string | Uint8Array) => {\n const path = join(dist, name);\n writeFileSync(path, content);\n written.push(path);\n };\n write('index.js', bundleJs);\n write('manifest.json', manifestJson);\n write('icon.svg', iconSvg);\n if (bundleCss !== undefined) write('index.css', bundleCss);\n write(\n 'bundle.zip',\n writeBundleZip({\n 'index.js': bundleJs,\n 'manifest.json': manifestJson,\n 'icon.svg': iconSvg,\n ...(bundleCss !== undefined ? { 'index.css': bundleCss } : {}),\n }),\n );\n }\n\n return { ok: true, problems: [], outputs, written };\n}\n\n/** Read a previous build from dist/ (for upload without rebuilding). */\nexport function readDist(dir: string): { bundle?: CustomChartBundle; problems: Problem[] } {\n const dist = join(resolve(dir), 'dist');\n const problems: Problem[] = [];\n const need = ['index.js', 'manifest.json', 'icon.svg'];\n for (const name of need) {\n if (!existsSync(join(dist, name))) {\n problems.push({ code: 'project.dist.missing', path: `/dist/${name}`, message: `dist/${name} is missing. Run databrain custom-chart build first.` });\n }\n }\n if (problems.length > 0) return { problems };\n let manifest: unknown;\n try {\n manifest = JSON.parse(readFileSync(join(dist, 'manifest.json'), 'utf8'));\n } catch (err) {\n return { problems: [{ code: 'project.dist.parse', path: '/dist/manifest.json', message: (err as Error).message }] };\n }\n const manifestProblems = validateManifest(manifest);\n if (manifestProblems.length > 0) return { problems: manifestProblems };\n const cssPath = join(dist, 'index.css');\n return {\n problems,\n bundle: {\n manifest: manifest as CustomChartManifest,\n bundleJs: readFileSync(join(dist, 'index.js'), 'utf8'),\n bundleCss: existsSync(cssPath) ? readFileSync(cssPath, 'utf8') : undefined,\n iconSvg: readFileSync(join(dist, 'icon.svg'), 'utf8'),\n },\n };\n}\n\n/** Compile caller-supplied source in memory. No filesystem, package resolution or execution. */\nexport async function buildSource(input: {\n manifest: unknown; chartTs: string; bundleCss?: string; iconSvg: string;\n}): Promise<BuildResult> {\n const problems = validateManifest(input.manifest);\n if (problems.length) return { ok: false, problems, written: [] };\n const manifest = input.manifest as CustomChartManifest;\n try {\n const common = {\n stdin: { contents: input.chartTs, loader: 'ts' as const, sourcefile: 'chart.ts' },\n bundle: true, platform: 'browser' as const, target: 'es2020', write: false,\n legalComments: 'none' as const, logLevel: 'silent' as const,\n plugins: [{ name: 'no-host-imports', setup(build: Parameters<Plugin['setup']>[0]) {\n build.onResolve({ filter: /.*/ }, args => ({ errors: [{ text:\n `Import \"${args.path}\" is unavailable in hosted chart source. Use browser APIs or pass a prebuilt bundle.` }] }));\n } }],\n };\n let shape = await esbuild({ ...common, format: 'esm', metafile: true });\n if (!Object.values(shape.metafile.outputs)[0]?.exports.includes('render')) {\n common.stdin.contents += '\\nexport { render };';\n shape = await esbuild({ ...common, format: 'esm', metafile: true });\n }\n if (!Object.values(shape.metafile.outputs)[0]?.exports.includes('render')) {\n return { ok: false, problems: [{ code: 'bundle.build.noRender', path: '/chart.ts', message: 'Export function render(ctx).' }], written: [] };\n }\n const built = await esbuild({ ...common, format: 'iife', globalName: '__dbnCustomChart', minify: true });\n const bundle: CustomChartBundle = {\n manifest, bundleCss: input.bundleCss, iconSvg: sanitizeIcon(input.iconSvg),\n bundleJs: `${readRuntimeShim().trimEnd()}\\nglobalThis.__dbnCustomChartSlots=${JSON.stringify(manifest.slots.map(s => s.id))};\\n${built.outputFiles!.map(f => f.text).join('\\n')}\\nglobalThis.__dbnCustomChart=__dbnCustomChart;\\n`,\n };\n const errors = validateBundle(bundle);\n return errors.length ? { ok: false, problems: errors, written: [] } : {\n ok: true, problems: [], written: [], outputs: { ...bundle, contentHash: contentHash(bundle), sizeBytes: bundleSizeBytes(bundle) },\n };\n } catch (error) {\n return { ok: false, problems: [{ code: 'bundle.build.failed', path: '/chart.ts', message: (error as Error).message }], written: [] };\n }\n}\n","import { buildSync } from 'esbuild';\nimport { existsSync, readdirSync, readFileSync, statSync } from 'node:fs';\nimport { dirname, join, resolve } from 'node:path';\nimport { fileURLToPath } from 'node:url';\n\n/**\n * The template project and the runtime shim are shipped as plain files, not\n * as strings baked into the bundle, so that they stay byte identical to the\n * copies other repos test against. In source they live in\n * src/custom-chart/; tsup copies them to dist/custom-chart/ after a build\n * (see tsup.config.ts onSuccess). This finds them from either place.\n */\nlet cachedDir: string | undefined;\n\nexport function customChartAssetDir(): string {\n if (cachedDir) return cachedDir;\n const here = dirname(fileURLToPath(import.meta.url));\n const candidates = [\n here,\n join(here, 'custom-chart'),\n join(here, '..', 'custom-chart'),\n join(here, '..', '..', 'custom-chart'),\n join(here, '..', 'src', 'custom-chart'),\n ];\n for (const candidate of candidates) {\n if (existsSync(join(candidate, 'runtime-shim.js')) && existsSync(join(candidate, 'template', 'manifest.json'))) {\n cachedDir = resolve(candidate);\n return cachedDir;\n }\n }\n throw new Error(\n `Custom chart assets not found near ${here}. Expected runtime-shim.js and template/ (run npm run build).`,\n );\n}\n\nexport function readRuntimeShim(): string {\n return readFileSync(join(customChartAssetDir(), 'runtime-shim.js'), 'utf8');\n}\n\nexport function templateDir(): string {\n return join(customChartAssetDir(), 'template');\n}\n\n/** Every file in the template folder, keyed by its file name. */\nexport function readTemplateFiles(): Record<string, string> {\n const dir = templateDir();\n const files: Record<string, string> = {};\n for (const name of readdirSync(dir).sort()) {\n const path = join(dir, name);\n if (statSync(path).isFile()) files[name] = readFileSync(path, 'utf8');\n }\n return files;\n}\n\nlet cachedBrowserRuntime: string | undefined;\n/** Packaged asset in dist; source mode compiles only this fixed trusted entrypoint. */\nexport function readCustomChartBrowserRuntime(): string {\n if (cachedBrowserRuntime) return cachedBrowserRuntime;\n const dir = customChartAssetDir();\n const packed = join(dir, 'custom-runtime.js');\n cachedBrowserRuntime = existsSync(packed) ? readFileSync(packed, 'utf8') : buildSync({\n entryPoints: [join(dir, '..', 'apps', 'custom-runtime', 'browser.ts')],\n bundle: true, write: false, platform: 'browser', target: 'es2020', minify: true,\n }).outputFiles[0].text;\n return cachedBrowserRuntime;\n}\n","/**\n * Allow-list SVG sanitiser for icon.svg. A verbatim copy of the backend's\n * `serverless/express/src/utils/helpers/sanitizeSvg.ts` (the one that gates\n * the upload), so the CLI and the validate tool refuse exactly what the\n * server refuses instead of guessing with regexes. Keep the two files in\n * step: a rule added on one side and not the other means \"validate says ok,\n * upload says no\".\n *\n * WHAT IT DOES\n * ------------\n * - Drops the DOCTYPE (entity expansion) and every processing instruction\n * except the XML declaration; drops CDATA and comments.\n * - Keeps only elements on ALLOWED_ELEMENTS. A disallowed element and\n * everything inside it is removed (`<script>`, `<foreignObject>`,\n * `<iframe>`, `<animate>` ... ).\n * - Keeps only attributes on ALLOWED_ATTRIBUTES (plus `data-*` and the\n * `xmlns*` declarations). Every `on*` attribute goes. Any attribute whose\n * value carries `javascript:`, `data:text/html`, or `vbscript:` after\n * entity decoding goes.\n * - `href` / `xlink:href` survive only as a same-document `#id` reference,\n * or, on `<image>` alone, as a `data:image/(png|jpeg|gif|webp);base64`\n * URI. An external `<image href>` is removed with the element.\n * - `<style>` and `style=\"\"` survive only when they contain no `url(`,\n * `@import`, `expression(` or `-moz-binding`. A style that reaches out\n * of the file is dropped entirely rather than partially rewritten.\n *\n * Returns the cleaned markup and a count of what was removed, so the caller\n * can refuse the icon rather than silently rewriting it.\n */\nconst ALLOWED_ELEMENTS: ReadonlySet<string> = new Set([\n 'svg',\n 'g',\n 'defs',\n 'symbol',\n 'use',\n 'title',\n 'desc',\n 'metadata',\n 'path',\n 'rect',\n 'circle',\n 'ellipse',\n 'line',\n 'polyline',\n 'polygon',\n 'text',\n 'tspan',\n 'textpath',\n 'image',\n 'lineargradient',\n 'radialgradient',\n 'stop',\n 'clippath',\n 'mask',\n 'pattern',\n 'marker',\n 'filter',\n 'fegaussianblur',\n 'feoffset',\n 'feblend',\n 'fecolormatrix',\n 'fecomposite',\n 'feflood',\n 'femerge',\n 'femergenode',\n 'fedropshadow',\n 'style',\n 'switch',\n]);\n\nconst ALLOWED_ATTRIBUTES: ReadonlySet<string> = new Set([\n 'id',\n 'class',\n 'style',\n 'href',\n 'xlink:href',\n 'xml:space',\n 'lang',\n 'xml:lang',\n 'version',\n 'baseprofile',\n 'viewbox',\n 'preserveaspectratio',\n 'width',\n 'height',\n 'x',\n 'y',\n 'x1',\n 'y1',\n 'x2',\n 'y2',\n 'cx',\n 'cy',\n 'r',\n 'rx',\n 'ry',\n 'fx',\n 'fy',\n 'fr',\n 'd',\n 'points',\n 'transform',\n 'transform-origin',\n 'fill',\n 'fill-opacity',\n 'fill-rule',\n 'stroke',\n 'stroke-width',\n 'stroke-linecap',\n 'stroke-linejoin',\n 'stroke-miterlimit',\n 'stroke-dasharray',\n 'stroke-dashoffset',\n 'stroke-opacity',\n 'opacity',\n 'color',\n 'display',\n 'visibility',\n 'overflow',\n 'clip-path',\n 'clip-rule',\n 'clippathunits',\n 'mask',\n 'maskunits',\n 'maskcontentunits',\n 'filter',\n 'filterunits',\n 'primitiveunits',\n 'in',\n 'in2',\n 'result',\n 'mode',\n 'operator',\n 'values',\n 'type',\n 'stddeviation',\n 'dx',\n 'dy',\n 'flood-color',\n 'flood-opacity',\n 'offset',\n 'stop-color',\n 'stop-opacity',\n 'gradientunits',\n 'gradienttransform',\n 'spreadmethod',\n 'patternunits',\n 'patterncontentunits',\n 'patterntransform',\n 'markerunits',\n 'markerwidth',\n 'markerheight',\n 'refx',\n 'refy',\n 'orient',\n 'font-family',\n 'font-size',\n 'font-weight',\n 'font-style',\n 'letter-spacing',\n 'text-anchor',\n 'dominant-baseline',\n 'alignment-baseline',\n 'text-decoration',\n 'word-spacing',\n 'paint-order',\n 'vector-effect',\n 'shape-rendering',\n 'text-rendering',\n 'image-rendering',\n 'color-interpolation',\n 'color-interpolation-filters',\n 'enable-background',\n 'startoffset',\n 'method',\n 'spacing',\n 'requiredfeatures',\n 'systemlanguage',\n 'role',\n 'aria-label',\n 'aria-labelledby',\n 'aria-hidden',\n 'focusable',\n 'tabindex',\n]);\n\n/** Pattern over a tag's raw text: name, then attributes, then optional `/`. */\nconst TAG_RE = /^<\\s*(\\/?)\\s*([A-Za-z_][\\w:.-]*)([\\s\\S]*?)(\\/?)\\s*>$/;\n\nconst ATTRIBUTE_RE =\n /([^\\s=/\"']+)(?:\\s*=\\s*(?:\"([^\"]*)\"|'([^']*)'|([^\\s\"'>]+)))?/g;\n\nconst decodeEntities = (value: string): string =>\n value\n .replace(/&#x([0-9a-fA-F]+);?/g, (_m, hex: string) =>\n String.fromCodePoint(parseInt(hex, 16)),\n )\n .replace(/&#(\\d+);?/g, (_m, dec: string) =>\n String.fromCodePoint(parseInt(dec, 10)),\n )\n .replace(/</g, '<')\n .replace(/>/g, '>')\n .replace(/"/g, '\"')\n .replace(/'/g, \"'\")\n .replace(/&/g, '&');\n\n/** Entity-decode, drop control and whitespace characters, lower-case. Then a scheme cannot hide as `java\\tscript:`. */\nconst normaliseForSchemeCheck = (value: string): string =>\n decodeEntities(value)\n .replace(/[\\u0000-\\u0020\\u007f-\\u00a0\\u200b-\\u200f\\u2028\\u2029\\ufeff]/g, '')\n .toLowerCase();\n\nconst DANGEROUS_SCHEME_RE =\n /^(javascript|vbscript|livescript|mocha|data:text\\/html|data:application)/;\n\nconst isDangerousValue = (value: string): boolean => {\n const normalised = normaliseForSchemeCheck(value);\n return (\n DANGEROUS_SCHEME_RE.test(normalised) ||\n normalised.includes('javascript:') ||\n normalised.includes('vbscript:')\n );\n};\n\nconst SAFE_DATA_IMAGE_RE =\n /^data:image\\/(png|jpeg|jpg|gif|webp);base64,[a-z0-9+/=]+$/;\n\nconst isAcceptableHref = (value: string, element: string): boolean => {\n const normalised = normaliseForSchemeCheck(value);\n if (normalised.startsWith('#')) return true;\n return element === 'image' && SAFE_DATA_IMAGE_RE.test(normalised);\n};\n\n/**\n * A style that leaves the document is dropped whole. `url(#id)` is a same\n * document paint server reference (`fill: url(#gradient)`) and is the one\n * `url(` a logo legitimately uses, so it is blanked before the test.\n */\nconst LOCAL_URL_RE = /url\\s*\\(\\s*(['\"]?)#[^)'\"]*\\1\\s*\\)/gi;\nconst STYLE_ESCAPE_RE =\n /url\\s*\\(|@import|expression\\s*\\(|-moz-binding|behavior\\s*:/i;\n\nconst isSafeStyle = (value: string): boolean =>\n !STYLE_ESCAPE_RE.test(decodeEntities(value).replace(LOCAL_URL_RE, ''));\n\nexport type SanitizeSvgResult = {\n svg: string;\n /** Elements, attributes and declarations that were taken out. */\n removed: number;\n /** True when the input did not even look like an SVG document. */\n isSvg: boolean;\n};\n\ntype Token =\n | { kind: 'text'; raw: string }\n | { kind: 'tag'; raw: string }\n | { kind: 'special'; raw: string };\n\n/**\n * Split markup into text, tags, and the specials (comments, CDATA, DOCTYPE,\n * processing instructions) that need their own terminators.\n */\nconst tokenize = (input: string): Token[] => {\n const tokens: Token[] = [];\n let index = 0;\n const length = input.length;\n\n const pushSpecial = (end: number, terminator: string): void => {\n const close = input.indexOf(terminator, end);\n const stop = close === -1 ? length : close + terminator.length;\n tokens.push({ kind: 'special', raw: input.slice(index, stop) });\n index = stop;\n };\n\n while (index < length) {\n const open = input.indexOf('<', index);\n if (open === -1) {\n tokens.push({ kind: 'text', raw: input.slice(index) });\n break;\n }\n if (open > index) {\n tokens.push({ kind: 'text', raw: input.slice(index, open) });\n index = open;\n }\n if (input.startsWith('<!--', index)) {\n pushSpecial(index + 4, '-->');\n continue;\n }\n if (input.startsWith('<![CDATA[', index)) {\n pushSpecial(index + 9, ']]>');\n continue;\n }\n if (input.startsWith('<?', index)) {\n pushSpecial(index + 2, '?>');\n continue;\n }\n if (input.startsWith('<!', index)) {\n // DOCTYPE may carry an internal subset in [ ... ]; find the real end.\n const bracket = input.indexOf('[', index);\n const plainEnd = input.indexOf('>', index);\n if (bracket !== -1 && (plainEnd === -1 || bracket < plainEnd)) {\n const subsetEnd = input.indexOf(']', bracket);\n pushSpecial(subsetEnd === -1 ? index + 2 : subsetEnd, '>');\n } else {\n pushSpecial(index + 2, '>');\n }\n continue;\n }\n // Ordinary tag: ends at the first `>` outside a quoted attribute value.\n let cursor = index + 1;\n let quote: string | null = null;\n while (cursor < length) {\n const ch = input[cursor];\n if (quote) {\n if (ch === quote) quote = null;\n } else if (ch === '\"' || ch === \"'\") {\n quote = ch;\n } else if (ch === '>') {\n break;\n }\n cursor += 1;\n }\n const stop = cursor >= length ? length : cursor + 1;\n tokens.push({ kind: 'tag', raw: input.slice(index, stop) });\n index = stop;\n }\n return tokens;\n};\n\nconst isXmlDeclaration = (raw: string): boolean => /^<\\?xml\\b/i.test(raw);\n\n/**\n * Filter a tag's attributes. Returns the rebuilt attribute string (or `null`\n * when the element itself must go because its only href was hostile) and the\n * number of attributes dropped.\n */\nconst cleanAttributes = (\n element: string,\n rawAttributes: string,\n): { attributes: string; removed: number; dropElement: boolean } => {\n let removed = 0;\n let dropElement = false;\n const kept: string[] = [];\n ATTRIBUTE_RE.lastIndex = 0;\n let match: RegExpExecArray | null;\n while ((match = ATTRIBUTE_RE.exec(rawAttributes)) !== null) {\n const [whole, name, dq, sq, bare] = match;\n if (!name || name === '/') continue;\n const lower = name.toLowerCase();\n const value = dq ?? sq ?? bare ?? '';\n\n if (lower.startsWith('on')) {\n removed += 1;\n continue;\n }\n const isNamespaceDeclaration =\n lower === 'xmlns' || lower.startsWith('xmlns:');\n const isData = lower.startsWith('data-');\n if (!isNamespaceDeclaration && !isData && !ALLOWED_ATTRIBUTES.has(lower)) {\n removed += 1;\n continue;\n }\n if (isDangerousValue(value)) {\n removed += 1;\n continue;\n }\n if (lower === 'href' || lower === 'xlink:href') {\n if (!isAcceptableHref(value, element)) {\n removed += 1;\n // An <image> or <use> whose only reason to exist is an external\n // reference is not a logo any more once that reference is gone.\n if (element === 'image' || element === 'use') dropElement = true;\n continue;\n }\n }\n if (lower === 'style' && !isSafeStyle(value)) {\n removed += 1;\n continue;\n }\n kept.push(whole.trim());\n }\n return {\n attributes: kept.length ? ` ${kept.join(' ')}` : '',\n removed,\n dropElement,\n };\n};\n\n/**\n * Sentinel pushed before a `<style>` open tag so the whole element can be\n * unwound if its content turns out to reach outside the file. NUL bytes cannot\n * occur in well formed XML text, so no real chunk can collide with it, and it\n * is filtered out before the chunks are joined.\n */\nconst STYLE_OPEN_MARKER = '\\u0000style-open\\u0000';\n\nexport const sanitizeSvg = (input: string): SanitizeSvgResult => {\n const tokens = tokenize(input);\n const out: string[] = [];\n let removed = 0;\n let isSvg = false;\n\n // Depth inside a dropped element: while > 0 every token is discarded. The\n // stack tracks which open tags were dropped so the matching close is too.\n let dropDepth = 0;\n const dropStack: string[] = [];\n // Inside <style>: content must be checked as CSS, not copied blind.\n let styleBuffer: string[] | null = null;\n\n for (const token of tokens) {\n if (token.kind === 'special') {\n if (isXmlDeclaration(token.raw) && dropDepth === 0) {\n out.push(token.raw);\n } else {\n removed += 1;\n }\n continue;\n }\n\n if (token.kind === 'text') {\n if (dropDepth > 0) continue;\n if (styleBuffer) {\n styleBuffer.push(token.raw);\n } else {\n out.push(token.raw);\n }\n continue;\n }\n\n const parsed = TAG_RE.exec(token.raw);\n if (!parsed) {\n // Unparseable tag text. Inside a drop it goes anyway; outside, it is\n // not markup we can vouch for, so it goes too.\n removed += 1;\n continue;\n }\n const [, closing, rawName, rawAttributes, selfClosingSlash] = parsed;\n const name = rawName.toLowerCase();\n const isClosing = closing === '/';\n const isSelfClosing = selfClosingSlash === '/';\n\n if (name === 'svg') isSvg = true;\n\n if (dropDepth > 0) {\n if (isClosing) {\n if (dropStack[dropStack.length - 1] === name) {\n dropStack.pop();\n dropDepth -= 1;\n }\n } else if (!isSelfClosing) {\n dropStack.push(name);\n dropDepth += 1;\n }\n continue;\n }\n\n if (isClosing) {\n if (name === 'style' && styleBuffer) {\n const css = styleBuffer.join('');\n styleBuffer = null;\n if (isSafeStyle(css)) {\n out.push(css, token.raw);\n } else {\n // Drop the <style> we already emitted the open tag for.\n const openIndex = out.lastIndexOf(STYLE_OPEN_MARKER);\n if (openIndex !== -1) out.splice(openIndex);\n removed += 1;\n }\n continue;\n }\n if (ALLOWED_ELEMENTS.has(name)) out.push(token.raw);\n continue;\n }\n\n if (!ALLOWED_ELEMENTS.has(name)) {\n removed += 1;\n if (!isSelfClosing) {\n dropStack.push(name);\n dropDepth += 1;\n }\n continue;\n }\n\n const cleaned = cleanAttributes(name, rawAttributes);\n removed += cleaned.removed;\n if (cleaned.dropElement) {\n removed += 1;\n if (!isSelfClosing) {\n dropStack.push(name);\n dropDepth += 1;\n }\n continue;\n }\n\n const rebuilt = `<${rawName}${cleaned.attributes}${\n isSelfClosing ? '/' : ''\n }>`;\n if (name === 'style' && !isSelfClosing) {\n out.push(STYLE_OPEN_MARKER, rebuilt);\n styleBuffer = [];\n continue;\n }\n out.push(rebuilt);\n }\n\n // An unterminated <style> at EOF: keep nothing of it.\n if (styleBuffer) {\n const openIndex = out.lastIndexOf(STYLE_OPEN_MARKER);\n if (openIndex !== -1) out.splice(openIndex);\n removed += 1;\n }\n\n return {\n svg: out.filter((chunk) => chunk !== STYLE_OPEN_MARKER).join(''),\n removed,\n isSvg,\n };\n};\n","/**\n * Shared shapes for the custom chart package pipeline (DAT-925).\n * Problem is the one error shape used by the manifest validator, the bundle\n * builder, the icon check and the backend API (section 5 of the brief).\n */\nexport interface Problem {\n code: string;\n path: string;\n message: string;\n}\n\nexport type CustomChartFieldType = 'string' | 'number' | 'boolean';\n\nexport interface CustomChartSlot {\n id: string;\n label: string;\n role: 'dimension' | 'measure';\n types: CustomChartFieldType[];\n required: boolean;\n description?: string;\n}\n\nexport type CustomChartOption = { id: string; label: string; group?: string } & (\n | { type: 'boolean'; default: boolean }\n | { type: 'number'; default: number; min: number; max: number; step?: number }\n | { type: 'string'; default: string; maxLength: number }\n | { type: 'select'; default: string; choices: { value: string; label: string }[] }\n | { type: 'color'; default: string }\n);\n\nexport interface CustomChartTranslation {\n name?: string;\n description?: string;\n slots?: Record<string, { label?: string; description?: string }>;\n options?: Record<string, { label?: string; group?: string; choices?: Record<string, string> }>;\n}\n\nexport interface CustomChartManifest {\n schemaVersion: 1 | 2;\n id: string;\n name: string;\n version?: string;\n description?: string;\n slots: CustomChartSlot[];\n options: CustomChartOption[];\n translations?: Record<string, CustomChartTranslation>;\n}\n\n/** The four artifacts a build produces and an upload sends. */\nexport interface CustomChartBundle {\n manifest: CustomChartManifest;\n bundleJs: string;\n bundleCss?: string;\n iconSvg: string;\n}\n\nexport const MAX_MANIFEST_BYTES = 32 * 1024;\nexport const MAX_BUNDLE_JS_BYTES = 2 * 1024 * 1024;\nexport const MAX_BUNDLE_CSS_BYTES = 256 * 1024;\nexport const MAX_ICON_BYTES = 16 * 1024;\nexport const MAX_SLOTS = 16;\nexport const MAX_OPTIONS = 32;\n","import { sanitizeSvg } from './sanitize-svg.js';\nimport { MAX_ICON_BYTES, type Problem } from './types.js';\n\n/**\n * Strict SVG check for icon.svg (brief sections 4 and 5). Nothing is rewritten:\n * an icon either passes every rule or is refused with a problem that says what\n * to remove. The check is the backend's own allow-list sanitiser\n * (`sanitize-svg.ts`, a copy of the server file): the icon is refused when the\n * sanitiser would remove anything, when it carries an `<image>` element, or\n * when it is not one `<svg>` element. The backend runs the same sanitiser on\n * upload and refuses with the same codes: bundle.icon.required,\n * bundle.icon.size, bundle.icon.notSvg, bundle.icon.unsafe. The message names\n * the rule that tripped when one of the common ones did.\n */\nconst UNSAFE = 'bundle.icon.unsafe';\nconst RULES: { pattern: RegExp; message: string }[] = [\n { pattern: /<\\s*script\\b/i, message: 'Remove every <script> element from icon.svg.' },\n { pattern: /<\\s*foreignObject\\b/i, message: 'Remove every <foreignObject> element from icon.svg.' },\n { pattern: /<\\s*image\\b/i, message: 'Remove every <image> element from icon.svg. Draw with paths and shapes.' },\n { pattern: /[\\s/]on[a-z]+\\s*=/i, message: 'Remove every on*= event attribute from icon.svg.' },\n { pattern: /<!--/, message: 'Remove every <!-- comment --> from icon.svg.' },\n { pattern: /<!(?:ENTITY|DOCTYPE)\\b/i, message: 'Remove the DOCTYPE and entity declarations from icon.svg.' },\n { pattern: /<!\\[CDATA\\[/, message: 'Remove every CDATA section from icon.svg.' },\n { pattern: /(?:xlink:)?href\\s*=\\s*[\"']\\s*(?!#)/i, message: 'Every href in icon.svg must start with # (no external or data references).' },\n { pattern: /javascript\\s*:/i, message: 'Remove every javascript: reference from icon.svg.' },\n { pattern: /@import|expression\\s*\\(|-moz-binding/i, message: 'Remove @import, expression() and -moz-binding from icon.svg styles.' },\n { pattern: /url\\s*\\(\\s*['\"]?\\s*(?!#)/i, message: 'Every url() in icon.svg must point at a same-document #id.' },\n];\nconst GENERIC_UNSAFE =\n 'icon.svg must use only plain SVG drawing elements and attributes: no script, event handlers, foreignObject, image, animation, external hrefs, url() styles, comments, DOCTYPE or unknown attributes.';\n\nconst XML_DECLARATION = /^\\s*<\\?xml[^>]*\\?>\\s*/i;\n\n/** One `<svg ...>...</svg>` (or `<svg .../>`) and nothing else, after the XML declaration. */\nexport function isSingleSvgRoot(svg: string): boolean {\n const body = svg.replace(/^/, '').replace(XML_DECLARATION, '').trim();\n return /^<svg[\\s>][\\s\\S]*<\\/svg>$/i.test(body) || /^<svg\\b[^>]*\\/>$/i.test(body);\n}\n\nexport function validateIcon(svg: unknown, path = '/icon.svg'): Problem[] {\n if (typeof svg !== 'string' || svg.trim().length === 0) {\n return [{ code: 'bundle.icon.required', path, message: 'icon.svg is required and must not be empty.' }];\n }\n if (Buffer.byteLength(svg, 'utf8') > MAX_ICON_BYTES) {\n return [{ code: 'bundle.icon.size', path, message: `icon.svg must be at most ${MAX_ICON_BYTES} bytes.` }];\n }\n const cleaned = sanitizeSvg(svg);\n if (!cleaned.isSvg || !isSingleSvgRoot(cleaned.svg)) {\n return [{ code: 'bundle.icon.notSvg', path, message: 'icon.svg must be one <svg> element with nothing after it.' }];\n }\n if (cleaned.removed > 0 || /<\\s*image\\b/i.test(svg)) {\n const rule = RULES.find((candidate) => candidate.pattern.test(svg));\n return [{ code: UNSAFE, path, message: rule ? rule.message : GENERIC_UNSAFE }];\n }\n return [];\n}\n\n/**\n * The form the CLI writes to dist/icon.svg and uploads: BOM, XML declaration\n * and comments removed, trimmed. The server refuses a comment rather than\n * stripping it, so the build strips them here; everything else is left as the\n * author wrote it and must already pass validateIcon.\n */\nexport function sanitizeIcon(svg: string): string {\n return svg\n .replace(/^/, '')\n .replace(XML_DECLARATION, '')\n .replace(/<!--[\\s\\S]*?-->/g, '')\n .trim();\n}\n","import { z } from 'zod';\nimport {\n MAX_MANIFEST_BYTES,\n MAX_OPTIONS,\n MAX_SLOTS,\n type CustomChartManifest,\n type Problem,\n} from './types.js';\n\n/**\n * Manifest v2 validator (brief section 3). It is a strict superset of the\n * plugin's sandbox manifest v1: every v1 manifest validates with\n * schemaVersion 1, and schemaVersion 2 unlocks version, description, the\n * color option type, the number step, the option group, slot descriptions and\n * translations. Unknown keys are refused at every level.\n *\n * The checks run in the plugin parser's order, step for step, and the backend\n * (`serverless/express/src/lib/customCharts/manifest.ts`) runs the same steps,\n * so the three validators produce the same problem list for the same\n * manifest. The cross-repo fixtures in tests/fixtures/custom-chart-manifests\n * pin the first code for every invalid case.\n *\n * 1. object, schemaVersion (stop on failure)\n * 2. top-level unknown and missing keys (stop on failure)\n * 3. id, name, version, description\n * 4. slots: shape and count, then each slot (unknown and missing keys\n * first, then the fields), then duplicate ids\n * 5. options: shape and count, then each option (reserved id, type,\n * unknown and missing keys, then the fields), then duplicate ids\n * 6. translations, only when everything above is clean\n * 7. size, only when everything above is clean\n */\n\nconst ID_PATTERN = /^[a-z][a-z0-9_-]{0,63}$/;\nconst FORBIDDEN_IDS = ['constructor', 'prototype', '__proto__'];\nconst LANGUAGE_PATTERN = /^[a-z]{2}(-[A-Z]{2})?$/;\nconst SEMVER_PATTERN =\n /^(0|[1-9]\\d*)\\.(0|[1-9]\\d*)\\.(0|[1-9]\\d*)(-[0-9A-Za-z.-]+)?(\\+[0-9A-Za-z.-]+)?$/;\nconst COLOR_PATTERN = /^#[0-9a-fA-F]{6}$/;\nconst FIELD_TYPES = ['string', 'number', 'boolean'];\nconst MAX_TEXT = 256;\nconst MAX_DESCRIPTION = 500;\nconst MAX_GROUP = 64;\nconst MAX_CHOICES = 32;\nconst MAX_STRING_OPTION_LENGTH = 1024;\n\nconst V1_TOP_KEYS = ['schemaVersion', 'id', 'name', 'slots', 'options'];\nconst V2_TOP_EXTRA_KEYS = ['version', 'description', 'translations'];\nconst V1_SLOT_KEYS = ['id', 'label', 'role', 'types', 'required'];\nconst V2_SLOT_EXTRA_KEYS = ['description'];\nconst OPTION_BASE_KEYS = ['id', 'label', 'type', 'default'];\nconst OPTION_TYPE_KEYS: Record<string, string[]> = {\n boolean: [],\n number: ['min', 'max'],\n string: ['maxLength'],\n select: ['choices'],\n color: [],\n};\nconst V2_OPTION_EXTRA_KEYS: Record<string, string[]> = {\n boolean: ['group'],\n number: ['group', 'step'],\n string: ['group'],\n select: ['group'],\n color: ['group'],\n};\nconst TRANSLATION_KEYS = ['name', 'description', 'slots', 'options'];\nconst SLOT_TRANSLATION_KEYS = ['label', 'description'];\nconst OPTION_TRANSLATION_KEYS = ['label', 'group', 'choices'];\n\nexport const isRecord = (value: unknown): value is Record<string, unknown> =>\n value !== null &&\n typeof value === 'object' &&\n !Array.isArray(value) &&\n Object.getPrototypeOf(value) === Object.prototype;\n\nexport const isId = (value: unknown): value is string =>\n typeof value === 'string' && ID_PATTERN.test(value) && !FORBIDDEN_IDS.includes(value);\n\nconst isText = (value: unknown, max = MAX_TEXT): value is string =>\n typeof value === 'string' && value.length > 0 && value.length <= max;\n\n/** A translated string may be empty: the plugin renders it as given. */\nconst isTranslationText = (value: unknown): value is string =>\n typeof value === 'string' && value.length <= MAX_TEXT;\n\nconst isFiniteNumber = (value: unknown): value is number =>\n typeof value === 'number' && Number.isFinite(value);\n\nexport const jsonBytes = (value: unknown): number =>\n Buffer.byteLength(JSON.stringify(value), 'utf8');\n\nclass Collector {\n readonly problems: Problem[] = [];\n\n add(code: string, path: string, message: string): void {\n this.problems.push({ code, path, message });\n }\n\n /**\n * Unknown keys report `<scope>.unknownKey`, one per key; a missing required\n * key reports `missingCode(key)`. Returns false when anything was reported,\n * so the caller can stop looking at a shape it cannot trust.\n */\n checkKeys(\n value: Record<string, unknown>,\n path: string,\n scope: string,\n required: readonly string[],\n optional: readonly string[],\n missingCode: (key: string) => string = (key) => `${scope}.${key}`,\n ): boolean {\n const allowed = new Set([...required, ...optional]);\n let ok = true;\n for (const key of Object.keys(value)) {\n if (!allowed.has(key)) {\n this.add(`${scope}.unknownKey`, `${path}/${key}`, `Remove \"${key}\". It is not a known key here.`);\n ok = false;\n }\n }\n for (const key of required) {\n if (!Object.hasOwn(value, key)) {\n this.add(missingCode(key), `${path}/${key}`, `Add the required key \"${key}\".`);\n ok = false;\n }\n }\n return ok;\n }\n}\n\nfunction validateSlot(c: Collector, value: unknown, path: string, v2: boolean): void {\n if (!isRecord(value)) {\n c.add('manifest.slot.invalid', path, 'A slot must be an object.');\n return;\n }\n if (!c.checkKeys(value, path, 'manifest.slot', V1_SLOT_KEYS, v2 ? V2_SLOT_EXTRA_KEYS : [])) return;\n if (!isId(value.id)) {\n c.add('manifest.slot.id', `${path}/id`, 'A slot id is lowercase, starts with a letter, and uses only a-z, 0-9, _ and -, up to 64 characters.');\n }\n if (!isText(value.label)) {\n c.add('manifest.slot.label', `${path}/label`, `A slot label is 1 to ${MAX_TEXT} characters.`);\n }\n const role = value.role;\n if (role !== 'dimension' && role !== 'measure') {\n c.add('manifest.slot.role', `${path}/role`, 'A slot role is \"dimension\" or \"measure\".');\n }\n if (typeof value.required !== 'boolean') {\n c.add('manifest.slot.required', `${path}/required`, 'required is true or false.');\n }\n const types = value.types;\n const typesOk =\n Array.isArray(types) &&\n types.length > 0 &&\n types.length <= 3 &&\n new Set(types).size === types.length &&\n types.every((type) => FIELD_TYPES.includes(type as string));\n if (!typesOk) {\n c.add('manifest.slot.types', `${path}/types`, 'types is a list of 1 to 3 distinct values from \"string\", \"number\" and \"boolean\".');\n } else if (role === 'measure' && (types.length !== 1 || (types as string[])[0] !== 'number')) {\n c.add('manifest.slot.types', `${path}/types`, 'A measure slot accepts only [\"number\"].');\n }\n if (Object.hasOwn(value, 'description') && !isText(value.description)) {\n c.add('manifest.slot.description', `${path}/description`, `A slot description is 1 to ${MAX_TEXT} characters.`);\n }\n}\n\nfunction validateOption(c: Collector, value: unknown, path: string, v2: boolean): void {\n if (!isRecord(value)) {\n c.add('manifest.option.invalid', path, 'An option must be an object.');\n return;\n }\n // Reserved before anything else: the author needs to hear \"reserved\" first.\n const id = value.id;\n if (typeof id === 'string' && (id === 'theme' || id.startsWith('theme.'))) {\n c.add('manifest.option.reserved', `${path}/id`, 'The option id \"theme\" (and any id starting with \"theme.\") is reserved for the host theme.');\n return;\n }\n const type = typeof value.type === 'string' ? value.type : '';\n const typeKeys = Object.hasOwn(OPTION_TYPE_KEYS, type) ? OPTION_TYPE_KEYS[type] : undefined;\n if (!typeKeys || (type === 'color' && !v2)) {\n c.add(\n 'manifest.option.type',\n `${path}/type`,\n v2\n ? 'An option type is one of \"boolean\", \"number\", \"string\", \"select\", \"color\".'\n : 'An option type is one of \"boolean\", \"number\", \"string\", \"select\" (color needs schemaVersion 2).',\n );\n return;\n }\n if (!c.checkKeys(value, path, 'manifest.option', [...OPTION_BASE_KEYS, ...typeKeys], v2 ? V2_OPTION_EXTRA_KEYS[type] : [])) return;\n if (!isId(id)) {\n c.add('manifest.option.id', `${path}/id`, 'An option id is lowercase, starts with a letter, and uses only a-z, 0-9, _ and -, up to 64 characters.');\n }\n if (!isText(value.label)) {\n c.add('manifest.option.label', `${path}/label`, `An option label is 1 to ${MAX_TEXT} characters.`);\n }\n if (Object.hasOwn(value, 'group') && !isText(value.group, MAX_GROUP)) {\n c.add('manifest.option.group', `${path}/group`, `An option group is 1 to ${MAX_GROUP} characters.`);\n }\n switch (type) {\n case 'boolean':\n if (typeof value.default !== 'boolean') {\n c.add('manifest.option.default', `${path}/default`, 'A boolean default is true or false.');\n }\n break;\n case 'number': {\n const rangeOk = isFiniteNumber(value.min) && isFiniteNumber(value.max) && value.min <= value.max;\n if (!rangeOk) {\n c.add('manifest.option.range', `${path}/min`, 'Set finite min and max with min <= max.');\n break;\n }\n const min = value.min as number;\n const max = value.max as number;\n if (!isFiniteNumber(value.default) || value.default < min || value.default > max) {\n c.add('manifest.option.default', `${path}/default`, 'A number default is a finite number between min and max.');\n }\n if (Object.hasOwn(value, 'step') && (!isFiniteNumber(value.step) || value.step <= 0 || value.step > max - min)) {\n c.add('manifest.option.step', `${path}/step`, 'step is a positive number no larger than max - min.');\n }\n break;\n }\n case 'string': {\n const maxLength = value.maxLength;\n if (!isFiniteNumber(maxLength) || !Number.isInteger(maxLength) || maxLength < 1 || maxLength > MAX_STRING_OPTION_LENGTH) {\n c.add('manifest.option.maxLength', `${path}/maxLength`, `A string option needs an integer \"maxLength\" from 1 to ${MAX_STRING_OPTION_LENGTH}.`);\n break;\n }\n if (typeof value.default !== 'string' || value.default.length > maxLength) {\n c.add('manifest.option.default', `${path}/default`, 'A string default is a string no longer than maxLength.');\n }\n break;\n }\n case 'select': {\n const choices = value.choices;\n const choicesOk =\n Array.isArray(choices) &&\n choices.length > 0 &&\n choices.length <= MAX_CHOICES &&\n choices.every(\n (choice) => isRecord(choice) && Object.keys(choice).length === 2 && isId(choice.value) && isText(choice.label),\n ) &&\n new Set(choices.map((choice) => choice.value)).size === choices.length;\n if (!choicesOk) {\n c.add('manifest.option.choices', `${path}/choices`, `choices is a list of 1 to ${MAX_CHOICES} distinct { value, label } pairs; a value follows the id rule.`);\n break;\n }\n if (!choices.some((choice) => choice.value === value.default)) {\n c.add('manifest.option.default', `${path}/default`, 'A select default is the value of one of its choices.');\n }\n break;\n }\n case 'color':\n if (typeof value.default !== 'string' || !COLOR_PATTERN.test(value.default)) {\n c.add('manifest.option.color', `${path}/default`, 'A color default is a 6 digit hex colour like \"#3366cc\".');\n }\n break;\n default:\n break;\n }\n}\n\n/** Runs only on a manifest whose slots and options are already valid. */\nfunction validateTranslations(\n c: Collector,\n value: unknown,\n slots: { id: string }[],\n options: { id: string; type: string; choices?: { value: string }[] }[],\n): void {\n const path = '/translations';\n if (!isRecord(value)) {\n c.add('manifest.translations.invalid', path, 'translations is an object keyed by language code.');\n return;\n }\n for (const [language, entry] of Object.entries(value)) {\n const langPath = `${path}/${language}`;\n if (!LANGUAGE_PATTERN.test(language)) {\n c.add('manifest.translations.language', langPath, `\"${language}\" is not a language code like \"fr\" or \"pt-BR\".`);\n continue;\n }\n if (!isRecord(entry)) {\n c.add('manifest.translations.invalid', langPath, 'A translation is an object with optional name, description, slots and options.');\n continue;\n }\n if (!c.checkKeys(entry, langPath, 'manifest.translations', [], TRANSLATION_KEYS)) continue;\n for (const key of ['name', 'description'] as const) {\n if (Object.hasOwn(entry, key) && !isTranslationText(entry[key])) {\n c.add('manifest.translations.text', `${langPath}/${key}`, `A translated string is at most ${MAX_TEXT} characters.`);\n }\n }\n if (Object.hasOwn(entry, 'slots')) {\n const entrySlots = entry.slots;\n if (!isRecord(entrySlots)) {\n c.add('manifest.translations.invalid', `${langPath}/slots`, 'slots is an object keyed by slot id.');\n } else {\n for (const [slotId, slotEntry] of Object.entries(entrySlots)) {\n const slotPath = `${langPath}/slots/${slotId}`;\n if (!slots.some((slot) => slot.id === slotId)) {\n c.add('manifest.translations.slot', slotPath, `\"${slotId}\" is not a slot id in this manifest.`);\n continue;\n }\n if (!isRecord(slotEntry)) {\n c.add('manifest.translations.invalid', slotPath, 'A slot translation is an object with optional label and description.');\n continue;\n }\n if (!c.checkKeys(slotEntry, slotPath, 'manifest.translations', [], SLOT_TRANSLATION_KEYS)) continue;\n for (const key of ['label', 'description'] as const) {\n if (Object.hasOwn(slotEntry, key) && !isTranslationText(slotEntry[key])) {\n c.add('manifest.translations.text', `${slotPath}/${key}`, `A translated string is at most ${MAX_TEXT} characters.`);\n }\n }\n }\n }\n }\n if (Object.hasOwn(entry, 'options')) {\n const entryOptions = entry.options;\n if (!isRecord(entryOptions)) {\n c.add('manifest.translations.invalid', `${langPath}/options`, 'options is an object keyed by option id.');\n } else {\n for (const [optionId, optionEntry] of Object.entries(entryOptions)) {\n const optionPath = `${langPath}/options/${optionId}`;\n const option = options.find((item) => item.id === optionId);\n if (!option) {\n c.add('manifest.translations.option', optionPath, `\"${optionId}\" is not an option id in this manifest.`);\n continue;\n }\n if (!isRecord(optionEntry)) {\n c.add('manifest.translations.invalid', optionPath, 'An option translation is an object with optional label, group and choices.');\n continue;\n }\n if (!c.checkKeys(optionEntry, optionPath, 'manifest.translations', [], OPTION_TRANSLATION_KEYS)) continue;\n for (const key of ['label', 'group'] as const) {\n if (Object.hasOwn(optionEntry, key) && !isTranslationText(optionEntry[key])) {\n c.add('manifest.translations.text', `${optionPath}/${key}`, `A translated string is at most ${MAX_TEXT} characters.`);\n }\n }\n if (!Object.hasOwn(optionEntry, 'choices')) continue;\n const choices = optionEntry.choices;\n if (!isRecord(choices)) {\n c.add('manifest.translations.invalid', `${optionPath}/choices`, 'choices is an object keyed by choice value.');\n continue;\n }\n for (const [choiceValue, label] of Object.entries(choices)) {\n const choicePath = `${optionPath}/choices/${choiceValue}`;\n if (option.type !== 'select' || !(option.choices ?? []).some((choice) => choice.value === choiceValue)) {\n c.add('manifest.translations.choice', choicePath, `\"${choiceValue}\" is not a choice of option \"${optionId}\".`);\n } else if (!isTranslationText(label)) {\n c.add('manifest.translations.text', choicePath, `A translated string is at most ${MAX_TEXT} characters.`);\n }\n }\n }\n }\n }\n }\n}\n\n/**\n * Validate a manifest value. Returns every problem found, first problem first.\n * An empty array means the manifest is valid.\n */\nexport function validateManifest(value: unknown): Problem[] {\n const c = new Collector();\n if (!isRecord(value)) {\n c.add('manifest.invalid', '', 'manifest.json must be a JSON object.');\n return c.problems;\n }\n\n // 1. schemaVersion\n const schemaVersion = value.schemaVersion;\n if (schemaVersion !== 1 && schemaVersion !== 2) {\n c.add('manifest.schemaVersion.unsupported', '/schemaVersion', 'schemaVersion must be 1 or 2.');\n return c.problems;\n }\n const v2 = schemaVersion === 2;\n\n // 2. unknown and missing top-level keys\n if (!c.checkKeys(value, '', 'manifest', V1_TOP_KEYS, v2 ? V2_TOP_EXTRA_KEYS : [], (key) => `manifest.${key}.required`)) {\n return c.problems;\n }\n\n // 3. id, name and the v2 labels (on v1 the keys were already refused above)\n if (!isId(value.id)) {\n c.add('manifest.id.invalid', '/id', 'id is lowercase, starts with a letter, and uses only a-z, 0-9, _ and -, up to 64 characters. It becomes the chart type \"custom:<id>\".');\n }\n if (!isText(value.name)) {\n c.add('manifest.name.invalid', '/name', `name is 1 to ${MAX_TEXT} characters.`);\n }\n if (Object.hasOwn(value, 'version') && (typeof value.version !== 'string' || !SEMVER_PATTERN.test(value.version))) {\n c.add('manifest.version.invalid', '/version', 'version is a semver string like \"1.2.0\".');\n }\n if (Object.hasOwn(value, 'description') && !isText(value.description, MAX_DESCRIPTION)) {\n c.add('manifest.description.invalid', '/description', `description is 1 to ${MAX_DESCRIPTION} characters.`);\n }\n\n // 4. slots\n const slots = value.slots;\n if (!Array.isArray(slots)) {\n c.add('manifest.slots.invalid', '/slots', 'slots must be an array.');\n } else if (slots.length === 0) {\n c.add('manifest.slots.empty', '/slots', 'Declare at least one slot.');\n } else if (slots.length > MAX_SLOTS) {\n c.add('manifest.slots.tooMany', '/slots', `Declare at most ${MAX_SLOTS} slots.`);\n } else {\n slots.forEach((slot, index) => validateSlot(c, slot, `/slots/${index}`, v2));\n const seen = new Set<string>();\n slots.forEach((slot, index) => {\n if (!isRecord(slot) || typeof slot.id !== 'string') return;\n if (seen.has(slot.id)) {\n c.add('manifest.slot.duplicate', `/slots/${index}/id`, `Slot id \"${slot.id}\" is used more than once.`);\n }\n seen.add(slot.id);\n });\n }\n\n // 5. options\n const options = value.options;\n if (!Array.isArray(options)) {\n c.add('manifest.options.invalid', '/options', 'options must be an array (it may be empty).');\n } else if (options.length > MAX_OPTIONS) {\n c.add('manifest.options.tooMany', '/options', `Declare at most ${MAX_OPTIONS} options.`);\n } else {\n options.forEach((option, index) => validateOption(c, option, `/options/${index}`, v2));\n const seen = new Set<string>();\n options.forEach((option, index) => {\n if (!isRecord(option) || typeof option.id !== 'string') return;\n if (seen.has(option.id)) {\n c.add('manifest.option.duplicate', `/options/${index}/id`, `Option id \"${option.id}\" is used more than once.`);\n }\n seen.add(option.id);\n });\n }\n\n // 6. translations, only on an otherwise clean manifest (v2 only; on v1 the key is unknown)\n if (c.problems.length === 0 && Object.hasOwn(value, 'translations')) {\n validateTranslations(\n c,\n value.translations,\n slots as { id: string }[],\n options as { id: string; type: string; choices?: { value: string }[] }[],\n );\n }\n\n // 7. size, only on an otherwise clean manifest\n if (c.problems.length === 0 && jsonBytes(value) > MAX_MANIFEST_BYTES) {\n c.add('manifest.size', '', `manifest.json must be at most ${MAX_MANIFEST_BYTES} bytes as JSON.`);\n }\n\n return c.problems;\n}\n\n/** Parse a manifest; returns null when validateManifest reports anything. */\nexport function parseManifest(value: unknown): CustomChartManifest | null {\n return validateManifest(value).length === 0 ? (value as CustomChartManifest) : null;\n}\n\n/** Zod view of manifest v2 for tool input schemas. Shape only: validateManifest owns the rules. */\nconst slotSchema = z.object({\n id: z.string(),\n label: z.string(),\n role: z.enum(['dimension', 'measure']),\n types: z.array(z.enum(['string', 'number', 'boolean'])),\n required: z.boolean(),\n description: z.string().optional(),\n});\n\nconst optionSchema = z.object({\n id: z.string(),\n label: z.string(),\n type: z.enum(['boolean', 'number', 'string', 'select', 'color']),\n default: z.union([z.string(), z.number(), z.boolean()]),\n min: z.number().optional(),\n max: z.number().optional(),\n step: z.number().optional(),\n maxLength: z.number().optional(),\n choices: z.array(z.object({ value: z.string(), label: z.string() })).optional(),\n group: z.string().optional(),\n});\n\nexport const manifestSchema = z.object({\n schemaVersion: z.union([z.literal(1), z.literal(2)]),\n id: z.string(),\n name: z.string(),\n version: z.string().optional(),\n description: z.string().optional(),\n slots: z.array(slotSchema),\n options: z.array(optionSchema),\n translations: z.record(z.unknown()).optional(),\n});\n\nexport type ManifestSlotInput = z.infer<typeof slotSchema>;\nexport type ManifestOptionInput = z.infer<typeof optionSchema>;\nexport { slotSchema as manifestSlotSchema, optionSchema as manifestOptionSchema };\n\n/** Deep key sort used by the content hash (brief section 5). Arrays keep their order. */\nexport function sortKeys<T>(value: T): T {\n if (Array.isArray(value)) return value.map((item) => sortKeys(item)) as T;\n if (isRecord(value)) {\n const out: Record<string, unknown> = {};\n for (const key of Object.keys(value).sort()) out[key] = sortKeys(value[key]);\n return out as T;\n }\n return value;\n}\n","import { strFromU8, strToU8, unzipSync, zipSync } from 'fflate';\n\n/**\n * dist/bundle.zip is for humans (drag and drop upload in the app). The API\n * takes JSON. The zip holds exactly the four build outputs at its root.\n */\nexport const BUNDLE_ZIP_FILES = ['index.js', 'manifest.json', 'icon.svg', 'index.css'] as const;\nexport type BundleZipFile = (typeof BUNDLE_ZIP_FILES)[number];\n\nexport type BundleZipInput = Partial<Record<BundleZipFile, string>> &\n Pick<Record<BundleZipFile, string>, 'index.js' | 'manifest.json' | 'icon.svg'>;\n\nexport function writeBundleZip(files: BundleZipInput): Uint8Array {\n const entries: Record<string, Uint8Array> = {};\n for (const name of BUNDLE_ZIP_FILES) {\n const content = files[name];\n if (typeof content === 'string') entries[name] = strToU8(content);\n }\n // Fixed mtime keeps the zip bytes stable for the same inputs.\n return zipSync(entries, { level: 6, mtime: new Date('2000-01-01T00:00:00Z') });\n}\n\nexport function readBundleZip(bytes: Uint8Array): Partial<Record<BundleZipFile, string>> {\n const unzipped = unzipSync(bytes, {\n filter: (file) => (BUNDLE_ZIP_FILES as readonly string[]).includes(file.name),\n });\n const files: Partial<Record<BundleZipFile, string>> = {};\n for (const name of BUNDLE_ZIP_FILES) {\n if (unzipped[name]) files[name] = strFromU8(unzipped[name]);\n }\n return files;\n}\n"],"mappings":";;;AAAA,SAAS,kBAAkB;AAC3B,SAAS,cAAAA,aAAY,WAAW,gBAAAC,eAAc,qBAAqB;AACnE,SAAS,QAAAC,OAAM,WAAAC,UAAS,WAAW;AACnC,SAAS,SAAS,eAA4B;;;ACH9C,SAAS,iBAAiB;AAC1B,SAAS,YAAY,aAAa,cAAc,gBAAgB;AAChE,SAAS,SAAS,MAAM,eAAe;AACvC,SAAS,qBAAqB;AAS9B,IAAI;AAEG,SAAS,sBAA8B;AAC5C,MAAI,UAAW,QAAO;AACtB,QAAM,OAAO,QAAQ,cAAc,YAAY,GAAG,CAAC;AACnD,QAAM,aAAa;AAAA,IACjB;AAAA,IACA,KAAK,MAAM,cAAc;AAAA,IACzB,KAAK,MAAM,MAAM,cAAc;AAAA,IAC/B,KAAK,MAAM,MAAM,MAAM,cAAc;AAAA,IACrC,KAAK,MAAM,MAAM,OAAO,cAAc;AAAA,EACxC;AACA,aAAW,aAAa,YAAY;AAClC,QAAI,WAAW,KAAK,WAAW,iBAAiB,CAAC,KAAK,WAAW,KAAK,WAAW,YAAY,eAAe,CAAC,GAAG;AAC9G,kBAAY,QAAQ,SAAS;AAC7B,aAAO;AAAA,IACT;AAAA,EACF;AACA,QAAM,IAAI;AAAA,IACR,sCAAsC,IAAI;AAAA,EAC5C;AACF;AAEO,SAAS,kBAA0B;AACxC,SAAO,aAAa,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AA,EACA;AACF,CAAC;AAED,IAAM,qBAA0C,oBAAI,IAAI;AAAA,EACtD;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA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MAAG,SAAS,EAAE,GAAG,QAAQ,aAAa,YAAY,MAAM,GAAG,WAAW,gBAAgB,MAAM,EAAE;AAAA,IAClI;AAAA,EACF,SAAS,OAAO;AACd,WAAO,EAAE,IAAI,OAAO,UAAU,CAAC,EAAE,MAAM,uBAAuB,MAAM,aAAa,SAAU,MAAgB,QAAQ,CAAC,GAAG,SAAS,CAAC,EAAE;AAAA,EACrI;AACF;","names":["existsSync","readFileSync","join","resolve","resolve","join","existsSync","readFileSync"]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/hosted/types.ts","../src/hosted/auth.ts"],"sourcesContent":["import type { RequestOptions } from '../client/types.js';\n\nexport const HOSTED_SCOPES = [\n 'mcp:dashboards:read', 'mcp:dashboards:write',\n 'mcp:datamarts:read', 'mcp:datamarts:write', 'mcp:embeds:write', 'mcp:query',\n 'mcp:custom-charts:read', 'mcp:custom-charts:write',\n] as const;\nexport type HostedScope = typeof HOSTED_SCOPES[number];\n\n/** A workspace this sign-in may act in, as the issuer resolved it for this request. */\nexport interface GrantedWorkspace {\n id: string;\n name?: string;\n}\n\n/** What the user approved at consent: a list of workspaces, or every workspace they can access. */\nexport type GrantWorkspaces = { mode: 'all' } | { mode: 'list'; ids: string[] };\n\n/** Only the trusted auth adapter may produce this context. Never populate it from MCP input. */\nexport interface HostedPrincipal {\n actorKind: 'user';\n credentialType: 'oauth' | 'pat';\n userId: string;\n companyId: string;\n /** Set only when exactly one workspace is granted: the default for every call. */\n workspaceId?: string;\n workspaceName?: string;\n /** The grant as approved. Absent from issuers that grant one workspace only. */\n workspaces?: GrantWorkspaces;\n /**\n * Live: the granted workspaces the user can still access, read on every request.\n * Absent only on a principal built before multi-workspace grants (then `workspaceId`).\n */\n grantedWorkspaces?: GrantedWorkspace[];\n /** OAuth application ID, NOT the tenant identifier called clientId by legacy tools. */\n clientId: string;\n resource: string;\n scopes: HostedScope[];\n /** Required for stored PATs. OAuth may delegate expiry to fresh provider validation. */\n expiresAt?: number;\n /** Explicit tenant allow-list resolved by the backend; absent means no tenant access. */\n tenantIds?: string[];\n}\n\nexport interface PatRecord {\n hash: string;\n revokedAt: number | null;\n principal: HostedPrincipal;\n}\n\nexport interface PatStore {\n findByHash(hash: string): Promise<PatRecord | undefined>;\n}\n\n/**\n * Opaque per-request handle from `HostedAdapter.delegate`. It carries nothing a tool,\n * log or serializer can read; only the adapter that made it can act with it.\n */\nexport interface HostedDelegation {\n readonly kind: 'hosted-delegation';\n}\n\n/** Production integration must recheck membership, policy and revocation on every call. */\nexport interface HostedAdapter {\n verifyOAuth(token: string, resource: string): Promise<HostedPrincipal>;\n resolvePatGrant(principal: HostedPrincipal): Promise<HostedPrincipal>;\n /**\n * Optional. Bind this request's verified OAuth bearer to an opaque handle, so\n * executeRequest can act as this user at the configured issuer, which re-verifies\n * the token and enforces workspace, object and role checks on every call.\n */\n delegate?(token: string, principal: HostedPrincipal): HostedDelegation;\n /**\n * Execute as this user/workspace with backend resource and tenant checks and auditing.\n * Receives NO incoming bearer or server service credential: only the opaque handle.\n * Do not impersonate a static service token.\n * Return the raw API result: the shared client performs its usual data unwrapping.\n * `workspace` is the granted workspace this tool call acts in, when one is known; the\n * adapter names it to the issuer and nowhere else.\n */\n executeRequest(options: RequestOptions, principal: HostedPrincipal,\n delegation?: HostedDelegation, workspace?: GrantedWorkspace): Promise<unknown>;\n}\n\nexport interface HostedConfig {\n publicUrl: string;\n issuer: string;\n /** Where the issuer's API is reached, when not at the issuer origin itself. */\n apiUrl?: string;\n port: number;\n bind: string;\n testOnly: boolean;\n}\n\n/** The backend's own status, code and message for a 4xx that is not an auth denial. */\nexport interface HostedErrorDetail { status: number; code?: string; message?: string }\n\nexport class HostedAuthError extends Error {\n constructor(public readonly status: 400 | 401 | 403 | 404 | 503, public readonly code: string,\n public readonly detail?: HostedErrorDetail) {\n super(code);\n }\n}\n","import { createHash, timingSafeEqual } from 'node:crypto';\nimport { open } from 'node:fs/promises';\nimport { constants } from 'node:fs';\nimport { z } from 'zod';\nimport { HOSTED_SCOPES, HostedAuthError } from './types.js';\nimport type { HostedAdapter, HostedPrincipal, PatRecord, PatStore } from './types.js';\n\nconst identity = z.string().min(1).max(512).regex(/^[^\\x00-\\x1f\\x7f]+$/);\nconst workspaceSchema = z.object({ id: identity, name: identity.optional() }).strip();\nexport const principalSchema = z.object({\n actorKind: z.literal('user'), credentialType: z.enum(['oauth', 'pat']),\n userId: identity, companyId: identity, workspaceId: identity.optional(), workspaceName: identity.optional(),\n workspaces: z.union([\n z.object({ mode: z.literal('all') }).strip(),\n z.object({ mode: z.literal('list'), ids: z.array(identity).min(1).max(100) }).strip(),\n ]).optional(),\n grantedWorkspaces: z.array(workspaceSchema).max(1000).optional(),\n // The issuer's own pick; recomputed below from grantedWorkspaces, never trusted on its own.\n defaultWorkspace: workspaceSchema.optional(),\n clientId: identity, resource: z.string().url(), scopes: z.array(z.enum(HOSTED_SCOPES)),\n expiresAt: z.number().finite().positive().optional(), tenantIds: z.array(identity).optional(),\n}).strict();\n\n/**\n * One shape for every issuer: the live granted list, and `workspaceId`/`workspaceName`\n * only when exactly one workspace is granted (the default for each call). An issuer\n * that grants one workspace sends only `workspaceId`; read that as a one-item list.\n * No workspace left, duplicate ids, or a `workspaceId` outside the list is refused.\n */\nfunction normalizeWorkspaces(value: z.infer<typeof principalSchema>): HostedPrincipal {\n const { defaultWorkspace: _default, workspaceId, workspaceName, grantedWorkspaces, ...rest } = value;\n const granted = grantedWorkspaces\n ?? (workspaceId ? [workspaceName === undefined ? { id: workspaceId } : { id: workspaceId, name: workspaceName }] : []);\n if (!granted.length || new Set(granted.map(item => item.id)).size !== granted.length ||\n (workspaceId !== undefined && !granted.some(item => item.id === workspaceId))) {\n throw new HostedAuthError(401, 'invalid_token');\n }\n const only = granted.length === 1 ? granted[0] : undefined;\n return {\n ...rest, grantedWorkspaces: granted,\n ...(only ? { workspaceId: only.id } : {}), ...(only?.name !== undefined ? { workspaceName: only.name } : {}),\n };\n}\nconst recordSchema = z.object({\n hash: z.string().regex(/^[a-f0-9]{64}$/), revokedAt: z.number().finite().nullable(),\n principal: principalSchema,\n}).strict();\n\nexport function hashCredential(token: string): string {\n return createHash('sha256').update(token).digest('hex');\n}\n\n/** Reread on every request: an atomic file replacement revokes immediately, without restart. */\nexport class FilePatStore implements PatStore {\n constructor(private readonly path: string) {}\n async findByHash(hash: string): Promise<PatRecord | undefined> {\n const handle = await open(this.path, constants.O_RDONLY | constants.O_NOFOLLOW);\n try {\n const stat = await handle.stat();\n if (!stat.isFile() || (stat.mode & 0o077) !== 0 || stat.size > 1_048_576 ||\n (process.getuid && stat.uid !== process.getuid())) throw new Error('Invalid credential store');\n const records = z.array(recordSchema).max(1000).parse(JSON.parse(await handle.readFile('utf8')));\n const matches = records.filter(record => timingSafeEqual(Buffer.from(record.hash, 'hex'), Buffer.from(hash, 'hex')));\n // Duplicate records are ambiguous, including an active/revoked pair.\n if (matches.length > 1) throw new Error('Ambiguous credential store');\n return matches[0];\n } finally { await handle.close(); }\n }\n}\n\nfunction validatePrincipal(value: unknown, resource: string, kind: 'oauth' | 'pat'): HostedPrincipal {\n const parsed = principalSchema.safeParse(value);\n if (!parsed.success || parsed.data.resource !== resource || parsed.data.credentialType !== kind ||\n (parsed.data.expiresAt !== undefined && parsed.data.expiresAt <= Date.now() / 1000) ||\n (kind === 'pat' && parsed.success && parsed.data.expiresAt === undefined)) throw new HostedAuthError(401, 'invalid_token');\n return normalizeWorkspaces(parsed.data);\n}\n\nexport function createBearerVerifier(adapter: HostedAdapter, resource: string, pats?: PatStore) {\n return async (token: string): Promise<HostedPrincipal> => {\n try {\n if (!/^[A-Za-z0-9._~+\\/-]+=*$/.test(token) || token.length > 8192) throw new HostedAuthError(401, 'invalid_token');\n if (!token.startsWith('dbp_')) {\n return validatePrincipal(await adapter.verifyOAuth(token, resource), resource, 'oauth');\n }\n if (!/^dbp_[A-Za-z0-9_-]{32,}$/.test(token)) throw new HostedAuthError(401, 'invalid_token');\n const record = await pats?.findByHash(hashCredential(token));\n if (!record || record.revokedAt !== null || record.hash !== hashCredential(token)) throw new HostedAuthError(401, 'invalid_token');\n const stored = validatePrincipal(record.principal, resource, 'pat');\n const resolved = validatePrincipal(await adapter.resolvePatGrant(structuredClone(stored)), resource, 'pat');\n // Membership resolution may narrow a stored grant, never move or expand it.\n for (const key of ['userId', 'companyId', 'clientId', 'resource'] as const) {\n if (resolved[key] !== stored[key]) throw new HostedAuthError(401, 'invalid_token');\n }\n if (resolved.grantedWorkspaces!.some(item => !stored.grantedWorkspaces!.some(old => old.id === item.id))) {\n throw new HostedAuthError(401, 'invalid_token');\n }\n if (resolved.expiresAt! > stored.expiresAt! || resolved.scopes.some(scope => !stored.scopes.includes(scope)) ||\n resolved.tenantIds?.some(id => !stored.tenantIds?.includes(id))) throw new HostedAuthError(401, 'invalid_token');\n return resolved;\n } catch (error) {\n if (error instanceof HostedAuthError) throw error;\n // No raw verifier, filesystem or remote errors can disclose credential values.\n throw new HostedAuthError(401, 'invalid_token');\n }\n };\n}\n"],"mappings":";;;AAEO,IAAM,gBAAgB;AAAA,EAC3B;AAAA,EAAuB;AAAA,EACvB;AAAA,EAAsB;AAAA,EAAuB;AAAA,EAAoB;AAAA,EACjE;AAAA,EAA0B;AAC5B;AA2FO,IAAM,kBAAN,cAA8B,MAAM;AAAA,EACzC,YAA4B,QAAqD,MAC/D,QAA4B;AAC5C,UAAM,IAAI;AAFgB;AAAqD;AAC/D;AAAA,EAElB;AACF;;;ACtGA,SAAS,YAAY,uBAAuB;AAC5C,SAAS,YAAY;AACrB,SAAS,iBAAiB;AAC1B,SAAS,SAAS;AAIlB,IAAM,WAAW,EAAE,OAAO,EAAE,IAAI,CAAC,EAAE,IAAI,GAAG,EAAE,MAAM,qBAAqB;AACvE,IAAM,kBAAkB,EAAE,OAAO,EAAE,IAAI,UAAU,MAAM,SAAS,SAAS,EAAE,CAAC,EAAE,MAAM;AAC7E,IAAM,kBAAkB,EAAE,OAAO;AAAA,EACtC,W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OAAM,IAAI,gBAAgB,KAAK,eAAe;AAAA,MACnF;AACA,UAAI,SAAS,kBAAmB,KAAK,UAAQ,CAAC,OAAO,kBAAmB,KAAK,SAAO,IAAI,OAAO,KAAK,EAAE,CAAC,GAAG;AACxG,cAAM,IAAI,gBAAgB,KAAK,eAAe;AAAA,MAChD;AACA,UAAI,SAAS,YAAa,OAAO,aAAc,SAAS,OAAO,KAAK,WAAS,CAAC,OAAO,OAAO,SAAS,KAAK,CAAC,KACvG,SAAS,WAAW,KAAK,QAAM,CAAC,OAAO,WAAW,SAAS,EAAE,CAAC,EAAG,OAAM,IAAI,gBAAgB,KAAK,eAAe;AACnH,aAAO;AAAA,IACT,SAAS,OAAO;AACd,UAAI,iBAAiB,gBAAiB,OAAM;AAE5C,YAAM,IAAI,gBAAgB,KAAK,eAAe;AAAA,IAChD;AAAA,EACF;AACF;","names":[]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/hosted/main.ts","../src/hosted/config.ts","../src/hosted/http.ts","../src/hosted/server.ts","../src/tools/infrastructure/create-workspace.ts","../src/hosted/authorization.ts","../src/hosted/provider.ts"],"sourcesContent":["import { isAbsolute } from 'node:path';\nimport { pathToFileURL } from 'node:url';\nimport { loadHostedConfig } from './config.js';\nimport { FilePatStore } from './auth.js';\nimport { createHostedHttpServer } from './http.js';\nimport type { HostedAdapter } from './types.js';\nimport { createProviderAdapter } from './provider.js';\n\nexport async function startHosted(): Promise<void> {\n const config = loadHostedConfig();\n let adapter: HostedAdapter;\n if (config.testOnly) {\n const file = process.env.DATABRAIN_HOSTED_TEST_FIXTURES;\n if (!file || !isAbsolute(file)) throw new Error('Hosted test mode requires an absolute fixture store path.');\n const { createTestOnlyAdapter } = await import('./test-only.js');\n adapter = createTestOnlyAdapter(file);\n } else {\n const file = process.env.DATABRAIN_HOSTED_ADAPTER_MODULE;\n if (!file) {\n adapter = createProviderAdapter(config);\n process.stderr.write('[databrain] OAuth configured: data tools run as the signed-in user through the issuer; PAT membership requires a trusted adapter.\\n');\n } else {\n if (!isAbsolute(file)) throw new Error('DATABRAIN_HOSTED_ADAPTER_MODULE must be an absolute path.');\n try { adapter = (await import(pathToFileURL(file).href)).adapter; }\n catch { throw new Error('Could not load the trusted hosted adapter.'); }\n }\n if (!adapter || ['verifyOAuth', 'resolvePatGrant', 'executeRequest'].some(key =>\n typeof adapter[key as keyof HostedAdapter] !== 'function')) throw new Error('Invalid trusted hosted adapter.');\n }\n const patPath = process.env.DATABRAIN_HOSTED_PAT_STORE;\n if (patPath && !isAbsolute(patPath)) throw new Error('Hosted PAT store must be an absolute path.');\n const server = createHostedHttpServer(config, adapter, patPath ? new FilePatStore(patPath) : undefined);\n await new Promise<void>((resolve, reject) => {\n server.once('error', reject);\n server.listen(config.port, config.bind, resolve);\n });\n process.stderr.write(`[databrain] hosted HTTP ready on ${config.bind}:${config.port}/mcp${config.testOnly ? ' — TEST ONLY: synthetic identity and data; no OAuth consent' : ''}\\n`);\n const shutdown = () => {\n server.close(() => process.exit(0));\n setTimeout(() => { server.closeAllConnections(); process.exit(0); }, 5000).unref();\n };\n process.once('SIGINT', shutdown);\n process.once('SIGTERM', shutdown);\n}\n","import type { HostedConfig } from './types.js';\n\nexport function loadHostedConfig(env: NodeJS.ProcessEnv = process.env): HostedConfig {\n const testOnly = env.DATABRAIN_HOSTED_TEST_ONLY === '1';\n if (testOnly && env.NODE_ENV === 'production') throw new Error('Hosted test mode is forbidden in production.');\n if (env.DATABRAIN_HOSTED_TEST_ONLY && !testOnly) throw new Error('Invalid DATABRAIN_HOSTED_TEST_ONLY.');\n const validUrl = (value: string | undefined, field: string) => {\n try {\n if (!value) throw new Error();\n const url = new URL(value);\n if (url.username || url.password || url.search || url.hash || url.protocol !== 'https:') throw new Error();\n return url;\n } catch { throw new Error(`Configure ${field} as an HTTPS URL without credentials, query or fragment.`); }\n };\n // Self-hosted installs set no new variable: with neither URL given, both come from the\n // HOSTED_APP_URL origin the image already has (app, API and /mcp on one origin). HTTPS only.\n let publicValue = env.DATABRAIN_HOSTED_PUBLIC_URL;\n let issuerValue = env.DATABRAIN_HOSTED_ISSUER;\n if (!publicValue && !issuerValue && env.HOSTED_APP_URL) {\n const origin = validUrl(env.HOSTED_APP_URL, 'HOSTED_APP_URL').origin;\n publicValue = `${origin}/mcp`;\n issuerValue = origin;\n }\n const publicUrl = validUrl(publicValue, 'DATABRAIN_HOSTED_PUBLIC_URL');\n validUrl(issuerValue, 'DATABRAIN_HOSTED_ISSUER');\n if (publicUrl.pathname !== '/mcp') throw new Error('DATABRAIN_HOSTED_PUBLIC_URL must end in /mcp.');\n const port = Number(env.DATABRAIN_HOSTED_PORT || '3144');\n // Loopback by default so a laptop run is not silently on the network. A\n // container has to be told, because binding loopback inside a task means the\n // load balancer health check can never reach it and the service never\n // stabilises. Set DATABRAIN_HOSTED_BIND=0.0.0.0 in ECS.\n const bind = env.DATABRAIN_HOSTED_BIND || '127.0.0.1';\n if (!Number.isInteger(port) || port < 1024 || port > 65535) throw new Error('Invalid DATABRAIN_HOSTED_PORT.');\n // Optional private address of the same API (for example ECS service discovery), so\n // the token check and data calls stay inside the network. The issuer is still the\n // identity the principal must name; this only changes where the requests go.\n let apiUrl: string | undefined;\n if (env.DATABRAIN_HOSTED_API_URL) {\n let url: URL | undefined;\n try { url = new URL(env.DATABRAIN_HOSTED_API_URL); } catch { url = undefined; }\n if (!url || !['http:', 'https:'].includes(url.protocol) || url.username || url.password || url.search\n || url.hash || url.pathname !== '/') {\n throw new Error('Configure DATABRAIN_HOSTED_API_URL as an origin without credentials, path, query or fragment.');\n }\n apiUrl = url.origin;\n }\n return { publicUrl: publicValue!, issuer: issuerValue!, ...(apiUrl ? { apiUrl } : {}), port,\n bind, testOnly };\n}\n","import { createServer, type IncomingMessage, type ServerResponse } from 'node:http';\nimport { StreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/streamableHttp.js';\nimport { createHostedToolServer, scopeError, workspaceError } from './server.js';\nimport { createBearerVerifier } from './auth.js';\nimport { WorkspaceSelectionError, authorizeTool, assertToolArguments, requiredScopes } from './authorization.js';\nimport { HOSTED_SCOPES, HostedAuthError } from './types.js';\nimport type { HostedAdapter, HostedConfig, PatStore } from './types.js';\n\nconst HEALTH_BODY = JSON.stringify({ status: 'ok' });\n\n/** Exactly `/health`, with or without a query string; `/health/x` is not health. */\nfunction healthPath(url: string | undefined): boolean {\n const path = (url || '/').split('?')[0];\n return path === '/health';\n}\n\nfunction reply(res: ServerResponse, status: number, body: unknown): void {\n res.writeHead(status, { 'Content-Type': 'application/json', 'Cache-Control': 'no-store' });\n res.end(JSON.stringify(body));\n}\n\nasync function readBody(req: IncomingMessage): Promise<unknown> {\n let size = 0;\n const chunks: Buffer[] = [];\n for await (const chunk of req) {\n size += chunk.length;\n if (size > 1_048_576) throw new Error('body_too_large');\n chunks.push(Buffer.from(chunk));\n }\n return JSON.parse(Buffer.concat(chunks).toString('utf8'));\n}\n\nexport function createHostedHttpServer(config: HostedConfig, adapter: HostedAdapter, pats?: PatStore) {\n const publicUrl = new URL(config.publicUrl);\n const metadataPath = `/.well-known/oauth-protected-resource${publicUrl.pathname === '/' ? '' : publicUrl.pathname}`;\n const metadataUrl = `${publicUrl.origin}${metadataPath}`;\n const verify = createBearerVerifier(adapter, config.publicUrl, pats);\n let active = 0;\n\n const server = createServer(async (req, res) => {\n // Liveness for the load balancer. It arrives with the target IP:port as Host,\n // so it runs before the Host/Origin checks and sets no CORS headers. It is\n // unauthenticated, reads no body and never echoes configuration.\n if (healthPath(req.url)) {\n if (req.method !== 'GET' && req.method !== 'HEAD') {\n res.writeHead(405, { Allow: 'GET, HEAD', 'Cache-Control': 'no-store' }); res.end(); return;\n }\n res.writeHead(200, { 'Content-Type': 'application/json', 'Cache-Control': 'no-store',\n 'Content-Length': Buffer.byteLength(HEALTH_BODY) });\n res.end(req.method === 'HEAD' ? undefined : HEALTH_BODY); return;\n }\n res.setHeader('X-Content-Type-Options', 'nosniff');\n if (config.testOnly) res.setHeader('X-Databrain-Test-Only', 'true');\n try {\n // Tailscale Serve may preserve the public Host or use the loopback destination.\n // Never use Host/Forwarded headers to construct discovery URLs or trust an identity.\n const localPort = (server.address() as { port: number } | null)?.port;\n if (![publicUrl.host, `127.0.0.1:${localPort}`, `localhost:${localPort}`].includes(req.headers.host || '')) {\n reply(res, 403, { error: 'invalid_host' }); return;\n }\n if (req.headers.origin && req.headers.origin !== publicUrl.origin) {\n reply(res, 403, { error: 'invalid_origin' }); return;\n }\n if (req.headers.origin) {\n res.setHeader('Access-Control-Allow-Origin', publicUrl.origin);\n res.setHeader('Vary', 'Origin');\n res.setHeader('Access-Control-Expose-Headers', 'WWW-Authenticate, MCP-Protocol-Version');\n }\n const url = new URL(req.url || '/', publicUrl.origin);\n if (req.method === 'GET' && [metadataPath, '/.well-known/oauth-protected-resource'].includes(url.pathname)) {\n reply(res, 200, {\n resource: config.publicUrl, authorization_servers: [config.issuer],\n scopes_supported: HOSTED_SCOPES, bearer_methods_supported: ['header'],\n resource_name: config.testOnly ? 'Databrain (TEST ONLY; fixture identity)' : 'Databrain',\n }); return;\n }\n if (url.pathname !== publicUrl.pathname) { reply(res, 404, { error: 'not_found' }); return; }\n if (url.search) { reply(res, 400, { error: 'query_parameters_not_supported' }); return; }\n if (req.method === 'OPTIONS') {\n res.setHeader('Access-Control-Allow-Methods', 'POST, GET, DELETE, OPTIONS');\n res.setHeader('Access-Control-Allow-Headers', 'Authorization, Content-Type, MCP-Protocol-Version, MCP-Session-Id');\n res.writeHead(204); res.end(); return;\n }\n // Duplicate Authorization fields and cookies never select a principal.\n const authFields = req.rawHeaders.filter((_, i) => i % 2 === 0 && req.rawHeaders[i].toLowerCase() === 'authorization');\n const match = /^Bearer ([^\\s]+)$/i.exec(req.headers.authorization || '');\n if (authFields.length !== 1 || !match) throw new HostedAuthError(401, 'invalid_token');\n const principal = await verify(match[1]);\n if (req.method !== 'POST') {\n res.setHeader('Allow', 'POST, OPTIONS');\n reply(res, 405, { error: 'method_not_allowed' }); return;\n }\n if (!req.headers['content-type']?.toLowerCase().startsWith('application/json')) {\n reply(res, 415, { error: 'unsupported_media_type' }); return;\n }\n if (active >= 32) { reply(res, 503, { error: 'server_busy' }); return; }\n active++;\n let body: any;\n try { body = await readBody(req); }\n catch { active--; reply(res, 400, { error: 'invalid_request' }); return; }\n try {\n // The installed SDK accepts batches; this endpoint does not. A batch must\n // never skip the per-call HTTP authorization/challenge path below.\n if (Array.isArray(body)) { reply(res, 400, { error: 'batch_not_supported' }); return; }\n // Only an OAuth bearer delegates; a PAT never leaves this process.\n const delegation = match[1].startsWith('dbp_') ? undefined : adapter.delegate?.(match[1], principal);\n if (body?.method === 'tools/call' && typeof body.params?.name === 'string') {\n try {\n authorizeTool(body.params.name, principal);\n assertToolArguments(body.params.name, body.params.arguments, principal, delegation !== undefined);\n } catch (error) {\n // A workspace the grant does not hold, or two that disagree: answer the call as a\n // tool error with the granted names, which an AI can act on. No tool runs.\n if (error instanceof WorkspaceSelectionError) {\n reply(res, 200, { jsonrpc: '2.0', id: body.id ?? null, result: workspaceError(error) }); return;\n }\n // A scope the grant lacks: consent already trimmed the scopes to the role, so a\n // step-up challenge (403 insufficient_scope) cannot get more. It made Claude Code\n // drop the whole connection to \"needs authentication\". Answer the call instead.\n if (error instanceof HostedAuthError && error.code === 'insufficient_scope') {\n const missing = requiredScopes(body.params.name).filter(scope => !principal.scopes.includes(scope));\n reply(res, 200, { jsonrpc: '2.0', id: body.id ?? null, result: scopeError(missing) }); return;\n }\n throw error;\n }\n }\n const mcp = createHostedToolServer(principal, adapter, delegation);\n const transport = new StreamableHTTPServerTransport({ sessionIdGenerator: undefined, enableJsonResponse: true });\n let closed = false;\n const close = () => { if (!closed) { closed = true; void mcp.close(); } };\n res.once('close', close);\n try {\n await mcp.connect(transport);\n await transport.handleRequest(req, res, body);\n } finally { if (res.writableEnded) close(); }\n } finally { active--; }\n } catch (error) {\n if (res.headersSent) { res.end(); return; }\n const status = error instanceof HostedAuthError ? error.status : 503;\n if (status === 401) res.setHeader('WWW-Authenticate', `Bearer error=\"invalid_token\", resource_metadata=\"${metadataUrl}\"`);\n reply(res, status, { error: error instanceof HostedAuthError ? error.code : 'temporarily_unavailable' });\n }\n });\n server.requestTimeout = 30_000;\n server.headersTimeout = 10_000;\n server.keepAliveTimeout = 5_000;\n return server;\n}\n","import { AsyncLocalStorage } from 'node:async_hooks';\nimport { z } from 'zod';\nimport { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';\nimport type { CallToolResult } from '@modelcontextprotocol/sdk/types.js';\nimport { instrument } from '@posthog/mcp';\nimport { PostHog } from 'posthog-node';\nimport { DatabrainClient } from '../client/databrain-client.js';\nimport type { RequestOptions } from '../client/types.js';\nimport { registerAllTools } from '../tools/registry.js';\nimport { registerCreateWorkspace } from '../tools/infrastructure/create-workspace.js';\nimport { PREVIEW_PATH } from '../apps/preview.js';\nimport { enableChartApps } from '../apps/feature.js';\nimport { registerChartApps } from '../apps/registry.js';\nimport { registerAllResources } from '../resources/registry.js';\nimport { registerAllPrompts } from '../prompts/registry.js';\nimport { MCP_SERVER_VERSION } from '../version.js';\nimport { DatabrainApiError, toolError, toolSuccess } from '../utils/errors.js';\nimport { toolContext } from '../utils/tool-context.js';\nimport {\n WORKSPACE_FREE_TOOLS, WORKSPACE_INPUT, WORKSPACE_SPANNING_TOOLS, WorkspaceSelectionError, assertBoundArguments,\n assertToolArguments, authorizeTool, grantedNames, grantedWorkspaces, namesWorkspace, selectWorkspace,\n} from './authorization.js';\nimport { HostedAuthError } from './types.js';\nimport type { GrantedWorkspace, HostedAdapter, HostedDelegation, HostedPrincipal } from './types.js';\n\n// Stdio's instrumentation changes the wire schema (required `context`). Use that\n// same implementation, with capture disabled until hosted telemetry has an owner.\n// No bearer, request headers, tool arguments or results are sent to analytics.\nconst schemaInstrumentation = new PostHog('hosted-contract-only', { disabled: true });\n\n/** The granted workspace the running tool call acts in. Set by the tool wrapper only. */\nconst callWorkspace = new AsyncLocalStorage<{ target?: GrantedWorkspace }>();\n\nconst WORKSPACE_HINT = 'Call whoami to see the workspaces this sign-in granted, then name one.';\n\n/** A workspace selection problem, as the tool result the AI reads: reason, code, next step. */\nexport function workspaceError(error: WorkspaceSelectionError) {\n return toolError(`Error: ${error.reason} (${error.code})\\n\\nHint: ${WORKSPACE_HINT}`);\n}\n\n/** A tool this sign-in's scopes do not cover, as a tool result: the connection stays up. */\nexport function scopeError(missing: readonly string[]) {\n return toolError(`Error: This connection cannot do this. It needs ${missing.join(', ')}, which your role did not allow ` +\n 'when you connected. (INSUFFICIENT_SCOPE)\\n\\nHint: Ask a Databrain admin for a role that allows it, then connect ' +\n 'again. If your role allows it in a workspace you did not choose, connect again and choose that workspace.');\n}\n\n/** The workspace list answer (bare or under `data`) cut to one workspace. */\nfunction onlyWorkspace(raw: unknown, target: GrantedWorkspace): unknown {\n const keep = (items: unknown) => Array.isArray(items) ? items.filter(item => {\n const record = item && typeof item === 'object' ? item as Record<string, unknown> : {};\n return record.id === target.id || record._id === target.id ||\n (record.id === undefined && record._id === undefined && target.name !== undefined && record.name === target.name);\n }) : items;\n if (Array.isArray(raw)) return keep(raw);\n if (raw && typeof raw === 'object' && 'data' in raw) return { ...raw, data: keep((raw as { data: unknown }).data) };\n return raw;\n}\n\n/** A request-local client: shared APIs/tools run unchanged, through a delegated executor. */\nclass HostedClient extends DatabrainClient {\n constructor(private readonly adapter: HostedAdapter, private readonly principal: HostedPrincipal,\n private readonly delegation?: HostedDelegation) {\n super({ apiUrl: 'https://api.usedatabrain.com', demoDomain: 'https://demo.usedatabrain.com' });\n }\n // A generated API credential must never replace the authorizing hosted principal.\n override setRuntimeApiToken(_token: string): void {}\n override isHosted(): boolean { return true; }\n\n override async request<T>(options: RequestOptions): Promise<T> {\n try {\n const name = toolContext.getStore()?.tool;\n if (!name) throw new HostedAuthError(403, 'tool_not_authorized');\n authorizeTool(name, this.principal);\n const named = callWorkspace.getStore()?.target;\n // With no workspace for the call (a tool that walks every granted workspace), a request\n // that names one top-level workspace acts in it, and says so in the header too.\n const target = named ?? (selectWorkspace(options.body, this.principal, ['workspaceName', 'workspaceId'])\n ?? selectWorkspace(options.query, this.principal, ['workspaceName', 'workspaceId']));\n const delegated = this.delegation !== undefined;\n // Chart preview rows are query output (a row may have a userId column); the issuer\n // treats them as data too. Every other body field is still checked.\n const opaque = options.method === 'POST' && options.path === PREVIEW_PATH ? ['rows']\n : options.method === 'POST' && /^\\/api\\/v2\\/custom-charts\\/[a-z][a-z0-9_-]*\\/bryn\\/turn$/.test(options.path) ? ['draft'] : undefined;\n assertBoundArguments(options.body, this.principal, { target, delegated, opaque });\n assertBoundArguments(options.query, this.principal, { target, delegated });\n let raw = await this.adapter.executeRequest(structuredClone(options), structuredClone(this.principal),\n this.delegation, target && structuredClone(target));\n // A call that named its workspace reads the workspace list as that workspace only, so a\n // tool that walks the list (list_dashboards) stays in it.\n if (named && options.method === 'GET' && options.path === '/api/v2/workspace') raw = onlyWorkspace(raw, named);\n if (!options.skipDataUnwrap && raw && typeof raw === 'object' && !Array.isArray(raw) &&\n 'data' in raw && raw.data !== null && raw.data !== undefined) return raw.data as T;\n return raw as T;\n } catch (error) {\n if (error instanceof WorkspaceSelectionError) {\n throw new DatabrainApiError(error.status, error.code, `${error.reason} (${error.code})`, undefined, WORKSPACE_HINT);\n }\n // The adapter may encounter secret-bearing remote bodies. Do not pass them to tools/logs.\n const status = error instanceof HostedAuthError ? error.status : 503;\n const detail = error instanceof HostedAuthError ? error.detail : undefined;\n // A 403 reaches the tool only with a reason about this user's own grant (the adapter\n // keeps no other 403 body): the role in that workspace, or workspace creation.\n if (status === 403 && detail?.code) {\n throw new DatabrainApiError(0, detail.code, `${detail.message ?? 'Your role does not allow this here.'} (${detail.code})`,\n undefined, 'Ask a Databrain admin for access, or pick a workspace where your role allows it.');\n }\n if (status === 401 || status === 403) {\n // Status 0 carries no API-key hint: a hosted user has no key to rotate, only a sign-in.\n throw new DatabrainApiError(0, 'HOSTED_REQUEST_DENIED',\n 'The signed-in Databrain user cannot do this: the sign-in expired or was revoked, or the granted scopes, role or workspace do not allow it. Sign in again or ask an admin.');\n }\n if (detail && (detail.code || detail.message)) {\n // The code rides with the message: a short message (\"The workspace does not allow\n // this.\") only makes sense next to its reason (DATA_APP_REQUIRED).\n const message = detail.message ?? (status === 404 ? 'Not found in the workspace this sign-in approved.'\n : 'The backend refused this request.');\n throw new DatabrainApiError(detail.status, detail.code ?? 'HOSTED_REQUEST_FAILED',\n detail.code && !message.includes(detail.code) ? `${message} (${detail.code})` : message,\n { error: { code: detail.code, message: detail.message } });\n }\n throw new DatabrainApiError(status, 'HOSTED_REQUEST_DENIED',\n status === 404\n ? 'Not found in the workspace this sign-in approved.'\n : 'The authorized backend could not complete this request. Check workspace access and hosted integration.');\n }\n }\n}\n\n/** The hosted description line for a tool that works in one workspace. */\nfunction workspaceLine(name: string, input: string): string {\n return WORKSPACE_SPANNING_TOOLS.has(name)\n ? ` With more than one granted workspace, name one in \\`${input}\\` to list only that one; leave it out to list them all.`\n : ` Needed when you granted more than one workspace: name it in \\`${input}\\`.`;\n}\n\nfunction whoami(principal: HostedPrincipal) {\n const granted = grantedWorkspaces(principal);\n return toolSuccess(JSON.stringify({\n status: 'authenticated', activeBearer: principal.credentialType,\n actorKind: principal.actorKind, userId: principal.userId, companyId: principal.companyId,\n workspaceGrant: principal.workspaces?.mode === 'all'\n ? 'All workspaces I can access (new ones are included)' : 'Only the workspaces chosen at sign-in',\n grantedWorkspaces: granted.map(item => ({ id: item.id, name: item.name })),\n ...(granted.length === 1 ? { workspaceId: granted[0].id, workspaceName: granted[0].name } : {}),\n note: granted.length === 1\n ? 'Every call works in this workspace.'\n : `Name the workspace on each call that works in one (the \\`${WORKSPACE_INPUT}\\` or \\`workspaceName\\` input): ${grantedNames(principal)}.`,\n oauthClientId: principal.clientId, scopes: principal.scopes,\n }, null, 2));\n}\n\nexport function createHostedToolServer(principal: HostedPrincipal, adapter: HostedAdapter,\n delegation?: HostedDelegation): McpServer {\n const bound = structuredClone(principal);\n const delegated = delegation !== undefined;\n const server = new McpServer({ name: 'databrain', version: MCP_SERVER_VERSION }, {\n instructions: 'Hosted Databrain: each request is authenticated as the signed-in user, with that user\\'s own role in each workspace. ' +\n 'Use whoami to see the verified identity, scopes and the workspaces this sign-in granted. ' +\n 'When more than one workspace is granted, name the workspace on each call (the workspace or workspaceName input). ' +\n 'Never ask for a service token or put credentials in tool arguments. ' +\n 'Workspace and tenant access come from the bearer principal. Existing dry-run and production-confirmation rules still apply. ' +\n 'For existing saved charts, call list_widgets with dashboardId or dashboardName and no clientId; no embed, data app or datamart is needed. Use render_metric_tile to draw a metricRef directly. Use ask_question for new data questions. Chart controls and save_chart_as_dashboard use the same workspace and role; ' +\n 'saving requires the existing production confirmation. Hosts without MCP Apps can read the text answer.',\n });\n\n // The existing shared registry already wraps server.tool for intent telemetry. Layer\n // hosted policy underneath it; keep every handler and annotation. A tool that works in one\n // workspace says so, and gains a `workspace` input when its own schema names none.\n const register = server.tool.bind(server) as (...args: any[]) => unknown;\n server.tool = ((...args: any[]) => {\n const name = args[0] as string;\n const index = args.length - 1;\n const handler = args[index] as (...values: any[]) => CallToolResult | Promise<CallToolResult>;\n const shape = typeof args[1] === 'string' && args[2] && typeof args[2] === 'object' &&\n !('readOnlyHint' in args[2]) ? args[2] as Record<string, unknown> : undefined;\n const ownSelector = shape && (['workspaceName', 'workspaceId'] as const).find(key => Object.hasOwn(shape, key));\n const addsInput = shape !== undefined && !ownSelector && !WORKSPACE_FREE_TOOLS.has(name) &&\n !Object.hasOwn(shape, WORKSPACE_INPUT);\n if (shape && !WORKSPACE_FREE_TOOLS.has(name)) {\n if (addsInput) {\n args[2] = { ...shape, [WORKSPACE_INPUT]: z.string().min(1).max(512).optional()\n .describe(WORKSPACE_SPANNING_TOOLS.has(name)\n ? 'Workspace id or name. Leave it out to cover every granted workspace.'\n : 'Workspace id or name. Needed when you granted more than one workspace.') };\n }\n args[1] = `${args[1]}${workspaceLine(name, addsInput ? WORKSPACE_INPUT : ownSelector!)}`;\n }\n args[index] = async (...values: any[]) => {\n try {\n authorizeTool(name, bound);\n const target = assertToolArguments(name, values[0], bound, delegated);\n if (name === 'whoami') return whoami(bound);\n const input = values[0] && typeof values[0] === 'object' ? values[0] as Record<string, unknown> : undefined;\n if (input?.projectDir !== undefined) return toolError('projectDir is not available on the hosted server. Pass chartTs, manifest, bundleCss and iconSvg to compile in memory.');\n if (!target && !WORKSPACE_FREE_TOOLS.has(name) && !WORKSPACE_SPANNING_TOOLS.has(name) && !namesWorkspace(input)) {\n throw new WorkspaceSelectionError(400, 'WORKSPACE_REQUIRED',\n `Name the workspace for this call. Granted: ${grantedNames(bound)}.`);\n }\n if (input) {\n // The new input is a header, never a body field. A selector the tool declares\n // gets the call's workspace when it was left out, as for one granted workspace before.\n if (addsInput) delete input[WORKSPACE_INPUT];\n if (shape && target) {\n if (Object.hasOwn(shape, 'workspaceId')) input.workspaceId = target.id;\n if (Object.hasOwn(shape, 'workspaceName') && target.name !== undefined) input.workspaceName = target.name;\n }\n }\n const result = await callWorkspace.run({ target }, () => handler(...values));\n // The iframe's follow-up calls have no model to repeat the workspace selector.\n // Carry only this verified call's workspace, never a draft's claimed choices.\n if (target && result?._meta?.['ui/resourceUri']) {\n return { ...result, _meta: { ...result._meta, 'databrain/workspace': target } };\n }\n return result;\n } catch (error) {\n if (error instanceof WorkspaceSelectionError) return workspaceError(error);\n return toolError('Hosted authorization denied this tool call. Check the granted scopes, workspace and tenant access.');\n }\n };\n return register(...args);\n }) as McpServer['tool'];\n\n const client = new HostedClient(adapter, bound, delegation);\n enableChartApps(server);\n registerAllTools(server, client);\n // Hosted only: a grant of \"All workspaces I can access\" may create one (the issuer decides).\n registerCreateWorkspace(server, client);\n registerAllResources(server);\n registerAllPrompts(server);\n // Keep Apps on the principal-bound client and under the same registration policy.\n registerChartApps(server, client);\n instrument(server, schemaInstrumentation, { beforeSend: () => null });\n return server;\n}\n","import { z } from 'zod';\nimport type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';\nimport type { DatabrainClient } from '../../client/databrain-client.js';\nimport { DatabrainApiError, toolError, toolSuccess, stringify } from '../../utils/errors.js';\nimport { WRITE_CREATE } from '../annotations.js';\n\n/**\n * Registered by the hosted server only (stdio dropped it in v0.2.0). The issuer allows it\n * only for a sign-in granted \"All workspaces I can access\" and a role that may create one.\n */\nexport function registerCreateWorkspace(server: McpServer, client: DatabrainClient): void {\n server.tool(\n 'create_workspace',\n 'Create a new workspace to connect a datasource or datamart for dashboard building. ' +\n 'Works only when this sign-in granted \"All workspaces I can access\" and your role may create workspaces; ' +\n 'the new workspace is usable on the next call.',\n {\n name: z.string().describe('Unique workspace name'),\n connectionType: z\n .enum(['DATASOURCE', 'DATAMART', 'MULTI_DATASOURCE'])\n .optional()\n .describe('Connection type — DATASOURCE, DATAMART, or MULTI_DATASOURCE'),\n datasourceName: z\n .string()\n .optional()\n .describe('Datasource name (required when connectionType is DATASOURCE)'),\n datamartName: z\n .string()\n .optional()\n .describe('Datamart name (required when connectionType is DATAMART)'),\n body: z\n .record(z.unknown())\n .optional()\n .describe('Raw API body passthrough for POST /api/v2/workspace'),\n },\n WRITE_CREATE,\n async (params) => {\n try {\n const body: Record<string, unknown> = params.body\n ? { ...params.body }\n : {\n name: params.name,\n connectionType: params.connectionType,\n datasourceName: params.datasourceName,\n datamartName: params.datamartName,\n };\n\n const result = await client.request<{ name?: string; id?: string; _id?: string; error?: unknown }>({\n method: 'POST',\n path: '/api/v2/workspace',\n body,\n auth: 'service',\n invalidateCache: ['list_workspaces'],\n });\n\n if (result.error) {\n return toolError(`Workspace creation failed: ${stringify(result.error)}`);\n }\n\n return toolSuccess(\n `Workspace \"${result.name || params.name}\" created successfully.\\n\\n` +\n 'Next steps:\\n' +\n '1. Use `create_data_app` to create a data app linked to this workspace\\n' +\n '2. Build dashboards in the Databrain UI using this workspace\\n' +\n '3. Use `create_embed` to embed dashboards',\n );\n } catch (err) {\n if (err instanceof DatabrainApiError) return err.toToolResponse();\n return toolError(`Unexpected error: ${(err as Error).message}`);\n }\n },\n );\n}\n","import { HostedAuthError } from './types.js';\nimport type { GrantedWorkspace, HostedPrincipal, HostedScope } from './types.js';\n\n/** Explicit exhaustive policy: a newly registered tool is denied until it gets a policy. */\nexport const TOOL_SCOPES: Readonly<Record<string, readonly HostedScope[]>> = {\n whoami: [],\n list_datasources: ['mcp:datamarts:read'], discover_datasource_schema: ['mcp:datamarts:read'],\n list_datamarts: ['mcp:datamarts:read'], list_workspaces: ['mcp:datamarts:read'],\n sync_datasource: ['mcp:datamarts:write'], create_datamart: ['mcp:datamarts:write'],\n list_data_apps: ['mcp:dashboards:read'], list_tenants: ['mcp:dashboards:read'],\n create_data_app: ['mcp:embeds:write'], list_api_tokens: ['mcp:embeds:write'],\n create_api_token: ['mcp:embeds:write'], rotate_api_token: ['mcp:embeds:write'],\n create_embed: ['mcp:embeds:write'], list_embeds: ['mcp:dashboards:read'],\n get_embed_details: ['mcp:dashboards:read'], update_embed: ['mcp:embeds:write'],\n customize_embed_theme: ['mcp:embeds:write'], get_demo_link: ['mcp:embeds:write'],\n generate_guest_token: ['mcp:embeds:write'], generate_embed_code: ['mcp:dashboards:read'],\n create_workspace: ['mcp:datamarts:write'],\n list_dashboards: ['mcp:dashboards:read'], create_dashboard: ['mcp:dashboards:write'],\n get_dashboard: ['mcp:dashboards:read'], dashboard_snapshot: ['mcp:dashboards:read'], generate_dashboard_tiles: ['mcp:dashboards:write', 'mcp:query', 'mcp:embeds:write'],\n ask_question: ['mcp:query'], get_semantic_layer: ['mcp:datamarts:read'],\n update_semantic_layer: ['mcp:datamarts:write'], start_semantic_layer_generation: ['mcp:datamarts:write'],\n get_semantic_layer_generation_status: ['mcp:datamarts:read'],\n search_semantic_model: ['mcp:datamarts:read'], get_semantic_entity: ['mcp:datamarts:read'],\n propose_semantic_change: ['mcp:datamarts:read'], apply_semantic_change: ['mcp:datamarts:write'],\n eval_semantic_model: ['mcp:datamarts:read', 'mcp:query'],\n list_widgets: ['mcp:dashboards:read'], validate_metric_spec: ['mcp:dashboards:read', 'mcp:query', 'mcp:embeds:write'],\n create_or_update_metric_from_query: ['mcp:dashboards:write', 'mcp:query', 'mcp:embeds:write'],\n create_workspace_metric: ['mcp:dashboards:write', 'mcp:query'],\n add_widget: ['mcp:dashboards:write', 'mcp:embeds:write'], update_widget: ['mcp:dashboards:write', 'mcp:embeds:write'],\n remove_widget: ['mcp:dashboards:write', 'mcp:embeds:write'], generate_widget: ['mcp:dashboards:write', 'mcp:query', 'mcp:embeds:write'],\n list_scheduled_reports: ['mcp:dashboards:read'],\n custom_chart_edit: ['mcp:custom-charts:write'], custom_chart_preview: ['mcp:custom-charts:read'],\n load_custom_chart_bundle: ['mcp:custom-charts:read'],\n load_custom_chart_runtime: ['mcp:custom-charts:read'],\n custom_chart_scaffold: [], custom_chart_validate: ['mcp:custom-charts:read'],\n custom_chart_upload: ['mcp:custom-charts:write'], list_custom_charts: ['mcp:custom-charts:read'],\n publish_custom_chart: ['mcp:custom-charts:write'],\n // Chart tile controls (the chart-apps composition). The same routes as ask_question and a\n // dashboard save; listed so a hosted composition is not refused for want of a policy.\n preview_chart: ['mcp:query'], save_chart_as_dashboard: ['mcp:dashboards:write', 'mcp:query'],\n // Hosted platform reads do not mint embed credentials. Guest rendering retains its backend boundary.\n render_metric_tile: ['mcp:dashboards:read', 'mcp:query'],\n render_chart: ['mcp:dashboards:read', 'mcp:query'],\n refresh_widget: ['mcp:dashboards:read', 'mcp:query'],\n drill_down: ['mcp:dashboards:read', 'mcp:query'],\n change_date_range: ['mcp:dashboards:read', 'mcp:query'],\n};\n\nexport function requiredScopes(name: string): readonly HostedScope[] {\n if (!Object.hasOwn(TOOL_SCOPES, name)) throw new HostedAuthError(403, 'tool_not_authorized');\n return TOOL_SCOPES[name];\n}\n\nexport function authorizeTool(name: string, principal: HostedPrincipal): void {\n if (principal.expiresAt !== undefined && principal.expiresAt <= Date.now() / 1000) throw new HostedAuthError(401, 'invalid_token');\n if (requiredScopes(name).some(scope => !principal.scopes.includes(scope))) throw new HostedAuthError(403, 'insufficient_scope');\n}\n\n/**\n * A tool's own top-level argument whose name is also a forbidden identity key, with the\n * only values it may take. `list_dashboards.scope` picks the listing, not an OAuth scope.\n */\nconst TOOL_ARGUMENT_VALUES: Readonly<Record<string, Readonly<Record<string, readonly string[]>>>> = {\n list_dashboards: { scope: ['auto', 'data_app', 'global'] },\n};\n\n/**\n * Top-level tool arguments that carry query rows, not selectors. A row may have a\n * `userId` or `workspaceId` column; the issuer treats the rows as data.\n */\nconst TOOL_OPAQUE_ARGUMENTS: Readonly<Record<string, readonly string[]>> = {\n preview_chart: ['rows'],\n custom_chart_preview: ['rows', 'manifest'],\n custom_chart_edit: ['draft'],\n};\n\n/**\n * Tool-call arguments: the allowed own arguments by exact value, then the shared binding rules\n * against the call's workspace. Returns that workspace (see selectWorkspace).\n */\nexport function assertToolArguments(name: string, value: unknown, principal: HostedPrincipal,\n delegated = false): GrantedWorkspace | undefined {\n const allowed = Object.hasOwn(TOOL_ARGUMENT_VALUES, name) ? TOOL_ARGUMENT_VALUES[name] : {};\n const opaque = Object.hasOwn(TOOL_OPAQUE_ARGUMENTS, name) ? TOOL_OPAQUE_ARGUMENTS[name] : undefined;\n const target = selectWorkspace(value, principal);\n if (!value || typeof value !== 'object' || Array.isArray(value)) {\n assertBoundArguments(value, principal, { target, delegated });\n return target;\n }\n const rest: Record<string, unknown> = {};\n for (const [key, child] of Object.entries(value)) {\n if (!Object.hasOwn(allowed, key)) { rest[key] = child; continue; }\n if (typeof child !== 'string' || !allowed[key].includes(child)) throw new HostedAuthError(403, 'identity_argument_forbidden');\n }\n assertBoundArguments(rest, principal, { target, delegated, opaque });\n return target;\n}\n\n/**\n * Tools that act on no single workspace: identity, the granted list, company-level\n * custom charts, local code generation, and workspace creation. Every other tool\n * reaches a workspace-scoped route, so it can name its workspace.\n */\nexport const WORKSPACE_FREE_TOOLS: ReadonlySet<string> = new Set([\n 'whoami', 'list_workspaces', 'create_workspace', 'generate_embed_code',\n 'custom_chart_preview', 'load_custom_chart_bundle', 'load_custom_chart_runtime',\n 'custom_chart_scaffold', 'custom_chart_validate', 'custom_chart_upload', 'list_custom_charts', 'publish_custom_chart',\n]);\n\n/**\n * Tools that walk every granted workspace when the call names none (each request then names\n * its own workspace), and only the named one when it does. They never ask for a workspace.\n */\nexport const WORKSPACE_SPANNING_TOOLS: ReadonlySet<string> = new Set(['list_dashboards']);\n\n/** The workspace selector a hosted tool gains when its own schema has none. Sent only as a header. */\nexport const WORKSPACE_INPUT = 'workspace';\n\n/** Raised before any request when a call names its workspace wrongly; the tool shows the message. */\nexport class WorkspaceSelectionError extends HostedAuthError {\n constructor(status: 400 | 404, code: string, public readonly reason: string) {\n super(status, code);\n }\n}\n\nexport function grantedWorkspaces(principal: HostedPrincipal): GrantedWorkspace[] {\n if (principal.grantedWorkspaces) return principal.grantedWorkspaces;\n return principal.workspaceId ? [{ id: principal.workspaceId, name: principal.workspaceName }] : [];\n}\n\n/** \"A, B, C\", at most 20 names, then \"and N more\". Only the user's own granted names. */\nexport function grantedNames(principal: HostedPrincipal): string {\n const names = grantedWorkspaces(principal).map(item => item.name ?? item.id);\n return names.length > 20 ? `${names.slice(0, 20).join(', ')} and ${names.length - 20} more` : names.join(', ');\n}\n\n/** A selector is a granted workspace's id, else its exact name. Nothing else, and never two. */\nfunction findGranted(principal: HostedPrincipal, key: string, value: unknown): GrantedWorkspace {\n const granted = grantedWorkspaces(principal);\n const matches = typeof value !== 'string' ? []\n : key === 'workspaceId' ? granted.filter(item => item.id === value)\n : key === 'workspaceName' ? granted.filter(item => item.name === value)\n : granted.some(item => item.id === value) ? granted.filter(item => item.id === value)\n : granted.filter(item => item.name === value);\n if (matches.length !== 1) {\n throw new WorkspaceSelectionError(404, 'NOT_FOUND_IN_GRANT',\n `Workspace ${JSON.stringify(String(value).slice(0, 100))} is not one this sign-in granted. Granted: ${grantedNames(principal)}.`);\n }\n return matches[0];\n}\n\n/**\n * The one workspace a tool call acts in: the top-level `workspace`, `workspaceName` or\n * `workspaceId` inputs (all must name the same granted workspace), else the only granted\n * workspace, else none (the issuer then asks for one, or the route is company-level).\n */\nexport function selectWorkspace(args: unknown, principal: HostedPrincipal,\n keys: readonly string[] = [WORKSPACE_INPUT, 'workspaceName', 'workspaceId']): GrantedWorkspace | undefined {\n let target: GrantedWorkspace | undefined;\n if (args && typeof args === 'object' && !Array.isArray(args)) {\n for (const key of keys) {\n const value = (args as Record<string, unknown>)[key];\n if (value === undefined) continue;\n const found = findGranted(principal, key, value);\n if (target && target.id !== found.id) {\n throw new WorkspaceSelectionError(400, 'WORKSPACE_CONFLICT',\n 'These inputs name different workspaces. Name one workspace for this call.');\n }\n target = found;\n }\n }\n const granted = grantedWorkspaces(principal);\n return target ?? (granted.length === 1 ? granted[0] : undefined);\n}\n\n/** True when a workspace selector appears anywhere in the arguments, raw passthroughs included. */\nexport function namesWorkspace(value: unknown): boolean {\n if (!value || typeof value !== 'object') return false;\n return Object.entries(value).some(([key, child]) =>\n ([WORKSPACE_INPUT, 'workspaceName', 'workspaceId'].includes(key) && child !== undefined) || namesWorkspace(child));\n}\n\n/** What one tool call is bound to, beyond the principal. */\nexport interface ArgumentBinding {\n /** The call's workspace (selectWorkspace). Every workspace selector must name it. */\n target?: GrantedWorkspace;\n /**\n * The call runs as a delegated OAuth request: the issuer checks any tenant id against its\n * route policy on that very call, as it does for the app user, and refuses a route that\n * claims none. Without it (a PAT, a non-delegating adapter) a tenant needs `tenantIds`.\n */\n delegated?: boolean;\n /** Top-level keys holding data rather than selectors (query rows), never scanned. */\n opaque?: readonly string[];\n}\n\nfunction tenantAllowed(child: unknown, principal: HostedPrincipal, delegated: boolean): boolean {\n if (typeof child !== 'string') return false;\n if (principal.tenantIds) return principal.tenantIds.includes(child);\n return delegated && principal.credentialType === 'oauth';\n}\n\n/**\n * Reject conflicting selectors even in raw body passthroughs before a handler can use them.\n * A workspace selector must name the call's target when it has one, else a granted workspace.\n */\nexport function assertBoundArguments(value: unknown, principal: HostedPrincipal, binding: ArgumentBinding = {}): void {\n if (!value || typeof value !== 'object') return;\n const { opaque, ...nested } = binding;\n for (const [key, child] of Object.entries(value)) {\n if (opaque?.includes(key) && !Array.isArray(value)) continue;\n if (key === 'workspaceId' || key === 'workspaceName') {\n const target = binding.target;\n if (target ? child !== (key === 'workspaceId' ? target.id : target.name) : !findGranted(principal, key, child)) {\n throw new HostedAuthError(403, 'principal_mismatch');\n }\n }\n if (['companyId', 'userId'].includes(key) && child !== principal[key as 'companyId' | 'userId']) {\n throw new HostedAuthError(403, 'principal_mismatch');\n }\n if (['scope', 'scopes', 'authUser', 'actorKind', 'credentialType', 'oauthClientId', 'operatorEmail',\n 'session_variables', 'isAllClient', 'isAdmin', 'serviceToken', 'apiToken', 'authorization'].includes(key)) {\n throw new HostedAuthError(403, 'identity_argument_forbidden');\n }\n if (['clientId', 'tenantId'].includes(key) && !tenantAllowed(child, principal, binding.delegated === true)) {\n throw new HostedAuthError(403, 'tenant_not_authorized');\n }\n assertBoundArguments(child, principal, nested);\n }\n}\n","import { parseStudioStream } from '../custom-chart/studio.js';\nimport { z } from 'zod';\nimport { MCP_SERVER_VERSION } from '../version.js';\nimport { toolContext } from '../utils/tool-context.js';\nimport { principalSchema } from './auth.js';\nimport { parseErrorBody } from '../client/error-body.js';\nimport { HostedAuthError } from './types.js';\nimport type { HostedAdapter, HostedConfig, HostedDelegation, HostedErrorDetail } from './types.js';\n\nconst USER_AGENT = `databrain-mcp-server/${MCP_SERVER_VERSION} (hosted)`;\nconst REQUEST_TIMEOUT_MS = 30_000;\n/** Request headers a tool may set; nothing that can carry or replace a credential. */\nconst FORWARDED_HEADERS = new Set(['idempotency-key']);\n/** The call's workspace, for the issuer's delegated path. Only this adapter sets it. */\nexport const WORKSPACE_HEADER = 'X-Databrain-Workspace';\n/**\n * 403 codes whose reason is about the caller's own grant in a workspace it named and holds,\n * so they reveal nothing about other workspaces or companies. Every other 403 stays opaque.\n */\nconst GRANT_DENIALS = new Set(['ROLE_NOT_ALLOWED', 'WORKSPACE_CREATE_NOT_ALLOWED', 'forbidden_permission', 'insufficient_scope']);\n\n/** Code and message only, as short plain text; never other body fields or the bearer. */\nfunction errorDetail(status: number, text: string, bearer: string | undefined): HostedErrorDetail {\n let parsed: unknown;\n try { parsed = JSON.parse(text); } catch { return { status }; }\n const envelope = parseErrorBody(parsed);\n // A dashboard write the API refuses tile by tile (422 `{tileVerdicts}`) carries no\n // envelope; its problems are the only reason a tool can act on.\n const tiles = tileProblems(parsed);\n const code = envelope.code ?? (tiles ? 'TILE_VALIDATION_FAILED' : undefined);\n const message = envelope.message ?? tiles;\n const clean = (value: string | undefined, max: number) => {\n if (!value) return undefined;\n let out = value.replace(/[\\u0000-\\u001f\\u007f]+/g, ' ').trim();\n if (bearer) out = out.split(bearer).join('[redacted]');\n return out.slice(0, max) || undefined;\n };\n // Long enough for the issuer's list of up to 20 granted workspace names.\n return { status, code: code && /^[A-Za-z0-9_.:-]{1,64}$/.test(code) ? code : undefined,\n message: clean(message, 1200) };\n}\n\n/** \"Tile t1: problem; problem. Tile t2: ...\" for the tiles a dashboard write refused. */\nfunction tileProblems(body: unknown): string | undefined {\n const verdicts = (body as { tileVerdicts?: unknown } | null)?.tileVerdicts;\n if (!Array.isArray(verdicts)) return undefined;\n const lines = verdicts.flatMap((verdict) => {\n const { id, ok, problems } = (verdict ?? {}) as { id?: unknown; ok?: unknown; problems?: unknown };\n if (ok !== false || !Array.isArray(problems)) return [];\n const words = problems.filter((p): p is string => typeof p === 'string');\n return words.length ? [`Tile ${typeof id === 'string' ? id : '?'}: ${words.join(' ')}`] : [];\n });\n return lines.length ? `The dashboard was not saved. ${lines.join(' ')}` : undefined;\n}\n\n/** BE-3's documented principal endpoint is the trust boundary for opaque OAuth tokens. */\nexport function createProviderAdapter(config: HostedConfig): HostedAdapter {\n const issuer = new URL(config.issuer);\n if (issuer.protocol !== 'https:' || issuer.username || issuer.password || issuer.search || issuer.hash) {\n throw new Error('Invalid hosted authorization server configuration.');\n }\n const apiBase = (config.apiUrl ?? config.issuer).replace(/\\/$/, '');\n const apiOrigin = new URL(apiBase).origin;\n const endpoint = `${apiBase}/oauth/principal`;\n const publicOrigin = new URL(config.publicUrl).origin;\n // Handle -> bearer, for this process only. A handle is unreadable and dies with its request.\n const bearers = new WeakMap<HostedDelegation, string>();\n const responseSchema = principalSchema.extend({ issuer: z.literal(config.issuer) }).strip();\n return {\n async verifyOAuth(token, resource) {\n try {\n // Only the explicitly configured issuer ever receives the token: here to verify it,\n // and in executeRequest for its delegated REST path. No redirects, other origins,\n // cache or service-token fallback.\n const response = await fetch(endpoint, {\n headers: { Authorization: `Bearer ${token}`, Accept: 'application/json' },\n redirect: 'error', signal: AbortSignal.timeout(10_000),\n });\n if (!response.ok) {\n if (response.status === 429 || response.status >= 500) throw new HostedAuthError(503, 'temporarily_unavailable');\n throw new HostedAuthError(401, 'invalid_token');\n }\n const { issuer: _issuer, ...principal } = responseSchema.parse(await response.json());\n if (principal.resource !== resource || principal.credentialType !== 'oauth') throw new HostedAuthError(401, 'invalid_token');\n return principal;\n } catch (error) {\n if (error instanceof HostedAuthError) throw error;\n throw new HostedAuthError(401, 'invalid_token');\n }\n },\n async resolvePatGrant() { throw new HostedAuthError(401, 'pat_membership_adapter_required'); },\n delegate(token) {\n const handle: HostedDelegation = Object.freeze({ kind: 'hosted-delegation' as const });\n bearers.set(handle, token);\n return handle;\n },\n async executeRequest(options, principal, delegation, workspace) {\n // The backend's delegated path re-verifies this bearer for this resource on every\n // call, then binds workspace, objects, role and audit. Only the configured issuer\n // receives it, exactly as for /oauth/principal. A guest-token call carries only the\n // guest token the backend minted for this user's earlier, authorized call.\n const bearer = delegation ? bearers.get(delegation) : undefined;\n if (options.auth !== 'none' && !bearer) throw new HostedAuthError(503, 'delegated_backend_adapter_required');\n if (options.auth === 'none' && !options.token) throw new HostedAuthError(403, 'tool_not_authorized');\n if (!options.path.startsWith('/') || options.path.startsWith('//')) throw new HostedAuthError(403, 'tool_not_authorized');\n const url = new URL(`${apiBase}${options.path}`);\n if (url.origin !== apiOrigin) throw new HostedAuthError(403, 'tool_not_authorized');\n for (const [key, value] of Object.entries(options.query ?? {})) url.searchParams.set(key, value);\n const headers: Record<string, string> = {\n Accept: 'application/json', 'Content-Type': 'application/json', 'User-Agent': USER_AGENT,\n 'X-Databrain-Client': USER_AGENT,\n };\n for (const [key, value] of Object.entries(options.headers ?? {})) {\n if (FORWARDED_HEADERS.has(key.toLowerCase())) headers[key] = value;\n }\n const tool = toolContext.getStore();\n if (tool) {\n headers['X-Databrain-MCP-Tool'] = tool.tool;\n headers['X-Databrain-MCP-Tool-Category'] = tool.category;\n headers['X-Databrain-MCP-Workflow-Intent'] = tool.workflowIntent;\n headers['X-Databrain-MCP-Input-Mode'] = tool.inputMode;\n }\n if (options.auth === 'none') {\n headers['x-plugin-token'] = options.token!;\n headers.Origin = publicOrigin;\n } else {\n headers.Authorization = `Bearer ${bearer}`;\n // Same rule as the bearer: only with it, only to the issuer, and only a workspace\n // this principal holds. The issuer re-checks it against the live grant.\n if (workspace) {\n if (!principal.grantedWorkspaces?.some(item => item.id === workspace.id) &&\n principal.workspaceId !== workspace.id) throw new HostedAuthError(403, 'principal_mismatch');\n headers[WORKSPACE_HEADER] = workspace.id;\n }\n }\n const response = await fetch(url, {\n method: options.method, headers, redirect: 'error',\n body: options.body === undefined ? undefined : JSON.stringify(options.body),\n signal: AbortSignal.timeout(options.timeoutMs ?? REQUEST_TIMEOUT_MS),\n });\n const text = await response.text();\n if (!response.ok) {\n // A wrong-workspace object is a 404, like a missing one. 401, 403 and 5xx keep the\n // status class only. Other 4xx keep the backend's code and message (a validation\n // reason, `layer_not_found`), so a tool can recover as it does on stdio.\n const forbidden = response.status === 403 ? errorDetail(403, text, bearer) : undefined;\n if (forbidden?.code && GRANT_DENIALS.has(forbidden.code)) {\n throw new HostedAuthError(403, 'delegated_request_denied',\n forbidden.code === 'insufficient_scope' && options.path === '/api/v2/guest-token/create'\n ? { ...forbidden, message: 'Guest/tenant operations require mcp:embeds:write. Ask an admin for a role that allows it, then connect again.' }\n : forbidden);\n }\n const status = response.status === 403 && options.responseFormat === 'studio-sse' ? 403 : response.status === 401 ? 401 : response.status >= 500 ? 503\n : response.status === 403 || response.status === 404 ? 404 : 400;\n const detail = status === 401 || status === 503 || response.status === 403 ? undefined\n : errorDetail(response.status, text, bearer);\n throw new HostedAuthError(status, 'delegated_request_denied', detail);\n }\n try { return options.responseFormat === 'studio-sse' ? parseStudioStream(text) : text ? JSON.parse(text) : null; }\n catch { throw new HostedAuthError(503, 'delegated_response_invalid'); }\n },\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,SAAS,kBAAkB;AAC3B,SAAS,qBAAqB;;;ACCvB,SAAS,iBAAiB,MAAyB,QAAQ,KAAmB;AACnF,QAAM,WAAW,IAAI,+BAA+B;AACpD,MAAI,YAAY,IAAI,aAAa,aAAc,OAAM,IAAI,MAAM,8CAA8C;AAC7G,MAAI,IAAI,8BAA8B,CAAC,SAAU,OAAM,IAAI,MAAM,qCAAqC;AACtG,QAAM,WAAW,CAAC,OAA2B,UAAkB;AAC7D,QAAI;AACF,UAAI,CAAC,MAAO,OAAM,IAAI,MAAM;AAC5B,YAAM,MAAM,IAAI,IAAI,KAAK;AACzB,UAAI,IAAI,YAAY,IAAI,YAAY,IAAI,UAAU,IAAI,QAAQ,IAAI,aAAa,SAAU,OAAM,IAAI,MAAM;AACzG,aAAO;AAAA,IACT,QAAQ;AAAE,YAAM,IAAI,MAAM,aAAa,KAAK,0DAA0D;AAAA,IAAG;AAAA,EAC3G;AAGA,MAAI,cAAc,IAAI;AACtB,MAAI,cAAc,IAAI;AACtB,MAAI,CAAC,eAAe,CAAC,eAAe,IAAI,gBAAgB;AACtD,UAAM,SAAS,SAAS,IAAI,gBAAgB,gBAAgB,EAAE;AAC9D,kBAAc,GAAG,MAAM;AACvB,k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EAAE,KAAK,SACxE,OAAO,QAAQ,GAA0B,MAAM,UAAU,EAAG,OAAM,IAAI,MAAM,iCAAiC;AAAA,EACjH;AACA,QAAM,UAAU,QAAQ,IAAI;AAC5B,MAAI,WAAW,CAAC,WAAW,OAAO,EAAG,OAAM,IAAI,MAAM,4CAA4C;AACjG,QAAM,SAAS,uBAAuB,QAAQ,SAAS,UAAU,IAAI,aAAa,OAAO,IAAI,MAAS;AACtG,QAAM,IAAI,QAAc,CAAC,SAAS,WAAW;AAC3C,WAAO,KAAK,SAAS,MAAM;AAC3B,WAAO,OAAO,OAAO,MAAM,OAAO,MAAM,OAAO;AAAA,EACjD,CAAC;AACD,UAAQ,OAAO,MAAM,oCAAoC,OAAO,IAAI,IAAI,OAAO,IAAI,OAAO,OAAO,WAAW,qEAAgE,EAAE;AAAA,CAAI;AAClL,QAAM,WAAW,MAAM;AACrB,WAAO,MAAM,MAAM,QAAQ,KAAK,CAAC,CAAC;AAClC,eAAW,MAAM;AAAE,aAAO,oBAAoB;AAAG,cAAQ,KAAK,CAAC;AAAA,IAAG,GAAG,GAAI,EAAE,MAAM;AAAA,EACnF;AACA,UAAQ,KAAK,UAAU,QAAQ;AAC/B,UAAQ,KAAK,WAAW,QAAQ;AAClC;","names":["z","z","z","z"]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/hosted/test-only.ts"],"sourcesContent":["import { FilePatStore } from './auth.js';\nimport { hashCredential } from './auth.js';\nimport { HostedAuthError } from './types.js';\nimport type { HostedAdapter } from './types.js';\n\n/** Explicit fixture harness. No login, consent, token issuer, real membership or live data. */\nexport function createTestOnlyAdapter(path: string): HostedAdapter {\n const fixtures = new FilePatStore(path);\n return {\n async verifyOAuth(token, resource) {\n const record = await fixtures.findByHash(hashCredential(token));\n if (!record || record.revokedAt !== null || record.principal.resource !== resource) {\n throw new HostedAuthError(401, 'invalid_token');\n }\n return record.principal;\n },\n async resolvePatGrant(principal) { return principal; },\n async executeRequest(options, principal) {\n if (options.method === 'GET' && options.path === '/api/v2/workspace') {\n const granted = principal.grantedWorkspaces ?? [{ id: principal.workspaceId!, name: principal.workspaceName }];\n return { data: granted.map(item => ({ id: item.id, name: item.name,\n datamart: { name: 'TEST ONLY — synthetic fixture, no live data' } })) };\n }\n throw new HostedAuthError(503, 'test_fixture_operation_unavailable');\n },\n };\n}\n"],"mappings":";;;;;;;;AAMO,SAAS,sBAAsB,MAA6B;AACjE,QAAM,WAAW,IAAI,aAAa,IAAI;AACtC,SAAO;AAAA,IACL,MAAM,YAAY,OAAO,UAAU;AACjC,YAAM,SAAS,MAAM,SAAS,WAAW,eAAe,KAAK,CAAC;AAC9D,UAAI,CAAC,UAAU,OAAO,cAAc,QAAQ,OAAO,UAAU,aAAa,UAAU;AAClF,cAAM,IAAI,gBAAgB,KAAK,eAAe;AAAA,MAChD;AACA,aAAO,OAAO;AAAA,IAChB;AAAA,IACA,MAAM,gBAAgB,WAAW;AAAE,aAAO;AAAA,IAAW;AAAA,IACrD,MAAM,eAAe,SAAS,WAAW;AACvC,UAAI,QAAQ,WAAW,SAAS,QAAQ,SAAS,qBAAqB;AACpE,cAAM,UAAU,UAAU,qBAAqB,CAAC,EAAE,IAAI,UAAU,aAAc,MAAM,UAAU,cAAc,CAAC;AAC7G,eAAO,EAAE,MAAM,QAAQ,IAAI,WAAS;AAAA,UAAE,IAAI,KAAK;AAAA,UAAI,MAAM,KAAK;AAAA,UAC5D,UAAU,EAAE,MAAM,mDAA8C;AAAA,QAAE,EAAE,EAAE;AAAA,MAC1E;AACA,YAAM,IAAI,gBAAgB,KAAK,oCAAoC;AAAA,IACrE;AAAA,EACF;AACF;","names":[]}
|
|
File without changes
|