Skip to content

generate util functions to consume response stream #571

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

Closed

Conversation

AllanZhengYP
Copy link
Contributor

@AllanZhengYP AllanZhengYP commented Jul 12, 2022

Issue #, if available:
Generated change PR: aws/aws-sdk-js-v3#3795

Description of changes:
Now if the deserialized object has streaming blob member(SSDK's input or SDK's output), customers can tranform the stream into string by

const body = response.Body;
const str = await body.transformToString("utf-8");

Or get the binary data by:

const binaryData: Uint8Array = await resp.Body.transformToByteArray();
// using buffer
const buf: Buffer = Buffer.from(await resp.Body.transformToByteArray());

Or transform to runtime-agnostic WebStream by:

// browsers and Node.js 16.5+
const webstream: ReadableStream = resp.Body.transformToWebStream();
const data = await webstream.json();
const buf = await webstream.arrayBuffer();

The output stream payload's interface is still assignable to the stream payload shape in the input, so this will work:

  new PutObjectCommand({ Body: bucket1Response.Body, Bucket: "Bucket2" });

At the same time, the payload stream is still an instance of the runtime-specific stream implementation, i.e. Stream.Readable in Node.js, ReadableStream in browsers and Blob in React Native. So the code like below will still work:

if (resp.Body instance of ReadableStream) {
  // Handle web stream
}

For SSDK, nothing changed other than operation input shape containing streaming blob member will more strict, for example:

diff --git a/private/aws-restjson-server/src/server/operations/StreamingTraits.ts b/private/aws-restjson-server/src/server/operations/StreamingTraits.ts
index a38915aeae..8a114617c4 100644
--- a/private/aws-restjson-server/src/server/operations/StreamingTraits.ts
+++ b/private/aws-restjson-server/src/server/operations/StreamingTraits.ts
@@ -137,16 +137,7 @@ export class StreamingTraitsHandler<Context> implements __ServiceHandler<Context
 
 export type StreamingTraits<Context> = __Operation<StreamingTraitsServerInput, StreamingTraitsServerOutput, Context>;
 
-type StreamingTraitsServerInputType = Omit<StreamingTraitsInputOutput, "blob"> & {
-  /**
-   * For *`StreamingTraitsInputOutput["blob"]`*, see {@link StreamingTraitsInputOutput.blob}.
-   */
-  blob?: StreamingTraitsInputOutput["blob"] | string | Uint8Array | Buffer;
-};
-/**
- * This interface extends from `StreamingTraitsInputOutput` interface. There are more parameters than `blob` defined in {@link StreamingTraitsInputOutput}
- */
-export interface StreamingTraitsServerInput extends StreamingTraitsServerInputType {}
+export interface StreamingTraitsServerInput extends StreamingTraitsInputOutput {}
 export namespace StreamingTraitsServerInput {
   /**
    * @internal

It should be OK as the type exposed to SSDK customers are more strict.

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@AllanZhengYP AllanZhengYP force-pushed the stream-improvement-serde branch from 31f5aec to 81b5253 Compare August 29, 2022 21:38
@AllanZhengYP AllanZhengYP force-pushed the stream-improvement-serde branch from 81b5253 to 0c25e0a Compare August 29, 2022 21:42
@AllanZhengYP AllanZhengYP marked this pull request as ready for review August 29, 2022 21:45
@AllanZhengYP AllanZhengYP requested a review from a team as a code owner August 29, 2022 21:45
@AllanZhengYP AllanZhengYP requested review from gosar and kuhe August 29, 2022 23:19
@AllanZhengYP AllanZhengYP force-pushed the stream-improvement-serde branch from 19568d5 to 18d6d2e Compare August 31, 2022 22:38
@kuhe kuhe self-assigned this Sep 12, 2022
@kuhe kuhe mentioned this pull request Sep 23, 2022
@kuhe
Copy link
Contributor

kuhe commented Sep 23, 2022

continued in #593

@kuhe kuhe closed this Sep 23, 2022
@kuhe kuhe reopened this Sep 23, 2022
@kuhe kuhe closed this Sep 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants