git ssb

1+

dinoworm ๐Ÿ› / vas



Tree: 7e467f6efa8f6320f4c92bbfbe6e905689e0b8eb

Files: 7e467f6efa8f6320f4c92bbfbe6e905689e0b8eb / http / client.js

1960 bytesRaw
1var defined = require('defined')
2var Url = require('url')
3var getIn = require('get-in')
4var Path = require('path')
5var toPull = require('stream-to-pull-stream')
6var defer = require('pull-defer')
7var get = require('simple-get')
8var pull = require('pull-stream')
9
10var defaultSerialize = require('../serialize')
11
12module.exports = createHttpClient
13
14function createHttpClient (client, options) {
15 options = defined(options, {})
16
17 var serialize = defined(options.serialize, defaultSerialize)
18 var manifest = client.manifest
19 var url = options.url
20 var base = isObject(url) ? url : Url.parse(url)
21
22 return map(manifest, [], function (name, type) {
23 return function (options, cb) {
24 var url = Url.format({
25 protocol: base.protocol,
26 host: base.host,
27 pathname: Path.join(
28 base.pathname || '/',
29 name.join('/')
30 ),
31 query: options
32 })
33
34 switch (type) {
35 case 'async':
36 case 'sync':
37 return get.concat({
38 url: url,
39 json: true
40 }, function (err, res, data) {
41 cb(err, data)
42 })
43 case 'source':
44 var deferred = defer.source()
45 get(url, function (err, res) {
46 if (err) return deferred.abort(err)
47
48 var source = pull(
49 toPull.source(res),
50 serialize.parse()
51 )
52 deferred.resolve(source)
53 })
54 return deferred
55 }
56 }
57 })
58}
59
60function map(manifest, name, fn) {
61 var o = {}
62 for(var key in manifest) {
63 var value = manifest[key]
64 if (value == null) continue
65 o[key] = (
66 isString(value) ? fn(name.concat(key), value)
67 : isObject(value) ? map(value, name.concat(key), fn)
68 : (function () { throw new Error('invalid manifest:' + value) })()
69 )
70 }
71 return o
72}
73
74function isObject (o) {
75 return o && 'object' === typeof o
76}
77
78function isString (s) {
79 return 'string' === typeof s
80}
81

Built with git-ssb-web