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

refactor: use fs.promises in module loader, Part 2 #4372

Merged
merged 2 commits into from Jan 30, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion 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');
5 changes: 3 additions & 2 deletions src/ModuleLoader.ts
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
Expand Down
27 changes: 4 additions & 23 deletions 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<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 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);
}
};
18 changes: 9 additions & 9 deletions 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',
Expand All @@ -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')]
}
};
@@ -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;

Expand All @@ -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);
}
};
15 changes: 7 additions & 8 deletions 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;

Expand All @@ -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);
}
};
@@ -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;

Expand All @@ -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);
}
};