Skip to content

Utility functions for Beaker Browser's DatArchive API

License

Notifications You must be signed in to change notification settings

krismuniz/dat-utils

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Dat Utils

Dat License:MIT

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).

Quick Example:
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.

Requirements

Installation

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.

Usage

To use dat-utils, you need to have Beaker Browser v0.8 installed since this is an ES6 module.

Importing the 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 of type="text/javascript" attribute to your <script> tag so you can import ES6 modules!

copyFile

copyFile(source, target, path[, options])

Deep-copy files from one Dat Archive to another using .copyFile().

Example
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

writeOrModifyFile(archive, path, data[, options])

Write a file, or modify it if it already exists.

Example
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

fileExists(archive, path)

Returns true if the file exists, or false if it doesn't

Example
import { fileExists } from '/modules/dat-utils.js'

const archive = new DatArchive(<dat_url>)

await fileExists(archive, '/data.txt')
// -> checks if /data.txt exists

deepWriteFile

deepWriteFile(archive, path, data[, options])

Deep-writes a file to an archive, creates parent directories as needed.

Example
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

deepMkdir(archive, path)

Recursively creates a directory on the path specified, creates parent directories as needed.

Example
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

Reference

API Reference

License

MIT

About

Utility functions for Beaker Browser's DatArchive API

dat://utils-krismuniz.hashbase.io

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published