dv-browser 2.4.1 → 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 -49
  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) {
@@ -60,17 +50,18 @@ let DvBrowser = function (config) {
60
50
  options.contentType = 'application/json';
61
51
  return processRequest('POST', options);
62
52
  };
63
- this.postEx = function (options) { return processRequest('POST', options); };
64
53
  this.request = function (method, options) { return processRequest(method, options); };
65
54
 
66
- function processRequest(method, options) {
55
+ function processRequest(method, _options) {
67
56
  return new Promise((resolve, reject) => {
57
+ const options = Object.assign({}, config, _options);
58
+
68
59
  let responseHeaders = [ ];
69
60
  let responseStatus = 0;
70
61
  let responseBody = [ ];
71
62
  let callbackCalled = false;
72
63
 
73
- let headers = JSON.parse(JSON.stringify(globalHeaders)); // Cloning object
64
+ let headers = [ ];
74
65
  let cookieString = cookiejar.getCookieStringSync(options.url);
75
66
  if (cookieString) {
76
67
  headers.push('Cookie: ' + cookieString);
@@ -80,36 +71,34 @@ let DvBrowser = function (config) {
80
71
  } else if (method === 'POST') {
81
72
  headers.push('Content-Type: application/x-www-form-urlencoded');
82
73
  }
83
- headers.push('Connection: ' + (options['connection'] ?? 'close'));
84
- headers.push('Accept: ' + (options['accept'] ?? config['accept']));
85
- headers.push('Accept-Language: ' + (options['acceptLanguage'] ?? config['acceptLanguage']));
86
- headers.push('User-Agent: ' + (options['userAgent'] ?? config['userAgent']));
87
- if (options['referer'] || referer) {
88
- headers.push('Referer: ' + (options['referer'] ?? referer));
89
- }
90
- if (!options['noDeflate']) {
91
- headers.push('Accept-Encoding: gzip, deflate');
92
- }
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');
93
81
  if (options['headers']) {
94
82
  for (let i = 0; i < options['headers'].length; i++) {
95
83
  headers.push(options['headers'][i]);
96
84
  }
97
85
  }
86
+ headers.push('Expect: ');
98
87
 
99
88
  let startTime = new Date().getTime();
100
89
  let timeoutControl = setInterval(() => {
101
90
  let curTime = new Date().getTime();
102
91
  let execTime = Math.round((curTime - startTime) / 1000);
103
- if (execTime > (config.timeout + config.connectTimeout) * 2) {
104
- 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);
105
94
  throw new DvBrowserError('Something went wrong and timeout expired');
106
95
  }
107
96
  }, 500);
108
97
  let curl = new Curl();
109
98
  curl.setOpt(Curl.option.URL, options.url);
110
- curl.setOpt(Curl.option.TIMEOUT, config.timeout);
111
- curl.setOpt(Curl.option.CONNECTTIMEOUT, config.connectTimeout);
112
- 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);
113
102
  curl.setOpt(Curl.option.LOW_SPEED_LIMIT, 512);
114
103
  curl.setOpt(Curl.option.NOPROGRESS, true);
115
104
  curl.setOpt(Curl.option.HTTPHEADER, headers);
@@ -119,16 +108,16 @@ let DvBrowser = function (config) {
119
108
  curl.setOpt(Curl.option.POST, true);
120
109
  curl.setOpt(Curl.option.POSTFIELDS, options.postData);
121
110
  }
122
- curl.setOpt(Curl.option.FOLLOWLOCATION, config.allowRedirect);
111
+ curl.setOpt(Curl.option.FOLLOWLOCATION, options.allowRedirect);
123
112
  curl.setOpt(Curl.option.SSL_VERIFYHOST, false);
124
113
  curl.setOpt(Curl.option.SSL_VERIFYPEER, false);
125
114
  if (!options['noDeflate']) {
126
115
  curl.setOpt(Curl.option.ACCEPT_ENCODING, 'gzip');
127
116
  }
128
- if (config.httpProxy) {
129
- curl.setOpt(Curl.option.PROXY, config.httpProxy);
130
- if (config.httpProxyUserPwd) {
131
- 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);
132
121
  }
133
122
  }
134
123
  curl.on('header', handleHeader);
@@ -144,7 +133,7 @@ let DvBrowser = function (config) {
144
133
  function handleHeader(chunk) {
145
134
  let header = chunk.toString().replace("\r", '').replace("\n", '');
146
135
  if (chunk.length > 2) {
147
- if (header.substr(0, 4) === 'HTTP') {
136
+ if (header.substring(0, 4) === 'HTTP') {
148
137
  let status = header.split(' ');
149
138
  responseStatus = status[1];
150
139
  } else {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dv-browser",
3
- "version": "2.4.1",
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
  }