Skip to content

Commit 7b6f29c

Browse files
committed
fix: enhance Cache-Control header handling in sendResponse function
1 parent 42c6ba8 commit 7b6f29c

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

src/index.js

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,11 +217,41 @@ class CoCreateFileSystem {
217217

218218
function sendResponse(src, statusCode, headers) {
219219
try {
220+
let cacheControl;
221+
if (statusCode >= 400) {
222+
cacheControl = "no-cache, no-store, must-revalidate";
223+
} else if (
224+
file &&
225+
file["cache-control"] !== undefined &&
226+
file["cache-control"] !== null
227+
) {
228+
const val = file["cache-control"];
229+
if (
230+
typeof val === "number" ||
231+
/^\s*\d+\s*$/.test(val)
232+
) {
233+
// If it's numeric (number or numeric string) treat it as max-age.
234+
cacheControl = `public, max-age=${String(
235+
val
236+
).trim()};`;
237+
} else {
238+
// use the value verbatim (allow full Cache-Control strings like "no-cache" or "public, max-age=600")
239+
cacheControl = val;
240+
}
241+
} else {
242+
cacheControl =
243+
organization["cache-control"] ||
244+
"public, max-age=3600";
245+
}
246+
247+
// Always override/set Cache-Control header so the response aligns with the file metadata/defaults
248+
headers["Cache-Control"] = cacheControl;
249+
220250
if (src instanceof Uint8Array) {
221251
src = Buffer.from(src);
222252
} else if (Buffer.isBuffer(src)) {
223-
console.log("buffer");
224-
return;
253+
// Buffer is fine to send — don't bail out here (previous code returned early)
254+
// keep src as-is
225255
} else if (typeof src === "object") {
226256
src = JSON.stringify(src);
227257
}

0 commit comments

Comments
 (0)