FiloSottile / mkcert

A simple zero-config tool to make locally trusted development certificates with any names you'd like.
https://mkcert.dev
BSD 3-Clause "New" or "Revised" License
48.79k stars 2.52k forks source link

wasm binary? #365

Open andykais opened 3 years ago

andykais commented 3 years ago

Is there any chance of compiling this code to a wasm binary that can be imported into nodejs and deno projects? I want to use mkcert to generate localhost certificates for a local web app package, but it will require end users to install mkcert themselves, and run commands. If mkcert were usable as a wasm binary (literally just expose the command line interface as a function in js) then local web apps could ship with mkcert as a dependency and install the root CA and generate the localhost certs themselves as part of the initialization process.

An example of what initializing mkcert as a wasm module might look like:

const wasmCode = await Deno.readFile("mkcert.wasm");
const wasmModule = new WebAssembly.Module(wasmCode);
const wasmInstance = new WebAssembly.Instance(wasmModule);
const mkcert = wasmInstance.exports.main as CallableFunction;
await mkcert('-install');
await mkcert('localhost 127.0.0.1');
aral commented 1 year ago

I wonder if this would be possible to get working with the current state of WASI. Compiling to WASM, as I understand, is the easy part. Getting it to launch external processes (certutil) less so.

Update: until this is implemented, I don’t think there’s a way to make this work: https://github.com/WebAssembly/WASI/issues/414

andykais commented 1 year ago

thanks for looking into this, it does look like we have to wait on upstream changes before this is possible

augustobmoura commented 1 year ago

When trying to compile to WASM I'm getting the following errors:

# filippo.io/mkcert
./main.go:193:39: undefined: CertutilInstallHelp
./main.go:195:77: undefined: NSSBrowsers
./main.go:272:9: m.installPlatform undefined (type *mkcert has no field or method installPlatform)
./main.go:280:80: undefined: NSSBrowsers
./main.go:283:104: undefined: NSSBrowsers
./main.go:284:14: undefined: CertutilInstallHelp
./main.go:285:78: undefined: NSSBrowsers
./main.go:287:114: undefined: NSSBrowsers
./main.go:288:82: undefined: CertutilInstallHelp
./main.go:311:13: undefined: CertutilInstallHelp
./main.go:311:13: too many errors

Command run:

GOOS=js GOARCH=wasm go build -ldflags "-X main.Version=$(git describe --tags)" -o mkcert.wasm

Anyone knows what would be the problem? I'm not familiar with these type of Go compilation errors, the variables seems to be defined, but it throws undefined errors nonetheless, maybe a GOARCH=wasm limitation?