@devries-cameron20 / composio Mirror · read-only
Fork 0

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

Mirrored from https://github.com/Camo67/composio.git

Synced Nov 21, 04:45 UTC

next
ts 25 lines 1.04 KB
Raw
1 import { Effect, FileSystem, Random } from 'effect';
2
3 /**
4 * Writes `content` to a sibling temp file and renames it over `filePath`.
5 *
6 * Renaming within a filesystem is atomic, so a reader — another CLI process,
7 * or the next run after this one is killed mid-write — sees either the old
8 * file or the new one, never a truncated mix. The temp file is a sibling
9 * rather than a tmpdir entry so both paths stay on the same volume, where
10 * rename cannot degrade into a copy.
11 */
12 export const writeFileAtomic = (filePath: string, content: string) =>
13 Effect.gen(function* () {
14 const fs = yield* FileSystem.FileSystem;
15
16 // Concurrent writers must not share a temp path, or one would rename the
17 // other's half-written file into place.
18 const suffix = yield* Random.nextIntBetween(0x100000, 0xffffff);
19 const tempPath = `${filePath}.tmp-${suffix.toString(16)}`;
20
21 yield* fs.writeFileString(tempPath, content);
22 yield* fs
23 .rename(tempPath, filePath)
24 .pipe(Effect.onError(() => fs.remove(tempPath).pipe(Effect.ignore)));
25 });