Skip to content
Permalink

Comparing changes

This is a direct comparison between two commits made in this repository or its related repositories. View the default comparison for this range or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: aws-amplify/amplify-js
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: d03d71121cef7a9b85562eb19051d774f26845b2
Choose a base ref
..
head repository: aws-amplify/amplify-js
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 7a0fd561f9d017387bccfc2c358395af8abf162c
Choose a head ref
Original file line number Diff line number Diff line change
@@ -224,7 +224,7 @@ describe('createCookieStorageAdapterFromNextServerContext', () => {
it('should throw error when no cookie storage adapter is created from the context', () => {
expect(() =>
createCookieStorageAdapterFromNextServerContext({
request: {} as any,
request: undefined,
response: new ServerResponse({} as any),
} as any)
).toThrowError();
Original file line number Diff line number Diff line change
@@ -7,18 +7,41 @@ import {
AmplifyServerContextError,
CookieStorage,
} from '@aws-amplify/core/internals/adapter-core';
import { IncomingMessage, ServerResponse } from 'http';

export const DATE_IN_THE_PAST = new Date(0);

export const createCookieStorageAdapterFromNextServerContext = (
context: NextServer.Context
): CookieStorage.Adapter => {
const { request, response } = context as
| NextServer.NextRequestAndNextResponseContext
| NextServer.NextRequestAndResponseContext;
const { request: req, response: res } =
context as Partial<NextServer.GetServerSidePropsContext>;

// When the server context is from `getServerSideProps`, the `req` is an instance
// of IncomingMessage, and the `res` is an instance of ServerResponse.
// We cannot import these two classes here from `http` as it breaks in Next
// Edge Runtime. Hence, we check the methods that we need to use for creating
// cookie adapter.
if (
req &&
res &&
Object.prototype.toString.call(req.cookies) === '[object Object]' &&
typeof res.setHeader === 'function'
) {
return createCookieStorageAdapterFromGetServerSidePropsContext(req, res);
}

if (request instanceof NextRequest && response) {
const { request, response } = context as Partial<
| NextServer.NextRequestAndNextResponseContext
| NextServer.NextRequestAndResponseContext
>;

// When the server context is from `middleware`, the `request` is an instance
// of `NextRequest`.
// When the server context is from a route handler, the `request` is an `Proxy`
// wrapped `Request`.
// The `NextRequest` and the `Proxy` are sharing the same interface by Next
// implementation. So we don't need to detect the difference.
if (request && response) {
if (response instanceof NextResponse) {
return createCookieStorageAdapterFromNextRequestAndNextResponse(
request,
@@ -32,21 +55,14 @@ export const createCookieStorageAdapterFromNextServerContext = (
}
}

const { cookies } = context as
| NextServer.ServerComponentContext
| NextServer.ServerActionContext;
const { cookies } = context as Partial<
NextServer.ServerComponentContext | NextServer.ServerActionContext
>;

if (typeof cookies === 'function') {
return createCookieStorageAdapterFromNextCookies(cookies);
}

const { request: req, response: res } =
context as NextServer.GetServerSidePropsContext;

if (req instanceof IncomingMessage && res instanceof ServerResponse) {
return createCookieStorageAdapterFromGetServerSidePropsContext(req, res);
}

// This should not happen normally.
throw new AmplifyServerContextError({
message:
Loading