File size: 1,648 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { type RequestHandler } from 'express';
import { getLogger } from 'calypso/server/lib/logger';

/**
 * Adds the `x-geoip-country-code` header to the request. This is used to simulate the header added
 * in production environments, which is not available in all environments.
 * @returns A middleware function that adds the `x-geoip-country-code` header to the request.
 */
export default function (): RequestHandler {
	// To avoid delays on handling individual requests, fetch the geolocation once during server
	// startup. This will retrieve the geolocation of the server (not the client), but it's expected
	// that the middleware would be enabled on a developer's own machine.
	let countryCode: string | undefined;
	let countryCodePromise: Promise< string | undefined > | undefined;

	async function getGeoCountryCode() {
		if ( ! countryCode ) {
			if ( ! countryCodePromise ) {
				countryCodePromise = fetch( 'https://public-api.wordpress.com/geo/' )
					.then( ( response ) => response.json() )
					.then( ( { country_short } ) => country_short )
					// Support offline development and set an expectation that the header is not
					// always available by quietly skipping if the request fails.
					.catch( () => {
						getLogger().info( 'Failed to fetch geolocation' );
					} );
			}

			countryCode = await countryCodePromise;
		}

		return countryCode;
	}

	// Prime the cache when the middleware is initialized.
	getGeoCountryCode();

	return async ( req, _res, next ) => {
		const countryCode = await getGeoCountryCode();
		if ( countryCode ) {
			req.headers[ 'x-geoip-country-code' ] = countryCode;
		}

		next();
	};
}