@verdaccio/url 13.0.5 → 13.1.0
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/build/_virtual/_rolldown/runtime.js +23 -0
- package/build/index.d.ts +0 -1
- package/build/index.js +87 -110
- package/build/index.js.map +1 -1
- package/build/index.mjs +89 -0
- package/build/index.mjs.map +1 -0
- package/package.json +20 -7
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
//#region \0rolldown/runtime.js
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
10
|
+
key = keys[i];
|
|
11
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
12
|
+
get: ((k) => from[k]).bind(null, key),
|
|
13
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
19
|
+
value: mod,
|
|
20
|
+
enumerable: true
|
|
21
|
+
}) : target, mod));
|
|
22
|
+
//#endregion
|
|
23
|
+
exports.__toESM = __toESM;
|
package/build/index.d.ts
CHANGED
package/build/index.js
CHANGED
|
@@ -1,126 +1,103 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
value: true
|
|
1
|
+
Object.defineProperties(exports, {
|
|
2
|
+
__esModule: { value: true },
|
|
3
|
+
[Symbol.toStringTag]: { value: "Module" }
|
|
5
4
|
});
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
var
|
|
15
|
-
var
|
|
16
|
-
var _validator = _interopRequireDefault(require("validator"));
|
|
17
|
-
var _core = require("@verdaccio/core");
|
|
18
|
-
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
19
|
-
const debug = (0, _debug.default)('verdaccio:core:url');
|
|
20
|
-
const validProtocols = ['https', 'http'];
|
|
21
|
-
|
|
5
|
+
const require_runtime = require("./_virtual/_rolldown/runtime.js");
|
|
6
|
+
let debug = require("debug");
|
|
7
|
+
debug = require_runtime.__toESM(debug);
|
|
8
|
+
let node_url = require("node:url");
|
|
9
|
+
let validator = require("validator");
|
|
10
|
+
validator = require_runtime.__toESM(validator);
|
|
11
|
+
let _verdaccio_core = require("@verdaccio/core");
|
|
12
|
+
//#region src/index.ts
|
|
13
|
+
var debug$1 = (0, debug.default)("verdaccio:core:url");
|
|
14
|
+
var validProtocols = ["https", "http"];
|
|
22
15
|
/**
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
16
|
+
* Check if URI is starting with "http://", "https://" or "//"
|
|
17
|
+
* @param {string} uri
|
|
18
|
+
*/
|
|
26
19
|
function isURLhasValidProtocol(uri) {
|
|
27
|
-
|
|
20
|
+
return /^(https?:)?\/\//.test(uri);
|
|
28
21
|
}
|
|
29
|
-
function isHost(url =
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
});
|
|
22
|
+
function isHost(url = "", options = {}) {
|
|
23
|
+
return validator.default.isURL(url, {
|
|
24
|
+
require_host: true,
|
|
25
|
+
allow_trailing_dot: false,
|
|
26
|
+
require_valid_protocol: false,
|
|
27
|
+
require_port: false,
|
|
28
|
+
require_tld: false,
|
|
29
|
+
...options
|
|
30
|
+
});
|
|
39
31
|
}
|
|
40
|
-
|
|
41
32
|
/**
|
|
42
|
-
|
|
43
|
-
|
|
33
|
+
* Detect running protocol (http or https)
|
|
34
|
+
*/
|
|
44
35
|
function getWebProtocol(headerProtocol, protocol) {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
return validProtocols.includes(returnProtocol) ? returnProtocol : defaultProtocol;
|
|
36
|
+
let returnProtocol;
|
|
37
|
+
const [, defaultProtocol] = validProtocols;
|
|
38
|
+
if (typeof headerProtocol === "string" && headerProtocol !== "") {
|
|
39
|
+
debug$1("header protocol: %o", protocol);
|
|
40
|
+
const commaIndex = headerProtocol.indexOf(",");
|
|
41
|
+
returnProtocol = commaIndex > 0 ? headerProtocol.slice(0, commaIndex) : headerProtocol;
|
|
42
|
+
} else {
|
|
43
|
+
debug$1("req protocol: %o", headerProtocol);
|
|
44
|
+
returnProtocol = protocol;
|
|
45
|
+
}
|
|
46
|
+
return validProtocols.includes(returnProtocol) ? returnProtocol : defaultProtocol;
|
|
57
47
|
}
|
|
58
48
|
function wrapPrefix(prefix) {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
return `/${prefix}/`;
|
|
65
|
-
} else if (prefix.startsWith('/') && !prefix.endsWith('/')) {
|
|
66
|
-
return `${prefix}/`;
|
|
67
|
-
} else {
|
|
68
|
-
return prefix;
|
|
69
|
-
}
|
|
49
|
+
if (prefix === "" || typeof prefix === "undefined" || prefix === null) return "";
|
|
50
|
+
else if (!prefix.startsWith("/") && prefix.endsWith("/")) return `/${prefix}`;
|
|
51
|
+
else if (!prefix.startsWith("/") && !prefix.endsWith("/")) return `/${prefix}/`;
|
|
52
|
+
else if (prefix.startsWith("/") && !prefix.endsWith("/")) return `${prefix}/`;
|
|
53
|
+
else return prefix;
|
|
70
54
|
}
|
|
71
|
-
|
|
72
55
|
/**
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
function combineBaseUrl(protocol, host, prefix =
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
debug('combined url %o', result);
|
|
84
|
-
return result;
|
|
56
|
+
* Create base url for registry.
|
|
57
|
+
* @return {String} base registry url
|
|
58
|
+
*/
|
|
59
|
+
function combineBaseUrl(protocol, host, prefix = "") {
|
|
60
|
+
debug$1("combined protocol %o", protocol);
|
|
61
|
+
debug$1("combined host %o", host);
|
|
62
|
+
debug$1("combined prefix %o", wrapPrefix(prefix));
|
|
63
|
+
const result = new node_url.URL(wrapPrefix(prefix), `${protocol}://${host}`).href;
|
|
64
|
+
debug$1("combined url %o", result);
|
|
65
|
+
return result;
|
|
85
66
|
}
|
|
86
67
|
function validateURL(publicUrl) {
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
// TODO: add error logger here
|
|
95
|
-
return false;
|
|
96
|
-
}
|
|
68
|
+
try {
|
|
69
|
+
const parsed = new node_url.URL(publicUrl);
|
|
70
|
+
if (!validProtocols.includes(parsed.protocol.replace(":", ""))) throw Error("invalid protocol");
|
|
71
|
+
return true;
|
|
72
|
+
} catch {
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
97
75
|
}
|
|
98
|
-
function getPublicUrl(url_prefix =
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
// This really should never happen - only set-cookie is allowed to have
|
|
114
|
-
// multiple values.
|
|
115
|
-
throw new Error('invalid forwarded protocol header value. Reading header ' + protoHeader);
|
|
116
|
-
}
|
|
117
|
-
const protocol = getWebProtocol(forwardedProtocolHeaderValue, requestOptions.protocol);
|
|
118
|
-
const combinedUrl = combineBaseUrl(protocol, host, url_prefix);
|
|
119
|
-
debug('public url by request %o', combinedUrl);
|
|
120
|
-
return combinedUrl;
|
|
121
|
-
} else {
|
|
122
|
-
return '/';
|
|
123
|
-
}
|
|
76
|
+
function getPublicUrl(url_prefix = "", requestOptions) {
|
|
77
|
+
if (validateURL(process.env.VERDACCIO_PUBLIC_URL)) {
|
|
78
|
+
const envURL = new node_url.URL(wrapPrefix(url_prefix), process.env.VERDACCIO_PUBLIC_URL).href;
|
|
79
|
+
debug$1("public url by env %o", envURL);
|
|
80
|
+
return envURL;
|
|
81
|
+
} else if (requestOptions.headers["host"]) {
|
|
82
|
+
const host = requestOptions.headers["host"];
|
|
83
|
+
if (!isHost(host)) throw new Error("invalid host");
|
|
84
|
+
const protoHeader = process.env.VERDACCIO_FORWARDED_PROTO?.toLocaleLowerCase() ?? _verdaccio_core.HEADERS.FORWARDED_PROTO.toLowerCase();
|
|
85
|
+
const forwardedProtocolHeaderValue = requestOptions.headers[protoHeader];
|
|
86
|
+
if (Array.isArray(forwardedProtocolHeaderValue)) throw new Error("invalid forwarded protocol header value. Reading header " + protoHeader);
|
|
87
|
+
const combinedUrl = combineBaseUrl(getWebProtocol(forwardedProtocolHeaderValue, requestOptions.protocol), host, url_prefix);
|
|
88
|
+
debug$1("public url by request %o", combinedUrl);
|
|
89
|
+
return combinedUrl;
|
|
90
|
+
} else return "/";
|
|
124
91
|
}
|
|
125
|
-
|
|
92
|
+
var isURL = validator.default.isURL;
|
|
93
|
+
//#endregion
|
|
94
|
+
exports.combineBaseUrl = combineBaseUrl;
|
|
95
|
+
exports.getPublicUrl = getPublicUrl;
|
|
96
|
+
exports.getWebProtocol = getWebProtocol;
|
|
97
|
+
exports.isHost = isHost;
|
|
98
|
+
exports.isURL = isURL;
|
|
99
|
+
exports.isURLhasValidProtocol = isURLhasValidProtocol;
|
|
100
|
+
exports.validateURL = validateURL;
|
|
101
|
+
exports.wrapPrefix = wrapPrefix;
|
|
102
|
+
|
|
126
103
|
//# sourceMappingURL=index.js.map
|
package/build/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/index.ts"],"sourcesContent":["import buildDebug from 'debug';\nimport type { IncomingHttpHeaders } from 'node:http';\nimport { URL } from 'node:url';\nimport validator from 'validator';\n\nimport { HEADERS } from '@verdaccio/core';\n\nconst debug = buildDebug('verdaccio:core:url');\n\nconst validProtocols = ['https', 'http'];\n\n/**\n * Check if URI is starting with \"http://\", \"https://\" or \"//\"\n * @param {string} uri\n */\nexport function isURLhasValidProtocol(uri: string): boolean {\n return /^(https?:)?\\/\\//.test(uri);\n}\n\nexport function isHost(url: string = '', options = {}): boolean {\n return validator.isURL(url, {\n require_host: true,\n allow_trailing_dot: false,\n require_valid_protocol: false,\n // @ts-ignore\n require_port: false,\n require_tld: false,\n ...options,\n });\n}\n\n/**\n * Detect running protocol (http or https)\n */\nexport function getWebProtocol(headerProtocol: string | undefined, protocol: string): string {\n let returnProtocol;\n const [, defaultProtocol] = validProtocols;\n // HAProxy variant might return http,http with X-Forwarded-Proto\n if (typeof headerProtocol === 'string' && headerProtocol !== '') {\n debug('header protocol: %o', protocol);\n const commaIndex = headerProtocol.indexOf(',');\n returnProtocol = commaIndex > 0 ? headerProtocol.slice(0, commaIndex) : headerProtocol;\n } else {\n debug('req protocol: %o', headerProtocol);\n returnProtocol = protocol;\n }\n\n return validProtocols.includes(returnProtocol) ? returnProtocol : defaultProtocol;\n}\n\nexport function wrapPrefix(prefix: string | void): string {\n if (prefix === '' || typeof prefix === 'undefined' || prefix === null) {\n return '';\n } else if (!prefix.startsWith('/') && prefix.endsWith('/')) {\n return `/${prefix}`;\n } else if (!prefix.startsWith('/') && !prefix.endsWith('/')) {\n return `/${prefix}/`;\n } else if (prefix.startsWith('/') && !prefix.endsWith('/')) {\n return `${prefix}/`;\n } else {\n return prefix;\n }\n}\n\n/**\n * Create base url for registry.\n * @return {String} base registry url\n */\nexport function combineBaseUrl(protocol: string, host: string, prefix: string = ''): string {\n debug('combined protocol %o', protocol);\n debug('combined host %o', host);\n const newPrefix = wrapPrefix(prefix);\n debug('combined prefix %o', newPrefix);\n const groupedURI = new URL(wrapPrefix(prefix), `${protocol}://${host}`);\n const result = groupedURI.href;\n debug('combined url %o', result);\n return result;\n}\n\nexport function validateURL(publicUrl: string | void) {\n try {\n const parsed = new URL(publicUrl as string);\n if (!validProtocols.includes(parsed.protocol.replace(':', ''))) {\n throw Error('invalid protocol');\n }\n return true;\n } catch {\n // TODO: add error logger here\n return false;\n }\n}\n\nexport type RequestOptions = {\n /**\n * Request host (without port in Express 4).\n */\n host: string;\n /**\n * Request protocol, usually http or https.\n */\n protocol: string;\n /**\n * Request headers.\n */\n headers: IncomingHttpHeaders;\n /**\n * Request remote address. This is the IPv4 or IPv6 address of the client.\n */\n remoteAddress?: string;\n /**\n * Logged username the request, usually after token verification.\n */\n username?: string;\n};\n\nexport function getPublicUrl(url_prefix: string = '', requestOptions: RequestOptions): string {\n if (validateURL(process.env.VERDACCIO_PUBLIC_URL as string)) {\n const envURL = new URL(wrapPrefix(url_prefix), process.env.VERDACCIO_PUBLIC_URL as string).href;\n debug('public url by env %o', envURL);\n return envURL;\n } else if (requestOptions.headers['host']) {\n const host = requestOptions.headers['host'];\n if (!isHost(host)) {\n throw new Error('invalid host');\n }\n\n // 'X-Forwarded-Proto' is the default header\n const protoHeader: string =\n process.env.VERDACCIO_FORWARDED_PROTO?.toLocaleLowerCase() ??\n HEADERS.FORWARDED_PROTO.toLowerCase();\n const forwardedProtocolHeaderValue = requestOptions.headers[protoHeader];\n\n if (Array.isArray(forwardedProtocolHeaderValue)) {\n // This really should never happen - only set-cookie is allowed to have\n // multiple values.\n throw new Error('invalid forwarded protocol header value. Reading header ' + protoHeader);\n }\n\n const protocol = getWebProtocol(forwardedProtocolHeaderValue, requestOptions.protocol);\n const combinedUrl = combineBaseUrl(protocol, host, url_prefix);\n debug('public url by request %o', combinedUrl);\n return combinedUrl;\n } else {\n return '/';\n }\n}\n\nexport const isURL = validator.isURL;\n"],"mappings":";;;;;;;;;;;;AAOA,IAAM,WAAA,GAAA,MAAA,SAAmB,oBAAoB;AAE7C,IAAM,iBAAiB,CAAC,SAAS,MAAM;;;;;AAMvC,SAAgB,sBAAsB,KAAsB;CAC1D,OAAO,kBAAkB,KAAK,GAAG;AACnC;AAEA,SAAgB,OAAO,MAAc,IAAI,UAAU,CAAC,GAAY;CAC9D,OAAO,UAAA,QAAU,MAAM,KAAK;EAC1B,cAAc;EACd,oBAAoB;EACpB,wBAAwB;EAExB,cAAc;EACd,aAAa;EACb,GAAG;CACL,CAAC;AACH;;;;AAKA,SAAgB,eAAe,gBAAoC,UAA0B;CAC3F,IAAI;CACJ,MAAM,GAAG,mBAAmB;CAE5B,IAAI,OAAO,mBAAmB,YAAY,mBAAmB,IAAI;EAC/D,QAAM,uBAAuB,QAAQ;EACrC,MAAM,aAAa,eAAe,QAAQ,GAAG;EAC7C,iBAAiB,aAAa,IAAI,eAAe,MAAM,GAAG,UAAU,IAAI;CAC1E,OAAO;EACL,QAAM,oBAAoB,cAAc;EACxC,iBAAiB;CACnB;CAEA,OAAO,eAAe,SAAS,cAAc,IAAI,iBAAiB;AACpE;AAEA,SAAgB,WAAW,QAA+B;CACxD,IAAI,WAAW,MAAM,OAAO,WAAW,eAAe,WAAW,MAC/D,OAAO;MACF,IAAI,CAAC,OAAO,WAAW,GAAG,KAAK,OAAO,SAAS,GAAG,GACvD,OAAO,IAAI;MACN,IAAI,CAAC,OAAO,WAAW,GAAG,KAAK,CAAC,OAAO,SAAS,GAAG,GAC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,4BAA4B,WAAW;EAC7C,OAAO;CACT,OACE,OAAO;AAEX;AAEA,IAAa,QAAQ,UAAA,QAAU"}
|
package/build/index.mjs
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import buildDebug from "debug";
|
|
2
|
+
import { URL } from "node:url";
|
|
3
|
+
import validator from "validator";
|
|
4
|
+
import { HEADERS } from "@verdaccio/core";
|
|
5
|
+
//#region src/index.ts
|
|
6
|
+
var debug = buildDebug("verdaccio:core:url");
|
|
7
|
+
var validProtocols = ["https", "http"];
|
|
8
|
+
/**
|
|
9
|
+
* Check if URI is starting with "http://", "https://" or "//"
|
|
10
|
+
* @param {string} uri
|
|
11
|
+
*/
|
|
12
|
+
function isURLhasValidProtocol(uri) {
|
|
13
|
+
return /^(https?:)?\/\//.test(uri);
|
|
14
|
+
}
|
|
15
|
+
function isHost(url = "", options = {}) {
|
|
16
|
+
return validator.isURL(url, {
|
|
17
|
+
require_host: true,
|
|
18
|
+
allow_trailing_dot: false,
|
|
19
|
+
require_valid_protocol: false,
|
|
20
|
+
require_port: false,
|
|
21
|
+
require_tld: false,
|
|
22
|
+
...options
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Detect running protocol (http or https)
|
|
27
|
+
*/
|
|
28
|
+
function getWebProtocol(headerProtocol, protocol) {
|
|
29
|
+
let returnProtocol;
|
|
30
|
+
const [, defaultProtocol] = validProtocols;
|
|
31
|
+
if (typeof headerProtocol === "string" && headerProtocol !== "") {
|
|
32
|
+
debug("header protocol: %o", protocol);
|
|
33
|
+
const commaIndex = headerProtocol.indexOf(",");
|
|
34
|
+
returnProtocol = commaIndex > 0 ? headerProtocol.slice(0, commaIndex) : headerProtocol;
|
|
35
|
+
} else {
|
|
36
|
+
debug("req protocol: %o", headerProtocol);
|
|
37
|
+
returnProtocol = protocol;
|
|
38
|
+
}
|
|
39
|
+
return validProtocols.includes(returnProtocol) ? returnProtocol : defaultProtocol;
|
|
40
|
+
}
|
|
41
|
+
function wrapPrefix(prefix) {
|
|
42
|
+
if (prefix === "" || typeof prefix === "undefined" || prefix === null) return "";
|
|
43
|
+
else if (!prefix.startsWith("/") && prefix.endsWith("/")) return `/${prefix}`;
|
|
44
|
+
else if (!prefix.startsWith("/") && !prefix.endsWith("/")) return `/${prefix}/`;
|
|
45
|
+
else if (prefix.startsWith("/") && !prefix.endsWith("/")) return `${prefix}/`;
|
|
46
|
+
else return prefix;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Create base url for registry.
|
|
50
|
+
* @return {String} base registry url
|
|
51
|
+
*/
|
|
52
|
+
function combineBaseUrl(protocol, host, prefix = "") {
|
|
53
|
+
debug("combined protocol %o", protocol);
|
|
54
|
+
debug("combined host %o", host);
|
|
55
|
+
debug("combined prefix %o", wrapPrefix(prefix));
|
|
56
|
+
const result = new URL(wrapPrefix(prefix), `${protocol}://${host}`).href;
|
|
57
|
+
debug("combined url %o", result);
|
|
58
|
+
return result;
|
|
59
|
+
}
|
|
60
|
+
function validateURL(publicUrl) {
|
|
61
|
+
try {
|
|
62
|
+
const parsed = new URL(publicUrl);
|
|
63
|
+
if (!validProtocols.includes(parsed.protocol.replace(":", ""))) throw Error("invalid protocol");
|
|
64
|
+
return true;
|
|
65
|
+
} catch {
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
function getPublicUrl(url_prefix = "", requestOptions) {
|
|
70
|
+
if (validateURL(process.env.VERDACCIO_PUBLIC_URL)) {
|
|
71
|
+
const envURL = new URL(wrapPrefix(url_prefix), process.env.VERDACCIO_PUBLIC_URL).href;
|
|
72
|
+
debug("public url by env %o", envURL);
|
|
73
|
+
return envURL;
|
|
74
|
+
} else if (requestOptions.headers["host"]) {
|
|
75
|
+
const host = requestOptions.headers["host"];
|
|
76
|
+
if (!isHost(host)) throw new Error("invalid host");
|
|
77
|
+
const protoHeader = process.env.VERDACCIO_FORWARDED_PROTO?.toLocaleLowerCase() ?? HEADERS.FORWARDED_PROTO.toLowerCase();
|
|
78
|
+
const forwardedProtocolHeaderValue = requestOptions.headers[protoHeader];
|
|
79
|
+
if (Array.isArray(forwardedProtocolHeaderValue)) throw new Error("invalid forwarded protocol header value. Reading header " + protoHeader);
|
|
80
|
+
const combinedUrl = combineBaseUrl(getWebProtocol(forwardedProtocolHeaderValue, requestOptions.protocol), host, url_prefix);
|
|
81
|
+
debug("public url by request %o", combinedUrl);
|
|
82
|
+
return combinedUrl;
|
|
83
|
+
} else return "/";
|
|
84
|
+
}
|
|
85
|
+
var isURL = validator.isURL;
|
|
86
|
+
//#endregion
|
|
87
|
+
export { combineBaseUrl, getPublicUrl, getWebProtocol, isHost, isURL, isURLhasValidProtocol, validateURL, wrapPrefix };
|
|
88
|
+
|
|
89
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../src/index.ts"],"sourcesContent":["import buildDebug from 'debug';\nimport type { IncomingHttpHeaders } from 'node:http';\nimport { URL } from 'node:url';\nimport validator from 'validator';\n\nimport { HEADERS } from '@verdaccio/core';\n\nconst debug = buildDebug('verdaccio:core:url');\n\nconst validProtocols = ['https', 'http'];\n\n/**\n * Check if URI is starting with \"http://\", \"https://\" or \"//\"\n * @param {string} uri\n */\nexport function isURLhasValidProtocol(uri: string): boolean {\n return /^(https?:)?\\/\\//.test(uri);\n}\n\nexport function isHost(url: string = '', options = {}): boolean {\n return validator.isURL(url, {\n require_host: true,\n allow_trailing_dot: false,\n require_valid_protocol: false,\n // @ts-ignore\n require_port: false,\n require_tld: false,\n ...options,\n });\n}\n\n/**\n * Detect running protocol (http or https)\n */\nexport function getWebProtocol(headerProtocol: string | undefined, protocol: string): string {\n let returnProtocol;\n const [, defaultProtocol] = validProtocols;\n // HAProxy variant might return http,http with X-Forwarded-Proto\n if (typeof headerProtocol === 'string' && headerProtocol !== '') {\n debug('header protocol: %o', protocol);\n const commaIndex = headerProtocol.indexOf(',');\n returnProtocol = commaIndex > 0 ? headerProtocol.slice(0, commaIndex) : headerProtocol;\n } else {\n debug('req protocol: %o', headerProtocol);\n returnProtocol = protocol;\n }\n\n return validProtocols.includes(returnProtocol) ? returnProtocol : defaultProtocol;\n}\n\nexport function wrapPrefix(prefix: string | void): string {\n if (prefix === '' || typeof prefix === 'undefined' || prefix === null) {\n return '';\n } else if (!prefix.startsWith('/') && prefix.endsWith('/')) {\n return `/${prefix}`;\n } else if (!prefix.startsWith('/') && !prefix.endsWith('/')) {\n return `/${prefix}/`;\n } else if (prefix.startsWith('/') && !prefix.endsWith('/')) {\n return `${prefix}/`;\n } else {\n return prefix;\n }\n}\n\n/**\n * Create base url for registry.\n * @return {String} base registry url\n */\nexport function combineBaseUrl(protocol: string, host: string, prefix: string = ''): string {\n debug('combined protocol %o', protocol);\n debug('combined host %o', host);\n const newPrefix = wrapPrefix(prefix);\n debug('combined prefix %o', newPrefix);\n const groupedURI = new URL(wrapPrefix(prefix), `${protocol}://${host}`);\n const result = groupedURI.href;\n debug('combined url %o', result);\n return result;\n}\n\nexport function validateURL(publicUrl: string | void) {\n try {\n const parsed = new URL(publicUrl as string);\n if (!validProtocols.includes(parsed.protocol.replace(':', ''))) {\n throw Error('invalid protocol');\n }\n return true;\n } catch {\n // TODO: add error logger here\n return false;\n }\n}\n\nexport type RequestOptions = {\n /**\n * Request host (without port in Express 4).\n */\n host: string;\n /**\n * Request protocol, usually http or https.\n */\n protocol: string;\n /**\n * Request headers.\n */\n headers: IncomingHttpHeaders;\n /**\n * Request remote address. This is the IPv4 or IPv6 address of the client.\n */\n remoteAddress?: string;\n /**\n * Logged username the request, usually after token verification.\n */\n username?: string;\n};\n\nexport function getPublicUrl(url_prefix: string = '', requestOptions: RequestOptions): string {\n if (validateURL(process.env.VERDACCIO_PUBLIC_URL as string)) {\n const envURL = new URL(wrapPrefix(url_prefix), process.env.VERDACCIO_PUBLIC_URL as string).href;\n debug('public url by env %o', envURL);\n return envURL;\n } else if (requestOptions.headers['host']) {\n const host = requestOptions.headers['host'];\n if (!isHost(host)) {\n throw new Error('invalid host');\n }\n\n // 'X-Forwarded-Proto' is the default header\n const protoHeader: string =\n process.env.VERDACCIO_FORWARDED_PROTO?.toLocaleLowerCase() ??\n HEADERS.FORWARDED_PROTO.toLowerCase();\n const forwardedProtocolHeaderValue = requestOptions.headers[protoHeader];\n\n if (Array.isArray(forwardedProtocolHeaderValue)) {\n // This really should never happen - only set-cookie is allowed to have\n // multiple values.\n throw new Error('invalid forwarded protocol header value. Reading header ' + protoHeader);\n }\n\n const protocol = getWebProtocol(forwardedProtocolHeaderValue, requestOptions.protocol);\n const combinedUrl = combineBaseUrl(protocol, host, url_prefix);\n debug('public url by request %o', combinedUrl);\n return combinedUrl;\n } else {\n return '/';\n }\n}\n\nexport const isURL = validator.isURL;\n"],"mappings":";;;;;AAOA,IAAM,QAAQ,WAAW,oBAAoB;AAE7C,IAAM,iBAAiB,CAAC,SAAS,MAAM;;;;;AAMvC,SAAgB,sBAAsB,KAAsB;CAC1D,OAAO,kBAAkB,KAAK,GAAG;AACnC;AAEA,SAAgB,OAAO,MAAc,IAAI,UAAU,CAAC,GAAY;CAC9D,OAAO,UAAU,MAAM,KAAK;EAC1B,cAAc;EACd,oBAAoB;EACpB,wBAAwB;EAExB,cAAc;EACd,aAAa;EACb,GAAG;CACL,CAAC;AACH;;;;AAKA,SAAgB,eAAe,gBAAoC,UAA0B;CAC3F,IAAI;CACJ,MAAM,GAAG,mBAAmB;CAE5B,IAAI,OAAO,mBAAmB,YAAY,mBAAmB,IAAI;EAC/D,MAAM,uBAAuB,QAAQ;EACrC,MAAM,aAAa,eAAe,QAAQ,GAAG;EAC7C,iBAAiB,aAAa,IAAI,eAAe,MAAM,GAAG,UAAU,IAAI;CAC1E,OAAO;EACL,MAAM,oBAAoB,cAAc;EACxC,iBAAiB;CACnB;CAEA,OAAO,eAAe,SAAS,cAAc,IAAI,iBAAiB;AACpE;AAEA,SAAgB,WAAW,Q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C,MAAM,+BAA+B,eAAe,QAAQ;EAE5D,IAAI,MAAM,QAAQ,4BAA4B,GAG5C,MAAM,IAAI,MAAM,6DAA6D,WAAW;EAI1F,MAAM,cAAc,eADH,eAAe,8BAA8B,eAAe,QAC1C,GAAU,MAAM,UAAU;EAC7D,MAAM,4BAA4B,WAAW;EAC7C,OAAO;CACT,OACE,OAAO;AAEX;AAEA,IAAa,QAAQ,UAAU"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@verdaccio/url",
|
|
3
|
-
"version": "13.0
|
|
3
|
+
"version": "13.1.0",
|
|
4
4
|
"description": "Verdaccio URL Utilities",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"enterprise",
|
|
@@ -29,29 +29,42 @@
|
|
|
29
29
|
"url": "https://opencollective.com/verdaccio"
|
|
30
30
|
},
|
|
31
31
|
"main": "./build/index.js",
|
|
32
|
+
"module": "./build/index.mjs",
|
|
32
33
|
"types": "./build/index.d.ts",
|
|
34
|
+
"exports": {
|
|
35
|
+
".": {
|
|
36
|
+
"import": {
|
|
37
|
+
"types": "./build/index.d.ts",
|
|
38
|
+
"default": "./build/index.mjs"
|
|
39
|
+
},
|
|
40
|
+
"require": {
|
|
41
|
+
"types": "./build/index.d.ts",
|
|
42
|
+
"default": "./build/index.js"
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
"./build/*": "./build/*"
|
|
46
|
+
},
|
|
33
47
|
"publishConfig": {
|
|
34
48
|
"access": "public"
|
|
35
49
|
},
|
|
36
50
|
"dependencies": {
|
|
37
|
-
"@verdaccio/core": "8.
|
|
51
|
+
"@verdaccio/core": "8.2.0",
|
|
38
52
|
"debug": "4.4.3",
|
|
39
53
|
"validator": "13.15.26"
|
|
40
54
|
},
|
|
41
55
|
"devDependencies": {
|
|
42
56
|
"node-mocks-http": "1.14.1",
|
|
57
|
+
"vite": "8.0.16",
|
|
43
58
|
"vitest": "4.1.2"
|
|
44
59
|
},
|
|
45
60
|
"engines": {
|
|
46
|
-
"node": ">=
|
|
61
|
+
"node": ">=22"
|
|
47
62
|
},
|
|
48
63
|
"scripts": {
|
|
49
64
|
"clean": "rimraf ./build",
|
|
50
65
|
"test": "vitest run",
|
|
51
66
|
"type-check": "tsc --noEmit -p tsconfig.build.json",
|
|
52
|
-
"
|
|
53
|
-
"build
|
|
54
|
-
"watch": "pnpm build:js -- --watch",
|
|
55
|
-
"build": "pnpm run build:js && pnpm run build:types"
|
|
67
|
+
"watch": "vite build --watch",
|
|
68
|
+
"build": "vite build && tsc --emitDeclarationOnly -p tsconfig.build.json"
|
|
56
69
|
}
|
|
57
70
|
}
|