Skip to content

Commit

Permalink
temp
Browse files Browse the repository at this point in the history
  • Loading branch information
monkeyWie committed Feb 10, 2023
1 parent 9a84dde commit 91046d5
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 35 deletions.
22 changes: 13 additions & 9 deletions pkg/download/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ func (d *Downloader) Resolve(req *base.Request) (rr *ResolveResult, err error) {
return
}

func (d *Downloader) DirectCreate(req *base.Request, opts *base.Options) (taskId string, err error) {
func (d *Downloader) CreateDirect(req *base.Request, opts *base.Options) (taskId string, err error) {
rr, err := d.Resolve(req)
if err != nil {
return
Expand Down Expand Up @@ -524,7 +524,7 @@ func (d *Downloader) restoreFetcher(task *Task) error {
return nil
}

func (d *Downloader) InstallExtensionByUrl(url string) error {
func (d *Downloader) InstallExtensionByGit(url string) error {
ext, err := d.fetchExtensionInfoByGit(url)
if err != nil {
return err
Expand All @@ -537,12 +537,16 @@ func (d *Downloader) InstallExtensionByUrl(url string) error {
return nil
}

func (d *Downloader) InstallExtensionByForFolder(path string) error {
d.fetchExtensionInfoByPath(path)
}
func (d *Downloader) InstallExtensionByFolder(path string) error {
ext, err := d.fetchExtensionInfoByFolder(path)
if err != nil {
return err
}

func (d *Downloader) installExtension(path string) error {
d.fetchExtensionInfoByPath(path)
if err := util.CopyDir(path, filepath.Join(d.cfg.StorageDir, "extensions", ext.Manifest.Name)); err != nil {
return err
}
return nil
}

func (d *Downloader) fetchExtensionInfoByGit(url string) (ext *extension.Extension, err error) {
Expand All @@ -562,15 +566,15 @@ func (d *Downloader) fetchExtensionInfoByGit(url string) (ext *extension.Extensi
// cut project name
_, projectDirName := filepath.Split(url)
projectDirName = strings.TrimSuffix(projectDirName, ".git")
ext, err = d.fetchExtensionInfoByPath(filepath.Join(extTempDir, projectDirName))
ext, err = d.fetchExtensionInfoByFolder(filepath.Join(extTempDir, projectDirName))
if err != nil {
return
}
ext.URL = url
return
}

func (d *Downloader) fetchExtensionInfoByPath(extPath string) (ext *extension.Extension, err error) {
func (d *Downloader) fetchExtensionInfoByFolder(extPath string) (ext *extension.Extension, err error) {
// resolve extension manifest
manifestTempPath := filepath.Join(extPath, "manifest.json")
if _, err = os.Stat(manifestTempPath); os.IsNotExist(err) {
Expand Down
45 changes: 22 additions & 23 deletions pkg/download/extension/engine.go
Original file line number Diff line number Diff line change
@@ -1,37 +1,36 @@
package extension

import (
"fmt"
"github.com/dop251/goja"
"github.com/dop251/goja_nodejs/eventloop"
"github.com/dop251/goja_nodejs/url"
"github.com/monkeyWie/gopeed/pkg/download/extension/polyfill/fetch"
"os"
)

func main() {
file, err := os.ReadFile("D:\\code\\study\\node\\gopeed-extension-test\\dist\\index.js")
if err != nil {
panic(err)
}
SCRIPT := string(file)
type Engine struct {
loop *eventloop.EventLoop
}

loop := eventloop.NewEventLoop()
loop.Run(func(vm *goja.Runtime) {
func (e *Engine) Run(script string) (err error) {
e.loop.Run(func(vm *goja.Runtime) {
url.Enable(vm)
if err := fetch.Enable(vm); err != nil {
panic(err)
if err = fetch.Enable(vm); err != nil {
return
}
SCRIPT = `
(async function(){
const resp = await fetch('https://www.baidu.com')
console.log(resp.text())
})()
`
_, err2 := vm.RunString(SCRIPT)
if err2 != nil {
fmt.Println(err2.Error())
}
//vm.RunProgram(prg)
_, err = vm.RunString(script)
return
})
return
}

func NewEngine() *Engine {
loop := eventloop.NewEventLoop()
return &Engine{
loop: loop,
}
}

func Run(script string) (err error) {
engine := NewEngine()
return engine.Run(script)
}
38 changes: 38 additions & 0 deletions pkg/download/extension/engine_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package extension

import (
"testing"
)

func TestRun(t *testing.T) {
type args struct {
script string
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "ok",
args: args{
script: "1+1",
},
wantErr: false,
},
{
name: "error",
args: args{
script: "a",
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := Run(tt.args.script); (err != nil) != tt.wantErr {
t.Errorf("Run() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
2 changes: 0 additions & 2 deletions pkg/download/extension/polyfill/fetch/fetch.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// Fetch polyfill
const fetch = (function () {
const __fetch__ = globalThis.__fetch__;
delete globalThis.__fetch__;
Expand Down Expand Up @@ -55,7 +54,6 @@ const fetch = (function () {
return new Promise((resolve, reject) => {
try {
const resp = __fetch__(options.method, options.url, options.headers, options.body);
// console.log(new Response(resp).text());
resolve(new Response(resp));
} catch (e) {
reject(e);
Expand Down
2 changes: 1 addition & 1 deletion pkg/rest/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func CreateTask(w http.ResponseWriter, r *http.Request) {
if req.Rid != "" {
taskId, err = Downloader.Create(req.Rid, req.Opts)
} else if req.Req != nil {
taskId, err = Downloader.DirectCreate(req.Req, req.Opts)
taskId, err = Downloader.CreateDirect(req.Req, req.Opts)
} else {
util.WriteJson(w, http.StatusBadRequest, model.NewResultWithMsg("param is required: rid or req"))
return
Expand Down

0 comments on commit 91046d5

Please sign in to comment.