Comparing version 2.3.0 to 2.4.0
import { FileStat } from 'tar'; | ||
import URLInfo from './url'; | ||
import { AxiosHeaders, RawAxiosRequestHeaders } from 'axios'; | ||
export default interface GitlyOptions { | ||
@@ -37,2 +38,6 @@ /** | ||
}; | ||
/** | ||
* Set the request headers (default: undefined) | ||
*/ | ||
headers?: RawAxiosRequestHeaders | AxiosHeaders; | ||
} |
@@ -29,3 +29,3 @@ "use strict"; | ||
const local = async () => (0, exists_1.default)(file); | ||
const remote = async () => (0, fetch_1.default)(url, file); | ||
const remote = async () => (0, fetch_1.default)(url, file, options); | ||
let order = [local, remote]; | ||
@@ -32,0 +32,0 @@ if ((await (0, offline_1.isOffline)()) || options.cache) { |
@@ -1,1 +0,2 @@ | ||
export default function fetch(url: string, file: string): Promise<string>; | ||
import GitlyOptions from '../interfaces/options'; | ||
export default function fetch(url: string, file: string, options?: GitlyOptions): Promise<string>; |
@@ -35,4 +35,5 @@ "use strict"; | ||
const pipeline = (0, util_1.promisify)(stream.pipeline); | ||
async function fetch(url, file) { | ||
async function fetch(url, file, options = {}) { | ||
const response = await axios_1.default.get(url, { | ||
headers: options.headers, | ||
responseType: 'stream', | ||
@@ -39,0 +40,0 @@ validateStatus: (status) => status >= 200 && status < 500, |
@@ -17,3 +17,2 @@ import GitlyOptions from '../interfaces/options'; | ||
*/ | ||
declare const _default: (url: string, options?: GitlyOptions) => URLInfo; | ||
export default _default; | ||
export default function parse(url: string, options?: GitlyOptions): URLInfo; |
@@ -18,7 +18,6 @@ "use strict"; | ||
*/ | ||
exports.default = (url, options = {}) => { | ||
function parse(url, options = {}) { | ||
const { url: normalized, host } = normalizeURL(url, options); | ||
// Parse the url | ||
const result = (0, url_1.parse)(normalized); | ||
const paths = (result.path || '').split('/').filter((p) => !!p); | ||
const result = new url_1.URL(normalized); | ||
const paths = (result.pathname || '').split('/').filter(Boolean); | ||
const owner = paths.shift() || ''; | ||
@@ -32,48 +31,34 @@ const repository = paths.shift() || ''; | ||
href: result.href || '', | ||
path: result.path || '', | ||
path: result.pathname || '', | ||
repository, | ||
owner, | ||
type: (result.hash || '#master').substr(1), | ||
type: (result.hash || '#master').substring(1), | ||
}; | ||
}; | ||
} | ||
exports.default = parse; | ||
function normalizeURL(url, options) { | ||
// Remove 'www.' | ||
url = url.replace('www.', ''); | ||
// Remove '.git' | ||
url = url.replace('.git', ''); | ||
const httpRegex = /http(s)?:\/\//; | ||
const tldRegex = /[\S]+\.([\D]+)/; | ||
let host = options.host || ''; | ||
if (/([\S]+):.+/.test(url) && !httpRegex.test(url)) { | ||
/** | ||
* Matches host:owner/repo | ||
*/ | ||
const matches = url.match(/([\S]+):.+/); | ||
// Get the host | ||
host = matches ? matches[1] : ''; | ||
// Remove the host from the url | ||
url = host ? url.replace(`${host}:`, '') : url; | ||
// Add the host back in the correct format | ||
url = `https://${host}.com/${url}`; | ||
const { host } = options; | ||
const isNotProtocol = !/http(s)?:\/\//.test(url); | ||
const hasHost = /([\S]+):.+/.test(url); | ||
const hasTLD = /[\S]+\.([\D]+)/.test(url); | ||
let normalizedURL = url.replace("www.", "").replace(".git", ""); | ||
let updatedHost = host || ""; | ||
if (isNotProtocol && hasHost) { | ||
// Matches host:owner/repo | ||
const hostMatch = url.match(/([\S]+):.+/); | ||
updatedHost = hostMatch ? hostMatch[1] : ""; | ||
normalizedURL = `https://${updatedHost}.com/${normalizedURL.replace(`${updatedHost}:`, "")}`; | ||
} | ||
else if (tldRegex.test(url) && !httpRegex.test(url)) { | ||
/** | ||
* Matches github.com/... | ||
*/ | ||
url = `https://${url}`; | ||
else if (isNotProtocol && hasTLD) { | ||
// Matches host.com/... | ||
normalizedURL = `https://${normalizedURL}`; | ||
} | ||
else if (/[\S]+\/[\S]+/.test(url) && !httpRegex.test(url)) { | ||
/** | ||
* Matches owner/repo | ||
*/ | ||
// Get the TLD if any | ||
const matches = (options.host || '').match(tldRegex); | ||
let match = 'com'; | ||
if (matches) | ||
match = matches[1]; | ||
let domain = options.host || 'github'; | ||
domain = domain.replace(`.${match}`, ''); | ||
url = `https://${domain}.${match}/${url}`; | ||
else if (isNotProtocol) { | ||
// Matches owner/repo | ||
const tldMatch = (host || "").match(/[\S]+\.([\D]+)/); | ||
const domain = (host || "github").replace(`.${tldMatch ? tldMatch[1] : "com"}`, ""); | ||
const tld = tldMatch ? tldMatch[1] : "com"; | ||
normalizedURL = `https://${domain}.${tld}/${normalizedURL}`; | ||
} | ||
return { url, host }; | ||
return { url: normalizedURL, host: updatedHost }; | ||
} |
{ | ||
"name": "gitly", | ||
"version": "2.3.0", | ||
"version": "2.4.0", | ||
"description": "An API to download and/or extract git repositories", | ||
@@ -13,21 +13,21 @@ "main": "lib/main.js", | ||
"dependencies": { | ||
"axios": "^1.2.3", | ||
"axios": "^1.3.2", | ||
"tar": "^6.1.13" | ||
}, | ||
"devDependencies": { | ||
"@types/jest": "^29.2.6", | ||
"@types/node": "^18.11.18", | ||
"@types/jest": "^29.4.0", | ||
"@types/node": "^18.13.0", | ||
"@types/shelljs": "^0.8.11", | ||
"@types/tar": "^6.1.3", | ||
"@typescript-eslint/eslint-plugin": "^5.48.2", | ||
"@typescript-eslint/eslint-plugin-tslint": "^5.48.2", | ||
"@typescript-eslint/parser": "^5.48.2", | ||
"eslint": "^8.32.0", | ||
"@typescript-eslint/eslint-plugin": "^5.51.0", | ||
"@typescript-eslint/eslint-plugin-tslint": "^5.51.0", | ||
"@typescript-eslint/parser": "^5.51.0", | ||
"eslint": "^8.33.0", | ||
"eslint-plugin-import": "^2.27.5", | ||
"eslint-plugin-prefer-arrow": "^1.2.3", | ||
"jest": "^29.3.1", | ||
"jest": "^29.4.2", | ||
"shelljs": "^0.8.5", | ||
"ts-jest": "^29.0.5", | ||
"tslib": "^2.4.1", | ||
"typescript": "^4.9.4" | ||
"tslib": "^2.5.0", | ||
"typescript": "^4.9.5" | ||
}, | ||
@@ -34,0 +34,0 @@ "scripts": { |
@@ -88,2 +88,6 @@ # gitly | ||
} | ||
/** | ||
* Set the request headers (default: undefined) | ||
*/ | ||
headers? RawAxiosRequestHeaders | AxiosHeaders | ||
} | ||
@@ -90,0 +94,0 @@ ``` |
44665
110
1013
Updatedaxios@^1.3.2