Unverified Commit a278e94d authored by Yagiz Nizipli's avatar Yagiz Nizipli Committed by GitHub

fix: avoid creation of buffers for read ops (#56421)

Example usage of `readFileSync().toString()` first creates a `Buffer` instance and later does another C++ call to convert Buffer to UTF-8. This pull request reduces the C++ calls.
parent f8308c9e
...@@ -35,8 +35,8 @@ export function createIncrementalCache( ...@@ -35,8 +35,8 @@ export function createIncrementalCache(
notFoundRoutes: [], notFoundRoutes: [],
}), }),
fs: { fs: {
readFile: (f) => fs.promises.readFile(f), readFile: fs.promises.readFile,
readFileSync: (f) => fs.readFileSync(f), readFileSync: fs.readFileSync,
writeFile: (f, d) => fs.promises.writeFile(f, d), writeFile: (f, d) => fs.promises.writeFile(f, d),
mkdir: (dir) => fs.promises.mkdir(dir, { recursive: true }), mkdir: (dir) => fs.promises.mkdir(dir, { recursive: true }),
stat: (f) => fs.promises.stat(f), stat: (f) => fs.promises.stat(f),
......
...@@ -75,7 +75,7 @@ export default class FileSystemCache implements CacheHandler { ...@@ -75,7 +75,7 @@ export default class FileSystemCache implements CacheHandler {
if (!this.tagsManifestPath || !this.fs || tagsManifest) return if (!this.tagsManifestPath || !this.fs || tagsManifest) return
try { try {
tagsManifest = JSON.parse( tagsManifest = JSON.parse(
this.fs.readFileSync(this.tagsManifestPath).toString('utf8') this.fs.readFileSync(this.tagsManifestPath, 'utf8')
) )
} catch (err: any) { } catch (err: any) {
tagsManifest = { version: 1, items: {} } tagsManifest = { version: 1, items: {} }
...@@ -131,9 +131,7 @@ export default class FileSystemCache implements CacheHandler { ...@@ -131,9 +131,7 @@ export default class FileSystemCache implements CacheHandler {
const { mtime } = await this.fs.stat(filePath) const { mtime } = await this.fs.stat(filePath)
const meta = JSON.parse( const meta = JSON.parse(
( await this.fs.readFile(filePath.replace(/\.body$/, '.meta'), 'utf8')
await this.fs.readFile(filePath.replace(/\.body$/, '.meta'))
).toString('utf8')
) )
const cacheEntry: CacheHandlerValue = { const cacheEntry: CacheHandlerValue = {
...@@ -155,7 +153,7 @@ export default class FileSystemCache implements CacheHandler { ...@@ -155,7 +153,7 @@ export default class FileSystemCache implements CacheHandler {
pathname: fetchCache ? key : `${key}.html`, pathname: fetchCache ? key : `${key}.html`,
fetchCache, fetchCache,
}) })
const fileData = (await this.fs.readFile(filePath)).toString('utf-8') const fileData = await this.fs.readFile(filePath, 'utf8')
const { mtime } = await this.fs.stat(filePath) const { mtime } = await this.fs.stat(filePath)
if (fetchCache) { if (fetchCache) {
...@@ -178,27 +176,25 @@ export default class FileSystemCache implements CacheHandler { ...@@ -178,27 +176,25 @@ export default class FileSystemCache implements CacheHandler {
} }
} else { } else {
const pageData = isAppPath const pageData = isAppPath
? ( ? await this.fs.readFile(
(
await this.getFsPath({
pathname: `${key}.rsc`,
appDir: true,
})
).filePath,
'utf8'
)
: JSON.parse(
await this.fs.readFile( await this.fs.readFile(
( (
await this.getFsPath({ await this.getFsPath({
pathname: `${key}.rsc`, pathname: `${key}.json`,
appDir: true, appDir: false,
}) })
).filePath ).filePath,
'utf8'
) )
).toString('utf8')
: JSON.parse(
(
await this.fs.readFile(
(
await this.getFsPath({
pathname: `${key}.json`,
appDir: false,
})
).filePath
)
).toString('utf8')
) )
let meta: { status?: number; headers?: OutgoingHttpHeaders } = {} let meta: { status?: number; headers?: OutgoingHttpHeaders } = {}
...@@ -206,9 +202,10 @@ export default class FileSystemCache implements CacheHandler { ...@@ -206,9 +202,10 @@ export default class FileSystemCache implements CacheHandler {
if (isAppPath) { if (isAppPath) {
try { try {
meta = JSON.parse( meta = JSON.parse(
( await this.fs.readFile(
await this.fs.readFile(filePath.replace(/\.html$/, '.meta')) filePath.replace(/\.html$/, '.meta'),
).toString('utf-8') 'utf8'
)
) )
} catch {} } catch {}
} }
......
...@@ -2,8 +2,8 @@ import _fs from 'fs' ...@@ -2,8 +2,8 @@ import _fs from 'fs'
import type { CacheFs } from '../../shared/lib/utils' import type { CacheFs } from '../../shared/lib/utils'
export const nodeFs: CacheFs = { export const nodeFs: CacheFs = {
readFile: (f) => _fs.promises.readFile(f), readFile: _fs.promises.readFile,
readFileSync: (f) => _fs.readFileSync(f), readFileSync: _fs.readFileSync,
writeFile: (f, d) => _fs.promises.writeFile(f, d), writeFile: (f, d) => _fs.promises.writeFile(f, d),
mkdir: (dir) => _fs.promises.mkdir(dir, { recursive: true }), mkdir: (dir) => _fs.promises.mkdir(dir, { recursive: true }),
stat: (f) => _fs.promises.stat(f), stat: (f) => _fs.promises.stat(f),
......
...@@ -722,14 +722,13 @@ export default class NextNodeServer extends BaseServer { ...@@ -722,14 +722,13 @@ export default class NextNodeServer extends BaseServer {
) )
} }
protected async getFallback(page: string): Promise<string> { protected getFallback(page: string): Promise<string> {
page = normalizePagePath(page) page = normalizePagePath(page)
const cacheFs = this.getCacheFilesystem() const cacheFs = this.getCacheFilesystem()
const html = await cacheFs.readFile( return cacheFs.readFile(
join(this.serverDistDir, 'pages', `${page}.html`) join(this.serverDistDir, 'pages', `${page}.html`),
'utf8'
) )
return html.toString('utf8')
} }
protected async handleNextImageRequest( protected async handleNextImageRequest(
...@@ -985,10 +984,11 @@ export default class NextNodeServer extends BaseServer { ...@@ -985,10 +984,11 @@ export default class NextNodeServer extends BaseServer {
return this.runApi(req, res, query, match) return this.runApi(req, res, query, match)
} }
protected async getPrefetchRsc(pathname: string) { protected getPrefetchRsc(pathname: string): Promise<string> {
return this.getCacheFilesystem() return this.getCacheFilesystem().readFile(
.readFile(join(this.serverDistDir, 'app', `${pathname}.prefetch.rsc`)) join(this.serverDistDir, 'app', `${pathname}.prefetch.rsc`),
.then((res) => res.toString()) 'utf8'
)
} }
protected getCacheFilesystem(): CacheFs { protected getCacheFilesystem(): CacheFs {
......
...@@ -7,6 +7,7 @@ import type { NextRouter } from './router/router' ...@@ -7,6 +7,7 @@ import type { NextRouter } from './router/router'
import type { ParsedUrlQuery } from 'querystring' import type { ParsedUrlQuery } from 'querystring'
import type { PreviewData } from 'next/types' import type { PreviewData } from 'next/types'
import { COMPILER_NAMES } from './constants' import { COMPILER_NAMES } from './constants'
import type fs from 'fs'
export type NextComponentType< export type NextComponentType<
Context extends BaseContext = NextPageContext, Context extends BaseContext = NextPageContext,
...@@ -447,8 +448,8 @@ export class MiddlewareNotFoundError extends Error { ...@@ -447,8 +448,8 @@ export class MiddlewareNotFoundError extends Error {
} }
export interface CacheFs { export interface CacheFs {
readFile(f: string): Promise<Buffer> readFile: typeof fs.promises.readFile
readFileSync(f: string): Buffer readFileSync: typeof fs.readFileSync
writeFile(f: string, d: any): Promise<void> writeFile(f: string, d: any): Promise<void>
mkdir(dir: string): Promise<void | string> mkdir(dir: string): Promise<void | string>
stat(f: string): Promise<{ mtime: Date }> stat(f: string): Promise<{ mtime: Date }>
......
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