Skip to content

Commit 7434604

Browse files
kuhetrivikr
andauthored
docs(streams): add readme section about stream mixins (#4036)
Co-authored-by: Trivikram Kamat <[email protected]>
1 parent e455ea5 commit 7434604

File tree

2 files changed

+45
-6
lines changed

2 files changed

+45
-6
lines changed

README.md

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
![Build Status](https://codebuild.us-west-2.amazonaws.com/badges?uuid=eyJlbmNyeXB0ZWREYXRhIjoiMmtFajZWQmNUbEhidnBKN1VncjRrNVI3d0JUcFpGWUd3STh4T3N3Rnljc1BMaEIrYm9HU2t4YTV1RlE1YmlnUG9XM3luY0Ftc2tBc0xTeVFJMkVOa24wPSIsIml2UGFyYW1ldGVyU3BlYyI6IlBDMDl6UEROK1dlU1h1OWciLCJtYXRlcmlhbFNldFNlcmlhbCI6MX0%3D&branch=main)
44
[![codecov](https://codecov.io/gh/aws/aws-sdk-js-v3/branch/main/graph/badge.svg)](https://codecov.io/gh/aws/aws-sdk-js-v3)
55
[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
6-
[![Dependabot Status](https://api.dependabot.com/badges/status?host=github&repo=aws/aws-sdk-js-v3)](https://dependabot.com)
76

87
The **AWS SDK for JavaScript v3** is a rewrite of v2 with some great new features.
98
As with version 2, it enables you to easily work with [Amazon Web Services](https://aws.amazon.com/),
@@ -37,6 +36,7 @@ visit our [code samples repo](https://github.com/aws-samples/aws-sdk-js-tests).
3736
1. [How to upgrade](#other-changes)
3837
1. [High Level Concepts in V3](#high-level-concepts)
3938
1. [Generated Packages](#generated-code)
39+
1. [Streams](#streams)
4040
1. [Paginators](#paginators)
4141
1. [Abort Controller](#abort-controller)
4242
1. [Middleware Stack](#middleware-stack)
@@ -282,6 +282,46 @@ Lastly we have higher level libraries in `/lib`. These are javascript specific l
282282
1. `/clients`. This sub directory is code generated and depends on code published from `/packages` . It is 1:1 with AWS services and operations. Manual edits should generally not occur here. These are published to NPM under `@aws-sdk/client-XXXX`.
283283
1. `/lib`. This sub directory depends on generated code published from `/clients`. It wraps existing AWS services and operations to make them easier to work with in Javascript. These are published to NPM under `@aws-sdk/lib-XXXX`
284284

285+
### Streams
286+
287+
Certain command outputs include streams, which have different implementations in
288+
Node.js and browsers. For convenience, a set of stream handling methods will be
289+
merged (`Object.assign`) to the output stream object, as defined in
290+
[SdkStreamMixin](serde-code-url).
291+
292+
Output types having this feature will be indicated by the `WithSdkStreamMixin<T, StreamKey>`
293+
[wrapper type](serde-code-url), where `T` is the original output type
294+
and `StreamKey` is the output property key having a stream type specific to
295+
the runtime environment.
296+
297+
[serde-code-url]: https://github.com/aws/aws-sdk-js-v3/blob/main/packages/types/src/serde.ts
298+
299+
Here is an example using `S3::GetObject`.
300+
301+
```js
302+
import { S3 } from "@aws-sdk/client-s3";
303+
304+
const client = new S3({});
305+
306+
const getObjectResult = await client.getObject({
307+
Bucket: "...",
308+
Key: "...",
309+
});
310+
311+
// env-specific stream with added mixin methods.
312+
const bodyStream = getObjectResult.Body;
313+
314+
// one-time transform.
315+
const bodyAsString = await bodyStream.transformToString();
316+
317+
// throws an error on 2nd call, stream cannot be rewound.
318+
const __error__ = await bodyStream.transformToString();
319+
```
320+
321+
Note that these methods will read the stream in order to collect it,
322+
so **you must save the output**. The methods cannot be called more than once
323+
on a stream.
324+
285325
### Paginators
286326

287327
Many AWS operations return paginated results when the response object is too large to return in a single response. In AWS SDK for JavaScript v2, the response contains a token you can use to retrieve the next page of results. You then need to write additional functions to process pages of results.

packages/types/src/serde.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,12 @@ export type SdkStream<BaseStream> = BaseStream & SdkStreamMixin;
8888

8989
/**
9090
* Indicates that any members of type T
91-
* that were of type BaseStream have been extended
91+
* that were of type T[StreamKey] have been extended
9292
* with the SdkStreamMixin helper methods.
9393
*/
94-
export type WithSdkStreamMixin<T, BaseStream> = {
95-
[key in keyof T]: T[key] extends BaseStream ? SdkStream<BaseStream> : T[key]
96-
}
94+
export type WithSdkStreamMixin<T, StreamKey extends keyof T> = {
95+
[key in keyof T]: T[key] extends T[StreamKey] ? SdkStream<T[StreamKey]> : T[key];
96+
};
9797

9898
/**
9999
* Interface for internal function to inject stream utility functions
@@ -111,4 +111,3 @@ export interface SdkStreamMixinInjector {
111111
export interface SdkStreamSerdeContext {
112112
sdkStreamMixin: SdkStreamMixinInjector;
113113
}
114-

0 commit comments

Comments
 (0)