@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.
- package/LICENSE +21 -0
- package/README.md +376 -178
- package/dist/buildHTML.d.ts +14 -0
- package/dist/buildHTML.js +67 -0
- 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 +3 -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 +3 -8
- package/dist/index.js +3 -24890
- 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 +15 -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 +1 -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 +7 -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/{v3Runtime → v3}/cleanRollupErrorMessage.d.ts +0 -1
- package/dist/v3/cleanRollupErrorMessage.js +9 -0
- package/dist/v3/computeBundleJSV3.d.ts +17 -0
- package/dist/v3/computeBundleJSV3.js +48 -0
- package/dist/v3/createVizContent.d.ts +8 -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/{v3Runtime → v3}/setupV3Runtime.d.ts +5 -5
- package/dist/v3/setupV3Runtime.js +341 -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 +14 -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 +37 -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.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.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,341 @@
|
|
1
|
+
import { parseId } from "./parseId";
|
2
|
+
import { cleanRollupErrorMessage } from "./cleanRollupErrorMessage";
|
3
|
+
import { getFileText } from "@vizhub/viz-utils";
|
4
|
+
// Flag for debugging.
|
5
|
+
const debug = false;
|
6
|
+
// Nothing happening.
|
7
|
+
const IDLE = "IDLE";
|
8
|
+
// An update has been enqueued
|
9
|
+
// via requestAnimationFrame.
|
10
|
+
const ENQUEUED = "ENQUEUED";
|
11
|
+
// An update (build and run) is pending,
|
12
|
+
// and the files have not changed.
|
13
|
+
const PENDING_CLEAN = "PENDING_CLEAN";
|
14
|
+
// An update (build and run) is pending,
|
15
|
+
// and the files have changed
|
16
|
+
// while this run is taking place.
|
17
|
+
const PENDING_DIRTY = "PENDING_DIRTY";
|
18
|
+
export const setupV3Runtime = ({ vizId, iframe, setSrcdocErrorMessage, getLatestContent, resolveSlugKey, writeFile, worker, }) => {
|
19
|
+
// Valid State Transitions:
|
20
|
+
//
|
21
|
+
// * IDLE --> ENQUEUED
|
22
|
+
// When the system is idle and files are changed.
|
23
|
+
//
|
24
|
+
// * ENQUEUED --> PENDING_CLEAN
|
25
|
+
// When the pending changes run.
|
26
|
+
//
|
27
|
+
// * PENDING_CLEAN --> IDLE
|
28
|
+
// When the pending update finishes running
|
29
|
+
// and files were not changed in the mean time.
|
30
|
+
//
|
31
|
+
// * PENDING_CLEAN --> PENDING_DIRTY
|
32
|
+
// When files are changed while an update is pending.
|
33
|
+
//
|
34
|
+
// * PENDING_DIRTY --> ENQUEUED
|
35
|
+
// When the pending update finishes running
|
36
|
+
// and files were changed in the mean time.
|
37
|
+
//
|
38
|
+
// When a build error happens, the state is set to IDLE.
|
39
|
+
// This is to prevent a build error from causing
|
40
|
+
// the whole system to stop working.
|
41
|
+
//
|
42
|
+
// Valid State Transitions (with build errors):
|
43
|
+
// TODO complete this section
|
44
|
+
let state = IDLE;
|
45
|
+
if (debug) {
|
46
|
+
setInterval(() => {
|
47
|
+
console.log("state", state);
|
48
|
+
}, 1000);
|
49
|
+
}
|
50
|
+
// Pending promise resolvers.
|
51
|
+
let pendingBuildPromise = null;
|
52
|
+
let pendingRunPromise = null;
|
53
|
+
// Logic around profiling build times.
|
54
|
+
const profileBuildTimes = debug;
|
55
|
+
let buildTimes = [];
|
56
|
+
const avg = (arr) => arr.reduce((a, b) => a + b, 0) / arr.length;
|
57
|
+
const n = 100;
|
58
|
+
// This runs when the build worker sends a message.
|
59
|
+
worker.addEventListener("message", async ({ data }) => {
|
60
|
+
const message = data;
|
61
|
+
// Handle 'buildResponse' messages.
|
62
|
+
// These are sent by the build worker in response
|
63
|
+
// to a 'buildRequest' message.
|
64
|
+
if (message.type === "buildResponse") {
|
65
|
+
const buildResult = message.buildResult;
|
66
|
+
const error = message.error;
|
67
|
+
if (profileBuildTimes && buildResult) {
|
68
|
+
buildTimes.push(buildResult.time);
|
69
|
+
// Every n times, log the rolling average.
|
70
|
+
if (buildTimes.length % n === 0) {
|
71
|
+
console.log("Average build time: " +
|
72
|
+
avg(buildTimes) +
|
73
|
+
" ms");
|
74
|
+
buildTimes = [];
|
75
|
+
}
|
76
|
+
}
|
77
|
+
// Regardless of whether the build succeeded or failed,
|
78
|
+
// resolve the pending build promise,
|
79
|
+
// so that the system remains responsive.
|
80
|
+
if (pendingBuildPromise) {
|
81
|
+
pendingBuildPromise(buildResult);
|
82
|
+
pendingBuildPromise = null;
|
83
|
+
}
|
84
|
+
if (error) {
|
85
|
+
setSrcdocErrorMessage(cleanRollupErrorMessage({
|
86
|
+
rawMessage: error.message,
|
87
|
+
vizId,
|
88
|
+
}));
|
89
|
+
}
|
90
|
+
}
|
91
|
+
// Handle 'contentRequest' messages.
|
92
|
+
// These are sent by the worker when it needs
|
93
|
+
// to get the content of a file, in order to
|
94
|
+
// populate its VizCache.
|
95
|
+
if (message.type === "contentRequest") {
|
96
|
+
const { vizId } = message;
|
97
|
+
const content = await getLatestContent(vizId);
|
98
|
+
const contentResponseMessage = {
|
99
|
+
type: "contentResponse",
|
100
|
+
vizId: message.vizId,
|
101
|
+
content,
|
102
|
+
};
|
103
|
+
if (debug) {
|
104
|
+
console.log("[v3 runtime] received contentRequest, sending contentResponse", contentResponseMessage);
|
105
|
+
}
|
106
|
+
// Send the content back to the worker.
|
107
|
+
worker.postMessage(contentResponseMessage);
|
108
|
+
}
|
109
|
+
// Handle 'resolveSlugRequest' messages.
|
110
|
+
// These are sent by the worker when it needs
|
111
|
+
// to resolve a slug import to a viz ID.
|
112
|
+
if (message.type === "resolveSlugRequest") {
|
113
|
+
const { slugKey } = message;
|
114
|
+
const resolveSlugResponseMessage = {
|
115
|
+
type: "resolveSlugResponse",
|
116
|
+
slugKey,
|
117
|
+
requestId: message.requestId,
|
118
|
+
vizId: await resolveSlugKey(slugKey),
|
119
|
+
};
|
120
|
+
if (debug) {
|
121
|
+
console.log("[v3 runtime] received resolveSlugRequest, sending resolveSlugResponse", resolveSlugResponseMessage);
|
122
|
+
}
|
123
|
+
// Send the viz ID back to the worker.
|
124
|
+
worker.postMessage(resolveSlugResponseMessage);
|
125
|
+
}
|
126
|
+
// Handle 'invalidateVizCacheResponse' messages.
|
127
|
+
// These are sent by the worker in response to
|
128
|
+
// an 'invalidateVizCacheRequest' message.
|
129
|
+
if (message.type === "invalidateVizCacheResponse") {
|
130
|
+
if (debug) {
|
131
|
+
console.log("[v3 runtime] received invalidateVizCacheResponse", message);
|
132
|
+
}
|
133
|
+
// Leverage existing infra for executing the hot reloading.
|
134
|
+
handleCodeChange();
|
135
|
+
}
|
136
|
+
if (message.type === "resetSrcdocResponse") {
|
137
|
+
const srcdoc = message.srcdoc;
|
138
|
+
const error = message.error;
|
139
|
+
if (error) {
|
140
|
+
setSrcdocErrorMessage(cleanRollupErrorMessage({
|
141
|
+
rawMessage: error.message,
|
142
|
+
vizId,
|
143
|
+
}));
|
144
|
+
}
|
145
|
+
else {
|
146
|
+
setSrcdocErrorMessage(null);
|
147
|
+
// Really reset the srcdoc!
|
148
|
+
// console.log('Really reset the srcdoc!');
|
149
|
+
if (srcdoc) {
|
150
|
+
iframe.srcdoc = srcdoc;
|
151
|
+
}
|
152
|
+
}
|
153
|
+
}
|
154
|
+
});
|
155
|
+
// This runs when the IFrame sends a message.
|
156
|
+
window.addEventListener("message", ({ data }) => {
|
157
|
+
// Handle 'runDone' and 'runError' messages.
|
158
|
+
// These happen in response to sending a 'runJS' message.
|
159
|
+
if (data.type === "runDone" ||
|
160
|
+
data.type === "runError") {
|
161
|
+
// console.log('got ' + data.type);
|
162
|
+
if (pendingRunPromise) {
|
163
|
+
// TODO pass errors out for display
|
164
|
+
// pendingRunPromise(data as V3WindowMessage);
|
165
|
+
pendingRunPromise();
|
166
|
+
pendingRunPromise = null;
|
167
|
+
}
|
168
|
+
}
|
169
|
+
if (data.type === "runError") {
|
170
|
+
setSrcdocErrorMessage(data.error.message);
|
171
|
+
}
|
172
|
+
if (data.type === "writeFile") {
|
173
|
+
if (data.fileName && data.content) {
|
174
|
+
writeFile(data.fileName, data.content);
|
175
|
+
}
|
176
|
+
}
|
177
|
+
});
|
178
|
+
const handleCodeChange = () => {
|
179
|
+
if (state === IDLE) {
|
180
|
+
state = ENQUEUED;
|
181
|
+
update();
|
182
|
+
}
|
183
|
+
else if (state === PENDING_CLEAN) {
|
184
|
+
state = PENDING_DIRTY;
|
185
|
+
}
|
186
|
+
};
|
187
|
+
// This runs when one or more imported vizzes are changed.
|
188
|
+
const invalidateVizCache = (changedVizIds) => {
|
189
|
+
// Send a message to the worker to invalidate the cache.
|
190
|
+
const message = {
|
191
|
+
type: "invalidateVizCacheRequest",
|
192
|
+
changedVizIds,
|
193
|
+
};
|
194
|
+
worker.postMessage(message);
|
195
|
+
};
|
196
|
+
const profileHotReloadFPS = true;
|
197
|
+
let updateCount = 0;
|
198
|
+
if (profileHotReloadFPS) {
|
199
|
+
setInterval(() => {
|
200
|
+
if (debug && updateCount > 0) {
|
201
|
+
console.log(updateCount +
|
202
|
+
" hot reload" +
|
203
|
+
(updateCount !== 1 ? "s" : "") +
|
204
|
+
" in the last second");
|
205
|
+
}
|
206
|
+
updateCount = 0;
|
207
|
+
}, 1000);
|
208
|
+
}
|
209
|
+
const build = () => {
|
210
|
+
return new Promise((resolve) => {
|
211
|
+
pendingBuildPromise = resolve;
|
212
|
+
const message = {
|
213
|
+
type: "buildRequest",
|
214
|
+
vizId,
|
215
|
+
enableSourcemap: true,
|
216
|
+
};
|
217
|
+
worker.postMessage(message);
|
218
|
+
});
|
219
|
+
};
|
220
|
+
// Builds and runs the latest files.
|
221
|
+
const update = async () => {
|
222
|
+
state = PENDING_CLEAN;
|
223
|
+
if (debug) {
|
224
|
+
console.log("update: before run");
|
225
|
+
}
|
226
|
+
// Build the code. This may fail and return `undefined`.
|
227
|
+
const buildResult = await build();
|
228
|
+
// If the build was successful, run the code.
|
229
|
+
if (buildResult !== undefined) {
|
230
|
+
await run(buildResult);
|
231
|
+
}
|
232
|
+
if (debug) {
|
233
|
+
console.log("update: after run");
|
234
|
+
}
|
235
|
+
updateCount++;
|
236
|
+
// TypeScript can't comprehend that `state`
|
237
|
+
// may change during the await calls above.
|
238
|
+
// @ts-ignore
|
239
|
+
if (state === PENDING_DIRTY) {
|
240
|
+
requestAnimationFrame(update);
|
241
|
+
state = ENQUEUED;
|
242
|
+
}
|
243
|
+
else {
|
244
|
+
state = IDLE;
|
245
|
+
}
|
246
|
+
};
|
247
|
+
let previousCSSFiles = [];
|
248
|
+
const run = (buildResult) => {
|
249
|
+
return new Promise((resolve) => {
|
250
|
+
const { src, warnings, cssFiles } = buildResult;
|
251
|
+
// Sanity check.
|
252
|
+
// At this point, since there were no errors,
|
253
|
+
// we expect there to be a `src` property.
|
254
|
+
// This should never happen, but log & error just in case!
|
255
|
+
if (src === undefined) {
|
256
|
+
if (debug) {
|
257
|
+
console.log("[v3 runtime] src is undefined, but no errors!");
|
258
|
+
}
|
259
|
+
throw new Error("[v3 runtime] src is undefined, but no errors!");
|
260
|
+
}
|
261
|
+
// Set pendingRunPromise because at this point,
|
262
|
+
// we expect an asynchronous response when the run is done.
|
263
|
+
// The iframe should send either a `runDone` or `runError` message.
|
264
|
+
pendingRunPromise = resolve;
|
265
|
+
// Handle build warnings
|
266
|
+
if (warnings.length > 0) {
|
267
|
+
// TODO: Distinguish between warnings and errors in UI
|
268
|
+
setSrcdocErrorMessage(warnings
|
269
|
+
.map((warning) => warning.message)
|
270
|
+
.join("\n\n"));
|
271
|
+
}
|
272
|
+
else {
|
273
|
+
setSrcdocErrorMessage(null); // Clear error message if no warnings
|
274
|
+
}
|
275
|
+
if (iframe.contentWindow) {
|
276
|
+
// For each cssFiles
|
277
|
+
for (const cssFile of cssFiles) {
|
278
|
+
const { vizId, fileName } = parseId(cssFile);
|
279
|
+
getLatestContent(vizId).then((content) => {
|
280
|
+
const src = getFileText(content, fileName);
|
281
|
+
if (src === null) {
|
282
|
+
// The file doesn't exist.
|
283
|
+
// TODO surface this error to the user
|
284
|
+
// in a nicer way than this.
|
285
|
+
console.warn(`Imported CSS file ${fileName} doesn't exist.`);
|
286
|
+
return;
|
287
|
+
}
|
288
|
+
// TODO only inject CSS if it has changed.
|
289
|
+
const runCSSMessage = {
|
290
|
+
type: "runCSS",
|
291
|
+
id: cssFile,
|
292
|
+
src,
|
293
|
+
};
|
294
|
+
if (debug) {
|
295
|
+
console.log("runCSSMessage", runCSSMessage);
|
296
|
+
}
|
297
|
+
iframe.contentWindow?.postMessage(runCSSMessage, window.location.origin);
|
298
|
+
});
|
299
|
+
}
|
300
|
+
// Detect which CSS files have been removed
|
301
|
+
// and remove them from the iframe.
|
302
|
+
const removedCSSFiles = previousCSSFiles.filter((id) => !cssFiles.includes(id));
|
303
|
+
previousCSSFiles = cssFiles;
|
304
|
+
if (debug) {
|
305
|
+
console.log("removedCSSFiles", removedCSSFiles);
|
306
|
+
}
|
307
|
+
for (const id of removedCSSFiles) {
|
308
|
+
const removeCSSMessage = {
|
309
|
+
type: "runCSS",
|
310
|
+
id,
|
311
|
+
src: "",
|
312
|
+
};
|
313
|
+
iframe.contentWindow?.postMessage(removeCSSMessage, window.location.origin);
|
314
|
+
}
|
315
|
+
// Clear the console before each run.
|
316
|
+
console.clear();
|
317
|
+
const runJSMessage = {
|
318
|
+
type: "runJS",
|
319
|
+
src,
|
320
|
+
};
|
321
|
+
iframe.contentWindow.postMessage(runJSMessage, window.location.origin);
|
322
|
+
}
|
323
|
+
});
|
324
|
+
};
|
325
|
+
const resetSrcdoc = (changedVizIds) => {
|
326
|
+
state = IDLE;
|
327
|
+
pendingBuildPromise = null;
|
328
|
+
pendingRunPromise = null;
|
329
|
+
const message = {
|
330
|
+
type: "resetSrcdocRequest",
|
331
|
+
vizId,
|
332
|
+
changedVizIds,
|
333
|
+
};
|
334
|
+
worker.postMessage(message);
|
335
|
+
};
|
336
|
+
return {
|
337
|
+
handleCodeChange,
|
338
|
+
invalidateVizCache,
|
339
|
+
resetSrcdoc,
|
340
|
+
};
|
341
|
+
};
|
@@ -0,0 +1,11 @@
|
|
1
|
+
import { VizId } from "@vizhub/viz-types";
|
2
|
+
export type SlugIdentifier = `${string}/${string}`;
|
3
|
+
export type SlugCache = {
|
4
|
+
get: (slug: SlugIdentifier) => Promise<VizId>;
|
5
|
+
set: (slug: SlugIdentifier, vizId: VizId) => void;
|
6
|
+
invalidate: (slug: SlugIdentifier) => void;
|
7
|
+
};
|
8
|
+
export declare const createSlugCache: ({ initialMappings, handleCacheMiss, }: {
|
9
|
+
initialMappings?: Record<SlugIdentifier, VizId>;
|
10
|
+
handleCacheMiss?: (slug: SlugIdentifier) => Promise<VizId>;
|
11
|
+
}) => SlugCache;
|
@@ -0,0 +1,35 @@
|
|
1
|
+
// A cache of viz IDs for slug resolution.
|
2
|
+
// Maps slug strings (username/slug) to viz IDs.
|
3
|
+
export const createSlugCache = ({ initialMappings = {}, handleCacheMiss, }) => {
|
4
|
+
// Track the mapping of slugs to viz IDs
|
5
|
+
const slugMap = new Map(Object.entries(initialMappings));
|
6
|
+
// Gets the viz ID for a slug.
|
7
|
+
// Returns the cached ID if it exists.
|
8
|
+
// Otherwise, calls handleCacheMiss to resolve the slug.
|
9
|
+
const get = async (slug) => {
|
10
|
+
const cachedVizId = slugMap.get(slug);
|
11
|
+
// Cache hit
|
12
|
+
if (cachedVizId !== undefined) {
|
13
|
+
return cachedVizId;
|
14
|
+
}
|
15
|
+
// Cache miss
|
16
|
+
if (!handleCacheMiss) {
|
17
|
+
throw new Error(`Unresolved slug ${slug}, cache miss handler not provided.`);
|
18
|
+
}
|
19
|
+
const resolvedVizId = await handleCacheMiss(slug);
|
20
|
+
if (resolvedVizId) {
|
21
|
+
slugMap.set(slug, resolvedVizId);
|
22
|
+
return resolvedVizId;
|
23
|
+
}
|
24
|
+
throw new Error(`Unresolved slug ${slug}`);
|
25
|
+
};
|
26
|
+
// Updates the mapping of a slug to a viz ID in the cache.
|
27
|
+
const set = (slug, vizId) => {
|
28
|
+
slugMap.set(slug, vizId);
|
29
|
+
};
|
30
|
+
// Removes a slug mapping from the cache.
|
31
|
+
const invalidate = (slug) => {
|
32
|
+
slugMap.delete(slug);
|
33
|
+
};
|
34
|
+
return { get, set, invalidate };
|
35
|
+
};
|
@@ -0,0 +1 @@
|
|
1
|
+
export {};
|
@@ -0,0 +1,105 @@
|
|
1
|
+
import { describe, it, expect, vi } from "vitest";
|
2
|
+
import { createSlugCache } from "./slugCache";
|
3
|
+
describe("SlugCache", () => {
|
4
|
+
describe("SlugCache - get method", () => {
|
5
|
+
it("should return vizId from cache if available", async () => {
|
6
|
+
const initialMappings = {
|
7
|
+
"user1/viz-slug": "viz-123",
|
8
|
+
};
|
9
|
+
const slugCache = createSlugCache({
|
10
|
+
initialMappings,
|
11
|
+
handleCacheMiss: vi.fn(),
|
12
|
+
});
|
13
|
+
const vizId = await slugCache.get("user1/viz-slug");
|
14
|
+
expect(vizId).toEqual("viz-123");
|
15
|
+
expect(vi.fn()).toHaveBeenCalledTimes(0); // handleCacheMiss should not be called
|
16
|
+
});
|
17
|
+
it("should resolve slug on cache miss and store it", async () => {
|
18
|
+
const handleCacheMissMock = vi
|
19
|
+
.fn()
|
20
|
+
.mockResolvedValue("viz-456");
|
21
|
+
const slugCache = createSlugCache({
|
22
|
+
initialMappings: {},
|
23
|
+
handleCacheMiss: handleCacheMissMock,
|
24
|
+
});
|
25
|
+
const vizId = await slugCache.get("user2/new-viz");
|
26
|
+
expect(handleCacheMissMock).toHaveBeenCalledWith("user2/new-viz");
|
27
|
+
expect(vizId).toEqual("viz-456");
|
28
|
+
// Verify that the cache now contains the resolved mapping
|
29
|
+
const cachedVizId = await slugCache.get("user2/new-viz");
|
30
|
+
expect(cachedVizId).toEqual("viz-456");
|
31
|
+
expect(handleCacheMissMock).toHaveBeenCalledTimes(1); // Should only be called once
|
32
|
+
});
|
33
|
+
it("should throw an error if handleCacheMiss does not return a vizId", async () => {
|
34
|
+
const handleCacheMissMock = vi
|
35
|
+
.fn()
|
36
|
+
.mockResolvedValue(undefined);
|
37
|
+
const slugCache = createSlugCache({
|
38
|
+
initialMappings: {},
|
39
|
+
handleCacheMiss: handleCacheMissMock,
|
40
|
+
});
|
41
|
+
await expect(slugCache.get("nonexistent/slug")).rejects.toThrow("Unresolved slug nonexistent/slug");
|
42
|
+
});
|
43
|
+
it("should throw an error if no handleCacheMiss is provided and slug not found", async () => {
|
44
|
+
const slugCache = createSlugCache({
|
45
|
+
initialMappings: {},
|
46
|
+
});
|
47
|
+
await expect(slugCache.get("missing/slug")).rejects.toThrow("Unresolved slug missing/slug, cache miss handler not provided.");
|
48
|
+
});
|
49
|
+
});
|
50
|
+
describe("SlugCache - set method", () => {
|
51
|
+
it("should add new mapping to the cache", async () => {
|
52
|
+
const slugCache = createSlugCache({
|
53
|
+
initialMappings: {},
|
54
|
+
handleCacheMiss: vi.fn(),
|
55
|
+
});
|
56
|
+
const slug = "user3/new-viz";
|
57
|
+
const vizId = "viz-789";
|
58
|
+
slugCache.set(slug, vizId);
|
59
|
+
// Verify new mapping is added
|
60
|
+
const resolvedVizId = await slugCache.get(slug);
|
61
|
+
expect(resolvedVizId).toEqual(vizId);
|
62
|
+
});
|
63
|
+
it("should update existing mapping in the cache", async () => {
|
64
|
+
const initialMappings = {
|
65
|
+
"user4/existing-viz": "old-viz-id",
|
66
|
+
};
|
67
|
+
const slugCache = createSlugCache({
|
68
|
+
initialMappings,
|
69
|
+
handleCacheMiss: vi.fn(),
|
70
|
+
});
|
71
|
+
const slug = "user4/existing-viz";
|
72
|
+
const updatedVizId = "updated-viz-id";
|
73
|
+
// Update existing mapping
|
74
|
+
slugCache.set(slug, updatedVizId);
|
75
|
+
// Verify mapping is updated
|
76
|
+
const resolvedVizId = await slugCache.get(slug);
|
77
|
+
expect(resolvedVizId).toEqual(updatedVizId);
|
78
|
+
});
|
79
|
+
});
|
80
|
+
describe("SlugCache - invalidate method", () => {
|
81
|
+
it("should remove a mapping from the cache", async () => {
|
82
|
+
const initialMappings = {
|
83
|
+
"user5/to-remove": "viz-to-remove",
|
84
|
+
};
|
85
|
+
const handleCacheMissMock = vi.fn();
|
86
|
+
const slugCache = createSlugCache({
|
87
|
+
initialMappings,
|
88
|
+
handleCacheMiss: handleCacheMissMock,
|
89
|
+
});
|
90
|
+
// Verify mapping exists initially
|
91
|
+
const initialVizId = await slugCache.get("user5/to-remove");
|
92
|
+
expect(initialVizId).toEqual("viz-to-remove");
|
93
|
+
// Invalidate the mapping
|
94
|
+
slugCache.invalidate("user5/to-remove");
|
95
|
+
// Trying to get it should trigger the cache miss handler
|
96
|
+
try {
|
97
|
+
await slugCache.get("user5/to-remove");
|
98
|
+
}
|
99
|
+
catch (e) {
|
100
|
+
// Ignore error if handleCacheMiss throws
|
101
|
+
}
|
102
|
+
expect(handleCacheMissMock).toHaveBeenCalledWith("user5/to-remove");
|
103
|
+
});
|
104
|
+
});
|
105
|
+
});
|
@@ -0,0 +1 @@
|
|
1
|
+
export declare const dsvParseSrc = "var EOL = {},\n EOF = {},\n QUOTE = 34,\n NEWLINE = 10,\n RETURN = 13;\n\nfunction objectConverter(columns) {\n return new Function(\n 'd',\n 'return {' +\n columns\n .map(function (name, i) {\n return (\n JSON.stringify(name) + ': d[' + i + '] || \"\"'\n );\n })\n .join(',') +\n '}',\n );\n}\n\nfunction customConverter(columns, f) {\n var object = objectConverter(columns);\n return function (row, i) {\n return f(object(row), i, columns);\n };\n}\n\nfunction dsv(delimiter) {\n var DELIMITER = delimiter.charCodeAt(0);\n\n function parse(text, f) {\n var convert,\n columns,\n rows = parseRows(text, function (row, i) {\n if (convert) return convert(row, i - 1);\n (columns = row),\n (convert = f\n ? customConverter(row, f)\n : objectConverter(row));\n });\n rows.columns = columns || [];\n return rows;\n }\n\n function parseRows(text, f) {\n var rows = [], // output rows\n N = text.length,\n I = 0, // current character index\n n = 0, // current line number\n t, // current token\n eof = N <= 0, // current token followed by EOF?\n eol = false; // current token followed by EOL?\n\n // Strip the trailing newline.\n if (text.charCodeAt(N - 1) === NEWLINE) --N;\n if (text.charCodeAt(N - 1) === RETURN) --N;\n\n function token() {\n if (eof) return EOF;\n if (eol) return (eol = false), EOL;\n\n // Unescape quotes.\n var i,\n j = I,\n c;\n if (text.charCodeAt(j) === QUOTE) {\n while (\n (I++ < N && text.charCodeAt(I) !== QUOTE) ||\n text.charCodeAt(++I) === QUOTE\n );\n if ((i = I) >= N) eof = true;\n else if ((c = text.charCodeAt(I++)) === NEWLINE)\n eol = true;\n else if (c === RETURN) {\n eol = true;\n if (text.charCodeAt(I) === NEWLINE) ++I;\n }\n return text.slice(j + 1, i - 1).replace(/\"\"/g, '\"');\n }\n\n // Find next delimiter or newline.\n while (I < N) {\n if ((c = text.charCodeAt((i = I++))) === NEWLINE)\n eol = true;\n else if (c === RETURN) {\n eol = true;\n if (text.charCodeAt(I) === NEWLINE) ++I;\n } else if (c !== DELIMITER) continue;\n return text.slice(j, i);\n }\n\n // Return last token before EOF.\n return (eof = true), text.slice(j, N);\n }\n\n while ((t = token()) !== EOF) {\n var row = [];\n while (t !== EOL && t !== EOF)\n row.push(t), (t = token());\n if (f && (row = f(row, n++)) == null) continue;\n rows.push(row);\n }\n\n return rows;\n }\n\n return {\n parse: parse,\n };\n}\n\nvar csv = dsv(',');\n\nvar csvParse = csv.parse;\n\nvar tsv = dsv('\t');\n\nvar tsvParse = tsv.parse;\n\nexport { csvParse, tsvParse };";
|
@@ -0,0 +1,121 @@
|
|
1
|
+
export const dsvParseSrc = `var EOL = {},
|
2
|
+
EOF = {},
|
3
|
+
QUOTE = 34,
|
4
|
+
NEWLINE = 10,
|
5
|
+
RETURN = 13;
|
6
|
+
|
7
|
+
function objectConverter(columns) {
|
8
|
+
return new Function(
|
9
|
+
'd',
|
10
|
+
'return {' +
|
11
|
+
columns
|
12
|
+
.map(function (name, i) {
|
13
|
+
return (
|
14
|
+
JSON.stringify(name) + ': d[' + i + '] || ""'
|
15
|
+
);
|
16
|
+
})
|
17
|
+
.join(',') +
|
18
|
+
'}',
|
19
|
+
);
|
20
|
+
}
|
21
|
+
|
22
|
+
function customConverter(columns, f) {
|
23
|
+
var object = objectConverter(columns);
|
24
|
+
return function (row, i) {
|
25
|
+
return f(object(row), i, columns);
|
26
|
+
};
|
27
|
+
}
|
28
|
+
|
29
|
+
function dsv(delimiter) {
|
30
|
+
var DELIMITER = delimiter.charCodeAt(0);
|
31
|
+
|
32
|
+
function parse(text, f) {
|
33
|
+
var convert,
|
34
|
+
columns,
|
35
|
+
rows = parseRows(text, function (row, i) {
|
36
|
+
if (convert) return convert(row, i - 1);
|
37
|
+
(columns = row),
|
38
|
+
(convert = f
|
39
|
+
? customConverter(row, f)
|
40
|
+
: objectConverter(row));
|
41
|
+
});
|
42
|
+
rows.columns = columns || [];
|
43
|
+
return rows;
|
44
|
+
}
|
45
|
+
|
46
|
+
function parseRows(text, f) {
|
47
|
+
var rows = [], // output rows
|
48
|
+
N = text.length,
|
49
|
+
I = 0, // current character index
|
50
|
+
n = 0, // current line number
|
51
|
+
t, // current token
|
52
|
+
eof = N <= 0, // current token followed by EOF?
|
53
|
+
eol = false; // current token followed by EOL?
|
54
|
+
|
55
|
+
// Strip the trailing newline.
|
56
|
+
if (text.charCodeAt(N - 1) === NEWLINE) --N;
|
57
|
+
if (text.charCodeAt(N - 1) === RETURN) --N;
|
58
|
+
|
59
|
+
function token() {
|
60
|
+
if (eof) return EOF;
|
61
|
+
if (eol) return (eol = false), EOL;
|
62
|
+
|
63
|
+
// Unescape quotes.
|
64
|
+
var i,
|
65
|
+
j = I,
|
66
|
+
c;
|
67
|
+
if (text.charCodeAt(j) === QUOTE) {
|
68
|
+
while (
|
69
|
+
(I++ < N && text.charCodeAt(I) !== QUOTE) ||
|
70
|
+
text.charCodeAt(++I) === QUOTE
|
71
|
+
);
|
72
|
+
if ((i = I) >= N) eof = true;
|
73
|
+
else if ((c = text.charCodeAt(I++)) === NEWLINE)
|
74
|
+
eol = true;
|
75
|
+
else if (c === RETURN) {
|
76
|
+
eol = true;
|
77
|
+
if (text.charCodeAt(I) === NEWLINE) ++I;
|
78
|
+
}
|
79
|
+
return text.slice(j + 1, i - 1).replace(/""/g, '"');
|
80
|
+
}
|
81
|
+
|
82
|
+
// Find next delimiter or newline.
|
83
|
+
while (I < N) {
|
84
|
+
if ((c = text.charCodeAt((i = I++))) === NEWLINE)
|
85
|
+
eol = true;
|
86
|
+
else if (c === RETURN) {
|
87
|
+
eol = true;
|
88
|
+
if (text.charCodeAt(I) === NEWLINE) ++I;
|
89
|
+
} else if (c !== DELIMITER) continue;
|
90
|
+
return text.slice(j, i);
|
91
|
+
}
|
92
|
+
|
93
|
+
// Return last token before EOF.
|
94
|
+
return (eof = true), text.slice(j, N);
|
95
|
+
}
|
96
|
+
|
97
|
+
while ((t = token()) !== EOF) {
|
98
|
+
var row = [];
|
99
|
+
while (t !== EOL && t !== EOF)
|
100
|
+
row.push(t), (t = token());
|
101
|
+
if (f && (row = f(row, n++)) == null) continue;
|
102
|
+
rows.push(row);
|
103
|
+
}
|
104
|
+
|
105
|
+
return rows;
|
106
|
+
}
|
107
|
+
|
108
|
+
return {
|
109
|
+
parse: parse,
|
110
|
+
};
|
111
|
+
}
|
112
|
+
|
113
|
+
var csv = dsv(',');
|
114
|
+
|
115
|
+
var csvParse = csv.parse;
|
116
|
+
|
117
|
+
var tsv = dsv('\t');
|
118
|
+
|
119
|
+
var tsvParse = tsv.parse;
|
120
|
+
|
121
|
+
export { csvParse, tsvParse };`;
|
@@ -0,0 +1,40 @@
|
|
1
|
+
import { parseId } from "../parseId";
|
2
|
+
import { dsvParseSrc } from "./dsvParseSrc";
|
3
|
+
const DEBUG = false;
|
4
|
+
// Escape backticks in a string so that it can be
|
5
|
+
// used in a template literal. Also need to escape backslashes.
|
6
|
+
const escapeBackticks = (str) =>
|
7
|
+
// str.replace(/`/g, '\\`');
|
8
|
+
str.replace(/\\/g, "\\\\").replace(/`/g, "\\`");
|
9
|
+
// Responsible for loading CSV and TSV files, which are
|
10
|
+
// in general called Delimiter-Separated Values (DSV).
|
11
|
+
export const transformDSV = () => ({
|
12
|
+
name: "transformDSV",
|
13
|
+
// `id` here is of the form
|
14
|
+
// `{vizId}/{fileName}`
|
15
|
+
transform: async (text, id) => {
|
16
|
+
DEBUG && console.log("[transformDSV]: load() " + id);
|
17
|
+
const { vizId, fileName } = parseId(id);
|
18
|
+
DEBUG &&
|
19
|
+
console.log(" [transformDSV] vizId: " + vizId);
|
20
|
+
DEBUG &&
|
21
|
+
console.log(" [transformDSV] fileName: " + fileName);
|
22
|
+
const isCSV = fileName.endsWith(".csv");
|
23
|
+
const isTSV = fileName.endsWith(".tsv");
|
24
|
+
if (isCSV || isTSV) {
|
25
|
+
DEBUG &&
|
26
|
+
console.log(" [transformDSV] tracking DSV import for " +
|
27
|
+
id);
|
28
|
+
const parseFunction = isCSV ? "csvParse" : "tsvParse";
|
29
|
+
return {
|
30
|
+
code: `
|
31
|
+
${dsvParseSrc}
|
32
|
+
const data = ${parseFunction}(\`${escapeBackticks(text)}\`);
|
33
|
+
export default data;
|
34
|
+
`,
|
35
|
+
map: { mappings: "" },
|
36
|
+
};
|
37
|
+
}
|
38
|
+
return undefined;
|
39
|
+
},
|
40
|
+
});
|
@@ -1,6 +1,8 @@
|
|
1
|
-
import { InputPluginOption } from
|
1
|
+
import { InputPluginOption } from "rollup";
|
2
|
+
export type SvelteCompiler = (code: string, options: any) => {
|
3
|
+
js: string;
|
4
|
+
};
|
2
5
|
export declare const svelteCompilerUrl = "https://cdn.jsdelivr.net/npm/svelte@4.2.9/compiler.cjs";
|
3
6
|
export declare const transformSvelte: ({ getSvelteCompiler, }: {
|
4
7
|
getSvelteCompiler?: () => Promise<any>;
|
5
8
|
}) => InputPluginOption;
|
6
|
-
//# sourceMappingURL=transformSvelte.d.ts.map
|