Skip to content

Commit

Permalink
fix process polyfill on middleware (#34426)
Browse files Browse the repository at this point in the history
Fixes the problem that global `process` variable has only the `env` field.
Also fixed the issue that the `env` field is empty when the `process` module is used as the value of the variable (which happens when the module is contained in a dependency of application).

## Bug

- [ ] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`
  • Loading branch information
nkzawa committed Feb 18, 2022
1 parent a74af1f commit 7c103fa
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 1 deletion.
7 changes: 7 additions & 0 deletions packages/next/build/webpack/loaders/next-middleware-loader.ts
Expand Up @@ -12,6 +12,13 @@ export default function middlewareLoader(this: any) {
return `
import { adapter } from 'next/dist/server/web/adapter'
// The condition is true when the "process" module is provided
if (process !== global.process) {
// prefer local process but global.process has correct "env"
process.env = global.process.env;
global.process = process;
}
var mod = require(${stringifiedPagePath})
var handler = mod.middleware || mod.default;
Expand Down
1 change: 1 addition & 0 deletions packages/next/server/web/sandbox/context.ts
Expand Up @@ -202,6 +202,7 @@ function createContext(options: {
File,
FormData,
process: {
...polyfills.process,
env: buildEnvironmentVariablesFrom(options.env),
},
ReadableStream: polyfills.ReadableStream,
Expand Down
3 changes: 2 additions & 1 deletion packages/next/server/web/sandbox/polyfills.ts
@@ -1,6 +1,7 @@
import { Crypto as WebCrypto } from 'next/dist/compiled/@peculiar/webcrypto'
import { CryptoKey } from 'next/dist/compiled/@peculiar/webcrypto'
import { v4 as uuid } from 'next/dist/compiled/uuid'
import processPolyfill from 'next/dist/compiled/process'
import { ReadableStream } from './readable-stream'

import crypto from 'crypto'
Expand All @@ -13,7 +14,7 @@ export function btoa(str: string) {
return Buffer.from(str, 'binary').toString('base64')
}

export { CryptoKey, ReadableStream }
export { CryptoKey, ReadableStream, processPolyfill as process }

export class Crypto extends WebCrypto {
// @ts-ignore Remove once types are updated and we deprecate node 12
Expand Down
5 changes: 5 additions & 0 deletions packages/next/types/misc.d.ts
Expand Up @@ -331,6 +331,11 @@ declare module 'next/dist/compiled/comment-json' {
export = m
}

declare module 'next/dist/compiled/process' {
import m from 'process'
export = m
}

declare module 'pnp-webpack-plugin' {
import webpack from 'webpack4'

Expand Down
12 changes: 12 additions & 0 deletions test/integration/middleware/core/pages/global/_middleware.js
@@ -0,0 +1,12 @@
import { NextResponse } from 'next/server'

export async function middleware(request, ev) {
console.log(process.env.MIDDLEWARE_TEST)

return NextResponse.json({
process: {
env: process.env,
nextTick: typeof process.nextTick,
},
})
}
24 changes: 24 additions & 0 deletions test/integration/middleware/core/test/index.test.js
Expand Up @@ -108,6 +108,30 @@ describe('Middleware base tests', () => {
}
})
})

describe('global', () => {
beforeAll(async () => {
context.appPort = await findPort()
context.app = await launchApp(context.appDir, context.appPort, {
env: {
MIDDLEWARE_TEST: 'asdf',
},
})
})

it('should contains process polyfill', async () => {
const res = await fetchViaHTTP(context.appPort, `/global`)
const json = await res.json()
expect(json).toEqual({
process: {
env: {
MIDDLEWARE_TEST: 'asdf',
},
nextTick: 'function',
},
})
})
})
})

function urlTests(_log, locale = '') {
Expand Down

0 comments on commit 7c103fa

Please sign in to comment.