# @aws-sdk/xhr-http-handler 中文文档

规范地址：https://npmcn.com/package/%40aws-sdk/xhr-http-handler/

版本：3\.1164\.0
npm 元数据更新时间：2026-11-06T20:14:27.845Z
中文译文更新时间：2026-11-07T12:21:34.720Z
软件包许可：Apache-2\.0

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40aws-sdk/xhr-http-handler
原文对照：https://npmcn.com/package/%40aws-sdk/xhr-http-handler/?lang=original
项目主页：https://github.com/aws/aws-sdk-js-v3/tree/main/packages/xhr-http-handler
源码仓库：https://github.com/aws/aws-sdk-js-v3

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：39.3 kB，13 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：包内自带
- 运行依赖：5 个
- 首次发布：2022-07-15T19:21:01.759Z
- 最新版本发布：2026-11-06T20:14:27.694Z
- 近 30 天下载量：94529（2026-10-08 至 2026-11-06，npm 官方下载统计）
- 已发布版本数：317（近 12 个月 194 个）

## 中文简介

提供使用 XMLHttp 请求请求的方式

## 安装

```sh
npm install @aws-sdk/xhr-http-handler
```

也可以使用：`pnpm add @aws-sdk/xhr-http-handler`、`yarn add @aws-sdk/xhr-http-handler`、`bun add @aws-sdk/xhr-http-handler`

## README

# 翻译:

[![NPM version](https://img.shields.io/npm/v/@aws-sdk/xhr-http-handler/latest.svg)](https://www.npmjs.com/package/@aws-sdk/xhr-http-handler) [![NPM downloads](https://img.shields.io/npm/dm/@aws-sdk/xhr-http-handler.svg)](https://www.npmjs.com/package/@aws-sdk/xhr-http-handler)

这个 `HttpHandler` 是基于 `XMLHttpRequest` 并可以替换,如果 需要特定用途 `fetch`。 。 。 .

## 警告:警告:

建议 `HttpHandler` 对于类似浏览器的环境是 `@smithy/fetch-http-handler`, (中文(简体) ), 这是默认. 此替代品仅经过测试 `S3` 在浏览器中.

## 圆柱体

本套件可访问以下全球范围的实施:

-   `XMLHttpRequest`
-   `TextEncoder`
-   `TransformStream`
-   `Blob`

您必须提供多填充, 例如: `TextEncoder` 财务报告和审定财务报表 `TransformStream`,用于环境 不以当地方式执行.

### 使用大小写: XMLHttp 请上传进度事件

使用 `Upload` 类从 `@aws-sdk/lib-storage` 软件包为普通,但提供不同的 `HttpHandler` 创建时 `S3Client` 或者说 `S3` 对象.

另见: [自由储存/README.md](https://github.com/aws/aws-sdk-js-v3/blob/main/lib/lib-storage/README.md)。 。 。 .

```javascript
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();
```

### 使用大小写: XMLHttp 请求下载进度和其他事件

`XhrHttpHandler` 扩展 `EventEmitter`。 。 。 .

##### 下载进度

```javascript
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(/*...*/));
```

##### 访问 `XMLHttpRequest` 对象.

您可以访问 `XMLHttpRequest` 检查或检查对象 附加附加事件听众.

```javascript
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(/*...*/));
```

你可以检查来源 `.ts` 文件或已发布 `.d.ts` 文件 或检查 `XhrHttpHandler.EVENTS` 运行时的对象.

## 常见问题

### @aws-sdk/xhr-http-handler 是什么？

提供使用 XMLHttp 请求请求的方式

### 如何安装 @aws-sdk/xhr-http-handler？

在项目目录运行 npm install @aws-sdk/xhr-http-handler，或使用 pnpm add / yarn add / bun add。

### @aws-sdk/xhr-http-handler 支持 TypeScript 吗？

支持。包内自带 TypeScript 类型声明（package\.json 声明了 types 入口），安装后即可获得类型提示，无需再安装 @types 包。

### @aws-sdk/xhr-http-handler 支持 ESM 还是 CommonJS？

同时提供 ES Module 与 CommonJS 入口，import 和 require\(\) 都可以使用，打包工具会自动选择合适的版本。

### @aws-sdk/xhr-http-handler 可以商用吗？

@aws-sdk/xhr-http-handler 使用 Apache-2\.0 许可。宽松许可：允许商业使用、修改和再分发，并附带明确的专利授权；分发时需保留许可与 NOTICE 文件，修改过的文件要注明改动。以上为概括，不构成法律意见，请以许可原文为准。

### @aws-sdk/xhr-http-handler 的下载量有多少？

根据 npm 官方下载统计，@aws-sdk/xhr-http-handler 在 2026/10/08 – 2026/11/06 这 30 天内被下载 94,529 次，日均约 3,151 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @aws-sdk/xhr-http-handler 还在维护吗？

最新版本 3\.1164\.0 发布于 2026/11/07（今天）；至今共发布 317 个版本，其中过去 12 个月发布了 194 个；首次发布于 2022/07/16。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

