Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

needle.js 29KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  1. //////////////////////////////////////////
  2. // Needle -- HTTP Client for Node.js
  3. // Written by Tomás Pollak <tomas@forkhq.com>
  4. // (c) 2012-2020 - Fork Ltd.
  5. // MIT Licensed
  6. //////////////////////////////////////////
  7. var fs = require('fs'),
  8. http = require('http'),
  9. https = require('https'),
  10. url = require('url'),
  11. stream = require('stream'),
  12. debug = require('debug')('needle'),
  13. stringify = require('./querystring').build,
  14. multipart = require('./multipart'),
  15. auth = require('./auth'),
  16. cookies = require('./cookies'),
  17. parsers = require('./parsers'),
  18. decoder = require('./decoder');
  19. //////////////////////////////////////////
  20. // variabilia
  21. var version = require('../package.json').version;
  22. var user_agent = 'Needle/' + version;
  23. user_agent += ' (Node.js ' + process.version + '; ' + process.platform + ' ' + process.arch + ')';
  24. var tls_options = 'agent pfx key passphrase cert ca ciphers rejectUnauthorized secureProtocol checkServerIdentity family';
  25. // older versions of node (< 0.11.4) prevent the runtime from exiting
  26. // because of connections in keep-alive state. so if this is the case
  27. // we'll default new requests to set a Connection: close header.
  28. var close_by_default = !http.Agent || http.Agent.defaultMaxSockets != Infinity;
  29. // see if we have Object.assign. otherwise fall back to util._extend
  30. var extend = Object.assign ? Object.assign : require('util')._extend;
  31. // these are the status codes that Needle interprets as redirects.
  32. var redirect_codes = [301, 302, 303, 307, 308];
  33. //////////////////////////////////////////
  34. // decompressors for gzip/deflate/br bodies
  35. function bind_opts(fn, options) {
  36. return fn.bind(null, options);
  37. }
  38. var decompressors = {};
  39. try {
  40. var zlib = require('zlib');
  41. // Enable Z_SYNC_FLUSH to avoid Z_BUF_ERROR errors (Node PR #2595)
  42. var zlib_options = {
  43. flush: zlib.Z_SYNC_FLUSH,
  44. finishFlush: zlib.Z_SYNC_FLUSH
  45. };
  46. var br_options = {
  47. flush: zlib.BROTLI_OPERATION_FLUSH,
  48. finishFlush: zlib.BROTLI_OPERATION_FLUSH
  49. };
  50. decompressors['x-deflate'] = bind_opts(zlib.Inflate, zlib_options);
  51. decompressors['deflate'] = bind_opts(zlib.Inflate, zlib_options);
  52. decompressors['x-gzip'] = bind_opts(zlib.Gunzip, zlib_options);
  53. decompressors['gzip'] = bind_opts(zlib.Gunzip, zlib_options);
  54. if (typeof zlib.BrotliDecompress === 'function') {
  55. decompressors['br'] = bind_opts(zlib.BrotliDecompress, br_options);
  56. }
  57. } catch(e) { /* zlib not available */ }
  58. //////////////////////////////////////////
  59. // options and aliases
  60. var defaults = {
  61. // data
  62. boundary : '--------------------NODENEEDLEHTTPCLIENT',
  63. encoding : 'utf8',
  64. parse_response : 'all', // same as true. valid options: 'json', 'xml' or false/null
  65. proxy : null,
  66. // headers
  67. headers : {},
  68. accept : '*/*',
  69. user_agent : user_agent,
  70. // numbers
  71. open_timeout : 10000,
  72. response_timeout : 0,
  73. read_timeout : 0,
  74. follow_max : 0,
  75. stream_length : -1,
  76. // booleans
  77. compressed : false,
  78. decode_response : true,
  79. parse_cookies : true,
  80. follow_set_cookies : false,
  81. follow_set_referer : false,
  82. follow_keep_method : false,
  83. follow_if_same_host : false,
  84. follow_if_same_protocol : false,
  85. follow_if_same_location : false
  86. }
  87. var aliased = {
  88. options: {
  89. decode : 'decode_response',
  90. parse : 'parse_response',
  91. timeout : 'open_timeout',
  92. follow : 'follow_max'
  93. },
  94. inverted: {}
  95. }
  96. // only once, invert aliased keys so we can get passed options.
  97. Object.keys(aliased.options).map(function(k) {
  98. var value = aliased.options[k];
  99. aliased.inverted[value] = k;
  100. });
  101. //////////////////////////////////////////
  102. // helpers
  103. function keys_by_type(type) {
  104. return Object.keys(defaults).map(function(el) {
  105. if (defaults[el] !== null && defaults[el].constructor == type)
  106. return el;
  107. }).filter(function(el) { return el })
  108. }
  109. function parse_content_type(header) {
  110. if (!header || header === '') return {};
  111. var found, charset = 'utf8', arr = header.split(';');
  112. if (arr.length > 1 && (found = arr[1].match(/charset=(.+)/)))
  113. charset = found[1];
  114. return { type: arr[0], charset: charset };
  115. }
  116. function is_stream(obj) {
  117. return typeof obj.pipe === 'function';
  118. }
  119. function get_stream_length(stream, given_length, cb) {
  120. if (given_length > 0)
  121. return cb(given_length);
  122. if (stream.end !== void 0 && stream.end !== Infinity && stream.start !== void 0)
  123. return cb((stream.end + 1) - (stream.start || 0));
  124. fs.stat(stream.path, function(err, stat) {
  125. cb(stat ? stat.size - (stream.start || 0) : null);
  126. });
  127. }
  128. function resolve_url(href, base) {
  129. if (url.URL)
  130. return new url.URL(href, base);
  131. // older Node version (< v6.13)
  132. return url.resolve(base, href);
  133. }
  134. function pump_streams(streams, cb) {
  135. if (stream.pipeline)
  136. return stream.pipeline.apply(null, streams.concat(cb));
  137. var tmp = streams.shift();
  138. while (streams.length) {
  139. tmp = tmp.pipe(streams.shift());
  140. tmp.once('error', function(e) {
  141. cb && cb(e);
  142. cb = null;
  143. })
  144. }
  145. }
  146. //////////////////////////////////////////
  147. // the main act
  148. function Needle(method, uri, data, options, callback) {
  149. // if (!(this instanceof Needle)) {
  150. // return new Needle(method, uri, data, options, callback);
  151. // }
  152. if (typeof uri !== 'string')
  153. throw new TypeError('URL must be a string, not ' + uri);
  154. this.method = method.toLowerCase();
  155. this.uri = uri;
  156. this.data = data;
  157. if (typeof options == 'function') {
  158. this.callback = options;
  159. this.options = {};
  160. } else {
  161. this.callback = callback;
  162. this.options = options;
  163. }
  164. }
  165. Needle.prototype.setup = function(uri, options) {
  166. function get_option(key, fallback) {
  167. // if original is in options, return that value
  168. if (typeof options[key] != 'undefined') return options[key];
  169. // otherwise, return value from alias or fallback/undefined
  170. return typeof options[aliased.inverted[key]] != 'undefined'
  171. ? options[aliased.inverted[key]] : fallback;
  172. }
  173. function check_value(expected, key) {
  174. var value = get_option(key),
  175. type = typeof value;
  176. if (type != 'undefined' && type != expected)
  177. throw new TypeError(type + ' received for ' + key + ', but expected a ' + expected);
  178. return (type == expected) ? value : defaults[key];
  179. }
  180. //////////////////////////////////////////////////
  181. // the basics
  182. var config = {
  183. http_opts : {
  184. localAddress: get_option('localAddress', undefined),
  185. lookup: get_option('lookup', undefined)
  186. }, // passed later to http.request() directly
  187. headers : {},
  188. output : options.output,
  189. proxy : get_option('proxy', defaults.proxy),
  190. parser : get_option('parse_response', defaults.parse_response),
  191. encoding : options.encoding || (options.multipart ? 'binary' : defaults.encoding)
  192. }
  193. keys_by_type(Boolean).forEach(function(key) {
  194. config[key] = check_value('boolean', key);
  195. })
  196. keys_by_type(Number).forEach(function(key) {
  197. config[key] = check_value('number', key);
  198. })
  199. // populate http_opts with given TLS options
  200. tls_options.split(' ').forEach(function(key) {
  201. if (typeof options[key] != 'undefined') {
  202. config.http_opts[key] = options[key];
  203. if (typeof options.agent == 'undefined')
  204. config.http_opts.agent = false; // otherwise tls options are skipped
  205. }
  206. });
  207. //////////////////////////////////////////////////
  208. // headers, cookies
  209. for (var key in defaults.headers)
  210. config.headers[key] = defaults.headers[key];
  211. config.headers['accept'] = options.accept || defaults.accept;
  212. config.headers['user-agent'] = options.user_agent || defaults.user_agent;
  213. if (options.content_type)
  214. config.headers['content-type'] = options.content_type;
  215. // set connection header if opts.connection was passed, or if node < 0.11.4 (close)
  216. if (options.connection || close_by_default)
  217. config.headers['connection'] = options.connection || 'close';
  218. if ((options.compressed || defaults.compressed) && typeof zlib != 'undefined')
  219. config.headers['accept-encoding'] = decompressors['br'] ? 'gzip, deflate, br' : 'gzip, deflate';
  220. if (options.cookies)
  221. config.headers['cookie'] = cookies.write(options.cookies);
  222. //////////////////////////////////////////////////
  223. // basic/digest auth
  224. if (uri.match(/[^\/]@/)) { // url contains user:pass@host, so parse it.
  225. var parts = (url.parse(uri).auth || '').split(':');
  226. options.username = parts[0];
  227. options.password = parts[1];
  228. }
  229. if (options.username) {
  230. if (options.auth && (options.auth == 'auto' || options.auth == 'digest')) {
  231. config.credentials = [options.username, options.password];
  232. } else {
  233. config.headers['authorization'] = auth.basic(options.username, options.password);
  234. }
  235. }
  236. // if proxy is present, set auth header from either url or proxy_user option.
  237. if (config.proxy) {
  238. if (config.proxy.indexOf('http') === -1)
  239. config.proxy = 'http://' + config.proxy;
  240. if (config.proxy.indexOf('@') !== -1) {
  241. var proxy = (url.parse(config.proxy).auth || '').split(':');
  242. options.proxy_user = proxy[0];
  243. options.proxy_pass = proxy[1];
  244. }
  245. if (options.proxy_user)
  246. config.headers['proxy-authorization'] = auth.basic(options.proxy_user, options.proxy_pass);
  247. }
  248. // now that all our headers are set, overwrite them if instructed.
  249. for (var h in options.headers)
  250. config.headers[h.toLowerCase()] = options.headers[h];
  251. config.uri_modifier = get_option('uri_modifier', null);
  252. return config;
  253. }
  254. Needle.prototype.start = function() {
  255. var out = new stream.PassThrough({ objectMode: false }),
  256. uri = this.uri,
  257. data = this.data,
  258. method = this.method,
  259. callback = (typeof this.options == 'function') ? this.options : this.callback,
  260. options = this.options || {};
  261. // if no 'http' is found on URL, prepend it.
  262. if (uri.indexOf('http') === -1)
  263. uri = uri.replace(/^(\/\/)?/, 'http://');
  264. var self = this, body, waiting = false, config = this.setup(uri, options);
  265. // unless options.json was set to false, assume boss also wants JSON if content-type matches.
  266. var json = options.json || (options.json !== false && config.headers['content-type'] == 'application/json');
  267. if (data) {
  268. if (options.multipart) { // boss says we do multipart. so we do it.
  269. var boundary = options.boundary || defaults.boundary;
  270. waiting = true;
  271. multipart.build(data, boundary, function(err, parts) {
  272. if (err) throw(err);
  273. config.headers['content-type'] = 'multipart/form-data; boundary=' + boundary;
  274. next(parts);
  275. });
  276. } else if (is_stream(data)) {
  277. if (method == 'get')
  278. throw new Error('Refusing to pipe() a stream via GET. Did you mean .post?');
  279. if (config.stream_length > 0 || (config.stream_length === 0 && data.path)) {
  280. // ok, let's get the stream's length and set it as the content-length header.
  281. // this prevents some servers from cutting us off before all the data is sent.
  282. waiting = true;
  283. get_stream_length(data, config.stream_length, function(length) {
  284. data.length = length;
  285. next(data);
  286. })
  287. } else {
  288. // if the boss doesn't want us to get the stream's length, or if it doesn't
  289. // have a file descriptor for that purpose, then just head on.
  290. body = data;
  291. }
  292. } else if (Buffer.isBuffer(data)) {
  293. body = data; // use the raw buffer as request body.
  294. } else if (method == 'get' && !json) {
  295. // append the data to the URI as a querystring.
  296. uri = uri.replace(/\?.*|$/, '?' + stringify(data));
  297. } else { // string or object data, no multipart.
  298. // if string, leave it as it is, otherwise, stringify.
  299. body = (typeof(data) === 'string') ? data
  300. : json ? JSON.stringify(data) : stringify(data);
  301. // ensure we have a buffer so bytecount is correct.
  302. body = Buffer.from(body, config.encoding);
  303. }
  304. }
  305. function next(body) {
  306. if (body) {
  307. if (body.length) config.headers['content-length'] = body.length;
  308. // if no content-type was passed, determine if json or not.
  309. if (!config.headers['content-type']) {
  310. config.headers['content-type'] = json
  311. ? 'application/json; charset=utf-8'
  312. : 'application/x-www-form-urlencoded'; // no charset says W3 spec.
  313. }
  314. }
  315. // unless a specific accept header was set, assume json: true wants JSON back.
  316. if (options.json && (!options.accept && !(options.headers || {}).accept))
  317. config.headers['accept'] = 'application/json';
  318. self.send_request(1, method, uri, config, body, out, callback);
  319. }
  320. if (!waiting) next(body);
  321. return out;
  322. }
  323. Needle.prototype.get_request_opts = function(method, uri, config) {
  324. var opts = config.http_opts,
  325. proxy = config.proxy,
  326. remote = proxy ? url.parse(proxy) : url.parse(uri);
  327. opts.protocol = remote.protocol;
  328. opts.host = remote.hostname;
  329. opts.port = remote.port || (remote.protocol == 'https:' ? 443 : 80);
  330. opts.path = proxy ? uri : remote.pathname + (remote.search || '');
  331. opts.method = method;
  332. opts.headers = config.headers;
  333. if (!opts.headers['host']) {
  334. // if using proxy, make sure the host header shows the final destination
  335. var target = proxy ? url.parse(uri) : remote;
  336. opts.headers['host'] = target.hostname;
  337. // and if a non standard port was passed, append it to the port header
  338. if (target.port && [80, 443].indexOf(target.port) === -1) {
  339. opts.headers['host'] += ':' + target.port;
  340. }
  341. }
  342. return opts;
  343. }
  344. Needle.prototype.should_follow = function(location, config, original) {
  345. if (!location) return false;
  346. // returns true if location contains matching property (host or protocol)
  347. function matches(property) {
  348. var property = original[property];
  349. return location.indexOf(property) !== -1;
  350. }
  351. // first, check whether the requested location is actually different from the original
  352. if (!config.follow_if_same_location && location === original)
  353. return false;
  354. if (config.follow_if_same_host && !matches('host'))
  355. return false; // host does not match, so not following
  356. if (config.follow_if_same_protocol && !matches('protocol'))
  357. return false; // procotol does not match, so not following
  358. return true;
  359. }
  360. Needle.prototype.send_request = function(count, method, uri, config, post_data, out, callback) {
  361. if (typeof config.uri_modifier === 'function') {
  362. var modified_uri = config.uri_modifier(uri);
  363. debug('Modifying request URI', uri + ' => ' + modified_uri);
  364. uri = modified_uri;
  365. }
  366. var request,
  367. timer,
  368. returned = 0,
  369. self = this,
  370. request_opts = this.get_request_opts(method, uri, config),
  371. protocol = request_opts.protocol == 'https:' ? https : http;
  372. function done(err, resp) {
  373. if (returned++ > 0)
  374. return debug('Already finished, stopping here.');
  375. if (timer) clearTimeout(timer);
  376. request.removeListener('error', had_error);
  377. out.done = true;
  378. if (callback)
  379. return callback(err, resp, resp ? resp.body : undefined);
  380. // NOTE: this event used to be called 'end', but the behaviour was confusing
  381. // when errors ocurred, because the stream would still emit an 'end' event.
  382. out.emit('done', err);
  383. // trigger the 'done' event on streams we're being piped to, if any
  384. var pipes = out._readableState.pipes || [];
  385. if (!pipes.forEach) pipes = [pipes];
  386. pipes.forEach(function(st) { st.emit('done', err); })
  387. }
  388. function had_error(err) {
  389. debug('Request error', err);
  390. out.emit('err', err);
  391. done(err || new Error('Unknown error when making request.'));
  392. }
  393. function set_timeout(type, milisecs) {
  394. if (timer) clearTimeout(timer);
  395. if (milisecs <= 0) return;
  396. timer = setTimeout(function() {
  397. out.emit('timeout', type);
  398. request.abort();
  399. // also invoke done() to terminate job on read_timeout
  400. if (type == 'read') done(new Error(type + ' timeout'));
  401. }, milisecs);
  402. }
  403. // handle errors on the underlying socket, that may be closed while writing
  404. // for an example case, see test/long_string_spec.js. we make sure this
  405. // scenario ocurred by verifying the socket's writable & destroyed states.
  406. function on_socket_end() {
  407. if (returned && !this.writable && this.destroyed === false) {
  408. this.destroy();
  409. had_error(new Error('Remote end closed socket abruptly.'))
  410. }
  411. }
  412. debug('Making request #' + count, request_opts);
  413. request = protocol.request(request_opts, function(resp) {
  414. var headers = resp.headers;
  415. debug('Got response', resp.statusCode, headers);
  416. out.emit('response', resp);
  417. set_timeout('read', config.read_timeout);
  418. // if we got cookies, parse them unless we were instructed not to. make sure to include any
  419. // cookies that might have been set on previous redirects.
  420. if (config.parse_cookies && (headers['set-cookie'] || config.previous_resp_cookies)) {
  421. resp.cookies = extend(config.previous_resp_cookies || {}, cookies.read(headers['set-cookie']));
  422. debug('Got cookies', resp.cookies);
  423. }
  424. // if redirect code is found, determine if we should follow it according to the given options.
  425. if (redirect_codes.indexOf(resp.statusCode) !== -1 && self.should_follow(headers.location, config, uri)) {
  426. // clear timer before following redirects to prevent unexpected setTimeout consequence
  427. clearTimeout(timer);
  428. if (count <= config.follow_max) {
  429. out.emit('redirect', headers.location);
  430. // unless 'follow_keep_method' is true, rewrite the request to GET before continuing.
  431. if (!config.follow_keep_method) {
  432. method = 'GET';
  433. post_data = null;
  434. delete config.headers['content-length']; // in case the original was a multipart POST request.
  435. }
  436. // if follow_set_cookies is true, insert cookies in the next request's headers.
  437. // we set both the original request cookies plus any response cookies we might have received.
  438. if (config.follow_set_cookies) {
  439. var request_cookies = cookies.read(config.headers['cookie']);
  440. config.previous_resp_cookies = resp.cookies;
  441. if (Object.keys(request_cookies).length || Object.keys(resp.cookies || {}).length) {
  442. config.headers['cookie'] = cookies.write(extend(request_cookies, resp.cookies));
  443. }
  444. } else if (config.headers['cookie']) {
  445. debug('Clearing original request cookie', config.headers['cookie']);
  446. delete config.headers['cookie'];
  447. }
  448. if (config.follow_set_referer)
  449. config.headers['referer'] = encodeURI(uri); // the original, not the destination URL.
  450. config.headers['host'] = null; // clear previous Host header to avoid conflicts.
  451. var redirect_url = resolve_url(headers.location, uri);
  452. debug('Redirecting to ' + redirect_url.toString());
  453. return self.send_request(++count, method, redirect_url.toString(), config, post_data, out, callback);
  454. } else if (config.follow_max > 0) {
  455. return done(new Error('Max redirects reached. Possible loop in: ' + headers.location));
  456. }
  457. }
  458. // if auth is requested and credentials were not passed, resend request, provided we have user/pass.
  459. if (resp.statusCode == 401 && headers['www-authenticate'] && config.credentials) {
  460. if (!config.headers['authorization']) { // only if authentication hasn't been sent
  461. var auth_header = auth.header(headers['www-authenticate'], config.credentials, request_opts);
  462. if (auth_header) {
  463. config.headers['authorization'] = auth_header;
  464. return self.send_request(count, method, uri, config, post_data, out, callback);
  465. }
  466. }
  467. }
  468. // ok, so we got a valid (non-redirect & authorized) response. let's notify the stream guys.
  469. out.emit('header', resp.statusCode, headers);
  470. out.emit('headers', headers);
  471. var pipeline = [],
  472. mime = parse_content_type(headers['content-type']),
  473. text_response = mime.type && (mime.type.indexOf('text/') != -1 || !!mime.type.match(/(\/|\+)(xml|json)$/));
  474. // To start, if our body is compressed and we're able to inflate it, do it.
  475. if (headers['content-encoding'] && decompressors[headers['content-encoding']]) {
  476. var decompressor = decompressors[headers['content-encoding']]();
  477. // make sure we catch errors triggered by the decompressor.
  478. decompressor.on('error', had_error);
  479. pipeline.push(decompressor);
  480. }
  481. // If parse is enabled and we have a parser for it, then go for it.
  482. if (config.parser && parsers[mime.type]) {
  483. // If a specific parser was requested, make sure we don't parse other types.
  484. var parser_name = config.parser.toString().toLowerCase();
  485. if (['xml', 'json'].indexOf(parser_name) == -1 || parsers[mime.type].name == parser_name) {
  486. // OK, so either we're parsing all content types or the one requested matches.
  487. out.parser = parsers[mime.type].name;
  488. pipeline.push(parsers[mime.type].fn());
  489. // Set objectMode on out stream to improve performance.
  490. out._writableState.objectMode = true;
  491. out._readableState.objectMode = true;
  492. }
  493. // If we're not parsing, and unless decoding was disabled, we'll try
  494. // decoding non UTF-8 bodies to UTF-8, using the iconv-lite library.
  495. } else if (text_response && config.decode_response && mime.charset) {
  496. pipeline.push(decoder(mime.charset));
  497. }
  498. // And `out` is the stream we finally push the decoded/parsed output to.
  499. pipeline.push(out);
  500. // Now, release the kraken!
  501. pump_streams([resp].concat(pipeline), function(err) {
  502. if (err) debug(err)
  503. // on node v8.x, if an error ocurrs on the receiving end,
  504. // then we want to abort the request to avoid having dangling sockets
  505. if (err && err.message == 'write after end') request.destroy();
  506. });
  507. // If the user has requested and output file, pipe the output stream to it.
  508. // In stream mode, we will still get the response stream to play with.
  509. if (config.output && resp.statusCode == 200) {
  510. // for some reason, simply piping resp to the writable stream doesn't
  511. // work all the time (stream gets cut in the middle with no warning).
  512. // so we'll manually need to do the readable/write(chunk) trick.
  513. var file = fs.createWriteStream(config.output);
  514. file.on('error', had_error);
  515. out.on('end', function() {
  516. if (file.writable) file.end();
  517. });
  518. file.on('close', function() {
  519. delete out.file;
  520. })
  521. out.on('readable', function() {
  522. var chunk;
  523. while ((chunk = this.read()) !== null) {
  524. if (file.writable) file.write(chunk);
  525. // if callback was requested, also push it to resp.body
  526. if (resp.body) resp.body.push(chunk);
  527. }
  528. })
  529. out.file = file;
  530. }
  531. // Only aggregate the full body if a callback was requested.
  532. if (callback) {
  533. resp.raw = [];
  534. resp.body = [];
  535. resp.bytes = 0;
  536. // Gather and count the amount of (raw) bytes using a PassThrough stream.
  537. var clean_pipe = new stream.PassThrough();
  538. clean_pipe.on('readable', function() {
  539. var chunk;
  540. while ((chunk = this.read()) != null) {
  541. resp.bytes += chunk.length;
  542. resp.raw.push(chunk);
  543. }
  544. })
  545. pump_streams([resp, clean_pipe], function(err) {
  546. if (err) debug(err);
  547. });
  548. // Listen on the 'readable' event to aggregate the chunks, but only if
  549. // file output wasn't requested. Otherwise we'd have two stream readers.
  550. if (!config.output || resp.statusCode != 200) {
  551. out.on('readable', function() {
  552. var chunk;
  553. while ((chunk = this.read()) !== null) {
  554. // We're either pushing buffers or objects, never strings.
  555. if (typeof chunk == 'string') chunk = Buffer.from(chunk);
  556. // Push all chunks to resp.body. We'll bind them in resp.end().
  557. resp.body.push(chunk);
  558. }
  559. })
  560. }
  561. }
  562. // And set the .body property once all data is in.
  563. out.on('end', function() {
  564. if (resp.body) { // callback mode
  565. // we want to be able to access to the raw data later, so keep a reference.
  566. resp.raw = Buffer.concat(resp.raw);
  567. // if parse was successful, we should have an array with one object
  568. if (resp.body[0] !== undefined && !Buffer.isBuffer(resp.body[0])) {
  569. // that's our body right there.
  570. resp.body = resp.body[0];
  571. // set the parser property on our response. we may want to check.
  572. if (out.parser) resp.parser = out.parser;
  573. } else { // we got one or several buffers. string or binary.
  574. resp.body = Buffer.concat(resp.body);
  575. // if we're here and parsed is true, it means we tried to but it didn't work.
  576. // so given that we got a text response, let's stringify it.
  577. if (text_response || out.parser) {
  578. resp.body = resp.body.toString();
  579. }
  580. }
  581. }
  582. // if an output file is being written to, make sure the callback
  583. // is triggered after all data has been written to it.
  584. if (out.file) {
  585. out.file.on('close', function() {
  586. done(null, resp);
  587. })
  588. } else { // elvis has left the building.
  589. done(null, resp);
  590. }
  591. });
  592. // out.on('error', function(err) {
  593. // had_error(err);
  594. // if (err.code == 'ERR_STREAM_DESTROYED' || err.code == 'ERR_STREAM_PREMATURE_CLOSE') {
  595. // request.abort();
  596. // }
  597. // })
  598. }); // end request call
  599. // unless open_timeout was disabled, set a timeout to abort the request.
  600. set_timeout('open', config.open_timeout);
  601. // handle errors on the request object. things might get bumpy.
  602. request.on('error', had_error);
  603. // make sure timer is cleared if request is aborted (issue #257)
  604. request.once('abort', function() {
  605. if (timer) clearTimeout(timer);
  606. })
  607. // handle socket 'end' event to ensure we don't get delayed EPIPE errors.
  608. request.once('socket', function(socket) {
  609. if (socket.connecting) {
  610. socket.once('connect', function() {
  611. set_timeout('response', config.response_timeout);
  612. })
  613. } else {
  614. set_timeout('response', config.response_timeout);
  615. }
  616. // socket.once('close', function(e) {
  617. // console.log('socket closed!', e);
  618. // })
  619. if (!socket.on_socket_end) {
  620. socket.on_socket_end = on_socket_end;
  621. socket.once('end', function() { process.nextTick(on_socket_end.bind(socket)) });
  622. }
  623. })
  624. if (post_data) {
  625. if (is_stream(post_data)) {
  626. pump_streams([post_data, request], function(err) {
  627. if (err) debug(err);
  628. });
  629. } else {
  630. request.write(post_data, config.encoding);
  631. request.end();
  632. }
  633. } else {
  634. request.end();
  635. }
  636. out.abort = function() { request.abort() }; // easier access
  637. out.request = request;
  638. return out;
  639. }
  640. //////////////////////////////////////////
  641. // exports
  642. if (typeof Promise !== 'undefined') {
  643. module.exports = function() {
  644. var verb, args = [].slice.call(arguments);
  645. if (args[0].match(/\.|\//)) // first argument looks like a URL
  646. verb = (args.length > 2) ? 'post' : 'get';
  647. else
  648. verb = args.shift();
  649. if (verb.match(/get|head/i) && args.length == 2)
  650. args.splice(1, 0, null); // assume no data if head/get with two args (url, options)
  651. return new Promise(function(resolve, reject) {
  652. module.exports.request(verb, args[0], args[1], args[2], function(err, resp) {
  653. return err ? reject(err) : resolve(resp);
  654. });
  655. })
  656. }
  657. }
  658. module.exports.version = version;
  659. module.exports.defaults = function(obj) {
  660. for (var key in obj) {
  661. var target_key = aliased.options[key] || key;
  662. if (defaults.hasOwnProperty(target_key) && typeof obj[key] != 'undefined') {
  663. if (target_key != 'parse_response' && target_key != 'proxy') {
  664. // ensure type matches the original, except for proxy/parse_response that can be null/bool or string
  665. var valid_type = defaults[target_key].constructor.name;
  666. if (obj[key].constructor.name != valid_type)
  667. throw new TypeError('Invalid type for ' + key + ', should be ' + valid_type);
  668. }
  669. defaults[target_key] = obj[key];
  670. } else {
  671. throw new Error('Invalid property for defaults:' + target_key);
  672. }
  673. }
  674. return defaults;
  675. }
  676. 'head get'.split(' ').forEach(function(method) {
  677. module.exports[method] = function(uri, options, callback) {
  678. return new Needle(method, uri, null, options, callback).start();
  679. }
  680. })
  681. 'post put patch delete'.split(' ').forEach(function(method) {
  682. module.exports[method] = function(uri, data, options, callback) {
  683. return new Needle(method, uri, data, options, callback).start();
  684. }
  685. })
  686. module.exports.request = function(method, uri, data, opts, callback) {
  687. return new Needle(method, uri, data, opts, callback).start();
  688. };