rsnext/packages/next/server/require.ts
Gal Schlezinger e69500462d
middlewares: limit process.env to inferred usage (#33186)
Production middlewares will only expose env vars that are statically analyzable, as mentioned here: https://nextjs.org/docs/api-reference/next/server#how-do-i-access-environment-variables

This creates some incompatibility with `next dev` and `next start`, where all `process.env` data is shared and can lead to unexpected behavior in runtime.

This PR fixes it by limiting the data in `process.env` with the inferred env vars from the code usage. I believe the test speaks for itself 🕺 

<!--
## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `yarn lint`
-->
2022-01-12 13:09:24 +00:00

117 lines
2.9 KiB
TypeScript

import { promises } from 'fs'
import { join } from 'path'
import {
FONT_MANIFEST,
MIDDLEWARE_MANIFEST,
PAGES_MANIFEST,
SERVER_DIRECTORY,
SERVERLESS_DIRECTORY,
} from '../shared/lib/constants'
import { normalizePagePath, denormalizePagePath } from './normalize-page-path'
import { normalizeLocalePath } from '../shared/lib/i18n/normalize-locale-path'
import type { PagesManifest } from '../build/webpack/plugins/pages-manifest-plugin'
import type { MiddlewareManifest } from '../build/webpack/plugins/middleware-plugin'
export function pageNotFoundError(page: string): Error {
const err: any = new Error(`Cannot find module for page: ${page}`)
err.code = 'ENOENT'
return err
}
export function getPagePath(
page: string,
distDir: string,
serverless: boolean,
dev?: boolean,
locales?: string[]
): string {
const serverBuildPath = join(
distDir,
serverless && !dev ? SERVERLESS_DIRECTORY : SERVER_DIRECTORY
)
const pagesManifest = require(join(
serverBuildPath,
PAGES_MANIFEST
)) as PagesManifest
try {
page = denormalizePagePath(normalizePagePath(page))
} catch (err) {
console.error(err)
throw pageNotFoundError(page)
}
let pagePath = pagesManifest[page]
if (!pagesManifest[page] && locales) {
const manifestNoLocales: typeof pagesManifest = {}
for (const key of Object.keys(pagesManifest)) {
manifestNoLocales[normalizeLocalePath(key, locales).pathname] =
pagesManifest[key]
}
pagePath = manifestNoLocales[page]
}
if (!pagePath) {
throw pageNotFoundError(page)
}
return join(serverBuildPath, pagePath)
}
export function requirePage(
page: string,
distDir: string,
serverless: boolean
): any {
const pagePath = getPagePath(page, distDir, serverless)
if (pagePath.endsWith('.html')) {
return promises.readFile(pagePath, 'utf8')
}
return require(pagePath)
}
export function requireFontManifest(distDir: string, serverless: boolean) {
const serverBuildPath = join(
distDir,
serverless ? SERVERLESS_DIRECTORY : SERVER_DIRECTORY
)
const fontManifest = require(join(serverBuildPath, FONT_MANIFEST))
return fontManifest
}
export function getMiddlewareInfo(params: {
dev?: boolean
distDir: string
page: string
serverless: boolean
}): { name: string; paths: string[]; env: string[] } {
const serverBuildPath = join(
params.distDir,
params.serverless && !params.dev ? SERVERLESS_DIRECTORY : SERVER_DIRECTORY
)
const middlewareManifest: MiddlewareManifest = require(join(
serverBuildPath,
MIDDLEWARE_MANIFEST
))
let page: string
try {
page = denormalizePagePath(normalizePagePath(params.page))
} catch (err) {
throw pageNotFoundError(params.page)
}
let pageInfo = middlewareManifest.middleware[page]
if (!pageInfo) {
throw pageNotFoundError(page)
}
return {
name: pageInfo.name,
paths: pageInfo.files.map((file) => join(params.distDir, file)),
env: pageInfo.env ?? [],
}
}