deno-canvas icon indicating copy to clipboard operation
deno-canvas copied to clipboard

Can’t use on Deno Deploy

Open RoyalIcing opened this issue 3 years ago • 4 comments

I’ve tried deploying this to Deno Deploy, and get the following error:

EvalError: Code generation from strings disallowed for this context
    at new Function (<anonymous>)
    at Qb (https://raw.githubusercontent.com/DjDeveloperr/deno-canvas/v1.3.0/src/lib.js:3977:17)
    at Rb (https://raw.githubusercontent.com/DjDeveloperr/deno-canvas/v1.3.0/src/lib.js:3985:15)
    at Module.<anonymous> (https://raw.githubusercontent.com/DjDeveloperr/deno-canvas/v1.3.0/src/lib.js:5090:30)
    at init (https://raw.githubusercontent.com/DjDeveloperr/deno-canvas/v1.3.0/src/canvaskit.ts:6:22)
    at init (https://raw.githubusercontent.com/DjDeveloperr/deno-canvas/v1.3.0/src/canvas.ts:6:20)
    at https://raw.githubusercontent.com/DjDeveloperr/deno-canvas/v1.3.0/mod.ts:2:22

https://raw.githubusercontent.com/DjDeveloperr/deno-canvas/v1.3.0/src/lib.js

The culprit on line 3977 looks like:

function Qb(a, b) {
        a = Pb(a);
        return (new Function(
          "body",
          "return function " + a +
            '() {\n    "use strict";    return body.apply(this, arguments);\n};\n',
        ))(b);
      }

There‘s also another dynamic function on line 5884:

kb: function (a, b, c, f) {
          a = dd(a);
          var g = ld[b];
          if (!g) {
            g = "";
            for (var l = 0; l < b; ++l) g += (0 !== l ? ", " : "") + "arg" + l;
            var p = "return function emval_allocator_" + b +
              "(constructor, argTypes, args) {\n";
            for (l = 0; l < b; ++l) {
              p += "var argType" + l +
                " = requireRegisteredType(Module['HEAP32'][(argTypes >>> 2) + " +
                l + '], "parameter ' + l + '");\nvar arg' + l + " = argType" +
                l + ".readValueFromPointer(args);\nargs += argType" + l +
                "['argPackAdvance'];\n";
            }
            g =
              (new Function(
                "requireRegisteredType",
                "Module",
                "__emval_register",
                p +
                  ("var obj = new constructor(" + g +
                    ");\nreturn __emval_register(obj);\n}\n"),
              ))(ad, r, xc);
            ld[b] = g;
          }
          return g(a, c, f);
        },

Is there any way to change what these functions are doing without dynamic functions?

RoyalIcing avatar Dec 17 '21 03:12 RoyalIcing

It should be possible, I have removed them before but hit into timeouts / memory limits. So essentially it doesn't really help running on Deploy. It does not give enough resources for deno-canvas to be usable.

DjDeveloperr avatar Dec 17 '21 05:12 DjDeveloperr

@DjDeveloperr I can maybe help out getting it to work on Deploy. Can you give a rough estimate of deno-canvas's CPU and memory requirements under normal conditions?

I had a real quick look and deno-canvas seems to load src/wasm.js, a 9 MB file.

(A 9 MB base64-encoded string that's first decoded, then executed as WASM. I assume that's where you're hitting Deploy's resource limits.)

Maybe you can try loading canvaskit-opt.wasm (6.8 MB) directly with WebAssembly.instantiateStreaming(). Deno's docs website loads a similarly sized blob without problem.

bnoordhuis avatar Dec 17 '21 10:12 bnoordhuis

@bnoordhuis Not sure about its CPU / memory requirements, but it's mainly because of WASM I assume. I will take a stab at making this module work in Deploy again and let you know about exact issue I'm hitting

WebAssembly.instantiateStreaming() looks interesting, I will try loading WASM using that in Deploy

DjDeveloperr avatar Dec 17 '21 11:12 DjDeveloperr

I have tried changing these two functions to not use dynamic code generation in this branch, but there's another issue: it seems canvaskit entirely depends on dynamically generated code in recent versions, to provide bindings for wasm exported functions. And all of which comes from WASM land... which I cannot patch manually

DjDeveloperr avatar Dec 17 '21 11:12 DjDeveloperr

It's working fine for me on Deno Deploy apparently!

jespertheend avatar Nov 12 '23 15:11 jespertheend

It's working fine for me on Deno Deploy apparently!

Yes, apparently some limitations were lifted some time in between so it works now. Thanks for confirming! I'll close this issue then.

DjDeveloperr avatar Nov 13 '23 04:11 DjDeveloperr