curl-wrap 1.0.0 → 1.0.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.
- package/README.md +0 -26
- package/index.js +21 -5
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -18,32 +18,6 @@ console.log(response.body);
|
|
|
18
18
|
console.log(response.statusCode);
|
|
19
19
|
```
|
|
20
20
|
|
|
21
|
-
hit12369: write documentation for this library
|
|
22
|
-
|
|
23
|
-
Workspace: # curl-wrap
|
|
24
|
-
|
|
25
|
-
Node.js library that wraps the curl command line.
|
|
26
|
-
|
|
27
|
-
## Install
|
|
28
|
-
|
|
29
|
-
```sh
|
|
30
|
-
npm install curl-wrap
|
|
31
|
-
# OR
|
|
32
|
-
yarn add curl-wrap
|
|
33
|
-
```
|
|
34
|
-
|
|
35
|
-
## Usage
|
|
36
|
-
|
|
37
|
-
### Basic Usage
|
|
38
|
-
|
|
39
|
-
```js
|
|
40
|
-
const {Curl} = require('curl-wrap');
|
|
41
|
-
|
|
42
|
-
const response = await Curl.get('https://www.example.com');
|
|
43
|
-
console.log(response.body);
|
|
44
|
-
console.log(response.statusCode);
|
|
45
|
-
```
|
|
46
|
-
|
|
47
21
|
### Creating a Curl Instance
|
|
48
22
|
|
|
49
23
|
```js
|
package/index.js
CHANGED
|
@@ -61,6 +61,7 @@ class CurlResponse {
|
|
|
61
61
|
this.ip = data.remote_ip || '';
|
|
62
62
|
this.url = data.url_effective || data.url || '';
|
|
63
63
|
this.errorMsg = data.errormsg || '';
|
|
64
|
+
this.curlTimeTaken = data.time_total || 0;
|
|
64
65
|
|
|
65
66
|
const headers = json.headers || {};
|
|
66
67
|
for (const [key, value] of Object.entries(headers)) {
|
|
@@ -894,6 +895,18 @@ class Curl {
|
|
|
894
895
|
return this;
|
|
895
896
|
}
|
|
896
897
|
|
|
898
|
+
/**
|
|
899
|
+
* enable or disable proxy use
|
|
900
|
+
* you need to set the proxy to use using the proxy() method
|
|
901
|
+
*
|
|
902
|
+
* @package {boolean} [shouldUseProxy=true] whether to use proxy or not
|
|
903
|
+
* @returns {Curl} self
|
|
904
|
+
*/
|
|
905
|
+
useProxy(shouldUseProxy = true) {
|
|
906
|
+
this.options.useProxy = shouldUseProxy;
|
|
907
|
+
return this;
|
|
908
|
+
}
|
|
909
|
+
|
|
897
910
|
/**
|
|
898
911
|
* Set proxy address (or options).
|
|
899
912
|
* Proxy type can be http, https, or socks5.
|
|
@@ -1091,7 +1104,7 @@ class Curl {
|
|
|
1091
1104
|
if (options.compress) {
|
|
1092
1105
|
args.push('--compressed');
|
|
1093
1106
|
}
|
|
1094
|
-
if (options.proxy) {
|
|
1107
|
+
if (options.proxy && options.useProxy !== false) {
|
|
1095
1108
|
args.push('--proxy', options.proxy);
|
|
1096
1109
|
}
|
|
1097
1110
|
if (options.timeout) {
|
|
@@ -1118,7 +1131,8 @@ class Curl {
|
|
|
1118
1131
|
return args;
|
|
1119
1132
|
}
|
|
1120
1133
|
|
|
1121
|
-
async
|
|
1134
|
+
async fetch() {
|
|
1135
|
+
const startTime = Date.now();
|
|
1122
1136
|
const cmd = this.options.cliCommand || 'curl';
|
|
1123
1137
|
const args = await this.getCurlArgs();
|
|
1124
1138
|
const curl = spawn(cmd, args);
|
|
@@ -1137,10 +1151,12 @@ class Curl {
|
|
|
1137
1151
|
});
|
|
1138
1152
|
|
|
1139
1153
|
curl.on('error', (error) => {
|
|
1154
|
+
error.timeTaken = Date.now() - startTime;
|
|
1140
1155
|
reject(error);
|
|
1141
1156
|
});
|
|
1142
1157
|
|
|
1143
1158
|
curl.on('close', async (code) => {
|
|
1159
|
+
response.timeTaken = Date.now() - startTime;
|
|
1144
1160
|
response.exitCode = code;
|
|
1145
1161
|
response.url = this.options.url;
|
|
1146
1162
|
response.body = stdout;
|
|
@@ -1185,7 +1201,7 @@ class Curl {
|
|
|
1185
1201
|
* @return {Promise<T>} a Promise in pending state
|
|
1186
1202
|
*/
|
|
1187
1203
|
then(successCallback, errorCallback) {
|
|
1188
|
-
return this.
|
|
1204
|
+
return this.fetch().then(successCallback, errorCallback);
|
|
1189
1205
|
}
|
|
1190
1206
|
|
|
1191
1207
|
/**
|
|
@@ -1196,7 +1212,7 @@ class Curl {
|
|
|
1196
1212
|
* @return {Promise<T>} a Promise in pending state
|
|
1197
1213
|
*/
|
|
1198
1214
|
catch(errorCallback) {
|
|
1199
|
-
return this.
|
|
1215
|
+
return this.fetch().catch(errorCallback);
|
|
1200
1216
|
}
|
|
1201
1217
|
|
|
1202
1218
|
/**
|
|
@@ -1207,7 +1223,7 @@ class Curl {
|
|
|
1207
1223
|
* @return {Promise<T>} a Promise in pending state
|
|
1208
1224
|
*/
|
|
1209
1225
|
finally(callback) {
|
|
1210
|
-
return this.
|
|
1226
|
+
return this.fetch().finally(callback);
|
|
1211
1227
|
}
|
|
1212
1228
|
}
|
|
1213
1229
|
|