README
¶
v8fetch 
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.
Documentation
¶
Overview ¶
Package v8fetch implements a fetch polyfill for the v8 engine embedded in Go.
Basic usage looks like:
ctx := v8.NewIsolate().NewContext() v8fetch.Inject(ctx, my_http_handler) _, err := ctx.Eval(` fetch("http://www.example.com/").then(process_response); fetch("/local/mux").then(process_response); `, "mycode.js")
This code is based off of https://github.com/olebedev/go-duktape-fetch/ which implements the fetch polyfill for the duktape JS engine.
Example (LocalServer) ¶
Output: Local: foo from the local server (200) Remote: bar from afar (200) Local (missing): 404 page not found (404)
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Inject ¶
Inject inserts the fetch polyfill into ctx. The server parameter may be non- nil to support relative URLs that have no host (e.g. /foo/bar instead of https://host.com/foo/bar). If server is nil, then such relative URLs will always fail. The fetch polyfill only supports http and https schemes.
Types ¶
type AddHeaders ¶
AddHeaders wraps Server and adds all of the provided headers to any request processed by it. This can be used to copy cookies from a client request to all fetch calls during server-side rendering.
func (AddHeaders) ServeHTTP ¶
func (a AddHeaders) ServeHTTP(w http.ResponseWriter, r *http.Request)