Beaker

Snippets

A collection of handy snippets for building p2p websites and apps in Beaker

Copied to clipboard
Get a website's title
var archive = new DatArchive(url)
var info = await archive.getInfo()
info.title // string
Get a website's type
var archive = new DatArchive(url)
var info = await archive.getInfo()
info.type // array of strings
Get a website's total size
var archive = new DatArchive(url)
var info = await archive.getInfo()
info.size // number (bytes)
Check if the user can edit a website
var archive = new DatArchive(url)
var info = await archive.getInfo()
info.isOwner // boolean
Get the last published version number of a website
var archive = new DatArchive(url)
var info = await archive.getInfo()
info.version // number
Check when an update was last downloaded
var archive = new DatArchive(url)
var info = await archive.getInfo()
info.mtime // number
Get the number of active peers
var archive = new DatArchive(url)
var info = await archive.getInfo()
info.peers // number
Configure a website's settings
var archive = new DatArchive(url)
await archive.configure({
  title: 'The name of the archive',
  description: 'A longer description of the archive',
  type: ['website']
})
Create a new website
await DatArchive.create({
  title: 'The name of the archive',
  description: 'A longer description of the archive',
  type: ['website']
})
Create a copy of a website
await DatArchive.fork(sourceUrl, {
  title: 'New name',
  description: 'New description'
})
Open Beaker's website selector modal
await DatArchive.selectArchive({
  prompt: 'Select an archive',
  buttonLabel: 'Accept'
})
Use a type filter in the select modal
await DatArchive.selectArchive({
  prompt: 'Select a fritter user profile',
  filters: {isOwner: true, type: ['user-profile', 'fritter-user-profile']}
})
Lookup the key of a DNS name
await DatArchive.resolveName('beakerbrowser.com')
Check if something is a file or a folder
var st = archive.stat('/foo')
st.isFile() // boolean
st.isDirectory() // boolean
Read a folder
await archive.readdir('/')
await archive.readdir('/', {recursive: true})
await archive.readdir('/', {stat: true})
Read a file
await archive.readFile('/index.html', 'utf8')
await archive.readFile('/img/logo/logo.png', 'binary')
Get the download progress of a file
var st = await archive.stat('/index.html')
var percent = st.downloaded / st.blocks
var bytes = st.size * percent
Get the download progress of a folder
var sts = await archive.readdir('/', {recursive: true, stat: true})
var blocks = 0, downloaded = 0, bytes = 0
for (var st of sts) {
  bytes += st.stat.bytes
  blocks += st.stat.blocks
  downloaded += st.stat.downloaded
}
var percentDownloaded = downloaded / blocks
var bytesDownloaded = size * percentDownloaded
Write files and folders
await archive.mkdir('/stuff')
await archive.writeFile('/hello.txt', str, 'utf8')
await archive.writeFile('/beaker.png', pngBase64, 'base64')
Delete files and folders
await archive.rmdir('/stuff')
await archive.unlink('/hello.txt')
await archive.rmdir('/stuff', {recursive: true})
Copy files and folders
await archive.copy('/stuff', '/stuff-backup')
await archive.copy('/hello.txt', '/goodbye.txt')
Move/rename files and folders
await archive.rename('/stuff', '/stuff-backup')
await archive.rename('/hello.txt', '/subfolder/hello.txt')
Watch all files
var archive = new DatArchive('dat://beakerbrowser.com')
archive.watch(e => console.log(e.path, 'has changed'))
Watch specific files or folders
var archive = new DatArchive('dat://beakerbrowser.com')
archive.watch('/index.html', e => console.log('index.html has changed'))
archive.watch('/js/', e => console.log('a file in js/ has changed'))
Watch using a glob pattern
var archive = new DatArchive('dat://beakerbrowser.com')
archive.watch('/posts/*.json', e => {
  console.log('a .json file in /posts/ has changed')
})
Get the change-history of a website
var archive = new DatArchive('dat://beakerbrowser.com')
var history = await archive.history()
Get an older version of a website
var archive = new DatArchive('dat://beakerbrowser.com')
var olderArchive = await archive.checkout(50)
Download all files in a website
var archive = new DatArchive('dat://beakerbrowser.com')
await archive.download('/')
List the peers on the site
var peers = await experimental.datPeers.list()
peers[..].id // string
peers[..].sessionData // object
Watch for connection events
experimental.datPeers.addEventListener('connect', ({peer}) => {
  console.log(peer.id, 'has connected')
})
experimental.datPeers.addEventListener('disconnect', ({peer}) => {
  console.log(peer.id, 'has disconnected')
})
Listen to messages from peers
experimental.datPeers.addEventListener('message', ({peer, message}) => {
  console.log(peer.id, 'has sent the following message:', message)
})
Reply to a peer's message
experimental.datPeers.addEventListener('message', ({peer, message}) => {
  peer.send({my: 'reply', message: 'is this'})
})
Broadcast a message to all peers
experimental.datPeers.broadcast({my: 'message'})
Set session data for all peers to read
experimental.datPeers.setSessionData({
  name: 'Bob',
  url: 'dat://1234..56'
})
Listen for peers to set their session data
experimental.datPeers.addEventListener('session-data', ({peer}) => {
  console.log(peer.id, 'has set their session data to', peer.sessionData)
})
List all of the user's saved dat archives
var archiveInfos = await experimental.library.list()
archiveInfos[..].url // string
archiveInfos[..].title // string
archiveInfos[..].isOwner // boolean
Save or unsave a dat archive
await experimental.library.add(datUrl)
await experimental.library.remove(datUrl)
Prompt to save or unsave an archive
await experimental.library.requestAdd(datUrl)
await experimental.library.requestRemove(datUrl)
Make a cross-origin Ajax request
var res = await experimental.globalFetch('https://example.com')
var body = await res.text()
Take a screenshot of a page
var png = await experimental.capturePage('dat://beakerbrowser.com', {
  width: 1024,
  height: 768
})
Take a thumbnail screenshot of a page
var png = await experimental.capturePage('dat://beakerbrowser.com', {
  width: 1024,
  height: 768,
  resizeTo: {width: 200, height: 150}
})