diff --git a/index.mjs b/index.mjs index a6247c4..5ba1eda 100644 --- a/index.mjs +++ b/index.mjs @@ -17,13 +17,22 @@ import { Font } from "./src/lib-font/lib-font-wrapper.mjs"; import Fondue from "./src/fondue/Fondue.mjs"; -export function fromPath(fontPath) { +export async function fromPath(fontPath) { + const { readFile } = await import("node:fs/promises"); + return new Promise((resolve, reject) => { const font = new Font(fontPath); font.onload = () => resolve(new Fondue(font)); font.onerror = (e) => reject(e.detail.message); - font.src = fontPath; + (async () => { + try { + const file = await readFile(fontPath); + font.fromDataBuffer(file.buffer, fontPath).catch(reject); + } catch (e) { + reject(e); + } + })(); }); } diff --git a/test/Fondue.test.mjs b/test/Fondue.test.mjs index 762518d..e83e7ca 100644 --- a/test/Fondue.test.mjs +++ b/test/Fondue.test.mjs @@ -50,7 +50,9 @@ describe("The loaded font", () => { test("throws an error when it doesn't exist.", async () => { await expect(() => fromPath("./fonts/foo.ttf")).rejects.toEqual( - "ENOENT: no such file or directory, open './fonts/foo.ttf'", + new Error( + "ENOENT: no such file or directory, open './fonts/foo.ttf'", + ), ); }); diff --git a/test/support/no-experimental-fetch.mjs b/test/support/no-experimental-fetch.mjs deleted file mode 100644 index 7527d25..0000000 --- a/test/support/no-experimental-fetch.mjs +++ /dev/null @@ -1,9 +0,0 @@ -// Alternative for `node --no-experimental-fetch`. -// The goal is to let the unit tests use the `fetch` shim function from `lib-font`, -// instead of the experimental `fetch` API that is now available in new Node.js versions. -// This is meant as a temporary workaround. -// -// See: -// https://github.com/Pomax/lib-font/blob/ebb3706649e9accfc8ac8df8d239dac3c167cd99/src/utils/shim-fetch.js#L6-L8 -// https://nodejs.org/docs/latest-v22.x/api/cli.html#--no-experimental-fetch -delete globalThis.fetch; diff --git a/vitest.config.mjs b/vitest.config.mjs deleted file mode 100644 index 67844bb..0000000 --- a/vitest.config.mjs +++ /dev/null @@ -1,7 +0,0 @@ -import { defineConfig } from "vite"; - -export default defineConfig({ - test: { - setupFiles: ["./test/support/no-experimental-fetch.mjs"], - }, -});