/
riak.js
139 lines (112 loc) · 3.97 KB
/
riak.js
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
var assert = require('assert'),
http = require('http'),
path = require('path'),
querystring = require('querystring'),
sys = require('sys'),
url = require('url');
var Client = exports.Client = function (params) {
this.params = params;
this.raw = params.raw || 'raw';
this.client = http.createClient(params.port || 8098,
params.host || 'localhost');
}
sys.inherits(Client, process.EventEmitter);
Client.prototype.makeLinkHeader = function(links) {
// [
// { doc: "/memos/memo0", tag="author" },
// { doc: "/memos/memo1", tag="author" },
// { doc: "/memos/memo2", tag="author" },
// ]
var self = this;
var pieces = links.map(function (val) {
return '</' + self.raw + val.doc + '>; riaktag="' + val.tag + '"';
});
return pieces.join(", ");
}
Client.prototype.documentPath = function(bucket, key) {
return path.join('/', this.raw, bucket, key);
}
Client.prototype.store = function(bucket, key, data, params) {
assert.ok(bucket);
assert.ok(key);
var headers = (params && params.headers) || {};
switch (params && params.type) {
case "json":
headers['content-type'] = "application/javascript";
data = JSON.stringify(data);
break;
case "binary":
headers['content-type'] = "application/octet-stream";
break;
case undefined:
case "text":
headers['content-type'] = "text/plain";
break;
default:
headers['content-type'] = params.type;
}
var document_path = this.documentPath(bucket, key);
if (params) {
var query = {};
['r', 'w', 'dw', 'returnbody'].forEach(function (f) {
if (params[f]) query[f] = params[f];
});
if (Object.keys(query).length > 0)
document_path += '?' + querystring.stringify(query);
}
return this.makeRequest('PUT', document_path,
headers, data);
}
Client.prototype.fetch = function(bucket, key, params) {
var document_path = this.documentPath(bucket, key);
if (params && 'r' in params) {
document_path += '?' + 'r=' + params.r;
}
return this.makeRequest('GET', this.documentPath(bucket, key));
}
Client.prototype.remove = function(bucket, key, params) {
return this.makeRequest('DELETE', this.documentPath(bucket, key));
}
Client.prototype.walk = function(start, spec) {
// walk(['bucket', 'key'], [['_', 'author', 1]]);
// walk(['bucket', 'key'], [['people', 'author', 1]]);
// walk(['bucket', 'key'], [['_', 'author', 1]])
// => /raw/bucket/key/_,author,1
var document_path = this.documentPath(start[0], start[1])
+ '/' + spec.map(function (step) {
return step.map(function (value) {
return encodeURIComponent(value || '_'); });
}).join('/');
debug('document_path: ' + document_path);
return this.makeRequest('GET', document_path);
}
var multipart = require('multipart');
Client.prototype.makeRequest = function(method, url, headers, data) {
assert.ok(method);
assert.ok(url);
var request = this.client.request(method, url, headers);
var promise = new process.Promise();
if (data) request.write(data);
request.addListener('response', function (response) {
var body = "";
response.addListener("data", function (chunk) { body += chunk; });
response.addListener("end", function () {
var resp = { headers: response.headers, data: body };
if ( response.statusCode < 200
|| response.statusCode >= 300) {
promise.emitError(resp, response.statusCode);
return;
}
// don't try to json parse a response if there's no content
if (response.statusCode != 204
&& (response.headers['content-type'] == 'application/json' ||
response.headers['content-type'] == 'application/javascript')
&& Number(response.headers['content-length'])) {
resp.data = JSON.parse(body);
}
promise.emitSuccess(resp, response.statusCode);
});
});
request.close();
return promise;
}