@aws-sdk/xhr-http-handler

中文文档
3.1164.0发布于 2026/11/07Apache-2.0TypeScriptESM + CJS近 30 天下载 9.5万

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

中文使用文档
文档目录4 节
  1. 警告:警告:
  2. 圆柱体
  3. 使用大小写: XMLHttp 请上传进度事件
  4. 使用大小写: XMLHttp 请求下载进度和其他事件

翻译:

NPM version NPM downloads

这个 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。 。 。 .

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。 。 。 .

下载进度
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 检查或检查对象 附加附加事件听众.

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 共发布 317 个版本,首次发布于 2022/07/16;过去 12 个月发布了 194 个版本。

版本发布日期距今
3.1164.0latest2026/11/07今天
3.1163.02026/11/061 天前
3.1162.02026/11/052 天前
3.1161.02026/10/318 天前
3.1160.02026/10/309 天前
3.1159.02026/10/2910 天前

关于 @aws-sdk/xhr-http-handler 的常见问题

@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 和提交记录判断。