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

Support for CDN resource of pthread worker #22654

Closed
DylanShang opened this issue Sep 29, 2024 · 2 comments
Closed

Support for CDN resource of pthread worker #22654

DylanShang opened this issue Sep 29, 2024 · 2 comments

Comments

@DylanShang
Copy link

DylanShang commented Sep 29, 2024

Hi, developers

When I host a multi-threaded package on platforms like jsDelivr with proper cross-origin isolation settings, I encounter an error such as:

Uncaught SecurityError: Failed to construct 'Worker': Script at 'https://cdn.jsdelivr.net/npm/xxx' cannot be accessed from origin 'http://127.0.0.1:8080'.

This issue arises because workers cannot be created across different origins. According to the discussion in this Webpack issue and the solution proposed in this comment, we can use fetch to bypass this limitation.

A potential solution is modify the allocateUnusedWorker function from

  allocateUnusedWorker() {
    var worker;
    var workerOptions = {
      "type": "module","name": "em-pthread"
    };
    worker = new Worker(new URL("your js file", import.meta.url), workerOptions);
    PThread.unusedWorkers.push(worker);
  }

to

allocateUnusedWorker() {
    var workerOptions = {
        type: "module",
        name: "em-pthread"
    };

    try {
        // Use XMLHttpRequest for a synchronous request
        var xhr = new XMLHttpRequest();
        xhr.open("GET", new URL('your js file', import.meta.url), false); // false means synchronous request
        xhr.send(null);

        // Check if the request was successful
        if (xhr.status === 200) {
            const scriptText = xhr.responseText; // Get the response text
            
            // Create a Blob URL
            const workerUrl = URL.createObjectURL(new Blob([scriptText], {
                type: 'application/javascript'
            }));

            // Create a Worker and add it to the unused Workers list
            PThread.unusedWorkers.push(new Worker(workerUrl, workerOptions));
        } else {
            console.error('Failed to fetch the script:', xhr.status);
        }
    } catch (error) {
        console.error("Error occurred while allocating worker:", error);
    }
}
@DylanShang DylanShang changed the title Support for CND resource of pthread worker Support for CDN resource of pthread worker Sep 29, 2024
@emscripten-core emscripten-core deleted a comment Sep 30, 2024
@msqr1
Copy link

msqr1 commented Oct 2, 2024

#21937 (comment) is a workaround, I think. This may be a duplicate of #21937

@DylanShang
Copy link
Author

close it as the duplicate issue

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