2013-10-18 16:17:53 -05: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')
|
2013-11-24 12:06:01 -05:00
|
|
|
, Cats = require('./status-cats')
|
2013-10-18 16:17:53 -05:00
|
|
|
, basic_auth = Middleware.basic_auth
|
|
|
|
, validate_name = Middleware.validate_name
|
|
|
|
, media = Middleware.media
|
|
|
|
, expect_json = Middleware.expect_json
|
2013-05-31 01:26:11 -05:00
|
|
|
|
2014-02-01 03:08:48 -05:00
|
|
|
function match(regexp) {
|
|
|
|
return function(req, res, next, value, name) {
|
|
|
|
if (regexp.exec(value)) {
|
|
|
|
next()
|
|
|
|
} else {
|
|
|
|
next('route')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-07 20:16:28 -05:00
|
|
|
module.exports = function(config_hash) {
|
2013-10-26 07:18:36 -05:00
|
|
|
var config = new Config(config_hash)
|
|
|
|
, storage = new Storage(config)
|
2013-06-07 20:16:28 -05:00
|
|
|
|
|
|
|
var can = function(action) {
|
2013-06-14 02:10:50 -05:00
|
|
|
return function(req, res, next) {
|
2013-06-07 20:16:28 -05:00
|
|
|
if (config['allow_'+action](req.params.package, req.remoteUser)) {
|
2013-10-26 07:18:36 -05:00
|
|
|
next()
|
2013-06-07 20:16:28 -05:00
|
|
|
} else {
|
2013-10-11 00:32:59 -05:00
|
|
|
if (!req.remoteUser) {
|
2013-12-15 15:38:16 -05:00
|
|
|
if (req.remoteUserError) {
|
2014-02-23 12:20:50 -05:00
|
|
|
var msg = "can't "+action+' restricted package, ' + req.remoteUserError
|
2013-12-15 15:38:16 -05:00
|
|
|
} else {
|
|
|
|
var msg = "can't "+action+" restricted package without auth, did you forget 'npm set always-auth true'?"
|
|
|
|
}
|
2013-10-05 09:49:08 -05:00
|
|
|
next(new UError({
|
|
|
|
status: 403,
|
2013-12-15 15:38:16 -05:00
|
|
|
msg: msg,
|
2013-10-26 07:18:36 -05:00
|
|
|
}))
|
2013-10-05 09:49:08 -05:00
|
|
|
} else {
|
|
|
|
next(new UError({
|
|
|
|
status: 403,
|
|
|
|
msg: 'user '+req.remoteUser+' not allowed to '+action+' it'
|
2013-10-26 07:18:36 -05:00
|
|
|
}))
|
2013-10-05 09:49:08 -05:00
|
|
|
}
|
2013-06-07 20:16:28 -05:00
|
|
|
}
|
2013-10-26 07:18:36 -05:00
|
|
|
}
|
|
|
|
}
|
2013-05-31 17:57:28 -05:00
|
|
|
|
2013-10-26 07:18:36 -05:00
|
|
|
var app = express()
|
2013-10-11 04:49:00 -05:00
|
|
|
|
2013-12-11 18:19:40 -05:00
|
|
|
// 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 07:27:23 -05:00
|
|
|
function error_reporting_middleware(req, res, next) {
|
2013-10-26 07:18:36 -05:00
|
|
|
var calls = 0
|
2013-12-05 07:27:23 -05:00
|
|
|
res.report_error = res.report_error || function(err) {
|
2013-10-26 07:18:36 -05:00
|
|
|
calls++
|
2013-10-11 04:49:00 -05:00
|
|
|
if (err.status && err.status >= 400 && err.status < 600) {
|
|
|
|
if (calls == 1) {
|
2013-10-26 07:18:36 -05:00
|
|
|
res.status(err.status)
|
2014-03-28 20:08:28 -05:00
|
|
|
res.send({error: err.msg || err.message || 'unknown error'})
|
2013-10-11 04:49:00 -05:00
|
|
|
}
|
|
|
|
} else {
|
2013-10-18 16:17:53 -05:00
|
|
|
Logger.logger.error({err: err}, 'unexpected error: @{!err.message}\n@{err.stack}')
|
2013-12-05 07:31:21 -05:00
|
|
|
if (!res.status || !res.send) {
|
|
|
|
Logger.logger.error('this is an error in express.js, please report this')
|
|
|
|
res.destroy()
|
2014-06-19 23:12:37 -05:00
|
|
|
} else if (calls == 1) {
|
2013-10-26 07:18:36 -05:00
|
|
|
res.status(500)
|
|
|
|
res.send({error: 'internal server error'})
|
2014-06-19 23:12:37 -05:00
|
|
|
} else {
|
|
|
|
// socket should be already closed
|
2013-10-11 04:49:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-10-26 07:18:36 -05:00
|
|
|
next()
|
2013-12-05 07:27:23 -05:00
|
|
|
}
|
2013-10-11 04:49:00 -05:00
|
|
|
|
2013-12-05 07:27:23 -05:00
|
|
|
app.use(error_reporting_middleware)
|
2013-10-26 07:18:36 -05:00
|
|
|
app.use(Middleware.log_and_etagify)
|
2013-11-24 12:06:01 -05:00
|
|
|
app.use(function(req, res, next) {
|
2013-12-10 05:12:34 -05:00
|
|
|
res.setHeader('X-Powered-By', config.user_agent)
|
2013-11-24 12:06:01 -05:00
|
|
|
next()
|
|
|
|
})
|
|
|
|
app.use(Cats.middleware)
|
2013-06-14 02:10:50 -05:00
|
|
|
app.use(basic_auth(function(user, pass) {
|
2013-10-26 07:18:36 -05:00
|
|
|
return config.authenticate(user, pass)
|
|
|
|
}))
|
2014-01-18 14:04:12 -05:00
|
|
|
app.use(express.json({strict: false, limit: config.max_body_size || '10mb'}))
|
2013-10-26 07:18:36 -05:00
|
|
|
app.use(express.compress())
|
2013-12-08 22:59:31 -05:00
|
|
|
app.use(Middleware.anti_loop(config))
|
2013-06-20 12:10:33 -05:00
|
|
|
|
2014-02-01 03:08:48 -05:00
|
|
|
// validate all of these params as a package name
|
|
|
|
// this might be too harsh, so ask if it causes trouble
|
2013-10-26 07:18:36 -05:00
|
|
|
app.param('package', validate_name)
|
|
|
|
app.param('filename', validate_name)
|
2014-02-01 03:08:48 -05:00
|
|
|
app.param('tag', validate_name)
|
|
|
|
app.param('version', validate_name)
|
|
|
|
app.param('revision', validate_name)
|
|
|
|
|
|
|
|
// these can't be safely put into express url for some reason
|
|
|
|
app.param('_rev', match(/^-rev$/))
|
|
|
|
app.param('org_couchdb_user', match(/^org\.couchdb\.user:/))
|
2013-05-31 01:26:11 -05:00
|
|
|
|
|
|
|
/* app.get('/', function(req, res) {
|
|
|
|
res.send({
|
|
|
|
error: 'unimplemented'
|
2013-10-26 07:18:36 -05:00
|
|
|
})
|
|
|
|
})*/
|
2013-05-31 01:26:11 -05:00
|
|
|
|
2013-06-07 20:16:28 -05:00
|
|
|
/* app.get('/-/all', function(req, res) {
|
2013-10-26 07:18:36 -05:00
|
|
|
var https = require('https')
|
|
|
|
var JSONStream = require('JSONStream')
|
2013-06-07 20:16:28 -05:00
|
|
|
var request = require('request')({
|
|
|
|
url: 'https://registry.npmjs.org/-/all',
|
|
|
|
})
|
|
|
|
.pipe(JSONStream.parse('*'))
|
|
|
|
.on('data', function(d) {
|
2013-10-26 07:18:36 -05:00
|
|
|
console.log(d)
|
|
|
|
})
|
|
|
|
})*/
|
2013-06-07 20:16:28 -05:00
|
|
|
|
2013-06-13 09:21:14 -05:00
|
|
|
// TODO: anonymous user?
|
|
|
|
app.get('/:package/:version?', can('access'), function(req, res, next) {
|
2013-12-08 22:58:25 -05:00
|
|
|
storage.get_package(req.params.package, {req: req}, function(err, info) {
|
2013-10-26 07:18:36 -05:00
|
|
|
if (err) return next(err)
|
|
|
|
info = utils.filter_tarball_urls(info, req, config)
|
2013-06-13 09:21:14 -05:00
|
|
|
|
2013-10-26 07:18:36 -05:00
|
|
|
var version = req.params.version
|
2014-01-18 13:57:44 -05:00
|
|
|
, t
|
2013-10-02 13:26:20 -05:00
|
|
|
if (!version) {
|
2013-10-26 07:18:36 -05:00
|
|
|
return res.send(info)
|
2013-10-02 13:26:20 -05:00
|
|
|
}
|
|
|
|
|
2014-01-18 13:57:44 -05:00
|
|
|
if ((t = utils.get_version(info, version)) != null) {
|
|
|
|
return res.send(t)
|
2013-10-02 13:26:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (info['dist-tags'] != null) {
|
|
|
|
if (info['dist-tags'][version] != null) {
|
2013-10-26 07:18:36 -05:00
|
|
|
version = info['dist-tags'][version]
|
2014-01-18 13:57:44 -05:00
|
|
|
if ((t = utils.get_version(info, version)) != null) {
|
|
|
|
return res.send(t)
|
2013-10-02 13:26:20 -05:00
|
|
|
}
|
2013-06-13 09:21:14 -05:00
|
|
|
}
|
|
|
|
}
|
2013-10-02 13:26:20 -05:00
|
|
|
|
|
|
|
return next(new UError({
|
|
|
|
status: 404,
|
|
|
|
msg: 'version not found: ' + req.params.version
|
2013-10-26 07:18:36 -05:00
|
|
|
}))
|
|
|
|
})
|
|
|
|
})
|
2013-05-31 17:57:28 -05:00
|
|
|
|
2013-06-07 20:16:28 -05:00
|
|
|
app.get('/:package/-/:filename', can('access'), function(req, res, next) {
|
2013-10-26 07:18:36 -05:00
|
|
|
var stream = storage.get_tarball(req.params.package, req.params.filename)
|
2014-03-07 14:39:20 -05:00
|
|
|
stream.on('content-length', function(v) {
|
|
|
|
res.header('Content-Length', v)
|
|
|
|
})
|
2013-06-20 08:07:34 -05:00
|
|
|
stream.on('error', function(err) {
|
2013-10-26 07:18:36 -05:00
|
|
|
return res.report_error(err)
|
|
|
|
})
|
2014-03-07 14:39:20 -05:00
|
|
|
res.header('Content-Type', 'application/octet-stream')
|
2013-10-26 07:18:36 -05:00
|
|
|
stream.pipe(res)
|
|
|
|
})
|
|
|
|
|
2014-04-12 12:20:26 -05:00
|
|
|
// searching packages
|
|
|
|
app.get('/-/all/:package?', can('access'), function(req, res, next) {
|
2014-06-22 09:12:13 -05:00
|
|
|
storage.search(req.param.startkey || 0, {req: req}, function(err, result) {
|
2014-04-12 12:20:26 -05:00
|
|
|
if (err) return next(err)
|
|
|
|
|
|
|
|
return res.send(result)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2013-05-31 01:26:11 -05:00
|
|
|
//app.get('/*', function(req, res) {
|
2013-10-26 07:18:36 -05:00
|
|
|
// proxy.request(req, res)
|
|
|
|
//})
|
|
|
|
|
2013-05-31 01:26:11 -05: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 07:18:36 -05:00
|
|
|
})
|
2014-02-23 12:20:50 -05:00
|
|
|
res.send({'ok':true,'name':'somebody','roles':[]})
|
2013-10-26 07:18:36 -05:00
|
|
|
})
|
2013-06-07 20:16:28 -05:00
|
|
|
|
2014-02-01 03:08:48 -05:00
|
|
|
app.get('/-/user/:org_couchdb_user', function(req, res, next) {
|
2013-10-26 07:18:36 -05:00
|
|
|
res.status(200)
|
2013-06-07 20:16:28 -05:00
|
|
|
return res.send({
|
2013-12-27 06:29:23 -05:00
|
|
|
ok: 'you are authenticated as "' + req.remoteUser + '"',
|
2013-10-26 07:18:36 -05:00
|
|
|
})
|
|
|
|
})
|
2013-06-07 20:16:28 -05:00
|
|
|
|
2014-02-01 03:08:48 -05:00
|
|
|
app.put('/-/user/:org_couchdb_user', function(req, res, next) {
|
2013-10-26 07:18:36 -05:00
|
|
|
res.status(409)
|
2013-06-14 02:56:02 -05:00
|
|
|
return res.send({
|
|
|
|
error: 'registration is not implemented',
|
2013-10-26 07:18:36 -05:00
|
|
|
})
|
|
|
|
})
|
2013-06-14 02:56:02 -05:00
|
|
|
|
2014-02-01 03:08:48 -05:00
|
|
|
app.put('/-/user/:org_couchdb_user/-rev/*', function(req, res, next) {
|
2013-12-06 12:46:51 -05:00
|
|
|
if (req.remoteUser == null) {
|
|
|
|
res.status(403)
|
|
|
|
return res.send({
|
|
|
|
error: 'bad username/password, access denied',
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2013-10-26 07:18:36 -05:00
|
|
|
res.status(201)
|
2013-06-07 20:16:28 -05:00
|
|
|
return res.send({
|
2013-12-06 12:46:51 -05:00
|
|
|
ok: 'you are authenticated as "' + req.remoteUser + '"',
|
2013-10-26 07:18:36 -05:00
|
|
|
})
|
|
|
|
})
|
2013-05-31 01:26:11 -05:00
|
|
|
|
2013-12-27 08:06:30 -05:00
|
|
|
// tagging a package
|
|
|
|
app.put('/:package/:tag', can('publish'), media('application/json'), function(req, res, next) {
|
|
|
|
if (typeof(req.body) !== 'string') return next('route')
|
|
|
|
|
2014-04-12 21:04:48 -05:00
|
|
|
var tags = {}
|
|
|
|
tags[req.params.tag] = req.body
|
|
|
|
storage.add_tags(req.params.package, tags, function(err) {
|
2013-12-27 08:06:30 -05:00
|
|
|
if (err) return next(err)
|
|
|
|
res.status(201)
|
|
|
|
return res.send({
|
|
|
|
ok: 'package tagged'
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2013-05-31 01:26:11 -05:00
|
|
|
// publishing a package
|
2013-10-11 00:32:12 -05:00
|
|
|
app.put('/:package/:_rev?/:revision?', can('publish'), media('application/json'), expect_json, function(req, res, next) {
|
2013-10-23 01:15:17 -05:00
|
|
|
var name = req.params.package
|
2013-10-02 13:01:18 -05:00
|
|
|
|
2013-10-22 02:29:57 -05:00
|
|
|
if (Object.keys(req.body).length == 1 && utils.is_object(req.body.users)) {
|
2013-10-02 13:01:18 -05:00
|
|
|
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-10-23 01:15:17 -05:00
|
|
|
}))
|
2013-10-02 13:01:18 -05:00
|
|
|
}
|
|
|
|
|
2013-05-31 17:57:28 -05:00
|
|
|
try {
|
2013-10-23 01:15:17 -05:00
|
|
|
var metadata = utils.validate_metadata(req.body, name)
|
2013-05-31 17:57:28 -05:00
|
|
|
} catch(err) {
|
2013-06-07 20:16:28 -05:00
|
|
|
return next(new UError({
|
2013-05-31 17:57:28 -05:00
|
|
|
status: 422,
|
|
|
|
msg: 'bad incoming package data',
|
2013-10-23 01:15:17 -05:00
|
|
|
}))
|
2013-05-31 01:26:11 -05:00
|
|
|
}
|
|
|
|
|
2013-10-11 00:32:12 -05:00
|
|
|
if (req.params._rev) {
|
2013-10-23 01:15:17 -05:00
|
|
|
storage.change_package(name, metadata, req.params.revision, function(err) {
|
2013-12-28 19:53:31 -05:00
|
|
|
after_change(err, 'package changed')
|
2013-10-23 01:15:17 -05:00
|
|
|
})
|
2013-10-11 00:32:12 -05:00
|
|
|
} else {
|
|
|
|
storage.add_package(name, metadata, function(err) {
|
2013-12-28 19:53:31 -05:00
|
|
|
after_change(err, 'created new package')
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function after_change(err, ok_message) {
|
2013-12-28 19:58:48 -05:00
|
|
|
// old npm behaviour
|
|
|
|
if (metadata._attachments == null) {
|
2013-10-23 01:15:17 -05:00
|
|
|
if (err) return next(err)
|
2013-10-26 07:18:36 -05:00
|
|
|
res.status(201)
|
2013-10-11 00:32:12 -05:00
|
|
|
return res.send({
|
2013-12-28 19:53:31 -05:00
|
|
|
ok: ok_message
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// npm-registry-client 0.3+ embeds tarball into the json upload
|
|
|
|
// https://github.com/isaacs/npm-registry-client/commit/e9fbeb8b67f249394f735c74ef11fe4720d46ca0
|
|
|
|
// issue #31, dealing with it here:
|
|
|
|
|
|
|
|
if (typeof(metadata._attachments) != 'object'
|
|
|
|
|| Object.keys(metadata._attachments).length != 1
|
|
|
|
|| typeof(metadata.versions) != 'object'
|
|
|
|
|| Object.keys(metadata.versions).length != 1) {
|
|
|
|
|
|
|
|
// npm is doing something strange again
|
|
|
|
// if this happens in normal circumstances, report it as a bug
|
|
|
|
return next(new UError({
|
|
|
|
status: 400,
|
|
|
|
msg: 'unsupported registry call',
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
if (err && err.status != 409) return next(err)
|
|
|
|
|
|
|
|
// at this point document is either created or existed before
|
|
|
|
var t1 = Object.keys(metadata._attachments)[0]
|
|
|
|
create_tarball(t1, metadata._attachments[t1], function(err) {
|
2013-12-29 01:41:31 -05:00
|
|
|
if (err) return next(err)
|
2013-12-28 19:53:31 -05:00
|
|
|
|
|
|
|
var t2 = Object.keys(metadata.versions)[0]
|
|
|
|
create_version(t2, metadata.versions[t2], function(err) {
|
2013-12-29 01:41:31 -05:00
|
|
|
if (err) return next(err)
|
2013-12-28 19:58:48 -05:00
|
|
|
|
2014-04-12 21:04:48 -05:00
|
|
|
add_tags(metadata['dist-tags'], function(err) {
|
|
|
|
if (err) return next(err)
|
|
|
|
|
|
|
|
res.status(201)
|
|
|
|
return res.send({
|
|
|
|
ok: ok_message
|
|
|
|
})
|
2013-12-28 19:53:31 -05:00
|
|
|
})
|
2013-10-23 01:15:17 -05:00
|
|
|
})
|
|
|
|
})
|
2013-10-11 00:32:12 -05:00
|
|
|
}
|
2013-12-28 19:53:31 -05:00
|
|
|
|
|
|
|
function create_tarball(filename, data, cb) {
|
|
|
|
var stream = storage.add_tarball(name, filename)
|
|
|
|
stream.on('error', function(err) {
|
|
|
|
cb(err)
|
|
|
|
})
|
|
|
|
stream.on('success', function() {
|
|
|
|
cb()
|
|
|
|
})
|
|
|
|
|
|
|
|
// this is dumb and memory-consuming, but what choices do we have?
|
|
|
|
stream.end(new Buffer(data.data, 'base64'))
|
|
|
|
stream.done()
|
|
|
|
}
|
|
|
|
|
|
|
|
function create_version(version, data, cb) {
|
2014-03-28 21:31:34 -05:00
|
|
|
storage.add_version(name, version, data, null, cb)
|
2013-12-28 19:53:31 -05:00
|
|
|
}
|
2014-04-12 21:04:48 -05:00
|
|
|
|
|
|
|
function add_tags(tags, cb) {
|
|
|
|
storage.add_tags(name, tags, cb)
|
|
|
|
}
|
2013-10-23 01:15:17 -05:00
|
|
|
})
|
2013-05-31 01:26:11 -05:00
|
|
|
|
2013-10-06 03:27:50 -05: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 07:18:36 -05:00
|
|
|
if (err) return next(err)
|
|
|
|
res.status(201)
|
2013-10-06 03:27:50 -05:00
|
|
|
return res.send({
|
|
|
|
ok: 'package removed'
|
2013-10-26 07:18:36 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2013-10-06 03:27:50 -05:00
|
|
|
|
2013-10-23 01:15:17 -05: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 07:18:36 -05:00
|
|
|
if (err) return next(err)
|
|
|
|
res.status(201)
|
2013-10-23 01:15:17 -05:00
|
|
|
return res.send({
|
|
|
|
ok: 'tarball removed'
|
2013-10-26 07:18:36 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2013-10-23 01:15:17 -05:00
|
|
|
|
2013-05-31 01:26:11 -05:00
|
|
|
// uploading package tarball
|
2013-06-07 20:16:28 -05:00
|
|
|
app.put('/:package/-/:filename/*', can('publish'), media('application/octet-stream'), function(req, res, next) {
|
2013-10-26 07:18:36 -05:00
|
|
|
var name = req.params.package
|
2013-05-31 17:57:28 -05:00
|
|
|
|
2013-10-26 07:18:36 -05:00
|
|
|
var stream = storage.add_tarball(name, req.params.filename)
|
|
|
|
req.pipe(stream)
|
2013-09-24 01:28:26 -05:00
|
|
|
|
|
|
|
// checking if end event came before closing
|
2013-10-26 07:18:36 -05:00
|
|
|
var complete = false
|
2013-09-24 01:28:26 -05:00
|
|
|
req.on('end', function() {
|
2013-10-26 07:18:36 -05:00
|
|
|
complete = true
|
|
|
|
stream.done()
|
|
|
|
})
|
2013-09-24 01:28:26 -05:00
|
|
|
req.on('close', function() {
|
|
|
|
if (!complete) {
|
2013-10-26 07:18:36 -05:00
|
|
|
stream.abort()
|
2013-09-24 01:28:26 -05:00
|
|
|
}
|
2013-10-26 07:18:36 -05:00
|
|
|
})
|
2013-09-24 01:28:26 -05:00
|
|
|
|
2013-06-20 08:07:34 -05:00
|
|
|
stream.on('error', function(err) {
|
2013-10-26 07:18:36 -05:00
|
|
|
return res.report_error(err)
|
|
|
|
})
|
2013-09-27 06:31:28 -05:00
|
|
|
stream.on('success', function() {
|
2013-10-26 07:18:36 -05:00
|
|
|
res.status(201)
|
2013-05-31 17:57:28 -05:00
|
|
|
return res.send({
|
|
|
|
ok: 'tarball uploaded successfully'
|
2013-10-26 07:18:36 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2013-05-31 01:26:11 -05:00
|
|
|
// adding a version
|
2013-06-07 20:16:28 -05:00
|
|
|
app.put('/:package/:version/-tag/:tag', can('publish'), media('application/json'), expect_json, function(req, res, next) {
|
2013-10-26 07:18:36 -05:00
|
|
|
var name = req.params.package
|
|
|
|
, version = req.params.version
|
|
|
|
, tag = req.params.tag
|
2013-05-31 01:26:11 -05:00
|
|
|
|
2013-05-31 17:57:28 -05:00
|
|
|
storage.add_version(name, version, req.body, tag, function(err) {
|
2013-10-26 07:18:36 -05:00
|
|
|
if (err) return next(err)
|
|
|
|
res.status(201)
|
2013-05-31 17:57:28 -05:00
|
|
|
return res.send({
|
|
|
|
ok: 'package published'
|
2013-10-26 07:18:36 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2013-05-31 01:26:11 -05:00
|
|
|
|
2013-10-26 07:18:36 -05:00
|
|
|
app.use(app.router)
|
2013-05-31 17:57:28 -05:00
|
|
|
app.use(function(err, req, res, next) {
|
2013-12-05 07:27:23 -05: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 07:18:36 -05:00
|
|
|
res.report_error(err)
|
|
|
|
})
|
2013-05-31 17:57:28 -05:00
|
|
|
|
2013-10-26 07:18:36 -05:00
|
|
|
return app
|
|
|
|
}
|
2013-05-31 01:26:11 -05:00
|
|
|
|