From 38425793172e1f033778ca0c188748a90cdce502 Mon Sep 17 00:00:00 2001 From: tarunama Date: Fri, 15 Jan 2021 14:48:22 +0900 Subject: [PATCH 01/10] refactor: multiple line import --- packages/next/next-server/server/api-utils.ts | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/packages/next/next-server/server/api-utils.ts b/packages/next/next-server/server/api-utils.ts index f04176eb33d96..8f1919d626b77 100644 --- a/packages/next/next-server/server/api-utils.ts +++ b/packages/next/next-server/server/api-utils.ts @@ -1,11 +1,21 @@ -import { IncomingMessage, ServerResponse } from 'http' +import { + IncomingMessage, + ServerResponse, +} from 'http' import { parse } from 'next/dist/compiled/content-type' import { CookieSerializeOptions } from 'next/dist/compiled/cookie' import getRawBody from 'raw-body' import { PageConfig } from 'next/types' import { Stream } from 'stream' -import { isResSent, NextApiRequest, NextApiResponse } from '../lib/utils' -import { decryptWithSecret, encryptWithSecret } from './crypto-utils' +import { + isResSent, + NextApiRequest, + NextApiResponse, +} from '../lib/utils' +import { + decryptWithSecret, + encryptWithSecret, +} from './crypto-utils' import { interopDefault } from './load-components' import { Params } from './router' import { sendEtagResponse } from './send-payload' From af93cfdcb8c1506c59a9305364e02306cb17f20c Mon Sep 17 00:00:00 2001 From: tarunama Date: Fri, 15 Jan 2021 14:57:33 +0900 Subject: [PATCH 02/10] refactor: add return types to function --- packages/next/next-server/server/api-utils.ts | 22 ++++++------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/packages/next/next-server/server/api-utils.ts b/packages/next/next-server/server/api-utils.ts index 8f1919d626b77..d0cb0d9eeceb8 100644 --- a/packages/next/next-server/server/api-utils.ts +++ b/packages/next/next-server/server/api-utils.ts @@ -1,21 +1,11 @@ -import { - IncomingMessage, - ServerResponse, -} from 'http' +import { IncomingMessage, ServerResponse } from 'http' import { parse } from 'next/dist/compiled/content-type' import { CookieSerializeOptions } from 'next/dist/compiled/cookie' import getRawBody from 'raw-body' import { PageConfig } from 'next/types' import { Stream } from 'stream' -import { - isResSent, - NextApiRequest, - NextApiResponse, -} from '../lib/utils' -import { - decryptWithSecret, - encryptWithSecret, -} from './crypto-utils' +import { isResSent, NextApiRequest, NextApiResponse } from '../lib/utils' +import { decryptWithSecret, encryptWithSecret } from './crypto-utils' import { interopDefault } from './load-components' import { Params } from './router' import { sendEtagResponse } from './send-payload' @@ -38,7 +28,7 @@ export async function apiResolver( apiContext: __ApiPreviewProps, propagateError: boolean, onError?: ({ err }: { err: any }) => Promise -) { +): Promise { const apiReq = req as NextApiRequest const apiRes = res as NextApiResponse @@ -176,7 +166,9 @@ function parseJson(str: string): object { * Parse cookies from `req` header * @param req request object */ -export function getCookieParser(req: IncomingMessage) { +export function getCookieParser( + req: IncomingMessage +): () => NextApiRequestCookies { return function parseCookie(): NextApiRequestCookies { const header: undefined | string | string[] = req.headers.cookie From 3781f0b41983558f7914511b0b847afeec6535eb Mon Sep 17 00:00:00 2001 From: tarunama Date: Fri, 15 Jan 2021 14:59:21 +0900 Subject: [PATCH 03/10] refactor: add warn message --- packages/next/next-server/server/api-utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/next/next-server/server/api-utils.ts b/packages/next/next-server/server/api-utils.ts index d0cb0d9eeceb8..2c30b95aacbee 100644 --- a/packages/next/next-server/server/api-utils.ts +++ b/packages/next/next-server/server/api-utils.ts @@ -304,7 +304,7 @@ export function tryGetPreviewData( try { cookies = getCookies() } catch { - // TODO: warn + console.warn('Did not get data from NextApiRequestCookies') return false } From 4f23b89f23e7165ce6e0b174386e3afa4a1f2661 Mon Sep 17 00:00:00 2001 From: tarunama Date: Fri, 15 Jan 2021 15:04:53 +0900 Subject: [PATCH 04/10] chore: add warn message when failed jsonwebtoken.verify --- packages/next/next-server/server/api-utils.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/next/next-server/server/api-utils.ts b/packages/next/next-server/server/api-utils.ts index 2c30b95aacbee..d4a80875705a3 100644 --- a/packages/next/next-server/server/api-utils.ts +++ b/packages/next/next-server/server/api-utils.ts @@ -339,8 +339,8 @@ export function tryGetPreviewData( tokenPreviewData, options.previewModeSigningKey ) as typeof encryptedPreviewData - } catch { - // TODO: warn + } catch (e) { + console.warn(`Failed to veriry jsonwebtoken: ${e}`) clearPreviewData(res as NextApiResponse) return false } From 7c7c312ea5762309ac01033e4fda3fcd394ac7fc Mon Sep 17 00:00:00 2001 From: tarunama Date: Fri, 15 Jan 2021 15:15:20 +0900 Subject: [PATCH 05/10] refactor: more readable that validating setPreviewData argument --- packages/next/next-server/server/api-utils.ts | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/packages/next/next-server/server/api-utils.ts b/packages/next/next-server/server/api-utils.ts index d4a80875705a3..947c882503020 100644 --- a/packages/next/next-server/server/api-utils.ts +++ b/packages/next/next-server/server/api-utils.ts @@ -371,22 +371,17 @@ function setPreviewData( maxAge?: number } & __ApiPreviewProps ): NextApiResponse { - if ( - typeof options.previewModeId !== 'string' || - options.previewModeId.length < 16 - ) { + const isNotValidData = (str: string): boolean => { + return typeof str !== 'string' || str.length < 16 + } + + if (isNotValidData(options.previewModeId)) { throw new Error('invariant: invalid previewModeId') } - if ( - typeof options.previewModeEncryptionKey !== 'string' || - options.previewModeEncryptionKey.length < 16 - ) { + if (isNotValidData(options.previewModeEncryptionKey)) { throw new Error('invariant: invalid previewModeEncryptionKey') } - if ( - typeof options.previewModeSigningKey !== 'string' || - options.previewModeSigningKey.length < 16 - ) { + if (isNotValidData(options.previewModeSigningKey)) { throw new Error('invariant: invalid previewModeSigningKey') } From 119d1d23a91f4c3f9f79cee43c5a9821f68161bb Mon Sep 17 00:00:00 2001 From: tarunama Date: Sun, 17 Jan 2021 10:55:51 +0900 Subject: [PATCH 06/10] chore: kindful warning message for enduser --- packages/next/next-server/server/api-utils.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/next/next-server/server/api-utils.ts b/packages/next/next-server/server/api-utils.ts index 947c882503020..ea980ffb793a2 100644 --- a/packages/next/next-server/server/api-utils.ts +++ b/packages/next/next-server/server/api-utils.ts @@ -304,7 +304,7 @@ export function tryGetPreviewData( try { cookies = getCookies() } catch { - console.warn('Did not get data from NextApiRequestCookies') + console.warn('If you have not enabled cookies, please do so.') return false } @@ -340,7 +340,7 @@ export function tryGetPreviewData( options.previewModeSigningKey ) as typeof encryptedPreviewData } catch (e) { - console.warn(`Failed to veriry jsonwebtoken: ${e}`) + // TODO: warn clearPreviewData(res as NextApiResponse) return false } From 685879d00679512ee12afa5d8e5c6076f90c65b3 Mon Sep 17 00:00:00 2001 From: tarunama Date: Sun, 17 Jan 2021 10:57:44 +0900 Subject: [PATCH 07/10] chore: cleanup --- packages/next/next-server/server/api-utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/next/next-server/server/api-utils.ts b/packages/next/next-server/server/api-utils.ts index ea980ffb793a2..05479fac87277 100644 --- a/packages/next/next-server/server/api-utils.ts +++ b/packages/next/next-server/server/api-utils.ts @@ -339,7 +339,7 @@ export function tryGetPreviewData( tokenPreviewData, options.previewModeSigningKey ) as typeof encryptedPreviewData - } catch (e) { + } catch { // TODO: warn clearPreviewData(res as NextApiResponse) return false From dfd2e57cc5217b5d8c5c252f9dbc0381ac463911 Mon Sep 17 00:00:00 2001 From: Tim Neutkens Date: Mon, 25 Jan 2021 18:24:56 +0100 Subject: [PATCH 08/10] Apply suggestions from code review --- packages/next/next-server/server/api-utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/next/next-server/server/api-utils.ts b/packages/next/next-server/server/api-utils.ts index 05479fac87277..a5f9430ab0776 100644 --- a/packages/next/next-server/server/api-utils.ts +++ b/packages/next/next-server/server/api-utils.ts @@ -304,7 +304,7 @@ export function tryGetPreviewData( try { cookies = getCookies() } catch { - console.warn('If you have not enabled cookies, please do so.') + // TODO: warn return false } From d7bfae6bb7a3afc6e7f0db9d2a2a6f14b23b4f3b Mon Sep 17 00:00:00 2001 From: Tim Neutkens Date: Mon, 25 Jan 2021 18:28:15 +0100 Subject: [PATCH 09/10] Update api-utils.ts --- packages/next/next-server/server/api-utils.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/next/next-server/server/api-utils.ts b/packages/next/next-server/server/api-utils.ts index a5f9430ab0776..6a1f3cea86e7b 100644 --- a/packages/next/next-server/server/api-utils.ts +++ b/packages/next/next-server/server/api-utils.ts @@ -364,6 +364,10 @@ export function tryGetPreviewData( } } +function isNotValidData (str: string): boolean { + return typeof str !== 'string' || str.length < 16 +} + function setPreviewData( res: NextApiResponse, data: object | string, // TODO: strict runtime type checking @@ -371,10 +375,6 @@ function setPreviewData( maxAge?: number } & __ApiPreviewProps ): NextApiResponse { - const isNotValidData = (str: string): boolean => { - return typeof str !== 'string' || str.length < 16 - } - if (isNotValidData(options.previewModeId)) { throw new Error('invariant: invalid previewModeId') } From c04002b8e1c9bfec0c4470061d2caa8015ee61f2 Mon Sep 17 00:00:00 2001 From: Tim Neutkens Date: Tue, 26 Jan 2021 10:30:57 +0100 Subject: [PATCH 10/10] Run prettier --- packages/next/next-server/server/api-utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/next/next-server/server/api-utils.ts b/packages/next/next-server/server/api-utils.ts index 6a1f3cea86e7b..e318de716a512 100644 --- a/packages/next/next-server/server/api-utils.ts +++ b/packages/next/next-server/server/api-utils.ts @@ -364,7 +364,7 @@ export function tryGetPreviewData( } } -function isNotValidData (str: string): boolean { +function isNotValidData(str: string): boolean { return typeof str !== 'string' || str.length < 16 }