@e-mc/request 0.14.0 → 0.14.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/README.md +4 -4
- package/index.js +27 -10
- package/package.json +3 -6
- package/util.d.ts +10 -3
- package/util.js +28 -2
package/README.md
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
|
|
10
10
|
## Interface
|
|
11
11
|
|
|
12
|
-
* [View Source](https://www.unpkg.com/@e-mc/types@0.14.
|
|
12
|
+
* [View Source](https://www.unpkg.com/@e-mc/types@0.14.2/lib/index.d.ts)
|
|
13
13
|
|
|
14
14
|
```typescript
|
|
15
15
|
import type { IModule, ModuleConstructor } from "./index";
|
|
@@ -253,9 +253,9 @@ instance.get("http://hostname/path/config.yml", options).then(data => {
|
|
|
253
253
|
|
|
254
254
|
## References
|
|
255
255
|
|
|
256
|
-
- https://www.unpkg.com/@e-mc/types@0.14.
|
|
257
|
-
- https://www.unpkg.com/@e-mc/types@0.14.
|
|
258
|
-
- https://www.unpkg.com/@e-mc/types@0.14.
|
|
256
|
+
- https://www.unpkg.com/@e-mc/types@0.14.2/lib/http.d.ts
|
|
257
|
+
- https://www.unpkg.com/@e-mc/types@0.14.2/lib/request.d.ts
|
|
258
|
+
- https://www.unpkg.com/@e-mc/types@0.14.2/lib/settings.d.ts
|
|
259
259
|
|
|
260
260
|
* https://www.npmjs.com/package/@types/node
|
|
261
261
|
|
package/index.js
CHANGED
|
@@ -18,7 +18,7 @@ const which = require('which');
|
|
|
18
18
|
const { fileURLToPath } = require('node:url');
|
|
19
19
|
const { createAbortError, errorMessage, errorValue, formatTime, getEncoding, getLogCurrent, isArray, isObject, isPlainObject, isString, parseExpires, sanitizeArgs, sanitizeCmd, supported } = require('@e-mc/types');
|
|
20
20
|
const Module = require('@e-mc/module');
|
|
21
|
-
const { asInt, fromSeconds, fromStatusCode, getBasicAuth, normalizeHeaders,
|
|
21
|
+
const { asInt, copyHeaders, fromSeconds, fromStatusCode, getBasicAuth, normalizeHeaders, trimPath } = require('@e-mc/request/util');
|
|
22
22
|
const HttpHost = require('@e-mc/request/http/host');
|
|
23
23
|
const HttpAdapter = require('@e-mc/request/http/adapter');
|
|
24
24
|
const kRequest = Symbol.for('request:constructor');
|
|
@@ -1408,6 +1408,7 @@ class Request extends Module {
|
|
|
1408
1408
|
silent ??= this.#singleton;
|
|
1409
1409
|
return new Promise((resolve, reject) => {
|
|
1410
1410
|
const init = [
|
|
1411
|
+
'--files-only=true',
|
|
1411
1412
|
'--links=false',
|
|
1412
1413
|
'--delete-excluded=false',
|
|
1413
1414
|
'--interactive=false',
|
|
@@ -1661,8 +1662,9 @@ class Request extends Module {
|
|
|
1661
1662
|
return { ...options, host, url };
|
|
1662
1663
|
}
|
|
1663
1664
|
open(uri, options) {
|
|
1664
|
-
let { host, url, httpVersion,
|
|
1665
|
-
|
|
1665
|
+
let { host, url, httpVersion, search, encoding, format, socketPath, expectContinue = false, timeout = this._config.connectTimeout, outStream } = options, headers = copyHeaders(options.headers), getting = false, posting = false;
|
|
1666
|
+
const method = (options.method?.toUpperCase() || 'GET');
|
|
1667
|
+
switch (method) {
|
|
1666
1668
|
case 'GET':
|
|
1667
1669
|
case 'DELETE':
|
|
1668
1670
|
getting = true;
|
|
@@ -1775,6 +1777,22 @@ class Request extends Module {
|
|
|
1775
1777
|
({ ca, cert, key, ciphers, version: minVersion } = certs);
|
|
1776
1778
|
}
|
|
1777
1779
|
}
|
|
1780
|
+
const basicAuth = HttpHost.getBasicAuth(url);
|
|
1781
|
+
if (baseHeaders || basicAuth) {
|
|
1782
|
+
headers = { ...baseHeaders, ...basicAuth, ...headers };
|
|
1783
|
+
}
|
|
1784
|
+
if (headers) {
|
|
1785
|
+
const cacheControl = headers['cache-control']?.toLowerCase();
|
|
1786
|
+
if (headers.authorization && !cacheControl?.includes('max-age')) {
|
|
1787
|
+
options.noCache ??= true;
|
|
1788
|
+
}
|
|
1789
|
+
else if (cacheControl?.includes('no-cache')) {
|
|
1790
|
+
options.noCache ??= 0;
|
|
1791
|
+
}
|
|
1792
|
+
if (cacheControl?.includes('no-store')) {
|
|
1793
|
+
options.noStore ??= true;
|
|
1794
|
+
}
|
|
1795
|
+
}
|
|
1778
1796
|
if (!proxy && httpVersion !== 1 && ((httpVersion || host.version) === 2 && version !== 1 || secure && version === 2 && host.failed(2, true) === 0)) {
|
|
1779
1797
|
let session = this.#session[0][origin], conn, maxConcurrentStreams = 0;
|
|
1780
1798
|
if (session) {
|
|
@@ -1791,7 +1809,10 @@ class Request extends Module {
|
|
|
1791
1809
|
else {
|
|
1792
1810
|
conn = session[0].at(-1);
|
|
1793
1811
|
}
|
|
1794
|
-
|
|
1812
|
+
headers ||= {};
|
|
1813
|
+
headers[':method'] = method;
|
|
1814
|
+
headers[':path'] = pathname;
|
|
1815
|
+
request = conn.request(headers);
|
|
1795
1816
|
if (getting) {
|
|
1796
1817
|
const listenerMap = {};
|
|
1797
1818
|
const onEvent = request.on.bind(request);
|
|
@@ -1917,10 +1938,6 @@ class Request extends Module {
|
|
|
1917
1938
|
}
|
|
1918
1939
|
}
|
|
1919
1940
|
}
|
|
1920
|
-
const basicAuth = HttpHost.getBasicAuth(url);
|
|
1921
|
-
if (baseHeaders || basicAuth) {
|
|
1922
|
-
headers = { ...baseHeaders, ...basicAuth, ...headers };
|
|
1923
|
-
}
|
|
1924
1941
|
request = (secure ? https : http).request(socketPath ? { socketPath, path: pathname } : {
|
|
1925
1942
|
protocol: host.protocol,
|
|
1926
1943
|
hostname,
|
|
@@ -2114,7 +2131,7 @@ class Request extends Module {
|
|
|
2114
2131
|
else {
|
|
2115
2132
|
options = {};
|
|
2116
2133
|
}
|
|
2117
|
-
const headers =
|
|
2134
|
+
const headers = copyHeaders(options.headers, 'content-type', 'content-length') || {};
|
|
2118
2135
|
if (!putting && (parts || contentType === "multipart/form-data" || contentType === 'form-data')) {
|
|
2119
2136
|
let valid = false;
|
|
2120
2137
|
if (isArray(parts)) {
|
|
@@ -2453,7 +2470,7 @@ class Request extends Module {
|
|
|
2453
2470
|
return [];
|
|
2454
2471
|
}).flat();
|
|
2455
2472
|
}
|
|
2456
|
-
return { pathname, headers:
|
|
2473
|
+
return { pathname, headers: copyHeaders(options.headers), binOpts };
|
|
2457
2474
|
}
|
|
2458
2475
|
mergeBinOpts(args, opts, binOpts, options) {
|
|
2459
2476
|
if (binOpts) {
|
package/package.json
CHANGED
|
@@ -1,12 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@e-mc/request",
|
|
3
|
-
"version": "0.14.
|
|
3
|
+
"version": "0.14.2",
|
|
4
4
|
"description": "Request constructor for E-mc.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "index.d.ts",
|
|
7
|
-
"publishConfig": {
|
|
8
|
-
"access": "public"
|
|
9
|
-
},
|
|
10
7
|
"repository": {
|
|
11
8
|
"type": "git",
|
|
12
9
|
"url": "git+https://github.com/anpham6/e-mc.git",
|
|
@@ -19,8 +16,8 @@
|
|
|
19
16
|
"license": "BSD-3-Clause",
|
|
20
17
|
"homepage": "https://github.com/anpham6/e-mc#readme",
|
|
21
18
|
"dependencies": {
|
|
22
|
-
"@e-mc/module": "0.14.
|
|
23
|
-
"@e-mc/types": "0.14.
|
|
19
|
+
"@e-mc/module": "0.14.2",
|
|
20
|
+
"@e-mc/types": "0.14.2",
|
|
24
21
|
"combined-stream": "^1.0.8",
|
|
25
22
|
"js-yaml": "^4.1.1",
|
|
26
23
|
"picomatch": "^4.0.4",
|
package/util.d.ts
CHANGED
|
@@ -4,10 +4,17 @@ import type { HttpProxySettings } from '@e-mc/types/lib/settings';
|
|
|
4
4
|
import type { IncomingHttpHeaders, OutgoingHttpHeaders } from 'node:http';
|
|
5
5
|
import type { Readable, Writable } from 'node:stream';
|
|
6
6
|
|
|
7
|
+
type OutgoingHeaders = OutgoingHttpHeaders | Headers;
|
|
8
|
+
|
|
7
9
|
declare namespace util {
|
|
8
|
-
function parseHeader
|
|
9
|
-
function
|
|
10
|
-
function
|
|
10
|
+
function parseHeader(headers: IncomingHttpHeaders | OutgoingHttpHeaders, name: "cache-control"): string[] | undefined;
|
|
11
|
+
function parseHeader(headers: IncomingHttpHeaders, name: "expires" | "date" | "age"): number | undefined;
|
|
12
|
+
function parseHeader(headers: IncomingHttpHeaders, name: "content-disposition"): string | undefined;
|
|
13
|
+
function parseHeader<T extends string | string[] | number>(headers: IncomingHttpHeaders, name: string): T | undefined;
|
|
14
|
+
/** @deprecated copyHeaders (rename) */
|
|
15
|
+
function parseOutgoingHeaders(headers: OutgoingHeaders | undefined, ...ignore: string[]): OutgoingHttpHeaders | undefined;
|
|
16
|
+
function copyHeaders(headers: OutgoingHeaders | undefined, ...ignore: string[]): OutgoingHttpHeaders | undefined;
|
|
17
|
+
function normalizeHeaders(headers: OutgoingHeaders): OutgoingHttpHeaders;
|
|
11
18
|
function getBasicAuth(auth: AuthValue): string;
|
|
12
19
|
function getBasicAuth(username: unknown, password?: unknown): string;
|
|
13
20
|
function hasBasicAuth(value: string): boolean;
|
package/util.js
CHANGED
|
@@ -17,7 +17,30 @@ function setHeader(result, key, value) {
|
|
|
17
17
|
const safeInt = (value) => value >= 0 ? Math.min(value, Number.MAX_SAFE_INTEGER) : NaN;
|
|
18
18
|
function parseHeader(headers, name) {
|
|
19
19
|
const value = headers[name];
|
|
20
|
+
if (!value) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
20
23
|
switch (name.toLowerCase()) {
|
|
24
|
+
case 'date':
|
|
25
|
+
case 'expires': {
|
|
26
|
+
const result = Date.parse(value);
|
|
27
|
+
if (!isNaN(result)) {
|
|
28
|
+
return result;
|
|
29
|
+
}
|
|
30
|
+
break;
|
|
31
|
+
}
|
|
32
|
+
case 'age': {
|
|
33
|
+
const result = parseInt(value);
|
|
34
|
+
if (!isNaN(result)) {
|
|
35
|
+
return result * 1000;
|
|
36
|
+
}
|
|
37
|
+
break;
|
|
38
|
+
}
|
|
39
|
+
case 'cache-control':
|
|
40
|
+
if (isString(value)) {
|
|
41
|
+
return value.toLowerCase().split(/\s*,\s*/);
|
|
42
|
+
}
|
|
43
|
+
break;
|
|
21
44
|
case 'content-disposition':
|
|
22
45
|
if (isString(value)) {
|
|
23
46
|
let result;
|
|
@@ -30,9 +53,11 @@ function parseHeader(headers, name) {
|
|
|
30
53
|
return result;
|
|
31
54
|
}
|
|
32
55
|
break;
|
|
56
|
+
default:
|
|
57
|
+
return value;
|
|
33
58
|
}
|
|
34
59
|
}
|
|
35
|
-
function
|
|
60
|
+
function copyHeaders(headers, ...ignore) {
|
|
36
61
|
if (!headers) {
|
|
37
62
|
return;
|
|
38
63
|
}
|
|
@@ -438,6 +463,7 @@ exports.asInt = asInt;
|
|
|
438
463
|
exports.byteLength = byteLength;
|
|
439
464
|
exports.checkRetryable = checkRetryable;
|
|
440
465
|
exports.cleanupStream = cleanupStream;
|
|
466
|
+
exports.copyHeaders = copyHeaders;
|
|
441
467
|
exports.fromSeconds = fromSeconds;
|
|
442
468
|
exports.fromStatusCode = fromStatusCode;
|
|
443
469
|
exports.fromURL = fromURL;
|
|
@@ -451,5 +477,5 @@ exports.isRetryable = isRetryable;
|
|
|
451
477
|
exports.normalizeHeaders = normalizeHeaders;
|
|
452
478
|
exports.parseHeader = parseHeader;
|
|
453
479
|
exports.parseHttpProxy = parseHttpProxy;
|
|
454
|
-
exports.parseOutgoingHeaders =
|
|
480
|
+
exports.parseOutgoingHeaders = copyHeaders;
|
|
455
481
|
exports.trimPath = trimPath;
|