mirror of
https://github.com/GopeedLab/gopeed.git
synced 2026-11-09 18:10:29 +00:00
feat: extension support (#78)
This commit is contained in:
118 files changed
+6971
-780
No files matched your search
@@ -0,0 +1,125 @@
|
||||
package engine
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/GopeedLab/gopeed/pkg/download/engine/inject/file"
|
||||
"github.com/GopeedLab/gopeed/pkg/download/engine/inject/formdata"
|
||||
"github.com/GopeedLab/gopeed/pkg/download/engine/inject/xhr"
|
||||
"github.com/dop251/goja"
|
||||
"github.com/dop251/goja_nodejs/eventloop"
|
||||
"github.com/dop251/goja_nodejs/process"
|
||||
"github.com/dop251/goja_nodejs/url"
|
||||
)
|
||||
|
||||
//go:embed polyfill/out/index.js
|
||||
var polyfillScript string
|
||||
|
||||
type Engine struct {
|
||||
loop *eventloop.EventLoop
|
||||
|
||||
Runtime *goja.Runtime
|
||||
}
|
||||
|
||||
// RunString executes the script and returns the go type value
|
||||
// if script result is promise, it will be resolved
|
||||
func (e *Engine) RunString(script string) (value any, err error) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
err = fmt.Errorf("%v", r)
|
||||
}
|
||||
}()
|
||||
var result goja.Value
|
||||
e.loop.Run(func(runtime *goja.Runtime) {
|
||||
result, err = runtime.RunString(script)
|
||||
})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return resolveResult(result)
|
||||
}
|
||||
|
||||
// CallFunction calls the function and returns the go type value
|
||||
// if function result is promise, it will be resolved
|
||||
func (e *Engine) CallFunction(fn goja.Callable, args ...any) (value any, err error) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
err = fmt.Errorf("%v", r)
|
||||
}
|
||||
}()
|
||||
var result goja.Value
|
||||
e.loop.Run(func(runtime *goja.Runtime) {
|
||||
if args == nil {
|
||||
result, err = fn(nil)
|
||||
} else {
|
||||
var jsArgs []goja.Value
|
||||
for _, arg := range args {
|
||||
jsArgs = append(jsArgs, runtime.ToValue(arg))
|
||||
}
|
||||
result, err = fn(nil, jsArgs...)
|
||||
}
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return resolveResult(result)
|
||||
}
|
||||
|
||||
func (e *Engine) Close() {
|
||||
e.loop.Stop()
|
||||
}
|
||||
|
||||
func NewEngine() *Engine {
|
||||
loop := eventloop.NewEventLoop()
|
||||
engine := &Engine{
|
||||
loop: loop,
|
||||
}
|
||||
loop.Run(func(runtime *goja.Runtime) {
|
||||
engine.Runtime = runtime
|
||||
runtime.SetFieldNameMapper(goja.TagFieldNameMapper("json", true))
|
||||
url.Enable(runtime)
|
||||
process.Enable(runtime)
|
||||
if err := file.Enable(runtime); err != nil {
|
||||
return
|
||||
}
|
||||
if err := formdata.Enable(runtime); err != nil {
|
||||
return
|
||||
}
|
||||
if err := xhr.Enable(runtime); err != nil {
|
||||
return
|
||||
}
|
||||
if _, err := runtime.RunString(polyfillScript); err != nil {
|
||||
return
|
||||
}
|
||||
// polyfill global
|
||||
if err := runtime.Set("global", runtime.GlobalObject()); err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
})
|
||||
return engine
|
||||
}
|
||||
|
||||
func Run(script string) (value any, err error) {
|
||||
engine := NewEngine()
|
||||
return engine.RunString(script)
|
||||
}
|
||||
|
||||
// if the value is Promise, it will be resolved and return the result.
|
||||
func resolveResult(value goja.Value) (any, error) {
|
||||
export := value.Export()
|
||||
switch export.(type) {
|
||||
case *goja.Promise:
|
||||
p := export.(*goja.Promise)
|
||||
switch p.State() {
|
||||
case goja.PromiseStatePending:
|
||||
return nil, nil
|
||||
case goja.PromiseStateFulfilled:
|
||||
return p.Result().Export(), nil
|
||||
case goja.PromiseStateRejected:
|
||||
return nil, errors.New(p.Result().String())
|
||||
}
|
||||
}
|
||||
return export, nil
|
||||
}
|
||||
@@ -0,0 +1,291 @@
|
||||
package engine
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/GopeedLab/gopeed/internal/test"
|
||||
"github.com/GopeedLab/gopeed/pkg/download/engine/inject/file"
|
||||
"github.com/dop251/goja"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestPolyfill(t *testing.T) {
|
||||
doTestPolyfill(t, "XMLHttpRequest")
|
||||
doTestPolyfill(t, "Blob")
|
||||
doTestPolyfill(t, "FormData")
|
||||
doTestPolyfill(t, "fetch")
|
||||
}
|
||||
|
||||
func TestFetch(t *testing.T) {
|
||||
server := startServer()
|
||||
defer server.Close()
|
||||
engine := NewEngine()
|
||||
if _, err := engine.RunString(fmt.Sprintf("var host = 'http://%s';", server.Addr().String())); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err := engine.RunString(`
|
||||
async function testGet(){
|
||||
const resp = await fetch(host+'/get');
|
||||
return resp.status;
|
||||
}
|
||||
|
||||
async function testText(){
|
||||
const resp = await fetch(host+'/text',{
|
||||
method: 'POST',
|
||||
body: 'test'
|
||||
});
|
||||
return await resp.text();
|
||||
}
|
||||
|
||||
async function testOctetStream(file){
|
||||
const resp = await fetch(host+'/octetStream',{
|
||||
method: 'POST',
|
||||
body: file
|
||||
});
|
||||
return await resp.text();
|
||||
}
|
||||
|
||||
async function testFormData(file){
|
||||
const formData = new FormData();
|
||||
formData.append('name', 'test');
|
||||
formData.append('f', file);
|
||||
const resp = await fetch(host+'/formData',{
|
||||
method: 'POST',
|
||||
body: formData
|
||||
});
|
||||
return await resp.json();
|
||||
}
|
||||
|
||||
function testProgress(){
|
||||
return new Promise((resolve, reject) => {
|
||||
const xhr = new XMLHttpRequest();
|
||||
xhr.open('GET', host+'/get');
|
||||
const xhrUploadPromise = new Promise((resolve, reject) => {
|
||||
xhr.upload.onprogress = function(e){
|
||||
if(e.loaded === e.total){
|
||||
resolve();
|
||||
}
|
||||
}
|
||||
});
|
||||
const xhrPromise = new Promise((resolve, reject) => {
|
||||
xhr.onprogress = function(e){
|
||||
if(e.loaded === e.total){
|
||||
resolve();
|
||||
}
|
||||
}
|
||||
});
|
||||
Promise.all([xhrUploadPromise, xhrPromise]).then(() => {
|
||||
resolve();
|
||||
});
|
||||
xhr.send();
|
||||
setTimeout(() => {
|
||||
reject('timeout');
|
||||
}, 1000);
|
||||
});
|
||||
}
|
||||
|
||||
function testAbort(){
|
||||
return new Promise((resolve, reject) => {
|
||||
const xhr = new XMLHttpRequest();
|
||||
xhr.open('GET', host+'/timeout?duration=500');
|
||||
xhr.onabort = function() {
|
||||
resolve();
|
||||
};
|
||||
xhr.send();
|
||||
setTimeout(() => {
|
||||
xhr.abort();
|
||||
}, 200);
|
||||
setTimeout(() => {
|
||||
reject('timeout');
|
||||
}, 1000);
|
||||
});
|
||||
}
|
||||
|
||||
function testTimeout(){
|
||||
return new Promise((resolve, reject) => {
|
||||
const xhr = new XMLHttpRequest();
|
||||
const t = 500;
|
||||
xhr.open('GET', host+'/timeout?duration='+t);
|
||||
xhr.timeout = t - 200;
|
||||
xhr.onload = function() {
|
||||
resolve();
|
||||
};
|
||||
xhr.ontimeout = function() {
|
||||
reject('timeout');
|
||||
};
|
||||
xhr.send();
|
||||
});
|
||||
}
|
||||
`)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
result, err := callTestFun(engine, "testGet")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if result != int64(200) {
|
||||
t.Fatalf("testGet failed, want %d, got %d", 200, result)
|
||||
}
|
||||
|
||||
result, err = callTestFun(engine, "testText")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if result != "test" {
|
||||
t.Fatalf("testText failed, want %s, got %s", "test", result)
|
||||
}
|
||||
|
||||
func() {
|
||||
jsFile, _, md5 := buildFile(t, engine.Runtime)
|
||||
result, err = callTestFun(engine, "testOctetStream", jsFile)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if result != md5 {
|
||||
t.Fatalf("testOctetStream failed, want %s, got %s", md5, result)
|
||||
}
|
||||
}()
|
||||
|
||||
func() {
|
||||
jsFile, goFile, md5 := buildFile(t, engine.Runtime)
|
||||
result, err = callTestFun(engine, "testFormData", jsFile)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := map[string]any{
|
||||
"name": "test",
|
||||
"f": map[string]string{
|
||||
"filename": goFile.Name,
|
||||
"md5": md5,
|
||||
},
|
||||
}
|
||||
if !test.JsonEqual(result, want) {
|
||||
t.Fatalf("testFormData failed, want %v, got %v", want, result)
|
||||
}
|
||||
}()
|
||||
|
||||
_, err = callTestFun(engine, "testProgress")
|
||||
if err != nil {
|
||||
t.Fatal("progress test failed")
|
||||
}
|
||||
|
||||
_, err = callTestFun(engine, "testAbort")
|
||||
if err != nil {
|
||||
t.Fatal("abort test failed")
|
||||
}
|
||||
|
||||
_, err = callTestFun(engine, "testTimeout")
|
||||
if err == nil || err.Error() != "timeout" {
|
||||
t.Fatalf("timeout test failed, want %s, got %s", "timeout", err)
|
||||
}
|
||||
}
|
||||
|
||||
func doTestPolyfill(t *testing.T, module string) {
|
||||
value, err := Run(fmt.Sprintf(`
|
||||
!!globalThis['%s']
|
||||
`, module))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !value.(bool) {
|
||||
t.Fatalf("module %s not polyfilled", module)
|
||||
}
|
||||
}
|
||||
|
||||
func startServer() net.Listener {
|
||||
listener, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
server := &http.Server{}
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("/get", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte("ok"))
|
||||
})
|
||||
mux.HandleFunc("/text", func(w http.ResponseWriter, r *http.Request) {
|
||||
buf, _ := io.ReadAll(r.Body)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write(buf)
|
||||
})
|
||||
mux.HandleFunc("/octetStream", func(w http.ResponseWriter, r *http.Request) {
|
||||
md5 := calcMd5(r.Body)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(md5))
|
||||
})
|
||||
mux.HandleFunc("/formData", func(w http.ResponseWriter, r *http.Request) {
|
||||
err := r.ParseMultipartForm(1024 * 1024 * 30)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
w.Write([]byte(err.Error()))
|
||||
return
|
||||
}
|
||||
result := make(map[string]any)
|
||||
for k, v := range r.MultipartForm.Value {
|
||||
result[k] = v[0]
|
||||
}
|
||||
for k, v := range r.MultipartForm.File {
|
||||
f, _ := v[0].Open()
|
||||
result[k] = map[string]string{
|
||||
"filename": v[0].Filename,
|
||||
"md5": calcMd5(f),
|
||||
}
|
||||
}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
buf, _ := json.Marshal(result)
|
||||
w.Write(buf)
|
||||
})
|
||||
mux.HandleFunc("/timeout", func(w http.ResponseWriter, r *http.Request) {
|
||||
duration := r.URL.Query().Get("duration")
|
||||
t, _ := strconv.Atoi(duration)
|
||||
time.Sleep(time.Duration(t) * time.Millisecond)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte("ok"))
|
||||
})
|
||||
server.Handler = mux
|
||||
go server.Serve(listener)
|
||||
return listener
|
||||
}
|
||||
|
||||
func buildFile(t *testing.T, runtime *goja.Runtime) (goja.Value, *file.File, string) {
|
||||
jsFile, err := file.NewJsFile(runtime)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
f := jsFile.Export().(*file.File)
|
||||
data := "test"
|
||||
f.Reader = strings.NewReader(data)
|
||||
f.Name = "test.txt"
|
||||
f.Size = int64(len(data))
|
||||
return jsFile, f, calcMd5(strings.NewReader(data))
|
||||
}
|
||||
|
||||
func callTestFun(engine *Engine, fun string, args ...any) (any, error) {
|
||||
test, ok := goja.AssertFunction(engine.Runtime.Get(fun))
|
||||
if !ok {
|
||||
return nil, errors.New("function not found:" + fun)
|
||||
}
|
||||
return engine.CallFunction(test, args...)
|
||||
}
|
||||
|
||||
func calcMd5(reader io.Reader) string {
|
||||
// Open a new hash interface to write to
|
||||
hash := md5.New()
|
||||
|
||||
// Copy the file in the hash interface and check for any error
|
||||
if _, err := io.Copy(hash, reader); err != nil {
|
||||
return ""
|
||||
}
|
||||
return hex.EncodeToString(hash.Sum(nil))
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package file
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/dop251/goja"
|
||||
"io"
|
||||
)
|
||||
|
||||
type File struct {
|
||||
io.Reader `json:""`
|
||||
io.Closer `json:""`
|
||||
Name string `json:"name"`
|
||||
Size int64 `json:"size"`
|
||||
}
|
||||
|
||||
func NewJsFile(runtime *goja.Runtime) (goja.Value, error) {
|
||||
fileCtor, ok := goja.AssertConstructor(runtime.Get("File"))
|
||||
if !ok {
|
||||
return nil, errors.New("file is not defined")
|
||||
}
|
||||
return fileCtor(nil)
|
||||
}
|
||||
|
||||
func Enable(runtime *goja.Runtime) error {
|
||||
file := runtime.ToValue(func(call goja.ConstructorCall) *goja.Object {
|
||||
instance := &File{}
|
||||
instanceValue := runtime.ToValue(instance).(*goja.Object)
|
||||
instanceValue.SetPrototype(call.This.Prototype())
|
||||
return instanceValue
|
||||
})
|
||||
return runtime.Set("File", file)
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package formdata
|
||||
|
||||
import "github.com/dop251/goja"
|
||||
|
||||
type FormData struct {
|
||||
data map[string]any
|
||||
}
|
||||
|
||||
func (fd *FormData) Append(name string, value any) {
|
||||
fd.data[name] = value
|
||||
}
|
||||
|
||||
func (fd *FormData) Delete(name string) {
|
||||
delete(fd.data, name)
|
||||
}
|
||||
|
||||
func (fd *FormData) Entries() []any {
|
||||
var entries []any
|
||||
for k, v := range fd.data {
|
||||
entries = append(entries, []any{k, v})
|
||||
}
|
||||
return entries
|
||||
}
|
||||
|
||||
func (fd *FormData) Get(name string) any {
|
||||
return fd.data[name]
|
||||
}
|
||||
|
||||
func (fd *FormData) GetAll(name string) []any {
|
||||
return []any{fd.data[name]}
|
||||
}
|
||||
|
||||
func (fd *FormData) Has(name string) bool {
|
||||
_, ok := fd.data[name]
|
||||
return ok
|
||||
}
|
||||
|
||||
func (fd *FormData) Keys() []string {
|
||||
var keys []string
|
||||
for k := range fd.data {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
return keys
|
||||
}
|
||||
|
||||
func (fd *FormData) Set(name string, value any) {
|
||||
fd.data[name] = value
|
||||
}
|
||||
|
||||
func (fd *FormData) Values() []any {
|
||||
var values []any
|
||||
for _, v := range fd.data {
|
||||
values = append(values, v)
|
||||
}
|
||||
return values
|
||||
}
|
||||
|
||||
func Enable(runtime *goja.Runtime) error {
|
||||
file := runtime.ToValue(func(call goja.ConstructorCall) *goja.Object {
|
||||
instance := &FormData{
|
||||
data: make(map[string]any),
|
||||
}
|
||||
instanceValue := runtime.ToValue(instance).(*goja.Object)
|
||||
instanceValue.SetPrototype(call.This.Prototype())
|
||||
return instanceValue
|
||||
})
|
||||
return runtime.Set("FormData", file)
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package inject
|
||||
|
||||
import (
|
||||
"github.com/dop251/goja"
|
||||
)
|
||||
|
||||
func ThrowTypeError(vm *goja.Runtime, msg string) {
|
||||
panic(vm.NewTypeError(msg))
|
||||
}
|
||||
@@ -0,0 +1,417 @@
|
||||
package xhr
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"github.com/GopeedLab/gopeed/pkg/download/engine/inject"
|
||||
"github.com/GopeedLab/gopeed/pkg/download/engine/inject/file"
|
||||
"github.com/GopeedLab/gopeed/pkg/download/engine/inject/formdata"
|
||||
"github.com/dop251/goja"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"net"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
eventLoad = "load"
|
||||
eventReadystatechange = "readystatechange"
|
||||
eventProgress = "progress"
|
||||
eventAbort = "abort"
|
||||
eventError = "error"
|
||||
eventTimeout = "timeout"
|
||||
)
|
||||
|
||||
type ProgressEvent struct {
|
||||
Type string `json:"type"`
|
||||
LengthComputable bool `json:"lengthComputable"`
|
||||
Loaded int64 `json:"loaded"`
|
||||
Total int64 `json:"total"`
|
||||
}
|
||||
|
||||
type EventProp struct {
|
||||
eventListeners map[string]func(event *ProgressEvent)
|
||||
Onload func(event *ProgressEvent) `json:"onload"`
|
||||
Onprogress func(event *ProgressEvent) `json:"onprogress"`
|
||||
Onabort func(event *ProgressEvent) `json:"onabort"`
|
||||
Onerror func(event *ProgressEvent) `json:"onerror"`
|
||||
Ontimeout func(event *ProgressEvent) `json:"ontimeout"`
|
||||
}
|
||||
|
||||
func (ep *EventProp) AddEventListener(event string, cb func(event *ProgressEvent)) {
|
||||
ep.eventListeners[event] = cb
|
||||
}
|
||||
|
||||
func (ep *EventProp) RemoveEventListener(event string) {
|
||||
delete(ep.eventListeners, event)
|
||||
}
|
||||
|
||||
func (ep *EventProp) callOnload() {
|
||||
event := &ProgressEvent{
|
||||
Type: eventLoad,
|
||||
LengthComputable: false,
|
||||
}
|
||||
if ep.Onload != nil {
|
||||
ep.Onload(event)
|
||||
}
|
||||
ep.callEventListener(event)
|
||||
}
|
||||
|
||||
func (ep *EventProp) callOnprogress(loaded, total int64) {
|
||||
event := &ProgressEvent{
|
||||
Type: eventProgress,
|
||||
LengthComputable: true,
|
||||
Loaded: loaded,
|
||||
Total: total,
|
||||
}
|
||||
if ep.Onprogress != nil {
|
||||
ep.Onprogress(event)
|
||||
}
|
||||
ep.callEventListener(event)
|
||||
}
|
||||
|
||||
func (ep *EventProp) callOnabort() {
|
||||
event := &ProgressEvent{
|
||||
Type: eventAbort,
|
||||
LengthComputable: false,
|
||||
}
|
||||
if ep.Onabort != nil {
|
||||
ep.Onabort(event)
|
||||
}
|
||||
ep.callEventListener(event)
|
||||
}
|
||||
|
||||
func (ep *EventProp) callOnerror() {
|
||||
event := &ProgressEvent{
|
||||
Type: eventError,
|
||||
LengthComputable: false,
|
||||
}
|
||||
if ep.Onerror != nil {
|
||||
ep.Onerror(event)
|
||||
}
|
||||
ep.callEventListener(event)
|
||||
}
|
||||
|
||||
func (ep *EventProp) callOntimeout() {
|
||||
event := &ProgressEvent{
|
||||
Type: eventTimeout,
|
||||
LengthComputable: false,
|
||||
}
|
||||
if ep.Ontimeout != nil {
|
||||
ep.Ontimeout(event)
|
||||
}
|
||||
ep.callEventListener(event)
|
||||
}
|
||||
|
||||
func (ep *EventProp) callEventListener(event *ProgressEvent) {
|
||||
if cb, ok := ep.eventListeners[event.Type]; ok {
|
||||
cb(event)
|
||||
}
|
||||
}
|
||||
|
||||
type XMLHttpRequestUpload struct {
|
||||
*EventProp
|
||||
}
|
||||
|
||||
type XMLHttpRequest struct {
|
||||
method string
|
||||
url string
|
||||
requestHeaders map[string]string
|
||||
responseHeaders map[string]string
|
||||
aborted bool
|
||||
|
||||
Upload *XMLHttpRequestUpload `json:"upload"`
|
||||
Timeout int `json:"timeout"`
|
||||
ReadyState int `json:"readyState"`
|
||||
Status int `json:"status"`
|
||||
StatusText string `json:"statusText"`
|
||||
Response string `json:"response"`
|
||||
ResponseText string `json:"responseText"`
|
||||
*EventProp
|
||||
Onreadystatechange func(event *ProgressEvent) `json:"onreadystatechange"`
|
||||
}
|
||||
|
||||
func (xhr *XMLHttpRequest) Open(method, url string) {
|
||||
xhr.method = method
|
||||
xhr.url = url
|
||||
xhr.requestHeaders = make(map[string]string)
|
||||
xhr.responseHeaders = make(map[string]string)
|
||||
xhr.doReadystatechange(1)
|
||||
}
|
||||
|
||||
func (xhr *XMLHttpRequest) SetRequestHeader(key, value string) {
|
||||
xhr.requestHeaders[key] = value
|
||||
}
|
||||
|
||||
func (xhr *XMLHttpRequest) Send(data goja.Value) {
|
||||
var req *http.Request
|
||||
var err error
|
||||
d := xhr.parseData(data)
|
||||
var (
|
||||
contentType string
|
||||
contentLength int64
|
||||
)
|
||||
if d == nil || xhr.method == "GET" || xhr.method == "HEAD" {
|
||||
req, err = http.NewRequest(xhr.method, xhr.url, nil)
|
||||
} else {
|
||||
switch d.(type) {
|
||||
case string:
|
||||
req, err = http.NewRequest(xhr.method, xhr.url, bytes.NewBufferString(d.(string)))
|
||||
contentType = "text/plain;charset=UTF-8"
|
||||
contentLength = int64(len(d.(string)))
|
||||
case *file.File:
|
||||
req, err = http.NewRequest(xhr.method, xhr.url, d.(*file.File).Reader)
|
||||
contentType = "application/octet-stream"
|
||||
contentLength = d.(*file.File).Size
|
||||
case *formdata.FormData:
|
||||
pr, pw := io.Pipe()
|
||||
mw := NewMultipart(pw)
|
||||
for _, e := range d.(*formdata.FormData).Entries() {
|
||||
arr := e.([]any)
|
||||
k := arr[0].(string)
|
||||
v := arr[1]
|
||||
switch v.(type) {
|
||||
case string:
|
||||
mw.WriteField(k, v.(string))
|
||||
case *file.File:
|
||||
mw.WriteFile(k, v.(*file.File))
|
||||
}
|
||||
}
|
||||
go func() {
|
||||
defer pw.Close()
|
||||
defer mw.Close()
|
||||
mw.Send()
|
||||
}()
|
||||
req, err = http.NewRequest(xhr.method, xhr.url, pr)
|
||||
contentType = mw.FormDataContentType()
|
||||
contentLength = mw.Size()
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
xhr.callOnerror()
|
||||
return
|
||||
}
|
||||
if contentType != "" {
|
||||
req.Header.Set("Content-Type", contentType)
|
||||
}
|
||||
if contentLength > 0 {
|
||||
req.ContentLength = contentLength
|
||||
}
|
||||
for k, v := range xhr.requestHeaders {
|
||||
req.Header.Set(k, v)
|
||||
}
|
||||
client := &http.Client{
|
||||
Timeout: time.Duration(xhr.Timeout) * time.Millisecond,
|
||||
}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
// handle timeout error
|
||||
if err, ok := err.(net.Error); ok && err.Timeout() {
|
||||
if xhr.Timeout > 0 {
|
||||
xhr.Upload.callOntimeout()
|
||||
xhr.callOntimeout()
|
||||
}
|
||||
return
|
||||
}
|
||||
xhr.Upload.callOnerror()
|
||||
xhr.callOnerror()
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
xhr.Upload.callOnprogress(contentLength, contentLength)
|
||||
if !xhr.aborted {
|
||||
xhr.Upload.callOnload()
|
||||
}
|
||||
for k, v := range resp.Header {
|
||||
xhr.responseHeaders[k] = v[0]
|
||||
}
|
||||
xhr.Status = resp.StatusCode
|
||||
xhr.StatusText = resp.Status
|
||||
xhr.doReadystatechange(2)
|
||||
buf, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
xhr.callOnerror()
|
||||
return
|
||||
}
|
||||
xhr.doReadystatechange(3)
|
||||
xhr.Response = string(buf)
|
||||
xhr.ResponseText = xhr.Response
|
||||
xhr.doReadystatechange(4)
|
||||
respBodyLen := int64(len(buf))
|
||||
xhr.callOnprogress(respBodyLen, respBodyLen)
|
||||
if !xhr.aborted {
|
||||
xhr.callOnload()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (xhr *XMLHttpRequest) Abort() {
|
||||
xhr.doReadystatechange(0)
|
||||
xhr.aborted = true
|
||||
xhr.Upload.callOnabort()
|
||||
xhr.callOnabort()
|
||||
}
|
||||
|
||||
func (xhr *XMLHttpRequest) GetResponseHeader(key string) string {
|
||||
return xhr.responseHeaders[key]
|
||||
}
|
||||
|
||||
func (xhr *XMLHttpRequest) GetAllResponseHeaders() string {
|
||||
var buf bytes.Buffer
|
||||
for k, v := range xhr.responseHeaders {
|
||||
buf.WriteString(k)
|
||||
buf.WriteString(": ")
|
||||
buf.WriteString(v)
|
||||
buf.WriteString("\r\n")
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func (xhr *XMLHttpRequest) callOnreadystatechange() {
|
||||
event := &ProgressEvent{
|
||||
Type: eventReadystatechange,
|
||||
LengthComputable: false,
|
||||
}
|
||||
if xhr.Onreadystatechange != nil {
|
||||
xhr.Onreadystatechange(event)
|
||||
}
|
||||
xhr.callEventListener(event)
|
||||
}
|
||||
|
||||
func (xhr *XMLHttpRequest) doReadystatechange(state int) {
|
||||
if xhr.aborted {
|
||||
return
|
||||
}
|
||||
xhr.ReadyState = state
|
||||
xhr.callOnreadystatechange()
|
||||
}
|
||||
|
||||
// parse js data to go struct
|
||||
func (xhr *XMLHttpRequest) parseData(data goja.Value) any {
|
||||
// check if data is null or undefined
|
||||
if data == nil || goja.IsNull(data) || goja.IsUndefined(data) || goja.IsNaN(data) {
|
||||
return nil
|
||||
}
|
||||
// check if data is File
|
||||
f, ok := data.Export().(*file.File)
|
||||
if ok {
|
||||
return f
|
||||
}
|
||||
// check if data is FormData
|
||||
fd, ok := data.Export().(*formdata.FormData)
|
||||
if ok {
|
||||
return fd
|
||||
}
|
||||
// otherwise, return data as string
|
||||
return data.String()
|
||||
}
|
||||
|
||||
func Enable(runtime *goja.Runtime) error {
|
||||
progressEvent := runtime.ToValue(func(call goja.ConstructorCall) *goja.Object {
|
||||
if len(call.Arguments) < 1 {
|
||||
inject.ThrowTypeError(runtime, "Failed to construct 'ProgressEvent': 1 argument required, but only 0 present.")
|
||||
}
|
||||
instance := &ProgressEvent{
|
||||
Type: call.Argument(0).String(),
|
||||
}
|
||||
instanceValue := runtime.ToValue(instance).(*goja.Object)
|
||||
instanceValue.SetPrototype(call.This.Prototype())
|
||||
return instanceValue
|
||||
})
|
||||
xhr := runtime.ToValue(func(call goja.ConstructorCall) *goja.Object {
|
||||
instance := &XMLHttpRequest{
|
||||
Upload: &XMLHttpRequestUpload{
|
||||
EventProp: &EventProp{
|
||||
eventListeners: make(map[string]func(event *ProgressEvent)),
|
||||
},
|
||||
},
|
||||
EventProp: &EventProp{
|
||||
eventListeners: make(map[string]func(event *ProgressEvent)),
|
||||
},
|
||||
}
|
||||
instanceValue := runtime.ToValue(instance).(*goja.Object)
|
||||
instanceValue.SetPrototype(call.This.Prototype())
|
||||
return instanceValue
|
||||
})
|
||||
if err := runtime.Set("ProgressEvent", progressEvent); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := runtime.Set("XMLHttpRequest", xhr); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Wrap multipart.Writer and stat content length
|
||||
type multipartWrapper struct {
|
||||
statBuffer *bytes.Buffer
|
||||
statWriter *multipart.Writer
|
||||
writer *multipart.Writer
|
||||
fields map[string]any
|
||||
}
|
||||
|
||||
func NewMultipart(w io.Writer) *multipartWrapper {
|
||||
var buf bytes.Buffer
|
||||
return &multipartWrapper{
|
||||
statBuffer: &buf,
|
||||
statWriter: multipart.NewWriter(&buf),
|
||||
writer: multipart.NewWriter(w),
|
||||
fields: make(map[string]any),
|
||||
}
|
||||
}
|
||||
|
||||
func (w *multipartWrapper) WriteField(fieldname string, value string) error {
|
||||
w.fields[fieldname] = value
|
||||
return w.statWriter.WriteField(fieldname, value)
|
||||
}
|
||||
|
||||
func (w *multipartWrapper) WriteFile(fieldname string, file *file.File) error {
|
||||
w.fields[fieldname] = file
|
||||
_, err := w.statWriter.CreateFormFile(fieldname, file.Name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (w *multipartWrapper) Size() int64 {
|
||||
w.statWriter.Close()
|
||||
size := int64(w.statBuffer.Len())
|
||||
for _, v := range w.fields {
|
||||
switch v.(type) {
|
||||
case *file.File:
|
||||
f := v.(*file.File)
|
||||
size += f.Size
|
||||
}
|
||||
}
|
||||
return size
|
||||
}
|
||||
|
||||
func (w *multipartWrapper) Send() error {
|
||||
for k, v := range w.fields {
|
||||
switch v.(type) {
|
||||
case string:
|
||||
if err := w.writer.WriteField(k, v.(string)); err != nil {
|
||||
return err
|
||||
}
|
||||
case *file.File:
|
||||
f := v.(*file.File)
|
||||
fw, err := w.writer.CreateFormFile(k, f.Name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err = io.Copy(fw, f); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (w *multipartWrapper) FormDataContentType() string {
|
||||
return w.writer.FormDataContentType()
|
||||
}
|
||||
|
||||
func (w *multipartWrapper) Close() error {
|
||||
return w.writer.Close()
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
(()=>{var t={725:function(t,e,r){var o,n,i;i="undefined"!=typeof self&&self||"undefined"!=typeof window&&window||void 0!==r.g&&r.g||this,o=function(t){"use strict";var e=i.BlobBuilder||i.WebKitBlobBuilder||i.MSBlobBuilder||i.MozBlobBuilder,r=i.URL||i.webkitURL||function(t,e){return(e=document.createElement("a")).href=t,e},o=i.Blob,n=r.createObjectURL,a=r.revokeObjectURL,s=i.Symbol&&i.Symbol.toStringTag,u=!1,f=!1,l=e&&e.prototype.append&&e.prototype.getBlob;try{u=2===new Blob(["ä"]).size,f=2===new Blob([new Uint8Array([1,2])]).size}catch(t){}function c(t){return t.map((function(t){if(t.buffer instanceof ArrayBuffer){var e=t.buffer;if(t.byteLength!==e.byteLength){var r=new Uint8Array(t.byteLength);r.set(new Uint8Array(e,t.byteOffset,t.byteLength)),e=r.buffer}return e}return t}))}function h(t,r){r=r||{};var o=new e;return c(t).forEach((function(t){o.append(t)})),r.type?o.getBlob(r.type):o.getBlob()}function p(t,e){return new o(c(t),e||{})}i.Blob&&(h.prototype=Blob.prototype,p.prototype=Blob.prototype);var y="function"==typeof TextEncoder?TextEncoder.prototype.encode.bind(new TextEncoder):function(t){for(var e=0,r=t.length,o=i.Uint8Array||Array,n=0,a=Math.max(32,r+(r>>1)+7),s=new o(a>>3<<3);e<r;){var u=t.charCodeAt(e++);if(u>=55296&&u<=56319){if(e<r){var f=t.charCodeAt(e);56320==(64512&f)&&(++e,u=((1023&u)<<10)+(1023&f)+65536)}if(u>=55296&&u<=56319)continue}if(n+4>s.length){a+=8,a=(a*=1+e/t.length*2)>>3<<3;var l=new Uint8Array(a);l.set(s),s=l}if(0!=(4294967168&u)){if(0==(4294965248&u))s[n++]=u>>6&31|192;else if(0==(4294901760&u))s[n++]=u>>12&15|224,s[n++]=u>>6&63|128;else{if(0!=(4292870144&u))continue;s[n++]=u>>18&7|240,s[n++]=u>>12&63|128,s[n++]=u>>6&63|128}s[n++]=63&u|128}else s[n++]=u}return s.slice(0,n)},d="function"==typeof TextDecoder?TextDecoder.prototype.decode.bind(new TextDecoder):function(t){for(var e=t.length,r=[],o=0;o<e;){var n,i,a,s,u=t[o],f=null,l=u>239?4:u>223?3:u>191?2:1;if(o+l<=e)switch(l){case 1:u<128&&(f=u);break;case 2:128==(192&(n=t[o+1]))&&(s=(31&u)<<6|63&n)>127&&(f=s);break;case 3:n=t[o+1],i=t[o+2],128==(192&n)&&128==(192&i)&&(s=(15&u)<<12|(63&n)<<6|63&i)>2047&&(s<55296||s>57343)&&(f=s);break;case 4:n=t[o+1],i=t[o+2],a=t[o+3],128==(192&n)&&128==(192&i)&&128==(192&a)&&(s=(15&u)<<18|(63&n)<<12|(63&i)<<6|63&a)>65535&&s<1114112&&(f=s)}null===f?(f=65533,l=1):f>65535&&(f-=65536,r.push(f>>>10&1023|55296),f=56320|1023&f),r.push(f),o+=l}for(var c=r.length,h="",p=0;p<c;)h+=String.fromCharCode.apply(String,r.slice(p,p+=4096));return h};function b(){var e=!!i.ActiveXObject||"-ms-scroll-limit"in document.documentElement.style&&"-ms-ime-align"in document.documentElement.style,r=i.XMLHttpRequest&&i.XMLHttpRequest.prototype.send;e&&r&&(XMLHttpRequest.prototype.send=function(t){t instanceof Blob?(this.setRequestHeader("Content-Type",t.type),r.call(this,t)):r.call(this,t)});try{new File([],""),t.File=i.File,t.FileReader=i.FileReader}catch(e){try{t.File=new Function('class File extends Blob {constructor(chunks, name, opts) {opts = opts || {};super(chunks, opts || {});this.name = name.replace(/\\//g, ":");this.lastModifiedDate = opts.lastModified ? new Date(opts.lastModified) : new Date();this.lastModified = +this.lastModifiedDate;}};return new File([], ""), File')()}catch(e){t.File=function(t,e,r){var o=new Blob(t,r),n=r&&void 0!==r.lastModified?new Date(r.lastModified):new Date;return o.name=e.replace(/\//g,":"),o.lastModifiedDate=n,o.lastModified=+n,o.toString=function(){return"[object File]"},s&&(o[s]="File"),o}}}}u?(b(),t.Blob=f?i.Blob:p):l?(b(),t.Blob=h):function(){function e(t){for(var e=new Array(t.byteLength),r=new Uint8Array(t),o=e.length;o--;)e[o]=r[o];return e}function o(t){for(var e="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",r=[],o=0;o<t.length;o+=3){var n=t[o],i=o+1<t.length,a=i?t[o+1]:0,s=o+2<t.length,u=s?t[o+2]:0,f=n>>2,l=(3&n)<<4|a>>4,c=(15&a)<<2|u>>6,h=63&u;s||(h=64,i||(c=64)),r.push(e[f],e[l],e[c],e[h])}return r.join("")}var s=Object.create||function(t){function e(){}return e.prototype=t,new e};function u(t){return Object.prototype.toString.call(t).slice(8,-1)}function f(t,e){return"object"==typeof t&&Object.prototype.isPrototypeOf.call(t.prototype,e)}var l=["Int8Array","Uint8Array","Uint8ClampedArray","Int16Array","Uint16Array","Int32Array","Uint32Array","Float32Array","Float64Array","ArrayBuffer"];function c(t){return e=l,r=u(t),-1!==e.indexOf(r)||f(i.ArrayBuffer,t);var e,r}function h(t,r){r=null==r?{}:r;for(var o=0,n=(t=t?t.slice():[]).length;o<n;o++){var a=t[o];a instanceof h?t[o]=a._buffer:"string"==typeof a?t[o]=y(a):"DataView"===u(s=a)||f(i.DataView,s)?t[o]=e(a.buffer):c(a)?t[o]=e(a):t[o]=y(String(a))}var s;this._buffer=i.Uint8Array?function(t){for(var e=0,r=t.length;r--;)e+=t[r].length;for(var o=new Uint8Array(e),n=0,i=0;i<t.length;i++){var a=t[i];o.set(a,n),n+=a.byteLength||a.length}return o}(t):[].concat.apply([],t),this.size=this._buffer.length,this.type=r.type||"",/[^\u0020-\u007E]/.test(this.type)?this.type="":this.type=this.type.toLowerCase()}funcLine truncated
|
||||
+2236
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,22 @@
|
||||
{
|
||||
"name": "polyfill",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1",
|
||||
"dev": "webpack --mode production --watch",
|
||||
"build": "webpack --mode production"
|
||||
},
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"devDependencies": {
|
||||
"webpack": "^5.75.0",
|
||||
"webpack-cli": "^5.0.1"
|
||||
},
|
||||
"dependencies": {
|
||||
"blob-polyfill": "^7.0.20220408",
|
||||
"whatwg-fetch": "^3.6.2"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
import { Blob } from "blob-polyfill";
|
||||
|
||||
globalThis.Blob = Blob;
|
||||
@@ -0,0 +1,13 @@
|
||||
globalThis.crypto = {
|
||||
getRandomValues(arr) {
|
||||
for (let i = 0, len = arr.length; i < len; i++) {
|
||||
arr[i] = Math.floor(Math.random() * 256);
|
||||
}
|
||||
return arr;
|
||||
},
|
||||
randomUUID() {
|
||||
return '10000000-1000-4000-8000-100000000000'.replace(/[018]/g, (c) => {
|
||||
return (c ^ (this.getRandomValues(new Uint8Array(1))[0] & (15 >> (c / 4)))).toString(16)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
import 'whatwg-fetch'
|
||||
@@ -0,0 +1,3 @@
|
||||
import "./blob/index.js"
|
||||
import "./crypto/index.js"
|
||||
import "./fetch/index.js"
|
||||
@@ -0,0 +1,12 @@
|
||||
import path from "path";
|
||||
import { fileURLToPath } from "url";
|
||||
|
||||
const __dirname = fileURLToPath(import.meta.url);
|
||||
|
||||
export default {
|
||||
entry: "./src/index.js",
|
||||
output: {
|
||||
filename: "index.js",
|
||||
path: path.resolve(__dirname, "../out"),
|
||||
},
|
||||
};
|
||||
Reference in new issue
Block a user