0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2025-01-20 22:52:46 -05:00

refactor: merge time prop instead overwrite it

remove dead code
This commit is contained in:
Juan Picado @jotadeveloper 2018-06-09 17:13:38 +02:00
parent 2208b4ef51
commit ee62559615
No known key found for this signature in database
GPG key ID: 18AC54485952D158
4 changed files with 21 additions and 94 deletions

View file

@ -11,7 +11,6 @@ import type {IStorage} from '../../types';
const pkgFileName = 'package.json';
const fileExist: string = 'EEXISTS';
const noSuchFile: string = 'ENOENT';
const resourceNotAvailable: string = 'EAGAIN';
const DEFAULT_REVISION: string = `0-0000000000000000`;
const generatePackageTemplate = function(name: string): Package {
@ -19,11 +18,11 @@ const generatePackageTemplate = function(name: string): Package {
// standard things
name,
versions: {},
'dist-tags': {},
time: {},
[DIST_TAGS]: {},
_uplinks: {},
_distfiles: {},
_attachments: {},
_uplinks: {},
_rev: '',
};
};
@ -179,6 +178,14 @@ export function checkPackageRemote(name: string, isAllowPublishOffline: boolean,
});
}
export function mergeTime(localMetadata: Package, remoteMetadata: Package) {
if ('time' in remoteMetadata) {
return Object.assign({}, localMetadata.time, remoteMetadata.time);
}
return localMetadata.time;
}
export function prepareSearchPackage(data: Package, time: mixed) {
const listVersions: Array<string> = Object.keys(data.versions);
const versions: Array<string> = semverSort(listVersions);
@ -189,7 +196,7 @@ export function prepareSearchPackage(data: Package, time: mixed) {
const pkg: any = {
name: version.name,
description: version.description,
'dist-tags': {latest},
[DIST_TAGS]: {latest},
maintainers: version.maintainers || [version.author].filter(Boolean),
author: version.author,
repository: version.repository,
@ -218,6 +225,5 @@ export {
fileExist,
noSuchFile,
pkgFileName,
resourceNotAvailable,
};

View file

@ -8,7 +8,8 @@ import ProxyStorage from './up-storage';
import Search from './search';
import LocalStorage from './local-storage';
import {ReadTarball} from '@verdaccio/streams';
import {checkPackageLocal, publishPackage, checkPackageRemote, cleanUpLinksRef} from './storage-utils';
import {checkPackageLocal, publishPackage, checkPackageRemote, cleanUpLinksRef,
mergeTime, generatePackageTemplate} from './storage-utils';
import {setupUpLinks, updateVersionsHiddenUpLink} from './uplink-util';
import {mergeVersions} from './metadata-utils';
import {ErrorCode, normalizeDistTags, validate_metadata, isObject, DIST_TAGS} from './utils';
@ -26,19 +27,6 @@ import type {
import type {IReadTarball, IUploadTarball} from '@verdaccio/streams';
const LoggerApi = require('../lib/logger');
const getDefaultMetadata = function(name): Package {
const pkgMetadata: Package = {
name,
versions: {},
'dist-tags': {},
_uplinks: {},
_distfiles: {},
_attachments: {},
_rev: '',
};
return pkgMetadata;
};
class Storage implements IStorageHandler {
localStorage: IStorage;
@ -417,9 +405,10 @@ class Storage implements IStorageHandler {
let exists = true;
const self = this;
const upLinks = [];
if (!packageInfo || packageInfo === null) {
exists = false;
packageInfo = getDefaultMetadata(name);
packageInfo = generatePackageTemplate(name);
}
for (let up in this.uplinks) {
@ -467,10 +456,7 @@ class Storage implements IStorageHandler {
fetched: Date.now(),
};
// added to fix verdaccio#73
if ('time' in upLinkResponse) {
packageInfo.time = upLinkResponse.time;
}
packageInfo.time = mergeTime(packageInfo, upLinkResponse);
updateVersionsHiddenUpLink(upLinkResponse.versions, upLink);

View file

@ -1,10 +1,7 @@
// @flow
import {ErrorCode, isObject, validate_metadata} from './utils';
import ProxyStorage from './up-storage';
import {mergeVersions} from './metadata-utils';
import type {Package, Versions, Config, Logger} from '@verdaccio/types';
import type {Versions, Config} from '@verdaccio/types';
import type {IProxy, ProxyList} from '../../types';
/**
@ -37,69 +34,3 @@ export function updateVersionsHiddenUpLink(versions: Versions, upLink: IProxy) {
}
}
}
export function fetchUplinkMetadata(name: string, packageInfo: Package,
options: any, upLink: any, logger: Logger): Promise<any> {
return new Promise(function(resolve, reject) {
const _options = Object.assign({}, options);
const upLinkMeta = packageInfo._uplinks[upLink.upname];
if (isObject(upLinkMeta)) {
const fetched = upLinkMeta.fetched;
// check whether is too soon to ask for metadata
if (fetched && (Date.now() - fetched) < upLink.maxage) {
return resolve(false);
}
_options.etag = upLinkMeta.etag;
}
upLink.getRemoteMetadata(name, _options, function handleUplinkMetadataResponse(err, upLinkResponse, eTag) {
if (err && err.remoteStatus === 304) {
upLinkMeta.fetched = Date.now();
}
if (err || !upLinkResponse) {
// $FlowFixMe
return reject(err || ErrorCode.get500('no data'));
}
try {
validate_metadata(upLinkResponse, name);
} catch (err) {
logger.error({
sub: 'out',
err: err,
}, 'package.json validating error @{!err.message}\n@{err.stack}');
return reject(err);
}
packageInfo._uplinks[upLink.upname] = {
etag: eTag,
fetched: Date.now(),
};
// added to fix verdaccio#73
if ('time' in upLinkResponse) {
packageInfo.time = upLinkResponse.time;
}
updateVersionsHiddenUpLink(upLinkResponse.versions, upLink);
try {
mergeVersions(packageInfo, upLinkResponse);
} catch (err) {
logger.error({
sub: 'out',
err: err,
}, 'package.json parsing error @{!err.message}\n@{err.stack}');
return reject(err);
}
// if we got to this point, assume that the correct package exists
// on the uplink
resolve(true);
});
});
}

View file

@ -81,6 +81,10 @@ function validate_metadata(object: Package, name: string) {
object['versions'] = {};
}
if (!isObject(object['time'])) {
object['time'] = {};
}
return object;
}