-
Notifications
You must be signed in to change notification settings - Fork 18
/
77-cloudant-cf.js
384 lines (333 loc) · 13.7 KB
/
77-cloudant-cf.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/**
* Copyright 2014,2016 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
module.exports = function(RED) {
"use strict";
var url = require('url');
var querystring = require('querystring');
var cfEnv = require("cfenv");
var Cloudant = require("cloudant");
var MAX_ATTEMPTS = 3;
var appEnv = cfEnv.getAppEnv();
var services = [];
// load the services bound to this application
for (var i in appEnv.services) {
if (appEnv.services.hasOwnProperty(i)) {
// filter the services to include only the Cloudant ones
if (i.match(/^(cloudant)/i)) {
services = services.concat(appEnv.services[i].map(function(v) {
return { name: v.name, label: v.label };
}));
}
}
}
//
// HTTP endpoints that will be accessed from the HTML file
//
RED.httpAdmin.get('/cloudant/vcap', function(req,res) {
res.send(JSON.stringify(services));
});
//
// Create and register nodes
//
function CloudantNode(n) {
RED.nodes.createNode(this, n);
this.name = n.name;
this.host = n.host;
this.url = n.host;
// remove unnecessary parts from host value
var parsedUrl = url.parse(this.host);
if (parsedUrl.host) {
this.host = parsedUrl.host;
}
if (this.host.indexOf("cloudant.com")!==-1) {
// extract only the account name
this.account = this.host.substring(0, this.host.indexOf('.'));
delete this.url;
}
var credentials = this.credentials;
if ((credentials) && (credentials.hasOwnProperty("username"))) { this.username = credentials.username; }
if ((credentials) && (credentials.hasOwnProperty("pass"))) { this.password = credentials.pass; }
}
RED.nodes.registerType("cloudant", CloudantNode, {
credentials: {
pass: {type:"password"},
username: {type:"text"}
}
});
function CloudantOutNode(n) {
RED.nodes.createNode(this,n);
this.operation = n.operation;
this.payonly = n.payonly || false;
this.database = _cleanDatabaseName(n.database, this);
this.cloudantConfig = _getCloudantConfig(n);
var node = this;
var credentials = {
account: node.cloudantConfig.account,
key: node.cloudantConfig.username,
password: node.cloudantConfig.password,
url: node.cloudantConfig.url
};
Cloudant(credentials, function(err, cloudant) {
if (err) { node.error(err.description, err); }
else {
// check if the database exists and create it if it doesn't
createDatabase(cloudant, node);
}
node.on("input", function(msg) {
if (err) {
return node.error(err.description, err);
}
delete msg._msgid;
handleMessage(cloudant, node, msg);
});
});
function createDatabase(cloudant, node) {
cloudant.db.list(function(err, all_dbs) {
if (err) {
if (err.status_code === 403) {
// if err.status_code is 403 then we are probably using
// an api key, so we can assume the database already exists
return;
}
node.error("Failed to list databases: " + err.description, err);
}
else {
if (all_dbs && all_dbs.indexOf(node.database) < 0) {
cloudant.db.create(node.database, function(err, body) {
if (err) {
node.error(
"Failed to create database: " + err.description,
err
);
}
});
}
}
});
}
function handleMessage(cloudant, node, msg) {
if (node.operation === "insert") {
var msg = node.payonly ? msg.payload : msg;
var root = node.payonly ? "payload" : "msg";
var doc = parseMessage(msg, root);
insertDocument(cloudant, node, doc, MAX_ATTEMPTS, function(err, body) {
if (err) {
console.trace();
console.log(node.error.toString());
node.error("Failed to insert document: " + err.description, msg);
}
});
}
else if (node.operation === "delete") {
var doc = parseMessage(msg.payload || msg, "");
if ("_rev" in doc && "_id" in doc) {
var db = cloudant.use(node.database);
db.destroy(doc._id, doc._rev, function(err, body) {
if (err) {
node.error("Failed to delete document: " + err.description, msg);
}
});
} else {
var err = new Error("_id and _rev are required to delete a document");
node.error(err.message, msg);
}
}
}
function parseMessage(msg, root) {
if (typeof msg !== "object") {
try {
msg = JSON.parse(msg);
// JSON.parse accepts numbers, so make sure that an
// object is return, otherwise create a new one
if (typeof msg !== "object") {
msg = JSON.parse('{"' + root + '":"' + msg + '"}');
}
} catch (e) {
// payload is not in JSON format
msg = JSON.parse('{"' + root + '":"' + msg + '"}');
}
}
return cleanMessage(msg);
}
// fix field values that start with _
// https://wiki.apache.org/couchdb/HTTP_Document_API#Special_Fields
function cleanMessage(msg) {
for (var key in msg) {
if (msg.hasOwnProperty(key) && !isFieldNameValid(key)) {
// remove _ from the start of the field name
var newKey = key.substring(1, msg.length);
msg[newKey] = msg[key];
delete msg[key];
node.warn("Property '" + key + "' renamed to '" + newKey + "'.");
}
}
return msg;
}
function isFieldNameValid(key) {
var allowedWords = [
'_id', '_rev', '_attachments', '_deleted', '_revisions',
'_revs_info', '_conflicts', '_deleted_conflicts', '_local_seq'
];
return key[0] !== '_' || allowedWords.indexOf(key) >= 0;
}
// Inserts a document +doc+ in a database +db+ that migh not exist
// beforehand. If the database doesn't exist, it will create one
// with the name specified in +db+. To prevent loops, it only tries
// +attempts+ number of times.
function insertDocument(cloudant, node, doc, attempts, callback) {
var db = cloudant.use(node.database);
db.insert(doc, function(err, body) {
if (err && err.status_code === 404 && attempts > 0) {
// status_code 404 means the database was not found
return cloudant.db.create(db.config.db, function() {
insertDocument(cloudant, node, doc, attempts-1, callback);
});
}
callback(err, body);
});
}
};
RED.nodes.registerType("cloudant out", CloudantOutNode);
function CloudantInNode(n) {
RED.nodes.createNode(this,n);
this.cloudantConfig = _getCloudantConfig(n);
this.database = _cleanDatabaseName(n.database, this);
this.search = n.search;
this.design = n.design;
this.index = n.index;
this.inputId = "";
var node = this;
var credentials = {
account: node.cloudantConfig.account,
key: node.cloudantConfig.username,
password: node.cloudantConfig.password,
url: node.cloudantConfig.url
};
Cloudant(credentials, function(err, cloudant) {
if (err) { node.error(err.description, err); }
node.on("input", function(msg) {
if (err) {
return node.error(err.description, err);
}
var db = cloudant.use(node.database);
var options = (typeof msg.payload === "object") ? msg.payload : {};
if (node.search === "_id_") {
var id = getDocumentId(msg.payload);
node.inputId = id;
db.get(id, function(err, body) {
sendDocumentOnPayload(err, body, msg);
});
}
else if (node.search === "_idx_") {
options.query = options.query || options.q || formatSearchQuery(msg.payload);
options.include_docs = options.include_docs || true;
options.limit = options.limit || 200;
db.search(node.design, node.index, options, function(err, body) {
sendDocumentOnPayload(err, body, msg);
});
}
else if (node.search === "_all_") {
options.include_docs = options.include_docs || true;
db.list(options, function(err, body) {
sendDocumentOnPayload(err, body, msg);
});
}
});
});
function getDocumentId(payload) {
if (typeof payload === "object") {
if ("_id" in payload || "id" in payload) {
return payload.id || payload._id;
}
}
return payload;
}
function formatSearchQuery(query) {
if (typeof query === "object") {
// useful when passing the query on HTTP params
if ("q" in query) { return query.q; }
var queryString = "";
for (var key in query) {
queryString += key + ":" + query[key] + " ";
}
return queryString.trim();
}
return query;
}
function sendDocumentOnPayload(err, body, msg) {
if (!err) {
msg.cloudant = body;
if ("rows" in body) {
msg.payload = body.rows.
map(function(el) {
if (el.doc._id.indexOf("_design/") < 0) {
return el.doc;
}
}).
filter(function(el) {
return el !== null && el !== undefined;
});
} else {
msg.payload = body;
}
}
else {
msg.payload = null;
if (err.description === "missing") {
node.warn(
"Document '" + node.inputId +
"' not found in database '" + node.database + "'.",
err
);
} else {
node.error(err.description, err);
}
}
node.send(msg);
}
}
RED.nodes.registerType("cloudant in", CloudantInNode);
// must return an object with, at least, values for account, username and
// password for the Cloudant service at the top-level of the object
function _getCloudantConfig(n) {
if (n.service === "_ext_") {
return RED.nodes.getNode(n.cloudant);
} else if (n.service !== "") {
var service = appEnv.getService(n.service);
var cloudantConfig = { };
var host = service.credentials.host;
cloudantConfig.username = service.credentials.username;
cloudantConfig.password = service.credentials.password;
cloudantConfig.account = host.substring(0, host.indexOf('.'));
return cloudantConfig;
}
}
// remove invalid characters from the database name
// https://wiki.apache.org/couchdb/HTTP_database_API#Naming_and_Addressing
function _cleanDatabaseName(database, node) {
var newDatabase = database;
// caps are not allowed
newDatabase = newDatabase.toLowerCase();
// remove trailing underscore
newDatabase = newDatabase.replace(/^_/, '');
// remove spaces and slashed
newDatabase = newDatabase.replace(/[\s\\/]+/g, '-');
if (newDatabase !== database) {
node.warn("Database renamed as '" + newDatabase + "'.");
}
return newDatabase;
}
};