Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace fetch(data) with local data usage #2794

Closed
mikalai-snap opened this issue Dec 2, 2024 · 3 comments
Closed

Replace fetch(data) with local data usage #2794

mikalai-snap opened this issue Dec 2, 2024 · 3 comments

Comments

@mikalai-snap
Copy link
Contributor

Search Terms

DecompressionStream, CSP, gzip, data URL, strict CSP, compressed JSON, fetch

Problem

The current implementation uses a data: URL for the compressed search index, which relies on decompressing it with DecompressionStream. This approach conflicts with stricter Content Security Policy rules, particularly when connect-src 'self' is enforced, as data: URLs are not allowed in such scenarios. This makes it harder to adopt the tool in environments with strict CSP policies.

Suggested Solution

Consider embedding the compressed search index as an inline Base64 string within the script and decompressing it directly in the browser using DecompressionStream. This avoids the need for data: URLs while maintaining compression benefits.

Here is an example of how that can be done:

// search.ts
window.searchData = "gzip+base64 string"; // as currently done, but without data:application/octet-stream;base64, prefix

// main.js
async function decompressAndParseData(base64Data) {
    const binaryData = Uint8Array.from(atob(base64Data), c => c.charCodeAt(0));
    const blob = new Blob([binaryData]);
    const decompressedStream = blob.stream().pipeThrough(new DecompressionStream("gzip"));
    const decompressedText = await new Response(decompressedStream).text();
    return JSON.parse(decompressedText);
}

const searchData = decompressAndParseData(window.searchData);
@mikalai-snap mikalai-snap added the enhancement Improved functionality label Dec 2, 2024
@Gerrit0
Copy link
Collaborator

Gerrit0 commented Dec 3, 2024

PR welcome!

1 similar comment
@Gerrit0
Copy link
Collaborator

Gerrit0 commented Dec 3, 2024

PR welcome!

@mikalai-snap
Copy link
Contributor Author

PR welcome!

Submitted! #2797

@Gerrit0 Gerrit0 closed this as completed in 2b84c16 Dec 4, 2024
@Gerrit0 Gerrit0 removed the enhancement Improved functionality label Feb 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants