git ssb

0+

ev / 0qc



Tree: e4ce6bb40173e08028c870d5b637e13128c98c83

Files: e4ce6bb40173e08028c870d5b637e13128c98c83 / bin.js

5229 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-links'))
57 .use(require('ssb-backlinks'))
58 .use(require('minsbot/decent-ws'))
59 .use(require('ssb-ebt'))
60 .use({
61 name: 'serve',
62 version: '1.0.0',
63 init: function (sbot) {
64 sbot.ws.use(function (req, res, next) {
65 //res.setHeader('Access-Control-Allow-Origin', '*')
66 var send = config
67 delete send.keys // very important to keep this, as it removes the server keys from the config before broadcast
68 send.address = sbot.ws.getAddress()
69 sbot.invite.create({modern: true}, function (err, cb) {
70 send.invite = cb
71 })
72 if(req.url == '/')
73 res.end(liteClient)
74 if(req.url == '/get-config')
75 res.end(JSON.stringify(send))
76 else next()
77 })
78 }
79 })
80
81 open('http://localhost:' + config.ws.port, {wait: false})
82
83
84 // add third-party plugins
85 //require('./plugins/plugins').loadUserPlugins(createSbot, config)
86
87 // start server
88
89 config.keys = keys
90 var server = createSbot(config)
91
92 // write RPC manifest to ~/.ssb/manifest.json
93 fs.writeFileSync(manifestFile, JSON.stringify(server.getManifest(), null, 2))
94
95
96 ProgressBar(server.progress)
97
98} else {
99
100 // normal command:
101 // create a client connection to the server
102
103 // read manifest.json
104 var manifest
105 try {
106 manifest = JSON.parse(fs.readFileSync(manifestFile))
107 } catch (err) {
108 throw explain(err,
109 'no manifest file'
110 + '- should be generated first time server is run'
111 )
112 }
113
114 // connect
115 require('ssb-client')(keys, {
116 manifest: manifest,
117 port: config.port,
118 host: config.host||'localhost',
119 caps: config.caps,
120 key: config.key || keys.id
121 }, function (err, rpc) {
122 if(err) {
123 if (/could not connect/.test(err.message)) {
124 console.log('Error: Could not connect to the scuttlebot server.')
125 console.log('Use the "server" command to start it.')
126 if(config.verbose) throw err
127 process.exit(1)
128 }
129 throw err
130 }
131
132 // add aliases
133 for (var k in cmdAliases) {
134 rpc[k] = rpc[cmdAliases[k]]
135 manifest[k] = manifest[cmdAliases[k]]
136 }
137
138 // add some extra commands
139 manifest.version = 'async'
140 manifest.config = 'sync'
141 rpc.version = function (cb) {
142 console.log(require('./package.json').version)
143 cb()
144 }
145 rpc.config = function (cb) {
146 console.log(JSON.stringify(config, null, 2))
147 cb()
148 }
149
150 // HACK
151 // we need to output the hash of blobs that are added via blobs.add
152 // because muxrpc doesnt support the `sink` callback yet, we need this manual override
153 // -prf
154 if (process.argv[2] === 'blobs.add') {
155 var filename = process.argv[3]
156 var source =
157 filename ? File(process.argv[3])
158 : !process.stdin.isTTY ? toPull.source(process.stdin)
159 : (function () {
160 console.error('USAGE:')
161 console.error(' blobs.add <filename> # add a file')
162 console.error(' source | blobs.add # read from stdin')
163 process.exit(1)
164 })()
165 var hasher = createHash('sha256')
166 pull(
167 source,
168 hasher,
169 rpc.blobs.add(function (err) {
170 if (err)
171 throw err
172 console.log('&'+hasher.digest)
173 process.exit()
174 })
175 )
176 return
177 }
178
179 // run commandline flow
180 muxrpcli(argv, manifest, rpc, config.verbose)
181 })
182}
183
184
185
186
187

Built with git-ssb-web