@aws-amplify/adapter-nextjs 1.2.16-unstable.969686f.0 → 1.2.16

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.
@@ -124,10 +124,25 @@ const createCookieStorageAdapterFromGetServerSidePropsContext = (request, respon
124
124
  return allCookies;
125
125
  },
126
126
  set(name, value, options) {
127
- response.appendHeader('Set-Cookie', `${ensureEncodedForJSCookie(name)}=${value};${options ? serializeSetCookieOptions(options) : ''}`);
127
+ const encodedName = ensureEncodedForJSCookie(name);
128
+ const existingValues = getExistingSetCookieValues(response.getHeader('Set-Cookie'));
129
+ // if the cookies have already been set, we don't need to set them again.
130
+ if (existingValues.findIndex(cookieValue => cookieValue.startsWith(`${encodedName}=`) &&
131
+ !cookieValue.startsWith(`${encodedName}=;`)) > -1) {
132
+ return;
133
+ }
134
+ response.appendHeader('Set-Cookie', `${encodedName}=${value};${options ? serializeSetCookieOptions(options) : ''}`);
128
135
  },
129
136
  delete(name) {
130
- response.appendHeader('Set-Cookie', `${ensureEncodedForJSCookie(name)}=;Expires=${exports.DATE_IN_THE_PAST.toUTCString()}`);
137
+ const encodedName = ensureEncodedForJSCookie(name);
138
+ const setCookieValue = `${encodedName}=;Expires=${exports.DATE_IN_THE_PAST.toUTCString()}`;
139
+ const existingValues = getExistingSetCookieValues(response.getHeader('Set-Cookie'));
140
+ // if the value for cookie deletion is already in the Set-Cookie header, we
141
+ // don't need to add the deletion value again.
142
+ if (existingValues.includes(setCookieValue)) {
143
+ return;
144
+ }
145
+ response.appendHeader('Set-Cookie', setCookieValue);
131
146
  },
132
147
  };
133
148
  };
@@ -173,4 +188,5 @@ const serializeSetCookieOptions = (options) => {
173
188
  // The implementation is borrowed from js-cookie without escaping `[()]` as
174
189
  // we are not using those chars in the auth keys.
175
190
  const ensureEncodedForJSCookie = (name) => encodeURIComponent(name).replace(/%(2[346B]|5E|60|7C)/g, decodeURIComponent);
191
+ const getExistingSetCookieValues = (values) => values === undefined ? [] : Array.isArray(values) ? values : [String(values)];
176
192
  //# sourceMappingURL=createCookieStorageAdapterFromNextServerContext.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"createCookieStorageAdapterFromNextServerContext.js","sources":["../../../src/utils/createCookieStorageAdapterFromNextServerContext.ts"],"sourcesContent":["\"use strict\";\n// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\nObject.defineProperty(exports, \"__esModule\", { value: true });\nexports.createCookieStorageAdapterFromNextServerContext = exports.DATE_IN_THE_PAST = void 0;\nconst server_js_1 = require(\"next/server.js\");\nconst adapter_core_1 = require(\"@aws-amplify/core/internals/adapter-core\");\nexports.DATE_IN_THE_PAST = new Date(0);\nconst createCookieStorageAdapterFromNextServerContext = (context) => {\n const { request: req, response: res } = context;\n // When the server context is from `getServerSideProps`, the `req` is an instance\n // of IncomingMessage, and the `res` is an instance of ServerResponse.\n // We cannot import these two classes here from `http` as it breaks in Next\n // Edge Runtime. Hence, we check the methods that we need to use for creating\n // cookie adapter.\n if (req &&\n res &&\n Object.prototype.toString.call(req.cookies) === '[object Object]' &&\n typeof res.setHeader === 'function') {\n return createCookieStorageAdapterFromGetServerSidePropsContext(req, res);\n }\n const { request, response } = context;\n // When the server context is from `middleware`, the `request` is an instance\n // of `NextRequest`.\n // When the server context is from a route handler, the `request` is an `Proxy`\n // wrapped `Request`.\n // The `NextRequest` and the `Proxy` are sharing the same interface by Next\n // implementation. So we don't need to detect the difference.\n if (request && response) {\n if (response instanceof server_js_1.NextResponse) {\n return createCookieStorageAdapterFromNextRequestAndNextResponse(request, response);\n }\n else {\n return createCookieStorageAdapterFromNextRequestAndHttpResponse(request, response);\n }\n }\n const { cookies } = context;\n if (typeof cookies === 'function') {\n return createCookieStorageAdapterFromNextCookies(cookies);\n }\n // This should not happen normally.\n throw new adapter_core_1.AmplifyServerContextError({\n message: 'Attempted to create cookie storage adapter from an unsupported Next.js server context.',\n });\n};\nexports.createCookieStorageAdapterFromNextServerContext = createCookieStorageAdapterFromNextServerContext;\nconst createCookieStorageAdapterFromNextRequestAndNextResponse = (request, response) => {\n const readonlyCookieStore = request.cookies;\n const mutableCookieStore = response.cookies;\n return {\n get(name) {\n return readonlyCookieStore.get(ensureEncodedForJSCookie(name));\n },\n getAll: readonlyCookieStore.getAll.bind(readonlyCookieStore),\n set(name, value, options) {\n mutableCookieStore.set(ensureEncodedForJSCookie(name), value, options);\n },\n delete(name) {\n mutableCookieStore.delete(ensureEncodedForJSCookie(name));\n },\n };\n};\nconst createCookieStorageAdapterFromNextRequestAndHttpResponse = (request, response) => {\n const readonlyCookieStore = request.cookies;\n const mutableCookieStore = createMutableCookieStoreFromHeaders(response.headers);\n return {\n get(name) {\n return readonlyCookieStore.get(ensureEncodedForJSCookie(name));\n },\n getAll: readonlyCookieStore.getAll.bind(readonlyCookieStore),\n ...mutableCookieStore,\n };\n};\nconst createCookieStorageAdapterFromNextCookies = (cookies) => {\n const cookieStore = cookies();\n // When Next cookies() is called in a server component, it returns a readonly\n // cookie store. Hence calling set and delete throws an error. However,\n // cookies() returns a mutable cookie store when called in a server action.\n // We have no way to detect which one is returned, so we try to call set and delete\n // and safely ignore the error if it is thrown.\n const setFunc = (name, value, options) => {\n try {\n cookieStore.set(ensureEncodedForJSCookie(name), value, options);\n }\n catch {\n // no-op\n }\n };\n const deleteFunc = name => {\n try {\n cookieStore.delete(ensureEncodedForJSCookie(name));\n }\n catch {\n // no-op\n }\n };\n return {\n get(name) {\n return cookieStore.get(ensureEncodedForJSCookie(name));\n },\n getAll: cookieStore.getAll.bind(cookieStore),\n set: setFunc,\n delete: deleteFunc,\n };\n};\nconst createCookieStorageAdapterFromGetServerSidePropsContext = (request, response) => {\n const cookiesMap = { ...request.cookies };\n const allCookies = Object.entries(cookiesMap).map(([name, value]) => ({\n name,\n value,\n }));\n return {\n get(name) {\n const value = cookiesMap[ensureEncodedForJSCookie(name)];\n return value\n ? {\n name,\n value,\n }\n : undefined;\n },\n getAll() {\n return allCookies;\n },\n set(name, value, options) {\n response.appendHeader('Set-Cookie', `${ensureEncodedForJSCookie(name)}=${value};${options ? serializeSetCookieOptions(options) : ''}`);\n },\n delete(name) {\n response.appendHeader('Set-Cookie', `${ensureEncodedForJSCookie(name)}=;Expires=${exports.DATE_IN_THE_PAST.toUTCString()}`);\n },\n };\n};\nconst createMutableCookieStoreFromHeaders = (headers) => {\n const setFunc = (name, value, options) => {\n headers.append('Set-Cookie', `${ensureEncodedForJSCookie(name)}=${value};${options ? serializeSetCookieOptions(options) : ''}`);\n };\n const deleteFunc = name => {\n headers.append('Set-Cookie', `${ensureEncodedForJSCookie(name)}=;Expires=${exports.DATE_IN_THE_PAST.toUTCString()}`);\n };\n return {\n set: setFunc,\n delete: deleteFunc,\n };\n};\nconst serializeSetCookieOptions = (options) => {\n const { expires, domain, httpOnly, sameSite, secure, path } = options;\n const serializedOptions = [];\n if (domain) {\n serializedOptions.push(`Domain=${domain}`);\n }\n if (expires) {\n serializedOptions.push(`Expires=${expires.toUTCString()}`);\n }\n if (httpOnly) {\n serializedOptions.push(`HttpOnly`);\n }\n if (sameSite) {\n serializedOptions.push(`SameSite=${sameSite}`);\n }\n if (secure) {\n serializedOptions.push(`Secure`);\n }\n if (path) {\n serializedOptions.push(`Path=${path}`);\n }\n return serializedOptions.join(';');\n};\n// Ensures the cookie names are encoded in order to look up the cookie store\n// that is manipulated by js-cookie on the client side.\n// Details of the js-cookie encoding behavior see:\n// https://github.com/js-cookie/js-cookie#encoding\n// The implementation is borrowed from js-cookie without escaping `[()]` as\n// we are not using those chars in the auth keys.\nconst ensureEncodedForJSCookie = (name) => encodeURIComponent(name).replace(/%(2[346B]|5E|60|7C)/g, decodeURIComponent);\n"],"names":[],"mappings":";;AACA;AACA;AACA,MAAM,CAAC,cAAc,CAAC,OAAO,EAAE,YAAY,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;AAC9D,OAAO,CAAC,+CAA+C,GAAG,OAAO,CAA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AAC,CAAC;AACzC,KAAK;AACL,IAAI,IAAI,IAAI,EAAE;AACd,QAAQ,iBAAiB,CAAC,IAAI,CAAC,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC;AAC/C,KAAK;AACL,IAAI,OAAO,iBAAiB,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AACvC,CAAC,CAAC;AACF;AACA;AACA;AACA;AACA;AACA;AACA,MAAM,wBAAwB,GAAG,CAAC,IAAI,KAAK,kBAAkB,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC,sBAAsB,EAAE,kBAAkB,CAAC;;"}
1
+ {"version":3,"file":"createCookieStorageAdapterFromNextServerContext.js","sources":["../../../src/utils/createCookieStorageAdapterFromNextServerContext.ts"],"sourcesContent":["\"use strict\";\n// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\nObject.defineProperty(exports, \"__esModule\", { value: true });\nexports.createCookieStorageAdapterFromNextServerContext = exports.DATE_IN_THE_PAST = void 0;\nconst server_js_1 = require(\"next/server.js\");\nconst adapter_core_1 = require(\"@aws-amplify/core/internals/adapter-core\");\nexports.DATE_IN_THE_PAST = new Date(0);\nconst createCookieStorageAdapterFromNextServerContext = (context) => {\n const { request: req, response: res } = context;\n // When the server context is from `getServerSideProps`, the `req` is an instance\n // of IncomingMessage, and the `res` is an instance of ServerResponse.\n // We cannot import these two classes here from `http` as it breaks in Next\n // Edge Runtime. Hence, we check the methods that we need to use for creating\n // cookie adapter.\n if (req &&\n res &&\n Object.prototype.toString.call(req.cookies) === '[object Object]' &&\n typeof res.setHeader === 'function') {\n return createCookieStorageAdapterFromGetServerSidePropsContext(req, res);\n }\n const { request, response } = context;\n // When the server context is from `middleware`, the `request` is an instance\n // of `NextRequest`.\n // When the server context is from a route handler, the `request` is an `Proxy`\n // wrapped `Request`.\n // The `NextRequest` and the `Proxy` are sharing the same interface by Next\n // implementation. So we don't need to detect the difference.\n if (request && response) {\n if (response instanceof server_js_1.NextResponse) {\n return createCookieStorageAdapterFromNextRequestAndNextResponse(request, response);\n }\n else {\n return createCookieStorageAdapterFromNextRequestAndHttpResponse(request, response);\n }\n }\n const { cookies } = context;\n if (typeof cookies === 'function') {\n return createCookieStorageAdapterFromNextCookies(cookies);\n }\n // This should not happen normally.\n throw new adapter_core_1.AmplifyServerContextError({\n message: 'Attempted to create cookie storage adapter from an unsupported Next.js server context.',\n });\n};\nexports.createCookieStorageAdapterFromNextServerContext = createCookieStorageAdapterFromNextServerContext;\nconst createCookieStorageAdapterFromNextRequestAndNextResponse = (request, response) => {\n const readonlyCookieStore = request.cookies;\n const mutableCookieStore = response.cookies;\n return {\n get(name) {\n return readonlyCookieStore.get(ensureEncodedForJSCookie(name));\n },\n getAll: readonlyCookieStore.getAll.bind(readonlyCookieStore),\n set(name, value, options) {\n mutableCookieStore.set(ensureEncodedForJSCookie(name), value, options);\n },\n delete(name) {\n mutableCookieStore.delete(ensureEncodedForJSCookie(name));\n },\n };\n};\nconst createCookieStorageAdapterFromNextRequestAndHttpResponse = (request, response) => {\n const readonlyCookieStore = request.cookies;\n const mutableCookieStore = createMutableCookieStoreFromHeaders(response.headers);\n return {\n get(name) {\n return readonlyCookieStore.get(ensureEncodedForJSCookie(name));\n },\n getAll: readonlyCookieStore.getAll.bind(readonlyCookieStore),\n ...mutableCookieStore,\n };\n};\nconst createCookieStorageAdapterFromNextCookies = (cookies) => {\n const cookieStore = cookies();\n // When Next cookies() is called in a server component, it returns a readonly\n // cookie store. Hence calling set and delete throws an error. However,\n // cookies() returns a mutable cookie store when called in a server action.\n // We have no way to detect which one is returned, so we try to call set and delete\n // and safely ignore the error if it is thrown.\n const setFunc = (name, value, options) => {\n try {\n cookieStore.set(ensureEncodedForJSCookie(name), value, options);\n }\n catch {\n // no-op\n }\n };\n const deleteFunc = name => {\n try {\n cookieStore.delete(ensureEncodedForJSCookie(name));\n }\n catch {\n // no-op\n }\n };\n return {\n get(name) {\n return cookieStore.get(ensureEncodedForJSCookie(name));\n },\n getAll: cookieStore.getAll.bind(cookieStore),\n set: setFunc,\n delete: deleteFunc,\n };\n};\nconst createCookieStorageAdapterFromGetServerSidePropsContext = (request, response) => {\n const cookiesMap = { ...request.cookies };\n const allCookies = Object.entries(cookiesMap).map(([name, value]) => ({\n name,\n value,\n }));\n return {\n get(name) {\n const value = cookiesMap[ensureEncodedForJSCookie(name)];\n return value\n ? {\n name,\n value,\n }\n : undefined;\n },\n getAll() {\n return allCookies;\n },\n set(name, value, options) {\n const encodedName = ensureEncodedForJSCookie(name);\n const existingValues = getExistingSetCookieValues(response.getHeader('Set-Cookie'));\n // if the cookies have already been set, we don't need to set them again.\n if (existingValues.findIndex(cookieValue => cookieValue.startsWith(`${encodedName}=`) &&\n !cookieValue.startsWith(`${encodedName}=;`)) > -1) {\n return;\n }\n response.appendHeader('Set-Cookie', `${encodedName}=${value};${options ? serializeSetCookieOptions(options) : ''}`);\n },\n delete(name) {\n const encodedName = ensureEncodedForJSCookie(name);\n const setCookieValue = `${encodedName}=;Expires=${exports.DATE_IN_THE_PAST.toUTCString()}`;\n const existingValues = getExistingSetCookieValues(response.getHeader('Set-Cookie'));\n // if the value for cookie deletion is already in the Set-Cookie header, we\n // don't need to add the deletion value again.\n if (existingValues.includes(setCookieValue)) {\n return;\n }\n response.appendHeader('Set-Cookie', setCookieValue);\n },\n };\n};\nconst createMutableCookieStoreFromHeaders = (headers) => {\n const setFunc = (name, value, options) => {\n headers.append('Set-Cookie', `${ensureEncodedForJSCookie(name)}=${value};${options ? serializeSetCookieOptions(options) : ''}`);\n };\n const deleteFunc = name => {\n headers.append('Set-Cookie', `${ensureEncodedForJSCookie(name)}=;Expires=${exports.DATE_IN_THE_PAST.toUTCString()}`);\n };\n return {\n set: setFunc,\n delete: deleteFunc,\n };\n};\nconst serializeSetCookieOptions = (options) => {\n const { expires, domain, httpOnly, sameSite, secure, path } = options;\n const serializedOptions = [];\n if (domain) {\n serializedOptions.push(`Domain=${domain}`);\n }\n if (expires) {\n serializedOptions.push(`Expires=${expires.toUTCString()}`);\n }\n if (httpOnly) {\n serializedOptions.push(`HttpOnly`);\n }\n if (sameSite) {\n serializedOptions.push(`SameSite=${sameSite}`);\n }\n if (secure) {\n serializedOptions.push(`Secure`);\n }\n if (path) {\n serializedOptions.push(`Path=${path}`);\n }\n return serializedOptions.join(';');\n};\n// Ensures the cookie names are encoded in order to look up the cookie store\n// that is manipulated by js-cookie on the client side.\n// Details of the js-cookie encoding behavior see:\n// https://github.com/js-cookie/js-cookie#encoding\n// The implementation is borrowed from js-cookie without escaping `[()]` as\n// we are not using those chars in the auth keys.\nconst ensureEncodedForJSCookie = (name) => encodeURIComponent(name).replace(/%(2[346B]|5E|60|7C)/g, decodeURIComponent);\nconst getExistingSetCookieValues = (values) => values === undefined ? [] : Array.isArray(values) ? values : [String(values)];\n"],"names":[],"mappings":";;AACA;AACA;AACA,MAAM,CAAC,cAAc,CAAC,OAAO,EAAE,YAAY,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;AAC9D,OAAO,CAAC,+CAA+C,GAAG,OAAO,CAAC,gBAAgB,GAAG,KAAK,CAAC,CAAC;AAC5F,MAAM,WAAW,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAAC;AAC9C,MAAM,cAAc,GAAG,OAAO,CAAC,0CAA0C,CAAC,CAAC;AAC3E,OAAO,CAAC,gBAAgB,GAAG,IAAI,IAAI,CAAC,CAAC,CAAC,CAAC;AACvC,MAAM,+CAA+C,GAAG,CAAC,OAAO,KAAK;AACrE,IAAI,MAAM,EAAE,OAAO,EAAE,GAAG,EAAE,QAAQ,EAAE,GAAG,EAAE,GAAG,OAAO,CAAC;AACpD;AACA;AACA;AACA;AACA;AACA,IAAI,IAAI,GAAG;AACX,QAAQ,GAAG;AACX,QAAQ,MAAM,CAAC,SAAS,CAAC,QAAQ,CAAC,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,KAAK,iBAAiB;AACzE,QAAQ,OAAO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,CAAC,SAAS,EAAE,QAAQ,CAAC,CAAC,CAAC,CAAC;AACvD,KAAK;AACL,IAAI,IAAI,MAAM,EAAE;AAChB,QAAQ,iBAAiB,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC;AACzC,KAAK;AACL,IAAI,IAAI,IAAI,EAAE;AACd,QAAQ,iBAAiB,CAAC,IAAI,CAAC,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC;AAC/C,KAAK;AACL,IAAI,OAAO,iBAAiB,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AACvC,CAAC,CAAC;AACF;AACA;AACA;AACA;AACA;AACA;AACA,MAAM,wBAAwB,GAAG,CAAC,IAAI,KAAK,kBAAkB,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC,sBAAsB,EAAE,kBAAkB,CAAC,CAAC;AACxH,MAAM,0BAA0B,GAAG,CAAC,MAAM,KAAK,MAAM,KAAK,SAAS,GAAG,EAAE,GAAG,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,GAAG,MAAM,GAAG,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;;"}
@@ -120,10 +120,25 @@ const createCookieStorageAdapterFromGetServerSidePropsContext = (request, respon
120
120
  return allCookies;
121
121
  },
122
122
  set(name, value, options) {
123
- response.appendHeader('Set-Cookie', `${ensureEncodedForJSCookie(name)}=${value};${options ? serializeSetCookieOptions(options) : ''}`);
123
+ const encodedName = ensureEncodedForJSCookie(name);
124
+ const existingValues = getExistingSetCookieValues(response.getHeader('Set-Cookie'));
125
+ // if the cookies have already been set, we don't need to set them again.
126
+ if (existingValues.findIndex(cookieValue => cookieValue.startsWith(`${encodedName}=`) &&
127
+ !cookieValue.startsWith(`${encodedName}=;`)) > -1) {
128
+ return;
129
+ }
130
+ response.appendHeader('Set-Cookie', `${encodedName}=${value};${options ? serializeSetCookieOptions(options) : ''}`);
124
131
  },
125
132
  delete(name) {
126
- response.appendHeader('Set-Cookie', `${ensureEncodedForJSCookie(name)}=;Expires=${DATE_IN_THE_PAST.toUTCString()}`);
133
+ const encodedName = ensureEncodedForJSCookie(name);
134
+ const setCookieValue = `${encodedName}=;Expires=${DATE_IN_THE_PAST.toUTCString()}`;
135
+ const existingValues = getExistingSetCookieValues(response.getHeader('Set-Cookie'));
136
+ // if the value for cookie deletion is already in the Set-Cookie header, we
137
+ // don't need to add the deletion value again.
138
+ if (existingValues.includes(setCookieValue)) {
139
+ return;
140
+ }
141
+ response.appendHeader('Set-Cookie', setCookieValue);
127
142
  },
128
143
  };
129
144
  };
@@ -169,6 +184,7 @@ const serializeSetCookieOptions = (options) => {
169
184
  // The implementation is borrowed from js-cookie without escaping `[()]` as
170
185
  // we are not using those chars in the auth keys.
171
186
  const ensureEncodedForJSCookie = (name) => encodeURIComponent(name).replace(/%(2[346B]|5E|60|7C)/g, decodeURIComponent);
187
+ const getExistingSetCookieValues = (values) => values === undefined ? [] : Array.isArray(values) ? values : [String(values)];
172
188
 
173
189
  export { DATE_IN_THE_PAST, createCookieStorageAdapterFromNextServerContext };
174
190
  //# sourceMappingURL=createCookieStorageAdapterFromNextServerContext.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"createCookieStorageAdapterFromNextServerContext.mjs","sources":["../../../src/utils/createCookieStorageAdapterFromNextServerContext.ts"],"sourcesContent":["// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\nimport { NextResponse } from 'next/server.js';\nimport { AmplifyServerContextError, } from '@aws-amplify/core/internals/adapter-core';\nexport const DATE_IN_THE_PAST = new Date(0);\nexport const createCookieStorageAdapterFromNextServerContext = (context) => {\n const { request: req, response: res } = context;\n // When the server context is from `getServerSideProps`, the `req` is an instance\n // of IncomingMessage, and the `res` is an instance of ServerResponse.\n // We cannot import these two classes here from `http` as it breaks in Next\n // Edge Runtime. Hence, we check the methods that we need to use for creating\n // cookie adapter.\n if (req &&\n res &&\n Object.prototype.toString.call(req.cookies) === '[object Object]' &&\n typeof res.setHeader === 'function') {\n return createCookieStorageAdapterFromGetServerSidePropsContext(req, res);\n }\n const { request, response } = context;\n // When the server context is from `middleware`, the `request` is an instance\n // of `NextRequest`.\n // When the server context is from a route handler, the `request` is an `Proxy`\n // wrapped `Request`.\n // The `NextRequest` and the `Proxy` are sharing the same interface by Next\n // implementation. So we don't need to detect the difference.\n if (request && response) {\n if (response instanceof NextResponse) {\n return createCookieStorageAdapterFromNextRequestAndNextResponse(request, response);\n }\n else {\n return createCookieStorageAdapterFromNextRequestAndHttpResponse(request, response);\n }\n }\n const { cookies } = context;\n if (typeof cookies === 'function') {\n return createCookieStorageAdapterFromNextCookies(cookies);\n }\n // This should not happen normally.\n throw new AmplifyServerContextError({\n message: 'Attempted to create cookie storage adapter from an unsupported Next.js server context.',\n });\n};\nconst createCookieStorageAdapterFromNextRequestAndNextResponse = (request, response) => {\n const readonlyCookieStore = request.cookies;\n const mutableCookieStore = response.cookies;\n return {\n get(name) {\n return readonlyCookieStore.get(ensureEncodedForJSCookie(name));\n },\n getAll: readonlyCookieStore.getAll.bind(readonlyCookieStore),\n set(name, value, options) {\n mutableCookieStore.set(ensureEncodedForJSCookie(name), value, options);\n },\n delete(name) {\n mutableCookieStore.delete(ensureEncodedForJSCookie(name));\n },\n };\n};\nconst createCookieStorageAdapterFromNextRequestAndHttpResponse = (request, response) => {\n const readonlyCookieStore = request.cookies;\n const mutableCookieStore = createMutableCookieStoreFromHeaders(response.headers);\n return {\n get(name) {\n return readonlyCookieStore.get(ensureEncodedForJSCookie(name));\n },\n getAll: readonlyCookieStore.getAll.bind(readonlyCookieStore),\n ...mutableCookieStore,\n };\n};\nconst createCookieStorageAdapterFromNextCookies = (cookies) => {\n const cookieStore = cookies();\n // When Next cookies() is called in a server component, it returns a readonly\n // cookie store. Hence calling set and delete throws an error. However,\n // cookies() returns a mutable cookie store when called in a server action.\n // We have no way to detect which one is returned, so we try to call set and delete\n // and safely ignore the error if it is thrown.\n const setFunc = (name, value, options) => {\n try {\n cookieStore.set(ensureEncodedForJSCookie(name), value, options);\n }\n catch {\n // no-op\n }\n };\n const deleteFunc = name => {\n try {\n cookieStore.delete(ensureEncodedForJSCookie(name));\n }\n catch {\n // no-op\n }\n };\n return {\n get(name) {\n return cookieStore.get(ensureEncodedForJSCookie(name));\n },\n getAll: cookieStore.getAll.bind(cookieStore),\n set: setFunc,\n delete: deleteFunc,\n };\n};\nconst createCookieStorageAdapterFromGetServerSidePropsContext = (request, response) => {\n const cookiesMap = { ...request.cookies };\n const allCookies = Object.entries(cookiesMap).map(([name, value]) => ({\n name,\n value,\n }));\n return {\n get(name) {\n const value = cookiesMap[ensureEncodedForJSCookie(name)];\n return value\n ? {\n name,\n value,\n }\n : undefined;\n },\n getAll() {\n return allCookies;\n },\n set(name, value, options) {\n response.appendHeader('Set-Cookie', `${ensureEncodedForJSCookie(name)}=${value};${options ? serializeSetCookieOptions(options) : ''}`);\n },\n delete(name) {\n response.appendHeader('Set-Cookie', `${ensureEncodedForJSCookie(name)}=;Expires=${DATE_IN_THE_PAST.toUTCString()}`);\n },\n };\n};\nconst createMutableCookieStoreFromHeaders = (headers) => {\n const setFunc = (name, value, options) => {\n headers.append('Set-Cookie', `${ensureEncodedForJSCookie(name)}=${value};${options ? serializeSetCookieOptions(options) : ''}`);\n };\n const deleteFunc = name => {\n headers.append('Set-Cookie', `${ensureEncodedForJSCookie(name)}=;Expires=${DATE_IN_THE_PAST.toUTCString()}`);\n };\n return {\n set: setFunc,\n delete: deleteFunc,\n };\n};\nconst serializeSetCookieOptions = (options) => {\n const { expires, domain, httpOnly, sameSite, secure, path } = options;\n const serializedOptions = [];\n if (domain) {\n serializedOptions.push(`Domain=${domain}`);\n }\n if (expires) {\n serializedOptions.push(`Expires=${expires.toUTCString()}`);\n }\n if (httpOnly) {\n serializedOptions.push(`HttpOnly`);\n }\n if (sameSite) {\n serializedOptions.push(`SameSite=${sameSite}`);\n }\n if (secure) {\n serializedOptions.push(`Secure`);\n }\n if (path) {\n serializedOptions.push(`Path=${path}`);\n }\n return serializedOptions.join(';');\n};\n// Ensures the cookie names are encoded in order to look up the cookie store\n// that is manipulated by js-cookie on the client side.\n// Details of the js-cookie encoding behavior see:\n// https://github.com/js-cookie/js-cookie#encoding\n// The implementation is borrowed from js-cookie without escaping `[()]` as\n// we are not using those chars in the auth keys.\nconst ensureEncodedForJSCookie = (name) => encodeURIComponent(name).replace(/%(2[346B]|5E|60|7C)/g, decodeURIComponent);\n"],"names":[],"mappings":";;;AAAA;AACA;AAGY,MAAC,gBAAgB,GAAG,IAAI,IAAI,CAAC,CAAC,EAAE;AAChC,MAAC,+CAA+C,GAAG,CAAC,OAAO,KAAK;AAC5E,IAAI,MAAM,EAAE,OAAO,EAAE,GAAG,EAAE,QAAQ,EAAE,GAAG,EAAE,GAAG,OAAO,CAAC;AACpD;AACA;AACA;AACA;AACA;AACA,IAAI,IAAI,GAAG;AACX,QAAQ,GAAG;AACX,QAAQ,MAAM,CAAC,SAAS,CAAC,QAAQ,CAAC,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,KAAK,iBAAiB;AACzE,QAAQ,OAAO,GAAG,CAAC,SAAS,KAAK,UAAU,EAAE;AAC7C,QAAQ,OAAO,uDAAuD,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;AACjF,KAAK;AACL,IAAI,MAAM,EAAE,OAAO,EAAE,QAAQ,EAAE,GAAG,OAAO,CAAC;AAC1C;AACA;AACA;AACA;AACA;AACA;AACA,IAAI,IAAI,OAAO,IAAI,QAAQ,EAAE;AAC7B,QAAQ,IAAI,QAAQ,YAAY,YAAY,EAAE;AAC9C,YAAY,OAAO,wDAAwD,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC;AAC/F,SAAS;AACT,aAAa;AACb,YAAY,OAAO,wDAAwD,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC;AAC/F,SAAS;AACT,KAAK;AACL,IAAI,MAAM,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC;AAChC,I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QAAQ,iBAAiB,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC;AACzC,KAAK;AACL,IAAI,IAAI,IAAI,EAAE;AACd,QAAQ,iBAAiB,CAAC,IAAI,CAAC,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC;AAC/C,KAAK;AACL,IAAI,OAAO,iBAAiB,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AACvC,CAAC,CAAC;AACF;AACA;AACA;AACA;AACA;AACA;AACA,MAAM,wBAAwB,GAAG,CAAC,IAAI,KAAK,kBAAkB,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC,sBAAsB,EAAE,kBAAkB,CAAC;;;;"}
1
+ {"version":3,"file":"createCookieStorageAdapterFromNextServerContext.mjs","sources":["../../../src/utils/createCookieStorageAdapterFromNextServerContext.ts"],"sourcesContent":["// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\nimport { NextResponse } from 'next/server.js';\nimport { AmplifyServerContextError, } from '@aws-amplify/core/internals/adapter-core';\nexport const DATE_IN_THE_PAST = new Date(0);\nexport const createCookieStorageAdapterFromNextServerContext = (context) => {\n const { request: req, response: res } = context;\n // When the server context is from `getServerSideProps`, the `req` is an instance\n // of IncomingMessage, and the `res` is an instance of ServerResponse.\n // We cannot import these two classes here from `http` as it breaks in Next\n // Edge Runtime. Hence, we check the methods that we need to use for creating\n // cookie adapter.\n if (req &&\n res &&\n Object.prototype.toString.call(req.cookies) === '[object Object]' &&\n typeof res.setHeader === 'function') {\n return createCookieStorageAdapterFromGetServerSidePropsContext(req, res);\n }\n const { request, response } = context;\n // When the server context is from `middleware`, the `request` is an instance\n // of `NextRequest`.\n // When the server context is from a route handler, the `request` is an `Proxy`\n // wrapped `Request`.\n // The `NextRequest` and the `Proxy` are sharing the same interface by Next\n // implementation. So we don't need to detect the difference.\n if (request && response) {\n if (response instanceof NextResponse) {\n return createCookieStorageAdapterFromNextRequestAndNextResponse(request, response);\n }\n else {\n return createCookieStorageAdapterFromNextRequestAndHttpResponse(request, response);\n }\n }\n const { cookies } = context;\n if (typeof cookies === 'function') {\n return createCookieStorageAdapterFromNextCookies(cookies);\n }\n // This should not happen normally.\n throw new AmplifyServerContextError({\n message: 'Attempted to create cookie storage adapter from an unsupported Next.js server context.',\n });\n};\nconst createCookieStorageAdapterFromNextRequestAndNextResponse = (request, response) => {\n const readonlyCookieStore = request.cookies;\n const mutableCookieStore = response.cookies;\n return {\n get(name) {\n return readonlyCookieStore.get(ensureEncodedForJSCookie(name));\n },\n getAll: readonlyCookieStore.getAll.bind(readonlyCookieStore),\n set(name, value, options) {\n mutableCookieStore.set(ensureEncodedForJSCookie(name), value, options);\n },\n delete(name) {\n mutableCookieStore.delete(ensureEncodedForJSCookie(name));\n },\n };\n};\nconst createCookieStorageAdapterFromNextRequestAndHttpResponse = (request, response) => {\n const readonlyCookieStore = request.cookies;\n const mutableCookieStore = createMutableCookieStoreFromHeaders(response.headers);\n return {\n get(name) {\n return readonlyCookieStore.get(ensureEncodedForJSCookie(name));\n },\n getAll: readonlyCookieStore.getAll.bind(readonlyCookieStore),\n ...mutableCookieStore,\n };\n};\nconst createCookieStorageAdapterFromNextCookies = (cookies) => {\n const cookieStore = cookies();\n // When Next cookies() is called in a server component, it returns a readonly\n // cookie store. Hence calling set and delete throws an error. However,\n // cookies() returns a mutable cookie store when called in a server action.\n // We have no way to detect which one is returned, so we try to call set and delete\n // and safely ignore the error if it is thrown.\n const setFunc = (name, value, options) => {\n try {\n cookieStore.set(ensureEncodedForJSCookie(name), value, options);\n }\n catch {\n // no-op\n }\n };\n const deleteFunc = name => {\n try {\n cookieStore.delete(ensureEncodedForJSCookie(name));\n }\n catch {\n // no-op\n }\n };\n return {\n get(name) {\n return cookieStore.get(ensureEncodedForJSCookie(name));\n },\n getAll: cookieStore.getAll.bind(cookieStore),\n set: setFunc,\n delete: deleteFunc,\n };\n};\nconst createCookieStorageAdapterFromGetServerSidePropsContext = (request, response) => {\n const cookiesMap = { ...request.cookies };\n const allCookies = Object.entries(cookiesMap).map(([name, value]) => ({\n name,\n value,\n }));\n return {\n get(name) {\n const value = cookiesMap[ensureEncodedForJSCookie(name)];\n return value\n ? {\n name,\n value,\n }\n : undefined;\n },\n getAll() {\n return allCookies;\n },\n set(name, value, options) {\n const encodedName = ensureEncodedForJSCookie(name);\n const existingValues = getExistingSetCookieValues(response.getHeader('Set-Cookie'));\n // if the cookies have already been set, we don't need to set them again.\n if (existingValues.findIndex(cookieValue => cookieValue.startsWith(`${encodedName}=`) &&\n !cookieValue.startsWith(`${encodedName}=;`)) > -1) {\n return;\n }\n response.appendHeader('Set-Cookie', `${encodedName}=${value};${options ? serializeSetCookieOptions(options) : ''}`);\n },\n delete(name) {\n const encodedName = ensureEncodedForJSCookie(name);\n const setCookieValue = `${encodedName}=;Expires=${DATE_IN_THE_PAST.toUTCString()}`;\n const existingValues = getExistingSetCookieValues(response.getHeader('Set-Cookie'));\n // if the value for cookie deletion is already in the Set-Cookie header, we\n // don't need to add the deletion value again.\n if (existingValues.includes(setCookieValue)) {\n return;\n }\n response.appendHeader('Set-Cookie', setCookieValue);\n },\n };\n};\nconst createMutableCookieStoreFromHeaders = (headers) => {\n const setFunc = (name, value, options) => {\n headers.append('Set-Cookie', `${ensureEncodedForJSCookie(name)}=${value};${options ? serializeSetCookieOptions(options) : ''}`);\n };\n const deleteFunc = name => {\n headers.append('Set-Cookie', `${ensureEncodedForJSCookie(name)}=;Expires=${DATE_IN_THE_PAST.toUTCString()}`);\n };\n return {\n set: setFunc,\n delete: deleteFunc,\n };\n};\nconst serializeSetCookieOptions = (options) => {\n const { expires, domain, httpOnly, sameSite, secure, path } = options;\n const serializedOptions = [];\n if (domain) {\n serializedOptions.push(`Domain=${domain}`);\n }\n if (expires) {\n serializedOptions.push(`Expires=${expires.toUTCString()}`);\n }\n if (httpOnly) {\n serializedOptions.push(`HttpOnly`);\n }\n if (sameSite) {\n serializedOptions.push(`SameSite=${sameSite}`);\n }\n if (secure) {\n serializedOptions.push(`Secure`);\n }\n if (path) {\n serializedOptions.push(`Path=${path}`);\n }\n return serializedOptions.join(';');\n};\n// Ensures the cookie names are encoded in order to look up the cookie store\n// that is manipulated by js-cookie on the client side.\n// Details of the js-cookie encoding behavior see:\n// https://github.com/js-cookie/js-cookie#encoding\n// The implementation is borrowed from js-cookie without escaping `[()]` as\n// we are not using those chars in the auth keys.\nconst ensureEncodedForJSCookie = (name) => encodeURIComponent(name).replace(/%(2[346B]|5E|60|7C)/g, decodeURIComponent);\nconst getExistingSetCookieValues = (values) => values === undefined ? [] : Array.isArray(values) ? values : [String(values)];\n"],"names":[],"mappings":";;;AAAA;AACA;AAGY,MAAC,gBAAgB,GAAG,IAAI,IAAI,CAAC,CAAC,EAAE;AAChC,MAAC,+CAA+C,GAAG,CAAC,OAAO,KAAK;AAC5E,IAAI,MAAM,EAAE,OAAO,EAAE,GAAG,EAAE,QAAQ,EAAE,GAAG,EAAE,GAAG,OAAO,CAAC;AACpD;AACA;AACA;AACA;AACA;AACA,IAAI,IAAI,GAAG;AACX,QAAQ,GAAG;AACX,QAAQ,MAAM,CAAC,SAAS,CAAC,QAAQ,CAAC,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,KAAK,iBAAiB;AACzE,QAAQ,OAAO,GAAG,CAAC,SAAS,KAAK,UAAU,EAAE;AAC7C,QAAQ,OAAO,uDAAuD,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;AACjF,KAAK;AACL,IAAI,MAAM,EAAE,OAAO,EAAE,QAAQ,EAAE,GAAG,OAAO,CAAC;AAC1C;AACA;AACA;AACA;AACA;AACA;AACA,IAAI,IAAI,OAAO,IAAI,QAAQ,EAAE;AAC7B,QAAQ,IAAI,QAAQ,YAAY,YAAY,EAAE;AAC9C,YAAY,OAAO,wDAAwD,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC;AAC/F,SAAS;AACT,aAAa;AACb,YAAY,OAAO,wDAAwD,CAA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,CAAC,CAAC;AACxH,MAAM,0BAA0B,GAAG,CAAC,MAAM,KAAK,MAAM,KAAK,SAAS,GAAG,EAAE,GAAG,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,GAAG,MAAM,GAAG,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;;;;"}
package/package.json CHANGED
@@ -1,75 +1,75 @@
1
1
  {
2
- "author": "Amazon Web Services",
3
- "name": "@aws-amplify/adapter-nextjs",
4
- "version": "1.2.16-unstable.969686f.0+969686f",
5
- "description": "The adapter for the supporting of using Amplify APIs in Next.js.",
6
- "peerDependencies": {
7
- "aws-amplify": "6.5.4-unstable.969686f.0+969686f",
8
- "next": ">=13.5.0 <15.0.0"
9
- },
10
- "dependencies": {
11
- "aws-jwt-verify": "^4.0.1",
12
- "cookie": "0.5.0"
13
- },
14
- "devDependencies": {
15
- "@types/cookie": "0.5.1",
16
- "@types/node": "^20.3.1",
17
- "@types/react": "^18.2.13",
18
- "@types/react-dom": "^18.2.6",
19
- "aws-amplify": "6.5.4-unstable.969686f.0+969686f",
20
- "jest-fetch-mock": "3.0.3",
21
- "next": ">= 13.5.0 < 15.0.0",
22
- "typescript": "5.0.2"
23
- },
24
- "publishConfig": {
25
- "access": "public"
26
- },
27
- "bugs": {
28
- "url": "https://github.com/aws/aws-amplify/issues"
29
- },
30
- "exports": {
31
- ".": {
32
- "types": "./dist/esm/index.d.ts",
33
- "import": "./dist/esm/index.mjs",
34
- "require": "./dist/cjs/index.js"
35
- },
36
- "./api": {
37
- "types": "./dist/esm/api/index.d.ts",
38
- "import": "./dist/esm/api/index.mjs",
39
- "require": "./dist/cjs/api/index.js"
40
- },
41
- "./data": {
42
- "types": "./dist/esm/api/index.d.ts",
43
- "import": "./dist/esm/api/index.mjs",
44
- "require": "./dist/cjs/api/index.js"
45
- },
46
- "./package.json": "./package.json"
47
- },
48
- "files": [
49
- "dist/cjs",
50
- "dist/esm",
51
- "src",
52
- "api",
53
- "data"
54
- ],
55
- "homepage": "https://aws-amplify.github.io/",
56
- "license": "Apache-2.0",
57
- "main": "./dist/cjs/index.js",
58
- "module": "./dist/esm/index.mjs",
59
- "typings": "./dist/esm/index.d.ts",
60
- "sideEffects": false,
61
- "scripts": {
62
- "build": "npm run clean && npm run build:esm-cjs",
63
- "build-with-test": "npm test && npm run build",
64
- "build:esm-cjs": "rollup --forceExit -c rollup.config.mjs",
65
- "build:watch": "npm run build:esm-cjs -- --watch",
66
- "clean": "npm run clean:size && rimraf dist",
67
- "clean:size": "rimraf dual-publish-tmp tmp*",
68
- "format": "echo \"Not implemented\"",
69
- "lint": "eslint '**/*.{ts,tsx}' && npm run ts-coverage",
70
- "lint:fix": "eslint '**/*.{ts,tsx}' --fix",
71
- "test": "npm run lint && jest -w 1 --coverage --logHeapUsage",
72
- "ts-coverage": "typescript-coverage-report -p ./tsconfig.build.json -t 90.31"
73
- },
74
- "gitHead": "969686fb9ab614e8660f88eac2f3083430627c68"
2
+ "author": "Amazon Web Services",
3
+ "name": "@aws-amplify/adapter-nextjs",
4
+ "version": "1.2.16",
5
+ "description": "The adapter for the supporting of using Amplify APIs in Next.js.",
6
+ "peerDependencies": {
7
+ "aws-amplify": "^6.0.7",
8
+ "next": ">=13.5.0 <15.0.0"
9
+ },
10
+ "dependencies": {
11
+ "aws-jwt-verify": "^4.0.1",
12
+ "cookie": "0.5.0"
13
+ },
14
+ "devDependencies": {
15
+ "@types/cookie": "0.5.1",
16
+ "@types/node": "^20.3.1",
17
+ "@types/react": "^18.2.13",
18
+ "@types/react-dom": "^18.2.6",
19
+ "aws-amplify": "6.5.4",
20
+ "jest-fetch-mock": "3.0.3",
21
+ "next": ">= 13.5.0 < 15.0.0",
22
+ "typescript": "5.0.2"
23
+ },
24
+ "publishConfig": {
25
+ "access": "public"
26
+ },
27
+ "bugs": {
28
+ "url": "https://github.com/aws/aws-amplify/issues"
29
+ },
30
+ "exports": {
31
+ ".": {
32
+ "types": "./dist/esm/index.d.ts",
33
+ "import": "./dist/esm/index.mjs",
34
+ "require": "./dist/cjs/index.js"
35
+ },
36
+ "./api": {
37
+ "types": "./dist/esm/api/index.d.ts",
38
+ "import": "./dist/esm/api/index.mjs",
39
+ "require": "./dist/cjs/api/index.js"
40
+ },
41
+ "./data": {
42
+ "types": "./dist/esm/api/index.d.ts",
43
+ "import": "./dist/esm/api/index.mjs",
44
+ "require": "./dist/cjs/api/index.js"
45
+ },
46
+ "./package.json": "./package.json"
47
+ },
48
+ "files": [
49
+ "dist/cjs",
50
+ "dist/esm",
51
+ "src",
52
+ "api",
53
+ "data"
54
+ ],
55
+ "homepage": "https://aws-amplify.github.io/",
56
+ "license": "Apache-2.0",
57
+ "main": "./dist/cjs/index.js",
58
+ "module": "./dist/esm/index.mjs",
59
+ "typings": "./dist/esm/index.d.ts",
60
+ "sideEffects": false,
61
+ "scripts": {
62
+ "build": "npm run clean && npm run build:esm-cjs",
63
+ "build-with-test": "npm test && npm run build",
64
+ "build:esm-cjs": "rollup --forceExit -c rollup.config.mjs",
65
+ "build:watch": "npm run build:esm-cjs -- --watch",
66
+ "clean": "npm run clean:size && rimraf dist",
67
+ "clean:size": "rimraf dual-publish-tmp tmp*",
68
+ "format": "echo \"Not implemented\"",
69
+ "lint": "eslint '**/*.{ts,tsx}' && npm run ts-coverage",
70
+ "lint:fix": "eslint '**/*.{ts,tsx}' --fix",
71
+ "test": "npm run lint && jest -w 1 --coverage --logHeapUsage",
72
+ "ts-coverage": "typescript-coverage-report -p ./tsconfig.build.json -t 90.31"
73
+ },
74
+ "gitHead": "eb86c26ff25c4385437a3d89b9f7be73e90d9c9d"
75
75
  }
@@ -171,20 +171,44 @@ const createCookieStorageAdapterFromGetServerSidePropsContext = (
171
171
  return allCookies;
172
172
  },
173
173
  set(name, value, options) {
174
+ const encodedName = ensureEncodedForJSCookie(name);
175
+
176
+ const existingValues = getExistingSetCookieValues(
177
+ response.getHeader('Set-Cookie'),
178
+ );
179
+
180
+ // if the cookies have already been set, we don't need to set them again.
181
+ if (
182
+ existingValues.findIndex(
183
+ cookieValue =>
184
+ cookieValue.startsWith(`${encodedName}=`) &&
185
+ !cookieValue.startsWith(`${encodedName}=;`),
186
+ ) > -1
187
+ ) {
188
+ return;
189
+ }
190
+
174
191
  response.appendHeader(
175
192
  'Set-Cookie',
176
- `${ensureEncodedForJSCookie(name)}=${value};${
193
+ `${encodedName}=${value};${
177
194
  options ? serializeSetCookieOptions(options) : ''
178
195
  }`,
179
196
  );
180
197
  },
181
198
  delete(name) {
182
- response.appendHeader(
183
- 'Set-Cookie',
184
- `${ensureEncodedForJSCookie(
185
- name,
186
- )}=;Expires=${DATE_IN_THE_PAST.toUTCString()}`,
199
+ const encodedName = ensureEncodedForJSCookie(name);
200
+ const setCookieValue = `${encodedName}=;Expires=${DATE_IN_THE_PAST.toUTCString()}`;
201
+ const existingValues = getExistingSetCookieValues(
202
+ response.getHeader('Set-Cookie'),
187
203
  );
204
+
205
+ // if the value for cookie deletion is already in the Set-Cookie header, we
206
+ // don't need to add the deletion value again.
207
+ if (existingValues.includes(setCookieValue)) {
208
+ return;
209
+ }
210
+
211
+ response.appendHeader('Set-Cookie', setCookieValue);
188
212
  },
189
213
  };
190
214
  };
@@ -250,3 +274,8 @@ const serializeSetCookieOptions = (
250
274
  // we are not using those chars in the auth keys.
251
275
  const ensureEncodedForJSCookie = (name: string): string =>
252
276
  encodeURIComponent(name).replace(/%(2[346B]|5E|60|7C)/g, decodeURIComponent);
277
+
278
+ const getExistingSetCookieValues = (
279
+ values: number | string | string[] | undefined,
280
+ ): string[] =>
281
+ values === undefined ? [] : Array.isArray(values) ? values : [String(values)];