A small set of utilities to ease the process of building decentralized apps on top of the DatArchive API in the form of an ES6 module.
With this module you can copy files from one archive to another (copyFile
), deep-write files (deepWriteFile
), check if a file exists (fileExists
), and create dirs without worrying about about the directory-tree (deepMkdir
).
import { copyFile } from '/modules/dat-utils.js'
const archiveA = await DatArchive.select() // or new DatArchive(<dat_url>)
const archiveB = await DatArchive.select() // or new DatArchive(<dat_url>)
copyFile(archiveA, archiveB, '/deep/path/to/index.html')
The code above deep-copies /deep/path/to/index.html
from archiveA
to archiveB
. No need to create /deep
, /deep/path
, or /deep/path/to/
directories, it creates parent directories as needed.
- Beaker Browser v0.8 or greater
- ES6 Modules support
DatArchive
API support
- Some knowledge of
DatArchive
API (it's really easy to learn!)
If you are using Beaker Browser, go to dat://utils-krismuniz.hashbase.io and click the "Install Module" button. You will be prompted to select your Dat site/app and when you select it, the installer will automatically add the module to your Dat site's source code!
Alternatively, you can download the source code straight from the GitHub repo.
To use dat-utils
, you need to have Beaker Browser v0.8 installed since this is an ES6 module.
To import dat-utils
into your JavaScript code, use the ES6 import
syntax.
import * as utils from '/modules/dat-utils.js'
utils.copyFile(archiveA, archiveB, '/index.html')
Remember to add the
type="module"
instead oftype="text/javascript"
attribute to your<script>
tag so you can import ES6 modules!
copyFile(source, target, path[, options])
Deep-copy files from one Dat Archive to another using .copyFile()
.
import { copyFile } from '/modules/dat-utils.js'
const from = await DatArchive.select() // or new DatArchive(<dat_url>)
const to = await DatArchive.select() // or new DatArchive(<dat_url>)
await copyFile(from, to, '/index.html')
// writes contents of '/index.html' from one archive to the other, regardless of directory-tree
writeOrModifyFile(archive, path, data[, options])
Write a file, or modify it if it already exists.
import { writeOrModifyFile } from '/modules/dat-utils.js'
const archive = new DatArchive(<dat_url>)
await writeOrModifyFile(archive, '/data.txt', 'hello world')
// -> deep-writes to /data.txt, or modifies if it already exists
fileExists(archive, path)
Returns true
if the file exists, or false
if it doesn't
import { fileExists } from '/modules/dat-utils.js'
const archive = new DatArchive(<dat_url>)
await fileExists(archive, '/data.txt')
// -> checks if /data.txt exists
deepWriteFile(archive, path, data[, options])
Deep-writes a file to an archive, creates parent directories as needed.
import { deepWriteFile } from '/modules/dat-utils.js'
const archive = new DatArchive(<dat_url>)
await deepWriteFile(archive, '/path/to/data.txt', 'hello world!')
// -> writes 'hello world' to /path/to/data.txt
deepMkdir(archive, path)
Recursively creates a directory on the path specified, creates parent directories as needed.
import { deepMkdir } from '/modules/dat-utils.js'
const archive = new DatArchive(<dat_url>)
await deepMkdir(archive, '/path/to/other/dir')
// -> creates /path, /path/to, /path/to/other, and /path/to/other/dir