Skip to content

Commit

Permalink
More dat experimentation, add mkdirp
Browse files Browse the repository at this point in the history
  • Loading branch information
jimpick committed Dec 5, 2017
1 parent 42e7fa7 commit c69d215
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 7 deletions.
30 changes: 27 additions & 3 deletions lib/mpl/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ var _events = require('events');

var _events2 = _interopRequireDefault(_events);

var _fs = require('fs');

var _fs2 = _interopRequireDefault(_fs);

var _mkdirp = require('mkdirp');

var _mkdirp2 = _interopRequireDefault(_mkdirp);

var _ipfs = require('ipfs');

var _ipfs2 = _interopRequireDefault(_ipfs);
Expand Down Expand Up @@ -58,12 +66,15 @@ var Network = function (_EventEmitter) {
});

if (!process.env.DAT_DIR) {
console.error('DAT_DIR environment variable not set');
process.exit(1);
throw new Error('DAT_DIR environment variable not set');
// process.exit(1)
}
(0, _datNode2.default)(process.env.DAT_DIR, function (dat, err) {
_this.datDir = process.env.DAT_DIR;
(0, _datNode2.default)(_this.datDir, function (err, dat) {
if (err) throw err; // What is the right way to handle errors here?

_this.dat = dat;

dat.joinNetwork(function (err) {
if (err) {
console.error('joinNetwork error', err);
Expand Down Expand Up @@ -138,6 +149,19 @@ var Network = function (_EventEmitter) {
this.Peers[peer] = new _automerge2.default.Connection(this.docSet, function (msg) {
console.log('Automerge.Connection> send to ' + peer + ':', msg);
_this3.room.sendTo(peer, JSON.stringify(msg));

if (_this3.dat) {
_mkdirp2.default.sync(_this3.datDir + '/' + peer);
_fs2.default.writeFileSync(_this3.datDir + '/' + peer + '/lastMessage.json', JSON.stringify(msg));
_this3.dat.importFiles();
/*
this.dat.archive.writeFile('/lastMessage.json', JSON.stringify(msg), err => {
if (err) {
console.error('writeFile error', err)
}
})
*/
}
});

this.Peers[peer].open();
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"debug": "^2.6.8",
"dotenv": "^4.0.0",
"ipfs": "^0.26.0",
"ipfs-pubsub-room": "^0.5.0"
"ipfs-pubsub-room": "^0.5.0",
"mkdirp": "^0.5.1"
},
"devDependencies": {
"babel-cli": "6.24.1",
Expand Down
25 changes: 22 additions & 3 deletions src/mpl/network.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import EventEmitter from 'events'

import fs from 'fs'
import mkdirp from 'mkdirp'
import IPFS from 'ipfs'
import Room from 'ipfs-pubsub-room'
import Dat from 'dat-node'
Expand Down Expand Up @@ -27,12 +29,15 @@ export default class Network extends EventEmitter {
})

if (!process.env.DAT_DIR) {
console.error('DAT_DIR environment variable not set')
process.exit(1)
throw new Error('DAT_DIR environment variable not set')
// process.exit(1)
}
Dat(process.env.DAT_DIR, (dat, err) => {
this.datDir = process.env.DAT_DIR
Dat(this.datDir, (err, dat) => {
if (err) throw err // What is the right way to handle errors here?

this.dat = dat

dat.joinNetwork(err => {
if (err) {
console.error('joinNetwork error', err)
Expand Down Expand Up @@ -84,6 +89,20 @@ export default class Network extends EventEmitter {
this.Peers[peer] = new Automerge.Connection(this.docSet, msg => {
console.log('Automerge.Connection> send to ' + peer + ':', msg)
this.room.sendTo(peer, JSON.stringify(msg))

if (this.dat) {
mkdirp.sync(`${this.datDir}/${peer}`)
fs.writeFileSync(`${this.datDir}/${peer}/lastMessage.json`, JSON.stringify(msg))
this.dat.importFiles()
/*
this.dat.archive.writeFile('/lastMessage.json', JSON.stringify(msg), err => {
if (err) {
console.error('writeFile error', err)
}
})
*/
}

})

this.Peers[peer].open()
Expand Down

0 comments on commit c69d215

Please sign in to comment.