Skip to content

Commit

Permalink
Merge pull request #16 from gennaroterry/main
Browse files Browse the repository at this point in the history
fix: Improve srcset rewriter to handle descriptors correctly.
  • Loading branch information
Percslol authored Nov 17, 2024
2 parents a3a8eba + 5742a13 commit f0ee177
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 17 deletions.
26 changes: 16 additions & 10 deletions src/shared/rewriters/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,13 @@ export function rewriteHtml(

const script = (src) => new Element("script", { src });

// for compatibility purpose
const base64Injected = bytesToBase64(new TextEncoder().encode(injected));

head.children.unshift(
script($scramjet.config.files.wasm),
script($scramjet.config.files.shared),
script("data:application/javascript;base64," + btoa(injected)),
script("data:application/javascript;base64," + base64Injected),
script($scramjet.config.files.client)
);
}
Expand Down Expand Up @@ -283,17 +286,20 @@ function traverseParsedHtml(
}

export function rewriteSrcset(srcset: string, meta: URLMeta) {
const urls = srcset.split(/ [0-9]+x,? ?/g);
if (!urls) return "";
const sufixes = srcset.match(/ [0-9]+x,? ?/g);
if (!sufixes) return "";
const rewrittenUrls = urls.map((url, i) => {
if (url && sufixes[i]) {
return rewriteUrl(url, meta) + sufixes[i];
}
const sources = srcset.split(",").map((src) => src.trim());
const rewrittenSources = sources.map((source) => {
// Split into URLs and descriptors (if any)
// e.g. url0, url1 1.5x, url2 2x
const [url, ...descriptors] = source.split(/\s+/);

// Rewrite the URLs and keep the descriptors (if any)
const rewrittenUrl = rewriteUrl(url.trim(), meta);
return descriptors.length > 0
? `${rewrittenUrl} ${descriptors.join(" ")}`
: rewrittenUrl;
});

return rewrittenUrls.join("");
return rewrittenSources.join(", ");
}

// function base64ToBytes(base64) {
Expand Down
41 changes: 36 additions & 5 deletions src/worker/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ export function errorTemplate(trace: string, fetchedURL: string) {
reload.addEventListener("click", () => location.reload());
version.textContent = ${JSON.stringify($scramjet.version.version)};
build.textContent = ${JSON.stringify($scramjet.version.build)};
document.getElementById('copy-button').addEventListener('click', async () => {
const text = document.getElementById('errorTrace').value;
await navigator.clipboard.writeText(text);
const btn = document.getElementById('copy-button');
btn.textContent = 'Copied!';
setTimeout(() => btn.textContent = 'Copy', 2000);
});
`;

return `<!DOCTYPE html>
Expand Down Expand Up @@ -94,10 +102,11 @@ export function errorTemplate(trace: string, fetchedURL: string) {
}
#version-wrapper {
width: 100%;
text-align: center;
width: auto;
text-align: right;
position: absolute;
bottom: 0.2rem;
top: 0.5rem;
right: 0.5rem;
font-size: 0.8rem;
color: var(--shore)!important;
i {
Expand All @@ -107,6 +116,26 @@ export function errorTemplate(trace: string, fetchedURL: string) {
}
z-index: 101;
}
#errorTrace-wrapper {
position: relative;
width: fit-content;
}
#copy-button {
position: absolute;
top: 0.5em;
right: 0.5em;
padding: 0.23em;
cursor: pointer;
opacity: 0;
transition: opacity 0.4s;
font-size: 0.9em;
}
#errorTrace-wrapper:hover #copy-button {
opacity: 1;
}
</style>
</head>
<body>
Expand All @@ -117,8 +146,10 @@ export function errorTemplate(trace: string, fetchedURL: string) {
<!-- <p id="errorMessage">Internal Server Error</p> -->
<div id="info">
<textarea id="errorTrace" cols="40" rows="10" readonly>
</textarea>
<div id="errorTrace-wrapper">
<textarea id="errorTrace" cols="40" rows="10" readonly></textarea>
<button id="copy-button" class="primary">Copy</button>
</div>
<div id="troubleshooting">
<p>Try:</p>
<ul>
Expand Down
22 changes: 20 additions & 2 deletions src/worker/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,29 @@ export async function handleFetch(
this
);
} catch (err) {
console.error("ERROR FROM SERVICE WORKER FETCH", err);
const errorDetails = {
message: err.message,
url: request.url,
destination: request.destination,
timestamp: new Date().toISOString(),
};
if (err.stack) {
errorDetails["stack"] = err.stack;
}

console.error("ERROR FROM SERVICE WORKER FETCH: ", errorDetails);

if (!["document", "iframe"].includes(request.destination))
return new Response(undefined, { status: 500 });

return renderError(err, unrewriteUrl(request.url));
const formattedError = Object.entries(errorDetails)
.map(
([key, value]) =>
`${key.charAt(0).toUpperCase() + key.slice(1)}: ${value}`
)
.join("\n\n");

return renderError(formattedError, unrewriteUrl(request.url));
}
}

Expand Down

0 comments on commit f0ee177

Please sign in to comment.