danthegoodman1 / Gildra

Mutli-tenant TLS terminating proxy for L7 traffic. Supports unlimited domains and certs with HTTP/1.1, 2, and 3. Manages ACME HTTP challenges magically. Sits in your infrastructure.
Other
3 stars 0 forks source link

JS bindings #29

Open danthegoodman1 opened 1 month ago

danthegoodman1 commented 1 month ago

A configured option for a route is to handle with JS (like cloudflare workers).

This could be used for adding simple tranformations to requests, enriching from an API, auth, or dropping it.

Goja seems to be massively faster than otto:

function factorial(n) {
    return n === 1 ? n : n * factorial(--n);
}

var i = 0;

while (i++ < 1e6) {
    factorial(10);
}

Ran twice each:

Otto in 10.80926s
Goja in 1.039494667s
Otto in 10.392454584s
Goja in 1.023689833s

When binding a simply multiply function from go and calling in JS:

 dangoodman: ~/code/test-go-js go run .
Goja multiply in 224.375µs result: 9
 dangoodman: ~/code/test-go-js go run .
Goja multiply in 309.5µs result: 9
 dangoodman: ~/code/test-go-js go run .
Goja multiply in 273.167µs result: 9
 dangoodman: ~/code/test-go-js go run .
Goja multiply in 305.25µs result: 9

 dangoodman: ~/code/test-go-js go run .
Otto multiply in 204.25µs result: 9
 dangoodman: ~/code/test-go-js go run .
Otto multiply in 165.125µs result: 9
 dangoodman: ~/code/test-go-js go run .
Otto multiply in 149.459µs result: 9
 dangoodman: ~/code/test-go-js go run .
Otto multiply in 180.5µs result: 9

This seems to just be creation time, running it 100 times:

Goja multiply in 445.25µs result: 495
panic: (anonymous): Line 1:10 Unexpected identifier (and 5 more errors)

goroutine 1 [running]:
main.ottoMultiply()
    /Users/dangoodman/code/test-go-js/multiply.go:40 +0x18c
main.main()
    /Users/dangoodman/code/test-go-js/main.go:12 +0x20
exit status 2

Otto can't even do a simple

for (let i = 0; i < 100; i++) {
    multiply(5, i)
}
danthegoodman1 commented 1 month ago

FWIW Goja 1,000x is still fast: Goja multiply in 611.375µs result: 4995 and I've seen it as low as 457.5µs

danthegoodman1 commented 1 month ago

Goja seems like the obvious choice here, it should add negligible time to requests

danthegoodman1 commented 1 month ago

Goja's docs aren't great but a lot of it can be interpreted from Otto's docs (e.g. vm.Set was effectively the same)

danthegoodman1 commented 5 days ago

See https://github.com/dop251/goja/discussions/586 comparing to rust as well for booting an isolate