Shashank3736 / captcha-canvas

A captcha generator by using skia-canvas.
https://captcha-canvas.js.org
Apache License 2.0
61 stars 12 forks source link

Support Deno #67

Open alexgleason opened 1 day ago

alexgleason commented 1 day ago

With Deno v1.46.3 and captcha-canvas v3, I get this error:

➜  ~ deno
Deno 1.46.3
exit using ctrl+d, ctrl+c, or close()
REPL is running with all permissions allowed.
To specify permissions, run `deno repl` with allow flags.
> import { Captcha } from 'npm:captcha-canvas@3.2.3';
Uncaught Error: Cannot find module './v6'
Require stack:
- /home/alex/.cache/deno/npm/registry.npmjs.org/skia-canvas/1.0.2/lib/index.js
- /home/alex/.cache/deno/npm/registry.npmjs.org/captcha-canvas/3.2.3/js-script/index.js
- /home/alex/.cache/deno/npm/registry.npmjs.org/captcha-canvas/3.2.3/js-script/index.js
    at Function.Module._resolveFilename (node:module:608:15)
    at Function.Module._load (node:module:486:27)
    at Module.require (node:module:674:19)
    at require (node:module:801:16)
    at Object.<anonymous> (file:///home/alex/.cache/deno/npm/registry.npmjs.org/skia-canvas/1.0.2/lib/index.js:23:29)
    at Object.<anonymous> (file:///home/alex/.cache/deno/npm/registry.npmjs.org/skia-canvas/1.0.2/lib/index.js:1071:4)
    at Module._compile (node:module:736:34)
    at Object.Module._extensions..js (node:module:757:11)
    at Module.load (node:module:655:32)
    at Function.Module._load (node:module:523:13)
> 

I also tried v2:

➜  ~ deno
Deno 1.46.3
exit using ctrl+d, ctrl+c, or close()
REPL is running with all permissions allowed.
To specify permissions, run `deno repl` with allow flags.
> import { Captcha } from 'npm:captcha-canvas';
Uncaught Error: Cannot find module '../build/Release/canvas.node'
Require stack:
- /home/alex/.cache/deno/npm/registry.npmjs.org/canvas/2.11.2/lib/bindings.js
- /home/alex/.cache/deno/npm/registry.npmjs.org/canvas/2.11.2/lib/canvas.js
- /home/alex/.cache/deno/npm/registry.npmjs.org/canvas/2.11.2/index.js
- /home/alex/.cache/deno/npm/registry.npmjs.org/captcha-canvas/2.3.1/index.js
- /home/alex/.cache/deno/npm/registry.npmjs.org/captcha-canvas/2.3.1/index.js
    at Function.Module._resolveFilename (node:module:608:15)
    at Function.Module._load (node:module:486:27)
    at Module.require (node:module:674:19)
    at require (node:module:801:16)
    at Object.<anonymous> (file:///home/alex/.cache/deno/npm/registry.npmjs.org/canvas/2.11.2/lib/bindings.js:3:18)
    at Object.<anonymous> (file:///home/alex/.cache/deno/npm/registry.npmjs.org/canvas/2.11.2/lib/bindings.js:15:4)
    at Module._compile (node:module:736:34)
    at Object.Module._extensions..js (node:module:757:11)
    at Module.load (node:module:655:32)
    at Function.Module._load (node:module:523:13)

In both cases, the error seems to stem from the underlying canvas library.

Deno has two implementations of Skai, one based on wasm and another based on ffi.

Maybe there's an easy workaround that lets this work in Deno without having to swap out the canvas library. But if not, I think what makes the most sense is to create a v4 of this project that doesn't rely on any library at all, just a Canvas TypeScript interface. So that new Captcha(canvas: HTMLCanvasElement) would work, with the constructor taking the canvas element directly, or maybe even new Captcha(ctx: CanvasRenderingContext2D), which is even more flexible. The caller is then responsible for supplying the canvas implementation, and it would make the library work in all environments (even browser).