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.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
fix: graceully handle other url protocols (#5278)
Myles Scolnick76a9daebb553
diff --git a/frontend/src/core/runtime/__tests__/runtime.test.ts b/frontend/src/core/runtime/__tests__/runtime.test.tsindex 5db1c6c770..d01d908ced 100644--- a/frontend/src/core/runtime/__tests__/runtime.test.ts+++ b/frontend/src/core/runtime/__tests__/runtime.test.ts@@ -4,6 +4,7 @@ import { describe, expect, it, vi } from "vitest";import { RuntimeManager } from "../runtime";import type { RuntimeConfig } from "../types";import type { SessionId } from "@/core/kernel/session";+import { Logger } from "@/utils/Logger";// Mock the session modulevi.mock("@/core/kernel/session", () => ({@@ -14,6 +15,7 @@ vi.mock("@/core/kernel/session", () => ({vi.mock("@/utils/Logger", () => ({Logger: {error: vi.fn(),+ warn: vi.fn(),},}));@@ -294,5 +296,169 @@ describe("RuntimeManager", () => {expect(httpRuntime.getWsURL("test" as SessionId).protocol).toBe("ws:");expect(httpsRuntime.getWsURL("test" as SessionId).protocol).toBe("wss:");});++ it("should handle blob URLs", () => {+ const runtime = new RuntimeManager({+ url: "blob:https://example.com/12345678-1234-1234-1234-123456789abc",+ });++ expect(runtime.httpURL.toString()).toBe(+ "blob:https://example.com/12345678-1234-1234-1234-123456789abc",+ );+ });++ it("should throw when creating WebSocket URLs from blob URLs", () => {+ const runtime = new RuntimeManager({+ url: "blob:https://example.com/12345678-1234-1234-1234-123456789abc",+ });+ const sessionId = "test" as SessionId;++ expect(runtime.getWsURL(sessionId).toString()).toMatchInlineSnapshot(+ `"blob:https://example.com/12345678-1234-1234-1234-123456789abc?session_id=test"`,+ );+ expect(Logger.warn).toHaveBeenCalledOnce();+ });++ it("should handle blob URLs in AI URLs", () => {+ const runtime = new RuntimeManager({+ url: "blob:https://example.com/12345678-1234-1234-1234-123456789abc",+ });+ const aiUrl = runtime.getAiURL("completion");++ expect(aiUrl.protocol).toBe("blob:");+ expect(aiUrl.pathname).toBe(+ "https://example.com/12345678-1234-1234-1234-123456789abc",+ );+ });++ it("should handle blob URLs in health check URLs", () => {+ const runtime = new RuntimeManager({+ url: "blob:https://example.com/12345678-1234-1234-1234-123456789abc",+ });+ const healthUrl = runtime.healthURL();++ expect(healthUrl.protocol).toBe("blob:");+ expect(healthUrl.pathname).toBe(+ "https://example.com/12345678-1234-1234-1234-123456789abc",+ );+ });++ it("should handle URLs with userinfo", () => {+ const runtime = new RuntimeManager({+ url: "https://user:[email protected]",+ });+ const wsUrl = runtime.getWsURL("test" as SessionId);++ expect(wsUrl.protocol).toBe("wss:");+ expect(wsUrl.username).toBe("user");+ expect(wsUrl.password).toBe("pass");+ expect(wsUrl.hostname).toBe("example.com");+ });++ it("should handle IPv6 addresses", () => {+ const runtime = new RuntimeManager({+ url: "http://[::1]:8080",+ });+ const wsUrl = runtime.getWsURL("test" as SessionId);++ expect(wsUrl.protocol).toBe("ws:");+ expect(wsUrl.hostname).toBe("[::1]");+ expect(wsUrl.port).toBe("8080");+ });++ it("should handle URLs with encoded characters", () => {+ const runtime = new RuntimeManager({+ url: "https://example.com/path%20with%20spaces",+ });+ const aiUrl = runtime.getAiURL("completion");++ expect(aiUrl.pathname).toBe("/path%20with%20spaces/api/ai/completion");+ });++ it("should handle URLs with multiple trailing slashes", () => {+ const runtime = new RuntimeManager({+ url: "https://example.com/path///",+ });+ const aiUrl = runtime.getAiURL("completion");++ expect(aiUrl.pathname).toBe("/path///api/ai/completion");+ });++ it("should handle URLs with port numbers", () => {+ const runtime = new RuntimeManager({+ url: "https://example.com:9443/app",+ });+ const wsUrl = runtime.getWsURL("test" as SessionId);++ expect(wsUrl.protocol).toBe("wss:");+ expect(wsUrl.hostname).toBe("example.com");+ expect(wsUrl.port).toBe("9443");+ expect(wsUrl.pathname).toBe("/app/ws");+ });++ it("should handle localhost variations", () => {+ const variants = [+ "http://127.0.0.1:8080",+ "http://localhost:8080",+ "http://[::1]:8080",+ ];++ variants.forEach((url) => {+ const runtime = new RuntimeManager({ url });+ const wsUrl = runtime.getWsURL("test" as SessionId);+ expect(wsUrl.protocol).toBe("ws:");+ });+ });++ it("should handle URLs with complex query parameters", () => {+ const runtime = new RuntimeManager({+ url: "https://example.com?param1=value1¶m2=value%20encoded&empty=",+ });+ const wsUrl = runtime.getWsURL("test" as SessionId);++ expect(wsUrl.searchParams.get("param1")).toBe("value1");+ expect(wsUrl.searchParams.get("param2")).toBe("value encoded");+ expect(wsUrl.searchParams.get("empty")).toBe("");+ expect(wsUrl.searchParams.get("session_id")).toBe("test");+ });++ it("should accept data URLs (valid URL format)", () => {+ const runtime = new RuntimeManager({+ url: "data:text/plain;base64,SGVsbG8gV29ybGQ=",+ });+ expect(runtime.httpURL.protocol).toBe("data:");+ });++ it("should accept file URLs (valid URL format)", () => {+ const runtime = new RuntimeManager({ url: "file:///path/to/file" });+ expect(runtime.httpURL.protocol).toBe("file:");+ });++ it("should accept custom protocol URLs (valid URL format)", () => {+ const runtime = new RuntimeManager({ url: "custom://example.com" });+ expect(runtime.httpURL.protocol).toBe("custom:");+ });++ it("should handle empty string URL", () => {+ expect(() => {+ new RuntimeManager({ url: "" });+ }).toThrow("Invalid runtime URL");+ });++ it("should handle malformed URLs", () => {+ const malformedUrls = [+ "http://",+ "https://",+ "not-a-url",+ "http://[invalid-ipv6",+ "https://exam ple.com", // space in hostname+ ];++ malformedUrls.forEach((url) => {+ expect(() => {+ new RuntimeManager({ url });+ }).toThrow("Invalid runtime URL");+ });+ });});});diff --git a/frontend/src/core/runtime/runtime.ts b/frontend/src/core/runtime/runtime.tsindex b178470d76..bdc44ca0b8 100644--- a/frontend/src/core/runtime/runtime.ts+++ b/frontend/src/core/runtime/runtime.ts@@ -1,5 +1,5 @@/* Copyright 2024 Marimo. All rights reserved. */-import { invariant } from "@/utils/invariant";+import type { RuntimeConfig } from "./types";import { Logger } from "@/utils/Logger";import { getSessionId, type SessionId } from "../kernel/session";@@ -174,7 +174,12 @@ export class RuntimeManager {}function asWsUrl(url: string): URL {- invariant(url.startsWith("http"), "URL must start with http");+ if (!url.startsWith("http")) {+ Logger.warn(`URL must start with http: ${url}`);+ const newUrl = new URL(url);+ newUrl.protocol = "ws";+ return newUrl;+ }// Replace the protocol http with wsreturn new URL(url.replace(/^http/, "ws"));}diff --git a/frontend/src/core/websocket/useMarimoWebSocket.tsx b/frontend/src/core/websocket/useMarimoWebSocket.tsxindex 87090c01e1..86058b4bb9 100644--- a/frontend/src/core/websocket/useMarimoWebSocket.tsx+++ b/frontend/src/core/websocket/useMarimoWebSocket.tsx@@ -276,7 +276,7 @@ export function useMarimoWebSocket(opts: {/*** Unique URL for this session.*/- url: runtimeManager.getWsURL(sessionId).toString(),+ url: () => runtimeManager.getWsURL(sessionId).toString(),/*** Open callback. Set the connection status to open.diff --git a/frontend/src/core/websocket/useWebSocket.tsx b/frontend/src/core/websocket/useWebSocket.tsxindex c82fd52bb8..67f49951a3 100644--- a/frontend/src/core/websocket/useWebSocket.tsx+++ b/frontend/src/core/websocket/useWebSocket.tsx@@ -9,7 +9,7 @@ import { Logger } from "@/utils/Logger";import { isStaticNotebook } from "../static/static-state";interface UseWebSocketOptions {- url: string;+ url: () => string;static: boolean;waitToConnect?: () => Promise<void>;onOpen?: (event: WebSocketEventMap["open"]) => void;@@ -33,7 +33,7 @@ export function useWebSocket(options: UseWebSocketOptions) {? new PyodideWebsocket(PyodideBridge.INSTANCE): options.static? new StaticWebsocket()- : new ReconnectingWebSocket(rest.url, undefined, {+ : new ReconnectingWebSocket(rest.url(), undefined, {// We don't want Infinity retriesmaxRetries: 10,debug: false,diff --git a/frontend/vitest.config.ts b/frontend/vitest.config.tsindex fbb909e6df..c8416ec93f 100644--- a/frontend/vitest.config.ts+++ b/frontend/vitest.config.ts@@ -10,6 +10,7 @@ export default defineConfig({sequence: {hooks: "parallel", // Maintain parallel hook execution from Vitest 1.x},+ watch: false,},plugins: [tsconfigPaths()],});