@aws-amplify/adapter-nextjs 1.2.32 → 1.2.33-unstable.d1dfa38.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/cjs/utils/createCookieStorageAdapterFromNextServerContext.js +3 -3
- package/dist/cjs/utils/createCookieStorageAdapterFromNextServerContext.js.map +1 -1
- package/dist/cjs/utils/createRunWithAmplifyServerContext.js +1 -1
- package/dist/cjs/utils/createRunWithAmplifyServerContext.js.map +1 -1
- package/dist/esm/utils/createCookieStorageAdapterFromNextServerContext.d.ts +1 -1
- package/dist/esm/utils/createCookieStorageAdapterFromNextServerContext.mjs +3 -3
- package/dist/esm/utils/createCookieStorageAdapterFromNextServerContext.mjs.map +1 -1
- package/dist/esm/utils/createRunWithAmplifyServerContext.mjs +1 -1
- package/dist/esm/utils/createRunWithAmplifyServerContext.mjs.map +1 -1
- package/package.json +73 -73
- package/src/utils/createCookieStorageAdapterFromNextServerContext.ts +5 -5
- package/src/utils/createRunWithAmplifyServerContext.ts +1 -1
|
@@ -7,7 +7,7 @@ exports.createCookieStorageAdapterFromNextServerContext = exports.DATE_IN_THE_PA
|
|
|
7
7
|
const server_js_1 = require("next/server.js");
|
|
8
8
|
const adapter_core_1 = require("@aws-amplify/core/internals/adapter-core");
|
|
9
9
|
exports.DATE_IN_THE_PAST = new Date(0);
|
|
10
|
-
const createCookieStorageAdapterFromNextServerContext = (context) => {
|
|
10
|
+
const createCookieStorageAdapterFromNextServerContext = async (context) => {
|
|
11
11
|
const { request: req, response: res } = context;
|
|
12
12
|
// When the server context is from `getServerSideProps`, the `req` is an instance
|
|
13
13
|
// of IncomingMessage, and the `res` is an instance of ServerResponse.
|
|
@@ -72,8 +72,8 @@ const createCookieStorageAdapterFromNextRequestAndHttpResponse = (request, respo
|
|
|
72
72
|
...mutableCookieStore,
|
|
73
73
|
};
|
|
74
74
|
};
|
|
75
|
-
const createCookieStorageAdapterFromNextCookies = (cookies) => {
|
|
76
|
-
const cookieStore = cookies();
|
|
75
|
+
const createCookieStorageAdapterFromNextCookies = async (cookies) => {
|
|
76
|
+
const cookieStore = await cookies();
|
|
77
77
|
// When Next cookies() is called in a server component, it returns a readonly
|
|
78
78
|
// cookie store. Hence calling set and delete throws an error. However,
|
|
79
79
|
// cookies() returns a mutable cookie store when called in a server action.
|
|
@@ -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 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,GAAG,CAAC,SAAS,KAAK,UAAU,EAAE;AAC7C,QAAQ,OAAO,uDAAuD,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;AACjF,KAAK;AACL,IAAI,MAAM,EAAE,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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;;"}
|
|
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 = async (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 = async (cookies) => {\n const cookieStore = await 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,OAAO,OAAO,KAAK;AAC3E,IAAI,MAAM,EAAE,OAAO,EAAE,GAAG,EAAE,QAAQ,EAAE,GAAG,EAAE,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CAAC,CAAC;AACnE,KAAK;AACL,IAAI,IAAI,QAAQ,EAAE;AAClB,QAAQ,iBAAiB,CAAC,IAAI,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC;AAC3C,KAAK;AACL,IAAI,IAAI,QAAQ,EAAE;AAClB,QAAQ,iBAAiB,CAAC,IAAI,CAAC,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;;"}
|
|
@@ -20,7 +20,7 @@ const createRunWithAmplifyServerContext = ({ config: resourcesConfig, }) => {
|
|
|
20
20
|
// static rendering uses the same unauthenticated role cross-sever.
|
|
21
21
|
nextServerContext === null
|
|
22
22
|
? core_1.sharedInMemoryStorage
|
|
23
|
-
: (0, adapter_core_1.createKeyValueStorageFromCookieStorageAdapter)((0, createCookieStorageAdapterFromNextServerContext_1.createCookieStorageAdapterFromNextServerContext)(nextServerContext), (0, createTokenValidator_1.createTokenValidator)({
|
|
23
|
+
: (0, adapter_core_1.createKeyValueStorageFromCookieStorageAdapter)(await (0, createCookieStorageAdapterFromNextServerContext_1.createCookieStorageAdapterFromNextServerContext)(nextServerContext), (0, createTokenValidator_1.createTokenValidator)({
|
|
24
24
|
userPoolId: resourcesConfig?.Auth.Cognito?.userPoolId,
|
|
25
25
|
userPoolClientId: resourcesConfig?.Auth.Cognito?.userPoolClientId,
|
|
26
26
|
}));
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createRunWithAmplifyServerContext.js","sources":["../../../src/utils/createRunWithAmplifyServerContext.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.createRunWithAmplifyServerContext = void 0;\nconst core_1 = require(\"@aws-amplify/core\");\nconst adapter_core_1 = require(\"aws-amplify/adapter-core\");\nconst createTokenValidator_1 = require(\"./createTokenValidator\");\nconst createCookieStorageAdapterFromNextServerContext_1 = require(\"./createCookieStorageAdapterFromNextServerContext\");\nconst createRunWithAmplifyServerContext = ({ config: resourcesConfig, }) => {\n const runWithAmplifyServerContext = async ({ nextServerContext, operation }) => {\n // When the Auth config is presented, attempt to create a Amplify server\n // context with token and credentials provider.\n if (resourcesConfig.Auth) {\n const keyValueStorage = \n // When `null` is passed as the value of `nextServerContext`, opt-in\n // unauthenticated role (primarily for static rendering). It's\n // safe to use the singleton `MemoryKeyValueStorage` here, as the\n // static rendering uses the same unauthenticated role cross-sever.\n nextServerContext === null\n ? core_1.sharedInMemoryStorage\n : (0, adapter_core_1.createKeyValueStorageFromCookieStorageAdapter)((0, createCookieStorageAdapterFromNextServerContext_1.createCookieStorageAdapterFromNextServerContext)(nextServerContext), (0, createTokenValidator_1.createTokenValidator)({\n userPoolId: resourcesConfig?.Auth.Cognito?.userPoolId,\n userPoolClientId: resourcesConfig?.Auth.Cognito?.userPoolClientId,\n }));\n const credentialsProvider = (0, adapter_core_1.createAWSCredentialsAndIdentityIdProvider)(resourcesConfig.Auth, keyValueStorage);\n const tokenProvider = (0, adapter_core_1.createUserPoolsTokenProvider)(resourcesConfig.Auth, keyValueStorage);\n return (0, adapter_core_1.runWithAmplifyServerContext)(resourcesConfig, {\n Auth: { credentialsProvider, tokenProvider },\n }, operation);\n }\n // Otherwise it may be the case that auth is not used, e.g. API key.\n // Omitting the `Auth` in the second parameter.\n return (0, adapter_core_1.runWithAmplifyServerContext)(resourcesConfig, {}, operation);\n };\n return runWithAmplifyServerContext;\n};\nexports.createRunWithAmplifyServerContext = createRunWithAmplifyServerContext;\n"],"names":[],"mappings":";;AACA;AACA;AACA,MAAM,CAAC,cAAc,CAAC,OAAO,EAAE,YAAY,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;AAC9D,OAAO,CAAC,iCAAiC,GAAG,KAAK,CAAC,CAAC;AACnD,MAAM,MAAM,GAAG,OAAO,CAAC,mBAAmB,CAAC,CAAC;AAC5C,MAAM,cAAc,GAAG,OAAO,CAAC,0BAA0B,CAAC,CAAC;AAC3D,MAAM,sBAAsB,GAAG,OAAO,CAAC,wBAAwB,CAAC,CAAC;AACjE,MAAM,iDAAiD,GAAG,OAAO,CAAC,mDAAmD,CAAC,CAAC;AACvH,MAAM,iCAAiC,GAAG,CAAC,EAAE,MAAM,EAAE,eAAe,GAAG,KAAK;AAC5E,IAAI,MAAM,2BAA2B,GAAG,OAAO,EAAE,iBAAiB,EAAE,SAAS,EAAE,KAAK;AACpF;AACA;AACA,QAAQ,IAAI,eAAe,CAAC,IAAI,EAAE;AAClC,YAAY,MAAM,eAAe;AACjC;AACA;AACA;AACA;AACA,YAAY,iBAAiB,KAAK,IAAI;AACtC,kBAAkB,MAAM,CAAC,qBAAqB;AAC9C,kBAAkB,IAAI,cAAc,CAAC,6CAA6C,EAAE,IAAI,iDAAiD,CAAC,+CAA+C,EAAE,iBAAiB,CAAC,EAAE,IAAI,sBAAsB,CAAC,oBAAoB,EAAE;
|
|
1
|
+
{"version":3,"file":"createRunWithAmplifyServerContext.js","sources":["../../../src/utils/createRunWithAmplifyServerContext.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.createRunWithAmplifyServerContext = void 0;\nconst core_1 = require(\"@aws-amplify/core\");\nconst adapter_core_1 = require(\"aws-amplify/adapter-core\");\nconst createTokenValidator_1 = require(\"./createTokenValidator\");\nconst createCookieStorageAdapterFromNextServerContext_1 = require(\"./createCookieStorageAdapterFromNextServerContext\");\nconst createRunWithAmplifyServerContext = ({ config: resourcesConfig, }) => {\n const runWithAmplifyServerContext = async ({ nextServerContext, operation }) => {\n // When the Auth config is presented, attempt to create a Amplify server\n // context with token and credentials provider.\n if (resourcesConfig.Auth) {\n const keyValueStorage = \n // When `null` is passed as the value of `nextServerContext`, opt-in\n // unauthenticated role (primarily for static rendering). It's\n // safe to use the singleton `MemoryKeyValueStorage` here, as the\n // static rendering uses the same unauthenticated role cross-sever.\n nextServerContext === null\n ? core_1.sharedInMemoryStorage\n : (0, adapter_core_1.createKeyValueStorageFromCookieStorageAdapter)(await (0, createCookieStorageAdapterFromNextServerContext_1.createCookieStorageAdapterFromNextServerContext)(nextServerContext), (0, createTokenValidator_1.createTokenValidator)({\n userPoolId: resourcesConfig?.Auth.Cognito?.userPoolId,\n userPoolClientId: resourcesConfig?.Auth.Cognito?.userPoolClientId,\n }));\n const credentialsProvider = (0, adapter_core_1.createAWSCredentialsAndIdentityIdProvider)(resourcesConfig.Auth, keyValueStorage);\n const tokenProvider = (0, adapter_core_1.createUserPoolsTokenProvider)(resourcesConfig.Auth, keyValueStorage);\n return (0, adapter_core_1.runWithAmplifyServerContext)(resourcesConfig, {\n Auth: { credentialsProvider, tokenProvider },\n }, operation);\n }\n // Otherwise it may be the case that auth is not used, e.g. API key.\n // Omitting the `Auth` in the second parameter.\n return (0, adapter_core_1.runWithAmplifyServerContext)(resourcesConfig, {}, operation);\n };\n return runWithAmplifyServerContext;\n};\nexports.createRunWithAmplifyServerContext = createRunWithAmplifyServerContext;\n"],"names":[],"mappings":";;AACA;AACA;AACA,MAAM,CAAC,cAAc,CAAC,OAAO,EAAE,YAAY,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;AAC9D,OAAO,CAAC,iCAAiC,GAAG,KAAK,CAAC,CAAC;AACnD,MAAM,MAAM,GAAG,OAAO,CAAC,mBAAmB,CAAC,CAAC;AAC5C,MAAM,cAAc,GAAG,OAAO,CAAC,0BAA0B,CAAC,CAAC;AAC3D,MAAM,sBAAsB,GAAG,OAAO,CAAC,wBAAwB,CAAC,CAAC;AACjE,MAAM,iDAAiD,GAAG,OAAO,CAAC,mDAAmD,CAAC,CAAC;AACvH,MAAM,iCAAiC,GAAG,CAAC,EAAE,MAAM,EAAE,eAAe,GAAG,KAAK;AAC5E,IAAI,MAAM,2BAA2B,GAAG,OAAO,EAAE,iBAAiB,EAAE,SAAS,EAAE,KAAK;AACpF;AACA;AACA,QAAQ,IAAI,eAAe,CAAC,IAAI,EAAE;AAClC,YAAY,MAAM,eAAe;AACjC;AACA;AACA;AACA;AACA,YAAY,iBAAiB,KAAK,IAAI;AACtC,kBAAkB,MAAM,CAAC,qBAAqB;AAC9C,kBAAkB,IAAI,cAAc,CAAC,6CAA6C,EAAE,MAAM,IAAI,iDAAiD,CAAC,+CAA+C,EAAE,iBAAiB,CAAC,EAAE,IAAI,sBAAsB,CAAC,oBAAoB,EAAE;AACtQ,oBAAoB,UAAU,EAAE,eAAe,EAAE,IAAI,CAAC,OAAO,EAAE,UAAU;AACzE,oBAAoB,gBAAgB,EAAE,eAAe,EAAE,IAAI,CAAC,OAAO,EAAE,gBAAgB;AACrF,iBAAiB,CAAC,CAAC,CAAC;AACpB,YAAY,MAAM,mBAAmB,GAAG,IAAI,cAAc,CAAC,yCAAyC,EAAE,eAAe,CAAC,IAAI,EAAE,eAAe,CAAC,CAAC;AAC7I,YAAY,MAAM,aAAa,GAAG,IAAI,cAAc,CAAC,4BAA4B,EAAE,eAAe,CAAC,IAAI,EAAE,eAAe,CAAC,CAAC;AAC1H,YAAY,OAAO,IAAI,cAAc,CAAC,2BAA2B,EAAE,eAAe,EAAE;AACpF,gBAAgB,IAAI,EAAE,EAAE,mBAAmB,EAAE,aAAa,EAAE;AAC5D,aAAa,EAAE,SAAS,CAAC,CAAC;AAC1B,SAAS;AACT;AACA;AACA,QAAQ,OAAO,IAAI,cAAc,CAAC,2BAA2B,EAAE,eAAe,EAAE,EAAE,EAAE,SAAS,CAAC,CAAC;AAC/F,KAAK,CAAC;AACN,IAAI,OAAO,2BAA2B,CAAC;AACvC,CAAC,CAAC;AACF,OAAO,CAAC,iCAAiC,GAAG,iCAAiC;;"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { CookieStorage } from '@aws-amplify/core/internals/adapter-core';
|
|
2
2
|
import { NextServer } from '../types';
|
|
3
3
|
export declare const DATE_IN_THE_PAST: Date;
|
|
4
|
-
export declare const createCookieStorageAdapterFromNextServerContext: (context: NextServer.Context) => CookieStorage.Adapter
|
|
4
|
+
export declare const createCookieStorageAdapterFromNextServerContext: (context: NextServer.Context) => Promise<CookieStorage.Adapter>;
|
|
@@ -4,7 +4,7 @@ import { AmplifyServerContextError } from '@aws-amplify/core/internals/adapter-c
|
|
|
4
4
|
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
5
5
|
// SPDX-License-Identifier: Apache-2.0
|
|
6
6
|
const DATE_IN_THE_PAST = new Date(0);
|
|
7
|
-
const createCookieStorageAdapterFromNextServerContext = (context) => {
|
|
7
|
+
const createCookieStorageAdapterFromNextServerContext = async (context) => {
|
|
8
8
|
const { request: req, response: res } = context;
|
|
9
9
|
// When the server context is from `getServerSideProps`, the `req` is an instance
|
|
10
10
|
// of IncomingMessage, and the `res` is an instance of ServerResponse.
|
|
@@ -68,8 +68,8 @@ const createCookieStorageAdapterFromNextRequestAndHttpResponse = (request, respo
|
|
|
68
68
|
...mutableCookieStore,
|
|
69
69
|
};
|
|
70
70
|
};
|
|
71
|
-
const createCookieStorageAdapterFromNextCookies = (cookies) => {
|
|
72
|
-
const cookieStore = cookies();
|
|
71
|
+
const createCookieStorageAdapterFromNextCookies = async (cookies) => {
|
|
72
|
+
const cookieStore = await cookies();
|
|
73
73
|
// When Next cookies() is called in a server component, it returns a readonly
|
|
74
74
|
// cookie store. Hence calling set and delete throws an error. However,
|
|
75
75
|
// cookies() returns a mutable cookie store when called in a server action.
|
|
@@ -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 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,CAAC,OAAO,EAAE,QAAQ,CAAC,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AA0B,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;;;;"}
|
|
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 = async (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 = async (cookies) => {\n const cookieStore = await 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,OAAO,OAAO,KAAK;AAClF,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,IAAI,IAAI,OAAO,OAAO,KAAK,UAAU,EAAE;AACvC,QAAQ,OAAO,yCAAyC,CAAC,OAAO,CAAC,CAAC;AAClE,KAAK;AACL;AACA,IAAI,MAAM,IAAI,yBAAyB,CAAC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|
|
17
17
|
// static rendering uses the same unauthenticated role cross-sever.
|
|
18
18
|
nextServerContext === null
|
|
19
19
|
? sharedInMemoryStorage
|
|
20
|
-
: createKeyValueStorageFromCookieStorageAdapter(createCookieStorageAdapterFromNextServerContext(nextServerContext), createTokenValidator({
|
|
20
|
+
: createKeyValueStorageFromCookieStorageAdapter(await createCookieStorageAdapterFromNextServerContext(nextServerContext), createTokenValidator({
|
|
21
21
|
userPoolId: resourcesConfig?.Auth.Cognito?.userPoolId,
|
|
22
22
|
userPoolClientId: resourcesConfig?.Auth.Cognito?.userPoolClientId,
|
|
23
23
|
}));
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createRunWithAmplifyServerContext.mjs","sources":["../../../src/utils/createRunWithAmplifyServerContext.ts"],"sourcesContent":["// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\nimport { sharedInMemoryStorage } from '@aws-amplify/core';\nimport { createAWSCredentialsAndIdentityIdProvider, createKeyValueStorageFromCookieStorageAdapter, createUserPoolsTokenProvider, runWithAmplifyServerContext as runWithAmplifyServerContextCore, } from 'aws-amplify/adapter-core';\nimport { createTokenValidator } from './createTokenValidator';\nimport { createCookieStorageAdapterFromNextServerContext } from './createCookieStorageAdapterFromNextServerContext';\nexport const createRunWithAmplifyServerContext = ({ config: resourcesConfig, }) => {\n const runWithAmplifyServerContext = async ({ nextServerContext, operation }) => {\n // When the Auth config is presented, attempt to create a Amplify server\n // context with token and credentials provider.\n if (resourcesConfig.Auth) {\n const keyValueStorage = \n // When `null` is passed as the value of `nextServerContext`, opt-in\n // unauthenticated role (primarily for static rendering). It's\n // safe to use the singleton `MemoryKeyValueStorage` here, as the\n // static rendering uses the same unauthenticated role cross-sever.\n nextServerContext === null\n ? sharedInMemoryStorage\n : createKeyValueStorageFromCookieStorageAdapter(createCookieStorageAdapterFromNextServerContext(nextServerContext), createTokenValidator({\n userPoolId: resourcesConfig?.Auth.Cognito?.userPoolId,\n userPoolClientId: resourcesConfig?.Auth.Cognito?.userPoolClientId,\n }));\n const credentialsProvider = createAWSCredentialsAndIdentityIdProvider(resourcesConfig.Auth, keyValueStorage);\n const tokenProvider = createUserPoolsTokenProvider(resourcesConfig.Auth, keyValueStorage);\n return runWithAmplifyServerContextCore(resourcesConfig, {\n Auth: { credentialsProvider, tokenProvider },\n }, operation);\n }\n // Otherwise it may be the case that auth is not used, e.g. API key.\n // Omitting the `Auth` in the second parameter.\n return runWithAmplifyServerContextCore(resourcesConfig, {}, operation);\n };\n return runWithAmplifyServerContext;\n};\n"],"names":["runWithAmplifyServerContext","runWithAmplifyServerContextCore"],"mappings":";;;;;AAAA;AACA;AAKY,MAAC,iCAAiC,GAAG,CAAC,EAAE,MAAM,EAAE,eAAe,GAAG,KAAK;AACnF,IAAI,MAAMA,6BAA2B,GAAG,OAAO,EAAE,iBAAiB,EAAE,SAAS,EAAE,KAAK;AACpF;AACA;AACA,QAAQ,IAAI,eAAe,CAAC,IAAI,EAAE;AAClC,YAAY,MAAM,eAAe;AACjC;AACA;AACA;AACA;AACA,YAAY,iBAAiB,KAAK,IAAI;AACtC,kBAAkB,qBAAqB;AACvC,kBAAkB,6CAA6C,CAAC,+CAA+C,CAAC,iBAAiB,CAAC,EAAE,oBAAoB,CAAC;
|
|
1
|
+
{"version":3,"file":"createRunWithAmplifyServerContext.mjs","sources":["../../../src/utils/createRunWithAmplifyServerContext.ts"],"sourcesContent":["// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\nimport { sharedInMemoryStorage } from '@aws-amplify/core';\nimport { createAWSCredentialsAndIdentityIdProvider, createKeyValueStorageFromCookieStorageAdapter, createUserPoolsTokenProvider, runWithAmplifyServerContext as runWithAmplifyServerContextCore, } from 'aws-amplify/adapter-core';\nimport { createTokenValidator } from './createTokenValidator';\nimport { createCookieStorageAdapterFromNextServerContext } from './createCookieStorageAdapterFromNextServerContext';\nexport const createRunWithAmplifyServerContext = ({ config: resourcesConfig, }) => {\n const runWithAmplifyServerContext = async ({ nextServerContext, operation }) => {\n // When the Auth config is presented, attempt to create a Amplify server\n // context with token and credentials provider.\n if (resourcesConfig.Auth) {\n const keyValueStorage = \n // When `null` is passed as the value of `nextServerContext`, opt-in\n // unauthenticated role (primarily for static rendering). It's\n // safe to use the singleton `MemoryKeyValueStorage` here, as the\n // static rendering uses the same unauthenticated role cross-sever.\n nextServerContext === null\n ? sharedInMemoryStorage\n : createKeyValueStorageFromCookieStorageAdapter(await createCookieStorageAdapterFromNextServerContext(nextServerContext), createTokenValidator({\n userPoolId: resourcesConfig?.Auth.Cognito?.userPoolId,\n userPoolClientId: resourcesConfig?.Auth.Cognito?.userPoolClientId,\n }));\n const credentialsProvider = createAWSCredentialsAndIdentityIdProvider(resourcesConfig.Auth, keyValueStorage);\n const tokenProvider = createUserPoolsTokenProvider(resourcesConfig.Auth, keyValueStorage);\n return runWithAmplifyServerContextCore(resourcesConfig, {\n Auth: { credentialsProvider, tokenProvider },\n }, operation);\n }\n // Otherwise it may be the case that auth is not used, e.g. API key.\n // Omitting the `Auth` in the second parameter.\n return runWithAmplifyServerContextCore(resourcesConfig, {}, operation);\n };\n return runWithAmplifyServerContext;\n};\n"],"names":["runWithAmplifyServerContext","runWithAmplifyServerContextCore"],"mappings":";;;;;AAAA;AACA;AAKY,MAAC,iCAAiC,GAAG,CAAC,EAAE,MAAM,EAAE,eAAe,GAAG,KAAK;AACnF,IAAI,MAAMA,6BAA2B,GAAG,OAAO,EAAE,iBAAiB,EAAE,SAAS,EAAE,KAAK;AACpF;AACA;AACA,QAAQ,IAAI,eAAe,CAAC,IAAI,EAAE;AAClC,YAAY,MAAM,eAAe;AACjC;AACA;AACA;AACA;AACA,YAAY,iBAAiB,KAAK,IAAI;AACtC,kBAAkB,qBAAqB;AACvC,kBAAkB,6CAA6C,CAAC,MAAM,+CAA+C,CAAC,iBAAiB,CAAC,EAAE,oBAAoB,CAAC;AAC/J,oBAAoB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46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
2
|
+
"author": "Amazon Web Services",
|
|
3
|
+
"name": "@aws-amplify/adapter-nextjs",
|
|
4
|
+
"version": "1.2.33-unstable.d1dfa38.0+d1dfa38",
|
|
5
|
+
"description": "The adapter for the supporting of using Amplify APIs in Next.js.",
|
|
6
|
+
"peerDependencies": {
|
|
7
|
+
"aws-amplify": "6.10.3-unstable.d1dfa38.0+d1dfa38",
|
|
8
|
+
"next": ">=13.5.0 <16.0.0"
|
|
9
|
+
},
|
|
10
|
+
"dependencies": {
|
|
11
|
+
"aws-jwt-verify": "^4.0.1",
|
|
12
|
+
"cookie": "^0.7.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.10.3-unstable.d1dfa38.0+d1dfa38",
|
|
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": "d1dfa384e9667f7d6c8b2674b0cf1d6973345880"
|
|
75
75
|
}
|
|
@@ -11,9 +11,9 @@ import { NextServer } from '../types';
|
|
|
11
11
|
|
|
12
12
|
export const DATE_IN_THE_PAST = new Date(0);
|
|
13
13
|
|
|
14
|
-
export const createCookieStorageAdapterFromNextServerContext = (
|
|
14
|
+
export const createCookieStorageAdapterFromNextServerContext = async (
|
|
15
15
|
context: NextServer.Context,
|
|
16
|
-
): CookieStorage.Adapter => {
|
|
16
|
+
): Promise<CookieStorage.Adapter> => {
|
|
17
17
|
const { request: req, response: res } =
|
|
18
18
|
context as Partial<NextServer.GetServerSidePropsContext>;
|
|
19
19
|
|
|
@@ -110,10 +110,10 @@ const createCookieStorageAdapterFromNextRequestAndHttpResponse = (
|
|
|
110
110
|
};
|
|
111
111
|
};
|
|
112
112
|
|
|
113
|
-
const createCookieStorageAdapterFromNextCookies = (
|
|
113
|
+
const createCookieStorageAdapterFromNextCookies = async (
|
|
114
114
|
cookies: NextServer.ServerComponentContext['cookies'],
|
|
115
|
-
): CookieStorage.Adapter => {
|
|
116
|
-
const cookieStore = cookies();
|
|
115
|
+
): Promise<CookieStorage.Adapter> => {
|
|
116
|
+
const cookieStore = await cookies();
|
|
117
117
|
|
|
118
118
|
// When Next cookies() is called in a server component, it returns a readonly
|
|
119
119
|
// cookie store. Hence calling set and delete throws an error. However,
|
|
@@ -32,7 +32,7 @@ export const createRunWithAmplifyServerContext = ({
|
|
|
32
32
|
nextServerContext === null
|
|
33
33
|
? sharedInMemoryStorage
|
|
34
34
|
: createKeyValueStorageFromCookieStorageAdapter(
|
|
35
|
-
createCookieStorageAdapterFromNextServerContext(
|
|
35
|
+
await createCookieStorageAdapterFromNextServerContext(
|
|
36
36
|
nextServerContext,
|
|
37
37
|
),
|
|
38
38
|
createTokenValidator({
|