xhr.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  1. 'use strict';
  2. var util = require('util');
  3. var urlutil = require('url');
  4. var http = require('http');
  5. var https = require('https');
  6. var debug = require('debug')('urllib');
  7. var ms = require('humanize-ms');
  8. var _Promise;
  9. var REQUEST_ID = 0;
  10. var MAX_VALUE = Math.pow(2, 31) - 10;
  11. var PROTO_RE = /^https?:\/\//i;
  12. function getAgent(agent, defaultAgent) {
  13. return agent === undefined ? defaultAgent : agent;
  14. }
  15. function makeCallback(resolve, reject) {
  16. return function (err, data, res) {
  17. if (err) {
  18. return reject(err);
  19. }
  20. resolve({
  21. data: data,
  22. status: res.statusCode,
  23. headers: res.headers,
  24. res: res
  25. });
  26. };
  27. }
  28. // exports.TIMEOUT = ms('5s');
  29. exports.TIMEOUTS = [ms('300s'), ms('300s')];
  30. var TEXT_DATA_TYPES = [
  31. 'json',
  32. 'text'
  33. ];
  34. exports.request = function request(url, args, callback) {
  35. // request(url, callback)
  36. if (arguments.length === 2 && typeof args === 'function') {
  37. callback = args;
  38. args = null;
  39. }
  40. if (typeof callback === 'function') {
  41. return exports.requestWithCallback(url, args, callback);
  42. }
  43. return new Promise(function (resolve, reject) {
  44. exports.requestWithCallback(url, args, makeCallback(resolve, reject));
  45. });
  46. };
  47. exports.requestWithCallback = function requestWithCallback(url, args, callback) {
  48. // requestWithCallback(url, callback)
  49. if (!url || (typeof url !== 'string' && typeof url !== 'object')) {
  50. var msg = util.format('expect request url to be a string or a http request options, but got %j', url);
  51. throw new Error(msg);
  52. }
  53. if (arguments.length === 2 && typeof args === 'function') {
  54. callback = args;
  55. args = null;
  56. }
  57. args = args || {};
  58. if (REQUEST_ID >= MAX_VALUE) {
  59. REQUEST_ID = 0;
  60. }
  61. var reqId = ++REQUEST_ID;
  62. args.requestUrls = args.requestUrls || [];
  63. var reqMeta = {
  64. requestId: reqId,
  65. url: url,
  66. args: args,
  67. ctx: args.ctx,
  68. };
  69. if (args.emitter) {
  70. args.emitter.emit('request', reqMeta);
  71. }
  72. args.timeout = args.timeout || exports.TIMEOUTS;
  73. args.maxRedirects = args.maxRedirects || 10;
  74. args.streaming = args.streaming || args.customResponse;
  75. var requestStartTime = Date.now();
  76. var parsedUrl;
  77. if (typeof url === 'string') {
  78. if (!PROTO_RE.test(url)) {
  79. // Support `request('www.server.com')`
  80. url = 'http://' + url;
  81. }
  82. parsedUrl = urlutil.parse(url);
  83. } else {
  84. parsedUrl = url;
  85. }
  86. var method = (args.type || args.method || parsedUrl.method || 'GET').toUpperCase();
  87. var port = parsedUrl.port || 80;
  88. var httplib = http;
  89. var agent = getAgent(args.agent, exports.agent);
  90. var fixJSONCtlChars = args.fixJSONCtlChars;
  91. if (parsedUrl.protocol === 'https:') {
  92. httplib = https;
  93. agent = getAgent(args.httpsAgent, exports.httpsAgent);
  94. if (!parsedUrl.port) {
  95. port = 443;
  96. }
  97. }
  98. // request through proxy tunnel
  99. // var proxyTunnelAgent = detectProxyAgent(parsedUrl, args);
  100. // if (proxyTunnelAgent) {
  101. // agent = proxyTunnelAgent;
  102. // }
  103. var options = {
  104. host: parsedUrl.hostname || parsedUrl.host || 'localhost',
  105. path: parsedUrl.path || '/',
  106. method: method,
  107. port: port,
  108. agent: agent,
  109. headers: args.headers || {},
  110. // default is dns.lookup
  111. // https://github.com/nodejs/node/blob/master/lib/net.js#L986
  112. // custom dnslookup require node >= 4.0.0
  113. // https://github.com/nodejs/node/blob/archived-io.js-v0.12/lib/net.js#L952
  114. lookup: args.lookup,
  115. };
  116. if (Array.isArray(args.timeout)) {
  117. options.requestTimeout = args.timeout[args.timeout.length - 1];
  118. } else if (typeof args.timeout !== 'undefined') {
  119. options.requestTimeout = args.timeout;
  120. }
  121. var sslNames = [
  122. 'pfx',
  123. 'key',
  124. 'passphrase',
  125. 'cert',
  126. 'ca',
  127. 'ciphers',
  128. 'rejectUnauthorized',
  129. 'secureProtocol',
  130. 'secureOptions',
  131. ];
  132. for (var i = 0; i < sslNames.length; i++) {
  133. var name = sslNames[i];
  134. if (args.hasOwnProperty(name)) {
  135. options[name] = args[name];
  136. }
  137. }
  138. // don't check ssl
  139. if (options.rejectUnauthorized === false && !options.hasOwnProperty('secureOptions')) {
  140. options.secureOptions = require('constants').SSL_OP_NO_TLSv1_2;
  141. }
  142. var auth = args.auth || parsedUrl.auth;
  143. if (auth) {
  144. options.auth = auth;
  145. }
  146. var body = args.content || args.data;
  147. var dataAsQueryString = method === 'GET' || method === 'HEAD' || args.dataAsQueryString;
  148. if (!args.content) {
  149. if (body && !(typeof body === 'string' || Buffer.isBuffer(body))) {
  150. if (dataAsQueryString) {
  151. // read: GET, HEAD, use query string
  152. body = args.nestedQuerystring ? qs.stringify(body) : querystring.stringify(body);
  153. } else {
  154. var contentType = options.headers['Content-Type'] || options.headers['content-type'];
  155. // auto add application/x-www-form-urlencoded when using urlencode form request
  156. if (!contentType) {
  157. if (args.contentType === 'json') {
  158. contentType = 'application/json';
  159. } else {
  160. contentType = 'application/x-www-form-urlencoded';
  161. }
  162. options.headers['Content-Type'] = contentType;
  163. }
  164. if (parseContentType(contentType).type === 'application/json') {
  165. body = JSON.stringify(body);
  166. } else {
  167. // 'application/x-www-form-urlencoded'
  168. body = args.nestedQuerystring ? qs.stringify(body) : querystring.stringify(body);
  169. }
  170. }
  171. }
  172. }
  173. // if it's a GET or HEAD request, data should be sent as query string
  174. if (dataAsQueryString && body) {
  175. options.path += (parsedUrl.query ? '&' : '?') + body;
  176. body = null;
  177. }
  178. var requestSize = 0;
  179. if (body) {
  180. var length = body.length;
  181. if (!Buffer.isBuffer(body)) {
  182. length = Buffer.byteLength(body);
  183. }
  184. requestSize = options.headers['Content-Length'] = length;
  185. }
  186. if (args.dataType === 'json') {
  187. options.headers.Accept = 'application/json';
  188. }
  189. if (typeof args.beforeRequest === 'function') {
  190. // you can use this hook to change every thing.
  191. args.beforeRequest(options);
  192. }
  193. var connectTimer = null;
  194. var responseTimer = null;
  195. var __err = null;
  196. var connected = false; // socket connected or not
  197. var keepAliveSocket = false; // request with keepalive socket
  198. var responseSize = 0;
  199. var statusCode = -1;
  200. var responseAborted = false;
  201. var remoteAddress = '';
  202. var remotePort = '';
  203. var timing = null;
  204. if (args.timing) {
  205. timing = {
  206. // socket assigned
  207. queuing: 0,
  208. // dns lookup time
  209. dnslookup: 0,
  210. // socket connected
  211. connected: 0,
  212. // request sent
  213. requestSent: 0,
  214. // Time to first byte (TTFB)
  215. waiting: 0,
  216. contentDownload: 0,
  217. };
  218. }
  219. function cancelConnectTimer() {
  220. if (connectTimer) {
  221. clearTimeout(connectTimer);
  222. connectTimer = null;
  223. }
  224. }
  225. function cancelResponseTimer() {
  226. if (responseTimer) {
  227. clearTimeout(responseTimer);
  228. responseTimer = null;
  229. }
  230. }
  231. function done(err, data, res) {
  232. cancelResponseTimer();
  233. if (!callback) {
  234. console.warn('[urllib:warn] [%s] [%s] [worker:%s] %s %s callback twice!!!',
  235. Date(), reqId, process.pid, options.method, url);
  236. // https://github.com/node-modules/urllib/pull/30
  237. if (err) {
  238. console.warn('[urllib:warn] [%s] [%s] [worker:%s] %s: %s\nstack: %s',
  239. Date(), reqId, process.pid, err.name, err.message, err.stack);
  240. }
  241. return;
  242. }
  243. var cb = callback;
  244. callback = null;
  245. var headers = {};
  246. if (res) {
  247. statusCode = res.statusCode;
  248. headers = res.headers;
  249. }
  250. // handle digest auth
  251. if (statusCode === 401 && headers['www-authenticate']
  252. && (!args.headers || !args.headers.Authorization) && args.digestAuth) {
  253. var authenticate = headers['www-authenticate'];
  254. if (authenticate.indexOf('Digest ') >= 0) {
  255. debug('Request#%d %s: got digest auth header WWW-Authenticate: %s', reqId, url, authenticate);
  256. args.headers = args.headers || {};
  257. args.headers.Authorization = digestAuthHeader(options.method, options.path, authenticate, args.digestAuth);
  258. debug('Request#%d %s: auth with digest header: %s', reqId, url, args.headers.Authorization);
  259. if (res.headers['set-cookie']) {
  260. args.headers.Cookie = res.headers['set-cookie'].join(';');
  261. }
  262. return exports.requestWithCallback(url, args, cb);
  263. }
  264. }
  265. var requestUseTime = Date.now() - requestStartTime;
  266. if (timing) {
  267. timing.contentDownload = requestUseTime;
  268. }
  269. debug('[%sms] done, %s bytes HTTP %s %s %s %s, keepAliveSocket: %s, timing: %j',
  270. requestUseTime, responseSize, statusCode, options.method, options.host, options.path,
  271. keepAliveSocket, timing);
  272. var response = {
  273. status: statusCode,
  274. statusCode: statusCode,
  275. headers: headers,
  276. size: responseSize,
  277. aborted: responseAborted,
  278. rt: requestUseTime,
  279. keepAliveSocket: keepAliveSocket,
  280. data: data,
  281. requestUrls: args.requestUrls,
  282. timing: timing,
  283. remoteAddress: remoteAddress,
  284. remotePort: remotePort,
  285. };
  286. if (err) {
  287. var agentStatus = '';
  288. if (agent && typeof agent.getCurrentStatus === 'function') {
  289. // add current agent status to error message for logging and debug
  290. agentStatus = ', agent status: ' + JSON.stringify(agent.getCurrentStatus());
  291. }
  292. err.message += ', ' + options.method + ' ' + url + ' ' + statusCode
  293. + ' (connected: ' + connected + ', keepalive socket: ' + keepAliveSocket + agentStatus + ')'
  294. + '\nheaders: ' + JSON.stringify(headers);
  295. err.data = data;
  296. err.path = options.path;
  297. err.status = statusCode;
  298. err.headers = headers;
  299. err.res = response;
  300. }
  301. cb(err, data, args.streaming ? res : response);
  302. if (args.emitter) {
  303. // keep to use the same reqMeta object on request event before
  304. reqMeta.url = url;
  305. reqMeta.socket = req && req.connection;
  306. reqMeta.options = options;
  307. reqMeta.size = requestSize;
  308. args.emitter.emit('response', {
  309. requestId: reqId,
  310. error: err,
  311. ctx: args.ctx,
  312. req: reqMeta,
  313. res: response,
  314. });
  315. }
  316. }
  317. function handleRedirect(res) {
  318. var err = null;
  319. if (args.followRedirect && statuses.redirect[res.statusCode]) { // handle redirect
  320. args._followRedirectCount = (args._followRedirectCount || 0) + 1;
  321. var location = res.headers.location;
  322. if (!location) {
  323. err = new Error('Got statusCode ' + res.statusCode + ' but cannot resolve next location from headers');
  324. err.name = 'FollowRedirectError';
  325. } else if (args._followRedirectCount > args.maxRedirects) {
  326. err = new Error('Exceeded maxRedirects. Probably stuck in a redirect loop ' + url);
  327. err.name = 'MaxRedirectError';
  328. } else {
  329. var newUrl = args.formatRedirectUrl ? args.formatRedirectUrl(url, location) : urlutil.resolve(url, location);
  330. debug('Request#%d %s: `redirected` from %s to %s', reqId, options.path, url, newUrl);
  331. // make sure timer stop
  332. cancelResponseTimer();
  333. // should clean up headers.Host on `location: http://other-domain/url`
  334. if (args.headers && args.headers.Host && PROTO_RE.test(location)) {
  335. args.headers.Host = null;
  336. }
  337. // avoid done will be execute in the future change.
  338. var cb = callback;
  339. callback = null;
  340. exports.requestWithCallback(newUrl, args, cb);
  341. return {
  342. redirect: true,
  343. error: null
  344. };
  345. }
  346. }
  347. return {
  348. redirect: false,
  349. error: err
  350. };
  351. }
  352. if (args.gzip) {
  353. if (!options.headers['Accept-Encoding'] && !options.headers['accept-encoding']) {
  354. options.headers['Accept-Encoding'] = 'gzip';
  355. }
  356. }
  357. function decodeContent(res, body, cb) {
  358. var encoding = res.headers['content-encoding'];
  359. // if (body.length === 0) {
  360. // return cb(null, body, encoding);
  361. // }
  362. // if (!encoding || encoding.toLowerCase() !== 'gzip') {
  363. return cb(null, body, encoding);
  364. // }
  365. // debug('gunzip %d length body', body.length);
  366. // zlib.gunzip(body, cb);
  367. }
  368. var writeStream = args.writeStream;
  369. debug('Request#%d %s %s with headers %j, options.path: %s',
  370. reqId, method, url, options.headers, options.path);
  371. args.requestUrls.push(url);
  372. function onResponse(res) {
  373. if (timing) {
  374. timing.waiting = Date.now() - requestStartTime;
  375. }
  376. debug('Request#%d %s `req response` event emit: status %d, headers: %j',
  377. reqId, url, res.statusCode, res.headers);
  378. if (args.streaming) {
  379. var result = handleRedirect(res);
  380. if (result.redirect) {
  381. res.resume();
  382. return;
  383. }
  384. if (result.error) {
  385. res.resume();
  386. return done(result.error, null, res);
  387. }
  388. return done(null, null, res);
  389. }
  390. res.on('close', function () {
  391. debug('Request#%d %s: `res close` event emit, total size %d',
  392. reqId, url, responseSize);
  393. });
  394. res.on('error', function () {
  395. debug('Request#%d %s: `res error` event emit, total size %d',
  396. reqId, url, responseSize);
  397. });
  398. res.on('aborted', function () {
  399. responseAborted = true;
  400. debug('Request#%d %s: `res aborted` event emit, total size %d',
  401. reqId, url, responseSize);
  402. });
  403. if (writeStream) {
  404. // If there's a writable stream to recieve the response data, just pipe the
  405. // response stream to that writable stream and call the callback when it has
  406. // finished writing.
  407. //
  408. // NOTE that when the response stream `res` emits an 'end' event it just
  409. // means that it has finished piping data to another stream. In the
  410. // meanwhile that writable stream may still writing data to the disk until
  411. // it emits a 'close' event.
  412. //
  413. // That means that we should not apply callback until the 'close' of the
  414. // writable stream is emited.
  415. //
  416. // See also:
  417. // - https://github.com/TBEDP/urllib/commit/959ac3365821e0e028c231a5e8efca6af410eabb
  418. // - http://nodejs.org/api/stream.html#stream_event_end
  419. // - http://nodejs.org/api/stream.html#stream_event_close_1
  420. var result = handleRedirect(res);
  421. if (result.redirect) {
  422. res.resume();
  423. return;
  424. }
  425. if (result.error) {
  426. res.resume();
  427. // end ths stream first
  428. writeStream.end();
  429. return done(result.error, null, res);
  430. }
  431. // you can set consumeWriteStream false that only wait response end
  432. if (args.consumeWriteStream === false) {
  433. res.on('end', done.bind(null, null, null, res));
  434. } else {
  435. // node 0.10, 0.12: only emit res aborted, writeStream close not fired
  436. if (isNode010 || isNode012) {
  437. first([
  438. [ writeStream, 'close' ],
  439. [ res, 'aborted' ],
  440. ], function(_, stream, event) {
  441. debug('Request#%d %s: writeStream or res %s event emitted', reqId, url, event);
  442. done(__err || null, null, res);
  443. });
  444. } else {
  445. writeStream.on('close', function() {
  446. debug('Request#%d %s: writeStream close event emitted', reqId, url);
  447. done(__err || null, null, res);
  448. });
  449. }
  450. }
  451. return res.pipe(writeStream);
  452. }
  453. // Otherwise, just concat those buffers.
  454. //
  455. // NOTE that the `chunk` is not a String but a Buffer. It means that if
  456. // you simply concat two chunk with `+` you're actually converting both
  457. // Buffers into Strings before concating them. It'll cause problems when
  458. // dealing with multi-byte characters.
  459. //
  460. // The solution is to store each chunk in an array and concat them with
  461. // 'buffer-concat' when all chunks is recieved.
  462. //
  463. // See also:
  464. // http://cnodejs.org/topic/4faf65852e8fb5bc65113403
  465. var chunks = [];
  466. res.on('data', function (chunk) {
  467. debug('Request#%d %s: `res data` event emit, size %d', reqId, url, chunk.length);
  468. responseSize += chunk.length;
  469. chunks.push(chunk);
  470. });
  471. res.on('end', function () {
  472. var body = Buffer.concat(chunks, responseSize);
  473. debug('Request#%d %s: `res end` event emit, total size %d, _dumped: %s',
  474. reqId, url, responseSize, res._dumped);
  475. if (__err) {
  476. // req.abort() after `res data` event emit.
  477. return done(__err, body, res);
  478. }
  479. var result = handleRedirect(res);
  480. if (result.error) {
  481. return done(result.error, body, res);
  482. }
  483. if (result.redirect) {
  484. return;
  485. }
  486. decodeContent(res, body, function (err, data, encoding) {
  487. if (err) {
  488. return done(err, body, res);
  489. }
  490. // if body not decode, dont touch it
  491. if (!encoding && TEXT_DATA_TYPES.indexOf(args.dataType) >= 0) {
  492. // try to decode charset
  493. try {
  494. data = decodeBodyByCharset(data, res);
  495. } catch (e) {
  496. debug('decodeBodyByCharset error: %s', e);
  497. // if error, dont touch it
  498. return done(null, data, res);
  499. }
  500. if (args.dataType === 'json') {
  501. if (responseSize === 0) {
  502. data = null;
  503. } else {
  504. var r = parseJSON(data, fixJSONCtlChars);
  505. if (r.error) {
  506. err = r.error;
  507. } else {
  508. data = r.data;
  509. }
  510. }
  511. }
  512. }
  513. if (responseAborted) {
  514. // err = new Error('Remote socket was terminated before `response.end()` was called');
  515. // err.name = 'RemoteSocketClosedError';
  516. debug('Request#%d %s: Remote socket was terminated before `response.end()` was called', reqId, url);
  517. }
  518. done(err, data, res);
  519. });
  520. });
  521. }
  522. var connectTimeout, responseTimeout;
  523. if (Array.isArray(args.timeout)) {
  524. connectTimeout = ms(args.timeout[0]);
  525. responseTimeout = ms(args.timeout[1]);
  526. } else { // set both timeout equal
  527. connectTimeout = responseTimeout = ms(args.timeout);
  528. }
  529. debug('ConnectTimeout: %d, ResponseTimeout: %d', connectTimeout, responseTimeout);
  530. function startConnectTimer() {
  531. debug('Connect timer ticking, timeout: %d', connectTimeout);
  532. connectTimer = setTimeout(function () {
  533. connectTimer = null;
  534. if (statusCode === -1) {
  535. statusCode = -2;
  536. }
  537. var msg = 'Connect timeout for ' + connectTimeout + 'ms';
  538. var errorName = 'ConnectionTimeoutError';
  539. if (!req.socket) {
  540. errorName = 'SocketAssignTimeoutError';
  541. msg += ', working sockets is full';
  542. }
  543. __err = new Error(msg);
  544. __err.name = errorName;
  545. __err.requestId = reqId;
  546. debug('ConnectTimeout: Request#%d %s %s: %s, connected: %s', reqId, url, __err.name, msg, connected);
  547. abortRequest();
  548. }, connectTimeout);
  549. }
  550. function startResposneTimer() {
  551. debug('Response timer ticking, timeout: %d', responseTimeout);
  552. responseTimer = setTimeout(function () {
  553. responseTimer = null;
  554. var msg = 'Response timeout for ' + responseTimeout + 'ms';
  555. var errorName = 'ResponseTimeoutError';
  556. __err = new Error(msg);
  557. __err.name = errorName;
  558. __err.requestId = reqId;
  559. debug('ResponseTimeout: Request#%d %s %s: %s, connected: %s', reqId, url, __err.name, msg, connected);
  560. abortRequest();
  561. }, responseTimeout);
  562. }
  563. var req;
  564. // request headers checker will throw error
  565. options.mode = args.mode ? args.mode : '';
  566. try {
  567. req = httplib.request(options, onResponse);
  568. } catch (err) {
  569. return done(err);
  570. }
  571. // environment detection: browser or nodejs
  572. if (typeof(window) === 'undefined') {
  573. // start connect timer just after `request` return, and just in nodejs environment
  574. startConnectTimer();
  575. } else {
  576. req.on('requestTimeout', function () {
  577. if (statusCode === -1) {
  578. statusCode = -2;
  579. }
  580. var msg = 'Connect timeout for ' + connectTimeout + 'ms';
  581. var errorName = 'ConnectionTimeoutError';
  582. __err = new Error(msg);
  583. __err.name = errorName;
  584. __err.requestId = reqId;
  585. abortRequest();
  586. });
  587. }
  588. function abortRequest() {
  589. debug('Request#%d %s abort, connected: %s', reqId, url, connected);
  590. // it wont case error event when req haven't been assigned a socket yet.
  591. if (!req.socket) {
  592. __err.noSocket = true;
  593. done(__err);
  594. }
  595. req.abort();
  596. }
  597. if (timing) {
  598. // request sent
  599. req.on('finish', function() {
  600. timing.requestSent = Date.now() - requestStartTime;
  601. });
  602. }
  603. req.once('socket', function (socket) {
  604. if (timing) {
  605. // socket queuing time
  606. timing.queuing = Date.now() - requestStartTime;
  607. }
  608. // https://github.com/nodejs/node/blob/master/lib/net.js#L377
  609. // https://github.com/nodejs/node/blob/v0.10.40-release/lib/net.js#L352
  610. // should use socket.socket on 0.10.x
  611. if (isNode010 && socket.socket) {
  612. socket = socket.socket;
  613. }
  614. var readyState = socket.readyState;
  615. if (readyState === 'opening') {
  616. socket.once('lookup', function(err, ip, addressType) {
  617. debug('Request#%d %s lookup: %s, %s, %s', reqId, url, err, ip, addressType);
  618. if (timing) {
  619. timing.dnslookup = Date.now() - requestStartTime;
  620. }
  621. if (ip) {
  622. remoteAddress = ip;
  623. }
  624. });
  625. socket.once('connect', function() {
  626. if (timing) {
  627. // socket connected
  628. timing.connected = Date.now() - requestStartTime;
  629. }
  630. // cancel socket timer at first and start tick for TTFB
  631. cancelConnectTimer();
  632. startResposneTimer();
  633. debug('Request#%d %s new socket connected', reqId, url);
  634. connected = true;
  635. if (!remoteAddress) {
  636. remoteAddress = socket.remoteAddress;
  637. }
  638. remotePort = socket.remotePort;
  639. });
  640. return;
  641. }
  642. debug('Request#%d %s reuse socket connected, readyState: %s', reqId, url, readyState);
  643. connected = true;
  644. keepAliveSocket = true;
  645. if (!remoteAddress) {
  646. remoteAddress = socket.remoteAddress;
  647. }
  648. remotePort = socket.remotePort;
  649. // reuse socket, timer should be canceled.
  650. cancelConnectTimer();
  651. startResposneTimer();
  652. });
  653. req.on('error', function (err) {
  654. //TypeError for browser fetch api, Error for browser xmlhttprequest api
  655. if (err.name === 'Error' || err.name === 'TypeError') {
  656. err.name = connected ? 'ResponseError' : 'RequestError';
  657. }
  658. err.message += ' (req "error")';
  659. debug('Request#%d %s `req error` event emit, %s: %s', reqId, url, err.name, err.message);
  660. done(__err || err);
  661. });
  662. if (writeStream) {
  663. writeStream.once('error', function (err) {
  664. err.message += ' (writeStream "error")';
  665. __err = err;
  666. debug('Request#%d %s `writeStream error` event emit, %s: %s', reqId, url, err.name, err.message);
  667. abortRequest();
  668. });
  669. }
  670. if (args.stream) {
  671. args.stream.pipe(req);
  672. args.stream.once('error', function (err) {
  673. err.message += ' (stream "error")';
  674. __err = err;
  675. debug('Request#%d %s `readStream error` event emit, %s: %s', reqId, url, err.name, err.message);
  676. abortRequest();
  677. });
  678. } else {
  679. req.end(body);
  680. }
  681. req.requestId = reqId;
  682. return req;
  683. };