0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-12-30 22:34:10 -05:00

chore: update some endpoints with flow

This commit is contained in:
Juan Picado @jotadeveloper 2018-03-02 23:19:08 +01:00
parent 3ed2bb3b34
commit 8f932f5f8a
No known key found for this signature in database
GPG key ID: 18AC54485952D158
8 changed files with 69 additions and 20 deletions

33
flow-typed/npm/cookies_vx.x.x.js vendored Normal file
View file

@ -0,0 +1,33 @@
// flow-typed signature: dd32612579acfe0d3fb825468a81fc4a
// flow-typed version: <<STUB>>/cookies_v0.7.1/flow_v0.64.0
/**
* This is an autogenerated libdef stub for:
*
* 'cookies'
*
* 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 'cookies' {
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.
*/
// Filename aliases
declare module 'cookies/index' {
declare module.exports: $Exports<'cookies'>;
}
declare module 'cookies/index.js' {
declare module.exports: $Exports<'cookies'>;
}

View file

@ -92,7 +92,7 @@
"eslint-plugin-react": "7.6.1", "eslint-plugin-react": "7.6.1",
"extract-text-webpack-plugin": "3.0.2", "extract-text-webpack-plugin": "3.0.2",
"file-loader": "1.1.6", "file-loader": "1.1.6",
"flow-bin": "0.64.0", "flow-bin": "0.66.0",
"flow-runtime": "0.16.0", "flow-runtime": "0.16.0",
"friendly-errors-webpack-plugin": "1.6.1", "friendly-errors-webpack-plugin": "1.6.1",
"github-markdown-css": "2.10.0", "github-markdown-css": "2.10.0",

View file

@ -1,20 +1,23 @@
'use strict'; // @flow
const _ = require('lodash'); import type {$Response, Router} from 'express';
const Cookies = require('cookies'); import type {$RequestExtend, $ResponseExtend, $NextFunctionVer, IAuth} from '../../../../types';
const createError = require('http-errors'); import {ErrorCode} from '../../../lib/utils';
module.exports = function(route, auth) { import _ from 'lodash';
route.get('/-/user/:org_couchdb_user', function(req, res, next) { import Cookies from 'cookies';
module.exports = function(route: Router, auth: IAuth) {
route.get('/-/user/:org_couchdb_user', function(req: $RequestExtend, res: $Response, next: $NextFunctionVer) {
res.status(200); res.status(200);
next({ next({
ok: 'you are authenticated as "' + req.remote_user.name + '"', ok: 'you are authenticated as "' + req.remote_user.name + '"',
}); });
}); });
route.put('/-/user/:org_couchdb_user/:_rev?/:revision?', function(req, res, next) { route.put('/-/user/:org_couchdb_user/:_rev?/:revision?', function(req: $RequestExtend, res: $Response, next: $NextFunctionVer) {
let token = (req.body.name && req.body.password) let token = (req.body.name && req.body.password)
? auth.aes_encrypt(req.body.name + ':' + req.body.password).toString('base64') ? auth.aes_encrypt(new Buffer(req.body.name + ':' + req.body.password)).toString('base64')
: undefined; : undefined;
if (_.isNil(req.remote_user.name) === false) { if (_.isNil(req.remote_user.name) === false) {
res.status(201); res.status(201);
@ -30,7 +33,7 @@ module.exports = function(route, auth) {
// With npm registering is the same as logging in, // With npm registering is the same as logging in,
// and npm accepts only an 409 error. // and npm accepts only an 409 error.
// So, changing status code here. // So, changing status code here.
return next( createError[err.status || 409](err.message) ); return next( ErrorCode.getCode(err.status, err.message) || ErrorCode.get409(err.message));
} }
return next(err); return next(err);
} }
@ -46,7 +49,7 @@ module.exports = function(route, auth) {
} }
}); });
route.delete('/-/user/token/*', function(req, res, next) { route.delete('/-/user/token/*', function(req: $RequestExtend, res: $Response, next: $NextFunctionVer) {
res.status(200); res.status(200);
next({ next({
ok: 'Logged out', ok: 'Logged out',
@ -56,7 +59,7 @@ module.exports = function(route, auth) {
// placeholder 'cause npm require to be authenticated to publish // placeholder 'cause npm require to be authenticated to publish
// we do not do any real authentication yet // we do not do any real authentication yet
route.post('/_session', Cookies.express(), function(req, res, next) { route.post('/_session', Cookies.express(), function(req: $RequestExtend, res: $ResponseExtend, next: $NextFunctionVer) {
res.cookies.set('AuthSession', String(Math.random()), { res.cookies.set('AuthSession', String(Math.random()), {
// npmjs.org sets 10h expire // npmjs.org sets 10h expire
expires: new Date(Date.now() + 10 * 60 * 60 * 1000), expires: new Date(Date.now() + 10 * 60 * 60 * 1000),

View file

@ -1,5 +1,10 @@
module.exports = function(route) { // @flow
route.get('/whoami', function(req, res, next) {
import type {$Response, Router} from 'express';
import type {$RequestExtend, $NextFunctionVer} from '../../../../types';
module.exports = function(route: Router) {
route.get('/whoami', (req: $RequestExtend, res: $Response, next: $NextFunctionVer): void => {
if (req.headers.referer === 'whoami') { if (req.headers.referer === 'whoami') {
next({username: req.remote_user.name}); next({username: req.remote_user.name});
} else { } else {
@ -7,7 +12,7 @@ module.exports = function(route) {
} }
}); });
route.get('/-/whoami', function(req, res, next) { route.get('/-/whoami', (req: $RequestExtend, res: $Response, next: $NextFunctionVer): mixed => {
next({username: req.remote_user.name}); next({username: req.remote_user.name});
}); });
}; };

View file

@ -7,9 +7,8 @@ import {ErrorCode} from './utils';
const Error = require('http-errors'); const Error = require('http-errors');
import type {Config, Logger, Callback} from '@verdaccio/types'; import type {Config, Logger, Callback} from '@verdaccio/types';
import type {$Request, $Response, NextFunction} from 'express'; import type {$Response, NextFunction} from 'express';
import type {$RequestExtend} from '../../types';
type $RequestExtend = $Request & {remote_user: any}
const LoggerApi = require('./logger'); const LoggerApi = require('./logger');
/** /**
@ -366,7 +365,7 @@ class Auth {
/** /**
* Encrypt a string. * Encrypt a string.
*/ */
aes_encrypt(buf: Buffer) { aes_encrypt(buf: Buffer): Buffer {
const c = Crypto.createCipher('aes192', this.secret); const c = Crypto.createCipher('aes192', this.secret);
const b1 = c.update(buf); const b1 = c.update(buf);
const b2 = c.final(); const b2 = c.final();

View file

@ -347,7 +347,10 @@ const ErrorCode = {
}, },
get404: (customMessage?: string) => { get404: (customMessage?: string) => {
return createError(404, customMessage || 'no such package available'); return createError(404, customMessage || 'no such package available');
}, },
getCode: (statusCode: number, customMessage: string) => {
return createError(statusCode, customMessage);
},
}; };
const parseConfigFile = (configPath: string) => YAML.safeLoad(fs.readFileSync(configPath, 'utf8')); const parseConfigFile = (configPath: string) => YAML.safeLoad(fs.readFileSync(configPath, 'utf8'));

View file

@ -14,12 +14,15 @@ import type {
IReadTarball, IReadTarball,
} from '@verdaccio/streams'; } from '@verdaccio/streams';
import type {ILocalData} from '@verdaccio/local-storage'; import type {ILocalData} from '@verdaccio/local-storage';
import type {NextFunction, $Request, $Response} from 'request';
export interface IAuth { export interface IAuth {
config: Config; config: Config;
logger: Logger; logger: Logger;
secret: string; secret: string;
plugins: Array<any>; plugins: Array<any>;
aes_encrypt(buf: Buffer): Buffer;
add_user(user: string, password: string, cb: Callback): any;
} }
export interface IWebSearch { export interface IWebSearch {
@ -102,3 +105,6 @@ export interface IStorage {
search(startKey: string, options: any): IUploadTarball; search(startKey: string, options: any): IUploadTarball;
} }
export type $RequestExtend = $Request & {remote_user?: any}
export type $ResponseExtend = $Response & {cookies?: any}
export type $NextFunctionVer = NextFunction & mixed;

BIN
yarn.lock

Binary file not shown.