0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2025-01-06 22:40:26 -05:00

fix crash in #52

This commit is contained in:
Alex Kocharin 2014-03-07 18:20:41 +00:00
parent 3b510437a8
commit 9c4c93695b
4 changed files with 75 additions and 12 deletions

View file

@ -127,22 +127,33 @@ module.exports.log_and_etagify = function(req, res, next) {
var _send = res.send
res.send = function(body) {
if (typeof(body) === 'string' || typeof(body) === 'object') {
res.header('Content-type', 'application/json')
try {
if (typeof(body) === 'string' || typeof(body) === 'object') {
res.header('Content-type', 'application/json')
if (typeof(body) === 'object' && body != null) {
if (body.error) {
res._sinopia_error = body.error
if (typeof(body) === 'object' && body != null) {
if (body.error) {
res._sinopia_error = body.error
}
body = JSON.stringify(body, undefined, '\t') + '\n'
}
body = JSON.stringify(body, undefined, '\t') + '\n'
}
// don't send etags with errors
if (!res.statusCode || (res.statusCode >= 200 && res.statusCode < 300)) {
res.header('ETag', '"' + md5sum(body) + '"')
// don't send etags with errors
if (!res.statusCode || (res.statusCode >= 200 && res.statusCode < 300)) {
res.header('ETag', '"' + md5sum(body) + '"')
}
} else {
// send(null), send(204), etc.
}
} catch(err) {
// if sinopia sends headers first, and then calls res.send()
// as an error handler, we can't report error properly,
// and should just close socket
if (err.message.match(/set headers after they are sent/)) {
return res.socket.destroy()
} else {
throw err
}
} else {
// send(null), send(204), etc.
}
res.send = _send

View file

@ -7,6 +7,7 @@ users:
uplinks:
express:
url: http://localhost:55550/
timeout: 100
server2:
url: http://localhost:55552/
baduplink:

View file

@ -41,6 +41,7 @@ describe('Func', function() {
require('./tags')()
require('./mirror')()
require('./race')()
require('./racycrash')()
require('./security')()
require('./addtag')()
})

View file

@ -0,0 +1,50 @@
var assert = require('assert')
, ex = module.exports
module.exports = function() {
var server = process.server
var express = process.express
describe('Racy', function() {
it('should not crash on error if client disconnects', function(_cb) {
express.get('/testexp-racycrash', function(_, res) {
res.send({
"name": "testexp-racycrash",
"versions": {
"0.1.0": {
"name": "testexp_tags",
"version": "0.1.0",
"dist": {
"shasum": "fake",
"tarball": "http://localhost:55550/testexp-racycrash/-/test.tar.gz"
}
}
}
})
})
express.get('/testexp-racycrash/-/test.tar.gz', function(_, res) {
res.header('content-length', 1e6)
res.write('test test test\n')
setTimeout(function() {
res.write('test test test\n')
res.socket.destroy()
cb()
}, 200)
})
server.request({uri:'/testexp-racycrash/-/test.tar.gz'}, function(err, res, body) {
assert.equal(body, 'test test test\n')
})
function cb() {
// test for NOT crashing
server.request({uri:'/testexp-racycrash'}, function(err, res, body) {
assert.equal(res.statusCode, 200)
_cb()
})
}
})
})
}