From 03320d6372129cc9979077e30c71d6d68abaefdc Mon Sep 17 00:00:00 2001 From: Lucas Stephan Date: Tue, 2 Apr 2024 10:26:02 +0300 Subject: [PATCH 1/3] add: rowData property to the Stream class --- src/index.ts | 39 ++++++++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/src/index.ts b/src/index.ts index 1b17da5..bee07bd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -9,7 +9,7 @@ type Streamable = | Response | null -interface StreamOption { +type StreamOption = { /** * A string identifying the type of event described. * @@ -31,6 +31,25 @@ interface StreamOption { * attempting to reconnect. */ retry?: number + /** + * The format of the data sent throw the stream. + * + * If set to true, the data will be directly sent without + * any transformation. + * + * By default all the data are sent with an id. + */ + rowData?: false +} | { + /** + * The format of the data sent throw the stream. + * + * If set to true, the data will be directly sent without + * any transformation. + * + * By default all the data are sent with an id. + */ + rowData: true } const encoder = new TextEncoder() @@ -58,6 +77,7 @@ export class Stream { private _retry?: number private _event?: string + private _rowData?: boolean private label: string = '' private labelUint8Array = new Uint8Array() @@ -98,11 +118,16 @@ export class Stream { constructor( callback?: ((stream: Stream) => void) | MaybePromise, - { retry, event }: StreamOption = {} + streamOption: StreamOption = {} ) { - if (retry) this._retry = retry - if (event) this._event = event - if (retry || event) this.composeLabel() + if (!streamOption.rowData && streamOption.retry) + this._retry = streamOption.retry + if (!streamOption.rowData && streamOption.event) + this._event = streamOption.event + if (streamOption.rowData) + this._rowData = streamOption.rowData + if (!streamOption.rowData && (streamOption.retry || streamOption.event)) + this.composeLabel() switch (typeof callback) { case 'function': @@ -174,6 +199,10 @@ export class Stream { ? Stream.concatUintArray(this.labelUint8Array, data) : data ) + } else if (this._rowData) { + this.controller.enqueue( + encoder.encode(typeof data === 'object' ? JSON.stringify(data) : data.toString()) + ) } else this.controller.enqueue( encoder.encode( From d925b1ec76e5dcbea0967ae9b2b47c358af251cb Mon Sep 17 00:00:00 2001 From: Lucas Stephan Date: Tue, 2 Apr 2024 10:29:27 +0300 Subject: [PATCH 2/3] fix typos --- src/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index bee07bd..203210d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -32,7 +32,7 @@ type StreamOption = { */ retry?: number /** - * The format of the data sent throw the stream. + * The format of the data sent through the stream. * * If set to true, the data will be directly sent without * any transformation. @@ -42,7 +42,7 @@ type StreamOption = { rowData?: false } | { /** - * The format of the data sent throw the stream. + * The format of the data sent through the stream. * * If set to true, the data will be directly sent without * any transformation. From a4546d1f2e41377e3e236752f645c5696a87c30b Mon Sep 17 00:00:00 2001 From: Matiix310 Date: Wed, 3 Apr 2024 15:20:41 +0300 Subject: [PATCH 3/3] fix some typos --- src/index.ts | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/index.ts b/src/index.ts index 203210d..bd2f719 100644 --- a/src/index.ts +++ b/src/index.ts @@ -39,7 +39,7 @@ type StreamOption = { * * By default all the data are sent with an id. */ - rowData?: false + rawData?: false } | { /** * The format of the data sent through the stream. @@ -49,7 +49,7 @@ type StreamOption = { * * By default all the data are sent with an id. */ - rowData: true + rawData: true } const encoder = new TextEncoder() @@ -77,7 +77,7 @@ export class Stream { private _retry?: number private _event?: string - private _rowData?: boolean + private _rawData?: boolean private label: string = '' private labelUint8Array = new Uint8Array() @@ -120,13 +120,13 @@ export class Stream { callback?: ((stream: Stream) => void) | MaybePromise, streamOption: StreamOption = {} ) { - if (!streamOption.rowData && streamOption.retry) + if (!streamOption.rawData && streamOption.retry) this._retry = streamOption.retry - if (!streamOption.rowData && streamOption.event) + if (!streamOption.rawData && streamOption.event) this._event = streamOption.event - if (streamOption.rowData) - this._rowData = streamOption.rowData - if (!streamOption.rowData && (streamOption.retry || streamOption.event)) + if (streamOption.rawData) + this._rawData = streamOption.rawData + if (!streamOption.rawData && (streamOption.retry || streamOption.event)) this.composeLabel() switch (typeof callback) { @@ -199,7 +199,7 @@ export class Stream { ? Stream.concatUintArray(this.labelUint8Array, data) : data ) - } else if (this._rowData) { + } else if (this._rawData) { this.controller.enqueue( encoder.encode(typeof data === 'object' ? JSON.stringify(data) : data.toString()) )