@ossiana/node-libcurl 1.0.6-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.
- package/binding.gyp +89 -0
- package/dist/src/export.d.ts +3 -0
- package/dist/src/export.js +8 -0
- package/dist/src/export.js.map +1 -0
- package/dist/src/fetch.d.ts +32 -0
- package/dist/src/fetch.js +90 -0
- package/dist/src/fetch.js.map +1 -0
- package/dist/src/index.d.ts +2 -0
- package/dist/src/index.js +5 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/libcurl.d.ts +31 -0
- package/dist/src/libcurl.js +149 -0
- package/dist/src/libcurl.js.map +1 -0
- package/index.js +1 -0
- package/package.json +29 -0
- package/readme.md +68 -0
- package/src/export.ts +4 -0
- package/src/fetch.ts +116 -0
- package/src/libcurl/bao_curl.cpp +263 -0
- package/src/libcurl/bao_curl_node_addon.cpp +427 -0
- package/src/libcurl/curlAsyncWorker.cpp +31 -0
- package/src/libcurl/include/bao_curl.h +82 -0
- package/src/libcurl/include/curl/Makefile.am +39 -0
- package/src/libcurl/include/curl/curl.h +3103 -0
- package/src/libcurl/include/curl/curlver.h +77 -0
- package/src/libcurl/include/curl/easy.h +123 -0
- package/src/libcurl/include/curl/mprintf.h +50 -0
- package/src/libcurl/include/curl/multi.h +457 -0
- package/src/libcurl/include/curl/options.h +68 -0
- package/src/libcurl/include/curl/stdcheaders.h +33 -0
- package/src/libcurl/include/curl/system.h +504 -0
- package/src/libcurl/include/curl/typecheck-gcc.h +707 -0
- package/src/libcurl/include/curl/urlapi.h +145 -0
- package/src/libcurl/include/curlAysncWorker.h +14 -0
- package/src/libcurl/include/request_tls_utils.h +30 -0
- package/src/libcurl/include/utils.h +13 -0
- package/src/libcurl/lib/Release/libcurl.dll +0 -0
- package/src/libcurl/lib/Release/libcurl_imp.lib +0 -0
- package/src/libcurl/utils.cpp +41 -0
- package/src/libcurl.ts +235 -0
- package/tsconfig.json +31 -0
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
#ifndef CURLINC_URLAPI_H
|
|
2
|
+
#define CURLINC_URLAPI_H
|
|
3
|
+
/***************************************************************************
|
|
4
|
+
* _ _ ____ _
|
|
5
|
+
* Project ___| | | | _ \| |
|
|
6
|
+
* / __| | | | |_) | |
|
|
7
|
+
* | (__| |_| | _ <| |___
|
|
8
|
+
* \___|\___/|_| \_\_____|
|
|
9
|
+
*
|
|
10
|
+
* Copyright (C) 2018 - 2021, Daniel Stenberg, <daniel@haxx.se>, et al.
|
|
11
|
+
*
|
|
12
|
+
* This software is licensed as described in the file COPYING, which
|
|
13
|
+
* you should have received as part of this distribution. The terms
|
|
14
|
+
* are also available at https://curl.se/docs/copyright.html.
|
|
15
|
+
*
|
|
16
|
+
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
17
|
+
* copies of the Software, and permit persons to whom the Software is
|
|
18
|
+
* furnished to do so, under the terms of the COPYING file.
|
|
19
|
+
*
|
|
20
|
+
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
21
|
+
* KIND, either express or implied.
|
|
22
|
+
*
|
|
23
|
+
***************************************************************************/
|
|
24
|
+
|
|
25
|
+
#include "curl.h"
|
|
26
|
+
|
|
27
|
+
#ifdef __cplusplus
|
|
28
|
+
extern "C" {
|
|
29
|
+
#endif
|
|
30
|
+
|
|
31
|
+
/* the error codes for the URL API */
|
|
32
|
+
typedef enum {
|
|
33
|
+
CURLUE_OK,
|
|
34
|
+
CURLUE_BAD_HANDLE, /* 1 */
|
|
35
|
+
CURLUE_BAD_PARTPOINTER, /* 2 */
|
|
36
|
+
CURLUE_MALFORMED_INPUT, /* 3 */
|
|
37
|
+
CURLUE_BAD_PORT_NUMBER, /* 4 */
|
|
38
|
+
CURLUE_UNSUPPORTED_SCHEME, /* 5 */
|
|
39
|
+
CURLUE_URLDECODE, /* 6 */
|
|
40
|
+
CURLUE_OUT_OF_MEMORY, /* 7 */
|
|
41
|
+
CURLUE_USER_NOT_ALLOWED, /* 8 */
|
|
42
|
+
CURLUE_UNKNOWN_PART, /* 9 */
|
|
43
|
+
CURLUE_NO_SCHEME, /* 10 */
|
|
44
|
+
CURLUE_NO_USER, /* 11 */
|
|
45
|
+
CURLUE_NO_PASSWORD, /* 12 */
|
|
46
|
+
CURLUE_NO_OPTIONS, /* 13 */
|
|
47
|
+
CURLUE_NO_HOST, /* 14 */
|
|
48
|
+
CURLUE_NO_PORT, /* 15 */
|
|
49
|
+
CURLUE_NO_QUERY, /* 16 */
|
|
50
|
+
CURLUE_NO_FRAGMENT, /* 17 */
|
|
51
|
+
CURLUE_NO_ZONEID, /* 18 */
|
|
52
|
+
CURLUE_BAD_FILE_URL, /* 19 */
|
|
53
|
+
CURLUE_BAD_FRAGMENT, /* 20 */
|
|
54
|
+
CURLUE_BAD_HOSTNAME, /* 21 */
|
|
55
|
+
CURLUE_BAD_IPV6, /* 22 */
|
|
56
|
+
CURLUE_BAD_LOGIN, /* 23 */
|
|
57
|
+
CURLUE_BAD_PASSWORD, /* 24 */
|
|
58
|
+
CURLUE_BAD_PATH, /* 25 */
|
|
59
|
+
CURLUE_BAD_QUERY, /* 26 */
|
|
60
|
+
CURLUE_BAD_SCHEME, /* 27 */
|
|
61
|
+
CURLUE_BAD_SLASHES, /* 28 */
|
|
62
|
+
CURLUE_BAD_USER, /* 29 */
|
|
63
|
+
CURLUE_LAST
|
|
64
|
+
} CURLUcode;
|
|
65
|
+
|
|
66
|
+
typedef enum {
|
|
67
|
+
CURLUPART_URL,
|
|
68
|
+
CURLUPART_SCHEME,
|
|
69
|
+
CURLUPART_USER,
|
|
70
|
+
CURLUPART_PASSWORD,
|
|
71
|
+
CURLUPART_OPTIONS,
|
|
72
|
+
CURLUPART_HOST,
|
|
73
|
+
CURLUPART_PORT,
|
|
74
|
+
CURLUPART_PATH,
|
|
75
|
+
CURLUPART_QUERY,
|
|
76
|
+
CURLUPART_FRAGMENT,
|
|
77
|
+
CURLUPART_ZONEID /* added in 7.65.0 */
|
|
78
|
+
} CURLUPart;
|
|
79
|
+
|
|
80
|
+
#define CURLU_DEFAULT_PORT (1<<0) /* return default port number */
|
|
81
|
+
#define CURLU_NO_DEFAULT_PORT (1<<1) /* act as if no port number was set,
|
|
82
|
+
if the port number matches the
|
|
83
|
+
default for the scheme */
|
|
84
|
+
#define CURLU_DEFAULT_SCHEME (1<<2) /* return default scheme if
|
|
85
|
+
missing */
|
|
86
|
+
#define CURLU_NON_SUPPORT_SCHEME (1<<3) /* allow non-supported scheme */
|
|
87
|
+
#define CURLU_PATH_AS_IS (1<<4) /* leave dot sequences */
|
|
88
|
+
#define CURLU_DISALLOW_USER (1<<5) /* no user+password allowed */
|
|
89
|
+
#define CURLU_URLDECODE (1<<6) /* URL decode on get */
|
|
90
|
+
#define CURLU_URLENCODE (1<<7) /* URL encode on set */
|
|
91
|
+
#define CURLU_APPENDQUERY (1<<8) /* append a form style part */
|
|
92
|
+
#define CURLU_GUESS_SCHEME (1<<9) /* legacy curl-style guessing */
|
|
93
|
+
#define CURLU_NO_AUTHORITY (1<<10) /* Allow empty authority when the
|
|
94
|
+
scheme is unknown. */
|
|
95
|
+
#define CURLU_ALLOW_SPACE (1<<11) /* Allow spaces in the URL */
|
|
96
|
+
|
|
97
|
+
typedef struct Curl_URL CURLU;
|
|
98
|
+
|
|
99
|
+
/*
|
|
100
|
+
* curl_url() creates a new CURLU handle and returns a pointer to it.
|
|
101
|
+
* Must be freed with curl_url_cleanup().
|
|
102
|
+
*/
|
|
103
|
+
CURL_EXTERN CURLU *curl_url(void);
|
|
104
|
+
|
|
105
|
+
/*
|
|
106
|
+
* curl_url_cleanup() frees the CURLU handle and related resources used for
|
|
107
|
+
* the URL parsing. It will not free strings previously returned with the URL
|
|
108
|
+
* API.
|
|
109
|
+
*/
|
|
110
|
+
CURL_EXTERN void curl_url_cleanup(CURLU *handle);
|
|
111
|
+
|
|
112
|
+
/*
|
|
113
|
+
* curl_url_dup() duplicates a CURLU handle and returns a new copy. The new
|
|
114
|
+
* handle must also be freed with curl_url_cleanup().
|
|
115
|
+
*/
|
|
116
|
+
CURL_EXTERN CURLU *curl_url_dup(CURLU *in);
|
|
117
|
+
|
|
118
|
+
/*
|
|
119
|
+
* curl_url_get() extracts a specific part of the URL from a CURLU
|
|
120
|
+
* handle. Returns error code. The returned pointer MUST be freed with
|
|
121
|
+
* curl_free() afterwards.
|
|
122
|
+
*/
|
|
123
|
+
CURL_EXTERN CURLUcode curl_url_get(CURLU *handle, CURLUPart what,
|
|
124
|
+
char **part, unsigned int flags);
|
|
125
|
+
|
|
126
|
+
/*
|
|
127
|
+
* curl_url_set() sets a specific part of the URL in a CURLU handle. Returns
|
|
128
|
+
* error code. The passed in string will be copied. Passing a NULL instead of
|
|
129
|
+
* a part string, clears that part.
|
|
130
|
+
*/
|
|
131
|
+
CURL_EXTERN CURLUcode curl_url_set(CURLU *handle, CURLUPart what,
|
|
132
|
+
const char *part, unsigned int flags);
|
|
133
|
+
|
|
134
|
+
/*
|
|
135
|
+
* curl_url_strerror() turns a CURLUcode value into the equivalent human
|
|
136
|
+
* readable error string. This is useful for printing meaningful error
|
|
137
|
+
* messages.
|
|
138
|
+
*/
|
|
139
|
+
CURL_EXTERN const char *curl_url_strerror(CURLUcode);
|
|
140
|
+
|
|
141
|
+
#ifdef __cplusplus
|
|
142
|
+
} /* end of extern "C" */
|
|
143
|
+
#endif
|
|
144
|
+
|
|
145
|
+
#endif /* CURLINC_URLAPI_H */
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#include <napi.h>
|
|
2
|
+
#include "bao_curl.h"
|
|
3
|
+
|
|
4
|
+
class curlAsyncWorker : public Napi::AsyncWorker
|
|
5
|
+
{
|
|
6
|
+
public:
|
|
7
|
+
curlAsyncWorker(Napi::Function &callback, BaoCurl& baoCurlInstance, Napi::Uint8Array m_sendBody);
|
|
8
|
+
~curlAsyncWorker();
|
|
9
|
+
void Execute();
|
|
10
|
+
void OnOK();
|
|
11
|
+
private:
|
|
12
|
+
BaoCurl& m_baoCurlInstance;
|
|
13
|
+
Napi::Uint8Array m_sendBody;
|
|
14
|
+
};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
#define REQUEST_TLS_METHOD_THROW(env,className,methodName,reason) {\
|
|
2
|
+
Napi::TypeError::New(##env,StringFormat("Failed to execute '%s' on '%s': %s",##methodName,##className,##reason)).ThrowAsJavaScriptException();\
|
|
3
|
+
return (##env).Undefined();\
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
#define REQUEST_TLS_METHOD_ARGS_CHECK(env,className,methodName,needLen,getLen) {\
|
|
7
|
+
if ( ##getLen != ##needLen){\
|
|
8
|
+
Napi::TypeError::New(##env,StringFormat("Failed to execute '%s' on '%s': %d arguments required, but only %d present.",##methodName,##className,##needLen,##getLen)).ThrowAsJavaScriptException();\
|
|
9
|
+
return (##env).Undefined();\
|
|
10
|
+
}\
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
#define REQUEST_TLS_METHOD_ARGS_TOO_MUCH_CHECK(env,className,methodName,maxLen,getLen) {\
|
|
14
|
+
if ( ##getLen > ##maxLen){\
|
|
15
|
+
Napi::TypeError::New(##env,StringFormat("Failed to execute '%s' on '%s': maximum of %d arguments required, but only %d present.",##methodName,##className,##maxLen,##getLen)).ThrowAsJavaScriptException();\
|
|
16
|
+
return (##env).Undefined();\
|
|
17
|
+
}\
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
#define REQUEST_TLS_METHOD_ARGS_NO_CONFIG(env,className,methodName,reqNums,getLen) {\
|
|
21
|
+
Napi::TypeError::New(##env,StringFormat("Failed to execute '%s' on '%s': [%s] arguments required, but only %d present.",##methodName,##className,##reqNums,##getLen)).ThrowAsJavaScriptException();\
|
|
22
|
+
return (##env).Undefined();\
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
#define REQUEST_TLS_METHOD_CHECK(env,condition,message) {\
|
|
26
|
+
if (!(condition)){\
|
|
27
|
+
Napi::TypeError::New(##env,##message).ThrowAsJavaScriptException();\
|
|
28
|
+
return (##env).Undefined();\
|
|
29
|
+
}\
|
|
30
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
#include <string>
|
|
2
|
+
#include <vector>
|
|
3
|
+
#include <cstdarg>
|
|
4
|
+
#include <assert.h>
|
|
5
|
+
#include <stdio.h>
|
|
6
|
+
#include <sstream>
|
|
7
|
+
#include <iostream>
|
|
8
|
+
#include <stdlib.h>
|
|
9
|
+
#include <time.h>
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
std::vector<std::string> StringSplit(const std::string& str, const std::string& pattern);
|
|
13
|
+
std::string StringFormat(const char* lpcszFormat, ...);
|
|
Binary file
|
|
Binary file
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
#include "utils.h"
|
|
2
|
+
|
|
3
|
+
std::vector<std::string> StringSplit(const std::string& str, const std::string& pattern)
|
|
4
|
+
{
|
|
5
|
+
std::string::size_type pos;
|
|
6
|
+
std::vector<std::string> result;
|
|
7
|
+
std::string _str(str);
|
|
8
|
+
_str += pattern;//��չ�ַ����Է������
|
|
9
|
+
size_t size = _str.size();
|
|
10
|
+
for (size_t i = 0; i < size; i++)
|
|
11
|
+
{
|
|
12
|
+
pos = _str.find(pattern, i);
|
|
13
|
+
if (pos < size)
|
|
14
|
+
{
|
|
15
|
+
std::string s = _str.substr(i, pos - i);
|
|
16
|
+
result.push_back(s);
|
|
17
|
+
i = pos + pattern.size() - 1;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return result;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
std::string StringFormat(const char* lpcszFormat, ...)
|
|
25
|
+
{
|
|
26
|
+
std::string strResult;
|
|
27
|
+
if (NULL != lpcszFormat)
|
|
28
|
+
{
|
|
29
|
+
va_list marker = NULL;
|
|
30
|
+
va_start(marker, lpcszFormat); //��ʼ����������
|
|
31
|
+
size_t nLength = _vscprintf(lpcszFormat, marker) + 1; //��ȡ��ʽ���ַ�������
|
|
32
|
+
std::vector<char> vBuffer(nLength, '\0'); //�������ڴ洢��ʽ���ַ������ַ�����
|
|
33
|
+
int nWritten = _vsnprintf_s(&vBuffer[0], vBuffer.size(), nLength, lpcszFormat, marker);
|
|
34
|
+
if (nWritten > 0)
|
|
35
|
+
{
|
|
36
|
+
strResult = &vBuffer[0];
|
|
37
|
+
}
|
|
38
|
+
va_end(marker); //��������
|
|
39
|
+
}
|
|
40
|
+
return strResult;
|
|
41
|
+
}
|
package/src/libcurl.ts
ADDED
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
import bindings from 'bindings'
|
|
2
|
+
|
|
3
|
+
const { BaoLibCurl } = bindings('bao_curl_node_addon');
|
|
4
|
+
|
|
5
|
+
export enum LibCurl_HTTP_VERSION {
|
|
6
|
+
http1_1,
|
|
7
|
+
http2,
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export class LibCurl {
|
|
11
|
+
private m_libCurl_impl_: any;
|
|
12
|
+
private m_isAsync_: boolean;
|
|
13
|
+
private m_isSending_: boolean;
|
|
14
|
+
constructor() {
|
|
15
|
+
this.m_libCurl_impl_ = new BaoLibCurl();
|
|
16
|
+
}
|
|
17
|
+
private checkSending(): void {
|
|
18
|
+
if (this.m_isSending_) {
|
|
19
|
+
throw new Error('the last request is sending, don\'t send one more request on one instance!')
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
public open(method: string, url: string, async: boolean = true): void {
|
|
23
|
+
this.checkSending();
|
|
24
|
+
this.m_libCurl_impl_.open(method, url);
|
|
25
|
+
this.m_isAsync_ = async;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
public setRequestHeader(key: string, value: string): void {
|
|
29
|
+
this.checkSending();
|
|
30
|
+
this.m_libCurl_impl_.setRequestHeader(key, value);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
*
|
|
35
|
+
* @param headers 多个header 以\n换行链接的文本
|
|
36
|
+
*/
|
|
37
|
+
public setRequestHeaders(headers: string): void {
|
|
38
|
+
this.checkSending();
|
|
39
|
+
this.m_libCurl_impl_.setRequestHeaders(headers);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
*
|
|
44
|
+
* @param proxy host:port sample:127.0.0.1:8888
|
|
45
|
+
* @param username
|
|
46
|
+
* @param password
|
|
47
|
+
*/
|
|
48
|
+
public setProxy(proxy: string, username?: string, password?: string): void {
|
|
49
|
+
this.checkSending();
|
|
50
|
+
if (username && password) {
|
|
51
|
+
this.m_libCurl_impl_.setProxy(proxy, username, password);
|
|
52
|
+
} else {
|
|
53
|
+
this.m_libCurl_impl_.setProxy(proxy);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
*
|
|
59
|
+
* @param connectTime 连接上远程服务器的最长等待时间
|
|
60
|
+
* @param sendTime 发送最长等待时间
|
|
61
|
+
* sendTime时长包含connectTime 所以sendTime要大于connectTime
|
|
62
|
+
*/
|
|
63
|
+
public setTimeout(connectTime: number, sendTime: number): void {
|
|
64
|
+
this.checkSending();
|
|
65
|
+
if (connectTime > sendTime) {
|
|
66
|
+
throw new Error('连接时间大于发送等待时间.');
|
|
67
|
+
}
|
|
68
|
+
this.m_libCurl_impl_.setTimeout(connectTime, sendTime);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
*
|
|
73
|
+
* @param key
|
|
74
|
+
* @param value
|
|
75
|
+
* @param domain cookie作用域 sample: .baidu.com baike.baidu.com
|
|
76
|
+
*/
|
|
77
|
+
public setCookie(key: string, value: string, domain: string): void {
|
|
78
|
+
this.checkSending();
|
|
79
|
+
this.m_libCurl_impl_.setCookie(key, value, domain);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
*
|
|
84
|
+
* @param key
|
|
85
|
+
* @param domain cookie作用域 sample: .baidu.com baike.baidu.com
|
|
86
|
+
*/
|
|
87
|
+
public removeCookie(key: string, domain: string): void {
|
|
88
|
+
this.checkSending();
|
|
89
|
+
this.m_libCurl_impl_.removeCookie(key, domain);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
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
|
|
97
|
+
*/
|
|
98
|
+
public getCookies(): string {
|
|
99
|
+
this.checkSending();
|
|
100
|
+
return this.m_libCurl_impl_.getCookies();
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
*
|
|
105
|
+
* @param key
|
|
106
|
+
* @returns 返回该key对应的cookie
|
|
107
|
+
* sample:
|
|
108
|
+
*/
|
|
109
|
+
public getCookie(key: string): string {
|
|
110
|
+
this.checkSending();
|
|
111
|
+
return this.m_libCurl_impl_.getCookie(key);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
*
|
|
116
|
+
* @returns 返回响应头
|
|
117
|
+
*/
|
|
118
|
+
public getResponseHeaders(): string {
|
|
119
|
+
this.checkSending();
|
|
120
|
+
return this.m_libCurl_impl_.getResponseHeaders();
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
*
|
|
125
|
+
* @returns 返回状态码
|
|
126
|
+
* sample: 200 403 404
|
|
127
|
+
*/
|
|
128
|
+
public getResponseStatus(): number {
|
|
129
|
+
this.checkSending();
|
|
130
|
+
return this.m_libCurl_impl_.getResponseStatus();
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* 重置curl 包括之前的所有设定
|
|
135
|
+
*/
|
|
136
|
+
public reset(): void {
|
|
137
|
+
this.checkSending();
|
|
138
|
+
this.m_libCurl_impl_.reset();
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
*
|
|
143
|
+
* @param isAllow 是否允许重定向
|
|
144
|
+
*/
|
|
145
|
+
public setRedirect(isAllow: boolean): void {
|
|
146
|
+
this.checkSending();
|
|
147
|
+
this.m_libCurl_impl_.setRedirect(isAllow);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* 打印libcurl内部的 解析信息、连接信息、tls信息等等
|
|
152
|
+
*/
|
|
153
|
+
public printInnerLogger(): void {
|
|
154
|
+
this.checkSending();
|
|
155
|
+
this.m_libCurl_impl_.printInnerLogger();
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
*
|
|
160
|
+
* @param version
|
|
161
|
+
* 设置http版本号
|
|
162
|
+
*/
|
|
163
|
+
public setHttpVersion(version: LibCurl_HTTP_VERSION): void {
|
|
164
|
+
this.checkSending();
|
|
165
|
+
this.m_libCurl_impl_.setHttpVersion(version);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
*
|
|
170
|
+
* @param body POST PUT PATCH时 发送的body
|
|
171
|
+
* 当body不为string或uint8array时 此函数将用JSON.stringify转换对象
|
|
172
|
+
*/
|
|
173
|
+
public send(body?: string | Uint8Array | any): Promise<undefined> | undefined {
|
|
174
|
+
this.checkSending();
|
|
175
|
+
this.m_isSending_ = true;
|
|
176
|
+
if (this.m_isAsync_) {
|
|
177
|
+
return new Promise((resolve, reject) => {
|
|
178
|
+
const callback = () => {
|
|
179
|
+
this.m_isSending_ = false;
|
|
180
|
+
resolve(void 0);
|
|
181
|
+
};
|
|
182
|
+
if (body) {
|
|
183
|
+
this.m_libCurl_impl_.sendAsync(body, callback);
|
|
184
|
+
} else {
|
|
185
|
+
this.m_libCurl_impl_.sendAsync(callback);
|
|
186
|
+
}
|
|
187
|
+
})
|
|
188
|
+
}
|
|
189
|
+
if (body) {
|
|
190
|
+
this.m_libCurl_impl_.send(body);
|
|
191
|
+
} else {
|
|
192
|
+
this.m_libCurl_impl_.send();
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
public getResponseBody(): Uint8Array {
|
|
197
|
+
this.checkSending();
|
|
198
|
+
return this.m_libCurl_impl_.getResponseBody();
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
public getResponseString(): string {
|
|
202
|
+
this.checkSending();
|
|
203
|
+
return this.m_libCurl_impl_.getResponseString();
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
public getResponseJson(): Object {
|
|
207
|
+
this.checkSending();
|
|
208
|
+
return JSON.parse(this.getResponseString());
|
|
209
|
+
}
|
|
210
|
+
|
|
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
|
+
[, callbackName] = /(.*) is not defined/g.exec(error.message)
|
|
230
|
+
return this.getResponseJsonp(callbackName);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
return eval(jsonstr);
|
|
234
|
+
}
|
|
235
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "es6",
|
|
4
|
+
"module": "commonjs",
|
|
5
|
+
"declaration": true,
|
|
6
|
+
"removeComments": true,
|
|
7
|
+
"noImplicitAny": false,
|
|
8
|
+
"sourceMap": true,
|
|
9
|
+
"rootDir": ".",
|
|
10
|
+
"outDir": "dist",
|
|
11
|
+
"typeRoots": [
|
|
12
|
+
"./typings"
|
|
13
|
+
],
|
|
14
|
+
"experimentalDecorators": true,
|
|
15
|
+
"emitDecoratorMetadata": true,
|
|
16
|
+
"watch": true,
|
|
17
|
+
"esModuleInterop": true
|
|
18
|
+
},
|
|
19
|
+
"lib": [
|
|
20
|
+
"es2017",
|
|
21
|
+
"es7",
|
|
22
|
+
"es6",
|
|
23
|
+
"dom"
|
|
24
|
+
],
|
|
25
|
+
"include": [
|
|
26
|
+
"src/*"
|
|
27
|
+
],
|
|
28
|
+
"exclude": [
|
|
29
|
+
|
|
30
|
+
]
|
|
31
|
+
}
|