Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

fix: patching vegaloader when rendering static content (#1770)

Myles Scolnicke762391b9f2c
diff --git a/frontend/src/core/static/__tests__/files.test.ts b/frontend/src/core/static/__tests__/files.test.ts
index a32f81f55b..1adebcd14e 100644
--- a/frontend/src/core/static/__tests__/files.test.ts
+++ b/frontend/src/core/static/__tests__/files.test.ts
@@ -1,15 +1,35 @@
/* Copyright 2024 Marimo. All rights reserved. */
-import { describe, afterEach, expect, it, vi } from "vitest";
+// @vitest-environment jsdom
+import { describe, expect, it, vi, beforeAll, afterAll } from "vitest";
import { patchFetch, patchVegaLoader } from "../files";
-import { DataURLString } from "@/utils/json/base64";
+import type { DataURLString } from "@/utils/json/base64";
+import { createLoader } from "@/plugins/impl/vega/vega-loader";
+import http from "node:http";
+
+// Start a tiny server to serve virtual files
+const server = http.createServer((request, response) => {
+ if (request.url === "/@file/remote-content.txt") {
+ response.writeHead(200, { "Content-Type": "text/plain" });
+ response.end("Remote content");
+ } else {
+ response.writeHead(404);
+ response.end();
+ }
+});
-describe("patchFetch", () => {
- const originalFetch = window.fetch;
+const host = "127.0.0.1";
+const port = 4321;
+const remoteURL = `http://${host}:${port}/@file/remote-content.txt`;
- afterEach(() => {
- window.fetch = originalFetch; // Restore original fetch after each test
- });
+beforeAll(async () => {
+ server.listen(port, host);
+});
+
+afterAll(async () => {
+ server.close();
+});
+describe("patchFetch", () => {
it("should return a blob response when a virtual file is fetched", async () => {
const virtualFiles = {
"/@file/virtual-file.txt":
@@ -18,7 +38,7 @@ describe("patchFetch", () => {
patchFetch(virtualFiles);
- const response = await fetch("/@file/virtual-file.txt");
+ const response = await window.fetch("/@file/virtual-file.txt");
const blob = await response.blob();
const text = await blob.text();
@@ -27,20 +47,18 @@ describe("patchFetch", () => {
});
it("should fallback to original fetch for non-virtual files", async () => {
- const mockResponse = new Response("Not a virtual file");
- window.fetch = vi.fn().mockResolvedValue(mockResponse);
+ vi.spyOn(window, "fetch");
const unpatch = patchFetch({}); // No virtual files
-
- const response = await fetch("/@file/non-virtual-file.txt");
+ const response = await window.fetch(remoteURL);
const text = await response.text();
-
unpatch();
+
expect(window.fetch).toHaveBeenCalledWith(
- "/@file/non-virtual-file.txt",
+ "http://127.0.0.1:4321/@file/remote-content.txt",
undefined,
);
- expect(text).toBe("Not a virtual file");
+ expect(text).toBe("Remote content");
});
});
@@ -51,25 +69,30 @@ describe("patchVegaLoader", () => {
"data:application/json;base64,eyJrZXkiOiAidmFsdWUifQ==" as DataURLString,
};
- const loader = {
- http: vi.fn((url) => Promise.resolve(`Original content for ${url}`)),
- };
- patchVegaLoader(loader, virtualFiles);
-
+ const loader = createLoader();
+ const unpatch = patchVegaLoader(loader, virtualFiles);
const content = await loader.http("virtual-file.json");
+ unpatch();
expect(content).toBe('{"key": "value"}');
});
it("should fallback to original http method for non-virtual files", async () => {
- const loader = {
- http: vi.fn((url) => Promise.resolve(`Original content for ${url}`)),
- };
+ const loader = createLoader();
const unpatch = patchVegaLoader(loader, {});
- const content = await loader.http("non-virtual-file.json");
+ const content = await loader.http(remoteURL);
unpatch(); // Restore the original http function
- expect(content).toBe("Original content for non-virtual-file.json");
- expect(loader.http).toHaveBeenCalledWith("non-virtual-file.json");
+ expect(content).toBe("Remote content");
+ });
+
+ it("should work with data URIs", async () => {
+ const loader = createLoader();
+ const unpatch = patchVegaLoader(loader, {});
+ const content = await loader.http(
+ "data:application/json;base64,eyJrZXkiOiAidmFsdWUifQ==",
+ );
+ unpatch();
+ expect(content).toBe('{"key": "value"}');
});
});
diff --git a/frontend/src/core/static/files.ts b/frontend/src/core/static/files.ts
index 811f73a340..9377213488 100644
--- a/frontend/src/core/static/files.ts
+++ b/frontend/src/core/static/files.ts
@@ -1,8 +1,9 @@
/* Copyright 2024 Marimo. All rights reserved. */
-import { StaticVirtualFiles } from "./types";
+import type { StaticVirtualFiles } from "./types";
import { deserializeBlob } from "@/utils/blob";
import { getStaticVirtualFiles } from "./static-state";
+import type { DataURLString } from "@/utils/json/base64";
/**
* Patch fetch to resolve virtual files
@@ -45,7 +46,8 @@ export function patchVegaLoader(
},
files: StaticVirtualFiles = getStaticVirtualFiles(),
) {
- const originalHttp = loader.http;
+ // const originalHttp = loader.http.bind(loader);
+ const originalHttp = loader.http.bind(loader);
loader.http = async (url: string) => {
if (files[url]) {
@@ -54,7 +56,18 @@ export function patchVegaLoader(
return blob.text();
}
- return originalHttp(url);
+ try {
+ return await originalHttp(url);
+ } catch (error) {
+ // If its a data URL, just return the data
+ if (url.startsWith("data:")) {
+ return deserializeBlob(url as DataURLString).then((blob) =>
+ blob.text(),
+ );
+ }
+ // Re-throw the error
+ throw error;
+ }
};
return () => {
diff --git a/frontend/src/plugins/impl/vega/vega-loader.ts b/frontend/src/plugins/impl/vega/vega-loader.ts
index f1a0a7e9cb..984a4c0234 100644
--- a/frontend/src/plugins/impl/vega/vega-loader.ts
+++ b/frontend/src/plugins/impl/vega/vega-loader.ts
@@ -1,8 +1,8 @@
/* Copyright 2024 Marimo. All rights reserved. */
// @ts-expect-error - no types
import * as vl from "vega-loader";
-import { DataFormat } from "./types";
-import { DataType } from "@/core/kernel/messages";
+import type { DataFormat } from "./types";
+import type { DataType } from "@/core/kernel/messages";
// Re-export the vega-loader functions to add TypeScript types