Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions shared/util/fileUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,18 @@ export async function loadFileBuffer(requireUrl: {
// in vitest, this is the url we need to load with readfilesync
// eslint-disable-next-line
const fs = require("fs");
let url = requireUrl.default;

if (url.startsWith("/@fs/")) {
url = url.substring("/@fs".length);
// Direct paths can contain literal percent sequences. Prefer an existing
// path, then decode Vite's /@fs and encoded absolute asset URLs.
const assetPath = requireUrl.default;
let url = assetPath;
if (assetPath.startsWith("/@fs/")) {
url = decodeURIComponent(assetPath.substring("/@fs".length));
} else if (!fs.existsSync(assetPath)) {
try {
url = decodeURIComponent(assetPath);
} catch {
// Preserve a malformed percent sequence as a literal filesystem path.
}
}
// On Windows, vite/vitest may yield paths like "/C:/..." after removing /@fs
// Node on Windows treats paths starting with "/" as relative to current drive,
Expand Down
63 changes: 63 additions & 0 deletions tests/src/unit/shared/fileUtil.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { loadFileBuffer, loadFontDataUrl } from "@shared/util/fileUtil.js";
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { pathToFileURL } from "node:url";
import { afterEach, beforeEach, describe, expect, it } from "vite-plus/test";

describe("Vite asset file loading", () => {
let directory: string;
const contents = Buffer.from([0, 127, 128, 255]);

beforeEach(() => {
directory = mkdtempSync(join(tmpdir(), "blocknote-file-url-"));
});

afterEach(() => {
rmSync(directory, { recursive: true, force: true });
});

for (const prefix of ["", "/@fs"]) {
it.each(["한글 font.ttf", "font #100% %20.ttf"])(
`reads encoded ${prefix || "absolute"} asset paths: %s`,
async (name) => {
const file = join(directory, name);
writeFileSync(file, contents);

const buffer = await loadFileBuffer({
default: prefix + pathToFileURL(file).pathname,
});

expect(buffer).toEqual(contents);
},
);
}

it("reads an unencoded filesystem path", async () => {
const file = join(directory, "font.ttf");
writeFileSync(file, contents);

expect(await loadFileBuffer({ default: file })).toEqual(contents);
});

it.each(["font%20.ttf", "font%foo.ttf"])(
"preserves a direct filesystem path containing %s",
async (name) => {
const file = join(directory, name);
writeFileSync(file, contents);

expect(await loadFileBuffer({ default: file })).toEqual(contents);
},
);

it("creates a font data URL from an encoded asset path", async () => {
const file = join(directory, "한글 font.ttf");
writeFileSync(file, contents);

expect(
await loadFontDataUrl({
default: "/@fs" + pathToFileURL(file).pathname,
}),
).toBe(`data:font/ttf;base64,${contents.toString("base64")}`);
});
});