Used to check for browser translation.
用于检测浏览器翻译。
ブラウザの翻訳を検出する
Utilities

File System


This section provides helper functions for common file system operations within the DID Spaces SDK, including robust file removal and flexible data writing capabilities. These utilities simplify interactions with local files when managing data for DID Spaces. For other common utilities, refer to the Utilities section.

Remove#

Removes a file or directory from the local file system. This function first attempts to truncate the file and then removes it, providing a more robust deletion mechanism, especially for files that might be locked or partially written.

Parameters

Name

Type

Description

path

string

The path to the file or directory to be removed.

options

object

Optional. An object containing options for the removal operation. Default is { force: true, maxRetries: 3 }.

options.force

boolean

When true, exceptions will be ignored if path does not exist.

options.maxRetries

number

The number of times Node.js will retry an operation when it fails due to a system error.

Returns

Name

Type

Description

Promise<void>

Promise

A Promise that resolves when the file or directory has been successfully removed.

Example

import { remove } from '@did-space/client/libs/fs';

async function removeFile(filePath) {
try {
await remove(filePath);
console.log(`File removed: ${filePath}`);
} catch (error) {
console.error(`Error removing file ${filePath}:`, error.message);
}
}

// Example usage:
removeFile('/path/to/your/file.txt');

This example demonstrates how to use the remove function to delete a specified file. The default options ensure that the operation is retried and ignores errors if the file doesn't exist.

Write Data#

Writes various types of data (string, Buffer, or Stream) to a specified target file path. This function ensures that the parent directory for the target file exists before attempting to write the data.

Parameters

Name

Type

Description

target

string

The full path to the file where the data will be written.

data

string | Buffer | Stream

The data to be written. This can be a string, a Buffer, or a Node.js Stream.

Returns

Name

Type

Description

Promise<void>

Promise

A Promise that resolves when the data has been successfully written to the file.

Example

import { writeData } from '@did-space/client/libs/data';
import { Readable } from 'stream';

async function writeContentToFile() {
const filePath = '/path/to/your/output.txt';

// Example 1: Write a string
try {
await writeData(filePath, 'Hello, DID Spaces!');
console.log('String data written successfully.');
} catch (error) {
console.error('Error writing string data:', error.message);
}

// Example 2: Write a Buffer
const bufferData = Buffer.from('Binary content here.');
try {
await writeData('/path/to/your/binary.bin', bufferData);
console.log('Buffer data written successfully.');
} catch (error) {
console.error('Error writing buffer data:', error.message);
}

// Example 3: Write from a Stream
const streamData = Readable.from(['Streamed ', 'content ', 'example.']);
try {
await writeData('/path/to/your/streamed.txt', streamData);
console.log('Stream data written successfully.');
} catch (error) {
console.error('Error writing stream data:', error.message);
}
}

writeContentToFile();

This example showcases writeData's versatility by demonstrating how to write string, Buffer, and Stream data to different files. The function automatically handles directory creation if necessary.

Stream To File#

Converts the completion or error events of a Node.js Writable Stream into a Promise. This utility is particularly useful when you need to await the completion of a stream operation, such as writing data to a file.

Parameters

Name

Type

Description

writeStream

Stream

The Writable Stream instance whose completion or error events are to be converted into a Promise.

Returns

Name

Type

Description

Promise<void>

Promise

A Promise that resolves when the writeStream emits a finish event, or rejects if it emits an error event.

Example

import { createWriteStream } from 'fs';
import { Readable } from 'stream';
import { streamToFile } from '@did-space/client/libs/stream';

async function writeStreamAndAwait() {
const filePath = '/path/to/your/awaitable_stream.txt';
const writeStream = createWriteStream(filePath);
const readableStream = Readable.from(['First chunk. ', 'Second chunk.']);

try {
// Pipe the readable stream to the writable stream
readableStream.pipe(writeStream);

// Await the completion of the write stream
await streamToFile(writeStream);
console.log(`Streamed content successfully written to ${filePath}`);
} catch (error) {
console.error('Error writing stream:', error.message);
}
}

writeStreamAndAwait();

This example demonstrates how to pipe data from a Readable stream to a file using createWriteStream, and then use streamToFile to asynchronously wait for the entire writing process to complete.


This section provided an overview of the file system utilities available in the DID Spaces SDK, enabling you to manage local files efficiently. Continue exploring other essential helper functions in the DID utilities section, or learn about HTTP Clients for network operations.