mirror of
https://github.com/verdaccio/verdaccio.git
synced 2025-01-20 22:52:46 -05:00
refactor: middleware use es6
This commit is contained in:
parent
c2619854dd
commit
824ad7d180
1 changed files with 10 additions and 11 deletions
|
@ -1,16 +1,13 @@
|
||||||
/* eslint prefer-rest-params: "off" */
|
|
||||||
|
|
||||||
import crypto from 'crypto';
|
import crypto from 'crypto';
|
||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
import {
|
import {
|
||||||
validate_name as utilValidateName,
|
validate_name as utilValidateName,
|
||||||
validate_package as utilValidatePackage,
|
validate_package as utilValidatePackage,
|
||||||
isObject} from '../../lib/utils';
|
isObject,
|
||||||
|
ErrorCode} from '../../lib/utils';
|
||||||
|
|
||||||
const createError = require('http-errors');
|
|
||||||
const Logger = require('../../lib/logger');
|
const Logger = require('../../lib/logger');
|
||||||
|
|
||||||
|
|
||||||
export function match(regexp) {
|
export function match(regexp) {
|
||||||
return function(req, res, next, value) {
|
return function(req, res, next, value) {
|
||||||
if (regexp.exec(value)) {
|
if (regexp.exec(value)) {
|
||||||
|
@ -34,7 +31,7 @@ export function validate_name(req, res, next, value, name) {
|
||||||
} else if (utilValidateName(value)) {
|
} else if (utilValidateName(value)) {
|
||||||
next();
|
next();
|
||||||
} else {
|
} else {
|
||||||
next( createError[403]('invalid ' + name) );
|
next( ErrorCode.get403('invalid ' + name));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,14 +42,14 @@ export function validatePackage(req, res, next, value, name) {
|
||||||
} else if (utilValidatePackage(value)) {
|
} else if (utilValidatePackage(value)) {
|
||||||
next();
|
next();
|
||||||
} else {
|
} else {
|
||||||
next( createError[403]('invalid ' + name) );
|
next( ErrorCode.get403('invalid ' + name) );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function media(expect) {
|
export function media(expect) {
|
||||||
return function(req, res, next) {
|
return function(req, res, next) {
|
||||||
if (req.headers['content-type'] !== expect) {
|
if (req.headers['content-type'] !== expect) {
|
||||||
next( createError[415]('wrong content-type, expect: ' + expect
|
next( ErrorCode.getCode(415, 'wrong content-type, expect: ' + expect
|
||||||
+ ', got: '+req.headers['content-type']) );
|
+ ', got: '+req.headers['content-type']) );
|
||||||
} else {
|
} else {
|
||||||
next();
|
next();
|
||||||
|
@ -70,7 +67,7 @@ export function encodeScopePackage(req, res, next) {
|
||||||
|
|
||||||
export function expect_json(req, res, next) {
|
export function expect_json(req, res, next) {
|
||||||
if (!isObject(req.body)) {
|
if (!isObject(req.body)) {
|
||||||
return next( createError[400]('can\'t parse incoming json') );
|
return next( ErrorCode.get400('can\'t parse incoming json') );
|
||||||
}
|
}
|
||||||
next();
|
next();
|
||||||
}
|
}
|
||||||
|
@ -83,7 +80,7 @@ export function anti_loop(config) {
|
||||||
for (let i=0; i<arr.length; i++) {
|
for (let i=0; i<arr.length; i++) {
|
||||||
let m = arr[i].match(/\s*(\S+)\s+(\S+)/);
|
let m = arr[i].match(/\s*(\S+)\s+(\S+)/);
|
||||||
if (m && m[2] === config.server_id) {
|
if (m && m[2] === config.server_id) {
|
||||||
return next( createError[508]('loop detected') );
|
return next( ErrorCode.getCode(508, 'loop detected') );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -121,7 +118,7 @@ export function allow(auth) {
|
||||||
} else {
|
} else {
|
||||||
// last plugin (that's our built-in one) returns either
|
// last plugin (that's our built-in one) returns either
|
||||||
// cb(err) or cb(null, true), so this should never happen
|
// cb(err) or cb(null, true), so this should never happen
|
||||||
throw createError('bug in the auth plugin system');
|
throw ErrorCode.get500('bug in the auth plugin system');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
@ -207,6 +204,7 @@ export function log(req, res, next) {
|
||||||
let _write = res.write;
|
let _write = res.write;
|
||||||
res.write = function(buf) {
|
res.write = function(buf) {
|
||||||
bytesout += buf.length;
|
bytesout += buf.length;
|
||||||
|
/* eslint prefer-rest-params: "off" */
|
||||||
_write.apply(res, arguments);
|
_write.apply(res, arguments);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -249,6 +247,7 @@ export function log(req, res, next) {
|
||||||
if (buf) {
|
if (buf) {
|
||||||
bytesout += buf.length;
|
bytesout += buf.length;
|
||||||
}
|
}
|
||||||
|
/* eslint prefer-rest-params: "off" */
|
||||||
_end.apply(res, arguments);
|
_end.apply(res, arguments);
|
||||||
log();
|
log();
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Reference in a new issue