Skip to content

cardigann/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.v3.

package main

import (
  "fmt"

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

func main() {
  // create an ecmascript context
  ctx := duktape.New()
  // push fetch into the global scope
  fetch.Define(ctx)
  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 produce <!DOCTYPE html> into stdout.

About

Server side fetch polyfill for go-duktape

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 80.1%
  • JavaScript 19.9%