@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
package/dist/requests.js CHANGED
@@ -1,249 +1,373 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.requests = void 0;
4
- const libcurl_1 = require("./libcurl");
5
- const utils_1 = require("./utils");
6
- class requestsResponse {
7
- curl;
8
- constructor(curl) {
9
- this.curl = curl;
10
- }
11
- get text() {
12
- return this.curl.getResponseString();
13
- }
14
- get json() {
15
- return this.curl.getResponseJson();
16
- }
17
- get buffer() {
18
- return this.curl.getResponseBody();
19
- }
20
- get headers() {
21
- return this.curl.getResponseHeaders();
22
- }
23
- get headersMap() {
24
- return this.curl.getResponseHeadersMap();
25
- }
26
- get status() {
27
- return this.curl.getResponseStatus();
28
- }
29
- get contentLength() {
30
- return this.curl.getResponseContentLength();
31
- }
32
- }
33
- const assignURLSearchParam = (target, source) => {
34
- source.forEach((value, key) => {
35
- target.append(key, value);
36
- });
37
- };
38
- class requests {
39
- option;
40
- needSetCookies;
41
- ja3;
42
- constructor(option = {}) {
43
- this.option = { ...option };
44
- const { cookies, timeout, verbose, redirect = false, proxy, httpVersion, interface: interface_, ja3 } = option;
45
- const curl = this.option.instance ||= new libcurl_1.LibCurl();
46
- if (cookies) {
47
- this.needSetCookies = !!cookies;
48
- }
49
- if (timeout) {
50
- curl.setTimeout(timeout, timeout);
51
- }
52
- if (verbose) {
53
- curl.printInnerLogger();
54
- }
55
- if (interface_) {
56
- curl.setInterface(interface_);
57
- }
58
- if (typeof httpVersion != 'undefined') {
59
- curl.setHttpVersion(httpVersion);
60
- }
61
- if (redirect) {
62
- curl.setRedirect(true);
63
- }
64
- if (proxy) {
65
- curl.setProxy(proxy);
66
- }
67
- this.ja3 = ja3 || (0, utils_1.libcurlRandomJA3Fingerprint)();
68
- curl.setJA3Fingerprint(this.ja3);
69
- }
70
- static session(option = {}) {
71
- return new requests(option);
72
- }
73
- static async get(url, requestOpt) {
74
- return requests.sendRequestStaic('GET', url, requestOpt);
75
- }
76
- static async post(url, requestOpt) {
77
- return requests.sendRequestStaic('POST', url, requestOpt);
78
- }
79
- static async put(url, requestOpt) {
80
- return requests.sendRequestStaic('PUT', url, requestOpt);
81
- }
82
- static async patch(url, requestOpt) {
83
- return requests.sendRequestStaic('PATCH', url, requestOpt);
84
- }
85
- static async trace(url, requestOpt) {
86
- return requests.sendRequestStaic('TRACE', url, requestOpt);
87
- }
88
- static async head(url, requestOpt) {
89
- return requests.sendRequestStaic('HEAD', url, requestOpt);
90
- }
91
- async get(url, requestOpt) {
92
- return this.sendRequest('GET', url, requestOpt);
93
- }
94
- async post(url, requestOpt) {
95
- return this.sendRequest('POST', url, requestOpt);
96
- }
97
- async put(url, requestOpt) {
98
- return this.sendRequest('PUT', url, requestOpt);
99
- }
100
- async patch(url, requestOpt) {
101
- return this.sendRequest('PATCH', url, requestOpt);
102
- }
103
- async trace(url, requestOpt) {
104
- return this.sendRequest('TRACE', url, requestOpt);
105
- }
106
- async head(url, requestOpt) {
107
- return this.sendRequest('HEAD', url, requestOpt);
108
- }
109
- setCookie(key, value, domain, path = '') {
110
- this.option.instance.setCookie({
111
- name: key,
112
- value,
113
- domain,
114
- path,
115
- });
116
- }
117
- getCookie(key, domain, path) {
118
- return this.option.instance.getCookie({
119
- name: key,
120
- domain: domain || "",
121
- path: path || "",
122
- });
123
- }
124
- getCookies(domain, path) {
125
- if (arguments.length == 0) {
126
- return this.option.instance.getCookies();
127
- }
128
- return this.option.instance.getCookies({
129
- domain: domain || "",
130
- path: path || "",
131
- });
132
- }
133
- getCookiesMap(domain, path) {
134
- if (arguments.length == 0) {
135
- return this.option.instance.getCookiesMap();
136
- }
137
- return this.option.instance.getCookiesMap({
138
- domain: domain || "",
139
- path: path || "",
140
- });
141
- }
142
- deleteCookie(key, domain, path) {
143
- this.option.instance.deleteCookie({
144
- name: key,
145
- domain: domain,
146
- path: path || "/",
147
- });
148
- }
149
- getJA3Fingerprint() {
150
- return {
151
- ja3: this.ja3,
152
- ja3_hash: (0, utils_1.md5)(this.ja3),
153
- };
154
- }
155
- async sendRequest(method, url, requestOpt) {
156
- const { instance: curl, cookies, timeout: timeoutOpt } = this.option;
157
- const { headers, data, json, params, timeout } = requestOpt || {};
158
- if (data && json) {
159
- throw new libcurl_1.LibCurlError('both data and json exist');
160
- }
161
- const url_ = new URL(url);
162
- if (this.needSetCookies) {
163
- this.needSetCookies = false;
164
- (0, utils_1.libcurlSetCookies)(curl, cookies, url_.hostname.split('.').slice(-2).join('.'));
165
- }
166
- if (params) {
167
- assignURLSearchParam(url_.searchParams, new URLSearchParams(params));
168
- }
169
- curl.open(method, url_);
170
- if (headers) {
171
- curl.setRequestHeaders(headers);
172
- }
173
- if (timeout) {
174
- curl.setTimeout(timeout, timeout);
175
- }
176
- else if (timeoutOpt) {
177
- curl.setTimeout(timeoutOpt, timeoutOpt);
178
- }
179
- let hasContentType = false;
180
- if (headers && (data || json)) {
181
- const contentTypeFilter = (e) => e.some(e => e.toLocaleLowerCase() == 'content-type');
182
- if (typeof headers == 'string') {
183
- hasContentType = /content-type/i.test(headers);
184
- }
185
- else if (headers instanceof Map) {
186
- hasContentType = contentTypeFilter([...headers.keys()]);
187
- }
188
- else {
189
- hasContentType = contentTypeFilter(Object.keys(headers));
190
- }
191
- }
192
- if (json) {
193
- if (!hasContentType) {
194
- curl.setRequestHeader('Content-Type', 'application/json');
195
- }
196
- await curl.send(json);
197
- }
198
- else if (data) {
199
- let sendData = data;
200
- if (!hasContentType) {
201
- if (typeof data == 'string') {
202
- curl.setRequestHeader('Content-Type', 'text/plain');
203
- }
204
- else if (data instanceof URLSearchParams) {
205
- curl.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
206
- }
207
- else if (data instanceof Uint8Array) {
208
- curl.setRequestHeader('Content-Type', 'application/octet-stream');
209
- }
210
- else {
211
- curl.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
212
- }
213
- }
214
- if (data instanceof Uint8Array) {
215
- }
216
- else if (!(data instanceof URLSearchParams) &&
217
- typeof data == 'object' && data != null) {
218
- sendData = Object.keys(data).map((e) => {
219
- const value = data[e];
220
- const type = typeof value;
221
- if (['object', 'boolean'].includes(type)) {
222
- return [e, JSON.stringify(value)];
223
- }
224
- else if (type == 'undefined') {
225
- return [e, ''];
226
- }
227
- else if (['string', 'number'].includes(type)) {
228
- return [e, value + ''];
229
- }
230
- else {
231
- throw new libcurl_1.LibCurlError(`data unkown type ${type}`);
232
- }
233
- })
234
- .map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`)
235
- .join('&');
236
- }
237
- await curl.send(sendData);
238
- }
239
- else {
240
- await curl.send();
241
- }
242
- return new requestsResponse(curl);
243
- }
244
- static async sendRequestStaic(method, url, requestStaticOpt) {
245
- return requests.session(requestStaticOpt).sendRequest(method, url, requestStaticOpt);
246
- }
247
- }
248
- exports.requests = requests;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.requests = void 0;
4
+ const libcurl_1 = require("./libcurl");
5
+ const utils_1 = require("./utils");
6
+ class requestsResponse {
7
+ curl;
8
+ constructor(curl) {
9
+ this.curl = curl;
10
+ }
11
+ get text() {
12
+ return this.curl.getResponseString();
13
+ }
14
+ get json() {
15
+ return this.curl.getResponseJson();
16
+ }
17
+ get buffer() {
18
+ return this.curl.getResponseBody();
19
+ }
20
+ get headers() {
21
+ return this.curl.getResponseHeaders();
22
+ }
23
+ get headersMap() {
24
+ return this.curl.getResponseHeadersMap();
25
+ }
26
+ get status() {
27
+ return this.curl.getResponseStatus();
28
+ }
29
+ get contentLength() {
30
+ return this.curl.getResponseContentLength();
31
+ }
32
+ }
33
+ const assignURLSearchParam = (target, source) => {
34
+ source.forEach((value, key) => {
35
+ target.append(key, value);
36
+ });
37
+ };
38
+ const ja3Md5Map = new Map();
39
+ class requests {
40
+ option;
41
+ needSetCookies;
42
+ lastJa3;
43
+ randomJa3;
44
+ retryOption = {
45
+ retryNum: 0,
46
+ conditionCallback(resp) {
47
+ const { status } = resp;
48
+ return status >= 200 && status < 400;
49
+ }
50
+ };
51
+ constructor(option = {}) {
52
+ this.option = { ...option };
53
+ const { cookies, timeout, verbose, redirect = false, proxy, httpVersion, interface: interface_, ja3 } = option;
54
+ const curl = this.option.instance ||= new libcurl_1.LibCurl();
55
+ switch (typeof cookies) {
56
+ case 'string':
57
+ this.needSetCookies = !!cookies;
58
+ break;
59
+ case 'object':
60
+ if (cookies !== null) {
61
+ if (cookies.value) {
62
+ if (cookies.uri) {
63
+ (0, utils_1.libcurlSetCookies)(curl, cookies.value, (0, utils_1.getUriTopLevelHost)(cookies.uri));
64
+ }
65
+ else {
66
+ this.needSetCookies = !!cookies;
67
+ }
68
+ }
69
+ }
70
+ break;
71
+ default:
72
+ break;
73
+ }
74
+ if (timeout) {
75
+ curl.setTimeout(timeout, timeout);
76
+ }
77
+ if (verbose) {
78
+ curl.printInnerLogger();
79
+ }
80
+ if (interface_) {
81
+ curl.setInterface(interface_);
82
+ }
83
+ if (typeof httpVersion != 'undefined') {
84
+ curl.setHttpVersion(httpVersion);
85
+ }
86
+ if (redirect) {
87
+ curl.setRedirect(true);
88
+ }
89
+ if (proxy) {
90
+ curl.setProxy(proxy);
91
+ }
92
+ this.randomJa3 = !!ja3;
93
+ if (ja3) {
94
+ this.lastJa3 = ja3;
95
+ }
96
+ }
97
+ static session(option = {}) {
98
+ return new requests(option);
99
+ }
100
+ async sendRequest(method, url, requestOpt) {
101
+ const { instance: curl, cookies, timeout: timeoutOpt, ja3 } = this.option;
102
+ const { headers, data, json, params, timeout, interface: interface_, redirect, proxy, httpVersion } = requestOpt || {};
103
+ if (data && json) {
104
+ throw new libcurl_1.LibCurlError('both data and json exist');
105
+ }
106
+ const url_ = new URL(url);
107
+ if (this.needSetCookies) {
108
+ this.needSetCookies = false;
109
+ (0, utils_1.libcurlSetCookies)(curl, cookies, (0, utils_1.getUriTopLevelHost)(url_));
110
+ }
111
+ if (params) {
112
+ assignURLSearchParam(url_.searchParams, new URLSearchParams(params));
113
+ }
114
+ curl.open(method, url_);
115
+ if (headers) {
116
+ curl.setRequestHeaders(headers);
117
+ }
118
+ if (typeof timeout == 'number') {
119
+ curl.setTimeout(timeout, timeout);
120
+ }
121
+ else if (timeoutOpt) {
122
+ curl.setTimeout(timeoutOpt, timeoutOpt);
123
+ }
124
+ if (typeof interface_ == 'string') {
125
+ curl.setInterface(interface_);
126
+ }
127
+ else {
128
+ if (this.option.interface) {
129
+ curl.setInterface(this.option.interface);
130
+ }
131
+ }
132
+ if (typeof redirect == 'boolean') {
133
+ curl.setRedirect(redirect);
134
+ }
135
+ else {
136
+ if (this.option.redirect) {
137
+ curl.setRedirect(this.option.redirect);
138
+ }
139
+ }
140
+ if (proxy) {
141
+ curl.setProxy(proxy);
142
+ }
143
+ else {
144
+ if (this.option.proxy) {
145
+ curl.setProxy(this.option.proxy);
146
+ }
147
+ }
148
+ if (typeof httpVersion == 'number') {
149
+ curl.setHttpVersion(httpVersion);
150
+ }
151
+ else {
152
+ if (this.option.httpVersion) {
153
+ curl.setHttpVersion(this.option.httpVersion);
154
+ }
155
+ }
156
+ if (ja3) {
157
+ curl.setJA3Fingerprint(ja3);
158
+ }
159
+ else {
160
+ if (this.randomJa3) {
161
+ this.lastJa3 = (0, utils_1.libcurlRandomJA3Fingerprint)();
162
+ curl.setJA3Fingerprint(this.lastJa3);
163
+ }
164
+ }
165
+ let hasContentType = false;
166
+ if (headers && (data || json)) {
167
+ const contentTypeFilter = (e) => e.some(e => e.toLocaleLowerCase() == 'content-type');
168
+ if (typeof headers == 'string') {
169
+ hasContentType = /content-type/i.test(headers);
170
+ }
171
+ else if (headers instanceof Map) {
172
+ hasContentType = contentTypeFilter([...headers.keys()]);
173
+ }
174
+ else {
175
+ hasContentType = contentTypeFilter(Object.keys(headers));
176
+ }
177
+ }
178
+ if (json) {
179
+ if (!hasContentType) {
180
+ curl.setRequestHeader('Content-Type', 'application/json');
181
+ }
182
+ await curl.send(json);
183
+ }
184
+ else if (data) {
185
+ let sendData = data;
186
+ if (!hasContentType) {
187
+ if (typeof data == 'string') {
188
+ curl.setRequestHeader('Content-Type', 'text/plain');
189
+ }
190
+ else if (data instanceof URLSearchParams) {
191
+ curl.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
192
+ }
193
+ else if (data instanceof Uint8Array) {
194
+ curl.setRequestHeader('Content-Type', 'application/octet-stream');
195
+ }
196
+ else {
197
+ curl.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
198
+ }
199
+ }
200
+ if (data instanceof Uint8Array) {
201
+ }
202
+ else if (!(data instanceof URLSearchParams) &&
203
+ typeof data == 'object' && data != null) {
204
+ sendData = Object.keys(data).map((e) => {
205
+ const value = data[e];
206
+ const type = typeof value;
207
+ if (['object', 'boolean'].includes(type)) {
208
+ return [e, JSON.stringify(value)];
209
+ }
210
+ else if (type == 'undefined') {
211
+ return [e, ''];
212
+ }
213
+ else if (['string', 'number'].includes(type)) {
214
+ return [e, value + ''];
215
+ }
216
+ else {
217
+ throw new libcurl_1.LibCurlError(`data unkown type ${type}`);
218
+ }
219
+ })
220
+ .map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`)
221
+ .join('&');
222
+ }
223
+ await curl.send(sendData);
224
+ }
225
+ else {
226
+ await curl.send();
227
+ }
228
+ return new requestsResponse(curl);
229
+ }
230
+ static async sendRequestStaic(method, url, requestStaticOpt) {
231
+ return requests.session(requestStaticOpt).sendRequestRetry(method, url, requestStaticOpt);
232
+ }
233
+ async sendRequestRetry(method, url, requestOpt) {
234
+ let isSuccess = false, resp;
235
+ const { retryNum, conditionCallback } = this.retryOption;
236
+ if (retryNum == 0) {
237
+ return this.sendRequest(method, url, requestOpt);
238
+ }
239
+ for (let i = 0; i <= retryNum; i++) {
240
+ try {
241
+ resp = await this.sendRequest(method, url, requestOpt);
242
+ isSuccess = conditionCallback(resp);
243
+ if (isSuccess) {
244
+ break;
245
+ }
246
+ }
247
+ catch (error) {
248
+ }
249
+ }
250
+ if (!isSuccess) {
251
+ throw new libcurl_1.LibCurlError(`failed after ${retryNum} retries`);
252
+ }
253
+ return resp;
254
+ }
255
+ static async get(url, requestOpt) {
256
+ return requests.sendRequestStaic('GET', url, requestOpt);
257
+ }
258
+ static async post(url, requestOpt) {
259
+ return requests.sendRequestStaic('POST', url, requestOpt);
260
+ }
261
+ static async put(url, requestOpt) {
262
+ return requests.sendRequestStaic('PUT', url, requestOpt);
263
+ }
264
+ static async patch(url, requestOpt) {
265
+ return requests.sendRequestStaic('PATCH', url, requestOpt);
266
+ }
267
+ static async trace(url, requestOpt) {
268
+ return requests.sendRequestStaic('TRACE', url, requestOpt);
269
+ }
270
+ static async head(url, requestOpt) {
271
+ return requests.sendRequestStaic('HEAD', url, requestOpt);
272
+ }
273
+ static async delete(url, requestOpt) {
274
+ return requests.sendRequestStaic('DELETE', url, requestOpt);
275
+ }
276
+ async get(url, requestOpt) {
277
+ return this.sendRequestRetry('GET', url, requestOpt);
278
+ }
279
+ async post(url, requestOpt) {
280
+ return this.sendRequestRetry('POST', url, requestOpt);
281
+ }
282
+ async put(url, requestOpt) {
283
+ return this.sendRequestRetry('PUT', url, requestOpt);
284
+ }
285
+ async patch(url, requestOpt) {
286
+ return this.sendRequestRetry('PATCH', url, requestOpt);
287
+ }
288
+ async trace(url, requestOpt) {
289
+ return this.sendRequestRetry('TRACE', url, requestOpt);
290
+ }
291
+ async head(url, requestOpt) {
292
+ return this.sendRequestRetry('HEAD', url, requestOpt);
293
+ }
294
+ async delete(url, requestOpt) {
295
+ return this.sendRequestRetry('DELETE', url, requestOpt);
296
+ }
297
+ setCookie(key, value, domain, path = '') {
298
+ this.option.instance.setCookie({
299
+ name: key,
300
+ value,
301
+ domain,
302
+ path,
303
+ });
304
+ }
305
+ getCookie(key, domain, path) {
306
+ return this.option.instance.getCookie({
307
+ name: key,
308
+ domain: domain || "",
309
+ path: path || "",
310
+ });
311
+ }
312
+ getCookies(domain, path) {
313
+ if (arguments.length == 0) {
314
+ return this.option.instance.getCookies();
315
+ }
316
+ return this.option.instance.getCookies({
317
+ domain: domain || "",
318
+ path: path || "",
319
+ });
320
+ }
321
+ getCookiesMap(domain, path) {
322
+ if (arguments.length == 0) {
323
+ return this.option.instance.getCookiesMap();
324
+ }
325
+ return this.option.instance.getCookiesMap({
326
+ domain: domain || "",
327
+ path: path || "",
328
+ });
329
+ }
330
+ deleteCookie(key, domain, path) {
331
+ this.option.instance.deleteCookie({
332
+ name: key,
333
+ domain: domain,
334
+ path: path || "/",
335
+ });
336
+ }
337
+ getJA3Fingerprint() {
338
+ if (!this.lastJa3) {
339
+ return {
340
+ ja3: '',
341
+ ja3_hash: '',
342
+ };
343
+ }
344
+ if (!ja3Md5Map.has(this.lastJa3)) {
345
+ const ja3_hash = (0, utils_1.md5)(this.lastJa3);
346
+ ja3Md5Map.set(this.lastJa3, ja3_hash);
347
+ return {
348
+ ja3: this.lastJa3,
349
+ ja3_hash,
350
+ };
351
+ }
352
+ const ja3_hash = ja3Md5Map.get(this.lastJa3);
353
+ return {
354
+ ja3: this.lastJa3,
355
+ ja3_hash,
356
+ };
357
+ }
358
+ retry(retryNum, conditionCallback) {
359
+ if (retryNum < 0) {
360
+ throw new libcurl_1.LibCurlError('retryNum must be great than 0');
361
+ }
362
+ const rq = requests.session({
363
+ ...this.option,
364
+ });
365
+ rq.retryOption.retryNum = retryNum;
366
+ if (typeof conditionCallback == 'function') {
367
+ rq.retryOption.conditionCallback = conditionCallback;
368
+ }
369
+ return rq;
370
+ }
371
+ }
372
+ exports.requests = requests;
249
373
  //# sourceMappingURL=requests.js.map