File

File

NFS-style file operations

Constructor

new File(ref)

Parameters:
Name Type Description
ref Object

the file's metadata including the XoR-name of ImmutableData containing the file's content.

Source:

Members

created

Get UTC date of file context creation

Source:

dataMapName

Get XOR address of file's underlying ImmutableData data map

Source:

modified

Get UTC date of file context modification

Source:

userMetadata

Get metadata passed during file insertion of update

Source:

version

Which version was this? Equals the underlying MutableData's entry version.

Source:

Methods

close() → {Promise}

Close file and commit to network.

Source:
Throws:
Example
// Assumes MutableData interface has been obtained
const content = '<html><body><h1>WebSite</h1></body></html>';
const asyncFn = async () => {
    try {
        const nfs = await mData.emulateAs('NFS');
        const fileContext = await nfs.open();
        await fileContext.write('<buffer or string>');
        await fileContext.close();
    } catch (err) {
        throw err;
    }
};

read(position, len) → {Promise.<{Buffer, Number}>}

Read the file. CONSTANTS.NFS_FILE_START and CONSTANTS.NFS_FILE_END may be used to read the entire content of the file. These constants are exposed by the safe-app-nodejs package.

Parameters:
Name Type Description
position Number | CONSTANTS.NFS_FILE_START
len Number | CONSTANTS.NFS_FILE_END
Source:
Throws:
Example
// Assumes MutableData interface has been obtained
const position = safe.CONSTANTS.NFS_FILE_START;
const len = safe.CONSTANTS.NFS_FILE_END;
const openMode = safe.CONSTANTS.NFS_FILE_MODE_READ;
const asyncFn = async () => {
  try {
    const nfs = await mData.emulateAs('NFS');
    let fileContext = await nfs.create('<buffer or string>');
    fileContext = await nfs.open(fileContext, openMode);
    const data = await fileContext.read(position, len);
  } catch (err) {
    throw err;
  }
};

size() → {Promise.<Number>}

Get file size

Source:
Example
// Assumes MutableData interface has been obtained
const asyncFn = async () => {
  try {
    const nfs = await mData.emulateAs('NFS');
    const fileContext = await nfs.create('<buffer or string>');
    const fileSize = await fileContext.size();
  } catch (err) {
    throw err;
  }
};

write(content) → {Promise}

Write file. Does not commit file to network.

Parameters:
Name Type Description
content Buffer | String
Source:
Throws:
Example
// Assumes MutableData interface has been obtained
const asyncFn = async () => {
    try {
        const nfs = await mData.emulateAs('NFS');
        const fileContext = await nfs.open();
        await fileContext.write('<buffer or string>');
    } catch (err) {
        throw err;
    }
};