@ossiana/node-libcurl 1.0.7-alpha-3 → 1.0.8-alpha-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 (50) hide show
  1. package/binding.gyp +15 -40
  2. package/dist/export.d.ts +2 -1
  3. package/dist/export.js +3 -1
  4. package/dist/export.js.map +1 -1
  5. package/dist/fetch.d.ts +4 -13
  6. package/dist/fetch.js +33 -68
  7. package/dist/fetch.js.map +1 -1
  8. package/dist/libcurl.d.ts +64 -11
  9. package/dist/libcurl.js +93 -48
  10. package/dist/libcurl.js.map +1 -1
  11. package/dist/requests.d.ts +66 -0
  12. package/dist/requests.js +186 -0
  13. package/dist/requests.js.map +1 -0
  14. package/dist/utils.d.ts +4 -0
  15. package/dist/utils.js +69 -0
  16. package/dist/utils.js.map +1 -0
  17. package/package.json +16 -6
  18. package/readme.md +28 -13
  19. package/src/export.ts +3 -3
  20. package/src/fetch.ts +38 -86
  21. package/src/libcurl/bao_curl.cpp +58 -22
  22. package/src/libcurl/bao_curl_node_addon.cpp +20 -13
  23. package/src/libcurl/curlAsyncWorker.cpp +6 -2
  24. package/src/libcurl/include/bao_curl.h +41 -38
  25. package/src/libcurl/include/curlAysncWorker.h +2 -0
  26. package/src/libcurl.ts +154 -59
  27. package/src/requests.ts +211 -0
  28. package/src/utils.ts +71 -0
  29. package/tsconfig.json +1 -0
  30. package/dist/src/export.d.ts +0 -3
  31. package/dist/src/export.js +0 -8
  32. package/dist/src/export.js.map +0 -1
  33. package/dist/src/fetch.d.ts +0 -32
  34. package/dist/src/fetch.js +0 -90
  35. package/dist/src/fetch.js.map +0 -1
  36. package/dist/src/index.d.ts +0 -1
  37. package/dist/src/index.js +0 -18
  38. package/dist/src/index.js.map +0 -1
  39. package/dist/src/libcurl.d.ts +0 -31
  40. package/dist/src/libcurl.js +0 -154
  41. package/dist/src/libcurl.js.map +0 -1
  42. package/index.js +0 -1
  43. package/src/libcurl/lib/Release/darwin/libcrypto.a +0 -0
  44. package/src/libcurl/lib/Release/darwin/libcurl.a +0 -0
  45. package/src/libcurl/lib/Release/darwin/libssl.a +0 -0
  46. package/src/libcurl/lib/Release/linux/libcrypto.a +0 -0
  47. package/src/libcurl/lib/Release/linux/libcurl.a +0 -0
  48. package/src/libcurl/lib/Release/linux/libssl.a +0 -0
  49. package/src/libcurl/lib/Release/windows/libcurl.dll +0 -0
  50. package/src/libcurl/lib/Release/windows/libcurl_imp.lib +0 -0
@@ -0,0 +1,66 @@
1
+ import { LibCurl, LibCurlBodyInfo, LibCurlCookiesAttr, LibCurlCookiesInfo, LibCurlHeadersAttr, LibCurlHeadersInfo, LibCurlProxyInfo, LibCurlHttpVersionInfo, LibCurlURLInfo } from "./libcurl";
2
+ type requestsHttpVersionInfo = LibCurlHttpVersionInfo;
3
+ type requestsHeadersInfo = LibCurlHeadersInfo;
4
+ type requestsBodyInfo = LibCurlBodyInfo;
5
+ type requestsCookiesInfo = LibCurlCookiesInfo;
6
+ type requestsProxyInfo = LibCurlProxyInfo;
7
+ type requestsURLInfo = LibCurlURLInfo;
8
+ interface requestsResponseImp {
9
+ readonly text: string;
10
+ readonly json: object;
11
+ readonly buffer: Uint8Array;
12
+ readonly headers: string;
13
+ readonly headersMap: LibCurlHeadersAttr;
14
+ readonly status: number;
15
+ }
16
+ declare class requestsResponse implements requestsResponseImp {
17
+ private curl;
18
+ constructor(curl: LibCurl);
19
+ get text(): string;
20
+ get json(): object;
21
+ get buffer(): Uint8Array;
22
+ get headers(): string;
23
+ get headersMap(): LibCurlHeadersAttr;
24
+ get status(): number;
25
+ }
26
+ interface requestsInitOption {
27
+ redirect?: boolean;
28
+ cookies?: requestsCookiesInfo;
29
+ proxy?: requestsProxyInfo;
30
+ body?: requestsBodyInfo;
31
+ httpVersion?: requestsHttpVersionInfo;
32
+ timeout?: number;
33
+ instance?: LibCurl;
34
+ }
35
+ type requestsParamsInfo = URLSearchParams | string | {
36
+ [key: string]: string;
37
+ };
38
+ interface requestsOption {
39
+ headers?: requestsHeadersInfo;
40
+ body?: requestsBodyInfo;
41
+ params?: requestsParamsInfo;
42
+ }
43
+ export declare class requests {
44
+ private option;
45
+ constructor(option?: requestsInitOption);
46
+ static session(option?: requestsInitOption): requests;
47
+ static get(url: requestsURLInfo, requestOpt?: requestsOption): Promise<requestsResponse>;
48
+ static post(url: requestsURLInfo, requestOpt?: requestsOption): Promise<requestsResponse>;
49
+ static put(url: requestsURLInfo, requestOpt?: requestsOption): Promise<requestsResponse>;
50
+ static patch(url: requestsURLInfo, requestOpt?: requestsOption): Promise<requestsResponse>;
51
+ static trace(url: requestsURLInfo, requestOpt?: requestsOption): Promise<requestsResponse>;
52
+ static head(url: requestsURLInfo, requestOpt?: requestsOption): Promise<requestsResponse>;
53
+ get(url: requestsURLInfo, requestOpt?: requestsOption): Promise<requestsResponse>;
54
+ post(url: requestsURLInfo, requestOpt?: requestsOption): Promise<requestsResponse>;
55
+ put(url: requestsURLInfo, requestOpt?: requestsOption): Promise<requestsResponse>;
56
+ patch(url: requestsURLInfo, requestOpt?: requestsOption): Promise<requestsResponse>;
57
+ trace(url: requestsURLInfo, requestOpt?: requestsOption): Promise<requestsResponse>;
58
+ head(url: requestsURLInfo, requestOpt?: requestsOption): Promise<requestsResponse>;
59
+ setCookie(key: string, value: string, domain?: string, path?: string): void;
60
+ getCookie(key: string, domain?: string, path?: string): string;
61
+ getCookies(domain?: string, path?: string): string;
62
+ getCookiesMap(domain?: string, path?: string): LibCurlCookiesAttr;
63
+ deleteCookie(key: string, domain: string, path?: string): void;
64
+ private sendRequest;
65
+ }
66
+ export {};
@@ -0,0 +1,186 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.requests = void 0;
13
+ const libcurl_1 = require("./libcurl");
14
+ const utils_1 = require("./utils");
15
+ class requestsResponse {
16
+ constructor(curl) {
17
+ this.curl = curl;
18
+ }
19
+ get text() {
20
+ return this.curl.getResponseString();
21
+ }
22
+ get json() {
23
+ return this.curl.getResponseJson();
24
+ }
25
+ get buffer() {
26
+ return this.curl.getResponseBody();
27
+ }
28
+ get headers() {
29
+ return this.curl.getResponseHeaders();
30
+ }
31
+ get headersMap() {
32
+ return this.curl.getResponseHeadersMap();
33
+ }
34
+ get status() {
35
+ return this.curl.getResponseStatus();
36
+ }
37
+ }
38
+ const assignURLSearchParam = (target, source) => {
39
+ source.forEach((value, key) => {
40
+ target.append(key, value);
41
+ });
42
+ };
43
+ class requests {
44
+ constructor(option = {}) {
45
+ var _a;
46
+ this.option = Object.assign({}, option);
47
+ const { cookies, timeout } = option;
48
+ const curl = (_a = this.option).instance || (_a.instance = new libcurl_1.LibCurl());
49
+ if (cookies) {
50
+ (0, utils_1.libcurlSetCookies)(curl, cookies, '.');
51
+ }
52
+ if (timeout) {
53
+ curl.setTimeout(timeout, timeout);
54
+ }
55
+ }
56
+ static session(option = {}) {
57
+ return new requests(option);
58
+ }
59
+ static get(url, requestOpt) {
60
+ return __awaiter(this, void 0, void 0, function* () {
61
+ return requests.session().sendRequest('GET', url, requestOpt);
62
+ });
63
+ }
64
+ static post(url, requestOpt) {
65
+ return __awaiter(this, void 0, void 0, function* () {
66
+ return requests.session().sendRequest('POST', url, requestOpt);
67
+ });
68
+ }
69
+ static put(url, requestOpt) {
70
+ return __awaiter(this, void 0, void 0, function* () {
71
+ return requests.session().sendRequest('PUT', url, requestOpt);
72
+ });
73
+ }
74
+ static patch(url, requestOpt) {
75
+ return __awaiter(this, void 0, void 0, function* () {
76
+ return requests.session().sendRequest('PATCH', url, requestOpt);
77
+ });
78
+ }
79
+ static trace(url, requestOpt) {
80
+ return __awaiter(this, void 0, void 0, function* () {
81
+ return requests.session().sendRequest('TRACE', url, requestOpt);
82
+ });
83
+ }
84
+ static head(url, requestOpt) {
85
+ return __awaiter(this, void 0, void 0, function* () {
86
+ return requests.session().sendRequest('HEAD', url, requestOpt);
87
+ });
88
+ }
89
+ get(url, requestOpt) {
90
+ return __awaiter(this, void 0, void 0, function* () {
91
+ return this.sendRequest('GET', url, requestOpt);
92
+ });
93
+ }
94
+ post(url, requestOpt) {
95
+ return __awaiter(this, void 0, void 0, function* () {
96
+ return this.sendRequest('POST', url, requestOpt);
97
+ });
98
+ }
99
+ put(url, requestOpt) {
100
+ return __awaiter(this, void 0, void 0, function* () {
101
+ return this.sendRequest('PUT', url, requestOpt);
102
+ });
103
+ }
104
+ patch(url, requestOpt) {
105
+ return __awaiter(this, void 0, void 0, function* () {
106
+ return this.sendRequest('PATCH', url, requestOpt);
107
+ });
108
+ }
109
+ trace(url, requestOpt) {
110
+ return __awaiter(this, void 0, void 0, function* () {
111
+ return this.sendRequest('TRACE', url, requestOpt);
112
+ });
113
+ }
114
+ head(url, requestOpt) {
115
+ return __awaiter(this, void 0, void 0, function* () {
116
+ return this.sendRequest('HEAD', url, requestOpt);
117
+ });
118
+ }
119
+ setCookie(key, value, domain = '', path = '') {
120
+ this.option.instance.setCookie({
121
+ name: key,
122
+ value,
123
+ domain,
124
+ path,
125
+ });
126
+ }
127
+ getCookie(key, domain, path) {
128
+ return this.option.instance.getCookie({
129
+ name: key,
130
+ domain: domain || "",
131
+ path: path || "",
132
+ });
133
+ }
134
+ getCookies(domain, path) {
135
+ if (arguments.length == 0) {
136
+ return this.option.instance.getCookies();
137
+ }
138
+ return this.option.instance.getCookies({
139
+ domain: domain || "",
140
+ path: path || "",
141
+ });
142
+ }
143
+ getCookiesMap(domain, path) {
144
+ if (arguments.length == 0) {
145
+ return this.option.instance.getCookiesMap();
146
+ }
147
+ return this.option.instance.getCookiesMap({
148
+ domain: domain || "",
149
+ path: path || "",
150
+ });
151
+ }
152
+ deleteCookie(key, domain, path) {
153
+ this.option.instance.deleteCookie({
154
+ name: key,
155
+ domain: domain,
156
+ path: path || "/",
157
+ });
158
+ }
159
+ sendRequest(method, url, requestOpt) {
160
+ return __awaiter(this, void 0, void 0, function* () {
161
+ const { instance: curl, redirect = false, proxy, httpVersion } = this.option;
162
+ const { headers, body, params } = requestOpt || {};
163
+ const url_ = new URL(url);
164
+ if (params) {
165
+ assignURLSearchParam(url_.searchParams, new URLSearchParams(params));
166
+ }
167
+ curl.open(method, url_, true);
168
+ if (headers) {
169
+ curl.setRequestHeaders(headers);
170
+ }
171
+ if (redirect) {
172
+ curl.setRedirect(true);
173
+ }
174
+ if (httpVersion) {
175
+ curl.setHttpVersion(httpVersion);
176
+ }
177
+ if (proxy) {
178
+ curl.setProxy(proxy);
179
+ }
180
+ yield curl.send(body);
181
+ return new requestsResponse(curl);
182
+ });
183
+ }
184
+ }
185
+ exports.requests = requests;
186
+ //# sourceMappingURL=requests.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"requests.js","sourceRoot":"","sources":["../src/requests.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,uCAAiN;AACjN,mCAA4C;AAoB5C,MAAM,gBAAgB;IAElB,YAAY,IAAa;QACrB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IACrB,CAAC;IAED,IAAW,IAAI;QACX,OAAO,IAAI,CAAC,IAAI,CAAC,iBAAiB,EAAE,CAAC;IACzC,CAAC;IAED,IAAW,IAAI;QACX,OAAO,IAAI,CAAC,IAAI,CAAC,eAAe,EAAE,CAAC;IACvC,CAAC;IAED,IAAW,MAAM;QACb,OAAO,IAAI,CAAC,IAAI,CAAC,eAAe,EAAE,CAAC;IACvC,CAAC;IAED,IAAW,OAAO;QACd,OAAO,IAAI,CAA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Q,CAAC,KAAK,CAAC,CAAC;aACxB;YACD,MAAM,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;YACtB,OAAO,IAAI,gBAAgB,CAAC,IAAI,CAAC,CAAC;QACtC,CAAC;KAAA;CACJ;AA7HD,4BA6HC"}
@@ -0,0 +1,4 @@
1
+ import { LibCurl, LibCurlCookieAttrArray, LibCurlCookiesInfo, LibCurlGetCookiesOption } from "./libcurl";
2
+ export declare const httpCookiesToArray: (cookies: string) => LibCurlCookieAttrArray[];
3
+ export declare const cookieOptFilter: (cookieOpt: LibCurlGetCookiesOption) => (e: LibCurlCookieAttrArray) => boolean;
4
+ export declare const libcurlSetCookies: (curl: LibCurl, cookies: LibCurlCookiesInfo, domain: string) => void;
package/dist/utils.js ADDED
@@ -0,0 +1,69 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.libcurlSetCookies = exports.cookieOptFilter = exports.httpCookiesToArray = void 0;
4
+ const httpCookiesToArray = (cookies) => {
5
+ const stringBooleanToJsBoolean = (e) => {
6
+ switch (e) {
7
+ case 'TRUE':
8
+ return true;
9
+ case 'FALSE':
10
+ return false;
11
+ default:
12
+ throw new Error(`unkonw type ${e}`);
13
+ }
14
+ };
15
+ const cookies_ = [];
16
+ for (const it of cookies.split('\n')) {
17
+ if (!it) {
18
+ continue;
19
+ }
20
+ const [domain, secure, path, cors, timestamp, name, value] = it.split('\t');
21
+ cookies_.push([domain, stringBooleanToJsBoolean(secure), path, stringBooleanToJsBoolean(cors), parseInt(timestamp), name, value]);
22
+ }
23
+ return cookies_;
24
+ };
25
+ exports.httpCookiesToArray = httpCookiesToArray;
26
+ const cookieOptFilter = (cookieOpt) => {
27
+ return (e) => {
28
+ if (cookieOpt) {
29
+ if (cookieOpt.domain) {
30
+ if (cookieOpt.domain != e[0])
31
+ return false;
32
+ }
33
+ if (cookieOpt.path) {
34
+ if (cookieOpt.path != e[2])
35
+ return false;
36
+ }
37
+ }
38
+ return true;
39
+ };
40
+ };
41
+ exports.cookieOptFilter = cookieOptFilter;
42
+ const libcurlSetCookies = (curl, cookies, domain) => {
43
+ if (typeof cookies == 'string') {
44
+ cookies.replace(/\s+/g, '')
45
+ .split(';')
46
+ .reverse()
47
+ .map(e => e.split('=', 2))
48
+ .forEach(([key, value]) => {
49
+ curl.setCookie({
50
+ name: key,
51
+ value,
52
+ domain,
53
+ path: '/',
54
+ });
55
+ });
56
+ }
57
+ else {
58
+ Object.keys(cookies).forEach(key => {
59
+ curl.setCookie({
60
+ name: key,
61
+ value: cookies[key],
62
+ domain,
63
+ path: '/',
64
+ });
65
+ });
66
+ }
67
+ };
68
+ exports.libcurlSetCookies = libcurlSetCookies;
69
+ //# sourceMappingURL=utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":";;;AAEO,MAAM,kBAAkB,GAAkD,CAAC,OAAO,EAAE,EAAE;IACzF,MAAM,wBAAwB,GAAG,CAAC,CAAS,EAAE,EAAE;QAC3C,QAAQ,CAAC,EAAE;YACP,KAAK,MAAM;gBACP,OAAO,IAAI,CAAC;YAChB,KAAK,OAAO;gBACR,OAAO,KAAK,CAAC;YACjB;gBACI,MAAM,IAAI,KAAK,CAAC,eAAe,CAAC,EAAE,CAAC,CAAA;SAC1C;IACL,CAAC,CAAA;IAMD,MAAM,QAAQ,GAAG,EAAE,CAAC;IACpB,KAAK,MAAM,EAAE,IAAI,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE;QAClC,IAAI,CAAC,EAAE,EAAE;YACL,SAAS;SACZ;QACD,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE,KAAK,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QAC5E,QAAQ,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE,wBAAwB,CAAC,MAAM,CAAC,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CAAC,OAAO,CAAC,GAAG,CAAC,EAAE;YAC/B,IAAI,CAAC,SAAS,CAAC;gBACX,IAAI,EAAE,GAAG;gBACT,KAAK,EAAE,OAAO,CAAC,GAAG,CAAC;gBACnB,MAAM;gBACN,IAAI,EAAE,GAAG;aACZ,CAAC,CAAC;QACP,CAAC,CAAC,CAAA;KACL;AACL,CAAC,CAAA;AAxBY,QAAA,iBAAiB,qBAwB7B"}
package/package.json CHANGED
@@ -1,15 +1,23 @@
1
1
  {
2
2
  "name": "@ossiana/node-libcurl",
3
- "version": "1.0.7-alpha-3",
3
+ "version": "1.0.8-alpha-2",
4
4
  "dependencies": {
5
5
  "bindings": "^1.5.0",
6
- "node-addon-api": "^5.0.0",
7
- "node-gyp": "^9.3.1"
6
+ "node-addon-api": "^5.0.0"
8
7
  },
9
8
  "scripts": {
10
- "test": "node ./test/requests_tls.js",
11
- "build:dev": "node-gyp configure --node_use_openssl=false --openssl_quic=false --node_shared_openssl=false && node-gyp -j 16 build --release",
12
- "unittest": "mocha"
9
+ "test": "node ./test/libcurl_test.js",
10
+ "install": "node-pre-gyp install && npm run build:dev",
11
+ "build:dev": "node-pre-gyp configure --node_use_openssl=false --openssl_quic=false --node_shared_openssl=false && node-pre-gyp -j 16 build --release",
12
+ "unittest": "mocha",
13
+ "tsc":"tsc -p tsconfig.json"
14
+ },
15
+ "binary": {
16
+ "module_name": "node-libcurl",
17
+ "module_path": "./lib/{configuration}/{platform}-{arch}/",
18
+ "package_name": "{module_name}-{platform}-{arch}-release.tar.gz",
19
+ "host": "https://ossianaa.github.io/static/github/release/node-libcurl/",
20
+ "remote_path": "v1.0.0"
13
21
  },
14
22
  "devDependencies": {
15
23
  "@types/node": "^18.11.18",
@@ -17,11 +25,13 @@
17
25
  "chai": "^4.3.7",
18
26
  "express": "^4.18.2",
19
27
  "mocha": "^10.2.0",
28
+ "node-gyp": "^9.3.1",
20
29
  "typescript": "^4.9.4"
21
30
  },
22
31
  "author": {
23
32
  "name": "Ossian"
24
33
  },
34
+ "main": "./dist/index.js",
25
35
  "repository": {
26
36
  "type": "git",
27
37
  "url": "git@github.com/Ossianaa/node-libcurl.git"
package/readme.md CHANGED
@@ -1,22 +1,22 @@
1
1
  # node-libcurl
2
2
 
3
- ## different with Nodejs fetch api
3
+ ## Different with Nodejs fetch api
4
4
  * JA3 FingerPrint is the same as chrome 108,it modified the BoringSSL extension, set the custom cipher suite with Libcurl
5
5
  ------------
6
6
 
7
7
  ## Build Status
8
8
 
9
- | Platform | Support |
10
- |:-----------------------------:|:---------:|
11
- | __Windows (x64)__ |__Yes__|
12
- | __Windows (x86)__ |__No__|
13
- | __Windows (ARM)__ |__No__|
14
- | __Ubuntu (x86_64)__ |__Yes__|
15
- | __MacOS (x86_64)__ |__Yes__|
16
- | __MacOS (arm64)__ |__No__|
9
+ | Platform | Support |
10
+ | :-----------------: | :-----: |
11
+ | __Windows (x64)__ | __Yes__ |
12
+ | __Windows (x86)__ | __No__ |
13
+ | __Windows (ARM)__ | __No__ |
14
+ | __Ubuntu (x86_64)__ | __Yes__ |
15
+ | __MacOS (x86_64)__ | __Yes__ |
16
+ | __MacOS (arm64)__ | __No__ |
17
17
  ------------
18
18
 
19
- ## how to build
19
+ ## How to build
20
20
  > npm i -g @ossiana/node-libcurl
21
21
 
22
22
  #### Some Problems on Windows
@@ -30,15 +30,15 @@ You should install Python 3.x and the Visual Studio compiler;
30
30
 
31
31
  ### import
32
32
  ```javascript
33
- import { LibCurl, fetch } from '@ossiana/node-libcurl'
33
+ import { LibCurl, fetch, requests} from '@ossiana/node-libcurl'
34
34
  ```
35
35
 
36
36
  ### browser fetch style
37
37
  ```javascript
38
38
  fetch("https://xxx.io/api/graphql/", {
39
- proxy: "127.0.0.1:8888",
39
+ proxy: "127.0.0.1:8888",
40
40
  method: "POST",
41
- headers: {
41
+ headers: {
42
42
  "Host": "xxx.io",
43
43
  "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36",
44
44
  "Content-Type": "application/json",
@@ -59,6 +59,21 @@ fetch("https://xxx.io/api/graphql/", {
59
59
  console.error('fetch error : %s', err.message);
60
60
  });
61
61
  ```
62
+ ### requests style
63
+ ```javascript
64
+ const session = requests.session();
65
+
66
+ session.setCookie('ua', '123=/1a', '.baidu.com', '/');
67
+
68
+ const res = await session.get('https://www.baidu.com', {
69
+ headers: {
70
+ "user-Agent": "1"
71
+ }
72
+ });
73
+ console.log(res.headersMap);
74
+ console.log(session.getCookiesMap().get('ua'));
75
+ console.log(res.text);
76
+ ```
62
77
 
63
78
 
64
79
 
package/src/export.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { LibCurl } from "./libcurl";
1
+ import { LibCurl } from "./libcurl"
2
2
  import { fetch } from "./fetch"
3
-
4
- export { LibCurl, fetch };
3
+ import { requests } from './requests'
4
+ export { LibCurl, fetch, requests };
package/src/fetch.ts CHANGED
@@ -1,19 +1,8 @@
1
- import { LibCurl, LibCurl_HTTP_VERSION } from "./libcurl";
2
-
3
- type LibCurlHeadersInfo = [string, string][] | object | string;
4
- type LibCurlBodyInfo = string | Uint8Array | any;
5
- type LibCurlCookiesInfo = string | object;
6
- type LibCurlHttpVersionInfo = LibCurl_HTTP_VERSION;
7
-
8
- type LibCurlProxyWithAccountInfo = {
9
- proxy: string;
10
- username: string;
11
- password: string;
12
- };
13
- type LibCurlProxyInfo = string | LibCurlProxyWithAccountInfo;
1
+ import { LibCurl, LibCurlBodyInfo, LibCurlMethodInfo, LibCurlHeadersInfo, LibCurlCookiesAttr, LibCurlHttpVersionInfo, LibCurlProxyInfo, LibCurlCookiesInfo } from "./libcurl";
2
+ import { libcurlSetCookies } from "./utils";
14
3
 
15
4
  interface LibCurlRequestInfo {
16
- method?: string;
5
+ method?: LibCurlMethodInfo;
17
6
  headers?: LibCurlHeadersInfo;
18
7
  body?: LibCurlBodyInfo;
19
8
  redirect?: boolean;
@@ -32,85 +21,48 @@ interface LibCurlResponseInfo {
32
21
  arraybuffer: () => Promise<ArrayBuffer>;
33
22
  text: () => Promise<string>;
34
23
  json: () => Promise<object>;
35
- jsonp: (callbackName?: string) => Promise<object>;
36
24
  headers: () => Promise<string>;
25
+ cookies: () => Promise<string>;
26
+ cookiesMap: () => Promise<LibCurlCookiesAttr>;
37
27
  }
38
28
 
39
29
  export async function fetch(url: string | URL, request: LibCurlRequestInfo = {}): Promise<LibCurlResponseInfo> {
40
30
  request.instance ||= new LibCurl();
41
31
  const curl = request.instance;
42
- return new Promise((resolve, reject) => {
43
- const { method = "GET",
44
- headers = [], redirect = false, httpVersion = 0,
45
- openInnerLog = false, proxy, body, cookies } = request;
46
- curl.open(method, url + '', true);
47
- if (Array.isArray(headers)) {
48
- headers.forEach(([key, value]) => {
49
- curl.setRequestHeader(key, value);
50
- });
51
- } else if (typeof headers == 'object') {
52
- Object.keys(headers).forEach((key: string) => {
53
- curl.setRequestHeader(key, headers[key]);
54
- })
55
- } else {
56
- curl.setRequestHeaders(headers);
57
- }
58
- if (redirect) {
59
- curl.setRedirect(true);
60
- }
61
- if (httpVersion) {
62
- curl.setHttpVersion(httpVersion);
63
- }
64
- if (openInnerLog) {
65
- curl.printInnerLogger();
66
- }
32
+ const { method = "GET",
33
+ headers, redirect = false, httpVersion = 0,
34
+ openInnerLog = false, proxy, body, cookies } = request;
35
+ curl.open(method, url + '', true);
36
+ if (headers) {
37
+ curl.setRequestHeaders(headers);
38
+ }
39
+ if (redirect) {
40
+ curl.setRedirect(true);
41
+ }
42
+ if (httpVersion) {
43
+ curl.setHttpVersion(httpVersion);
44
+ }
45
+ if (openInnerLog) {
46
+ curl.printInnerLogger();
47
+ }
48
+ if (cookies) {
49
+ const { hostname } = new URL(url);
67
50
  if (cookies) {
68
- const { hostname } = new URL(url);
69
- if (typeof cookies == 'string') {
70
- cookies.replace(/\s+/g, '')
71
- .split(';')
72
- .reverse()//保证顺序不颠倒
73
- .map(e => e.split('=', 2))
74
- .forEach(([key, value]) => {
75
- curl.setCookie(key, value, hostname)
76
- });
77
- } else {
78
- Object.keys(cookies).forEach(key => {
79
- curl.setCookie(key, cookies[key], hostname);
80
- })
81
- }
82
- }
83
- if (proxy) {
84
- if (typeof proxy == "string") {
85
- curl.setProxy(proxy);
86
- } else {
87
- const {
88
- proxy: proxy_,
89
- username,
90
- password,
91
- } = proxy;
92
- curl.setProxy(proxy_, username, password);
93
- }
51
+ libcurlSetCookies(curl, cookies, hostname);
94
52
  }
95
- let promise: Promise<undefined>;
96
- if (body) {
97
- promise = curl.send(body);
98
- } else {
99
- promise = curl.send();
100
- }
101
- promise.then(() => {
102
-
103
- resolve(
104
- {
105
- status: () => curl.getResponseStatus(),
106
- arraybuffer: async () => curl.getResponseBody().buffer,
107
- text: async () => curl.getResponseString(),
108
- json: async () => curl.getResponseJson(),
109
- jsonp: async (callbackName?: string) => curl.getResponseJsonp(callbackName),
110
- headers: async () => curl.getResponseHeaders(),
111
- }
112
- )
113
- })
53
+ }
54
+ if (proxy) {
55
+ curl.setProxy(proxy);
56
+ }
57
+ await curl.send(body);
58
+ return {
59
+ status: () => curl.getResponseStatus(),
60
+ arraybuffer: async () => curl.getResponseBody().buffer,
61
+ text: async () => curl.getResponseString(),
62
+ json: async () => curl.getResponseJson(),
63
+ headers: async () => curl.getResponseHeaders(),
64
+ cookies: async () => curl.getCookies(),
65
+ cookiesMap: async () => curl.getCookiesMap(),
66
+ } as LibCurlResponseInfo;
114
67
 
115
- })
116
68
  }