3.679.0 • Published 8 months ago

@aws-sdk/xhr-http-handler v3.679.0

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
8 months ago

@aws-sdk/xhr-http-handler

NPM version NPM downloads

This HttpHandler is based on XMLHttpRequest and can be substituted if requiring a specific use case not covered by fetch.

Warning :warning:

The recommended HttpHandler for browser-like environments is @smithy/fetch-http-handler, which is the default. This alternative has only been tested against S3 in browsers.

Polyfills

The following global-scope implementations are accessed by this package:

  • XMLHttpRequest
  • TextEncoder
  • TransformStream
  • Blob

You will have to supply polyfills, for example for TextEncoder and TransformStream, for environments that do not implement them natively.

Use case: XMLHttpRequest upload progress events

Use the Upload class from the @aws-sdk/lib-storage package as normal, except supplying a different HttpHandler when creating the S3Client or S3 object(s).

See also: lib-storage/README.md.

import { XhrHttpHandler } from "@aws-sdk/xhr-http-handler";
import { S3Client } from "@aws-sdk/client-s3";
import { Upload } from "@aws-sdk/lib-storage";

const client = new S3Client({
  requestHandler: new XhrHttpHandler({}), // overrides default FetchHttpHandler in browsers.
});

const upload = new Upload({
  client,
  params: {
    /* ... */
  },
});

upload.on("httpUploadProgress", (progress) => {
  // Note, this event will be emitted much more frequently when using the XhrHttpHandler.
  // Your application should be ready to throttle the event listener if it is
  // computationally expensive.

  // The default FetchHttpHandler only emits this event upon the completion of each
  // part, a minimum of 5 MB. Using XHR will emit this event continuously, including
  // for files smaller than the chunk size, which use single-part upload.
  console.log(progress);

  console.log(
    progress.loaded, // Bytes uploaded so far.
    progress.total // Total bytes. Divide these two for progress percentage.
  );
});

const completeMultiPartUpload = await upload.done();

Use case: XMLHttpRequest download progress and other events

XhrHttpHandler extends EventEmitter.

Download progress
import { XhrHttpHandler } from "@aws-sdk/xhr-http-handler";
import { S3Client, GetObjectCommand } from "@aws-sdk/client-s3";

const handler = new XhrHttpHandler({});

handler.on(XhrHttpHandler.EVENTS.PROGRESS, (progress, request) => {
  if (progress.lengthComputable) {
    console.log(
      progress.loaded, // bytes
      progress.total // bytes
    );
    console.log(
      request // contains the request information to differentiate
      // requests from the same handler.
    );
  }
});

const client = new S3Client({
  requestHandler: handler,
});

await client.send(new GetObjectCommand(/*...*/));
Accessing the XMLHttpRequest object.

You can access the XMLHttpRequest object to inspect it or to attach addiional event listeners.

import { XhrHttpHandler } from "@aws-sdk/xhr-http-handler";
import { S3Client, GetObjectCommand } from "@aws-sdk/client-s3";

const handler = new XhrHttpHandler({});

handler.on(XhrHttpHandler.EVENTS.XHR_INSTANTIATED, (xhr) => {
  // a new XMLHttpRequest is created for each command sent.
  // this is immediately after instantiation.
});

handler.on(XhrHttpHandler.EVENTS.BEFORE_XHR_SEND, (xhr) => {
  // a new XMLHttpRequest is created for each command sent.
  // this is immediately before calling `xhr.send(body)`.
});

const client = new S3Client({
  requestHandler: handler,
});

await client.send(new GetObjectCommand(/*...*/));

You can check the source .ts file or published .d.ts file for the full list of events, or inspect the XhrHttpHandler.EVENTS object at runtime.

3.679.0

8 months ago

3.664.0

8 months ago

3.667.0

8 months ago

3.662.0

8 months ago

3.654.0

9 months ago

3.649.0

9 months ago

3.620.0

11 months ago

3.598.0

12 months ago

3.609.0

12 months ago

3.616.0

11 months ago

3.577.0

1 year ago

3.575.0

1 year ago

3.572.0

1 year ago

3.567.0

1 year ago

3.535.0

1 year ago

3.533.0

1 year ago

3.523.0

1 year ago

3.521.0

1 year ago

3.515.0

1 year ago

3.511.0

1 year ago

3.502.0

1 year ago

3.496.0

1 year ago

3.495.0

1 year ago

3.489.0

1 year ago

3.485.0

1 year ago

3.468.0

2 years ago

3.460.0

2 years ago

3.465.0

2 years ago

3.451.0

2 years ago

3.449.0

2 years ago

3.387.0

2 years ago

3.378.0

2 years ago

3.370.0

2 years ago

3.391.0

2 years ago

3.408.0

2 years ago

3.413.0

2 years ago

3.369.0

2 years ago

3.363.0

2 years ago

3.418.0

2 years ago

3.425.0

2 years ago

3.398.0

2 years ago

3.375.0

2 years ago

3.428.0

2 years ago

3.433.0

2 years ago

3.410.0

2 years ago

3.347.0

2 years ago

3.353.0

2 years ago

3.357.0

2 years ago

3.342.0

2 years ago

3.341.0

2 years ago

3.338.0

2 years ago

3.329.0

2 years ago

3.337.0

2 years ago

3.310.0

2 years ago

3.306.0

2 years ago

3.303.0

2 years ago

3.296.0

2 years ago

3.295.0

2 years ago

3.290.0

2 years ago

3.292.0

2 years ago

3.271.0

2 years ago

3.272.0

2 years ago

3.274.0

2 years ago

3.266.0

2 years ago

3.289.0

2 years ago

3.267.0

2 years ago

3.266.1

2 years ago

3.282.0

2 years ago

3.254.0

2 years ago

3.257.0

2 years ago

3.249.0

2 years ago

3.197.0

3 years ago

3.198.0

3 years ago

3.193.0

3 years ago

3.208.0

3 years ago

3.199.0

3 years ago

3.212.0

3 years ago

3.215.0

3 years ago

3.190.0

3 years ago

3.188.0

3 years ago

3.224.0

3 years ago

3.204.0

3 years ago

3.222.0

3 years ago

3.200.0

3 years ago

3.226.0

3 years ago

3.201.0

3 years ago

3.186.0

3 years ago

3.183.0

3 years ago

3.162.0

3 years ago

3.171.0

3 years ago

3.160.0

3 years ago

3.170.0

3 years ago

3.159.0

3 years ago

3.168.0

3 years ago

3.178.0

3 years ago

3.131.0

3 years ago