-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
342 lines (293 loc) · 9.33 KB
/
index.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
var argv = require('optimist').argv;
var path = require('path');
var fs = require('fs');
var mime = require('mime');
var requests = require("request");
var tmp = require("tmp");
module.exports = {
parse: function(input_params, json_parsed, callback){
json_parsed = typeof json_parsed !== 'undefined' || json_parsed === null ? json_parsed : true;
var request = new Request()
if (json_parsed == true) {
params = input_params
} else {
try {
params = JSON.parse(input_params);
} catch(e) {
throw "You didn't pass valid json inputs.";
};
}
var num_files = 0;
var num_written_files = 0;
var are_all_inputs_processed = false;
var checkForEnd = function(){
num_written_files++;
if (are_all_inputs_processed && num_files == num_written_files){
callback(request);
}
}
if (!(toType( params ) == "object")){
throw "Can't parse keys/values from your json inputs.";
}
if (!(("_blockspring_spec" in params) && params._blockspring_spec)){
request.params = params;
callback(request);
return;
}
Object.keys(params).forEach(function(var_name){
// Ignore blockspring_spec flag
if (var_name == "_blockspring_spec"){
return;
}
// Add any errors to the request object
else if ((var_name == "_errors") && (toType( params[var_name] ) == "array")){
params[var_name].forEach(function(error){
if((toType( error ) == "object") && ("title" in error)){
request._errors.push(error);
}
});
}
// Add any headers to the request object
else if ((var_name == "_headers") && (toType( params[var_name] ) == "object")){
request.addHeaders(params[var_name]);
}
// Process file spec
else if (
toType(params[var_name]) == "object" &&
"filename" in params[var_name] &&
params[var_name].filename &&
((("data" in params[var_name]) && params[var_name].data) ||
(("url" in params[var_name]) && params[var_name].url))
) {
num_files++;
tmp.tmpName({postfix: '-' + params[var_name].filename }, function _tempNameGenerated(err, tmp_name) {
if (err) {
request.params[var_name] = params[var_name];
checkForEnd();
} else {
if ("data" in params[var_name]){
fs.writeFile(tmp_name, params[var_name].data, {encoding:'base64'}, function(err){
if(err){
request.params[var_name] = params[var_name]
} else {
request.params[var_name] = tmp_name;
}
checkForEnd();
});
} else {
var file = fs.createWriteStream(tmp_name);
requests(params[var_name].url).pipe(file);
file.on('finish', function() {
request.params[var_name] = tmp_name;
checkForEnd();
});
}
}
});
}
// Otherwise treat it as a normal input
else {
request.params[var_name] = params[var_name];
}
});
// tmp lib might fire callback before looping through all inputs
// so we need to keep track of when we finish
are_all_inputs_processed = true;
if (num_files == num_written_files){
callback(request);
}
},
run: function(block, data, options, callback) {
// checks in case callback was provided in place of data or options.
if (typeof data === 'function') {
// allow data and api_key to be optional
callback = data;
data = {};
} else if (typeof options === 'function') {
// allow api_key to be optional, but keep callback as last argument
callback = options;
} else if (typeof options === "string"){
options = {
api_key: options,
cache: false,
expiry: null
}
}
// checks in case callback was provided in place of data or options.
data = typeof data !== 'undefined' || data === null ? data : {};
options = typeof options !== 'undefined' ? options : { api_key: null, cache: false, expiry: null }
if (!("api_key" in options)){
options.api_key = null;
}
if (!(toType( data ) == "object")){
throw "your data needs to be an object.";
}
var api_key = options.api_key || process.env.BLOCKSPRING_API_KEY || "";
var cache = ("cache" in options) ? options.cache : false;
var expiry = ("expiry" in options) ? options.expiry : null;
var blockspring_url = process.env.BLOCKSPRING_URL || 'https://sender.blockspring.com'
var block = block.split("/").slice(-1)[0];
requests.post({
url: blockspring_url + "/api_v2/blocks/" + block + "?api_key=" + api_key + "&cache=" + cache + "&expiry=" + expiry,
body: data,
json: true
}, function(err, response, body) {
if (callback){
callback(body);
}
});
},
runParsed: function(block, data, options, callback) {
// checks in case callback was provided in place of data or options.
if (typeof data === 'function') {
// allow data and api_key to be optional
callback = data;
data = {};
} else if (typeof options === 'function') {
// allow options to be optional, but keep callback as last argument
callback = options;
} else if (typeof options === "string"){
options = {
api_key: options,
cache: false,
expiry: null
}
}
// checks in case callback was provided in place of data or options.
data = typeof data !== 'undefined' || data === null ? data : {};
options = typeof options !== 'undefined' ? options : { api_key: null, cache: false, expiry: null }
if (!("api_key" in options)){
options.api_key = null;
}
if (!(toType( data ) == "object")){
throw "your data needs to be an object.";
}
var api_key = options.api_key || process.env.BLOCKSPRING_API_KEY || "";
var cache = ("cache" in options) ? options.cache : false;
var expiry = ("expiry" in options) ? options.expiry : null;
var blockspring_url = process.env.BLOCKSPRING_URL || 'https://sender.blockspring.com'
var block = block.split("/").slice(-1)[0];
requests.post({
url: blockspring_url + "/api_v2/blocks/" + block + "?api_key=" + api_key + "&cache=" + cache + "&expiry=" + expiry,
body: data,
json: true
}, function(err, response, body) {
try {
if (!(toType( body ) == "object")){
if (callback){
callback(body);
}
return body;
} else {
body["_headers"] = response.headers;
}
} catch(e) {
if (callback){
callback(body);
}
return body;
};
return this.parse(body, true, callback)
}.bind(this));
},
define: function(block) {
var processStdin = function(callback) {
if (process.stdin.isTTY) {
var request = new Request;
callback(request);
} else {
var stdIn = '';
process.stdin.setEncoding('utf8');
process.stdin.on('readable', function() {
var chunk = process.stdin.read();
if (chunk !== null) {
stdIn = stdIn + chunk;
}
});
process.stdin.on('end', function() {
this.parse(stdIn, false, callback);
}.bind(this));
}
}.bind(this);
var processArgs = function(request, callback) {
for (key in argv) {
if(['_', '$0'].indexOf(key) === -1) {
request.params[key] = argv[key];
}
}
callback(request);
};
// Grab stdin first
processStdin(function(request){
// Then try to get arguments
processArgs(request, function(request){
// call the block when finished
var response = new Response;
block.call({}, request, response);
});
});
}
};
function toType(obj) {
return ({}).toString.call(obj).match(/\s([a-zA-Z]+)/)[1].toLowerCase();
};
function Request(){
this.params = {};
this.stdin = '';
this._errors = [];
this._headers = {};
this.getErrors = function(){
return this._errors;
};
this.addError = function(error){
this._errors.push(error);
};
this.addHeaders = function(headers){
this._headers = headers;
};
this.getHeaders = function(){
return this._headers;
};
}
function Response() {
this.result = {
_blockspring_spec: true,
_errors: []
};
this.addOutput = function(name, value, callback) {
this.result[name] = value;
if(callback) {
process.nextTick(callback);
}
return this;
};
this.addFileOutput = function(name, filepath, callback) {
var filename = path.basename(filepath);
fs.readFile(filepath, { encoding: 'base64' }, function (err, data) {
if (err) throw err;
this.result[name] = {
"filename": filename,
"content-type": mime.lookup(filepath),
"data": data
};
if (callback) {
callback();
}
}.bind(this));
return this;
};
this.addErrorOutput = function(title, message, callback) {
message = typeof message !== 'undefined' ? message : null;
this.result._errors.push({
title: title,
message: message
})
if(callback) {
process.nextTick(callback);
}
return this;
};
this.end = function() {
console.log(JSON.stringify(this.result));
};
}