├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── bin.js ├── index.js ├── package.json └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - '0.10' 4 | - '0.12' 5 | - 'stable' 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Mathias Buus 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # hash-to-port 2 | 3 | Hash a value to a valid port 4 | 5 | ``` 6 | npm install hash-to-port 7 | ``` 8 | 9 | [![build status](http://img.shields.io/travis/mafintosh/hash-to-port.svg?style=flat)](http://travis-ci.org/mafintosh/hash-to-port) 10 | 11 | ## Usage 12 | 13 | ``` js 14 | var toPort = require('hash-to-port') 15 | 16 | console.log(toPort('example.com')) // returns 22019 17 | ``` 18 | 19 | ## API 20 | 21 | #### `var port = toPort(value, ?from, ?to)` 22 | 23 | Hash the given value to a valid port (`>= from <= to`). 24 | The same value will *always* hash to the same port but note that two different values might hash to the same port as well 25 | 26 | - `value` 27 | - `from` (default: `1024`) 28 | - `to` (default: `65535`) 29 | 30 | ## License 31 | 32 | MIT 33 | -------------------------------------------------------------------------------- /bin.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | var hash = require(".") 3 | 4 | var hostname = process.argv[2] 5 | var from = parseInt(process.argv[3]) 6 | var to = parseInt(process.argv[4]) 7 | 8 | console.log(hash(hostname, from, to)) 9 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var hash = require('hash-index') 2 | 3 | module.exports = function (hostname, from, to) { 4 | from = from || 1024 5 | to = to || 65535 6 | 7 | if (typeof from !== 'number' || typeof to !== 'number') { 8 | throw new TypeError('"to" and "from" arguments must be valid numbers!') 9 | } 10 | 11 | if (from >= to) { 12 | throw new RangeError('"to" must be greater or equal to than "from"') 13 | } 14 | 15 | if (from < 1024 || to > 65535) { 16 | throw new RangeError('port numbers must be between 1024 and 65535') 17 | } 18 | 19 | return hash(hostname, to - from) + from 20 | } 21 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "hash-to-port", 3 | "version": "1.2.0", 4 | "description": "Hash a value to a valid port", 5 | "main": "index.js", 6 | "bin": "bin.js", 7 | "scripts": { 8 | "test": "tape test.js" 9 | }, 10 | "dependencies": { 11 | "hash-index": "^3.0.0" 12 | }, 13 | "devDependencies": { 14 | "tape": "^4.2.1" 15 | }, 16 | "repository": { 17 | "type": "git", 18 | "url": "https://github.com/mafintosh/hash-to-port.git" 19 | }, 20 | "author": "Mathias Buus (@mafintosh)", 21 | "license": "MIT", 22 | "bugs": { 23 | "url": "https://github.com/mafintosh/hash-to-port/issues" 24 | }, 25 | "homepage": "https://github.com/mafintosh/hash-to-port" 26 | } 27 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | var tape = require('tape') 2 | var toPort = require('./') 3 | 4 | tape('is consistent', function (t) { 5 | t.same(toPort('test.com'), toPort('test.com')) 6 | t.same(toPort('example.com'), toPort('example.com')) 7 | t.same(toPort('example.com', 2000), toPort('example.com', 2000)) 8 | t.same(toPort('example.com', 2000, 3000), toPort('example.com', 2000, 3000)) 9 | t.end() 10 | }) 11 | 12 | tape('is >=1024 <=65535', function (t) { 13 | for (var i = 0; i < 5000; i++) { 14 | var port = toPort('example-' + i + '.com') 15 | t.ok(port <= 65535) 16 | t.ok(port >= 1024) 17 | } 18 | t.end() 19 | }) 20 | 21 | tape('rejects invalid port choices', function(t) { 22 | t.throws(function () { toPort('foo', 'not a number') }, /must be valid numbers/) 23 | t.throws(function () { toPort('foo', 2000, 'not a number') }, /must be valid numbers/) 24 | t.throws(function () { toPort('foo', 9001, 9000) }, /must be greater or equal to/) 25 | t.throws(function () { toPort('foo', 1023, 9000) }, /must be between 1024 and 65535/) 26 | t.throws(function () { toPort('foo', 1024, 65536) }, /must be between 1024 and 65535/) 27 | t.end() 28 | }) 29 | --------------------------------------------------------------------------------