Skip to content

Commit

Permalink
chore: drop undici (#56899)
Browse files Browse the repository at this point in the history
### What?

Note: This is not a breaking change, just removing some unused code.

### Why?

Since #56896 we don't need this, as Node.js 18+ has `fetch` exposed by default.

### How?

Depends on #56896, #56909

We already didn't load `fetch` if `globalThis` had it (ie. Node.js 18+ environments), and since we are dropping support for Node.js 16, these code paths should have no effect on runtime behavior.
  • Loading branch information
balazsorban44 committed Oct 19, 2023
1 parent 33db463 commit 4c46ddd
Show file tree
Hide file tree
Showing 30 changed files with 3 additions and 163 deletions.
1 change: 0 additions & 1 deletion packages/next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,6 @@
"timers-browserify": "2.0.12",
"tty-browserify": "0.0.1",
"ua-parser-js": "1.0.35",
"undici": "5.26.3",
"unistore": "3.4.1",
"util": "0.12.4",
"uuid": "8.3.2",
Expand Down
2 changes: 0 additions & 2 deletions packages/next/src/build/templates/app-route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import '../../server/node-polyfill-headers'

import {
AppRouteRouteModule,
type AppRouteRouteModuleOptions,
Expand Down
1 change: 0 additions & 1 deletion packages/next/src/build/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import type { WebpackLayerName } from '../lib/constants'
import type { AppPageModule } from '../server/future/route-modules/app-page/module'

import '../server/require-hook'
import '../server/node-polyfill-fetch'
import '../server/node-polyfill-crypto'
import '../server/node-environment'

Expand Down
3 changes: 0 additions & 3 deletions packages/next/src/cli/next-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ import os from 'os'
import childProcess from 'child_process'

import { bold, cyan, yellow } from '../lib/picocolors'
const { fetch } = require('next/dist/compiled/undici') as {
fetch: typeof global.fetch
}
import type { CliCommand } from '../lib/commands'
import { PHASE_INFO } from '../shared/lib/constants'
import loadConfig from '../server/config'
Expand Down
21 changes: 0 additions & 21 deletions packages/next/src/compiled/undici/LICENSE

This file was deleted.

3 changes: 0 additions & 3 deletions packages/next/src/compiled/undici/index.js

This file was deleted.

1 change: 0 additions & 1 deletion packages/next/src/compiled/undici/package.json

This file was deleted.

2 changes: 0 additions & 2 deletions packages/next/src/export/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import type {
WorkerRenderOpts,
} from './types'

// Polyfill fetch for the export worker.
import '../server/node-polyfill-fetch'
import '../server/node-polyfill-web-streams'
import '../server/node-environment'

Expand Down
3 changes: 0 additions & 3 deletions packages/next/src/lib/download-swc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ import fs from 'fs'
import path from 'path'
import * as Log from '../build/output/log'
import tar from 'next/dist/compiled/tar'
const { fetch } = require('next/dist/compiled/undici') as {
fetch: typeof global.fetch
}
const { WritableStream } = require('node:stream/web') as {
WritableStream: typeof global.WritableStream
}
Expand Down
4 changes: 0 additions & 4 deletions packages/next/src/lib/mkcert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ import { getCacheDirectory } from './helpers/get-cache-directory'
import * as Log from '../build/output/log'
import { execSync } from 'child_process'

const { fetch } = require('next/dist/compiled/undici') as {
fetch: typeof global.fetch
}

const MKCERT_VERSION = 'v1.4.4'

export interface SelfSignedCertificate {
Expand Down
1 change: 0 additions & 1 deletion packages/next/src/lib/patch-incorrect-lockfile.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { promises } from 'fs'
import '../server/node-polyfill-fetch'
import * as Log from '../build/output/log'
import findUp from 'next/dist/compiled/find-up'
// @ts-ignore no-json types
Expand Down
6 changes: 3 additions & 3 deletions packages/next/src/server/app-render/action-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,10 +423,10 @@ export async function handleAction({
} else {
// React doesn't yet publish a busboy version of decodeAction
// so we polyfill the parsing of FormData.
const UndiciRequest = require('next/dist/compiled/undici').Request
const fakeRequest = new UndiciRequest('http://localhost', {
const fakeRequest = new Request('http://localhost', {
method: 'POST',
headers: { 'Content-Type': req.headers['content-type'] },
// @ts-expect-error
headers: { 'Content-Type': contentType },
body: nodeToWebReadableStream(req),
duplex: 'half',
})
Expand Down
1 change: 0 additions & 1 deletion packages/next/src/server/config-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ export function loadWebpackHook() {
'@babel/runtime/package.json',
'next/dist/compiled/@babel/runtime/package.json',
],
['undici', 'next/dist/compiled/undici'],
].map(
// Use dynamic require.resolve to avoid statically analyzable since they're only for build time
([request, replacement]) => [request, require.resolve(replacement)]
Expand Down
1 change: 0 additions & 1 deletion packages/next/src/server/dev/static-paths-worker.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { NextConfigComplete } from '../config-shared'

import '../require-hook'
import '../node-polyfill-fetch'
import '../node-environment'

import {
Expand Down
3 changes: 0 additions & 3 deletions packages/next/src/server/lib/mock-request.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { MockedRequest, MockedResponse } from './mock-request'

// We use `Headers` here which is provided by the polyfill.
import '../node-polyfill-fetch'

describe('MockedRequest', () => {
it('should have the correct properties', () => {
const req = new MockedRequest({
Expand Down
1 change: 0 additions & 1 deletion packages/next/src/server/lib/route-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import type { IncomingMessage, ServerResponse } from 'http'
import type { RenderServer } from './router-server'

import '../require-hook'
import '../node-polyfill-fetch'

import url from 'url'
import path from 'path'
Expand Down
1 change: 0 additions & 1 deletion packages/next/src/server/lib/router-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import type { WorkerRequestHandler, WorkerUpgradeHandler } from './types'
import type { DevBundler } from './router-utils/setup-dev-bundler'
import type { NextUrlWithParsedQuery } from '../request-meta'
// This is required before other imports to ensure the require hook is setup.
import '../node-polyfill-fetch'
import '../node-environment'
import '../require-hook'

Expand Down
1 change: 0 additions & 1 deletion packages/next/src/server/lib/start-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ if (performance.getEntriesByName('next-start').length === 0) {
performance.mark('next-start')
}
import '../next'
import '../node-polyfill-fetch'
import '../require-hook'

import type { IncomingMessage, ServerResponse } from 'http'
Expand Down
1 change: 0 additions & 1 deletion packages/next/src/server/next-server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import './node-environment'
import './require-hook'
import './node-polyfill-fetch'
import './node-polyfill-form'
import './node-polyfill-web-streams'
import './node-polyfill-crypto'
Expand Down
1 change: 0 additions & 1 deletion packages/next/src/server/next.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import type { NextUrlWithParsedQuery } from './request-meta'
import type { WorkerRequestHandler, WorkerUpgradeHandler } from './lib/types'

import './require-hook'
import './node-polyfill-fetch'
import './node-polyfill-crypto'

import type { default as Server } from './next-server'
Expand Down
54 changes: 0 additions & 54 deletions packages/next/src/server/node-polyfill-fetch.ts

This file was deleted.

16 changes: 0 additions & 16 deletions packages/next/src/server/node-polyfill-headers.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import type { IncomingHttpHeaders } from 'http'

// We use `Headers` here which is provided by the polyfill.
import '../../../node-polyfill-fetch'

import { HeadersAdapter, ReadonlyHeadersError } from './headers'

describe('HeadersAdapter', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// We use `Headers` here which is provided by the polyfill.
import '../../../node-polyfill-fetch'

import { RequestCookies } from '../cookies'
import {
ReadonlyRequestCookiesError,
Expand Down
3 changes: 0 additions & 3 deletions packages/next/src/server/web/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { toNodeOutgoingHttpHeaders } from './utils'

// We use `Headers` here which is provided by the polyfill.
import '../node-polyfill-fetch'

describe('toNodeHeaders', () => {
it('should handle multiple set-cookie headers correctly', () => {
const headers = new Headers()
Expand Down
3 changes: 0 additions & 3 deletions packages/next/src/telemetry/post-payload.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import retry from 'next/dist/compiled/async-retry'
const { fetch } = require('next/dist/compiled/undici') as {
fetch: typeof global.fetch
}

export function _postPayload(endpoint: string, body: object, signal?: any) {
if (!signal && 'timeout' in AbortSignal) {
Expand Down
19 changes: 0 additions & 19 deletions packages/next/taskfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,6 @@ const externals = {
'next/dist/build/webpack/plugins/terser-webpack-plugin',

// TODO: Add @swc/helpers to externals once @vercel/ncc switch to swc-loader

undici: 'undici',
}
// eslint-disable-next-line camelcase
externals['node-html-parser'] = 'next/dist/compiled/node-html-parser'
Expand Down Expand Up @@ -321,22 +319,6 @@ export async function ncc_node_platform(task, opts) {
)
}

externals['undici'] = 'next/dist/compiled/undici'
export async function ncc_undici(task, opts) {
await task
.source(relative(__dirname, require.resolve('undici')))
.ncc({ packageName: 'undici', externals })
.target('src/compiled/undici')

const outputFile = join('src/compiled/undici/index.js')
await fs.writeFile(
outputFile,
(
await fs.readFile(outputFile, 'utf8')
).replace(/process\.emitWarning/g, 'void')
)
}

// eslint-disable-next-line camelcase
externals['acorn'] = 'next/dist/compiled/acorn'
export async function ncc_acorn(task, opts) {
Expand Down Expand Up @@ -2239,7 +2221,6 @@ export async function ncc(task, opts) {
'ncc_node_cssescape',
'ncc_node_platform',
'ncc_node_shell_quote',
'ncc_undici',
'ncc_acorn',
'ncc_amphtml_validator',
'ncc_arg',
Expand Down
2 changes: 0 additions & 2 deletions packages/next/types/misc.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ declare module 'next/dist/compiled/@mswjs/interceptors/ClientRequest' {
export * from '@mswjs/interceptors/ClientRequest'
}

declare module 'next/dist/compiled/undici' {}

declare module 'next/dist/compiled/jest-worker' {
export * from 'jest-worker'
}
Expand Down
3 changes: 0 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions test/unit/web-runtime/next-server-node.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import 'next/dist/server/node-polyfill-fetch'

it('should be able to require next/server outside edge', () => {
require('next/server')
})

0 comments on commit 4c46ddd

Please sign in to comment.