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

Commit 5db3f06

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 5db3f06

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ module.exports = createDat
1818
* @param {Boolean} [opts.createIfMissing = true] - Create storage if it does not exit.
1919
* @param {Boolean} [opts.errorIfExists = false] - Error if storage exists.
2020
* @param {Boolean} [opts.temp = false] - Use random-access-memory for temporary storage
21+
* @param {string} [opts.secretDir] - Directory for storage of secrets
2122
* @param {function(err, dat)} cb - callback that returns `Dat` instance
2223
* @see defaultStorage for storage information
2324
*/

lib/storage.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@ module.exports = defaultStorage
1616
* Object is a hyperdrive storage object `{metadata: fn, content: fn}`.
1717
* @param {object} [opts] - options
1818
* @param {Boolean} [opts.temp] - Use temporary storage (random-access-memory)
19+
* @param {string} [opts.secretDir] - Directory for storage of secrets.
1920
* @returns {object} hyperdrive storage object
2021
*/
2122
function defaultStorage (storage, opts) {
2223
// Use custom storage or ram
2324
if (typeof storage !== 'string') return storage
2425
if (opts.temp) return ram
2526
if (opts.latest === false) {
26-
// Store as SLEEP files inluding content.data
27+
// Store as SLEEP files including content.data
2728
return {
2829
metadata: function (name, opts) {
2930
// I don't think we want this, we may get multiple 'ogd' sources
@@ -39,17 +40,23 @@ function defaultStorage (storage, opts) {
3940
try {
4041
// Store in .dat with secret in ~/.dat
4142
if (fs.statSync(storage).isDirectory()) {
42-
return datStore(storage)
43+
if (typeof opts.secretDir !== 'undefined') {
44+
fs.statSync(opts.secretDir).isDirectory()
45+
}
46+
return datStore(storage, opts)
4347
}
4448
} catch (e) {
4549
// Does not exist, make dir
4650
try {
4751
fs.mkdirSync(storage)
52+
if (typeof opts.secretDir !== 'undefined') {
53+
fs.mkdirSync(opts.secretDir)
54+
}
4855
} catch (e) {
4956
// Invalid path
5057
throw new Error('Invalid storage path')
5158
}
52-
return datStore(storage)
59+
return datStore(storage, opts)
5360
}
5461
error()
5562

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 secretDir in the second opts argument in the form of {secretDir: '/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('/my-dir', {secretDir: '/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)