dv-browser 2.1.1 → 2.2.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 +25 -33
  2. package/package.json +3 -3
package/index.js CHANGED
@@ -1,16 +1,14 @@
1
+ // noinspection JSUnusedGlobalSymbols
2
+
1
3
  "use strict";
2
4
  let Curl = require('node-libcurl').Curl;
3
5
  let CurlIpResolve = require('node-libcurl').CurlIpResolve;
4
6
  let Cookie = require('tough-cookie').Cookie;
5
7
  let CookieJar = require('tough-cookie').CookieJar;
6
8
 
7
- let DvBrowser = function () {
8
- let self = this;
9
-
9
+ let DvBrowser = function (opts) {
10
10
  let globalHeaders = [
11
- 'Accept-Language: ru,en-US,en;q=0.8,ru;q=0.6',
12
11
  'Connection: close',
13
- ,
14
12
  'Cache-Control: max-age=0',
15
13
  'Proxy-Connection: close',
16
14
  'Expect: ' // remove "Expect: 100-continue" header
@@ -21,19 +19,20 @@ let DvBrowser = function () {
21
19
  connectTimeout: 5,
22
20
  httpProxy: null,
23
21
  httpProxyUserPwd: null,
24
- allowRedirect: true
22
+ allowRedirect: true,
23
+ userAgent: opts['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',
25
24
  };
26
25
 
27
26
  let cookiejar = new CookieJar();
28
27
  let referer = '';
29
28
 
30
- self.getProxy = function () { return config.httpProxy; };
31
- self.setProxy = function (host, creds) { config.httpProxy = host; config.httpProxyUserPwd = creds; };
32
- self.setReferer = function (url) { referer = url; };
33
- self.setCookie = function (cookie, url, options) { cookiejar.setCookieSync(cookie, url, options); };
34
- self.setTimeout = function (connect, read) { config.connectTimeout = connect; config.timeout = read; };
35
- self.setAllowRedirect = function (allowRedirect) { config.allowRedirect = allowRedirect };
36
- self.getCookie = function (key, url) {
29
+ 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
+ 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
+ this.getCookie = function (key, url) {
37
36
  let cookies = cookiejar.getCookiesSync(url);
38
37
  for (let cookie of cookies) {
39
38
  if (cookie.key === key) {
@@ -43,14 +42,14 @@ let DvBrowser = function () {
43
42
  return null;
44
43
  };
45
44
 
46
- self.get = function (url, options) {
45
+ this.get = function (url, options) {
47
46
  if (!options) options = { };
48
47
  options.url = url;
49
48
  return processRequest('GET', options);
50
49
  };
51
- self.post = function (url, postData) { return processRequest('POST', { url: url, postData: postData }); };
52
- self.postEx = function (options) { return processRequest('POST', options); };
53
- self.request = function (method, options) { return processRequest(method, options); };
50
+ this.post = function (url, postData) { return processRequest('POST', { url: url, postData: postData }); };
51
+ this.postEx = function (options) { return processRequest('POST', options); };
52
+ this.request = function (method, options) { return processRequest(method, options); };
54
53
 
55
54
  function processRequest(method, options) {
56
55
  return new Promise((resolve, reject) => {
@@ -64,25 +63,18 @@ let DvBrowser = function () {
64
63
  if (cookieString) {
65
64
  headers.push('Cookie: ' + cookieString);
66
65
  }
67
- if (referer) {
68
- headers.push('Referer: ' + referer);
66
+ if (options['referer'] || referer) {
67
+ headers.push('Referer: ' + (options['referer'] ?? referer));
69
68
  }
70
- if (options.contentType) {
71
- headers.push('Content-Type: ' + options.contentType);
69
+ if (options['contentType']) {
70
+ headers.push('Content-Type: ' + options['contentType']);
72
71
  } else if (method === 'POST') {
73
72
  headers.push('Content-Type: application/x-www-form-urlencoded');
74
73
  }
75
- if (options.accept) {
76
- headers.push('Accept: ' + options.accept);
77
- } else {
78
- headers.push('Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8');
79
- }
80
- if (options.userAgent) {
81
- headers.push('User-Agent: ' + options.userAgent);
82
- } else {
83
- headers.push('User-Agent: 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');
84
- }
85
- if (!options.noDeflate) {
74
+ headers.push('Accept: ' + (options['accept'] ? options['accept'] : 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'));
75
+ headers.push('Accept-Language: ' + (options['acceptLanguage'] ? options['acceptLanguage'] : 'ru,en-US,en;q=0.8,ru;q=0.6'));
76
+ headers.push('User-Agent: ' + (options['userAgent'] ?? config['userAgent']));
77
+ if (!options['noDeflate']) {
86
78
  headers.push('Accept-Encoding: gzip, deflate');
87
79
  }
88
80
  if (options['headers']) {
@@ -117,7 +109,7 @@ let DvBrowser = function () {
117
109
  curl.setOpt(Curl.option.FOLLOWLOCATION, config.allowRedirect);
118
110
  curl.setOpt(Curl.option.SSL_VERIFYHOST, false);
119
111
  curl.setOpt(Curl.option.SSL_VERIFYPEER, false);
120
- if (!options.noDeflate) {
112
+ if (!options['noDeflate']) {
121
113
  curl.setOpt(Curl.option.ACCEPT_ENCODING, 'gzip');
122
114
  }
123
115
  if (config.httpProxy) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dv-browser",
3
- "version": "2.1.1",
3
+ "version": "2.2.0",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -17,7 +17,7 @@
17
17
  },
18
18
  "homepage": "https://github.com/druidvav/node-browser",
19
19
  "dependencies": {
20
- "node-libcurl": "^2.0.2",
21
- "tough-cookie": "^3.0.1"
20
+ "node-libcurl": "2.3.4",
21
+ "tough-cookie": "4.0.0"
22
22
  }
23
23
  }