@ossiana/node-libcurl 1.1.3-alpha-3 → 1.2.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.
Files changed (48) hide show
  1. package/LICENSE +21 -21
  2. package/dist/export.d.ts +6 -4
  3. package/dist/export.js +13 -9
  4. package/dist/export.js.map +1 -1
  5. package/dist/fetch.d.ts +27 -27
  6. package/dist/fetch.js +51 -51
  7. package/dist/fetch.js.map +1 -1
  8. package/dist/index.d.ts +1 -1
  9. package/dist/index.js +17 -17
  10. package/dist/ja3Map.d.ts +12 -0
  11. package/dist/ja3Map.js +16 -0
  12. package/dist/ja3Map.js.map +1 -0
  13. package/dist/libcurl.d.ts +186 -172
  14. package/dist/libcurl.js +364 -350
  15. package/dist/libcurl.js.map +1 -1
  16. package/dist/libcurlWebSocket.d.ts +30 -0
  17. package/dist/libcurlWebSocket.js +83 -0
  18. package/dist/libcurlWebSocket.js.map +1 -0
  19. package/dist/requests.d.ts +101 -82
  20. package/dist/requests.js +372 -248
  21. package/dist/requests.js.map +1 -1
  22. package/dist/utils.d.ts +7 -6
  23. package/dist/utils.js +110 -101
  24. package/dist/utils.js.map +1 -1
  25. package/package.json +48 -41
  26. package/readme.md +79 -79
  27. package/scripts/bindings.js +15 -0
  28. package/scripts/postinstall.js +104 -0
  29. package/binding.gyp +0 -97
  30. package/src/ja3Table.json +0 -102
  31. package/src/libcurl/bao_curl.cpp +0 -338
  32. package/src/libcurl/bao_curl_multi.cpp +0 -123
  33. package/src/libcurl/bao_curl_node_addon.cpp +0 -511
  34. package/src/libcurl/include/bao_curl.h +0 -116
  35. package/src/libcurl/include/curl/Makefile.am +0 -39
  36. package/src/libcurl/include/curl/curl.h +0 -3104
  37. package/src/libcurl/include/curl/curlver.h +0 -77
  38. package/src/libcurl/include/curl/easy.h +0 -123
  39. package/src/libcurl/include/curl/mprintf.h +0 -50
  40. package/src/libcurl/include/curl/multi.h +0 -457
  41. package/src/libcurl/include/curl/options.h +0 -68
  42. package/src/libcurl/include/curl/stdcheaders.h +0 -33
  43. package/src/libcurl/include/curl/system.h +0 -504
  44. package/src/libcurl/include/curl/typecheck-gcc.h +0 -707
  45. package/src/libcurl/include/curl/urlapi.h +0 -145
  46. package/src/libcurl/include/request_tls_utils.h +0 -30
  47. package/src/libcurl/include/utils.h +0 -23
  48. package/src/libcurl/utils.cpp +0 -43
@@ -1,123 +0,0 @@
1
- #include "bao_curl.h"
2
-
3
- #define CHECK_CURLMOK(e) \
4
- { \
5
- CURLMcode code = (e); \
6
- this->m_lastCode = code; \
7
- if (code != CURLMcode::CURLM_OK) \
8
- { \
9
- printf("CURLM Error:%s\n", curl_multi_strerror(code)); \
10
- } \
11
- }
12
- NAMESPACE_BAO_START
13
-
14
- BaoCurlMulti::BaoCurlMulti()
15
- {
16
- this->m_pCURLM = curl_multi_init();
17
- CHECK_CURLMOK(curl_multi_setopt(this->m_pCURLM, CURLMOPT_PIPELINING));
18
- this->m_bRunning = true;
19
- }
20
-
21
- BaoCurlMulti::~BaoCurlMulti()
22
- {
23
- this->m_lock.lock();
24
- this->m_bRunning = false;
25
- this->m_lock.unlock();
26
- if (this->m_pCURLM != nullptr)
27
- {
28
- this->m_thread.join(); // 等待执行完在销毁
29
- curl_multi_cleanup(this->m_pCURLM);
30
- this->m_pCURLM = nullptr;
31
- }
32
- }
33
-
34
- void BaoCurlMulti::pushQueue(BaoCurl &curl)
35
- {
36
- this->m_lock.lock();
37
- CHECK_CURLMOK(curl_multi_add_handle(this->m_pCURLM, curl.m_pCURL));
38
- this->m_lock.unlock();
39
- }
40
-
41
- void BaoCurlMulti::startThread()
42
- {
43
- auto callback = [&]()
44
- {
45
- while (true)
46
- {
47
- this->m_lock.lock();
48
- bool isRunning = this->m_bRunning;
49
- this->m_lock.unlock();
50
- if (!isRunning)
51
- {
52
- break;
53
- }
54
-
55
- int running;
56
- do
57
- {
58
- this->m_lock.lock();
59
- CURLMcode mc = curl_multi_perform(this->m_pCURLM, &running);
60
- this->m_lock.unlock();
61
- CHECK_CURLMOK(mc);
62
- if (running)
63
- {
64
- mc = curl_multi_poll(this->m_pCURLM, NULL, 0, 1000, NULL);
65
- if (mc)
66
- {
67
-
68
- CHECK_CURLMOK(mc);
69
- break;
70
- }
71
- }
72
- int ret = 0;
73
- while (true)
74
- {
75
- this->m_lock.lock();
76
- CURLMsg *msg = curl_multi_info_read(this->m_pCURLM, &ret);
77
- this->m_lock.unlock();
78
-
79
- if (!(msg && msg->msg == CURLMSG_DONE))
80
- {
81
- break;
82
- }
83
- CURL *handle = msg->easy_handle;
84
-
85
- this->m_lock.lock();
86
- CHECK_CURLMOK(curl_multi_remove_handle(this->m_pCURLM, handle));
87
- BaoCurl *pCurl = nullptr;
88
- curl_easy_getinfo(handle, CURLINFO_PRIVATE, &pCurl);
89
- this->m_lock.unlock();
90
-
91
- if (pCurl)
92
- {
93
- pCurl->m_postdata.reset(); // 释放内存
94
- pCurl->m_lastCode = msg->data.result;
95
- if (pCurl->m_publishCallback)
96
- {
97
- bool success = pCurl->m_lastCode == CURLE_OK;
98
- std::string errMsg = success ? "" : pCurl->getLastCurlCodeError();
99
- auto &callbackPtr = pCurl->m_publishCallback;
100
- (*callbackPtr)(success, errMsg);
101
- callbackPtr.reset(); // 减少引用计数
102
-
103
- /* callback->NonBlockingCall(
104
- [success, callback](Napi::Env env, Napi::Function jsCallback)
105
- {
106
- callback->Unref(env);
107
- jsCallback.Call({Napi::Boolean::New(env, success)});
108
- }); */
109
- }
110
- }
111
- else
112
- {
113
- std::cout << "ptr is nullptr" << std::endl;
114
- }
115
- };
116
-
117
- } while (running);
118
- }
119
- };
120
- this->m_thread = std::thread(callback);
121
- }
122
-
123
- NAMESPACE_BAO_END
@@ -1,511 +0,0 @@
1
- #include <napi.h>
2
- #include <iostream>
3
- #include "bao_curl.h"
4
- #include "request_tls_utils.h"
5
-
6
- using namespace std;
7
- using namespace bao;
8
-
9
- BaoCurlMulti *g_curlMulti = nullptr;
10
-
11
- void initLibCurl()
12
- {
13
- curl_global_init(CURL_GLOBAL_ALL);
14
- g_curlMulti = new BaoCurlMulti();
15
- g_curlMulti->startThread();
16
- }
17
-
18
- void uninitLibCurl()
19
- {
20
- delete g_curlMulti;
21
- curl_global_cleanup();
22
- }
23
-
24
- class BaoLibCurlWarp : public Napi::ObjectWrap<BaoLibCurlWarp>
25
- {
26
- public:
27
- static Napi::Object Init(Napi::Env env, Napi::Object exports);
28
- BaoLibCurlWarp(const Napi::CallbackInfo &info);
29
- ~BaoLibCurlWarp();
30
- // static Napi::Value CreateNewItem(const Napi::CallbackInfo& info);
31
-
32
- private:
33
- BaoCurl m_curl;
34
- Napi::Value open(const Napi::CallbackInfo &info);
35
- Napi::Value setRequestHeader(const Napi::CallbackInfo &info);
36
- Napi::Value setRequestHeaders(const Napi::CallbackInfo &info);
37
- Napi::Value setProxy(const Napi::CallbackInfo &info);
38
- Napi::Value setTimeout(const Napi::CallbackInfo &info);
39
- Napi::Value setCookie(const Napi::CallbackInfo &info);
40
- Napi::Value deleteCookie(const Napi::CallbackInfo &info);
41
- Napi::Value getCookies(const Napi::CallbackInfo &info);
42
- Napi::Value getCookie(const Napi::CallbackInfo &info);
43
- Napi::Value getResponseStatus(const Napi::CallbackInfo &info);
44
- Napi::Value reset(const Napi::CallbackInfo &info);
45
- Napi::Value setRedirect(const Napi::CallbackInfo &info);
46
- Napi::Value printInnerLogger(const Napi::CallbackInfo &info);
47
- Napi::Value setHttpVersion(const Napi::CallbackInfo &info);
48
- Napi::Value setInterface(const Napi::CallbackInfo &info);
49
- Napi::Value setJA3Fingerprint(const Napi::CallbackInfo &info);
50
- Napi::Value send(const Napi::CallbackInfo &info);
51
- Napi::Value sendAsync(const Napi::CallbackInfo &info);
52
- Napi::Value getResponseBody(const Napi::CallbackInfo &info);
53
- Napi::Value getResponseString(const Napi::CallbackInfo &info);
54
- Napi::Value getResponseHeaders(const Napi::CallbackInfo &info);
55
- Napi::Value getResponseContentLength(const Napi::CallbackInfo &info);
56
-
57
- // static Napi::Value multiExecute(const Napi::CallbackInfo &info);
58
- static Napi::Value globalInit(const Napi::CallbackInfo &info);
59
- static Napi::Value globalCleanup(const Napi::CallbackInfo &info);
60
- };
61
-
62
- Napi::Object BaoLibCurlWarp::Init(Napi::Env env, Napi::Object exports)
63
- {
64
- // This method is used to hook the accessor and method callbacks
65
- std::vector<Napi::ClassPropertyDescriptor<BaoLibCurlWarp>> methodList = {
66
- InstanceMethod<&BaoLibCurlWarp::open>("open", static_cast<napi_property_attributes>(napi_writable | napi_configurable)),
67
- InstanceMethod<&BaoLibCurlWarp::setRequestHeader>("setRequestHeader", static_cast<napi_property_attributes>(napi_writable | napi_configurable)),
68
- InstanceMethod<&BaoLibCurlWarp::setRequestHeaders>("setRequestHeaders", static_cast<napi_property_attributes>(napi_writable | napi_configurable)), InstanceMethod<&BaoLibCurlWarp::setProxy>("setProxy", static_cast<napi_property_attributes>(napi_writable | napi_configurable)), InstanceMethod<&BaoLibCurlWarp::setTimeout>("setTimeout", static_cast<napi_property_attributes>(napi_writable | napi_configurable)), InstanceMethod<&BaoLibCurlWarp::setCookie>("setCookie", static_cast<napi_property_attributes>(napi_writable | napi_configurable)), InstanceMethod<&BaoLibCurlWarp::deleteCookie>("deleteCookie", static_cast<napi_property_attributes>(napi_writable | napi_configurable)), InstanceMethod<&BaoLibCurlWarp::getCookies>("getCookies", static_cast<napi_property_attributes>(napi_writable | napi_configurable)), InstanceMethod<&BaoLibCurlWarp::getCookie>("getCookie", static_cast<napi_property_attributes>(napi_writable | napi_configurable)), InstanceMethod<&BaoLibCurlWarp::getResponseStatus>("getResponseStatus", static_cast<napi_property_attributes>(napi_writable | napi_configurable)), InstanceMethod<&BaoLibCurlWarp::reset>("reset", static_cast<napi_property_attributes>(napi_writable | napi_configurable)), InstanceMethod<&BaoLibCurlWarp::setRedirect>("setRedirect", static_cast<napi_property_attributes>(napi_writable | napi_configurable)),
69
- InstanceMethod<&BaoLibCurlWarp::printInnerLogger>("printInnerLogger", static_cast<napi_property_attributes>(napi_writable | napi_configurable)),
70
- InstanceMethod<&BaoLibCurlWarp::setHttpVersion>("setHttpVersion", static_cast<napi_property_attributes>(napi_writable | napi_configurable)),
71
- // InstanceMethod<&BaoLibCurlWarp::send>("send", static_cast<napi_property_attributes>(napi_writable | napi_configurable)),
72
- InstanceMethod<&BaoLibCurlWarp::getResponseBody>("getResponseBody", static_cast<napi_property_attributes>(napi_writable | napi_configurable)),
73
- InstanceMethod<&BaoLibCurlWarp::getResponseString>("getResponseString", static_cast<napi_property_attributes>(napi_writable | napi_configurable)),
74
- InstanceMethod<&BaoLibCurlWarp::sendAsync>("sendAsync", static_cast<napi_property_attributes>(napi_writable | napi_configurable)),
75
- InstanceMethod<&BaoLibCurlWarp::getResponseHeaders>("getResponseHeaders", static_cast<napi_property_attributes>(napi_writable | napi_configurable)),
76
- InstanceMethod<&BaoLibCurlWarp::getResponseContentLength>("getResponseContentLength", static_cast<napi_property_attributes>(napi_writable | napi_configurable)),
77
- InstanceMethod<&BaoLibCurlWarp::setInterface>("setInterface", static_cast<napi_property_attributes>(napi_writable | napi_configurable)),
78
- InstanceMethod<&BaoLibCurlWarp::setJA3Fingerprint>("setJA3Fingerprint", static_cast<napi_property_attributes>(napi_writable | napi_configurable)),
79
- // StaticMethod<&BaoLibCurlWarp::multiExecute>("multiExecute", static_cast<napi_property_attributes>(napi_writable | napi_configurable)),
80
- StaticMethod<&BaoLibCurlWarp::globalInit>("globalInit", static_cast<napi_property_attributes>(napi_writable | napi_configurable)),
81
- StaticMethod<&BaoLibCurlWarp::globalCleanup>("globalCleanup", static_cast<napi_property_attributes>(napi_writable | napi_configurable)),
82
- // StaticMethod<&BaoLibCurlWarp::CreateNewItem>("CreateNewItem", static_cast<napi_property_attributes>(napi_writable | napi_configurable)),
83
- };
84
- Napi::Function func = DefineClass(env, "BaoLibCurl", methodList);
85
- auto constructor = Napi::Persistent(func);
86
- constructor.SuppressDestruct();
87
- exports.Set("BaoLibCurl", func);
88
- return exports;
89
- }
90
-
91
- BaoLibCurlWarp::BaoLibCurlWarp(const Napi::CallbackInfo &info)
92
- : Napi::ObjectWrap<BaoLibCurlWarp>(info)
93
- {
94
- }
95
-
96
- BaoLibCurlWarp::~BaoLibCurlWarp()
97
- {
98
- }
99
-
100
- /*
101
- open(method, url)
102
- */
103
- Napi::Value BaoLibCurlWarp::open(const Napi::CallbackInfo &info)
104
- {
105
- Napi::Env env = info.Env();
106
- size_t argsLen = info.Length();
107
- REQUEST_TLS_METHOD_ARGS_CHECK(env, "BaoCurl", "open", 2, argsLen)
108
- REQUEST_TLS_METHOD_CHECK(env, info[0].IsString(), "argument 0 is not a string")
109
- REQUEST_TLS_METHOD_CHECK(env, info[1].IsString(), "argument 1 is not a string")
110
- string method = info[0].As<Napi::String>().Utf8Value();
111
- string url = info[1].As<Napi::String>().Utf8Value();
112
- this->m_curl.open(method, url);
113
- return env.Undefined();
114
- }
115
-
116
- /*
117
- setRequestHeader(key,value)
118
- */
119
- Napi::Value BaoLibCurlWarp::setRequestHeader(const Napi::CallbackInfo &info)
120
- {
121
- Napi::Env env = info.Env();
122
- size_t argsLen = info.Length();
123
- REQUEST_TLS_METHOD_ARGS_CHECK(env, "BaoCurl", "setRequestHeader", 2, argsLen)
124
- REQUEST_TLS_METHOD_CHECK(env, info[0].IsString(), "argument 0 is not a string")
125
- REQUEST_TLS_METHOD_CHECK(env, info[1].IsString(), "argument 1 is not a string")
126
-
127
- string key = info[0].As<Napi::String>().Utf8Value();
128
- string value = info[1].As<Napi::String>().Utf8Value();
129
- if (value == "")
130
- {
131
- REQUEST_TLS_METHOD_CHECK(env, key != "", "key and value are empty")
132
- // if the value is empty then only set key;
133
- key += ";";
134
- this->m_curl.setRequestHeaders(key);
135
- return env.Undefined();
136
- }
137
- this->m_curl.setRequestHeader(key, value);
138
- return env.Undefined();
139
- }
140
-
141
- /*
142
- setRequestHeaders(headers)
143
- */
144
- Napi::Value BaoLibCurlWarp::setRequestHeaders(const Napi::CallbackInfo &info)
145
- {
146
- Napi::Env env = info.Env();
147
- size_t argsLen = info.Length();
148
- REQUEST_TLS_METHOD_ARGS_CHECK(env, "BaoCurl", "setRequestHeaders", 1, argsLen)
149
- REQUEST_TLS_METHOD_CHECK(env, info[0].IsString(), "argument 0 is not a string")
150
- string headers = info[0].As<Napi::String>().Utf8Value();
151
- this->m_curl.setRequestHeaders(headers);
152
- return env.Undefined();
153
- }
154
-
155
- /*
156
- setProxy(proxy)
157
- setProxy(proxy,username,password)
158
- */
159
- Napi::Value BaoLibCurlWarp::setProxy(const Napi::CallbackInfo &info)
160
- {
161
- Napi::Env env = info.Env();
162
- size_t argsLen = info.Length();
163
- REQUEST_TLS_METHOD_ARGS_TOO_MUCH_CHECK(env, "BaoCurl", "setProxy", 3, argsLen)
164
- REQUEST_TLS_METHOD_CHECK(env, info[0].IsString(), "argument 0 is not a string")
165
- string proxy = info[0].As<Napi::String>().Utf8Value();
166
- if (argsLen == 1)
167
- {
168
- this->m_curl.setProxy(proxy);
169
- return env.Undefined();
170
- }
171
- else if (argsLen == 3)
172
- {
173
- REQUEST_TLS_METHOD_CHECK(env, info[1].IsString(), "argument 1 is not a string")
174
- REQUEST_TLS_METHOD_CHECK(env, info[2].IsString(), "argument 2 is not a string")
175
- // if take the username and password
176
- string username = info[1].As<Napi::String>().Utf8Value();
177
- string password = info[2].As<Napi::String>().Utf8Value();
178
- this->m_curl.setProxy(proxy, username, password);
179
- return env.Undefined();
180
- }
181
- REQUEST_TLS_METHOD_ARGS_NO_CONFIG(env, "BaoCurl", "setProxy", "1 or 2", argsLen)
182
- }
183
-
184
- /*
185
- setTimeout(connectTime, sendTime)
186
- */
187
- Napi::Value BaoLibCurlWarp::setTimeout(const Napi::CallbackInfo &info)
188
- {
189
- Napi::Env env = info.Env();
190
- size_t argsLen = info.Length();
191
- REQUEST_TLS_METHOD_ARGS_CHECK(env, "BaoCurl", "setTimeout", 2, argsLen)
192
- REQUEST_TLS_METHOD_CHECK(env, info[0].IsNumber(), "argument 0 is not a number")
193
- REQUEST_TLS_METHOD_CHECK(env, info[1].IsNumber(), "argument 1 is not a number")
194
- int32_t connectTime = info[0].As<Napi::Number>().Int32Value();
195
- int32_t sendTime = info[1].As<Napi::Number>().Int32Value();
196
- this->m_curl.setTimeout(connectTime, sendTime);
197
- return env.Undefined();
198
- }
199
-
200
- /*
201
- setCookie(key, value, domain)
202
- */
203
- Napi::Value BaoLibCurlWarp::setCookie(const Napi::CallbackInfo &info)
204
- {
205
- Napi::Env env = info.Env();
206
- size_t argsLen = info.Length();
207
- REQUEST_TLS_METHOD_ARGS_CHECK(env, "BaoCurl", "setCookie", 4, argsLen)
208
- REQUEST_TLS_METHOD_CHECK(env, info[0].IsString(), "argument 0 is not a string")
209
- REQUEST_TLS_METHOD_CHECK(env, info[1].IsString(), "argument 1 is not a string")
210
- REQUEST_TLS_METHOD_CHECK(env, info[2].IsString(), "argument 2 is not a string")
211
- REQUEST_TLS_METHOD_CHECK(env, info[3].IsString(), "argument 3 is not a string")
212
- string key = info[0].As<Napi::String>().Utf8Value();
213
- string value = info[1].As<Napi::String>().Utf8Value();
214
- string domain = info[2].As<Napi::String>().Utf8Value();
215
- string path = info[3].As<Napi::String>().Utf8Value();
216
- this->m_curl.setCookie(key, value, domain, path);
217
- return env.Undefined();
218
- }
219
-
220
- /*
221
- deleteCookie(key, domain)
222
- */
223
- Napi::Value BaoLibCurlWarp::deleteCookie(const Napi::CallbackInfo &info)
224
- {
225
- Napi::Env env = info.Env();
226
- size_t argsLen = info.Length();
227
- REQUEST_TLS_METHOD_ARGS_CHECK(env, "BaoCurl", "deleteCookie", 3, argsLen)
228
- string key = info[0].As<Napi::String>().Utf8Value();
229
- string domain = info[1].As<Napi::String>().Utf8Value();
230
- string path = info[2].As<Napi::String>().Utf8Value();
231
- this->m_curl.deleteCookie(key, domain, path);
232
- return env.Undefined();
233
- }
234
-
235
- /*
236
- getCookies()
237
- */
238
- Napi::Value BaoLibCurlWarp::getCookies(const Napi::CallbackInfo &info)
239
- {
240
- Napi::Env env = info.Env();
241
- // size_t argsLen = info.Length();
242
-
243
- return Napi::String::New(env, this->m_curl.getCookies());
244
- }
245
-
246
- /*
247
- getCookie(key)
248
- */
249
- Napi::Value BaoLibCurlWarp::getCookie(const Napi::CallbackInfo &info)
250
- {
251
- Napi::Env env = info.Env();
252
- size_t argsLen = info.Length();
253
- REQUEST_TLS_METHOD_ARGS_CHECK(env, "BaoCurl", "getCookie", 3, argsLen)
254
- REQUEST_TLS_METHOD_CHECK(env, info[0].IsString(), "argument 0 is not a string")
255
- REQUEST_TLS_METHOD_CHECK(env, info[1].IsString(), "argument 1 is not a string")
256
- REQUEST_TLS_METHOD_CHECK(env, info[2].IsString(), "argument 2 is not a string")
257
- std::string key = info[0].As<Napi::String>().Utf8Value();
258
- std::string domain = info[1].As<Napi::String>().Utf8Value();
259
- std::string path = info[2].As<Napi::String>().Utf8Value();
260
- return Napi::String::New(env, this->m_curl.getCookie(key, domain, path));
261
- }
262
-
263
- /*
264
- getResponseStatus()
265
- */
266
- Napi::Value BaoLibCurlWarp::getResponseStatus(const Napi::CallbackInfo &info)
267
- {
268
- Napi::Env env = info.Env();
269
- // size_t argsLen = info.Length();
270
-
271
- return Napi::Number::New(env, this->m_curl.getResponseStatus());
272
- }
273
-
274
- /*
275
- getResponseContentLength()
276
- */
277
- Napi::Value BaoLibCurlWarp::getResponseContentLength(const Napi::CallbackInfo &info)
278
- {
279
- Napi::Env env = info.Env();
280
- // size_t argsLen = info.Length();
281
- return Napi::Number::New(env, this->m_curl.getResponseContentLength());
282
- }
283
-
284
- /*
285
- reset()
286
- */
287
- Napi::Value BaoLibCurlWarp::reset(const Napi::CallbackInfo &info)
288
- {
289
- Napi::Env env = info.Env();
290
- // size_t argsLen = info.Length();
291
-
292
- this->m_curl.reset();
293
- return env.Undefined();
294
- }
295
-
296
- /*
297
- setRedirect(isAllow)
298
- */
299
- Napi::Value BaoLibCurlWarp::setRedirect(const Napi::CallbackInfo &info)
300
- {
301
- Napi::Env env = info.Env();
302
- size_t argsLen = info.Length();
303
- REQUEST_TLS_METHOD_ARGS_CHECK(env, "BaoCurl", "setRedirect", 1, argsLen)
304
- REQUEST_TLS_METHOD_CHECK(env, info[0].IsBoolean(), "argument 0 is not a boolean")
305
- this->m_curl.setRedirect(
306
- info[0].As<Napi::Boolean>().Value());
307
- return env.Undefined();
308
- }
309
-
310
- /*
311
- printInnerLogger()
312
- */
313
- Napi::Value BaoLibCurlWarp::printInnerLogger(const Napi::CallbackInfo &info)
314
- {
315
- Napi::Env env = info.Env();
316
- // size_t argsLen = info.Length();
317
- this->m_curl.printInnerLogger();
318
- return env.Undefined();
319
- }
320
-
321
- /*
322
- setHttpVersion(double)
323
- */
324
- Napi::Value BaoLibCurlWarp::setHttpVersion(const Napi::CallbackInfo &info)
325
- {
326
- Napi::Env env = info.Env();
327
- size_t argsLen = info.Length();
328
- REQUEST_TLS_METHOD_ARGS_CHECK(env, "BaoCurl", "setHttpVersion", 1, argsLen);
329
- REQUEST_TLS_METHOD_CHECK(env, info[0].IsNumber(), "argument 0 is not a number")
330
- int32_t ver = info[0].As<Napi::Number>().Int32Value();
331
- if (ver == 0)
332
- {
333
- this->m_curl.setHttpVersion(BaoCurl::HttpVersion::http1_1);
334
- }
335
- else if (ver == 1)
336
- {
337
- this->m_curl.setHttpVersion(BaoCurl::HttpVersion::http2);
338
- }
339
- else
340
- {
341
- REQUEST_TLS_METHOD_THROW(env, "BaoCurl", "setHttpVersion", "Version Not Support.")
342
- }
343
-
344
- return env.Undefined();
345
- }
346
-
347
- /*
348
- setInterface(string)
349
- */
350
- Napi::Value BaoLibCurlWarp::setInterface(const Napi::CallbackInfo &info)
351
- {
352
- Napi::Env env = info.Env();
353
- size_t argsLen = info.Length();
354
- REQUEST_TLS_METHOD_ARGS_CHECK(env, "BaoCurl", "setInterface", 1, argsLen);
355
- REQUEST_TLS_METHOD_CHECK(env, info[0].IsString(), "argument 0 is not a string")
356
- std::string network = info[0].As<Napi::String>().Utf8Value();
357
- this->m_curl.setInterface(network);
358
-
359
- return env.Undefined();
360
- }
361
-
362
- /*
363
- setJA3Fingerprint(number,string,string,string,number)
364
- */
365
- Napi::Value BaoLibCurlWarp::setJA3Fingerprint(const Napi::CallbackInfo &info)
366
- {
367
- Napi::Env env = info.Env();
368
- size_t argsLen = info.Length();
369
- REQUEST_TLS_METHOD_ARGS_CHECK(env, "BaoCurl", "setJA3Fingerprint", 6, argsLen);
370
- REQUEST_TLS_METHOD_CHECK(env, info[0].IsNumber(), "argument 0 is not a number")
371
- int tlsVersion = info[0].As<Napi::Number>().Int32Value();
372
- REQUEST_TLS_METHOD_CHECK(env, info[1].IsString(), "argument 1 is not a string")
373
- std::string ciphers = info[1].As<Napi::String>().Utf8Value();
374
- REQUEST_TLS_METHOD_CHECK(env, info[2].IsString(), "argument 2 is not a string")
375
- std::string tls13_ciphers = info[2].As<Napi::String>().Utf8Value();
376
- REQUEST_TLS_METHOD_CHECK(env, info[3].IsString(), "argument 3 is not a string")
377
- std::string extensions = info[3].As<Napi::String>().Utf8Value();
378
- REQUEST_TLS_METHOD_CHECK(env, info[4].IsString(), "argument 4 is not a string")
379
- std::string supportGroups = info[4].As<Napi::String>().Utf8Value();
380
- REQUEST_TLS_METHOD_CHECK(env, info[5].IsNumber(), "argument 5 is not a number")
381
- int ecPointFormat = info[5].As<Napi::Number>().Int32Value();
382
- this->m_curl.setJA3Fingerprint(
383
- tlsVersion,
384
- ciphers,
385
- tls13_ciphers,
386
- extensions,
387
- supportGroups,
388
- ecPointFormat);
389
-
390
- return env.Undefined();
391
- }
392
-
393
-
394
- /*
395
- sendAsync()
396
- */
397
- Napi::Value BaoLibCurlWarp::sendAsync(const Napi::CallbackInfo &info)
398
- {
399
- Napi::Env env = info.Env();
400
- size_t argsLen = info.Length();
401
- Napi::Promise::Deferred deferred = Napi::Promise::Deferred::New(env);
402
-
403
- Napi::ThreadSafeFunction *tsfn = new Napi::ThreadSafeFunction;
404
- *tsfn = Napi::ThreadSafeFunction::New(
405
- env,
406
- Napi::Function::New(env, [env, deferred](const Napi::CallbackInfo &info)
407
- {
408
- bool success = info[0].As<Napi::Boolean>().Value();
409
- if (success)
410
- {
411
- deferred.Resolve(env.Undefined());
412
- } else {
413
- Napi::String errMsg = info[1].As<Napi::String>();
414
- deferred.Reject(errMsg);
415
- } }),
416
- "Test", 0, 1, [tsfn](Napi::Env env)
417
- { delete tsfn; });
418
- auto &callbackPtr =
419
- std::make_shared<std::function<void(bool, std::string)>>([tsfn](bool success, std::string errMsg)
420
- { tsfn->NonBlockingCall(
421
- [tsfn, success, errMsg](Napi::Env env, Napi::Function jsCallback)
422
- {
423
- tsfn->Unref(env);
424
- jsCallback.Call({Napi::Boolean::New(env, success), Napi::String::New(env, errMsg.c_str())});
425
- }); });
426
-
427
- this->m_curl.setOnPublishCallback(callbackPtr);
428
-
429
- if (argsLen > 0)
430
- {
431
- if (info[0].IsTypedArray())
432
- {
433
- Napi::Uint8Array u8Arr = info[0].As<Napi::Uint8Array>();
434
- this->m_curl.sendByte(reinterpret_cast<const char *>(u8Arr.Data()), u8Arr.ByteLength());
435
- }
436
- else if (info[0].IsString())
437
- {
438
- string str = info[0].As<Napi::String>().Utf8Value();
439
- size_t strLen = str.size();
440
- this->m_curl.sendByte(str.c_str(), strLen);
441
- }
442
- else
443
- {
444
- vector<Napi::Value> argsList{info[0].As<Napi::Value>()};
445
- string jsonStr = env.Global().Get("JSON").As<Napi::Object>().Get("stringify").As<Napi::Function>().Call(argsList).As<Napi::String>().Utf8Value();
446
- size_t strLen = jsonStr.size();
447
- this->m_curl.sendByte(jsonStr.c_str(), strLen);
448
- }
449
- }
450
- else
451
- {
452
- this->m_curl.sendByte(nullptr, 0);
453
- }
454
-
455
- g_curlMulti->pushQueue(this->m_curl);
456
- return deferred.Promise();
457
- }
458
-
459
- /*
460
- getResponseHeaders()
461
- */
462
- Napi::Value BaoLibCurlWarp::getResponseHeaders(const Napi::CallbackInfo &info)
463
- {
464
- Napi::Env env = info.Env();
465
- // size_t argsLen = info.Length();
466
- return Napi::String::New(env, this->m_curl.getResponseHeaders());
467
- }
468
- /*
469
- getResponseBody()
470
- */
471
- Napi::Value BaoLibCurlWarp::getResponseBody(const Napi::CallbackInfo &info)
472
- {
473
- Napi::Env env = info.Env();
474
- // size_t argsLen = info.Length();
475
- string utf8Str = this->m_curl.getResponseBody();
476
- size_t utf8Size = utf8Str.size();
477
- Napi::Uint8Array uint8buffer = Napi::Uint8Array::New(env, utf8Size);
478
- memcpy((void *)uint8buffer.Data(), utf8Str.c_str(), utf8Size);
479
- return uint8buffer;
480
- }
481
-
482
- /*
483
- getResponseString()
484
- */
485
- Napi::Value BaoLibCurlWarp::getResponseString(const Napi::CallbackInfo &info)
486
- {
487
- Napi::Env env = info.Env();
488
- // size_t argsLen = info.Length();
489
- return Napi::String::New(env, this->m_curl.getResponseBody());
490
- }
491
-
492
- Napi::Value BaoLibCurlWarp::globalInit(const Napi::CallbackInfo &info)
493
- {
494
- initLibCurl();
495
- return info.Env().Undefined();
496
- }
497
-
498
- Napi::Value BaoLibCurlWarp::globalCleanup(const Napi::CallbackInfo &info)
499
- {
500
- uninitLibCurl();
501
- return info.Env().Undefined();
502
- }
503
-
504
- // Initialize native add-on
505
- Napi::Object Init(Napi::Env env, Napi::Object exports)
506
- {
507
-
508
- BaoLibCurlWarp::Init(env, exports);
509
- return exports;
510
- }
511
- NODE_API_MODULE(bao_curl_node_addon, Init)