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

Issues with Cloudflare Workers/Pages #401

Closed
raveclassic opened this issue Aug 4, 2022 · 2 comments
Closed

Issues with Cloudflare Workers/Pages #401

raveclassic opened this issue Aug 4, 2022 · 2 comments
Labels

Comments

@raveclassic
Copy link

raveclassic commented Aug 4, 2022

Hey @mtth! Thanks for this awesome library!

I'm currently trying to set up some encoding/decoding processes in a Cloudflare Worker environment. Basically it nails down to encoding jsons on the server side and encoding them on the client side.
The pitfall is that Cloudflare Workers are running pure V8, not Node, so most of avsc internals don't work out of the box due to missing Buffer, missing utils package etc.
I am able to polyfill them in my bundler so that altough bundle size increases quite a bit, it's still fine for the POC, let's skip this part for now.
The main issue with avsc is that it heavily relies on new Function contstructor and building functions from source code directly in runtime.
Unfortunately, due to security reasons, Cloudflare does not allow such things in worker environment, so the code below throws:

avsc/lib/types.js

Lines 723 to 736 in c639014

Type.prototype._createBranchConstructor = function () {
// jshint -W054
var name = this.branchName;
if (name === 'null') {
return null;
}
var attr = ~name.indexOf('.') ? 'this[\'' + name + '\']' : 'this.' + name;
var body = 'return function Branch$(val) { ' + attr + ' = val; };';
var Branch = (new Function(body))();
Branch.type = this;
Branch.prototype.unwrap = new Function('return ' + attr + ';');
Branch.prototype.unwrapped = Branch.prototype.unwrap; // Deprecated.
return Branch;
};

var Branch = (new Function(body))(); - throws EvalError: Code generation from strings disallowed for this context, this error is specific to Cloudflare and how they set V8 up.

So the question here - without digging too much in the code and purposes of these "branch constructors", is it even possible to eliminate this in avsc? What are the reasons for having such function constructors? Is it performance or something else?

Thanks! Really looking forward to seeing avsc work in Cloudflare Workers!

@mtth
Copy link
Owner

mtth commented Aug 10, 2022

Hi @raveclassic. Code generation is not currently optional, unfortunately for your use-case. #223 was opened a while back to support this feature but is still pending. #221 is also related.

What are the reasons for having such function constructors? Is it performance or something else?

Right, performance. The dynamically generated functions provide a ~10x speed boost over the static alternative (the PR linked above has a few numbers if you are curious).

W.r.t. next steps, I am still happy to review a PR which implements this as an optional feature. It's a rather large change though so will require a non-trivial amount of work to do well. I can provide a few pointers on where to start if you/your team are interested in picking this up.

@mtth mtth added the question label Aug 10, 2022
@mtth
Copy link
Owner

mtth commented Nov 2, 2022

Closing this as the improvement is already tracked in #221.

@mtth mtth closed this as not planned Won't fix, can't repro, duplicate, stale Nov 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants