xjs-node 1.0.2 → 1.0.4

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.
@@ -1,3 +1,4 @@
1
+ /// <reference types="node" />
1
2
  import { ClientOption, HttpResponse, IHttpClient, RequestOption } from "./i-http-client";
2
3
  import { Loggable } from "xjs-common";
3
4
  export declare const s_clientMode: {
@@ -25,29 +26,24 @@ export declare class HttpResolverContext implements IHttpClient {
25
26
  private _cookies?;
26
27
  get clientMode(): string;
27
28
  constructor(cmv: number, op?: ClientOption, _l?: Loggable);
28
- /**
29
- * request GET to the url.
30
- * @param url target url.
31
- * @param op.headers http headers.
32
- * @param op.ignoreQuery {@link RequestOption.ignoreQuery}
33
- * @param op.downloadPath {@link RequestOption.downloadPath}
34
- * @param op.timeout {@link RequestOption.timeout}
35
- * @returns string encoded by utf-8 as response payload.
36
- */
37
29
  get(url: string, op?: RequestOption & {
38
30
  outerRedirectCount?: number;
39
- }): Promise<HttpResponse>;
40
- /**
41
- * request POST to the url.
42
- * @param url target url.
43
- * @param payload request payload. if this is an object, it is treated as json.
44
- * @param op.headers http headers.
45
- * @param op.ignoreQuery {@link RequestOption.ignoreQuery}
46
- * @param op.downloadPath {@link RequestOption.downloadPath}
47
- * @param op.timeout {@link RequestOption.timeout}
48
- * @returns string encoded by utf-8 as response payload.
49
- */
50
- post(url: string, payload: any, op?: RequestOption): Promise<HttpResponse>;
31
+ responseType: "string";
32
+ }): Promise<HttpResponse<string>>;
33
+ get(url: string, op?: RequestOption & {
34
+ outerRedirectCount?: number;
35
+ responseType: "buffer";
36
+ }): Promise<HttpResponse<Buffer>>;
37
+ get(url: string, op?: RequestOption & {
38
+ outerRedirectCount?: number;
39
+ }): Promise<HttpResponse<string>>;
40
+ post(url: string, payload: any, op?: RequestOption & {
41
+ responseType: "string";
42
+ }): Promise<HttpResponse<string>>;
43
+ post(url: string, payload: any, op?: RequestOption & {
44
+ responseType: "buffer";
45
+ }): Promise<HttpResponse<Buffer>>;
46
+ post(url: string, payload: any, op?: RequestOption): Promise<HttpResponse<string>>;
51
47
  private createProxyAgent;
52
48
  private getIn;
53
49
  private postIn;
@@ -97,15 +97,6 @@ class HttpResolverContext {
97
97
  this._chHeaders = s_mode2headers.get(this._mode)(this.cmv);
98
98
  }
99
99
  }
100
- /**
101
- * request GET to the url.
102
- * @param url target url.
103
- * @param op.headers http headers.
104
- * @param op.ignoreQuery {@link RequestOption.ignoreQuery}
105
- * @param op.downloadPath {@link RequestOption.downloadPath}
106
- * @param op.timeout {@link RequestOption.timeout}
107
- * @returns string encoded by utf-8 as response payload.
108
- */
109
100
  async get(url, op) {
110
101
  const u = new url_1.URL(url);
111
102
  const proxyAgent = this._proxyConfig && await this.createProxyAgent(u);
@@ -113,16 +104,6 @@ class HttpResolverContext {
113
104
  Object.assign(rc, op);
114
105
  return await this._als.run(rc, this.getIn, u).finally(() => proxyAgent?.destroy());
115
106
  }
116
- /**
117
- * request POST to the url.
118
- * @param url target url.
119
- * @param payload request payload. if this is an object, it is treated as json.
120
- * @param op.headers http headers.
121
- * @param op.ignoreQuery {@link RequestOption.ignoreQuery}
122
- * @param op.downloadPath {@link RequestOption.downloadPath}
123
- * @param op.timeout {@link RequestOption.timeout}
124
- * @returns string encoded by utf-8 as response payload.
125
- */
126
107
  async post(url, payload, op) {
127
108
  const u = new url_1.URL(url);
128
109
  const proxyAgent = this._proxyConfig && await this.createProxyAgent(u);
@@ -238,9 +219,9 @@ class HttpResolverContext {
238
219
  retBuf = zlib.gunzipSync(retBuf);
239
220
  else if (contentEncofing == "br")
240
221
  retBuf = zlib.brotliDecompressSync(retBuf);
241
- const data = retBuf.toString("utf8");
222
+ const data = rc.responseType === "buffer" ? retBuf : retBuf.toString("utf8");
242
223
  if (sc !== 2) {
243
- if (data.trim())
224
+ if (xjs_common_1.UType.isString(data) && data.trim())
244
225
  this.warn(data);
245
226
  reject(new xjs_common_1.XjsErr(s_errCode, `Https received a error status ${res.statusCode}`));
246
227
  }
@@ -1,3 +1,4 @@
1
+ /// <reference types="node" />
1
2
  import { Loggable } from "xjs-common";
2
3
  import { HttpResolverContext } from "./http-resolver-context";
3
4
  import { ClientOption, HttpResponse, IHttpClient, RequestOption } from "./i-http-client";
@@ -30,7 +31,21 @@ export declare class HttpResolver implements IHttpClient {
30
31
  newContext(op?: ClientOption): HttpResolverContext;
31
32
  get(url: string, op?: RequestOption & ClientOption & {
32
33
  redirectAsNewRequest?: boolean;
33
- }): Promise<HttpResponse>;
34
- post(url: string, payload: any, op?: RequestOption & ClientOption): Promise<HttpResponse>;
34
+ responseType: "string";
35
+ }): Promise<HttpResponse<string>>;
36
+ get(url: string, op?: RequestOption & ClientOption & {
37
+ redirectAsNewRequest?: boolean;
38
+ responseType: "buffer";
39
+ }): Promise<HttpResponse<Buffer>>;
40
+ get(url: string, op?: RequestOption & ClientOption & {
41
+ redirectAsNewRequest?: boolean;
42
+ }): Promise<HttpResponse<string>>;
43
+ post(url: string, payload: any, op?: RequestOption & ClientOption & {
44
+ responseType: "string";
45
+ }): Promise<HttpResponse<string>>;
46
+ post(url: string, payload: any, op?: RequestOption & ClientOption & {
47
+ responseType: "buffer";
48
+ }): Promise<HttpResponse<Buffer>>;
49
+ post(url: string, payload: any, op?: RequestOption & ClientOption): Promise<HttpResponse<string>>;
35
50
  private fixCmv;
36
51
  }
@@ -1,8 +1,15 @@
1
1
  /// <reference types="node" />
2
+ /// <reference types="node" />
2
3
  import { IncomingHttpHeaders, OutgoingHttpHeaders } from "http";
3
4
  import { ClientMode, ProxyConfig } from "./http-resolver";
4
5
  export interface ClientOption {
6
+ /**
7
+ * {@link s_clientMode} that is imitated. default is random between chrome or firefox.
8
+ */
5
9
  mode?: ClientMode;
10
+ /**
11
+ * proxy configuration.
12
+ */
6
13
  proxy?: ProxyConfig;
7
14
  }
8
15
  export interface RequestOption {
@@ -20,16 +27,20 @@ export interface RequestOption {
20
27
  * timeout milliseconds to wait for socket inactivity. default is infinity.
21
28
  */
22
29
  timeout?: number;
30
+ /**
31
+ * type of response payload. default is string (utf-8).
32
+ */
33
+ responseType?: "string" | "buffer";
23
34
  }
24
- export interface HttpResponse {
35
+ export interface HttpResponse<T = string | Buffer> {
25
36
  /**
26
37
  * http headers in the response.
27
38
  */
28
39
  headers?: IncomingHttpHeaders;
29
40
  /**
30
- * string encoded by utf-8 as response payload.
41
+ * response payload which has a type depends on {@link RequestOption.responseType}.
31
42
  */
32
- payload?: string;
43
+ payload?: T;
33
44
  }
34
45
  export interface IHttpClient {
35
46
  /**
@@ -41,12 +52,21 @@ export interface IHttpClient {
41
52
  * @param op.ignoreQuery {@link RequestOption.ignoreQuery}
42
53
  * @param op.downloadPath {@link RequestOption.downloadPath}
43
54
  * @param op.timeout {@link RequestOption.timeout}
55
+ * @param op.responseType {@link RequestOption.responseType}
44
56
  * @param op.redirectAsNewRequest handle redirect as new request. this may be efficient when using proxy which is implemented reverse proxy.
45
57
  * @returns http response. {@link HttpResponse}
46
- */
58
+ */
59
+ get(url: string, op?: RequestOption & ClientOption & {
60
+ redirectAsNewRequest?: boolean;
61
+ responseType: "string";
62
+ }): Promise<HttpResponse<string>>;
63
+ get(url: string, op?: RequestOption & ClientOption & {
64
+ redirectAsNewRequest?: boolean;
65
+ responseType: "buffer";
66
+ }): Promise<HttpResponse<Buffer>>;
47
67
  get(url: string, op?: RequestOption & ClientOption & {
48
68
  redirectAsNewRequest?: boolean;
49
- }): Promise<HttpResponse>;
69
+ }): Promise<HttpResponse<string>>;
50
70
  /**
51
71
  * request POST to the url with new context.
52
72
  * @param url target url. (currently https only)
@@ -57,7 +77,14 @@ export interface IHttpClient {
57
77
  * @param op.ignoreQuery {@link RequestOption.ignoreQuery}
58
78
  * @param op.downloadPath {@link RequestOption.downloadPath}
59
79
  * @param op.timeout {@link RequestOption.timeout}
80
+ * @param op.responseType {@link RequestOption.responseType}
60
81
  * @returns http response. {@link HttpResponse}
61
82
  */
62
- post(url: string, payload: any, op?: RequestOption & ClientOption): Promise<HttpResponse>;
83
+ post(url: string, payload: any, op?: RequestOption & ClientOption & {
84
+ responseType: "string";
85
+ }): Promise<HttpResponse<string>>;
86
+ post(url: string, payload: any, op?: RequestOption & ClientOption & {
87
+ responseType: "buffer";
88
+ }): Promise<HttpResponse<Buffer>>;
89
+ post(url: string, payload: any, op?: RequestOption & ClientOption): Promise<HttpResponse<string>>;
63
90
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xjs-node",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "description": "library modules for nodejs + typescript that bundled general-purpose implementations.",
5
5
  "repository": {
6
6
  "type": "git",