Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(fix, typescript): respect stream terminator #3429

Merged
merged 1 commit into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions generators/typescript/sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,20 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.15.0-rc1] - 2024-04-22

- Fix: Minor fixes to SSE processing. In particular, stream terminal characters are now
respected like `[DONE]` and JSON parsed data is sent to the deserialize function.

## [0.15.0-rc0] - 2024-04-19

- Feature: Bump to v38 of IR and support server-sent events where the events are sent
with a `data: ` prefix and terminated with a new line.
- Feature: Bump to v38 of IR and support server-sent events where the events are sent
with a `data: ` prefix and terminated with a new line.

## [0.14.1-rc5] - 2024-04-17

- Fix: Code snippets are generated for file upload endpoints using `fs.readStream`. Previously,
generation for these endpoints was being skipped.
- Fix: Code snippets are generated for file upload endpoints using `fs.readStream`. Previously,
generation for these endpoints was being skipped.

- Fix: If integration tests are not enabled, simple jest tests with a `yarn test`
script will be created.
Expand Down
2 changes: 1 addition & 1 deletion generators/typescript/sdk/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.15.0-rc0
0.15.0-rc1
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,10 @@ export class GeneratedThrowingEndpointResponse implements GeneratedEndpointRespo
const eventShape = this.response.value._visit<
StreamingFetcher.MessageEventShape | StreamingFetcher.SSEEventShape
>({
sse: () => ({ type: "sse" }),
sse: (sse) => ({
type: "sse",
streamTerminator: ts.factory.createStringLiteral(sse.terminator ?? "[DONE]")
}),
json: (json) => ({
type: "json",
messageTerminator: ts.factory.createStringLiteral(json.terminator ?? "\n")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ export class StreamingUtilsImpl extends CoreUtility implements StreamUtils {
ts.factory.createStringLiteral("sse")
)
);
if (eventShape.streamTerminator != null) {
eventShapeProperties.push(
ts.factory.createPropertyAssignment(
ts.factory.createIdentifier("streamTerminator"),
eventShape.streamTerminator ?? ts.factory.createStringLiteral("\n")
)
);
}
} else if (eventShape.type === "json") {
eventShapeProperties.push(
ts.factory.createPropertyAssignment(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,15 @@ export class Stream<T> implements AsyncIterable<T> {
*/
private prefix: string | undefined;
private messageTerminator: string;
private streamTerminator: string | undefined;

constructor({ stream, parse, eventShape }: Stream.Args & { parse: (val: unknown) => Promise<T> }) {
this.stream = stream;
this.parse = parse;
if (eventShape.type === "sse") {
this.prefix = DATA_PREFIX;
this.messageTerminator = "\n";
this.streamTerminator = eventShape.streamTerminator;
} else {
this.messageTerminator = eventShape.messageTerminator;
}
Expand All @@ -66,10 +68,13 @@ export class Stream<T> implements AsyncIterable<T> {
prefixSeen = true;
buf = buf.slice(prefixIndex + this.prefix.length);
}
// Yield message from the prefix to the terminator
const line = buf.slice(0, terminatorIndex).trimEnd();
console.log(line);
const message = await this.parse(line);
// If the stream terminator is present, return
if (this.streamTerminator != null && line.includes(this.streamTerminator)) {
return;
}
// Otherwise, yield message from the prefix to the terminator
const message = await this.parse(JSON.parse(line));
yield message;
buf = buf.slice(terminatorIndex + 1);
prefixSeen = false;
Expand Down Expand Up @@ -129,6 +134,6 @@ export function readableStreamAsyncIterable<T>(stream: any): AsyncIterableIterat
},
[Symbol.asyncIterator]() {
return this;
}
},
};
}
Empty file.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading