Skip to content

Commit

Permalink
feat(compartment-mapper): One concurrent read
Browse files Browse the repository at this point in the history
  • Loading branch information
kriskowal committed Jul 26, 2023
1 parent cf668e2 commit 29048a2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
17 changes: 14 additions & 3 deletions packages/compartment-mapper/src/node-powers.js
Expand Up @@ -40,15 +40,26 @@ const makeReadPowersSloppy = ({ fs, url = undefined, crypto = undefined }) => {
const pathToFileURL =
url === undefined ? fakePathToFileURL : url.pathToFileURL;

let readMutex = Promise.resolve(undefined);

/**
* @param {string} location
*/
const read = async location => {
const promise = readMutex;
let release = Function.prototype;
readMutex = new Promise(resolve => {
release = resolve;
});
await promise;

const path = fileURLToPath(location);
try {
const path = fileURLToPath(location);
// We await here to ensure that we release the mutex only after
// completing the read.
return await fs.promises.readFile(path);
} catch (error) {
throw Error(error.message);
} finally {
release(undefined);
}
};

Expand Down
6 changes: 5 additions & 1 deletion packages/compartment-mapper/test/scaffold.js
Expand Up @@ -15,7 +15,11 @@ import {
} from '../index.js';
import { makeReadPowers } from '../src/node-powers.js';

export const readPowers = makeReadPowers({ fs, crypto, url });
export const readPowers = makeReadPowers({
fs,
crypto,
url,
});

export const sanitizePaths = (text = '', tolerateLineChange = false) => {
if (tolerateLineChange) {
Expand Down

0 comments on commit 29048a2

Please sign in to comment.