Unverified Commit a70e55ea authored by Sukka's avatar Sukka Committed by GitHub

perf: download and save mkcert in stream (#63527)

Previously, Next.js would buffer the `mkcert` binary in memory and write
it to disk all at once. The pull request changes this to pipe the
download stream to the disk. This improves memory consumption by
avoiding having to load the entire `mkcert` binary into memory.

---------
Co-authored-by: 's avatarJJ Kasper <jj@jjsweb.site>
parent 8d8ffa81
...@@ -3,6 +3,9 @@ import path from 'path' ...@@ -3,6 +3,9 @@ import path from 'path'
import { getCacheDirectory } from './helpers/get-cache-directory' import { getCacheDirectory } from './helpers/get-cache-directory'
import * as Log from '../build/output/log' import * as Log from '../build/output/log'
import { execSync } from 'child_process' import { execSync } from 'child_process'
const { WritableStream } = require('node:stream/web') as {
WritableStream: typeof global.WritableStream
}
const MKCERT_VERSION = 'v1.4.4' const MKCERT_VERSION = 'v1.4.4'
...@@ -53,10 +56,37 @@ async function downloadBinary() { ...@@ -53,10 +56,37 @@ async function downloadBinary() {
Log.info(`Download response was successful, writing to disk`) Log.info(`Download response was successful, writing to disk`)
const arrayBuffer = await response.arrayBuffer() const binaryWriteStream = fs.createWriteStream(binaryPath)
const buffer = Buffer.from(arrayBuffer)
await response.body.pipeTo(
new WritableStream({
write(chunk) {
return new Promise((resolve, reject) => {
binaryWriteStream.write(chunk, (error) => {
if (error) {
reject(error)
return
}
resolve()
})
})
},
close() {
return new Promise((resolve, reject) => {
binaryWriteStream.close((error) => {
if (error) {
reject(error)
return
}
resolve()
})
})
},
})
)
await fs.promises.writeFile(binaryPath, buffer)
await fs.promises.chmod(binaryPath, 0o755) await fs.promises.chmod(binaryPath, 0o755)
return binaryPath return binaryPath
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment