Skip to content

Commit 39011c8

Browse files
alexforsythtrivikr
andauthored
docs(readme): highlight high level concepts (#2205)
* fix: add table of contents to top-level readme * chore: add high level concept details * chore: high level concepts go at the bottom * Update README.md Co-authored-by: Trivikram Kamat <[email protected]> * Update README.md Co-authored-by: Trivikram Kamat <[email protected]> Co-authored-by: Trivikram Kamat <[email protected]>
1 parent 7135cd6 commit 39011c8

File tree

1 file changed

+250
-1
lines changed

1 file changed

+250
-1
lines changed

README.md

+250-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,24 @@ and provides step-by-step migration instructions to v3.
2626
To test your universal JavaScript code in Node.js, browser and react-native environments,
2727
visit our [code samples repo](https://github.com/aws-samples/aws-sdk-js-tests).
2828

29-
## Getting started
29+
# Table of Contents
30+
31+
1. [Getting Started](#getting-started)
32+
1. [New Features](#new-features)
33+
1. [Modularized packages](#modularized-packages)
34+
1. [API consistency changes](#api-changes)
35+
1. [Configuration](#configuration)
36+
1. [Middleware Stack](#Middleware)
37+
1. [How to upgrade](#other-changes)
38+
1. [High Level Concepts in V3](#high-level-concepts)
39+
1. [Generated Packages](#generated-code)
40+
1. [Paginators](#paginators)
41+
1. [Abort Controller](#abort-controller)
42+
1. [Middleware Stack](#middleware-stack)
43+
1. [Install from Source](#install-from-Source)
44+
1. [Giving feedback and contributing](#giving-feedback-and-contributing)
45+
46+
## Getting Started
3047

3148
Let’s walk through setting up a project that depends on DynamoDB from the SDK and makes a simple service call. The following steps use yarn as an example. These steps assume you have Node.js and yarn already installed.
3249

@@ -215,3 +232,235 @@ track the discussion daily, so feel free to open a GitHub issue if your question
215232
You can open pull requests for fixes or additions to the new AWS SDK for JavaScript v3.
216233
All pull requests must be submitted under the Apache 2.0 license and will be reviewed by an SDK team member prior to merging.
217234
Accompanying unit tests are appreciated. See [Contributing](CONTRIBUTING.md) for more information.
235+
236+
## High Level Concepts
237+
238+
This is an introduction to some of the high level concepts behind AWS SDK for JavaScript (v3)
239+
which are shared between services and might make your life easier. Please consult the user
240+
guide and API reference for service specific details.
241+
242+
#### Terminology:
243+
244+
**Bare-bones clients/commands**: This refers to a modular way of consuming individual operations on JS SDK clients. It results in less code being imported and thus more performant. It is otherwise equivlent to the aggregated clients/commands.
245+
246+
```
247+
// this imports a bare-bones version of S3 that exposes the .send operation
248+
import { S3Client } from "@aws-sdk/client-s3"
249+
250+
// this imports just the getObject operation from S3
251+
import { GetObjectCommand } from "@aws-sdk/client-s3"
252+
253+
//usage
254+
const bareBonesS3 = new S3Client({...});
255+
await bareBonesS3.send(new GetObjectCommand({...}));
256+
```
257+
258+
**Aggregated clients/commands**: This refers to a way of consuming clients that contain all operations on them. Under the hood this calls the bare-bones commands. This imports all commands on a particular client and results in more code being imported and thus less performant. This is 1:1 with v2's style.
259+
260+
```
261+
// this imports an aggregated version of S3 that exposes the .send operation
262+
import { S3 } from "@aws-sdk/client-s3"
263+
264+
// No need to import an operation as all operations are already on the S3 prototype
265+
266+
//usage
267+
const aggregatedS3 = new S3({...});
268+
await aggregatedS3.getObject({...}));
269+
```
270+
271+
### Generated Code
272+
273+
The v3 codebase is generated from internal AWS models that AWS services expose. We use [smithy-typescript](https://github.com/awslabs/smithy-typescript) to generate all code in the `/clients` subdirectory. These packages always have a prefix of `@aws-sdk/client-XXXX` and are one-to-one with AWS services and service operations. You should be importing `@aws-sdk/client-XXXX` for most usage.
274+
275+
Clients depend on common "utility" code in `/packages`. The code in `/packages` is manually written and outside of special cases (like credenitials or abort controller) is generally not very useful alone.
276+
277+
Lastly we have higher level libraries in `/lib`. These are javascript specific libraries that wrap client operations to make them easier to work with. Popular examples are `@aws-sdk/lib-dynamodb` which [simplifies working with items in Amazon DynamoDB](https://github.com/aws/aws-sdk-js-v3/blob/main/lib/lib-dynamodb/README.md) or `@aws-sdk/lib-storage` which exposes the `Upload` function and [simplifies parallel uploads in S3's multipartUpload](https://github.com/aws/aws-sdk-js-v3/blob/main/lib/lib-storage/README.md).
278+
279+
1. `/packages`. This sub directory is where most manual code updates are done. These are published to NPM under `@aws-sdk/XXXX` and have no special prefix.
280+
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`.
281+
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`
282+
283+
### Paginators
284+
285+
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.
286+
287+
In AWS SDK for JavaScript v3 we’ve improved pagination using async generator functions, which are similar to generator functions, with the following differences:
288+
289+
- When called, async generator functions return an object, an async generator whose methods (`next`, `throw`, and `return`) return promises for { `value`, `done` }, instead of directly returning { `value`, `done` }. This automatically makes the returned async generator objects async iterators.
290+
- await expressions and `for await (x of y)` statements are allowed.
291+
- The behavior of `yield*` is modified to support delegation to async iterables.
292+
293+
The Async Iterators were added in the ES2018 iteration of JavaScript. They are supported by Node.js 10.x+ and by all modern browsers, including Chrome 63+, Firefox 57+, Safari 11.1+, and Edge 79+. If you’re using TypeScript v2.3+, you can compile Async Iterators to older versions of JavaScript.
294+
295+
An async iterator is much like an iterator, except that its `next()` method returns a promise for a { `value`, `done` } pair. As an implicit aspect of the Async Iteration protocol, the next promise is not requested until the previous one resolves. This is a simple, yet a very powerful pattern.
296+
297+
#### Example Pagination Usage
298+
299+
In v3, the clients expose paginateOperationName APIs that are written using async generators, allowing you to use async iterators in a for await..of loop. You can perform the paginateListTables operation from `@aws-sdk/client-dynamodb` as follows:
300+
301+
```
302+
const {
303+
DynamoDBClient,
304+
paginateListTables,
305+
} = require("@aws-sdk/client-dynamodb");
306+
307+
...
308+
const paginatorConfig = {
309+
client: new DynamoDBClient({}),
310+
pageSize: 25
311+
};
312+
const commandParams = {};
313+
const paginator = paginateListTables(paginatorConfig, commandParams);
314+
315+
const tableNames = [];
316+
for await (const page of paginator) {
317+
// page contains a single paginated output.
318+
tableNames.push(...page.TableNames);
319+
}
320+
...
321+
322+
```
323+
324+
Or simplified:
325+
326+
```
327+
...
328+
const client = new DynamoDBClient({});
329+
330+
const tableNames = [];
331+
for await (const page of paginateListTables({ client }, {})) {
332+
// page contains a single paginated output.
333+
tableNames.push(...page.TableNames);
334+
}
335+
...
336+
```
337+
338+
### Abort Controller
339+
340+
In v3, we support the AbortController interface which allows you to abort requests as and when desired.
341+
342+
The [AbortController Interface](https://dom.spec.whatwg.org/#interface-abortcontroller) provides an `abort()` method that toggles the state of a corresponding AbortSignal object. Most APIs accept an AbortSignal object, and respond to `abort()` by rejecting any unsettled promise with an “AbortError”.
343+
344+
```
345+
// Returns a new controller whose signal is set to a newly created AbortSignal object.
346+
const controller = new AbortController();
347+
348+
// Returns the AbortSignal object associated with controller.
349+
const signal = controller.signal;
350+
351+
// Invoking this method will set controller’s AbortSignal's aborted flag
352+
// and signal to any observers that the associated activity is to be aborted.
353+
controller.abort();
354+
```
355+
356+
#### AbortController Usage
357+
358+
In JavaScript SDK v3, we added an implementation of WHATWG AbortController interface in `@aws-sdk/abort-controller`. To use it, you need to send `AbortController.signal` as `abortSignal` in the httpOptions parameter when calling `.send()` operation on the client as follows:
359+
360+
```
361+
const { AbortController } = require("@aws-sdk/abort-controller");
362+
const { S3Client, CreateBucketCommand } = require("@aws-sdk/client-s3");
363+
364+
...
365+
366+
const abortController = new AbortController();
367+
const client = new S3Client(clientParams);
368+
369+
const requestPromise = client.send(new CreateBucketCommand(commandParams), {
370+
abortSignal: abortController.signal,
371+
});
372+
373+
// The abortController can be aborted any time.
374+
// The request will not be created if abortSignal is already aborted.
375+
// The request will be destroyed if abortSignal is aborted before response is returned.
376+
abortController.abort();
377+
378+
// This will fail with "AbortError" as abortSignal is aborted.
379+
await requestPromise;
380+
```
381+
382+
For a full pagination deep dive please check out our [blog post](https://aws.amazon.com/blogs/developer/pagination-using-async-iterators-in-modular-aws-sdk-for-javascript/).
383+
384+
#### AbortController Example
385+
386+
The following code snippet shows how to upload a file using S3's putObject API in the browser with support to abort the upload. First, create a controller using the `AbortController()` constructor, then grab a reference to its associated AbortSignal object using the AbortController.signal property. When the `PutObjectCommand` is called with `.send()` operation, pass in AbortController.signal as abortSignal in the httpOptions parameter. This will allow you to abort the PutObject operation by calling `abortController.abort()`.
387+
388+
```#JavaScript
389+
const abortController = new AbortController();
390+
const abortSignal = abortController.signal;
391+
392+
const uploadBtn = document.querySelector('.upload');
393+
const abortBtn = document.querySelector('.abort');
394+
395+
uploadBtn.addEventListener('click', uploadObject);
396+
397+
abortBtn.addEventListener('click', function() {
398+
abortController.abort();
399+
console.log('Upload aborted');
400+
});
401+
402+
const uploadObject = async (file) => {
403+
...
404+
const client = new S3Client(clientParams);
405+
try {
406+
await client.send(new PutObjectCommand(commandParams), { abortSignal });
407+
} catch(e) {
408+
if (e.name === "AbortError") {
409+
uploadProgress.textContent = 'Upload aborted: ' + e.message;
410+
}
411+
...
412+
}
413+
}
414+
```
415+
416+
For a full abort controller deep dive please check out our [blog post](https://aws.amazon.com/blogs/developer/abortcontroller-in-modular-aws-sdk-for-javascript/).
417+
418+
### Middleware Stack
419+
420+
The JavaScript SDK maintains a series of asynchronous actions. These series include actions that serialize input parameters into the data over the wire and deserialize response data into JavaScript objects. Such actions are implemented using functions called middleware and executed in a specific order. The object that hosts all the middleware including the ordering information is called a Middleware Stack. You can add your custom actions to the SDK and/or remove the default ones.
421+
422+
When an API call is made, SDK sorts the middleware according to the step it belongs to and its priority within each step. The input parameters pass through each middleware. An HTTP request gets created and updated along the process. The HTTP Handler sends a request to the service, and receives a response. A response object is passed back through the same middleware stack in reverse, and is deserialized into a JavaScript object.
423+
424+
A middleware is a higher-order function that transfers user input and/or HTTP request, then delegates to “next” middleware. It also transfers the result from “next” middleware. A middleware function also has access to context parameter, which optionally contains data to be shared across middleware.
425+
426+
For example, you can use middleware to add a custom header like S3 object metadata:
427+
428+
```
429+
const { S3 } = require("@aws-sdk/client-s3");
430+
const client = new S3({ region: "us-west-2" });
431+
// Middleware added to client, applies to all commands.
432+
client.middlewareStack.add(
433+
(next, context) => async (args) => {
434+
args.request.headers["x-amz-meta-foo"] = "bar";
435+
const result = next(args);
436+
// result.response contains data returned from next middleware.
437+
return result;
438+
},
439+
{
440+
step: "build",
441+
name: "addFooMetadataMiddleware",
442+
tags: ["METADATA", "FOO"],
443+
}
444+
);
445+
446+
await client.putObject(params);
447+
```
448+
449+
Specifying the absolute location of your middleware
450+
The example above adds middleware to `build` step of middleware stack. The middleware stack contains five steps to manage a request’s lifecycle:
451+
452+
- The **initialize** lifecycle step initializes an API call. This step typically adds default input values to a command. The HTTP request has not yet been constructed.
453+
- The **serialize** lifecycle step constructs an HTTP request for the API call. Example of typical serialization tasks include input validation and building an HTTP request from user input. The downstream middleware will have access to serialized HTTP request object in callback’s parameter `args.request`.
454+
- The **build** lifecycle step builds on top of serialized HTTP request. Examples of typical build tasks include injecting HTTP headers that describe a stable aspect of the request, such as `Content-Length` or a body checksum. Any request alterations will be applied to all retries.
455+
- The **finalizeRequest** lifecycle step prepares the request to be sent over the wire. The request in this stage is semantically complete and should therefore only be altered to match the recipient’s expectations. Examples of typical finalization tasks include request signing, performing retries and injecting hop-by-hop headers.
456+
- The **deserialize** lifecycle step deserializes the raw response object to a structured response. The upstream middleware have access to deserialized data in next callbacks return value: `result.output`.
457+
Each middleware must be added to a specific step. By default each middleware in the same step has undifferentiated order. In some cases, you might want to execute a middleware before or after another middleware in the same step. You can achieve it by specifying its `priority`.
458+
459+
```
460+
client.middlewareStack.add(middleware, {
461+
step: "initialize",
462+
priority: "high", // or "low".
463+
});
464+
```
465+
466+
For a full middleware stack deep dive please check out our [blog post](https://aws.amazon.com/blogs/developer/middleware-stack-modular-aws-sdk-js/).

0 commit comments

Comments
 (0)