xjs-node 1.0.2 → 1.0.3

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,4 +1,7 @@
1
- import { ClientOption, HttpResponse, IHttpClient, RequestOption } from "./i-http-client";
1
+ /// <reference types="node" />
2
+ /// <reference types="node" />
3
+ import { OutgoingHttpHeaders, IncomingHttpHeaders } from "http";
4
+ import { ClientOption, IHttpClient } from "./i-http-client";
2
5
  import { Loggable } from "xjs-common";
3
6
  export declare const s_clientMode: {
4
7
  nodejs: {
@@ -25,29 +28,70 @@ export declare class HttpResolverContext implements IHttpClient {
25
28
  private _cookies?;
26
29
  get clientMode(): string;
27
30
  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
- get(url: string, op?: RequestOption & {
31
+ get(url: string, op?: {
32
+ headers?: OutgoingHttpHeaders;
33
+ ignoreQuery?: boolean;
34
+ downloadPath?: string;
35
+ timeout?: number;
36
+ } & {
38
37
  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>;
38
+ }): Promise<{
39
+ headers?: IncomingHttpHeaders;
40
+ payload: string;
41
+ }>;
42
+ get(url: string, op?: {
43
+ headers?: OutgoingHttpHeaders;
44
+ ignoreQuery?: boolean;
45
+ downloadPath?: string;
46
+ timeout?: number;
47
+ responseType: "string";
48
+ } & {
49
+ outerRedirectCount?: number;
50
+ }): Promise<{
51
+ headers?: IncomingHttpHeaders;
52
+ payload: string;
53
+ }>;
54
+ get(url: string, op?: {
55
+ headers?: OutgoingHttpHeaders;
56
+ ignoreQuery?: boolean;
57
+ downloadPath?: string;
58
+ timeout?: number;
59
+ responseType: "buffer";
60
+ } & {
61
+ outerRedirectCount?: number;
62
+ }): Promise<{
63
+ headers?: IncomingHttpHeaders;
64
+ payload: Buffer;
65
+ }>;
66
+ post(url: string, payload: any, op?: {
67
+ headers?: OutgoingHttpHeaders;
68
+ ignoreQuery?: boolean;
69
+ downloadPath?: string;
70
+ timeout?: number;
71
+ }): Promise<{
72
+ headers?: IncomingHttpHeaders;
73
+ payload: string;
74
+ }>;
75
+ post(url: string, payload: any, op?: {
76
+ headers?: OutgoingHttpHeaders;
77
+ ignoreQuery?: boolean;
78
+ downloadPath?: string;
79
+ timeout?: number;
80
+ responseType: "string";
81
+ }): Promise<{
82
+ headers?: IncomingHttpHeaders;
83
+ payload: string;
84
+ }>;
85
+ post(url: string, payload: any, op?: {
86
+ headers?: OutgoingHttpHeaders;
87
+ ignoreQuery?: boolean;
88
+ downloadPath?: string;
89
+ timeout?: number;
90
+ responseType: "buffer";
91
+ }): Promise<{
92
+ headers?: IncomingHttpHeaders;
93
+ payload: Buffer;
94
+ }>;
51
95
  private createProxyAgent;
52
96
  private getIn;
53
97
  private postIn;
@@ -104,6 +104,7 @@ class HttpResolverContext {
104
104
  * @param op.ignoreQuery {@link RequestOption.ignoreQuery}
105
105
  * @param op.downloadPath {@link RequestOption.downloadPath}
106
106
  * @param op.timeout {@link RequestOption.timeout}
107
+ * @param op.responseType {@link RequestOption.responseType}
107
108
  * @returns string encoded by utf-8 as response payload.
108
109
  */
109
110
  async get(url, op) {
@@ -121,6 +122,7 @@ class HttpResolverContext {
121
122
  * @param op.ignoreQuery {@link RequestOption.ignoreQuery}
122
123
  * @param op.downloadPath {@link RequestOption.downloadPath}
123
124
  * @param op.timeout {@link RequestOption.timeout}
125
+ * @param op.responseType {@link RequestOption.responseType}
124
126
  * @returns string encoded by utf-8 as response payload.
125
127
  */
126
128
  async post(url, payload, op) {
@@ -238,9 +240,9 @@ class HttpResolverContext {
238
240
  retBuf = zlib.gunzipSync(retBuf);
239
241
  else if (contentEncofing == "br")
240
242
  retBuf = zlib.brotliDecompressSync(retBuf);
241
- const data = retBuf.toString("utf8");
243
+ const data = rc.responseType === "buffer" ? retBuf : retBuf.toString("utf8");
242
244
  if (sc !== 2) {
243
- if (data.trim())
245
+ if (xjs_common_1.UType.isString(data) && data.trim())
244
246
  this.warn(data);
245
247
  reject(new xjs_common_1.XjsErr(s_errCode, `Https received a error status ${res.statusCode}`));
246
248
  }
@@ -1,6 +1,9 @@
1
+ /// <reference types="node" />
2
+ /// <reference types="node" />
1
3
  import { Loggable } from "xjs-common";
2
4
  import { HttpResolverContext } from "./http-resolver-context";
3
- import { ClientOption, HttpResponse, IHttpClient, RequestOption } from "./i-http-client";
5
+ import { ClientOption, IHttpClient } from "./i-http-client";
6
+ import { IncomingHttpHeaders, OutgoingHttpHeaders } from "http";
4
7
  export interface ClientMode {
5
8
  id: number;
6
9
  cipherOrder: number[];
@@ -28,9 +31,69 @@ export declare class HttpResolver implements IHttpClient {
28
31
  * @returns a http client as new context.
29
32
  */
30
33
  newContext(op?: ClientOption): HttpResolverContext;
31
- get(url: string, op?: RequestOption & ClientOption & {
34
+ get(url: string, op?: {
35
+ headers?: OutgoingHttpHeaders;
36
+ ignoreQuery?: boolean;
37
+ downloadPath?: string;
38
+ timeout?: number;
39
+ } & ClientOption & {
32
40
  redirectAsNewRequest?: boolean;
33
- }): Promise<HttpResponse>;
34
- post(url: string, payload: any, op?: RequestOption & ClientOption): Promise<HttpResponse>;
41
+ }): Promise<{
42
+ headers?: IncomingHttpHeaders;
43
+ payload: string;
44
+ }>;
45
+ get(url: string, op?: {
46
+ headers?: OutgoingHttpHeaders;
47
+ ignoreQuery?: boolean;
48
+ downloadPath?: string;
49
+ timeout?: number;
50
+ responseType: "string";
51
+ } & ClientOption & {
52
+ redirectAsNewRequest?: boolean;
53
+ }): Promise<{
54
+ headers?: IncomingHttpHeaders;
55
+ payload: string;
56
+ }>;
57
+ get(url: string, op?: {
58
+ headers?: OutgoingHttpHeaders;
59
+ ignoreQuery?: boolean;
60
+ downloadPath?: string;
61
+ timeout?: number;
62
+ responseType: "buffer";
63
+ } & ClientOption & {
64
+ redirectAsNewRequest?: boolean;
65
+ }): Promise<{
66
+ headers?: IncomingHttpHeaders;
67
+ payload: Buffer;
68
+ }>;
69
+ post(url: string, payload: any, op?: {
70
+ headers?: OutgoingHttpHeaders;
71
+ ignoreQuery?: boolean;
72
+ downloadPath?: string;
73
+ timeout?: number;
74
+ } & ClientOption): Promise<{
75
+ headers?: IncomingHttpHeaders;
76
+ payload: string;
77
+ }>;
78
+ post(url: string, payload: any, op?: {
79
+ headers?: OutgoingHttpHeaders;
80
+ ignoreQuery?: boolean;
81
+ downloadPath?: string;
82
+ timeout?: number;
83
+ responseType: "string";
84
+ } & ClientOption): Promise<{
85
+ headers?: IncomingHttpHeaders;
86
+ payload: string;
87
+ }>;
88
+ post(url: string, payload: any, op?: {
89
+ headers?: OutgoingHttpHeaders;
90
+ ignoreQuery?: boolean;
91
+ downloadPath?: string;
92
+ timeout?: number;
93
+ responseType: "buffer";
94
+ } & ClientOption): Promise<{
95
+ headers?: IncomingHttpHeaders;
96
+ payload: Buffer;
97
+ }>;
35
98
  private fixCmv;
36
99
  }
@@ -1,4 +1,5 @@
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 {
@@ -20,6 +21,10 @@ export interface RequestOption {
20
21
  * timeout milliseconds to wait for socket inactivity. default is infinity.
21
22
  */
22
23
  timeout?: number;
24
+ /**
25
+ * type of response payload. default is string (utf-8).
26
+ */
27
+ responseType?: "string" | "buffer";
23
28
  }
24
29
  export interface HttpResponse {
25
30
  /**
@@ -27,9 +32,9 @@ export interface HttpResponse {
27
32
  */
28
33
  headers?: IncomingHttpHeaders;
29
34
  /**
30
- * string encoded by utf-8 as response payload.
35
+ * response payload which has a type depends on {@link RequestOption.responseType}.
31
36
  */
32
- payload?: string;
37
+ payload?: string | Buffer;
33
38
  }
34
39
  export interface IHttpClient {
35
40
  /**
@@ -41,6 +46,7 @@ export interface IHttpClient {
41
46
  * @param op.ignoreQuery {@link RequestOption.ignoreQuery}
42
47
  * @param op.downloadPath {@link RequestOption.downloadPath}
43
48
  * @param op.timeout {@link RequestOption.timeout}
49
+ * @param op.responseType {@link RequestOption.responseType}
44
50
  * @param op.redirectAsNewRequest handle redirect as new request. this may be efficient when using proxy which is implemented reverse proxy.
45
51
  * @returns http response. {@link HttpResponse}
46
52
  */
@@ -57,6 +63,7 @@ export interface IHttpClient {
57
63
  * @param op.ignoreQuery {@link RequestOption.ignoreQuery}
58
64
  * @param op.downloadPath {@link RequestOption.downloadPath}
59
65
  * @param op.timeout {@link RequestOption.timeout}
66
+ * @param op.responseType {@link RequestOption.responseType}
60
67
  * @returns http response. {@link HttpResponse}
61
68
  */
62
69
  post(url: string, payload: any, op?: RequestOption & ClientOption): Promise<HttpResponse>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xjs-node",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "library modules for nodejs + typescript that bundled general-purpose implementations.",
5
5
  "repository": {
6
6
  "type": "git",