@vercel/functions 1.6.0 → 2.0.1

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/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @vercel/functions
2
2
 
3
+ ## 2.0.1
4
+
5
+ ### Patch Changes
6
+
7
+ - Add Vercel Function Cache api ([#13221](https://github.com/vercel/vercel/pull/13221))
8
+
9
+ ## 2.0.0
10
+
11
+ ### Major Changes
12
+
13
+ - [cli] Remove support for node@16 ([#12857](https://github.com/vercel/vercel/pull/12857))
14
+
3
15
  ## 1.6.0
4
16
 
5
17
  ### Minor Changes
@@ -0,0 +1,13 @@
1
+ import { FunctionCache } from './types';
2
+ export declare class InMemoryCache implements FunctionCache {
3
+ private cache;
4
+ get(key: string, options?: {
5
+ tags?: string[];
6
+ }): Promise<unknown | null>;
7
+ set(key: string, value: unknown, options?: {
8
+ ttl?: number;
9
+ tags?: string[];
10
+ }): Promise<void>;
11
+ delete(key: string): Promise<void>;
12
+ expireTag(tag: string | string[]): Promise<void>;
13
+ }
@@ -0,0 +1,70 @@
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 in_memory_cache_exports = {};
20
+ __export(in_memory_cache_exports, {
21
+ InMemoryCache: () => InMemoryCache
22
+ });
23
+ module.exports = __toCommonJS(in_memory_cache_exports);
24
+ class InMemoryCache {
25
+ constructor() {
26
+ this.cache = {};
27
+ }
28
+ async get(key, options) {
29
+ const entry = this.cache[key];
30
+ if (entry) {
31
+ if (entry.ttl && entry.lastModified + entry.ttl * 1e3 < Date.now()) {
32
+ await this.delete(key);
33
+ return null;
34
+ }
35
+ if (options?.tags) {
36
+ for (const tag of options.tags) {
37
+ entry.tags.add(tag);
38
+ }
39
+ }
40
+ return entry.value;
41
+ }
42
+ return null;
43
+ }
44
+ async set(key, value, options) {
45
+ this.cache[key] = {
46
+ value,
47
+ lastModified: Date.now(),
48
+ ttl: options?.ttl,
49
+ tags: new Set(options?.tags || [])
50
+ };
51
+ }
52
+ async delete(key) {
53
+ delete this.cache[key];
54
+ }
55
+ async expireTag(tag) {
56
+ const tags = Array.isArray(tag) ? tag : [tag];
57
+ for (const key in this.cache) {
58
+ if (Object.prototype.hasOwnProperty.call(this.cache, key)) {
59
+ const entry = this.cache[key];
60
+ if (tags.some((t) => entry.tags.has(t))) {
61
+ delete this.cache[key];
62
+ }
63
+ }
64
+ }
65
+ }
66
+ }
67
+ // Annotate the CommonJS export names for ESM import in node:
68
+ 0 && (module.exports = {
69
+ InMemoryCache
70
+ });
@@ -0,0 +1,16 @@
1
+ import { CacheOptions, FunctionCache } from './types';
2
+ /**
3
+ * Retrieves the Vercel Function Cache.
4
+ *
5
+ * Keys are hashed to ensure they are unique and consistent. The hashing function can be overridden by providing a custom
6
+ * `keyHashFunction` in the `cacheOptions` parameter.
7
+ *
8
+ * To specify a namespace for the cache keys, you can pass a `namespace` option in the `cacheOptions` parameter. If
9
+ * a namespace is provided, the cache keys will be prefixed with the namespace followed by a separator (default is `$`). The
10
+ * namespaceSeparator can also be customized using the `namespaceSeparator` option.
11
+ *
12
+ * @param cacheOptions - Optional configuration for the cache.
13
+ * @returns An instance of the Vercel Function Cache.
14
+ * @throws {Error} If no cache is available in the context and `InMemoryCache` cannot be created.
15
+ */
16
+ export declare const getFunctionCache: (cacheOptions?: CacheOptions) => FunctionCache;
package/cache/index.js ADDED
@@ -0,0 +1,68 @@
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 cache_exports = {};
20
+ __export(cache_exports, {
21
+ getFunctionCache: () => getFunctionCache
22
+ });
23
+ module.exports = __toCommonJS(cache_exports);
24
+ var import_get_context = require("../get-context");
25
+ var import_in_memory_cache = require("./in-memory-cache");
26
+ const defaultKeyHashFunction = (key) => {
27
+ let hash = 5381;
28
+ for (let i = 0; i < key.length; i++) {
29
+ hash = hash * 33 ^ key.charCodeAt(i);
30
+ }
31
+ return (hash >>> 0).toString(16);
32
+ };
33
+ const defaultNamespaceSeparator = "$";
34
+ const getFunctionCache = (cacheOptions) => {
35
+ let cache;
36
+ if ((0, import_get_context.getContext)().cache) {
37
+ cache = (0, import_get_context.getContext)().cache;
38
+ } else {
39
+ cache = new import_in_memory_cache.InMemoryCache();
40
+ }
41
+ const hashFunction = cacheOptions?.keyHashFunction || defaultKeyHashFunction;
42
+ const makeKey = (key) => {
43
+ let prefix = "";
44
+ if (cacheOptions?.namespace) {
45
+ const namespaceSeparator = cacheOptions.namespaceSeparator || defaultNamespaceSeparator;
46
+ prefix = `${cacheOptions.namespace}${namespaceSeparator}`;
47
+ }
48
+ return `${prefix}${hashFunction(key)}`;
49
+ };
50
+ return {
51
+ get: (key, options) => {
52
+ return cache.get(makeKey(key), options);
53
+ },
54
+ set: (key, value, options) => {
55
+ return cache.set(makeKey(key), value, options);
56
+ },
57
+ delete: (key) => {
58
+ return cache.delete(makeKey(key));
59
+ },
60
+ expireTag: (tag) => {
61
+ return cache.expireTag(tag);
62
+ }
63
+ };
64
+ };
65
+ // Annotate the CommonJS export names for ESM import in node:
66
+ 0 && (module.exports = {
67
+ getFunctionCache
68
+ });
@@ -0,0 +1,66 @@
1
+ /**
2
+ * Interface representing the function cache.
3
+ */
4
+ export interface FunctionCache {
5
+ /**
6
+ * Deletes a value from the cache.
7
+ *
8
+ * @param {string} key - The key of the value to delete.
9
+ * @returns {Promise<void>} A promise that resolves when the value is deleted.
10
+ */
11
+ delete: (key: string) => Promise<void>;
12
+ /**
13
+ * Retrieves a value from the cache.
14
+ *
15
+ * @param {string} key - The key of the value to retrieve.
16
+ * @param {Object} [options] - Optional settings for the cache entry.
17
+ * @param {string[]} [options.tags] - Optional tags to associate wit the cache entry.
18
+ * @returns {Promise<unknown | null>} A promise that resolves to the value, or null if not found.
19
+ */
20
+ get: (key: string, options?: {
21
+ tags?: string[];
22
+ }) => Promise<unknown | null>;
23
+ /**
24
+ * Sets a value in the cache.
25
+ *
26
+ * @param {string} key - The key of the value to set.
27
+ * @param {unknown} value - The value to set.
28
+ * @param {Object} [options] - Optional settings for the cache entry.
29
+ * @param {string} [options.name] - Optional user-friendly name for the cache entry used for o11y.
30
+ * @param {string[]} [options.tags] - Optional tags to associate with the cache entry.
31
+ * @param {number} [options.ttl] - Optional time-to-live for the cache entry, in seconds.
32
+ * @returns {Promise<void>} A promise that resolves when the value is set.
33
+ */
34
+ set: (key: string, value: unknown, options?: {
35
+ name?: string;
36
+ tags?: string[];
37
+ ttl?: number;
38
+ }) => Promise<void>;
39
+ /**
40
+ * Expires cache entries by tag.
41
+ *
42
+ * @param {string | string[]} tag - The tag or tags of the cache entries to expire.
43
+ * @returns {Promise<void>} A promise that resolves when the cache entries expiration request is received.
44
+ */
45
+ expireTag: (tag: string | string[]) => Promise<void>;
46
+ }
47
+ /**
48
+ * Interface representing options for configuring the cache.
49
+ */
50
+ export interface CacheOptions {
51
+ /**
52
+ * Optional custom hash function for generating keys.
53
+ *
54
+ * @param {string} key - The key to hash.
55
+ * @returns {string} The hashed key.
56
+ */
57
+ keyHashFunction?: (key: string) => string;
58
+ /**
59
+ * Optional namespace to prefix cache keys.
60
+ */
61
+ namespace?: string;
62
+ /**
63
+ * Optional separator string for the namespace.
64
+ */
65
+ namespaceSeparator?: string;
66
+ }
package/cache/types.js ADDED
@@ -0,0 +1,16 @@
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 __copyProps = (to, from, except, desc) => {
7
+ if (from && typeof from === "object" || typeof from === "function") {
8
+ for (let key of __getOwnPropNames(from))
9
+ if (!__hasOwnProp.call(to, key) && key !== except)
10
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
11
+ }
12
+ return to;
13
+ };
14
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
15
+ var types_exports = {};
16
+ module.exports = __toCommonJS(types_exports);
@@ -0,0 +1,133 @@
1
+ # Interface: FunctionCache
2
+
3
+ [index](../modules/index.md).FunctionCache
4
+
5
+ Interface representing the function cache.
6
+
7
+ ## Table of contents
8
+
9
+ ### Properties
10
+
11
+ - [delete](index.FunctionCache.md#delete)
12
+ - [expireTag](index.FunctionCache.md#expiretag)
13
+ - [get](index.FunctionCache.md#get)
14
+ - [set](index.FunctionCache.md#set)
15
+
16
+ ## Properties
17
+
18
+ ### delete
19
+
20
+ • **delete**: (`key`: `string`) => [`Promise`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)<`void`\>
21
+
22
+ #### Type declaration
23
+
24
+ ▸ (`key`): [`Promise`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)<`void`\>
25
+
26
+ Deletes a value from the cache.
27
+
28
+ ##### Parameters
29
+
30
+ | Name | Type | Description |
31
+ | :---- | :------- | :------------------------------ |
32
+ | `key` | `string` | The key of the value to delete. |
33
+
34
+ ##### Returns
35
+
36
+ [`Promise`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)<`void`\>
37
+
38
+ A promise that resolves when the value is deleted.
39
+
40
+ #### Defined in
41
+
42
+ [packages/functions/src/cache/types.ts:11](https://github.com/vercel/vercel/blob/main/packages/functions/src/cache/types.ts#L11)
43
+
44
+ ---
45
+
46
+ ### expireTag
47
+
48
+ • **expireTag**: (`tag`: `string` \| `string`[]) => [`Promise`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)<`void`\>
49
+
50
+ #### Type declaration
51
+
52
+ ▸ (`tag`): [`Promise`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)<`void`\>
53
+
54
+ Expires cache entries by tag.
55
+
56
+ ##### Parameters
57
+
58
+ | Name | Type | Description |
59
+ | :---- | :--------------------- | :---------------------------------------------- |
60
+ | `tag` | `string` \| `string`[] | The tag or tags of the cache entries to expire. |
61
+
62
+ ##### Returns
63
+
64
+ [`Promise`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)<`void`\>
65
+
66
+ A promise that resolves when the cache entries expiration request is received.
67
+
68
+ #### Defined in
69
+
70
+ [packages/functions/src/cache/types.ts:46](https://github.com/vercel/vercel/blob/main/packages/functions/src/cache/types.ts#L46)
71
+
72
+ ---
73
+
74
+ ### get
75
+
76
+ • **get**: (`key`: `string`, `options?`: { `tags?`: `string`[] }) => [`Promise`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)<`unknown`\>
77
+
78
+ #### Type declaration
79
+
80
+ ▸ (`key`, `options?`): [`Promise`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)<`unknown`\>
81
+
82
+ Retrieves a value from the cache.
83
+
84
+ ##### Parameters
85
+
86
+ | Name | Type | Description |
87
+ | :-------------- | :--------- | :---------------------------------------------- |
88
+ | `key` | `string` | The key of the value to retrieve. |
89
+ | `options?` | `Object` | Optional settings for the cache entry. |
90
+ | `options.tags?` | `string`[] | Optional tags to associate wit the cache entry. |
91
+
92
+ ##### Returns
93
+
94
+ [`Promise`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)<`unknown`\>
95
+
96
+ A promise that resolves to the value, or null if not found.
97
+
98
+ #### Defined in
99
+
100
+ [packages/functions/src/cache/types.ts:21](https://github.com/vercel/vercel/blob/main/packages/functions/src/cache/types.ts#L21)
101
+
102
+ ---
103
+
104
+ ### set
105
+
106
+ • **set**: (`key`: `string`, `value`: `unknown`, `options?`: { `name?`: `string` ; `tags?`: `string`[] ; `ttl?`: `number` }) => [`Promise`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)<`void`\>
107
+
108
+ #### Type declaration
109
+
110
+ ▸ (`key`, `value`, `options?`): [`Promise`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)<`void`\>
111
+
112
+ Sets a value in the cache.
113
+
114
+ ##### Parameters
115
+
116
+ | Name | Type | Description |
117
+ | :-------------- | :--------- | :------------------------------------------------------------- |
118
+ | `key` | `string` | The key of the value to set. |
119
+ | `value` | `unknown` | The value to set. |
120
+ | `options?` | `Object` | Optional settings for the cache entry. |
121
+ | `options.name?` | `string` | Optional user-friendly name for the cache entry used for o11y. |
122
+ | `options.tags?` | `string`[] | Optional tags to associate with the cache entry. |
123
+ | `options.ttl?` | `number` | Optional time-to-live for the cache entry, in seconds. |
124
+
125
+ ##### Returns
126
+
127
+ [`Promise`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)<`void`\>
128
+
129
+ A promise that resolves when the value is set.
130
+
131
+ #### Defined in
132
+
133
+ [packages/functions/src/cache/types.ts:34](https://github.com/vercel/vercel/blob/main/packages/functions/src/cache/types.ts#L34)
@@ -4,6 +4,7 @@
4
4
 
5
5
  ### Interfaces
6
6
 
7
+ - [FunctionCache](../interfaces/index.FunctionCache.md)
7
8
  - [Geo](../interfaces/index.Geo.md)
8
9
  - [Request](../interfaces/index.Request.md)
9
10
 
@@ -11,6 +12,7 @@
11
12
 
12
13
  - [geolocation](index.md#geolocation)
13
14
  - [getEnv](index.md#getenv)
15
+ - [getFunctionCache](index.md#getfunctioncache)
14
16
  - [ipAddress](index.md#ipaddress)
15
17
  - [next](index.md#next)
16
18
  - [rewrite](index.md#rewrite)
@@ -116,6 +118,41 @@ https://vercel.com/docs/projects/environment-variables/system-environment-variab
116
118
 
117
119
  ---
118
120
 
121
+ ### getFunctionCache
122
+
123
+ ▸ **getFunctionCache**(`cacheOptions?`): [`FunctionCache`](../interfaces/index.FunctionCache.md)
124
+
125
+ Retrieves the Vercel Function Cache.
126
+
127
+ Keys are hashed to ensure they are unique and consistent. The hashing function can be overridden by providing a custom
128
+ `keyHashFunction` in the `cacheOptions` parameter.
129
+
130
+ To specify a namespace for the cache keys, you can pass a `namespace` option in the `cacheOptions` parameter. If
131
+ a namespace is provided, the cache keys will be prefixed with the namespace followed by a separator (default is `$`). The
132
+ namespaceSeparator can also be customized using the `namespaceSeparator` option.
133
+
134
+ **`Throws`**
135
+
136
+ If no cache is available in the context and `InMemoryCache` cannot be created.
137
+
138
+ #### Parameters
139
+
140
+ | Name | Type | Description |
141
+ | :-------------- | :------------- | :------------------------------------ |
142
+ | `cacheOptions?` | `CacheOptions` | Optional configuration for the cache. |
143
+
144
+ #### Returns
145
+
146
+ [`FunctionCache`](../interfaces/index.FunctionCache.md)
147
+
148
+ An instance of the Vercel Function Cache.
149
+
150
+ #### Defined in
151
+
152
+ [packages/functions/src/cache/index.ts:29](https://github.com/vercel/vercel/blob/main/packages/functions/src/cache/index.ts#L29)
153
+
154
+ ---
155
+
119
156
  ### ipAddress
120
157
 
121
158
  ▸ **ipAddress**(`input`): `string` \| `undefined`
package/get-context.d.ts CHANGED
@@ -1,5 +1,7 @@
1
+ import { FunctionCache } from './cache/types';
1
2
  type Context = {
2
3
  waitUntil?: (promise: Promise<unknown>) => void;
4
+ cache?: FunctionCache;
3
5
  headers?: Record<string, string>;
4
6
  };
5
7
  export declare const SYMBOL_FOR_REQ_CONTEXT: unique symbol;
package/index.d.ts CHANGED
@@ -3,3 +3,5 @@ export { geolocation, ipAddress } from './headers';
3
3
  export { getEnv } from './get-env';
4
4
  export { waitUntil } from './wait-until';
5
5
  export { rewrite, next } from './middleware';
6
+ export { getFunctionCache } from './cache';
7
+ export type { FunctionCache } from './cache/types';
package/index.js CHANGED
@@ -20,6 +20,7 @@ var src_exports = {};
20
20
  __export(src_exports, {
21
21
  geolocation: () => import_headers.geolocation,
22
22
  getEnv: () => import_get_env.getEnv,
23
+ getFunctionCache: () => import_cache.getFunctionCache,
23
24
  ipAddress: () => import_headers.ipAddress,
24
25
  next: () => import_middleware.next,
25
26
  rewrite: () => import_middleware.rewrite,
@@ -30,10 +31,12 @@ var import_headers = require("./headers");
30
31
  var import_get_env = require("./get-env");
31
32
  var import_wait_until = require("./wait-until");
32
33
  var import_middleware = require("./middleware");
34
+ var import_cache = require("./cache");
33
35
  // Annotate the CommonJS export names for ESM import in node:
34
36
  0 && (module.exports = {
35
37
  geolocation,
36
38
  getEnv,
39
+ getFunctionCache,
37
40
  ipAddress,
38
41
  next,
39
42
  rewrite,
package/package.json CHANGED
@@ -26,7 +26,7 @@
26
26
  "require": "./middleware.js"
27
27
  }
28
28
  },
29
- "version": "1.6.0",
29
+ "version": "2.0.1",
30
30
  "repository": {
31
31
  "directory": "packages/functions",
32
32
  "type": "git",
@@ -55,7 +55,7 @@
55
55
  }
56
56
  },
57
57
  "engines": {
58
- "node": ">= 16"
58
+ "node": ">= 18"
59
59
  },
60
60
  "license": "Apache-2.0",
61
61
  "publishConfig": {