forked from actions/setup-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtil.js
More file actions
35 lines (35 loc) · 1.35 KB
/
Copy pathUtil.js
File metadata and controls
35 lines (35 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
"use strict";
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
Object.defineProperty(exports, "__esModule", { value: true });
const url = require("url");
const path = require("path");
/**
* creates an url from a request url and optional base url (http://server:8080)
* @param {string} resource - a fully qualified url or relative path
* @param {string} baseUrl - an optional baseUrl (http://server:8080)
* @return {string} - resultant url
*/
function getUrl(resource, baseUrl) {
const pathApi = path.posix || path;
if (!baseUrl) {
return resource;
}
else if (!resource) {
return baseUrl;
}
else {
const base = url.parse(baseUrl);
const resultantUrl = url.parse(resource);
// resource (specific per request) elements take priority
resultantUrl.protocol = resultantUrl.protocol || base.protocol;
resultantUrl.auth = resultantUrl.auth || base.auth;
resultantUrl.host = resultantUrl.host || base.host;
resultantUrl.pathname = pathApi.resolve(base.pathname, resultantUrl.pathname);
if (!resultantUrl.pathname.endsWith('/') && resource.endsWith('/')) {
resultantUrl.pathname += '/';
}
return url.format(resultantUrl);
}
}
exports.getUrl = getUrl;