@vercel/edge-config
Advanced tools
Comparing version 1.2.0 to 1.2.1-9744f945-20240625144149
@@ -6,3 +6,3 @@ // src/index.ts | ||
var name = "@vercel/edge-config"; | ||
var version = "1.2.0"; | ||
var version = "1.2.1-9744f945-20240625144149"; | ||
@@ -95,12 +95,2 @@ // src/utils/tracing.ts | ||
}; | ||
function hasOwnProperty(obj, prop) { | ||
return Object.prototype.hasOwnProperty.call(obj, prop); | ||
} | ||
function pick(obj, keys) { | ||
const ret = {}; | ||
keys.forEach((key) => { | ||
ret[key] = obj[key]; | ||
}); | ||
return ret; | ||
} | ||
function assertIsKey(key) { | ||
@@ -184,102 +174,4 @@ if (typeof key !== "string") { | ||
// src/utils/fetch-with-cached-response.ts | ||
function createResponse(cachedResponseEntry) { | ||
return new Response(cachedResponseEntry.response, { | ||
headers: { | ||
...cachedResponseEntry.headers, | ||
Age: String( | ||
// age header may not be 0 when serving stale content, must be >= 1 | ||
Math.max(1, Math.floor((Date.now() - cachedResponseEntry.time) / 1e3)) | ||
) | ||
}, | ||
status: cachedResponseEntry.status | ||
}); | ||
} | ||
function createHandleStaleIfError(cachedResponseEntry, staleIfError) { | ||
return function handleStaleIfError(response) { | ||
switch (response.status) { | ||
case 500: | ||
case 502: | ||
case 503: | ||
case 504: | ||
return typeof staleIfError === "number" && cachedResponseEntry.time < Date.now() + staleIfError * 1e3 ? createResponse(cachedResponseEntry) : response; | ||
default: | ||
return response; | ||
} | ||
}; | ||
} | ||
function createHandleStaleIfErrorException(cachedResponseEntry, staleIfError) { | ||
return function handleStaleIfError(reason) { | ||
if (typeof staleIfError === "number" && cachedResponseEntry.time < Date.now() + staleIfError * 1e3) { | ||
return createResponse(cachedResponseEntry); | ||
} | ||
throw reason; | ||
}; | ||
} | ||
var cache = /* @__PURE__ */ new Map(); | ||
function extractStaleIfError(cacheControlHeader) { | ||
if (!cacheControlHeader) | ||
return null; | ||
const matched = /stale-if-error=(?<staleIfError>\d+)/i.exec( | ||
cacheControlHeader | ||
); | ||
return (matched == null ? void 0 : matched.groups) ? Number(matched.groups.staleIfError) : null; | ||
} | ||
var fetchWithCachedResponse = trace( | ||
async function fetchWithCachedResponse2(url, options = {}) { | ||
const { headers: customHeaders = new Headers(), ...customOptions } = options; | ||
const authHeader = customHeaders.get("Authorization"); | ||
const cacheKey = `${url},${authHeader || ""}`; | ||
const cachedResponseEntry = cache.get(cacheKey); | ||
if (cachedResponseEntry) { | ||
const { etag: etag2, response: cachedResponse } = cachedResponseEntry; | ||
const headers = new Headers(customHeaders); | ||
headers.set("If-None-Match", etag2); | ||
const staleIfError = extractStaleIfError(headers.get("Cache-Control")); | ||
const res2 = await fetch(url, { | ||
...customOptions, | ||
headers | ||
}).then( | ||
createHandleStaleIfError(cachedResponseEntry, staleIfError), | ||
createHandleStaleIfErrorException(cachedResponseEntry, staleIfError) | ||
); | ||
if (res2.status === 304) { | ||
res2.cachedResponseBody = JSON.parse(cachedResponse); | ||
return res2; | ||
} | ||
const newETag = res2.headers.get("ETag"); | ||
if (res2.ok && newETag) | ||
cache.set(cacheKey, { | ||
etag: newETag, | ||
response: await res2.clone().text(), | ||
headers: Object.fromEntries(res2.headers.entries()), | ||
status: res2.status, | ||
time: Date.now() | ||
}); | ||
return res2; | ||
} | ||
const res = await fetch(url, options); | ||
const etag = res.headers.get("ETag"); | ||
if (res.ok && etag) { | ||
cache.set(cacheKey, { | ||
etag, | ||
response: await res.clone().text(), | ||
headers: Object.fromEntries(res.headers.entries()), | ||
status: res.status, | ||
time: Date.now() | ||
}); | ||
} | ||
return res; | ||
}, | ||
{ | ||
name: "fetchWithCachedResponse", | ||
attributesSuccess(result) { | ||
return { | ||
status: result.status | ||
}; | ||
} | ||
} | ||
); | ||
// src/index.ts | ||
var fetchWithCachedResponse = fetch; | ||
var jsonParseCache = /* @__PURE__ */ new Map(); | ||
@@ -354,11 +246,3 @@ var readFileTraced = trace(readFile, { name: "readFile" }); | ||
const digest2 = res.headers.get("x-edge-config-digest"); | ||
let body; | ||
if (!res.ok) { | ||
await consumeResponseBody(res); | ||
body = res.cachedResponseBody; | ||
if (!body) | ||
return null; | ||
} else { | ||
body = await res.json(); | ||
} | ||
const body = await res.json(); | ||
return { digest: digest2, items: body }; | ||
@@ -390,6 +274,2 @@ }); | ||
} | ||
async function getLocalEdgeConfig(connection) { | ||
const edgeConfig = await getPrivateEdgeConfig(connection) || await getFileSystemEdgeConfig(connection); | ||
return edgeConfig; | ||
} | ||
async function consumeResponseBody(res) { | ||
@@ -424,3 +304,3 @@ await res.arrayBuffer(); | ||
const shouldUseDevelopmentCache = !options.disableDevelopmentCache && process.env.NODE_ENV === "development" && process.env.EDGE_CONFIG_DISABLE_DEVELOPMENT_SWR !== "1"; | ||
const getInMemoryEdgeConfig = createGetInMemoryEdgeConfig( | ||
const _getInMemoryEdgeConfig = createGetInMemoryEdgeConfig( | ||
shouldUseDevelopmentCache, | ||
@@ -432,41 +312,26 @@ connection, | ||
const api = { | ||
get: trace( | ||
async function get2(key) { | ||
const localEdgeConfig = await getInMemoryEdgeConfig() || await getLocalEdgeConfig(connection); | ||
if (localEdgeConfig) { | ||
assertIsKey(key); | ||
return Promise.resolve(localEdgeConfig.items[key]); | ||
get: async function get2(key) { | ||
assertIsKey(key); | ||
return fetchWithCachedResponse( | ||
`${baseUrl}/item/${key}?version=${version2}`, | ||
{ | ||
headers: new Headers(headers), | ||
cache: fetchCache | ||
} | ||
assertIsKey(key); | ||
return fetchWithCachedResponse( | ||
`${baseUrl}/item/${key}?version=${version2}`, | ||
{ | ||
headers: new Headers(headers), | ||
cache: fetchCache | ||
} | ||
).then(async (res) => { | ||
if (res.ok) | ||
return res.json(); | ||
await consumeResponseBody(res); | ||
if (res.status === 401) | ||
throw new Error(ERRORS.UNAUTHORIZED); | ||
if (res.status === 404) { | ||
if (res.headers.has("x-edge-config-digest")) | ||
return void 0; | ||
throw new Error(ERRORS.EDGE_CONFIG_NOT_FOUND); | ||
} | ||
if (res.cachedResponseBody !== void 0) | ||
return res.cachedResponseBody; | ||
throw new UnexpectedNetworkError(res); | ||
}); | ||
}, | ||
{ name: "get", isVerboseTrace: false, attributes: { edgeConfigId } } | ||
), | ||
).then(async (res) => { | ||
if (res.ok) | ||
return res.json(); | ||
await consumeResponseBody(res); | ||
if (res.status === 401) | ||
throw new Error(ERRORS.UNAUTHORIZED); | ||
if (res.status === 404) { | ||
if (res.headers.has("x-edge-config-digest")) | ||
return void 0; | ||
throw new Error(ERRORS.EDGE_CONFIG_NOT_FOUND); | ||
} | ||
throw new UnexpectedNetworkError(res); | ||
}); | ||
}, | ||
has: trace( | ||
async function has2(key) { | ||
const localEdgeConfig = await getInMemoryEdgeConfig() || await getLocalEdgeConfig(connection); | ||
if (localEdgeConfig) { | ||
assertIsKey(key); | ||
return Promise.resolve(hasOwnProperty(localEdgeConfig.items, key)); | ||
} | ||
assertIsKey(key); | ||
@@ -494,10 +359,2 @@ return fetch(`${baseUrl}/item/${key}?version=${version2}`, { | ||
async function getAll2(keys) { | ||
const localEdgeConfig = await getInMemoryEdgeConfig() || await getLocalEdgeConfig(connection); | ||
if (localEdgeConfig) { | ||
if (keys === void 0) { | ||
return Promise.resolve(localEdgeConfig.items); | ||
} | ||
assertIsKeys(keys); | ||
return Promise.resolve(pick(localEdgeConfig.items, keys)); | ||
} | ||
if (Array.isArray(keys)) | ||
@@ -524,4 +381,2 @@ assertIsKeys(keys); | ||
throw new Error(ERRORS.EDGE_CONFIG_NOT_FOUND); | ||
if (res.cachedResponseBody !== void 0) | ||
return res.cachedResponseBody; | ||
throw new UnexpectedNetworkError(res); | ||
@@ -534,6 +389,2 @@ }); | ||
async function digest2() { | ||
const localEdgeConfig = await getInMemoryEdgeConfig() || await getLocalEdgeConfig(connection); | ||
if (localEdgeConfig) { | ||
return Promise.resolve(localEdgeConfig.digest); | ||
} | ||
return fetchWithCachedResponse( | ||
@@ -549,4 +400,2 @@ `${baseUrl}/digest?version=${version2}`, | ||
await consumeResponseBody(res); | ||
if (res.cachedResponseBody !== void 0) | ||
return res.cachedResponseBody; | ||
throw new UnexpectedNetworkError(res); | ||
@@ -553,0 +402,0 @@ }); |
{ | ||
"name": "@vercel/edge-config", | ||
"version": "1.2.0", | ||
"version": "1.2.1-9744f945-20240625144149", | ||
"description": "Ultra-low latency data at the edge", | ||
@@ -5,0 +5,0 @@ "homepage": "https://vercel.com", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4
135533
1026
1