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

Commit 3592dc9

Browse files
committed
dat-json
0 parents  commit 3592dc9

7 files changed

Lines changed: 226 additions & 0 deletions

File tree

.gitignore

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Logs
2+
logs
3+
*.log
4+
5+
# Runtime data
6+
pids
7+
*.pid
8+
*.seed
9+
10+
# Directory for instrumented libs generated by jscoverage/JSCover
11+
lib-cov
12+
13+
# Coverage directory used by tools like istanbul
14+
coverage
15+
16+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
17+
.grunt
18+
19+
# node-waf configuration
20+
.lock-wscript
21+
22+
# Compiled binary addons (http://nodejs.org/api/addons.html)
23+
build/Release
24+
25+
# Dependency directory
26+
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
27+
node_modules

.travis.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
language: node_js
2+
node_js:
3+
- '4'
4+
- '6'
5+
cache:
6+
directories:
7+
- node_modules

LICENSE.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# [MIT License](https://spdx.org/licenses/MIT)
2+
3+
Copyright (c) 2017 Joe Hand <joe@hand.email>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6+
7+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# dat-json
2+
3+
read &amp; write dat.json files. Uses toiletdb under the hood.
4+
5+
[![npm][npm-image]][npm-url]
6+
[![travis][travis-image]][travis-url]
7+
[![standard][standard-image]][standard-url]
8+
9+
## Install
10+
11+
```
12+
npm install dat-json
13+
```
14+
15+
## Usage
16+
17+
```js
18+
var DatJSON = require('dat-json')
19+
20+
var datjson = DatJSON(archive)
21+
22+
datjson.create({title: 'a dat', description: 'exciting'}, function (err) {
23+
if (err) throw err
24+
})
25+
```
26+
27+
## API
28+
29+
### `var datjson = DatJSON(archive)`
30+
31+
#### `datjson.create([data], cb)`
32+
33+
Create a new `dat.json` file in the archive with the default keys (`name`, `url`, `title`, `description`). Pass in any additional data to add on initial create.
34+
35+
#### `datjson.write(key, val, cb)` or `datjson.write(data, cb)`
36+
37+
Write a single `key` and `value` or an object, `data`, to the `dat.json` file.
38+
39+
#### `datjson.delete(key, cb)`
40+
41+
Delete a `key` from the `dat.json` file.
42+
43+
#### `datjson.read(cb)`
44+
45+
Read the current `dat.json`.
46+
47+
## License
48+
49+
[MIT](LICENSE.md)
50+
51+
[npm-image]: https://img.shields.io/npm/v/dat-json.svg?style=flat-square
52+
[npm-url]: https://www.npmjs.com/package/dat-json
53+
[travis-image]: https://img.shields.io/travis/joehand/dat-json.svg?style=flat-square
54+
[travis-url]: https://travis-ci.org/joehand/dat-json
55+
[standard-image]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square
56+
[standard-url]: http://npm.im/standard

index.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
var stringKey = require('dat-encoding').toStr
2+
// var path = require('path')
3+
var xtend = require('xtend')
4+
var toiletdb = require('toiletdb')
5+
6+
module.exports = function (archive) {
7+
var db = toiletdb({name: '/dat.json', fs: archive})
8+
var that = {
9+
read: function (cb) {
10+
archive.stat('/dat.json', function (err, stat) {
11+
if (err) return cb(err)
12+
db.read(cb)
13+
})
14+
},
15+
write: function (key, val, cb) {
16+
if (!archive.writable) return cb(new Error('Archive not writable'))
17+
if (typeof key === 'object') return writeAll(key, val) // assume val = cb
18+
// TODO: validate things
19+
db.write(key, val, cb)
20+
},
21+
delete: db.delete,
22+
create: function (data, cb) {
23+
if (typeof data === 'function') return that.create(null, data)
24+
if (!archive.writable) return cb(new Error('Archive not writable'))
25+
var defaults = xtend({
26+
title: '', // TODO path.basename(dir),
27+
name: '',
28+
description: ''
29+
}, data, {
30+
url: 'dat://' + stringKey(archive.key) // force the key as default
31+
})
32+
that.write(defaults, cb)
33+
}
34+
}
35+
36+
return that
37+
38+
function writeAll (data, cb) {
39+
if (!archive.writable) return cb(new Error('Archive not writable'))
40+
var keys = Object.keys(data)
41+
var pending = keys.length
42+
keys.map(function (key) {
43+
that.write(key, data[key], function (err) {
44+
if (err) return cb(err)
45+
if (!--pending) return cb()
46+
})
47+
})
48+
}
49+
}

package.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "dat-json",
3+
"description": "read &amp; write dat.json files",
4+
"version": "0.0.0",
5+
"author": "Joe Hand <joe@hand.email>",
6+
"bugs": {
7+
"url": "https://github.com/joehand/dat-json/issues"
8+
},
9+
"devDependencies": {
10+
"random-access-memory": "^2.4.0",
11+
"standard": "*",
12+
"tap-spec": "^4.0.2",
13+
"tape": "^4.0.0"
14+
},
15+
"homepage": "https://github.com/joehand/dat-json",
16+
"keywords": [],
17+
"license": "MIT",
18+
"main": "index.js",
19+
"repository": {
20+
"type": "git",
21+
"url": "https://github.com/joehand/dat-json.git"
22+
},
23+
"scripts": {
24+
"test": "standard && tape test/*.js | tap-spec"
25+
},
26+
"dependencies": {
27+
"dat-encoding": "^4.0.2",
28+
"toiletdb": "github:joehand/toiletdb#custom-fs",
29+
"xtend": "^4.0.1"
30+
}
31+
}

test/index.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
var test = require('tape')
2+
var hyperdrive = require('hyperdrive')
3+
var ram = require('random-access-memory')
4+
var datJSON = require('..')
5+
6+
test('Default dat.json', function (t) {
7+
var archive = hyperdrive(ram)
8+
archive.ready(function () {
9+
var datjson = datJSON(archive)
10+
datjson.read(function (err) {
11+
t.ok(err, 'error read before write')
12+
datjson.create({name: 'test'}, function (err) {
13+
t.error(err, 'no error')
14+
15+
datjson.read(function (err, data) {
16+
t.error(err, 'no error')
17+
t.ok(data, 'has metadata')
18+
t.same(data.url, `dat://${archive.key.toString('hex')}`)
19+
t.same(data.name, 'test', 'has name value')
20+
t.end()
21+
})
22+
})
23+
})
24+
})
25+
})
26+
27+
test('Write dat.json to archive', function (t) {
28+
var archive = hyperdrive(ram)
29+
archive.ready(function () {
30+
var datjson = datJSON(archive)
31+
datjson.create(function (err) {
32+
t.error(err, 'no error')
33+
datjson.write({specialVal: 'cat'}, check)
34+
35+
function check (err) {
36+
t.error(err, 'no error')
37+
datjson.read(function (err, data) {
38+
t.error(err, 'no error')
39+
t.ok(data, 'has metadata')
40+
t.same(data.url, `dat://${archive.key.toString('hex')}`, 'url ok')
41+
t.same(data.specialVal, 'cat', 'has special value')
42+
t.end()
43+
})
44+
}
45+
})
46+
})
47+
})

0 commit comments

Comments
 (0)