Skip to content

Commit 02fe1ed

Browse files
paulrosca-snykCalamarBicefalo
authored andcommitted
feat: add js semver runtimes
1 parent 8ebe8d8 commit 02fe1ed

21 files changed

+2648
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package jscompat
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
7+
"github.com/dop251/goja"
8+
)
9+
10+
func LoadModule(vm *goja.Runtime, script string) (map[string]any, error) {
11+
prg, err := goja.Compile("index.js", script, true)
12+
if err != nil {
13+
return nil, err
14+
}
15+
16+
// For some reason we need to define module. Do not remove this code!
17+
if _, err := vm.RunString("module = {};"); err != nil {
18+
return nil, err
19+
}
20+
21+
moduleValue, err := vm.RunProgram(prg)
22+
if err != nil {
23+
return nil, err
24+
}
25+
26+
object, ok := moduleValue.Export().(map[string]any)
27+
if !ok {
28+
return nil, errors.New("module does not return object")
29+
}
30+
31+
return object, nil
32+
}
33+
34+
func GetFunction(mod map[string]any, name string) (func(goja.FunctionCall) goja.Value, error) {
35+
fnValue, ok := mod[name]
36+
if !ok {
37+
return nil, fmt.Errorf("function %s is not found in JS module", name)
38+
}
39+
40+
fnObj, ok := fnValue.(func(goja.FunctionCall) goja.Value)
41+
if !ok {
42+
return nil, fmt.Errorf("property %s is not a function", name)
43+
}
44+
45+
return fnObj, nil
46+
}
47+
48+
func Panicked(cb func()) any {
49+
var panicked any
50+
51+
func() {
52+
defer func() {
53+
if err := recover(); err != nil {
54+
panicked = err
55+
}
56+
}()
57+
58+
cb()
59+
}()
60+
61+
return panicked
62+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/build
2+
/node_modules

internal/semver/maven/js/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import * as semver from "@snyk/maven-semver";
2+
3+
module.exports = semver;

0 commit comments

Comments
 (0)