Skip to content

Commit de3c618

Browse files
committed
fix: add handling for font files and improve error processing in CoCreateFileSystem
1 parent 924041b commit de3c618

File tree

1 file changed

+37
-13
lines changed

1 file changed

+37
-13
lines changed

src/index.js

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,39 @@ class CoCreateFileSystem {
199199

200200
let contentType = file["content-type"] || "text/html";
201201

202-
if (
202+
// Add handling for font files
203+
if (contentType.startsWith("font/") || /\.(woff2?|ttf|otf)$/i.test(pathname)) {
204+
try {
205+
if (typeof src === "string") {
206+
if (/^([A-Za-z0-9+/]+={0,2})$/.test(src)) {
207+
// Decode base64-encoded font data
208+
src = Buffer.from(src, "base64");
209+
} else {
210+
throw new Error("Font data is not valid base64");
211+
}
212+
} else if (typeof src === "object" && src.src) {
213+
// Handle case where src is an object containing base64 data
214+
if (/^([A-Za-z0-9+/]+={0,2})$/.test(src.src)) {
215+
src = Buffer.from(src.src, "base64");
216+
} else {
217+
throw new Error("Font data inside object is not valid base64");
218+
}
219+
} else {
220+
throw new Error("Font data is not a valid base64 string or object");
221+
}
222+
} catch (err) {
223+
console.error("Error processing font file:", {
224+
message: err.message,
225+
contentType,
226+
srcType: typeof src,
227+
srcPreview: typeof src === "string" ? src.slice(0, 50) : null
228+
});
229+
let pageNotFound = await getDefaultFile("/404.html");
230+
return sendResponse(pageNotFound.object[0].src, 404, {
231+
"Content-Type": "text/html"
232+
});
233+
}
234+
} else if (
203235
/^data:image\/[a-zA-Z0-9+.-]+;base64,([A-Za-z0-9+/]+={0,2})$/.test(
204236
src
205237
)
@@ -244,10 +276,7 @@ class CoCreateFileSystem {
244276
file["cache-control"] !== null
245277
) {
246278
const val = file["cache-control"];
247-
if (
248-
typeof val === "number" ||
249-
/^\s*\d+\s*$/.test(val)
250-
) {
279+
if (typeof val === "number" || /^\s*\d+\s*$/.test(val)) {
251280
// If it's numeric (number or numeric string) treat it as max-age.
252281
cacheControl = `public, max-age=${String(
253282
val
@@ -257,19 +286,14 @@ class CoCreateFileSystem {
257286
cacheControl = val;
258287
}
259288
} else {
260-
cacheControl =
261-
organization["cache-control"] ||
262-
"public, max-age=3600";
289+
cacheControl = organization["cache-control"] || "public, max-age=3600";
263290
}
264291

265292
// Always override/set Cache-Control header so the response aligns with the file metadata/defaults
266293
headers["Cache-Control"] = cacheControl;
267294

268-
if (src instanceof Uint8Array) {
269-
src = Buffer.from(src);
270-
} else if (Buffer.isBuffer(src)) {
271-
// Buffer is fine to send — don't bail out here (previous code returned early)
272-
// keep src as-is
295+
if (src instanceof Uint8Array || Buffer.isBuffer(src)) {
296+
// Ensure binary data is sent as-is
273297
} else if (typeof src === "object") {
274298
src = JSON.stringify(src);
275299
}

0 commit comments

Comments
 (0)