diff --git a/.changeset/cyan-ducks-beg.md b/.changeset/cyan-ducks-beg.md new file mode 100644 index 000000000..e64e3228d --- /dev/null +++ b/.changeset/cyan-ducks-beg.md @@ -0,0 +1,5 @@ +--- +'@vercel/edge-config': patch +--- + +read body to avoid memory leaks diff --git a/src/index.node.ts b/src/index.node.ts index 2d819feae..41da228c7 100644 --- a/src/index.node.ts +++ b/src/index.node.ts @@ -78,7 +78,12 @@ export function createClient( headers: new Headers(headers), cache: 'no-store', }).then( - (res) => { + async (res) => { + if (res.ok) return res.json(); + // Read body to avoid memory leaks. + // see https://github.com/nodejs/undici/blob/v5.21.2/README.md#garbage-collection + // see https://github.com/node-fetch/node-fetch/issues/83 + await res.arrayBuffer(); if (res.status === 401) throw new Error(ERRORS.UNAUTHORIZED); if (res.status === 404) { // if the x-edge-config-digest header is present, it means @@ -90,7 +95,6 @@ export function createClient( } if (res.cachedResponseBody !== undefined) return res.cachedResponseBody as T; - if (res.ok) return res.json(); throw new Error(ERRORS.UNEXPECTED); }, () => { @@ -161,13 +165,17 @@ export function createClient( }, ).then( async (res) => { + if (res.ok) return res.json(); + // Read body to avoid memory leaks. + // see https://github.com/nodejs/undici/blob/v5.21.2/README.md#garbage-collection + // see https://github.com/node-fetch/node-fetch/issues/83 + await res.arrayBuffer(); if (res.status === 401) throw new Error(ERRORS.UNAUTHORIZED); // the /items endpoint never returns 404, so if we get a 404 // it means the edge config itself did not exist if (res.status === 404) throw new Error(ERRORS.EDGE_CONFIG_NOT_FOUND); if (res.cachedResponseBody !== undefined) return res.cachedResponseBody as T; - if (res.ok) return res.json(); throw new Error(ERRORS.UNEXPECTED); }, () => { @@ -186,10 +194,14 @@ export function createClient( headers: new Headers(headers), cache: 'no-store', }).then( - (res) => { + async (res) => { + if (res.ok) return res.json() as Promise; + // Read body to avoid memory leaks. + // see https://github.com/nodejs/undici/blob/v5.21.2/README.md#garbage-collection + // see https://github.com/node-fetch/node-fetch/issues/83 + await res.arrayBuffer(); if (res.cachedResponseBody !== undefined) return res.cachedResponseBody as string; - if (res.ok) return res.json() as Promise; throw new Error(ERRORS.UNEXPECTED); }, () => {