Skip to content

Commit 6cb7c28

Browse files
committed
json based config file
1 parent 7bf6094 commit 6cb7c28

4 files changed

Lines changed: 89 additions & 8 deletions

File tree

examples/test-loopback-party.js

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
1+
const fs = require('fs/promises')
12
const debug = require('debug')('test.local-db')
23
const WRTC = require('wrtc')
34
const BouncerModel = require('@dataparty/bouncer-model/dist/bouncer-model.json')
45
const Dataparty = require('../src')
56

67
async function main(){
7-
const dbPath = '/tmp/local-peer-party-loki.db'
8+
const dbPath = await fs.mkdtemp('/tmp/tingo-party')
9+
const configPath = await fs.mkdtemp('/tmp/tingo-party-config')
810

911
debug('db location', dbPath)
1012

11-
let hostLocal = new Dataparty.LokiParty({
13+
let config = new Dataparty.Config.JsonFileConfig({basePath: configPath})
14+
15+
let hostLocal = new Dataparty.TingoParty({
1216
path: dbPath,
1317
model: BouncerModel,
14-
config: new Dataparty.Config.MemoryConfig()
18+
config
1519
})
1620

1721
let loopback = new Dataparty.Comms.LoopbackChannel()
@@ -25,7 +29,7 @@ async function main(){
2529
comms: comms1,
2630
hostParty: hostLocal,
2731
model: BouncerModel,
28-
config: new Dataparty.Config.MemoryConfig()
32+
config
2933
})
3034

3135

@@ -38,6 +42,8 @@ async function main(){
3842
})
3943

4044

45+
await config.start()
46+
4147

4248
await peer1.loadIdentity()
4349
await peer2.loadIdentity()
@@ -78,6 +84,14 @@ async function main(){
7884
}
7985

8086

81-
main().catch(err=>{
82-
console.error(err)
83-
})
87+
try{
88+
89+
main().catch(err=>{
90+
console.error(err)
91+
})
92+
93+
}
94+
catch(err){
95+
console.log('crash')
96+
console.log(err)
97+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@dataparty/api",
33
"private": false,
4-
"version": "1.2.7",
4+
"version": "1.2.8",
55
"main": "dist/dataparty.js",
66
"browser": "src/index-browser.js",
77
"source": "src/index.js",

src/config/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
const NconfConfig = require('./nconf')
22
const MemoryConfig = require('./memory')
3+
const JsonFileConfig = require('./json-file')
34
const LocalStorageConfig = require('./local-storage')
45

56
module.exports = {
67
NconfConfig,
78
MemoryConfig,
9+
JsonFileConfig,
810
LocalStorageConfig
911
}

src/config/json-file.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
'use strict';
2+
3+
const fs = require('fs')
4+
const deepSet = require('deep-set')
5+
const reach = require('../utils/reach')
6+
const logger = require('debug')('dataparty.config.memory');
7+
8+
/**
9+
* @class
10+
* @implements {Config}
11+
*/
12+
class JsonFileConfig {
13+
14+
constructor(defaults){
15+
this.path = reach(defaults, 'basePath') +'/config.json'
16+
this.defaults = defaults || {}
17+
this.content = Object.assign({}, this.defaults)
18+
}
19+
20+
async load(){
21+
if(!fs.existsSync(this.path)){return}
22+
23+
let rawdata = fs.readFileSync(this.path)
24+
25+
if(rawdata && rawdata.length > 0){
26+
this.content = JSON.parse(rawdata)
27+
}
28+
}
29+
30+
async start () {
31+
await this.load()
32+
return this
33+
}
34+
35+
clear () {
36+
this.content = {}
37+
}
38+
39+
readAll(){
40+
41+
return Object.assign({}, this.content)
42+
}
43+
44+
read(key){
45+
logger('reading path: ' + key)
46+
return reach( this.content, key)
47+
}
48+
49+
async write(key, value){
50+
51+
deepSet(this.content, key, value)
52+
await this.save()
53+
}
54+
55+
56+
exists(key){
57+
return (read(key) !== undefined)
58+
}
59+
60+
async save(){
61+
fs.writeFileSync(this.path, JSON.stringify(this.content, null, 2))
62+
}
63+
}
64+
65+
module.exports = JsonFileConfig

0 commit comments

Comments
 (0)