2020-10-23 15:05:46 -05:00
|
|
|
import { Logger, IPluginStorage, IPackageStorage, ILocalPackageManager } from '@verdaccio/types';
|
2021-09-25 17:08:00 -05:00
|
|
|
import { errorUtils } from '@verdaccio/core';
|
2020-10-23 15:05:46 -05:00
|
|
|
|
|
|
|
import { ConfigMemory } from '../src/local-memory';
|
|
|
|
import MemoryHandler from '../src/memory-handler';
|
|
|
|
import LocalMemory from '../src/index';
|
|
|
|
|
|
|
|
import config from './partials/config';
|
|
|
|
import pkgExample from './partials/pkg';
|
|
|
|
|
|
|
|
const logger: Logger = {
|
|
|
|
error: (e) => console.warn(e),
|
|
|
|
info: (e) => console.warn(e),
|
|
|
|
debug: (e) => console.warn(e),
|
|
|
|
child: (e) => console.warn(e),
|
|
|
|
warn: (e) => console.warn(e),
|
|
|
|
http: (e) => console.warn(e),
|
|
|
|
trace: (e) => console.warn(e),
|
|
|
|
};
|
|
|
|
|
|
|
|
const defaultConfig = { logger, config: null };
|
|
|
|
|
|
|
|
const mockStringify = jest.fn((value) => {
|
|
|
|
return jest.requireActual('../src/utils.ts').stringifyPackage(value);
|
|
|
|
});
|
|
|
|
|
|
|
|
const mockParsePackage = jest.fn((value) => {
|
|
|
|
return jest.requireActual('../src/utils.ts').parsePackage(value);
|
|
|
|
});
|
|
|
|
|
|
|
|
jest.mock('../src/utils.ts', () => ({
|
|
|
|
stringifyPackage: (value) => mockStringify(value),
|
|
|
|
parsePackage: (value) => mockParsePackage(value),
|
|
|
|
}));
|
|
|
|
|
|
|
|
describe('memory unit test .', () => {
|
2021-03-30 11:43:57 -05:00
|
|
|
test('should create an MemoryHandler instance', () => {
|
|
|
|
const memoryHandler = new MemoryHandler(
|
|
|
|
'test',
|
|
|
|
{
|
|
|
|
['foo']: 'bar',
|
|
|
|
},
|
|
|
|
logger
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(memoryHandler).toBeDefined();
|
|
|
|
});
|
2020-10-23 15:05:46 -05:00
|
|
|
|
2021-03-30 11:43:57 -05:00
|
|
|
test('should save a package', (done) => {
|
|
|
|
const localMemory: IPluginStorage<ConfigMemory> = new LocalMemory(config, defaultConfig);
|
|
|
|
const pkgName = 'test';
|
2020-10-23 15:05:46 -05:00
|
|
|
|
2021-03-30 11:43:57 -05:00
|
|
|
const handler = localMemory.getPackageStorage(pkgName);
|
|
|
|
expect(handler).toBeDefined();
|
2020-10-23 15:05:46 -05:00
|
|
|
|
2021-03-30 11:43:57 -05:00
|
|
|
if (handler) {
|
|
|
|
handler.savePackage(pkgName, pkgExample, (err) => {
|
|
|
|
expect(err).toBeNull();
|
|
|
|
handler.readPackage(pkgName, (err, data) => {
|
2020-10-23 15:05:46 -05:00
|
|
|
expect(err).toBeNull();
|
2021-03-30 11:43:57 -05:00
|
|
|
expect(data).toEqual(pkgExample);
|
|
|
|
done();
|
2020-10-23 15:05:46 -05:00
|
|
|
});
|
|
|
|
});
|
2021-03-30 11:43:57 -05:00
|
|
|
}
|
|
|
|
});
|
2020-10-23 15:05:46 -05:00
|
|
|
|
2021-03-30 11:43:57 -05:00
|
|
|
test('should fails on save a package', (done) => {
|
|
|
|
mockStringify.mockImplementationOnce(() => {
|
|
|
|
throw new Error('error on parse');
|
2020-10-23 15:05:46 -05:00
|
|
|
});
|
|
|
|
|
2021-03-30 11:43:57 -05:00
|
|
|
const localMemory: IPluginStorage<ConfigMemory> = new LocalMemory(config, defaultConfig);
|
|
|
|
const pkgName = 'test';
|
2020-10-23 15:05:46 -05:00
|
|
|
|
2021-03-30 11:43:57 -05:00
|
|
|
const handler: IPackageStorage = localMemory.getPackageStorage(pkgName) as ILocalPackageManager;
|
2020-10-23 15:05:46 -05:00
|
|
|
|
2021-03-30 11:43:57 -05:00
|
|
|
handler.savePackage(pkgName, pkgExample, (err) => {
|
2021-09-25 17:08:00 -05:00
|
|
|
expect(err).toEqual(errorUtils.getInternalError('error on parse'));
|
2021-03-30 11:43:57 -05:00
|
|
|
done();
|
2020-10-23 15:05:46 -05:00
|
|
|
});
|
2021-03-30 11:43:57 -05:00
|
|
|
});
|
2020-10-23 15:05:46 -05:00
|
|
|
|
2021-03-30 11:43:57 -05:00
|
|
|
test('should fails on read a package', (done) => {
|
|
|
|
const localMemory: IPluginStorage<ConfigMemory> = new LocalMemory(config, defaultConfig);
|
|
|
|
const pkgName = 'test';
|
2020-10-23 15:05:46 -05:00
|
|
|
|
2021-03-30 11:43:57 -05:00
|
|
|
const handler = localMemory.getPackageStorage(pkgName);
|
|
|
|
expect(handler).toBeDefined();
|
2020-10-23 15:05:46 -05:00
|
|
|
|
2021-03-30 11:43:57 -05:00
|
|
|
if (handler) {
|
|
|
|
handler.readPackage(pkgName, (err) => {
|
|
|
|
expect(err).not.toBeNull();
|
|
|
|
expect(err.code).toBe(404);
|
|
|
|
done();
|
2020-10-23 15:05:46 -05:00
|
|
|
});
|
2021-03-30 11:43:57 -05:00
|
|
|
}
|
|
|
|
});
|
2020-10-23 15:05:46 -05:00
|
|
|
|
2021-03-30 11:43:57 -05:00
|
|
|
test('should update a package', (done) => {
|
|
|
|
const localMemory: IPluginStorage<ConfigMemory> = new LocalMemory(config, defaultConfig);
|
|
|
|
const pkgName = 'test';
|
2020-10-23 15:05:46 -05:00
|
|
|
|
2021-03-30 11:43:57 -05:00
|
|
|
const handler = localMemory.getPackageStorage(pkgName);
|
|
|
|
expect(handler).toBeDefined();
|
|
|
|
const onEnd = jest.fn();
|
|
|
|
|
|
|
|
if (handler) {
|
|
|
|
handler.savePackage(pkgName, pkgExample, (err) => {
|
|
|
|
expect(err).toBeNull();
|
|
|
|
|
|
|
|
handler.updatePackage(
|
|
|
|
pkgName,
|
|
|
|
(json, callback) => {
|
|
|
|
expect(json).toBeDefined();
|
|
|
|
expect(json.name).toBe(pkgExample.name);
|
|
|
|
expect(callback).toBeDefined();
|
|
|
|
callback(null);
|
|
|
|
},
|
|
|
|
(name, data, onEnd) => {
|
|
|
|
expect(name).toBe(pkgName);
|
|
|
|
expect(data.name).toBe(pkgExample.name);
|
|
|
|
onEnd();
|
|
|
|
expect(onEnd).toHaveBeenCalled();
|
|
|
|
done();
|
|
|
|
},
|
|
|
|
(data) => {
|
|
|
|
expect(data).toBeDefined();
|
|
|
|
return data;
|
|
|
|
},
|
|
|
|
onEnd
|
|
|
|
);
|
2020-10-23 15:05:46 -05:00
|
|
|
});
|
2021-03-30 11:43:57 -05:00
|
|
|
}
|
|
|
|
});
|
2020-10-23 15:05:46 -05:00
|
|
|
|
2021-03-30 11:43:57 -05:00
|
|
|
test('should parse fails on update a package', (done) => {
|
|
|
|
mockParsePackage.mockImplementationOnce(() => {
|
|
|
|
throw new Error('error on parse');
|
2020-10-23 15:05:46 -05:00
|
|
|
});
|
2021-03-30 11:43:57 -05:00
|
|
|
const localMemory: IPluginStorage<ConfigMemory> = new LocalMemory(config, defaultConfig);
|
2020-10-23 15:05:46 -05:00
|
|
|
|
2021-03-30 11:43:57 -05:00
|
|
|
const pkgName = 'test';
|
2020-10-23 15:05:46 -05:00
|
|
|
|
2021-03-30 11:43:57 -05:00
|
|
|
const handler = localMemory.getPackageStorage(pkgName);
|
|
|
|
expect(handler).toBeDefined();
|
|
|
|
const onEnd = jest.fn((err) => {
|
|
|
|
expect(err).not.toBeNull();
|
2021-09-25 17:08:00 -05:00
|
|
|
expect(err).toEqual(errorUtils.getInternalError('error on parse'));
|
2021-03-30 11:43:57 -05:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
if (handler) {
|
|
|
|
handler.savePackage(pkgName, pkgExample, (err) => {
|
|
|
|
expect(err).toBeNull();
|
|
|
|
handler.updatePackage(
|
|
|
|
pkgName,
|
|
|
|
() => {},
|
|
|
|
() => {},
|
|
|
|
// @ts-ignore
|
|
|
|
() => {},
|
|
|
|
onEnd
|
|
|
|
);
|
2020-10-23 15:05:46 -05:00
|
|
|
});
|
2021-03-30 11:43:57 -05:00
|
|
|
}
|
|
|
|
});
|
2020-10-23 15:05:46 -05:00
|
|
|
|
2021-03-30 11:43:57 -05:00
|
|
|
test('should fail updateHandler update a package', (done) => {
|
|
|
|
const localMemory: IPluginStorage<ConfigMemory> = new LocalMemory(config, defaultConfig);
|
|
|
|
const pkgName = 'test';
|
2020-10-23 15:05:46 -05:00
|
|
|
|
2021-03-30 11:43:57 -05:00
|
|
|
const handler = localMemory.getPackageStorage(pkgName);
|
|
|
|
expect(handler).toBeDefined();
|
|
|
|
const onEnd = jest.fn((err) => {
|
|
|
|
expect(err).not.toBeNull();
|
2021-09-25 17:08:00 -05:00
|
|
|
expect(err).toEqual(errorUtils.getInternalError('some error'));
|
2021-03-30 11:43:57 -05:00
|
|
|
done();
|
2020-10-23 15:05:46 -05:00
|
|
|
});
|
|
|
|
|
2021-03-30 11:43:57 -05:00
|
|
|
if (handler) {
|
|
|
|
handler.savePackage(pkgName, pkgExample, (err) => {
|
|
|
|
expect(err).toBeNull();
|
|
|
|
|
|
|
|
handler.updatePackage(
|
|
|
|
pkgName,
|
|
|
|
(json, callback) => {
|
|
|
|
expect(json).toBeDefined();
|
|
|
|
expect(json.name).toBe(pkgExample.name);
|
|
|
|
expect(callback).toBeDefined();
|
2021-09-25 17:08:00 -05:00
|
|
|
callback(errorUtils.getInternalError('some error'));
|
2021-03-30 11:43:57 -05:00
|
|
|
},
|
|
|
|
() => {},
|
|
|
|
// @ts-ignore
|
|
|
|
() => {},
|
|
|
|
onEnd
|
|
|
|
);
|
2020-10-23 15:05:46 -05:00
|
|
|
});
|
2021-03-30 11:43:57 -05:00
|
|
|
}
|
|
|
|
});
|
2020-10-23 15:05:46 -05:00
|
|
|
|
2021-03-30 11:43:57 -05:00
|
|
|
test('should onWrite update a package', (done) => {
|
|
|
|
const localMemory: IPluginStorage<ConfigMemory> = new LocalMemory(config, defaultConfig);
|
|
|
|
const pkgName = 'test';
|
2020-10-23 15:05:46 -05:00
|
|
|
|
2021-03-30 11:43:57 -05:00
|
|
|
const handler = localMemory.getPackageStorage(pkgName);
|
|
|
|
expect(handler).toBeDefined();
|
|
|
|
const onEnd = jest.fn((err) => {
|
|
|
|
expect(err).not.toBeNull();
|
2021-09-25 17:08:00 -05:00
|
|
|
expect(err).toEqual(errorUtils.getInternalError('error on parse the metadata'));
|
2021-03-30 11:43:57 -05:00
|
|
|
done();
|
2020-10-23 15:05:46 -05:00
|
|
|
});
|
|
|
|
|
2021-03-30 11:43:57 -05:00
|
|
|
if (handler) {
|
|
|
|
handler.savePackage(pkgName, pkgExample, (err) => {
|
|
|
|
expect(err).toBeNull();
|
|
|
|
|
|
|
|
handler.updatePackage(
|
|
|
|
pkgName,
|
|
|
|
(json, callback) => {
|
|
|
|
expect(json).toBeDefined();
|
|
|
|
expect(json.name).toBe(pkgExample.name);
|
|
|
|
expect(callback).toBeDefined();
|
|
|
|
callback(null);
|
|
|
|
},
|
|
|
|
(name, data, onEnd) => {
|
|
|
|
expect(name).toBe(pkgName);
|
|
|
|
expect(data.name).toBe(pkgExample.name);
|
|
|
|
onEnd();
|
|
|
|
expect(onEnd).toHaveBeenCalled();
|
|
|
|
done();
|
|
|
|
},
|
|
|
|
() => {
|
|
|
|
throw new Error('dadsads');
|
|
|
|
},
|
|
|
|
onEnd
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2020-10-23 15:05:46 -05:00
|
|
|
|
2021-03-30 11:43:57 -05:00
|
|
|
test('should delete a package', (done) => {
|
|
|
|
const localMemory: IPluginStorage<ConfigMemory> = new LocalMemory(config, defaultConfig);
|
|
|
|
const pkgName = 'test2';
|
2020-10-23 15:05:46 -05:00
|
|
|
|
2021-03-30 11:43:57 -05:00
|
|
|
const handler: IPackageStorage = localMemory.getPackageStorage(pkgName);
|
|
|
|
expect(handler).toBeDefined();
|
|
|
|
if (handler) {
|
|
|
|
handler.createPackage(pkgName, pkgExample, (err) => {
|
|
|
|
expect(err).toBeNull();
|
2021-09-08 12:06:37 -05:00
|
|
|
handler.deletePackage(pkgName).then((err) => {
|
|
|
|
expect(err).toBeUndefined();
|
2021-03-30 11:43:57 -05:00
|
|
|
handler.readPackage(pkgName, (err) => {
|
|
|
|
expect(err).not.toBeNull();
|
|
|
|
expect(err.message).toMatch(/no such package/);
|
2020-10-23 15:05:46 -05:00
|
|
|
done();
|
|
|
|
});
|
2021-03-30 11:43:57 -05:00
|
|
|
});
|
2020-10-23 15:05:46 -05:00
|
|
|
});
|
2021-03-30 11:43:57 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2020-10-23 15:05:46 -05:00
|
|
|
|
2021-03-30 11:43:57 -05:00
|
|
|
describe('writing files', () => {
|
|
|
|
test('should write a tarball', (done) => {
|
|
|
|
const localMemory: IPluginStorage<ConfigMemory> = new LocalMemory(config, defaultConfig);
|
|
|
|
const pkgName = 'test';
|
|
|
|
const dataTarball = '12345';
|
|
|
|
|
|
|
|
const handler = localMemory.getPackageStorage(pkgName);
|
|
|
|
if (handler) {
|
|
|
|
const stream = handler.writeTarball(pkgName);
|
|
|
|
stream.on('data', (data) => {
|
|
|
|
expect(data.toString()).toBe(dataTarball);
|
|
|
|
});
|
|
|
|
stream.on('open', () => {
|
|
|
|
stream.done();
|
|
|
|
stream.end();
|
|
|
|
});
|
|
|
|
stream.on('success', () => {
|
|
|
|
done();
|
|
|
|
});
|
2020-10-23 15:05:46 -05:00
|
|
|
|
2021-03-30 11:43:57 -05:00
|
|
|
stream.write(dataTarball);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
test('should abort while write a tarball', (done) => {
|
|
|
|
const localMemory: IPluginStorage<ConfigMemory> = new LocalMemory(config, defaultConfig);
|
|
|
|
const pkgName = 'test-abort.tar.gz';
|
|
|
|
const handler = localMemory.getPackageStorage(pkgName);
|
|
|
|
|
|
|
|
if (handler) {
|
|
|
|
const stream = handler.writeTarball(pkgName);
|
|
|
|
stream.on('error', (err) => {
|
|
|
|
expect(err).not.toBeNull();
|
|
|
|
expect(err.message).toMatch(/transmision aborted/);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
stream.on('open', () => {
|
|
|
|
stream.abort();
|
2020-10-23 15:05:46 -05:00
|
|
|
});
|
2021-03-30 11:43:57 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2020-10-23 15:05:46 -05:00
|
|
|
|
2021-03-30 11:43:57 -05:00
|
|
|
describe('reading files', () => {
|
|
|
|
test('should read a tarball', (done) => {
|
|
|
|
const localMemory: IPluginStorage<ConfigMemory> = new LocalMemory(config, defaultConfig);
|
|
|
|
const pkgName = 'test.tar.gz';
|
|
|
|
const dataTarball = '12345';
|
2020-10-23 15:05:46 -05:00
|
|
|
|
2021-03-30 11:43:57 -05:00
|
|
|
const handler = localMemory.getPackageStorage(pkgName);
|
2020-10-23 15:05:46 -05:00
|
|
|
|
2021-03-30 11:43:57 -05:00
|
|
|
if (handler) {
|
|
|
|
const stream = handler.writeTarball(pkgName);
|
|
|
|
stream.on('open', () => {
|
|
|
|
stream.done();
|
|
|
|
stream.end();
|
2020-10-23 15:05:46 -05:00
|
|
|
});
|
2021-03-30 11:43:57 -05:00
|
|
|
stream.on('success', () => {
|
|
|
|
const readStream = handler.readTarball(pkgName);
|
|
|
|
readStream.on('data', (data) => {
|
|
|
|
expect(data.toString()).toBe(dataTarball);
|
|
|
|
done();
|
|
|
|
});
|
2020-10-23 15:05:46 -05:00
|
|
|
});
|
2021-03-30 11:43:57 -05:00
|
|
|
stream.write(dataTarball);
|
|
|
|
}
|
|
|
|
});
|
2020-10-23 15:05:46 -05:00
|
|
|
|
2021-03-30 11:43:57 -05:00
|
|
|
test('should abort read a tarball', (done) => {
|
|
|
|
const localMemory: IPluginStorage<ConfigMemory> = new LocalMemory(config, defaultConfig);
|
|
|
|
const pkgName = 'test2.tar.gz';
|
|
|
|
const dataTarball = '12345';
|
2020-10-23 15:05:46 -05:00
|
|
|
|
2021-03-30 11:43:57 -05:00
|
|
|
const handler = localMemory.getPackageStorage(pkgName);
|
2020-10-23 15:05:46 -05:00
|
|
|
|
2021-03-30 11:43:57 -05:00
|
|
|
if (handler) {
|
|
|
|
const stream = handler.writeTarball(pkgName);
|
|
|
|
stream.on('open', () => {
|
|
|
|
stream.done();
|
|
|
|
stream.end();
|
|
|
|
});
|
|
|
|
stream.on('success', () => {
|
|
|
|
const readStream = handler.readTarball(pkgName);
|
|
|
|
readStream.on('data', () => {
|
|
|
|
readStream.abort();
|
|
|
|
});
|
|
|
|
readStream.on('error', (err) => {
|
|
|
|
expect(err).not.toBeNull();
|
|
|
|
expect(err.message).toMatch(/read has been aborted/);
|
|
|
|
done();
|
|
|
|
});
|
2020-10-23 15:05:46 -05:00
|
|
|
});
|
2021-03-30 11:43:57 -05:00
|
|
|
stream.write(dataTarball);
|
|
|
|
}
|
|
|
|
});
|
2020-10-23 15:05:46 -05:00
|
|
|
|
2021-03-30 11:43:57 -05:00
|
|
|
test('should fails read a tarball not found', (done) => {
|
|
|
|
const localMemory: IPluginStorage<ConfigMemory> = new LocalMemory(config, defaultConfig);
|
|
|
|
const pkgName = 'test2.tar.gz';
|
|
|
|
const handler = localMemory.getPackageStorage(pkgName);
|
|
|
|
|
|
|
|
if (handler) {
|
|
|
|
const readStream = handler.readTarball('not-found');
|
|
|
|
readStream.on('error', (err) => {
|
|
|
|
expect(err).not.toBeNull();
|
|
|
|
expect(err.message).toMatch(/no such package/);
|
|
|
|
done();
|
2020-10-23 15:05:46 -05:00
|
|
|
});
|
2021-03-30 11:43:57 -05:00
|
|
|
}
|
2020-10-23 15:05:46 -05:00
|
|
|
});
|
|
|
|
});
|