Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an option to select another mirror #4

Merged
merged 1 commit into from
Aug 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ const _cache = new Map()
module.exports = async function (alias = 'lts_active', opts = {}) {
const now = opts.now || new Date()
const cache = opts.cache || _cache
const mirror = opts.mirror || 'https://nodejs.org/dist/'

const a = Array.isArray(alias) ? alias : [alias]
const versions = await getLatestVersionsByCodename(now, cache)
const versions = await getLatestVersionsByCodename(now, cache, mirror)

// Reduce to an object
const m = a.reduce((m, a) => {
Expand Down Expand Up @@ -50,15 +51,15 @@ function getSchedule (cache) {
}).json()
}

function getVersions (cache) {
return got('https://nodejs.org/dist/index.json', {
function getVersions (cache, mirror) {
return got(mirror.replace(/\/$/, '') + '/index.json', {
cache
}).json()
}

async function getLatestVersionsByCodename (now, cache) {
async function getLatestVersionsByCodename (now, cache, mirror) {
const schedule = await getSchedule(cache)
const versions = await getVersions(cache)
const versions = await getVersions(cache, mirror)

// Composite aliases point to the HEAD for each release line
const maintained = {}
Expand All @@ -67,10 +68,10 @@ async function getLatestVersionsByCodename (now, cache) {
const lts = {}

const aliases = versions.reduce((obj, ver) => {
const { major, minor, patch } = splitVersion(ver.version)
const { major, minor, patch, tag } = splitVersion(ver.version)
const versionName = major !== '0' ? `v${major}` : `v${major}.${minor}`
const codename = ver.lts ? ver.lts.toLowerCase() : versionName
const version = `${major}.${minor}.${patch}`
const version = tag !== '' ? `${major}.${minor}.${patch}-${tag}` : `${major}.${minor}.${patch}`
const s = schedule[versionName]

// Version Object
Expand All @@ -79,6 +80,7 @@ async function getLatestVersionsByCodename (now, cache) {
major,
minor,
patch,
tag,
codename,
versionName,
start: s && s.start && new Date(s.start),
Expand Down Expand Up @@ -147,6 +149,6 @@ async function getLatestVersionsByCodename (now, cache) {
}

function splitVersion (ver) {
const [, major, minor, patch] = /^v([0-9]*)\.([0-9]*)\.([0-9]*)/.exec(ver).map((n) => parseInt(n, 10))
return { major, minor, patch }
const [, major, minor, patch, tag] = /^v([0-9]*)\.([0-9]*)\.([0-9]*)(?:-([0-9A-Za-z-_]+))?/.exec(ver).map((n, i) => i < 4 ? parseInt(n, 10) : n || '')
return { major, minor, patch, tag }
}
13 changes: 12 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ suite('nv', () => {
assert.strictEqual(versions[0].versionName, 'v10')
assert.strictEqual(versions[0].start.toISOString(), '2018-04-24T00:00:00.000Z')
assert.strictEqual(versions[0].lts.toISOString(), '2018-10-30T00:00:00.000Z')
assert.strictEqual(versions[0].maintenance.toISOString(), '2020-04-01T00:00:00.000Z')
assert.strictEqual(versions[0].maintenance.toISOString(), '2020-05-19T00:00:00.000Z')
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change was necessary to make the tests pass. Didn't investigate further since it's unrelated.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting, since this test technically tests the upstream json it is probably they changed it. Now that you have this feature though, we could totally setup a mock mirror and point to it for the tests!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, didn't see this PR earlier - this was extended in nodejs/Release@9bc5275

assert.strictEqual(versions[0].end.toISOString(), '2021-04-30T00:00:00.000Z')
})

Expand Down Expand Up @@ -115,4 +115,15 @@ suite('nv', () => {
assert.strictEqual(versions[0].major, 10)
assert.strictEqual(versions[1].major, 12)
})

test('mirror: v8-canary', async () => {
const mirror = 'https://nodejs.org/download/v8-canary/'
const versions = await nv('v13', { now, mirror })
assert.strictEqual(versions.length, 1)
assert.strictEqual(versions[0].major, 13)
assert.strictEqual(versions[0].minor, 0)
assert.strictEqual(versions[0].patch, 0)
assert.strictEqual(versions[0].tag, 'v8-canary20191022e5d3472f57')
assert.strictEqual(versions[0].versionName, 'v13')
})
})