Skip to content

Commit

Permalink
Merge pull request #7551 from hashicorp/f-ui/use-new-text-encoder-for…
Browse files Browse the repository at this point in the history
…-stream-frames

UI: Use new text encoder for stream frames
  • Loading branch information
DingoEatingFuzz committed Mar 30, 2020
2 parents 28737b2 + c30a153 commit 91988f3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
14 changes: 8 additions & 6 deletions ui/app/utils/stream-frames.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import { TextDecoderLite } from 'text-encoder-lite';
import base64js from 'base64-js';

const decoder = new TextDecoderLite('utf-8');

/**
*
* @param {string} chunk
Expand All @@ -15,7 +20,7 @@ export function decode(chunk) {
const frames = lines.map(line => JSON.parse(line)).filter(frame => frame.Data);

if (frames.length) {
frames.forEach(frame => (frame.Data = b64DecodeUnicode(frame.Data)));
frames.forEach(frame => (frame.Data = b64decode(frame.Data)));
return {
offset: frames[frames.length - 1].Offset,
message: frames.mapBy('Data').join(''),
Expand All @@ -25,9 +30,6 @@ export function decode(chunk) {
return {};
}

function b64DecodeUnicode(str) {
// from bytestream, to percent-encoding, to original string.
return decodeURIComponent(window.atob(str).split('').map(function(c) {
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
}).join(''));
function b64decode(str) {
return decoder.decode(base64js.toByteArray(str));
}
13 changes: 13 additions & 0 deletions ui/tests/unit/utils/stream-frames-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { module, test } from 'qunit';
import { decode } from 'nomad-ui/utils/stream-frames';
import { TextEncoderLite } from 'text-encoder-lite';
import base64js from 'base64-js';

const Encoder = new TextEncoderLite('utf-8');
const encode = str => base64js.fromByteArray(Encoder.encode(str));

module('Unit | Util | stream-frames', function() {
const { btoa } = window;
Expand Down Expand Up @@ -31,6 +36,14 @@ module('Unit | Util | stream-frames', function() {
in: '',
out: {},
},
{
name: 'Multi-byte unicode',
in: `{"Offset":1,"Data":"${encode('ワンワン 🐶')}"}`,
out: {
offset: 1,
message: 'ワンワン 🐶',
},
},
];

decodeTestCases.forEach(testCase => {
Expand Down

0 comments on commit 91988f3

Please sign in to comment.