0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2025-01-06 22:40:26 -05:00

refactor: local database methods are async

This commit is contained in:
Juan Picado @jotadeveloper 2018-04-20 19:40:48 +02:00
parent 102fa22507
commit 848e004eb5
No known key found for this signature in database
GPG key ID: 18AC54485952D158
7 changed files with 274 additions and 265 deletions

View file

@ -16,7 +16,7 @@
},
"dependencies": {
"@verdaccio/file-locking": "0.0.5",
"@verdaccio/local-storage": "0.4.2",
"@verdaccio/local-storage": "1.0.1",
"@verdaccio/streams": "1.0.0",
"JSONStream": "1.3.2",
"async": "2.6.0",
@ -51,7 +51,7 @@
"@commitlint/cli": "6.1.3",
"@commitlint/config-conventional": "6.1.3",
"@commitlint/travis-cli": "6.1.3",
"@verdaccio/types": "2.0.5",
"@verdaccio/types": "2.1.0",
"babel-cli": "6.26.0",
"babel-core": "6.26.0",
"babel-eslint": "8.2.2",

View file

@ -8,7 +8,7 @@ import type {IAuth, $ResponseExtend, $RequestExtend, $NextFunctionVer, IStorageH
function addSearchWebApi(route: Router, storage: IStorageHandler, auth: IAuth) {
// Search package
route.get('/search/:anything', function(req: $RequestExtend, res: $ResponseExtend, next: $NextFunctionVer) {
const results = Search.query(req.params.anything);
const results: any = Search.query(req.params.anything);
const packages = [];
const getPackageInfo = function(i) {

View file

@ -98,12 +98,12 @@ class LocalStorage implements IStorage {
data = normalizePackage(data);
const removeFailed = this.localData.remove(name);
this.localData.remove(name, (removeFailed) => {
if (removeFailed) {
// This will happen when database is locked
return callback(ErrorCode.get422(removeFailed.message));
}
storage.deletePackage(pkgFileName, (err) => {
if (err) {
return callback(err);
@ -113,6 +113,7 @@ class LocalStorage implements IStorage {
this._deleteAttachments(storage, attachments, callback);
});
});
});
}
/**
@ -254,12 +255,14 @@ class LocalStorage implements IStorage {
data.versions[version] = metadata;
tagVersion(data, version, tag);
let addFailed = this.localData.add(name);
this.localData.add(name, (addFailed) => {
if (addFailed) {
return cb(ErrorCode.get422(addFailed.message));
}
cb();
});
}, callback);
}

View file

@ -33,8 +33,10 @@ class Search implements IWebSearch {
*/
query(query: string) {
return query === '*'
? this.storage.localStorage.localData.get().map( function( pkg ) {
? this.storage.localStorage.localData.get((items) => {
items.map( function( pkg ) {
return {ref: pkg, score: 1};
});
}) : this.index.search(query);
}

View file

@ -495,25 +495,28 @@ class Storage implements IStorageHandler {
*/
getLocalDatabase(callback: Callback) {
let self = this;
let locals = this.localStorage.localData.get();
let packages = [];
this.localStorage.localData.get((err, locals) => {
if (err) {
callback(err);
}
const getPackage = function(i) {
self.localStorage.getPackageMetadata(locals[i], function(err, info) {
let packages = [];
const getPackage = function(itemPkg) {
self.localStorage.getPackageMetadata(locals[itemPkg], function(err, info) {
if (_.isNil(err)) {
const latest = info[DIST_TAGS].latest;
if (latest && info.versions[latest]) {
packages.push(info.versions[latest]);
} else {
self.logger.warn( {package: locals[i]}, 'package @{package} does not have a "latest" tag?' );
self.logger.warn( {package: locals[itemPkg]}, 'package @{package} does not have a "latest" tag?' );
}
}
if (i >= locals.length - 1) {
if (itemPkg >= locals.length - 1) {
callback(null, packages);
} else {
getPackage(i + 1);
getPackage(itemPkg + 1);
}
});
};
@ -523,6 +526,7 @@ class Storage implements IStorageHandler {
} else {
callback(null, []);
}
});
}
/**

View file

@ -34,7 +34,7 @@ describe('LocalStorage', () => {
expect(storage).toBeDefined();
});
describe('LocalStorage::addPackage', () => {
describe('LocalStorage::preparePackage', () => {
test('should add a package', (done) => {
const metadata = JSON.parse(readMetadata());
// $FlowFixMe
@ -69,14 +69,13 @@ describe('LocalStorage', () => {
test('should fails on add a package', (done) => {
const metadata = JSON.parse(readMetadata());
storage.addPackage(pkgName, metadata, (err, data) => {
storage.addPackage(pkgName, metadata, (err) => {
expect(err).not.toBeNull();
expect(err.statusCode).toEqual(409);
// expect(err.status).toMatch(/this package is already present/);
expect(err.message).toMatch(/this package is already present/);
done();
});
});
});
describe('LocalStorage::addVersion', () => {
test('should add new version without tag', (done) => {
@ -327,5 +326,6 @@ describe('LocalStorage', () => {
});
});
});
});
});

BIN
yarn.lock

Binary file not shown.