@vercel/functions 1.1.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/headers.d.ts +83 -0
- package/dist/headers.js +84 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.js +14 -3
- package/package.json +8 -3
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* City of the original client IP as calculated by Vercel Proxy.
|
|
3
|
+
*/
|
|
4
|
+
export declare const CITY_HEADER_NAME = "x-vercel-ip-city";
|
|
5
|
+
/**
|
|
6
|
+
* Country of the original client IP as calculated by Vercel Proxy.
|
|
7
|
+
*/
|
|
8
|
+
export declare const COUNTRY_HEADER_NAME = "x-vercel-ip-country";
|
|
9
|
+
/**
|
|
10
|
+
* Client IP as calculated by Vercel Proxy.
|
|
11
|
+
*/
|
|
12
|
+
export declare const IP_HEADER_NAME = "x-real-ip";
|
|
13
|
+
/**
|
|
14
|
+
* Latitude of the original client IP as calculated by Vercel Proxy.
|
|
15
|
+
*/
|
|
16
|
+
export declare const LATITUDE_HEADER_NAME = "x-vercel-ip-latitude";
|
|
17
|
+
/**
|
|
18
|
+
* Longitude of the original client IP as calculated by Vercel Proxy.
|
|
19
|
+
*/
|
|
20
|
+
export declare const LONGITUDE_HEADER_NAME = "x-vercel-ip-longitude";
|
|
21
|
+
/**
|
|
22
|
+
* Country region of the original client IP calculated by Vercel Proxy.
|
|
23
|
+
*
|
|
24
|
+
* See [docs](https://vercel.com/docs/concepts/edge-network/headers#x-vercel-ip-country-region).
|
|
25
|
+
*/
|
|
26
|
+
export declare const REGION_HEADER_NAME = "x-vercel-ip-country-region";
|
|
27
|
+
/**
|
|
28
|
+
* The request ID for each request generated by Vercel Proxy.
|
|
29
|
+
*/
|
|
30
|
+
export declare const REQUEST_ID_HEADER_NAME = "x-vercel-id";
|
|
31
|
+
/**
|
|
32
|
+
* Unicode characters for emoji flags start at this number, and run up to 127469.
|
|
33
|
+
*/
|
|
34
|
+
export declare const EMOJI_FLAG_UNICODE_STARTING_POSITION = 127397;
|
|
35
|
+
/**
|
|
36
|
+
* We define a new type so this function can be reused with
|
|
37
|
+
* the global `Request`, `node-fetch` and other types.
|
|
38
|
+
*/
|
|
39
|
+
interface Request {
|
|
40
|
+
headers: {
|
|
41
|
+
get(name: string): string | null;
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* The location information of a given request.
|
|
46
|
+
*/
|
|
47
|
+
export interface Geo {
|
|
48
|
+
/** The city that the request originated from. */
|
|
49
|
+
city?: string;
|
|
50
|
+
/** The country that the request originated from. */
|
|
51
|
+
country?: string;
|
|
52
|
+
/** The flag emoji for the country the request originated from. */
|
|
53
|
+
flag?: string;
|
|
54
|
+
/** The [Vercel Edge Network region](https://vercel.com/docs/concepts/edge-network/regions) that received the request. */
|
|
55
|
+
region?: string;
|
|
56
|
+
/** The region part of the ISO 3166-2 code of the client IP.
|
|
57
|
+
* See [docs](https://vercel.com/docs/concepts/edge-network/headers#x-vercel-ip-country-region).
|
|
58
|
+
*/
|
|
59
|
+
countryRegion?: string;
|
|
60
|
+
/** The latitude of the client. */
|
|
61
|
+
latitude?: string;
|
|
62
|
+
/** The longitude of the client. */
|
|
63
|
+
longitude?: string;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Returns the IP address of the request from the headers.
|
|
67
|
+
*
|
|
68
|
+
* @see {@link IP_HEADER_NAME}
|
|
69
|
+
* @param request The incoming request object which provides the IP
|
|
70
|
+
*/
|
|
71
|
+
export declare function ipAddress(request: Request): string | undefined;
|
|
72
|
+
/**
|
|
73
|
+
* Returns the location information for the incoming request.
|
|
74
|
+
*
|
|
75
|
+
* @see {@link CITY_HEADER_NAME}
|
|
76
|
+
* @see {@link COUNTRY_HEADER_NAME}
|
|
77
|
+
* @see {@link REGION_HEADER_NAME}
|
|
78
|
+
* @see {@link LATITUDE_HEADER_NAME}
|
|
79
|
+
* @see {@link LONGITUDE_HEADER_NAME}
|
|
80
|
+
* @param request The incoming request object which provides the geolocation data
|
|
81
|
+
*/
|
|
82
|
+
export declare function geolocation(request: Request): Geo;
|
|
83
|
+
export {};
|
package/dist/headers.js
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var headers_exports = {};
|
|
20
|
+
__export(headers_exports, {
|
|
21
|
+
CITY_HEADER_NAME: () => CITY_HEADER_NAME,
|
|
22
|
+
COUNTRY_HEADER_NAME: () => COUNTRY_HEADER_NAME,
|
|
23
|
+
EMOJI_FLAG_UNICODE_STARTING_POSITION: () => EMOJI_FLAG_UNICODE_STARTING_POSITION,
|
|
24
|
+
IP_HEADER_NAME: () => IP_HEADER_NAME,
|
|
25
|
+
LATITUDE_HEADER_NAME: () => LATITUDE_HEADER_NAME,
|
|
26
|
+
LONGITUDE_HEADER_NAME: () => LONGITUDE_HEADER_NAME,
|
|
27
|
+
REGION_HEADER_NAME: () => REGION_HEADER_NAME,
|
|
28
|
+
REQUEST_ID_HEADER_NAME: () => REQUEST_ID_HEADER_NAME,
|
|
29
|
+
geolocation: () => geolocation,
|
|
30
|
+
ipAddress: () => ipAddress
|
|
31
|
+
});
|
|
32
|
+
module.exports = __toCommonJS(headers_exports);
|
|
33
|
+
const CITY_HEADER_NAME = "x-vercel-ip-city";
|
|
34
|
+
const COUNTRY_HEADER_NAME = "x-vercel-ip-country";
|
|
35
|
+
const IP_HEADER_NAME = "x-real-ip";
|
|
36
|
+
const LATITUDE_HEADER_NAME = "x-vercel-ip-latitude";
|
|
37
|
+
const LONGITUDE_HEADER_NAME = "x-vercel-ip-longitude";
|
|
38
|
+
const REGION_HEADER_NAME = "x-vercel-ip-country-region";
|
|
39
|
+
const REQUEST_ID_HEADER_NAME = "x-vercel-id";
|
|
40
|
+
const EMOJI_FLAG_UNICODE_STARTING_POSITION = 127397;
|
|
41
|
+
function getHeader(request, key) {
|
|
42
|
+
return request.headers.get(key) ?? void 0;
|
|
43
|
+
}
|
|
44
|
+
function getFlag(countryCode) {
|
|
45
|
+
const regex = new RegExp("^[A-Z]{2}$").test(countryCode);
|
|
46
|
+
if (!countryCode || !regex)
|
|
47
|
+
return void 0;
|
|
48
|
+
return String.fromCodePoint(
|
|
49
|
+
...countryCode.split("").map((char) => EMOJI_FLAG_UNICODE_STARTING_POSITION + char.charCodeAt(0))
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
function ipAddress(request) {
|
|
53
|
+
return getHeader(request, IP_HEADER_NAME);
|
|
54
|
+
}
|
|
55
|
+
function getRegionFromRequestId(requestId) {
|
|
56
|
+
if (!requestId) {
|
|
57
|
+
return "dev1";
|
|
58
|
+
}
|
|
59
|
+
return requestId.split(":")[0];
|
|
60
|
+
}
|
|
61
|
+
function geolocation(request) {
|
|
62
|
+
return {
|
|
63
|
+
city: getHeader(request, CITY_HEADER_NAME),
|
|
64
|
+
country: getHeader(request, COUNTRY_HEADER_NAME),
|
|
65
|
+
flag: getFlag(getHeader(request, COUNTRY_HEADER_NAME)),
|
|
66
|
+
countryRegion: getHeader(request, REGION_HEADER_NAME),
|
|
67
|
+
region: getRegionFromRequestId(getHeader(request, REQUEST_ID_HEADER_NAME)),
|
|
68
|
+
latitude: getHeader(request, LATITUDE_HEADER_NAME),
|
|
69
|
+
longitude: getHeader(request, LONGITUDE_HEADER_NAME)
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
73
|
+
0 && (module.exports = {
|
|
74
|
+
CITY_HEADER_NAME,
|
|
75
|
+
COUNTRY_HEADER_NAME,
|
|
76
|
+
EMOJI_FLAG_UNICODE_STARTING_POSITION,
|
|
77
|
+
IP_HEADER_NAME,
|
|
78
|
+
LATITUDE_HEADER_NAME,
|
|
79
|
+
LONGITUDE_HEADER_NAME,
|
|
80
|
+
REGION_HEADER_NAME,
|
|
81
|
+
REQUEST_ID_HEADER_NAME,
|
|
82
|
+
geolocation,
|
|
83
|
+
ipAddress
|
|
84
|
+
});
|
package/dist/index.d.ts
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
export
|
|
1
|
+
export { geolocation, ipAddress } from './headers';
|
|
2
|
+
export { waitUntil } from './wait-until';
|
package/dist/index.js
CHANGED
|
@@ -3,6 +3,10 @@ var __defProp = Object.defineProperty;
|
|
|
3
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
5
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
6
10
|
var __copyProps = (to, from, except, desc) => {
|
|
7
11
|
if (from && typeof from === "object" || typeof from === "function") {
|
|
8
12
|
for (let key of __getOwnPropNames(from))
|
|
@@ -11,12 +15,19 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
11
15
|
}
|
|
12
16
|
return to;
|
|
13
17
|
};
|
|
14
|
-
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
15
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
16
19
|
var src_exports = {};
|
|
20
|
+
__export(src_exports, {
|
|
21
|
+
geolocation: () => import_headers.geolocation,
|
|
22
|
+
ipAddress: () => import_headers.ipAddress,
|
|
23
|
+
waitUntil: () => import_wait_until.waitUntil
|
|
24
|
+
});
|
|
17
25
|
module.exports = __toCommonJS(src_exports);
|
|
18
|
-
|
|
26
|
+
var import_headers = require("./headers");
|
|
27
|
+
var import_wait_until = require("./wait-until");
|
|
19
28
|
// Annotate the CommonJS export names for ESM import in node:
|
|
20
29
|
0 && (module.exports = {
|
|
21
|
-
|
|
30
|
+
geolocation,
|
|
31
|
+
ipAddress,
|
|
32
|
+
waitUntil
|
|
22
33
|
});
|
package/package.json
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
"dist"
|
|
10
10
|
],
|
|
11
11
|
"types": "./dist/index.d.ts",
|
|
12
|
-
"version": "1.
|
|
12
|
+
"version": "1.2.0",
|
|
13
13
|
"repository": {
|
|
14
14
|
"directory": "packages/functions",
|
|
15
15
|
"type": "git",
|
|
@@ -19,8 +19,11 @@
|
|
|
19
19
|
"url": "https://github.com/vercel/vercel/issues"
|
|
20
20
|
},
|
|
21
21
|
"devDependencies": {
|
|
22
|
-
"typescript": "4.9.5",
|
|
23
22
|
"tinyspawn": "1.3.1",
|
|
23
|
+
"typedoc": "0.24.6",
|
|
24
|
+
"typedoc-plugin-markdown": "3.15.2",
|
|
25
|
+
"typedoc-plugin-mdn-links": "3.0.3",
|
|
26
|
+
"typescript": "4.9.5",
|
|
24
27
|
"vitest": "1.3.1"
|
|
25
28
|
},
|
|
26
29
|
"engines": {
|
|
@@ -31,7 +34,9 @@
|
|
|
31
34
|
"access": "public"
|
|
32
35
|
},
|
|
33
36
|
"scripts": {
|
|
37
|
+
"pretest": "pnpm run build",
|
|
34
38
|
"test": "vitest",
|
|
35
|
-
"build": "node ../../utils/build.mjs"
|
|
39
|
+
"build": "node ../../utils/build.mjs",
|
|
40
|
+
"build:docs": "typedoc && prettier --write docs/**/*.md docs/*.md"
|
|
36
41
|
}
|
|
37
42
|
}
|