Skip to content

Issues with Cloudflare Workers/Pages #401

@raveclassic

Description

@raveclassic

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions