Skip to content

Commit

Permalink
Restrict parallel execution of load hook as well. (#4200)
Browse files Browse the repository at this point in the history
Co-authored-by: Lukas Taegert-Atkinson <lukastaegert@users.noreply.github.com>
  • Loading branch information
schummar and lukastaegert committed Aug 5, 2021
1 parent b34a411 commit f11aed1
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 3 deletions.
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';

0 comments on commit f11aed1

Please sign in to comment.