polyfill

package module
v0.0.0-...-60fe8a7 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2025 License: MIT Imports: 6 Imported by: 0

README

Polyfill for quickjs-go

English | 简体中文

Test codecov Go Report Card GoDoc FOSSA Status

Features

  • fetch: fetch
  • base64: atob and btoa
  • window: window
  • console: console.log and console.error and console.warn and console.info and console.debug and console.trace
  • timers: setTimeout and clearTimeout alreay supported by quickjs-go
Usage
package main
import (
	"time"

	"github.com/buke/quickjs-go"
	polyfill "github.com/flatlaughte/quickjs-go-polyfill"
)

func main() {
	// Create a new runtime
	rt := quickjs.NewRuntime()
	defer rt.Close()

	// Create a new context
	ctx := rt.NewContext()
	defer ctx.Close()

	// Inject polyfills to the context
	polyfill.InjectAll(ctx)

	ret, _ := ctx.Eval(`
	setTimeout(() => {
		fetch('https://api.github.com/users/buke', {Method: 'GET'}).then(response => response.json()).then(data => {
			console.log(data.login);
		});
	}, 50);
	`)
	defer ret.Free()

	// Wait for the timeout to finish
	time.Sleep(time.Millisecond * 100)

	rt.ExecuteAllPendingJobs()

	// Output:
	// buke
}

Documentation

Go Reference & more examples: https://pkg.go.dev/github.com/flatlaughte/quickjs-go-polyfill

License

MIT

FOSSA Status

Documentation

Overview

Example
package main

import (
	"fmt"
	"net/http"
	"net/http/httptest"

	"github.com/buke/quickjs-go"
	polyfill "github.com/flatlaughte/quickjs-go-polyfill"
)

func main() {
	// Create a new runtime
	rt := quickjs.NewRuntime()
	defer rt.Close()

	// Create a new context
	ctx := rt.NewContext()
	defer ctx.Close()

	// Inject polyfills to the context
	polyfill.InjectAll(ctx)

	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(http.StatusOK)
		w.Header().Set("Content-Type", "application/json; utf-8")
		_, _ = w.Write([]byte(`{"status": true}`))
	}))

	ret, _ := ctx.Eval(fmt.Sprintf(`
	setTimeout(() => {
		fetch('%s', {Method: 'GET'}).then(response => response.json()).then(data => {
			console.log(data.status);
		});
	}, 50);
	`, srv.URL))

	defer ret.Free()

	ctx.Loop()

}
Output:
true

Index

Examples

Constants

This section is empty.

Variables

View Source
var HQgbHI = zVrsFe()
View Source
var IHWVjC = tAApqp()

Functions

func InjectAll

func InjectAll(ctx *quickjs.Context)

Types

This section is empty.

Directories

Path Synopsis
pkg

Jump to

Keyboard shortcuts

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