This repository was archived by the owner on Apr 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathput.js
More file actions
54 lines (43 loc) · 1.22 KB
/
put.js
File metadata and controls
54 lines (43 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
const hyperquest = require('hyperquest')
, bl = require('bl')
, xtend = require('xtend')
, url = require('url')
, replicatorBase = '/_replicator/'
/*
{
"_id": "fullfatnpm",
"source": "https://fullfatdb.npmjs.com/registry",
"target": "registry",
"continuous": true,
"user_ctx": {
"name": "rvagg",
"roles": [
"_admin"
]
},
"owner": "rvagg"
}
*/
function put (couch, user, pass, id, doc, callback) {
doc = xtend(doc, { _id: id })
var data = JSON.stringify(doc)
, options = {
auth: user + ':' + pass
, headers: {
'content-type' : 'application/json'
, 'content-length' : data.length
, 'referer' : url.parse(couch).hostname
}
}
, req = hyperquest.put(couch + replicatorBase + id, options)
req.pipe(bl(function (err, data) {
if (err)
return callback(err)
var _data = JSON.parse(data.toString())
if (typeof _data.error == 'string')
return callback(new Error('Got error from couch: ' + _data.reason + ' (' + JSON.stringify(_data) + ')'))
callback(null, _data)
}))
req.end(data)
}
module.exports = put