import buildDebug from 'debug' ; import { errorUtils } from '@verdaccio/core' ; import { Callback , Config , IPluginStorage , Logger , PluginOptions , Token } from '@verdaccio/types' ; import MemoryHandler , { DataHandler } from './memory-handler' ; export type ConfigMemory = Config & { limit ? : number ; } ; export interface MemoryLocalStorage { secret : string ; list : string [ ] ; files : DataHandler ; } const debug = buildDebug ( 'verdaccio:plugin:storage:local-memory' ) ; const DEFAULT_LIMIT = 1000 ; class LocalMemory implements IPluginStorage < ConfigMemory > { private path : string ; private limit : number ; public logger : Logger ; private data : MemoryLocalStorage ; public config : ConfigMemory ; public constructor ( config , options ) { this . config = config ; this . limit = config . limit || DEFAULT_LIMIT ; this . logger = options . logger ; this . data = this . _createEmtpyDatabase ( ) ; this . path = '/' ; debug ( 'start plugin' ) ; } public init ( ) { return Promise . resolve ( ) ; } public getSecret ( ) { return Promise . resolve ( this . data . secret ) ; } public setSecret ( secret ) { return new Promise ( ( resolve ) : void => { this . data . secret = secret ; resolve ( null ) ; } ) ; } async add ( name ) { return new Promise ( ( resolve , reject ) : void => { const { list } = this . data ; if ( list . length < this . limit ) { if ( list . indexOf ( name ) === - 1 ) { list . push ( name ) ; } resolve ( ) ; } else { this . logger . info ( { limit : this . limit } , 'Storage memory has reached limit of @{limit} packages' ) ; reject ( new Error ( 'Storage memory has reached limit of limit packages' ) ) ; } } ) ; } public search ( onPackage , onEnd ) { this . logger . warn ( '[verdaccio/memory]: search method not implemented, PR is welcome' ) ; onEnd ( ) ; } async remove ( name ) { return new Promise ( ( resolve ) : void => { const { list } = this . data ; const item = list . indexOf ( name ) ; if ( item !== - 1 ) { list . splice ( item , 1 ) ; } return resolve ( ) ; } ) ; } async get ( ) { debug ( 'data list length %o' , this . data ?. list ?. length ) ; return Promise . resolve ( this . data ?. list ) ; } public getPackageStorage ( packageInfo ) { return new MemoryHandler ( packageInfo , this . data . files , this . logger ) ; } private _createEmtpyDatabase ( ) { const list = [ ] ; const files = { } ; const emptyDatabase = { list , files , secret : '' , } ; return emptyDatabase ; } public saveToken ( ) { this . logger . warn ( '[verdaccio/memory][saveToken] save token has not been implemented yet' ) ; return Promise . reject ( errorUtils . getServiceUnavailable ( 'method not implemented' ) ) ; } public deleteToken ( user , tokenKey ) { this . logger . warn ( { tokenKey , user } , '[verdaccio/memory][deleteToken] delete token has not been implemented yet @{user}' ) ; return Promise . reject ( errorUtils . getServiceUnavailable ( 'method not implemented' ) ) ; } public readTokens ( ) { this . logger . warn ( '[verdaccio/memory][readTokens] read tokens has not been implemented yet ' ) ; return Promise . reject ( errorUtils . getServiceUnavailable ( 'method not implemented' ) ) ; } } export default LocalMemory ;