@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.
- package/LICENSE +21 -0
- package/README.md +240 -194
- package/dist/common/packageJson.d.ts +39 -0
- package/dist/common/packageJson.js +71 -0
- package/dist/common/packageJson.test.d.ts +1 -0
- package/dist/common/packageJson.test.js +146 -0
- package/dist/common/sucrasePlugin.d.ts +2 -0
- package/dist/common/sucrasePlugin.js +24 -0
- package/dist/common/virtualFileSystem.d.ts +3 -0
- package/dist/common/virtualFileSystem.js +75 -0
- package/dist/determineRuntimeVersion.d.ts +2 -0
- package/dist/determineRuntimeVersion.js +22 -0
- package/dist/determineRuntimeVersion.test.d.ts +1 -0
- package/dist/determineRuntimeVersion.test.js +101 -0
- package/dist/fixtures/basicHTML.d.ts +2 -0
- package/dist/fixtures/basicHTML.js +22 -0
- package/dist/fixtures/fetchProxy.d.ts +2 -0
- package/dist/fixtures/fetchProxy.js +25 -0
- package/dist/fixtures/index.d.ts +3 -0
- package/dist/fixtures/index.js +9 -0
- package/dist/fixtures/jsScriptTag.d.ts +2 -0
- package/dist/fixtures/jsScriptTag.js +19 -0
- package/dist/index.d.ts +14 -8
- package/dist/index.js +59 -24882
- package/dist/index.test.d.ts +1 -0
- package/dist/index.test.js +50 -0
- package/dist/magicSandbox/fixProtocollessUrls.d.ts +4 -0
- package/dist/magicSandbox/fixProtocollessUrls.js +10 -0
- package/dist/magicSandbox/generateInterceptorScript.d.ts +4 -0
- package/dist/magicSandbox/generateInterceptorScript.js +178 -0
- package/dist/magicSandbox/index.d.ts +19 -0
- package/dist/magicSandbox/index.js +48 -0
- package/dist/test/fixtures/basicHTML.d.ts +2 -0
- package/dist/test/fixtures/basicHTML.js +19 -0
- package/dist/test/fixtures/fetchProxy.d.ts +2 -0
- package/dist/test/fixtures/fetchProxy.js +22 -0
- package/dist/test/fixtures/index.d.ts +6 -0
- package/dist/test/fixtures/index.js +6 -0
- package/dist/test/fixtures/jsScriptTag.d.ts +2 -0
- package/dist/test/fixtures/jsScriptTag.js +16 -0
- package/dist/test/fixtures/magicSandbox/basicHTML.d.ts +5 -0
- package/dist/test/fixtures/magicSandbox/basicHTML.js +17 -0
- package/dist/test/fixtures/magicSandbox/fetchProxy.d.ts +11 -0
- package/dist/test/fixtures/magicSandbox/fetchProxy.js +18 -0
- package/dist/test/fixtures/magicSandbox/index.d.ts +6 -0
- package/dist/test/fixtures/magicSandbox/index.js +6 -0
- package/dist/test/fixtures/magicSandbox/jsScriptTag.d.ts +8 -0
- package/dist/test/fixtures/magicSandbox/jsScriptTag.js +13 -0
- package/dist/test/fixtures/magicSandbox/protocolTest.d.ts +5 -0
- package/dist/test/fixtures/magicSandbox/protocolTest.js +17 -0
- package/dist/test/fixtures/magicSandbox/styleTest.d.ts +8 -0
- package/dist/test/fixtures/magicSandbox/styleTest.js +19 -0
- package/dist/test/fixtures/magicSandbox/xmlTest.d.ts +8 -0
- package/dist/test/fixtures/magicSandbox/xmlTest.js +23 -0
- package/dist/test/fixtures/protocolTest.d.ts +2 -0
- package/dist/test/fixtures/protocolTest.js +19 -0
- package/dist/test/fixtures/styleTest.d.ts +2 -0
- package/dist/test/fixtures/styleTest.js +22 -0
- package/dist/test/fixtures/v1/basicHTML.d.ts +3 -0
- package/dist/test/fixtures/v1/basicHTML.js +15 -0
- package/dist/test/fixtures/v1/fetchProxy.d.ts +5 -0
- package/dist/test/fixtures/v1/fetchProxy.js +12 -0
- package/dist/test/fixtures/v1/index.d.ts +6 -0
- package/dist/test/fixtures/v1/index.js +6 -0
- package/dist/test/fixtures/v1/jsScriptTag.d.ts +4 -0
- package/dist/test/fixtures/v1/jsScriptTag.js +9 -0
- package/dist/test/fixtures/v1/protocolTest.d.ts +3 -0
- package/dist/test/fixtures/v1/protocolTest.js +15 -0
- package/dist/test/fixtures/v1/styleTest.d.ts +4 -0
- package/dist/test/fixtures/v1/styleTest.js +15 -0
- package/dist/test/fixtures/v1/xmlTest.d.ts +4 -0
- package/dist/test/fixtures/v1/xmlTest.js +19 -0
- package/dist/test/fixtures/v2/basicBundle.d.ts +5 -0
- package/dist/test/fixtures/v2/basicBundle.js +5 -0
- package/dist/test/fixtures/v2/basicBundleNoExtension.d.ts +5 -0
- package/dist/test/fixtures/v2/basicBundleNoExtension.js +5 -0
- package/dist/test/fixtures/v2/d3Import.d.ts +4 -0
- package/dist/test/fixtures/v2/d3Import.js +7 -0
- package/dist/test/fixtures/v2/d3ImportPkg.d.ts +5 -0
- package/dist/test/fixtures/v2/d3ImportPkg.js +19 -0
- package/dist/test/fixtures/v2/d3RosettaImportPkg.d.ts +5 -0
- package/dist/test/fixtures/v2/d3RosettaImportPkg.js +19 -0
- package/dist/test/fixtures/v2/es6Preserve.d.ts +4 -0
- package/dist/test/fixtures/v2/es6Preserve.js +6 -0
- package/dist/test/fixtures/v2/generatorSupport.d.ts +4 -0
- package/dist/test/fixtures/v2/generatorSupport.js +6 -0
- package/dist/test/fixtures/v2/index.d.ts +14 -0
- package/dist/test/fixtures/v2/index.js +15 -0
- package/dist/test/fixtures/v2/jsxTranspile.d.ts +4 -0
- package/dist/test/fixtures/v2/jsxTranspile.js +6 -0
- package/dist/test/fixtures/v2/reactDomImport.d.ts +4 -0
- package/dist/test/fixtures/v2/reactDomImport.js +8 -0
- package/dist/test/fixtures/v2/reactDomImportPkg.d.ts +5 -0
- package/dist/test/fixtures/v2/reactDomImportPkg.js +24 -0
- package/dist/test/fixtures/v2/reactImport.d.ts +4 -0
- package/dist/test/fixtures/v2/reactImport.js +7 -0
- package/dist/test/fixtures/v2/reactImportPkg.d.ts +5 -0
- package/dist/test/fixtures/v2/reactImportPkg.js +17 -0
- package/dist/test/fixtures/v2/sourceMapErrorFixture.d.ts +5 -0
- package/dist/test/fixtures/v2/sourceMapErrorFixture.js +19 -0
- package/dist/test/fixtures/v2/unicodeSupport.d.ts +4 -0
- package/dist/test/fixtures/v2/unicodeSupport.js +6 -0
- package/dist/test/fixtures/v3/basicIndexJS.d.ts +3 -0
- package/dist/test/fixtures/v3/basicIndexJS.js +7 -0
- package/dist/test/fixtures/v3/cssImport.d.ts +4 -0
- package/dist/test/fixtures/v3/cssImport.js +9 -0
- package/dist/test/fixtures/v3/csvImport.d.ts +4 -0
- package/dist/test/fixtures/v3/csvImport.js +11 -0
- package/dist/test/fixtures/v3/csvStrangeChars.d.ts +4 -0
- package/dist/test/fixtures/v3/csvStrangeChars.js +12 -0
- package/dist/test/fixtures/v3/index.d.ts +9 -0
- package/dist/test/fixtures/v3/index.js +9 -0
- package/dist/test/fixtures/v3/jsExport.d.ts +4 -0
- package/dist/test/fixtures/v3/jsExport.js +10 -0
- package/dist/test/fixtures/v3/sourcemap.d.ts +3 -0
- package/dist/test/fixtures/v3/sourcemap.js +10 -0
- package/dist/test/fixtures/v3/svelte.d.ts +4 -0
- package/dist/test/fixtures/v3/svelte.js +18 -0
- package/dist/test/fixtures/v3/vizContent.d.ts +2 -0
- package/dist/test/fixtures/v3/vizContent.js +11 -0
- package/dist/test/fixtures/v3/vizImport.d.ts +6 -0
- package/dist/test/fixtures/v3/vizImport.js +85 -0
- package/dist/test/fixtures/v4/d3Usage.d.ts +5 -0
- package/dist/test/fixtures/v4/d3Usage.js +31 -0
- package/dist/test/fixtures/v4/esmBuild.d.ts +5 -0
- package/dist/test/fixtures/v4/esmBuild.js +18 -0
- package/dist/test/fixtures/v4/fetchInterception.d.ts +5 -0
- package/dist/test/fixtures/v4/fetchInterception.js +18 -0
- package/dist/test/fixtures/v4/index.d.ts +9 -0
- package/dist/test/fixtures/v4/index.js +9 -0
- package/dist/test/fixtures/v4/jsScriptTagTypeModule.d.ts +4 -0
- package/dist/test/fixtures/v4/jsScriptTagTypeModule.js +18 -0
- package/dist/test/fixtures/v4/jsScriptTagTypeModules.d.ts +5 -0
- package/dist/test/fixtures/v4/jsScriptTagTypeModules.js +12 -0
- package/dist/test/fixtures/v4/reactHooks.d.ts +6 -0
- package/dist/test/fixtures/v4/reactHooks.js +47 -0
- package/dist/test/fixtures/v4/reactJsx.d.ts +6 -0
- package/dist/test/fixtures/v4/reactJsx.js +34 -0
- package/dist/test/fixtures/v4/threeJsUsage.d.ts +4 -0
- package/dist/test/fixtures/v4/threeJsUsage.js +24 -0
- package/dist/test/fixtures/v4/typeScriptSupport.d.ts +5 -0
- package/dist/test/fixtures/v4/typeScriptSupport.js +29 -0
- package/dist/test/fixtures/xmlTest.d.ts +2 -0
- package/dist/test/fixtures/xmlTest.js +26 -0
- package/dist/test/index.test.d.ts +1 -0
- package/dist/test/index.test.js +40 -0
- package/dist/test/testInBrowser.d.ts +16 -0
- package/dist/test/testInBrowser.js +47 -0
- package/dist/test/testStackTrace.d.ts +9 -0
- package/dist/test/testStackTrace.js +55 -0
- package/dist/test/v1.test.d.ts +1 -0
- package/dist/test/v1.test.js +65 -0
- package/dist/test/v2.test.d.ts +1 -0
- package/dist/test/v2.test.js +121 -0
- package/dist/test/v3.test.d.ts +1 -0
- package/dist/test/v3.test.js +114 -0
- package/dist/test/v4.test.d.ts +1 -0
- package/dist/test/v4.test.js +100 -0
- package/dist/types.d.ts +2 -0
- package/dist/types.js +1 -0
- package/dist/utils/getFileText.d.ts +2 -0
- package/dist/utils/getFileText.js +13 -0
- package/dist/utils/getFileText.test.d.ts +1 -0
- package/dist/utils/getFileText.test.js +72 -0
- package/dist/utils/vizContentToFileCollection.d.ts +8 -0
- package/dist/utils/vizContentToFileCollection.js +19 -0
- package/dist/utils/vizContentToFileCollection.test.d.ts +1 -0
- package/dist/utils/vizContentToFileCollection.test.js +75 -0
- package/dist/v2/computeBundleJSV2.d.ts +7 -0
- package/dist/v2/computeBundleJSV2.js +29 -0
- package/dist/v2/getComputedIndexHtml.d.ts +4 -0
- package/dist/v2/getComputedIndexHtml.js +81 -0
- package/dist/v2/getComputedIndexHtml.test.d.ts +1 -0
- package/dist/v2/getComputedIndexHtml.test.js +134 -0
- package/dist/v2/index.d.ts +1 -0
- package/dist/v2/index.js +1 -0
- package/dist/v2/v2Build.d.ts +7 -0
- package/dist/v2/v2Build.js +11 -0
- package/dist/v3/computeBundleJSV3.d.ts +18 -0
- package/dist/v3/computeBundleJSV3.js +48 -0
- package/dist/v3/createVizContent.d.ts +9 -0
- package/dist/v3/createVizContent.js +23 -0
- package/dist/{v3Runtime → v3}/extractVizImport.d.ts +2 -3
- package/dist/v3/extractVizImport.js +23 -0
- package/dist/v3/extractVizImport.test.d.ts +1 -0
- package/dist/v3/extractVizImport.test.js +30 -0
- package/dist/v3/htmlTemplate.d.ts +5 -0
- package/dist/v3/htmlTemplate.js +89 -0
- package/dist/v3/index.d.ts +1 -0
- package/dist/v3/index.js +1 -0
- package/dist/v3/parseId.d.ts +6 -0
- package/dist/v3/parseId.js +5 -0
- package/dist/v3/parseId.test.d.ts +1 -0
- package/dist/v3/parseId.test.js +25 -0
- package/dist/v3/slugCache.d.ts +11 -0
- package/dist/v3/slugCache.js +35 -0
- package/dist/v3/slugCache.test.d.ts +1 -0
- package/dist/v3/slugCache.test.js +105 -0
- package/dist/v3/transformDSV/dsvParseSrc.d.ts +1 -0
- package/dist/v3/transformDSV/dsvParseSrc.js +121 -0
- package/dist/v3/transformDSV/index.d.ts +2 -0
- package/dist/v3/transformDSV/index.js +40 -0
- package/dist/{v3Runtime → v3}/transformSvelte.d.ts +4 -2
- package/dist/v3/transformSvelte.js +77 -0
- package/dist/{v3Runtime → v3}/types.d.ts +18 -18
- package/dist/v3/types.js +1 -0
- package/dist/v3/v3Build.d.ts +15 -0
- package/dist/v3/v3Build.js +30 -0
- package/dist/v3/vizCache.d.ts +10 -0
- package/dist/v3/vizCache.js +36 -0
- package/dist/v3/vizCache.test.d.ts +1 -0
- package/dist/v3/vizCache.test.js +98 -0
- package/dist/{v3Runtime → v3}/vizLoad.d.ts +3 -4
- package/dist/v3/vizLoad.js +46 -0
- package/dist/v3/vizResolve.d.ts +7 -0
- package/dist/v3/vizResolve.js +69 -0
- package/dist/v4/index.d.ts +10 -0
- package/dist/v4/index.js +134 -0
- package/package.json +35 -38
- package/dist/assets/setupV3Runtime-BVr5lyKp.js +0 -240
- package/dist/computeSrcDoc.d.ts +0 -13
- package/dist/computeSrcDoc.d.ts.map +0 -1
- package/dist/computeSrcDoc.test.d.ts +0 -2
- package/dist/computeSrcDoc.test.d.ts.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/useRuntime.d.ts +0 -13
- package/dist/useRuntime.d.ts.map +0 -1
- package/dist/v2Runtime/bundle/bubleJSXOnly.d.ts +0 -5
- package/dist/v2Runtime/bundle/bubleJSXOnly.d.ts.map +0 -1
- package/dist/v2Runtime/bundle/packageJson.d.ts +0 -10
- package/dist/v2Runtime/bundle/packageJson.d.ts.map +0 -1
- package/dist/v2Runtime/computeSrcDocV2.d.ts +0 -3
- package/dist/v2Runtime/computeSrcDocV2.d.ts.map +0 -1
- package/dist/v2Runtime/computeSrcDocV2.test.d.ts +0 -2
- package/dist/v2Runtime/computeSrcDocV2.test.d.ts.map +0 -1
- package/dist/v2Runtime/getComputedIndexHtml.d.ts +0 -4
- package/dist/v2Runtime/getComputedIndexHtml.d.ts.map +0 -1
- package/dist/v2Runtime/getComputedIndexHtml.test.d.ts +0 -2
- package/dist/v2Runtime/getComputedIndexHtml.test.d.ts.map +0 -1
- package/dist/v2Runtime/getText.d.ts +0 -3
- package/dist/v2Runtime/getText.d.ts.map +0 -1
- package/dist/v2Runtime/transformFiles.d.ts +0 -3
- package/dist/v2Runtime/transformFiles.d.ts.map +0 -1
- package/dist/v2Runtime/v3FilesToV2Files.d.ts +0 -3
- package/dist/v2Runtime/v3FilesToV2Files.d.ts.map +0 -1
- package/dist/v2Runtime/v3FilesToV2Files.test.d.ts +0 -2
- package/dist/v2Runtime/v3FilesToV2Files.test.d.ts.map +0 -1
- package/dist/v3Runtime/build.d.ts +0 -14
- package/dist/v3Runtime/build.d.ts.map +0 -1
- package/dist/v3Runtime/build.test.d.ts +0 -2
- package/dist/v3Runtime/build.test.d.ts.map +0 -1
- package/dist/v3Runtime/cleanRollupErrorMessage.d.ts +0 -5
- package/dist/v3Runtime/cleanRollupErrorMessage.d.ts.map +0 -1
- package/dist/v3Runtime/computeSrcDocV3.d.ts +0 -7
- package/dist/v3Runtime/computeSrcDocV3.d.ts.map +0 -1
- package/dist/v3Runtime/extractVizImport.d.ts.map +0 -1
- package/dist/v3Runtime/extractVizImport.test.d.ts +0 -2
- package/dist/v3Runtime/extractVizImport.test.d.ts.map +0 -1
- package/dist/v3Runtime/generateRollupErrorMessage.d.ts +0 -5
- package/dist/v3Runtime/generateRollupErrorMessage.d.ts.map +0 -1
- package/dist/v3Runtime/importFromViz.d.ts +0 -1
- package/dist/v3Runtime/importFromViz.d.ts.map +0 -1
- package/dist/v3Runtime/index.d.ts +0 -2
- package/dist/v3Runtime/index.d.ts.map +0 -1
- package/dist/v3Runtime/parseId.d.ts +0 -7
- package/dist/v3Runtime/parseId.d.ts.map +0 -1
- package/dist/v3Runtime/setupV3Runtime.d.ts +0 -15
- package/dist/v3Runtime/setupV3Runtime.d.ts.map +0 -1
- package/dist/v3Runtime/transformDSV/index.d.ts +0 -3
- package/dist/v3Runtime/transformDSV/index.d.ts.map +0 -1
- package/dist/v3Runtime/transformSvelte.d.ts.map +0 -1
- package/dist/v3Runtime/types.d.ts.map +0 -1
- package/dist/v3Runtime/urlLoad.d.ts +0 -1
- package/dist/v3Runtime/urlLoad.d.ts.map +0 -1
- package/dist/v3Runtime/virtual.d.ts +0 -1
- package/dist/v3Runtime/virtual.d.ts.map +0 -1
- package/dist/v3Runtime/vizCache.d.ts +0 -11
- package/dist/v3Runtime/vizCache.d.ts.map +0 -1
- package/dist/v3Runtime/vizCache.test.d.ts +0 -2
- package/dist/v3Runtime/vizCache.test.d.ts.map +0 -1
- package/dist/v3Runtime/vizLoad.d.ts.map +0 -1
- package/dist/v3Runtime/vizLoadSvelte.d.ts +0 -1
- package/dist/v3Runtime/vizLoadSvelte.d.ts.map +0 -1
- package/dist/v3Runtime/vizResolve.d.ts +0 -10
- package/dist/v3Runtime/vizResolve.d.ts.map +0 -1
- package/dist/v3Runtime/worker.d.ts +0 -2
- 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,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,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,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,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,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,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,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,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,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,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,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,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
|
+
};
|