@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,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
|
@@ -0,0 +1,77 @@
|
|
1
|
+
import { parseId } from "./parseId";
|
2
|
+
const debug = false;
|
3
|
+
// The cache of fetched files.
|
4
|
+
const fetchedFileCache = new Map();
|
5
|
+
let compile;
|
6
|
+
const svelteURL = "https://cdn.jsdelivr.net/npm/svelte@4.2.9";
|
7
|
+
export const svelteCompilerUrl = `${svelteURL}/compiler.cjs`;
|
8
|
+
// Responsible for transforming Svelte files.
|
9
|
+
// Inspired by:
|
10
|
+
// * https://github.com/sveltejs/sites/blob/master/packages/repl/src/lib/workers/compiler/index.js#L2
|
11
|
+
// * https://github.com/sveltejs/sites/blob/master/packages/repl/src/lib/workers/bundler/index.js#L358
|
12
|
+
// * https://github.com/sveltejs/rollup-plugin-svelte/blob/master/index.js#L146C4-L146C51
|
13
|
+
export const transformSvelte = ({ getSvelteCompiler, }) => ({
|
14
|
+
name: "transformSvelte",
|
15
|
+
load: async (resolved) => {
|
16
|
+
if (!resolved.startsWith(svelteURL)) {
|
17
|
+
return;
|
18
|
+
}
|
19
|
+
if (debug) {
|
20
|
+
console.log("[transformSvelte]: load() " + resolved);
|
21
|
+
}
|
22
|
+
const cachedFile = fetchedFileCache.get(resolved);
|
23
|
+
if (cachedFile)
|
24
|
+
return cachedFile;
|
25
|
+
const fetchedFile = await fetch(resolved).then((res) => res.text());
|
26
|
+
fetchedFileCache.set(resolved, fetchedFile);
|
27
|
+
return fetchedFile;
|
28
|
+
},
|
29
|
+
// From https://github.com/sveltejs/sites/blob/master/packages/repl/src/lib/workers/bundler/index.js#L255C2-L271C5
|
30
|
+
resolveId: async (importee, importer) => {
|
31
|
+
if (debug) {
|
32
|
+
console.log("[transformSvelte] resolveId() " + importee);
|
33
|
+
console.log("importee: " + importee);
|
34
|
+
console.log("importer: " + importer);
|
35
|
+
}
|
36
|
+
// importing from Svelte
|
37
|
+
if (importee === `svelte`) {
|
38
|
+
return `${svelteURL}/src/runtime/index.js`;
|
39
|
+
}
|
40
|
+
if (importee.startsWith(`svelte/`)) {
|
41
|
+
const sub_path = importee.slice(7);
|
42
|
+
return `${svelteURL}/src/runtime/${sub_path}/index.js`;
|
43
|
+
}
|
44
|
+
// importing from a URL
|
45
|
+
if (/^https?:/.test(importee))
|
46
|
+
return importee;
|
47
|
+
// Relative imports
|
48
|
+
if (importee.startsWith(".")) {
|
49
|
+
if (importer && importer.startsWith(svelteURL)) {
|
50
|
+
const resolved = new URL(importee, importer).href;
|
51
|
+
const url = new URL(importee, importer).href;
|
52
|
+
if (debug) {
|
53
|
+
console.log("[transformSvelte] resolveId() " + resolved);
|
54
|
+
}
|
55
|
+
return resolved;
|
56
|
+
}
|
57
|
+
}
|
58
|
+
},
|
59
|
+
transform: async (code, id) => {
|
60
|
+
const { fileName } = parseId(id);
|
61
|
+
const isSvelte = fileName.endsWith(".svelte");
|
62
|
+
if (isSvelte) {
|
63
|
+
if (!compile) {
|
64
|
+
if (!getSvelteCompiler) {
|
65
|
+
throw new Error("Svelte compiler not available");
|
66
|
+
}
|
67
|
+
compile = await getSvelteCompiler();
|
68
|
+
}
|
69
|
+
const compiled = compile(code, {
|
70
|
+
filename: fileName,
|
71
|
+
hydratable: true,
|
72
|
+
});
|
73
|
+
return compiled.js;
|
74
|
+
}
|
75
|
+
return undefined;
|
76
|
+
},
|
77
|
+
});
|
@@ -1,7 +1,8 @@
|
|
1
|
-
import {
|
1
|
+
import { VizContent, VizId } from "@vizhub/viz-types";
|
2
|
+
import type { PackageJson } from "../common/packageJson";
|
2
3
|
export type V3BuildResult = {
|
3
4
|
src: string | undefined;
|
4
|
-
pkg:
|
5
|
+
pkg: PackageJson | undefined;
|
5
6
|
warnings: Array<V3BuildError>;
|
6
7
|
time: number;
|
7
8
|
cssFiles: Array<ResolvedVizFileId>;
|
@@ -12,54 +13,53 @@ export type V3BuildError = {
|
|
12
13
|
};
|
13
14
|
export type ResolvedVizFileId = string;
|
14
15
|
export type V3WorkerMessage = {
|
15
|
-
type:
|
16
|
+
type: "contentRequest";
|
16
17
|
vizId: VizId;
|
17
18
|
} | {
|
18
|
-
type:
|
19
|
+
type: "contentResponse";
|
19
20
|
vizId: VizId;
|
20
|
-
content:
|
21
|
+
content: VizContent;
|
21
22
|
} | {
|
22
|
-
type:
|
23
|
+
type: "buildRequest";
|
23
24
|
vizId: VizId;
|
24
25
|
enableSourcemap: boolean;
|
25
26
|
} | {
|
26
|
-
type:
|
27
|
+
type: "buildResponse";
|
27
28
|
buildResult?: V3BuildResult;
|
28
29
|
error?: Error;
|
29
30
|
} | {
|
30
|
-
type:
|
31
|
+
type: "invalidateVizCacheRequest";
|
31
32
|
changedVizIds: Array<VizId>;
|
32
33
|
} | {
|
33
|
-
type:
|
34
|
+
type: "invalidateVizCacheResponse";
|
34
35
|
} | {
|
35
|
-
type:
|
36
|
+
type: "resolveSlugRequest";
|
36
37
|
slugKey: string;
|
37
38
|
requestId: string;
|
38
39
|
} | {
|
39
|
-
type:
|
40
|
+
type: "resolveSlugResponse";
|
40
41
|
slugKey: string;
|
41
42
|
vizId: VizId;
|
42
43
|
requestId: string;
|
43
44
|
} | {
|
44
|
-
type:
|
45
|
+
type: "resetSrcdocRequest";
|
45
46
|
vizId: VizId;
|
46
47
|
changedVizIds: Array<VizId>;
|
47
48
|
} | {
|
48
|
-
type:
|
49
|
+
type: "resetSrcdocResponse";
|
49
50
|
srcdoc?: string;
|
50
51
|
error?: Error;
|
51
52
|
};
|
52
53
|
export type V3WindowMessage = {
|
53
|
-
type:
|
54
|
+
type: "runJS";
|
54
55
|
src: string;
|
55
56
|
} | {
|
56
|
-
type:
|
57
|
+
type: "runCSS";
|
57
58
|
src: string;
|
58
59
|
id: ResolvedVizFileId;
|
59
60
|
} | {
|
60
|
-
type:
|
61
|
+
type: "runDone";
|
61
62
|
} | {
|
62
|
-
type:
|
63
|
+
type: "runError";
|
63
64
|
error: Error;
|
64
65
|
};
|
65
|
-
//# sourceMappingURL=types.d.ts.map
|
package/dist/v3/types.js
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
export {};
|
@@ -0,0 +1,15 @@
|
|
1
|
+
import { RollupBuild, RollupOptions } from "rollup";
|
2
|
+
import { FileCollection } from "../types";
|
3
|
+
import { VizCache } from "./vizCache";
|
4
|
+
import { VizId } from "@vizhub/viz-types";
|
5
|
+
import { SlugCache } from "./slugCache";
|
6
|
+
import { SvelteCompiler } from "./transformSvelte";
|
7
|
+
export declare const v3Build: ({ files, rollup, enableSourcemap, vizCache, vizId, slugCache, getSvelteCompiler, }: {
|
8
|
+
files: FileCollection;
|
9
|
+
rollup: (options: RollupOptions) => Promise<RollupBuild>;
|
10
|
+
enableSourcemap?: boolean;
|
11
|
+
vizCache: VizCache;
|
12
|
+
vizId: VizId;
|
13
|
+
slugCache?: SlugCache;
|
14
|
+
getSvelteCompiler?: () => Promise<SvelteCompiler>;
|
15
|
+
}) => Promise<string>;
|
@@ -0,0 +1,30 @@
|
|
1
|
+
import { computeBundleJSV3 } from "./computeBundleJSV3";
|
2
|
+
import { htmlTemplate } from "./htmlTemplate";
|
3
|
+
import { parseId } from "./parseId";
|
4
|
+
import { getFileText } from "../utils/getFileText";
|
5
|
+
export const v3Build = async ({ files, rollup, enableSourcemap = true, vizCache, vizId, slugCache, getSvelteCompiler, }) => {
|
6
|
+
const { src, cssFiles } = await computeBundleJSV3({
|
7
|
+
files,
|
8
|
+
rollup,
|
9
|
+
enableSourcemap,
|
10
|
+
vizCache,
|
11
|
+
vizId,
|
12
|
+
slugCache,
|
13
|
+
getSvelteCompiler,
|
14
|
+
});
|
15
|
+
// Generate CSS styles from imported CSS files
|
16
|
+
let styles = "";
|
17
|
+
// Inject CSS files.
|
18
|
+
if (cssFiles.length > 0) {
|
19
|
+
for (let i = 0; i < cssFiles.length; i++) {
|
20
|
+
const id = cssFiles[i];
|
21
|
+
const indent = i > 0 ? " " : "\n ";
|
22
|
+
const styleElementId = "injected-style" + id;
|
23
|
+
const { vizId, fileName } = parseId(id);
|
24
|
+
const content = await vizCache.get(vizId);
|
25
|
+
const src = getFileText(content, fileName);
|
26
|
+
styles += `${indent}<style id="${styleElementId}">${src}</style>`;
|
27
|
+
}
|
28
|
+
}
|
29
|
+
return htmlTemplate({ cdn: "", src, styles });
|
30
|
+
};
|
@@ -0,0 +1,10 @@
|
|
1
|
+
import { VizContent, VizId } from "@vizhub/viz-types";
|
2
|
+
export type VizCache = {
|
3
|
+
get: (vizId: string) => Promise<VizContent>;
|
4
|
+
set: (content: VizContent) => void;
|
5
|
+
invalidate: (vizId: string) => void;
|
6
|
+
};
|
7
|
+
export declare const createVizCache: ({ initialContents, handleCacheMiss, }: {
|
8
|
+
initialContents: Array<VizContent>;
|
9
|
+
handleCacheMiss?: (vizId: VizId) => Promise<VizContent>;
|
10
|
+
}) => VizCache;
|
@@ -0,0 +1,36 @@
|
|
1
|
+
// A cache of viz content.
|
2
|
+
// For use in resolving imports from other vizzes.
|
3
|
+
// Runs both on the server and in the browser.
|
4
|
+
export const createVizCache = ({ initialContents, handleCacheMiss, }) => {
|
5
|
+
// Track the content of cached vizzes.
|
6
|
+
const contentMap = new Map(initialContents.map((content) => [content.id, content]));
|
7
|
+
// Gets the content of a viz.
|
8
|
+
// Returns the cached content if it exists.
|
9
|
+
// Otherwise, calls handleCacheMiss to fetch the content.
|
10
|
+
const get = async (vizId) => {
|
11
|
+
const cachedContent = contentMap.get(vizId);
|
12
|
+
// Cache hit
|
13
|
+
if (cachedContent !== undefined) {
|
14
|
+
return cachedContent;
|
15
|
+
}
|
16
|
+
// Cache miss
|
17
|
+
if (!handleCacheMiss) {
|
18
|
+
throw new Error(`Unresolved import from vizId ${vizId}, cache miss handler not provided.`);
|
19
|
+
}
|
20
|
+
const freshContent = await handleCacheMiss(vizId);
|
21
|
+
if (freshContent) {
|
22
|
+
contentMap.set(vizId, freshContent);
|
23
|
+
return freshContent;
|
24
|
+
}
|
25
|
+
// TODO surface this error to the user
|
26
|
+
throw new Error(`Unresolved import from vizId ${vizId}`);
|
27
|
+
};
|
28
|
+
// Updates the content of a viz in the cache.
|
29
|
+
const set = (content) => {
|
30
|
+
contentMap.set(content.id, content);
|
31
|
+
};
|
32
|
+
const invalidate = (vizId) => {
|
33
|
+
contentMap.delete(vizId);
|
34
|
+
};
|
35
|
+
return { get, set, invalidate };
|
36
|
+
};
|
@@ -0,0 +1 @@
|
|
1
|
+
export {};
|
@@ -0,0 +1,98 @@
|
|
1
|
+
import { describe, it, expect, vi } from "vitest";
|
2
|
+
import { createVizCache } from "./vizCache";
|
3
|
+
import { sampleVizContent } from "../test/fixtures/v3";
|
4
|
+
describe("VizCache", () => {
|
5
|
+
describe("VizCache - get method", () => {
|
6
|
+
it("should return content from cache if available", async () => {
|
7
|
+
const vizCache = createVizCache({
|
8
|
+
initialContents: [sampleVizContent],
|
9
|
+
handleCacheMiss: vi.fn(),
|
10
|
+
});
|
11
|
+
const content = await vizCache.get(sampleVizContent.id);
|
12
|
+
expect(content).toEqual(sampleVizContent);
|
13
|
+
expect(vi.fn()).toHaveBeenCalledTimes(0); // handleCacheMiss should not be called
|
14
|
+
});
|
15
|
+
it("should fetch content on cache miss and store it", async () => {
|
16
|
+
const handleCacheMissMock = vi
|
17
|
+
.fn()
|
18
|
+
.mockResolvedValue(sampleVizContent);
|
19
|
+
const vizCache = createVizCache({
|
20
|
+
initialContents: [],
|
21
|
+
handleCacheMiss: handleCacheMissMock,
|
22
|
+
});
|
23
|
+
const content = await vizCache.get(sampleVizContent.id);
|
24
|
+
expect(handleCacheMissMock).toHaveBeenCalledWith(sampleVizContent.id);
|
25
|
+
expect(content).toEqual(sampleVizContent);
|
26
|
+
// Verify that the cache now contains the fetched content
|
27
|
+
const cachedContent = await vizCache.get(sampleVizContent.id);
|
28
|
+
expect(cachedContent).toEqual(sampleVizContent);
|
29
|
+
});
|
30
|
+
it("should throw an error if handleCacheMiss does not return content", async () => {
|
31
|
+
const handleCacheMissMock = vi
|
32
|
+
.fn()
|
33
|
+
.mockResolvedValue(undefined);
|
34
|
+
const vizCache = createVizCache({
|
35
|
+
initialContents: [],
|
36
|
+
handleCacheMiss: handleCacheMissMock,
|
37
|
+
});
|
38
|
+
await expect(vizCache.get("nonexistentId")).rejects.toThrow("Unresolved import from vizId nonexistentId");
|
39
|
+
});
|
40
|
+
// Add tests for set
|
41
|
+
});
|
42
|
+
describe("VizCache - set method", () => {
|
43
|
+
it("should add new content to the cache", async () => {
|
44
|
+
const vizCache = createVizCache({
|
45
|
+
initialContents: [],
|
46
|
+
handleCacheMiss: vi.fn(),
|
47
|
+
});
|
48
|
+
const newContent = {
|
49
|
+
id: "newContent",
|
50
|
+
files: {},
|
51
|
+
title: "New Content",
|
52
|
+
};
|
53
|
+
vizCache.set(newContent);
|
54
|
+
// Verify new content is added
|
55
|
+
const content = await vizCache.get(newContent.id);
|
56
|
+
expect(content).toEqual(newContent);
|
57
|
+
});
|
58
|
+
it("should update existing content in the cache", async () => {
|
59
|
+
const updatedContent = {
|
60
|
+
...sampleVizContent,
|
61
|
+
title: "Updated Content Title",
|
62
|
+
};
|
63
|
+
const vizCache = createVizCache({
|
64
|
+
initialContents: [sampleVizContent],
|
65
|
+
handleCacheMiss: vi.fn(),
|
66
|
+
});
|
67
|
+
// Update existing content
|
68
|
+
vizCache.set(updatedContent);
|
69
|
+
// Verify content is updated
|
70
|
+
const content = await vizCache.get(updatedContent.id);
|
71
|
+
expect(content).toEqual(updatedContent);
|
72
|
+
});
|
73
|
+
it("should keep the cache consistent after multiple set operations", async () => {
|
74
|
+
const vizCache = createVizCache({
|
75
|
+
initialContents: [],
|
76
|
+
handleCacheMiss: vi.fn(),
|
77
|
+
});
|
78
|
+
// Adding multiple contents
|
79
|
+
const contentA = {
|
80
|
+
id: "contentA",
|
81
|
+
files: {},
|
82
|
+
title: "Content A",
|
83
|
+
};
|
84
|
+
const contentB = {
|
85
|
+
id: "contentB",
|
86
|
+
files: {},
|
87
|
+
title: "Content B",
|
88
|
+
};
|
89
|
+
vizCache.set(contentA);
|
90
|
+
vizCache.set(contentB);
|
91
|
+
// Verify both contents are retrievable
|
92
|
+
const retrievedA = await vizCache.get(contentA.id);
|
93
|
+
const retrievedB = await vizCache.get(contentB.id);
|
94
|
+
expect(retrievedA).toEqual(contentA);
|
95
|
+
expect(retrievedB).toEqual(contentB);
|
96
|
+
});
|
97
|
+
});
|
98
|
+
});
|
@@ -1,8 +1,7 @@
|
|
1
|
-
import { InputPluginOption } from
|
2
|
-
import { ResolvedVizFileId } from
|
3
|
-
import { VizCache } from
|
1
|
+
import { InputPluginOption } from "rollup";
|
2
|
+
import { ResolvedVizFileId } from "./types";
|
3
|
+
import { VizCache } from "./vizCache";
|
4
4
|
export declare const vizLoad: ({ vizCache, trackCSSImport, }: {
|
5
5
|
vizCache: VizCache;
|
6
6
|
trackCSSImport: (cssFile: ResolvedVizFileId) => void;
|
7
7
|
}) => InputPluginOption;
|
8
|
-
//# sourceMappingURL=vizLoad.d.ts.map
|