mirror of
https://github.com/verdaccio/verdaccio.git
synced 2024-12-16 21:56:25 -05:00
refactor: clean up ref method
This commit is contained in:
parent
4d5e8aab0b
commit
c1bc2610c4
4 changed files with 32 additions and 31 deletions
|
@ -2,7 +2,7 @@
|
|||
|
||||
import _ from 'lodash';
|
||||
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 type {Package, Version} from '@verdaccio/types';
|
||||
|
@ -52,7 +52,7 @@ function normalizePackage(pkg: Package) {
|
|||
}
|
||||
|
||||
// normalize dist-tags
|
||||
normalize_dist_tags(pkg);
|
||||
normalizeDistTags(pkg);
|
||||
|
||||
return pkg;
|
||||
}
|
||||
|
@ -71,6 +71,23 @@ function cleanUpReadme(version: Version): 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
|
||||
* @param {*} name
|
||||
|
|
|
@ -8,10 +8,10 @@ import ProxyStorage from './up-storage';
|
|||
import Search from './search';
|
||||
import LocalStorage from './local-storage';
|
||||
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 {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 {
|
||||
Versions,
|
||||
|
@ -26,7 +26,6 @@ import type {
|
|||
import type {IReadTarball, IUploadTarball} from '@verdaccio/streams';
|
||||
|
||||
const LoggerApi = require('../lib/logger');
|
||||
const WHITELIST = ['_rev', 'name', 'versions', DIST_TAGS, 'readme', 'time'];
|
||||
const getDefaultMetadata = function(name): Package {
|
||||
const pkgMetadata: Package = {
|
||||
name,
|
||||
|
@ -47,9 +46,6 @@ class Storage implements IStorageHandler {
|
|||
logger: Logger;
|
||||
uplinks: ProxyList;
|
||||
|
||||
/**
|
||||
* @param {*} config
|
||||
*/
|
||||
constructor(config: Config) {
|
||||
this.config = config;
|
||||
this.uplinks = setupUpLinks(config);
|
||||
|
@ -169,9 +165,9 @@ class Storage implements IStorageHandler {
|
|||
// trying local first
|
||||
// flow: should be IReadTarball
|
||||
let localStream: any = self.localStorage.getTarball(name, filename);
|
||||
let is_open = false;
|
||||
let isOpen = false;
|
||||
localStream.on('error', (err) => {
|
||||
if (is_open || err.status !== 404) {
|
||||
if (isOpen || err.status !== 404) {
|
||||
return readStream.emit('error', err);
|
||||
}
|
||||
|
||||
|
@ -202,7 +198,7 @@ class Storage implements IStorageHandler {
|
|||
readStream.emit('content-length', v);
|
||||
});
|
||||
localStream.on('open', function() {
|
||||
is_open = true;
|
||||
isOpen = true;
|
||||
localStream.pipe(readStream);
|
||||
});
|
||||
return readStream;
|
||||
|
@ -214,10 +210,10 @@ class Storage implements IStorageHandler {
|
|||
function serveFile(file: DistFile) {
|
||||
let uplink: any = null;
|
||||
|
||||
for (let p in self.uplinks) {
|
||||
for (let uplinkId in self.uplinks) {
|
||||
// $FlowFixMe
|
||||
if (self.uplinks[p].isUplinkValid(file.url)) {
|
||||
uplink = self.uplinks[p];
|
||||
if (self.uplinks[uplinkId].isUplinkValid(file.url)) {
|
||||
uplink = self.uplinks[uplinkId];
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -304,28 +300,17 @@ class Storage implements IStorageHandler {
|
|||
}
|
||||
|
||||
this._syncUplinksMetadata(options.name, data, {req: options.req},
|
||||
function getPackageSynUpLinksCallback(err, result: Package, uplink_errors) {
|
||||
function getPackageSynUpLinksCallback(err, result: Package, uplinkErrors) {
|
||||
if (err) {
|
||||
return options.callback(err);
|
||||
}
|
||||
|
||||
const propertyToKeep = [...WHITELIST];
|
||||
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);
|
||||
normalizeDistTags(cleanUpLinksRef(options.keepUpLinkData, result));
|
||||
|
||||
// npm can throw if this field doesn't exist
|
||||
result._attachments = {};
|
||||
|
||||
options.callback(null, result, uplink_errors);
|
||||
options.callback(null, result, uplinkErrors);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
|
|
@ -247,7 +247,7 @@ function semverSort(listVersions: Array<string>) {
|
|||
* Flatten arrays of tags.
|
||||
* @param {*} data
|
||||
*/
|
||||
function normalize_dist_tags(pkg: Package) {
|
||||
export function normalizeDistTags(pkg: Package) {
|
||||
let sorted;
|
||||
if (!pkg[DIST_TAGS].latest) {
|
||||
// overwrite latest with highest known version based on semver sort
|
||||
|
@ -443,7 +443,6 @@ export {
|
|||
semverSort,
|
||||
parse_address,
|
||||
get_version,
|
||||
normalize_dist_tags,
|
||||
tagVersion,
|
||||
combineBaseUrl,
|
||||
filter_tarball_urls,
|
||||
|
|
|
@ -70,7 +70,7 @@ export type Utils = {
|
|||
isObject: (value: any) => boolean;
|
||||
validate_name: (value: any) => boolean;
|
||||
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>;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue