mirror of
https://github.com/verdaccio/verdaccio.git
synced 2025-01-06 22:40:26 -05:00
refactor: add constant file
starting with json headers common usage
This commit is contained in:
parent
e63674478a
commit
101fd78b0d
13 changed files with 69 additions and 52 deletions
|
@ -13,7 +13,7 @@ import publish from './api/publish';
|
|||
import search from './api/search';
|
||||
import pkg from './api/package';
|
||||
|
||||
const {match, validate_name, validatePackage, encodeScopePackage, anti_loop} = require('../middleware');
|
||||
const {match, validateName, validatePackage, encodeScopePackage, anti_loop} = require('../middleware');
|
||||
|
||||
export default function(config: Config, auth: IAuth, storage: IStorageHandler) {
|
||||
/* eslint new-cap:off */
|
||||
|
@ -25,11 +25,11 @@ export default function(config: Config, auth: IAuth, storage: IStorageHandler) {
|
|||
// $FlowFixMe
|
||||
app.param('package', validatePackage);
|
||||
// $FlowFixMe
|
||||
app.param('filename', validate_name);
|
||||
app.param('tag', validate_name);
|
||||
app.param('version', validate_name);
|
||||
app.param('revision', validate_name);
|
||||
app.param('token', validate_name);
|
||||
app.param('filename', validateName);
|
||||
app.param('tag', validateName);
|
||||
app.param('version', validateName);
|
||||
app.param('revision', validateName);
|
||||
app.param('token', validateName);
|
||||
|
||||
// these can't be safely put into express url for some reason
|
||||
// TODO: For some reason? what reason?
|
||||
|
|
|
@ -7,6 +7,7 @@ import {
|
|||
validate_package as utilValidatePackage,
|
||||
isObject,
|
||||
ErrorCode} from '../lib/utils';
|
||||
import {HEADERS} from '../lib/constants';
|
||||
import type {$ResponseExtend, $RequestExtend, $NextFunctionVer, IAuth} from '../../types';
|
||||
import type {Config} from '@verdaccio/types';
|
||||
|
||||
|
@ -30,7 +31,7 @@ export function securityIframe(req: $RequestExtend, res: $ResponseExtend, next:
|
|||
|
||||
// flow: express does not match properly
|
||||
// flow info https://github.com/flowtype/flow-typed/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+express
|
||||
export function validate_name(req: $RequestExtend, res: $ResponseExtend, next: $NextFunctionVer, value: string, name: string) {
|
||||
export function validateName(req: $RequestExtend, res: $ResponseExtend, next: $NextFunctionVer, value: string, name: string) {
|
||||
if (value.charAt(0) === '-') {
|
||||
// special case in couchdb usually
|
||||
next('route');
|
||||
|
@ -142,7 +143,7 @@ export function allow(auth: IAuth) {
|
|||
try {
|
||||
if (_.isString(body) || _.isObject(body)) {
|
||||
if (!res.getHeader('Content-type')) {
|
||||
res.header('Content-type', 'application/json');
|
||||
res.header('Content-type', HEADERS.JSON);
|
||||
}
|
||||
|
||||
if (typeof(body) === 'object' && _.isNil(body) === false) {
|
||||
|
|
|
@ -7,7 +7,7 @@ import addPackageWebApi from './endpoint/package';
|
|||
import addSearchWebApi from './endpoint/search';
|
||||
|
||||
import Search from '../../lib/search';
|
||||
import {match, validate_name, validatePackage, securityIframe} from '../middleware';
|
||||
import {match, validateName, validatePackage, securityIframe} from '../middleware';
|
||||
import type {Config} from '@verdaccio/types';
|
||||
import type {IAuth, IStorageHandler} from '../../../types';
|
||||
|
||||
|
@ -25,9 +25,9 @@ module.exports = function(config: Config, auth: IAuth, storage: IStorageHandler)
|
|||
// $FlowFixMe
|
||||
route.param('package', validatePackage);
|
||||
// $FlowFixMe
|
||||
route.param('filename', validate_name);
|
||||
route.param('filename', validateName);
|
||||
// $FlowFixMe
|
||||
route.param('version', validate_name);
|
||||
route.param('version', validateName);
|
||||
route.param('anything', match(/.*/));
|
||||
|
||||
route.use(bodyParser.urlencoded({extended: false}));
|
||||
|
|
7
src/lib/constants.js
Normal file
7
src/lib/constants.js
Normal file
|
@ -0,0 +1,7 @@
|
|||
// @flow
|
||||
|
||||
export const HEADERS = {
|
||||
JSON: 'application/json',
|
||||
JSON_CHARSET: 'application/json; charset=utf-8',
|
||||
OCTET_STREAM: 'application/octet-stream',
|
||||
};
|
|
@ -9,6 +9,7 @@ import Stream from 'stream';
|
|||
import URL from 'url';
|
||||
import {parseInterval, isObject, ErrorCode} from './utils';
|
||||
import {ReadTarball} from '@verdaccio/streams';
|
||||
import {HEADERS} from '../lib/constants';
|
||||
|
||||
import type {
|
||||
Config,
|
||||
|
@ -23,7 +24,7 @@ const LoggerApi = require('./logger');
|
|||
const encode = function(thing) {
|
||||
return encodeURIComponent(thing).replace(/^%40/, '@');
|
||||
};
|
||||
const jsonContentType = 'application/json';
|
||||
const jsonContentType = HEADERS.JSON;
|
||||
const contenTypeAccept = `${jsonContentType};`;
|
||||
|
||||
/**
|
||||
|
@ -135,7 +136,7 @@ class ProxyStorage implements IProxy {
|
|||
|
||||
if (isObject(options.json)) {
|
||||
json = JSON.stringify(options.json);
|
||||
headers['Content-Type'] = headers['Content-Type'] || 'application/json';
|
||||
headers['Content-Type'] = headers['Content-Type'] || HEADERS.JSON;
|
||||
}
|
||||
|
||||
let requestCallback = cb ? (function(err, res, body) {
|
||||
|
|
|
@ -7,6 +7,7 @@ import {Link} from 'react-router-dom';
|
|||
import API from '../../../utils/api';
|
||||
import storage from '../../../utils/storage';
|
||||
import {getRegistryURL} from '../../../utils/url';
|
||||
import {HEADERS} from '../../../../lib/constants';
|
||||
|
||||
import classes from './header.scss';
|
||||
import './logo.png';
|
||||
|
@ -66,8 +67,8 @@ export default class Header extends React.Component {
|
|||
let resp = await API.request(`login`, 'POST', {
|
||||
body: JSON.stringify(credentials),
|
||||
headers: {
|
||||
Accept: 'application/json',
|
||||
'Content-Type': 'application/json'
|
||||
Accept: HEADERS.JSON,
|
||||
'Content-Type': HEADERS.JSON
|
||||
}
|
||||
}).then((response) => response.json());
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import assert from 'assert';
|
||||
import _ from 'lodash';
|
||||
|
||||
import {HEADERS} from '../../../src/lib/constants';
|
||||
import {notify} from '../../../src/lib/notify';
|
||||
|
||||
export default function(express) {
|
||||
|
@ -8,7 +9,7 @@ export default function(express) {
|
|||
notify: {
|
||||
method: 'POST',
|
||||
headers: [{
|
||||
'Content-Type': 'application/json'
|
||||
'Content-Type': HEADERS.JSON
|
||||
}],
|
||||
endpoint: "http://localhost:55550/api/notify",
|
||||
content: '{"color":"green","message":"New package published: * {{ name }}*","notify":true,"message_format":"text"}'
|
||||
|
@ -50,7 +51,7 @@ export default function(express) {
|
|||
|
||||
const configMultipleHeader = _.cloneDeep(config);
|
||||
configMultipleHeader.notify.headers = {
|
||||
'Content-Type': 'application/json'
|
||||
'Content-Type': HEADERS.JSON
|
||||
};
|
||||
|
||||
notify(metadata, configMultipleHeader).then(function (body) {
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
|
||||
import assert from 'assert';
|
||||
import {generateSha} from '../lib/test.utils';
|
||||
import {HEADERS} from '../../../src/lib/constants';
|
||||
|
||||
export default function(server, server2) {
|
||||
|
||||
|
@ -9,7 +10,7 @@ export default function(server, server2) {
|
|||
return server.request({
|
||||
uri: '/@test%2fscoped',
|
||||
headers: {
|
||||
'content-type': 'application/json',
|
||||
'content-type': HEADERS.JSON,
|
||||
},
|
||||
method: 'PUT',
|
||||
json: require('./scoped.json'),
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import assert from 'assert';
|
||||
import {HEADERS} from '../../../src/lib/constants';
|
||||
|
||||
export default function (server, server2) {
|
||||
|
||||
|
@ -8,7 +9,7 @@ export default function (server, server2) {
|
|||
return server.request({
|
||||
uri: '/readme-test',
|
||||
headers: {
|
||||
'content-type': 'application/json',
|
||||
'content-type': HEADERS.JSON,
|
||||
},
|
||||
method: 'PUT',
|
||||
json: require('./pkg-readme.json'),
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import assert from 'assert';
|
||||
import {generateSha} from '../lib/test.utils';
|
||||
import {HEADERS} from '../../../src/lib/constants';
|
||||
|
||||
export default function(server, server2, express) {
|
||||
describe('should test preserve tags when publishing something', () => {
|
||||
|
@ -8,7 +9,7 @@ export default function(server, server2, express) {
|
|||
return server.request({
|
||||
uri: '/testpkg-preserve',
|
||||
headers: {
|
||||
'content-type': 'application/json',
|
||||
'content-type': HEADERS.JSON,
|
||||
},
|
||||
method: 'PUT',
|
||||
json: require('./preserve_tags.json'),
|
||||
|
|
|
@ -4,6 +4,7 @@ import _ from 'lodash';
|
|||
import assert from 'assert';
|
||||
import smartRequest from './request';
|
||||
import type {IServerBridge} from '../flow/types';
|
||||
import {HEADERS} from '../../src/lib/constants';
|
||||
|
||||
const buildAuthHeader = (user, pass): string => {
|
||||
return `Basic ${(new Buffer(`${user}:${pass}`)).toString('base64')}`;
|
||||
|
@ -25,7 +26,7 @@ export default class Server implements IServerBridge {
|
|||
assert(options.uri);
|
||||
const headers = options.headers || {};
|
||||
|
||||
headers.accept = headers.accept || 'application/json';
|
||||
headers.accept = headers.accept || HEADERS.JSON;
|
||||
headers['user-agent'] = headers['user-agent'] || this.userAgent;
|
||||
headers.authorization = headers.authorization || this.authstr;
|
||||
|
||||
|
@ -78,7 +79,7 @@ export default class Server implements IServerBridge {
|
|||
uri: `/${encodeURIComponent(name)}`,
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
'content-type': 'application/json',
|
||||
'content-type': HEADERS.JSON,
|
||||
},
|
||||
}).send(data);
|
||||
}
|
||||
|
@ -92,7 +93,7 @@ export default class Server implements IServerBridge {
|
|||
uri: `/${encodeURIComponent(name)}/${encodeURIComponent(version)}/-tag/latest`,
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
'content-type': 'application/json',
|
||||
'content-type': HEADERS.JSON,
|
||||
},
|
||||
}).send(data);
|
||||
}
|
||||
|
@ -110,7 +111,7 @@ export default class Server implements IServerBridge {
|
|||
uri: `/${encodeURIComponent(name)}/-/${encodeURIComponent(filename)}/whatever`,
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
'content-type': 'application/octet-stream',
|
||||
'content-type': HEADERS.OCTET_STREAM,
|
||||
},
|
||||
}).send(data);
|
||||
}
|
||||
|
@ -120,7 +121,7 @@ export default class Server implements IServerBridge {
|
|||
uri: `/${encodeURIComponent(name)}/-rev/whatever`,
|
||||
method: 'DELETE',
|
||||
headers: {
|
||||
'content-type': 'application/json; charset=utf-8',
|
||||
'content-type': HEADERS.JSON_CHARSET,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
@ -130,7 +131,7 @@ export default class Server implements IServerBridge {
|
|||
uri: `/${encodeURIComponent(name)}/-/${filename}/-rev/whatever`,
|
||||
method: 'DELETE',
|
||||
headers: {
|
||||
'content-type': 'application/json; charset=utf-8',
|
||||
'content-type': HEADERS.JSON_CHARSET,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
@ -141,7 +142,7 @@ export default class Server implements IServerBridge {
|
|||
uri: `/${encodeURIComponent(name)}/${encodeURIComponent(tag)}`,
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
'content-type': 'application/json',
|
||||
'content-type': HEADERS.JSON,
|
||||
},
|
||||
}).send(JSON.stringify(version));
|
||||
}
|
||||
|
@ -151,7 +152,7 @@ export default class Server implements IServerBridge {
|
|||
uri: `/${encodeURIComponent(name)}/-/${encodeURIComponent(filename)}/whatever`,
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
'content-type': 'application/octet-stream',
|
||||
'content-type': HEADERS.OCTET_STREAM,
|
||||
'content-length': size,
|
||||
},
|
||||
timeout: 1000,
|
||||
|
@ -208,7 +209,7 @@ export default class Server implements IServerBridge {
|
|||
uri: '/-/_debug',
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'content-type': 'application/json',
|
||||
'content-type': HEADERS.JSON,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ import _ from 'lodash';
|
|||
import path from 'path';
|
||||
import rimraf from 'rimraf';
|
||||
|
||||
import {HEADERS} from '../../src/lib/constants';
|
||||
import configDefault from './partials/config/access';
|
||||
import Config from '../../src/lib/config';
|
||||
import endPointAPI from '../../src/api/index';
|
||||
|
@ -45,7 +46,7 @@ describe('api with no limited access configuration', () => {
|
|||
test('should test fails on fetch endpoint /-/jquery', (done) => {
|
||||
request(app)
|
||||
.get('/jquery')
|
||||
.set('content-type', 'application/json; charset=utf-8')
|
||||
.set('content-type', HEADERS.JSON_CHARSET)
|
||||
.expect('Content-Type', /json/)
|
||||
.expect(404)
|
||||
.end(function(err, res) {
|
||||
|
@ -59,7 +60,7 @@ describe('api with no limited access configuration', () => {
|
|||
test('should success on fetch endpoint /-/vue', (done) => {
|
||||
request(app)
|
||||
.get('/vue')
|
||||
.set('content-type', 'application/json; charset=utf-8')
|
||||
.set('content-type', HEADERS.JSON_CHARSET)
|
||||
.expect('Content-Type', /json/)
|
||||
.expect(200)
|
||||
.end(function(err, res) {
|
||||
|
|
|
@ -8,6 +8,7 @@ import publishMetadata from './partials/publish-api';
|
|||
import forbiddenPlace from './partials/forbidden-place';
|
||||
import Config from '../../src/lib/config';
|
||||
import endPointAPI from '../../src/api/index';
|
||||
import {HEADERS} from '../../src/lib/constants';
|
||||
|
||||
require('../../src/lib/logger').setup([]);
|
||||
const credentials = { name: 'Jota', password: 'secretPass' };
|
||||
|
@ -260,7 +261,7 @@ describe('endpoint unit test', () => {
|
|||
|
||||
request(app)
|
||||
.get('/jquery')
|
||||
.set('content-type', 'application/json; charset=utf-8')
|
||||
.set('content-type', HEADERS.JSON_CHARSET)
|
||||
.expect('Content-Type', /json/)
|
||||
.expect(200)
|
||||
.end(function(err, res) {
|
||||
|
@ -278,7 +279,7 @@ describe('endpoint unit test', () => {
|
|||
|
||||
request(app)
|
||||
.get('/jquery/1.5.1')
|
||||
.set('content-type', 'application/json; charset=utf-8')
|
||||
.set('content-type', HEADERS.JSON_CHARSET)
|
||||
.expect('Content-Type', /json/)
|
||||
.expect(200)
|
||||
.end(function(err, res) {
|
||||
|
@ -296,7 +297,7 @@ describe('endpoint unit test', () => {
|
|||
|
||||
request(app)
|
||||
.get('/jquery/latest')
|
||||
.set('content-type', 'application/json; charset=utf-8')
|
||||
.set('content-type', HEADERS.JSON_CHARSET)
|
||||
.expect('Content-Type', /json/)
|
||||
.expect(200)
|
||||
.end(function(err, res) {
|
||||
|
@ -314,7 +315,7 @@ describe('endpoint unit test', () => {
|
|||
|
||||
request(app)
|
||||
.get('/jquery/never-will-exist-this-tag')
|
||||
.set('content-type', 'application/json; charset=utf-8')
|
||||
.set('content-type', HEADERS.JSON_CHARSET)
|
||||
.expect('Content-Type', /json/)
|
||||
.expect(404)
|
||||
.end(function(err, res) {
|
||||
|
@ -329,7 +330,7 @@ describe('endpoint unit test', () => {
|
|||
|
||||
request(app)
|
||||
.get('/@verdaccio/not-found')
|
||||
.set('content-type', 'application/json; charset=utf-8')
|
||||
.set('content-type', HEADERS.JSON_CHARSET)
|
||||
.expect('Content-Type', /json/)
|
||||
.expect(404)
|
||||
.end(function(err, res) {
|
||||
|
@ -344,7 +345,7 @@ describe('endpoint unit test', () => {
|
|||
|
||||
request(app)
|
||||
.get('/forbidden-place')
|
||||
.set('content-type', 'application/json; charset=utf-8')
|
||||
.set('content-type', HEADERS.JSON_CHARSET)
|
||||
.expect('Content-Type', /json/)
|
||||
.expect(403)
|
||||
.end(function(err, res) {
|
||||
|
@ -401,8 +402,8 @@ describe('endpoint unit test', () => {
|
|||
.put('/jquery/verdaccio-tag')
|
||||
.send(JSON.stringify(jqueryVersion))
|
||||
.set('accept', 'gzip')
|
||||
.set('accept-encoding', 'application/json')
|
||||
.set('content-type', 'application/json')
|
||||
.set('accept-encoding', HEADERS.JSON)
|
||||
.set('content-type', HEADERS.JSON)
|
||||
.expect(201)
|
||||
.end(function(err, res) {
|
||||
if (err) {
|
||||
|
@ -419,8 +420,8 @@ describe('endpoint unit test', () => {
|
|||
|
||||
request(app)
|
||||
.get('/-/package/jquery/dist-tags')
|
||||
.set('accept-encoding', 'application/json')
|
||||
.set('content-type', 'application/json')
|
||||
.set('accept-encoding', HEADERS.JSON)
|
||||
.set('content-type', HEADERS.JSON)
|
||||
.expect(200)
|
||||
.end(function(err, res) {
|
||||
if (err) {
|
||||
|
@ -438,7 +439,7 @@ describe('endpoint unit test', () => {
|
|||
request(app)
|
||||
.post('/-/package/jquery/dist-tags')
|
||||
.send(JSON.stringify(jqueryUpdatedVersion))
|
||||
.set('content-type', 'application/json')
|
||||
.set('content-type', HEADERS.JSON)
|
||||
.expect(201)
|
||||
.end(function(err, res) {
|
||||
if (err) {
|
||||
|
@ -455,8 +456,8 @@ describe('endpoint unit test', () => {
|
|||
|
||||
request(app)
|
||||
.get('/-/package/jquery/dist-tags')
|
||||
.set('accept-encoding', 'application/json')
|
||||
.set('content-type', 'application/json')
|
||||
.set('accept-encoding', HEADERS.JSON)
|
||||
.set('content-type', HEADERS.JSON)
|
||||
.expect(200)
|
||||
.end(function(err, res) {
|
||||
if (err) {
|
||||
|
@ -473,8 +474,8 @@ describe('endpoint unit test', () => {
|
|||
|
||||
request(app)
|
||||
.del('/-/package/jquery/dist-tags/verdaccio-tag')
|
||||
.set('accept-encoding', 'application/json')
|
||||
.set('content-type', 'application/json')
|
||||
.set('accept-encoding', HEADERS.JSON)
|
||||
.set('content-type', HEADERS.JSON)
|
||||
//.expect('Content-Type', /json/)
|
||||
.expect(201)
|
||||
.end(function(err, res) {
|
||||
|
@ -496,8 +497,8 @@ describe('endpoint unit test', () => {
|
|||
const cacheTime = now - 6000000;
|
||||
request(app)
|
||||
.get('/-/all/since?stale=update_after&startkey=' + cacheTime)
|
||||
// .set('accept-encoding', 'application/json')
|
||||
// .set('content-type', 'application/json')
|
||||
// .set('accept-encoding', HEADERS.JSON)
|
||||
// .set('content-type', HEADERS.JSON)
|
||||
//.expect('Content-Type', /json/)
|
||||
.expect(200)
|
||||
.end(function(err, res) {
|
||||
|
@ -516,7 +517,7 @@ describe('endpoint unit test', () => {
|
|||
test('should publish a new package', (done) => {
|
||||
request(app)
|
||||
.put('/@scope%2fpk1-test')
|
||||
.set('content-type', 'application/json')
|
||||
.set('content-type', HEADERS.JSON)
|
||||
.send(JSON.stringify(publishMetadata))
|
||||
.expect(201)
|
||||
.end(function(err, res) {
|
||||
|
@ -535,7 +536,7 @@ describe('endpoint unit test', () => {
|
|||
//FUTURE: for some reason it does not remove the scope folder
|
||||
request(app)
|
||||
.del('/@scope%2fpk1-test/-rev/4-6abcdb4efd41a576')
|
||||
.set('content-type', 'application/json')
|
||||
.set('content-type', HEADERS.JSON)
|
||||
.expect(201)
|
||||
.end(function(err, res) {
|
||||
if (err) {
|
||||
|
@ -553,13 +554,13 @@ describe('endpoint unit test', () => {
|
|||
beforeAll(async function() {
|
||||
await request(app)
|
||||
.put('/@scope%2fpk1-test')
|
||||
.set('content-type', 'application/json')
|
||||
.set('content-type', HEADERS.JSON)
|
||||
.send(JSON.stringify(publishMetadata))
|
||||
.expect(201);
|
||||
|
||||
await request(app)
|
||||
.put('/forbidden-place')
|
||||
.set('content-type', 'application/json')
|
||||
.set('content-type', HEADERS.JSON)
|
||||
.send(JSON.stringify(forbiddenPlace))
|
||||
.expect(201);
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue