2017-12-15 21:39:18 +00:00
|
|
|
import EmberObject, { computed } from '@ember/object';
|
2017-11-14 18:54:36 +00:00
|
|
|
import { task } from 'ember-concurrency';
|
|
|
|
import TextDecoder from 'nomad-ui/utils/classes/text-decoder';
|
2017-11-21 01:09:28 +00:00
|
|
|
import AbstractLogger from './abstract-logger';
|
2018-02-26 20:23:01 +00:00
|
|
|
import { fetchFailure } from './log';
|
2017-11-14 18:54:36 +00:00
|
|
|
|
2017-11-21 01:09:28 +00:00
|
|
|
export default EmberObject.extend(AbstractLogger, {
|
2017-11-14 18:54:36 +00:00
|
|
|
reader: null,
|
|
|
|
|
2017-11-21 01:09:28 +00:00
|
|
|
additionalParams: computed(() => ({
|
|
|
|
follow: true,
|
|
|
|
})),
|
|
|
|
|
2017-11-14 18:54:36 +00:00
|
|
|
start() {
|
2019-03-26 07:46:44 +00:00
|
|
|
return this.poll.perform();
|
2017-11-14 18:54:36 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
stop() {
|
2019-03-26 07:46:44 +00:00
|
|
|
const reader = this.reader;
|
2017-11-14 18:54:36 +00:00
|
|
|
if (reader) {
|
|
|
|
reader.cancel();
|
|
|
|
}
|
2019-03-26 07:46:44 +00:00
|
|
|
return this.poll.cancelAll();
|
2017-11-14 18:54:36 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
poll: task(function*() {
|
2019-03-26 07:46:44 +00:00
|
|
|
const url = this.fullUrl;
|
|
|
|
const logFetch = this.logFetch;
|
2017-11-21 01:09:28 +00:00
|
|
|
|
2017-11-14 18:54:36 +00:00
|
|
|
let streamClosed = false;
|
2017-11-21 01:09:28 +00:00
|
|
|
let buffer = '';
|
2017-11-14 18:54:36 +00:00
|
|
|
|
2017-11-21 01:09:28 +00:00
|
|
|
const decoder = new TextDecoder();
|
2018-02-26 20:23:01 +00:00
|
|
|
const reader = yield logFetch(url).then(res => res.body.getReader(), fetchFailure(url));
|
|
|
|
|
|
|
|
if (!reader) {
|
|
|
|
return;
|
|
|
|
}
|
2017-11-21 01:09:28 +00:00
|
|
|
|
2017-11-14 18:54:36 +00:00
|
|
|
this.set('reader', reader);
|
|
|
|
|
|
|
|
while (!streamClosed) {
|
|
|
|
yield reader.read().then(({ value, done }) => {
|
|
|
|
streamClosed = done;
|
2017-11-21 01:09:28 +00:00
|
|
|
|
|
|
|
// There is no guarantee that value will be a complete JSON object,
|
|
|
|
// so it needs to be buffered.
|
|
|
|
buffer += decoder.decode(value, { stream: true });
|
|
|
|
|
|
|
|
// Only when the buffer contains a close bracket can we be sure the buffer
|
|
|
|
// is in a complete state
|
|
|
|
if (buffer.indexOf('}') !== -1) {
|
|
|
|
// The buffer can be one or more complete frames with additional text for the
|
|
|
|
// next frame
|
|
|
|
const [, chunk, newBuffer] = buffer.match(/(.*\})(.*)$/);
|
|
|
|
|
|
|
|
// Peel chunk off the front of the buffer (since it represents complete frames)
|
|
|
|
// and set the buffer to be the remainder
|
|
|
|
buffer = newBuffer;
|
|
|
|
|
|
|
|
// Assuming the logs endpoint never returns nested JSON (it shouldn't), at this
|
|
|
|
// point chunk is a series of valid JSON objects with no delimiter.
|
|
|
|
const lines = chunk.replace(/\}\{/g, '}\n{').split('\n');
|
|
|
|
const frames = lines.map(line => JSON.parse(line)).filter(frame => frame.Data);
|
|
|
|
|
|
|
|
if (frames.length) {
|
|
|
|
frames.forEach(frame => (frame.Data = window.atob(frame.Data)));
|
|
|
|
this.set('endOffset', frames[frames.length - 1].Offset);
|
2019-03-26 07:46:44 +00:00
|
|
|
this.write(frames.mapBy('Data').join(''));
|
2017-11-21 01:09:28 +00:00
|
|
|
}
|
|
|
|
}
|
2017-11-14 18:54:36 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}),
|
2017-11-21 22:57:19 +00:00
|
|
|
}).reopenClass({
|
2018-05-24 22:52:15 +00:00
|
|
|
isSupported: !!window.ReadableStream && !isSafari(),
|
2017-11-14 18:54:36 +00:00
|
|
|
});
|
2018-05-24 22:52:15 +00:00
|
|
|
|
|
|
|
// Fetch streaming doesn't work in Safari yet despite all the primitives being in place.
|
|
|
|
// Bug: https://bugs.webkit.org/show_bug.cgi?id=185924
|
|
|
|
// Until this is fixed, Safari needs to be explicitly targeted for poll-based logging.
|
|
|
|
function isSafari() {
|
|
|
|
const oldSafariTest = /constructor/i.test(window.HTMLElement);
|
|
|
|
const newSafariTest = (function(p) {
|
|
|
|
return p.toString() === '[object SafariRemoteNotification]';
|
|
|
|
})(!window['safari'] || (typeof window.safari !== 'undefined' && window.safari.pushNotification));
|
|
|
|
return oldSafariTest || newSafariTest;
|
|
|
|
}
|