https://github.com/kuoruan/v8go-polyfills
Add polyfills to rogchap/v8go
https://github.com/kuoruan/v8go-polyfills
javascript polyfill v8 v8go
Last synced: 28 days ago
JSON representation
Add polyfills to rogchap/v8go
- Host: GitHub
- URL: https://github.com/kuoruan/v8go-polyfills
- Owner: kuoruan
- License: mit
- Created: 2021-03-05T09:13:40.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2023-11-22T20:53:03.000Z (over 1 year ago)
- Last Synced: 2025-03-28T03:41:24.997Z (about 2 months ago)
- Topics: javascript, polyfill, v8, v8go
- Language: Go
- Homepage:
- Size: 170 KB
- Stars: 64
- Watchers: 2
- Forks: 17
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Polyfills for [V8Go](https://github.com/rogchap/v8go)
## Install
```shell
go get -u go.kuoruan.net/v8go-polyfills
```> This module uses Golang [embed](https://golang.org/pkg/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
```go
package mainimport (
"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)
}
}
```