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

Restrict parallel execution of load hook. #4200

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 3 additions & 3 deletions src/ModuleLoader.ts
Expand Up @@ -220,9 +220,9 @@ export class ModuleLoader {
timeStart('load modules', 3);
let source: string | SourceDescription;
try {
source =
(await this.pluginDriver.hookFirst('load', [id])) ??
(await this.readQueue.run(async () => readFile(id)));
source = await this.readQueue.run(
async () => (await this.pluginDriver.hookFirst('load', [id])) ?? (await readFile(id))
);
} catch (err) {
timeEnd('load modules', 3);
let msg = `Could not load ${id}`;
Expand Down
@@ -0,0 +1 @@
export const x1 = 1;
@@ -0,0 +1 @@
export const x2 = 2;
@@ -0,0 +1 @@
export const x3 = 3;
@@ -0,0 +1 @@
export const x4 = 4;
@@ -0,0 +1 @@
export const x5 = 5;
@@ -0,0 +1,37 @@
const assert = require('assert');
const fs = require('fs');

const fsReadFile = fs.readFile;
let currentReads = 0;
let maxReads = 0;

module.exports = {
description: 'maxParallelFileReads with plugin',
options: {
maxParallelFileReads: 3,
plugins: [
{
async load(id) {
return new Promise((fulfil, reject) =>
fs.readFile(id, 'utf-8', (err, contents) => (err ? reject(err) : fulfil(contents)))
);
}
}
]
},
before() {
fs.readFile = (path, options, callback) => {
console.log('mock read');
currentReads++;
maxReads = Math.max(maxReads, currentReads);
fsReadFile(path, options, (err, data) => {
currentReads--;
callback(err, data);
});
};
},
after() {
fs.readFile = fsReadFile;
assert.strictEqual(maxReads, 3, 'Wrong number of parallel file reads: ' + maxReads);
}
};
@@ -0,0 +1,5 @@
export * from './1';
export * from './2';
export * from './3';
export * from './4';
export * from './5';