FileSystem
Provides file system operations within a Sandbox.
Constructors
new FileSystem()
new FileSystem( sandboxId: string, toolboxApi: ToolboxApi, getRootDir: () => Promise<string>): FileSystemParameters:
sandboxIdstringtoolboxApiToolboxApigetRootDir() => Promise<string>
Returns:
FileSystem
Methods
createFolder()
createFolder(path: string, mode: string): Promise<void>Create a new directory in the Sandbox with specified permissions.
Parameters:
pathstring - Path where the directory should be created. Relative paths are resolved based on the user’s root directory.modestring - Directory permissions in octal format (e.g. “755”)
Returns:
Promise<void>
Example:
// Create a directory with standard permissionsawait fs.createFolder('app/data', '755');deleteFile()
deleteFile(path: string): Promise<void>Deletes a file or directory from the Sandbox.
Parameters:
pathstring - Path to the file or directory to delete. Relative paths are resolved based on the user’s root directory.
Returns:
Promise<void>
Example:
// Delete a fileawait fs.deleteFile('app/temp.log');downloadFile()
Call Signature
downloadFile(remotePath: string, timeout?: number): Promise<Buffer<ArrayBufferLike>>Downloads a file from the Sandbox. This method loads the entire file into memory, so it is not recommended for downloading large files.
Parameters:
remotePathstring - Path to the file to download. Relative paths are resolved based on the user’s root directory.timeout?number - Timeout for the download operation in seconds. 0 means no timeout. Default is 30 minutes.
Returns:
Promise<Buffer<ArrayBufferLike>>- The file contents as a Buffer.
Example:
// Download and process a fileconst fileBuffer = await fs.downloadFile('tmp/data.json');console.log('File content:', fileBuffer.toString());Call Signature
downloadFile( remotePath: string, localPath: string,timeout?: number): Promise<void>Downloads a file from the Sandbox and saves it to a local file. This method uses streaming to download the file, so it is recommended for downloading larger files.
Parameters:
remotePathstring - Path to the file to download in the Sandbox. Relative paths are resolved based on the user’s root directory.localPathstring - Path to save the downloaded file.timeout?number - Timeout for the download operation in seconds. 0 means no timeout. Default is 30 minutes.
Returns:
Promise<void>
Example:
// Download and save a fileawait fs.downloadFile('tmp/data.json', 'local_file.json');findFiles()
findFiles(path: string, pattern: string): Promise<Match[]>Searches for text patterns within files in the Sandbox.
Parameters:
pathstring - Directory to search in. Relative paths are resolved based on the user’s root directory.patternstring - Search pattern
Returns:
Promise<Match[]>- Array of matches with file and line information
Example:
// Find all TODO comments in TypeScript filesconst matches = await fs.findFiles('app/src', 'TODO:');matches.forEach(match => { console.log(`${match.file}:${match.line}: ${match.content}`);});getFileDetails()
getFileDetails(path: string): Promise<FileInfo>Retrieves detailed information about a file or directory.
Parameters:
pathstring - Path to the file or directory. Relative paths are resolved based on the user’s root directory.
Returns:
Promise<FileInfo>- Detailed file information including size, permissions, modification time
Example:
// Get file detailsconst info = await fs.getFileDetails('app/config.json');console.log(`Size: ${info.size}, Modified: ${info.modTime}`);listFiles()
listFiles(path: string): Promise<FileInfo[]>Lists contents of a directory in the Sandbox.
Parameters:
pathstring - Directory path to list. Relative paths are resolved based on the user’s root directory.
Returns:
Promise<FileInfo[]>- Array of file and directory information
Example:
// List directory contentsconst files = await fs.listFiles('app/src');files.forEach(file => { console.log(`${file.name} (${file.size} bytes)`);});moveFiles()
moveFiles(source: string, destination: string): Promise<void>Moves or renames a file or directory.
Parameters:
sourcestring - Source path. Relative paths are resolved based on the user’s root directory.destinationstring - Destination path. Relative paths are resolved based on the user’s root directory.
Returns:
Promise<void>
Example:
// Move a file to a new locationawait fs.moveFiles('app/temp/data.json', 'app/data/data.json');replaceInFiles()
replaceInFiles( files: string[], pattern: string,newValue: string): Promise<ReplaceResult[]>Replaces text content in multiple files.
Parameters:
filesstring[] - Array of file paths to process. Relative paths are resolved based on the user’spatternstring - Pattern to replacenewValuestring - Replacement text
Returns:
Promise<ReplaceResult[]>- Results of the replace operation for each file
Example:
// Update version number across multiple filesconst results = await fs.replaceInFiles( ['app/package.json', 'app/version.ts'], '"version": "1.0.0"', '"version": "1.1.0"');searchFiles()
searchFiles(path: string, pattern: string): Promise<SearchFilesResponse>Searches for files and directories by name pattern in the Sandbox.
Parameters:
pathstring - Directory to search in. Relative paths are resolved based on the user’spatternstring - File name pattern (supports globs)
Returns:
Promise<SearchFilesResponse>- Search results with matching files
Example:
// Find all TypeScript filesconst result = await fs.searchFiles('app', '*.ts');result.files.forEach(file => console.log(file));setFilePermissions()
setFilePermissions(path: string, permissions: FilePermissionsParams): Promise<void>Sets permissions and ownership for a file or directory.
Parameters:
pathstring - Path to the file or directory. Relative paths are resolved based on the user’s root directory.permissionsFilePermissionsParams - Permission settings
Returns:
Promise<void>
Example:
// Set file permissions and ownershipawait fs.setFilePermissions('app/script.sh', { owner: 'synodes', group: 'users', mode: '755' // Execute permission for shell script});uploadFile()
Call Signature
uploadFile( file: Buffer, remotePath: string,timeout?: number): Promise<void>Uploads a file to the Sandbox. This method loads the entire file into memory, so it is not recommended for uploading large files.
Parameters:
fileBuffer - Buffer of the file to upload.remotePathstring - Destination path in the Sandbox. Relative paths are resolved based on the user’s root directory.timeout?number - Timeout for the upload operation in seconds. 0 means no timeout. Default is 30 minutes.
Returns:
Promise<void>
Example:
// Upload a configuration fileawait fs.uploadFile(Buffer.from('{"setting": "value"}'), 'tmp/config.json');Call Signature
uploadFile( localPath: string, remotePath: string,timeout?: number): Promise<void>Uploads a file from the local file system to the Sandbox. This method uses streaming to upload the file, so it is recommended for uploading larger files.
Parameters:
localPathstring - Path to the local file to upload.remotePathstring - Destination path in the Sandbox. Relative paths are resolved based on the user’s root directory.timeout?number - Timeout for the upload operation in seconds. 0 means no timeout. Default is 30 minutes.
Returns:
Promise<void>
Example:
// Upload a local fileawait fs.uploadFile('local_file.txt', 'tmp/file.txt');uploadFiles()
uploadFiles(files: FileUpload[], timeout?: number): Promise<void>Uploads multiple files to the Sandbox. If files already exist at the destination paths, they will be overwritten.
Parameters:
filesFileUpload[] - Array of files to upload.timeout?number = … - Timeout for the upload operation in seconds. 0 means no timeout. Default is 30 minutes.
Returns:
Promise<void>
Example:
// Upload multiple text filesconst files = [ { source: Buffer.from('Content of file 1'), destination: '/tmp/file1.txt' }, { source: 'app/data/file2.txt', destination: '/tmp/file2.txt' }, { source: Buffer.from('{"key": "value"}'), destination: '/tmp/config.json' }];await fs.uploadFiles(files);FilePermissionsParams
Parameters for setting file permissions in the Sandbox.
Properties:
group?string - Group owner of the filemode?string - File mode/permissions in octal format (e.g. “644”)owner?string - User owner of the file
Example:
const permissions: FilePermissionsParams = { mode: '644', owner: 'synodes', group: 'users'};FileUpload
Represents a file to be uploaded to the Sandbox.
Properties:
destinationstring - Absolute destination path in the Sandbox. Relative paths are resolved based on the user’s root directory.sourcestring | Buffer<ArrayBufferLike> - File to upload. If a Buffer, it is interpreted as the file content which is loaded into memory. Make sure it fits into memory, otherwise use the local file path which content will be streamed to the Sandbox.