Files: bde3bf7b399b05e5d5c6475ee35ef2745c6788de / test / util.js
1524 bytesRaw
1 | var pull = require('pull-stream') |
2 | var crypto = require('crypto') |
3 | |
4 | var log = exports.log = function log (name) { |
5 | if(process.env.DEBUG) |
6 | return pull.through(function (e) { |
7 | console.log(name, e) |
8 | }) |
9 | else |
10 | return pull.through() |
11 | } |
12 | |
13 | function bindAll(obj, context) { |
14 | var o = {} |
15 | for(var k in obj) { |
16 | if(obj[k]) |
17 | o[k] = obj[k].bind(context) |
18 | } |
19 | return o |
20 | } |
21 | |
22 | exports.peers = function (nameA, a, nameB, b, async) { |
23 | var na = nameA[0].toUpperCase(), nb = nameB[0].toUpperCase() |
24 | //this is just a hack to fake RPC. over rpc each method is called |
25 | //with the remote id in the current this context. |
26 | a._onConnect({id: nameB, blobs: bindAll(b, {id: nameA})}, nb+na) |
27 | b._onConnect({id: nameA, blobs: bindAll(a, {id: nameB})}, na+nb) |
28 | } |
29 | |
30 | |
31 | exports.hash = function (buf) { |
32 | buf = 'string' == typeof buf ? new Buffer(buf) : buf |
33 | return '&'+crypto.createHash('sha256') |
34 | .update(buf).digest('base64')+'.sha256' |
35 | } |
36 | |
37 | exports.fake = function (string, length) { |
38 | var b = new Buffer(length) |
39 | var n = Buffer.byteLength(string) |
40 | for(var i = 0; i < length; i += n) |
41 | b.write(string, i) |
42 | return b |
43 | } |
44 | |
45 | |
46 | exports.sync = function noAsync (test, done) { |
47 | function async(fn) { |
48 | return fn |
49 | } |
50 | async.through = function () { return pull.through() } |
51 | async.done = done |
52 | test(async) |
53 | } |
54 | |
55 | exports.tests = function (tests) { |
56 | tests(function (name, async) { |
57 | return require('../inject')( |
58 | require('./mock/blobs')(name, async), |
59 | require('./mock/set')(async), |
60 | name |
61 | ) |
62 | }, exports.sync) |
63 | } |
64 | |
65 | |
66 |
Built with git-ssb-web