1
0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-11-13 03:35:52 +01:00
verdaccio/lib/index.js

317 lines
8.4 KiB
JavaScript
Raw Normal View History

2013-10-18 23:17:53 +02:00
var express = require('express')
, cookies = require('cookies')
, utils = require('./utils')
, Storage = require('./storage')
, Config = require('./config')
, UError = require('./error').UserError
, Middleware = require('./middleware')
, Logger = require('./logger')
, Cats = require('./status-cats')
2013-10-18 23:17:53 +02:00
, basic_auth = Middleware.basic_auth
, validate_name = Middleware.validate_name
, media = Middleware.media
, expect_json = Middleware.expect_json
2013-05-31 08:26:11 +02:00
2013-06-08 03:16:28 +02:00
module.exports = function(config_hash) {
2013-10-26 14:18:36 +02:00
var config = new Config(config_hash)
, storage = new Storage(config)
2013-06-08 03:16:28 +02:00
var can = function(action) {
return function(req, res, next) {
2013-06-08 03:16:28 +02:00
if (config['allow_'+action](req.params.package, req.remoteUser)) {
2013-10-26 14:18:36 +02:00
next()
2013-06-08 03:16:28 +02:00
} else {
if (!req.remoteUser) {
2013-10-05 16:49:08 +02:00
next(new UError({
status: 403,
msg: "can't "+action+" restricted package without auth, did you forget 'npm set always-auth true'?",
2013-10-26 14:18:36 +02:00
}))
2013-10-05 16:49:08 +02:00
} else {
next(new UError({
status: 403,
msg: 'user '+req.remoteUser+' not allowed to '+action+' it'
2013-10-26 14:18:36 +02:00
}))
2013-10-05 16:49:08 +02:00
}
2013-06-08 03:16:28 +02:00
}
2013-10-26 14:18:36 +02:00
}
}
2013-06-01 00:57:28 +02:00
2013-10-26 14:18:36 +02:00
var app = express()
// run in production mode by default, just in case
// it shouldn't make any difference anyway
app.set('env', process.env.NODE_ENV || 'production')
2013-12-05 13:27:23 +01:00
function error_reporting_middleware(req, res, next) {
2013-10-26 14:18:36 +02:00
var calls = 0
2013-12-05 13:27:23 +01:00
res.report_error = res.report_error || function(err) {
2013-10-26 14:18:36 +02:00
calls++
if (err.status && err.status >= 400 && err.status < 600) {
if (calls == 1) {
2013-10-26 14:18:36 +02:00
res.status(err.status)
res.send({error: err.msg || err.message || 'unknown error'})
}
} else {
2013-10-18 23:17:53 +02:00
Logger.logger.error({err: err}, 'unexpected error: @{!err.message}\n@{err.stack}')
if (!res.status || !res.send) {
Logger.logger.error('this is an error in express.js, please report this')
res.destroy()
}
if (calls == 1) {
2013-10-26 14:18:36 +02:00
res.status(500)
res.send({error: 'internal server error'})
}
}
}
2013-10-26 14:18:36 +02:00
next()
2013-12-05 13:27:23 +01:00
}
2013-12-05 13:27:23 +01:00
app.use(error_reporting_middleware)
2013-10-26 14:18:36 +02:00
app.use(Middleware.log_and_etagify)
app.use(function(req, res, next) {
res.setHeader('X-Powered-By', config.user_agent)
next()
})
app.use(Cats.middleware)
app.use(basic_auth(function(user, pass) {
2013-10-26 14:18:36 +02:00
return config.authenticate(user, pass)
}))
app.use(express.json({strict: false}))
2013-06-08 03:16:28 +02:00
// TODO: npm DO NOT support compression :(
2013-10-26 14:18:36 +02:00
app.use(express.compress())
2013-12-09 04:59:31 +01:00
app.use(Middleware.anti_loop(config))
2013-10-26 14:18:36 +02:00
app.param('package', validate_name)
app.param('filename', validate_name)
2013-05-31 08:26:11 +02:00
/* app.get('/', function(req, res) {
res.send({
error: 'unimplemented'
2013-10-26 14:18:36 +02:00
})
})*/
2013-05-31 08:26:11 +02:00
2013-06-08 03:16:28 +02:00
/* app.get('/-/all', function(req, res) {
2013-10-26 14:18:36 +02:00
var https = require('https')
var JSONStream = require('JSONStream')
2013-06-08 03:16:28 +02:00
var request = require('request')({
url: 'https://registry.npmjs.org/-/all',
ca: require('./npmsslkeys'),
})
.pipe(JSONStream.parse('*'))
.on('data', function(d) {
2013-10-26 14:18:36 +02:00
console.log(d)
})
})*/
2013-06-08 03:16:28 +02:00
2013-06-13 16:21:14 +02:00
// TODO: anonymous user?
app.get('/:package/:version?', can('access'), function(req, res, next) {
2013-12-09 04:58:25 +01:00
storage.get_package(req.params.package, {req: req}, function(err, info) {
2013-10-26 14:18:36 +02:00
if (err) return next(err)
info = utils.filter_tarball_urls(info, req, config)
2013-06-13 16:21:14 +02:00
2013-10-26 14:18:36 +02:00
var version = req.params.version
if (!version) {
2013-10-26 14:18:36 +02:00
return res.send(info)
}
if (info.versions[version] != null) {
2013-10-26 14:18:36 +02:00
return res.send(info.versions[version])
}
if (info['dist-tags'] != null) {
if (info['dist-tags'][version] != null) {
2013-10-26 14:18:36 +02:00
version = info['dist-tags'][version]
if (info.versions[version] != null) {
2013-10-26 14:18:36 +02:00
return res.send(info.versions[version])
}
2013-06-13 16:21:14 +02:00
}
}
return next(new UError({
status: 404,
msg: 'version not found: ' + req.params.version
2013-10-26 14:18:36 +02:00
}))
})
})
2013-06-01 00:57:28 +02:00
2013-06-08 03:16:28 +02:00
app.get('/:package/-/:filename', can('access'), function(req, res, next) {
2013-10-26 14:18:36 +02:00
var stream = storage.get_tarball(req.params.package, req.params.filename)
2013-06-20 15:07:34 +02:00
stream.on('error', function(err) {
2013-10-26 14:18:36 +02:00
return res.report_error(err)
})
res.header('content-type', 'application/octet-stream')
stream.pipe(res)
})
2013-05-31 08:26:11 +02:00
//app.get('/*', function(req, res) {
2013-10-26 14:18:36 +02:00
// proxy.request(req, res)
//})
2013-05-31 08:26:11 +02:00
// placeholder 'cause npm require to be authenticated to publish
// we do not do any real authentication yet
app.post('/_session', cookies.express(), function(req, res) {
res.cookies.set('AuthSession', String(Math.random()), {
// npmjs.org sets 10h expire
expires: new Date(Date.now() + 10*60*60*1000)
2013-10-26 14:18:36 +02:00
})
res.send({"ok":true,"name":"somebody","roles":[]})
})
2013-06-08 03:16:28 +02:00
app.get('/-/user/:argument', function(req, res, next) {
// can't put 'org.couchdb.user' in route address for some reason
2013-10-26 14:18:36 +02:00
if (req.params.argument.split(':')[0] !== 'org.couchdb.user') return next('route')
res.status(200)
2013-06-08 03:16:28 +02:00
return res.send({
2013-06-14 09:56:02 +02:00
ok: 'you are authenticated as "' + req.user + '"',
2013-10-26 14:18:36 +02:00
})
})
2013-06-08 03:16:28 +02:00
app.put('/-/user/:argument', function(req, res, next) {
2013-06-14 09:56:02 +02:00
// can't put 'org.couchdb.user' in route address for some reason
2013-10-26 14:18:36 +02:00
if (req.params.argument.split(':')[0] !== 'org.couchdb.user') return next('route')
res.status(409)
2013-06-14 09:56:02 +02:00
return res.send({
error: 'registration is not implemented',
2013-10-26 14:18:36 +02:00
})
})
2013-06-14 09:56:02 +02:00
app.put('/-/user/:argument/-rev/*', function(req, res, next) {
2013-06-08 03:16:28 +02:00
// can't put 'org.couchdb.user' in route address for some reason
2013-10-26 14:18:36 +02:00
if (req.params.argument.split(':')[0] !== 'org.couchdb.user') return next('route')
2013-12-06 18:46:51 +01:00
if (req.remoteUser == null) {
res.status(403)
return res.send({
error: 'bad username/password, access denied',
})
}
2013-10-26 14:18:36 +02:00
res.status(201)
2013-06-08 03:16:28 +02:00
return res.send({
2013-12-06 18:46:51 +01:00
ok: 'you are authenticated as "' + req.remoteUser + '"',
2013-10-26 14:18:36 +02:00
})
})
2013-05-31 08:26:11 +02:00
// publishing a package
app.put('/:package/:_rev?/:revision?', can('publish'), media('application/json'), expect_json, function(req, res, next) {
if (req.params._rev != null && req.params._rev != '-rev') return next('route')
var name = req.params.package
if (Object.keys(req.body).length == 1 && utils.is_object(req.body.users)) {
return next(new UError({
// 501 status is more meaningful, but npm doesn't show error message for 5xx
status: 404,
msg: 'npm star|unstar calls are not implemented',
}))
}
2013-06-01 00:57:28 +02:00
try {
var metadata = utils.validate_metadata(req.body, name)
2013-06-01 00:57:28 +02:00
} catch(err) {
2013-06-08 03:16:28 +02:00
return next(new UError({
2013-06-01 00:57:28 +02:00
status: 422,
msg: 'bad incoming package data',
}))
2013-05-31 08:26:11 +02:00
}
if (req.params._rev) {
storage.change_package(name, metadata, req.params.revision, function(err) {
if (err) return next(err)
res.status(201)
return res.send({
ok: 'package changed'
})
})
} else {
storage.add_package(name, metadata, function(err) {
if (err) return next(err)
2013-10-26 14:18:36 +02:00
res.status(201)
return res.send({
ok: 'created new package'
})
})
}
})
2013-05-31 08:26:11 +02:00
// unpublishing an entire package
app.delete('/:package/-rev/*', can('publish'), function(req, res, next) {
storage.remove_package(req.params.package, function(err) {
2013-10-26 14:18:36 +02:00
if (err) return next(err)
res.status(201)
return res.send({
ok: 'package removed'
2013-10-26 14:18:36 +02:00
})
})
})
// removing a tarball
app.delete('/:package/-/:filename/-rev/:revision', can('publish'), function(req, res, next) {
storage.remove_tarball(req.params.package, req.params.filename, req.params.revision, function(err) {
2013-10-26 14:18:36 +02:00
if (err) return next(err)
res.status(201)
return res.send({
ok: 'tarball removed'
2013-10-26 14:18:36 +02:00
})
})
})
2013-05-31 08:26:11 +02:00
// uploading package tarball
2013-06-08 03:16:28 +02:00
app.put('/:package/-/:filename/*', can('publish'), media('application/octet-stream'), function(req, res, next) {
2013-10-26 14:18:36 +02:00
var name = req.params.package
2013-06-01 00:57:28 +02:00
2013-10-26 14:18:36 +02:00
var stream = storage.add_tarball(name, req.params.filename)
req.pipe(stream)
2013-09-24 08:28:26 +02:00
// checking if end event came before closing
2013-10-26 14:18:36 +02:00
var complete = false
2013-09-24 08:28:26 +02:00
req.on('end', function() {
2013-10-26 14:18:36 +02:00
complete = true
stream.done()
})
2013-09-24 08:28:26 +02:00
req.on('close', function() {
if (!complete) {
2013-10-26 14:18:36 +02:00
stream.abort()
2013-09-24 08:28:26 +02:00
}
2013-10-26 14:18:36 +02:00
})
2013-09-24 08:28:26 +02:00
2013-06-20 15:07:34 +02:00
stream.on('error', function(err) {
2013-10-26 14:18:36 +02:00
return res.report_error(err)
})
2013-09-27 13:31:28 +02:00
stream.on('success', function() {
2013-10-26 14:18:36 +02:00
res.status(201)
2013-06-01 00:57:28 +02:00
return res.send({
ok: 'tarball uploaded successfully'
2013-10-26 14:18:36 +02:00
})
})
})
2013-05-31 08:26:11 +02:00
// adding a version
2013-06-08 03:16:28 +02:00
app.put('/:package/:version/-tag/:tag', can('publish'), media('application/json'), expect_json, function(req, res, next) {
2013-10-26 14:18:36 +02:00
var name = req.params.package
, version = req.params.version
, tag = req.params.tag
2013-05-31 08:26:11 +02:00
2013-06-01 00:57:28 +02:00
storage.add_version(name, version, req.body, tag, function(err) {
2013-10-26 14:18:36 +02:00
if (err) return next(err)
res.status(201)
2013-06-01 00:57:28 +02:00
return res.send({
ok: 'package published'
2013-10-26 14:18:36 +02:00
})
})
})
2013-05-31 08:26:11 +02:00
2013-10-26 14:18:36 +02:00
app.use(app.router)
2013-06-01 00:57:28 +02:00
app.use(function(err, req, res, next) {
2013-12-05 13:27:23 +01:00
if (typeof(res.report_error) !== 'function') {
// in case of very early error this middleware may not be loaded before error is generated
// fixing that
error_reporting_middleware(req, res, function(){})
}
2013-10-26 14:18:36 +02:00
res.report_error(err)
})
2013-06-01 00:57:28 +02:00
2013-10-26 14:18:36 +02:00
return app
}
2013-05-31 08:26:11 +02:00