curl-wrap 1.0.0 → 1.0.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 +0 -26
- package/index.js +22 -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
|
@@ -56,11 +56,13 @@ class CurlResponse {
|
|
|
56
56
|
|
|
57
57
|
setCurlJson(json, options = {}) {
|
|
58
58
|
if (!json) return;
|
|
59
|
+
this.curlJson = json;
|
|
59
60
|
const data = json.json || {};
|
|
60
61
|
this.statusCode = data.response_code || 0;
|
|
61
62
|
this.ip = data.remote_ip || '';
|
|
62
63
|
this.url = data.url_effective || data.url || '';
|
|
63
64
|
this.errorMsg = data.errormsg || '';
|
|
65
|
+
this.curlTimeTaken = Math.round((data.time_total || 0) * 1000);
|
|
64
66
|
|
|
65
67
|
const headers = json.headers || {};
|
|
66
68
|
for (const [key, value] of Object.entries(headers)) {
|
|
@@ -894,6 +896,18 @@ class Curl {
|
|
|
894
896
|
return this;
|
|
895
897
|
}
|
|
896
898
|
|
|
899
|
+
/**
|
|
900
|
+
* enable or disable proxy use
|
|
901
|
+
* you need to set the proxy to use using the proxy() method
|
|
902
|
+
*
|
|
903
|
+
* @package {boolean} [shouldUseProxy=true] whether to use proxy or not
|
|
904
|
+
* @returns {Curl} self
|
|
905
|
+
*/
|
|
906
|
+
useProxy(shouldUseProxy = true) {
|
|
907
|
+
this.options.useProxy = shouldUseProxy;
|
|
908
|
+
return this;
|
|
909
|
+
}
|
|
910
|
+
|
|
897
911
|
/**
|
|
898
912
|
* Set proxy address (or options).
|
|
899
913
|
* Proxy type can be http, https, or socks5.
|
|
@@ -1091,7 +1105,7 @@ class Curl {
|
|
|
1091
1105
|
if (options.compress) {
|
|
1092
1106
|
args.push('--compressed');
|
|
1093
1107
|
}
|
|
1094
|
-
if (options.proxy) {
|
|
1108
|
+
if (options.proxy && options.useProxy !== false) {
|
|
1095
1109
|
args.push('--proxy', options.proxy);
|
|
1096
1110
|
}
|
|
1097
1111
|
if (options.timeout) {
|
|
@@ -1118,7 +1132,8 @@ class Curl {
|
|
|
1118
1132
|
return args;
|
|
1119
1133
|
}
|
|
1120
1134
|
|
|
1121
|
-
async
|
|
1135
|
+
async fetch() {
|
|
1136
|
+
const startTime = Date.now();
|
|
1122
1137
|
const cmd = this.options.cliCommand || 'curl';
|
|
1123
1138
|
const args = await this.getCurlArgs();
|
|
1124
1139
|
const curl = spawn(cmd, args);
|
|
@@ -1137,10 +1152,12 @@ class Curl {
|
|
|
1137
1152
|
});
|
|
1138
1153
|
|
|
1139
1154
|
curl.on('error', (error) => {
|
|
1155
|
+
error.timeTaken = Date.now() - startTime;
|
|
1140
1156
|
reject(error);
|
|
1141
1157
|
});
|
|
1142
1158
|
|
|
1143
1159
|
curl.on('close', async (code) => {
|
|
1160
|
+
response.timeTaken = Date.now() - startTime;
|
|
1144
1161
|
response.exitCode = code;
|
|
1145
1162
|
response.url = this.options.url;
|
|
1146
1163
|
response.body = stdout;
|
|
@@ -1185,7 +1202,7 @@ class Curl {
|
|
|
1185
1202
|
* @return {Promise<T>} a Promise in pending state
|
|
1186
1203
|
*/
|
|
1187
1204
|
then(successCallback, errorCallback) {
|
|
1188
|
-
return this.
|
|
1205
|
+
return this.fetch().then(successCallback, errorCallback);
|
|
1189
1206
|
}
|
|
1190
1207
|
|
|
1191
1208
|
/**
|
|
@@ -1196,7 +1213,7 @@ class Curl {
|
|
|
1196
1213
|
* @return {Promise<T>} a Promise in pending state
|
|
1197
1214
|
*/
|
|
1198
1215
|
catch(errorCallback) {
|
|
1199
|
-
return this.
|
|
1216
|
+
return this.fetch().catch(errorCallback);
|
|
1200
1217
|
}
|
|
1201
1218
|
|
|
1202
1219
|
/**
|
|
@@ -1207,7 +1224,7 @@ class Curl {
|
|
|
1207
1224
|
* @return {Promise<T>} a Promise in pending state
|
|
1208
1225
|
*/
|
|
1209
1226
|
finally(callback) {
|
|
1210
|
-
return this.
|
|
1227
|
+
return this.fetch().finally(callback);
|
|
1211
1228
|
}
|
|
1212
1229
|
}
|
|
1213
1230
|
|