@vizhub/runtime 0.1.4 → 0.2.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 (287) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +240 -194
  3. package/dist/common/packageJson.d.ts +39 -0
  4. package/dist/common/packageJson.js +71 -0
  5. package/dist/common/packageJson.test.d.ts +1 -0
  6. package/dist/common/packageJson.test.js +146 -0
  7. package/dist/common/sucrasePlugin.d.ts +2 -0
  8. package/dist/common/sucrasePlugin.js +24 -0
  9. package/dist/common/virtualFileSystem.d.ts +3 -0
  10. package/dist/common/virtualFileSystem.js +75 -0
  11. package/dist/determineRuntimeVersion.d.ts +2 -0
  12. package/dist/determineRuntimeVersion.js +22 -0
  13. package/dist/determineRuntimeVersion.test.d.ts +1 -0
  14. package/dist/determineRuntimeVersion.test.js +101 -0
  15. package/dist/fixtures/basicHTML.d.ts +2 -0
  16. package/dist/fixtures/basicHTML.js +22 -0
  17. package/dist/fixtures/fetchProxy.d.ts +2 -0
  18. package/dist/fixtures/fetchProxy.js +25 -0
  19. package/dist/fixtures/index.d.ts +3 -0
  20. package/dist/fixtures/index.js +9 -0
  21. package/dist/fixtures/jsScriptTag.d.ts +2 -0
  22. package/dist/fixtures/jsScriptTag.js +19 -0
  23. package/dist/index.d.ts +14 -8
  24. package/dist/index.js +59 -24882
  25. package/dist/index.test.d.ts +1 -0
  26. package/dist/index.test.js +50 -0
  27. package/dist/magicSandbox/fixProtocollessUrls.d.ts +4 -0
  28. package/dist/magicSandbox/fixProtocollessUrls.js +10 -0
  29. package/dist/magicSandbox/generateInterceptorScript.d.ts +4 -0
  30. package/dist/magicSandbox/generateInterceptorScript.js +178 -0
  31. package/dist/magicSandbox/index.d.ts +19 -0
  32. package/dist/magicSandbox/index.js +48 -0
  33. package/dist/test/fixtures/basicHTML.d.ts +2 -0
  34. package/dist/test/fixtures/basicHTML.js +19 -0
  35. package/dist/test/fixtures/fetchProxy.d.ts +2 -0
  36. package/dist/test/fixtures/fetchProxy.js +22 -0
  37. package/dist/test/fixtures/index.d.ts +6 -0
  38. package/dist/test/fixtures/index.js +6 -0
  39. package/dist/test/fixtures/jsScriptTag.d.ts +2 -0
  40. package/dist/test/fixtures/jsScriptTag.js +16 -0
  41. package/dist/test/fixtures/magicSandbox/basicHTML.d.ts +5 -0
  42. package/dist/test/fixtures/magicSandbox/basicHTML.js +17 -0
  43. package/dist/test/fixtures/magicSandbox/fetchProxy.d.ts +11 -0
  44. package/dist/test/fixtures/magicSandbox/fetchProxy.js +18 -0
  45. package/dist/test/fixtures/magicSandbox/index.d.ts +6 -0
  46. package/dist/test/fixtures/magicSandbox/index.js +6 -0
  47. package/dist/test/fixtures/magicSandbox/jsScriptTag.d.ts +8 -0
  48. package/dist/test/fixtures/magicSandbox/jsScriptTag.js +13 -0
  49. package/dist/test/fixtures/magicSandbox/protocolTest.d.ts +5 -0
  50. package/dist/test/fixtures/magicSandbox/protocolTest.js +17 -0
  51. package/dist/test/fixtures/magicSandbox/styleTest.d.ts +8 -0
  52. package/dist/test/fixtures/magicSandbox/styleTest.js +19 -0
  53. package/dist/test/fixtures/magicSandbox/xmlTest.d.ts +8 -0
  54. package/dist/test/fixtures/magicSandbox/xmlTest.js +23 -0
  55. package/dist/test/fixtures/protocolTest.d.ts +2 -0
  56. package/dist/test/fixtures/protocolTest.js +19 -0
  57. package/dist/test/fixtures/styleTest.d.ts +2 -0
  58. package/dist/test/fixtures/styleTest.js +22 -0
  59. package/dist/test/fixtures/v1/basicHTML.d.ts +3 -0
  60. package/dist/test/fixtures/v1/basicHTML.js +15 -0
  61. package/dist/test/fixtures/v1/fetchProxy.d.ts +5 -0
  62. package/dist/test/fixtures/v1/fetchProxy.js +12 -0
  63. package/dist/test/fixtures/v1/index.d.ts +6 -0
  64. package/dist/test/fixtures/v1/index.js +6 -0
  65. package/dist/test/fixtures/v1/jsScriptTag.d.ts +4 -0
  66. package/dist/test/fixtures/v1/jsScriptTag.js +9 -0
  67. package/dist/test/fixtures/v1/protocolTest.d.ts +3 -0
  68. package/dist/test/fixtures/v1/protocolTest.js +15 -0
  69. package/dist/test/fixtures/v1/styleTest.d.ts +4 -0
  70. package/dist/test/fixtures/v1/styleTest.js +15 -0
  71. package/dist/test/fixtures/v1/xmlTest.d.ts +4 -0
  72. package/dist/test/fixtures/v1/xmlTest.js +19 -0
  73. package/dist/test/fixtures/v2/basicBundle.d.ts +5 -0
  74. package/dist/test/fixtures/v2/basicBundle.js +5 -0
  75. package/dist/test/fixtures/v2/basicBundleNoExtension.d.ts +5 -0
  76. package/dist/test/fixtures/v2/basicBundleNoExtension.js +5 -0
  77. package/dist/test/fixtures/v2/d3Import.d.ts +4 -0
  78. package/dist/test/fixtures/v2/d3Import.js +7 -0
  79. package/dist/test/fixtures/v2/d3ImportPkg.d.ts +5 -0
  80. package/dist/test/fixtures/v2/d3ImportPkg.js +19 -0
  81. package/dist/test/fixtures/v2/d3RosettaImportPkg.d.ts +5 -0
  82. package/dist/test/fixtures/v2/d3RosettaImportPkg.js +19 -0
  83. package/dist/test/fixtures/v2/es6Preserve.d.ts +4 -0
  84. package/dist/test/fixtures/v2/es6Preserve.js +6 -0
  85. package/dist/test/fixtures/v2/generatorSupport.d.ts +4 -0
  86. package/dist/test/fixtures/v2/generatorSupport.js +6 -0
  87. package/dist/test/fixtures/v2/index.d.ts +14 -0
  88. package/dist/test/fixtures/v2/index.js +15 -0
  89. package/dist/test/fixtures/v2/jsxTranspile.d.ts +4 -0
  90. package/dist/test/fixtures/v2/jsxTranspile.js +6 -0
  91. package/dist/test/fixtures/v2/reactDomImport.d.ts +4 -0
  92. package/dist/test/fixtures/v2/reactDomImport.js +8 -0
  93. package/dist/test/fixtures/v2/reactDomImportPkg.d.ts +5 -0
  94. package/dist/test/fixtures/v2/reactDomImportPkg.js +24 -0
  95. package/dist/test/fixtures/v2/reactImport.d.ts +4 -0
  96. package/dist/test/fixtures/v2/reactImport.js +7 -0
  97. package/dist/test/fixtures/v2/reactImportPkg.d.ts +5 -0
  98. package/dist/test/fixtures/v2/reactImportPkg.js +17 -0
  99. package/dist/test/fixtures/v2/sourceMapErrorFixture.d.ts +5 -0
  100. package/dist/test/fixtures/v2/sourceMapErrorFixture.js +19 -0
  101. package/dist/test/fixtures/v2/unicodeSupport.d.ts +4 -0
  102. package/dist/test/fixtures/v2/unicodeSupport.js +6 -0
  103. package/dist/test/fixtures/v3/basicIndexJS.d.ts +3 -0
  104. package/dist/test/fixtures/v3/basicIndexJS.js +7 -0
  105. package/dist/test/fixtures/v3/cssImport.d.ts +4 -0
  106. package/dist/test/fixtures/v3/cssImport.js +9 -0
  107. package/dist/test/fixtures/v3/csvImport.d.ts +4 -0
  108. package/dist/test/fixtures/v3/csvImport.js +11 -0
  109. package/dist/test/fixtures/v3/csvStrangeChars.d.ts +4 -0
  110. package/dist/test/fixtures/v3/csvStrangeChars.js +12 -0
  111. package/dist/test/fixtures/v3/index.d.ts +9 -0
  112. package/dist/test/fixtures/v3/index.js +9 -0
  113. package/dist/test/fixtures/v3/jsExport.d.ts +4 -0
  114. package/dist/test/fixtures/v3/jsExport.js +10 -0
  115. package/dist/test/fixtures/v3/sourcemap.d.ts +3 -0
  116. package/dist/test/fixtures/v3/sourcemap.js +10 -0
  117. package/dist/test/fixtures/v3/svelte.d.ts +4 -0
  118. package/dist/test/fixtures/v3/svelte.js +18 -0
  119. package/dist/test/fixtures/v3/vizContent.d.ts +2 -0
  120. package/dist/test/fixtures/v3/vizContent.js +11 -0
  121. package/dist/test/fixtures/v3/vizImport.d.ts +6 -0
  122. package/dist/test/fixtures/v3/vizImport.js +85 -0
  123. package/dist/test/fixtures/v4/d3Usage.d.ts +5 -0
  124. package/dist/test/fixtures/v4/d3Usage.js +31 -0
  125. package/dist/test/fixtures/v4/esmBuild.d.ts +5 -0
  126. package/dist/test/fixtures/v4/esmBuild.js +18 -0
  127. package/dist/test/fixtures/v4/fetchInterception.d.ts +5 -0
  128. package/dist/test/fixtures/v4/fetchInterception.js +18 -0
  129. package/dist/test/fixtures/v4/index.d.ts +9 -0
  130. package/dist/test/fixtures/v4/index.js +9 -0
  131. package/dist/test/fixtures/v4/jsScriptTagTypeModule.d.ts +4 -0
  132. package/dist/test/fixtures/v4/jsScriptTagTypeModule.js +18 -0
  133. package/dist/test/fixtures/v4/jsScriptTagTypeModules.d.ts +5 -0
  134. package/dist/test/fixtures/v4/jsScriptTagTypeModules.js +12 -0
  135. package/dist/test/fixtures/v4/reactHooks.d.ts +6 -0
  136. package/dist/test/fixtures/v4/reactHooks.js +47 -0
  137. package/dist/test/fixtures/v4/reactJsx.d.ts +6 -0
  138. package/dist/test/fixtures/v4/reactJsx.js +34 -0
  139. package/dist/test/fixtures/v4/threeJsUsage.d.ts +4 -0
  140. package/dist/test/fixtures/v4/threeJsUsage.js +24 -0
  141. package/dist/test/fixtures/v4/typeScriptSupport.d.ts +5 -0
  142. package/dist/test/fixtures/v4/typeScriptSupport.js +29 -0
  143. package/dist/test/fixtures/xmlTest.d.ts +2 -0
  144. package/dist/test/fixtures/xmlTest.js +26 -0
  145. package/dist/test/index.test.d.ts +1 -0
  146. package/dist/test/index.test.js +40 -0
  147. package/dist/test/testInBrowser.d.ts +16 -0
  148. package/dist/test/testInBrowser.js +47 -0
  149. package/dist/test/testStackTrace.d.ts +9 -0
  150. package/dist/test/testStackTrace.js +55 -0
  151. package/dist/test/v1.test.d.ts +1 -0
  152. package/dist/test/v1.test.js +65 -0
  153. package/dist/test/v2.test.d.ts +1 -0
  154. package/dist/test/v2.test.js +121 -0
  155. package/dist/test/v3.test.d.ts +1 -0
  156. package/dist/test/v3.test.js +114 -0
  157. package/dist/test/v4.test.d.ts +1 -0
  158. package/dist/test/v4.test.js +100 -0
  159. package/dist/types.d.ts +2 -0
  160. package/dist/types.js +1 -0
  161. package/dist/utils/getFileText.d.ts +2 -0
  162. package/dist/utils/getFileText.js +13 -0
  163. package/dist/utils/getFileText.test.d.ts +1 -0
  164. package/dist/utils/getFileText.test.js +72 -0
  165. package/dist/utils/vizContentToFileCollection.d.ts +8 -0
  166. package/dist/utils/vizContentToFileCollection.js +19 -0
  167. package/dist/utils/vizContentToFileCollection.test.d.ts +1 -0
  168. package/dist/utils/vizContentToFileCollection.test.js +75 -0
  169. package/dist/v2/computeBundleJSV2.d.ts +7 -0
  170. package/dist/v2/computeBundleJSV2.js +29 -0
  171. package/dist/v2/getComputedIndexHtml.d.ts +4 -0
  172. package/dist/v2/getComputedIndexHtml.js +81 -0
  173. package/dist/v2/getComputedIndexHtml.test.d.ts +1 -0
  174. package/dist/v2/getComputedIndexHtml.test.js +134 -0
  175. package/dist/v2/index.d.ts +1 -0
  176. package/dist/v2/index.js +1 -0
  177. package/dist/v2/v2Build.d.ts +7 -0
  178. package/dist/v2/v2Build.js +11 -0
  179. package/dist/v3/computeBundleJSV3.d.ts +18 -0
  180. package/dist/v3/computeBundleJSV3.js +48 -0
  181. package/dist/v3/createVizContent.d.ts +9 -0
  182. package/dist/v3/createVizContent.js +23 -0
  183. package/dist/{v3Runtime → v3}/extractVizImport.d.ts +2 -3
  184. package/dist/v3/extractVizImport.js +23 -0
  185. package/dist/v3/extractVizImport.test.d.ts +1 -0
  186. package/dist/v3/extractVizImport.test.js +30 -0
  187. package/dist/v3/htmlTemplate.d.ts +5 -0
  188. package/dist/v3/htmlTemplate.js +89 -0
  189. package/dist/v3/index.d.ts +1 -0
  190. package/dist/v3/index.js +1 -0
  191. package/dist/v3/parseId.d.ts +6 -0
  192. package/dist/v3/parseId.js +5 -0
  193. package/dist/v3/parseId.test.d.ts +1 -0
  194. package/dist/v3/parseId.test.js +25 -0
  195. package/dist/v3/slugCache.d.ts +11 -0
  196. package/dist/v3/slugCache.js +35 -0
  197. package/dist/v3/slugCache.test.d.ts +1 -0
  198. package/dist/v3/slugCache.test.js +105 -0
  199. package/dist/v3/transformDSV/dsvParseSrc.d.ts +1 -0
  200. package/dist/v3/transformDSV/dsvParseSrc.js +121 -0
  201. package/dist/v3/transformDSV/index.d.ts +2 -0
  202. package/dist/v3/transformDSV/index.js +40 -0
  203. package/dist/{v3Runtime → v3}/transformSvelte.d.ts +4 -2
  204. package/dist/v3/transformSvelte.js +77 -0
  205. package/dist/{v3Runtime → v3}/types.d.ts +18 -18
  206. package/dist/v3/types.js +1 -0
  207. package/dist/v3/v3Build.d.ts +15 -0
  208. package/dist/v3/v3Build.js +30 -0
  209. package/dist/v3/vizCache.d.ts +10 -0
  210. package/dist/v3/vizCache.js +36 -0
  211. package/dist/v3/vizCache.test.d.ts +1 -0
  212. package/dist/v3/vizCache.test.js +98 -0
  213. package/dist/{v3Runtime → v3}/vizLoad.d.ts +3 -4
  214. package/dist/v3/vizLoad.js +46 -0
  215. package/dist/v3/vizResolve.d.ts +7 -0
  216. package/dist/v3/vizResolve.js +69 -0
  217. package/dist/v4/index.d.ts +10 -0
  218. package/dist/v4/index.js +134 -0
  219. package/package.json +35 -38
  220. package/dist/assets/setupV3Runtime-BVr5lyKp.js +0 -240
  221. package/dist/computeSrcDoc.d.ts +0 -13
  222. package/dist/computeSrcDoc.d.ts.map +0 -1
  223. package/dist/computeSrcDoc.test.d.ts +0 -2
  224. package/dist/computeSrcDoc.test.d.ts.map +0 -1
  225. package/dist/index.d.ts.map +0 -1
  226. package/dist/useRuntime.d.ts +0 -13
  227. package/dist/useRuntime.d.ts.map +0 -1
  228. package/dist/v2Runtime/bundle/bubleJSXOnly.d.ts +0 -5
  229. package/dist/v2Runtime/bundle/bubleJSXOnly.d.ts.map +0 -1
  230. package/dist/v2Runtime/bundle/packageJson.d.ts +0 -10
  231. package/dist/v2Runtime/bundle/packageJson.d.ts.map +0 -1
  232. package/dist/v2Runtime/computeSrcDocV2.d.ts +0 -3
  233. package/dist/v2Runtime/computeSrcDocV2.d.ts.map +0 -1
  234. package/dist/v2Runtime/computeSrcDocV2.test.d.ts +0 -2
  235. package/dist/v2Runtime/computeSrcDocV2.test.d.ts.map +0 -1
  236. package/dist/v2Runtime/getComputedIndexHtml.d.ts +0 -4
  237. package/dist/v2Runtime/getComputedIndexHtml.d.ts.map +0 -1
  238. package/dist/v2Runtime/getComputedIndexHtml.test.d.ts +0 -2
  239. package/dist/v2Runtime/getComputedIndexHtml.test.d.ts.map +0 -1
  240. package/dist/v2Runtime/getText.d.ts +0 -3
  241. package/dist/v2Runtime/getText.d.ts.map +0 -1
  242. package/dist/v2Runtime/transformFiles.d.ts +0 -3
  243. package/dist/v2Runtime/transformFiles.d.ts.map +0 -1
  244. package/dist/v2Runtime/v3FilesToV2Files.d.ts +0 -3
  245. package/dist/v2Runtime/v3FilesToV2Files.d.ts.map +0 -1
  246. package/dist/v2Runtime/v3FilesToV2Files.test.d.ts +0 -2
  247. package/dist/v2Runtime/v3FilesToV2Files.test.d.ts.map +0 -1
  248. package/dist/v3Runtime/build.d.ts +0 -14
  249. package/dist/v3Runtime/build.d.ts.map +0 -1
  250. package/dist/v3Runtime/build.test.d.ts +0 -2
  251. package/dist/v3Runtime/build.test.d.ts.map +0 -1
  252. package/dist/v3Runtime/cleanRollupErrorMessage.d.ts +0 -5
  253. package/dist/v3Runtime/cleanRollupErrorMessage.d.ts.map +0 -1
  254. package/dist/v3Runtime/computeSrcDocV3.d.ts +0 -7
  255. package/dist/v3Runtime/computeSrcDocV3.d.ts.map +0 -1
  256. package/dist/v3Runtime/extractVizImport.d.ts.map +0 -1
  257. package/dist/v3Runtime/extractVizImport.test.d.ts +0 -2
  258. package/dist/v3Runtime/extractVizImport.test.d.ts.map +0 -1
  259. package/dist/v3Runtime/generateRollupErrorMessage.d.ts +0 -5
  260. package/dist/v3Runtime/generateRollupErrorMessage.d.ts.map +0 -1
  261. package/dist/v3Runtime/importFromViz.d.ts +0 -1
  262. package/dist/v3Runtime/importFromViz.d.ts.map +0 -1
  263. package/dist/v3Runtime/index.d.ts +0 -2
  264. package/dist/v3Runtime/index.d.ts.map +0 -1
  265. package/dist/v3Runtime/parseId.d.ts +0 -7
  266. package/dist/v3Runtime/parseId.d.ts.map +0 -1
  267. package/dist/v3Runtime/setupV3Runtime.d.ts +0 -15
  268. package/dist/v3Runtime/setupV3Runtime.d.ts.map +0 -1
  269. package/dist/v3Runtime/transformDSV/index.d.ts +0 -3
  270. package/dist/v3Runtime/transformDSV/index.d.ts.map +0 -1
  271. package/dist/v3Runtime/transformSvelte.d.ts.map +0 -1
  272. package/dist/v3Runtime/types.d.ts.map +0 -1
  273. package/dist/v3Runtime/urlLoad.d.ts +0 -1
  274. package/dist/v3Runtime/urlLoad.d.ts.map +0 -1
  275. package/dist/v3Runtime/virtual.d.ts +0 -1
  276. package/dist/v3Runtime/virtual.d.ts.map +0 -1
  277. package/dist/v3Runtime/vizCache.d.ts +0 -11
  278. package/dist/v3Runtime/vizCache.d.ts.map +0 -1
  279. package/dist/v3Runtime/vizCache.test.d.ts +0 -2
  280. package/dist/v3Runtime/vizCache.test.d.ts.map +0 -1
  281. package/dist/v3Runtime/vizLoad.d.ts.map +0 -1
  282. package/dist/v3Runtime/vizLoadSvelte.d.ts +0 -1
  283. package/dist/v3Runtime/vizLoadSvelte.d.ts.map +0 -1
  284. package/dist/v3Runtime/vizResolve.d.ts +0 -10
  285. package/dist/v3Runtime/vizResolve.d.ts.map +0 -1
  286. package/dist/v3Runtime/worker.d.ts +0 -2
  287. package/dist/v3Runtime/worker.d.ts.map +0 -1
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Datavis Tech INC
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,257 +1,303 @@
1
- # @vizhub-core/runtime
1
+ # @vizhub/runtime
2
2
 
3
- The `@vizhub-core/runtime` package is a core component of the VizHub platform, responsible for executing and rendering visualizations in an isolated environment. It supports multiple runtime versions (V2 and V3) and provides a flexible system for handling dependencies, file transformations, and hot reloading.
3
+ [![NPM version](https://img.shields.io/npm/v/@vizhub/runtime.svg)](https://www.npmjs.com/package/@vizhub/runtime)
4
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
4
5
 
5
- ## Features
6
+ A powerful, flexible runtime environment for executing code sandboxes in the browser. `@vizhub/runtime` powers [VizHub](https://vizhub.com/) and can be used to build similar interactive coding platforms.
6
7
 
7
- - **Source code to HTML transformation**: Converts source code to HTML with embedded scripts and styles, ready for rendering in a browser.
8
- - **File transformations**: Transforms various file types (e.g., JavaScript, CSS, CSV, Svelte) to be used in the runtime environment.
9
- - **Hot reloading**: Automatically reloads visualizations when files are updated, providing a smooth development experience.
10
- - **Dependency management**: Handles external dependencies via CDN and supports custom package configurations.
11
- - **Virtual file system**: Implements a virtual file system for handling imports and file loading within the runtime.
12
- - **Web Worker-based build system**: Uses Web Workers to offload the build process, ensuring a responsive UI during development.
13
- - **Multi-version runtime support**: Supports both V2 and V3 runtime environments, allowing for backward compatibility and future-proofing.
8
+ ## Overview
14
9
 
15
- ## Installation
10
+ `@vizhub/runtime` intelligently detects the appropriate runtime version based on the provided files and generates executable HTML that can be used within an iframe's `srcdoc` attribute. It handles everything from simple HTML/JS/CSS combinations to complex module bundling, dependency resolution, and cross-viz imports.
11
+
12
+ ## Key Features
13
+
14
+ - **Multi-Version Runtime Support**
15
+
16
+ - **v1**: Simple HTML execution with fetch proxying
17
+ - **v2**: JavaScript bundling with Rollup, JSX support, and CDN-based dependency resolution
18
+ - **v3**: Advanced module bundling with Svelte support and cross-viz imports
19
+ - **v4**: Modern ES Modules with import maps for direct browser execution
20
+
21
+ - **Comprehensive Tooling**
22
+
23
+ - **Bundling**: Seamless integration with Rollup for module bundling
24
+ - **Transpilation**: Support for JSX (v2) and Svelte components (v3)
25
+ - **Dependency Management**: Automatic resolution via CDNs (jsDelivr/unpkg)
26
+ - **Caching**: Efficient viz content and slug resolution caching
27
+ - **Debugging**: Sourcemap generation for improved debugging experience
16
28
 
17
- Install the package using npm:
29
+ - **Advanced Capabilities**
30
+ - **Cross-Viz Imports**: Import code from other viz instances using `@username/vizIdOrSlug` syntax
31
+ - **Fetch Interception**: Handle cross-origin requests and authentication
32
+ - **File Type Support**: Process JS, JSX, CSS, CSV, JSON, and more
33
+
34
+ ## Installation
18
35
 
19
36
  ```bash
20
- npm install @vizhub-core/runtime
37
+ npm install @vizhub/runtime
21
38
  ```
22
39
 
23
40
  ## Usage
24
41
 
25
- The `@vizhub-core/runtime` package is designed to be used within the VizHub platform but can also be utilized in other environments where isolated execution of visualizations is required.
26
-
27
- ### Example
28
-
29
- Here's an example of how to use the runtime to compute the `srcdoc` for a visualization:
42
+ ### Basic Usage
30
43
 
31
44
  ```javascript
32
- import { expect, test } from "vitest";
33
- import { rollup } from "rollup";
34
- import { computeSrcDoc, createVizCache, VizCache } from "@vizhub/runtime";
35
-
36
- test("computeSrcDoc", async () => {
37
- const content = {
38
- files: {
39
- "32748932": {
40
- name: "index.js",
41
- text: "export const main = (container) => container.innerHTML = 'Hello, world!';",
42
- },
43
- },
44
- };
45
-
46
- const vizCache: VizCache = createVizCache({
47
- initialContents: [content],
48
- handleCacheMiss: async () => {
49
- return content;
50
- },
51
- });
52
-
53
- const resolveSlug = async (slug: string) => {
54
- throw new Error("Not implemented");
55
- };
56
-
57
- const getSvelteCompiler = async () => {
58
- throw new Error("Not implemented");
59
- };
60
-
61
- const { initialSrcdoc, initialSrcdocError } = await computeSrcDoc({
62
- rollup,
63
- content,
64
- vizCache,
65
- resolveSlug,
66
- getSvelteCompiler,
67
- });
68
-
69
- const randomID = initialSrcdoc.match(/viz-container-(\d+)/)?.[1];
70
-
71
- const srcdocWithoutMapping = initialSrcdoc.replace(
72
- /\/\/# sourceMappingURL=.*$/gm,
73
- ""
74
- );
75
-
76
- expect(srcdocWithoutMapping).toEqual(`<!DOCTYPE html>
77
- <html>
78
- <head>
79
- <meta charset="utf-8">
80
- <style>
81
- body {
82
- margin: 0;
83
- overflow: hidden;
84
- }
85
- #viz-container-${randomID} {
86
- height: 100vh;
87
- }
88
- </style>
89
- </head>
90
- <body>
91
- <div id="viz-container-${randomID}"></div>
92
- <script id="injected-script">(function(g,f){typeof exports==='object'&&typeof module!=='undefined'?f(exports):typeof define==='function'&&define.amd?define(['exports'],f):(g=typeof globalThis!=='undefined'?globalThis:g||self,f(g.Viz={}));})(this,(function(exports){'use strict';const main = (container) => container.innerHTML = 'Hello, world!';exports.main=main;}));
93
-
94
-
95
- <script>
96
- (() => {
97
- let cleanup;
98
- const render = () => {
99
- const container = document.getElementById('viz-container-${randomID}');
100
- typeof cleanup === 'function' && cleanup();
101
- cleanup = Viz.main(container, { state: window.state, setState, writeFile });
102
- };
103
- const setState = (next) => {
104
- window.state = next(window.state);
105
- render();
106
- };
107
- const writeFile = (fileName, content) => {
108
- parent.postMessage({ type: 'writeFile', fileName, content }, "*");
109
- };
110
- const run = () => {
111
- try {
112
- setState((state) => state || {});
113
- } catch (error) {
114
- console.error(error);
115
- parent.postMessage({ type: 'runError', error }, "*");
116
- }
117
- }
118
- run();
119
- const runJS = (src) => {
120
- document.getElementById('injected-script')?.remove();
121
- const script = document.createElement('script');
122
- script.textContent = src;
123
- script.id = 'injected-script';
124
- document.body.appendChild(script);
125
- run();
126
- };
127
- const runCSS = (src, id) => {
128
- const styleElementId = 'injected-style' + id;
129
- let style = document.getElementById(styleElementId);
130
- if (!style) {
131
- style = document.createElement('style');
132
- style.type = 'text/css';
133
- style.id = styleElementId;
134
- document.head.appendChild(style);
135
- }
136
- style.textContent = src;
137
- };
138
- onmessage = (message) => {
139
- switch (message.data.type) {
140
- case 'runJS':
141
- runJS(message.data.src);
142
- parent.postMessage({ type: 'runDone' }, "*");
143
- break;
144
- case 'runCSS':
145
- runCSS(message.data.src, message.data.id);
146
- break;
147
- case 'ping':
148
- parent.postMessage({ type: 'pong' }, "*");
149
- break;
150
- default:
151
- break;
152
- }
153
- }
154
- })();
155
- </script>
156
- </body>
157
- </html>`);
45
+ import { buildHTML } from '@vizhub/runtime';
46
+ import { rollup } from 'rollup';
47
+
48
+ // Simple v1 runtime (HTML only)
49
+ const html = await buildHTML({
50
+ files: {
51
+ 'index.html': '<html><body><h1>Hello World</h1></body></html>'
52
+ }
158
53
  });
54
+
55
+ // v2 runtime with bundling
56
+ const html = await buildHTML({
57
+ files: {
58
+ 'index.html': '<html><body><div id="root"></div><script src="bundle.js"></script></body></html>',
59
+ 'index.js': 'import { message } from "./message"; console.log(message);',
60
+ 'message.js': 'export const message = "Hello, bundled world!";'
61
+ },
62
+ rollup
63
+ });
64
+
65
+ // Use the generated HTML in an iframe
66
+ const iframe = document.createElement('iframe');
67
+ iframe.srcdoc = html;
68
+ document.body.appendChild(iframe);
159
69
  ```
160
70
 
161
- You can use the generated HTML and run it as a standalone page or within an iframe to display the visualization.
71
+ ### Runtime Versions
162
72
 
163
- ## API Documentation
73
+ The library automatically detects which runtime version to use based on the files provided:
164
74
 
165
- ### `computeSrcDoc`
75
+ - **v1**: When only `index.html` is present
76
+ - **v2**: When both `index.html` and `index.js` (or `index.jsx`) are present
77
+ - **v3**: When only `index.js` is present (no `index.html`)
78
+ - **v4**: When `index.html` contains ES module scripts with import maps
166
79
 
167
- Computes the `srcdoc` for a visualization based on its content and dependencies.
80
+ ### Advanced Usage: v3 Runtime with Cross-Viz Imports
168
81
 
169
- #### Parameters
82
+ ```javascript
83
+ import { buildHTML, createVizCache, createSlugCache } from '@vizhub/runtime';
84
+ import { rollup } from 'rollup';
85
+ import { compile } from 'svelte/compiler';
86
+
87
+ // Create caches for viz content and slug resolution
88
+ const vizCache = createVizCache({
89
+ initialContents: [
90
+ {
91
+ id: 'viz-123',
92
+ files: {
93
+ 'file1': { name: 'index.js', text: 'export const value = 42;' }
94
+ }
95
+ }
96
+ ],
97
+ handleCacheMiss: async (vizId) => {
98
+ // Fetch viz content from your backend
99
+ return await fetchVizContent(vizId);
100
+ }
101
+ });
170
102
 
171
- - **`rollup`**: The Rollup instance used for bundling.
172
- - **`content`**: The content of the visualization, including files like `index.js` and `index.html`.
173
- - **`vizCache`**: A cache of visualizations used to resolve imports from other visualizations.
174
- - **`resolveSlug`**: A function to resolve slug-based imports to a viz ID.
175
- - **`getSvelteCompiler`**: A function to get the Svelte compiler.
103
+ const slugCache = createSlugCache({
104
+ initialMappings: {
105
+ 'username/my-viz': 'viz-123'
106
+ },
107
+ handleCacheMiss: async (slug) => {
108
+ // Resolve slug to vizId from your backend
109
+ return await resolveSlug(slug);
110
+ }
111
+ });
176
112
 
177
- #### Returns
113
+ // Build HTML with cross-viz imports
114
+ const html = await buildHTML({
115
+ files: {
116
+ 'index.js': 'import { value } from "@username/my-viz"; console.log(value);'
117
+ },
118
+ rollup,
119
+ vizCache,
120
+ vizId: 'current-viz-id',
121
+ slugCache,
122
+ getSvelteCompiler: async () => compile
123
+ });
124
+ ```
178
125
 
179
- A promise that resolves to an object containing:
126
+ ## API Reference
180
127
 
181
- - **`initialSrcdoc`**: The computed `srcdoc` for the visualization.
182
- - **`initialSrcdocError`**: Any error that occurred during the computation.
128
+ ### buildHTML(options)
183
129
 
184
- ### `createVizCache`
130
+ Builds HTML that can be used as the `srcdoc` of an iframe.
185
131
 
186
- Creates a cache for visualizations, used to resolve imports from other visualizations.
132
+ #### Options
187
133
 
188
- #### Parameters
134
+ - **files**: `FileCollection` - A map of filenames to their contents
135
+ - **rollup**: `(options: RollupOptions) => Promise<RollupBuild>` - Rollup function (required for v2, v3, v4)
136
+ - **enableSourcemap**: `boolean` - Whether to include sourcemaps (default: true)
137
+ - **vizCache**: `VizCache` - Cache for viz content (required for v3 with cross-viz imports)
138
+ - **vizId**: `string` - ID of the current viz (required for v3 with cross-viz imports)
139
+ - **slugCache**: `SlugCache` - Cache for slug resolution (optional for v3)
140
+ - **getSvelteCompiler**: `() => Promise<SvelteCompiler>` - Function that returns Svelte compiler (optional for v3)
189
141
 
190
- - **`initialContents`**: An array of initial contents to populate the cache.
191
- - **`handleCacheMiss`**: A function called when a visualization is not found in the cache.
142
+ ### createVizCache(options)
192
143
 
193
- #### Returns
144
+ Creates a cache for viz content.
194
145
 
195
- A `VizCache` object with the following methods:
146
+ #### Options
196
147
 
197
- - **`get(vizId)`**: Retrieves the content of a visualization by its ID.
198
- - **`set(content)`**: Adds or updates the content of a visualization in the cache.
199
- - **`invalidate(vizId)`**: Invalidates the cache for a specific visualization.
148
+ - **initialContents**: `VizContent[]` - Initial viz contents to populate the cache
149
+ - **handleCacheMiss**: `(vizId: string) => Promise<VizContent>` - Function to handle cache misses
200
150
 
201
- ### `setJSDOM`
151
+ ### createSlugCache(options)
202
152
 
203
- Sets the DOM parser for environments where the native `DOMParser` is not available (e.g., Node.js).
153
+ Creates a cache for slug resolution.
204
154
 
205
- #### Parameters
155
+ #### Options
206
156
 
207
- - **`JSDOM`**: The `JSDOM` instance to use for parsing HTML.
157
+ - **initialMappings**: `Record<string, string>` - Initial slug to vizId mappings
158
+ - **handleCacheMiss**: `(slug: string) => Promise<string>` - Function to handle cache misses
208
159
 
209
- ### `cleanRollupErrorMessage`
160
+ ## Examples
210
161
 
211
- Cleans up Rollup error messages by removing unnecessary details like the viz ID.
162
+ ### v1: Simple HTML
163
+
164
+ ```javascript
165
+ const html = await buildHTML({
166
+ files: {
167
+ 'index.html': `
168
+ <!DOCTYPE html>
169
+ <html>
170
+ <head>
171
+ <style>
172
+ body { font-family: sans-serif; }
173
+ </style>
174
+ </head>
175
+ <body>
176
+ <h1>Hello World</h1>
177
+ <script>
178
+ console.log('Hello from v1 runtime!');
179
+ </script>
180
+ </body>
181
+ </html>
182
+ `
183
+ }
184
+ });
185
+ ```
212
186
 
213
- #### Parameters
187
+ ### v2: Bundled JavaScript
214
188
 
215
- - **`rawMessage`**: The raw error message from Rollup.
216
- - **`vizId`**: The ID of the visualization.
189
+ ```javascript
190
+ const html = await buildHTML({
191
+ files: {
192
+ 'index.html': `
193
+ <!DOCTYPE html>
194
+ <html>
195
+ <body>
196
+ <div id="root"></div>
197
+ <script src="bundle.js"></script>
198
+ </body>
199
+ </html>
200
+ `,
201
+ 'index.js': `
202
+ import { render } from './render';
203
+ render(document.getElementById('root'));
204
+ `,
205
+ 'render.js': `
206
+ export function render(element) {
207
+ element.innerHTML = '<h1>Hello from v2 runtime!</h1>';
208
+ }
209
+ `,
210
+ 'package.json': JSON.stringify({
211
+ dependencies: {
212
+ 'd3': '7.8.5'
213
+ }
214
+ })
215
+ },
216
+ rollup
217
+ });
218
+ ```
217
219
 
218
- #### Returns
220
+ ### v4: ES Modules with Import Maps
219
221
 
220
- - A cleaned-up error message string.
222
+ ```javascript
223
+ const html = await buildHTML({
224
+ files: {
225
+ 'index.html': `
226
+ <!DOCTYPE html>
227
+ <html>
228
+ <head>
229
+ <script type="importmap">
230
+ {
231
+ "imports": {
232
+ "utils": "./utils.js"
233
+ }
234
+ }
235
+ </script>
236
+ </head>
237
+ <body>
238
+ <div id="app"></div>
239
+ <script type="module" src="index.js"></script>
240
+ </body>
241
+ </html>
242
+ `,
243
+ 'index.js': `
244
+ import { createApp } from 'utils';
245
+ createApp(document.getElementById('app'));
246
+ `,
247
+ 'utils.js': `
248
+ export function createApp(element) {
249
+ element.innerHTML = '<h1>Hello from v4 runtime!</h1>';
250
+ }
251
+ `
252
+ },
253
+ rollup
254
+ });
255
+ ```
221
256
 
222
257
  ## Development
223
258
 
224
- To contribute to the development of this package, follow these steps:
225
-
226
- ### Clone the Repository
259
+ ### Setup
227
260
 
228
261
  ```bash
229
- git clone https://github.com/vizhub-core/vizhub3.git
230
- cd vizhub3
262
+ git clone https://github.com/vizhub-core/vizhub-runtime.git
263
+ cd vizhub-runtime
264
+ npm install
231
265
  ```
232
266
 
233
- ### Install Dependencies
267
+ ### Build
234
268
 
235
269
  ```bash
236
- npm install
270
+ npm run build
237
271
  ```
238
272
 
239
- ### Run Tests
240
-
241
- The package uses Vitest for testing. Run the tests with:
273
+ ### Test
242
274
 
243
275
  ```bash
244
- npm run test
276
+ npm test
245
277
  ```
246
278
 
247
- ### Build the Package
279
+ Run specific tests:
248
280
 
249
- To build the package, use:
281
+ ```bash
282
+ npx vitest run -t "should handle CSS imports"
283
+ ```
284
+
285
+ ### Type Checking
250
286
 
251
287
  ```bash
252
- npm run build
288
+ npm run typecheck
253
289
  ```
254
290
 
291
+ ## Contributing
292
+
293
+ Contributions are welcome! Please feel free to submit a Pull Request.
294
+
295
+ 1. Fork the repository
296
+ 2. Create your feature branch (`git checkout -b feature/amazing-feature`)
297
+ 3. Commit your changes (`git commit -m 'Add some amazing feature'`)
298
+ 4. Push to the branch (`git push origin feature/amazing-feature`)
299
+ 5. Open a Pull Request
300
+
255
301
  ## License
256
302
 
257
- This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
303
+ This project is licensed under the MIT License - see the LICENSE file for details.
@@ -0,0 +1,39 @@
1
+ import { FileCollection } from "../types";
2
+ export type Licence = string;
3
+ export interface PackageJson {
4
+ dependencies?: {
5
+ [key: string]: string;
6
+ };
7
+ vizhub?: {
8
+ libraries?: {
9
+ [key: string]: VizHubLibraryConfig;
10
+ };
11
+ };
12
+ license?: Licence | {
13
+ type: string;
14
+ };
15
+ }
16
+ export interface Dependency {
17
+ name: string;
18
+ version: string;
19
+ }
20
+ export interface VizHubLibraryConfig {
21
+ path?: string;
22
+ global?: string;
23
+ }
24
+ export interface VizHubLibraryConfigs {
25
+ [key: string]: VizHubLibraryConfig;
26
+ }
27
+ export declare const packageJSON: (files: FileCollection) => PackageJson;
28
+ export declare const dependencies: (files: FileCollection) => {
29
+ [key: string]: string;
30
+ };
31
+ export declare const getConfiguredLibraries: (files: FileCollection) => {
32
+ [key: string]: VizHubLibraryConfig;
33
+ };
34
+ export declare const dependencySource: ({ name, version }: Dependency, libraries: VizHubLibraryConfigs, cdn?: string) => string;
35
+ export declare const getLicense: (files: FileCollection) => string | {
36
+ type: string;
37
+ } | undefined;
38
+ export declare const getConfiguredGlobals: (pkg: PackageJson) => Record<string, string>;
39
+ export declare const getGlobals: (pkg: PackageJson) => Record<string, string>;
@@ -0,0 +1,71 @@
1
+ const EMPTY_PKG_JSON = {
2
+ dependencies: {},
3
+ vizhub: {},
4
+ license: "MIT",
5
+ };
6
+ const DEBUG = false;
7
+ export const packageJSON = (files) => {
8
+ const packageJsonText = files["package.json"];
9
+ DEBUG &&
10
+ console.log("[packageJSON] packageJsonText:", packageJsonText);
11
+ try {
12
+ const pkg = packageJsonText
13
+ ? JSON.parse(packageJsonText)
14
+ : EMPTY_PKG_JSON;
15
+ DEBUG &&
16
+ console.log("[packageJSON] pkg:", JSON.stringify(pkg, null, 2));
17
+ return pkg;
18
+ }
19
+ catch {
20
+ DEBUG &&
21
+ console.log("[packageJSON] Error parsing package.json");
22
+ return EMPTY_PKG_JSON;
23
+ }
24
+ };
25
+ export const dependencies = (files) => packageJSON(files).dependencies || {};
26
+ export const getConfiguredLibraries = (files) => {
27
+ const vizhubConfig = packageJSON(files).vizhub || {};
28
+ return vizhubConfig.libraries || {};
29
+ };
30
+ export const dependencySource = ({ name, version }, libraries, cdn = "jsdelivr") => {
31
+ const path = libraries[name]
32
+ ? libraries[name].path || ""
33
+ : "";
34
+ const src = cdn === "jsdelivr"
35
+ ? `https://cdn.jsdelivr.net/npm/${name}@${version}${path}`
36
+ : `https://unpkg.com/${name}@${version}${path}`;
37
+ return src;
38
+ };
39
+ export const getLicense = (files) => {
40
+ const license = packageJSON(files).license;
41
+ if (typeof license === "object" &&
42
+ license !== null &&
43
+ "type" in license) {
44
+ return license.type;
45
+ }
46
+ return license || EMPTY_PKG_JSON.license;
47
+ };
48
+ export const getConfiguredGlobals = (pkg) => {
49
+ const libraries = pkg?.vizhub?.libraries;
50
+ if (libraries) {
51
+ return Object.entries(libraries).reduce((accumulator, [packageName, config]) => {
52
+ if (config.global) {
53
+ accumulator[packageName] = config.global;
54
+ }
55
+ return accumulator;
56
+ }, {});
57
+ }
58
+ return {};
59
+ };
60
+ const defaultGlobals = {
61
+ d3: "d3",
62
+ react: "React",
63
+ "react-dom": "ReactDOM",
64
+ };
65
+ export const getGlobals = (pkg) => {
66
+ const configuredGlobals = getConfiguredGlobals(pkg);
67
+ return {
68
+ ...defaultGlobals,
69
+ ...configuredGlobals,
70
+ };
71
+ };
@@ -0,0 +1 @@
1
+ export {};