From 8e9793734be88ea9747212221fdb61ecbd39d0f4 Mon Sep 17 00:00:00 2001 From: Koala Yeung Date: Wed, 9 Feb 2022 11:15:15 +0800 Subject: [PATCH] Simplify PackagistVersion * Extract latest version string logics from BasePackagistService.findLatestRelease into findLatestVersion. * Reuse BasePackagistService.findLatestVersion in PackagistVersion to find the latest release. Remove duplicated logics. --- services/packagist/packagist-base.js | 39 +++++++++++++++++-- .../packagist/packagist-version.service.js | 16 +------- 2 files changed, 38 insertions(+), 17 deletions(-) diff --git a/services/packagist/packagist-base.js b/services/packagist/packagist-base.js index 6fc06d355325b3..4bbd7729eb395d 100644 --- a/services/packagist/packagist-base.js +++ b/services/packagist/packagist-base.js @@ -1,5 +1,5 @@ import Joi from 'joi' -import { BaseJsonService } from '../index.js' +import { BaseJsonService, NotFound } from '../index.js' import { isStable, latest } from '../php-version.js' const packageSchema = Joi.array().items( @@ -125,9 +125,42 @@ class BasePackagistService extends BaseJsonService { return expanded } + /** + * From an array of package, derive the latest release + * version string. + * + * @param {Array} versions An array of package versions. + * @param {boolean} includePrereleases Should pre-release version id be included. + * + * @throws {NotFound} If the packages array is empty or not an array. + * @returns {string} The version string of the latest version. + */ + findLatestVersion(versions, includePrereleases = false) { + if (!Array.isArray(versions) || versions.length === 0) { + throw new NotFound({ prettyMessage: 'no released version found' }) + } + + const versionStrings = versions + .map(version => version.version) + .filter( + versionString => + typeof versionString === 'string' || versionString instanceof String + ) + + let version = latest(versionStrings) + if (!includePrereleases) { + version = + latest(versionStrings.filter(isStable)) || latest(versionStrings) + } + + if (version === undefined) { + throw new NotFound({ prettyMessage: 'no released version found' }) + } + return version + } + findLatestRelease(json) { - const versions = json.map(version => version.version) - const release = latest(versions.filter(isStable)) || latest(versions) + const release = this.findLatestVersion(json) return json.filter(version => version.version === release)[0] } } diff --git a/services/packagist/packagist-version.service.js b/services/packagist/packagist-version.service.js index 089736f9ac4323..e75a5b8ea6a134 100644 --- a/services/packagist/packagist-version.service.js +++ b/services/packagist/packagist-version.service.js @@ -1,6 +1,5 @@ import Joi from 'joi' import { renderVersionBadge } from '../version.js' -import { isStable, latest } from '../php-version.js' import { optionalUrl } from '../validators.js' import { NotFound, redirector } from '../index.js' import { @@ -77,18 +76,6 @@ class PackagistVersion extends BasePackagistService { return renderVersionBadge({ version }) } - transform({ includePrereleases, json, user, repo }) { - const versionsData = json.packages[this.getPackageName(user, repo)] - const versions = versionsData.map(version => version.version) - - if (includePrereleases) { - return { version: latest(versions) } - } else { - const stableVersion = latest(versions.filter(isStable)) - return { version: stableVersion || latest(versions) } - } - } - async handle( { user, repo }, { include_prereleases: includePrereleases, server } @@ -100,7 +87,8 @@ class PackagistVersion extends BasePackagistService { schema: includePrereleases ? schema : allVersionsSchema, server, }) - const { version } = this.transform({ includePrereleases, json, user, repo }) + const packageJson = json.packages[this.getPackageName(user, repo)] + const version = this.findLatestVersion(packageJson, includePrereleases) if (version === undefined) { throw new NotFound({ prettyMessage: 'no released version found' }) }