open-nomad/ui/app/utils/stream-frames.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

40 lines
1.1 KiB
JavaScript
Raw Normal View History

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
import { TextDecoderLite } from 'text-encoder-lite';
import base64js from 'base64-js';
const decoder = new TextDecoderLite('utf-8');
/**
*
* @param {string} chunk
* Chunk is an undelimited string of valid JSON objects as returned by a streaming endpoint.
* Each JSON object in a chunk contains two properties:
* Offset {number} The index from the beginning of the stream at which this JSON object starts
* Data {string} A base64 encoded string representing the contents of the stream this JSON
* object represents.
*/
export function decode(chunk) {
2021-12-28 14:45:20 +00:00
const lines = chunk.replace(/\}\{/g, '}\n{').split('\n').without('');
2021-12-28 16:08:12 +00:00
const frames = lines
.map((line) => JSON.parse(line))
.filter((frame) => frame.Data);
if (frames.length) {
2021-12-28 14:45:20 +00:00
frames.forEach((frame) => (frame.Data = b64decode(frame.Data)));
return {
offset: frames[frames.length - 1].Offset,
message: frames.mapBy('Data').join(''),
};
}
return {};
}
function b64decode(str) {
return decoder.decode(base64js.toByteArray(str));
}