From cad868cae221cb4a1017579b26321ac27b1b4624 Mon Sep 17 00:00:00 2001 From: Juan Picado Date: Sat, 29 Mar 2025 10:47:35 +0100 Subject: [PATCH] chore: clean up code (#5169) * chore: clean up code * cleanup * format * cleanup * Update index.ts * remove deps * clean up test * clean up * clean up --- .pnp.cjs | 10 - package.json | 7 +- src/api/middleware.ts | 4 +- src/api/web/api/package.ts | 4 +- src/lib/config-utils.ts | 47 ---- src/lib/constants.ts | 1 - src/lib/storage.ts | 2 +- src/lib/utils.ts | 159 +------------ src/types/index.ts | 25 +-- test/unit/modules/utils/config-utils.spec.ts | 71 ------ test/unit/modules/utils/utils.spec.ts | 225 +------------------ yarn.lock | 7 +- 12 files changed, 18 insertions(+), 544 deletions(-) delete mode 100644 src/lib/config-utils.ts delete mode 100644 test/unit/modules/utils/config-utils.spec.ts diff --git a/.pnp.cjs b/.pnp.cjs index 529b4e1e2..a5962a7b5 100755 --- a/.pnp.cjs +++ b/.pnp.cjs @@ -106,16 +106,12 @@ function $$SETUP_STATE(hydrateRuntimeState, basePath) { ["eslint-plugin-simple-import-sort", "virtual:7f7b3df50ee4b7b1719ad19fad11505dc2788f3227a7e5cc9ca19f71d8cb309c9d33b532ea2b2b60ab65abf6cc12153df4643c5e6e17d01ea0ae0492723bb4b4#npm:12.1.1"],\ ["eslint-plugin-verdaccio", "npm:10.0.0"],\ ["express", "npm:4.21.2"],\ - ["express-rate-limit", "npm:5.5.1"],\ - ["fast-safe-stringify", "npm:2.1.1"],\ ["fs-extra", "npm:10.1.0"],\ ["handlebars", "npm:4.7.8"],\ ["jest", "virtual:7f7b3df50ee4b7b1719ad19fad11505dc2788f3227a7e5cc9ca19f71d8cb309c9d33b532ea2b2b60ab65abf6cc12153df4643c5e6e17d01ea0ae0492723bb4b4#npm:29.7.0"],\ ["jest-config", "virtual:7f7b3df50ee4b7b1719ad19fad11505dc2788f3227a7e5cc9ca19f71d8cb309c9d33b532ea2b2b60ab65abf6cc12153df4643c5e6e17d01ea0ae0492723bb4b4#npm:29.7.0"],\ ["jest-environment-node", "npm:29.7.0"],\ ["jest-junit", "npm:15.0.0"],\ - ["js-yaml", "npm:4.1.0"],\ - ["jsonwebtoken", "npm:9.0.2"],\ ["kleur", "npm:4.1.5"],\ ["lockfile-lint", "npm:4.14.0"],\ ["lodash", "npm:4.17.21"],\ @@ -134,7 +130,6 @@ function $$SETUP_STATE(hydrateRuntimeState, basePath) { ["supertest", "npm:7.0.0"],\ ["ts-node", "virtual:7f7b3df50ee4b7b1719ad19fad11505dc2788f3227a7e5cc9ca19f71d8cb309c9d33b532ea2b2b60ab65abf6cc12153df4643c5e6e17d01ea0ae0492723bb4b4#npm:10.9.2"],\ ["typescript", "patch:typescript@npm%3A4.9.5#~builtin::version=4.9.5&hash=289587"],\ - ["validator", "npm:13.12.0"],\ ["verdaccio-audit", "npm:13.0.0-next-8.13"],\ ["verdaccio-auth-memory", "npm:10.2.2"],\ ["verdaccio-htpasswd", "npm:13.0.0-next-8.13"],\ @@ -15503,16 +15498,12 @@ function $$SETUP_STATE(hydrateRuntimeState, basePath) { ["eslint-plugin-simple-import-sort", "virtual:7f7b3df50ee4b7b1719ad19fad11505dc2788f3227a7e5cc9ca19f71d8cb309c9d33b532ea2b2b60ab65abf6cc12153df4643c5e6e17d01ea0ae0492723bb4b4#npm:12.1.1"],\ ["eslint-plugin-verdaccio", "npm:10.0.0"],\ ["express", "npm:4.21.2"],\ - ["express-rate-limit", "npm:5.5.1"],\ - ["fast-safe-stringify", "npm:2.1.1"],\ ["fs-extra", "npm:10.1.0"],\ ["handlebars", "npm:4.7.8"],\ ["jest", "virtual:7f7b3df50ee4b7b1719ad19fad11505dc2788f3227a7e5cc9ca19f71d8cb309c9d33b532ea2b2b60ab65abf6cc12153df4643c5e6e17d01ea0ae0492723bb4b4#npm:29.7.0"],\ ["jest-config", "virtual:7f7b3df50ee4b7b1719ad19fad11505dc2788f3227a7e5cc9ca19f71d8cb309c9d33b532ea2b2b60ab65abf6cc12153df4643c5e6e17d01ea0ae0492723bb4b4#npm:29.7.0"],\ ["jest-environment-node", "npm:29.7.0"],\ ["jest-junit", "npm:15.0.0"],\ - ["js-yaml", "npm:4.1.0"],\ - ["jsonwebtoken", "npm:9.0.2"],\ ["kleur", "npm:4.1.5"],\ ["lockfile-lint", "npm:4.14.0"],\ ["lodash", "npm:4.17.21"],\ @@ -15531,7 +15522,6 @@ function $$SETUP_STATE(hydrateRuntimeState, basePath) { ["supertest", "npm:7.0.0"],\ ["ts-node", "virtual:7f7b3df50ee4b7b1719ad19fad11505dc2788f3227a7e5cc9ca19f71d8cb309c9d33b532ea2b2b60ab65abf6cc12153df4643c5e6e17d01ea0ae0492723bb4b4#npm:10.9.2"],\ ["typescript", "patch:typescript@npm%3A4.9.5#~builtin::version=4.9.5&hash=289587"],\ - ["validator", "npm:13.12.0"],\ ["verdaccio-audit", "npm:13.0.0-next-8.13"],\ ["verdaccio-auth-memory", "npm:10.2.2"],\ ["verdaccio-htpasswd", "npm:13.0.0-next-8.13"],\ diff --git a/package.json b/package.json index f38e1239a..95fe68d4b 100644 --- a/package.json +++ b/package.json @@ -42,19 +42,13 @@ "debug": "4.4.0", "envinfo": "7.14.0", "express": "4.21.2", - "express-rate-limit": "5.5.1", - "fast-safe-stringify": "2.1.1", "handlebars": "4.7.8", - "js-yaml": "4.1.0", - "jsonwebtoken": "9.0.2", - "kleur": "4.1.5", "lodash": "4.17.21", "lru-cache": "7.18.3", "mime": "3.0.0", "mkdirp": "1.0.4", "pkginfo": "0.4.1", "semver": "7.6.3", - "validator": "13.12.0", "verdaccio-audit": "13.0.0-next-8.13", "verdaccio-htpasswd": "13.0.0-next-8.13" }, @@ -119,6 +113,7 @@ "jest-config": "29.7.0", "jest-environment-node": "29.7.0", "jest-junit": "15.0.0", + "kleur": "4.1.5", "lockfile-lint": "4.14.0", "nock": "13.5.6", "node-mocks-http": "^1.16.2", diff --git a/src/api/middleware.ts b/src/api/middleware.ts index 67ee294d4..a4d8e97e7 100644 --- a/src/api/middleware.ts +++ b/src/api/middleware.ts @@ -2,9 +2,9 @@ import buildDebug from 'debug'; import fs from 'fs'; import _ from 'lodash'; import path from 'path'; -import validator from 'validator'; import { Config } from '@verdaccio/types'; +import { isURL } from '@verdaccio/url'; import { HTTP_STATUS } from '../lib/constants'; import { $NextFunctionVer, $RequestExtend, $ResponseExtend } from '../types'; @@ -22,7 +22,7 @@ export function serveFavicon(config: Config) { } else if (!_.isEmpty(logoConf)) { debug('custom favicon'); if ( - validator.isURL(logoConf, { + isURL(logoConf, { require_host: true, require_valid_protocol: true, }) diff --git a/src/api/web/api/package.ts b/src/api/web/api/package.ts index b38e649e2..7d8e1a4c1 100644 --- a/src/api/web/api/package.ts +++ b/src/api/web/api/package.ts @@ -7,7 +7,7 @@ import { getLocalRegistryTarballUri, } from '@verdaccio/tarball'; import { Config, Manifest } from '@verdaccio/types'; -import { generateGravatarUrl } from '@verdaccio/utils'; +import { addGravatarSupport, formatAuthor, generateGravatarUrl } from '@verdaccio/utils'; import Auth from '../../../lib/auth'; import { DIST_TAGS, HEADERS, HEADER_TYPE, HTTP_STATUS } from '../../../lib/constants'; @@ -15,10 +15,8 @@ import { logger } from '../../../lib/logger'; import Storage from '../../../lib/storage'; import { ErrorCode, - addGravatarSupport, addScope, deleteProperties, - formatAuthor, isVersionValid, parseReadme, sortByName, diff --git a/src/lib/config-utils.ts b/src/lib/config-utils.ts deleted file mode 100644 index d31e3827e..000000000 --- a/src/lib/config-utils.ts +++ /dev/null @@ -1,47 +0,0 @@ -import _ from 'lodash'; - -import { PackageList } from '@verdaccio/types'; -import { getMatchedPackagesSpec } from '@verdaccio/utils'; - -import { LegacyPackageList, MatchedPackage } from '../types'; -import { ErrorCode } from './utils'; - -/** - * Normalize user list. - * @return {Array} - */ -export function normalizeUserList(oldFormat: any, newFormat: any): any { - const result: any[][] = []; - /* eslint prefer-rest-params: "off" */ - - for (let i = 0; i < arguments.length; i++) { - if (arguments[i] == null) { - continue; - } - - // if it's a string, split it to array - if (_.isString(arguments[i])) { - result.push(arguments[i].split(/\s+/)); - } else if (Array.isArray(arguments[i])) { - result.push(arguments[i]); - } else { - throw ErrorCode.getInternalError( - 'CONFIG: bad package acl (array or string expected): ' + JSON.stringify(arguments[i]) - ); - } - } - return _.flatten(result); -} - -/** - * Check whether an uplink can proxy - */ -export function hasProxyTo(pkg: string, upLink: string, packages: PackageList): boolean { - const matchedPkg: MatchedPackage = getMatchedPackagesSpec(pkg, packages); - const proxyList = typeof matchedPkg !== 'undefined' ? matchedPkg.proxy : []; - if (proxyList) { - return proxyList.some((curr) => upLink === curr); - } - - return false; -} diff --git a/src/lib/constants.ts b/src/lib/constants.ts index 198804cba..d051e4379 100644 --- a/src/lib/constants.ts +++ b/src/lib/constants.ts @@ -21,7 +21,6 @@ export const DIST_TAGS = 'dist-tags'; export const LATEST = 'latest'; export const USERS = 'users'; export const DEFAULT_MIN_LIMIT_PASSWORD = 3; -export const DEFAULT_USER = 'Anonymous'; export const keyPem = 'verdaccio-key.pem'; export const certPem = 'verdaccio-cert.pem'; diff --git a/src/lib/storage.ts b/src/lib/storage.ts index 6384ed7c5..5b4f6508c 100644 --- a/src/lib/storage.ts +++ b/src/lib/storage.ts @@ -4,6 +4,7 @@ import buildDebug from 'debug'; import _ from 'lodash'; import Stream from 'stream'; +import { hasProxyTo } from '@verdaccio/config'; import { PLUGIN_CATEGORY, pluginUtils, validatioUtils } from '@verdaccio/core'; import { asyncLoadPlugin } from '@verdaccio/loaders'; import LocalDatabasePlugin from '@verdaccio/local-storage-legacy'; @@ -25,7 +26,6 @@ import { GenericBody, Token, TokenFilter } from '@verdaccio/types'; import { StoragePluginLegacy } from '../../types/custom'; import { logger } from '../lib/logger'; import { IPluginFilters, ISyncUplinks, StringValue } from '../types'; -import { hasProxyTo } from './config-utils'; import { API_ERROR, DIST_TAGS, HTTP_STATUS } from './constants'; import LocalStorage, { StoragePlugin } from './local-storage'; import { mergeVersions } from './metadata-utils'; diff --git a/src/lib/utils.ts b/src/lib/utils.ts index f0e56a905..75c5cbd78 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -1,29 +1,15 @@ import fs from 'fs'; import _ from 'lodash'; import semver from 'semver'; -import { URL } from 'url'; -import validator from 'validator'; import { parseConfigFile } from '@verdaccio/config'; // eslint-disable-next-line max-len import { errorUtils, validatioUtils } from '@verdaccio/core'; import { StringValue } from '@verdaccio/types'; -import { Author, Config, Package, Version } from '@verdaccio/types'; -import { - GENERIC_AVATAR, - buildToken as buildTokenUtil, - generateGravatarUrl, - normalizeContributors, -} from '@verdaccio/utils'; +import { Config, Manifest, Version } from '@verdaccio/types'; +import { buildToken as buildTokenUtil } from '@verdaccio/utils'; -import { AuthorAvatar } from '../types'; -import { - DEFAULT_DOMAIN, - DEFAULT_PORT, - DEFAULT_PROTOCOL, - DEFAULT_USER, - DIST_TAGS, -} from './constants'; +import { DEFAULT_DOMAIN, DEFAULT_PORT, DEFAULT_PROTOCOL, DIST_TAGS } from './constants'; import { logger } from './logger'; const { @@ -37,10 +23,6 @@ const { getServiceUnavailable, getUnauthorized, } = errorUtils; -const validProtocols = ['https', 'http']; -export function convertPayloadToBase64(payload: string): Buffer { - return Buffer.from(payload, 'base64'); -} /** * Check whether an element is an Object @@ -56,7 +38,7 @@ export function isObjectOrArray(obj: any): boolean { return _.isObject(obj) && _.isNull(obj) === false; } -export function tagVersion(data: Package, version: string, tag: StringValue): boolean { +export function tagVersion(data: Manifest, version: string, tag: StringValue): boolean { if (tag && data[DIST_TAGS][tag] !== version && semver.parse(version, true)) { // valid version - store data[DIST_TAGS][tag] = version; @@ -69,7 +51,7 @@ export function tagVersion(data: Package, version: string, tag: StringValue): bo * Gets version from a package object taking into account semver weirdness. * @return {String} return the semantic version of a package */ -export function getVersion(pkg: Package, version: any): Version | void { +export function getVersion(pkg: Manifest, version: any): Version | void { // this condition must allow cast if (_.isNil(pkg.versions[version]) === false) { return pkg.versions[version]; @@ -153,7 +135,7 @@ export function semverSort(listVersions: string[]): string[] { * Flatten arrays of tags. * @param {*} data */ -export function normalizeDistTags(pkg: Package): void { +export function normalizeDistTags(pkg: Manifest): void { let sorted; if (!pkg[DIST_TAGS].latest) { // overwrite latest with highest known version based on semver sort @@ -287,56 +269,6 @@ export function deleteProperties(propertiesToDelete: string[], objectItem: any): return objectItem; } -export function addGravatarSupport(pkgInfo: Package, online = true): AuthorAvatar { - const pkgInfoCopy = { ...pkgInfo } as any; - const author: any = _.get(pkgInfo, 'latest.author', null) as any; - const contributors: AuthorAvatar[] = normalizeContributors( - _.get(pkgInfo, 'latest.contributors', []) - ); - const maintainers = _.get(pkgInfo, 'latest.maintainers', []); - - // for author. - if (author && _.isObject(author)) { - const { email } = author as Author; - pkgInfoCopy.latest.author.avatar = generateGravatarUrl(email, online); - } - - if (author && _.isString(author)) { - pkgInfoCopy.latest.author = { - avatar: GENERIC_AVATAR, - email: '', - author, - }; - } - - // for contributors - if (_.isEmpty(contributors) === false) { - pkgInfoCopy.latest.contributors = contributors.map((contributor): AuthorAvatar => { - if (isObject(contributor)) { - contributor.avatar = generateGravatarUrl(contributor.email, online); - } else if (_.isString(contributor)) { - contributor = { - avatar: GENERIC_AVATAR, - email: contributor, - name: contributor, - }; - } - - return contributor; - }); - } - - // for maintainers - if (_.isEmpty(maintainers) === false) { - pkgInfoCopy.latest.maintainers = maintainers.map((maintainer: any): void => { - maintainer.avatar = generateGravatarUrl(maintainer.email, online); - return maintainer; - }); - } - - return pkgInfoCopy; -} - /** * parse package readme - markdown/ascii * @param {String} packageName name of package @@ -356,53 +288,6 @@ export function parseReadme(packageName: string, readme: string): string | void return 'ERROR: No README data found!'; } -export type AuthorFormat = Author | string | null | object | void; - -/** - * Formats author field for webui. - * @see https://docs.npmjs.com/files/package.json#author - * @param {string|object|undefined} author - */ -export function formatAuthor(author: AuthorFormat): any { - let authorDetails = { - name: DEFAULT_USER, - email: '', - url: '', - }; - - if (_.isNil(author)) { - return authorDetails; - } - - if (_.isString(author)) { - authorDetails = { - ...authorDetails, - name: author as string, - }; - } - - if (_.isObject(author)) { - authorDetails = { - ...authorDetails, - ...(author as Author), - }; - } - - return authorDetails; -} - -/** - * Apply whitespaces based on the length - * @param {*} str the log message - * @return {String} - */ -export function pad(str, max): string { - if (str.length < max) { - return str + ' '.repeat(max - str.length); - } - return str; -} - /** * return a masquerade string with its first and last {charNum} and three dots in between. * @param {String} str @@ -431,7 +316,7 @@ export function isVersionValid(packageMeta, packageVersion): boolean { return hasMatchVersion; } -export function isRelatedToDeprecation(pkgInfo: Package): boolean { +export function isRelatedToDeprecation(pkgInfo: Manifest): boolean { const { versions } = pkgInfo; for (const version in versions) { if (Object.prototype.hasOwnProperty.call(versions[version], 'deprecated')) { @@ -441,31 +326,6 @@ export function isRelatedToDeprecation(pkgInfo: Package): boolean { return false; } -export function validateURL(publicUrl: string | void) { - try { - const parsed = new URL(publicUrl as string); - if (!validProtocols.includes(parsed.protocol.replace(':', ''))) { - throw Error('invalid protocol'); - } - return true; - } catch (err: any) { - // TODO: add error logger here - return false; - } -} - -export function isHost(url: string = '', options = {}): boolean { - return validator.isURL(url, { - require_host: true, - allow_trailing_dot: false, - require_valid_protocol: false, - // @ts-ignore - require_port: false, - require_tld: false, - ...options, - }); -} - /** * * @param config @@ -478,9 +338,4 @@ export function hasLogin(config: Config) { return _.isNil(config?.web?.login) || config?.web?.login === true; } -export function isNodeVersionHigherThanV22() { - const [major, minor] = process.versions.node.split('.').map(Number); - return major > 22 || (major === 22 && minor > 0); -} - export { buildTokenUtil as buildToken, parseConfigFile }; diff --git a/src/types/index.ts b/src/types/index.ts index 4d86ef5eb..abb1821e5 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -2,20 +2,15 @@ import { NextFunction, Request, Response } from 'express'; import { pluginUtils } from '@verdaccio/core'; import { - Author, Callback, Config, - JWTSignOptions, Logger, - Package, + Manifest, PackageAccess, RemoteUser, - Version, StringValue as verdaccio$StringValue, } from '@verdaccio/types'; -import Storage from '../lib/storage'; - export type StringValue = verdaccio$StringValue; // legacy should be removed in long term @@ -50,7 +45,7 @@ export interface Utils { isObject: (value: any) => boolean; validate_name: (value: any) => boolean; tag_version: (value: any, version: string, tag: string) => void; - normalizeDistTags: (pkg: Package) => void; + normalizeDistTags: (pkg: Manifest) => void; semverSort: (keys: string[]) => string[]; } @@ -68,12 +63,7 @@ export interface Profile { export type $RequestExtend = Request & { remote_user?: any; log: Logger }; export type $ResponseExtend = Response & { cookies?: any }; export type $NextFunctionVer = NextFunction & any; -export type $SidebarPackage = Package & { latest: any }; - -interface IAuthMiddleware { - apiJWTmiddleware(): $NextFunctionVer; - webUIJWTmiddleware(): $NextFunctionVer; -} +export type $SidebarPackage = Manifest & { latest: any }; export interface ISyncUplinks { uplinksLook?: boolean; @@ -82,12 +72,3 @@ export interface ISyncUplinks { } export type IPluginFilters = pluginUtils.ManifestFilter[]; - -/** - * @property { string | number | Styles } [ruleOrSelector] - */ -export interface Styles { - [ruleOrSelector: string]: string | number | Styles; -} - -export type AuthorAvatar = Author & { avatar?: string }; diff --git a/test/unit/modules/utils/config-utils.spec.ts b/test/unit/modules/utils/config-utils.spec.ts deleted file mode 100644 index dabdc37a9..000000000 --- a/test/unit/modules/utils/config-utils.spec.ts +++ /dev/null @@ -1,71 +0,0 @@ -import _ from 'lodash'; -import path from 'path'; - -import { normalisePackageAccess } from '@verdaccio/config'; - -import { hasProxyTo } from '../../../../src/lib/config-utils'; -import { parseConfigFile } from '../../../../src/lib/utils'; - -describe('Config Utilities', () => { - const parseConfigurationFile = (conf) => { - const { name, ext } = path.parse(conf); - const format = ext.startsWith('.') ? ext.substring(1) : 'yaml'; - - return path.join(__dirname, `../../partials/config/${format}/${name}.${format}`); - }; - - describe('hasProxyTo', () => { - test('should test basic config', () => { - const packages = normalisePackageAccess( - parseConfigFile(parseConfigurationFile('pkgs-basic')).packages - ); - // react - expect(hasProxyTo('react', 'facebook', packages)).toBeFalsy(); - expect(hasProxyTo('react', 'google', packages)).toBeFalsy(); - // vue - expect(hasProxyTo('vue', 'google', packages)).toBeFalsy(); - expect(hasProxyTo('vue', 'fake', packages)).toBeFalsy(); - expect(hasProxyTo('vue', 'npmjs', packages)).toBeTruthy(); - // angular - expect(hasProxyTo('angular', 'google', packages)).toBeFalsy(); - expect(hasProxyTo('angular', 'facebook', packages)).toBeFalsy(); - expect(hasProxyTo('angular', 'npmjs', packages)).toBeTruthy(); - }); - - test('should test resolve based on custom package access', () => { - const packages = normalisePackageAccess( - parseConfigFile(parseConfigurationFile('pkgs-custom')).packages - ); - // react - expect(hasProxyTo('react', 'facebook', packages)).toBeTruthy(); - expect(hasProxyTo('react', 'google', packages)).toBeFalsy(); - // vue - expect(hasProxyTo('vue', 'google', packages)).toBeFalsy(); - expect(hasProxyTo('vue', 'fake', packages)).toBeFalsy(); - expect(hasProxyTo('vue', 'npmjs', packages)).toBeTruthy(); - // angular - expect(hasProxyTo('angular', 'google', packages)).toBeTruthy(); - expect(hasProxyTo('angular', 'facebook', packages)).toBeFalsy(); - expect(hasProxyTo('angular', 'npmjs', packages)).toBeFalsy(); - }); - - test('should not resolve any proxy', () => { - const packages = normalisePackageAccess( - parseConfigFile(parseConfigurationFile('pkgs-empty')).packages - ); - // react - expect(hasProxyTo('react', 'npmjs', packages)).toBeFalsy(); - expect(hasProxyTo('react', 'npmjs', packages)).toBeFalsy(); - // vue - expect(hasProxyTo('vue', 'npmjs', packages)).toBeFalsy(); - expect(hasProxyTo('vue', 'npmjs', packages)).toBeFalsy(); - expect(hasProxyTo('vue', 'npmjs', packages)).toBeFalsy(); - // angular - expect(hasProxyTo('angular', 'npmjs', packages)).toBeFalsy(); - expect(hasProxyTo('angular', 'npmjs', packages)).toBeFalsy(); - expect(hasProxyTo('angular', 'npmjs', packages)).toBeFalsy(); - // private - expect(hasProxyTo('private', 'fake', packages)).toBeFalsy(); - }); - }); -}); diff --git a/test/unit/modules/utils/utils.spec.ts b/test/unit/modules/utils/utils.spec.ts index 28ecb531d..a3a84fab3 100644 --- a/test/unit/modules/utils/utils.spec.ts +++ b/test/unit/modules/utils/utils.spec.ts @@ -1,15 +1,8 @@ import { GENERIC_AVATAR, generateGravatarUrl } from '@verdaccio/utils'; -import { DEFAULT_USER, DIST_TAGS } from '../../../../src/lib/constants'; +import { DIST_TAGS } from '../../../../src/lib/constants'; import { logger, setup } from '../../../../src/lib/logger'; -import { - addGravatarSupport, - formatAuthor, - getVersion, - normalizeDistTags, - parseReadme, - sortByName, -} from '../../../../src/lib/utils'; +import { getVersion, normalizeDistTags, parseReadme, sortByName } from '../../../../src/lib/utils'; import { getByQualityPriorityValue } from '../../../../src/utils/string'; setup([]); @@ -197,218 +190,4 @@ describe('Utilities', () => { ); }); }); - - describe('addGravatarSupport', () => { - test('check for blank object', () => { - // @ts-ignore - expect(addGravatarSupport({})).toEqual({}); - }); - - test('author, contributors and maintainers fields are not present', () => { - const packageInfo = { - latest: {}, - }; - - // @ts-ignore - expect(addGravatarSupport(packageInfo)).toEqual(packageInfo); - }); - - test('author field is a blank object', () => { - const packageInfo = { latest: { author: {} } }; - - // @ts-ignore - expect(addGravatarSupport(packageInfo)).toEqual(packageInfo); - }); - - test('author field is a string type', () => { - const packageInfo = { - latest: { author: 'user@verdccio.org' }, - }; - const result = { - latest: { - author: { - author: 'user@verdccio.org', - avatar: GENERIC_AVATAR, - email: '', - }, - }, - }; - - // @ts-ignore - expect(addGravatarSupport(packageInfo)).toEqual(result); - }); - - test('author field is an object type with author information', () => { - const packageInfo = { - latest: { author: { name: 'verdaccio', email: 'user@verdccio.org' } }, - }; - const result = { - latest: { - author: { - avatar: 'https://www.gravatar.com/avatar/794d7f6ef93d0689437de3c3e48fadc7', - email: 'user@verdccio.org', - name: 'verdaccio', - }, - }, - }; - - // @ts-ignore - expect(addGravatarSupport(packageInfo)).toEqual(result); - }); - - test('contributor field is a blank array', () => { - const packageInfo = { - latest: { - contributors: [], - }, - }; - - // @ts-ignore - expect(addGravatarSupport(packageInfo)).toEqual(packageInfo); - }); - - describe('contributors', () => { - test('contributors field has contributors', () => { - const packageInfo = { - latest: { - contributors: [ - { name: 'user', email: 'user@verdccio.org' }, - { name: 'user1', email: 'user1@verdccio.org' }, - ], - }, - }; - - const result = { - latest: { - contributors: [ - { - avatar: 'https://www.gravatar.com/avatar/794d7f6ef93d0689437de3c3e48fadc7', - email: 'user@verdccio.org', - name: 'user', - }, - { - avatar: 'https://www.gravatar.com/avatar/51105a49ce4a9c2bfabf0f6a2cba3762', - email: 'user1@verdccio.org', - name: 'user1', - }, - ], - }, - }; - - // @ts-ignore - expect(addGravatarSupport(packageInfo)).toEqual(result); - }); - - test('contributors field is an object', () => { - const packageInfo = { - latest: { - contributors: { name: 'user', email: 'user@verdccio.org' }, - }, - }; - - const result = { - latest: { - contributors: [ - { - avatar: 'https://www.gravatar.com/avatar/794d7f6ef93d0689437de3c3e48fadc7', - email: 'user@verdccio.org', - name: 'user', - }, - ], - }, - }; - - // @ts-ignore - expect(addGravatarSupport(packageInfo)).toEqual(result); - }); - - test('contributors field is a string', () => { - const contributor = 'Barney Rubble (http://barnyrubble.tumblr.com/)'; - const packageInfo = { - latest: { - contributors: contributor, - }, - }; - - const result = { - latest: { - contributors: [ - { - avatar: GENERIC_AVATAR, - email: contributor, - name: contributor, - }, - ], - }, - }; - - // @ts-ignore - expect(addGravatarSupport(packageInfo)).toEqual(result); - }); - }); - - test('maintainers field is a blank array', () => { - const packageInfo = { - latest: { - maintainers: [], - }, - }; - - // @ts-ignore - expect(addGravatarSupport(packageInfo)).toEqual(packageInfo); - }); - - test('maintainers field has maintainers', () => { - const packageInfo = { - latest: { - maintainers: [ - { name: 'user', email: 'user@verdccio.org' }, - { name: 'user1', email: 'user1@verdccio.org' }, - ], - }, - }; - - const result = { - latest: { - maintainers: [ - { - avatar: 'https://www.gravatar.com/avatar/794d7f6ef93d0689437de3c3e48fadc7', - email: 'user@verdccio.org', - name: 'user', - }, - { - avatar: 'https://www.gravatar.com/avatar/51105a49ce4a9c2bfabf0f6a2cba3762', - email: 'user1@verdccio.org', - name: 'user1', - }, - ], - }, - }; - - // @ts-ignore - expect(addGravatarSupport(packageInfo)).toEqual(result); - }); - }); - - describe('formatAuthor', () => { - test('should check author field different values', () => { - const author = 'verdaccioNpm'; - expect(formatAuthor(author).name).toEqual(author); - }); - test('should check author field for object value', () => { - const user = { - name: 'Verdaccion NPM', - email: 'verdaccio@verdaccio.org', - url: 'https://verdaccio.org', - }; - expect(formatAuthor(user).url).toEqual(user.url); - expect(formatAuthor(user).email).toEqual(user.email); - expect(formatAuthor(user).name).toEqual(user.name); - }); - test('should check author field for other value', () => { - expect(formatAuthor(null).name).toEqual(DEFAULT_USER); - expect(formatAuthor({}).name).toEqual(DEFAULT_USER); - expect(formatAuthor([]).name).toEqual(DEFAULT_USER); - }); - }); }); diff --git a/yarn.lock b/yarn.lock index 8dfe01975..59a8afbf6 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6337,7 +6337,7 @@ __metadata: languageName: node linkType: hard -"fast-safe-stringify@npm:2.1.1, fast-safe-stringify@npm:^2.1.1": +"fast-safe-stringify@npm:^2.1.1": version: 2.1.1 resolution: "fast-safe-stringify@npm:2.1.1" checksum: a851cbddc451745662f8f00ddb622d6766f9bd97642dabfd9a405fb0d646d69fc0b9a1243cbf67f5f18a39f40f6fa821737651ff1bceeba06c9992ca2dc5bd3d @@ -12088,16 +12088,12 @@ __metadata: eslint-plugin-simple-import-sort: 12.1.1 eslint-plugin-verdaccio: 10.0.0 express: 4.21.2 - express-rate-limit: 5.5.1 - fast-safe-stringify: 2.1.1 fs-extra: 10.1.0 handlebars: 4.7.8 jest: 29.7.0 jest-config: 29.7.0 jest-environment-node: 29.7.0 jest-junit: 15.0.0 - js-yaml: 4.1.0 - jsonwebtoken: 9.0.2 kleur: 4.1.5 lockfile-lint: 4.14.0 lodash: 4.17.21 @@ -12116,7 +12112,6 @@ __metadata: supertest: 7.0.0 ts-node: 10.9.2 typescript: 4.9.5 - validator: 13.12.0 verdaccio-audit: 13.0.0-next-8.13 verdaccio-auth-memory: 10.2.2 verdaccio-htpasswd: 13.0.0-next-8.13