You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
2.7 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package jsruntime
  2. import (
  3. "encoding/json"
  4. "git.ouxuan.net/hasaki-service/hasaki-sdk/hskhttpdo"
  5. "github.com/dop251/goja"
  6. "github.com/dop251/goja_nodejs/console"
  7. "github.com/dop251/goja_nodejs/eventloop"
  8. "github.com/dop251/goja_nodejs/require"
  9. "io/ioutil"
  10. "time"
  11. )
  12. type JsRuntime struct {
  13. MainSrc string
  14. UseSrc string
  15. Relys Relys
  16. TimeoutSec int64
  17. runtime *goja.Runtime
  18. registry *require.Registry
  19. requireModule *require.RequireModule
  20. loop *eventloop.EventLoop
  21. }
  22. type Rely struct {
  23. Variable string
  24. FileName string
  25. }
  26. type Relys []Rely
  27. func NewJsRuntime(mainFileName string, useFileName string, rels Relys) (*JsRuntime, error) {
  28. jr := JsRuntime{
  29. Relys: rels,
  30. TimeoutSec: 8,
  31. }
  32. mainScript, err := ioutil.ReadFile(mainFileName)
  33. if err != nil {
  34. return nil, err
  35. }
  36. jr.MainSrc = string(mainScript)
  37. jr.registry = new(require.Registry) // this can be shared by multiple runtimes
  38. jr.loop = eventloop.NewEventLoop()
  39. jr.loop.Run(func(r *goja.Runtime) {
  40. jr.runtime = r
  41. })
  42. jr.requireModule = jr.registry.Enable(jr.runtime)
  43. console.Enable(jr.runtime)
  44. jr.runtime.Set("GoVueUse", func(v interface{}) {
  45. jr.runtime.Set("GoVueUseCall", v)
  46. })
  47. jr.setAjax()
  48. for e := range rels {
  49. v, err := jr.requireModule.Require(rels[e].FileName)
  50. if err != nil {
  51. return nil, err
  52. }
  53. jr.runtime.Set(rels[e].Variable, v)
  54. }
  55. go func() {
  56. for {
  57. useSrc, _ := ioutil.ReadFile(useFileName)
  58. jr.UseSrc = string(useSrc)
  59. time.Sleep(time.Second * 2)
  60. }
  61. }()
  62. return &jr, nil
  63. }
  64. func (jr *JsRuntime) setAjax() {
  65. jr.runtime.Set("GO_AJAX", func(call goja.FunctionCall) string {
  66. m := call.Argument(0)
  67. url := call.Argument(1)
  68. data := call.Argument(3)
  69. res, err := hskhttpdo.HttpDo{
  70. Url: url.String(),
  71. Raw: []byte(data.String()),
  72. }.Request(m.String())
  73. r := map[string]string{
  74. "code": "200",
  75. "data": string(res),
  76. }
  77. if err != nil {
  78. r["code"] = "502"
  79. }
  80. rs, _ := json.Marshal(r)
  81. return string(rs)
  82. })
  83. }
  84. func (jr *JsRuntime) SetVariable(name string, value interface{}) {
  85. jr.runtime.Set(name, value)
  86. }
  87. func (jr *JsRuntime) Render(href, tplSrc string) string {
  88. jr.registry.Lock()
  89. isLock := true
  90. go func() {
  91. time.Sleep(time.Duration(jr.TimeoutSec) * time.Second)
  92. if isLock {
  93. jr.runtime.Interrupt("timeout")
  94. }
  95. }()
  96. defer func() {
  97. isLock = false
  98. jr.registry.Unlock()
  99. }()
  100. _, err := jr.runtime.RunString(jr.UseSrc)
  101. if err != nil {
  102. return err.Error()
  103. }
  104. jr.runtime.Set("HTML_SRC", tplSrc)
  105. jr.runtime.Set("SELF_HREF", href)
  106. result := ""
  107. jr.runtime.Set("HTML_OUT", func(data string) {
  108. result = data
  109. })
  110. _, err = jr.runtime.RunString(jr.MainSrc)
  111. if err != nil {
  112. return err.Error()
  113. }
  114. return result
  115. }