diff --git a/packages/store/src/index.ts b/packages/store/src/index.ts index c1eef8b2b..49313a8a8 100644 --- a/packages/store/src/index.ts +++ b/packages/store/src/index.ts @@ -1,4 +1,4 @@ -export { Storage } from './storage'; +export { OVERWRITE_MODE, Storage } from './storage'; export * from './lib/storage-utils'; export * from './lib/versions-utils'; export * from './lib/star-utils'; diff --git a/packages/store/src/storage.ts b/packages/store/src/storage.ts index 8330d0bae..3f763cb39 100644 --- a/packages/store/src/storage.ts +++ b/packages/store/src/storage.ts @@ -91,12 +91,11 @@ import { IGetPackageOptionsNext, OwnerManifestBody, StarManifestBody } from './t const debug = buildDebug('verdaccio:storage'); -const OVERWRITE_MODE = 'allow_overwrite'; - export type Filters = pluginUtils.ManifestFilter[]; export const noSuchFile = 'ENOENT'; export const resourceNotAvailable = 'EAGAIN'; export const PROTO_NAME = '__proto__'; +export const OVERWRITE_MODE = 'allow_overwrite'; class Storage { public localStorage: LocalStorage; @@ -105,6 +104,8 @@ class Storage { public readonly logger: Logger; public readonly uplinks: ProxyInstanceList; private searchService: Search; + private allowPackageOverwrite: boolean; + public constructor(config: Config, logger: Logger) { this.config = config; this.logger = logger.child({ module: 'storage' }); @@ -114,6 +115,7 @@ class Storage { // @ts-ignore this.localStorage = null; debug('uplinks available %o', Object.keys(this.uplinks)); + this.allowPackageOverwrite = false; } static ABBREVIATED_HEADER = 'application/vnd.npm.install-v1+json'; @@ -692,12 +694,15 @@ class Storage { /** * Check if developer mode is enabled via environment variable. + * + * Overwrite mode is only allowed when the storage is empty. */ private async checkDevMode() { if (process.env.VERDACCIO_DEV_MODE === OVERWRITE_MODE) { const packages = await this.localStorage.getStoragePlugin().get(); if (packages.length === 0) { this.logger.warn('Developer mode is enabled; you can overwrite packages'); + this.allowPackageOverwrite = true; } else { this.logger.warn('Storage must be empty to enable developer mode'); } @@ -1188,7 +1193,7 @@ class Storage { // if continue, the version to be published does not exist if (localManifest?.versions[versionToPublish] != null) { debug('%s version %s already exists (locally)', name, versionToPublish); - if (process.env.VERDACCIO_DEV_MODE === OVERWRITE_MODE) { + if (this.allowPackageOverwrite) { const filename = composeTarballFromPackage(name, versionToPublish); await this.removeTarball(name, filename, localManifest._rev, username!); delete localManifest.versions[versionToPublish]; @@ -1202,7 +1207,7 @@ class Storage { const remoteManifest = await this.checkPackageRemote(name, uplinksLook); if (remoteManifest?.versions[versionToPublish] != null) { debug('%s version %s already exists (upstream)', name, versionToPublish); - if (process.env.VERDACCIO_DEV_MODE !== OVERWRITE_MODE) { + if (!this.allowPackageOverwrite) { throw errorUtils.getConflict(); } } diff --git a/packages/store/test/storage.spec.ts b/packages/store/test/storage.spec.ts index 0002bdf25..e94c36811 100644 --- a/packages/store/test/storage.spec.ts +++ b/packages/store/test/storage.spec.ts @@ -35,7 +35,7 @@ import { Version, } from '@verdaccio/types'; -import { Storage } from '../src'; +import { OVERWRITE_MODE, Storage } from '../src'; import manifestFooRemoteNpmjs from './fixtures/manifests/foo-npmjs.json'; import { configExample } from './helpers'; @@ -368,7 +368,7 @@ describe('storage', () => { __dirname ) ); - process.env.VERDACCIO_DEV_MODE = 'allow_overwrite'; + process.env.VERDACCIO_DEV_MODE = OVERWRITE_MODE; const storage = new Storage(config, logger); await storage.init(config); const bodyNewManifest1 = generatePackageMetadata(pkgName, '1.0.0');