polyfills

package module
v0.0.0-...-e43970d Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2022 License: MIT Imports: 7 Imported by: 0

README

Polyfills for V8Go

Install

go get -u github.com/beyazit/v8go-polyfills

This module uses Golang embed, so requires Go version 1.16

Polyfill List

  • base64: atob and btoa

  • console: console.log

  • fetch: fetch

  • timers: setTimeout, clearTimeout, setInterval and clearInterval

  • url: URL and URLSearchParams

Usage

fetch polyfill
package main

import (
	"errors"
	"fmt"
	"time"

	"github.com/beyazit/v8go-polyfills/fetch"
	"rogchap.com/v8go"
)

func main() {
	iso, _ := v8go.NewIsolate()
	global, _ := v8go.NewObjectTemplate(iso)

	if err := fetch.InjectTo(iso, global); err != nil {
		panic(err)
	}

	ctx, _ := v8go.NewContext(iso, global)

	val, err := ctx.RunScript("fetch('https://www.example.com').then(res => res.text())", "fetch.js")
	if err != nil {
		panic(err)
	}

	proms, err := val.AsPromise()
	if err != nil {
		panic(err)
	}
	done := make(chan bool, 1)

	go func() {
		for proms.State() == v8go.Pending {
			continue
		}
		done <- true
	}()

	select {
	case <-time.After(time.Second * 10):
		panic(errors.New("request timeout"))
	case <-done:
		html := proms.Result().String()
		fmt.Println(html)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func InjectToContext

func InjectToContext(ctx *v8go.Context, opt ...interface{}) error

func InjectToGlobalObject

func InjectToGlobalObject(iso *v8go.Isolate, global *v8go.ObjectTemplate, opt ...interface{}) error

func Version

func Version() string

Types

This section is empty.

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

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