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

refactor: clean up ref method

This commit is contained in:
Juan Picado @jotadeveloper 2018-04-24 07:28:06 +02:00
parent 4d5e8aab0b
commit c1bc2610c4
No known key found for this signature in database
GPG key ID: 18AC54485952D158
4 changed files with 32 additions and 31 deletions

View file

@ -2,7 +2,7 @@
import _ from 'lodash'; import _ from 'lodash';
import crypto from 'crypto'; import crypto from 'crypto';
import {ErrorCode, isObject, normalize_dist_tags} from './utils'; import {ErrorCode, isObject, normalizeDistTags, DIST_TAGS} from './utils';
import Search from './search'; import Search from './search';
import type {Package, Version} from '@verdaccio/types'; import type {Package, Version} from '@verdaccio/types';
@ -52,7 +52,7 @@ function normalizePackage(pkg: Package) {
} }
// normalize dist-tags // normalize dist-tags
normalize_dist_tags(pkg); normalizeDistTags(pkg);
return pkg; return pkg;
} }
@ -71,6 +71,23 @@ function cleanUpReadme(version: Version): Version {
return version; return version;
} }
export const WHITELIST = ['_rev', 'name', 'versions', DIST_TAGS, 'readme', 'time'];
export function cleanUpLinksRef(keepUpLinkData: boolean, result: Package): Package {
const propertyToKeep = [...WHITELIST];
if (keepUpLinkData === true) {
propertyToKeep.push('_uplinks');
}
for (let i in result) {
if (propertyToKeep.indexOf(i) === -1) { // Remove sections like '_uplinks' from response
delete result[i];
}
}
return result;
}
/** /**
* Check whether a package it is already a local package * Check whether a package it is already a local package
* @param {*} name * @param {*} name

View file

@ -8,10 +8,10 @@ import ProxyStorage from './up-storage';
import Search from './search'; import Search from './search';
import LocalStorage from './local-storage'; import LocalStorage from './local-storage';
import {ReadTarball} from '@verdaccio/streams'; import {ReadTarball} from '@verdaccio/streams';
import {checkPackageLocal, publishPackage, checkPackageRemote} from './storage-utils'; import {checkPackageLocal, publishPackage, checkPackageRemote, cleanUpLinksRef} from './storage-utils';
import {setupUpLinks, updateVersionsHiddenUpLink} from './uplink-util'; import {setupUpLinks, updateVersionsHiddenUpLink} from './uplink-util';
import {mergeVersions} from './metadata-utils'; import {mergeVersions} from './metadata-utils';
import {ErrorCode, normalize_dist_tags, validate_metadata, isObject, DIST_TAGS} from './utils'; import {ErrorCode, normalizeDistTags, validate_metadata, isObject, DIST_TAGS} from './utils';
import type {IStorage, IProxy, IStorageHandler, ProxyList, StringValue} from '../../types'; import type {IStorage, IProxy, IStorageHandler, ProxyList, StringValue} from '../../types';
import type { import type {
Versions, Versions,
@ -26,7 +26,6 @@ import type {
import type {IReadTarball, IUploadTarball} from '@verdaccio/streams'; import type {IReadTarball, IUploadTarball} from '@verdaccio/streams';
const LoggerApi = require('../lib/logger'); const LoggerApi = require('../lib/logger');
const WHITELIST = ['_rev', 'name', 'versions', DIST_TAGS, 'readme', 'time'];
const getDefaultMetadata = function(name): Package { const getDefaultMetadata = function(name): Package {
const pkgMetadata: Package = { const pkgMetadata: Package = {
name, name,
@ -47,9 +46,6 @@ class Storage implements IStorageHandler {
logger: Logger; logger: Logger;
uplinks: ProxyList; uplinks: ProxyList;
/**
* @param {*} config
*/
constructor(config: Config) { constructor(config: Config) {
this.config = config; this.config = config;
this.uplinks = setupUpLinks(config); this.uplinks = setupUpLinks(config);
@ -169,9 +165,9 @@ class Storage implements IStorageHandler {
// trying local first // trying local first
// flow: should be IReadTarball // flow: should be IReadTarball
let localStream: any = self.localStorage.getTarball(name, filename); let localStream: any = self.localStorage.getTarball(name, filename);
let is_open = false; let isOpen = false;
localStream.on('error', (err) => { localStream.on('error', (err) => {
if (is_open || err.status !== 404) { if (isOpen || err.status !== 404) {
return readStream.emit('error', err); return readStream.emit('error', err);
} }
@ -202,7 +198,7 @@ class Storage implements IStorageHandler {
readStream.emit('content-length', v); readStream.emit('content-length', v);
}); });
localStream.on('open', function() { localStream.on('open', function() {
is_open = true; isOpen = true;
localStream.pipe(readStream); localStream.pipe(readStream);
}); });
return readStream; return readStream;
@ -214,10 +210,10 @@ class Storage implements IStorageHandler {
function serveFile(file: DistFile) { function serveFile(file: DistFile) {
let uplink: any = null; let uplink: any = null;
for (let p in self.uplinks) { for (let uplinkId in self.uplinks) {
// $FlowFixMe // $FlowFixMe
if (self.uplinks[p].isUplinkValid(file.url)) { if (self.uplinks[uplinkId].isUplinkValid(file.url)) {
uplink = self.uplinks[p]; uplink = self.uplinks[uplinkId];
} }
} }
@ -304,28 +300,17 @@ class Storage implements IStorageHandler {
} }
this._syncUplinksMetadata(options.name, data, {req: options.req}, this._syncUplinksMetadata(options.name, data, {req: options.req},
function getPackageSynUpLinksCallback(err, result: Package, uplink_errors) { function getPackageSynUpLinksCallback(err, result: Package, uplinkErrors) {
if (err) { if (err) {
return options.callback(err); return options.callback(err);
} }
const propertyToKeep = [...WHITELIST]; normalizeDistTags(cleanUpLinksRef(options.keepUpLinkData, result));
if (options.keepUpLinkData === true) {
propertyToKeep.push('_uplinks');
}
for (let i in result) {
if (propertyToKeep.indexOf(i) === -1) { // Remove sections like '_uplinks' from response
delete result[i];
}
}
normalize_dist_tags(result);
// npm can throw if this field doesn't exist // npm can throw if this field doesn't exist
result._attachments = {}; result._attachments = {};
options.callback(null, result, uplink_errors); options.callback(null, result, uplinkErrors);
}); });
}); });
} }

View file

@ -247,7 +247,7 @@ function semverSort(listVersions: Array<string>) {
* Flatten arrays of tags. * Flatten arrays of tags.
* @param {*} data * @param {*} data
*/ */
function normalize_dist_tags(pkg: Package) { export function normalizeDistTags(pkg: Package) {
let sorted; let sorted;
if (!pkg[DIST_TAGS].latest) { if (!pkg[DIST_TAGS].latest) {
// overwrite latest with highest known version based on semver sort // overwrite latest with highest known version based on semver sort
@ -443,7 +443,6 @@ export {
semverSort, semverSort,
parse_address, parse_address,
get_version, get_version,
normalize_dist_tags,
tagVersion, tagVersion,
combineBaseUrl, combineBaseUrl,
filter_tarball_urls, filter_tarball_urls,

View file

@ -70,7 +70,7 @@ export type Utils = {
isObject: (value: any) => boolean; isObject: (value: any) => boolean;
validate_name: (value: any) => boolean; validate_name: (value: any) => boolean;
tag_version: (value: any, version: string, tag: string) => void; tag_version: (value: any, version: string, tag: string) => void;
normalize_dist_tags: (pkg: Package) => void; normalizeDistTags: (pkg: Package) => void;
semverSort: (keys: Array<string>) => Array<string>; semverSort: (keys: Array<string>) => Array<string>;
} }