0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2025-02-03 23:09:17 -05:00

add a code to bind on unix sockets

use "listen: 'unix:sinopia.sock'" for this
This commit is contained in:
Alex Kocharin 2015-04-22 02:36:57 +03:00
parent 137fd5978f
commit 97386397c2
4 changed files with 48 additions and 20 deletions

View file

@ -91,6 +91,7 @@ packages:
# - 0.0.0.0:4873 # listen on all addresses (INADDR_ANY) # - 0.0.0.0:4873 # listen on all addresses (INADDR_ANY)
# - https://example.org:4873 # if you want to use https # - https://example.org:4873 # if you want to use https
# - [::1]:4873 # ipv6 # - [::1]:4873 # ipv6
# - unix:/tmp/sinopia.sock # unix socket
# Configure HTTPS, it is required if you use "https" protocol above. # Configure HTTPS, it is required if you use "https" protocol above.
#https: #https:

View file

@ -77,16 +77,16 @@ function get_listen_addresses() {
} }
addresses = addresses.map(function(addr) { addresses = addresses.map(function(addr) {
addr = Utils.parse_address(addr) var parsed_addr = Utils.parse_address(addr)
if (!addr) { if (!parsed_addr) {
logger.logger.warn({ addr: addr }, logger.logger.warn({ addr: addr },
'invalid address - @{addr}, we expect a port (e.g. "4873"),' 'invalid address - @{addr}, we expect a port (e.g. "4873"),'
+ ' host:port (e.g. "localhost:4873") or full url' + ' host:port (e.g. "localhost:4873") or full url'
+ ' (e.g. "http://localhost:4873/")') + ' (e.g. "http://localhost:4873/")')
} }
return addr return parsed_addr
}).filter(Boolean) }).filter(Boolean)
@ -142,19 +142,25 @@ function afterConfigLoad() {
} }
webServer webServer
.listen(addr.port, addr.host) .listen(addr.port || addr.path, addr.host)
.on('error', function(err) { .on('error', function(err) {
logger.logger.fatal({ err: err }, 'cannot create server: @{err.message}') logger.logger.fatal({ err: err }, 'cannot create server: @{err.message}')
process.exit(2) process.exit(2)
}) })
logger.logger.warn({ logger.logger.warn({
addr: URL.format({ addr: ( addr.path
protocol: addr.proto, ? URL.format({
hostname: addr.host, protocol: 'unix',
port: addr.port, pathname: addr.path,
pathname: '/', })
}), : URL.format({
protocol: addr.proto,
hostname: addr.host,
port: addr.port,
pathname: '/',
})
),
version: 'Sinopia/'+pkg.version, version: 'Sinopia/'+pkg.version,
}, 'http address - @{addr}') }, 'http address - @{addr}')
}) })

View file

@ -126,23 +126,34 @@ module.exports.parse_address = function parse_address(addr) {
// //
// Allow: // Allow:
// //
// - https:localhost:1234 - protocol + host + port // - https:localhost:1234 - protocol + host + port
// - localhost:1234 - host + port // - localhost:1234 - host + port
// - 1234 - port // - 1234 - port
// - http::1234 - protocol + port // - http::1234 - protocol + port
// - https://localhost:443/ - full url + https // - https://localhost:443/ - full url + https
// - http://[::1]:443/ - ipv6 // - http://[::1]:443/ - ipv6
// - unix:/tmp/http.sock - unix sockets
// - https://unix:/tmp/http.sock - unix sockets (https)
//
// TODO: refactor it to something more reasonable?
// //
// protocol : // ( host )|( ipv6 ): port / // protocol : // ( host )|( ipv6 ): port /
var m = /^((https?):(\/\/)?)?((([^\/:]*)|\[([^\[\]]+)\]):)?(\d+)\/?$/.exec(addr) var m = /^((https?):(\/\/)?)?((([^\/:]*)|\[([^\[\]]+)\]):)?(\d+)\/?$/.exec(addr)
if (!m) return null if (m) return {
return {
proto: m[2] || 'http', proto: m[2] || 'http',
host: m[6] || m[7] || 'localhost', host: m[6] || m[7] || 'localhost',
port: m[8] || '4873', port: m[8] || '4873',
} }
var m = /^((https?):(\/\/)?)?unix:(.*)$/.exec(addr)
if (m) return {
proto: m[2] || 'http',
path: m[4],
}
return null
} }
// function filters out bad semver versions and sorts the array // function filters out bad semver versions and sorts the array

View file

@ -6,12 +6,17 @@ describe('Parse address', function() {
it(what, function() { it(what, function() {
if (proto === null) { if (proto === null) {
assert.strictEqual(parse(what), null) assert.strictEqual(parse(what), null)
} else { } else if (port) {
assert.deepEqual(parse(what), { assert.deepEqual(parse(what), {
proto: proto, proto: proto,
host: host, host: host,
port: port, port: port,
}) })
} else {
assert.deepEqual(parse(what), {
proto: proto,
path: host,
})
} }
}) })
} }
@ -26,7 +31,12 @@ describe('Parse address', function() {
addTest('[::1]:4873', 'http', '::1', '4873') addTest('[::1]:4873', 'http', '::1', '4873')
addTest('https:[::1]:4873', 'https', '::1', '4873') addTest('https:[::1]:4873', 'https', '::1', '4873')
addTest('unix:/tmp/foo.sock', 'http', '/tmp/foo.sock')
addTest('http:unix:foo.sock', 'http', 'foo.sock')
addTest('https://unix:foo.sock', 'https', 'foo.sock')
addTest('blah', null) addTest('blah', null)
addTest('blah://4873', null) addTest('blah://4873', null)
addTest('https://blah:4873///', null) addTest('https://blah:4873///', null)
addTest('unix:1234', 'http', 'unix', '1234') // not unix socket
}) })