Files: aed5ebed79d6ab447fb2352806b064238968e9a4 / backup / async / exportIdentity.js
1358 bytesRaw
1 | const nest = require('depnest') |
2 | const { onceTrue } = require('mutant') |
3 | const path = require('path') |
4 | const fs = require('fs') |
5 | const os = require('os') |
6 | const config = require('../../config').create().config.sync.load() |
7 | const peersFile = path.join(config.path, "gossip.json") |
8 | const secretFile = path.join(config.path, "secret") |
9 | |
10 | |
11 | exports.gives = nest('backup.async.exportIdentity') |
12 | |
13 | |
14 | exports.needs = nest({ |
15 | 'keys.sync.id': 'first', |
16 | 'sbot.obs.connection': 'first' |
17 | }) |
18 | |
19 | exports.create = function (api) { |
20 | return nest('backup.async.exportIdentity', (filename, cb) => { |
21 | if ("undefined" == typeof filename) { |
22 | cb(new Error('backup requires a filename')) |
23 | } else { |
24 | |
25 | console.log(`should export identity to file ${filename}`) |
26 | |
27 | const peers = JSON.parse(fs.readFileSync(peersFile)) |
28 | const secret = fs.readFileSync(secretFile, "utf8") |
29 | |
30 | onceTrue(api.sbot.obs.connection, sbot => { |
31 | |
32 | let feedId = api.keys.sync.id() |
33 | |
34 | sbot.latestSequence(feedId, (err, sequence) => { |
35 | |
36 | if (err) return cb(err) |
37 | |
38 | let data = { |
39 | exportDate: new Date().toISOString(), |
40 | latestSequence: sequence, |
41 | peers: peers, |
42 | secret: secret |
43 | } |
44 | |
45 | fs.writeFileSync(filename, JSON.stringify(data, null, 2), "utf8") |
46 | |
47 | cb() |
48 | }) |
49 | }) |
50 | } |
51 | return true |
52 | }) |
53 | } |
54 |
Built with git-ssb-web