@@ -20,6 +20,7 @@ import {
2020 searchFormatPart ,
2121} from "../Utils/utils" ;
2222import websocket from "../Utils/websocket" ;
23+ import eventsource from "../Utils/eventsource" ;
2324import styles from "./index.module.css" ;
2425
2526export interface WebsocketOptions {
@@ -36,7 +37,7 @@ export interface WebsocketOptions {
3637 */
3738 onError ?: ( ( e : Event ) => void ) | undefined ;
3839 /**
39- * Callback allback which formats the websocket data stream.
40+ * Callback which formats the websocket data stream.
4041 */
4142 formatMessage ?: ( ( message : any ) => string ) | undefined ;
4243 /**
@@ -50,6 +51,38 @@ export interface WebsocketOptions {
5051 reconnectWait ?: number ;
5152}
5253
54+ export interface EventSourceOptions {
55+ /**
56+ * Boolean indicating if CORS should be set to include credentials
57+ */
58+ withCredentials ?: boolean ;
59+ /**
60+ * Callback when the eventsource is opened
61+ */
62+ onOpen ?: ( ( e : Event , eventSource : EventSource ) => void ) | undefined ;
63+ /**
64+ * Callback when the eventsource is closed
65+ */
66+ onClose ?: ( ( e : Event ) => void ) | undefined ;
67+ /**
68+ * Callback when the eventsource has an error
69+ */
70+ onError ?: ( ( e : Event ) => void ) | undefined ;
71+ /**
72+ * Callback which formats the eventsource data stream.
73+ */
74+ formatMessage ?: ( ( message : any ) => string ) | undefined ;
75+ /**
76+ * Set to true, to reconnect the EventSource automatically.
77+ */
78+ reconnect ?: boolean ;
79+ /**
80+ * Set the time to wait between reconnects in seconds.
81+ * Default is 1s
82+ */
83+ reconnectWait ?: number ;
84+ }
85+
5386export interface ErrorStatus extends Error {
5487 /**
5588 * Status code
@@ -262,6 +295,15 @@ export interface LazyLogProps {
262295 * Options object which will be passed through to websocket.
263296 */
264297 websocketOptions ?: WebsocketOptions ;
298+ /**
299+ * Set to `true` to specify that url is an eventsource URL.
300+ * Defaults to `false` to download data until completion.
301+ */
302+ eventsource ?: boolean ;
303+ /**
304+ * Options object which will be passed through to evensource.
305+ */
306+ eventsourceOptions ?: EventSourceOptions ;
265307 /**
266308 * Set the width in pixels for the component.
267309 * Defaults to `'auto'` if unspecified.
@@ -331,6 +373,8 @@ export default class LazyLog extends Component<LazyLogProps, LazyLogState> {
331373 style : { } ,
332374 websocket : false ,
333375 websocketOptions : { } ,
376+ eventsource : false ,
377+ eventsourceOptions : { } ,
334378 width : "auto" ,
335379 } ;
336380
@@ -477,15 +521,21 @@ export default class LazyLog extends Component<LazyLogProps, LazyLogState> {
477521 const {
478522 stream : isStream ,
479523 websocket : isWebsocket ,
524+ eventsource : isEventsource ,
480525 url,
481526 fetchOptions,
482527 websocketOptions,
528+ eventsourceOptions,
483529 } = this . props ;
484530
485531 if ( isWebsocket ) {
486532 return websocket ( url ! , websocketOptions ! ) ;
487533 }
488534
535+ if ( isEventsource ) {
536+ return eventsource ( url ! , eventsourceOptions ! ) ;
537+ }
538+
489539 if ( isStream ) {
490540 return stream ( url ! , fetchOptions ) ;
491541 }
@@ -529,10 +579,10 @@ export default class LazyLog extends Component<LazyLogProps, LazyLogState> {
529579
530580 handleUpdate = ( { lines : moreLines , encodedLog } : any ) => {
531581 this . encodedLog = encodedLog ;
532- const { scrollToLine, follow, stream, websocket } = this . props ;
582+ const { scrollToLine, follow, stream, websocket, eventsource } = this . props ;
533583
534- // handle stream and socket updates batched update mode
535- if ( stream || websocket ) {
584+ // handle stream, socket and eventsource updates batched update mode
585+ if ( stream || websocket || eventsource ) {
536586 this . setState ( ( state , props ) => {
537587 const { scrollToLine, follow } = props ;
538588 const { count : previousCount } = state ;
@@ -736,9 +786,9 @@ export default class LazyLog extends Component<LazyLogProps, LazyLogState> {
736786
737787 handleSearch = ( keywords : string | undefined ) => {
738788 const { resultLines, searchKeywords } = this . state ;
739- const { caseInsensitive, stream, websocket } = this . props ;
789+ const { caseInsensitive, stream, websocket, eventsource } = this . props ;
740790 const currentResultLines =
741- ! stream && ! websocket && keywords === searchKeywords
791+ ! stream && ! websocket && ! eventsource && keywords === searchKeywords
742792 ? resultLines
743793 : searchLines ( keywords , this . encodedLog ! , caseInsensitive ! ) ;
744794
0 commit comments