Files: f70ff5274e668e1abac1005e5a219e16938ee5cb / decent.js
4153 bytesRaw
1 | var fs = require('fs') |
2 | var path = require('path') |
3 | var pull = require('pull-stream') |
4 | var toPull = require('stream-to-pull-stream') |
5 | var File = require('pull-file') |
6 | var explain = require('explain-error') |
7 | var ssbKeys = require('ssb-keys') |
8 | var stringify = require('pull-stringify') |
9 | var createHash = require('multiblob/util').createHash |
10 | var minimist = require('minimist') |
11 | var muxrpcli = require('muxrpcli') |
12 | var cmdAliases = require('./lib/cli-cmd-aliases') |
13 | |
14 | //get config as cli options after --, options before that are |
15 | //options to the command. |
16 | var argv = process.argv.slice(2) |
17 | var i = argv.indexOf('--') |
18 | var conf = argv.slice(i+1) |
19 | argv = ~i ? argv.slice(0, i) : argv |
20 | |
21 | var config = require('./plugins/ssb-config/inject')(process.env.ssb_appname, minimist(conf)) |
22 | |
23 | var keys = ssbKeys.loadOrCreateSync(path.join(config.path, 'secret')) |
24 | |
25 | var manifestFile = path.join(config.path, 'manifest.json') |
26 | |
27 | if (argv[0] == 'server') { |
28 | |
29 | // special server command: |
30 | // import sbot and start the server |
31 | |
32 | var createSbot = require('./lib') |
33 | .use(require('./plugins/plugins')) |
34 | .use(require('./plugins/master')) |
35 | .use(require('./plugins/gossip')) |
36 | .use(require('./plugins/replicate')) |
37 | .use(require('ssb-friends')) |
38 | .use(require('ssb-blobs')) |
39 | .use(require('./plugins/invite')) |
40 | .use(require('./plugins/local')) |
41 | .use(require('./plugins/logging')) |
42 | .use(require('./plugins/private')) |
43 | .use(require('ssb-query')) |
44 | .use(require('ssb-links')) |
45 | .use(require('ssb-ws')) |
46 | .use(require('ssb-ebt')) |
47 | |
48 | // add third-party plugins |
49 | require('./plugins/plugins').loadUserPlugins(createSbot, config) |
50 | |
51 | // start server |
52 | |
53 | config.keys = keys |
54 | var server = createSbot(config) |
55 | |
56 | // write RPC manifest to ~/.ssb/manifest.json |
57 | fs.writeFileSync(manifestFile, JSON.stringify(server.getManifest(), null, 2)) |
58 | |
59 | var lite = require('./serve') |
60 | lite.serve() |
61 | |
62 | } else { |
63 | |
64 | // normal command: |
65 | // create a client connection to the server |
66 | |
67 | // read manifest.json |
68 | var manifest |
69 | try { |
70 | manifest = JSON.parse(fs.readFileSync(manifestFile)) |
71 | } catch (err) { |
72 | throw explain(err, |
73 | 'no manifest file' |
74 | + '- should be generated first time server is run' |
75 | ) |
76 | } |
77 | |
78 | // connect |
79 | require('./plugins/ssb-client')(keys, { |
80 | manifest: manifest, |
81 | port: config.port, |
82 | host: config.host||'localhost', |
83 | caps: config.caps, |
84 | key: config.key || keys.id |
85 | }, function (err, rpc) { |
86 | if(err) { |
87 | if (/could not connect/.test(err.message)) { |
88 | console.log('Error: Could not connect to the scuttlebot server.') |
89 | console.log('Use the "server" command to start it.') |
90 | if(config.verbose) throw err |
91 | process.exit(1) |
92 | } |
93 | throw err |
94 | } |
95 | |
96 | // add aliases |
97 | for (var k in cmdAliases) { |
98 | rpc[k] = rpc[cmdAliases[k]] |
99 | manifest[k] = manifest[cmdAliases[k]] |
100 | } |
101 | |
102 | // add some extra commands |
103 | manifest.version = 'async' |
104 | manifest.config = 'sync' |
105 | rpc.version = function (cb) { |
106 | console.log(require('./package.json').version) |
107 | cb() |
108 | } |
109 | rpc.config = function (cb) { |
110 | console.log(JSON.stringify(config, null, 2)) |
111 | cb() |
112 | } |
113 | |
114 | // HACK |
115 | // we need to output the hash of blobs that are added via blobs.add |
116 | // because muxrpc doesnt support the `sink` callback yet, we need this manual override |
117 | // -prf |
118 | if (process.argv[2] === 'blobs.add') { |
119 | var filename = process.argv[3] |
120 | var source = |
121 | filename ? File(process.argv[3]) |
122 | : !process.stdin.isTTY ? toPull.source(process.stdin) |
123 | : (function () { |
124 | console.error('USAGE:') |
125 | console.error(' blobs.add <filename> # add a file') |
126 | console.error(' source | blobs.add # read from stdin') |
127 | process.exit(1) |
128 | })() |
129 | var hasher = createHash('sha256') |
130 | pull( |
131 | source, |
132 | hasher, |
133 | rpc.blobs.add(function (err) { |
134 | if (err) |
135 | throw err |
136 | console.log('&'+hasher.digest) |
137 | process.exit() |
138 | }) |
139 | ) |
140 | return |
141 | } |
142 | |
143 | // run commandline flow |
144 | muxrpcli(argv, manifest, rpc, config.verbose) |
145 | }) |
146 | } |
147 | |
148 | |
149 | |
150 | |
151 | |
152 | |
153 |
Built with git-ssb-web