git ssb

0+

ev / 0qc



Tree: 3fd43b43c3b954ff0c298a7fd570c4d12ba97d72

Files: 3fd43b43c3b954ff0c298a7fd570c4d12ba97d72 / bin.js

5261 bytesRaw
1#! /usr/bin/env node
2
3var fs = require('fs')
4var path = require('path')
5var pull = require('pull-stream')
6var toPull = require('stream-to-pull-stream')
7var File = require('pull-file')
8var explain = require('explain-error')
9var ssbKeys = require('ssb-keys')
10var stringify = require('pull-stringify')
11var createHash = require('multiblob/util').createHash
12var minimist = require('minimist')
13var muxrpcli = require('muxrpcli')
14var cmdAliases = require('minsbot/lib/cli-cmd-aliases')
15var ProgressBar = require('minsbot/lib/progress')
16var packageJson = require('minsbot/package.json')
17
18var open = require('opn')
19
20//get config as cli options after --, options before that are
21//options to the command.
22var argv = process.argv.slice(2)
23var i = argv.indexOf('--')
24var conf = argv.slice(i+1)
25argv = ~i ? argv.slice(0, i) : argv
26
27var network = 'ssb'
28//var network = 'decent'
29//var network = 'testnet'
30
31var config = require('minsbot/config/inject')(network)
32
33keys = ssbKeys.loadOrCreateSync(path.join(config.path, 'secret'))
34
35var liteClient = fs.readFileSync(path.join('./build/index.html'))
36
37var manifestFile = path.join(config.path, 'manifest.json')
38
39if (argv[0] == 'server') {
40 console.log('my key ID:', keys.public)
41
42 // special server command:
43 // import sbot and start the server
44
45 var createSbot = require('minsbot')
46 .use(require('minsbot/plugins/plugins'))
47 .use(require('minsbot/plugins/master'))
48 .use(require('minsbot/plugins/gossip'))
49 .use(require('minsbot/plugins/replicate'))
50 .use(require('ssb-friends'))
51 .use(require('ssb-blobs'))
52 .use(require('minsbot/plugins/invite'))
53 .use(require('minsbot/plugins/local'))
54 .use(require('minsbot/plugins/logging'))
55 .use(require('minsbot/query'))
56 .use(require('ssb-search'))
57 .use(require('ssb-links'))
58 .use(require('ssb-backlinks'))
59 .use(require('minsbot/decent-ws'))
60 .use(require('ssb-ebt'))
61 .use({
62 name: 'serve',
63 version: '1.0.0',
64 init: function (sbot) {
65 sbot.ws.use(function (req, res, next) {
66 //res.setHeader('Access-Control-Allow-Origin', '*')
67 var send = config
68 delete send.keys // very important to keep this, as it removes the server keys from the config before broadcast
69 send.address = sbot.ws.getAddress()
70 sbot.invite.create({modern: true}, function (err, cb) {
71 send.invite = cb
72 })
73 if(req.url == '/')
74 res.end(liteClient)
75 if(req.url == '/get-config')
76 res.end(JSON.stringify(send))
77 else next()
78 })
79 }
80 })
81
82 open('http://localhost:' + config.ws.port, {wait: false})
83
84
85 // add third-party plugins
86 //require('./plugins/plugins').loadUserPlugins(createSbot, config)
87
88 // start server
89
90 config.keys = keys
91 var server = createSbot(config)
92
93 // write RPC manifest to ~/.ssb/manifest.json
94 fs.writeFileSync(manifestFile, JSON.stringify(server.getManifest(), null, 2))
95
96
97 ProgressBar(server.progress)
98
99} else {
100
101 // normal command:
102 // create a client connection to the server
103
104 // read manifest.json
105 var manifest
106 try {
107 manifest = JSON.parse(fs.readFileSync(manifestFile))
108 } catch (err) {
109 throw explain(err,
110 'no manifest file'
111 + '- should be generated first time server is run'
112 )
113 }
114
115 // connect
116 require('ssb-client')(keys, {
117 manifest: manifest,
118 port: config.port,
119 host: config.host||'localhost',
120 caps: config.caps,
121 key: config.key || keys.id
122 }, function (err, rpc) {
123 if(err) {
124 if (/could not connect/.test(err.message)) {
125 console.log('Error: Could not connect to the scuttlebot server.')
126 console.log('Use the "server" command to start it.')
127 if(config.verbose) throw err
128 process.exit(1)
129 }
130 throw err
131 }
132
133 // add aliases
134 for (var k in cmdAliases) {
135 rpc[k] = rpc[cmdAliases[k]]
136 manifest[k] = manifest[cmdAliases[k]]
137 }
138
139 // add some extra commands
140 manifest.version = 'async'
141 manifest.config = 'sync'
142 rpc.version = function (cb) {
143 console.log(require('./package.json').version)
144 cb()
145 }
146 rpc.config = function (cb) {
147 console.log(JSON.stringify(config, null, 2))
148 cb()
149 }
150
151 // HACK
152 // we need to output the hash of blobs that are added via blobs.add
153 // because muxrpc doesnt support the `sink` callback yet, we need this manual override
154 // -prf
155 if (process.argv[2] === 'blobs.add') {
156 var filename = process.argv[3]
157 var source =
158 filename ? File(process.argv[3])
159 : !process.stdin.isTTY ? toPull.source(process.stdin)
160 : (function () {
161 console.error('USAGE:')
162 console.error(' blobs.add <filename> # add a file')
163 console.error(' source | blobs.add # read from stdin')
164 process.exit(1)
165 })()
166 var hasher = createHash('sha256')
167 pull(
168 source,
169 hasher,
170 rpc.blobs.add(function (err) {
171 if (err)
172 throw err
173 console.log('&'+hasher.digest)
174 process.exit()
175 })
176 )
177 return
178 }
179
180 // run commandline flow
181 muxrpcli(argv, manifest, rpc, config.verbose)
182 })
183}
184
185
186
187
188

Built with git-ssb-web