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 (Basic) ¶
package main import ( "os" "github.com/augustoroman/v8" "github.com/augustoroman/v8/v8console" "github.com/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") }
Output: <!DOCTYPE html>
Example (LocalServer) ¶
package main import ( "fmt" "net/http" "net/http/httptest" "os" "github.com/augustoroman/v8" "github.com/augustoroman/v8/v8console" "github.com/augustoroman/v8fetch" ) func main() { // If you are running a binary that is also an http server, you probably // have an http.Handler that is routing & managing you requests. That's // "local" in this example: local := http.NewServeMux() local.HandleFunc("/foo", func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "foo from the local server") }) // But you might also need to fetch results from somewhere else, like // S3 or a CDN or something. In this example, it's the remote server. remote := http.NewServeMux() remote.HandleFunc("/bar", func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "bar from afar") }) server := httptest.NewServer(remote) defer server.Close() ctx := v8.NewIsolate().NewContext() v8console.Config{"", os.Stdout, os.Stderr, false}.Inject(ctx) v8fetch.Inject(ctx, local) // local may be nil if there's no local server ctx.Eval(fmt.Sprintf(` fetch("/foo") .then(r => console.log('Local:', r.body, '('+r.status+')')); fetch("%s/bar") .then(r => console.log('Remote:', r.body, '('+r.status+')')); fetch("/no-such-page") .then(r => console.log('Local (missing):', r.body, '('+r.status+')')); `, server.URL), "mycode.js") }
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)