Skip to content

Commit

Permalink
move util
Browse files Browse the repository at this point in the history
  • Loading branch information
ijjk committed May 23, 2023
1 parent 1c70aa6 commit 28409af
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 20 deletions.
2 changes: 1 addition & 1 deletion packages/next/src/server/lib/render-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
deleteAppClientCache as _deleteAppClientCache,
} from '../../build/webpack/plugins/nextjs-require-cache-hot-reloader'
import { clearModuleContext as _clearModuleContext } from '../web/sandbox/context'
import { getFreePort } from './server-ipc'
import { getFreePort } from '../lib/worker-utils'
export const WORKER_SELF_EXIT_CODE = 77

const MAXIMUM_HEAP_SIZE_ALLOWED =
Expand Down
19 changes: 1 addition & 18 deletions packages/next/src/server/lib/server-ipc/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import type NextServer from '../../next-server'

import http from 'http'
import { getNodeOptionsWithoutInspect } from '../utils'
import { deserializeErr, errorToJSON } from '../../render'
import crypto from 'crypto'
import isError from '../../../lib/is-error'
import { genRenderExecArgv } from '../worker-utils'
import { genRenderExecArgv, getFreePort } from '../worker-utils'

// we can't use process.send as jest-worker relies on
// it already and can cause unexpected message errors
Expand Down Expand Up @@ -80,22 +79,6 @@ export async function createIpcServer(
}
}

export const getFreePort = async (): Promise<number> => {
return new Promise((resolve, reject) => {
const server = http.createServer(() => {})
server.listen(0, () => {
const address = server.address()
server.close()

if (address && typeof address === 'object') {
resolve(address.port)
} else {
reject(new Error('invalid address from server: ' + address?.toString()))
}
})
})
}

export const createWorker = async (
ipcPort: number,
ipcValidationKey: string,
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/server/lib/start-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { ChildProcess } from 'child_process'
import { normalizeRepeatedSlashes } from '../../shared/lib/utils'
import { initialEnv } from '@next/env'
import { genExecArgv, getNodeOptionsWithoutInspect } from './utils'
import { getFreePort } from './server-ipc'
import { getFreePort } from './worker-utils'

export interface StartServerOptions {
dir: string
Expand Down
17 changes: 17 additions & 0 deletions packages/next/src/server/lib/worker-utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
import * as Log from '../../build/output/log'
import http from 'http'

export const getFreePort = async (): Promise<number> => {
return new Promise((resolve, reject) => {
const server = http.createServer(() => {})
server.listen(0, () => {
const address = server.address()
server.close()

if (address && typeof address === 'object') {
resolve(address.port)
} else {
reject(new Error('invalid address from server: ' + address?.toString()))
}
})
})
}

export const genRenderExecArgv = (type: 'pages' | 'app') => {
const isDebugging =
Expand Down

0 comments on commit 28409af

Please sign in to comment.