@vizhub/runtime 0.1.4 → 0.3.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 (291) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +376 -178
  3. package/dist/buildHTML.d.ts +14 -0
  4. package/dist/buildHTML.js +67 -0
  5. package/dist/common/packageJson.d.ts +39 -0
  6. package/dist/common/packageJson.js +71 -0
  7. package/dist/common/packageJson.test.d.ts +1 -0
  8. package/dist/common/packageJson.test.js +146 -0
  9. package/dist/common/sucrasePlugin.d.ts +2 -0
  10. package/dist/common/sucrasePlugin.js +24 -0
  11. package/dist/common/virtualFileSystem.d.ts +3 -0
  12. package/dist/common/virtualFileSystem.js +75 -0
  13. package/dist/determineRuntimeVersion.d.ts +3 -0
  14. package/dist/determineRuntimeVersion.js +22 -0
  15. package/dist/determineRuntimeVersion.test.d.ts +1 -0
  16. package/dist/determineRuntimeVersion.test.js +101 -0
  17. package/dist/fixtures/basicHTML.d.ts +2 -0
  18. package/dist/fixtures/basicHTML.js +22 -0
  19. package/dist/fixtures/fetchProxy.d.ts +2 -0
  20. package/dist/fixtures/fetchProxy.js +25 -0
  21. package/dist/fixtures/index.d.ts +3 -0
  22. package/dist/fixtures/index.js +9 -0
  23. package/dist/fixtures/jsScriptTag.d.ts +2 -0
  24. package/dist/fixtures/jsScriptTag.js +19 -0
  25. package/dist/index.d.ts +3 -8
  26. package/dist/index.js +3 -24890
  27. package/dist/index.test.d.ts +1 -0
  28. package/dist/index.test.js +50 -0
  29. package/dist/magicSandbox/fixProtocollessUrls.d.ts +4 -0
  30. package/dist/magicSandbox/fixProtocollessUrls.js +10 -0
  31. package/dist/magicSandbox/generateInterceptorScript.d.ts +4 -0
  32. package/dist/magicSandbox/generateInterceptorScript.js +178 -0
  33. package/dist/magicSandbox/index.d.ts +19 -0
  34. package/dist/magicSandbox/index.js +48 -0
  35. package/dist/test/fixtures/basicHTML.d.ts +2 -0
  36. package/dist/test/fixtures/basicHTML.js +19 -0
  37. package/dist/test/fixtures/fetchProxy.d.ts +2 -0
  38. package/dist/test/fixtures/fetchProxy.js +22 -0
  39. package/dist/test/fixtures/index.d.ts +6 -0
  40. package/dist/test/fixtures/index.js +6 -0
  41. package/dist/test/fixtures/jsScriptTag.d.ts +2 -0
  42. package/dist/test/fixtures/jsScriptTag.js +16 -0
  43. package/dist/test/fixtures/magicSandbox/basicHTML.d.ts +5 -0
  44. package/dist/test/fixtures/magicSandbox/basicHTML.js +17 -0
  45. package/dist/test/fixtures/magicSandbox/fetchProxy.d.ts +11 -0
  46. package/dist/test/fixtures/magicSandbox/fetchProxy.js +18 -0
  47. package/dist/test/fixtures/magicSandbox/index.d.ts +6 -0
  48. package/dist/test/fixtures/magicSandbox/index.js +6 -0
  49. package/dist/test/fixtures/magicSandbox/jsScriptTag.d.ts +8 -0
  50. package/dist/test/fixtures/magicSandbox/jsScriptTag.js +13 -0
  51. package/dist/test/fixtures/magicSandbox/protocolTest.d.ts +5 -0
  52. package/dist/test/fixtures/magicSandbox/protocolTest.js +17 -0
  53. package/dist/test/fixtures/magicSandbox/styleTest.d.ts +8 -0
  54. package/dist/test/fixtures/magicSandbox/styleTest.js +19 -0
  55. package/dist/test/fixtures/magicSandbox/xmlTest.d.ts +8 -0
  56. package/dist/test/fixtures/magicSandbox/xmlTest.js +23 -0
  57. package/dist/test/fixtures/protocolTest.d.ts +2 -0
  58. package/dist/test/fixtures/protocolTest.js +19 -0
  59. package/dist/test/fixtures/styleTest.d.ts +2 -0
  60. package/dist/test/fixtures/styleTest.js +22 -0
  61. package/dist/test/fixtures/v1/basicHTML.d.ts +3 -0
  62. package/dist/test/fixtures/v1/basicHTML.js +15 -0
  63. package/dist/test/fixtures/v1/fetchProxy.d.ts +5 -0
  64. package/dist/test/fixtures/v1/fetchProxy.js +12 -0
  65. package/dist/test/fixtures/v1/index.d.ts +6 -0
  66. package/dist/test/fixtures/v1/index.js +6 -0
  67. package/dist/test/fixtures/v1/jsScriptTag.d.ts +4 -0
  68. package/dist/test/fixtures/v1/jsScriptTag.js +9 -0
  69. package/dist/test/fixtures/v1/protocolTest.d.ts +3 -0
  70. package/dist/test/fixtures/v1/protocolTest.js +15 -0
  71. package/dist/test/fixtures/v1/styleTest.d.ts +4 -0
  72. package/dist/test/fixtures/v1/styleTest.js +15 -0
  73. package/dist/test/fixtures/v1/xmlTest.d.ts +4 -0
  74. package/dist/test/fixtures/v1/xmlTest.js +19 -0
  75. package/dist/test/fixtures/v2/basicBundle.d.ts +5 -0
  76. package/dist/test/fixtures/v2/basicBundle.js +5 -0
  77. package/dist/test/fixtures/v2/basicBundleNoExtension.d.ts +5 -0
  78. package/dist/test/fixtures/v2/basicBundleNoExtension.js +5 -0
  79. package/dist/test/fixtures/v2/d3Import.d.ts +4 -0
  80. package/dist/test/fixtures/v2/d3Import.js +7 -0
  81. package/dist/test/fixtures/v2/d3ImportPkg.d.ts +5 -0
  82. package/dist/test/fixtures/v2/d3ImportPkg.js +19 -0
  83. package/dist/test/fixtures/v2/d3RosettaImportPkg.d.ts +5 -0
  84. package/dist/test/fixtures/v2/d3RosettaImportPkg.js +19 -0
  85. package/dist/test/fixtures/v2/es6Preserve.d.ts +4 -0
  86. package/dist/test/fixtures/v2/es6Preserve.js +6 -0
  87. package/dist/test/fixtures/v2/generatorSupport.d.ts +4 -0
  88. package/dist/test/fixtures/v2/generatorSupport.js +6 -0
  89. package/dist/test/fixtures/v2/index.d.ts +14 -0
  90. package/dist/test/fixtures/v2/index.js +15 -0
  91. package/dist/test/fixtures/v2/jsxTranspile.d.ts +4 -0
  92. package/dist/test/fixtures/v2/jsxTranspile.js +6 -0
  93. package/dist/test/fixtures/v2/reactDomImport.d.ts +4 -0
  94. package/dist/test/fixtures/v2/reactDomImport.js +8 -0
  95. package/dist/test/fixtures/v2/reactDomImportPkg.d.ts +5 -0
  96. package/dist/test/fixtures/v2/reactDomImportPkg.js +24 -0
  97. package/dist/test/fixtures/v2/reactImport.d.ts +4 -0
  98. package/dist/test/fixtures/v2/reactImport.js +7 -0
  99. package/dist/test/fixtures/v2/reactImportPkg.d.ts +5 -0
  100. package/dist/test/fixtures/v2/reactImportPkg.js +17 -0
  101. package/dist/test/fixtures/v2/sourceMapErrorFixture.d.ts +5 -0
  102. package/dist/test/fixtures/v2/sourceMapErrorFixture.js +19 -0
  103. package/dist/test/fixtures/v2/unicodeSupport.d.ts +4 -0
  104. package/dist/test/fixtures/v2/unicodeSupport.js +6 -0
  105. package/dist/test/fixtures/v3/basicIndexJS.d.ts +3 -0
  106. package/dist/test/fixtures/v3/basicIndexJS.js +7 -0
  107. package/dist/test/fixtures/v3/cssImport.d.ts +4 -0
  108. package/dist/test/fixtures/v3/cssImport.js +9 -0
  109. package/dist/test/fixtures/v3/csvImport.d.ts +4 -0
  110. package/dist/test/fixtures/v3/csvImport.js +11 -0
  111. package/dist/test/fixtures/v3/csvStrangeChars.d.ts +4 -0
  112. package/dist/test/fixtures/v3/csvStrangeChars.js +12 -0
  113. package/dist/test/fixtures/v3/index.d.ts +9 -0
  114. package/dist/test/fixtures/v3/index.js +9 -0
  115. package/dist/test/fixtures/v3/jsExport.d.ts +4 -0
  116. package/dist/test/fixtures/v3/jsExport.js +10 -0
  117. package/dist/test/fixtures/v3/sourcemap.d.ts +3 -0
  118. package/dist/test/fixtures/v3/sourcemap.js +10 -0
  119. package/dist/test/fixtures/v3/svelte.d.ts +4 -0
  120. package/dist/test/fixtures/v3/svelte.js +18 -0
  121. package/dist/test/fixtures/v3/vizContent.d.ts +2 -0
  122. package/dist/test/fixtures/v3/vizContent.js +11 -0
  123. package/dist/test/fixtures/v3/vizImport.d.ts +6 -0
  124. package/dist/test/fixtures/v3/vizImport.js +85 -0
  125. package/dist/test/fixtures/v4/d3Usage.d.ts +5 -0
  126. package/dist/test/fixtures/v4/d3Usage.js +31 -0
  127. package/dist/test/fixtures/v4/esmBuild.d.ts +5 -0
  128. package/dist/test/fixtures/v4/esmBuild.js +18 -0
  129. package/dist/test/fixtures/v4/fetchInterception.d.ts +5 -0
  130. package/dist/test/fixtures/v4/fetchInterception.js +18 -0
  131. package/dist/test/fixtures/v4/index.d.ts +9 -0
  132. package/dist/test/fixtures/v4/index.js +9 -0
  133. package/dist/test/fixtures/v4/jsScriptTagTypeModule.d.ts +4 -0
  134. package/dist/test/fixtures/v4/jsScriptTagTypeModule.js +18 -0
  135. package/dist/test/fixtures/v4/jsScriptTagTypeModules.d.ts +5 -0
  136. package/dist/test/fixtures/v4/jsScriptTagTypeModules.js +12 -0
  137. package/dist/test/fixtures/v4/reactHooks.d.ts +6 -0
  138. package/dist/test/fixtures/v4/reactHooks.js +47 -0
  139. package/dist/test/fixtures/v4/reactJsx.d.ts +6 -0
  140. package/dist/test/fixtures/v4/reactJsx.js +34 -0
  141. package/dist/test/fixtures/v4/threeJsUsage.d.ts +4 -0
  142. package/dist/test/fixtures/v4/threeJsUsage.js +24 -0
  143. package/dist/test/fixtures/v4/typeScriptSupport.d.ts +5 -0
  144. package/dist/test/fixtures/v4/typeScriptSupport.js +29 -0
  145. package/dist/test/fixtures/xmlTest.d.ts +2 -0
  146. package/dist/test/fixtures/xmlTest.js +26 -0
  147. package/dist/test/index.test.d.ts +1 -0
  148. package/dist/test/index.test.js +40 -0
  149. package/dist/test/testInBrowser.d.ts +15 -0
  150. package/dist/test/testInBrowser.js +47 -0
  151. package/dist/test/testStackTrace.d.ts +9 -0
  152. package/dist/test/testStackTrace.js +55 -0
  153. package/dist/test/v1.test.d.ts +1 -0
  154. package/dist/test/v1.test.js +65 -0
  155. package/dist/test/v2.test.d.ts +1 -0
  156. package/dist/test/v2.test.js +121 -0
  157. package/dist/test/v3.test.d.ts +1 -0
  158. package/dist/test/v3.test.js +114 -0
  159. package/dist/test/v4.test.d.ts +1 -0
  160. package/dist/test/v4.test.js +100 -0
  161. package/dist/types.d.ts +1 -0
  162. package/dist/types.js +1 -0
  163. package/dist/utils/getFileText.d.ts +2 -0
  164. package/dist/utils/getFileText.js +13 -0
  165. package/dist/utils/getFileText.test.d.ts +1 -0
  166. package/dist/utils/getFileText.test.js +72 -0
  167. package/dist/utils/vizContentToFileCollection.d.ts +7 -0
  168. package/dist/utils/vizContentToFileCollection.js +19 -0
  169. package/dist/utils/vizContentToFileCollection.test.d.ts +1 -0
  170. package/dist/utils/vizContentToFileCollection.test.js +75 -0
  171. package/dist/v2/computeBundleJSV2.d.ts +7 -0
  172. package/dist/v2/computeBundleJSV2.js +29 -0
  173. package/dist/v2/getComputedIndexHtml.d.ts +4 -0
  174. package/dist/v2/getComputedIndexHtml.js +81 -0
  175. package/dist/v2/getComputedIndexHtml.test.d.ts +1 -0
  176. package/dist/v2/getComputedIndexHtml.test.js +134 -0
  177. package/dist/v2/index.d.ts +1 -0
  178. package/dist/v2/index.js +1 -0
  179. package/dist/v2/v2Build.d.ts +7 -0
  180. package/dist/v2/v2Build.js +11 -0
  181. package/dist/{v3Runtime → v3}/cleanRollupErrorMessage.d.ts +0 -1
  182. package/dist/v3/cleanRollupErrorMessage.js +9 -0
  183. package/dist/v3/computeBundleJSV3.d.ts +17 -0
  184. package/dist/v3/computeBundleJSV3.js +48 -0
  185. package/dist/v3/createVizContent.d.ts +8 -0
  186. package/dist/v3/createVizContent.js +23 -0
  187. package/dist/{v3Runtime → v3}/extractVizImport.d.ts +2 -3
  188. package/dist/v3/extractVizImport.js +23 -0
  189. package/dist/v3/extractVizImport.test.d.ts +1 -0
  190. package/dist/v3/extractVizImport.test.js +30 -0
  191. package/dist/v3/htmlTemplate.d.ts +5 -0
  192. package/dist/v3/htmlTemplate.js +89 -0
  193. package/dist/v3/index.d.ts +1 -0
  194. package/dist/v3/index.js +1 -0
  195. package/dist/v3/parseId.d.ts +6 -0
  196. package/dist/v3/parseId.js +5 -0
  197. package/dist/v3/parseId.test.d.ts +1 -0
  198. package/dist/v3/parseId.test.js +25 -0
  199. package/dist/{v3Runtime → v3}/setupV3Runtime.d.ts +5 -5
  200. package/dist/v3/setupV3Runtime.js +341 -0
  201. package/dist/v3/slugCache.d.ts +11 -0
  202. package/dist/v3/slugCache.js +35 -0
  203. package/dist/v3/slugCache.test.d.ts +1 -0
  204. package/dist/v3/slugCache.test.js +105 -0
  205. package/dist/v3/transformDSV/dsvParseSrc.d.ts +1 -0
  206. package/dist/v3/transformDSV/dsvParseSrc.js +121 -0
  207. package/dist/v3/transformDSV/index.d.ts +2 -0
  208. package/dist/v3/transformDSV/index.js +40 -0
  209. package/dist/{v3Runtime → v3}/transformSvelte.d.ts +4 -2
  210. package/dist/v3/transformSvelte.js +77 -0
  211. package/dist/{v3Runtime → v3}/types.d.ts +18 -18
  212. package/dist/v3/types.js +1 -0
  213. package/dist/v3/v3Build.d.ts +14 -0
  214. package/dist/v3/v3Build.js +30 -0
  215. package/dist/v3/vizCache.d.ts +10 -0
  216. package/dist/v3/vizCache.js +36 -0
  217. package/dist/v3/vizCache.test.d.ts +1 -0
  218. package/dist/v3/vizCache.test.js +98 -0
  219. package/dist/{v3Runtime → v3}/vizLoad.d.ts +3 -4
  220. package/dist/v3/vizLoad.js +46 -0
  221. package/dist/v3/vizResolve.d.ts +7 -0
  222. package/dist/v3/vizResolve.js +69 -0
  223. package/dist/v4/index.d.ts +10 -0
  224. package/dist/v4/index.js +134 -0
  225. package/package.json +37 -38
  226. package/dist/assets/setupV3Runtime-BVr5lyKp.js +0 -240
  227. package/dist/computeSrcDoc.d.ts +0 -13
  228. package/dist/computeSrcDoc.d.ts.map +0 -1
  229. package/dist/computeSrcDoc.test.d.ts +0 -2
  230. package/dist/computeSrcDoc.test.d.ts.map +0 -1
  231. package/dist/index.d.ts.map +0 -1
  232. package/dist/useRuntime.d.ts +0 -13
  233. package/dist/useRuntime.d.ts.map +0 -1
  234. package/dist/v2Runtime/bundle/bubleJSXOnly.d.ts +0 -5
  235. package/dist/v2Runtime/bundle/bubleJSXOnly.d.ts.map +0 -1
  236. package/dist/v2Runtime/bundle/packageJson.d.ts +0 -10
  237. package/dist/v2Runtime/bundle/packageJson.d.ts.map +0 -1
  238. package/dist/v2Runtime/computeSrcDocV2.d.ts +0 -3
  239. package/dist/v2Runtime/computeSrcDocV2.d.ts.map +0 -1
  240. package/dist/v2Runtime/computeSrcDocV2.test.d.ts +0 -2
  241. package/dist/v2Runtime/computeSrcDocV2.test.d.ts.map +0 -1
  242. package/dist/v2Runtime/getComputedIndexHtml.d.ts +0 -4
  243. package/dist/v2Runtime/getComputedIndexHtml.d.ts.map +0 -1
  244. package/dist/v2Runtime/getComputedIndexHtml.test.d.ts +0 -2
  245. package/dist/v2Runtime/getComputedIndexHtml.test.d.ts.map +0 -1
  246. package/dist/v2Runtime/getText.d.ts +0 -3
  247. package/dist/v2Runtime/getText.d.ts.map +0 -1
  248. package/dist/v2Runtime/transformFiles.d.ts +0 -3
  249. package/dist/v2Runtime/transformFiles.d.ts.map +0 -1
  250. package/dist/v2Runtime/v3FilesToV2Files.d.ts +0 -3
  251. package/dist/v2Runtime/v3FilesToV2Files.d.ts.map +0 -1
  252. package/dist/v2Runtime/v3FilesToV2Files.test.d.ts +0 -2
  253. package/dist/v2Runtime/v3FilesToV2Files.test.d.ts.map +0 -1
  254. package/dist/v3Runtime/build.d.ts +0 -14
  255. package/dist/v3Runtime/build.d.ts.map +0 -1
  256. package/dist/v3Runtime/build.test.d.ts +0 -2
  257. package/dist/v3Runtime/build.test.d.ts.map +0 -1
  258. package/dist/v3Runtime/cleanRollupErrorMessage.d.ts.map +0 -1
  259. package/dist/v3Runtime/computeSrcDocV3.d.ts +0 -7
  260. package/dist/v3Runtime/computeSrcDocV3.d.ts.map +0 -1
  261. package/dist/v3Runtime/extractVizImport.d.ts.map +0 -1
  262. package/dist/v3Runtime/extractVizImport.test.d.ts +0 -2
  263. package/dist/v3Runtime/extractVizImport.test.d.ts.map +0 -1
  264. package/dist/v3Runtime/generateRollupErrorMessage.d.ts +0 -5
  265. package/dist/v3Runtime/generateRollupErrorMessage.d.ts.map +0 -1
  266. package/dist/v3Runtime/importFromViz.d.ts +0 -1
  267. package/dist/v3Runtime/importFromViz.d.ts.map +0 -1
  268. package/dist/v3Runtime/index.d.ts +0 -2
  269. package/dist/v3Runtime/index.d.ts.map +0 -1
  270. package/dist/v3Runtime/parseId.d.ts +0 -7
  271. package/dist/v3Runtime/parseId.d.ts.map +0 -1
  272. package/dist/v3Runtime/setupV3Runtime.d.ts.map +0 -1
  273. package/dist/v3Runtime/transformDSV/index.d.ts +0 -3
  274. package/dist/v3Runtime/transformDSV/index.d.ts.map +0 -1
  275. package/dist/v3Runtime/transformSvelte.d.ts.map +0 -1
  276. package/dist/v3Runtime/types.d.ts.map +0 -1
  277. package/dist/v3Runtime/urlLoad.d.ts +0 -1
  278. package/dist/v3Runtime/urlLoad.d.ts.map +0 -1
  279. package/dist/v3Runtime/virtual.d.ts +0 -1
  280. package/dist/v3Runtime/virtual.d.ts.map +0 -1
  281. package/dist/v3Runtime/vizCache.d.ts +0 -11
  282. package/dist/v3Runtime/vizCache.d.ts.map +0 -1
  283. package/dist/v3Runtime/vizCache.test.d.ts +0 -2
  284. package/dist/v3Runtime/vizCache.test.d.ts.map +0 -1
  285. package/dist/v3Runtime/vizLoad.d.ts.map +0 -1
  286. package/dist/v3Runtime/vizLoadSvelte.d.ts +0 -1
  287. package/dist/v3Runtime/vizLoadSvelte.d.ts.map +0 -1
  288. package/dist/v3Runtime/vizResolve.d.ts +0 -10
  289. package/dist/v3Runtime/vizResolve.d.ts.map +0 -1
  290. package/dist/v3Runtime/worker.d.ts +0 -2
  291. package/dist/v3Runtime/worker.d.ts.map +0 -1
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const puppeteer_1 = __importDefault(require("puppeteer"));
7
+ const vitest_1 = require("vitest");
8
+ const index_1 = require("./index");
9
+ const fixtures_1 = require("./fixtures");
10
+ let browser;
11
+ (0, vitest_1.beforeAll)(async () => {
12
+ browser = await puppeteer_1.default.launch();
13
+ });
14
+ (0, vitest_1.afterAll)(async () => {
15
+ await browser.close();
16
+ });
17
+ async function testInBrowser(files, expectedLog) {
18
+ const page = await browser.newPage();
19
+ try {
20
+ // Capture console.log output
21
+ const logs = [];
22
+ page.on("console", (message) => logs.push(message.text()));
23
+ // Load the HTML
24
+ await page.setContent((0, index_1.computeSrcDoc)(files));
25
+ // Wait a bit for scripts to execute
26
+ await new Promise((resolve) => setTimeout(resolve, 100));
27
+ // Check console output
28
+ (0, vitest_1.expect)(logs).toContain(expectedLog);
29
+ }
30
+ finally {
31
+ await page.close();
32
+ }
33
+ }
34
+ (0, vitest_1.describe)("VizHub Runtime", () => {
35
+ (0, vitest_1.it)("should generate srcdoc HTML", () => {
36
+ const srcdoc = (0, index_1.computeSrcDoc)(fixtures_1.basicHTML);
37
+ (0, vitest_1.expect)(srcdoc).toContain("<!DOCTYPE html>");
38
+ (0, vitest_1.expect)(srcdoc).toContain("<title>My HTML Document</title>");
39
+ (0, vitest_1.expect)(srcdoc).toContain("Hello, World!");
40
+ });
41
+ (0, vitest_1.it)("basicHTML", async () => {
42
+ await testInBrowser(fixtures_1.basicHTML, "Hello, World!");
43
+ });
44
+ (0, vitest_1.it)("jsScriptTag", async () => {
45
+ await testInBrowser(fixtures_1.jsScriptTag, "Hello, JS!");
46
+ });
47
+ (0, vitest_1.it)("fetchProxy", async () => {
48
+ await testInBrowser(fixtures_1.fetchProxy, "Hello, Fetch!");
49
+ });
50
+ });
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Fixes protocol-less URLs to use HTTPS
3
+ */
4
+ export declare function fixProtocollessUrls(html: string): string;
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Fixes protocol-less URLs to use HTTPS
3
+ */
4
+ export function fixProtocollessUrls(html) {
5
+ // Fix link tags
6
+ html = html.replace(/<link.*?href=["']\/\/.*?["'].*?>/g, (match) => match.replace("//", "https://"));
7
+ // Fix script tags
8
+ html = html.replace(/<script.*?src=["']\/\/.*?["'].*?>/g, (match) => match.replace("//", "https://"));
9
+ return html;
10
+ }
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Generates the script that intercepts XMLHttpRequest and fetch calls
3
+ */
4
+ export declare function generateInterceptorScript(fileNames: string[], filesString: string): string;
@@ -0,0 +1,178 @@
1
+ /**
2
+ * Generates the script that intercepts XMLHttpRequest and fetch calls
3
+ */
4
+ export function generateInterceptorScript(fileNames, filesString) {
5
+ return `<script>
6
+ (function() {
7
+ // Store file data for interception
8
+ const __filesURI = "${filesString}";
9
+ const __files = JSON.parse(decodeURIComponent(__filesURI));
10
+ const __fileNames = ${JSON.stringify(fileNames)};
11
+
12
+ // Override XMLHttpRequest
13
+ const OriginalXHR = window.XMLHttpRequest;
14
+ window.XMLHttpRequest = function() {
15
+ this.xhr = new OriginalXHR();
16
+ return this;
17
+ };
18
+
19
+ // Override open method to intercept file requests
20
+ window.XMLHttpRequest.prototype.open = function(method, url, async, user, password) {
21
+ if (__fileNames.includes(url)) {
22
+ this.file = url;
23
+ this.responseText = __files[url];
24
+
25
+ // Handle XML files
26
+ if (url.endsWith(".xml")) {
27
+ try {
28
+ const parser = new DOMParser();
29
+ this.responseXML = parser.parseFromString(this.responseText, "text/xml");
30
+ } catch (e) {}
31
+ }
32
+
33
+ // Mark as completed
34
+ this.readyState = 4;
35
+ this.status = 200;
36
+ } else {
37
+ // Pass through to real XHR
38
+ this.xhr.open(method, url, async, user, password);
39
+ }
40
+ };
41
+
42
+ // Implement other XHR methods
43
+ window.XMLHttpRequest.prototype.setRequestHeader = function(header, value) {
44
+ if (this.file) return;
45
+ return this.xhr.setRequestHeader(header, value);
46
+ };
47
+
48
+ window.XMLHttpRequest.prototype.abort = function() {
49
+ return this.xhr.abort();
50
+ };
51
+
52
+ window.XMLHttpRequest.prototype.getAllResponseHeaders = function() {
53
+ return this.xhr.getAllResponseHeaders();
54
+ };
55
+
56
+ window.XMLHttpRequest.prototype.getResponseHeader = function(header) {
57
+ return this.xhr.getResponseHeader(header);
58
+ };
59
+
60
+ window.XMLHttpRequest.prototype.overrideMimeType = function(mime) {
61
+ return this.xhr.overrideMimeType(mime);
62
+ };
63
+
64
+ window.XMLHttpRequest.prototype.send = function(data) {
65
+ const that = this;
66
+
67
+ // Process in next tick to support libraries that attach handlers after send
68
+ setTimeout(() => {
69
+ // Wire up event handlers
70
+ that.xhr.onerror = that.onerror;
71
+ that.xhr.onprogress = that.onprogress;
72
+
73
+ if (that.responseType || that.responseType === '') {
74
+ that.xhr.responseType = that.responseType;
75
+ }
76
+
77
+ // Handle onload
78
+ if (that.onload) {
79
+ const onload = that.onload;
80
+ that.xhr.onload = that.onload = function() {
81
+ try {
82
+ that.response = this.response;
83
+ that.readyState = this.readyState;
84
+ that.status = this.status;
85
+ that.statusText = this.statusText;
86
+ } catch (e) {}
87
+
88
+ try {
89
+ if (that.responseType === '') {
90
+ that.responseXML = this.responseXML;
91
+ that.responseText = this.responseText;
92
+ }
93
+ if (that.responseType === 'text') {
94
+ that.responseText = this.responseText;
95
+ }
96
+ } catch (e) {}
97
+
98
+ onload();
99
+ };
100
+ }
101
+
102
+ // Handle onreadystatechange
103
+ if (that.onreadystatechange) {
104
+ const ready = that.onreadystatechange;
105
+ that.xhr.onreadystatechange = function() {
106
+ try {
107
+ that.readyState = this.readyState;
108
+ that.responseText = this.responseText;
109
+ that.responseXML = this.responseXML;
110
+ that.responseType = this.responseType;
111
+ that.status = this.status;
112
+ that.statusText = this.statusText;
113
+ } catch (e) {}
114
+
115
+ ready();
116
+ };
117
+ }
118
+
119
+ // For local files, trigger callbacks directly
120
+ if (that.file) {
121
+ if (that.onreadystatechange) {
122
+ return that.onreadystatechange();
123
+ }
124
+ if (that.onload) {
125
+ return that.onload();
126
+ }
127
+ }
128
+
129
+ // For real requests, pass through
130
+ that.xhr.send(data);
131
+ }, 0);
132
+ };
133
+
134
+ // Override fetch API
135
+ const originalFetch = window.fetch;
136
+ window.fetch = function(input, init) {
137
+ let url = input;
138
+
139
+ if (input instanceof Request) {
140
+ url = input.url;
141
+ }
142
+
143
+ // Fix blob:// protocol issues
144
+ if (typeof url === 'string') {
145
+ url = url.replace('blob://', 'http://');
146
+ }
147
+
148
+ // Intercept requests for local files
149
+ if (__fileNames.includes(url)) {
150
+ const responseText = __files[url];
151
+
152
+ return Promise.resolve({
153
+ ok: true,
154
+ status: 200,
155
+ statusText: 'ok',
156
+ url: url,
157
+ text: () => Promise.resolve(responseText),
158
+ json: () => Promise.resolve(JSON.parse(responseText)),
159
+ blob: () => Promise.resolve(new Blob([responseText])),
160
+ arrayBuffer: () => {
161
+ const buffer = new ArrayBuffer(responseText.length * 2);
162
+ const bufferView = new Uint16Array(buffer);
163
+
164
+ for (let i = 0; i < responseText.length; i++) {
165
+ bufferView[i] = responseText.charCodeAt(i);
166
+ }
167
+
168
+ return Promise.resolve(buffer);
169
+ }
170
+ });
171
+ }
172
+
173
+ // Pass through to original fetch
174
+ return originalFetch(input, init);
175
+ };
176
+ })();
177
+ </script>`;
178
+ }
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Magic Sandbox
3
+ *
4
+ * Originally created for Blockbuilder by Ian Johnson (@enjalot)
5
+ * Contributors: Geoffery Miller (@georules), Paweł Kowalski (@pavelloz),
6
+ * Erik Hazzard (@erikhazzard), Curran Kelleher (@curran), Micah Stubbs (@micahstubbs)
7
+ *
8
+ * This utility transforms HTML templates and handles file references to create
9
+ * a sandboxed environment that intercepts file requests.
10
+ */
11
+ type FileContent = {
12
+ content: string;
13
+ };
14
+ type FileCollection = Record<string, FileContent>;
15
+ /**
16
+ * Transforms HTML and files to create a sandboxed environment
17
+ */
18
+ export default function magicSandbox(template: string, files: FileCollection): string;
19
+ export {};
@@ -0,0 +1,48 @@
1
+ import { fixProtocollessUrls } from './fixProtocollessUrls';
2
+ import { generateInterceptorScript } from './generateInterceptorScript';
3
+ /**
4
+ * Escapes special characters in a string for use in regular expressions
5
+ */
6
+ function escapeRegExp(str) {
7
+ return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
8
+ }
9
+ /**
10
+ * Transforms HTML and files to create a sandboxed environment
11
+ */
12
+ export default function magicSandbox(template, files) {
13
+ // Fix protocol-less URLs (//example.com) to use HTTPS
14
+ template = fixProtocollessUrls(template);
15
+ // Process files and track which ones need to be handled by XHR/fetch
16
+ const referencedFiles = {};
17
+ for (const [filename, file] of Object.entries(files)) {
18
+ if (!file.content)
19
+ continue;
20
+ if (filename === "index.html" || filename === "thumbnail.png")
21
+ continue;
22
+ // Replace <script src="file.js"> with inline <script>content</script>
23
+ if (filename.endsWith(".js")) {
24
+ const scriptPattern = new RegExp(`<script.*?src=["']${escapeRegExp(filename)}["'].*?>`, "g");
25
+ if (template.match(scriptPattern)) {
26
+ template = template.replace(scriptPattern, `<script>${file.content}`);
27
+ continue;
28
+ }
29
+ }
30
+ // Replace <link href="file.css"> with inline <style>content</style>
31
+ if (filename.endsWith(".css")) {
32
+ const linkPattern = new RegExp(`<link.*?href=["']${escapeRegExp(filename)}["'].*?>`, "g");
33
+ if (template.match(linkPattern)) {
34
+ template = template.replace(linkPattern, `<style>${file.content}</style>`);
35
+ continue;
36
+ }
37
+ }
38
+ // Skip HTML files for XHR/fetch interception
39
+ if (!filename.endsWith(".html")) {
40
+ referencedFiles[filename] = file.content;
41
+ }
42
+ }
43
+ // Encode file references for use in the sandboxed environment
44
+ const fileNames = Object.keys(referencedFiles);
45
+ const filesString = encodeURIComponent(JSON.stringify(referencedFiles));
46
+ // Assemble the final HTML with meta tag and override scripts
47
+ return `<meta charset="utf-8">${generateInterceptorScript(fileNames, filesString)}${template}`;
48
+ }
@@ -0,0 +1,2 @@
1
+ import { VizFiles } from "@vizhub/viz-types";
2
+ export declare const basicHTML: VizFiles;
@@ -0,0 +1,19 @@
1
+ import { generateVizFileId } from "@vizhub/viz-types";
2
+ export const basicHTML = {
3
+ [generateVizFileId()]: {
4
+ name: "index.html",
5
+ text: `<!DOCTYPE html>
6
+ <html>
7
+ <head>
8
+ <title>My HTML Document</title>
9
+ </head>
10
+ <body>
11
+ <h1>Hello, World!</h1>
12
+ <p>This is my first HTML document.</p>
13
+ <script>
14
+ console.log('Hello, World!');
15
+ </script>
16
+ </body>
17
+ </html>`,
18
+ },
19
+ };
@@ -0,0 +1,2 @@
1
+ import { VizFiles } from "@vizhub/viz-types";
2
+ export declare const fetchProxy: VizFiles;
@@ -0,0 +1,22 @@
1
+ import { generateVizFileId } from "@vizhub/viz-types";
2
+ export const fetchProxy = {
3
+ [generateVizFileId()]: {
4
+ name: "index.html",
5
+ text: `<!DOCTYPE html>
6
+ <html>
7
+ <body>
8
+ <script src="index.js"></script>
9
+ </body>
10
+ </html>`,
11
+ },
12
+ [generateVizFileId()]: {
13
+ name: "index.js",
14
+ text: `fetch("data.csv")
15
+ .then((response) => response.text())
16
+ .then(console.log);`,
17
+ },
18
+ [generateVizFileId()]: {
19
+ name: "data.csv",
20
+ text: `Hello, Fetch!`,
21
+ },
22
+ };
@@ -0,0 +1,6 @@
1
+ export { basicHTML } from "./basicHTML";
2
+ export { jsScriptTag } from "./jsScriptTag";
3
+ export { fetchProxy } from "./fetchProxy";
4
+ export { styleTest } from "./styleTest";
5
+ export { xmlTest } from "./xmlTest";
6
+ export { protocolTest } from "./protocolTest";
@@ -0,0 +1,6 @@
1
+ export { basicHTML } from "./basicHTML";
2
+ export { jsScriptTag } from "./jsScriptTag";
3
+ export { fetchProxy } from "./fetchProxy";
4
+ export { styleTest } from "./styleTest";
5
+ export { xmlTest } from "./xmlTest";
6
+ export { protocolTest } from "./protocolTest";
@@ -0,0 +1,2 @@
1
+ import { VizFiles } from "@vizhub/viz-types";
2
+ export declare const jsScriptTag: VizFiles;
@@ -0,0 +1,16 @@
1
+ import { generateVizFileId } from "@vizhub/viz-types";
2
+ export const jsScriptTag = {
3
+ [generateVizFileId()]: {
4
+ name: "index.html",
5
+ text: `<!DOCTYPE html>
6
+ <html>
7
+ <body>
8
+ <script src="index.js"></script>
9
+ </body>
10
+ </html>`,
11
+ },
12
+ [generateVizFileId()]: {
13
+ name: "index.js",
14
+ text: `console.log('Hello, JS!');`,
15
+ },
16
+ };
@@ -0,0 +1,5 @@
1
+ export declare const basicHTML: {
2
+ "index.html": {
3
+ content: string;
4
+ };
5
+ };
@@ -0,0 +1,17 @@
1
+ export const basicHTML = {
2
+ "index.html": {
3
+ content: `<!DOCTYPE html>
4
+ <html>
5
+ <head>
6
+ <title>My HTML Document</title>
7
+ </head>
8
+ <body>
9
+ <h1>Hello, World!</h1>
10
+ <p>This is my first HTML document.</p>
11
+ <script>
12
+ console.log('Hello, World!');
13
+ </script>
14
+ </body>
15
+ </html>`,
16
+ },
17
+ };
@@ -0,0 +1,11 @@
1
+ export declare const fetchProxy: {
2
+ "index.html": {
3
+ content: string;
4
+ };
5
+ "index.js": {
6
+ content: string;
7
+ };
8
+ "data.csv": {
9
+ content: string;
10
+ };
11
+ };
@@ -0,0 +1,18 @@
1
+ export const fetchProxy = {
2
+ "index.html": {
3
+ content: `<!DOCTYPE html>
4
+ <html>
5
+ <body>
6
+ <script src="index.js"></script>
7
+ </body>
8
+ </html>`,
9
+ },
10
+ "index.js": {
11
+ content: `fetch("data.csv")
12
+ .then((response) => response.text())
13
+ .then(console.log);`,
14
+ },
15
+ "data.csv": {
16
+ content: `Hello, Fetch!`,
17
+ },
18
+ };
@@ -0,0 +1,6 @@
1
+ export { basicHTML } from "./basicHTML";
2
+ export { jsScriptTag } from "./jsScriptTag";
3
+ export { fetchProxy } from "./fetchProxy";
4
+ export { styleTest } from "./styleTest";
5
+ export { xmlTest } from "./xmlTest";
6
+ export { protocolTest } from "./protocolTest";
@@ -0,0 +1,6 @@
1
+ export { basicHTML } from "./basicHTML";
2
+ export { jsScriptTag } from "./jsScriptTag";
3
+ export { fetchProxy } from "./fetchProxy";
4
+ export { styleTest } from "./styleTest";
5
+ export { xmlTest } from "./xmlTest";
6
+ export { protocolTest } from "./protocolTest";
@@ -0,0 +1,8 @@
1
+ export declare const jsScriptTag: {
2
+ "index.html": {
3
+ content: string;
4
+ };
5
+ "index.js": {
6
+ content: string;
7
+ };
8
+ };
@@ -0,0 +1,13 @@
1
+ export const jsScriptTag = {
2
+ "index.html": {
3
+ content: `<!DOCTYPE html>
4
+ <html>
5
+ <body>
6
+ <script src="index.js"></script>
7
+ </body>
8
+ </html>`,
9
+ },
10
+ "index.js": {
11
+ content: `console.log('Hello, JS!');`,
12
+ },
13
+ };
@@ -0,0 +1,5 @@
1
+ export declare const protocolTest: {
2
+ "index.html": {
3
+ content: string;
4
+ };
5
+ };
@@ -0,0 +1,17 @@
1
+ export const protocolTest = {
2
+ "index.html": {
3
+ content: `<!DOCTYPE html>
4
+ <html>
5
+ <head>
6
+ <link href="//fonts.googleapis.com/css?family=Roboto" rel="stylesheet">
7
+ <script src="//code.jquery.com/jquery-3.6.0.min.js"></script>
8
+ </head>
9
+ <body>
10
+ <div>Protocol Test</div>
11
+ <script>
12
+ console.log('Protocol test loaded');
13
+ </script>
14
+ </body>
15
+ </html>`,
16
+ },
17
+ };
@@ -0,0 +1,8 @@
1
+ export declare const styleTest: {
2
+ "index.html": {
3
+ content: string;
4
+ };
5
+ "styles.css": {
6
+ content: string;
7
+ };
8
+ };
@@ -0,0 +1,19 @@
1
+ export const styleTest = {
2
+ "index.html": {
3
+ content: `<!DOCTYPE html>
4
+ <html>
5
+ <head>
6
+ <link rel="stylesheet" href="styles.css">
7
+ </head>
8
+ <body>
9
+ <div id="test">Test</div>
10
+ <script>
11
+ console.log(window.getComputedStyle(document.getElementById('test')).color);
12
+ </script>
13
+ </body>
14
+ </html>`
15
+ },
16
+ "styles.css": {
17
+ content: `#test { color: rgb(255, 0, 0); }`
18
+ }
19
+ };
@@ -0,0 +1,8 @@
1
+ export declare const xmlTest: {
2
+ "index.html": {
3
+ content: string;
4
+ };
5
+ "data.xml": {
6
+ content: string;
7
+ };
8
+ };
@@ -0,0 +1,23 @@
1
+ export const xmlTest = {
2
+ "index.html": {
3
+ content: `<!DOCTYPE html>
4
+ <html>
5
+ <body>
6
+ <script>
7
+ const xhr = new XMLHttpRequest();
8
+ xhr.open('GET', 'data.xml');
9
+ xhr.onload = () => {
10
+ console.log(xhr.responseXML.documentElement.nodeName);
11
+ };
12
+ xhr.send();
13
+ </script>
14
+ </body>
15
+ </html>`
16
+ },
17
+ "data.xml": {
18
+ content: `<?xml version="1.0" encoding="UTF-8"?>
19
+ <root>
20
+ <item>Test</item>
21
+ </root>`
22
+ }
23
+ };
@@ -0,0 +1,2 @@
1
+ import { VizFiles } from "@vizhub/viz-types";
2
+ export declare const protocolTest: VizFiles;
@@ -0,0 +1,19 @@
1
+ import { generateVizFileId } from "@vizhub/viz-types";
2
+ export const protocolTest = {
3
+ [generateVizFileId()]: {
4
+ name: "index.html",
5
+ text: `<!DOCTYPE html>
6
+ <html>
7
+ <head>
8
+ <link href="//fonts.googleapis.com/css?family=Roboto" rel="stylesheet">
9
+ <script src="//code.jquery.com/jquery-3.6.0.min.js"></script>
10
+ </head>
11
+ <body>
12
+ <div>Protocol Test</div>
13
+ <script>
14
+ console.log('Protocol test loaded');
15
+ </script>
16
+ </body>
17
+ </html>`,
18
+ },
19
+ };
@@ -0,0 +1,2 @@
1
+ import { VizFiles } from "@vizhub/viz-types";
2
+ export declare const styleTest: VizFiles;
@@ -0,0 +1,22 @@
1
+ import { generateVizFileId } from "@vizhub/viz-types";
2
+ export const styleTest = {
3
+ [generateVizFileId()]: {
4
+ name: "index.html",
5
+ text: `<!DOCTYPE html>
6
+ <html>
7
+ <head>
8
+ <link rel="stylesheet" href="styles.css">
9
+ </head>
10
+ <body>
11
+ <div id="test">Test</div>
12
+ <script>
13
+ console.log(window.getComputedStyle(document.getElementById('test')).color);
14
+ </script>
15
+ </body>
16
+ </html>`,
17
+ },
18
+ [generateVizFileId()]: {
19
+ name: "styles.css",
20
+ text: `#test { color: rgb(255, 0, 0); }`,
21
+ },
22
+ };
@@ -0,0 +1,3 @@
1
+ export declare const basicHTML: {
2
+ "index.html": string;
3
+ };