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

Failed to execute 'postMessage' on 'Worker' when worker:true and using transformHeader function #623

Open
UlTriX opened this issue Jan 28, 2019 · 14 comments

Comments

@UlTriX
Copy link

UlTriX commented Jan 28, 2019

Using Papa v5.0.0-beta.0 trying to use transformHeader config as follow:

Papa.parse(file, { header: true, preview: 1000, worker: true, transformHeader: function (header) { return header; } });

and getting error:

Error: Failed to execute 'postMessage' on 'Worker': function (header) { return header; } could not be cloned.

What am I missing?

Thank you

@nbrysondev
Copy link

I have the same issue for the transform callback. Is this feature fully implemented?

@fabricedesre
Copy link

Functions are not transferable so in worker mode a different strategy should be used. I can think of 2 options:

  • postMessage to the main thread to run the transformation functions (likely slow).
  • define the transform functions in their own file and import it in the worker with importScripts('...');

@rbrundritt
Copy link

It is possible to pass a simple function to a web worker, although a bit hackish. You would convert the function to a string then use eval in the work to re-inflate it. For example:

From the UI thread.

var headerTransform = function(h) { return h.trim(); };

worker.postMessage({
      transformHeader: headerTransform.toString()
});

In the worker:

self.onmessage = function (e) {
    if (e.data.transformHeader) {
        eval('var transformHeader= ' + e.data.transformHeader);

       //Use the transform header.
        transformHeader(e.data.data);
    }

    //...
};

@pokoli
Copy link
Collaborator

pokoli commented Jun 6, 2019

@rbrundritt wow, this is really tricky but I do not think there is another workarround.

Do you think we can document it and thus close this issue?

@fabricedesre
Copy link

That won't work if you use a CSP that prevents eval() to protect against XSS though...

@AlayVora
Copy link

AlayVora commented Dec 30, 2021

Going through same issue. Do we have any recommended solution or documentation related to this issue?

imReker added a commit to imReker/PapaParse that referenced this issue Sep 5, 2022
When worker set to true, convert transformHeader function to string, so the config could be cloned to worker and convert back in worker thread by `eval`.
So, when using Content Security Policy, this workaround will still fail, consider to use vkThread instead.
@ac-ih
Copy link

ac-ih commented Dec 7, 2022

@mholt any eta or leads on how/when this issue will be resolved? Or perhaps an easier workaround (the one by imReker is risky)?
@pokoli

@mholt
Copy link
Owner

mholt commented Dec 8, 2022

Sorry, I haven't actually been maintaining this library for several years now. @pokoli is your person! :)

@0biWanKenobi
Copy link

ping

@pokoli
Copy link
Collaborator

pokoli commented Feb 27, 2023

@0biWanKenobi do you have any proposal to solve the issue?

@Fanna1119
Copy link

any update on this?

@KoalaBear84
Copy link

I'm getting the same for beforeFirstChunk and chunk. No idea how to solve it.

Papa.parse(event.target.files[0], {
	worker: true,
	header: true,
	dynamicTyping: true,
	beforeFirstChunk: function (chunk) {
		debugger;
		return chunk;
	},
	chunk: function chunk(results, parser) {
		debugger;
	}
});

@KoalaBear84
Copy link

KoalaBear84 commented Oct 16, 2023

I now do somewhat understand the issue:
https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm#things_that_dont_work_with_structured_clone

Function cannot be 'posted' / used in a Worker.

I see that PapaParse does something special for only functions like step, chunk, complete and error, and not for the other function parameters:

PapaParse/papaparse.js

Lines 213 to 216 in e2d570e

_config.step = isFunction(_config.step);
_config.chunk = isFunction(_config.chunk);
_config.complete = isFunction(_config.complete);
_config.error = isFunction(_config.error);

But I also see when I'm not using beforeFirstChunk, it 'works', but it does NOT chunk, it just get's back with 57K rows, even though I have limited the chunk on 10240 bytes:

Papa.RemoteChunkSize = Papa.LocalChunkSize = 10 * 1024;

Oh, nvm, when you use a worker I guess the above chunksizes are now available and you need to fill chunkSize in the config, then it works

Don't have enough knowledge to fix it. I see there some bad attempts made for the transform in a push request 😅

@adamreisnz
Copy link

This appears to still be an issue as of today (Aug 2024), when using the beforeChunk function.

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