dv-browser 2.4.2 → 2.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +38 -48
  2. package/package.json +2 -2
package/index.js CHANGED
@@ -1,37 +1,27 @@
1
- // noinspection JSUnusedGlobalSymbols
2
-
3
1
  "use strict";
4
2
  let Curl = require('node-libcurl').Curl;
5
3
  let CurlIpResolve = require('node-libcurl').CurlIpResolve;
6
4
  let Cookie = require('tough-cookie').Cookie;
7
5
  let CookieJar = require('tough-cookie').CookieJar;
8
6
 
9
- let DvBrowser = function (config) {
10
- if (!config) config = { };
11
- config.timeout = config.timeout ?? 15;
12
- config.connectTimeout = config.connectTimeout ?? 5;
13
- config.httpProxy = config.httpProxy ?? null;
14
- config.httpProxyUserPwd = config.httpProxyUserPwd ?? null;
15
- config.allowRedirect = config.allowRedirect ?? true;
16
- config.userAgent = config.userAgent ?? 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.122 Safari/537.36';
17
- config.accept = config.accept ?? 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
18
- config.acceptLanguage = config.acceptLanguage ?? 'ru,en-US,en;q=0.8,ru;q=0.6';
19
-
20
- let globalHeaders = [
21
- 'Cache-Control: max-age=0',
22
- 'Proxy-Connection: close',
23
- 'Expect: ' // remove "Expect: 100-continue" header
24
- ];
25
-
26
- let cookiejar = new CookieJar();
27
- let referer = '';
7
+ let DvBrowser = function (_config) {
8
+ const config = Object.assign({}, {
9
+ timeout: 15,
10
+ connectTimeout: 5,
11
+ httpProxy: null,
12
+ httpProxyUserPwd: null,
13
+ allowRedirect: true,
14
+ userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.122 Safari/537.36',
15
+ accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
16
+ acceptLanguage: 'ru,en-US,en;q=0.8,ru;q=0.6',
17
+ cacheControl: 'max-age=0',
18
+ proxyConnection: 'close',
19
+ }, _config);
20
+
21
+ const cookiejar = new CookieJar(null);
28
22
 
29
23
  this.getProxy = function () { return config.httpProxy; };
30
- this.setProxy = function (host, creds) { config.httpProxy = host; config.httpProxyUserPwd = creds; };
31
- this.setReferer = function (url) { referer = url; };
32
24
  this.setCookie = function (cookie, url, options) { cookiejar.setCookieSync(cookie, url, options); };
33
- this.setTimeout = function (connect, read) { config.connectTimeout = connect; config.timeout = read; };
34
- this.setAllowRedirect = function (allowRedirect) { config.allowRedirect = allowRedirect };
35
25
  this.getCookie = function (key, url) {
36
26
  let cookies = cookiejar.getCookiesSync(url);
37
27
  for (let cookie of cookies) {
@@ -62,14 +52,16 @@ let DvBrowser = function (config) {
62
52
  };
63
53
  this.request = function (method, options) { return processRequest(method, options); };
64
54
 
65
- function processRequest(method, options) {
55
+ function processRequest(method, _options) {
66
56
  return new Promise((resolve, reject) => {
57
+ const options = Object.assign({}, config, _options);
58
+
67
59
  let responseHeaders = [ ];
68
60
  let responseStatus = 0;
69
61
  let responseBody = [ ];
70
62
  let callbackCalled = false;
71
63
 
72
- let headers = JSON.parse(JSON.stringify(globalHeaders)); // Cloning object
64
+ let headers = [ ];
73
65
  let cookieString = cookiejar.getCookieStringSync(options.url);
74
66
  if (cookieString) {
75
67
  headers.push('Cookie: ' + cookieString);
@@ -79,36 +71,34 @@ let DvBrowser = function (config) {
79
71
  } else if (method === 'POST') {
80
72
  headers.push('Content-Type: application/x-www-form-urlencoded');
81
73
  }
82
- headers.push('Connection: ' + (options['connection'] ?? 'close'));
83
- headers.push('Accept: ' + (options['accept'] ?? config['accept']));
84
- headers.push('Accept-Language: ' + (options['acceptLanguage'] ?? config['acceptLanguage']));
85
- headers.push('User-Agent: ' + (options['userAgent'] ?? config['userAgent']));
86
- if (options['referer'] || referer) {
87
- headers.push('Referer: ' + (options['referer'] ?? referer));
88
- }
89
- if (!options['noDeflate']) {
90
- headers.push('Accept-Encoding: gzip, deflate');
91
- }
74
+ headers.push('Connection: ' + options['connection']);
75
+ headers.push('Accept: ' + options['accept']);
76
+ headers.push('Accept-Language: ' + options['acceptLanguage']);
77
+ headers.push('Cache-Control: ' + options['cacheControl']);
78
+ headers.push('Proxy-Connection: ' + options['proxyConnection']);
79
+ if (options['referer']) headers.push('Referer: ' + options['referer']);
80
+ if (!options['noDeflate']) headers.push('Accept-Encoding: gzip, deflate');
92
81
  if (options['headers']) {
93
82
  for (let i = 0; i < options['headers'].length; i++) {
94
83
  headers.push(options['headers'][i]);
95
84
  }
96
85
  }
86
+ headers.push('Expect: ');
97
87
 
98
88
  let startTime = new Date().getTime();
99
89
  let timeoutControl = setInterval(() => {
100
90
  let curTime = new Date().getTime();
101
91
  let execTime = Math.round((curTime - startTime) / 1000);
102
- if (execTime > (config.timeout + config.connectTimeout) * 2) {
103
- console.log('timeout', options.url, curTime - startTime, config);
92
+ if (execTime > (options.timeout + options.connectTimeout) * 2) {
93
+ console.log('timeout', options.url, curTime - startTime, options);
104
94
  throw new DvBrowserError('Something went wrong and timeout expired');
105
95
  }
106
96
  }, 500);
107
97
  let curl = new Curl();
108
98
  curl.setOpt(Curl.option.URL, options.url);
109
- curl.setOpt(Curl.option.TIMEOUT, config.timeout);
110
- curl.setOpt(Curl.option.CONNECTTIMEOUT, config.connectTimeout);
111
- curl.setOpt(Curl.option.LOW_SPEED_TIME, config.timeout);
99
+ curl.setOpt(Curl.option.TIMEOUT, options.timeout);
100
+ curl.setOpt(Curl.option.CONNECTTIMEOUT, options.connectTimeout);
101
+ curl.setOpt(Curl.option.LOW_SPEED_TIME, options.timeout);
112
102
  curl.setOpt(Curl.option.LOW_SPEED_LIMIT, 512);
113
103
  curl.setOpt(Curl.option.NOPROGRESS, true);
114
104
  curl.setOpt(Curl.option.HTTPHEADER, headers);
@@ -118,16 +108,16 @@ let DvBrowser = function (config) {
118
108
  curl.setOpt(Curl.option.POST, true);
119
109
  curl.setOpt(Curl.option.POSTFIELDS, options.postData);
120
110
  }
121
- curl.setOpt(Curl.option.FOLLOWLOCATION, config.allowRedirect);
111
+ curl.setOpt(Curl.option.FOLLOWLOCATION, options.allowRedirect);
122
112
  curl.setOpt(Curl.option.SSL_VERIFYHOST, false);
123
113
  curl.setOpt(Curl.option.SSL_VERIFYPEER, false);
124
114
  if (!options['noDeflate']) {
125
115
  curl.setOpt(Curl.option.ACCEPT_ENCODING, 'gzip');
126
116
  }
127
- if (config.httpProxy) {
128
- curl.setOpt(Curl.option.PROXY, config.httpProxy);
129
- if (config.httpProxyUserPwd) {
130
- curl.setOpt(Curl.option.PROXYUSERPWD, config.httpProxyUserPwd);
117
+ if (options.httpProxy) {
118
+ curl.setOpt(Curl.option.PROXY, options.httpProxy);
119
+ if (options.httpProxyUserPwd) {
120
+ curl.setOpt(Curl.option.PROXYUSERPWD, options.httpProxyUserPwd);
131
121
  }
132
122
  }
133
123
  curl.on('header', handleHeader);
@@ -143,7 +133,7 @@ let DvBrowser = function (config) {
143
133
  function handleHeader(chunk) {
144
134
  let header = chunk.toString().replace("\r", '').replace("\n", '');
145
135
  if (chunk.length > 2) {
146
- if (header.substr(0, 4) === 'HTTP') {
136
+ if (header.substring(0, 4) === 'HTTP') {
147
137
  let status = header.split(' ');
148
138
  responseStatus = status[1];
149
139
  } else {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dv-browser",
3
- "version": "2.4.2",
3
+ "version": "2.5.0",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -18,6 +18,6 @@
18
18
  "homepage": "https://github.com/druidvav/node-browser",
19
19
  "dependencies": {
20
20
  "node-libcurl": "3.0.0",
21
- "tough-cookie": "4.1.2"
21
+ "tough-cookie": "4.1.3"
22
22
  }
23
23
  }