augustoroman / v8fetch

Package v8fetch implements a fetch polyfill for the v8 engine embedded in Go.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

v8fetch GoDoc

Fetch polyfill for v8 bindings in go based off of the duktape fetch bindings. The javascript code & bundling is taken almost entirely from the duktape bindings.

Basic Usage

package main

import (
  "os"

  "gopkg.in/augustoroman/v8"
  "gopkg.in/augustoroman/v8/v8console"
  "gopkg.in/augustoroman/v8fetch"
)

func main() {
  ctx := v8.NewIsolate().NewContext()
  v8console.Config{"", os.Stdout, os.Stderr, true}.Inject(ctx)
  v8fetch.Inject(ctx, nil)

  ctx.Eval(`
        fetch('https://golang.org/')
            .then(r => console.log(r.body.slice(0, 15)));
        `, "code.js")
}

This program will output <!DOCTYPE html> to stdout.

Like the duktape bindings, you can specify a local http instance with http.Handler interface as a the second parameter. It will be used for all local requests which url starts with /(single slash). See the examples for more detail.

About

Package v8fetch implements a fetch polyfill for the v8 engine embedded in Go.

License:MIT License


Languages

Language:Go 83.7%Language:JavaScript 16.3%