github.com/manicqin/nomad@v0.9.5/ui/app/utils/stream-frames.js (about) 1 /** 2 * 3 * @param {string} chunk 4 * Chunk is an undelimited string of valid JSON objects as returned by a streaming endpoint. 5 * Each JSON object in a chunk contains two properties: 6 * Offset {number} The index from the beginning of the stream at which this JSON object starts 7 * Data {string} A base64 encoded string representing the contents of the stream this JSON 8 * object represents. 9 */ 10 export function decode(chunk) { 11 const lines = chunk 12 .replace(/\}\{/g, '}\n{') 13 .split('\n') 14 .without(''); 15 const frames = lines.map(line => JSON.parse(line)).filter(frame => frame.Data); 16 17 if (frames.length) { 18 frames.forEach(frame => (frame.Data = window.atob(frame.Data))); 19 return { 20 offset: frames[frames.length - 1].Offset, 21 message: frames.mapBy('Data').join(''), 22 }; 23 } 24 25 return {}; 26 }