import assert from 'assert' ; import { Package } from '@verdaccio/types' ; import { DIST_TAGS } from './constants' ; export function isPackageNameScoped ( name ) { return name . startsWith ( '@' ) ; } param { * } name return { Boolean } export function validateName ( name ) { if ( typeof name !== 'string' ) { return false ; } let normalizedName = name . toLowerCase ( ) ; const isScoped = isPackageNameScoped ( name ) ; const scopedName = name . split ( '/' , 2 ) [ 1 ] ; if ( isScoped && typeof scopedName !== 'undefined' ) { normalizedName = scopedName . toLowerCase ( ) ; } /**
     * Some context about the first regex
     * - npm used to have a different tarball naming system.
     * eg: http://registry.npmjs.com/thirty-two
     * https://registry.npmjs.org/thirty-two/-/thirty-two@0.0.1.tgz
     * The file name thirty-two@0.0.1.tgz, the version and the pkg name was separated by an at (@)
     * while nowadays the naming system is based in dashes
     * https://registry.npmjs.org/verdaccio/-/verdaccio-1.4.0.tgz
     *
     * more info here: https://github.com/rlidwka/sinopia/issues/75
     */ return ! ( ! normalizedName . match ( /^[-a-zA-Z0-9_.!~*'()@]+$/ ) || normalizedName . startsWith ( '.' ) || [ 'node_modules' , '__proto__' , 'favicon.ico' ] . includes ( normalizedName ) ) ; } return { Boolean } export function validatePackage ( name ) { const nameList = name . split ( '/' , 2 ) ; if ( nameList . length === 1 ) { return validateName ( nameList [ 0 ] ) ; } return nameList [ 0 ] [ 0 ] === '@' && validateName ( nameList [ 0 ] . slice ( 1 ) ) && validateName ( nameList [ 1 ] ) ; } param { * } object param { * } name return { Object } export function validateMetadata ( object , name ) { assert ( isObject ( object ) , 'not a json object' ) ; assert . strictEqual ( object . name , name ) ; if ( ! isObject ( object [ DIST_TAGS ] ) ) { object [ DIST_TAGS ] = { } ; } if ( ! isObject ( object [ 'versions' ] ) ) { object [ 'versions' ] = { } ; } if ( ! isObject ( object [ 'time' ] ) ) { object [ 'time' ] = { } ; } return object ; } param { * } obj return { Boolean } export function isObject ( obj ) { if ( obj === null || typeof obj === 'undefined' ) { return false ; } return ( ( typeof obj === 'object' || typeof obj . prototype === 'undefined' ) && Array . isArray ( obj ) === false ) ; }