@e-mc/request 0.10.0 → 0.10.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
@@ -9,7 +9,7 @@
9
9
 
10
10
  ## Interface
11
11
 
12
- * [View Source](https://www.unpkg.com/@e-mc/types@0.10.0/lib/index.d.ts)
12
+ * [View Source](https://www.unpkg.com/@e-mc/types@0.10.1/lib/index.d.ts)
13
13
 
14
14
  ```typescript
15
15
  import type { IModule, ModuleConstructor } from "./index";
@@ -117,6 +117,7 @@ interface RequestModule {
117
117
  proxy?: {
118
118
  address?: string;
119
119
  port?: number;
120
+ origin?: string;
120
121
  username?: string;
121
122
  password?: string;
122
123
  include?: string[];
@@ -203,9 +204,9 @@ instance.get("http://hostname/path/config.yml", options).then(data => {
203
204
 
204
205
  ## References
205
206
 
206
- - https://www.unpkg.com/@e-mc/types@0.10.0/lib/http.d.ts
207
- - https://www.unpkg.com/@e-mc/types@0.10.0/lib/request.d.ts
208
- - https://www.unpkg.com/@e-mc/types@0.10.0/lib/settings.d.ts
207
+ - https://www.unpkg.com/@e-mc/types@0.10.1/lib/http.d.ts
208
+ - https://www.unpkg.com/@e-mc/types@0.10.1/lib/request.d.ts
209
+ - https://www.unpkg.com/@e-mc/types@0.10.1/lib/settings.d.ts
209
210
 
210
211
  * https://www.npmjs.com/package/@types/node
211
212
 
@@ -1,5 +1,5 @@
1
- import type { HttpHostConstructor } from '../../../types/lib/request';
2
-
3
- declare const HttpHost: HttpHostConstructor;
4
-
1
+ import type { HttpHostConstructor } from '../../../types/lib/request';
2
+
3
+ declare const HttpHost: HttpHostConstructor;
4
+
5
5
  export = HttpHost;
package/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import type { RequestConstructor } from '../types/lib';
2
-
3
- declare const Request: RequestConstructor;
4
-
1
+ import type { RequestConstructor } from '../types/lib';
2
+
3
+ declare const Request: RequestConstructor;
4
+
5
5
  export = Request;
package/index.js CHANGED
@@ -129,18 +129,17 @@ function setOutgoingHeaders(output, headers) {
129
129
  }
130
130
  function getProxySettings(request, agentTimeout) {
131
131
  const proxy = request.proxy;
132
- if (proxy?.address && proxy.port) {
133
- const port = (0, util_1.asInt)(proxy.port);
134
- const address = (!module_1.isURL(proxy.address) ? port === 80 ? 'http://' : 'https://' : '') + proxy.address;
132
+ if (proxy && (proxy.origin || proxy.address && proxy.port)) {
133
+ const origin = proxy.origin || ((!module_1.isURL(proxy.address) ? (0, util_1.asInt)(proxy.port) === 80 ? 'http://' : 'https://' : '') + proxy.address + ':' + proxy.port);
135
134
  try {
136
- let host = new URL(address + ':' + port), include, exclude;
135
+ let host = new URL(origin), include, exclude;
137
136
  if (proxy.username) {
138
137
  host = new URL(host.protocol + '//' + (0, util_1.getBasicAuth)(proxy.username, proxy.password) + host.host);
139
138
  }
140
139
  if ((0, types_1.isArray)(proxy.include)) {
141
140
  include = proxy.include;
142
141
  }
143
- else if ((0, types_1.isArray)(proxy.exclude)) {
142
+ if ((0, types_1.isArray)(proxy.exclude)) {
144
143
  exclude = proxy.exclude;
145
144
  }
146
145
  return { host, include, exclude, keepAlive: proxy.keep_alive, agentTimeout };
@@ -1566,7 +1565,7 @@ class Request extends module_1 {
1566
1565
  return proxy;
1567
1566
  }
1568
1567
  if ((0, types_1.isArray)(include) && !include.some(value => REGEXP_GLOBWITHIN.test(value) ? pm.isMatch(uri, value) : uri.startsWith(value)) || (0, types_1.isArray)(exclude) && exclude.some(value => REGEXP_GLOBWITHIN.test(value) ? pm.isMatch(uri, value) : uri.startsWith(value))) {
1569
- return undefined;
1568
+ return;
1570
1569
  }
1571
1570
  return proxy;
1572
1571
  }
@@ -2160,11 +2159,10 @@ class Request extends module_1 {
2160
2159
  if (proxy) {
2161
2160
  const pkg = secure ? 'https-proxy-agent' : 'http-proxy-agent';
2162
2161
  try {
2163
- const { protocol, hostname: proxyname, port, username, password, href } = proxy.host;
2164
2162
  keepAlive ?? (keepAlive = proxy.keepAlive);
2165
2163
  agentTimeout ?? (agentTimeout = proxy.agentTimeout);
2166
- agent = require(pkg)(keepAlive !== null || agentTimeout > 0 ? { protocol, hostname: proxyname, port, username, password, keepAlive: keepAlive ?? true, timeout: agentTimeout } : href);
2167
- const proxyHeaders = this[kHeaders] && getBaseHeaders(href, this[kHeaders]) || getBaseHeaders(href, HTTP.HEADERS);
2164
+ const proxyHeaders = this[kHeaders] && getBaseHeaders(proxy.host.href, this[kHeaders]) || getBaseHeaders(proxy.host.href, HTTP.HEADERS);
2165
+ agent = require(pkg)(proxy.host, typeof keepAlive === 'boolean' || agentTimeout > 0 || proxyHeaders ? { keepAlive: keepAlive ?? true, timeout: agentTimeout, headers: proxyHeaders } : undefined);
2168
2166
  if (proxyHeaders) {
2169
2167
  baseHeaders = { ...baseHeaders, ...proxyHeaders };
2170
2168
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@e-mc/request",
3
- "version": "0.10.0",
3
+ "version": "0.10.1",
4
4
  "description": "Request constructor for E-mc.",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -20,8 +20,8 @@
20
20
  "license": "BSD 3-Clause",
21
21
  "homepage": "https://github.com/anpham6/e-mc#readme",
22
22
  "dependencies": {
23
- "@e-mc/module": "0.10.0",
24
- "@e-mc/types": "0.10.0",
23
+ "@e-mc/module": "0.10.1",
24
+ "@e-mc/types": "0.10.1",
25
25
  "combined-stream": "^1.0.8",
26
26
  "js-yaml": "^4.1.0",
27
27
  "picomatch": "^4.0.2",
package/util.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import type { AuthValue } from '../types/lib/http';
2
+ import type { HttpProxySettings } from '../types/lib/settings';
2
3
 
3
4
  import type { IncomingHttpHeaders, OutgoingHttpHeaders } from 'http';
4
5
  import type { Readable, Writable } from 'stream';
@@ -11,6 +12,7 @@ declare namespace util {
11
12
  function hasBasicAuth(value: string): boolean;
12
13
  function checkRetryable(err: unknown): boolean;
13
14
  function isRetryable(value: number, timeout?: boolean): boolean;
15
+ function parseHttpProxy(value?: string): HttpProxySettings | undefined;
14
16
  function trimPath(value: string): string;
15
17
  function asInt(value: unknown): number;
16
18
  function asFloat(value: unknown): number;
package/util.js CHANGED
@@ -5,6 +5,7 @@ exports.getBasicAuth = getBasicAuth;
5
5
  exports.hasBasicAuth = hasBasicAuth;
6
6
  exports.checkRetryable = checkRetryable;
7
7
  exports.isRetryable = isRetryable;
8
+ exports.parseHttpProxy = parseHttpProxy;
8
9
  exports.trimPath = trimPath;
9
10
  exports.asInt = asInt;
10
11
  exports.asFloat = asFloat;
@@ -17,8 +18,10 @@ exports.byteLength = byteLength;
17
18
  exports.cleanupStream = cleanupStream;
18
19
  const path = require("path");
19
20
  const fs = require("fs");
21
+ const util = require("util");
20
22
  const module_1 = require("@e-mc/module");
21
23
  const types_1 = require("@e-mc/types");
24
+ const SUPPORTED_USVSTRING = (0, types_1.supported)(16, 8);
22
25
  const safeInt = (value) => value >= 0 ? Math.min(value, Number.MAX_SAFE_INTEGER) : NaN;
23
26
  function parseHeader(headers, name) {
24
27
  const value = headers[name];
@@ -65,7 +68,7 @@ function getBasicAuth(username, password) {
65
68
  if ((0, types_1.isObject)(username)) {
66
69
  ({ username, password } = username);
67
70
  }
68
- return (0, types_1.isString)(username) ? encodeURIComponent(username) + ((0, types_1.isString)(password) ? ':' + encodeURIComponent(password) : '') + '@' : '';
71
+ return (0, types_1.isString)(username) ? encodeURIComponent(SUPPORTED_USVSTRING ? util.toUSVString(username) : username) + ((0, types_1.isString)(password) ? ':' + encodeURIComponent(SUPPORTED_USVSTRING ? util.toUSVString(password) : password) : '') + '@' : '';
69
72
  }
70
73
  function hasBasicAuth(value) {
71
74
  try {
@@ -117,6 +120,38 @@ function isRetryable(value, timeout) {
117
120
  return false;
118
121
  }
119
122
  }
123
+ function parseHttpProxy(value) {
124
+ value || (value = process.env.HTTP_PROXY || process.env.HTTPS_PROXY);
125
+ if (value) {
126
+ try {
127
+ const url = new URL(value);
128
+ if (url.port) {
129
+ let exclude;
130
+ if (process.env.NO_PROXY) {
131
+ exclude = [];
132
+ for (const item of process.env.NO_PROXY.trim().split(/\s*,\s*/)) {
133
+ if (module_1.isURL(item)) {
134
+ exclude.push(item);
135
+ }
136
+ else {
137
+ exclude.push(`https://${item}`, `http://${item}`);
138
+ }
139
+ }
140
+ }
141
+ return {
142
+ address: url.protocol + '//' + url.hostname,
143
+ port: url.port,
144
+ origin: url.origin,
145
+ username: url.username ? decodeURIComponent(url.username) : '',
146
+ password: url.password ? decodeURIComponent(url.password) : '',
147
+ exclude
148
+ };
149
+ }
150
+ }
151
+ catch {
152
+ }
153
+ }
154
+ }
120
155
  function trimPath(value) {
121
156
  const length = value.length - 1;
122
157
  return value[length] === '/' ? value.substring(0, length) : value;