Skip to content

Commit

Permalink
refactor: use fs.promises in module loader
Browse files Browse the repository at this point in the history
  • Loading branch information
dnalborczyk committed Jan 29, 2022
1 parent 5364114 commit 9aae18f
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 71 deletions.
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);
}
};

0 comments on commit 9aae18f

Please sign in to comment.