Skip to content
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/couchbase/sg-bucket
go 1.19

require (
github.com/couchbasedeps/v8go v1.7.5-0.20230711212500-516f3127f59a
github.com/robertkrimen/otto v0.0.0-20211024170158-b87d35c0b86f
github.com/stretchr/testify v1.7.1
golang.org/x/text v0.3.7
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/couchbasedeps/v8go v1.7.5-0.20230711212500-516f3127f59a h1:eAnRt+HIzIEN9wOjtIdVcWxKl87gWwRMqUErcmu92Eo=
github.com/couchbasedeps/v8go v1.7.5-0.20230711212500-516f3127f59a/go.mod h1:idYCHzuZs5u+Yv9mYwhOBheFP82+7arI2Y8xVeU2Ttk=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
Expand Down
294 changes: 294 additions & 0 deletions js/js_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,294 @@
//go:build cb_sg_v8

/*
Copyright 2022-Present Couchbase, Inc.

Use of this software is governed by the Business Source License included in
the file licenses/BSL-Couchbase.txt. As of the Change Date specified in that
file, in accordance with the Business Source License, use of this software will
be governed by the Apache License, Version 2.0, included in the file
licenses/APL2.txt.
*/

package js

import (
"context"
"encoding/json"
"math"
"math/big"
"strconv"
"testing"
"time"

"github.com/couchbasedeps/v8go"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestSquare(t *testing.T) {
TestWithVMs(t, func(t *testing.T, vm VM) {
assert.NotNil(t, vm.Context())
service := NewService(vm, "square", `function(n) {return n * n;}`)
assert.Equal(t, "square", service.Name())
assert.Equal(t, vm, service.Host())

// Test Run:
result, err := service.Run(vm.Context(), 13)
assert.NoError(t, err)
assert.EqualValues(t, 169, result)

// Test WithRunner:
result, err = service.WithRunner(func(runner Runner) (any, error) {
assert.Equal(t, vm.Context(), runner.Context())
return runner.Run(9)
})
assert.NoError(t, err)
assert.EqualValues(t, 81, result)
})
}

func TestSquareV8Args(t *testing.T) {
vm := V8.NewVM(testCtx(t))
defer vm.Close()

service := NewService(vm, "square", `function(n) {return n * n;}`)
result, err := service.WithRunner(func(runner Runner) (any, error) {
v8Runner := runner.(*V8Runner)
nine, err := v8Runner.NewInt(9)
require.NoError(t, err)
result, err := v8Runner.RunWithV8Args(nine)
if err != nil {
return nil, err
}
return result.Integer(), nil
})
assert.NoError(t, err)
assert.EqualValues(t, 81, result)
}

func TestJSON(t *testing.T) {
ctx := testCtx(t)

var pool VMPool
pool.Init(ctx, V8, 4)
defer pool.Close()

service := NewService(&pool, "length", `function(v) {return v.length;}`)

result, err := service.Run(ctx, []string{"a", "b", "c"})
if assert.NoError(t, err) {
assert.EqualValues(t, 3, result)
}

result, err = service.Run(ctx, JSONString(`[1,2,3,4]`))
if assert.NoError(t, err) {
assert.EqualValues(t, 4, result)
}
}

func TestCallback(t *testing.T) {
ctx := testCtx(t)

vm := V8.NewVM(ctx)
defer vm.Close()

src := `(function() {
return hey(1234, "hey you guys!");
});`

var heyParam string

// A callback function that's callable from JS as hey(num, str)
hey := func(r *V8Runner, this *v8go.Object, args []*v8go.Value) (result any, err error) {
assert.Equal(t, len(args), 2)
assert.Equal(t, int64(1234), args[0].Integer())
heyParam = args[1].String()
return 5678, nil
}

service := NewCustomService(vm, "callbacks", func(tmpl *V8BasicTemplate) (V8Template, error) {
err := tmpl.SetScript(src)
tmpl.GlobalCallback("hey", hey)
return tmpl, err
})

result, err := service.Run(ctx)
assert.NoError(t, err)
assert.Equal(t, 5678, result)
assert.Equal(t, "hey you guys!", heyParam)
}

// Test conversion of numbers into/out of JavaScript.
func TestNumbers(t *testing.T) {
TestWithVMs(t, func(t *testing.T, vm VM) {
service := NewService(vm, "numbers", `function(n, expectedStr) {
if (typeof(n) != 'number' && typeof(n) != 'bigint') throw "Unexpectedly n is a " + typeof(n);
var str = n.toString();
console.info("n=",n,"str=",str);
if (str != expectedStr) throw "Got " + str + " instead of " + expectedStr;
return n;
}`)

t.Run("integers", func(t *testing.T) {
testInt := func(n int64) {
result, err := service.Run(nil, n, strconv.FormatInt(n, 10))
if assert.NoError(t, err) {
assert.EqualValues(t, n, result)
}
}

testInt(-1)
testInt(0)
testInt(1)
testInt(math.MaxInt32)
testInt(math.MinInt32)
testInt(math.MaxInt64)
testInt(math.MinInt64)
testInt(math.MaxInt64 - 1)
testInt(math.MinInt64 + 1)
testInt(JavascriptMaxSafeInt)
testInt(JavascriptMinSafeInt)
testInt(JavascriptMaxSafeInt + 1)
testInt(JavascriptMinSafeInt - 1)
})

t.Run("floats", func(t *testing.T) {
testFloat := func(n float64) {
result, err := service.Run(nil, n, strconv.FormatFloat(n, 'f', -1, 64))
if assert.NoError(t, err) {
assert.EqualValues(t, n, result)
}
}

testFloat(-1.0)
testFloat(0.0)
testFloat(0.001)
testFloat(1.0)
testFloat(math.MaxInt32)
testFloat(math.MinInt32)
testFloat(math.MaxInt64)
testFloat(math.MinInt64)
testFloat(float64(JavascriptMaxSafeInt))
testFloat(float64(JavascriptMinSafeInt))
testFloat(float64(JavascriptMaxSafeInt + 1))
testFloat(float64(JavascriptMinSafeInt - 1))
testFloat(12345678.12345678)
testFloat(22.0 / 7.0)
testFloat(0.1)
})

t.Run("json_Number_integer", func(t *testing.T) {
hugeInt := json.Number("123456789012345")
result, err := service.Run(nil, hugeInt, string(hugeInt))
if assert.NoError(t, err) {
assert.EqualValues(t, 123456789012345, result)
}
})

if vm.Engine().languageVersion >= 11 { // (Otto does not support BigInts)
t.Run("json_Number_huge_integer", func(t *testing.T) {
hugeInt := json.Number("1234567890123456789012345678901234567890")
result, err := service.Run(nil, hugeInt, string(hugeInt))
if assert.NoError(t, err) {
ibig := new(big.Int)
ibig, _ = ibig.SetString(string(hugeInt), 10)
assert.EqualValues(t, ibig, result)
}
})
}

t.Run("json_Number_float", func(t *testing.T) {
floatStr := json.Number("1234567890.123")
result, err := service.Run(nil, floatStr, string(floatStr))
if assert.NoError(t, err) {
assert.EqualValues(t, 1234567890.123, result)
}
})
})
}

// For security purposes, verify that JS APIs to do network or file I/O are not present:
func TestNoIO(t *testing.T) {
ctx := testCtx(t)

vm := V8.NewVM(ctx) // Otto appears to have no way to refer to the global object...
defer vm.Close()

service := NewService(vm, "check", `function() {
// Ensure that global fns/classes enabling network or file access are missing:
if (globalThis.fetch !== undefined) throw "fetch exists";
if (globalThis.XMLHttpRequest !== undefined) throw "XMLHttpRequest exists";
if (globalThis.File !== undefined) throw "File exists";
if (globalThis.require !== undefined) throw "require exists";
// But the following should exist:
if (globalThis.Math === undefined) throw "Math is missing!";
if (globalThis.String === undefined) throw "String is missing!";
if (globalThis.Number === undefined) throw "Number is missing!";
if (globalThis.console === undefined) throw "console is missing!";
}`)
_, err := service.Run(ctx)
assert.NoError(t, err)
}

// Verify that ECMAScript modules can't be loaded. (The older `require` is checked in TestNoIO.)
func TestNoModules(t *testing.T) {
ctx := testCtx(t)
vm := V8.NewVM(ctx) // Otto doesn't support ES modules
defer vm.Close()

src := `import foo from 'foo';
(function() { });`

service := NewCustomService(vm, "check", func(tmpl *V8BasicTemplate) (V8Template, error) {
err := tmpl.SetScript(src)
return tmpl, err
})
_, err := service.Run(ctx)
assert.ErrorContains(t, err, "Cannot use import statement outside a module")
}

func TestTimeout(t *testing.T) {
TestWithVMs(t, func(t *testing.T, vm VM) {
ctx := vm.Context()

ctx, cancel := context.WithTimeout(ctx, 2*time.Second)
defer cancel()

service := NewService(vm, "forever", `function() { while (true) ; }`)
start := time.Now()
_, err := service.Run(ctx)
assert.Less(t, time.Since(start), 4*time.Second)
assert.Equal(t, context.DeadlineExceeded, err)
})
}

func TestOutOfMemory(t *testing.T) {
ctx := testCtx(t)
vm := V8.NewVM(ctx)
defer vm.Close()

service := NewService(vm, "OOM", `
function() {
let a = ["supercalifragilisticexpialidocious"];
while (true) {
a = [a, a];
}
}`)
_, err := service.Run(ctx)
assert.ErrorContains(t, err, "ExecutionTerminated: script execution has been terminated")
}

func TestStackOverflow(t *testing.T) {
ctx := testCtx(t)
vm := V8.NewVM(ctx)
defer vm.Close()

service := NewService(vm, "Overflow", `
function() {
function recurse(n) {console.log("level ", n); return recurse(n + 1) * recurse(n + 2);}
return recurse(0);
}`)
_, err := service.Run(ctx)
assert.ErrorContains(t, err, "Maximum call stack size exceeded")
}
75 changes: 75 additions & 0 deletions js/logging.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
Copyright 2023-Present Couchbase, Inc.

Use of this software is governed by the Business Source License included in
the file licenses/BSL-Couchbase.txt. As of the Change Date specified in that
file, in accordance with the Business Source License, use of this software will
be governed by the Apache License, Version 2.0, included in the file
licenses/APL2.txt.
*/

package js

import (
"context"
"log"
)

type LogLevel uint32

const (
// LevelNone disables all logging
LevelNone LogLevel = iota
// LevelError enables only error logging.
LevelError
// LevelWarn enables warn and error logging.
LevelWarn
// LevelInfo enables info, warn, and error logging.
LevelInfo
// LevelDebug enables debug, info, warn, and error logging.
LevelDebug
// LevelTrace enables trace, debug, info, warn, and error logging.
LevelTrace
)

var (
logLevelNamesPrint = []string{"JS: [NON] ", "JS: [ERR] ", "JS: [WRN] ", "JS: [INF] ", "JS: [DBG] ", "JS: [TRC] "}
)

// Set this to configure the log level.
var Logging LogLevel = LevelInfo

// Set this callback to redirect logging elsewhere. Default value writes to Go `log.Printf`
var LoggingCallback = func(ctx context.Context, level LogLevel, fmt string, args ...any) {
log.Printf(logLevelNamesPrint[level]+fmt, args...)
}

func logError(ctx context.Context, fmt string, args ...any) {
if Logging >= LevelError {
LoggingCallback(ctx, LevelError, fmt, args...)
}
}

func warn(ctx context.Context, fmt string, args ...any) {
if Logging >= LevelWarn {
LoggingCallback(ctx, LevelWarn, fmt, args...)
}
}

func info(ctx context.Context, fmt string, args ...any) {
if Logging >= LevelInfo {
LoggingCallback(ctx, LevelInfo, fmt, args...)
}
}

func debug(ctx context.Context, fmt string, args ...any) {
if Logging >= LevelDebug {
LoggingCallback(ctx, LevelDebug, fmt, args...)
}
}

func trace(ctx context.Context, fmt string, args ...any) {
if Logging >= LevelTrace {
LoggingCallback(ctx, LevelTrace, fmt, args...)
}
}
Loading