/ src / server / connector.js
connector.js
 1  import express from 'express'
 2  const router = express.Router()
 3  
 4  // This module is built for connecting to other AOs via the Tor network.
 5  //
 6  // This is a useful feature, but it's currently disabled because of a
 7  // faulty js library ('tor-request')
 8  
 9  // Deprecate this
10  import tr from 'tor-request' 
11  
12  import crypto from '../crypto.js'
13  import calculations from '../calculations.js'
14  import request from 'superagent'
15  
16  function postEvent(address, secret, body, callback){
17    tr.request({
18        url: 'http://' + address + '/events',
19        headers: {"Authorization": secret},  // didn't work,  "Transfer-Encoding": "chunked"
20        method: 'post',
21        body,
22        json: true,
23      }, function (err, res, resBody) {
24            if (err){
25                return callback(err)
26            }
27            callback(err, resBody)
28    })
29  }
30  
31  function checkHash(address, secret, taskId, callback){
32    tr.request({
33        url: 'http://' + address + '/taskhash/' + taskId,
34        headers: {"Authorization": secret},
35        method: 'post',
36        json: true,
37      }, function (err, res, resBody) {
38            if (err){
39                return callback(err)
40            }
41            callback(null, resBody)
42    })
43  }
44  
45  function getState(address, secret, callback){
46    tr.request({
47        url: 'http://' + address + '/state',
48        headers: {"Authorization": secret},
49        method: 'post',
50        body: { x: true },
51        json: true
52      }, function (err, res, resBody) {
53            if (err){
54                return callback(err)
55            }
56            callback(null, resBody)
57    })
58  }
59  
60  export default {
61      postEvent,
62      getState,
63      checkHash,
64  }