diff --git a/lib/mpl/network.js b/lib/mpl/network.js index 882419c..f2dedc3 100644 --- a/lib/mpl/network.js +++ b/lib/mpl/network.js @@ -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); @@ -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); @@ -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(); diff --git a/package.json b/package.json index 142a2e2..52344dc 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/mpl/network.js b/src/mpl/network.js index ace6787..6449009 100644 --- a/src/mpl/network.js +++ b/src/mpl/network.js @@ -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' @@ -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) @@ -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()