From 9aae18f787620b61b221f2aa5b646139738d6716 Mon Sep 17 00:00:00 2001 From: Daniel Nalborczyk Date: Sat, 29 Jan 2022 17:35:58 -0500 Subject: [PATCH] refactor: use fs.promises in module loader --- browser/fs.ts | 5 +++- src/ModuleLoader.ts | 5 ++-- src/utils/fs.ts | 27 +++---------------- .../_config.js | 15 +++++------ .../max-parallel-file-reads-error/_config.js | 18 ++++++------- .../_config.js | 15 +++++------ .../max-parallel-file-reads-set/_config.js | 15 +++++------ .../_config.js | 21 +++++++-------- 8 files changed, 50 insertions(+), 71 deletions(-) diff --git a/browser/fs.ts b/browser/fs.ts index 31337a88016..4f6ce519593 100644 --- a/browser/fs.ts +++ b/browser/fs.ts @@ -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'); diff --git a/src/ModuleLoader.ts b/src/ModuleLoader.ts index 5b7aa53174e..831a96fe153 100644 --- a/src/ModuleLoader.ts +++ b/src/ModuleLoader.ts @@ -29,7 +29,7 @@ import { errUnresolvedImport, errUnresolvedImportTreatedAsExternal } from './utils/error'; -import { readFile } from './utils/fs'; +import { promises as fs } from './utils/fs'; import { isAbsolute, isRelative, resolve } from './utils/path'; import { Queue } from './utils/queue'; import relativeId from './utils/relativeId'; @@ -240,7 +240,8 @@ export class ModuleLoader { let source: LoadResult; try { source = await this.readQueue.run( - async () => (await this.pluginDriver.hookFirst('load', [id])) ?? (await readFile(id)) + async () => + (await this.pluginDriver.hookFirst('load', [id])) ?? (await fs.readFile(id, 'utf8')) ); } catch (err: any) { timeEnd('load modules', 3); diff --git a/src/utils/fs.ts b/src/utils/fs.ts index 0dc8d1ab65d..42b3535756a 100644 --- a/src/utils/fs.ts +++ b/src/utils/fs.ts @@ -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 => - new Promise((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 { - return new Promise((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 { + await fs.mkdir(dirname(dest), { recursive: true }); + await fs.writeFile(dest, data); } diff --git a/test/function/samples/max-parallel-file-reads-default/_config.js b/test/function/samples/max-parallel-file-reads-default/_config.js index 306952571a6..6284f3cb199 100644 --- a/test/function/samples/max-parallel-file-reads-default/_config.js +++ b/test/function/samples/max-parallel-file-reads-default/_config.js @@ -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); } }; diff --git a/test/function/samples/max-parallel-file-reads-error/_config.js b/test/function/samples/max-parallel-file-reads-error/_config.js index c535cfaa10c..08e4c8d3e85 100644 --- a/test/function/samples/max-parallel-file-reads-error/_config.js +++ b/test/function/samples/max-parallel-file-reads-error/_config.js @@ -1,8 +1,8 @@ -const fs = require('fs'); -const path = require('path'); +const { promises } = require('fs'); +const { join } = require('path'); const { loader } = require('../../../utils.js'); -const fsReadFile = fs.readFile; +const fsReadFile = promises.readFile; module.exports = { description: 'maxParallelFileReads: fileRead error is forwarded', @@ -13,19 +13,19 @@ module.exports = { }) }, before() { - fs.readFile = (path, options, callback) => { + promises.readFile = (path, options) => { if (path.endsWith('dep.js')) { - return callback(new Error('broken')); + throw new Error('broken'); } - fsReadFile(path, options, callback); + fsReadFile(path, options); }; }, after() { - fs.readFile = fsReadFile; + promises.readFile = fsReadFile; }, error: { - message: `Could not load ${path.join(__dirname, 'dep.js')} (imported by main): broken`, - watchFiles: ['main', path.join(__dirname, 'dep.js')] + message: `Could not load ${join(__dirname, 'dep.js')} (imported by main): broken`, + watchFiles: ['main', join(__dirname, 'dep.js')] } }; diff --git a/test/function/samples/max-parallel-file-reads-infinity/_config.js b/test/function/samples/max-parallel-file-reads-infinity/_config.js index 486ed945fd9..82cad65d3ca 100644 --- a/test/function/samples/max-parallel-file-reads-infinity/_config.js +++ b/test/function/samples/max-parallel-file-reads-infinity/_config.js @@ -1,7 +1,7 @@ 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; @@ -11,17 +11,16 @@ module.exports = { maxParallelFileReads: 0 }, 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); } }; diff --git a/test/function/samples/max-parallel-file-reads-set/_config.js b/test/function/samples/max-parallel-file-reads-set/_config.js index de68cc464a9..6b247837538 100644 --- a/test/function/samples/max-parallel-file-reads-set/_config.js +++ b/test/function/samples/max-parallel-file-reads-set/_config.js @@ -1,7 +1,7 @@ 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; @@ -11,17 +11,16 @@ module.exports = { maxParallelFileReads: 3 }, 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, 3, 'Wrong number of parallel file reads: ' + maxReads); } }; diff --git a/test/function/samples/max-parallel-file-reads-with-plugin/_config.js b/test/function/samples/max-parallel-file-reads-with-plugin/_config.js index 0e8b6fd3ea3..b4df04b0a34 100644 --- a/test/function/samples/max-parallel-file-reads-with-plugin/_config.js +++ b/test/function/samples/max-parallel-file-reads-with-plugin/_config.js @@ -1,7 +1,7 @@ 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; @@ -11,26 +11,23 @@ module.exports = { maxParallelFileReads: 3, plugins: [ { - async load(id) { - return new Promise((fulfil, reject) => - fs.readFile(id, 'utf-8', (err, contents) => (err ? reject(err) : fulfil(contents))) - ); + load(id) { + return promises.readFile(id, 'utf-8'); } } ] }, 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, 3, 'Wrong number of parallel file reads: ' + maxReads); } };