1
0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-11-08 23:25:51 +01:00

#103 Remove Search references from local storage

This commit is contained in:
Juan Picado @jotadeveloper 2017-05-25 15:40:47 +02:00
parent fe73eba54e
commit e9929c23e1
No known key found for this signature in database
GPG Key ID: 18AC54485952D158
3 changed files with 71 additions and 61 deletions

@ -14,7 +14,6 @@ const URL = require('url');
const fs_storage = require('./local-fs');
const LocalData = require('./local-data');
const Logger = require('./logger');
const Search = require('./search');
const MyStreams = require('./streams');
const Utils = require('./utils');
const info_file = 'package.json';
@ -61,20 +60,21 @@ class Storage {
* @param {*} callback
* @return {Function}
*/
add_package(name, info, callback) {
let storage = this.storage(name);
if (!storage) return callback( Error[404]('this package cannot be added') );
addPackage(name, info, callback) {
const storage = this.storage(name);
if (!storage) {
return callback( Error[404]('this package cannot be added'));
}
storage.create_json(info_file, get_boilerplate(name), function(err) {
if (err && err.code === 'EEXISTS') {
return callback( Error[409]('this package is already present') );
}
let latest = info['dist-tags'].latest;
const latest = info['dist-tags'].latest;
if (latest && info.versions[latest]) {
Search.add(info.versions[latest]);
return callback(null, info.versions[latest]);
}
callback();
return callback();
});
}
@ -84,7 +84,7 @@ class Storage {
* @param {*} callback
* @return {Function}
*/
remove_package(name, callback) {
removePackage(name, callback) {
this.logger.info( {name: name}
, 'unpublishing @{name} (all)');
@ -101,7 +101,7 @@ class Storage {
return callback(err);
}
}
this._normalize_package(data);
this._normalizePackage(data);
storage.unlink(info_file, function(err) {
if (err) {
@ -129,8 +129,6 @@ class Storage {
});
});
});
Search.remove(name);
this.localList.remove(name);
}
@ -140,8 +138,8 @@ class Storage {
* @param {*} newdata
* @param {*} callback
*/
update_versions(name, newdata, callback) {
this._read_create_package(name, (err, data) => {
updateVersions(name, newdata, callback) {
this._readCreatePackage(name, (err, data) => {
if (err) {
return callback(err);
}
@ -209,7 +207,7 @@ class Storage {
if (change) {
this.logger.debug('updating package info');
this._write_package(name, data, function(err) {
this._writePackage(name, data, function(err) {
callback(err, data);
});
} else {
@ -226,8 +224,8 @@ class Storage {
* @param {*} tag
* @param {*} callback
*/
add_version(name, version, metadata, tag, callback) {
this.update_package(name, (data, cb) => {
addVersion(name, version, metadata, tag, callback) {
this._updatePackage(name, (data, cb) => {
// keep only one readme per package
data.readme = metadata.readme;
delete metadata.readme;
@ -264,8 +262,8 @@ class Storage {
* @param {*} tags
* @param {*} callback
*/
merge_tags(name, tags, callback) {
this.update_package(name, function updater(data, cb) {
mergeTags(name, tags, callback) {
this._updatePackage(name, function updater(data, cb) {
for (let t in tags) {
if (tags[t] === null) {
delete data['dist-tags'][t];
@ -288,8 +286,8 @@ class Storage {
* @param {*} tags
* @param {*} callback
*/
replace_tags(name, tags, callback) {
this.update_package(name, function updater(data, cb) {
replaceTags(name, tags, callback) {
this._updatePackage(name, function updater(data, cb) {
data['dist-tags'] = {};
for (let t in tags) {
@ -317,12 +315,12 @@ class Storage {
* @param {*} callback
* @return {Function}
*/
change_package(name, metadata, revision, callback) {
changePackage(name, metadata, revision, callback) {
if (!Utils.is_object(metadata.versions) || !Utils.is_object(metadata['dist-tags'])) {
return callback( Error[422]('bad data') );
}
this.update_package(name, (data, cb) => {
this._updatePackage(name, (data, cb) => {
for (let ver in data.versions) {
if (metadata.versions[ver] == null) {
this.logger.info( {name: name, version: ver},
@ -352,10 +350,10 @@ class Storage {
* @param {*} revision
* @param {*} callback
*/
remove_tarball(name, filename, revision, callback) {
removeTarball(name, filename, revision, callback) {
assert(Utils.validate_name(filename));
this.update_package(name, (data, cb) => {
this._updatePackage(name, (data, cb) => {
if (data._attachments[filename]) {
delete data._attachments[filename];
cb();
@ -379,7 +377,7 @@ class Storage {
* @param {String} filename
* @return {Function}
*/
add_tarball(name, filename) {
addTarball(name, filename) {
assert(Utils.validate_name(filename));
let stream = MyStreams.uploadTarballStream();
@ -417,7 +415,7 @@ class Storage {
stream.emit('error', Error[409]('this tarball is already present'));
} else if (err.code === 'ENOENT') {
// check if package exists to throw an appropriate message
this.get_package(name, function(_err, res) {
this.getPackage(name, function(_err, res) {
if (_err) {
stream.emit('error', _err);
} else {
@ -434,7 +432,7 @@ class Storage {
stream.emit('open');
});
wstream.on('success', () => {
this.update_package(name, function updater(data, cb) {
this._updatePackage(name, function updater(data, cb) {
data._attachments[filename] = {
shasum: shasum.digest('hex'),
};
@ -470,7 +468,7 @@ class Storage {
* @param {*} callback
* @return {Function}
*/
get_tarball(name, filename, callback) {
getTarball(name, filename, callback) {
assert(Utils.validate_name(filename));
const stream = MyStreams.readTarballStream();
stream.abort = function() {
@ -513,7 +511,7 @@ class Storage {
* @param {*} callback
* @return {Function}
*/
get_package(name, options, callback) {
getPackage(name, options, callback) {
if (typeof(options) === 'function') {
callback = options, options = {};
}
@ -528,10 +526,10 @@ class Storage {
if (err.code === 'ENOENT') {
return callback( Error[404]('no such package available') );
} else {
return callback(this._internal_error(err, info_file, 'error reading'));
return callback(this._internalError(err, info_file, 'error reading'));
}
}
this._normalize_package(result);
this._normalizePackage(result);
callback(err, result);
});
}
@ -550,7 +548,7 @@ class Storage {
* @param {*} _callback callback that gets invoked after it's all updated
* @return {Function}
*/
update_package(name, updateFn, _callback) {
_updatePackage(name, updateFn, _callback) {
const storage = this.storage(name);
if (!storage) {
return _callback( Error[404]('no such package available') );
@ -585,12 +583,12 @@ class Storage {
}
}
this._normalize_package(json);
this._normalizePackage(json);
updateFn(json, (err) => {
if (err) {
return callback(err);
}
this._write_package(name, json, callback);
this._writePackage(name, json, callback);
});
});
}
@ -604,14 +602,14 @@ class Storage {
search(startKey, options) {
const stream = new Stream.PassThrough({objectMode: true});
this._each_package((item, cb) => {
this._eachPackage((item, cb) => {
fs.stat(item.path, (err, stats) => {
if (err) {
return cb(err);
}
if (stats.mtime > startKey) {
this.get_package(item.name, options, function(err, data) {
this.getPackage(item.name, options, function(err, data) {
if (err) {
return cb(err);
}
@ -682,7 +680,7 @@ class Storage {
* @param {*} on_package
* @param {*} on_end
*/
_each_package(on_package, on_end) {
_eachPackage(on_package, on_end) {
let storages = {};
storages[this.config.storage] = true;
@ -737,7 +735,7 @@ class Storage {
* Normalise package properties, tags, revision id.
* @param {Object} pkg package reference.
*/
_normalize_package(pkg) {
_normalizePackage(pkg) {
['versions', 'dist-tags', '_distfiles', '_attachments', '_uplinks'].forEach(function(key) {
if (!Utils.is_object(pkg[key])) {
pkg[key] = {};
@ -756,11 +754,11 @@ class Storage {
* @param {*} callback
* @return {Function}
*/
_read_create_package(name, callback) {
_readCreatePackage(name, callback) {
const storage = this.storage(name);
if (!storage) {
let data = get_boilerplate(name);
this._normalize_package(data);
this._normalizePackage(data);
return callback(null, data);
}
storage.read_json(info_file, (err, data) => {
@ -770,10 +768,10 @@ class Storage {
// if package doesn't exist, we create it here
data = get_boilerplate(name);
} else {
return callback(this._internal_error(err, info_file, 'error reading'));
return callback(this._internalError(err, info_file, 'error reading'));
}
}
this._normalize_package(data);
this._normalizePackage(data);
callback(null, data);
});
}
@ -785,7 +783,7 @@ class Storage {
* @param {*} message
* @return {Object} Error instance
*/
_internal_error(err, file, message) {
_internalError(err, file, message) {
this.logger.error( {err: err, file: file},
message + ' @{file}: @{!err.message}' );
return Error[500]();
@ -798,7 +796,7 @@ class Storage {
* @param {*} callback
* @return {Function}
*/
_write_package(name, json, callback) {
_writePackage(name, json, callback) {
// calculate revision a la couchdb
if (typeof(json._rev) !== 'string') {
json._rev = '0-0000000000000000';

@ -5,7 +5,9 @@ const async = require('async');
const Error = require('http-errors');
const semver = require('semver');
const Crypto = require('crypto');
const _ = require('lodash');
const Stream = require('stream');
const Search = require('./search');
const LocalStorage = require('./local-storage');
const Logger = require('./logger');
const MyStreams = require('./streams');
@ -84,7 +86,7 @@ class Storage {
* @param {*} cb the callback method
*/
function check_package_local(cb) {
self.localStorage.get_package(name, {}, function(err, results) {
self.localStorage.getPackage(name, {}, function(err, results) {
if (err && err.status !== 404) {
return cb(err);
}
@ -128,7 +130,14 @@ class Storage {
* @param {*} cb callback method
*/
function publish_package(cb) {
self.localStorage.add_package(name, metadata, callback);
self.localStorage.addPackage(name, metadata, (err, latest) => {
if (!_.isNull(err)) {
return cb(err);
} else if (!_.isUndefined(latest)) {
Search.add(latest);
}
return cb();
});
}
}
@ -142,7 +151,7 @@ class Storage {
* @param {*} callback
*/
add_version(name, version, metadata, tag, callback) {
this.localStorage.add_version(name, version, metadata, tag, callback);
this.localStorage.addVersion(name, version, metadata, tag, callback);
}
/**
@ -153,7 +162,7 @@ class Storage {
* @param {*} callback
*/
merge_tags(name, tag_hash, callback) {
this.localStorage.merge_tags(name, tag_hash, callback);
this.localStorage.mergeTags(name, tag_hash, callback);
}
/**
@ -164,7 +173,7 @@ class Storage {
* @param {*} callback
*/
replace_tags(name, tag_hash, callback) {
this.localStorage.replace_tags(name, tag_hash, callback);
this.localStorage.replaceTags(name, tag_hash, callback);
}
/**
@ -177,7 +186,7 @@ class Storage {
* @param {*} callback
*/
change_package(name, metadata, revision, callback) {
this.localStorage.change_package(name, metadata, revision, callback);
this.localStorage.changePackage(name, metadata, revision, callback);
}
/**
@ -188,7 +197,9 @@ class Storage {
* @param {*} callback
*/
remove_package(name, callback) {
this.localStorage.remove_package(name, callback);
this.localStorage.removePackage(name, callback);
// update the indexer
Search.remove(name);
}
/**
@ -203,7 +214,7 @@ class Storage {
* @param {*} callback
*/
remove_tarball(name, filename, revision, callback) {
this.localStorage.remove_tarball(name, filename, revision, callback);
this.localStorage.removeTarball(name, filename, revision, callback);
}
/**
@ -215,7 +226,7 @@ class Storage {
* @return {Stream}
*/
add_tarball(name, filename) {
return this.localStorage.add_tarball(name, filename);
return this.localStorage.addTarball(name, filename);
}
/**
@ -238,7 +249,7 @@ class Storage {
// information about it, so fetching package info is unnecessary
// trying local first
let rstream = self.localStorage.get_tarball(name, filename);
let rstream = self.localStorage.getTarball(name, filename);
let is_open = false;
rstream.on('error', function(err) {
if (is_open || err.status !== 404) {
@ -249,7 +260,7 @@ class Storage {
let err404 = err;
rstream.abort();
rstream = null; // gc
self.localStorage.get_package(name, function(err, info) {
self.localStorage.getPackage(name, function(err, info) {
if (!err && info._distfiles && info._distfiles[filename] != null) {
// information about this file exists locally
serve_file(info._distfiles[filename]);
@ -296,7 +307,7 @@ class Storage {
}
let savestream = null;
if (uplink.config.cache) {
savestream = self.localStorage.add_tarball(name, filename);
savestream = self.localStorage.addTarball(name, filename);
}
let on_open = function() {
// prevent it from being called twice
@ -349,7 +360,7 @@ class Storage {
/**
Retrieve a package metadata for {name} package
Function invokes local.get_package and uplink.get_package for every
Function invokes localStorage.getPackage and uplink.get_package for every
uplink with proxy_access rights against {name} and combines results
into one json object
Used storages: local && uplink (proxy_access)
@ -362,7 +373,7 @@ class Storage {
callback = options, options = {};
}
this.localStorage.get_package(name, options, (err, data) => {
this.localStorage.getPackage(name, options, (err, data) => {
if (err && (!err.status || err.status >= 500)) {
// report internal errors right away
return callback(err);
@ -453,7 +464,7 @@ class Storage {
let packages = [];
const getPackage = function(i) {
self.localStorage.get_package(locals[i], function(err, info) {
self.localStorage.getPackage(locals[i], function(err, info) {
if (!err) {
let latest = info['dist-tags'].latest;
if (latest && info.versions[latest]) {
@ -581,7 +592,7 @@ class Storage {
, uplink_errors );
}
self.localStorage.update_versions(name, pkginfo, function(err, pkginfo) {
self.localStorage.updateVersions(name, pkginfo, function(err, pkginfo) {
if (err) return callback(err);
return callback(null, pkginfo, uplink_errors);
});

@ -31,6 +31,7 @@
"jju": "^1.3.0",
"js-yaml": "^3.6.0",
"lockfile": "^1.0.1",
"lodash": "^4.17.4",
"lunr": "^0.7.0",
"minimatch": "^3.0.2",
"mkdirp": "^0.5.1",