Skip to content

Commit

Permalink
fix(bundle-source): Accommodate Windows lack of atomic rename (merge #…
Browse files Browse the repository at this point in the history
  • Loading branch information
kriskowal committed Jan 11, 2024
2 parents b3bed95 + 48135aa commit 6bbcc75
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
5 changes: 3 additions & 2 deletions packages/bundle-source/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,12 +296,13 @@ export const makeNodeBundleCache = async (
pid,
nonce,
) => {
const [fs, path, url, crypto, timers] = await Promise.all([
const [fs, path, url, crypto, timers, os] = await Promise.all([
await loadModule('fs'),
await loadModule('path'),
await loadModule('url'),
await loadModule('crypto'),
await loadModule('timers'),
await loadModule('os'),
]);

if (nonce === undefined) {
Expand All @@ -316,6 +317,6 @@ export const makeNodeBundleCache = async (

const cwd = makeFileReader('', { fs, path });
await fs.promises.mkdir(dest, { recursive: true });
const destWr = makeAtomicFileWriter(dest, { fs, path }, pid, nonce);
const destWr = makeAtomicFileWriter(dest, { fs, path, os }, pid, nonce);
return makeBundleCache(destWr, cwd, readPowers, options);
};
19 changes: 17 additions & 2 deletions packages/bundle-source/src/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,19 +120,34 @@ export const makeFileWriter = (
* >,
* },
* path: Pick<import('path'), 'resolve' | 'relative' | 'normalize'>,
* os: Pick<import('os'), 'platform'>,
* }} io
* @param {number} [pid]
* @param {number} [nonce]
* @param {(there: string) => ReturnType<makeAtomicFileWriter>} make
*/
export const makeAtomicFileWriter = (
fileName,
{ fs, path },
{ fs, path, os },
pid = undefined,
nonce = undefined,
make = there => makeAtomicFileWriter(there, { fs, path }, pid, nonce, make),
make = there =>
makeAtomicFileWriter(there, { fs, path, os }, pid, nonce, make),
) => {
const writer = makeFileWriter(fileName, { fs, path }, make);

// Windows does not support atomic rename so we do the next best albeit racey
// thing.
if (os.platform() === 'win32') {
return harden({
...writer,
atomicWriteText: async (txt, opts) => {
await writer.writeText(txt, opts);
return writer.readOnly().stat();
},
});
}

return harden({
...writer,
atomicWriteText: async (txt, opts) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/bundle-source/src/tool.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { main } from './main.js';
const { Fail } = assert;

/* global process */
const allowedModules = ['fs', 'path', 'url', 'crypto', 'timers'];
const allowedModules = ['fs', 'path', 'url', 'crypto', 'timers', 'os'];
const loadModule = spec => {
allowedModules.includes(spec) || Fail`Not allowed to import ${spec}`;
return import(spec);
Expand Down

0 comments on commit 6bbcc75

Please sign in to comment.