@ossiana/node-libcurl 1.0.7-alpha-2 → 1.0.8-alpha-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.
Files changed (51) hide show
  1. package/LICENSE +21 -0
  2. package/binding.gyp +18 -57
  3. package/dist/export.d.ts +2 -1
  4. package/dist/export.js +3 -1
  5. package/dist/export.js.map +1 -1
  6. package/dist/fetch.d.ts +4 -13
  7. package/dist/fetch.js +33 -68
  8. package/dist/fetch.js.map +1 -1
  9. package/dist/libcurl.d.ts +64 -11
  10. package/dist/libcurl.js +87 -47
  11. package/dist/libcurl.js.map +1 -1
  12. package/dist/requests.d.ts +66 -0
  13. package/dist/requests.js +186 -0
  14. package/dist/requests.js.map +1 -0
  15. package/dist/utils.d.ts +4 -0
  16. package/dist/utils.js +69 -0
  17. package/dist/utils.js.map +1 -0
  18. package/package.json +38 -29
  19. package/readme.md +36 -9
  20. package/src/export.ts +3 -3
  21. package/src/fetch.ts +38 -86
  22. package/src/libcurl/bao_curl.cpp +58 -22
  23. package/src/libcurl/bao_curl_node_addon.cpp +20 -13
  24. package/src/libcurl/curlAsyncWorker.cpp +6 -2
  25. package/src/libcurl/include/bao_curl.h +41 -38
  26. package/src/libcurl/include/curlAysncWorker.h +2 -0
  27. package/src/libcurl.ts +149 -58
  28. package/src/requests.ts +211 -0
  29. package/src/utils.ts +71 -0
  30. package/tsconfig.json +1 -0
  31. package/dist/src/export.d.ts +0 -3
  32. package/dist/src/export.js +0 -8
  33. package/dist/src/export.js.map +0 -1
  34. package/dist/src/fetch.d.ts +0 -32
  35. package/dist/src/fetch.js +0 -90
  36. package/dist/src/fetch.js.map +0 -1
  37. package/dist/src/index.d.ts +0 -1
  38. package/dist/src/index.js +0 -18
  39. package/dist/src/index.js.map +0 -1
  40. package/dist/src/libcurl.d.ts +0 -31
  41. package/dist/src/libcurl.js +0 -154
  42. package/dist/src/libcurl.js.map +0 -1
  43. package/index.js +0 -1
  44. package/src/libcurl/lib/Release/darwin/libcrypto.a +0 -0
  45. package/src/libcurl/lib/Release/darwin/libcurl.a +0 -0
  46. package/src/libcurl/lib/Release/darwin/libssl.a +0 -0
  47. package/src/libcurl/lib/Release/linux/libcrypto.a +0 -0
  48. package/src/libcurl/lib/Release/linux/libcurl.a +0 -0
  49. package/src/libcurl/lib/Release/linux/libssl.a +0 -0
  50. package/src/libcurl/lib/Release/windows/libcurl.dll +0 -0
  51. package/src/libcurl/lib/Release/windows/libcurl_imp.lib +0 -0
package/src/libcurl.ts CHANGED
@@ -1,12 +1,71 @@
1
1
  import bindings from 'bindings'
2
+ import { httpCookiesToArray, cookieOptFilter } from './utils';
2
3
 
3
4
  const { BaoLibCurl } = bindings('bao_curl_node_addon');
4
5
 
5
- export enum LibCurl_HTTP_VERSION {
6
+ export enum LibCurlHttpVersionInfo {
6
7
  http1_1,
7
8
  http2,
8
9
  }
9
10
 
11
+ //Domain Secure Path CORS TimeStamp Name Value
12
+ export type LibCurlSetCookieOption = {
13
+ domain?: string;
14
+ // secure?: boolean;
15
+ path?: string;
16
+ // cors?: boolean;
17
+ name: string;
18
+ value: string;
19
+ }
20
+
21
+ export type LibCurlCookiesInfo = string | { [key: string]: string };
22
+
23
+
24
+ export type LibCurlGetCookiesOption = {
25
+ domain?: string;
26
+ path?: string;
27
+ }
28
+
29
+ export type LibCurlGetCookieOption = {
30
+ name: string;
31
+ domain: string;
32
+ path?: string;
33
+ }
34
+
35
+ export type LibCurlCookieAttrArray = [
36
+ domain: string, secure: boolean, path: string, cors: boolean, timestamp: number, name: string, value: string
37
+ ]
38
+
39
+ export type LibCurlCookieAttrObject = {
40
+ domain: string, secure: boolean, path: string, cors: boolean, timestamp: number, value: string
41
+ }
42
+
43
+ export type LibCurlCookiesAttr = Map<string, LibCurlCookieAttrObject>
44
+
45
+ export type LibCurlHeadersAttr = Map<string, string>
46
+
47
+ export type LibCurlHeadersInfo = string | { [key: string]: [value: string] } | LibCurlHeadersAttr
48
+
49
+ export type LibCurlBodyInfo = string | Uint8Array | URLSearchParams | any;
50
+
51
+ export type LibCurlMethodInfo = 'GET' | 'POST' | 'HEAD' | 'PUT' | 'DELETE' | 'CONNECT' | 'OPTIONS' | 'TRACE' | 'PATCH'
52
+
53
+ export type LibCurlProxyWithAccountInfo = {
54
+ proxy: string;
55
+ username: string;
56
+ password: string;
57
+ };
58
+
59
+ export type LibCurlProxyInfo = string | LibCurlProxyWithAccountInfo;
60
+
61
+ export type LibCurlURLInfo = string | URL;
62
+
63
+ export class LibCurlError extends Error {
64
+ constructor(e: string) {
65
+ super(e)
66
+ }
67
+ }
68
+
10
69
  export class LibCurl {
11
70
  private m_libCurl_impl_: any;
12
71
  private m_isAsync_: boolean;
@@ -19,9 +78,9 @@ export class LibCurl {
19
78
  throw new Error('the last request is sending, don\'t send one more request on one instance!')
20
79
  }
21
80
  }
22
- public open(method: string, url: string, async: boolean = true): void {
81
+ public open(method: LibCurlMethodInfo, url: LibCurlURLInfo, async: boolean = true): void {
23
82
  this.checkSending();
24
- this.m_libCurl_impl_.open(method, url);
83
+ this.m_libCurl_impl_.open(method, url+'');
25
84
  this.m_isAsync_ = async;
26
85
  }
27
86
 
@@ -32,11 +91,25 @@ export class LibCurl {
32
91
 
33
92
  /**
34
93
  *
35
- * @param headers 多个header 以\n换行链接的文本
94
+ * @param headers
36
95
  */
37
- public setRequestHeaders(headers: string): void {
96
+ public setRequestHeaders(headers: LibCurlHeadersInfo): void {
38
97
  this.checkSending();
39
- this.m_libCurl_impl_.setRequestHeaders(headers);
98
+ if (!headers) {
99
+ return;
100
+ }
101
+ if (headers instanceof Map) {
102
+ headers.forEach((value, key) => this.m_libCurl_impl_.setRequestHeader(key, value))
103
+ } else if (typeof headers == 'string') {
104
+ this.m_libCurl_impl_.setRequestHeaders(headers);
105
+ } else if (typeof headers == 'object') {
106
+ Object.keys(headers).forEach((key) => {
107
+ const value = headers[key];
108
+ this.m_libCurl_impl_.setRequestHeader(key, value);
109
+ })
110
+ } else {
111
+ throw new TypeError('unkown type')
112
+ }
40
113
  }
41
114
 
42
115
  /**
@@ -45,12 +118,12 @@ export class LibCurl {
45
118
  * @param username
46
119
  * @param password
47
120
  */
48
- public setProxy(proxy: string, username?: string, password?: string): void {
121
+ public setProxy(proxyOpt: LibCurlProxyInfo): void {
49
122
  this.checkSending();
50
- if (username && password) {
51
- this.m_libCurl_impl_.setProxy(proxy, username, password);
123
+ if (typeof proxyOpt == 'string') {
124
+ this.m_libCurl_impl_.setProxy(proxyOpt);
52
125
  } else {
53
- this.m_libCurl_impl_.setProxy(proxy);
126
+ this.m_libCurl_impl_.setProxy(proxyOpt.proxy, proxyOpt.username, proxyOpt.password);
54
127
  }
55
128
  }
56
129
 
@@ -74,41 +147,60 @@ export class LibCurl {
74
147
  * @param value
75
148
  * @param domain cookie作用域 sample: .baidu.com baike.baidu.com
76
149
  */
77
- public setCookie(key: string, value: string, domain: string): void {
150
+ public setCookie(cookieOpt: LibCurlSetCookieOption): void {
78
151
  this.checkSending();
79
- this.m_libCurl_impl_.setCookie(key, value, domain);
152
+ this.m_libCurl_impl_.setCookie(cookieOpt.name, cookieOpt.value, cookieOpt.domain, cookieOpt.path);
80
153
  }
81
154
 
82
155
  /**
83
156
  *
84
- * @param key
157
+ * @param cookieOpt
85
158
  * @param domain cookie作用域 sample: .baidu.com baike.baidu.com
86
159
  */
87
- public removeCookie(key: string, domain: string): void {
160
+ public deleteCookie(cookieOpt: LibCurlGetCookieOption): void {
88
161
  this.checkSending();
89
- this.m_libCurl_impl_.removeCookie(key, domain);
162
+ this.m_libCurl_impl_.deleteCookie(cookieOpt.name, cookieOpt.domain, cookieOpt.path || "/");
90
163
  }
91
164
 
92
165
  /**
93
- *
94
- * @returns 返回所有Cookies 以\n连接
95
- * sample: .127.0.0.1 TRUE / FALSE 3000000000 a b
96
- * .127.0.0.1 TRUE /api FALSE 3000000000 c d
166
+ * @param {LibCurlGetCookiesOption}cookieOpt
167
+ * @returns 返回所有Cookies sample:'a=b;c=d;'
168
+ */
169
+ public getCookies(cookieOpt?: LibCurlGetCookiesOption): string {
170
+ this.checkSending();
171
+ const cookies_ = this.m_libCurl_impl_.getCookies();
172
+ return httpCookiesToArray(cookies_).filter(cookieOptFilter(cookieOpt)).map(e => `${e[5]}=${encodeURIComponent(e[6])}`).join(';');
173
+ }
174
+
175
+ /**
176
+ * @param {LibCurlGetCookiesOption}cookieOpt
177
+ * @returns 返回所有Cookie的Map
97
178
  */
98
- public getCookies(): string {
179
+ public getCookiesMap(cookieOpt?: LibCurlGetCookiesOption): LibCurlCookiesAttr {
99
180
  this.checkSending();
100
- return this.m_libCurl_impl_.getCookies();
181
+ const cookies_ = this.m_libCurl_impl_.getCookies();
182
+ return httpCookiesToArray(cookies_).filter(cookieOptFilter(cookieOpt)).reduce((e: LibCurlCookiesAttr, t: LibCurlCookieAttrArray) => {
183
+ e.set(t[5], {
184
+ domain: t[0],
185
+ secure: t[1],
186
+ path: t[2],
187
+ cors: t[3],
188
+ timestamp: t[4],
189
+ value: t[6],
190
+ } as LibCurlCookieAttrObject)
191
+ return e;
192
+ }, new Map<string, LibCurlCookieAttrObject>());
101
193
  }
102
194
 
103
195
  /**
104
196
  *
105
- * @param key
106
- * @returns 返回该key对应的cookie
197
+ * @param cookieOpt
198
+ * @returns 返回该cookieOpt对应的cookieValue
107
199
  * sample:
108
200
  */
109
- public getCookie(key: string): string {
201
+ public getCookie(cookieOpt: LibCurlGetCookieOption): string {
110
202
  this.checkSending();
111
- return this.m_libCurl_impl_.getCookie(key);
203
+ return this.m_libCurl_impl_.getCookie(cookieOpt.name, cookieOpt.domain || "", cookieOpt.path || "");
112
204
  }
113
205
 
114
206
  /**
@@ -120,6 +212,24 @@ export class LibCurl {
120
212
  return this.m_libCurl_impl_.getResponseHeaders();
121
213
  }
122
214
 
215
+ /**
216
+ * @returns 返回响应头 Map
217
+ */
218
+ public getResponseHeadersMap(): LibCurlHeadersAttr {
219
+ this.checkSending();
220
+ const headers_ = this.m_libCurl_impl_.getResponseHeaders();
221
+ return headers_.split('\r\n')
222
+ .slice(1)//HTTP/1.1 200 OK
223
+ .reduce((e: LibCurlHeadersAttr, t: string) => {
224
+ if (!t) {
225
+ return e;
226
+ }
227
+ const [key, value] = t.split(': ');
228
+ e.set(key, value);
229
+ return e;
230
+ }, new Map<string, string>());
231
+ }
232
+
123
233
  /**
124
234
  *
125
235
  * @returns 返回状态码
@@ -160,7 +270,7 @@ export class LibCurl {
160
270
  * @param version
161
271
  * 设置http版本号
162
272
  */
163
- public setHttpVersion(version: LibCurl_HTTP_VERSION): void {
273
+ public setHttpVersion(version: LibCurlHttpVersionInfo): void {
164
274
  this.checkSending();
165
275
  this.m_libCurl_impl_.setHttpVersion(version);
166
276
  }
@@ -170,14 +280,18 @@ export class LibCurl {
170
280
  * @param body POST PUT PATCH时 发送的body
171
281
  * 当body不为string或uint8array时 此函数将用JSON.stringify转换对象
172
282
  */
173
- public send(body?: string | Uint8Array | any): Promise<undefined> | undefined {
283
+ public send(body?: LibCurlBodyInfo): Promise<undefined> | undefined {
174
284
  this.checkSending();
175
285
  this.m_isSending_ = true;
176
286
  if (this.m_isAsync_) {
177
287
  return new Promise((resolve, reject) => {
178
- const callback = () => {
288
+ const callback = (curlcode: number, curlcodeError: string) => {
179
289
  this.m_isSending_ = false;
180
- resolve(void 0);
290
+ if (curlcode != 0) {
291
+ reject(new LibCurlError(curlcodeError));
292
+ } else {
293
+ resolve(void 0);
294
+ }
181
295
  };
182
296
  if (body) {
183
297
  this.m_libCurl_impl_.sendAsync(body, callback);
@@ -187,7 +301,11 @@ export class LibCurl {
187
301
  })
188
302
  }
189
303
  if (body) {
190
- this.m_libCurl_impl_.send(body);
304
+ if (body instanceof URLSearchParams) {
305
+ this.m_libCurl_impl_.send(body + '');
306
+ } else {
307
+ this.m_libCurl_impl_.send(body);
308
+ }
191
309
  } else {
192
310
  this.m_libCurl_impl_.send();
193
311
  }
@@ -208,32 +326,5 @@ export class LibCurl {
208
326
  return JSON.parse(this.getResponseString());
209
327
  }
210
328
 
211
- /**
212
- *
213
- * @param callbackName
214
- * @returns JSON
215
- * unsafe
216
- * sample __WX__({a:1})
217
- */
218
- public getResponseJsonp(callbackName?: string): Object {
219
- this.checkSending();
220
- const str: string = this.getResponseString();
221
- let jsonstr: string = str;
222
- if (callbackName) {
223
- [, jsonstr] = new RegExp(`\s*${callbackName}[\s\S]*(.*)[\s\S]*`, 'g').exec(str)
224
- } else {
225
- try {
226
- eval(str)
227
- throw new Error('it seem not a jsonp');
228
- } catch (error) {
229
- try {
230
- [, callbackName] = /(.*) is not defined/g.exec(error.message);
231
- return this.getResponseJsonp(callbackName);
232
- } catch {
233
- throw new Error('it seem not a jsonp')
234
- }
235
- }
236
- }
237
- return eval(jsonstr);
238
- }
329
+
239
330
  }
@@ -0,0 +1,211 @@
1
+ import { LibCurl, LibCurlBodyInfo, LibCurlCookiesAttr, LibCurlCookiesInfo, LibCurlHeadersAttr, LibCurlHeadersInfo, LibCurlMethodInfo, LibCurlProxyInfo, LibCurlHttpVersionInfo, LibCurlURLInfo } from "./libcurl"
2
+ import { libcurlSetCookies } from "./utils";
3
+
4
+ type requestsHttpVersionInfo = LibCurlHttpVersionInfo;
5
+ type requestsHeadersInfo = LibCurlHeadersInfo;
6
+ type requestsBodyInfo = LibCurlBodyInfo;
7
+ type requestsCookiesInfo = LibCurlCookiesInfo;
8
+ type requestsMethodInfo = LibCurlMethodInfo;
9
+
10
+ type requestsProxyInfo = LibCurlProxyInfo;
11
+ type requestsURLInfo = LibCurlURLInfo
12
+
13
+ interface requestsResponseImp {
14
+ readonly text: string;
15
+ readonly json: object;
16
+ readonly buffer: Uint8Array;
17
+ readonly headers: string;
18
+ readonly headersMap: LibCurlHeadersAttr;
19
+ readonly status: number;
20
+ }
21
+
22
+ class requestsResponse implements requestsResponseImp {
23
+ private curl: LibCurl;
24
+ constructor(curl: LibCurl) {
25
+ this.curl = curl;
26
+ }
27
+
28
+ public get text(): string {
29
+ return this.curl.getResponseString();
30
+ }
31
+
32
+ public get json(): object {
33
+ return this.curl.getResponseJson();
34
+ }
35
+
36
+ public get buffer(): Uint8Array {
37
+ return this.curl.getResponseBody();
38
+ }
39
+
40
+ public get headers(): string {
41
+ return this.curl.getResponseHeaders();
42
+ }
43
+
44
+ public get headersMap(): LibCurlHeadersAttr {
45
+ return this.curl.getResponseHeadersMap();
46
+ }
47
+
48
+ public get status(): number {
49
+ return this.curl.getResponseStatus();
50
+ }
51
+
52
+
53
+ }
54
+
55
+ interface requestsInitOption {
56
+ redirect?: boolean;
57
+ cookies?: requestsCookiesInfo;
58
+ proxy?: requestsProxyInfo;
59
+ body?: requestsBodyInfo;
60
+ httpVersion?: requestsHttpVersionInfo;
61
+
62
+ /**
63
+ * 单位(秒)
64
+ */
65
+ timeout?: number;
66
+ /**
67
+ * 传入LibCurl实例可以做持久化连接
68
+ */
69
+ instance?: LibCurl;
70
+ }
71
+
72
+ type requestsParamsInfo = URLSearchParams | string | { [key: string]: string };
73
+
74
+ interface requestsOption {
75
+ headers?: requestsHeadersInfo;
76
+ body?: requestsBodyInfo;
77
+ params?: requestsParamsInfo;
78
+ }
79
+
80
+ const assignURLSearchParam = (target: URLSearchParams, source: URLSearchParams) => {
81
+ source.forEach((value, key) => {
82
+ target.append(key, value);
83
+ })
84
+ }
85
+
86
+ export class requests {
87
+ private option: requestsInitOption;
88
+ constructor(option: requestsInitOption = {}) {
89
+ this.option = { ...option };
90
+ const { cookies, timeout } = option;
91
+ const curl = this.option.instance ||= new LibCurl();
92
+
93
+ if (cookies) {
94
+ libcurlSetCookies(curl, cookies, '.');
95
+ }
96
+ if (timeout) {
97
+ curl.setTimeout(timeout, timeout);
98
+ }
99
+ }
100
+
101
+ static session(option: requestsInitOption = {}): requests {
102
+ return new requests(option);
103
+ }
104
+
105
+
106
+ //暂定6种常用方法
107
+ static async get(url: requestsURLInfo, requestOpt?: requestsOption): Promise<requestsResponse> {
108
+ return requests.session().sendRequest('GET', url, requestOpt);
109
+ }
110
+ static async post(url: requestsURLInfo, requestOpt?: requestsOption): Promise<requestsResponse> {
111
+ return requests.session().sendRequest('POST', url, requestOpt);
112
+ }
113
+ static async put(url: requestsURLInfo, requestOpt?: requestsOption): Promise<requestsResponse> {
114
+ return requests.session().sendRequest('PUT', url, requestOpt);
115
+ }
116
+ static async patch(url: requestsURLInfo, requestOpt?: requestsOption): Promise<requestsResponse> {
117
+ return requests.session().sendRequest('PATCH', url, requestOpt);
118
+ }
119
+ static async trace(url: requestsURLInfo, requestOpt?: requestsOption): Promise<requestsResponse> {
120
+ return requests.session().sendRequest('TRACE', url, requestOpt);
121
+ }
122
+ static async head(url: requestsURLInfo, requestOpt?: requestsOption): Promise<requestsResponse> {
123
+ return requests.session().sendRequest('HEAD', url, requestOpt);
124
+ }
125
+ async get(url: requestsURLInfo, requestOpt?: requestsOption): Promise<requestsResponse> {
126
+ return this.sendRequest('GET', url, requestOpt);
127
+ }
128
+ async post(url: requestsURLInfo, requestOpt?: requestsOption): Promise<requestsResponse> {
129
+ return this.sendRequest('POST', url, requestOpt);
130
+ }
131
+ async put(url: requestsURLInfo, requestOpt?: requestsOption): Promise<requestsResponse> {
132
+ return this.sendRequest('PUT', url, requestOpt);
133
+ }
134
+ async patch(url: requestsURLInfo, requestOpt?: requestsOption): Promise<requestsResponse> {
135
+ return this.sendRequest('PATCH', url, requestOpt);
136
+ }
137
+ async trace(url: requestsURLInfo, requestOpt?: requestsOption): Promise<requestsResponse> {
138
+ return this.sendRequest('TRACE', url, requestOpt);
139
+ }
140
+ async head(url: requestsURLInfo, requestOpt?: requestsOption): Promise<requestsResponse> {
141
+ return this.sendRequest('HEAD', url, requestOpt);
142
+ }
143
+
144
+ setCookie(key: string, value: string, domain: string = '', path: string = '') {
145
+ this.option.instance.setCookie({
146
+ name: key,
147
+ value,
148
+ domain,
149
+ path,
150
+ });
151
+ }
152
+
153
+ getCookie(key: string, domain?: string, path?: string): string {
154
+ return this.option.instance.getCookie({
155
+ name: key,
156
+ domain: domain || "",
157
+ path: path || "",
158
+ })
159
+ }
160
+
161
+ getCookies(domain?: string, path?: string): string {
162
+ if (arguments.length == 0) {
163
+ return this.option.instance.getCookies();
164
+ }
165
+ return this.option.instance.getCookies({
166
+ domain: domain || "",
167
+ path: path || "",
168
+ })
169
+ }
170
+ getCookiesMap(domain?: string, path?: string): LibCurlCookiesAttr {
171
+ if (arguments.length == 0) {
172
+ return this.option.instance.getCookiesMap();
173
+ }
174
+ return this.option.instance.getCookiesMap({
175
+ domain: domain || "",
176
+ path: path || "",
177
+ })
178
+ }
179
+
180
+ deleteCookie(key: string, domain: string, path?: string) {
181
+ this.option.instance.deleteCookie({
182
+ name: key,
183
+ domain: domain,
184
+ path: path || "/",
185
+ })
186
+ }
187
+
188
+ private async sendRequest(method: requestsMethodInfo, url: requestsURLInfo, requestOpt?: requestsOption): Promise<requestsResponse> {
189
+ const { instance: curl, redirect = false, proxy, httpVersion } = this.option;
190
+ const { headers, body, params } = requestOpt || {};
191
+ const url_ = new URL(url);
192
+ if (params) {
193
+ assignURLSearchParam(url_.searchParams, new URLSearchParams(params));
194
+ }
195
+ curl.open(method, url + '', true);
196
+ if (headers) {
197
+ curl.setRequestHeaders(headers);
198
+ }
199
+ if (redirect) {
200
+ curl.setRedirect(true);
201
+ }
202
+ if (httpVersion) {
203
+ curl.setHttpVersion(httpVersion);
204
+ }
205
+ if (proxy) {
206
+ curl.setProxy(proxy);
207
+ }
208
+ await curl.send(body);
209
+ return new requestsResponse(curl);
210
+ }
211
+ }
package/src/utils.ts ADDED
@@ -0,0 +1,71 @@
1
+ import { LibCurl, LibCurlCookieAttrArray, LibCurlCookiesInfo, LibCurlGetCookiesOption } from "./libcurl";
2
+
3
+ export const httpCookiesToArray: (cookies: string) => LibCurlCookieAttrArray[] = (cookies) => {
4
+ const stringBooleanToJsBoolean = (e: string) => {
5
+ switch (e) {
6
+ case 'TRUE':
7
+ return true;
8
+ case 'FALSE':
9
+ return false;
10
+ default:
11
+ throw new Error(`unkonw type ${e}`)
12
+ }
13
+ }
14
+ /*
15
+ * Domain Secure Path CORS TimeStamp Name Value
16
+ * sample: .127.0.0.1 TRUE / FALSE 3000000000 a b
17
+ * .127.0.0.1 TRUE /api FALSE 3000000000 c d
18
+ */
19
+ const cookies_ = [];
20
+ for (const it of cookies.split('\n')) {
21
+ if (!it) {
22
+ continue;
23
+ }
24
+ const [domain, secure, path, cors, timestamp, name, value] = it.split('\t');
25
+ cookies_.push([domain, stringBooleanToJsBoolean(secure), path, stringBooleanToJsBoolean(cors), parseInt(timestamp), name, value])
26
+ }
27
+ return cookies_;
28
+ }
29
+
30
+ export const cookieOptFilter = (cookieOpt: LibCurlGetCookiesOption) => {
31
+ return (e: LibCurlCookieAttrArray) => {
32
+ if (cookieOpt) {
33
+ if (cookieOpt.domain) {
34
+ if (cookieOpt.domain != e[0])
35
+ return false;
36
+ }
37
+ if (cookieOpt.path) {
38
+ if (cookieOpt.path != e[2])
39
+ return false;
40
+ }
41
+ }
42
+ return true;
43
+ }
44
+ }
45
+
46
+
47
+ export const libcurlSetCookies = (curl: LibCurl, cookies: LibCurlCookiesInfo, domain: string) => {
48
+ if (typeof cookies == 'string') {
49
+ cookies.replace(/\s+/g, '')
50
+ .split(';')
51
+ .reverse()//保证顺序不颠倒
52
+ .map(e => e.split('=', 2))
53
+ .forEach(([key, value]) => {
54
+ curl.setCookie({
55
+ name: key,
56
+ value,
57
+ domain,
58
+ path: '/',
59
+ })
60
+ });
61
+ } else {
62
+ Object.keys(cookies).forEach(key => {
63
+ curl.setCookie({
64
+ name: key,
65
+ value: cookies[key],
66
+ domain,
67
+ path: '/',
68
+ });
69
+ })
70
+ }
71
+ }
package/tsconfig.json CHANGED
@@ -17,6 +17,7 @@
17
17
  "esModuleInterop": true
18
18
  },
19
19
  "lib": [
20
+ "es2019",
20
21
  "es2017",
21
22
  "es7",
22
23
  "es6",
@@ -1,3 +0,0 @@
1
- import { LibCurl } from "./libcurl";
2
- import { fetch } from "./fetch";
3
- export { LibCurl, fetch };
@@ -1,8 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.fetch = exports.LibCurl = void 0;
4
- const libcurl_1 = require("./libcurl");
5
- Object.defineProperty(exports, "LibCurl", { enumerable: true, get: function () { return libcurl_1.LibCurl; } });
6
- const fetch_1 = require("./fetch");
7
- Object.defineProperty(exports, "fetch", { enumerable: true, get: function () { return fetch_1.fetch; } });
8
- //# sourceMappingURL=export.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"export.js","sourceRoot":"","sources":["../../src/export.ts"],"names":[],"mappings":";;;AAAA,uCAAoC;AAG3B,wFAHA,iBAAO,OAGA;AAFhB,mCAA+B;AAEb,sFAFT,aAAK,OAES"}
@@ -1,32 +0,0 @@
1
- import { LibCurl, LibCurl_HTTP_VERSION } from "./libcurl";
2
- type LibCurlHeadersInfo = [string, string][] | object | string;
3
- type LibCurlBodyInfo = string | Uint8Array | any;
4
- type LibCurlCookiesInfo = string | object;
5
- type LibCurlHttpVersionInfo = LibCurl_HTTP_VERSION;
6
- type LibCurlProxyWithAccountInfo = {
7
- proxy: string;
8
- username: string;
9
- password: string;
10
- };
11
- type LibCurlProxyInfo = string | LibCurlProxyWithAccountInfo;
12
- interface LibCurlRequestInfo {
13
- method?: string;
14
- headers?: LibCurlHeadersInfo;
15
- body?: LibCurlBodyInfo;
16
- redirect?: boolean;
17
- cookies?: LibCurlCookiesInfo;
18
- httpVersion?: LibCurlHttpVersionInfo;
19
- openInnerLog?: boolean;
20
- proxy?: LibCurlProxyInfo;
21
- instance?: LibCurl;
22
- }
23
- interface LibCurlResponseInfo {
24
- status: () => number;
25
- arraybuffer: () => Promise<ArrayBuffer>;
26
- text: () => Promise<string>;
27
- json: () => Promise<object>;
28
- jsonp: (callbackName?: string) => Promise<object>;
29
- headers: () => Promise<string>;
30
- }
31
- export declare function fetch(url: string | URL, request?: LibCurlRequestInfo): Promise<LibCurlResponseInfo>;
32
- export {};