Documentation / /
Guides

Write to a dat:// website

The DatArchive API provides a simple set of APIs to write files, create directories, and delete data.

To create a writable dat:// website, first create a new DatArchive instance:

var archive = await DatArchive.create({title: 'My website'})

Writing files and folders

mkdir()

The mkdir() method creates new folders. It will throw an error if a file/folder already exists at the target, or if no parent folder exists.

await archive.mkdir('/stuff')

writeFile()

The writeFile() method writes files. It accepts an encoding as its third parameter.

await archive.writeFile('/hello.txt', str, 'utf8')
await archive.writeFile('/beaker.png', pngBase64, 'base64')
await archive.writeFile('/beaker.png', pngArrayBuffer, 'binary')

If the encoding is omitted, then ‘utf8’ will be assumed for strings and ‘binary’ will be assumed for ArrayBuffers:

await archive.writeFile('/hello.txt', str)
await archive.writeFile('/beaker.png', pngArrayBuffer)

Deleting files and folders

rmdir()

The rmdir() method deletes folders. It will fail if the folder is not empty.

await archive.rmdir('/stuff')

The unlink() method deletes files.

await archive.unlink('/hello.txt')

Recursive deletes

You can delete non-empty folders by specifying the recursive option.

// delete /stuff and all of its contents
await archive.rmdir('/stuff', {recursive: true})

Copying, moving, and renaming

copy()

The copy() method copies files and folders. If the folder is not empty, it will copy all of its contents.

await archive.copy('/stuff', '/stuff-backup')
await archive.copy('/hello.txt', '/goodbye.txt')

rename()

The rename() method moves/renames files and folders. If the folder is not empty, it will move all of its contents.

await archive.rename('/stuff', '/stuff-backup')
await archive.rename('/hello.txt', '/goodbye.txt')

Further reading