Skip to content

Commit 8bddb02

Browse files
committed
fix: improve file content decoding and enhance font handling logic
1 parent c1110b7 commit 8bddb02

File tree

1 file changed

+6
-28
lines changed

1 file changed

+6
-28
lines changed

src/index.js

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -199,35 +199,13 @@ class CoCreateFileSystem {
199199

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

202-
try {
203-
if (typeof src === "string") {
204-
// Decode the file content based on its MIME type
205-
if (/^(image|audio|video|font|application\/octet-stream|application\/x-font-ttf|application\/x-font-woff|application\/x-font-woff2|application\/x-font-opentype|application\/x-font-truetype|application\/x-font-eot)/.test(contentType)) {
206-
src = Buffer.from(src, "base64");
207-
} else if (/^(application\/zip|application\/x-7z-compressed|application\/x-rar-compressed|application\/pdf)/.test(contentType)) {
208-
src = Buffer.from(src, "binary");
209-
} else {
210-
src = Buffer.from(src, "utf8");
211-
}
212-
} else {
213-
throw new Error("File content is not in a valid format");
214-
}
215-
} catch (err) {
216-
console.error("Error decoding file content:", {
217-
message: err.message,
218-
contentType,
219-
srcType: typeof src
220-
});
221-
let pageNotFound = await getDefaultFile("/404.html");
222-
return sendResponse(pageNotFound.object[0].src, 404, {
223-
"Content-Type": "text/html"
224-
});
225-
}
226-
227202
// Remove redundant handling for `src.src` in font file processing
228203
if (contentType.startsWith("font/") || /\.(woff2?|ttf|otf)$/i.test(pathname)) {
229204
try {
230205
if (typeof src === "string") {
206+
if (src.startsWith("data:font/")) {
207+
src = src.substring(src.indexOf(",") + 1);
208+
}
231209
if (/^([A-Za-z0-9+/]+={0,2})$/.test(src)) {
232210
// Decode base64-encoded font data
233211
src = Buffer.from(src, "base64");
@@ -275,10 +253,10 @@ class CoCreateFileSystem {
275253
contentType === "text/xml" ||
276254
contentType === "application/xml"
277255
) {
278-
const protocol = "https://"; // || req.headers['x-forwarded-proto'] || req.protocol;
256+
const protocol = "https://";
279257
src = src.replaceAll("{{$host}}", `${protocol}${hostname}`);
280-
} else {
281-
// Log unknown file types
258+
} else if (contentType !== "text/javascript" || contentType === "text/css") {
259+
282260
console.warn(`Unknown content type: ${contentType}`);
283261
}
284262

0 commit comments

Comments
 (0)