Skip to content

Commit

Permalink
lib
Browse files Browse the repository at this point in the history
  • Loading branch information
mislav committed Jun 23, 2022
1 parent 33989a8 commit fcd7e28
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 9 deletions.
49 changes: 41 additions & 8 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8360,6 +8360,7 @@ exports.default = default_1;
"use strict";

Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.parseReleaseDownloadUrl = exports.parseArchiveUrl = void 0;
const core_1 = __nccwpck_require__(2186);
const url_1 = __nccwpck_require__(8835);
const crypto_1 = __nccwpck_require__(6417);
Expand Down Expand Up @@ -8389,10 +8390,10 @@ function stream(url, headers, cb) {
async function resolveDownload(apiClient, url) {
if (url.hostname == 'github.com') {
const api = apiClient.rest;
const archive = url.pathname.match(/^\/([^/]+)\/([^/]+)\/archive\/([^/]+)(\.tar\.gz|\.zip)$/);
const archive = parseArchiveUrl(url);
if (archive != null) {
const [, owner, repo, ref, ext] = archive;
const res = await (ext == '.zip'
const { owner, repo, ref } = archive;
const res = await (archive.ext == '.zip'
? api.repos.downloadZipballArchive
: api.repos.downloadTarballArchive)({
owner,
Expand All @@ -8408,13 +8409,14 @@ async function resolveDownload(apiClient, url) {
// contains resolved commit SHA instead of the tag name in directory path.
return new url_1.URL(loc.replace('/legacy.', '/'));
}
const release = url.pathname.match(/^\/([^/]+)\/([^/]+)\/releases\/download\/([^/]+)\/(.+)$/);
if (release != null) {
const [, owner, repo, tag, path] = release;
const download = parseReleaseDownloadUrl(url);
if (download != null) {
const { owner, repo } = download;
const tag = download.tagname;
const res = await api.repos.getReleaseByTag({ owner, repo, tag });
const asset = res.data.assets.find((a) => a.name == path);
const asset = res.data.assets.find((a) => a.name == download.name);
if (asset == null) {
throw new Error(`could not find asset '${path}' in '${tag}' release`);
throw new Error(`could not find asset '${download.name}' in '${tag}' release`);
}
const assetRes = await apiClient.request(asset.url, {
headers: { accept: 'application/octet-stream' },
Expand All @@ -8426,6 +8428,37 @@ async function resolveDownload(apiClient, url) {
}
return url;
}
function parseArchiveUrl(url) {
const match = url.pathname.match(/^\/([^/]+)\/([^/]+)\/archive\/(.+)(\.tar\.gz|\.zip)$/);
if (match == null) {
return null;
}
return {
owner: match[1],
repo: match[2],
ref: match[3],
ext: match[4],
};
}
exports.parseArchiveUrl = parseArchiveUrl;
function parseReleaseDownloadUrl(url) {
const match = url.pathname.match(/^\/([^/]+)\/([^/]+)\/releases\/download\/(.+)$/);
if (match == null) {
return null;
}
const parts = match[3].split('/');
if (parts.length < 2) {
return null;
}
const name = parts.pop() || '';
return {
owner: match[1],
repo: match[2],
tagname: decodeURIComponent(parts.join('/')),
name: name,
};
}
exports.parseReleaseDownloadUrl = parseReleaseDownloadUrl;
function log(url) {
const params = Array.from(url.searchParams.keys());
const q = params.length > 0 ? `?${params.join(',')}` : '';
Expand Down
2 changes: 1 addition & 1 deletion lib/index.js.map

Large diffs are not rendered by default.

0 comments on commit fcd7e28

Please sign in to comment.