-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
3,641 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,4 +24,5 @@ bin/ | |
|
||
.DS_Store | ||
|
||
dist/ | ||
node_modules/ | ||
cmd/web/dist/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package extension | ||
|
||
import ( | ||
_ "embed" | ||
"errors" | ||
"fmt" | ||
"github.com/dop251/goja" | ||
"github.com/dop251/goja_nodejs/eventloop" | ||
"github.com/dop251/goja_nodejs/url" | ||
"github.com/monkeyWie/gopeed/pkg/download/extension/inject/file" | ||
"github.com/monkeyWie/gopeed/pkg/download/extension/inject/formdata" | ||
"github.com/monkeyWie/gopeed/pkg/download/extension/inject/xhr" | ||
) | ||
|
||
//go:embed polyfill/dist/index.js | ||
var polyfillScript string | ||
|
||
type Engine struct { | ||
loop *eventloop.EventLoop | ||
|
||
Runtime *goja.Runtime | ||
} | ||
|
||
func (e *Engine) RunString(script string) (value goja.Value, err error) { | ||
defer func() { | ||
if r := recover(); r != nil { | ||
err = fmt.Errorf("%v", r) | ||
} | ||
}() | ||
e.loop.Run(func(runtime *goja.Runtime) { | ||
value, err = runtime.RunString(script) | ||
}) | ||
return | ||
} | ||
|
||
func (e *Engine) RunNative(cb goja.Callable, args ...goja.Value) (value goja.Value, err error) { | ||
defer func() { | ||
if r := recover(); r != nil { | ||
err = fmt.Errorf("%v", r) | ||
} | ||
}() | ||
e.loop.Run(func(runtime *goja.Runtime) { | ||
if args == nil { | ||
value, err = cb(nil) | ||
} else { | ||
value, err = cb(nil, args...) | ||
} | ||
}) | ||
return | ||
} | ||
|
||
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 | ||
url.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 | ||
} | ||
return | ||
}) | ||
return engine | ||
} | ||
|
||
func Run(script string) (value goja.Value, err error) { | ||
engine := NewEngine() | ||
return engine.RunString(script) | ||
} | ||
|
||
// ResolveResult 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 | ||
} |
Oops, something went wrong.