package js import ( "github.com/dop251/goja" "github.com/ssgo/httpclient" "github.com/ssgo/u" ) type Http struct { client *httpclient.ClientPool } var defaultHttp = &Http{ client: httpclient.GetClient(0), } var h2cHttp = &Http{ client: httpclient.GetClientH2C(0), } func RequireHTTP() map[string]any { return map[string]any{ "get": defaultHttp.Get, "get2C": h2cHttp.Get, "head": defaultHttp.Head, "head2C": h2cHttp.Head, "post": defaultHttp.Post, "post2C": h2cHttp.Post, "put": defaultHttp.Put, "put2C": h2cHttp.Put, "delete": defaultHttp.Delete, "delete2C": h2cHttp.Delete, "do": defaultHttp.Do, "do2C": h2cHttp.Do, "upload": defaultHttp.Upload, "upload2C": h2cHttp.Upload, "download": defaultHttp.Download, "download2C": h2cHttp.Download, } } func makeResult(r *httpclient.Result, vm *goja.Runtime) goja.Value { if r.Error != nil { return vm.NewGoError(r.Error) } headers := map[string]string{} for k, v := range r.Response.Header { headers[k] = v[0] } return vm.ToValue(map[string]any{ "status": r.Response.Status, "statusCode": r.Response.StatusCode, "headers": headers, "data": r.Map(), "result": r.String(), }) } func (hc *Http) Get(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value { args := MakeArgs(&argsIn, vm) if err := args.Check(1); err != nil { return err } return makeResult(hc.client.Get(args.Str(0), args.Map2StrArr(1)...), vm) } func (hc *Http) Head(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value { args := MakeArgs(&argsIn, vm) if err := args.Check(1); err != nil { return err } return makeResult(hc.client.Head(args.Str(0), args.Map2StrArr(1)...), vm) } func (hc *Http) Post(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value { args := MakeArgs(&argsIn, vm) if err := args.Check(2); err != nil { return err } return makeResult(hc.client.Post(args.Str(0), args.Any(1), args.Map2StrArr(2)...), vm) } func (hc *Http) Put(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value { args := MakeArgs(&argsIn, vm) if err := args.Check(2); err != nil { return err } return makeResult(hc.client.Put(args.Str(0), args.Any(1), args.Map2StrArr(2)...), vm) } func (hc *Http) Delete(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value { args := MakeArgs(&argsIn, vm) if err := args.Check(2); err != nil { return err } return makeResult(hc.client.Delete(args.Str(0), args.Any(1), args.Map2StrArr(2)...), vm) } func (hc *Http) Do(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value { args := MakeArgs(&argsIn, vm) if err := args.Check(3); err != nil { return err } if len(argsIn.Arguments) == 3 { argsIn.Arguments = append(argsIn.Arguments, vm.ToValue(nil)) } var r *httpclient.Result if cb, ok := goja.AssertFunction(argsIn.Arguments[3]); ok { r = hc.client.ManualDo(args.Str(0), args.Str(1), args.Any(2), args.Map2StrArr(4)...) buf := make([]byte, 1024) for { n, err := r.Response.Body.Read(buf) if err != nil { break } _, _ = cb(argsIn.This, vm.ToValue(u.String(buf[0:n]))) } } else { r = hc.client.Do(args.Str(0), args.Str(1), args.Any(2), args.Map2StrArr(4)...) } return makeResult(r, vm) } func (hc *Http) Upload(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value { args := MakeArgs(&argsIn, vm) if err := args.Check(3); err != nil { return err } postData := map[string]string{} postFiles := map[string]any{} u.Convert(args.Any(1), &postData) u.Convert(args.Any(2), &postFiles) r, _ := hc.client.MPost(args.Str(0), postData, postFiles, args.Map2StrArr(3)...) return makeResult(r, vm) } func (hc *Http) Download(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value { args := MakeArgs(&argsIn, vm) if err := args.Check(3); err != nil { return err } var r *httpclient.Result if cb, ok := goja.AssertFunction(argsIn.Arguments[3]); ok { r, _ = hc.client.Download(args.Str(0), args.Str(1), func(start, end int64, ok bool, finished, total int64) { _, _ = cb(argsIn.This, vm.ToValue(finished), vm.ToValue(total)) }, args.Map2StrArr(3)...) } else { r, _ = hc.client.Download(args.Str(0), args.Str(1), nil, args.Map2StrArr(3)...) } return makeResult(r, vm) }