dPay

dPack Vault

A NodeJS API for dPack which is compatible with dPack's API. Useful for testing and for writing apps that work in the browser and in NodeJS.

var DPackVault = require('@dpack/vault')

// create a new vault
var vault = await DPackVault.create({
  localPath: './my-dpack-vault',
  title: 'My dPack Vault',
  description: 'A test of the node DPack Vault API'
})

// load an existing vault from disk
var vault = await DPackVault.load({
  localPath: './my-dpack-vault'
})

// load an existing vault from the URL:
var vault = new DPackVault(dPackURL, {localPath: './my-dpack-vault'})

// using the instance
await vault.writeFile('hello.txt', 'world')
var names = await vault.readdir('/')
console.log(names) // => ['hello.txt']

By default, @dpack/vault stores the dPack data in the localPath folder using dPack's syncing and historical log format (dPack's internal structure).
If you want the folder to show the latest files (the dPack CLI behavior) pass latest: true in the dPackOptions.

var vault = await DPackVault.create({
  localPath: './my-dpack-vault',
  dPackOptions: {latest: true}
})
var vault = await DPackVault.load({
  localPath: './my-dpack-vault',
  dPackOptions: {latest: true}
})
var vault = new DPackVault(dPackURL, {
  localPath: './my-dpack-vault',
  dPackOptions: {latest: true}
})

You can also pass options through to @dpack/core with dPackOptions, or pass options to its .joinNetwork([opts]) method with netOptions:

var vault = new DPackVault(dPackURL, {
  localPath: './my-dpack-vault',
  dPackOptions: {
    live: true
  },
  netOptions: {
    upload: false
  }
})

This will extend @dpack/vault's defaults.

Differences from Browser API

  • This module adds the localPath parameter. Use the localPath to specify where the data for the vault should be stored. If not provided, the vault will be stored in memory.
  • This module also adds dPackOptions and netOptions to configure the @dpack/core usage.
  • This module also adds DPackVault.load() to read an vault from disk.
  • This module does yet not include DPackVault.fork.
  • This module does yet not include DPackVault.unlink.
  • This module will not include DPackVault.selectVault.
  • vault.getInfo() does not give a valid mtime or size.
  • networked: opt is not yet supported.

Quick API reference

Refer to the dPack API.

var vault = new DPackVault(url, {localPath:, dPackOptions:, netOptions:})
var vault = await DPackVault.create({localPath:, dPackOptions:, netOptions:, title:, description:, type:, author:, networked:})
var vault = await DPackVault.load({localPath:, dPackOptions:, netOptions:})
var key = await DPackVault.resolveName(url)
vault.url
await vault.configure({title:, description:, type:, author:, networked:})
var info = await vault.getInfo({timeout:})
var stat = await vault.stat(path, {timeout:})
var content = await vault.readFile(path, {encoding:, timeout:})
var names = vault.readdir(path, {recursive:, stat:, timeout:})
await vault.writeFile(path, data, encoding)
await vault.mkdir(path)
await vault.unlink(path)
await vault.rmdir(path, {recursive:})
var history = await vault.history({start:, end:, reverse:, timeout:})
await vault.download(path, {timeout:})
var emitter = vault.createFileActivityStream(pattern)
var emitter = vault.createNetworkActivityStream()

// node-only:
vault._loadPromise // promise for when the vault is ready to use
vault._close() // exit flock, close all files