Skip to content

Commit

Permalink
Return ArrayBuffer from open()
Browse files Browse the repository at this point in the history
This is a breaking change part of grafana#1020.
  • Loading branch information
Ivan Mirić committed Apr 28, 2021
1 parent 14c6cd9 commit 89ff054
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
8 changes: 5 additions & 3 deletions js/initcontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,9 @@ func (i *InitContext) compileImport(src, filename string) (*goja.Program, error)
return pgm, err
}

// Open implements open() in the init context and will read and return the contents of a file.
// If the second argument is "b" it returns the data as a binary array, otherwise as a string.
// Open implements open() in the init context and will read and return the
// contents of a file. If the second argument is "b" it returns an ArrayBuffer
// instance, otherwise a string representation.
func (i *InitContext) Open(ctx context.Context, filename string, args ...string) (goja.Value, error) {
if lib.GetState(ctx) != nil {
return nil, errors.New(openCantBeUsedOutsideInitContextMsg)
Expand Down Expand Up @@ -242,7 +243,8 @@ func (i *InitContext) Open(ctx context.Context, filename string, args ...string)
}

if len(args) > 0 && args[0] == "b" {
return i.runtime.ToValue(data), nil
ab := i.runtime.NewArrayBuffer(data)
return i.runtime.ToValue(&ab), nil
}
return i.runtime.ToValue(string(data)), nil
}
29 changes: 13 additions & 16 deletions js/initcontext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,24 +239,21 @@ func TestInitContextRequire(t *testing.T) {
})
}

func createAndReadFile(t *testing.T, file string, content []byte, expectedLength int, binary bool) (*BundleInstance, error) {
func createAndReadFile(t *testing.T, file string, content []byte, expectedLength int, binary string) (*BundleInstance, error) {
fs := afero.NewMemMapFs()
assert.NoError(t, fs.MkdirAll("/path/to", 0o755))
assert.NoError(t, afero.WriteFile(fs, "/path/to/"+file, content, 0o644))

binaryArg := ""
if binary {
binaryArg = ",\"b\""
}

data := fmt.Sprintf(`
export let data = open("/path/to/%s"%s);
let binArg = "%s";
export let data = open("/path/to/%s", binArg);
var expectedLength = %d;
if (data.length != expectedLength) {
throw new Error("Length not equal, expected: " + expectedLength + ", actual: " + data.length);
var len = binArg === "b" ? "byteLength" : "length";
if (data[len] != expectedLength) {
throw new Error("Length not equal, expected: " + expectedLength + ", actual: " + data[len]);
}
export default function() {}
`, file, binaryArg, expectedLength)
`, binary, file, expectedLength)
b, err := getSimpleBundle(t, "/path/to/script.js", data, fs)

if !assert.NoError(t, err) {
Expand All @@ -283,7 +280,7 @@ func TestInitContextOpen(t *testing.T) {
}
for _, tc := range testCases {
t.Run(tc.file, func(t *testing.T) {
bi, err := createAndReadFile(t, tc.file, tc.content, tc.length, false)
bi, err := createAndReadFile(t, tc.file, tc.content, tc.length, "")
if !assert.NoError(t, err) {
return
}
Expand All @@ -292,12 +289,12 @@ func TestInitContextOpen(t *testing.T) {
}

t.Run("Binary", func(t *testing.T) {
bi, err := createAndReadFile(t, "/path/to/file.bin", []byte("hi!\x0f\xff\x01"), 6, true)
bi, err := createAndReadFile(t, "/path/to/file.bin", []byte("hi!\x0f\xff\x01"), 6, "b")
if !assert.NoError(t, err) {
return
}
bytes := []byte{104, 105, 33, 15, 255, 1}
assert.Equal(t, bytes, bi.Runtime.Get("data").Export())
buf := bi.Runtime.NewArrayBuffer([]byte{104, 105, 33, 15, 255, 1})
assert.Equal(t, buf, bi.Runtime.Get("data").Export())
})

testdata := map[string]string{
Expand All @@ -307,7 +304,7 @@ func TestInitContextOpen(t *testing.T) {

for name, loadPath := range testdata {
t.Run(name, func(t *testing.T) {
_, err := createAndReadFile(t, loadPath, []byte("content"), 7, false)
_, err := createAndReadFile(t, loadPath, []byte("content"), 7, "")
if !assert.NoError(t, err) {
return
}
Expand Down Expand Up @@ -409,7 +406,7 @@ func TestRequestWithBinaryFile(t *testing.T) {

v, err := bi.exports[consts.DefaultFn](goja.Undefined())
assert.NoError(t, err)
assert.NotNil(t, v)
require.NotNil(t, v)
assert.Equal(t, true, v.Export())

<-ch
Expand Down

0 comments on commit 89ff054

Please sign in to comment.