Skip to content

Commit

Permalink
refactor: avoid fetch in lib-font, use readFile instead
Browse files Browse the repository at this point in the history
  • Loading branch information
Robbert committed Oct 19, 2024
1 parent e0289b5 commit 467c142
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 19 deletions.
13 changes: 11 additions & 2 deletions index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
})();
});
}

Expand Down
4 changes: 3 additions & 1 deletion test/Fondue.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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'",
),
);
});

Expand Down
9 changes: 0 additions & 9 deletions test/support/no-experimental-fetch.mjs

This file was deleted.

7 changes: 0 additions & 7 deletions vitest.config.mjs

This file was deleted.

0 comments on commit 467c142

Please sign in to comment.