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:
parent
102fa22507
commit
848e004eb5
7 changed files with 274 additions and 265 deletions
|
@ -16,7 +16,7 @@
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@verdaccio/file-locking": "0.0.5",
|
"@verdaccio/file-locking": "0.0.5",
|
||||||
"@verdaccio/local-storage": "0.4.2",
|
"@verdaccio/local-storage": "1.0.1",
|
||||||
"@verdaccio/streams": "1.0.0",
|
"@verdaccio/streams": "1.0.0",
|
||||||
"JSONStream": "1.3.2",
|
"JSONStream": "1.3.2",
|
||||||
"async": "2.6.0",
|
"async": "2.6.0",
|
||||||
|
@ -51,7 +51,7 @@
|
||||||
"@commitlint/cli": "6.1.3",
|
"@commitlint/cli": "6.1.3",
|
||||||
"@commitlint/config-conventional": "6.1.3",
|
"@commitlint/config-conventional": "6.1.3",
|
||||||
"@commitlint/travis-cli": "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-cli": "6.26.0",
|
||||||
"babel-core": "6.26.0",
|
"babel-core": "6.26.0",
|
||||||
"babel-eslint": "8.2.2",
|
"babel-eslint": "8.2.2",
|
||||||
|
|
|
@ -8,7 +8,7 @@ import type {IAuth, $ResponseExtend, $RequestExtend, $NextFunctionVer, IStorageH
|
||||||
function addSearchWebApi(route: Router, storage: IStorageHandler, auth: IAuth) {
|
function addSearchWebApi(route: Router, storage: IStorageHandler, auth: IAuth) {
|
||||||
// Search package
|
// Search package
|
||||||
route.get('/search/:anything', function(req: $RequestExtend, res: $ResponseExtend, next: $NextFunctionVer) {
|
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 packages = [];
|
||||||
|
|
||||||
const getPackageInfo = function(i) {
|
const getPackageInfo = function(i) {
|
||||||
|
|
|
@ -98,12 +98,12 @@ class LocalStorage implements IStorage {
|
||||||
|
|
||||||
data = normalizePackage(data);
|
data = normalizePackage(data);
|
||||||
|
|
||||||
const removeFailed = this.localData.remove(name);
|
this.localData.remove(name, (removeFailed) => {
|
||||||
|
|
||||||
if (removeFailed) {
|
if (removeFailed) {
|
||||||
// This will happen when database is locked
|
// This will happen when database is locked
|
||||||
return callback(ErrorCode.get422(removeFailed.message));
|
return callback(ErrorCode.get422(removeFailed.message));
|
||||||
}
|
}
|
||||||
|
|
||||||
storage.deletePackage(pkgFileName, (err) => {
|
storage.deletePackage(pkgFileName, (err) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
return callback(err);
|
return callback(err);
|
||||||
|
@ -113,6 +113,7 @@ class LocalStorage implements IStorage {
|
||||||
this._deleteAttachments(storage, attachments, callback);
|
this._deleteAttachments(storage, attachments, callback);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -254,12 +255,14 @@ class LocalStorage implements IStorage {
|
||||||
data.versions[version] = metadata;
|
data.versions[version] = metadata;
|
||||||
tagVersion(data, version, tag);
|
tagVersion(data, version, tag);
|
||||||
|
|
||||||
let addFailed = this.localData.add(name);
|
this.localData.add(name, (addFailed) => {
|
||||||
if (addFailed) {
|
if (addFailed) {
|
||||||
return cb(ErrorCode.get422(addFailed.message));
|
return cb(ErrorCode.get422(addFailed.message));
|
||||||
}
|
}
|
||||||
|
|
||||||
cb();
|
cb();
|
||||||
|
});
|
||||||
|
|
||||||
}, callback);
|
}, callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -33,8 +33,10 @@ class Search implements IWebSearch {
|
||||||
*/
|
*/
|
||||||
query(query: string) {
|
query(query: string) {
|
||||||
return query === '*'
|
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};
|
return {ref: pkg, score: 1};
|
||||||
|
});
|
||||||
}) : this.index.search(query);
|
}) : this.index.search(query);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -495,25 +495,28 @@ class Storage implements IStorageHandler {
|
||||||
*/
|
*/
|
||||||
getLocalDatabase(callback: Callback) {
|
getLocalDatabase(callback: Callback) {
|
||||||
let self = this;
|
let self = this;
|
||||||
let locals = this.localStorage.localData.get();
|
this.localStorage.localData.get((err, locals) => {
|
||||||
let packages = [];
|
if (err) {
|
||||||
|
callback(err);
|
||||||
|
}
|
||||||
|
|
||||||
const getPackage = function(i) {
|
let packages = [];
|
||||||
self.localStorage.getPackageMetadata(locals[i], function(err, info) {
|
const getPackage = function(itemPkg) {
|
||||||
|
self.localStorage.getPackageMetadata(locals[itemPkg], function(err, info) {
|
||||||
if (_.isNil(err)) {
|
if (_.isNil(err)) {
|
||||||
const latest = info[DIST_TAGS].latest;
|
const latest = info[DIST_TAGS].latest;
|
||||||
|
|
||||||
if (latest && info.versions[latest]) {
|
if (latest && info.versions[latest]) {
|
||||||
packages.push(info.versions[latest]);
|
packages.push(info.versions[latest]);
|
||||||
} else {
|
} 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);
|
callback(null, packages);
|
||||||
} else {
|
} else {
|
||||||
getPackage(i + 1);
|
getPackage(itemPkg + 1);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
@ -523,6 +526,7 @@ class Storage implements IStorageHandler {
|
||||||
} else {
|
} else {
|
||||||
callback(null, []);
|
callback(null, []);
|
||||||
}
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -34,7 +34,7 @@ describe('LocalStorage', () => {
|
||||||
expect(storage).toBeDefined();
|
expect(storage).toBeDefined();
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('LocalStorage::addPackage', () => {
|
describe('LocalStorage::preparePackage', () => {
|
||||||
test('should add a package', (done) => {
|
test('should add a package', (done) => {
|
||||||
const metadata = JSON.parse(readMetadata());
|
const metadata = JSON.parse(readMetadata());
|
||||||
// $FlowFixMe
|
// $FlowFixMe
|
||||||
|
@ -69,14 +69,13 @@ describe('LocalStorage', () => {
|
||||||
test('should fails on add a package', (done) => {
|
test('should fails on add a package', (done) => {
|
||||||
const metadata = JSON.parse(readMetadata());
|
const metadata = JSON.parse(readMetadata());
|
||||||
|
|
||||||
storage.addPackage(pkgName, metadata, (err, data) => {
|
storage.addPackage(pkgName, metadata, (err) => {
|
||||||
expect(err).not.toBeNull();
|
expect(err).not.toBeNull();
|
||||||
expect(err.statusCode).toEqual(409);
|
expect(err.statusCode).toEqual(409);
|
||||||
// expect(err.status).toMatch(/this package is already present/);
|
expect(err.message).toMatch(/this package is already present/);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
|
||||||
describe('LocalStorage::addVersion', () => {
|
describe('LocalStorage::addVersion', () => {
|
||||||
test('should add new version without tag', (done) => {
|
test('should add new version without tag', (done) => {
|
||||||
|
@ -327,5 +326,6 @@ describe('LocalStorage', () => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
BIN
yarn.lock
BIN
yarn.lock
Binary file not shown.
Loading…
Reference in a new issue