Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Since babel can run in many workers, and since we write the external stubs as a side-effect of discovering that they're needed from within babel, it's possible that a stub is written at the exact moment another worker is reading it. So we need to make sure our writes are atomic.
- Loading branch information
Showing
2 changed files
with
17 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { randomBytes } from 'crypto'; | ||
import { outputFileSync } from 'fs-extra'; | ||
import { renameSync } from 'fs'; | ||
|
||
// if the path already exists, this will replace it atomically so that nobody | ||
// will ever read an inconsistent state. | ||
// | ||
// if it doesn't already exist, this will create any intervening directories and | ||
// then create the file. | ||
export function atomicWrite(path: string, content: string) { | ||
let suffix = randomBytes(8).toString('hex'); | ||
outputFileSync(path + suffix, content); | ||
renameSync(path + suffix, path); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters