-
Notifications
You must be signed in to change notification settings - Fork 0
/
hitit.js
executable file
·242 lines (207 loc) · 8.72 KB
/
hitit.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
/*
hitit - v1.2.8
Written by Federico Pereiro (fpereiro@gmail.com) and released into the public domain.
*/
(function () {
// *** SETUP ***
var fs = require ('fs');
var http = require ('http');
var https = require ('https');
var Path = require ('path');
var dale = require ('dale');
var mime = require ('mime');
var teishi = require ('teishi');
var type = teishi.type, log = teishi.clog;
var h = exports;
h.one = function (state, o, cb) {
var resolve = function (w, copy) {
return type (w) === 'function' ? w (state) : (copy ? teishi.copy (w) : w);
}
if (type (o) === 'object' && type (state) === 'object') {
dale.go (['tag', 'host', 'port', 'method', 'path', 'headers', 'body', 'code', 'apres', 'delay', 'timeout', 'https', 'rejectUnauthorized'], function (k) {
if (k === 'apres') return;
if (o [k] === undefined) o [k] = resolve (state [k], k === 'body');
else o [k] = resolve (o [k]);
});
o.apres = o.apres || state.apres;
}
if (teishi.stop ([
['state', state, 'object'],
['options', o, 'object'],
['cb', cb, ['function', 'undefined'], 'oneOf'],
function () {return dale.go ({
string: ['tag', 'host', 'method', 'path'],
integer: ['port', 'delay', 'timeout'],
boolean: ['https', 'rejectUnauthorized', 'raw'],
function: 'apres',
headers: 'object'
}, function (keys, type) {
return dale.go (keys, function (key) {
return ['options.' + key, o [key], [type, 'undefined'], 'oneOf']
})
})},
function () {return [
['o.code', o.code, ['*', undefined, 0, -1].concat (dale.go (http.STATUS_CODES, function (v, k) {return parseInt (k)})), teishi.test.equal, 'oneOf'],
[o.port !== undefined, ['options.port', o.port, {min: 1, max: 65535}, teishi.test.range]],
[o.method !== undefined, ['options.method', (o.method || '' ).toLowerCase (), ['get', 'head', 'post', 'put', 'delete', 'trace', 'connect', 'patch', 'options'], teishi.test.equal, 'oneOf']],
]},
], function (error) {
if (type (cb) === 'function') cb ({
code: -2,
error: error
});
else log (error);
})) return;
cb = cb || log;
o.headers = dale.obj (o.headers || {}, teishi.copy (state.headers) || {}, function (v, k) {return [k, v]});
var multipart = type (o.body) === 'object' && o.body.multipart;
if (multipart) {
var boundary = Math.floor (Math.random () * Math.pow (10, 16));
o.headers ['content-type'] = 'multipart/form-data; boundary=' + boundary;
}
else if (teishi.complex (o.body)) {
o.body = JSON.stringify (o.body);
if (! o.headers ['content-type']) o.headers ['content-type'] = 'application/json';
}
else if (o.body === undefined) o.body = '';
else o.body = o.body + '';
if (o.path [0] !== '/') o.path = '/' + o.path;
var startTime = Date.now ();
var request = (o.https ? https : http).request ({
port: o.port,
hostname: o.host,
method: o.method,
headers: o.headers,
path: o.path,
rejectUnauthorized: ! o.rejectUnauthorized === false
}, function (response) {
if (! o.raw) {
response.setEncoding ('utf8');
response.body = '';
}
else response.body = [];
response.on ('data', function (buffer) {
! o.raw ? response.body += buffer.toString () : response.body.push (buffer);
});
response.on ('end', function () {
var rdata = {
code: response.statusCode,
headers: response.headers,
body: ! o.raw ? response.body : Buffer.concat (response.body),
time: [startTime, Date.now ()],
request: o
}
if (! o.raw) {
var parsed;
if ((response.headers ['content-type'] || '').match (/^application\/json/)) {
parsed = teishi.parse (response.body);
if (parsed === false) return cb (dale.obj (0, rdata, function () {
return ['error', 'Invalid JSON!'];
}));
rdata.body = parsed;
}
}
if (o.code !== '*' && rdata.code !== (o.code || 200)) return cb (rdata);
setTimeout (function () {
if (o.apres) {
var result = o.apres (state, o, rdata, cb);
if (result === undefined) return;
return result ? cb (null, rdata) : cb (rdata);
}
cb (null, rdata);
}, o.delay || 0);
});
response.on ('error', function (error) {
cb ({code: 0, error: error.toString (), request: o});
});
});
var timeout;
if (o.timeout === undefined) o.timeout = 60;
request.setTimeout (o.timeout * 1000, function () {
timeout = true;
request.abort ();
cb ({code: 0, error: 'Timed out after ' + o.timeout + ' seconds, request aborted.', request: o});
});
request.on ('error', function (error) {
if (! timeout) cb ({code: -1, error: error.toString (), request: o});
});
if (! multipart) request.end (o.body);
else {
var content = type (o.body.multipart) === 'array' ? o.body.multipart : [o.body.multipart];
var queue = [], counter = 1, rwrite = function (what, enc, p) {
if (p === undefined) {
p = counter++;
queue.push (p);
}
if (Math.min.apply (Math, queue) === p) {
var cb = function () {
queue.splice (queue.indexOf (p), 1);
if (queue.length === 0) request.end ();
}
if (what.length === 0) cb ();
else request.write (what, enc, cb);
}
else setTimeout (function () {
rwrite (what, enc, p);
}, 1);
}
// https://github.com/mscdex/busboy/blob/master/lib/types/multipart.js#L174
dale.go (content, function (v) {
if (type (v) !== 'object') return log ('Invalid multipart file or field!', v);
if (v.path && ! v.filename) v.filename = Path.basename (v.path);
rwrite ('--' + boundary + '\r\n' + 'Content-Disposition: form-data; name="' + v.name + '";');
if (v.filename) rwrite (' filename="' + encodeURIComponent (v.filename) + '"');
var contentType = v.contentType || (v.path ? mime.lookup (v.path) : (teishi.complex (v.value) ? 'application/json' : 'text/plain'));
if (contentType !== 'application/octet-stream') rwrite ('\r\nContent-Type: ' + contentType + '; charset=utf-8');
rwrite ('\r\n\r\n');
rwrite (v.path ? fs.readFileSync (v.path, 'binary') : (teishi.complex (v.value) ? teishi.str (v.value) : v.value + ''), v.path ? 'binary' : 'utf8');
rwrite ('\r\n');
});
rwrite ('--' + boundary + '--\r\n');
}
}
h.seq = function (state, seq, cb, map) {
if (teishi.stop ([
['state', state, 'object'],
['sequence', seq, 'array'],
['cb', cb, 'function'],
['map', map, ['function', 'undefined'], 'oneOf'],
], cb)) return;
if (seq.length === 0) return cb (null, []);
var fseq = [];
var counter = 0;
var hist = [];
var preproc = function (seq) {
dale.go (seq, function (v) {
if (! v || (type (v) === 'array' && v.length === 0)) return;
if (type (v) === 'array' && teishi.complex (v [0])) preproc (v);
else fseq.push (map ? map (v) : v);
});
}
preproc (seq);
var t = Date.now ();
var CB = function () {
log ('Starting request (' + (Math.round ((Date.now () - t) / 10) / 100) + 's)', fseq [counter].tag, '(' + (counter + 1) + '/' + fseq.length + ')');
h.one (state, fseq [counter++], function (error, rdata) {
if (error) return cb (error, hist);
hist.push (rdata);
if (counter < fseq.length) CB ();
else cb (null, hist);
});
}
CB ();
}
h.stdmap = function (req) {
if (type (req) !== 'array') return log ('Request passed to h.stdmap must be an array!');
return {
tag: req [0],
method: req [1],
path: req [2],
headers: req [3],
body: req [4],
code: req [5],
apres: req [6],
delay: req [7]
}
}
}) ();