Skip to content

Commit

Permalink
feat: drop unnecessary environment abstractions
Browse files Browse the repository at this point in the history
  • Loading branch information
rexxars committed Sep 20, 2023
1 parent 2583570 commit f7d4fe5
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 57 deletions.
11 changes: 1 addition & 10 deletions src/abstractions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type {ReadableStream as NodeWebReadableStream} from 'node:stream/web'
import type {EventSourceMessage} from './types'

/**
Expand All @@ -8,15 +7,7 @@ import type {EventSourceMessage} from './types'
* @internal
*/
export interface EnvAbstractions {
getStream(
body: NodeJS.ReadableStream | NodeWebReadableStream<Uint8Array>
): NodeWebReadableStream<Uint8Array>
getStream(body: ReadableStream<Uint8Array>): ReadableStream<Uint8Array>
getStream(
body: NodeJS.ReadableStream | NodeWebReadableStream<Uint8Array> | ReadableStream<Uint8Array>
): NodeWebReadableStream<Uint8Array> | ReadableStream<Uint8Array>

getTextDecoderStream(encoding: 'utf-8'): TextDecoderStream
getStream(body: NodeJS.ReadableStream | ReadableStream<Uint8Array>): ReadableStream<Uint8Array>
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const noop = () => {
*/
export function createEventSource(
options: EventSourceOptions,
{getStream, getTextDecoderStream}: EnvAbstractions
{getStream}: EnvAbstractions
): EventSourceClient {
const {onMessage, onConnect = noop, onDisconnect = noop} = options
const {fetch, url, initialLastEventId} = validate(options)
Expand Down Expand Up @@ -184,10 +184,10 @@ export function createEventSource(

// Ensure that the response stream is a web stream
// @todo Figure out a way to make this work without casting
const bodyStream = getStream(body) as ReadableStream<Uint8Array>
const bodyStream = getStream(body as any)

// EventSources are always UTF-8 per spec
const stream = bodyStream.pipeThrough<string>(getTextDecoderStream('utf-8'))
const stream = bodyStream.pipeThrough<string>(new TextDecoderStream('utf-8'))
const reader = stream.getReader()
let open = true

Expand Down
21 changes: 2 additions & 19 deletions src/default.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type {ReadableStream as NodeWebReadableStream} from 'node:stream/web'
import type {EnvAbstractions} from './abstractions'
import type {EventSourceClient, EventSourceOptions} from './types'
import {createEventSource as createSource} from './client'
Expand All @@ -11,7 +10,6 @@ export * from './constants'
*/
const defaultAbstractions: EnvAbstractions = {
getStream,
getTextDecoderStream,
}

/**
Expand All @@ -34,26 +32,11 @@ export function createEventSource(options: EventSourceOptions): EventSourceClien
* @private
*/
function getStream(
body: NodeJS.ReadableStream | NodeWebReadableStream<Uint8Array>
): NodeWebReadableStream<Uint8Array>
function getStream(body: ReadableStream<Uint8Array>): ReadableStream<Uint8Array>
function getStream(
body: NodeJS.ReadableStream | NodeWebReadableStream<Uint8Array> | ReadableStream<Uint8Array>
): NodeWebReadableStream<Uint8Array> | ReadableStream<Uint8Array> {
body: NodeJS.ReadableStream | ReadableStream<Uint8Array>
): ReadableStream<Uint8Array> {
if (!(body instanceof ReadableStream)) {
throw new Error('Invalid stream, expected a web ReadableStream')
}

return body
}

/**
* Returns a `TextDecoderStream` instance from the web streams API
*
* @param encoding - Should always be 'utf-8' (per eventsource spec)
* @returns A TextDecoderStream instance
* @private
*/
function getTextDecoderStream(encoding: 'utf-8'): TextDecoderStream {
return new TextDecoderStream(encoding)
}
29 changes: 4 additions & 25 deletions src/node.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import {Readable} from 'node:stream'
import {
TextDecoderStream as NodeWebTextDecoderStream,
ReadableStream as NodeWebReadableStream,
} from 'node:stream/web'

import {createEventSource as createSource} from './client'
import type {EventSourceClient, EventSourceOptions} from './types'
import type {EnvAbstractions} from './abstractions'
Expand All @@ -13,7 +8,6 @@ export * from './constants'

const nodeAbstractions: EnvAbstractions = {
getStream,
getTextDecoderStream,
}

/**
Expand All @@ -36,12 +30,8 @@ export function createEventSource(options: EventSourceOptions): EventSourceClien
* @private
*/
function getStream(
body: NodeJS.ReadableStream | NodeWebReadableStream<Uint8Array>
): NodeWebReadableStream<Uint8Array>
function getStream(body: ReadableStream<Uint8Array>): ReadableStream<Uint8Array>
function getStream(
body: NodeJS.ReadableStream | NodeWebReadableStream<Uint8Array> | ReadableStream<Uint8Array>
): NodeWebReadableStream<Uint8Array> | ReadableStream<Uint8Array> {
body: NodeJS.ReadableStream | ReadableStream<Uint8Array>
): ReadableStream<Uint8Array> {
if ('getReader' in body) {
// Already a web stream
return body
Expand All @@ -56,17 +46,6 @@ function getStream(
throw new Error('Node.js 18 or higher required (`Readable.toWeb()` not defined)')
}

return Readable.toWeb(Readable.from(body))
}

/**
* Returns a `TextDecoderStream` instance from the web streams API
*
* @param encoding - Should always be 'utf-8' (per eventsource spec)
* @returns A TextDecoderStream instance
* @private
*/
function getTextDecoderStream(encoding: 'utf-8'): TextDecoderStream {
// @todo See if there is any way around the casting here
return new NodeWebTextDecoderStream(encoding) as unknown as TextDecoderStream
// @todo Figure out if we can prevent casting
return Readable.toWeb(Readable.from(body)) as ReadableStream<Uint8Array>
}

0 comments on commit f7d4fe5

Please sign in to comment.