unprint 0.14.1 → 0.14.2

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.2",
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,34 @@ 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;
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
+ /* eslint-enable no-param-reassign */
960
+
933
961
  async function request(url, body, customOptions = {}, method = 'GET') {
934
962
  const options = merge.all([{
935
963
  timeout: 1000,
@@ -959,20 +987,7 @@ async function request(url, body, customOptions = {}, method = 'GET') {
959
987
  // httpAgent: options.httpAgent || new http.Agent({ ...options.agent }),
960
988
  });
961
989
 
962
- if (options.proxy) {
963
- const proxyAgent = tunnel.httpsOverHttp({
964
- proxy: {
965
- host: options.proxy.host,
966
- port: options.proxy.port,
967
- },
968
- });
969
-
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
- }
990
+ setProxy(instance, options, url);
976
991
 
977
992
  const res = await limiter.schedule(async () => instance.get(url));
978
993
 
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));