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

next
ts 127 lines 3.21 KB
Raw
1 import * as fs from 'node:fs';
2 import * as os from 'node:os';
3 import * as path from 'node:path';
4 import { isFsCaseSensitive } from 'is-fs-case-sensitive';
5 import type { Platform, Uint8ArrayEncoding } from './types';
6
7 const findNearestExistingDirectory = (filePath: string): string => {
8 let candidate = path.resolve(filePath);
9
10 try {
11 if (!fs.statSync(candidate).isDirectory()) {
12 candidate = path.dirname(candidate);
13 }
14 } catch {
15 candidate = path.dirname(candidate);
16 }
17
18 while (true) {
19 try {
20 if (fs.statSync(candidate).isDirectory()) {
21 return candidate;
22 }
23 } catch {
24 // Walk toward the filesystem root until an existing directory is found.
25 }
26
27 const parent = path.dirname(candidate);
28 if (parent === candidate) {
29 return candidate;
30 }
31 candidate = parent;
32 }
33 };
34
35 /**
36 * Node.js platform implementation.
37 * Provides full file system and OS operations using Node.js built-in modules.
38 */
39 export const platform = {
40 supportsFileSystem: true,
41
42 homedir(): string | null {
43 try {
44 return os.homedir();
45 } catch {
46 return null;
47 }
48 },
49
50 joinPath(...paths: string[]): string {
51 return path.join(...paths);
52 },
53
54 resolvePath(...paths: string[]): string {
55 return path.resolve(...paths);
56 },
57
58 isAbsolutePath(filePath: string): boolean {
59 return path.isAbsolute(filePath);
60 },
61
62 basename(filePath: string): string {
63 return path.basename(filePath);
64 },
65
66 existsSync(filePath: string): boolean {
67 return fs.existsSync(filePath);
68 },
69
70 realpathSync(filePath: string): string {
71 return fs.realpathSync(filePath);
72 },
73
74 isFileSystemCaseSensitive(filePath: string): boolean {
75 try {
76 return isFsCaseSensitive(findNearestExistingDirectory(filePath));
77 } catch {
78 // Detection must not weaken this security guard. If the target mount
79 // cannot be inspected, compare case-insensitively and accept possible
80 // false positives instead of allowing a casing bypass.
81 return false;
82 }
83 },
84
85 mkdirSync(dirPath: string): void {
86 fs.mkdirSync(dirPath, { recursive: true });
87 },
88
89 readFileSync(filePath: string, encoding?: Uint8ArrayEncoding): string | Uint8Array {
90 if (encoding === undefined) {
91 const buf = fs.readFileSync(filePath, { encoding: null });
92 const uint8 = new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);
93 return uint8;
94 }
95
96 const str = fs.readFileSync(filePath, { encoding });
97 return str;
98 },
99
100 writeFileSync(
101 filePath: string,
102 content: string | Uint8Array,
103 encoding?: Uint8ArrayEncoding
104 ): void {
105 if (encoding && typeof content === 'string') {
106 fs.writeFileSync(filePath, content, { encoding });
107 } else {
108 fs.writeFileSync(filePath, content);
109 }
110 },
111
112 writeFileExclusiveSync(filePath: string, content: Uint8Array): void {
113 const fd = fs.openSync(filePath, 'wx');
114 try {
115 try {
116 fs.writeFileSync(fd, content);
117 } finally {
118 fs.closeSync(fd);
119 }
120 } catch (error) {
121 // The path was created here, so a failed write or close must not leave
122 // a partial file that a retry would treat as an existing download.
123 fs.rmSync(filePath, { force: true });
124 throw error;
125 }
126 },
127 } as Platform;