Vaultrice SDK - v0.9.11
    Preparing search index...

    Class NonLocalStorage

    Provides a persistent, optionally encrypted, remote key-value store. Supports atomic operations, TTL, and presence via websockets.

    Use this class to interact with the Vaultrice API for storing and retrieving data.

    Hierarchy

    • default
      • NonLocalStorage
    Index

    Constructors

    • Create a NonLocalStorage instance.

      Parameters

      • credentials: { apiKey?: string; apiSecret?: string; accessToken?: string; projectId: string }

        API credentials.

      • Optionalid: string

        Optional ID.

      Returns NonLocalStorage

    • Create a NonLocalStorage instance.

      Parameters

      • credentials: { apiKey?: string; apiSecret?: string; accessToken?: string; projectId: string }

        API credentials.

      • Optionaloptions: InstanceOptions

        Optional instance options.

      Returns NonLocalStorage

    Properties

    id: string

    Unique instance identifier

    isConnected: boolean = false

    Indicates if the WebSocket connection is currently established. True if connected, false otherwise.

    Methods

    • Retrieves an access token for a given project using API credentials.

      Parameters

      • projectId: string

        The unique identifier of the project.

      • apiKey: string

        The API key associated with the project.

      • apiSecret: string

        The API secret associated with the project.

      Returns Promise<string>

      A promise that resolves to the access token as a string.

      const token = await NonLocalStorage.retrieveAccessToken('projectId', 'apiKey', 'apiSecret');
      
    • Sets the access token to be used for authentication.

      Parameters

      • accessToken: string

        The access token string to set.

      Returns number

      token expiration in milliseconds from now

    • Registers a handler function to be called when the access token is about to expire.

      Parameters

      • handler: () => void

        A callback function that will be invoked before the access token expires.

      Returns void

    • Removes a previously registered handler for the access token expiring event.

      Parameters

      • handler: () => void

        The callback function to remove from the access token expiring handlers list.

      Returns void

    • Retrieve or initialize encryption settings for end-to-end encryption.

      Parameters

      • OptionalsaltLength: number

        Optional salt length in bytes (default: 16).

      Returns Promise<EncryptionSettingsInfos>

      Promise resolving to encryption settings information.

      Error if called without encryption configuration.

      This method is mandatory when using end-to-end encryption. It fetches the encryption salt and key version from the server, then initializes the encryption handler.

    • Rotate encryption keys to enhance security.

      Parameters

      • OptionalsaltLength: number

        Optional salt length in bytes (default: 16).

      Returns Promise<EncryptionSettingsInfos>

      Promise resolving to new encryption settings information.

      Error if called without encryption configuration.

      This generates new encryption settings while preserving access to data encrypted with previous keys. Useful for periodic security rotation.

    • Store a value by key.

      Parameters

      • name: string

        The key name.

      • value: ValueType

        The value to store.

      • Optionaloptions: { ttl?: number; ifAbsent?: boolean }

        Optional overrides.

        • Optionalttl?: number

          Time-to-live in milliseconds for this item.

        • OptionalifAbsent?: boolean

          If true only set if item absent.

      Returns Promise<undefined | SetReturnType>

      Metadata about the stored item.

      3600000 (1 hour)
      
      false
      
    • Store multiple values at once.

      Parameters

      • items: Record<string, { value: ValueType; ttl?: number; ifAbsent?: boolean }>

        Object of key/value pairs. Example: { key1: { value: 'foo' }, key2: { value: 'bar', ttl: 3600000 }, key2: { value: 'zip', ifAbsent: true } } ttl is time-to-live in milliseconds for each item.

      Returns Promise<undefined | SetItemsType>

      Metadata for each stored item.

      3600000 (1 hour) ifAbsent if true only set if item absent - for each item.

      false
      
    • Retrieve multiple values by key.

      Parameters

      • names: string[]

        Array of key names.

      Returns Promise<undefined | ItemsType>

      Object of items found.

    • Retrieve all items, optionally filtered by prefix.

      Parameters

      • Optionaloptions: { prefix?: string }

        Optional prefix filter.

      Returns Promise<undefined | ItemsType>

      Object of all items.

    • Get all keys, optionally filtered by prefix.

      Parameters

      • Optionaloptions: { prefix?: string }

        Optional prefix filter.

      Returns Promise<undefined | string[]>

      Array of key names.

    • Remove a value by key.

      Parameters

      • name: string

        The key name.

      Returns Promise<undefined>

    • Remove multiple values by key.

      Parameters

      • names: string[]

        Array of key names.

      Returns Promise<undefined>

    • Atomically increment a numeric value.

      Parameters

      • name: string

        The key name.

      • value: number = 1

        Amount to increment by (default 1).

      • Optionaloptions: { ttl?: number }

        Optional TTL override.

      Returns Promise<ItemType>

      The updated item.

    • Atomically decrement a numeric value.

      Parameters

      • name: string

        The key name.

      • value: number = 1

        Amount to decrement by (default 1).

      • Optionaloptions: { ttl?: number }

        Optional TTL override.

      Returns Promise<ItemType>

      The updated item.

    • Remove all items for this instance.

      Returns Promise<undefined>

    • Send a message to the server or other clients.

      Parameters

      • msg: JSONObj

        The message object to send.

      • options: { transport?: "ws" | "http" } = ...

        Transport options (WebSocket or HTTP).

      Returns Promise<undefined>

      Error if encryption is configured but getEncryptionSettings() not called.

      Messages are automatically encrypted if encryption is configured. WebSocket transport is preferred for real-time delivery, but HTTP can be used as fallback.

      await instance.send({ type: 'chat', message: 'Hello everyone!' });
      // Send via HTTP instead of WebSocket
      await instance.send({ data: 'important' }, { transport: 'http' });
    • Register an event handler for WebSocket connection events.

      Parameters

      • event: "connect"

        The 'connect' event name.

      • handler: () => void

        Function to call when connected.

      Returns any

    • Register an event handler for WebSocket disconnection events.

      Parameters

      • event: "disconnect"

        The 'disconnect' event name.

      • handler: () => void

        Function to call when disconnected.

      Returns any

    • Register an event handler for presence join events.

      Parameters

      • event: "presence:join"

        The 'presence:join' event name.

      • handler: (joinedConnection: JoinedConnection) => void

        Function to call when a connection joins.

      Returns any

    • Register an event handler for presence leave events.

      Parameters

      • event: "presence:leave"

        The 'presence:leave' event name.

      • handler: (leavedConnection: LeavedConnection) => void

        Function to call when a connection leaves.

      Returns any

    • Register an event handler for incoming messages.

      Parameters

      • event: "message"

        The 'message' event name.

      • handler: (data: JSONObj) => void

        Function to call when a message is received.

      Returns any

    • Register an event handler for WebSocket errors.

      Parameters

      • event: "error"

        The 'error' event name.

      • handler: (error: Error) => void

        Function to call when an error occurs.

      Returns any

    • Register an event handler for all setItem events.

      Parameters

      • event: "setItem"

        The 'setItem' event name.

      • handler: (item: { value: ValueType } & SetReturnType & { prop: string }) => void

        Function to call when any item is set.

      Returns any

    • Register an event handler for specific setItem events.

      Parameters

      • event: "setItem"

        The 'setItem' event name.

      • name: string

        The specific item name to listen for.

      • handler: (item: { value: ValueType } & SetReturnType & { prop: string }) => void

        Function to call when the named item is set.

      Returns any

    • Register an event handler for all removeItem events.

      Parameters

      • event: "removeItem"

        The 'removeItem' event name.

      • handler: (item: { prop: string }) => void

        Function to call when any item is removed.

      Returns any

    • Register an event handler for specific removeItem events.

      Parameters

      • event: "removeItem"

        The 'removeItem' event name.

      • name: string

        The specific item name to listen for.

      • handler: (item: { prop: string }) => void

        Function to call when the named item is removed.

      Returns any

    • Remove an event handler for WebSocket connection events.

      Parameters

      • event: "connect"

        The 'connect' event name.

      • handler: () => void

        Function to remove.

      Returns any

    • Remove an event handler for WebSocket disconnection events.

      Parameters

      • event: "disconnect"

        The 'disconnect' event name.

      • handler: () => void

        Function to remove.

      Returns any

    • Remove an event handler for presence join events.

      Parameters

      • event: "presence:join"

        The 'presence:join' event name.

      • handler: (joinedConnection: JoinedConnection) => void

        Function to remove.

      Returns any

    • Remove an event handler for presence leave events.

      Parameters

      • event: "presence:leave"

        The 'presence:leave' event name.

      • handler: (leavedConnection: LeavedConnection) => void

        Function to remove.

      Returns any

    • Remove an event handler for incoming messages.

      Parameters

      • event: "message"

        The 'message' event name.

      • handler: (data: JSONObj) => void

        Function to remove.

      Returns any

    • Remove an event handler for WebSocket errors.

      Parameters

      • event: "error"

        The 'error' event name.

      • handler: (error: Error) => void

        Function to remove.

      Returns any

    • Remove an event handler for all setItem events.

      Parameters

      • event: "setItem"

        The 'setItem' event name.

      • handler: (item: { value: ValueType } & SetReturnType & { prop: string }) => void

        Function to remove.

      Returns any

    • Remove an event handler for specific setItem events.

      Parameters

      • event: "setItem"

        The 'setItem' event name.

      • name: string

        The specific item name.

      • handler: (item: { value: ValueType } & SetReturnType & { prop: string }) => void

        Function to remove.

      Returns any

    • Remove an event handler for all removeItem events.

      Parameters

      • event: "removeItem"

        The 'removeItem' event name.

      • handler: (item: { prop: string }) => void

        Function to remove.

      Returns any

    • Remove an event handler for specific removeItem events.

      Parameters

      • event: "removeItem"

        The 'removeItem' event name.

      • name: string

        The specific item name.

      • handler: (item: { prop: string }) => void

        Function to remove.

      Returns any

    • Opens the WebSocket connection.

      Returns Promise<void>

      Does usually not need to be used, since the WebSocke connection is automatically established on WS feature usage.

    • Close the WebSocket connection and clean up resources.

      Returns Promise<void>

      If the instance has joined the presence channel, it will automatically leave before disconnecting. This ensures proper cleanup and notifies other clients of the departure. All event handlers are also cleaned up.

    • Join the presence channel to announce this connection to others.

      Parameters

      • data: JSONObj

        Optional data to associate with this connection.

      Returns Promise<undefined>

      Error if encryption is configured but getEncryptionSettings() not called.

      After joining, this connection will:

      • Appear in getJoinedConnections() results for other clients
      • Trigger 'presence:join' events for other connected clients
      • Automatically send 'presence:leave' when disconnecting
      await instance.join({
      username: 'Alice',
      status: 'online',
      avatar: 'avatar1.png'
      });
    • Leave the presence channel.

      Returns Promise<undefined>

      Notifies other connected clients that this connection has left. This is automatically called when disconnecting if the connection had previously joined.

    • Get a list of all currently connected clients in the presence channel.

      Returns Promise<JoinedConnections>

      Promise resolving to an array of connected clients with their data.

      Error if encryption is configured but getEncryptionSettings() not called.

      Each connection object includes:

      • connectionId: Unique identifier for the connection
      • joinedAt: Timestamp when the connection joined
      • data: Custom data provided when joining (automatically decrypted)
      const connections = await instance.getJoinedConnections();
      console.log(`${connections.length} users online`);
      connections.forEach(conn => {
      console.log(`User: ${conn.data.username}, joined: ${new Date(conn.joinedAt)}`);
      });