0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2025-01-13 22:48:31 -05:00

fix(flow): fix flow definitions

This commit is contained in:
Juan Picado @jotadeveloper 2018-02-19 19:29:14 +01:00
parent 27efad9648
commit d086073069
No known key found for this signature in database
GPG key ID: 18AC54485952D158
19 changed files with 878 additions and 583 deletions

View file

@ -75,6 +75,7 @@
"no-invalid-this": 2,
"new-cap": 2,
"one-var": 2,
"quote-props":["error", "as-needed"],
"no-console": [
2,
{

View file

@ -7,13 +7,18 @@
.*/coverage/.*
.*/.vscode/.*
.*/build/.*
[include]
.*/docs/.*
.*/scripts/.*
.*/assets/.*
.*/bin/.*
.*/systemd/.*
.*/website/.*
.*/wiki/.*
.*/docs/.*
.*/tools/.*
[libs]
node_modules/@verdaccio/types/lib/
[lints]
[options]
suppress_comment= \\(.\\|\n\\)*\\$FlowFixMe

109
flow-typed/npm/node-mocks-http_vx.x.x.js vendored Normal file
View file

@ -0,0 +1,109 @@
// flow-typed signature: 0c37b93b28df38b46c7edb9bc9d278ad
// flow-typed version: <<STUB>>/node-mocks-http_v1.6.7/flow_v0.64.0
/**
* This is an autogenerated libdef stub for:
*
* 'node-mocks-http'
*
* Fill this stub out by replacing all the `any` types.
*
* Once filled out, we encourage you to share your work with the
* community by sending a pull request to:
* https://github.com/flowtype/flow-typed
*/
declare module 'node-mocks-http' {
declare module.exports: any;
}
/**
* We include stubs for each file inside this npm package in case you need to
* require those files directly. Feel free to delete any files that aren't
* needed.
*/
declare module 'node-mocks-http/lib/express/mock-application' {
declare module.exports: any;
}
declare module 'node-mocks-http/lib/express/mock-express' {
declare module.exports: any;
}
declare module 'node-mocks-http/lib/express/mock-request' {
declare module.exports: any;
}
declare module 'node-mocks-http/lib/express/utils/define-getter' {
declare module.exports: any;
}
declare module 'node-mocks-http/lib/http-mock' {
declare module.exports: any;
}
declare module 'node-mocks-http/lib/mockEventEmitter' {
declare module.exports: any;
}
declare module 'node-mocks-http/lib/mockRequest' {
declare module.exports: any;
}
declare module 'node-mocks-http/lib/mockResponse' {
declare module.exports: any;
}
declare module 'node-mocks-http/lib/mockWritableStream' {
declare module.exports: any;
}
declare module 'node-mocks-http/lib/node/_http_incoming' {
declare module.exports: any;
}
declare module 'node-mocks-http/lib/node/_http_server' {
declare module.exports: any;
}
declare module 'node-mocks-http/lib/node/http' {
declare module.exports: any;
}
// Filename aliases
declare module 'node-mocks-http/lib/express/mock-application.js' {
declare module.exports: $Exports<'node-mocks-http/lib/express/mock-application'>;
}
declare module 'node-mocks-http/lib/express/mock-express.js' {
declare module.exports: $Exports<'node-mocks-http/lib/express/mock-express'>;
}
declare module 'node-mocks-http/lib/express/mock-request.js' {
declare module.exports: $Exports<'node-mocks-http/lib/express/mock-request'>;
}
declare module 'node-mocks-http/lib/express/utils/define-getter.js' {
declare module.exports: $Exports<'node-mocks-http/lib/express/utils/define-getter'>;
}
declare module 'node-mocks-http/lib/http-mock.js' {
declare module.exports: $Exports<'node-mocks-http/lib/http-mock'>;
}
declare module 'node-mocks-http/lib/mockEventEmitter.js' {
declare module.exports: $Exports<'node-mocks-http/lib/mockEventEmitter'>;
}
declare module 'node-mocks-http/lib/mockRequest.js' {
declare module.exports: $Exports<'node-mocks-http/lib/mockRequest'>;
}
declare module 'node-mocks-http/lib/mockResponse.js' {
declare module.exports: $Exports<'node-mocks-http/lib/mockResponse'>;
}
declare module 'node-mocks-http/lib/mockWritableStream.js' {
declare module.exports: $Exports<'node-mocks-http/lib/mockWritableStream'>;
}
declare module 'node-mocks-http/lib/node/_http_incoming.js' {
declare module.exports: $Exports<'node-mocks-http/lib/node/_http_incoming'>;
}
declare module 'node-mocks-http/lib/node/_http_server.js' {
declare module.exports: $Exports<'node-mocks-http/lib/node/_http_server'>;
}
declare module 'node-mocks-http/lib/node/http.js' {
declare module.exports: $Exports<'node-mocks-http/lib/node/http'>;
}

View file

@ -1,31 +1,31 @@
/* eslint comma-dangle: 0 */
module.exports = {
'name': 'verdaccio-jest',
'verbose': true,
'collectCoverage': true,
'coveragePathIgnorePatterns': [
name: 'verdaccio-jest',
verbose: true,
collectCoverage: true,
coveragePathIgnorePatterns: [
'node_modules',
'fixtures'
],
'testEnvironment': 'jest-environment-jsdom-global',
'testRegex': '(/test/unit.*\\.spec|test/functional.*\\.func|/test/webui/.*\\.spec)\\.js',
testEnvironment: 'jest-environment-jsdom-global',
testRegex: '(/test/unit.*\\.spec|test/functional.*\\.func|/test/webui/.*\\.spec)\\.js',
// 'testRegex': '(test/functional.*\\.func)\\.js'
'setupFiles': [
setupFiles: [
'./test/webui/global.js'
],
'modulePathIgnorePatterns': [
modulePathIgnorePatterns: [
'global.js'
],
'testPathIgnorePatterns': [
testPathIgnorePatterns: [
'__snapshots__'
],
'moduleNameMapper': {
moduleNameMapper: {
'\\.(scss)$': '<rootDir>/node_modules/identity-obj-proxy',
'github-markdown-css': '<rootDir>/node_modules/identity-obj-proxy',
'\\.(png)$': '<rootDir>/node_modules/identity-obj-proxy'
},
'transformIgnorePatterns': [
transformIgnorePatterns: [
'<rootDir>/node_modules/(?!react-syntax-highlighter)'
]
};

View file

@ -1,11 +1,11 @@
/* eslint comma-dangle: 0 */
module.exports = {
'name': 'verdaccio-e2e-jest',
'verbose': true,
'collectCoverage': false,
'globalSetup': './test/e2e/pre-setup.js',
'globalTeardown': './test/e2e/teardown.js',
'testEnvironment': './test/e2e/puppeteer_environment.js',
'testRegex': '(/test/e2e/e2e.*\\.spec)\\.js'
name: 'verdaccio-e2e-jest',
verbose: true,
collectCoverage: false,
globalSetup: './test/e2e/pre-setup.js',
globalTeardown: './test/e2e/teardown.js',
testEnvironment: './test/e2e/puppeteer_environment.js',
testRegex: '(/test/e2e/e2e.*\\.spec)\\.js'
};

View file

@ -52,7 +52,7 @@
"@commitlint/cli": "6.1.0",
"@commitlint/config-conventional": "6.1.0",
"@commitlint/travis-cli": "6.1.0",
"@verdaccio/types": "0.3.1",
"@verdaccio/types": "1.0.0",
"axios": "0.17.1",
"babel-cli": "6.26.0",
"babel-core": "6.26.0",
@ -105,6 +105,7 @@
"jest-environment-jsdom-global": "1.0.3",
"jest-environment-node": "22.2.0",
"localstorage-memory": "1.0.2",
"node-mocks-http": "1.6.7",
"node-sass": "4.7.2",
"normalize.css": "7.0.0",
"ora": "1.4.0",

View file

@ -57,11 +57,11 @@ class Config {
assert(self.storage, 'CONFIG: storage path not defined');
const users = {
'all': true,
'anonymous': true,
'undefined': true,
'owner': true,
'none': true,
all: true,
anonymous: true,
undefined: true,
owner: true,
none: true,
};
const check_user_or_uplink = function(arg) {

View file

@ -6,7 +6,6 @@ import Crypto from 'crypto';
import assert from 'assert';
import fs from 'fs';
import Path from 'path';
import Stream from 'stream';
import UrlNode from 'url';
import _ from 'lodash';
// $FlowFixMe
@ -172,7 +171,7 @@ class LocalStorage implements IStorage {
sha: version.dist.shasum,
};
/* eslint spaced-comment: 0 */
//$FlowFixMe
// $FlowFixMe
const upLink: string = version[Symbol.for('__verdaccio_uplink')];
if (_.isNil(upLink) === false) {
@ -232,7 +231,9 @@ class LocalStorage implements IStorage {
* @param {*} tag
* @param {*} callback
*/
addVersion(name: string, version: string, metadata: Version,
addVersion(name: string,
version: string,
metadata: Version,
tag: string,
callback: Callback) {
this._updatePackage(name, (data, cb) => {
@ -533,7 +534,7 @@ class LocalStorage implements IStorage {
* @private
* @return {ReadTarball}
*/
_streamSuccessReadTarBall(storage: any, filename: string) {
_streamSuccessReadTarBall(storage: any, filename: string): IReadTarball {
const stream: IReadTarball = new ReadTarball();
const readTarballStream = storage.readTarball(filename);
const e404 = Utils.ErrorCode.get404;
@ -588,7 +589,7 @@ class LocalStorage implements IStorage {
* @return {Function}
*/
search(startKey: string, options: any) {
const stream = new Stream.PassThrough({objectMode: true});
const stream = new UploadTarball({objectMode: true});
this._eachPackage((item, cb) => {
fs.stat(item.path, (err, stats) => {
@ -609,21 +610,21 @@ class LocalStorage implements IStorage {
if (data.versions[latest]) {
const version: Version = data.versions[latest];
const pkg: any = {
'name': version.name,
'description': version.description,
name: version.name,
description: version.description,
'dist-tags': {latest},
'maintainers': version.maintainers || [version.author].filter(Boolean),
'author': version.author,
'repository': version.repository,
'readmeFilename': version.readmeFilename || '',
'homepage': version.homepage,
'keywords': version.keywords,
'bugs': version.bugs,
'license': version.license,
'time': {
maintainers: version.maintainers || [version.author].filter(Boolean),
author: version.author,
repository: version.repository,
readmeFilename: version.readmeFilename || '',
homepage: version.homepage,
keywords: version.keywords,
bugs: version.bugs,
license: version.license,
time: {
modified: item.time ? new Date(item.time).toISOString() : stats.mtime,
},
'versions': {[latest]: 'latest'},
versions: {[latest]: 'latest'},
};
stream.push(pkg);

View file

@ -17,13 +17,14 @@ const DEFAULT_REVISION: string = `0-0000000000000000`;
const generatePackageTemplate = function(name: string): Package {
return {
// standard things
'name': name,
'versions': {},
name,
versions: {},
'dist-tags': {},
'time': {},
'_distfiles': {},
'_attachments': {},
'_uplinks': {},
time: {},
_distfiles: {},
_attachments: {},
_uplinks: {},
_rev: '',
};
};

View file

@ -17,6 +17,7 @@ import type {
IStorage,
IProxy,
IStorageHandler,
Versions,
ProxyList,
Package,
Config,
@ -27,17 +28,22 @@ import type {
Logger,
} from '@verdaccio/types';
import type {IReadTarball} from '@verdaccio/streams';
import type {IReadTarball, IUploadTarball} from '@verdaccio/streams';
const LoggerApi = require('../lib/logger');
const WHITELIST = ['_rev', 'name', 'versions', 'dist-tags', 'readme', 'time'];
const getDefaultMetadata = (name) => {
return {
'name': name,
'versions': {},
const getDefaultMetadata = function(name): Package {
const pkgMetadata: Package = {
name,
versions: {},
'dist-tags': {},
'_uplinks': {},
_uplinks: {},
_distfiles: {},
_attachments: {},
_rev: '',
};
return pkgMetadata;
};
/**
@ -97,6 +103,7 @@ class Storage implements IStorageHandler {
*/
const checkPackageRemote = () => {
return new Promise((resolve, reject) => {
// $FlowFixMe
self._syncUplinksMetadata(name, null, {}, (err, results, err_results) => {
// something weird
if (err && err.status !== 404) {
@ -111,7 +118,7 @@ class Storage implements IStorageHandler {
// if uplink fails with a status other than 404, we report failure
if (_.isNil(err_results[i][0]) === false) {
if (err_results[i][0].status !== 404) {
if (_.isNil(this.config.publish) === false &&
if (this.config.publish &&
_.isBoolean(this.config.publish.allow_offline) &&
this.config.publish.allow_offline) {
return resolve();
@ -166,7 +173,7 @@ class Storage implements IStorageHandler {
* @param {*} tag
* @param {*} callback
*/
addVersion(name: string, version: Version, metadata: Package, tag: string, callback: Callback) {
addVersion(name: string, version: string, metadata: Version, tag: string, callback: Callback) {
this.localStorage.addVersion(name, version, metadata, tag, callback);
}
@ -242,7 +249,7 @@ class Storage implements IStorageHandler {
* @param {*} filename
* @return {Stream}
*/
add_tarball(name: string, filename: string) {
add_tarball(name: string, filename: string): IUploadTarball {
return this.localStorage.addTarball(name, filename);
}
@ -266,7 +273,8 @@ class Storage implements IStorageHandler {
// information about it, so fetching package info is unnecessary
// trying local first
let localStream: IReadTarball = self.localStorage.getTarball(name, filename);
// flow: should be IReadTarball
let localStream: any = self.localStorage.getTarball(name, filename);
let is_open = false;
localStream.on('error', (err) => {
if (is_open || err.status !== 404) {
@ -276,7 +284,8 @@ class Storage implements IStorageHandler {
// local reported 404
let err404 = err;
localStream.abort();
localStream = null; // gc
// $FlowFixMe
localStream = null; // we force for garbage collector
self.localStorage.getPackageMetadata(name, (err, info: Package) => {
if (_.isNil(err) && info._distfiles && _.isNil(info._distfiles[filename]) === false) {
// information about this file exists locally
@ -525,23 +534,16 @@ class Storage implements IStorageHandler {
* Function fetches package metadata from uplinks and synchronizes it with local data
if package is available locally, it MUST be provided in pkginfo
returns callback(err, result, uplink_errors)
* @param {*} name
* @param {*} packageInfo
* @param {*} options
* @param {*} callback
*/
_syncUplinksMetadata(name: string, packageInfo: Package, options: any, callback: Callback) {
let exists = false;
_syncUplinksMetadata(name: string, packageInfo: Package, options: any, callback: Callback): void {
let exists = true;
const self = this;
const upLinks = [];
if (_.isNil(packageInfo)) {
if (!packageInfo || packageInfo === null) {
exists = false;
packageInfo = getDefaultMetadata(name);
} else {
exists = true;
}
for (let up in this.uplinks) {
if (this.config.hasProxyTo(name, up)) {
upLinks.push(this.uplinks[up]);
@ -591,7 +593,7 @@ class Storage implements IStorageHandler {
// added to fix verdaccio#73
if ('time' in upLinkResponse) {
packageInfo['time'] = upLinkResponse.time;
packageInfo.time = upLinkResponse.time;
}
this._updateVersionsHiddenUpLink(upLinkResponse.versions, upLink);

View file

@ -13,11 +13,13 @@ import {ReadTarball} from '@verdaccio/streams';
import type {
IProxy,
Config,
UpLinkConf,
Callback,
Headers,
Logger,
} from '@verdaccio/types';
import type {IUploadTarball} from '@verdaccio/streams';
// import type {IUploadTarball, IReadTarball} from '@verdaccio/streams';
const LoggerApi = require('./logger');
const encode = function(thing) {
@ -42,7 +44,7 @@ const setConfig = (config, key, def) => {
* (same for storage.js, local-storage.js, up-storage.js)
*/
class ProxyStorage implements IProxy {
config: Config;
config: UpLinkConf;
failed_requests: number;
userAgent: string;
ca: string | void;
@ -76,7 +78,7 @@ class ProxyStorage implements IProxy {
this.config.url = this.config.url.replace(/\/$/, '');
if (Number(this.config.timeout) >= 1000) {
if (this.config.timeout && Number(this.config.timeout) >= 1000) {
this.logger.warn(['Too big timeout value: ' + this.config.timeout,
'We changed time format to nginx-like one',
'(see http://nginx.org/en/docs/syntax.html)',
@ -96,14 +98,14 @@ class ProxyStorage implements IProxy {
* @param {*} cb
* @return {Request}
*/
request(options: any, cb: Callback) {
request(options: any, cb?: Callback) {
let json;
if (this._statusCheck() === false) {
let streamRead = new Stream.Readable();
process.nextTick(function() {
if (_.isFunction(cb)) {
if (cb) {
cb(ErrorCode.get500('uplink is offline'));
}
// $FlowFixMe
@ -142,6 +144,7 @@ class ProxyStorage implements IProxy {
// $FlowFixMe
processBody(err, body);
logActivity();
// $FlowFixMe
cb(err, res, body);
/**
@ -261,8 +264,9 @@ class ProxyStorage implements IProxy {
* @private
*/
_setAuth(headers: any) {
const auth = this.config.auth;
if (_.isNil(this.config.auth) || headers['authorization']) {
if (typeof auth === 'undefined' || headers['authorization']) {
return headers;
}
@ -273,10 +277,12 @@ class ProxyStorage implements IProxy {
// get NPM_TOKEN http://blog.npmjs.org/post/118393368555/deploying-with-npm-private-modules
// or get other variable export in env
let token: any = process.env.NPM_TOKEN;
if (this.config.auth.token) {
token = this.config.auth.token;
} else if (this.config.auth.token_env) {
token = process.env[this.config.auth.token_env];
if (auth.token) {
token = auth.token;
} else if (auth.token_env ) {
// $FlowFixMe
token = process.env[auth.token_env];
}
if (_.isNil(token)) {
@ -284,8 +290,9 @@ class ProxyStorage implements IProxy {
}
// define type Auth allow basic and bearer
const type = this.config.auth.type;
const type = auth.type;
this._setHeaderAuthorization(headers, type, token);
return headers;
}
@ -334,12 +341,15 @@ class ProxyStorage implements IProxy {
* @param {Object} headers
* @private
*/
_overrideWithUplinkConfigHeaders(headers: any) {
// add/override headers specified in the config
for (let key in this.config.headers) {
if (Object.prototype.hasOwnProperty.call(this.config.headers, key)) {
headers[key] = this.config.headers[key];
_overrideWithUplinkConfigHeaders(headers: Headers) {
if (!this.config.headers) {
return headers;
}
// add/override headers specified in the config
/* eslint guard-for-in: 0 */
for (let key in this.config.headers) {
headers[key] = this.config.headers[key];
}
}
@ -447,8 +457,8 @@ class ProxyStorage implements IProxy {
* @return {Stream}
*/
search(options: any) {
const transformStream: IUploadTarball = new Stream.PassThrough({objectMode: true});
const requestStream: IUploadTarball = this.request({
const transformStream: any = new Stream.PassThrough({objectMode: true});
const requestStream: stream$Readable = this.request({
uri: options.req.url,
req: options.req,
headers: {
@ -486,6 +496,8 @@ class ProxyStorage implements IProxy {
});
transformStream.abort = () => {
// FIXME: this is clearly a potential issue
// $FlowFixMe
requestStream.abort();
transformStream.emit('end');
};

View file

@ -278,14 +278,14 @@ function normalize_dist_tags(data) {
const parseIntervalTable = {
'': 1000,
'ms': 1,
's': 1000,
'm': 60*1000,
'h': 60*60*1000,
'd': 86400000,
'w': 7*86400000,
'M': 30*86400000,
'y': 365*86400000,
ms: 1,
s: 1000,
m: 60*1000,
h: 60*60*1000,
d: 86400000,
w: 7*86400000,
M: 30*86400000,
y: 365*86400000,
};
/**

View file

@ -10,7 +10,7 @@ import {readFile} from '../functional/lib/test.utils';
const readMetadata = (fileName: string = 'metadata') => readFile(`../../unit/partials/${fileName}`);
import type {IStorage} from '@verdaccio/types';
import type {IStorage, Config} from '@verdaccio/types';
setup([]);

View file

@ -1,5 +1,5 @@
const config = {
storage: __dirname + '/store/test-storage',
storage: `${__dirname}/store/test-storage`,
uplinks: {
'npmjs': {
'url': 'https://registry.npmjs.org/'
@ -17,6 +17,12 @@ const config = {
allow_publish: 'nobody'
},
'react': {
allow_access: '$all',
allow_publish: '$all',
proxy: 'npmjs'
},
'jquery': {
allow_access: '$all',
allow_publish: '$all',

79
test/unit/store.spec.js Normal file
View file

@ -0,0 +1,79 @@
// @flow
import _ from 'lodash';
import httpMocks from 'node-mocks-http';
// $FlowFixMe
import configExample from './partials/config';
import AppConfig from '../../src/lib/config';
import Storage from '../../src/lib/storage';
import {setup} from '../../src/lib/logger';
import type {IStorageHandler, Config} from '@verdaccio/types';
setup(configExample.logs);
const generateStorage = function(): IStorageHandler {
const storageConfig = _.clone(configExample);
const storage = `./unit/partials/store/test-storage-store.spec`;
storageConfig.self_path = __dirname;
storageConfig.storage = storage;
const config: Config = new AppConfig(storageConfig);
return new Storage(config);
}
describe('StorageTest', () => {
jest.setTimeout(1000000);
beforeAll((done)=> {
const storage: IStorageHandler = generateStorage();
var request = httpMocks.createRequest({
method: 'GET',
url: '/react',
params: {}
});
storage.getPackage({
name: 'react',
req: request,
callback: () => {
const stream = storage.get_tarball('react', 'react-16.1.0.tgz');
stream.on('content-length', function(content) {
if (content) {
expect(content).toBeTruthy();
done();
}
});
},
});
});
test('should be defined', () => {
const storage: IStorageHandler = generateStorage();
expect(storage).toBeDefined();
});
test('should fetch from uplink react metadata from nmpjs', (done) => {
const storage: IStorageHandler = generateStorage();
// $FlowFixMe
storage._syncUplinksMetadata('react', null, {}, (err, metadata, errors) => {
expect(metadata).toBeInstanceOf(Object);
done();
});
});
test('should fails on fetch from uplink metadata from nmpjs', (done) => {
const storage: IStorageHandler = generateStorage();
// $FlowFixMe
storage._syncUplinksMetadata('@verdaccio/404', null, {}, (err, metadata, errors) => {
expect(errors).toBeInstanceOf(Array);
expect(errors[0][0].statusCode).toBe(404);
expect(errors[0][0].message).toMatch(/package doesn't exist on uplink/);
done();
});
});
});

View file

@ -6,13 +6,20 @@ import _ from 'lodash';
import configExample from './partials/config';
import {setup} from '../../src/lib/logger';
import type {UpLinkConf, Config} from '@verdaccio/types';
setup([]);
describe('UpStorge', () => {
const uplinkDefault = {
url: 'https://registry.npmjs.org/'
const uplinkDefault: UpLinkConf = {
url: 'https://registry.npmjs.org/',
fail_timeout: '5m',
max_fails: 2,
maxage: '2m',
timeout: '1m',
};
let generateProxy = (config: UpLinkConf = uplinkDefault) => {
const appConfig: Config = new AppConfig(configExample);

View file

@ -24,9 +24,9 @@ export default {
plugins: [
new webpack.DefinePlugin({
'__DEBUG__': true,
__DEBUG__: true,
'process.env.NODE_ENV': '"development"',
'__APP_VERSION__': `"${getPackageVersion()}"`,
__APP_VERSION__: `"${getPackageVersion()}"`,
}),
new HTMLWebpackPlugin({
title: 'Verdaccio',

View file

@ -18,9 +18,9 @@ const prodConf = {
plugins: [
new webpack.DefinePlugin({
'__DEBUG__': false,
__DEBUG__: false,
'process.env.NODE_ENV': '"production"',
'__APP_VERSION__': `"${getPackageVersion()}"`,
__APP_VERSION__: `"${getPackageVersion()}"`,
}),
new webpack.optimize.UglifyJsPlugin({
sourceMap: true,

829
yarn.lock

File diff suppressed because it is too large Load diff