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.

refactor: simplify base64 utils (#7943)

Myles Scolnicka681a6ceef31
diff --git a/frontend/src/components/data-table/column-summary/legacy-chart-spec.ts b/frontend/src/components/data-table/column-summary/legacy-chart-spec.ts
index b7049f9676..2c2bf856d6 100644
--- a/frontend/src/components/data-table/column-summary/legacy-chart-spec.ts
+++ b/frontend/src/components/data-table/column-summary/legacy-chart-spec.ts
@@ -12,10 +12,9 @@ import type { TopLevelFacetedUnitSpec } from "@/plugins/impl/data-explorer/queri
import { arrow } from "@/plugins/impl/vega/formats";
import { parseCsvData } from "@/plugins/impl/vega/loader";
import {
- byteStringToBinary,
+ base64ToUint8Array,
extractBase64FromDataURL,
isDataURLString,
- typedAtob,
} from "@/utils/json/base64";
export function getLegacyNumericSpec(
@@ -281,12 +280,12 @@ export function getDataSpecAndSourceName<T>(data: string | T[]): {
} else if (isDataURLString(data)) {
sourceName = "data_0";
const base64 = extractBase64FromDataURL(data);
- const decoded = typedAtob(base64);
+ const decoded = window.atob(base64);
// eslint-disable-next-line unicorn/prefer-ternary
if (decoded.startsWith(ARROW_MAGIC_NUMBER)) {
dataSpec = {
- values: byteStringToBinary(decoded),
+ values: base64ToUint8Array(base64),
// @ts-expect-error vega-typings does not include arrow format
format: { type: "arrow" },
};
diff --git a/frontend/src/components/editor/actions/name-cell-input.tsx b/frontend/src/components/editor/actions/name-cell-input.tsx
index a5062bf7d6..6e991fe0cc 100644
--- a/frontend/src/components/editor/actions/name-cell-input.tsx
+++ b/frontend/src/components/editor/actions/name-cell-input.tsx
@@ -98,10 +98,8 @@ export const NameCellContentEditable: React.FC<{
e.stopPropagation();
// On Enter, blur the input to commit the change
- if (e.key === "Enter") {
- if (e.target instanceof HTMLElement) {
- e.target.blur();
- }
+ if (e.key === "Enter" && e.target instanceof HTMLElement) {
+ e.target.blur();
}
}}
>
diff --git a/frontend/src/utils/__tests__/data-views.test.ts b/frontend/src/utils/__tests__/data-views.test.ts
index c81aaab990..a592afae19 100644
--- a/frontend/src/utils/__tests__/data-views.test.ts
+++ b/frontend/src/utils/__tests__/data-views.test.ts
@@ -1,7 +1,6 @@
/* Copyright 2026 Marimo. All rights reserved. */
import { describe, expect, it } from "vitest";
import {
- dataViewToBase64,
decodeFromWire,
isWireFormat,
serializeBuffersToBase64,
@@ -260,50 +259,6 @@ describe("Immutability Tests", () => {
});
});
-describe("dataViewToBase64", () => {
- it("should convert a DataView to a base64 string", () => {
- const encoder = new TextEncoder();
- const bytes = encoder.encode("Hello, World!");
- const dataView = new DataView(bytes.buffer);
- const base64 = dataViewToBase64(dataView);
-
- // Decode and verify
- const decoded = atob(base64);
- expect(decoded).toBe("Hello, World!");
- });
-
- it("should handle empty DataView", () => {
- const dataView = new DataView(new ArrayBuffer(0));
- const base64 = dataViewToBase64(dataView);
- expect(base64).toBe("");
- });
-
- it("should handle DataView with offset and length", () => {
- const encoder = new TextEncoder();
- const bytes = encoder.encode("Hello, World!");
- // Create a DataView that only looks at "World!"
- const dataView = new DataView(bytes.buffer, 7, 6);
- const base64 = dataViewToBase64(dataView);
-
- const decoded = atob(base64);
- expect(decoded).toBe("World!");
- });
-
- it("should handle binary data", () => {
- const bytes = new Uint8Array([0, 1, 2, 255, 254, 253]);
- const dataView = new DataView(bytes.buffer);
- const base64 = dataViewToBase64(dataView);
-
- // Verify round-trip
- const decoded = atob(base64);
- const decodedBytes = new Uint8Array(decoded.length);
- for (let i = 0; i < decoded.length; i++) {
- decodedBytes[i] = decoded.charCodeAt(i);
- }
- expect([...decodedBytes]).toEqual([0, 1, 2, 255, 254, 253]);
- });
-});
-
describe("serializeBuffersToBase64", () => {
it("should return empty arrays when no DataViews present", () => {
const input = { a: 1, b: "text", c: { d: true } };
diff --git a/frontend/src/utils/data-views.ts b/frontend/src/utils/data-views.ts
index 457ce47536..2a29133d2d 100644
--- a/frontend/src/utils/data-views.ts
+++ b/frontend/src/utils/data-views.ts
@@ -3,26 +3,11 @@ import { get, set } from "lodash-es";
import { invariant } from "./invariant";
import {
type Base64String,
- binaryToByteString,
- byteStringToBinary,
- typedAtob,
- typedBtoa,
+ base64ToDataView,
+ dataViewToBase64,
} from "./json/base64";
import { Logger } from "./Logger";
-/**
- * Convert a DataView to a base64 string.
- */
-export function dataViewToBase64(dataView: DataView): Base64String {
- const bytes = new Uint8Array(
- dataView.buffer,
- dataView.byteOffset,
- dataView.byteLength,
- );
- const byteString = binaryToByteString(bytes);
- return typedBtoa(byteString);
-}
-
/**
* Recursively find all DataViews in an object and return their paths.
*
@@ -148,8 +133,7 @@ export function decodeFromWire<T extends Record<string, unknown>>(input: {
// Handle both base64 strings (from wire format) and DataViews (direct usage)
if (typeof buffer === "string") {
- const bytes = byteStringToBinary(typedAtob(buffer));
- set(out, bufferPath, new DataView(bytes.buffer));
+ set(out, bufferPath, base64ToDataView(buffer));
} else {
set(out, bufferPath, buffer);
}
diff --git a/frontend/src/utils/json/__tests__/base64.test.ts b/frontend/src/utils/json/__tests__/base64.test.ts
index 2c3e38d0ea..b798873e77 100644
--- a/frontend/src/utils/json/__tests__/base64.test.ts
+++ b/frontend/src/utils/json/__tests__/base64.test.ts
@@ -2,99 +2,175 @@
import { describe, expect, test } from "vitest";
import {
type Base64String,
- type ByteString,
base64ToDataURL,
- byteStringToBinary,
+ base64ToDataView,
+ base64ToUint8Array,
type DataURLString,
- deserializeBase64,
+ dataViewToBase64,
+ deserializeJson,
extractBase64FromDataURL,
isDataURLString,
type JsonString,
- typedAtob,
- typedBtoa,
+ uint8ArrayToBase64,
} from "../base64";
-describe("base64Utils", () => {
- const testData = {
- name: "marimo inc",
- type: "company",
- };
+describe("base64", () => {
+ describe("deserializeJson", () => {
+ test("deserializes JSON string to object", () => {
+ const json = '{"name":"marimo","type":"notebook"}' as JsonString<{
+ name: string;
+ type: string;
+ }>;
+ const result = deserializeJson(json);
+ expect(result).toEqual({ name: "marimo", type: "notebook" });
+ });
- const expectedBase64 =
- "JTdCJTIybmFtZSUyMiUzQSUyMm1hcmltbyUyMGluYyUyMiUyQyUyMnR5cGUlMjIlM0ElMjJjb21wYW55JTIyJTdE" as Base64String<
- JsonString<typeof testData>
- >;
-
- test("serializeJsonToBase64 should correctly encode JSON to Base64", () => {
- const base64Encoded = serializeJsonToBase64(testData);
- expect(base64Encoded).toBe(expectedBase64);
+ test("deserializes JSON array", () => {
+ const json = "[1,2,3]" as JsonString<number[]>;
+ const result = deserializeJson(json);
+ expect(result).toEqual([1, 2, 3]);
+ });
});
- test("deserializeBase64ToJson should correctly decode Base64 to JSON", () => {
- const jsonDecoded = deserializeBase64(expectedBase64);
- expect(JSON.parse(jsonDecoded)).toEqual(testData);
- });
+ describe("base64ToDataURL", () => {
+ test("creates proper data URL with mime type", () => {
+ const base64 = "SGVsbG8=" as Base64String;
+ const result = base64ToDataURL(base64, "text/plain");
+ expect(result).toBe("data:text/plain;base64,SGVsbG8=");
+ });
- test("serializeJsonToBase64 and deserializeBase64ToJson should be reversible", () => {
- const base64Encoded = serializeJsonToBase64(testData);
- const jsonDecoded = deserializeBase64(base64Encoded);
- expect(JSON.parse(jsonDecoded)).toEqual(testData);
+ test("handles image mime types", () => {
+ const base64 = "iVBORw0KGgo=" as Base64String;
+ const result = base64ToDataURL(base64, "image/png");
+ expect(result).toBe("data:image/png;base64,iVBORw0KGgo=");
+ });
});
- test("base64ToDataURL should create proper data URL", () => {
- const base64 = "SGVsbG8=" as Base64String;
- const result = base64ToDataURL(base64, "text/plain");
- expect(result).toBe("data:text/plain;base64,SGVsbG8=");
+ describe("isDataURLString", () => {
+ test.each([
+ ["data:text/plain;base64,SGVsbG8=", true],
+ ["data:image/png;base64,iVBORw0KGgoAAAANSUhEUgA", true],
+ ["not-a-data-url", false],
+ ["base64,SGVsbG8=", false],
+ ["data:text/plain,hello", false],
+ ["data:text/plain;charset=utf-8,hello", false],
+ ])("isDataURLString(%s) returns %s", (input, expected) => {
+ expect(isDataURLString(input)).toBe(expected);
+ });
});
- test("typedAtob and typedBtoa should be reversible", () => {
- const original = "hello world" as ByteString;
- const encoded = typedBtoa(original);
- const decoded = typedAtob(encoded);
- expect(decoded).toBe(original);
+ describe("extractBase64FromDataURL", () => {
+ test.each([
+ ["data:text/plain;base64,SGVsbG8=", "SGVsbG8="],
+ [
+ "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgA",
+ "iVBORw0KGgoAAAANSUhEUgA",
+ ],
+ ["data:application/json;base64,", ""],
+ ["data:text/html;charset=utf-8;base64,PGh0bWw+", "PGh0bWw+"],
+ ])("extracts base64 from %s", (dataUrl, expected) => {
+ expect(extractBase64FromDataURL(dataUrl as DataURLString)).toBe(expected);
+ });
});
- test.each([
- ["data:text/plain;base64,SGVsbG8=", true],
- ["data:image/png;base64,iVBORw0KGgoAAAANSUhEUgA", true],
- ["not-a-data-url", false],
- ["base64,SGVsbG8=", false],
- ])("isDataURLString(%s) should return %s", (input, expected) => {
- expect(isDataURLString(input)).toBe(expected);
+ describe("base64ToUint8Array", () => {
+ test("converts base64 to Uint8Array", () => {
+ const base64 = "QUJD" as Base64String; // "ABC"
+ const result = base64ToUint8Array(base64);
+ expect(result).toEqual(new Uint8Array([65, 66, 67]));
+ });
+
+ test("handles empty base64 string", () => {
+ const base64 = "" as Base64String;
+ const result = base64ToUint8Array(base64);
+ expect(result).toEqual(new Uint8Array([]));
+ });
});
- test("isDataURLString should reject data URLs without base64", () => {
- expect(isDataURLString("data:text/plain,hello")).toBe(false);
- expect(isDataURLString("data:text/plain;charset=utf-8,hello")).toBe(false);
+ describe("base64ToDataView", () => {
+ test("converts base64 to DataView", () => {
+ const base64 = "QUJD" as Base64String; // "ABC"
+ const result = base64ToDataView(base64);
+ expect(result.byteLength).toBe(3);
+ expect(result.getUint8(0)).toBe(65);
+ expect(result.getUint8(1)).toBe(66);
+ expect(result.getUint8(2)).toBe(67);
+ });
});
- test.each([
- ["data:text/plain;base64,SGVsbG8=", "SGVsbG8="],
- [
- "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgA",
- "iVBORw0KGgoAAAANSUhEUgA",
- ],
- ["data:application/json;base64,", ""],
- ["data:text/html;charset=utf-8;base64,PGh0bWw+", "PGh0bWw+"],
- ])("extractBase64FromDataURL(%s) should return %s", (dataUrl, expected) => {
- expect(extractBase64FromDataURL(dataUrl as DataURLString)).toBe(expected);
+ describe("uint8ArrayToBase64", () => {
+ test("converts Uint8Array to base64", () => {
+ const array = new Uint8Array([65, 66, 67]);
+ const result = uint8ArrayToBase64(array);
+ expect(result).toBe("QUJD");
+ });
+
+ test("handles empty Uint8Array", () => {
+ const array = new Uint8Array([]);
+ const result = uint8ArrayToBase64(array);
+ expect(result).toBe("");
+ });
});
- test("byteStringToBinary should convert to Uint8Array", () => {
- const bytes = "ABC" as ByteString;
- const result = byteStringToBinary(bytes);
- expect(result).toEqual(new Uint8Array([65, 66, 67]));
+ describe("dataViewToBase64", () => {
+ test("should convert a DataView to a base64 string", () => {
+ const encoder = new TextEncoder();
+ const bytes = encoder.encode("Hello, World!");
+ const dataView = new DataView(bytes.buffer);
+ const base64 = dataViewToBase64(dataView);
+
+ // Decode and verify
+ const decoded = atob(base64);
+ expect(decoded).toBe("Hello, World!");
+ });
+
+ test("should handle empty DataView", () => {
+ const dataView = new DataView(new ArrayBuffer(0));
+ const base64 = dataViewToBase64(dataView);
+ expect(base64).toBe("");
+ });
+
+ test("should handle DataView with offset and length", () => {
+ const encoder = new TextEncoder();
+ const bytes = encoder.encode("Hello, World!");
+ // Create a DataView that only looks at "World!"
+ const dataView = new DataView(bytes.buffer, 7, 6);
+ const base64 = dataViewToBase64(dataView);
+
+ const decoded = atob(base64);
+ expect(decoded).toBe("World!");
+ });
+
+ test("should handle binary data", () => {
+ const bytes = new Uint8Array([0, 1, 2, 255, 254, 253]);
+ const dataView = new DataView(bytes.buffer);
+ const base64 = dataViewToBase64(dataView);
+
+ // Verify round-trip
+ const decoded = atob(base64);
+ const decodedBytes = new Uint8Array(decoded.length);
+ for (let i = 0; i < decoded.length; i++) {
+ decodedBytes[i] = decoded.charCodeAt(i);
+ }
+ expect([...decodedBytes]).toEqual([0, 1, 2, 255, 254, 253]);
+ });
});
- test("byteStringToBinary should handle empty string", () => {
- const bytes = "" as ByteString;
- const result = byteStringToBinary(bytes);
- expect(result).toEqual(new Uint8Array([]));
+ describe("round-trip conversions", () => {
+ test("Uint8Array to base64 and back", () => {
+ const original = new Uint8Array([1, 2, 3, 255, 128, 0]);
+ const base64 = uint8ArrayToBase64(original);
+ const result = base64ToUint8Array(base64);
+ expect(result).toEqual(original);
+ });
+
+ test("DataView to base64 and back", () => {
+ const buffer = new ArrayBuffer(4);
+ const original = new DataView(buffer);
+ original.setUint32(0, 0x12_34_56_78);
+ const base64 = dataViewToBase64(original);
+ const result = base64ToDataView(base64);
+ expect(result.getUint32(0)).toBe(0x12_34_56_78);
+ });
});
});
-
-// Serialization: JSON to Base64
-function serializeJsonToBase64<T>(jsonObject: T) {
- const jsonString = JSON.stringify(jsonObject);
- return btoa(encodeURIComponent(jsonString)) as Base64String<JsonString<T>>;
-}
diff --git a/frontend/src/utils/json/base64.ts b/frontend/src/utils/json/base64.ts
index 636843a440..e42d1258fc 100644
--- a/frontend/src/utils/json/base64.ts
+++ b/frontend/src/utils/json/base64.ts
@@ -3,64 +3,88 @@
import type { NotificationMessageData } from "@/core/kernel/messages";
import type { TypedString } from "../typed";
+/**
+ * A JSON string of a given type.
+ */
export type JsonString<T = unknown> = TypedString<"Json"> & {
_of_: T;
};
-export type Base64String<T = unknown> = TypedString<"Base64"> & {
- _of_: T;
-};
-
-export type ByteString<T = unknown> = TypedString<"ByteString"> & {
- _of_: T;
-};
-
-export type DataURLString = `data:${string};base64,${Base64String<string>}`;
+/**
+ * A base64-encoded string.
+ */
+export type Base64String = TypedString<"Base64">;
-// Deserialization: Base64 to String
-export function deserializeBase64<T>(base64: Base64String<T>): T {
- const decodedString = decodeURIComponent(atob(base64));
- return decodedString as T;
-}
+/**
+ * A data URL string.
+ */
+export type DataURLString = `data:${string};base64,${Base64String}`;
-// Deserialization: String to JSON
+/**
+ * Typed JSON deserialization.
+ */
export function deserializeJson<T>(jsonString: JsonString<T>): T {
return JSON.parse(jsonString) as T;
}
-export function base64ToDataURL<T>(
- base64: Base64String<T>,
+/**
+ * Convert a base64 string to a data URL string.
+ */
+export function base64ToDataURL(
+ base64: Base64String,
mimeType: string,
): DataURLString {
- return `data:${mimeType};base64,${base64}` as DataURLString;
-}
-
-export function typedAtob<T>(base64: Base64String<T>): ByteString<T> {
- return window.atob(base64) as ByteString<T>;
-}
-
-export function typedBtoa<T>(bytes: ByteString<T>): Base64String<T> {
- return window.btoa(bytes) as Base64String<T>;
+ return `data:${mimeType};base64,${base64}`;
}
+/**
+ * Check if a string is a data URL string.
+ */
export function isDataURLString(str: string): str is DataURLString {
return str.startsWith("data:") && str.includes(";base64,");
}
+/**
+ * Extract the base64 string from a data URL string.
+ */
export function extractBase64FromDataURL(str: DataURLString): Base64String {
return str.split(",")[1] as Base64String;
}
-export function byteStringToBinary(bytes: ByteString): Uint8Array {
- return Uint8Array.from(bytes, (c) => c.charCodeAt(0));
+/**
+ * Convert a base64 string to a Uint8Array.
+ */
+export function base64ToUint8Array(bytes: Base64String): Uint8Array {
+ const binary = window.atob(bytes);
+ return Uint8Array.from(binary, (c) => c.charCodeAt(0));
+}
+
+/**
+ * Convert a base64 string to a DataView.
+ */
+export function base64ToDataView(bytes: Base64String): DataView {
+ const uint8Array = base64ToUint8Array(bytes);
+ return new DataView(uint8Array.buffer);
+}
+
+/**
+ * Convert a Uint8Array to a base64 string.
+ */
+export function uint8ArrayToBase64(binary: Uint8Array): Base64String {
+ const chars = Array.from(binary, (byte) => String.fromCharCode(byte));
+ return window.btoa(chars.join("")) as Base64String;
}
-export function binaryToByteString(binary: Uint8Array): ByteString {
- let result = "";
- for (const byte of binary) {
- result += String.fromCharCode(byte);
- }
- return result as ByteString;
+/**
+ * Convert a DataView to a base64 string.
+ */
+export function dataViewToBase64(dataView: DataView): Base64String {
+ const uint8Array = new Uint8Array(
+ dataView.buffer,
+ dataView.byteOffset,
+ dataView.byteLength,
+ );
+ return uint8ArrayToBase64(uint8Array);
}
export function safeExtractSetUIElementMessageBuffers(
@@ -68,8 +92,5 @@ export function safeExtractSetUIElementMessageBuffers(
): readonly DataView[] {
// @ts-expect-error - TypeScript doesn't know that these strings are actually base64 strings
const strs: Base64String[] = notification.buffers ?? [];
- return strs.map((str) => {
- const bytes = byteStringToBinary(typedAtob(str));
- return new DataView(bytes.buffer);
- });
+ return strs.map(base64ToDataView);
}