v8fetch

package module
v0.0.0-...-2dd140a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 2, 2016 License: MIT Imports: 11 Imported by: 1

README

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.

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

func Inject(ctx *v8.Context, server http.Handler) error

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

type AddHeaders struct {
	Server  http.Handler
	Headers http.Header
}

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)

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL