@e-mc/request 0.9.8 → 0.9.10

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/LICENSE CHANGED
@@ -1,11 +1,11 @@
1
- Copyright 2024 An Pham
2
-
3
- Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
4
-
5
- 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
6
-
7
- 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8
-
9
- 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
10
-
1
+ Copyright 2024 An Pham
2
+
3
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
4
+
5
+ 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
6
+
7
+ 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8
+
9
+ 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
10
+
11
11
  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package/README.md CHANGED
@@ -1,212 +1,212 @@
1
- # @e-mc/request
2
-
3
- * NodeJS 16
4
- * ES2020
5
-
6
- ## General Usage
7
-
8
- * [Read the Docs](https://e-mc.readthedocs.io)
9
-
10
- ## Interface
11
-
12
- * [View Source](https://www.unpkg.com/@e-mc/types@0.9.8/lib/index.d.ts)
13
-
14
- ```typescript
15
- import type { IModule, ModuleConstructor } from "./index";
16
- import type { HttpAgentSettings, HttpProtocolVersion, HttpRequestClient, InternetProtocolVersion } from "./http";
17
- import type { ApplyOptions, Aria2Options, FormDataPart, HeadersOnCallback, HostConfig, OpenOptions, PostOptions, ProxySettings, ReadExpectType, RequestInit, StatusOnCallback } from "./request";
18
- import type { DnsLookupSettings, RequestModule, RequestSettings } from "./settings";
19
-
20
- import type { ClientRequest, OutgoingHttpHeaders } from "http";
21
- import type { LookupFunction } from "net";
22
- import type { Writable } from "stream";
23
-
24
- interface IRequest extends IModule {
25
- module: RequestModule;
26
- startTime: number;
27
- acceptEncoding: boolean;
28
- keepAlive: boolean | null;
29
- readTimeout: number;
30
- readExpect: ReadExpectType;
31
- proxy: ProxySettings | null;
32
- init(config?: RequestInit): this;
33
- apply(options: ApplyOptions): this;
34
- addDns(hostname: string, address: string, timeout: number): void;
35
- addDns(hostname: string, address: string, family?: string, timeout?: number): void;
36
- lookupDns(hostname: string): LookupFunction;
37
- proxyOf(uri: string, localhost?: boolean): ProxySettings | undefined;
38
- statusOn(name: number | number[], callback: StatusOnCallback): void;
39
- statusOn(name: number | number[], globUrl: string, callback: StatusOnCallback): void;
40
- headersOn(name: string | string[], callback: HeadersOnCallback): void;
41
- headersOn(name: string | string[], globUrl: string, callback: HeadersOnCallback): void;
42
- headersOf(uri: string): OutgoingHttpHeaders | undefined;
43
- aria2c(uri: string | URL, pathname: string): Promise<string[]>;
44
- aria2c(uri: string | URL, options?: Aria2Options): Promise<string[]>;
45
- json(uri: string | URL, options?: OpenOptions): Promise<object | null>;
46
- pipe(uri: string | URL, to: Writable, options?: OpenOptions): Promise<null>;
47
- opts(url: string | URL, options?: OpenOptions): HostConfig;
48
- open(uri: string | URL, options: OpenOptions): HttpRequestClient;
49
- head(uri: string | URL, options?: OpenOptions): ClientRequest;
50
- post(uri: string | URL, data: unknown, contentType: string): Promise<Buffer | string | null>;
51
- post(uri: string | URL, parts: FormDataPart[]): Promise<Buffer | string | null>;
52
- post(uri: string | URL, form: Record<string, unknown>, parts: FormDataPart[]): Promise<Buffer | string | null>;
53
- post(uri: string | URL, data: unknown, options: PostOptions): Promise<Buffer | string | null>;
54
- post(uri: string | URL, data: unknown, contentType?: string, options?: PostOptions): Promise<Buffer | string | null>;
55
- get(uri: string | URL, options?: OpenOptions): Promise<Buffer | object | string | null>;
56
- detach(singleton?: boolean): void;
57
- reset(): void;
58
- close(): void;
59
- set agentTimeout(value);
60
- get agentTimeout(): number;
61
- set httpVersion(value);
62
- get httpVersion(): HttpProtocolVersion | null;
63
- set ipVersion(value);
64
- get ipVersion(): InternetProtocolVersion;
65
- get settings(): RequestSettings;
66
- }
67
-
68
- interface RequestConstructor extends ModuleConstructor {
69
- readCACert(value: string, cache?: boolean): string;
70
- readTLSKey(value: string, cache?: boolean): string;
71
- readTLSCert(value: string, cache?: boolean): string;
72
- isCert(value: string): boolean;
73
- fromURL(url: URL, value: string): string;
74
- fromStatusCode(value: number | string): string;
75
- defineHttpAgent(options: HttpAgentSettings): void;
76
- defineDnsLookup(options: DnsLookupSettings, clear?: boolean): void;
77
- getAria2Path(): string;
78
- readonly prototype: IRequest;
79
- new(module?: RequestModule): IRequest;
80
- }
81
- ```
82
-
83
- ## Settings
84
-
85
- ```typescript
86
- import type { PermittedDirectories } from "./core";
87
- import type { SecureConfig } from "./http";
88
- import type { PurgeComponent } from "./settings";
89
-
90
- import type { LookupAddress } from "dns";
91
- import type { OutgoingHttpHeaders } from "http";
92
-
93
- interface RequestModule {
94
- handler: "@e-mc/request";
95
- timeout?: number | string;
96
- read_timeout?: number | string;
97
- agent?: {
98
- keep_alive?: boolean;
99
- timeout?: number | string;
100
- };
101
- connect?: {
102
- timeout?: number | string;
103
- retry_wait?: number | string;
104
- retry_after?: number | string;
105
- retry_limit?: number;
106
- redirect_limit?: number;
107
- };
108
- dns?: {
109
- family?: number;
110
- expires?: number | string;
111
- resolve?: Record<string, Partial<LookupAddress>>;
112
- };
113
- use?: {
114
- http_version?: 1 | 2;
115
- accept_encoding?: boolean;
116
- };
117
- proxy?: {
118
- address?: string;
119
- port?: number;
120
- username?: string;
121
- password?: string;
122
- include?: string[];
123
- exclude?: string[];
124
- keep_alive?: boolean;
125
- };
126
- headers: Record<string, OutgoingHttpHeaders>;
127
- certs?: Record<string, SecureConfig<string | string[]>>;
128
- localhost?: string[];
129
- protocol?: {
130
- "http/1.1"?: string[];
131
- h2c?: string[];
132
- h2?: string[];
133
- };
134
- post_limit?: number | string;
135
- settings?: {
136
- broadcast_id?: string | string[];
137
- time_format?: "readable" | "relative" | "none";
138
- purge?: PurgeComponent;
139
- }
140
- }
141
-
142
- interface DownloadModule {
143
- expires?: number | string;
144
- aria2?: {
145
- bin?: string | false;
146
- exec?: {
147
- uid?: number;
148
- gid?: number;
149
- };
150
- update_status?: number | { interval?: number; broadcast_only?: boolean };
151
- max_concurrent_downloads?: number;
152
- max_connection_per_server?: number;
153
- bt_stop_timeout?: number;
154
- bt_tracker_connect_timeout?: number;
155
- bt_tracker_timeout?: number;
156
- min_split_size?: string;
157
- disk_cache?: number | string;
158
- lowest_speed_limit?: number | string;
159
- always_resume?: boolean;
160
- file_allocation?: "none" | "prealloc" | "trunc" | "falloc";
161
- conf_path?: string;
162
- };
163
- }
164
- ```
165
-
166
- ### Example usage
167
-
168
- ```javascript
169
- const Request = require("@e-mc/request");
170
-
171
- const instance = new Request({
172
- read_timeout: 30,
173
- connect: {
174
- timeout: 20, // Seconds
175
- retry_wait: 1,
176
- retry_after: 30,
177
- retry_limit: 3, // Max attempts
178
- redirect_limit: 10
179
- },
180
- use: {
181
- http_version: 2,
182
- accept_encoding: true
183
- },
184
- dns: {
185
- family: 4 // ipVersion
186
- },
187
- agent: { keep_alive: true }
188
- });
189
- request.init({ ipVersion: 6 });
190
-
191
- const options = {
192
- format: "yaml",
193
- httpVersion: 1,
194
- silent: true,
195
- headers: { "x-goog-user-project": "project-1" }
196
- };
197
- instance.get("http://hostname/path/config.yml", options).then(data => {
198
- console.log(data.property);
199
- });
200
- ```
201
-
202
- ## References
203
-
204
- - https://www.unpkg.com/@e-mc/types@0.9.8/lib/http.d.ts
205
- - https://www.unpkg.com/@e-mc/types@0.9.8/lib/request.d.ts
206
- - https://www.unpkg.com/@e-mc/types@0.9.8/lib/settings.d.ts
207
-
208
- * https://www.npmjs.com/package/@types/node
209
-
210
- ## LICENSE
211
-
1
+ # @e-mc/request
2
+
3
+ * NodeJS 16
4
+ * ES2020
5
+
6
+ ## General Usage
7
+
8
+ * [Read the Docs](https://e-mc.readthedocs.io)
9
+
10
+ ## Interface
11
+
12
+ * [View Source](https://www.unpkg.com/@e-mc/types@0.9.10/lib/index.d.ts)
13
+
14
+ ```typescript
15
+ import type { IModule, ModuleConstructor } from "./index";
16
+ import type { HttpAgentSettings, HttpProtocolVersion, HttpRequestClient, InternetProtocolVersion } from "./http";
17
+ import type { ApplyOptions, Aria2Options, FormDataPart, HeadersOnCallback, HostConfig, OpenOptions, PostOptions, ProxySettings, ReadExpectType, RequestInit, StatusOnCallback } from "./request";
18
+ import type { DnsLookupSettings, RequestModule, RequestSettings } from "./settings";
19
+
20
+ import type { ClientRequest, OutgoingHttpHeaders } from "http";
21
+ import type { LookupFunction } from "net";
22
+ import type { Writable } from "stream";
23
+
24
+ interface IRequest extends IModule {
25
+ module: RequestModule;
26
+ startTime: number;
27
+ acceptEncoding: boolean;
28
+ keepAlive: boolean | null;
29
+ readTimeout: number;
30
+ readExpect: ReadExpectType;
31
+ proxy: ProxySettings | null;
32
+ init(config?: RequestInit): this;
33
+ apply(options: ApplyOptions): this;
34
+ addDns(hostname: string, address: string, timeout: number): void;
35
+ addDns(hostname: string, address: string, family?: string, timeout?: number): void;
36
+ lookupDns(hostname: string): LookupFunction;
37
+ proxyOf(uri: string, localhost?: boolean): ProxySettings | undefined;
38
+ statusOn(name: number | number[], callback: StatusOnCallback): void;
39
+ statusOn(name: number | number[], globUrl: string, callback: StatusOnCallback): void;
40
+ headersOn(name: string | string[], callback: HeadersOnCallback): void;
41
+ headersOn(name: string | string[], globUrl: string, callback: HeadersOnCallback): void;
42
+ headersOf(uri: string): OutgoingHttpHeaders | undefined;
43
+ aria2c(uri: string | URL, pathname: string): Promise<string[]>;
44
+ aria2c(uri: string | URL, options?: Aria2Options): Promise<string[]>;
45
+ json(uri: string | URL, options?: OpenOptions): Promise<object | null>;
46
+ pipe(uri: string | URL, to: Writable, options?: OpenOptions): Promise<null>;
47
+ opts(url: string | URL, options?: OpenOptions): HostConfig;
48
+ open(uri: string | URL, options: OpenOptions): HttpRequestClient;
49
+ head(uri: string | URL, options?: OpenOptions): ClientRequest;
50
+ post(uri: string | URL, data: unknown, contentType: string): Promise<Buffer | string | null>;
51
+ post(uri: string | URL, parts: FormDataPart[]): Promise<Buffer | string | null>;
52
+ post(uri: string | URL, form: Record<string, unknown>, parts: FormDataPart[]): Promise<Buffer | string | null>;
53
+ post(uri: string | URL, data: unknown, options: PostOptions): Promise<Buffer | string | null>;
54
+ post(uri: string | URL, data: unknown, contentType?: string, options?: PostOptions): Promise<Buffer | string | null>;
55
+ get(uri: string | URL, options?: OpenOptions): Promise<Buffer | object | string | null>;
56
+ detach(singleton?: boolean): void;
57
+ reset(): void;
58
+ close(): void;
59
+ set agentTimeout(value);
60
+ get agentTimeout(): number;
61
+ set httpVersion(value);
62
+ get httpVersion(): HttpProtocolVersion | null;
63
+ set ipVersion(value);
64
+ get ipVersion(): InternetProtocolVersion;
65
+ get settings(): RequestSettings;
66
+ }
67
+
68
+ interface RequestConstructor extends ModuleConstructor {
69
+ readCACert(value: string, cache?: boolean): string;
70
+ readTLSKey(value: string, cache?: boolean): string;
71
+ readTLSCert(value: string, cache?: boolean): string;
72
+ isCert(value: string): boolean;
73
+ fromURL(url: URL, value: string): string;
74
+ fromStatusCode(value: number | string): string;
75
+ defineHttpAgent(options: HttpAgentSettings): void;
76
+ defineDnsLookup(options: DnsLookupSettings, clear?: boolean): void;
77
+ getAria2Path(): string;
78
+ readonly prototype: IRequest;
79
+ new(module?: RequestModule): IRequest;
80
+ }
81
+ ```
82
+
83
+ ## Settings
84
+
85
+ ```typescript
86
+ import type { PermittedDirectories } from "./core";
87
+ import type { SecureConfig } from "./http";
88
+ import type { PurgeComponent } from "./settings";
89
+
90
+ import type { LookupAddress } from "dns";
91
+ import type { OutgoingHttpHeaders } from "http";
92
+
93
+ interface RequestModule {
94
+ handler: "@e-mc/request";
95
+ timeout?: number | string;
96
+ read_timeout?: number | string;
97
+ agent?: {
98
+ keep_alive?: boolean;
99
+ timeout?: number | string;
100
+ };
101
+ connect?: {
102
+ timeout?: number | string;
103
+ retry_wait?: number | string;
104
+ retry_after?: number | string;
105
+ retry_limit?: number;
106
+ redirect_limit?: number;
107
+ };
108
+ dns?: {
109
+ family?: number;
110
+ expires?: number | string;
111
+ resolve?: Record<string, Partial<LookupAddress>>;
112
+ };
113
+ use?: {
114
+ http_version?: 1 | 2;
115
+ accept_encoding?: boolean;
116
+ };
117
+ proxy?: {
118
+ address?: string;
119
+ port?: number;
120
+ username?: string;
121
+ password?: string;
122
+ include?: string[];
123
+ exclude?: string[];
124
+ keep_alive?: boolean;
125
+ };
126
+ headers: Record<string, OutgoingHttpHeaders>;
127
+ certs?: Record<string, SecureConfig<string | string[]>>;
128
+ localhost?: string[];
129
+ protocol?: {
130
+ "http/1.1"?: string[];
131
+ h2c?: string[];
132
+ h2?: string[];
133
+ };
134
+ post_limit?: number | string;
135
+ settings?: {
136
+ broadcast_id?: string | string[];
137
+ time_format?: "readable" | "relative" | "none";
138
+ purge?: PurgeComponent;
139
+ }
140
+ }
141
+
142
+ interface DownloadModule {
143
+ expires?: number | string;
144
+ aria2?: {
145
+ bin?: string | false;
146
+ exec?: {
147
+ uid?: number;
148
+ gid?: number;
149
+ };
150
+ update_status?: number | { interval?: number; broadcast_only?: boolean };
151
+ max_concurrent_downloads?: number;
152
+ max_connection_per_server?: number;
153
+ bt_stop_timeout?: number;
154
+ bt_tracker_connect_timeout?: number;
155
+ bt_tracker_timeout?: number;
156
+ min_split_size?: string;
157
+ disk_cache?: number | string;
158
+ lowest_speed_limit?: number | string;
159
+ always_resume?: boolean;
160
+ file_allocation?: "none" | "prealloc" | "trunc" | "falloc";
161
+ conf_path?: string;
162
+ };
163
+ }
164
+ ```
165
+
166
+ ### Example usage
167
+
168
+ ```javascript
169
+ const Request = require("@e-mc/request");
170
+
171
+ const instance = new Request({
172
+ read_timeout: 30,
173
+ connect: {
174
+ timeout: 20, // Seconds
175
+ retry_wait: 1,
176
+ retry_after: 30,
177
+ retry_limit: 3, // Max attempts
178
+ redirect_limit: 10
179
+ },
180
+ use: {
181
+ http_version: 2,
182
+ accept_encoding: true
183
+ },
184
+ dns: {
185
+ family: 4 // ipVersion
186
+ },
187
+ agent: { keep_alive: true }
188
+ });
189
+ request.init({ ipVersion: 6 });
190
+
191
+ const options = {
192
+ format: "yaml",
193
+ httpVersion: 1,
194
+ silent: true,
195
+ headers: { "x-goog-user-project": "project-1" }
196
+ };
197
+ instance.get("http://hostname/path/config.yml", options).then(data => {
198
+ console.log(data.property);
199
+ });
200
+ ```
201
+
202
+ ## References
203
+
204
+ - https://www.unpkg.com/@e-mc/types@0.9.10/lib/http.d.ts
205
+ - https://www.unpkg.com/@e-mc/types@0.9.10/lib/request.d.ts
206
+ - https://www.unpkg.com/@e-mc/types@0.9.10/lib/settings.d.ts
207
+
208
+ * https://www.npmjs.com/package/@types/node
209
+
210
+ ## LICENSE
211
+
212
212
  BSD 3-Clause
@@ -97,7 +97,7 @@ class HttpHost {
97
97
  case 1:
98
98
  return status;
99
99
  default:
100
- return this._tlsConnect || (this._tlsConnect = new Promise(resolve => {
100
+ return this._tlsConnect ||= new Promise(resolve => {
101
101
  const alpn = 'h' + version;
102
102
  const socket = tls.connect(+this.port, this.hostname, { ALPNProtocols: [alpn], requestCert: true, rejectUnauthorized: false }, () => {
103
103
  this._tlsConnect = null;
@@ -124,7 +124,7 @@ class HttpHost {
124
124
  resolve(data[3] = 0);
125
125
  })
126
126
  .end();
127
- }));
127
+ });
128
128
  }
129
129
  }
130
130
  return 1;
package/index.js CHANGED
@@ -92,7 +92,7 @@ function getBaseHeaders(uri, headers) {
92
92
  let result;
93
93
  for (const pathname in headers) {
94
94
  if (pathname === uri || uri.startsWith(pathname + '/')) {
95
- (result || (result = [])).push([pathname, headers[pathname]]);
95
+ (result ||= []).push([pathname, headers[pathname]]);
96
96
  }
97
97
  }
98
98
  if (result) {
@@ -103,7 +103,7 @@ function getBaseHeaders(uri, headers) {
103
103
  }
104
104
  }
105
105
  function setDnsCache(hostname, value, expires) {
106
- expires ?? (expires = DNS.EXPIRES);
106
+ expires ??= DNS.EXPIRES;
107
107
  if (expires > 0 && !DNS.CACHE[hostname]) {
108
108
  DNS.CACHE[hostname] = value;
109
109
  if (expires !== Infinity) {
@@ -768,7 +768,6 @@ class Request extends module_1 {
768
768
  const output = [];
769
769
  let count = 0;
770
770
  this[kConnectHttp].forEach((protocol, index) => {
771
- var _o;
772
771
  const title = 'HTTP' + (index + 1);
773
772
  for (const origin in protocol) {
774
773
  const value = protocol[origin];
@@ -778,7 +777,7 @@ class Request extends module_1 {
778
777
  if (item[1] === title && Array.isArray(item[2]) && item[2][0].startsWith(origin)) {
779
778
  item[1] = '';
780
779
  item[2][0] = item[2][0].substring(origin.length);
781
- (_o = item[4]).titleBgColor && (_o.titleBgColor = undefined);
780
+ item[4].titleBgColor &&= undefined;
782
781
  item[4].titleJustify = 'right';
783
782
  args.push(item);
784
783
  log.splice(i--, 1);
@@ -839,7 +838,7 @@ class Request extends module_1 {
839
838
  if (config) {
840
839
  const { headers, httpVersion, ipVersion, readTimeout } = config;
841
840
  if ((0, types_1.isObject)(headers)) {
842
- setOutgoingHeaders(this[kHeaders] || (this[kHeaders] = {}), headers);
841
+ setOutgoingHeaders(this[kHeaders] ||= {}, headers);
843
842
  }
844
843
  if (httpVersion !== undefined) {
845
844
  this.httpVersion = httpVersion;
@@ -929,7 +928,6 @@ class Request extends module_1 {
929
928
  setDnsCache(hostname, this[kConnectDns][hostname] = [{ address, family }], timeout);
930
929
  }
931
930
  lookupDns(hostname) {
932
- var _o;
933
931
  const resolved = this[kConnectDns][hostname] || DNS.CACHE[hostname];
934
932
  if (resolved) {
935
933
  return (...args) => {
@@ -941,7 +939,7 @@ class Request extends module_1 {
941
939
  }
942
940
  };
943
941
  }
944
- const pending = (_o = this[kPendingDns])[hostname] || (_o[hostname] = []);
942
+ const pending = this[kPendingDns][hostname] ||= [];
945
943
  return (value, options, callback) => {
946
944
  if (pending.push(callback) === 1) {
947
945
  const configure = (family) => family === 0 ? options : { family, hints: family === 6 ? dns.V4MAPPED : 0 };
@@ -1019,7 +1017,7 @@ class Request extends module_1 {
1019
1017
  globUrl = '*';
1020
1018
  }
1021
1019
  if ((0, types_1.isString)(globUrl) && typeof callback === 'function') {
1022
- const on = this[kStatusOn] || (this[kStatusOn] = new Map());
1020
+ const on = this[kStatusOn] ||= new Map();
1023
1021
  for (const item of !Array.isArray(code) ? [code] : code) {
1024
1022
  let status = on.get(item);
1025
1023
  if (!status) {
@@ -1035,7 +1033,7 @@ class Request extends module_1 {
1035
1033
  globUrl = '*';
1036
1034
  }
1037
1035
  if ((0, types_1.isString)(globUrl) && typeof callback === 'function') {
1038
- const on = this[kHeadersOn] || (this[kHeadersOn] = new Map());
1036
+ const on = this[kHeadersOn] ||= new Map();
1039
1037
  for (const item of !Array.isArray(name) ? [name] : name) {
1040
1038
  let headers = on.get(item);
1041
1039
  if (!headers) {
@@ -1129,7 +1127,7 @@ class Request extends module_1 {
1129
1127
  if (!module_1.createDir(pathname)) {
1130
1128
  return Promise.reject((0, types_1.errorMessage)("aria2", "Path is not a directory", pathname));
1131
1129
  }
1132
- silent ?? (silent = this[kSingleton]);
1130
+ silent ??= this[kSingleton];
1133
1131
  return new Promise((resolve, reject) => {
1134
1132
  let protocol, origin, username, password;
1135
1133
  if (uri instanceof URL) {
@@ -1333,7 +1331,7 @@ class Request extends module_1 {
1333
1331
  if (match[3] === '100') {
1334
1332
  result.push(file);
1335
1333
  }
1336
- messageUnit || (messageUnit = match[2]);
1334
+ messageUnit ||= match[2];
1337
1335
  break;
1338
1336
  case 'INPR':
1339
1337
  if (ARIA2.ALWAYS_RESUME) {
@@ -1407,21 +1405,19 @@ class Request extends module_1 {
1407
1405
  return this.get(uri, options);
1408
1406
  }
1409
1407
  opts(url, options) {
1410
- var _o, _p, _q, _r;
1411
1408
  if (typeof url === 'string') {
1412
1409
  url = new URL(url);
1413
1410
  }
1414
1411
  let host;
1415
1412
  if (this.host) {
1416
- host = (_o = HTTP.HOST)[_p = url.origin] || (_o[_p] = new host_1(url, HTTP.VERSION));
1413
+ host = HTTP.HOST[url.origin] ||= new host_1(url, HTTP.VERSION);
1417
1414
  }
1418
1415
  else {
1419
- host = (_q = this[kHostInfo])[_r = url.origin] || (_q[_r] = new host_1(url, this.httpVersion || 1));
1416
+ host = this[kHostInfo][url.origin] ||= new host_1(url, this.httpVersion || 1);
1420
1417
  }
1421
1418
  return { ...options, host, url };
1422
1419
  }
1423
1420
  open(uri, options) {
1424
- var _o, _p;
1425
1421
  let { host, url, httpVersion, method = 'GET', search, encoding, format, headers, postData, keepAlive, agentTimeout, socketPath, timeout = this._config.connectTimeout, outStream } = options;
1426
1422
  const getting = method === 'GET';
1427
1423
  const posting = method === 'POST';
@@ -1433,7 +1429,7 @@ class Request extends module_1 {
1433
1429
  if (format.includes('/')) {
1434
1430
  format = format.split('/').pop().split('-').pop();
1435
1431
  }
1436
- headers || (headers = {});
1432
+ headers ||= {};
1437
1433
  switch (format = format.trim().toLowerCase()) {
1438
1434
  case 'yaml':
1439
1435
  headers.accept = 'application/yaml, application/x-yaml, text/yaml, text/x-yaml';
@@ -1481,7 +1477,7 @@ class Request extends module_1 {
1481
1477
  const version = this.httpVersion;
1482
1478
  let request, ca, cert, key, minVersion, baseHeaders = this.headersOf(uri);
1483
1479
  if (getting && this.acceptEncoding && !localhost && !baseHeaders?.['accept-encoding']) {
1484
- (_o = (headers || (headers = {})))['accept-encoding'] || (_o['accept-encoding'] = 'gzip, deflate, br' + (LIB_ZSTD ? ', zstd' : ''));
1480
+ (headers ||= {})['accept-encoding'] ||= 'gzip, deflate, br' + (LIB_ZSTD ? ', zstd' : '');
1485
1481
  }
1486
1482
  if (secure) {
1487
1483
  const certs = this[kCerts]?.[0][origin] || this.host && TLS.TEXT[origin];
@@ -1490,7 +1486,7 @@ class Request extends module_1 {
1490
1486
  }
1491
1487
  }
1492
1488
  if (!proxy && httpVersion !== 1 && ((httpVersion || host.version) === 2 && version !== 1 || secure && version === 2 && host.failed(2, true) === 0)) {
1493
- request = ((_p = this[kSession][0])[origin] || (_p[origin] = http2.connect(origin, { lookup: this.lookupDns(hostname), ca, cert, key, minVersion, settings: localhost ? { maxFrameSize: 16777215, enablePush: false } : { enablePush: false } }))).request({ ...baseHeaders, ...host_1.getBasicAuth(url), ...headers, ':path': pathname, ':method': method });
1489
+ request = (this[kSession][0][origin] ||= http2.connect(origin, { lookup: this.lookupDns(hostname), ca, cert, key, minVersion, settings: localhost ? { maxFrameSize: 16777215, enablePush: false } : { enablePush: false } })).request({ ...baseHeaders, ...host_1.getBasicAuth(url), ...headers, ':path': pathname, ':method': method });
1494
1490
  if (getting) {
1495
1491
  const listenerMap = {};
1496
1492
  const onEvent = request.on.bind(request);
@@ -1528,7 +1524,6 @@ class Request extends module_1 {
1528
1524
  this.matchHeaders(url, response, request, options);
1529
1525
  });
1530
1526
  request.on = function (event, listener) {
1531
- var _o;
1532
1527
  switch (event) {
1533
1528
  case 'data':
1534
1529
  case 'error':
@@ -1538,7 +1533,7 @@ class Request extends module_1 {
1538
1533
  break;
1539
1534
  }
1540
1535
  if (!connected) {
1541
- (listenerMap[_o = event + '-on'] || (listenerMap[_o] = [])).push(listener);
1536
+ (listenerMap[event + '-on'] ||= []).push(listener);
1542
1537
  }
1543
1538
  default:
1544
1539
  onEvent(event, listener);
@@ -1547,7 +1542,6 @@ class Request extends module_1 {
1547
1542
  return this;
1548
1543
  };
1549
1544
  request.once = function (event, listener) {
1550
- var _o;
1551
1545
  switch (event) {
1552
1546
  case 'data':
1553
1547
  case 'error':
@@ -1557,7 +1551,7 @@ class Request extends module_1 {
1557
1551
  break;
1558
1552
  }
1559
1553
  if (!connected) {
1560
- (listenerMap[_o = event + '-once'] || (listenerMap[_o] = [])).push(listener);
1554
+ (listenerMap[event + '-once'] ||= []).push(listener);
1561
1555
  }
1562
1556
  default:
1563
1557
  onceEvent(event, listener);
@@ -1574,6 +1568,8 @@ class Request extends module_1 {
1574
1568
  if (proxy) {
1575
1569
  const pkg = secure ? 'https-proxy-agent' : 'http-proxy-agent';
1576
1570
  try {
1571
+ keepAlive ??= proxy.keepAlive;
1572
+ agentTimeout ??= proxy.agentTimeout;
1577
1573
  const proxyHeaders = this[kHeaders] && getBaseHeaders(proxy.host.href, this[kHeaders]) || getBaseHeaders(proxy.host.href, HTTP.HEADERS);
1578
1574
  agent = require(pkg)(proxy.host, typeof keepAlive === 'boolean' || agentTimeout > 0 || proxyHeaders ? { keepAlive: keepAlive ?? true, timeout: agentTimeout, headers: proxyHeaders } : undefined);
1579
1575
  if (proxyHeaders) {
@@ -1592,7 +1588,7 @@ class Request extends module_1 {
1592
1588
  }
1593
1589
  else if (agentTimeout !== 0) {
1594
1590
  keepAlive = this.keepAlive || false;
1595
- agentTimeout ?? (agentTimeout = this.agentTimeout);
1591
+ agentTimeout ??= this.agentTimeout;
1596
1592
  if (keepAlive || agentTimeout > 0) {
1597
1593
  agent = new (secure ? https.Agent : http.Agent)({ keepAlive, timeout: agentTimeout });
1598
1594
  }
@@ -1722,16 +1718,16 @@ class Request extends module_1 {
1722
1718
  let formData;
1723
1719
  ({ formData, dataEncoding } = options);
1724
1720
  if (formData) {
1725
- (parts || (parts = [])).push(...Array.isArray(formData) ? formData : [formData]);
1721
+ (parts ||= []).push(...Array.isArray(formData) ? formData : [formData]);
1726
1722
  }
1727
1723
  else {
1728
- contentType || (contentType = options.contentType);
1724
+ contentType ||= options.contentType;
1729
1725
  }
1730
1726
  }
1731
1727
  else {
1732
1728
  options = {};
1733
1729
  }
1734
- const headers = options.headers || (options.headers = {});
1730
+ const headers = options.headers ||= {};
1735
1731
  for (const attr in headers) {
1736
1732
  const name = attr.toLowerCase();
1737
1733
  if (name === 'content-type' || name === 'content-length') {
@@ -1772,8 +1768,8 @@ class Request extends module_1 {
1772
1768
  if (target) {
1773
1769
  try {
1774
1770
  if (typeof target === 'string') {
1775
- filename || (filename = path.basename(target));
1776
- type || (type = module_1.lookupMime(filename));
1771
+ filename ||= path.basename(target);
1772
+ type ||= module_1.lookupMime(filename);
1777
1773
  target = fs.readFileSync(target);
1778
1774
  }
1779
1775
  else if (target instanceof stream.Readable) {
@@ -1790,7 +1786,7 @@ class Request extends module_1 {
1790
1786
  const result = await module_1.resolveMime(target);
1791
1787
  let ext;
1792
1788
  if (result) {
1793
- type || (type = result.mime);
1789
+ type ||= result.mime;
1794
1790
  ext = result.ext;
1795
1791
  }
1796
1792
  else if (type) {
@@ -1837,7 +1833,7 @@ class Request extends module_1 {
1837
1833
  }
1838
1834
  else {
1839
1835
  data = JSON.stringify(data);
1840
- contentType || (contentType = "application/json");
1836
+ contentType ||= "application/json";
1841
1837
  }
1842
1838
  headers['content-length'] = Buffer.byteLength(data, (0, types_1.getEncoding)(dataEncoding)).toString();
1843
1839
  }
@@ -2086,7 +2082,7 @@ class Request extends module_1 {
2086
2082
  const redirectResponse = (statusCode, location) => {
2087
2083
  abortResponse();
2088
2084
  if (location) {
2089
- if (request.follow_redirect === false) {
2085
+ if (request.followRedirect === false || request.follow_redirect === false) {
2090
2086
  throwError(formatStatus(statusCode, 'Follow redirect was disabled'));
2091
2087
  }
2092
2088
  else if (++redirects <= this._config.redirectLimit) {
@@ -2357,11 +2353,11 @@ class Request extends module_1 {
2357
2353
  set agentTimeout(value) {
2358
2354
  if (value > 0) {
2359
2355
  this[kAgentTimeout] = value;
2360
- this.keepAlive ?? (this.keepAlive = true);
2356
+ this.keepAlive ??= true;
2361
2357
  }
2362
2358
  else {
2363
2359
  this[kAgentTimeout] = 0;
2364
- this.keepAlive ?? (this.keepAlive = false);
2360
+ this.keepAlive ??= false;
2365
2361
  }
2366
2362
  }
2367
2363
  get agentTimeout() {
@@ -2391,8 +2387,7 @@ class Request extends module_1 {
2391
2387
  return this[kIpVersion];
2392
2388
  }
2393
2389
  get settings() {
2394
- var _o;
2395
- return (_o = this.module).settings || (_o.settings = {});
2390
+ return this.module.settings ||= {};
2396
2391
  }
2397
2392
  }
2398
2393
  _a = kSingleton, _b = kHttpVersion, _c = kHeaders, _d = kCerts, _e = kConnectDns, _f = kPendingDns, _g = kConnectHttp, _h = kStatusOn, _j = kHeadersOn, _k = kDownloading, _l = kHostInfo, _m = kSession;
package/package.json CHANGED
@@ -1,30 +1,30 @@
1
- {
2
- "name": "@e-mc/request",
3
- "version": "0.9.8",
4
- "description": "Request constructor for E-mc.",
5
- "main": "index.js",
6
- "types": "index.d.ts",
7
- "publishConfig": {
8
- "access": "public"
9
- },
10
- "repository": {
11
- "type": "git",
12
- "url": "git+https://github.com/anpham6/e-mc.git",
13
- "directory": "src/request"
14
- },
15
- "keywords": [
16
- "squared",
17
- "squared-functions"
18
- ],
19
- "author": "An Pham <anpham6@gmail.com>",
20
- "license": "BSD 3-Clause",
21
- "homepage": "https://github.com/anpham6/e-mc#readme",
22
- "dependencies": {
23
- "@e-mc/module": "0.9.8",
24
- "@e-mc/types": "0.9.8",
25
- "combined-stream": "^1.0.8",
26
- "js-yaml": "^4.1.0",
27
- "picomatch": "^4.0.2",
28
- "which": "^2.0.2"
29
- }
30
- }
1
+ {
2
+ "name": "@e-mc/request",
3
+ "version": "0.9.10",
4
+ "description": "Request constructor for E-mc.",
5
+ "main": "index.js",
6
+ "types": "index.d.ts",
7
+ "publishConfig": {
8
+ "access": "public"
9
+ },
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git+https://github.com/anpham6/e-mc.git",
13
+ "directory": "src/request"
14
+ },
15
+ "keywords": [
16
+ "squared",
17
+ "squared-functions"
18
+ ],
19
+ "author": "An Pham <anpham6@gmail.com>",
20
+ "license": "BSD 3-Clause",
21
+ "homepage": "https://github.com/anpham6/e-mc#readme",
22
+ "dependencies": {
23
+ "@e-mc/module": "0.9.10",
24
+ "@e-mc/types": "0.9.10",
25
+ "combined-stream": "^1.0.8",
26
+ "js-yaml": "^4.1.0",
27
+ "picomatch": "^4.0.2",
28
+ "which": "^2.0.2"
29
+ }
30
+ }
package/util.d.ts CHANGED
@@ -1,26 +1,26 @@
1
- import type { AuthValue } from '../types/lib/http';
2
-
3
- import type { IncomingHttpHeaders, OutgoingHttpHeaders } from 'http';
4
- import type { Readable, Writable } from 'stream';
5
-
6
- declare namespace util {
7
- function parseHeader<T = unknown>(headers: IncomingHttpHeaders, name: string): T | undefined;
8
- function normalizeHeaders(headers: OutgoingHttpHeaders): OutgoingHttpHeaders;
9
- function getBasicAuth(auth: AuthValue): string;
10
- function getBasicAuth(username: unknown, password?: unknown): string;
11
- function hasBasicAuth(value: string): boolean;
12
- function checkRetryable(err: unknown): boolean;
13
- function isRetryable(value: number, timeout?: boolean): boolean;
14
- function trimPath(value: string): string;
15
- function asInt(value: unknown): number;
16
- function asFloat(value: unknown): number;
17
- function fromSeconds(value: unknown): number;
18
- function getTransferRate(length: number, timeMs: number, unitSeparator?: string): string;
19
- function hasSameStat(src: string, dest: string, keepEmpty?: boolean): boolean;
20
- function hasSize(value: string, keepEmpty?: boolean): boolean;
21
- function getSize(value: string, diskUsed?: boolean): number;
22
- function byteLength(value: Bufferable, encoding?: BufferEncoding): number;
23
- function cleanupStream(target: Readable | Writable, pathname?: string): void;
24
- }
25
-
1
+ import type { AuthValue } from '../types/lib/http';
2
+
3
+ import type { IncomingHttpHeaders, OutgoingHttpHeaders } from 'http';
4
+ import type { Readable, Writable } from 'stream';
5
+
6
+ declare namespace util {
7
+ function parseHeader<T = unknown>(headers: IncomingHttpHeaders, name: string): T | undefined;
8
+ function normalizeHeaders(headers: OutgoingHttpHeaders): OutgoingHttpHeaders;
9
+ function getBasicAuth(auth: AuthValue): string;
10
+ function getBasicAuth(username: unknown, password?: unknown): string;
11
+ function hasBasicAuth(value: string): boolean;
12
+ function checkRetryable(err: unknown): boolean;
13
+ function isRetryable(value: number, timeout?: boolean): boolean;
14
+ function trimPath(value: string): string;
15
+ function asInt(value: unknown): number;
16
+ function asFloat(value: unknown): number;
17
+ function fromSeconds(value: unknown): number;
18
+ function getTransferRate(length: number, timeMs: number, unitSeparator?: string): string;
19
+ function hasSameStat(src: string, dest: string, keepEmpty?: boolean): boolean;
20
+ function hasSize(value: string, keepEmpty?: boolean): boolean;
21
+ function getSize(value: string, diskUsed?: boolean): number;
22
+ function byteLength(value: Bufferable, encoding?: BufferEncoding): number;
23
+ function cleanupStream(target: Readable | Writable, pathname?: string): void;
24
+ }
25
+
26
26
  export = util;