Skip to main content
Version: v5.1

FileSystemWrapper

Use the FileSystemWrapper class to perform operations on system files and folders when using platform APIs.

createReadStream

Creates a readable stream from the file you pass.

ParameterRequiredTypeDescription
fileYesStringPath to the file
optionsNoObjectDefine additional options for your read stream
options.flagsNoStringA file system flag that describes how to handle the file, such as 'r' to read, 'a' to append, or 'w' to write. The default is 'w'.
options.encodingNoString or nullFile encoding. The default is 'utf8'.
options.fdNoNumber or FileHandlerA file descriptor to use for the stream instead of opening a new one. If specified, the path parameter is ignored.
options.modeNoNumberDefine the file's permissions with an octal. The default is 0o666, which is readable and writable for the owner, group, and others.
options.autoCloseNoBooleanIf set to false, the file descriptor won't automatically closed when the stream ends. The default value is true.
options.emitCloseNoBooleanIf set to false, the 'close' event will not be emitted once the file is closed.
options.startNoNumberByte offset to start reading the file.
options.endNoNumberByte offset to stop reading the file.
options.highWaterMarkNoNumberThe maximum amount of data in bytes that can buffer at a time before the stream pauses, allowing the consumer to catch up. The default value is 64 * 1024.
options.fsNoObjectnull
options.signalNoAbortSignal or nullNotifies observers when the stream aborts.

Returns

{Promise<ReadStream>} - Returns a promise with a read stream

Example


let file = {"folder/folder/example.txt"};

let options = {
mode: 0o777,
flag: 'r'
};

const readableStream = await fs.createReadStream(file, options);

createWriteStream

Creates a writable stream for the file you pass.

ParameterRequiredTypeDescription
fileYesStringPath to the file
optionsNoObjectDefine additional options for your write stream
options.flagsNoStringA file system flag that describes how to handle the file, such as 'r' to read, 'a' to append, or 'w' to write. The default is 'w'.
options.encodingNoString or nullFile encoding. The default is 'utf8'.
options.fdNoNumber or StringA file descriptor to use for the stream instead of opening a new one. If specified, the path parameter is ignored.
options.modeNoNumberDefine the file's permissions with an octal. The default is 0o666, which is readable and writable for the owner, group, and others.
options.autoCloseNoBooleanIf set to false, the file descriptor won't automatically be closed when the stream ends. The default value is true.
options.emitCloseNoBooleanIf set to false, the 'close' event will not be emitted once the file is closed.
options.startNoNumberByte offset to start reading the file.
options.fsNoObject or nullDo we need this? (Specify the appropriate type if needed)
options.signalNoAbortSignal or nullNotifies observers when the stream aborts.
options.highWaterMarkNoNumberThe maximum amount of data in bytes that can buffer at a time before the stream pauses, allowing the consumer to catch up. The default value is 16384.
options.flushNoBooleanSet to 'true' to flush data after it is successfully written to the file. The default is 'false'.

Returns

{Promise<WriteStream>} - Returns a promise with a writable stream

Example

let file = "folder/folder/example.txt";

let options = {
mode: 0o777,
flag: 'w'
};

const writableStream = await fs.createWriteStream(file, options);

writableStream.write('Hello, World!');
writableStream.end();

existsSync

Synchronously checks if the file you pass exists in the File Service.

ParameterRequiredTypeDescription
fileYesStringPath to the file

Returns

{Boolean} - Returns true if the file exists and false if it does not exist

Example


let file = "folder/folder/example.txt";

if (await fs.existsSync(file)) {
// Function to perform if the file exists
} else {
// Function to perform if the file doesn't exist
}

Asynchronously deletes the file you pass.

ParameterRequiredTypeDescription
fileYesStringPath to the file

Example

let file = "folder/folder/example.txt";

await fs.unlink(file);

Returns

{Promise} - Returns a promise that resolves when the file is successfully deleted

unlinkSync

Synchronously deletes the file you pass.

ParameterRequiredTypeDescription
fileYesStringPath to the file

Example


const file = "folder/folder/example.txt";

fs.unlinkSync(file);

Returns

No return value

mkdirsSync

Synchronously creates a directory and any required subdirectories.

ParameterRequiredTypeDescription
dirYesStringDirectory path.
optionsYesInteger or ObjectDefine the file's permissions with an octal. The default is 0o666, which is readable and writable for the owner, group, and others. Pass the octal directly or as an object with a 'mode' property and the octal as its value.

Example


const dir = "folder/folder/example";
const mode = 0o2775;
const options = {
mode: 0o2775
};

fs.mkdirsSync(dir, mode);
fs.mkdirsSync(dir, options);

Returns

No return value

readFileSync

Synchronously creates a readable stream from the file you pass.

ParameterRequiredTypeDescription
fileYesStringPath to the file
optionsNoFileOptionsPass an options object. The default is 'undefined'.
options.encodingNoString or nullFile encoding. The default is 'utf8'.
options.flagsNoStringA file system flag that describes how to handle the file, such as 'r' to read, 'a' to append, or 'w' to write. The default is 'w'.
options.signalNoAbortSignalnull

Example


const file = "folder/folder/example.txt";

const readableStream = await fs.readFileSync(file);

Returns

{ReadStream} - Returns a promise with a read stream

readdirSync

Synchronously reads the contents of a directory.

ParameterRequiredTypeDescription
dirYesStringPath to the directory.

Example


let dir = "root/folder/";

const readableDir = await fs.readdirSync(dir);

Returns

{ReadStream} - Returns a promise with a read stream

rmSync

Synchronously removes files and directories.

ParameterRequiredTypeDescription
fileYesStringPath to the directory.
optionsNoOptionsOptionally pass an options object. The default value is undefined.
options.forceNoBooleanSet to true to ignore exceptions if the path does not exist. The default value is false.
options.maxRetriesNoNumberSet the number of retries if an EBUSY, EMFILE, ENFILE, ENOTEMPTY, or EPERM error occurs. The retries occur with a linear backoff wait of the time defined in 'retryDelay'. The default value is 0. This option is ignored if 'recursive' is not true.
options.recursiveNoBooleanSet to true to recursively remove the directory. On failure, the recursive operation retries. The default value is false.
options.retryDelayNoNumberThe time in milliseconds to wait between retries. This option is ignored if 'recursive' is false. The default value is 100.

Example


const file = "root/folder/";
const options = {
maxRetries: 10,
recursive: true
};

await fs.rmSync(file, options);

Returns

No return value

writeFileSync

Synchronously writes data to a file.

ParameterRequiredTypeDescription
fileYesStringPath to the file
dataYesString or BufferContent to write to the file as a string or buffer
optionsNoOptionsOptionally pass an options object. The default value is undefined.
options.encodingNoString or nullFile encoding. The default is 'utf8'.
options.modeNoNumberDefine the file's permissions with an octal. The default is 0o666, which is readable and writable for the owner, group, and others.
options.flagNoStringA file system flag that describes how to handle the file, such as 'r' to read, 'a' to append, or 'w' to write. The default is 'w'.
options.flushNoBooleanSet to 'true' to flush data after it is successfully written to the file. The default is 'false'.

Example


let file = "folder/folder/example.txt";
let data = "String to write";

const writtenFile = await fs.writeFileSync(file, data);

Returns

No return value

rmdirSync

Synchronously removes a directory.

ParameterRequiredTypeDescription
dirYesStringPath to the directory.
optionsNoOptionsOptionally pass an options object. The default value is undefined.
options.maxRetriesNoNumberSet the number of retries if an EBUSY, EMFILE, ENFILE, ENOTEMPTY, or EPERM error occurs. The retries occur with a linear backoff wait of the time defined in 'retryDelay'. The default value is 0. This option is ignored if 'recursive' is not true.
options.recursiveNoBooleanSet to true to recursively remove the directory. On failure, the recursive operation retries. The default value is false.
options.retryDelayNoNumberThe time in milliseconds to wait between retries. This option is ignored if 'recursive' is false. The default value is 100.

Example


let dir = "root-folder/folder";

await fs.rmdirSync(dir, options);

const options = {
maxRetries: 10,
recursive: true
};

Returns

No return value