0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2025-01-13 22:48:31 -05:00

fix: Fix storing same named files from different pkgs in memory plugin (#3200)

The memory plugin was not correctly writing tarballs that have the same name but are from different packages and have different content.
This commit is contained in:
Daniel Tschinder 2022-05-30 17:49:42 +01:00 committed by GitHub
parent ef793182d5
commit b8981136b0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 78 additions and 27 deletions

View file

@ -0,0 +1,5 @@
---
'verdaccio-memory': patch
---
Fix storing tarballs with identical names from different packages in memory plugin

View file

@ -1,5 +1,6 @@
import buildDebug from 'debug'; import buildDebug from 'debug';
import { fs } from 'memfs'; import { fs } from 'memfs';
import path from 'path';
import { VerdaccioError, errorUtils } from '@verdaccio/core'; import { VerdaccioError, errorUtils } from '@verdaccio/core';
import { ReadTarball, UploadTarball } from '@verdaccio/streams'; import { ReadTarball, UploadTarball } from '@verdaccio/streams';
@ -35,7 +36,7 @@ class MemoryHandler implements IPackageStorageManager {
this.data = data; this.data = data;
this.name = packageName; this.name = packageName;
this.logger = logger; this.logger = logger;
this.path = '/'; this.path = `/${packageName}`;
debug('initialized'); debug('initialized');
} }
@ -113,11 +114,15 @@ class MemoryHandler implements IPackageStorageManager {
} }
public writeTarball(name: string): IUploadTarball { public writeTarball(name: string): IUploadTarball {
debug('write tarball %o', name);
const uploadStream: IUploadTarball = new UploadTarball({}); const uploadStream: IUploadTarball = new UploadTarball({});
const temporalName = `/${name}`; const temporalName = `${this.path}/${name}`;
debug('write tarball %o', temporalName);
process.nextTick(function () { process.nextTick(function () {
fs.mkdirp(path.dirname(temporalName), (mkdirpError) => {
if (mkdirpError) {
return uploadStream.emit('error', mkdirpError);
}
fs.stat(temporalName, function (fileError, stats) { fs.stat(temporalName, function (fileError, stats) {
if (!fileError && stats) { if (!fileError && stats) {
return uploadStream.emit('error', errorUtils.getConflict()); return uploadStream.emit('error', errorUtils.getConflict());
@ -149,13 +154,14 @@ class MemoryHandler implements IPackageStorageManager {
} }
}); });
}); });
});
return uploadStream; return uploadStream;
} }
public readTarball(name: string): IReadTarball { public readTarball(name: string): IReadTarball {
const pathName = `/${name}`; const pathName = `${this.path}/${name}`;
debug('read tarball %o', name); debug('read tarball %o', pathName);
const readTarballStream: IReadTarball = new ReadTarball({}); const readTarballStream: IReadTarball = new ReadTarball({});

View file

@ -299,6 +299,46 @@ describe('writing files', () => {
}); });
} }
}); });
test('should support writting identical tarball filenames from different packages', (done) => {
const localMemory: IPluginStorage<ConfigMemory> = new LocalMemory(config, defaultConfig);
const pkgName1 = 'package1';
const pkgName2 = 'package2';
const filename = 'tarball-3.0.0.tgz';
const dataTarball1 = '12345';
const dataTarball2 = '12345678';
const handler = localMemory.getPackageStorage(pkgName1);
if (handler) {
const stream = handler.writeTarball(filename);
stream.on('data', (data) => {
expect(data.toString()).toBe(dataTarball1);
});
stream.on('open', () => {
stream.done();
stream.end();
});
stream.on('success', () => {
const handler = localMemory.getPackageStorage(pkgName2);
if (handler) {
const stream = handler.writeTarball(filename);
stream.on('data', (data) => {
expect(data.toString()).toBe(dataTarball2);
});
stream.on('open', () => {
stream.done();
stream.end();
});
stream.on('success', () => {
done();
});
stream.write(dataTarball2);
}
});
stream.write(dataTarball1);
}
});
}); });
describe('reading files', () => { describe('reading files', () => {