dv-browser 2.4.2 → 2.5.1

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 +39 -48
  2. package/package.json +2 -2
package/index.js CHANGED
@@ -1,37 +1,28 @@
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
+ connection: 'close',
19
+ proxyConnection: 'close',
20
+ }, _config);
21
+
22
+ const cookiejar = new CookieJar(null);
28
23
 
29
24
  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
25
  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
26
  this.getCookie = function (key, url) {
36
27
  let cookies = cookiejar.getCookiesSync(url);
37
28
  for (let cookie of cookies) {
@@ -62,14 +53,16 @@ let DvBrowser = function (config) {
62
53
  };
63
54
  this.request = function (method, options) { return processRequest(method, options); };
64
55
 
65
- function processRequest(method, options) {
56
+ function processRequest(method, _options) {
66
57
  return new Promise((resolve, reject) => {
58
+ const options = Object.assign({}, config, _options);
59
+
67
60
  let responseHeaders = [ ];
68
61
  let responseStatus = 0;
69
62
  let responseBody = [ ];
70
63
  let callbackCalled = false;
71
64
 
72
- let headers = JSON.parse(JSON.stringify(globalHeaders)); // Cloning object
65
+ let headers = [ ];
73
66
  let cookieString = cookiejar.getCookieStringSync(options.url);
74
67
  if (cookieString) {
75
68
  headers.push('Cookie: ' + cookieString);
@@ -79,36 +72,34 @@ let DvBrowser = function (config) {
79
72
  } else if (method === 'POST') {
80
73
  headers.push('Content-Type: application/x-www-form-urlencoded');
81
74
  }
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
- }
75
+ headers.push('Connection: ' + options['connection']);
76
+ headers.push('Accept: ' + options['accept']);
77
+ headers.push('Accept-Language: ' + options['acceptLanguage']);
78
+ headers.push('Cache-Control: ' + options['cacheControl']);
79
+ headers.push('Proxy-Connection: ' + options['proxyConnection']);
80
+ if (options['referer']) headers.push('Referer: ' + options['referer']);
81
+ if (!options['noDeflate']) headers.push('Accept-Encoding: gzip, deflate');
92
82
  if (options['headers']) {
93
83
  for (let i = 0; i < options['headers'].length; i++) {
94
84
  headers.push(options['headers'][i]);
95
85
  }
96
86
  }
87
+ headers.push('Expect: ');
97
88
 
98
89
  let startTime = new Date().getTime();
99
90
  let timeoutControl = setInterval(() => {
100
91
  let curTime = new Date().getTime();
101
92
  let execTime = Math.round((curTime - startTime) / 1000);
102
- if (execTime > (config.timeout + config.connectTimeout) * 2) {
103
- console.log('timeout', options.url, curTime - startTime, config);
93
+ if (execTime > (options.timeout + options.connectTimeout) * 2) {
94
+ console.log('timeout', options.url, curTime - startTime, options);
104
95
  throw new DvBrowserError('Something went wrong and timeout expired');
105
96
  }
106
97
  }, 500);
107
98
  let curl = new Curl();
108
99
  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);
100
+ curl.setOpt(Curl.option.TIMEOUT, options.timeout);
101
+ curl.setOpt(Curl.option.CONNECTTIMEOUT, options.connectTimeout);
102
+ curl.setOpt(Curl.option.LOW_SPEED_TIME, options.timeout);
112
103
  curl.setOpt(Curl.option.LOW_SPEED_LIMIT, 512);
113
104
  curl.setOpt(Curl.option.NOPROGRESS, true);
114
105
  curl.setOpt(Curl.option.HTTPHEADER, headers);
@@ -118,16 +109,16 @@ let DvBrowser = function (config) {
118
109
  curl.setOpt(Curl.option.POST, true);
119
110
  curl.setOpt(Curl.option.POSTFIELDS, options.postData);
120
111
  }
121
- curl.setOpt(Curl.option.FOLLOWLOCATION, config.allowRedirect);
112
+ curl.setOpt(Curl.option.FOLLOWLOCATION, options.allowRedirect);
122
113
  curl.setOpt(Curl.option.SSL_VERIFYHOST, false);
123
114
  curl.setOpt(Curl.option.SSL_VERIFYPEER, false);
124
115
  if (!options['noDeflate']) {
125
116
  curl.setOpt(Curl.option.ACCEPT_ENCODING, 'gzip');
126
117
  }
127
- if (config.httpProxy) {
128
- curl.setOpt(Curl.option.PROXY, config.httpProxy);
129
- if (config.httpProxyUserPwd) {
130
- curl.setOpt(Curl.option.PROXYUSERPWD, config.httpProxyUserPwd);
118
+ if (options.httpProxy) {
119
+ curl.setOpt(Curl.option.PROXY, options.httpProxy);
120
+ if (options.httpProxyUserPwd) {
121
+ curl.setOpt(Curl.option.PROXYUSERPWD, options.httpProxyUserPwd);
131
122
  }
132
123
  }
133
124
  curl.on('header', handleHeader);
@@ -143,7 +134,7 @@ let DvBrowser = function (config) {
143
134
  function handleHeader(chunk) {
144
135
  let header = chunk.toString().replace("\r", '').replace("\n", '');
145
136
  if (chunk.length > 2) {
146
- if (header.substr(0, 4) === 'HTTP') {
137
+ if (header.substring(0, 4) === 'HTTP') {
147
138
  let status = header.split(' ');
148
139
  responseStatus = status[1];
149
140
  } 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.1",
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
  }