SAFEApp

SAFEApp

Holds a session with the network and is the primary interface to interact with the network

Constructor

new SAFEApp(appInfo, networkStateCallBackopt, optionsopt)

Parameters:
Name Type Attributes Default Description
appInfo AppInfo
networkStateCallBack function <optional>
null

optional callback function to receive network state updates

options InitOptions <optional>

initilalisation options

Source:
Example
const safe = require( '@maidsafe/safe-node-app' );

const appInfo = {
    id     : 'net.maidsafe.example',
    name   : 'Example SAFE App',
    vendor : 'MaidSafe.net Ltd'
};

const networkStateCallback = (state) => {
    console.log('Network state change event: ', state);
};

const initialisationOptions = {
    log            : true,
    registerScheme : false
};

const asyncFn = async () => {
    try {
        const app = await safe.initialiseApp(
            appInfo,
            networkStateCallBack,
            initialisationOptions
        );
    } catch (err) {
        throw err;
    }
};

Members

appInfo

Returns the AppInfo used to initialise current app.

Source:
Example
const safe = require( '@maidsafe/safe-node-app' );

const appInfo = {
    id     : 'net.maidsafe.example',
    name   : 'Example SAFE App',
    vendor : 'MaidSafe.net Ltd'
};

const asyncFn = async () => {
  try {
    const app = await safe.initialiseApp(appInfo);
    const appInfo = app.appInfo;
  } catch(err) {
    throw err;
  }
};

auth

Get an AuthInterface instance

Source:
Example
const safe = require( '@maidsafe/safe-node-app' );

const appInfo = {
    id     : 'net.maidsafe.example',
    name   : 'Example SAFE App',
    vendor : 'MaidSafe.net Ltd'
};

const asyncFn = async () => {
    try {
        const app = await safe.initialiseApp(appInfo);
        const auth = app.auth;
    } catch (err) {
        throw err;
    }
};

cipherOpt

Get a CipherOptInterface interface

Source:
Example
const safe = require( '@maidsafe/safe-node-app' );

const appInfo = {
    id     : 'net.maidsafe.example',
    name   : 'Example SAFE App',
    vendor : 'MaidSafe.net Ltd'
};

const asyncFn = async () => {
    try {
        const app = await safe.initialiseApp(appInfo);
        const cipherOpt = app.cipherOpt;
    } catch (err) {
        throw err;
    }
};

connection

Returns pointer to current connection object held in memory.

Source:
Example
const safe = require( '@maidsafe/safe-node-app' );

const appInfo = {
    id     : 'net.maidsafe.example',
    name   : 'Example SAFE App',
    vendor : 'MaidSafe.net Ltd'
};

const asyncFn = async () => {
  try {
    const app = await safe.initialiseApp(appInfo);
    const connection = app.connection;
  } catch(err) {
    throw err;
  }
};

crypto

Get a CryptoInterface interface

Source:
Example
const safe = require( '@maidsafe/safe-node-app' );

const appInfo = {
    id     : 'net.maidsafe.example',
    name   : 'Example SAFE App',
    vendor : 'MaidSafe.net Ltd'
};

const asyncFn = async () => {
    try {
        const app = await safe.initialiseApp(appInfo);
        const crypto = app.crypto;
    } catch (err) {
        throw err;
    }
};

immutableData

Source:
Example
const safe = require( '@maidsafe/safe-node-app' );

const appInfo = {
    id     : 'net.maidsafe.example',
    name   : 'Example SAFE App',
    vendor : 'MaidSafe.net Ltd'
};

const asyncFn = async () => {
    try {
        const app = await safe.initialiseApp(appInfo);
        const immutableData = app.immutableData;
    } catch (err) {
        throw err;
    }
};

mutableData

Source:
Example
const safe = require( '@maidsafe/safe-node-app' );

const appInfo = {
    id     : 'net.maidsafe.example',
    name   : 'Example SAFE App',
    vendor : 'MaidSafe.net Ltd'
};

const asyncFn = async () => {
    try {
        const app = await safe.initialiseApp(appInfo);
        const mutableData = app.mutableData;
    } catch (err) {
        throw err;
    }
};

networkState

Textual representation of the current network connection state.

Source:
Example
const safe = require( '@maidsafe/safe-node-app' );

const appInfo = {
    id     : 'net.maidsafe.example',
    name   : 'Example SAFE App',
    vendor : 'MaidSafe.net Ltd'
};

const asyncFn = async () => {
  try {
    const app = await safe.initialiseApp(appInfo);
    const networkState = app.networkState;
  } catch(err) {
    throw err;
  }
};

web

Get a WebInterface interface

Source:
Example
const safe = require( '@maidsafe/safe-node-app' );

const appInfo = {
    id     : 'net.maidsafe.example',
    name   : 'Example SAFE App',
    vendor : 'MaidSafe.net Ltd'
};

const asyncFn = async () => {
    try {
        const app = await safe.initialiseApp(appInfo);
        const web = app.web;
    } catch (err) {
        throw err;
    }
};

Methods

appIsMock() → {Boolean}

Returns true if the underlyging library was compiled against mock routing.

Source:
Example
const safe = require( '@maidsafe/safe-node-app' );

const appInfo = {
    id     : 'net.maidsafe.example',
    name   : 'Example SAFE App',
    vendor : 'MaidSafe.net Ltd'
};

const asyncFn = async () => {
  try {
    const app = await safe.initialiseApp(appInfo);
    const isMock = await app.appIsMock();
  } catch(err) {
    throw err;
  }
};

clearObjectCache() → {Promise}

Resets the object cache kept by the underlying library.

Source:
Example
const safe = require( '@maidsafe/safe-node-app' );

const appInfo = {
    id     : 'net.maidsafe.example',
    name   : 'Example SAFE App',
    vendor : 'MaidSafe.net Ltd'
};

const asyncFn = async () => {
  try {
    const app = await safe.initialiseApp(appInfo);
    await app.clearObjectCache();
  } catch(err) {
    throw err;
  }
};

fetch(url) → {Promise.<NetworkResource>}

Experimental function to lookup a given safe://-URL in accordance with the public name resolution and find the requested network resource.

Parameters:
Name Type Description
url String

the url you want to fetch

Source:
Throws:
Example
const safe = require( '@maidsafe/safe-node-app' );

const appInfo = {
    id     : 'net.maidsafe.example',
    name   : 'Example SAFE App',
    vendor : 'MaidSafe.net Ltd'
};

const asyncFn = async () => {
  // If you have an XOR-URL with a type tag, and therefore represents MutableData,
  // use this operation to fetch an interface to the underlying data structure.
  try {
    const app = await safe.initialiseApp(appInfo);
    const unRegisteredUri = await app.auth.genConnUri();
    await app.auth.loginFromUri(unRegisteredUri);
    const data = await app.fetch(
        'safe://hyfktcerbwpctjz6ws8468hncw1ddpzrz65z3mapzx9wr413r7gj3w6yt5y:15001'
    );
  } catch(err) {
    throw err;
  }
};

getAccountInfo() → {Promise.<AccountInfo>}

Returns account information, specifically, number of mutations done and available.

Source:
Example
const safe = require( '@maidsafe/safe-node-app' );

const appInfo = {
    id     : 'net.maidsafe.example',
    name   : 'Example SAFE App',
    vendor : 'MaidSafe.net Ltd'
};

const asyncFn = async () => {
  try {
    const app = await safe.initialiseApp(appInfo);
    const getAccountInfo = await app.getAccountInfo();
  } catch(err) {
    throw err;
  }
};

getOwnContainerName() → {Promise.<String>}

Returns the name of the app's own container.

Source:
Example
const safe = require( '@maidsafe/safe-node-app' );

const appInfo = {
    id     : 'net.maidsafe.example',
    name   : 'Example SAFE App',
    vendor : 'MaidSafe.net Ltd'
};

const asyncFn = async () => {
  try {
    const app = await safe.initialiseApp(appInfo);
    const rootContainerName = await app.getOwnContainerName();
  } catch(err) {
    throw err;
  }
};

isNetStateConnected() → {Boolean}

Returns true if current network connection state is CONNECTED.

Source:
Example
const safe = require( '@maidsafe/safe-node-app' );

const appInfo = {
    id     : 'net.maidsafe.example',
    name   : 'Example SAFE App',
    vendor : 'MaidSafe.net Ltd'
};

const asyncFn = async () => {
  try {
    const app = await safe.initialiseApp(appInfo);
    const isNetStateConnected = app.isNetStateConnected();
  } catch(err) {
    throw err;
  }
};

isNetStateDisconnected() → {Boolean}

Returns true if current network connection state is DISCONNECTED.

Source:
Example
const safe = require( '@maidsafe/safe-node-app' );

const appInfo = {
    id     : 'net.maidsafe.example',
    name   : 'Example SAFE App',
    vendor : 'MaidSafe.net Ltd'
};

const asyncFn = async () => {
  try {
    const app = await safe.initialiseApp(appInfo);
    const isNetStateDisconnected = app.isNetStateDisconnected();
  } catch(err) {
    throw err;
  }
};

isNetStateInit() → {Boolean}

Returns true if current network connection state is INIT. This is state means the library has been initialised but there is no connection made with the network yet.

Source:
Example
const safe = require( '@maidsafe/safe-node-app' );

const appInfo = {
    id     : 'net.maidsafe.example',
    name   : 'Example SAFE App',
    vendor : 'MaidSafe.net Ltd'
};

const asyncFn = async () => {
  try {
    const app = await safe.initialiseApp(appInfo);
    const isNetStateInit = app.isNetStateInit();
  } catch(err) {
    throw err;
  }
};

logPath(logFilenameopt) → {Promise.<String>}

Generate the log path for the provided filename. If the filename provided is null, it then returns the path of where the safe_core log file is located.

Parameters:
Name Type Attributes Description
logFilename String <optional>

optional log filename to generate the path

Source:
Example
const safe = require( '@maidsafe/safe-node-app' );

const appInfo = {
    id     : 'net.maidsafe.example',
    name   : 'Example SAFE App',
    vendor : 'MaidSafe.net Ltd'
};

const asyncFn = async () => {
  try {
    const app = await safe.initialiseApp(appInfo);
    const logPath = await app.logPath();
  } catch(err) {
    throw err;
  }
};

reconnect() → {Promise}

Reconnect to the network. Must be invoked when the client decides to connect back after the connection was lost.

Source:
Example
const safe = require( '@maidsafe/safe-node-app' );

const appInfo = {
    id     : 'net.maidsafe.example',
    name   : 'Example SAFE App',
    vendor : 'MaidSafe.net Ltd'
};

const asyncFn = async () => {
  try {
    const app = await safe.initialiseApp(appInfo);
    await app.reconnect();
  } catch(err) {
    throw err;
  }
};

webFetch(url, optionsopt) → {Promise.<{body: Buffer, headers: Object}>}

Function to lookup a given safe://-URL in accordance with the public name resolution and find the requested network resource.

Parameters:
Name Type Attributes Default Description
url String

the url you want to fetch

options WebFetchOptions <optional>
null

additional options

Source:
Throws:
Example
const safe = require( '@maidsafe/safe-node-app' );

const appInfo = {
    id     : 'net.maidsafe.example',
    name   : 'Example SAFE App',
    vendor : 'MaidSafe.net Ltd'
};

const asyncFn = async () => {
  const app = await safe.initialiseApp(appInfo);
  const unRegisteredUri = await app.auth.genConnUri();
  await app.auth.loginFromUri(unRegisteredUri);
  const webFetchOptions = {
      range: {
          start:safe.CONSTANTS.NFS_FILE_START,
          end: safe.CONSTANTS.NFS_FILE_END
      }
  };
  try {
    const data = await app.webFetch(
      'safe://home.safenetwork',
      webFetchOptions
    );
    // Alternatively, fetch an ImmutableData XOR-URL such as:
    // safe://hygkdkftyhkmzma5cjwgcghws9hyorcucqyqna1uaje68hyquah7nd9kh3rjy
  } catch(err) {
    throw err;
  }
};