Skip to content

Commit

Permalink
read envs
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Apr 29, 2024
1 parent 6db7475 commit 1913fab
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 16 deletions.
2 changes: 1 addition & 1 deletion packages/next/src/build/entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ export function getEdgeServerEntry(opts: {
absoluteDocumentPath: opts.pages['/_document'],
absoluteErrorPath: opts.pages['/_error'],
absolutePagePath: opts.absolutePagePath,
buildId: opts.buildId,
buildId: 'process.env.__NEXT_BUILD_ID', // opts.buildId,
dev: opts.isDev,
isServerComponent: opts.isServerComponent,
page: opts.page,
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/build/templates/edge-ssr-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ const render = getRender({
serverActions: isServerComponent ? serverActions : undefined,
subresourceIntegrityManifest,
config: nextConfig,
buildId: process.env.__NEXT_BUILD_ID || 'VAR_BUILD_ID',
buildId: process.env.__NEXT_BUILD_ID!,
nextFontManifest,
incrementalCacheHandler,
interceptionRouteRewrites,
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/build/templates/edge-ssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ const render = getRender({
reactLoadableManifest,
subresourceIntegrityManifest,
config: nextConfig,
buildId: process.env.__NEXT_BUILD_ID || 'VAR_BUILD_ID',
buildId: process.env.__NEXT_BUILD_ID!,
nextFontManifest,
incrementalCacheHandler,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ const edgeSSRLoader: webpack.LoaderDefinitionFunction<EdgeSSRLoaderQuery> =
{
VAR_USERLAND: pageModPath,
VAR_PAGE: page,
VAR_BUILD_ID: buildId,
// VAR_BUILD_ID: buildId,
},
{
sriEnabled: JSON.stringify(sriEnabled),
Expand All @@ -167,7 +167,7 @@ const edgeSSRLoader: webpack.LoaderDefinitionFunction<EdgeSSRLoaderQuery> =
{
VAR_USERLAND: pageModPath,
VAR_PAGE: page,
VAR_BUILD_ID: buildId,
// VAR_BUILD_ID: buildId,
VAR_MODULE_DOCUMENT: documentPath,
VAR_MODULE_APP: appPath,
VAR_MODULE_GLOBAL_ERROR: errorPath,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ export interface EdgeFunctionDefinition {
name: string
page: string
matchers: MiddlewareMatcher[]
environments: Record<string, string>
wasm?: AssetBinding[]
assets?: AssetBinding[]
regions?: string[] | string
environments?: Record<string, string>
}

export interface MiddlewareManifest {
Expand Down
2 changes: 2 additions & 0 deletions packages/next/src/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1431,6 +1431,7 @@ export default class NextNodeServer extends BaseServer<
name: string
paths: string[]
wasm: { filePath: string; name: string }[]
environments: { [key: string]: string }
assets?: { filePath: string; name: string }[]
} | null {
const manifest = this.getMiddlewareManifest()
Expand Down Expand Up @@ -1472,6 +1473,7 @@ export default class NextNodeServer extends BaseServer<
filePath: join(this.distDir, binding.filePath),
}
}),
environments: pageInfo.environments,
}
}

Expand Down
30 changes: 20 additions & 10 deletions packages/next/src/server/web/sandbox/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,14 @@ async function loadWasm(
return modules
}

function buildEnvironmentVariablesFrom(): Record<string, string | undefined> {
function buildEnvironmentVariablesFrom(
injectedEnvironments: Record<string, string>
): Record<string, string | undefined> {
const pairs = Object.keys(process.env).map((key) => [key, process.env[key]])
const env = Object.fromEntries(pairs)
for (const key of Object.keys(injectedEnvironments)) {
env[key] = injectedEnvironments[key]
}
env.NEXT_RUNTIME = 'edge'
return env
}
Expand All @@ -122,23 +127,24 @@ Learn more: https://nextjs.org/docs/api-reference/edge-runtime`)
throw error
}

function createProcessPolyfill() {
const processPolyfill = { env: buildEnvironmentVariablesFrom() }
const overridenValue: Record<string, any> = {}
function createProcessPolyfill(environments: Record<string, string>) {
const processPolyfill = { env: buildEnvironmentVariablesFrom(environments) }
const overriddenValue: Record<string, any> = {}

for (const key of Object.keys(process)) {
if (key === 'env') continue
Object.defineProperty(processPolyfill, key, {
get() {
if (overridenValue[key] !== undefined) {
return overridenValue[key]
if (overriddenValue[key] !== undefined) {
return overriddenValue[key]
}
if (typeof (process as any)[key] === 'function') {
return () => throwUnsupportedAPIError(`process.${key}`)
}
return undefined
},
set(value) {
overridenValue[key] = value
overriddenValue[key] = value
},
enumerable: false,
})
Expand Down Expand Up @@ -244,14 +250,15 @@ export const requestStore = new AsyncLocalStorage<{
async function createModuleContext(options: ModuleContextOptions) {
const warnedEvals = new Set<string>()
const warnedWasmCodegens = new Set<string>()
const wasm = await loadWasm(options.edgeFunctionEntry.wasm ?? [])
const { edgeFunctionEntry } = options
const wasm = await loadWasm(edgeFunctionEntry.wasm ?? [])
const runtime = new EdgeRuntime({
codeGeneration:
process.env.NODE_ENV !== 'production'
? { strings: true, wasm: true }
: undefined,
extend: (context) => {
context.process = createProcessPolyfill()
context.process = createProcessPolyfill(edgeFunctionEntry.environments)

Object.defineProperty(context, 'require', {
enumerable: false,
Expand Down Expand Up @@ -470,7 +477,10 @@ interface ModuleContextOptions {
onWarning: (warn: Error) => void
useCache: boolean
distDir: string
edgeFunctionEntry: Pick<EdgeFunctionDefinition, 'assets' | 'wasm'>
edgeFunctionEntry: Pick<
EdgeFunctionDefinition,
'assets' | 'wasm' | 'environments'
>
}

function getModuleContextShared(options: ModuleContextOptions) {
Expand Down

0 comments on commit 1913fab

Please sign in to comment.