-
Notifications
You must be signed in to change notification settings - Fork 26.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Do not polyfill node built-in modules on edge functions (#36190)
As the title. This is intended to be applied on both middleware and edge functions. ## Bug - [ ] Related issues linked using `fixes #number` - [x] 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` Co-authored-by: Jiachi Liu <4800338+huozhi@users.noreply.github.com>
- Loading branch information
Showing
8 changed files
with
191 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
test/integration/middleware/with-builtin-module/pages/using-child-process/_middleware.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { NextResponse } from 'next/server' | ||
import { spawn } from 'child_process' | ||
|
||
export async function middleware(request) { | ||
console.log(spawn('ls', ['-lh', '/usr'])) | ||
return NextResponse.next() | ||
} |
7 changes: 7 additions & 0 deletions
7
test/integration/middleware/with-builtin-module/pages/using-path/_middleware.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { NextResponse } from 'next/server' | ||
import { basename } from 'path' | ||
|
||
export async function middleware(request) { | ||
console.log(basename('/foo/bar/baz/asdf/quux.html')) | ||
return NextResponse.next() | ||
} |
90 changes: 90 additions & 0 deletions
90
test/integration/middleware/with-builtin-module/test/index.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* eslint-env jest */ | ||
|
||
import stripAnsi from 'next/dist/compiled/strip-ansi' | ||
import { join } from 'path' | ||
import { | ||
fetchViaHTTP, | ||
findPort, | ||
killApp, | ||
launchApp, | ||
nextBuild, | ||
waitFor, | ||
} from 'next-test-utils' | ||
|
||
const context = {} | ||
const WEBPACK_BREAKING_CHANGE = 'BREAKING CHANGE:' | ||
|
||
jest.setTimeout(1000 * 60 * 2) | ||
context.appDir = join(__dirname, '../') | ||
|
||
describe('Middleware importing Node.js built-in module', () => { | ||
function getModuleNotFound(name) { | ||
return `Module not found: Can't resolve '${name}'` | ||
} | ||
|
||
function getBuiltinApisNotSupported(name) { | ||
return `Native Node.js APIs are not supported in the Edge Runtime. Found \`${name}\` imported.\n` | ||
} | ||
|
||
describe('dev mode', () => { | ||
let output = '' | ||
|
||
beforeAll(async () => { | ||
context.appPort = await findPort() | ||
context.app = await launchApp(context.appDir, context.appPort, { | ||
env: { __NEXT_TEST_WITH_DEVTOOL: 1 }, | ||
onStdout(msg) { | ||
output += msg | ||
}, | ||
onStderr(msg) { | ||
output += msg | ||
}, | ||
}) | ||
}) | ||
|
||
beforeEach(() => (output = '')) | ||
afterAll(() => killApp(context.app)) | ||
|
||
it('shows error when importing path module', async () => { | ||
const res = await fetchViaHTTP(context.appPort, '/using-path') | ||
await waitFor(500) | ||
expect(res.status).toBe(500) | ||
expect(output).toContain(getModuleNotFound('path')) | ||
expect(output).toContain(getBuiltinApisNotSupported('path')) | ||
expect(stripAnsi(output)).toContain("import { basename } from 'path'") | ||
expect(output).not.toContain(WEBPACK_BREAKING_CHANGE) | ||
}) | ||
|
||
it('shows error when importing child_process module', async () => { | ||
const res = await fetchViaHTTP(context.appPort, '/using-child-process') | ||
await waitFor(500) | ||
expect(res.status).toBe(500) | ||
expect(output).toContain(getModuleNotFound('child_process')) | ||
expect(output).toContain(getBuiltinApisNotSupported('child_process')) | ||
expect(stripAnsi(output)).toContain( | ||
"import { spawn } from 'child_process'" | ||
) | ||
expect(output).not.toContain(WEBPACK_BREAKING_CHANGE) | ||
}) | ||
}) | ||
|
||
describe('production mode', () => { | ||
let buildResult | ||
|
||
beforeAll(async () => { | ||
buildResult = await nextBuild(context.appDir, undefined, { | ||
stderr: true, | ||
stdout: true, | ||
}) | ||
}) | ||
|
||
it('should have middleware error during build', () => { | ||
expect(buildResult.stderr).toContain(getModuleNotFound('child_process')) | ||
expect(buildResult.stderr).toContain(getModuleNotFound('path')) | ||
expect(buildResult.stderr).toContain( | ||
getBuiltinApisNotSupported('child_process') | ||
) | ||
expect(buildResult.stderr).not.toContain(WEBPACK_BREAKING_CHANGE) | ||
}) | ||
}) | ||
}) |