@e-mc/request 0.11.8 → 0.12.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.
package/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # @e-mc/request
2
2
 
3
- * NodeJS 16 LTS
4
- * ES2021
3
+ * NodeJS 18
4
+ * ES2022
5
5
 
6
6
  ## General Usage
7
7
 
@@ -9,12 +9,12 @@
9
9
 
10
10
  ## Interface
11
11
 
12
- * [View Source](https://www.unpkg.com/@e-mc/types@0.11.8/lib/index.d.ts)
12
+ * [View Source](https://www.unpkg.com/@e-mc/types@0.12.1/lib/index.d.ts)
13
13
 
14
14
  ```typescript
15
15
  import type { IModule, ModuleConstructor } from "./index";
16
16
  import type { HttpAgentSettings, HttpProtocolVersion, HttpRequestClient, InternetProtocolVersion } from "./http";
17
- import type { ApplyOptions, Aria2Options, FormDataPart, HeadersOnCallback, HostConfig, IHttpAdapter, OpenOptions, PostOptions, ProxySettings, PutOptions, ReadExpectType, RequestInit, StatusOnCallback } from "./request";
17
+ import type { ApplyOptions, Aria2Options, FormDataPart, HeadersOnCallback, HostConfig, IHttpAdapter, OpenOptions, PostOptions, ProxySettings, PutOptions, RcloneOptions, ReadExpectType, RequestInit, StatusOnCallback } from "./request";
18
18
  import type { DnsLookupSettings, RequestModule, RequestSettings } from "./settings";
19
19
 
20
20
  import type { ClientRequest, OutgoingHttpHeaders } from "node:http";
@@ -42,6 +42,8 @@ interface IRequest extends IModule {
42
42
  headersOf(uri: string): OutgoingHttpHeaders | undefined;
43
43
  aria2c(uri: string | URL, pathname: string): Promise<string[]>;
44
44
  aria2c(uri: string | URL, options?: Aria2Options): Promise<string[]>;
45
+ rclone(uri: string, pathname: string): Promise<string[]>;
46
+ rclone(uri: string, options?: RcloneOptions): Promise<string[]>;
45
47
  json(uri: string | URL, options?: OpenOptions): Promise<object | null>;
46
48
  pipe(uri: string | URL, to: Writable, options?: OpenOptions): Promise<null>;
47
49
  opts(url: string | URL, options?: OpenOptions): HostConfig;
@@ -72,9 +74,9 @@ interface RequestConstructor extends ModuleConstructor {
72
74
  readTLSKey(value: string, cache?: boolean): string;
73
75
  readTLSCert(value: string, cache?: boolean): string;
74
76
  isCert(value: string): boolean;
75
- /** @deprecated */
77
+ /** @deprecated @e-mc/request/util */
76
78
  fromURL(url: URL, value: string): string;
77
- /** @deprecated */
79
+ /** @deprecated @e-mc/request/util */
78
80
  fromStatusCode(value: number | string): string;
79
81
  defineHttpAgent(options: HttpAgentSettings): void;
80
82
  defineDnsLookup(options: DnsLookupSettings, clear?: boolean): void;
@@ -168,6 +170,48 @@ interface DownloadModule {
168
170
  file_allocation?: "none" | "prealloc" | "trunc" | "falloc";
169
171
  conf_path?: string;
170
172
  };
173
+ rclone?: {
174
+ bin?: string | false;
175
+ exec?: {
176
+ uid?: number;
177
+ gid?: number;
178
+ };
179
+ /* Copy */
180
+ check_first?: boolean;
181
+ checksum?: boolean;
182
+ cutoff_mode?: "HARD" | "SOFT" | "CAUTIOUS";
183
+ ignore_case_sync?: boolean;
184
+ ignore_checksum?: boolean;
185
+ ignore_existing?: boolean;
186
+ ignore_size?: boolean;
187
+ ignore_times?: boolean;
188
+ immutable?: boolean;
189
+ inplace?: boolean;
190
+ max_backlog?: number;
191
+ max_duration?: string;
192
+ max_transfer?: string;
193
+ metadata?: boolean;
194
+ modify_window?: string;
195
+ multi_thread_chunk_size?: string;
196
+ multi_thread_cutoff?: string;
197
+ multi_thread_streams?: number;
198
+ multi_thread_write_buffer_size?: string;
199
+ no_check_dest?: boolean;
200
+ no_traverse?: boolean;
201
+ no_update_dir_modtime?: boolean;
202
+ refresh_times?: boolean;
203
+ size_only?: boolean;
204
+ update?: boolean;
205
+ /* Listing */
206
+ fast_list?: boolean;
207
+ /* Networking */
208
+ bind?: string;
209
+ contimeout?: string;
210
+ disable_http2?: boolean;
211
+ timeout?: string;
212
+ /* Config */
213
+ config?: string;
214
+ };
171
215
  }
172
216
  ```
173
217
 
@@ -209,9 +253,9 @@ instance.get("http://hostname/path/config.yml", options).then(data => {
209
253
 
210
254
  ## References
211
255
 
212
- - https://www.unpkg.com/@e-mc/types@0.11.8/lib/http.d.ts
213
- - https://www.unpkg.com/@e-mc/types@0.11.8/lib/request.d.ts
214
- - https://www.unpkg.com/@e-mc/types@0.11.8/lib/settings.d.ts
256
+ - https://www.unpkg.com/@e-mc/types@0.12.1/lib/http.d.ts
257
+ - https://www.unpkg.com/@e-mc/types@0.12.1/lib/request.d.ts
258
+ - https://www.unpkg.com/@e-mc/types@0.12.1/lib/settings.d.ts
215
259
 
216
260
  * https://www.npmjs.com/package/@types/node
217
261
 
@@ -1,15 +1,15 @@
1
1
  "use strict";
2
- var _a;
3
2
  const fs = require("node:fs");
4
3
  const http2 = require("node:http2");
5
4
  const yaml = require("js-yaml");
6
5
  const types_1 = require("@e-mc/types");
7
6
  const module_1 = require("@e-mc/module");
8
7
  const util_1 = require("@e-mc/request/util");
9
- const kOutStream = Symbol('outStream');
10
- const kOptions = Symbol('options');
11
8
  let LOG_TIMEFORMAT = 'readable';
12
9
  class HttpAdapter {
10
+ instance;
11
+ state;
12
+ uri;
13
13
  static isUnsupported(value) {
14
14
  return value === 421 || value === 505;
15
15
  }
@@ -38,20 +38,26 @@ class HttpAdapter {
38
38
  break;
39
39
  }
40
40
  }
41
+ contentLength = 0;
42
+ retries = 0;
43
+ redirects = 0;
44
+ closed = false;
45
+ aborted = false;
46
+ timeout = null;
47
+ dataTime = null;
48
+ delayTime = undefined;
49
+ opts;
50
+ client;
51
+ resolve;
52
+ reject;
53
+ startTime;
54
+ #outStream = null;
55
+ #options;
41
56
  constructor(instance, state, uri, options) {
42
57
  this.instance = instance;
43
58
  this.state = state;
44
59
  this.uri = uri;
45
- this.contentLength = 0;
46
- this.retries = 0;
47
- this.redirects = 0;
48
- this.closed = false;
49
- this.aborted = false;
50
- this.timeout = null;
51
- this.dataTime = null;
52
- this.delayTime = undefined;
53
- this[_a] = null;
54
- this[kOptions] = options;
60
+ this.#options = options;
55
61
  this.startTime = state.log ? process.hrtime.bigint() : BigInt(0);
56
62
  this.setOpts();
57
63
  }
@@ -91,14 +97,14 @@ class HttpAdapter {
91
97
  this.retryResponse(statusCode, headers['retry-after']);
92
98
  }
93
99
  else if (HttpAdapter.isUnsupported(statusCode)) {
94
- this.retryDownload(true, this.formatNgFlags(http2.constants.NGHTTP2_PROTOCOL_ERROR, statusCode));
100
+ this.retryDownload(true, this.#formatNgFlags(http2.constants.NGHTTP2_PROTOCOL_ERROR, statusCode));
95
101
  }
96
102
  else {
97
103
  switch (flags) {
98
104
  case http2.constants.NGHTTP2_PROTOCOL_ERROR:
99
105
  case http2.constants.NGHTTP2_INADEQUATE_SECURITY:
100
106
  case http2.constants.NGHTTP2_HTTP_1_1_REQUIRED:
101
- this.retryDownload(true, this.formatNgFlags(flags, statusCode, headers.location));
107
+ this.retryDownload(true, this.#formatNgFlags(flags, statusCode, headers.location));
102
108
  break;
103
109
  default:
104
110
  this.retryDownload(false, this.formatStatus(statusCode));
@@ -186,7 +192,7 @@ class HttpAdapter {
186
192
  if (uri) {
187
193
  this.uri = uri;
188
194
  }
189
- this.opts = this.instance.opts(this.uri, this[kOptions]);
195
+ this.opts = this.instance.opts(this.uri, this.#options);
190
196
  }
191
197
  setWriteStream() {
192
198
  const pipeTo = this.pipeTo;
@@ -454,7 +460,7 @@ class HttpAdapter {
454
460
  return;
455
461
  }
456
462
  }
457
- this.sendWarning(this.formatRetry((0, util_1.fromStatusCode)(statusCode)));
463
+ this.sendWarning(this.#formatRetry((0, util_1.fromStatusCode)(statusCode)));
458
464
  if ((0, util_1.isRetryable)(statusCode, true)) {
459
465
  setImmediate(this.init.bind(this));
460
466
  }
@@ -468,7 +474,7 @@ class HttpAdapter {
468
474
  return (0, util_1.isRetryable)(value) && ++this.retries <= this.retryLimit;
469
475
  }
470
476
  retryTimeout() {
471
- this.sendWarning(this.formatRetry("HTTP connection timeout"));
477
+ this.sendWarning(this.#formatRetry("HTTP connection timeout"));
472
478
  this.init();
473
479
  }
474
480
  terminate(err) {
@@ -506,10 +512,10 @@ class HttpAdapter {
506
512
  this.outStream = null;
507
513
  }
508
514
  }
509
- formatNgFlags(value, statusCode, location) {
515
+ #formatNgFlags(value, statusCode, location) {
510
516
  return location ? `Using HTTP 1.1 for URL redirect (${location})` : this.formatStatus(statusCode, value ? 'NGHTTP2 Error ' + value : '');
511
517
  }
512
- formatRetry(message) {
518
+ #formatRetry(message) {
513
519
  return `${message} (${this.retries} / ${this.retryLimit})`;
514
520
  }
515
521
  set abortController(value) {
@@ -519,13 +525,13 @@ class HttpAdapter {
519
525
  return this.opts.outAbort || null;
520
526
  }
521
527
  set outStream(value) {
522
- this[kOutStream] = value;
528
+ this.#outStream = value;
523
529
  if (value) {
524
530
  this.opts.outStream = value;
525
531
  }
526
532
  }
527
533
  get outStream() {
528
- return this[kOutStream];
534
+ return this.#outStream;
529
535
  }
530
536
  get destroyed() {
531
537
  return this.client.destroyed || this.httpVersion === 2 && this.client.aborted;
@@ -555,5 +561,4 @@ class HttpAdapter {
555
561
  return this.state.config.redirectLimit;
556
562
  }
557
563
  }
558
- _a = kOutStream;
559
564
  module.exports = HttpAdapter;
@@ -1,18 +1,6 @@
1
1
  "use strict";
2
- var _a, _b, _c, _d;
3
2
  const tls = require("node:tls");
4
3
  const types_1 = require("@e-mc/types");
5
- const kProtocol = Symbol('protocol');
6
- const kSecure = Symbol('secure');
7
- const kHostname = Symbol('hostname');
8
- const kPort = Symbol('port');
9
- const kOrigin = Symbol('origin');
10
- const kStreamSize = Symbol('streamSize');
11
- const kVersion = Symbol('version');
12
- const kVersionData = Symbol('versionData');
13
- const kAltSvc = Symbol('altSvc');
14
- const kAltSvcQueue = Symbol('altSvcQueue');
15
- const kAltSvcError = Symbol('altSvcError');
16
4
  const HOST_LOCAL = new Set(['localhost']);
17
5
  const HOST_STREAM = new Map();
18
6
  const HOST_HTTP_1_1 = [];
@@ -73,44 +61,52 @@ class HttpHost {
73
61
  this.defineProtocolNegotiation(protocol);
74
62
  }
75
63
  }
64
+ localhost;
65
+ _tlsConnect = null;
66
+ #version;
67
+ #altSvc = [];
68
+ #altSvcQueue = [];
69
+ #altSvcError = [];
70
+ #protocol;
71
+ #secure;
72
+ #hostname;
73
+ #port;
74
+ #origin;
75
+ #streamSize;
76
+ #versionData = [
77
+ [0, 0, 0, 1, -1],
78
+ [0, 0, 0, -1, -1]
79
+ ];
76
80
  constructor(url, httpVersion = 1) {
77
- this[_a] = [
78
- [0, 0, 0, 1, -1],
79
- [0, 0, 0, -1, -1]
80
- ];
81
- this[_b] = [];
82
- this[_c] = [];
83
- this[_d] = [];
84
- this._tlsConnect = null;
85
81
  const { protocol, hostname } = url;
86
82
  const secure = protocol === 'https:';
87
83
  const port = url.port || (secure ? '443' : '80');
88
84
  let address = hostname + ':' + port;
89
- this[kProtocol] = protocol;
90
- this[kSecure] = secure;
91
- this[kHostname] = hostname;
92
- this[kPort] = port;
93
- this[kOrigin] = url.origin;
85
+ this.#protocol = protocol;
86
+ this.#secure = secure;
87
+ this.#hostname = hostname;
88
+ this.#port = port;
89
+ this.#origin = url.origin;
94
90
  this.localhost = HOST_LOCAL.has(hostname);
95
- this[kStreamSize] = HOST_STREAM.get(address) || (this.localhost ? 65536 : 4096);
91
+ this.#streamSize = HOST_STREAM.get(address) || (this.localhost ? 65536 : 4096);
96
92
  if (protocol !== 'file:' && !HOST_HTTP_1_1.includes(address = protocol + '//' + address)) {
97
93
  if (secure) {
98
- this[kVersion] = HOST_ALPN_H2.includes(address) ? 2 : httpVersion;
94
+ this.#version = HOST_ALPN_H2.includes(address) ? 2 : httpVersion;
99
95
  return;
100
96
  }
101
97
  if (HOST_ALPN_H2C.includes(address)) {
102
- this[kVersion] = 2;
98
+ this.#version = 2;
103
99
  return;
104
100
  }
105
101
  }
106
- this[kVersion] = 1;
107
- for (const version of this[kVersionData]) {
102
+ this.#version = 1;
103
+ for (const version of this.#versionData) {
108
104
  version[4] = 0;
109
105
  }
110
106
  }
111
107
  async hasProtocol(version) {
112
108
  if (version > 1) {
113
- const data = this[kVersionData][version - 1];
109
+ const data = this.#versionData[version - 1];
114
110
  if (!data || !this.secure) {
115
111
  return 0;
116
112
  }
@@ -153,11 +149,11 @@ class HttpHost {
153
149
  return 1;
154
150
  }
155
151
  success(version, status) {
156
- const data = this[kVersionData][version - 1];
152
+ const data = this.#versionData[version - 1];
157
153
  return status ? data[0] : ++data[0];
158
154
  }
159
155
  failed(version, status) {
160
- const data = this[kVersionData][version - 1];
156
+ const data = this.#versionData[version - 1];
161
157
  if (status) {
162
158
  return data[1];
163
159
  }
@@ -165,7 +161,7 @@ class HttpHost {
165
161
  return ++data[1];
166
162
  }
167
163
  error(version, status) {
168
- const data = this[kVersionData][version - 1];
164
+ const data = this.#versionData[version - 1];
169
165
  if (status) {
170
166
  return data[2];
171
167
  }
@@ -180,7 +176,7 @@ class HttpHost {
180
176
  this.clearAltSvc();
181
177
  return;
182
178
  }
183
- const data = this[kVersionData];
179
+ const data = this.#versionData;
184
180
  for (let i = data.length - 1; i >= version; --i) {
185
181
  const host = data[i];
186
182
  if (host[4] === 0) {
@@ -188,16 +184,16 @@ class HttpHost {
188
184
  }
189
185
  const increment = (flag) => {
190
186
  host[4] = flag;
191
- if (this[kVersion] < i + 1) {
192
- this[kVersion] = i + 1;
187
+ if (this.#version < i + 1) {
188
+ this.#version = i + 1;
193
189
  return true;
194
190
  }
195
191
  return false;
196
192
  };
197
193
  const addresses = [];
198
194
  const time = Date.now();
199
- const hostname = this[kHostname];
200
- const excluded = this[kAltSvcError];
195
+ const hostname = this.#hostname;
196
+ const excluded = this.#altSvcError;
201
197
  for (const match of altSvc.matchAll(new RegExp(`h${i + 1}(?:-\\d+)?="([^:]*):(\\d+)"([^,]*)`, 'g'))) {
202
198
  const port = match[2];
203
199
  if (!match[1] && port === this.port) {
@@ -219,7 +215,7 @@ class HttpHost {
219
215
  }
220
216
  if (addresses.length > 0) {
221
217
  this.closeAltSvc();
222
- this[kAltSvcQueue] = addresses.sort((a, b) => {
218
+ this.#altSvcQueue = addresses.sort((a, b) => {
223
219
  if (a[0] === hostname) {
224
220
  return -1;
225
221
  }
@@ -248,10 +244,10 @@ class HttpHost {
248
244
  }
249
245
  }
250
246
  didAltSvc(version) {
251
- return this[kVersionData][version][4] !== -1;
247
+ return this.#versionData[version][4] !== -1;
252
248
  }
253
249
  nextAltSvc() {
254
- const queue = this[kAltSvcQueue].shift();
250
+ const queue = this.#altSvcQueue.shift();
255
251
  if (queue) {
256
252
  const [hostname, port, expires, version] = queue;
257
253
  const timeout = expires - Date.now();
@@ -266,20 +262,20 @@ class HttpHost {
266
262
  }
267
263
  }, timeout);
268
264
  }
269
- this[kAltSvc] = [hostname, port, timer, version, this.protocol + '//' + hostname + ':' + port];
265
+ this.#altSvc = [hostname, port, timer, version, this.protocol + '//' + hostname + ':' + port];
270
266
  return true;
271
267
  }
272
268
  return false;
273
269
  }
274
270
  closeAltSvc(error) {
275
- const [hostname, port, timeout, version] = this[kAltSvc];
271
+ const [hostname, port, timeout, version] = this.#altSvc;
276
272
  if (hostname) {
277
- this[kAltSvc] = [];
273
+ this.#altSvc = [];
278
274
  if (timeout) {
279
275
  clearTimeout(timeout);
280
276
  }
281
277
  if (error) {
282
- this[kAltSvcError].push(`h${version}:${hostname}:${port}`);
278
+ this.#altSvcError.push(`h${version}:${hostname}:${port}`);
283
279
  return this.nextAltSvc();
284
280
  }
285
281
  }
@@ -287,7 +283,7 @@ class HttpHost {
287
283
  }
288
284
  clearAltSvc(version) {
289
285
  if (version) {
290
- if (this[kAltSvcQueue].length === 0) {
286
+ if (this.#altSvcQueue.length === 0) {
291
287
  this.flagAltSvc(version, 0);
292
288
  }
293
289
  else if (!this.closeAltSvc(true)) {
@@ -296,9 +292,9 @@ class HttpHost {
296
292
  }
297
293
  else {
298
294
  this.closeAltSvc();
299
- this[kAltSvcQueue] = [];
300
- this[kAltSvcError] = [];
301
- for (const item of this[kVersionData]) {
295
+ this.#altSvcQueue = [];
296
+ this.#altSvcError = [];
297
+ for (const item of this.#versionData) {
302
298
  if (item[3]) {
303
299
  item[4] = -1;
304
300
  }
@@ -306,11 +302,11 @@ class HttpHost {
306
302
  }
307
303
  }
308
304
  flagAltSvc(version, value) {
309
- this[kVersionData][version - 1][4] = value;
305
+ this.#versionData[version - 1][4] = value;
310
306
  }
311
307
  reset() {
312
308
  this.clearAltSvc();
313
- this[kVersionData].forEach((item, index) => {
309
+ this.#versionData.forEach((item, index) => {
314
310
  item[0] = 0;
315
311
  item[1] = 0;
316
312
  item[2] = 0;
@@ -320,37 +316,36 @@ class HttpHost {
320
316
  });
321
317
  }
322
318
  v2() {
323
- return this[kVersion] === 2;
319
+ return this.#version === 2;
324
320
  }
325
321
  set version(value) {
326
322
  switch (value) {
327
323
  case 1:
328
324
  case 2:
329
- this.flagAltSvc(this[kVersion] = value, -1);
325
+ this.flagAltSvc(this.#version = value, -1);
330
326
  break;
331
327
  }
332
328
  }
333
329
  get version() {
334
- return this[kVersion];
330
+ return this.#version;
335
331
  }
336
332
  get protocol() {
337
- return this[kProtocol];
333
+ return this.#protocol;
338
334
  }
339
335
  get secure() {
340
- return this[kSecure];
336
+ return this.#secure;
341
337
  }
342
338
  get hostname() {
343
- return this[kAltSvc][0] || this[kHostname];
339
+ return this.#altSvc[0] || this.#hostname;
344
340
  }
345
341
  get port() {
346
- return this[kAltSvc][1] || this[kPort];
342
+ return this.#altSvc[1] || this.#port;
347
343
  }
348
344
  get origin() {
349
- return this[kAltSvc][4] || this[kOrigin];
345
+ return this.#altSvc[4] || this.#origin;
350
346
  }
351
347
  get streamSize() {
352
- return this[kStreamSize];
348
+ return this.#streamSize;
353
349
  }
354
350
  }
355
- _a = kVersionData, _b = kAltSvc, _c = kAltSvcQueue, _d = kAltSvcError;
356
351
  module.exports = HttpHost;