Skip to content

olebedev/go-duktape-fetch

Repository files navigation

go-duktape-fetch wercker status

Fetch polyfill for go-duktape.

Usage

First of all install the package go get gopkg.in/olebedev/go-duktape-fetch.v2.

package main

import (
  "fmt"

  "gopkg.in/olebedev/go-duktape.v2"
  "gopkg.in/olebedev/go-duktape-fetch.v2"
)

func main() {
  // create an ecmascript context
  ctx := duktape.New()
  // push fetch into the global scope
  fetch.PushGlobal(ctx, nil)
  ch := make(chan string)
  ctx.PushGlobalGoFunction("cbk", func(c *duktape.Context) int {
    ch <- c.SafeToString(-1)
    return 0
  })
  // make a request
  ctx.PevalString(`
    fetch('http://ya.ru').then(function(resp) {
      return resp.text();
    }).then(function(body) {
      // release channel
      cbk(body.slice(0, 15));
    });
  `)
  // print head line of the response body
  fmt.Println(<-ch)
}

This program will prodice <!DOCTYPE html> into stdout.

Also you can define fetch with some other good possibilities. In particular you can specify 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 tests for more detail.

About

Server side fetch polyfill for go-duktape

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published