Git Product home page Git Product logo

v8go-polyfills's Introduction

Polyfills for V8Go

Install

go get -u go.kuoruan.net/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"

	"go.kuoruan.net/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)
	}
}

v8go-polyfills's People

Contributors

danielgatis avatar kuoruan avatar notnotquinn avatar xingwangt avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

v8go-polyfills's Issues

TypeError: res.arrayBuffer is not a function

package main

import (
	"errors"
	"fmt"
	"log"
	"time"

	"go.kuoruan.net/v8go-polyfills/fetch"
	"rogchap.com/v8go"
)

func main() {
	iso := v8go.NewIsolate()
	global := v8go.NewObjectTemplate(iso)
	if err := fetch.InjectTo(iso, global); err != nil {
		log.Fatal(err)
		return
	}

	ctx := v8go.NewContext(iso, global)
	val, err := ctx.RunScript(`fetch("https://github.com/mengqiy/wasm-go-poc/raw/main/formatjson/formatjson.wasm").then(res => res.arrayBuffer())`, "fetch.js")
	fmt.Println(val, 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)
	}
}

May I ask a question?how to setprototype?

We can consider atob or btoa as browser functions, which have no inheritance relationship with the prototype chain, such as browser functions Crypto and crypto
In the browser
crypto__ proto__==Crypto. prototype
How should a template be defined in v8go to make this condition hold?
Crypto

ReactJS SSR Example?

Hi @kuoruan ,

I would really love to see an example/template on how I can use this lib with ReactJS using SSR serving.

I am trying to import my compile ReactJS code and injecting it into ctx.RunScript function.
However, I get this error:

panic serving [::1]:52970: ReferenceError: require is not defined

Can you please advise?

Will this lib support LazyLoad feature as well?

base64: atob returns incorrect response for binary strings.

Issue:

While decoding a binary string, for example signatures in JWS access tokens for various identity providers, there are few issues:

  1. StdEncoding is unable to handle unpadded base64 strings.

  2. Fixing above issue using RawStdEncoding leads to other issues:

    • bytes returned to javascript world is not same as what was decoded in Go.
    • if a string has null character, output gets terminated at that string.

Sample Code:

package main

import (
	stdBase64 "encoding/base64"
	"fmt"
	"os"
	"go.kuoruan.net/v8go-polyfills/base64"
	"go.kuoruan.net/v8go-polyfills/console"
	"rogchap.com/v8go"
)

const script = `
let result = atob('CN7ySxWjjWNpbYPB1n/TBR8avujZS2cHdXRR5ZM7Fi6QBjlVzBqPu0CI9pQXcW9fvbMXdqU+57XY/QdGozJT19+gbQDM0ZQVzjwqtpyLorcPHjMqum+7dHD6XF5cXo3NZlKGsxcnvSVyClBDU5M1dUCe8bB9yV0wVM6ge+0WAmTX2GYbncilTjDw0bSJI1Z+71NT8UQCfmimKhVxJiKrnkaTrTw2Ma/1I2w4Dny3cRlFtCtob9cvNOeeIm8HtQoi/7HXoE0uFr1C39OL2hCC1TJsxX94djtNFqd9aUOPYrwT+zErSokSvbNYS5WpEjEpRJze9+TCV9NLmqCnARK4Bw'); 
let e = result.split("").map((c) => c.charCodeAt(0));
console.log("console value: ", e)
`

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

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

	ctx := v8go.NewContext(iso, global)
	if err := console.InjectTo(ctx, console.WithOutput(os.Stdout)); err != nil {
		panic(err)
	}

	_, err := ctx.RunScript(script, "fetch.js")
	if err != nil {
		panic(err)
	}
	//GO bytes
	byts, err := stdBase64.RawStdEncoding.DecodeString("CN7ySxWjjWNpbYPB1n/TBR8avujZS2cHdXRR5ZM7Fi6QBjlVzBqPu0CI9pQXcW9fvbMXdqU+57XY/QdGozJT19+gbQDM0ZQVzjwqtpyLorcPHjMqum+7dHD6XF5cXo3NZlKGsxcnvSVyClBDU5M1dUCe8bB9yV0wVM6ge+0WAmTX2GYbncilTjDw0bSJI1Z+71NT8UQCfmimKhVxJiKrnkaTrTw2Ma/1I2w4Dny3cRlFtCtob9cvNOeeIm8HtQoi/7HXoE0uFr1C39OL2hCC1TJsxX94djtNFqd9aUOPYrwT+zErSokSvbNYS5WpEjEpRJze9+TCV9NLmqCnARK4Bw")
	fmt.Println("go value: : ", byts)
}

Expected:

Both javascript and golang byte arrays should be same.

Actual:

console val:  8,65533,65533,75,21,65533,65533,99,105,109,65533,65533,65533,127,65533,5,31,26,65533,65533,65533,75,103,7,117,116,81,65533,59,22,46,65533,6,57,85,65533,26,65533,65533,64,65533,65533,65533,23,113,111,95,65533,65533,23,118,65533,62,65533,65533,65533,7,70,65533,50,83,65533,2016,109

go val:  [8 222 242 75 21 163 141 99 105 109 131 193 214 127 211 5 31 26 190 232 217 75 103 7 117 116 81 229 147 59 22 46 144 6 57 85 204 26 143 187 64 136 246 148 23 113 111 95 189 179 23 118 165 62 231 181 216 253 7 70 163 50 83 215 223 160 109 0 204 209 148 21 206 60 42 182 156 139 162 183 15 30 51 42 186 111 187 116 112 250 92 94 92 94 141 205 102 82 134 179 23 39 189 37 114 10 80 67 83 147 53 117 64 158 241 176 125 201 93 48 84 206 160 123 237 22 2 100 215 216 102 27 157 200 165 78 48 240 209 180 137 35 86 126 239 83 83 241 68 2 126 104 166 42 21 113 38 34 171 158 70 147 173 60 54 49 175 245 35 108 56 14 124 183 113 25 69 180 43 104 111 215 47 52 231 158 34 111 7 181 10 34 255 177 215 160 77 46 22 189 66 223 211 139 218 16 130 213 50 108 197 127 120 118 59 77 22 167 125 105 67 143 98 188 19 251 49 43 74 137 18 189 179 88 75 149 169 18 49 41 68 156 222 247 228 194 87 211 75 154 160 167 1 18 184 7]

Fetch polyfill not returning `status` or `headers`

Hi there, the polyfills work really well. Was just trying out the fetch polyfill and seems like the response gets returned properly but the response doesn't contain status and headers property.

Here's an example script:

await function makeReq() {
  const URL = `http://mockbin.com/request?foo=bar&foo=baz`;
  const responseBody = await fetch(URL);
  const response = JSON.parse(responseBody);
  
  return {
              res: responseBody,
              resJson: response,
              status: responseBody.status
  };
}

In the above, both res and resJson resolve to the response body but the status field shows up as empty. Similar result if I try responseBody.headers.

Am I missing something in the above code?

Example Fails

None of these polyfills currently work. Running the example (i.e. go run test.go) with the code from the README fails with:

/go/pkg/mod/go.kuoruan.net/[email protected]/internal/version.go:26:2: package embed is not in GOROOT (/usr/local/go/src/embed)

It looks like the module is expecting a build-time version number.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.