@benswinburne/closest-aws-region

中文文档
2.0.1发布于 2021/11/21MITCommonJS零依赖近 30 天下载 3875

Determines the closest AWS region using average latency between regions

原始使用文档
文档目录11 节
  1. Why?
  2. Installation
  3. Import/Require
  4. CommonJS:
  5. ES Modules:
  6. closestAWSRegion(to, [options])
  7. Options
  8. default
  9. filters
  10. excludeTo (defaults to false)
  11. data

Find the Closest AWS Region

Why?

Whilst using lambda@edge your code runs in any one of the available data centres, which is not necessarily true for your data or other services perhaps.

For example, if I'm using Dynamodb global tables and my data exists in eu-west-1, us-east-2 and ap-southeast-1, when my lambda@edge function runs and sets up a DocumentClient using process.env.AWS_REGION, the value of this environment variable may be any of the available AWS regions, perhaps eu-central-1.

What this means is that the DocumentClient will fail to retrieve the data as it attempts to access it from Dynamodb at eu-central-1 as it hasn't been replicated to that region.

Rather than hard coding or chosing one of the three regions in which I've stored my data, using this package I can determine which of the regions which contains my data is closest to process.env.AWS_REGION (or at least has the lowest latency).

Installation

npm install @benswinburne/closest-aws-region

Import/Require

CommonJS:

var closestAWSRegion = require('@benswinburne/closest-aws-region')

ES Modules:

import closestAWSRegion from '@benswinburne/closest-aws-region'

Quick Start

// AWS_REGION = eu-central-1

const region = findClosestAWSRegion(process.env.AWS_REGION, {
  filters: ['eu-west-1', 'us-east-1', 'ap-southeast-1'],
}); // eu-west-1

API

closestAWSRegion(to, [options])

Find the closest region to that specified in to.

Note: If options.filters is not set, the closest region will always be to.

Options

default

The default region to select in the event that no region is found. This defaults to us-east-1 if not set.

filters

Specify the region(s) to limit your search to.

// AWS_REGION = eu-central-1

const region = findClosestAWSRegion(process.env.AWS_REGION, {
  filters: ['eu-west-1', 'us-east-1']
}); // eu-west-1

excludeTo (defaults to false)

If for example you are looking for the closest region to eu-west-1, with no filters applied obviously eu-west-1 is the closted region. By excluding to, the return value will be the closest region to eu-west-1 which is not eu-west-1.

data

This option allows for overwriting the static datasource. The format must follow the structure used by cloudping.co.

Note: it's not recommended that a remote datasource is used with lambda@edge because the latency savings by finding the closest region are lost during the roundtrip to an external source

// AWS_REGION = eu-central-1

const response = await fetch('https://api.cloudping.co/averages');
const data = await response.json();

const region = findClosestAWSRegion(process.env.AWS_REGION, {
  data: data
});

版本与发布记录

@benswinburne/closest-aws-region 共发布 4 个版本,首次发布于 2019/04/10;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.0.1latest2021/11/214 年前
2.0.02021/05/065 年前
1.0.12019/04/107 年前
1.0.02019/04/107 年前

关于 @benswinburne/closest-aws-region 的常见问题

@benswinburne/closest-aws-region 是什么?

Determines the closest AWS region using average latency between regions

如何安装 @benswinburne/closest-aws-region?

在项目目录运行 npm install @benswinburne/closest-aws-region,或使用 pnpm add / yarn add / bun add。

@benswinburne/closest-aws-region 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/benswinburne__closest-aws-region 是否存在。

@benswinburne/closest-aws-region 支持 ESM 还是 CommonJS?

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

@benswinburne/closest-aws-region 可以商用吗?

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

@benswinburne/closest-aws-region 的下载量有多少?

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

@benswinburne/closest-aws-region 还在维护吗?

最新版本 2.0.1 发布于 2021/11/21(4 年前);至今共发布 4 个版本,其中过去 12 个月发布了 0 个;首次发布于 2019/04/10。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。