Skip to content
This repository was archived by the owner on Jan 7, 2022. It is now read-only.

Commit 95394d7

Browse files
committed
Add software that allows for specification of both the dat directory as
well as the directory for the dat secrets.
1 parent 85c1089 commit 95394d7

3 files changed

Lines changed: 22 additions & 6 deletions

File tree

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ function createDat (dirOrStorage, opts, cb) {
3232

3333
var archive
3434
var key = opts.key
35-
var dir = (typeof dirOrStorage === 'string') ? dirOrStorage : null
35+
var dir = (typeof dirOrStorage === 'string') ? dirOrStorage :
36+
(typeof dirOrStorage === 'object') ? dirOrStorage.dir : null
3637
var storage = datStore(dirOrStorage, opts)
3738
var createIfMissing = !(opts.createIfMissing === false)
3839
var errorIfExists = opts.errorIfExists || false

lib/storage.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ module.exports = defaultStorage
2020
*/
2121
function defaultStorage (storage, opts) {
2222
// Use custom storage or ram
23-
if (typeof storage !== 'string') return storage
23+
if (typeof storage !== 'string' &&
24+
typeof storage !== 'object') return storage
2425
if (opts.temp) return ram
2526
if (opts.latest === false) {
2627
// Store as SLEEP files inluding content.data
@@ -38,13 +39,20 @@ function defaultStorage (storage, opts) {
3839

3940
try {
4041
// Store in .dat with secret in ~/.dat
41-
if (fs.statSync(storage).isDirectory()) {
42+
if ((typeof storage === 'object' &&
43+
fs.statSync(storage.dir).isDirectory()) ||
44+
fs.statSync(storage).isDirectory()) {
4245
return datStore(storage)
4346
}
4447
} catch (e) {
4548
// Does not exist, make dir
4649
try {
47-
fs.mkdirSync(storage)
50+
if (typeof storage === 'object') {
51+
fs.mkdirSync(storage.dir)
52+
fs.mkdirSync(storage.secret_dir)
53+
} else {
54+
fs.mkdirSync(storage)
55+
}
4856
} catch (e) {
4957
// Invalid path
5058
throw new Error('Invalid storage path')

readme.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,24 +102,31 @@ We'll go through what these are for and a few of the common usages of each eleme
102102

103103
### Storage
104104

105-
Every dat archive has **storage**, this is the required first argument for dat-node. By default, we use [dat-storage](http://github.com/datproject/dat-storage) which stores the secret key in `~/.dat/` and the rest of the ddata in `dir/.dat`. Other common options are:
105+
Every dat archive has **storage**, this is the required first argument for dat-node. By default, we use [dat-storage](http://github.com/datproject/dat-storage) which stores the secret key in `~/.dat/` and the rest of the data in `dir/.dat`. Other common options are:
106106

107107
* **Persistent storage**: Stored files in `/my-dir` and metadata in `my-dir/.dat` by passing `/my-dir` as the first argument.
108108
* **Temporary Storage**: Use the `temp: true` option to keep metadata stored in memory.
109+
* **Secret Key Dir**: Specify first argument as object in the form of {dir: '/my-dir', secret_dir: '/my-secrets'} to store files
110+
in /my-dir and secrets in /my-secrets respectively.
109111

110112
```js
111113
// Permanent Storage
112114
Dat('/my-dir', function (err, dat) {
113115
// Do Dat Stuff
114116
})
115117

118+
// Secret Storage
119+
Dat({dir: '/my-dir', secret_dir: '/my-secrets'}, function (err, dat) {
120+
// Do Dat Stuff
121+
})
122+
116123
// Temporary Storage
117124
Dat('/my-dir', {temp: true}, function (err, dat) {
118125
// Do Dat Stuff
119126
})
120127
```
121128

122-
Both of these will import files from `/my-dir` when doing `dat.importFiles()` but only the first will make a `.dat` folder and keep the metadata on disk.
129+
All of these will import files from `/my-dir` when doing `dat.importFiles()` but only the first two will make a `.dat` folder and keep the metadata on disk.
123130

124131
The storage argument can also be passed through to hyperdrive for more advanced storage use cases.
125132

0 commit comments

Comments
 (0)