Skip to content

Commit 33b9ad4

Browse files
authored
Merge pull request #581 from marle3003/develop
Develop
2 parents b6e622e + 6632622 commit 33b9ad4

37 files changed

+949
-463
lines changed

engine/eventhandler_test.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ export default () => {
4040
export default () => {
4141
on('http', () => {
4242
console.log('a log message from event handler')
43-
return true
44-
})
43+
}, { track: true })
4544
}
4645
`,
4746
run: func(evt common.EventEmitter) []*common.Action {
@@ -61,8 +60,7 @@ export default () => {
6160
export default () => {
6261
on('http', () => {
6362
console.warn('a log message from event handler')
64-
return true
65-
})
63+
}, { track: true })
6664
}
6765
`,
6866
run: func(evt common.EventEmitter) []*common.Action {
@@ -82,8 +80,7 @@ export default () => {
8280
export default () => {
8381
on('http', () => {
8482
console.warn('a log message from event handler')
85-
return true
86-
})
83+
}, { track: true })
8784
}
8885
`,
8986
logger: &enginetest.Logger{IsLevelEnabledFunc: func(level string) bool { return false }},
@@ -101,8 +98,7 @@ export default () => {
10198
script: `import { on } from 'mokapi'
10299
export default () => {
103100
on('http', (msg) => {
104-
return true
105-
})
101+
}, { track: true })
106102
}
107103
`,
108104
run: func(evt common.EventEmitter) []*common.Action {
@@ -121,11 +117,9 @@ export default () => {
121117
export default () => {
122118
on('http', (req) => {
123119
req.method = 'GET'
124-
return true
125120
})
126121
on('http', (req) => {
127122
req.method = 'DELETE'
128-
return true
129123
})
130124
}
131125
`,

engine/js_test.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,7 @@ func TestJsOn(t *testing.T) {
173173
import {on, sleep} from 'mokapi'
174174
export default function() {
175175
on('http', function(request, response) {
176-
return true
177-
});
176+
}, { track: true });
178177
}
179178
`))
180179
r.NoError(t, err)
@@ -196,8 +195,7 @@ func TestJsOn(t *testing.T) {
196195
export default function() {
197196
on('http', function() {
198197
sleep(1000);
199-
return true
200-
});
198+
}, { track: true });
201199
}
202200
`))
203201
r.NoError(t, err)
@@ -216,8 +214,7 @@ func TestJsOn(t *testing.T) {
216214
export default function() {
217215
on('http', function() {
218216
sleep('1s');
219-
return true
220-
});
217+
}, { track: true });
221218
}
222219
`))
223220
r.NoError(t, err)
@@ -234,7 +231,7 @@ func TestJsOn(t *testing.T) {
234231
err := e.AddScript(newScript("test.js", `
235232
import {on} from 'mokapi'
236233
export default function() {
237-
on('http', function() {return true}, {tags: {'name': 'foobar'}});
234+
on('http', function() {}, {tags: {'name': 'foobar'}, track: true });
238235
}
239236
`))
240237
r.NoError(t, err)
@@ -250,7 +247,7 @@ func TestJsOn(t *testing.T) {
250247
err := e.AddScript(newScript("test.js", `
251248
import {on} from 'mokapi'
252249
export default function() {
253-
on('http', function() {return true}, {tags: {'foo': 'bar'}});
250+
on('http', function() {}, {tags: {'foo': 'bar'}, track: true });
254251
}
255252
`))
256253
r.NoError(t, err)
@@ -367,7 +364,7 @@ func TestJsOpen(t *testing.T) {
367364
import { foo } from 'foo'
368365
import { on } from 'mokapi'
369366
export default function() {
370-
on('http', function() { return true }, { tags: { name: foo } });
367+
on('http', function() { }, { tags: { name: foo }, track: true });
371368
}
372369
`)
373370
s.Config.Listeners.Add("", func(config dynamic.ConfigEvent) {

go.mod

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,21 @@ toolchain go1.24.1
66

77
require (
88
github.com/Masterminds/sprig v2.22.0+incompatible
9-
github.com/bradleyfalzon/ghinstallation/v2 v2.15.0
9+
github.com/bradleyfalzon/ghinstallation/v2 v2.16.0
1010
github.com/brianvoe/gofakeit/v6 v6.28.0
1111
github.com/dop251/goja v0.0.0-20250309171923-bcd7cc6bf64c
1212
github.com/evanw/esbuild v0.25.5
1313
github.com/fsnotify/fsnotify v1.9.0
1414
github.com/go-co-op/gocron v1.37.0
15-
github.com/go-git/go-git/v5 v5.16.0
15+
github.com/go-git/go-git/v5 v5.16.1
1616
github.com/google/uuid v1.6.0
1717
github.com/jinzhu/inflection v1.0.0
1818
github.com/pkg/errors v0.9.1
1919
github.com/sirupsen/logrus v1.9.3
2020
github.com/stretchr/testify v1.10.0
2121
github.com/yuin/gopher-lua v1.1.1
22-
golang.org/x/net v0.40.0
23-
golang.org/x/text v0.25.0
22+
golang.org/x/net v0.41.0
23+
golang.org/x/text v0.26.0
2424
gopkg.in/go-asn1-ber/asn1-ber.v1 v1.0.0-20181015200546-f715ec2f112d
2525
gopkg.in/yaml.v3 v3.0.1
2626
layeh.com/gopher-luar v1.0.11
@@ -42,7 +42,7 @@ require (
4242
github.com/go-sourcemap/sourcemap v2.1.3+incompatible // indirect
4343
github.com/golang-jwt/jwt/v4 v4.5.2 // indirect
4444
github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8 // indirect
45-
github.com/google/go-github/v71 v71.0.0 // indirect
45+
github.com/google/go-github/v72 v72.0.0 // indirect
4646
github.com/google/go-querystring v1.1.0 // indirect
4747
github.com/google/pprof v0.0.0-20230207041349-798e818bf904 // indirect
4848
github.com/huandu/xstrings v1.3.2 // indirect
@@ -58,7 +58,7 @@ require (
5858
github.com/skeema/knownhosts v1.3.1 // indirect
5959
github.com/xanzy/ssh-agent v0.3.3 // indirect
6060
go.uber.org/atomic v1.9.0 // indirect
61-
golang.org/x/crypto v0.38.0 // indirect
61+
golang.org/x/crypto v0.39.0 // indirect
6262
golang.org/x/sys v0.33.0 // indirect
6363
gopkg.in/warnings.v0 v0.1.2 // indirect
6464
)

go.sum

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be h1:9AeTilPcZAjCFI
1717
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be/go.mod h1:ySMOLuWl6zY27l47sB3qLNK6tF2fkHG55UZxx8oIVo4=
1818
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio=
1919
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
20-
github.com/bradleyfalzon/ghinstallation/v2 v2.15.0 h1:7r2rPUM04rgszMP0U1UZ1M5VoVVIlsaBSnpABfYxcQY=
21-
github.com/bradleyfalzon/ghinstallation/v2 v2.15.0/go.mod h1:PoH9Vhy82OeRFZfxsVrk3mfQhVkEzou9OOwPOsEhiXE=
20+
github.com/bradleyfalzon/ghinstallation/v2 v2.16.0 h1:B91r9bHtXp/+XRgS5aZm6ZzTdz3ahgJYmkt4xZkgDz8=
21+
github.com/bradleyfalzon/ghinstallation/v2 v2.16.0/go.mod h1:OeVe5ggFzoBnmgitZe/A+BqGOnv1DvU/0uiLQi1wutM=
2222
github.com/brianvoe/gofakeit/v6 v6.28.0 h1:Xib46XXuQfmlLS2EXRuJpqcw8St6qSZz75OUo0tgAW4=
2323
github.com/brianvoe/gofakeit/v6 v6.28.0/go.mod h1:Xj58BMSnFqcn/fAQeSK+/PLtC5kSb7FJIq4JyGa8vEs=
2424
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
@@ -54,8 +54,8 @@ github.com/go-git/go-billy/v5 v5.6.2 h1:6Q86EsPXMa7c3YZ3aLAQsMA0VlWmy43r6FHqa/UN
5454
github.com/go-git/go-billy/v5 v5.6.2/go.mod h1:rcFC2rAsp/erv7CMz9GczHcuD0D32fWzH+MJAU+jaUU=
5555
github.com/go-git/go-git-fixtures/v4 v4.3.2-0.20231010084843-55a94097c399 h1:eMje31YglSBqCdIqdhKBW8lokaMrL3uTkpGYlE2OOT4=
5656
github.com/go-git/go-git-fixtures/v4 v4.3.2-0.20231010084843-55a94097c399/go.mod h1:1OCfN199q1Jm3HZlxleg+Dw/mwps2Wbk9frAWm+4FII=
57-
github.com/go-git/go-git/v5 v5.16.0 h1:k3kuOEpkc0DeY7xlL6NaaNg39xdgQbtH5mwCafHO9AQ=
58-
github.com/go-git/go-git/v5 v5.16.0/go.mod h1:4Ge4alE/5gPs30F2H1esi2gPd69R0C39lolkucHBOp8=
57+
github.com/go-git/go-git/v5 v5.16.1 h1:TuxMBWNL7R05tXsUGi0kh1vi4tq0WfXNLlIrAkXG1k8=
58+
github.com/go-git/go-git/v5 v5.16.1/go.mod h1:4Ge4alE/5gPs30F2H1esi2gPd69R0C39lolkucHBOp8=
5959
github.com/go-sourcemap/sourcemap v2.1.3+incompatible h1:W1iEw64niKVGogNgBN3ePyLFfuisuzeidWPMPWmECqU=
6060
github.com/go-sourcemap/sourcemap v2.1.3+incompatible/go.mod h1:F8jJfvm2KbVjc5NqelyYJmf/v5J0dwNLS2mL4sNA1Jg=
6161
github.com/golang-jwt/jwt/v4 v4.5.2 h1:YtQM7lnr8iZ+j5q71MGKkNw9Mn7AjHM68uc9g5fXeUI=
@@ -65,8 +65,8 @@ github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8/go.mod h1:wcDNUv
6565
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
6666
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
6767
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
68-
github.com/google/go-github/v71 v71.0.0 h1:Zi16OymGKZZMm8ZliffVVJ/Q9YZreDKONCr+WUd0Z30=
69-
github.com/google/go-github/v71 v71.0.0/go.mod h1:URZXObp2BLlMjwu0O8g4y6VBneUj2bCHgnI8FfgZ51M=
68+
github.com/google/go-github/v72 v72.0.0 h1:FcIO37BLoVPBO9igQQ6tStsv2asG4IPcYFi655PPvBM=
69+
github.com/google/go-github/v72 v72.0.0/go.mod h1:WWtw8GMRiL62mvIquf1kO3onRHeWWKmK01qdCY8c5fg=
7070
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
7171
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
7272
github.com/google/pprof v0.0.0-20230207041349-798e818bf904 h1:4/hN5RUoecvl+RmJRE2YxKWtnnQls6rQjjW5oV7qg2U=
@@ -139,13 +139,13 @@ github.com/yuin/gopher-lua v1.1.1/go.mod h1:GBR0iDaNXjAgGg9zfCvksxSRnQx76gclCIb7
139139
go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
140140
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
141141
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
142-
golang.org/x/crypto v0.38.0 h1:jt+WWG8IZlBnVbomuhg2Mdq0+BBQaHbtqHEFEigjUV8=
143-
golang.org/x/crypto v0.38.0/go.mod h1:MvrbAqul58NNYPKnOra203SB9vpuZW0e+RRZV+Ggqjw=
142+
golang.org/x/crypto v0.39.0 h1:SHs+kF4LP+f+p14esP5jAoDpHU8Gu/v9lFRK6IT5imM=
143+
golang.org/x/crypto v0.39.0/go.mod h1:L+Xg3Wf6HoL4Bn4238Z6ft6KfEpN0tJGo53AAPC632U=
144144
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 h1:2dVuKD2vS7b0QIHQbpyTISPd0LeHDbnYEryqj5Q1ug8=
145145
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56/go.mod h1:M4RDyNAINzryxdtnbRXRL/OHtkFuWGRjvuhBJpk2IlY=
146146
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
147-
golang.org/x/net v0.40.0 h1:79Xs7wF06Gbdcg4kdCCIQArK11Z1hr5POQ6+fIYHNuY=
148-
golang.org/x/net v0.40.0/go.mod h1:y0hY0exeL2Pku80/zKK7tpntoX23cqL3Oa6njdgRtds=
147+
golang.org/x/net v0.41.0 h1:vBTly1HeNPEn3wtREYfy4GZ/NECgw2Cnl+nK6Nz3uvw=
148+
golang.org/x/net v0.41.0/go.mod h1:B/K4NNqkfmg07DQYrbwvSluqCJOOXwUjeb/5lOisjbA=
149149
golang.org/x/sys v0.0.0-20190204203706-41f3e6584952/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
150150
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
151151
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
@@ -159,8 +159,8 @@ golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9sn
159159
golang.org/x/term v0.32.0 h1:DR4lr0TjUs3epypdhTOkMmuF5CDFJ/8pOnbzMZPQ7bg=
160160
golang.org/x/term v0.32.0/go.mod h1:uZG1FhGx848Sqfsq4/DlJr3xGGsYMu/L5GW4abiaEPQ=
161161
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
162-
golang.org/x/text v0.25.0 h1:qVyWApTSYLk/drJRO5mDlNYskwQznZmkpV2c8q9zls4=
163-
golang.org/x/text v0.25.0/go.mod h1:WEdwpYrmk1qmdHvhkSTNPm3app7v4rsT8F2UD6+VHIA=
162+
golang.org/x/text v0.26.0 h1:P42AVeLghgTYr4+xUnTRKDMqpar+PtX7KWuNQL21L8M=
163+
golang.org/x/text v0.26.0/go.mod h1:QK15LZJUUQVJxhz7wXgxSy/CJaTFjd0G+YLonydOVQA=
164164
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
165165
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
166166
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

js/mokapi/mokapi.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ func Require(vm *goja.Runtime, module *goja.Object) {
3636
obj.Set("encoding", f.Marshal)
3737
obj.Set("date", f.Date)
3838
obj.Set("marshal", f.Marshal)
39+
obj.Set("patch", patch)
40+
obj.Set("Delete", Delete)
3941
}
4042

4143
func (m *Module) Sleep(i interface{}) {

js/mokapi/on.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ import (
1010
)
1111

1212
type onArgs struct {
13-
tags map[string]string
13+
tags map[string]string
14+
track bool
1415
}
1516

1617
func (m *Module) On(event string, do goja.Value, vArgs goja.Value) {
17-
args, err := getOnArgs(m.vm, vArgs)
18+
eventArgs, err := getOnArgs(m.vm, vArgs)
1819
if err != nil {
1920
panic(m.vm.ToValue(err.Error()))
2021
}
@@ -47,6 +48,10 @@ func (m *Module) On(event string, do goja.Value, vArgs goja.Value) {
4748
return r.ToBoolean(), nil
4849
}
4950

51+
if eventArgs.track {
52+
return true, nil
53+
}
54+
5055
newHashes, err := getHashes(args...)
5156
if err != nil {
5257
return false, err
@@ -55,7 +60,7 @@ func (m *Module) On(event string, do goja.Value, vArgs goja.Value) {
5560
return haveChanges(origin, newHashes), nil
5661
}
5762

58-
m.host.On(event, f, args.tags)
63+
m.host.On(event, f, eventArgs.tags)
5964
}
6065

6166
func getOnArgs(vm *goja.Runtime, args goja.Value) (onArgs, error) {
@@ -80,6 +85,15 @@ func getOnArgs(vm *goja.Runtime, args goja.Value) (onArgs, error) {
8085
for _, key := range tagsO.Keys() {
8186
result.tags[key] = tagsO.Get(key).String()
8287
}
88+
case "track":
89+
v := params.Get(k)
90+
if goja.IsUndefined(v) || goja.IsNull(v) {
91+
continue
92+
}
93+
if v.ExportType().Kind() != reflect.Bool {
94+
return onArgs{}, fmt.Errorf("unexpected type for track: %v", util.JsType(v.Export()))
95+
}
96+
result.track = v.ToBoolean()
8397
}
8498
}
8599
return result, nil

js/mokapi/on_test.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,16 @@ func TestModule_On(t *testing.T) {
3030

3131
_, err := vm.RunString(`
3232
const m = require('mokapi')
33-
m.on('http', () => true)
33+
let result = 0;
34+
m.on('http', () => result++)
3435
`)
3536
r.NoError(t, err)
3637
r.Equal(t, "http", event)
3738
b, err := handler()
3839
r.NoError(t, err)
39-
r.Equal(t, true, b)
40+
r.Equal(t, false, b)
41+
v, _ := vm.RunString("result")
42+
r.Equal(t, int64(1), v.Export())
4043
},
4144
},
4245
{
@@ -49,16 +52,19 @@ func TestModule_On(t *testing.T) {
4952

5053
_, err := vm.RunString(`
5154
const m = require('mokapi')
52-
m.on('http', (param) => param === 'foo')
55+
let result = false
56+
m.on('http', (param) => result = param === 'foo')
5357
`)
5458
r.NoError(t, err)
5559
b, err := handler("foo")
5660
r.NoError(t, err)
5761
r.Equal(t, true, b)
62+
v, _ := vm.RunString("result")
63+
r.Equal(t, true, v.Export())
5864
},
5965
},
6066
{
61-
name: "event handler changes params but returns void",
67+
name: "event handler changes params",
6268
test: func(t *testing.T, vm *goja.Runtime, host *enginetest.Host) {
6369
var handler func(args ...interface{}) (bool, error)
6470
host.OnFunc = func(evt string, do func(args ...interface{}) (bool, error), tags map[string]string) {

js/mokapi/patch.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package mokapi
2+
3+
var Delete = &struct{}{}
4+
5+
func patch(target, patch any) any {
6+
if patch == nil {
7+
return target
8+
}
9+
if target == nil {
10+
return patch
11+
}
12+
13+
mapTarget, isTargetMap := target.(map[string]any)
14+
mapPatch, isPatchMap := patch.(map[string]any)
15+
16+
if isTargetMap && isPatchMap {
17+
return patchMap(mapTarget, mapPatch)
18+
} else {
19+
return patch
20+
}
21+
}
22+
23+
func patchMap(t, p map[string]any) map[string]any {
24+
result := make(map[string]any)
25+
26+
// copy original value
27+
for k, v := range t {
28+
result[k] = v
29+
}
30+
31+
for k, v := range p {
32+
if v == Delete {
33+
delete(result, k)
34+
continue
35+
}
36+
37+
if vt, ok := t[k]; ok {
38+
result[k] = patch(vt, v)
39+
} else {
40+
result[k] = v
41+
}
42+
}
43+
return result
44+
}

0 commit comments

Comments
 (0)