@frontmcp/ui 0.5.0

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.
Files changed (393) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +438 -0
  3. package/package.json +147 -0
  4. package/src/adapters/index.d.ts +10 -0
  5. package/src/adapters/index.js +18 -0
  6. package/src/adapters/index.js.map +1 -0
  7. package/src/adapters/platform-meta.d.ts +165 -0
  8. package/src/adapters/platform-meta.js +310 -0
  9. package/src/adapters/platform-meta.js.map +1 -0
  10. package/src/base-template/bridge.d.ts +89 -0
  11. package/src/base-template/bridge.js +452 -0
  12. package/src/base-template/bridge.js.map +1 -0
  13. package/src/base-template/default-base-template.d.ts +91 -0
  14. package/src/base-template/default-base-template.js +435 -0
  15. package/src/base-template/default-base-template.js.map +1 -0
  16. package/src/base-template/index.d.ts +14 -0
  17. package/src/base-template/index.js +30 -0
  18. package/src/base-template/index.js.map +1 -0
  19. package/src/base-template/polyfills.d.ts +30 -0
  20. package/src/base-template/polyfills.js +190 -0
  21. package/src/base-template/polyfills.js.map +1 -0
  22. package/src/base-template/theme-styles.d.ts +73 -0
  23. package/src/base-template/theme-styles.js +95 -0
  24. package/src/base-template/theme-styles.js.map +1 -0
  25. package/src/bridge/adapters/base-adapter.d.ts +103 -0
  26. package/src/bridge/adapters/base-adapter.js +314 -0
  27. package/src/bridge/adapters/base-adapter.js.map +1 -0
  28. package/src/bridge/adapters/claude.adapter.d.ts +66 -0
  29. package/src/bridge/adapters/claude.adapter.js +145 -0
  30. package/src/bridge/adapters/claude.adapter.js.map +1 -0
  31. package/src/bridge/adapters/ext-apps.adapter.d.ts +142 -0
  32. package/src/bridge/adapters/ext-apps.adapter.js +416 -0
  33. package/src/bridge/adapters/ext-apps.adapter.js.map +1 -0
  34. package/src/bridge/adapters/gemini.adapter.d.ts +63 -0
  35. package/src/bridge/adapters/gemini.adapter.js +160 -0
  36. package/src/bridge/adapters/gemini.adapter.js.map +1 -0
  37. package/src/bridge/adapters/generic.adapter.d.ts +55 -0
  38. package/src/bridge/adapters/generic.adapter.js +108 -0
  39. package/src/bridge/adapters/generic.adapter.js.map +1 -0
  40. package/src/bridge/adapters/index.d.ts +25 -0
  41. package/src/bridge/adapters/index.js +65 -0
  42. package/src/bridge/adapters/index.js.map +1 -0
  43. package/src/bridge/adapters/openai.adapter.d.ts +64 -0
  44. package/src/bridge/adapters/openai.adapter.js +194 -0
  45. package/src/bridge/adapters/openai.adapter.js.map +1 -0
  46. package/src/bridge/core/adapter-registry.d.ts +121 -0
  47. package/src/bridge/core/adapter-registry.js +271 -0
  48. package/src/bridge/core/adapter-registry.js.map +1 -0
  49. package/src/bridge/core/bridge-factory.d.ts +198 -0
  50. package/src/bridge/core/bridge-factory.js +428 -0
  51. package/src/bridge/core/bridge-factory.js.map +1 -0
  52. package/src/bridge/core/index.d.ts +9 -0
  53. package/src/bridge/core/index.js +22 -0
  54. package/src/bridge/core/index.js.map +1 -0
  55. package/src/bridge/index.d.ts +61 -0
  56. package/src/bridge/index.js +94 -0
  57. package/src/bridge/index.js.map +1 -0
  58. package/src/bridge/runtime/iife-generator.d.ts +61 -0
  59. package/src/bridge/runtime/iife-generator.js +940 -0
  60. package/src/bridge/runtime/iife-generator.js.map +1 -0
  61. package/src/bridge/runtime/index.d.ts +8 -0
  62. package/src/bridge/runtime/index.js +16 -0
  63. package/src/bridge/runtime/index.js.map +1 -0
  64. package/src/bridge/types.d.ts +385 -0
  65. package/src/bridge/types.js +11 -0
  66. package/src/bridge/types.js.map +1 -0
  67. package/src/build/cdn-resources.d.ts +140 -0
  68. package/src/build/cdn-resources.js +314 -0
  69. package/src/build/cdn-resources.js.map +1 -0
  70. package/src/build/index.d.ts +294 -0
  71. package/src/build/index.js +325 -0
  72. package/src/build/index.js.map +1 -0
  73. package/src/build/widget-manifest.d.ts +212 -0
  74. package/src/build/widget-manifest.js +652 -0
  75. package/src/build/widget-manifest.js.map +1 -0
  76. package/src/bundler/bundler.d.ts +110 -0
  77. package/src/bundler/bundler.js +432 -0
  78. package/src/bundler/bundler.js.map +1 -0
  79. package/src/bundler/cache.d.ts +172 -0
  80. package/src/bundler/cache.js +250 -0
  81. package/src/bundler/cache.js.map +1 -0
  82. package/src/bundler/index.d.ts +41 -0
  83. package/src/bundler/index.js +73 -0
  84. package/src/bundler/index.js.map +1 -0
  85. package/src/bundler/sandbox/enclave-adapter.d.ts +120 -0
  86. package/src/bundler/sandbox/enclave-adapter.js +339 -0
  87. package/src/bundler/sandbox/enclave-adapter.js.map +1 -0
  88. package/src/bundler/sandbox/executor.d.ts +13 -0
  89. package/src/bundler/sandbox/executor.js +22 -0
  90. package/src/bundler/sandbox/executor.js.map +1 -0
  91. package/src/bundler/sandbox/policy.d.ts +61 -0
  92. package/src/bundler/sandbox/policy.js +238 -0
  93. package/src/bundler/sandbox/policy.js.map +1 -0
  94. package/src/bundler/types.d.ts +347 -0
  95. package/src/bundler/types.js +132 -0
  96. package/src/bundler/types.js.map +1 -0
  97. package/src/components/alert.d.ts +71 -0
  98. package/src/components/alert.js +189 -0
  99. package/src/components/alert.js.map +1 -0
  100. package/src/components/alert.schema.d.ts +114 -0
  101. package/src/components/alert.schema.js +105 -0
  102. package/src/components/alert.schema.js.map +1 -0
  103. package/src/components/avatar.d.ts +76 -0
  104. package/src/components/avatar.js +176 -0
  105. package/src/components/avatar.js.map +1 -0
  106. package/src/components/avatar.schema.d.ts +169 -0
  107. package/src/components/avatar.schema.js +103 -0
  108. package/src/components/avatar.schema.js.map +1 -0
  109. package/src/components/badge.d.ts +70 -0
  110. package/src/components/badge.js +149 -0
  111. package/src/components/badge.js.map +1 -0
  112. package/src/components/badge.schema.d.ts +109 -0
  113. package/src/components/badge.schema.js +96 -0
  114. package/src/components/badge.schema.js.map +1 -0
  115. package/src/components/button.d.ts +111 -0
  116. package/src/components/button.js +336 -0
  117. package/src/components/button.js.map +1 -0
  118. package/src/components/button.schema.d.ts +148 -0
  119. package/src/components/button.schema.js +121 -0
  120. package/src/components/button.schema.js.map +1 -0
  121. package/src/components/card.d.ts +60 -0
  122. package/src/components/card.js +117 -0
  123. package/src/components/card.js.map +1 -0
  124. package/src/components/card.schema.d.ts +113 -0
  125. package/src/components/card.schema.js +98 -0
  126. package/src/components/card.schema.js.map +1 -0
  127. package/src/components/form.d.ts +239 -0
  128. package/src/components/form.js +420 -0
  129. package/src/components/form.js.map +1 -0
  130. package/src/components/form.schema.d.ts +441 -0
  131. package/src/components/form.schema.js +406 -0
  132. package/src/components/form.schema.js.map +1 -0
  133. package/src/components/index.d.ts +29 -0
  134. package/src/components/index.js +98 -0
  135. package/src/components/index.js.map +1 -0
  136. package/src/components/list.d.ts +127 -0
  137. package/src/components/list.js +279 -0
  138. package/src/components/list.js.map +1 -0
  139. package/src/components/list.schema.d.ts +134 -0
  140. package/src/components/list.schema.js +168 -0
  141. package/src/components/list.schema.js.map +1 -0
  142. package/src/components/modal.d.ts +111 -0
  143. package/src/components/modal.js +260 -0
  144. package/src/components/modal.js.map +1 -0
  145. package/src/components/modal.schema.d.ts +186 -0
  146. package/src/components/modal.schema.js +167 -0
  147. package/src/components/modal.schema.js.map +1 -0
  148. package/src/components/table.d.ts +105 -0
  149. package/src/components/table.js +283 -0
  150. package/src/components/table.js.map +1 -0
  151. package/src/components/table.schema.d.ts +159 -0
  152. package/src/components/table.schema.js +173 -0
  153. package/src/components/table.schema.js.map +1 -0
  154. package/src/handlebars/helpers.d.ts +348 -0
  155. package/src/handlebars/helpers.js +605 -0
  156. package/src/handlebars/helpers.js.map +1 -0
  157. package/src/handlebars/index.d.ts +193 -0
  158. package/src/handlebars/index.js +350 -0
  159. package/src/handlebars/index.js.map +1 -0
  160. package/src/index.d.ts +50 -0
  161. package/src/index.js +192 -0
  162. package/src/index.js.map +1 -0
  163. package/src/layouts/base.d.ts +88 -0
  164. package/src/layouts/base.js +227 -0
  165. package/src/layouts/base.js.map +1 -0
  166. package/src/layouts/index.d.ts +7 -0
  167. package/src/layouts/index.js +25 -0
  168. package/src/layouts/index.js.map +1 -0
  169. package/src/layouts/presets.d.ts +133 -0
  170. package/src/layouts/presets.js +277 -0
  171. package/src/layouts/presets.js.map +1 -0
  172. package/src/pages/consent.d.ts +116 -0
  173. package/src/pages/consent.js +218 -0
  174. package/src/pages/consent.js.map +1 -0
  175. package/src/pages/error.d.ts +100 -0
  176. package/src/pages/error.js +263 -0
  177. package/src/pages/error.js.map +1 -0
  178. package/src/pages/index.d.ts +8 -0
  179. package/src/pages/index.js +27 -0
  180. package/src/pages/index.js.map +1 -0
  181. package/src/react/Alert.d.ts +101 -0
  182. package/src/react/Alert.js +51 -0
  183. package/src/react/Alert.js.map +1 -0
  184. package/src/react/Badge.d.ts +100 -0
  185. package/src/react/Badge.js +55 -0
  186. package/src/react/Badge.js.map +1 -0
  187. package/src/react/Button.d.ts +108 -0
  188. package/src/react/Button.js +52 -0
  189. package/src/react/Button.js.map +1 -0
  190. package/src/react/Card.d.ts +103 -0
  191. package/src/react/Card.js +55 -0
  192. package/src/react/Card.js.map +1 -0
  193. package/src/react/hooks/context.d.ts +178 -0
  194. package/src/react/hooks/context.js +287 -0
  195. package/src/react/hooks/context.js.map +1 -0
  196. package/src/react/hooks/index.d.ts +41 -0
  197. package/src/react/hooks/index.js +61 -0
  198. package/src/react/hooks/index.js.map +1 -0
  199. package/src/react/hooks/tools.d.ts +283 -0
  200. package/src/react/hooks/tools.js +465 -0
  201. package/src/react/hooks/tools.js.map +1 -0
  202. package/src/react/index.d.ts +80 -0
  203. package/src/react/index.js +113 -0
  204. package/src/react/index.js.map +1 -0
  205. package/src/react/types.d.ts +105 -0
  206. package/src/react/types.js +12 -0
  207. package/src/react/types.js.map +1 -0
  208. package/src/react/utils.d.ts +42 -0
  209. package/src/react/utils.js +99 -0
  210. package/src/react/utils.js.map +1 -0
  211. package/src/registry/index.d.ts +45 -0
  212. package/src/registry/index.js +67 -0
  213. package/src/registry/index.js.map +1 -0
  214. package/src/registry/render-template.d.ts +86 -0
  215. package/src/registry/render-template.js +239 -0
  216. package/src/registry/render-template.js.map +1 -0
  217. package/src/registry/tool-ui.registry.d.ts +260 -0
  218. package/src/registry/tool-ui.registry.js +438 -0
  219. package/src/registry/tool-ui.registry.js.map +1 -0
  220. package/src/registry/uri-utils.d.ts +55 -0
  221. package/src/registry/uri-utils.js +97 -0
  222. package/src/registry/uri-utils.js.map +1 -0
  223. package/src/render/index.d.ts +7 -0
  224. package/src/render/index.js +14 -0
  225. package/src/render/index.js.map +1 -0
  226. package/src/render/prerender.d.ts +56 -0
  227. package/src/render/prerender.js +98 -0
  228. package/src/render/prerender.js.map +1 -0
  229. package/src/renderers/cache.d.ts +144 -0
  230. package/src/renderers/cache.js +240 -0
  231. package/src/renderers/cache.js.map +1 -0
  232. package/src/renderers/html.renderer.d.ts +122 -0
  233. package/src/renderers/html.renderer.js +204 -0
  234. package/src/renderers/html.renderer.js.map +1 -0
  235. package/src/renderers/index.d.ts +35 -0
  236. package/src/renderers/index.js +70 -0
  237. package/src/renderers/index.js.map +1 -0
  238. package/src/renderers/mdx.renderer.d.ts +119 -0
  239. package/src/renderers/mdx.renderer.js +305 -0
  240. package/src/renderers/mdx.renderer.js.map +1 -0
  241. package/src/renderers/react.renderer.d.ts +95 -0
  242. package/src/renderers/react.renderer.js +260 -0
  243. package/src/renderers/react.renderer.js.map +1 -0
  244. package/src/renderers/registry.d.ts +133 -0
  245. package/src/renderers/registry.js +232 -0
  246. package/src/renderers/registry.js.map +1 -0
  247. package/src/renderers/types.d.ts +341 -0
  248. package/src/renderers/types.js +9 -0
  249. package/src/renderers/types.js.map +1 -0
  250. package/src/renderers/utils/detect.d.ts +106 -0
  251. package/src/renderers/utils/detect.js +267 -0
  252. package/src/renderers/utils/detect.js.map +1 -0
  253. package/src/renderers/utils/hash.d.ts +39 -0
  254. package/src/renderers/utils/hash.js +75 -0
  255. package/src/renderers/utils/hash.js.map +1 -0
  256. package/src/renderers/utils/index.d.ts +8 -0
  257. package/src/renderers/utils/index.js +28 -0
  258. package/src/renderers/utils/index.js.map +1 -0
  259. package/src/renderers/utils/transpiler.d.ts +88 -0
  260. package/src/renderers/utils/transpiler.js +215 -0
  261. package/src/renderers/utils/transpiler.js.map +1 -0
  262. package/src/runtime/adapters/html.adapter.d.ts +58 -0
  263. package/src/runtime/adapters/html.adapter.js +131 -0
  264. package/src/runtime/adapters/html.adapter.js.map +1 -0
  265. package/src/runtime/adapters/index.d.ts +25 -0
  266. package/src/runtime/adapters/index.js +54 -0
  267. package/src/runtime/adapters/index.js.map +1 -0
  268. package/src/runtime/adapters/mdx.adapter.d.ts +72 -0
  269. package/src/runtime/adapters/mdx.adapter.js +241 -0
  270. package/src/runtime/adapters/mdx.adapter.js.map +1 -0
  271. package/src/runtime/adapters/react.adapter.d.ts +69 -0
  272. package/src/runtime/adapters/react.adapter.js +245 -0
  273. package/src/runtime/adapters/react.adapter.js.map +1 -0
  274. package/src/runtime/adapters/types.d.ts +94 -0
  275. package/src/runtime/adapters/types.js +11 -0
  276. package/src/runtime/adapters/types.js.map +1 -0
  277. package/src/runtime/csp.d.ts +37 -0
  278. package/src/runtime/csp.js +140 -0
  279. package/src/runtime/csp.js.map +1 -0
  280. package/src/runtime/index.d.ts +16 -0
  281. package/src/runtime/index.js +72 -0
  282. package/src/runtime/index.js.map +1 -0
  283. package/src/runtime/mcp-bridge.d.ts +100 -0
  284. package/src/runtime/mcp-bridge.js +581 -0
  285. package/src/runtime/mcp-bridge.js.map +1 -0
  286. package/src/runtime/renderer-runtime.d.ts +132 -0
  287. package/src/runtime/renderer-runtime.js +389 -0
  288. package/src/runtime/renderer-runtime.js.map +1 -0
  289. package/src/runtime/sanitizer.d.ts +171 -0
  290. package/src/runtime/sanitizer.js +318 -0
  291. package/src/runtime/sanitizer.js.map +1 -0
  292. package/src/runtime/types.d.ts +414 -0
  293. package/src/runtime/types.js +12 -0
  294. package/src/runtime/types.js.map +1 -0
  295. package/src/runtime/wrapper.d.ts +375 -0
  296. package/src/runtime/wrapper.js +1793 -0
  297. package/src/runtime/wrapper.js.map +1 -0
  298. package/src/styles/index.d.ts +7 -0
  299. package/src/styles/index.js +11 -0
  300. package/src/styles/index.js.map +1 -0
  301. package/src/styles/variants.d.ts +50 -0
  302. package/src/styles/variants.js +175 -0
  303. package/src/styles/variants.js.map +1 -0
  304. package/src/theme/cdn.d.ts +194 -0
  305. package/src/theme/cdn.js +375 -0
  306. package/src/theme/cdn.js.map +1 -0
  307. package/src/theme/index.d.ts +17 -0
  308. package/src/theme/index.js +57 -0
  309. package/src/theme/index.js.map +1 -0
  310. package/src/theme/platforms.d.ts +106 -0
  311. package/src/theme/platforms.js +161 -0
  312. package/src/theme/platforms.js.map +1 -0
  313. package/src/theme/presets/github-openai.d.ts +49 -0
  314. package/src/theme/presets/github-openai.js +189 -0
  315. package/src/theme/presets/github-openai.js.map +1 -0
  316. package/src/theme/presets/index.d.ts +10 -0
  317. package/src/theme/presets/index.js +17 -0
  318. package/src/theme/presets/index.js.map +1 -0
  319. package/src/theme/theme.d.ts +395 -0
  320. package/src/theme/theme.js +332 -0
  321. package/src/theme/theme.js.map +1 -0
  322. package/src/tool-template/builder.d.ts +212 -0
  323. package/src/tool-template/builder.js +397 -0
  324. package/src/tool-template/builder.js.map +1 -0
  325. package/src/tool-template/index.d.ts +15 -0
  326. package/src/tool-template/index.js +38 -0
  327. package/src/tool-template/index.js.map +1 -0
  328. package/src/types/index.d.ts +13 -0
  329. package/src/types/index.js +26 -0
  330. package/src/types/index.js.map +1 -0
  331. package/src/types/ui-config.d.ts +357 -0
  332. package/src/types/ui-config.js +12 -0
  333. package/src/types/ui-config.js.map +1 -0
  334. package/src/types/ui-runtime.d.ts +965 -0
  335. package/src/types/ui-runtime.js +117 -0
  336. package/src/types/ui-runtime.js.map +1 -0
  337. package/src/validation/error-box.d.ts +55 -0
  338. package/src/validation/error-box.js +75 -0
  339. package/src/validation/error-box.js.map +1 -0
  340. package/src/validation/index.d.ts +12 -0
  341. package/src/validation/index.js +21 -0
  342. package/src/validation/index.js.map +1 -0
  343. package/src/validation/wrapper.d.ts +96 -0
  344. package/src/validation/wrapper.js +117 -0
  345. package/src/validation/wrapper.js.map +1 -0
  346. package/src/web-components/core/attribute-parser.d.ts +85 -0
  347. package/src/web-components/core/attribute-parser.js +189 -0
  348. package/src/web-components/core/attribute-parser.js.map +1 -0
  349. package/src/web-components/core/base-element.d.ts +197 -0
  350. package/src/web-components/core/base-element.js +289 -0
  351. package/src/web-components/core/base-element.js.map +1 -0
  352. package/src/web-components/core/index.d.ts +8 -0
  353. package/src/web-components/core/index.js +18 -0
  354. package/src/web-components/core/index.js.map +1 -0
  355. package/src/web-components/elements/fmcp-alert.d.ts +45 -0
  356. package/src/web-components/elements/fmcp-alert.js +93 -0
  357. package/src/web-components/elements/fmcp-alert.js.map +1 -0
  358. package/src/web-components/elements/fmcp-badge.d.ts +46 -0
  359. package/src/web-components/elements/fmcp-badge.js +99 -0
  360. package/src/web-components/elements/fmcp-badge.js.map +1 -0
  361. package/src/web-components/elements/fmcp-button.d.ts +124 -0
  362. package/src/web-components/elements/fmcp-button.js +233 -0
  363. package/src/web-components/elements/fmcp-button.js.map +1 -0
  364. package/src/web-components/elements/fmcp-card.d.ts +52 -0
  365. package/src/web-components/elements/fmcp-card.js +115 -0
  366. package/src/web-components/elements/fmcp-card.js.map +1 -0
  367. package/src/web-components/elements/fmcp-input.d.ts +95 -0
  368. package/src/web-components/elements/fmcp-input.js +248 -0
  369. package/src/web-components/elements/fmcp-input.js.map +1 -0
  370. package/src/web-components/elements/fmcp-select.d.ts +99 -0
  371. package/src/web-components/elements/fmcp-select.js +243 -0
  372. package/src/web-components/elements/fmcp-select.js.map +1 -0
  373. package/src/web-components/elements/index.d.ts +12 -0
  374. package/src/web-components/elements/index.js +34 -0
  375. package/src/web-components/elements/index.js.map +1 -0
  376. package/src/web-components/index.d.ts +49 -0
  377. package/src/web-components/index.js +75 -0
  378. package/src/web-components/index.js.map +1 -0
  379. package/src/web-components/register.d.ts +56 -0
  380. package/src/web-components/register.js +80 -0
  381. package/src/web-components/register.js.map +1 -0
  382. package/src/web-components/types.d.ts +121 -0
  383. package/src/web-components/types.js +25 -0
  384. package/src/web-components/types.js.map +1 -0
  385. package/src/widgets/index.d.ts +7 -0
  386. package/src/widgets/index.js +24 -0
  387. package/src/widgets/index.js.map +1 -0
  388. package/src/widgets/progress.d.ts +132 -0
  389. package/src/widgets/progress.js +303 -0
  390. package/src/widgets/progress.js.map +1 -0
  391. package/src/widgets/resource.d.ts +162 -0
  392. package/src/widgets/resource.js +340 -0
  393. package/src/widgets/resource.js.map +1 -0
@@ -0,0 +1,238 @@
1
+ "use strict";
2
+ /**
3
+ * Sandbox Security Policy
4
+ *
5
+ * Defines and validates security policies for bundler execution.
6
+ *
7
+ * @packageDocumentation
8
+ */
9
+ Object.defineProperty(exports, "__esModule", { value: true });
10
+ exports.SecurityError = void 0;
11
+ exports.validateSource = validateSource;
12
+ exports.validateImports = validateImports;
13
+ exports.validateSize = validateSize;
14
+ exports.mergePolicy = mergePolicy;
15
+ exports.throwOnViolations = throwOnViolations;
16
+ const types_1 = require("../types");
17
+ /**
18
+ * Patterns that indicate unsafe code.
19
+ */
20
+ const UNSAFE_PATTERNS = {
21
+ eval: /\beval\s*\(/g,
22
+ functionConstructor: /\bnew\s+Function\s*\(/g,
23
+ dynamicImport: /\bimport\s*\(/g,
24
+ require: /\brequire\s*\(/g,
25
+ processEnv: /\bprocess\.env\b/g,
26
+ globalThis: /\bglobalThis\b/g,
27
+ windowLocation: /\bwindow\.location\b/g,
28
+ documentCookie: /\bdocument\.cookie\b/g,
29
+ innerHTML: /\.innerHTML\s*=/g,
30
+ outerHTML: /\.outerHTML\s*=/g,
31
+ document_write: /\bdocument\.write\s*\(/g,
32
+ };
33
+ /**
34
+ * Validate source code against a security policy.
35
+ *
36
+ * @param source - Source code to validate
37
+ * @param policy - Security policy to enforce
38
+ * @returns Array of security violations (empty if valid)
39
+ *
40
+ * @example
41
+ * ```typescript
42
+ * const violations = validateSource(code, DEFAULT_SECURITY_POLICY);
43
+ * if (violations.length > 0) {
44
+ * throw new Error(`Security violations: ${violations.map(v => v.message).join(', ')}`);
45
+ * }
46
+ * ```
47
+ */
48
+ function validateSource(source, policy = types_1.DEFAULT_SECURITY_POLICY) {
49
+ const violations = [];
50
+ // Check for eval usage
51
+ if (policy.noEval !== false) {
52
+ const evalMatches = [...source.matchAll(UNSAFE_PATTERNS.eval)];
53
+ for (const match of evalMatches) {
54
+ violations.push({
55
+ type: 'eval-usage',
56
+ message: 'eval() is not allowed for security reasons',
57
+ location: getLocation(source, match.index ?? 0),
58
+ value: match[0],
59
+ });
60
+ }
61
+ const fnMatches = [...source.matchAll(UNSAFE_PATTERNS.functionConstructor)];
62
+ for (const match of fnMatches) {
63
+ violations.push({
64
+ type: 'eval-usage',
65
+ message: 'new Function() is not allowed for security reasons',
66
+ location: getLocation(source, match.index ?? 0),
67
+ value: match[0],
68
+ });
69
+ }
70
+ }
71
+ // Check for dynamic imports
72
+ if (policy.noDynamicImports !== false) {
73
+ const matches = [...source.matchAll(UNSAFE_PATTERNS.dynamicImport)];
74
+ for (const match of matches) {
75
+ violations.push({
76
+ type: 'dynamic-import',
77
+ message: 'Dynamic imports are not allowed for security reasons',
78
+ location: getLocation(source, match.index ?? 0),
79
+ value: match[0],
80
+ });
81
+ }
82
+ }
83
+ // Check for require usage
84
+ if (policy.noRequire !== false) {
85
+ const matches = [...source.matchAll(UNSAFE_PATTERNS.require)];
86
+ for (const match of matches) {
87
+ violations.push({
88
+ type: 'require-usage',
89
+ message: 'require() is not allowed for security reasons',
90
+ location: getLocation(source, match.index ?? 0),
91
+ value: match[0],
92
+ });
93
+ }
94
+ }
95
+ // Validate imports
96
+ const importViolations = validateImports(source, policy);
97
+ violations.push(...importViolations);
98
+ return violations;
99
+ }
100
+ /**
101
+ * Validate import statements against policy.
102
+ *
103
+ * @param source - Source code to check
104
+ * @param policy - Security policy
105
+ * @returns Array of import violations
106
+ */
107
+ function validateImports(source, policy = types_1.DEFAULT_SECURITY_POLICY) {
108
+ const violations = [];
109
+ // Extract import statements
110
+ const importPattern = /import\s+(?:(?:\{[^}]*\}|[\w*]+)\s+from\s+)?['"]([^'"]+)['"]/g;
111
+ const imports = [];
112
+ let match;
113
+ while ((match = importPattern.exec(source)) !== null) {
114
+ imports.push({ module: match[1], index: match.index });
115
+ }
116
+ // Also check for require-style imports that might slip through
117
+ const requirePattern = /require\s*\(\s*['"]([^'"]+)['"]\s*\)/g;
118
+ while ((match = requirePattern.exec(source)) !== null) {
119
+ imports.push({ module: match[1], index: match.index });
120
+ }
121
+ for (const imp of imports) {
122
+ // Check blocked imports first
123
+ if (policy.blockedImports) {
124
+ for (const blocked of policy.blockedImports) {
125
+ if (blocked.test(imp.module)) {
126
+ violations.push({
127
+ type: 'blocked-import',
128
+ message: `Import '${imp.module}' is blocked by security policy`,
129
+ location: getLocation(source, imp.index),
130
+ value: imp.module,
131
+ });
132
+ break;
133
+ }
134
+ }
135
+ }
136
+ // Check if import is in allowed list
137
+ if (policy.allowedImports && policy.allowedImports.length > 0) {
138
+ const isAllowed = policy.allowedImports.some((pattern) => pattern.test(imp.module));
139
+ if (!isAllowed) {
140
+ // Check if already reported as blocked
141
+ const alreadyBlocked = violations.some((v) => v.type === 'blocked-import' && v.value === imp.module);
142
+ if (!alreadyBlocked) {
143
+ violations.push({
144
+ type: 'disallowed-import',
145
+ message: `Import '${imp.module}' is not in the allowed imports list`,
146
+ location: getLocation(source, imp.index),
147
+ value: imp.module,
148
+ });
149
+ }
150
+ }
151
+ }
152
+ }
153
+ return violations;
154
+ }
155
+ /**
156
+ * Validate bundle size against policy.
157
+ *
158
+ * @param size - Bundle size in bytes
159
+ * @param policy - Security policy
160
+ * @returns Violation if size exceeds limit, undefined otherwise
161
+ */
162
+ function validateSize(size, policy = types_1.DEFAULT_SECURITY_POLICY) {
163
+ const maxSize = policy.maxBundleSize ?? types_1.DEFAULT_SECURITY_POLICY.maxBundleSize ?? 512000;
164
+ if (size > maxSize) {
165
+ return {
166
+ type: 'size-exceeded',
167
+ message: `Bundle size (${formatBytes(size)}) exceeds maximum allowed (${formatBytes(maxSize)})`,
168
+ value: String(size),
169
+ };
170
+ }
171
+ return undefined;
172
+ }
173
+ /**
174
+ * Create a merged security policy with defaults.
175
+ *
176
+ * @param userPolicy - User-provided policy overrides
177
+ * @returns Merged policy with defaults
178
+ */
179
+ function mergePolicy(userPolicy) {
180
+ if (!userPolicy) {
181
+ return { ...types_1.DEFAULT_SECURITY_POLICY };
182
+ }
183
+ return {
184
+ allowedImports: userPolicy.allowedImports ?? types_1.DEFAULT_SECURITY_POLICY.allowedImports,
185
+ blockedImports: userPolicy.blockedImports ?? types_1.DEFAULT_SECURITY_POLICY.blockedImports,
186
+ maxBundleSize: userPolicy.maxBundleSize ?? types_1.DEFAULT_SECURITY_POLICY.maxBundleSize,
187
+ maxTransformTime: userPolicy.maxTransformTime ?? types_1.DEFAULT_SECURITY_POLICY.maxTransformTime,
188
+ noEval: userPolicy.noEval ?? types_1.DEFAULT_SECURITY_POLICY.noEval,
189
+ noDynamicImports: userPolicy.noDynamicImports ?? types_1.DEFAULT_SECURITY_POLICY.noDynamicImports,
190
+ noRequire: userPolicy.noRequire ?? types_1.DEFAULT_SECURITY_POLICY.noRequire,
191
+ allowedGlobals: userPolicy.allowedGlobals ?? types_1.DEFAULT_SECURITY_POLICY.allowedGlobals,
192
+ };
193
+ }
194
+ /**
195
+ * Get line and column from source index.
196
+ */
197
+ function getLocation(source, index) {
198
+ const lines = source.slice(0, index).split('\n');
199
+ return {
200
+ line: lines.length,
201
+ column: lines[lines.length - 1].length + 1,
202
+ };
203
+ }
204
+ /**
205
+ * Format bytes to human-readable string.
206
+ */
207
+ function formatBytes(bytes) {
208
+ if (bytes < 1024)
209
+ return `${bytes} B`;
210
+ if (bytes < 1024 * 1024)
211
+ return `${(bytes / 1024).toFixed(1)} KB`;
212
+ return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
213
+ }
214
+ /**
215
+ * Security error thrown when policy is violated.
216
+ */
217
+ class SecurityError extends Error {
218
+ violations;
219
+ constructor(message, violations) {
220
+ super(message);
221
+ this.name = 'SecurityError';
222
+ this.violations = violations;
223
+ }
224
+ }
225
+ exports.SecurityError = SecurityError;
226
+ /**
227
+ * Throw if any violations exist.
228
+ *
229
+ * @param violations - Array of violations to check
230
+ * @throws SecurityError if violations exist
231
+ */
232
+ function throwOnViolations(violations) {
233
+ if (violations.length > 0) {
234
+ const message = violations.map((v) => v.message).join('; ');
235
+ throw new SecurityError(`Security policy violation: ${message}`, violations);
236
+ }
237
+ }
238
+ //# sourceMappingURL=policy.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"policy.js","sourceRoot":"","sources":["../../../../src/bundler/sandbox/policy.ts"],"names":[],"mappings":";AAAA;;;;;;GAMG;;;AAqCH,wCAyDC;AASD,0CAqDC;AASD,oCAeC;AAQD,kCAeC;AAyCD,8CAKC;AAtPD,oCAAmD;AAEnD;;GAEG;AACH,MAAM,eAAe,GAAG;IACtB,IAAI,EAAE,cAAc;IACpB,mBAAmB,EAAE,wBAAwB;IAC7C,aAAa,EAAE,gBAAgB;IAC/B,OAAO,EAAE,iBAAiB;IAC1B,UAAU,EAAE,mBAAmB;IAC/B,UAAU,EAAE,iBAAiB;IAC7B,cAAc,EAAE,uBAAuB;IACvC,cAAc,EAAE,uBAAuB;IACvC,SAAS,EAAE,kBAAkB;IAC7B,SAAS,EAAE,kBAAkB;IAC7B,cAAc,EAAE,yBAAyB;CAC1C,CAAC;AAEF;;;;;;;;;;;;;;GAcG;AACH,SAAgB,cAAc,CAAC,MAAc,EAAE,SAAyB,+BAAuB;IAC7F,MAAM,UAAU,GAAwB,EAAE,CAAC;IAE3C,uBAAuB;IACvB,IAAI,MAAM,CAAC,MAAM,KAAK,KAAK,EAAE,CAAC;QAC5B,MAAM,WAAW,GAAG,CAAC,GAAG,MAAM,CAAC,QAAQ,CAAC,eAAe,CAAC,IAAI,CAAC,CAAC,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C,IAAI,CAAC,IAAI,CAAC,CAAC;QAC5D,MAAM,IAAI,aAAa,CAAC,8BAA8B,OAAO,EAAE,EAAE,UAAU,CAAC,CAAC;IAC/E,CAAC;AACH,CAAC","sourcesContent":["/**\n * Sandbox Security Policy\n *\n * Defines and validates security policies for bundler execution.\n *\n * @packageDocumentation\n */\n\nimport type { SecurityPolicy, SecurityViolation } from '../types';\nimport { DEFAULT_SECURITY_POLICY } from '../types';\n\n/**\n * Patterns that indicate unsafe code.\n */\nconst UNSAFE_PATTERNS = {\n eval: /\\beval\\s*\\(/g,\n functionConstructor: /\\bnew\\s+Function\\s*\\(/g,\n dynamicImport: /\\bimport\\s*\\(/g,\n require: /\\brequire\\s*\\(/g,\n processEnv: /\\bprocess\\.env\\b/g,\n globalThis: /\\bglobalThis\\b/g,\n windowLocation: /\\bwindow\\.location\\b/g,\n documentCookie: /\\bdocument\\.cookie\\b/g,\n innerHTML: /\\.innerHTML\\s*=/g,\n outerHTML: /\\.outerHTML\\s*=/g,\n document_write: /\\bdocument\\.write\\s*\\(/g,\n};\n\n/**\n * Validate source code against a security policy.\n *\n * @param source - Source code to validate\n * @param policy - Security policy to enforce\n * @returns Array of security violations (empty if valid)\n *\n * @example\n * ```typescript\n * const violations = validateSource(code, DEFAULT_SECURITY_POLICY);\n * if (violations.length > 0) {\n * throw new Error(`Security violations: ${violations.map(v => v.message).join(', ')}`);\n * }\n * ```\n */\nexport function validateSource(source: string, policy: SecurityPolicy = DEFAULT_SECURITY_POLICY): SecurityViolation[] {\n const violations: SecurityViolation[] = [];\n\n // Check for eval usage\n if (policy.noEval !== false) {\n const evalMatches = [...source.matchAll(UNSAFE_PATTERNS.eval)];\n for (const match of evalMatches) {\n violations.push({\n type: 'eval-usage',\n message: 'eval() is not allowed for security reasons',\n location: getLocation(source, match.index ?? 0),\n value: match[0],\n });\n }\n\n const fnMatches = [...source.matchAll(UNSAFE_PATTERNS.functionConstructor)];\n for (const match of fnMatches) {\n violations.push({\n type: 'eval-usage',\n message: 'new Function() is not allowed for security reasons',\n location: getLocation(source, match.index ?? 0),\n value: match[0],\n });\n }\n }\n\n // Check for dynamic imports\n if (policy.noDynamicImports !== false) {\n const matches = [...source.matchAll(UNSAFE_PATTERNS.dynamicImport)];\n for (const match of matches) {\n violations.push({\n type: 'dynamic-import',\n message: 'Dynamic imports are not allowed for security reasons',\n location: getLocation(source, match.index ?? 0),\n value: match[0],\n });\n }\n }\n\n // Check for require usage\n if (policy.noRequire !== false) {\n const matches = [...source.matchAll(UNSAFE_PATTERNS.require)];\n for (const match of matches) {\n violations.push({\n type: 'require-usage',\n message: 'require() is not allowed for security reasons',\n location: getLocation(source, match.index ?? 0),\n value: match[0],\n });\n }\n }\n\n // Validate imports\n const importViolations = validateImports(source, policy);\n violations.push(...importViolations);\n\n return violations;\n}\n\n/**\n * Validate import statements against policy.\n *\n * @param source - Source code to check\n * @param policy - Security policy\n * @returns Array of import violations\n */\nexport function validateImports(source: string, policy: SecurityPolicy = DEFAULT_SECURITY_POLICY): SecurityViolation[] {\n const violations: SecurityViolation[] = [];\n\n // Extract import statements\n const importPattern = /import\\s+(?:(?:\\{[^}]*\\}|[\\w*]+)\\s+from\\s+)?['\"]([^'\"]+)['\"]/g;\n const imports: Array<{ module: string; index: number }> = [];\n\n let match;\n while ((match = importPattern.exec(source)) !== null) {\n imports.push({ module: match[1], index: match.index });\n }\n\n // Also check for require-style imports that might slip through\n const requirePattern = /require\\s*\\(\\s*['\"]([^'\"]+)['\"]\\s*\\)/g;\n while ((match = requirePattern.exec(source)) !== null) {\n imports.push({ module: match[1], index: match.index });\n }\n\n for (const imp of imports) {\n // Check blocked imports first\n if (policy.blockedImports) {\n for (const blocked of policy.blockedImports) {\n if (blocked.test(imp.module)) {\n violations.push({\n type: 'blocked-import',\n message: `Import '${imp.module}' is blocked by security policy`,\n location: getLocation(source, imp.index),\n value: imp.module,\n });\n break;\n }\n }\n }\n\n // Check if import is in allowed list\n if (policy.allowedImports && policy.allowedImports.length > 0) {\n const isAllowed = policy.allowedImports.some((pattern) => pattern.test(imp.module));\n if (!isAllowed) {\n // Check if already reported as blocked\n const alreadyBlocked = violations.some((v) => v.type === 'blocked-import' && v.value === imp.module);\n if (!alreadyBlocked) {\n violations.push({\n type: 'disallowed-import',\n message: `Import '${imp.module}' is not in the allowed imports list`,\n location: getLocation(source, imp.index),\n value: imp.module,\n });\n }\n }\n }\n }\n\n return violations;\n}\n\n/**\n * Validate bundle size against policy.\n *\n * @param size - Bundle size in bytes\n * @param policy - Security policy\n * @returns Violation if size exceeds limit, undefined otherwise\n */\nexport function validateSize(\n size: number,\n policy: SecurityPolicy = DEFAULT_SECURITY_POLICY,\n): SecurityViolation | undefined {\n const maxSize = policy.maxBundleSize ?? DEFAULT_SECURITY_POLICY.maxBundleSize ?? 512000;\n\n if (size > maxSize) {\n return {\n type: 'size-exceeded',\n message: `Bundle size (${formatBytes(size)}) exceeds maximum allowed (${formatBytes(maxSize)})`,\n value: String(size),\n };\n }\n\n return undefined;\n}\n\n/**\n * Create a merged security policy with defaults.\n *\n * @param userPolicy - User-provided policy overrides\n * @returns Merged policy with defaults\n */\nexport function mergePolicy(userPolicy?: Partial<SecurityPolicy>): SecurityPolicy {\n if (!userPolicy) {\n return { ...DEFAULT_SECURITY_POLICY };\n }\n\n return {\n allowedImports: userPolicy.allowedImports ?? DEFAULT_SECURITY_POLICY.allowedImports,\n blockedImports: userPolicy.blockedImports ?? DEFAULT_SECURITY_POLICY.blockedImports,\n maxBundleSize: userPolicy.maxBundleSize ?? DEFAULT_SECURITY_POLICY.maxBundleSize,\n maxTransformTime: userPolicy.maxTransformTime ?? DEFAULT_SECURITY_POLICY.maxTransformTime,\n noEval: userPolicy.noEval ?? DEFAULT_SECURITY_POLICY.noEval,\n noDynamicImports: userPolicy.noDynamicImports ?? DEFAULT_SECURITY_POLICY.noDynamicImports,\n noRequire: userPolicy.noRequire ?? DEFAULT_SECURITY_POLICY.noRequire,\n allowedGlobals: userPolicy.allowedGlobals ?? DEFAULT_SECURITY_POLICY.allowedGlobals,\n };\n}\n\n/**\n * Get line and column from source index.\n */\nfunction getLocation(source: string, index: number): { line: number; column: number } {\n const lines = source.slice(0, index).split('\\n');\n return {\n line: lines.length,\n column: lines[lines.length - 1].length + 1,\n };\n}\n\n/**\n * Format bytes to human-readable string.\n */\nfunction formatBytes(bytes: number): string {\n if (bytes < 1024) return `${bytes} B`;\n if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;\n return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;\n}\n\n/**\n * Security error thrown when policy is violated.\n */\nexport class SecurityError extends Error {\n readonly violations: SecurityViolation[];\n\n constructor(message: string, violations: SecurityViolation[]) {\n super(message);\n this.name = 'SecurityError';\n this.violations = violations;\n }\n}\n\n/**\n * Throw if any violations exist.\n *\n * @param violations - Array of violations to check\n * @throws SecurityError if violations exist\n */\nexport function throwOnViolations(violations: SecurityViolation[]): void {\n if (violations.length > 0) {\n const message = violations.map((v) => v.message).join('; ');\n throw new SecurityError(`Security policy violation: ${message}`, violations);\n }\n}\n"]}
@@ -0,0 +1,347 @@
1
+ /**
2
+ * Bundler Types
3
+ *
4
+ * Type definitions for the in-memory bundler system.
5
+ *
6
+ * @packageDocumentation
7
+ */
8
+ /**
9
+ * Source type for bundler input.
10
+ */
11
+ export type SourceType = 'jsx' | 'tsx' | 'mdx' | 'html' | 'auto';
12
+ /**
13
+ * Output format for bundles.
14
+ */
15
+ export type OutputFormat = 'iife' | 'esm' | 'cjs';
16
+ /**
17
+ * Options for bundling source code.
18
+ */
19
+ export interface BundleOptions {
20
+ /**
21
+ * Source code to bundle.
22
+ */
23
+ source: string;
24
+ /**
25
+ * Source type (jsx, tsx, mdx, html, or auto).
26
+ * @default 'auto'
27
+ */
28
+ sourceType?: SourceType;
29
+ /**
30
+ * Output format.
31
+ * @default 'iife'
32
+ */
33
+ format?: OutputFormat;
34
+ /**
35
+ * Minify output code.
36
+ * @default false
37
+ */
38
+ minify?: boolean;
39
+ /**
40
+ * Generate source maps.
41
+ * @default false
42
+ */
43
+ sourceMaps?: boolean | 'inline';
44
+ /**
45
+ * External modules (not bundled).
46
+ * @default ['react', 'react-dom']
47
+ */
48
+ externals?: string[];
49
+ /**
50
+ * JSX configuration.
51
+ */
52
+ jsx?: {
53
+ /**
54
+ * JSX runtime mode.
55
+ * @default 'automatic'
56
+ */
57
+ runtime?: 'automatic' | 'classic';
58
+ /**
59
+ * Import source for automatic runtime.
60
+ * @default 'react'
61
+ */
62
+ importSource?: string;
63
+ };
64
+ /**
65
+ * Security policy for bundling.
66
+ */
67
+ security?: SecurityPolicy;
68
+ /**
69
+ * Target environment.
70
+ * @default 'es2020'
71
+ */
72
+ target?: string;
73
+ /**
74
+ * Global variable name for IIFE format.
75
+ * @default 'Widget'
76
+ */
77
+ globalName?: string;
78
+ /**
79
+ * Cache key override. If not provided, computed from source hash.
80
+ */
81
+ cacheKey?: string;
82
+ /**
83
+ * Skip cache lookup.
84
+ * @default false
85
+ */
86
+ skipCache?: boolean;
87
+ }
88
+ /**
89
+ * Options specifically for SSR bundling.
90
+ */
91
+ export interface SSRBundleOptions extends BundleOptions {
92
+ /**
93
+ * Context data to inject during SSR.
94
+ */
95
+ context?: Record<string, unknown>;
96
+ /**
97
+ * Component export name to render.
98
+ * @default 'default'
99
+ */
100
+ componentExport?: string;
101
+ /**
102
+ * Whether to include hydration script.
103
+ * @default false
104
+ */
105
+ includeHydration?: boolean;
106
+ }
107
+ /**
108
+ * Result of a bundle operation.
109
+ */
110
+ export interface BundleResult {
111
+ /**
112
+ * Bundled/transformed code.
113
+ */
114
+ code: string;
115
+ /**
116
+ * Content hash of the bundle.
117
+ */
118
+ hash: string;
119
+ /**
120
+ * Whether result was served from cache.
121
+ */
122
+ cached: boolean;
123
+ /**
124
+ * Bundle size in bytes.
125
+ */
126
+ size: number;
127
+ /**
128
+ * Source map (if generated).
129
+ */
130
+ map?: string;
131
+ /**
132
+ * Performance metrics.
133
+ */
134
+ metrics: BundleMetrics;
135
+ /**
136
+ * Detected or specified source type.
137
+ */
138
+ sourceType: SourceType;
139
+ /**
140
+ * Output format used.
141
+ */
142
+ format: OutputFormat;
143
+ }
144
+ /**
145
+ * Performance metrics for bundling.
146
+ */
147
+ export interface BundleMetrics {
148
+ /**
149
+ * Time to transform source (ms).
150
+ */
151
+ transformTime: number;
152
+ /**
153
+ * Time to bundle (ms).
154
+ */
155
+ bundleTime: number;
156
+ /**
157
+ * Total processing time (ms).
158
+ */
159
+ totalTime: number;
160
+ /**
161
+ * Cache lookup time (ms).
162
+ */
163
+ cacheTime?: number;
164
+ }
165
+ /**
166
+ * Result of SSR rendering.
167
+ */
168
+ export interface SSRResult extends BundleResult {
169
+ /**
170
+ * Rendered HTML output.
171
+ */
172
+ html: string;
173
+ /**
174
+ * Hydration script (if included).
175
+ */
176
+ hydrationScript?: string;
177
+ /**
178
+ * SSR-specific metrics.
179
+ */
180
+ ssrMetrics: {
181
+ /**
182
+ * Time to render component (ms).
183
+ */
184
+ renderTime: number;
185
+ };
186
+ }
187
+ /**
188
+ * Security policy for bundler execution.
189
+ */
190
+ export interface SecurityPolicy {
191
+ /**
192
+ * Allowed import patterns (regex).
193
+ * @default [/^react/, /^@frontmcp\/ui/]
194
+ */
195
+ allowedImports?: RegExp[];
196
+ /**
197
+ * Blocked import patterns (regex).
198
+ * @default [/^fs/, /^net/, /^child_process/, /^os/, /^path/]
199
+ */
200
+ blockedImports?: RegExp[];
201
+ /**
202
+ * Maximum bundle size in bytes.
203
+ * @default 512000 (500KB)
204
+ */
205
+ maxBundleSize?: number;
206
+ /**
207
+ * Maximum transform time in ms.
208
+ * @default 5000 (5s)
209
+ */
210
+ maxTransformTime?: number;
211
+ /**
212
+ * Block eval() and Function() usage.
213
+ * @default true
214
+ */
215
+ noEval?: boolean;
216
+ /**
217
+ * Block dynamic imports.
218
+ * @default true
219
+ */
220
+ noDynamicImports?: boolean;
221
+ /**
222
+ * Block require() usage.
223
+ * @default true
224
+ */
225
+ noRequire?: boolean;
226
+ /**
227
+ * Allowed global variables.
228
+ * @default ['console', 'Math', 'JSON', 'Date', 'Array', 'Object', 'String', 'Number', 'Boolean', 'Promise', 'Map', 'Set', 'WeakMap', 'WeakSet', 'Symbol', 'Reflect', 'Proxy', 'Error', 'TypeError', 'RangeError', 'parseInt', 'parseFloat', 'isNaN', 'isFinite', 'encodeURI', 'encodeURIComponent', 'decodeURI', 'decodeURIComponent', 'setTimeout', 'clearTimeout', 'setInterval', 'clearInterval']
229
+ */
230
+ allowedGlobals?: string[];
231
+ }
232
+ /**
233
+ * Security violation details.
234
+ */
235
+ export interface SecurityViolation {
236
+ /**
237
+ * Type of violation.
238
+ */
239
+ type: 'blocked-import' | 'disallowed-import' | 'eval-usage' | 'dynamic-import' | 'require-usage' | 'size-exceeded' | 'timeout' | 'blocked-global';
240
+ /**
241
+ * Human-readable message.
242
+ */
243
+ message: string;
244
+ /**
245
+ * Location in source (if available).
246
+ */
247
+ location?: {
248
+ line: number;
249
+ column: number;
250
+ };
251
+ /**
252
+ * Offending pattern/value.
253
+ */
254
+ value?: string;
255
+ }
256
+ /**
257
+ * Configuration options for creating a bundler instance.
258
+ */
259
+ export interface BundlerOptions {
260
+ /**
261
+ * Default security policy.
262
+ */
263
+ defaultSecurity?: SecurityPolicy;
264
+ /**
265
+ * Cache configuration.
266
+ */
267
+ cache?: {
268
+ /**
269
+ * Maximum number of cached entries.
270
+ * @default 100
271
+ */
272
+ maxSize?: number;
273
+ /**
274
+ * TTL for cache entries in ms.
275
+ * @default 300000 (5 minutes)
276
+ */
277
+ ttl?: number;
278
+ /**
279
+ * Disable caching entirely.
280
+ * @default false
281
+ */
282
+ disabled?: boolean;
283
+ };
284
+ /**
285
+ * Enable verbose logging.
286
+ * @default false
287
+ */
288
+ verbose?: boolean;
289
+ /**
290
+ * Custom esbuild options.
291
+ */
292
+ esbuildOptions?: Record<string, unknown>;
293
+ }
294
+ /**
295
+ * Cache entry for bundled results.
296
+ */
297
+ export interface CacheEntry {
298
+ /**
299
+ * Bundle result.
300
+ */
301
+ result: BundleResult;
302
+ /**
303
+ * Creation timestamp.
304
+ */
305
+ createdAt: number;
306
+ /**
307
+ * Last access timestamp.
308
+ */
309
+ lastAccessedAt: number;
310
+ /**
311
+ * Access count.
312
+ */
313
+ accessCount: number;
314
+ }
315
+ /**
316
+ * Context for transform operations.
317
+ */
318
+ export interface TransformContext {
319
+ /**
320
+ * Source type being transformed.
321
+ */
322
+ sourceType: SourceType;
323
+ /**
324
+ * File path (for source maps).
325
+ */
326
+ filename?: string;
327
+ /**
328
+ * Source code.
329
+ */
330
+ source: string;
331
+ /**
332
+ * Active security policy.
333
+ */
334
+ security: SecurityPolicy;
335
+ }
336
+ /**
337
+ * Default security policy.
338
+ */
339
+ export declare const DEFAULT_SECURITY_POLICY: SecurityPolicy;
340
+ /**
341
+ * Default bundle options.
342
+ */
343
+ export declare const DEFAULT_BUNDLE_OPTIONS: Required<Pick<BundleOptions, 'sourceType' | 'format' | 'minify' | 'sourceMaps' | 'externals' | 'jsx' | 'target' | 'globalName' | 'skipCache'>>;
344
+ /**
345
+ * Default bundler options.
346
+ */
347
+ export declare const DEFAULT_BUNDLER_OPTIONS: Required<BundlerOptions>;