-
-
Notifications
You must be signed in to change notification settings - Fork 35
added fsConfig for cacheHandler #889
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
base: canary
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,4 +1,4 @@ | ||||||||||||||||||||||||||||||||||||
import { promises as fsPromises } from 'node:fs'; | ||||||||||||||||||||||||||||||||||||
import {Mode, PathLike, promises as fsPromises} from 'node:fs'; | ||||||||||||||||||||||||||||||||||||
import path from 'node:path'; | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
import type { | ||||||||||||||||||||||||||||||||||||
|
@@ -177,6 +177,20 @@ export type TTLParameters = { | |||||||||||||||||||||||||||||||||||
estimateExpireAge(staleAge: number): number; | ||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||||||||
* A Configuration options for file system management. Used for testing. By default, it's a regular fsPromise. | ||||||||||||||||||||||||||||||||||||
* | ||||||||||||||||||||||||||||||||||||
* @default fsPromise | ||||||||||||||||||||||||||||||||||||
* | ||||||||||||||||||||||||||||||||||||
* @since 1.9.0 | ||||||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||||||
export type fsConfig = { | ||||||||||||||||||||||||||||||||||||
open: typeof fsPromises.open; | ||||||||||||||||||||||||||||||||||||
readFile: typeof fsPromises.readFile; | ||||||||||||||||||||||||||||||||||||
writeFile: typeof fsPromises.writeFile; | ||||||||||||||||||||||||||||||||||||
mkdir: typeof fsPromises.mkdir | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||||||||
* Configuration options for the {@link CacheHandler}. | ||||||||||||||||||||||||||||||||||||
* | ||||||||||||||||||||||||||||||||||||
|
@@ -374,6 +388,12 @@ export class CacheHandler implements NextCacheHandler { | |||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
static #serverDistDir: string; | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
static #fsConf: fsConfig = fsPromises; | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
static setFsConf(fsConf: fsConfig) { | ||||||||||||||||||||||||||||||||||||
CacheHandler.#fsConf = fsConf; | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
Comment on lines
+391
to
+396
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧹 Nitpick (assertive) Assess thread safety with mutable static properties Modifying the static property
🧹 Nitpick (assertive) Ensure consistency in static property and method definitions
Apply this diff to update the names: -static #fsConf: fsConfig = fsPromises;
+static #fsConf: FsConfig = {
+ open: fsPromises.open,
+ readFile: fsPromises.readFile,
+ writeFile: fsPromises.writeFile,
+ mkdir: fsPromises.mkdir,
+ stat: fsPromises.stat,
+ close: (handle) => handle.close(),
+};
-static setFsConf(fsConf: fsConfig) {
+static setFsConf(fsConf: FsConfig) {
CacheHandler.#fsConf = fsConf;
} 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||
static async #readPagesRouterPage(cacheKey: string): Promise<CacheHandlerValue | null> { | ||||||||||||||||||||||||||||||||||||
let cacheHandlerValue: CacheHandlerValue | null = null; | ||||||||||||||||||||||||||||||||||||
let pageHtmlHandle: fsPromises.FileHandle | null = null; | ||||||||||||||||||||||||||||||||||||
|
@@ -392,12 +412,12 @@ export class CacheHandler implements NextCacheHandler { | |||||||||||||||||||||||||||||||||||
const pageHtmlPath = path.join(CacheHandler.#serverDistDir, 'pages', `${cacheKey}.html`); | ||||||||||||||||||||||||||||||||||||
const pageDataPath = path.join(CacheHandler.#serverDistDir, 'pages', `${cacheKey}.json`); | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
pageHtmlHandle = await fsPromises.open(pageHtmlPath, 'r'); | ||||||||||||||||||||||||||||||||||||
pageHtmlHandle = await CacheHandler.#fsConf.open(pageHtmlPath, 'r'); | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
const [pageHtmlFile, { mtimeMs }, pageData] = await Promise.all([ | ||||||||||||||||||||||||||||||||||||
pageHtmlHandle.readFile('utf-8'), | ||||||||||||||||||||||||||||||||||||
pageHtmlHandle.stat(), | ||||||||||||||||||||||||||||||||||||
fsPromises.readFile(pageDataPath, 'utf-8').then((data) => JSON.parse(data) as object), | ||||||||||||||||||||||||||||||||||||
CacheHandler.#fsConf.readFile(pageDataPath, 'utf-8').then((data) => JSON.parse(data) as object), | ||||||||||||||||||||||||||||||||||||
Comment on lines
+415
to
+420
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Codebase verification Direct The verification revealed that while
🔗 Analysis chainVerify all Ensure that all file system operations within Run the following script to check for any missed references: Also applies to: 470-470, 473-474, 564-564, 597-597 Let me verify if there are any actual usages of 🏁 Scripts executedThe following scripts were executed for the analysis: Script: #!/bin/bash
# Description: Find all occurrences of fsPromises in cache-handler.ts
# Test: Search for 'fsPromises' in the file. Expect: No occurrences found.
rg 'fsPromises' packages/cache-handler/src/cache-handler.ts
Length of output: 386 Script: #!/bin/bash
# Search for actual usage of fsPromises in function calls or operations
ast-grep --pattern 'fsPromises.$_($_)'
# Also check for any direct fsPromises usage
ast-grep --pattern 'fsPromises'
Length of output: 3001 |
||||||||||||||||||||||||||||||||||||
]); | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
if (CacheHandler.#debug) { | ||||||||||||||||||||||||||||||||||||
|
@@ -447,11 +467,11 @@ export class CacheHandler implements NextCacheHandler { | |||||||||||||||||||||||||||||||||||
const pageHtmlPath = path.join(CacheHandler.#serverDistDir, 'pages', `${cacheKey}.html`); | ||||||||||||||||||||||||||||||||||||
const pageDataPath = path.join(CacheHandler.#serverDistDir, 'pages', `${cacheKey}.json`); | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
await fsPromises.mkdir(path.dirname(pageHtmlPath), { recursive: true }); | ||||||||||||||||||||||||||||||||||||
await CacheHandler.#fsConf.mkdir(path.dirname(pageHtmlPath), { recursive: true }); | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
await Promise.all([ | ||||||||||||||||||||||||||||||||||||
fsPromises.writeFile(pageHtmlPath, pageData.html), | ||||||||||||||||||||||||||||||||||||
fsPromises.writeFile(pageDataPath, JSON.stringify(pageData.pageData)), | ||||||||||||||||||||||||||||||||||||
CacheHandler.#fsConf.writeFile(pageHtmlPath, pageData.html), | ||||||||||||||||||||||||||||||||||||
CacheHandler.#fsConf.writeFile(pageDataPath, JSON.stringify(pageData.pageData)), | ||||||||||||||||||||||||||||||||||||
Comment on lines
+473
to
+474
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Handle potential JSON stringification errors When writing Consider wrapping |
||||||||||||||||||||||||||||||||||||
]); | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
if (CacheHandler.#debug) { | ||||||||||||||||||||||||||||||||||||
|
@@ -541,7 +561,7 @@ export class CacheHandler implements NextCacheHandler { | |||||||||||||||||||||||||||||||||||
let buildId: string | undefined; | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
try { | ||||||||||||||||||||||||||||||||||||
buildId = await fsPromises.readFile(path.join(serverDistDir, '..', 'BUILD_ID'), 'utf-8'); | ||||||||||||||||||||||||||||||||||||
buildId = await CacheHandler.#fsConf.readFile(path.join(serverDistDir, '..', 'BUILD_ID'), 'utf-8'); | ||||||||||||||||||||||||||||||||||||
} catch (_error) { | ||||||||||||||||||||||||||||||||||||
buildId = undefined; | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
|
@@ -574,7 +594,7 @@ export class CacheHandler implements NextCacheHandler { | |||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
try { | ||||||||||||||||||||||||||||||||||||
const prerenderManifestData = await fsPromises.readFile( | ||||||||||||||||||||||||||||||||||||
const prerenderManifestData = await CacheHandler.#fsConf.readFile( | ||||||||||||||||||||||||||||||||||||
path.join(serverDistDir, '..', 'prerender-manifest.json'), | ||||||||||||||||||||||||||||||||||||
'utf-8', | ||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Refine the
fsConfig
type definition and documentationThe introduction of the
fsConfig
type to encapsulate file system operations is a good approach for enhancing flexibility and testability. However, consider the following improvements:CacheHandler
are included in thefsConfig
type. Methods likestat
andclose
are used but are not defined infsConfig
.fsConfig
toFsConfig
for consistency.Apply this diff to refine the type definition:
📝 Committable suggestion