Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix process polyfill on middleware #34426

Merged
merged 7 commits into from Feb 18, 2022
Merged
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