gh-release-fetch
Advanced tools
Comparing version 1.0.4 to 1.1.0
@@ -0,1 +1,7 @@ | ||
/// <reference types="node" /> | ||
import { RequestInit } from 'node-fetch'; | ||
import { Agent } from 'http'; | ||
interface DownloadOptions { | ||
agent?: Agent; | ||
} | ||
export interface Release { | ||
@@ -8,5 +14,6 @@ repository: string; | ||
} | ||
export declare function fetchLatest(release: Release): Promise<void>; | ||
export declare function fetchVersion(release: Release): Promise<void>; | ||
export declare function updateAvailable(repository: string, currentVersion: string): Promise<boolean>; | ||
export declare function fetchLatest(release: Release, fetchOptions?: RequestInit): Promise<void>; | ||
export declare function fetchVersion(release: Release, { agent }?: DownloadOptions): Promise<void>; | ||
export declare function updateAvailable(repository: string, currentVersion: string, fetchOptions?: RequestInit): Promise<boolean>; | ||
export declare function newerVersion(latestVersion: string, currentVersion: string): boolean; | ||
export {}; |
@@ -15,19 +15,19 @@ "use strict"; | ||
const semver_1 = require("semver"); | ||
function fetchLatest(release) { | ||
function fetchLatest(release, fetchOptions) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
release.version = yield resolveRelease(release.repository); | ||
return fetchVersion(release); | ||
release.version = yield resolveRelease(release.repository, fetchOptions); | ||
return fetchVersion(release, { agent: fetchOptions && fetchOptions.agent }); | ||
}); | ||
} | ||
exports.fetchLatest = fetchLatest; | ||
function fetchVersion(release) { | ||
function fetchVersion(release, { agent } = {}) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
validateRelease(release); | ||
yield downloadFile(release); | ||
yield downloadFile(release, { agent }); | ||
}); | ||
} | ||
exports.fetchVersion = fetchVersion; | ||
function updateAvailable(repository, currentVersion) { | ||
function updateAvailable(repository, currentVersion, fetchOptions) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const latestVersion = yield resolveRelease(repository); | ||
const latestVersion = yield resolveRelease(repository, fetchOptions); | ||
return newerVersion(latestVersion, currentVersion); | ||
@@ -37,5 +37,5 @@ }); | ||
exports.updateAvailable = updateAvailable; | ||
function resolveRelease(repository) { | ||
function resolveRelease(repository, fetchOptions) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const res = yield node_fetch_1.default(`https://api.github.com/repos/${repository}/releases/latest`); | ||
const res = yield node_fetch_1.default(`https://api.github.com/repos/${repository}/releases/latest`, fetchOptions); | ||
const json = yield res.json(); | ||
@@ -50,7 +50,10 @@ if (res.status === 403 && | ||
} | ||
function downloadFile(release) { | ||
function downloadFile(release, { agent }) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const url = `https://github.com/${release.repository}/releases/download/${release.version}/${release.package}`; | ||
mkdirp.sync(release.destination); | ||
yield download(url, release.destination, { extract: release.extract }); | ||
yield download(url, release.destination, { | ||
extract: release.extract, | ||
agent | ||
}); | ||
}); | ||
@@ -57,0 +60,0 @@ } |
{ | ||
"name": "gh-release-fetch", | ||
"version": "1.0.4", | ||
"version": "1.1.0", | ||
"description": "A library to fetch release binaries from GitHub Releases", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
import { newerVersion, fetchLatest } from "./index"; | ||
test('compare versions', () => { | ||
expect(newerVersion("0.1.0", "0.0.1")).toBe(true) | ||
expect(newerVersion("v0.1.0", "v0.0.1")).toBe(true) | ||
expect(newerVersion("v0.0.1", "")).toBe(true) | ||
test("compare versions", () => { | ||
expect(newerVersion("0.1.0", "0.0.1")).toBe(true); | ||
expect(newerVersion("v0.1.0", "v0.0.1")).toBe(true); | ||
expect(newerVersion("v0.0.1", "")).toBe(true); | ||
expect(newerVersion("0.0.1", "0.0.1")).toBe(false) | ||
expect(newerVersion("v0.0.1", "v0.0.1")).toBe(false) | ||
expect(newerVersion("", "0.0.1")).toBe(false) | ||
}) | ||
expect(newerVersion("0.0.1", "0.0.1")).toBe(false); | ||
expect(newerVersion("v0.0.1", "v0.0.1")).toBe(false); | ||
expect(newerVersion("", "0.0.1")).toBe(false); | ||
}); | ||
jest.mock('node-fetch'); | ||
jest.mock("node-fetch"); | ||
jest.mock("download"); | ||
describe('fetchLatest', () => { | ||
test('should throw error when api limit is reached', async () => { | ||
describe("fetchLatest", () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
test("should throw error when api limit is reached", async () => { | ||
const fetch = require("node-fetch"); | ||
@@ -22,19 +27,52 @@ const response = { | ||
Promise.resolve({ | ||
message: 'API rate limit exceeded for ', | ||
message: "API rate limit exceeded for ", | ||
}), | ||
}; | ||
fetch.mockResolvedValue(response) | ||
fetch.mockResolvedValue(response); | ||
await expect( | ||
fetchLatest({ | ||
repository: 'netlify/test', | ||
package: 'test', | ||
destination: 'bin/test', | ||
version: '1.0.0', | ||
repository: "netlify/test", | ||
package: "test", | ||
destination: "bin/test", | ||
version: "1.0.0", | ||
extract: true, | ||
}) | ||
).rejects.toEqual( | ||
new Error('API rate limit exceeded, please try again later') | ||
new Error("API rate limit exceeded, please try again later") | ||
); | ||
}); | ||
test("should add fetch options to API call when passed as a second argument", async () => { | ||
const fetch = require("node-fetch"); | ||
const response = { | ||
status: 200, | ||
json: () => | ||
Promise.resolve({ | ||
tag_name: "v1.0.0", | ||
}), | ||
}; | ||
fetch.mockResolvedValue(response); | ||
await expect( | ||
fetchLatest( | ||
{ | ||
repository: "netlify/test", | ||
package: "test", | ||
destination: "bin/test", | ||
version: "1.0.0", | ||
extract: true, | ||
}, | ||
{ headers: { Authorization: "token some_token" } } | ||
) | ||
); | ||
expect(fetch).toHaveBeenCalledTimes(1); | ||
expect( | ||
fetch | ||
).toHaveBeenCalledWith( | ||
"https://api.github.com/repos/netlify/test/releases/latest", | ||
{ headers: { Authorization: "token some_token" } } | ||
); | ||
}); | ||
}); |
import * as download from 'download'; | ||
import * as mkdirp from 'mkdirp'; | ||
import fetch from 'node-fetch'; | ||
import fetch, { RequestInit } from 'node-fetch'; | ||
import { gt } from 'semver'; | ||
import { Agent } from 'http'; | ||
interface DownloadOptions { agent?: Agent; } | ||
export interface Release { | ||
@@ -14,20 +17,34 @@ repository: string; | ||
export async function fetchLatest(release: Release) { | ||
release.version = await resolveRelease(release.repository); | ||
return fetchVersion(release); | ||
export async function fetchLatest( | ||
release: Release, | ||
fetchOptions?: RequestInit | ||
) { | ||
release.version = await resolveRelease(release.repository, fetchOptions); | ||
return fetchVersion(release, { agent: fetchOptions && fetchOptions.agent }); | ||
} | ||
export async function fetchVersion(release: Release) { | ||
export async function fetchVersion( | ||
release: Release, | ||
{ agent }: DownloadOptions = {} | ||
) { | ||
validateRelease(release); | ||
await downloadFile(release); | ||
await downloadFile(release, { agent }); | ||
} | ||
export async function updateAvailable(repository: string, currentVersion: string): Promise<boolean> { | ||
const latestVersion = await resolveRelease(repository); | ||
export async function updateAvailable( | ||
repository: string, | ||
currentVersion: string, | ||
fetchOptions?: RequestInit | ||
): Promise<boolean> { | ||
const latestVersion = await resolveRelease(repository, fetchOptions); | ||
return newerVersion(latestVersion, currentVersion); | ||
} | ||
async function resolveRelease(repository: string): Promise<string> { | ||
async function resolveRelease( | ||
repository: string, | ||
fetchOptions?: RequestInit | ||
): Promise<string> { | ||
const res = await fetch( | ||
`https://api.github.com/repos/${repository}/releases/latest` | ||
`https://api.github.com/repos/${repository}/releases/latest`, | ||
fetchOptions | ||
); | ||
@@ -45,6 +62,9 @@ const json = await res.json(); | ||
async function downloadFile(release: Release) { | ||
async function downloadFile(release: Release, { agent }: DownloadOptions) { | ||
const url = `https://github.com/${release.repository}/releases/download/${release.version}/${release.package}`; | ||
mkdirp.sync(release.destination); | ||
await download(url, release.destination, { extract: release.extract }); | ||
await download(url, release.destination, { | ||
extract: release.extract, | ||
agent | ||
}); | ||
} | ||
@@ -51,0 +71,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
12644
313
0
124905