-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Tomas Pilar <[email protected]>
- Loading branch information
Tomas Pilar
committed
Dec 15, 2023
1 parent
ac84a23
commit c14067f
Showing
4 changed files
with
136 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import { | ||
EventStreamContentType, | ||
fetchEventSource, | ||
} from '@ai-zen/node-fetch-event-source'; | ||
|
||
import { TypedReadable } from '../utils/stream.js'; | ||
import { BaseError, HttpError, InternalError } from '../errors.js'; | ||
import { safeParseJson } from '../helpers/common.js'; | ||
import { RawHeaders } from '../client.js'; | ||
|
||
export interface ApiEventClient { | ||
stream: <T>(opts: { | ||
url: string; | ||
headers?: RawHeaders; | ||
body?: any; | ||
signal?: AbortSignal; | ||
}) => TypedReadable<T>; | ||
} | ||
|
||
export function createApiEventClient(clientOptions: { | ||
baseUrl?: string; | ||
headers?: RawHeaders; | ||
}): ApiEventClient { | ||
return { | ||
stream: function fetchSSE<T>({ | ||
url, | ||
headers, | ||
body, | ||
signal, | ||
}: Parameters<ApiEventClient['stream']>[0]) { | ||
const outputStream = new TypedReadable<T>({ | ||
autoDestroy: true, | ||
objectMode: true, | ||
signal: signal, | ||
}); | ||
|
||
const onClose = () => { | ||
if (outputStream.readable) { | ||
outputStream.push(null); | ||
} | ||
}; | ||
|
||
const delegatedController = new AbortController(); | ||
if (signal) { | ||
signal.addEventListener( | ||
'abort', | ||
() => { | ||
delegatedController.abort(); | ||
}, | ||
{ | ||
once: true, | ||
}, | ||
); | ||
} | ||
|
||
const onError = (e: unknown) => { | ||
const err = | ||
e instanceof BaseError | ||
? e | ||
: new InternalError('Unexpected error', { cause: e }); | ||
|
||
delegatedController.abort(); | ||
if (outputStream.readable) { | ||
outputStream.emit('error', err); | ||
throw err; | ||
} | ||
onClose(); | ||
}; | ||
fetchEventSource(new URL(url, clientOptions.baseUrl).toString(), { | ||
method: 'POST', | ||
body: JSON.stringify(body), | ||
headers: { | ||
...clientOptions.headers, | ||
...headers, | ||
'Content-Type': 'application/json', | ||
}, | ||
signal: delegatedController.signal, | ||
onclose: onClose, | ||
async onopen(response) { | ||
const contentType = response.headers.get('content-type') || ''; | ||
|
||
if (response.ok && contentType === EventStreamContentType) { | ||
return; | ||
} | ||
|
||
const responseData = contentType.startsWith('application/json') | ||
? await response.json().catch(() => null) | ||
: null; | ||
|
||
onError(new HttpError(responseData)); | ||
}, | ||
onmessage(message) { | ||
if (message.event === 'close') { | ||
onClose(); | ||
return; | ||
} | ||
if (message.data === '') { | ||
return; | ||
} | ||
|
||
const result = safeParseJson(message.data); | ||
if (result === null) { | ||
onError( | ||
new InternalError( | ||
`Failed to parse message "${JSON.stringify(message)}"`, | ||
), | ||
); | ||
return; | ||
} | ||
|
||
outputStream.push(result); | ||
}, | ||
onerror: onError, | ||
}).catch(() => { | ||
/* Prevent uncaught exception (errors are handled inside the stream) */ | ||
}); | ||
|
||
return outputStream; | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters