kuoruan / v8go-polyfills Goto Github PK
View Code? Open in Web Editor NEWAdd polyfills to rogchap/v8go
License: MIT License
Add polyfills to rogchap/v8go
License: MIT License
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?
While decoding a binary string, for example signatures in JWS access tokens for various identity providers, there are few issues:
StdEncoding is unable to handle unpadded base64 strings.
Fixing above issue using RawStdEncoding leads to other issues:
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)
}
Both javascript and golang byte arrays should be same.
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]
Does it support or plan to support FileSystem API from JS code?
Best.
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.
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?
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)
}
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.