NFS

NFS

NFS emulation on top of a MutableData

Constructor

new NFS()

Source:

Methods

create(content) → {Promise.<File>}

Helper function to create and save file to the network

Parameters:
Name Type Description
content String | Buffer
Source:
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.create(content);
    } catch(err) {
      throw err;
    }
};

delete(fileName, version) → {Promise.<Number>}

Delete a file from path. Directly commit to the network.

Parameters:
Name Type Description
fileName String | Buffer
version Number | CONSTANTS.GET_NEXT_VERSION

the version successor number

Source:
Example
// Assumes MutableData interface has been obtained
const content = '<html><body><h1>Updated WebSite</h1></body></html>';
const fileName = 'index.html';
const asyncFn = async () => {
    try {
      const version = await mData.getVersion();
      const nfs = await mData.emulateAs('NFS');
      const fileContext = await nfs.create(content);
      fileContext = await nfs.insert(fileName, fileContext);
      const version = await nfs.delete(fileName, version + 1);
    } catch(err) {
      throw err;
    }
};

fetch(fileName) → {Promise.<File>}

Find the file of the given filename (aka keyName in the MutableData)

Parameters:
Name Type Description
fileName String

the path/file name

Source:
Example
// Assumes MutableData interface has been obtained
const content = '<html><body><h1>WebSite</h1></body></html>';
const asyncFn = async () => {
    const fileName = 'index.html';
    try {
      const nfs = await mData.emulateAs('NFS');
      const fileContext = await nfs.create(content);
      await nfs.insert(fileName, fileContext);
      const fileContext = await nfs.fetch(fileName);
    } catch(err) {
      throw err;
    }
};

insert(fileName, file, userMetadata) → {Promise.<File>}

Insert the given file into the underlying MutableData, directly commit to the network.

Note: As this application layer, the network does not check any of the metadata provided.

Parameters:
Name Type Description
fileName String | Buffer

The path to store the file under

file File

The file to serialise and store

userMetadata String | Buffer
Source:
Example
// Assumes MutableData interface has been obtained
const content = '<html><body><h1>WebSite</h1></body></html>';
const userMetadata = 'text/html';
const asyncFn = async () => {
    try {
      const nfs = await mData.emulateAs('NFS');
      let fileContext = await nfs.create(content);
      const fileName = 'index.html';
      fileContext = await nfs.insert(fileName, fileContext, userMetadata);
    } catch(err) {
      throw err;
    }
};

open(file, openModeopt) → {Promise.<File>}

Open a file for reading or writing.

Open modes (these constants are exported by the safe-app-nodejs package):

CONSTANTS.NFS_FILE_MODE_OVERWRITE: Replaces the entire content of the file when writing data.

CONSTANTS.NFS_FILE_MODE_APPEND: Appends to existing data in the file.

CONSTANTS.NFS_FILE_MODE_READ: Open file to read.

Parameters:
Name Type Attributes Default Description
file File | null

If no File is passed, then a new instance is created in CONSTANTS.NFS_FILE_MODE_OVERWRITE

openMode Number | CONSTANTS.NFS_FILE_MODE_OVERWRITE | CONSTANTS.NFS_FILE_MODE_APPEND | CONSTANTS.NFS_FILE_MODE_READ <optional>
CONSTANTS.NFS_FILE_MODE_OVERWRITE
Source:
Example
// Assumes MutableData interface has been obtained
const asyncFn = async () => {
    try {
      const nfs = await mData.emulateAs('NFS');
      const fileContext = await nfs.open();
    } catch(err) {
      throw err;
    }
};

update(fileName, file, version, userMetadata) → {Promise.<File>}

Replace a path with a new file. Directly commit to the network.

CONSTANTS.GET_NEXT_VERSION: Applies update to next file version.

Note: As this application layer, the network does not check any of the metadata provided.

Parameters:
Name Type Description
fileName String | Buffer

the path to store the file under

file File

the file to serialise and store

version Number | CONSTANTS.GET_NEXT_VERSION

the version successor number

userMetadata String | Buffer

optional parameter for updating user metadata

Source:
Example
// Assumes MutableData interface has been obtained
const content = '<html><body><h1>Updated WebSite</h1></body></html>';
const userMetadata = 'text/html';
const asyncFn = async () => {
    try {
      const version = safe.CONSTANTS.GET_NEXT_VERSION;
      const nfs = await mData.emulateAs('NFS');
      const fileContext = await nfs.create(content);
      const fileName = 'index.html';
      fileContext = await nfs.update(fileName, fileContext, version + 1, userMetadata);
    } catch(err) {
      throw err;
    }
};