npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@benswinburne/closest-aws-region

v2.0.1

Published

Determines the closest AWS region using average latency between regions

Downloads

1,848

Readme

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
});