Skip to content

Commit

Permalink
Mark this.router protected (vercel#23573)
Browse files Browse the repository at this point in the history
* Mark this.router protected

Seems that some people (vercel#23558) are using Next.js internals based on the typings that TS generates automatically even though these values are not documented / should not be used in applications. This ensures the router value on Server is not used by accident.

* Mark all variables as protected

vercel#23349

* fix type error

Co-authored-by: JJ Kasper <jj@jjsweb.site>
  • Loading branch information
2 people authored and SokratisVidros committed Apr 20, 2021
1 parent 1100161 commit 20e2107
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
6 changes: 4 additions & 2 deletions packages/next/next-server/server/image-optimizer.ts
Expand Up @@ -8,6 +8,7 @@ import isAnimated from 'next/dist/compiled/is-animated'
import { join } from 'path'
import Stream from 'stream'
import nodeUrl, { UrlWithParsedQuery } from 'url'
import { NextConfig } from '../../next-server/server/config-shared'
import { fileExists } from '../../lib/file-exists'
import { ImageConfig, imageConfigDefault } from './image-config'
import { processBuffer, Operation } from './lib/squoosh/main'
Expand All @@ -30,9 +31,10 @@ export async function imageOptimizer(
server: Server,
req: IncomingMessage,
res: ServerResponse,
parsedUrl: UrlWithParsedQuery
parsedUrl: UrlWithParsedQuery,
nextConfig: NextConfig,
distDir: string
) {
const { nextConfig, distDir } = server
const imageData: ImageConfig = nextConfig.images || imageConfigDefault
const { deviceSizes = [], imageSizes = [], domains = [], loader } = imageData

Expand Down
35 changes: 21 additions & 14 deletions packages/next/next-server/server/next-server.ts
Expand Up @@ -124,18 +124,18 @@ export type ServerConstructor = {
}

export default class Server {
dir: string
quiet: boolean
nextConfig: NextConfig
distDir: string
pagesDir?: string
publicDir: string
hasStaticDir: boolean
serverBuildDir: string
pagesManifest?: PagesManifest
buildId: string
minimalMode: boolean
renderOpts: {
protected dir: string
protected quiet: boolean
protected nextConfig: NextConfig
protected distDir: string
protected pagesDir?: string
protected publicDir: string
protected hasStaticDir: boolean
protected serverBuildDir: string
protected pagesManifest?: PagesManifest
protected buildId: string
protected minimalMode: boolean
protected renderOpts: {
poweredByHeader: boolean
buildId: string
generateEtags: boolean
Expand All @@ -161,7 +161,7 @@ export default class Server {
private compression?: Middleware
private onErrorMiddleware?: ({ err }: { err: Error }) => Promise<void>
private incrementalCache: IncrementalCache
router: Router
protected router: Router
protected dynamicRoutes?: DynamicRoutes
protected customRoutes: CustomRoutes

Expand Down Expand Up @@ -784,7 +784,14 @@ export default class Server {
type: 'route',
name: '_next/image catchall',
fn: (req, res, _params, parsedUrl) =>
imageOptimizer(server, req, res, parsedUrl),
imageOptimizer(
server,
req,
res,
parsedUrl,
server.nextConfig,
server.distDir
),
},
{
match: route('/_next/:path*'),
Expand Down

0 comments on commit 20e2107

Please sign in to comment.