Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use fs.promises in module loader (#4372)
Co-authored-by: Lukas Taegert-Atkinson <lukastaegert@users.noreply.github.com>
- Loading branch information
1 parent
4611705
commit 50f46bc
Showing
8 changed files
with
50 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
import { throwNoFileSystem } from './error'; | ||
|
||
export const readFile = throwNoFileSystem('fs.readFile'); | ||
export const promises = { | ||
readFile: throwNoFileSystem('fs.readFile') | ||
}; | ||
|
||
export const writeFile = throwNoFileSystem('fs.writeFile'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,9 @@ | ||
import fs from 'fs'; | ||
import { promises as fs } from 'fs'; | ||
import { dirname } from './path'; | ||
|
||
export * from 'fs'; | ||
|
||
export const readFile = (file: string): Promise<string> => | ||
new Promise<string>((fulfil, reject) => | ||
fs.readFile(file, 'utf-8', (err, contents) => (err ? reject(err) : fulfil(contents))) | ||
); | ||
|
||
function mkdirpath(path: string) { | ||
const dir = dirname(path); | ||
fs.mkdirSync(dir, { recursive: true }); | ||
} | ||
|
||
export function writeFile(dest: string, data: string | Uint8Array): Promise<void> { | ||
return new Promise<void>((fulfil, reject) => { | ||
mkdirpath(dest); | ||
|
||
fs.writeFile(dest, data, err => { | ||
if (err) { | ||
reject(err); | ||
} else { | ||
fulfil(); | ||
} | ||
}); | ||
}); | ||
export async function writeFile(dest: string, data: string | Uint8Array): Promise<void> { | ||
await fs.mkdir(dirname(dest), { recursive: true }); | ||
await fs.writeFile(dest, data); | ||
} |
15 changes: 7 additions & 8 deletions
15
test/function/samples/max-parallel-file-reads-default/_config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,23 @@ | ||
const assert = require('assert'); | ||
const fs = require('fs'); | ||
const { promises } = require('fs'); | ||
|
||
const fsReadFile = fs.readFile; | ||
const fsReadFile = promises.readFile; | ||
let currentReads = 0; | ||
let maxReads = 0; | ||
|
||
module.exports = { | ||
description: 'maxParallelFileReads not set', | ||
before() { | ||
fs.readFile = (path, options, callback) => { | ||
promises.readFile = async (path, options) => { | ||
currentReads++; | ||
maxReads = Math.max(maxReads, currentReads); | ||
fsReadFile(path, options, (err, data) => { | ||
currentReads--; | ||
callback(err, data); | ||
}); | ||
const content = await fsReadFile(path, options); | ||
currentReads--; | ||
return content; | ||
}; | ||
}, | ||
after() { | ||
fs.readFile = fsReadFile; | ||
promises.readFile = fsReadFile; | ||
assert.strictEqual(maxReads, 5, 'Wrong number of parallel file reads: ' + maxReads); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters