@fastify/throttle

中文文档
3.0.4发布于 2026/10/09MITTypeScriptCommonJS构建来源已验证近 30 天下载 2832

固定的喉咙反应

中文使用文档
文档目录4 节
  1. 安装
  2. 使用量
  3. 选项
  4. 许可证

@ fastify/ throttle (法语)

CI NPM version neostandard javascript style

调高请求的下载速度 .

安装

npm i @fastify/throttle

使用量

注册插件,并在必要时通过自定义选项.

此插件将添加一个 onSend 钩到快取实例,它会拖动响应的下载速度.

import Fastify from 'fastify'

const fastify = Fastify()
await fastify.register(import('@fastify/throttle'), {
  bytesPerSecond: 1024 * 1024, // 1MB/s
  streamPayloads: true, // throttle the payload if it is a stream
  bufferPayloads: true, // throttle the payload if it is a Buffer
  stringPayloads: true // throttle the payload if it is a string
})

fastify.get('/', (request, reply) => {
  reply.send({ hello: 'world' })
})

fastify.listen({ port: 3000 }, err => {
  if (err) {
    throw err
  }
  console.log('Server listening at http://localhost:3000')
})

选项

在插件注册期间, 您可以通过以下选项 :

await fastify.register(import('@fastify/throttle'), {
  bytesPerSecond: 1000, // 1000 bytes per second
  streamPayloads: true, // throttle the payload if it is a stream
  bufferPayloads: true, // throttle the payload if it is a Buffer
  stringPayloads: true // throttle the payload if it is a string
})

您可以将节流全球定义为插件选项或每个路由选项. 每个路由的节流阀选项与插件选项相同.

页眉说明默认
bytesPerSecond每秒允许的字节、 数字或函数16384 (英语)
streamPayloads如果有效载荷是溪流的话,就把它压住真实
bufferPayloads如果有效载荷是缓冲器,则将其吸出虚假
stringPayloads如果有效载荷是绳子, 则按下虚假
async如果字节Persecond是返回承诺的函数, 设置为 true虚假

在全球范围设置节奏的实例:

  const fastify = require('fastify')()

  await fastify.register(import('@fastify/throttle'), {
    bytesPerSecond: 1024 // 1KB/s
  })

  fastify.get('/', (req, reply) => {
    reply.send(createReadStream(resolve(__dirname, __filename)))
  })

  fastify.listen({ port: 3000 })

设置每条路线节奏的示例:

  'use strict'

  const fastify = require('fastify')()

  await fastify.register(import('@fastify/throttle'))

  fastify.get('/', {
    config: {
      throttle: {
        bytesPerSecond: 1000
      }
    }
  }, (req, reply) => {
    reply.send(createReadStream(resolve(__dirname, __filename)))
  })

  fastify.listen({ port: 3000 })

这个 bytesPerSecond 选项可以是数字或函数。 函数用于 bytesPerSecond 有以下类型脚本定义 :

type BytesPerSecond = (request: FastifyRequest) => ((elapsedTime: number, bytes: number) => number) | Promise<((elapsedTime: number, bytes: number) => number)>

request 是快捷请求对象.

elapsedTime 是流起于几秒后的时间. bytes 已经发送的字节.

您必须确保返回值为整数或 Infinity。 。 。 .

例如,您可以通过定义 : 页:1 bytesPerSecond 像这样:

  const fastify = require('fastify')()

  await fastify.register(import('@fastify/throttle'))

  fastify.get('/', {
    config: {
      throttle: {
        bytesPerSecond: function bytesPerSecondfactory(request) {
          // this function runs for every request
          const client = request.headers['customer-id']

          return function (elapsedTime, bytes) {
            return CONFIG[client] * 2 // return a number of xyz
          }
        }
      }
    }
  }, (req, reply) => {
    reply.send(createReadStream(resolve(__dirname, __filename)))
  })

  fastify.listen({ port: 3000 })

这个 bytesPerSecond 函数可以是同步函数,也可以是同步函数。 如果您提供了同位素函数, 它会被插件检测到 。 如果是一个同步函数返回承诺, 您必须设置 async 选项到 true,以便插件知道它应该等待承诺.

  const fastify = require('fastify')()

  await fastify.register(import('@fastify/throttle'))

  fastify.get('/', {
    config: {
      throttle: {
        async: true,
        bytesPerSecond: function bytesPerSecondfactory(request) {
          // this function runs for every request
          const client = request.headers['customer-id']

          return Promise.resolve(function (elapsedTime, bytes) {
            return CONFIG[client] * 2 // return a number of xyz
          })
        }
      }
    }
  }, (req, reply) => {
    reply.send(createReadStream(resolve(__dirname, __filename)))
  })

  fastify.listen({ port: 3000 })

许可证

麻省理工学院

版本与发布记录

@fastify/throttle 共发布 9 个版本,首次发布于 2023/08/26;过去 12 个月发布了 2 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
3.0.4latest2026/10/091 个月前
3.0.32026/08/083 个月前
3.0.22025/01/111.8 年前
3.0.12024/11/122 年前
3.0.0next2024/09/062 年前
3.0.0-pre.fv5.12024/07/112 年前

关于 @fastify/throttle 的常见问题

@fastify/throttle 是什么?

固定的喉咙反应

如何安装 @fastify/throttle?

在项目目录运行 npm install @fastify/throttle,或使用 pnpm add / yarn add / bun add。

@fastify/throttle 支持 TypeScript 吗?

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

@fastify/throttle 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@fastify/throttle 可以商用吗?

@fastify/throttle 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@fastify/throttle 的下载量有多少?

根据 npm 官方下载统计,@fastify/throttle 在 2026/10/15 – 2026/11/13 这 30 天内被下载 2,832 次,日均约 94 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@fastify/throttle 还在维护吗?

最新版本 3.0.4 发布于 2026/10/09(1 个月前);至今共发布 9 个版本,其中过去 12 个月发布了 2 个;首次发布于 2023/08/26。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。