Skip to content

Commit

Permalink
Return ArrayBuffer from encoding.b64decode() by default
Browse files Browse the repository at this point in the history
The previous default behavior of returning string should now be
specified as an additional "s" argument.

This is a breaking change part of #1020.
  • Loading branch information
Ivan Mirić committed Mar 29, 2021
1 parent 924d346 commit dea54f4
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 14 deletions.
18 changes: 14 additions & 4 deletions js/modules/k6/encoding/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ func (e *Encoding) B64encode(ctx context.Context, input interface{}, encoding st
}

// B64decode returns the decoded data of the base64 encoded input string using
// the given encoding.
func (e *Encoding) B64decode(ctx context.Context, input string, encoding string) string {
// the given encoding. If format is "s" it returns the data as a string,
// otherwise as an ArrayBuffer.
func (e *Encoding) B64decode(ctx context.Context, input, encoding, format string) interface{} {
var output []byte
var err error

Expand All @@ -78,9 +79,18 @@ func (e *Encoding) B64decode(ctx context.Context, input string, encoding string)
output, err = base64.StdEncoding.DecodeString(input)
}

rt := common.GetRuntime(ctx)
if err != nil {
common.Throw(common.GetRuntime(ctx), err)
common.Throw(rt, err)
}

var out interface{}
if format == "s" {
out = string(output)
} else {
ab := rt.NewArrayBuffer(output)
out = &ab
}

return string(output)
return out
}
36 changes: 26 additions & 10 deletions js/modules/k6/encoding/encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,12 @@ func TestEncodingAlgorithms(t *testing.T) {
t.Run("DefaultDec", func(t *testing.T) {
_, err := rt.RunString(`
var correct = "hello world";
var decoded = encoding.b64decode("aGVsbG8gd29ybGQ=");
if (decoded !== correct) {
throw new Error("Decoding mismatch: " + decoded);
var decBin = encoding.b64decode("aGVsbG8gd29ybGQ=");
var decText = String.fromCharCode.apply(null, new Uint8Array(decBin));
decText = decodeURIComponent(escape(decText));
if (decText !== correct) {
throw new Error("Decoding mismatch: " + decText);
}`)
assert.NoError(t, err)
})
Expand All @@ -70,6 +73,17 @@ func TestEncodingAlgorithms(t *testing.T) {
}`)
assert.NoError(t, err)
})
t.Run("DefaultArrayBufferDec", func(t *testing.T) {
_, err := rt.RunString(`
var exp = "hello";
var decBin = encoding.b64decode("aGVsbG8=");
var decText = String.fromCharCode.apply(null, new Uint8Array(decBin));
decText = decodeURIComponent(escape(decText));
if (decText !== exp) {
throw new Error("Decoding mismatch: " + decText);
}`)
assert.NoError(t, err)
})
t.Run("DefaultUnicodeEnc", func(t *testing.T) {
_, err := rt.RunString(`
var correct = "44GT44KT44Gr44Gh44Gv5LiW55WM";
Expand All @@ -82,9 +96,11 @@ func TestEncodingAlgorithms(t *testing.T) {
t.Run("DefaultUnicodeDec", func(t *testing.T) {
_, err := rt.RunString(`
var correct = "こんにちは世界";
var decoded = encoding.b64decode("44GT44KT44Gr44Gh44Gv5LiW55WM");
if (decoded !== correct) {
throw new Error("Decoding mismatch: " + decoded);
var decBin = encoding.b64decode("44GT44KT44Gr44Gh44Gv5LiW55WM");
var decText = String.fromCharCode.apply(null, new Uint8Array(decBin));
decText = decodeURIComponent(escape(decText));
if (decText !== correct) {
throw new Error("Decoding mismatch: " + decText);
}`)
assert.NoError(t, err)
})
Expand All @@ -100,7 +116,7 @@ func TestEncodingAlgorithms(t *testing.T) {
t.Run("StdDec", func(t *testing.T) {
_, err := rt.RunString(`
var correct = "hello world";
var decoded = encoding.b64decode("aGVsbG8gd29ybGQ=", "std");
var decoded = encoding.b64decode("aGVsbG8gd29ybGQ=", "std", "s");
if (decoded !== correct) {
throw new Error("Decoding mismatch: " + decoded);
}`)
Expand All @@ -118,7 +134,7 @@ func TestEncodingAlgorithms(t *testing.T) {
t.Run("RawStdDec", func(t *testing.T) {
_, err := rt.RunString(`
var correct = "hello world";
var decoded = encoding.b64decode("aGVsbG8gd29ybGQ", "rawstd");
var decoded = encoding.b64decode("aGVsbG8gd29ybGQ", "rawstd", "s");
if (decoded !== correct) {
throw new Error("Decoding mismatch: " + decoded);
}`)
Expand All @@ -136,7 +152,7 @@ func TestEncodingAlgorithms(t *testing.T) {
t.Run("URLDec", func(t *testing.T) {
_, err := rt.RunString(`
var correct = "小飼弾..";
var decoded = encoding.b64decode("5bCP6aO85by-Li4=", "url");
var decoded = encoding.b64decode("5bCP6aO85by-Li4=", "url", "s");
if (decoded !== correct) {
throw new Error("Decoding mismatch: " + decoded);
}`)
Expand All @@ -154,7 +170,7 @@ func TestEncodingAlgorithms(t *testing.T) {
t.Run("RawURLDec", func(t *testing.T) {
_, err := rt.RunString(`
var correct = "小飼弾..";
var decoded = encoding.b64decode("5bCP6aO85by-Li4", "rawurl");
var decoded = encoding.b64decode("5bCP6aO85by-Li4", "rawurl", "s");
if (decoded !== correct) {
throw new Error("Decoding mismatch: " + decoded);
}`)
Expand Down

0 comments on commit dea54f4

Please sign in to comment.