Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: avoid creation of buffers for read ops #56421

Merged
merged 2 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/next/src/export/helpers/create-incremental-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ export function createIncrementalCache(
notFoundRoutes: [],
}),
fs: {
readFile: (f) => fs.promises.readFile(f),
readFileSync: (f) => fs.readFileSync(f),
readFile: fs.promises.readFile,
readFileSync: fs.readFileSync,
writeFile: (f, d) => fs.promises.writeFile(f, d),
mkdir: (dir) => fs.promises.mkdir(dir, { recursive: true }),
stat: (f) => fs.promises.stat(f),
Expand Down
45 changes: 21 additions & 24 deletions packages/next/src/server/lib/incremental-cache/file-system-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export default class FileSystemCache implements CacheHandler {
if (!this.tagsManifestPath || !this.fs || tagsManifest) return
try {
tagsManifest = JSON.parse(
this.fs.readFileSync(this.tagsManifestPath).toString('utf8')
this.fs.readFileSync(this.tagsManifestPath, 'utf8')
)
} catch (err: any) {
tagsManifest = { version: 1, items: {} }
Expand Down Expand Up @@ -131,9 +131,7 @@ export default class FileSystemCache implements CacheHandler {
const { mtime } = await this.fs.stat(filePath)

const meta = JSON.parse(
(
await this.fs.readFile(filePath.replace(/\.body$/, '.meta'))
).toString('utf8')
await this.fs.readFile(filePath.replace(/\.body$/, '.meta'), 'utf8')
)

const cacheEntry: CacheHandlerValue = {
Expand All @@ -155,7 +153,7 @@ export default class FileSystemCache implements CacheHandler {
pathname: fetchCache ? key : `${key}.html`,
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)

if (fetchCache) {
Expand All @@ -178,37 +176,36 @@ export default class FileSystemCache implements CacheHandler {
}
} else {
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.getFsPath({
pathname: `${key}.rsc`,
appDir: true,
pathname: `${key}.json`,
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 } = {}

if (isAppPath) {
try {
meta = JSON.parse(
(
await this.fs.readFile(filePath.replace(/\.html$/, '.meta'))
).toString('utf-8')
await this.fs.readFile(
filePath.replace(/\.html$/, '.meta'),
'utf8'
)
)
} catch {}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/next/src/server/lib/node-fs-methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import _fs from 'fs'
import type { CacheFs } from '../../shared/lib/utils'

export const nodeFs: CacheFs = {
readFile: (f) => _fs.promises.readFile(f),
readFileSync: (f) => _fs.readFileSync(f),
readFile: _fs.promises.readFile,
readFileSync: _fs.readFileSync,
writeFile: (f, d) => _fs.promises.writeFile(f, d),
mkdir: (dir) => _fs.promises.mkdir(dir, { recursive: true }),
stat: (f) => _fs.promises.stat(f),
Expand Down
18 changes: 9 additions & 9 deletions packages/next/src/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
const cacheFs = this.getCacheFilesystem()
const html = await cacheFs.readFile(
join(this.serverDistDir, 'pages', `${page}.html`)
return cacheFs.readFile(
join(this.serverDistDir, 'pages', `${page}.html`),
'utf8'
)

return html.toString('utf8')
}

protected async handleNextImageRequest(
Expand Down Expand Up @@ -985,10 +984,11 @@ export default class NextNodeServer extends BaseServer {
return this.runApi(req, res, query, match)
}

protected async getPrefetchRsc(pathname: string) {
return this.getCacheFilesystem()
.readFile(join(this.serverDistDir, 'app', `${pathname}.prefetch.rsc`))
.then((res) => res.toString())
protected getPrefetchRsc(pathname: string): Promise<string> {
return this.getCacheFilesystem().readFile(
join(this.serverDistDir, 'app', `${pathname}.prefetch.rsc`),
'utf8'
)
}

protected getCacheFilesystem(): CacheFs {
Expand Down
5 changes: 3 additions & 2 deletions packages/next/src/shared/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type { NextRouter } from './router/router'
import type { ParsedUrlQuery } from 'querystring'
import type { PreviewData } from 'next/types'
import { COMPILER_NAMES } from './constants'
import type fs from 'fs'

export type NextComponentType<
Context extends BaseContext = NextPageContext,
Expand Down Expand Up @@ -447,8 +448,8 @@ export class MiddlewareNotFoundError extends Error {
}

export interface CacheFs {
readFile(f: string): Promise<Buffer>
readFileSync(f: string): Buffer
readFile: typeof fs.promises.readFile
readFileSync: typeof fs.readFileSync
writeFile(f: string, d: any): Promise<void>
mkdir(dir: string): Promise<void | string>
stat(f: string): Promise<{ mtime: Date }>
Expand Down
Loading