unprint 0.14.1 → 0.14.3

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.
package/README.md CHANGED
@@ -209,9 +209,24 @@ Returns
209
209
 
210
210
  ### Proxy
211
211
  ```javascript
212
- unprint.get({ // or unprint.options();
213
- host: '127.0.0.1',
214
- port: 8888,
212
+ unprint.options({ // or unprint.options();
213
+ proxy: {
214
+ enable: true,
215
+ use: false, // don't use for all requests by default
216
+ host: '127.0.0.1',
217
+ port: 8888,
218
+ hostnames: [
219
+ 'www.google.com',
220
+ 'www.example.com',
221
+ ],
222
+ }
223
+ });
224
+
225
+ unprint.get({
226
+ proxy: {
227
+ use: true, // use proxy for this request
228
+ // all other proxy options can be supplied here
229
+ },
215
230
  });
216
231
  ```
217
232
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "unprint",
3
- "version": "0.14.1",
3
+ "version": "0.14.3",
4
4
  "description": "Simplify common web scraping tasks while staying in control of the data.",
5
5
  "main": "src/app.js",
6
6
  "scripts": {
package/src/app.js CHANGED
@@ -930,6 +930,36 @@ function getLimiter(url, options) {
930
930
  };
931
931
  }
932
932
 
933
+ /* eslint-disable no-param-reassign */
934
+ function setProxy(instance, options, url) {
935
+ const { hostname } = new URL(url);
936
+
937
+ if (options.proxy
938
+ && options.proxy.enable !== false
939
+ && options.proxy.use !== false // use is a local override for enable
940
+ && (options.proxy.use
941
+ || options.proxy.hostnames?.includes(hostname))
942
+ ) {
943
+ const proxyAgent = tunnel.httpsOverHttp({
944
+ proxy: {
945
+ host: options.proxy.host,
946
+ port: options.proxy.port,
947
+ },
948
+ });
949
+
950
+ instance.defaults.httpAgent = proxyAgent;
951
+ instance.defaults.httpsAgent = proxyAgent;
952
+
953
+ return true;
954
+ }
955
+
956
+ instance.defaults.httpAgent = options.httpsAgent || new http.Agent({ ...options.agent });
957
+ instance.defaults.httpsAgent = options.httpsAgent || new https.Agent({ ...options.agent });
958
+
959
+ return false;
960
+ }
961
+ /* eslint-enable no-param-reassign */
962
+
933
963
  async function request(url, body, customOptions = {}, method = 'GET') {
934
964
  const options = merge.all([{
935
965
  timeout: 1000,
@@ -939,16 +969,6 @@ async function request(url, body, customOptions = {}, method = 'GET') {
939
969
 
940
970
  const { limiter, interval, concurrency } = getLimiter(url, options);
941
971
 
942
- const feedbackBase = {
943
- url,
944
- method,
945
- interval,
946
- concurrency,
947
- options,
948
- };
949
-
950
- events.emit('requestInit', feedbackBase);
951
-
952
972
  const instance = axios.create({
953
973
  data: body,
954
974
  validateStatus: null,
@@ -959,20 +979,18 @@ async function request(url, body, customOptions = {}, method = 'GET') {
959
979
  // httpAgent: options.httpAgent || new http.Agent({ ...options.agent }),
960
980
  });
961
981
 
962
- if (options.proxy) {
963
- const proxyAgent = tunnel.httpsOverHttp({
964
- proxy: {
965
- host: options.proxy.host,
966
- port: options.proxy.port,
967
- },
968
- });
982
+ const isProxied = setProxy(instance, options, url);
969
983
 
970
- instance.defaults.httpAgent = proxyAgent;
971
- instance.defaults.httpsAgent = proxyAgent;
972
- } else {
973
- instance.defaults.httpAgent = options.httpsAgent || new http.Agent({ ...options.agent });
974
- instance.defaults.httpsAgent = options.httpsAgent || new https.Agent({ ...options.agent });
975
- }
984
+ const feedbackBase = {
985
+ url,
986
+ method,
987
+ interval,
988
+ concurrency,
989
+ isProxied,
990
+ options,
991
+ };
992
+
993
+ events.emit('requestInit', feedbackBase);
976
994
 
977
995
  const res = await limiter.schedule(async () => instance.get(url));
978
996
 
package/tests/init.js CHANGED
@@ -18,12 +18,11 @@ async function initTest() {
18
18
  interval: 100,
19
19
  },
20
20
  },
21
- /*
22
21
  proxy: {
23
22
  host: '192.168.178.25',
24
23
  port: 8888,
24
+ hostnames: ['127.0.0.2'],
25
25
  },
26
- */
27
26
  });
28
27
 
29
28
  unprint.on('requestInit', (initData) => console.log('init', initData));