8889841cindex.js000066600000001361150513541220006214 0ustar00'use strict'; /** * Check if we're required to add a port number. * * @see https://url.spec.whatwg.org/#default-port * @param {Number|String} port Port number we need to check * @param {String} protocol Protocol we need to check against. * @returns {Boolean} Is it a default port for the given protocol * @api private */ module.exports = function required(port, protocol) { protocol = protocol.split(':')[0]; port = +port; if (!port) return false; switch (protocol) { case 'http': case 'ws': return port !== 80; case 'https': case 'wss': return port !== 443; case 'ftp': return port !== 21; case 'gopher': return port !== 70; case 'file': return false; } return port !== 0; }; README.md000066600000003426150513541220006032 0ustar00# requires-port [![Made by unshift](https://img.shields.io/badge/made%20by-unshift-00ffcc.svg?style=flat-square)](http://unshift.io)[![Version npm](http://img.shields.io/npm/v/requires-port.svg?style=flat-square)](http://browsenpm.org/package/requires-port)[![Build Status](http://img.shields.io/travis/unshiftio/requires-port/master.svg?style=flat-square)](https://travis-ci.org/unshiftio/requires-port)[![Dependencies](https://img.shields.io/david/unshiftio/requires-port.svg?style=flat-square)](https://david-dm.org/unshiftio/requires-port)[![Coverage Status](http://img.shields.io/coveralls/unshiftio/requires-port/master.svg?style=flat-square)](https://coveralls.io/r/unshiftio/requires-port?branch=master)[![IRC channel](http://img.shields.io/badge/IRC-irc.freenode.net%23unshift-00a8ff.svg?style=flat-square)](http://webchat.freenode.net/?channels=unshift) The module name says it all, check if a protocol requires a given port. ## Installation This module is intended to be used with browserify or Node.js and is distributed in the public npm registry. To install it simply run the following command from your CLI: ```j npm install --save requires-port ``` ## Usage The module exports it self as function and requires 2 arguments: 1. The port number, can be a string or number. 2. Protocol, can be `http`, `http:` or even `https://yomoma.com`. We just split it at `:` and use the first result. We currently accept the following protocols: - `http` - `https` - `ws` - `wss` - `ftp` - `gopher` - `file` It returns a boolean that indicates if protocol requires this port to be added to your URL. ```js 'use strict'; var required = require('requires-port'); console.log(required('8080', 'http')) // true console.log(required('80', 'http')) // false ``` # License MIT .npmignore000066600000000026150513541220006543 0ustar00node_modules coverage .travis.yml000066600000000525150513541220006661 0ustar00sudo: false language: node_js node_js: - "4" - "iojs" - "0.12" - "0.10" script: - "npm run test-travis" after_script: - "npm install coveralls@2 && cat coverage/lcov.info | coveralls" matrix: fast_finish: true notifications: irc: channels: - "irc.freenode.org#unshift" on_success: change on_failure: change test.js000066600000006501150513541220006065 0ustar00describe('requires-port', function () { 'use strict'; var assume = require('assume') , required = require('./'); it('is exported as a function', function () { assume(required).is.a('function'); }); it('does not require empty ports', function () { assume(required('', 'http')).false(); assume(required('', 'wss')).false(); assume(required('', 'ws')).false(); assume(required('', 'cowsack')).false(); }); it('assumes true for unknown protocols',function () { assume(required('808', 'foo')).true(); assume(required('80', 'bar')).true(); }); it('never requires port numbers for file', function () { assume(required(8080, 'file')).false(); }); it('does not require port 80 for http', function () { assume(required('80', 'http')).false(); assume(required(80, 'http')).false(); assume(required(80, 'http://')).false(); assume(required(80, 'http://www.google.com')).false(); assume(required('8080', 'http')).true(); assume(required(8080, 'http')).true(); assume(required(8080, 'http://')).true(); assume(required(8080, 'http://www.google.com')).true(); }); it('does not require port 80 for ws', function () { assume(required('80', 'ws')).false(); assume(required(80, 'ws')).false(); assume(required(80, 'ws://')).false(); assume(required(80, 'ws://www.google.com')).false(); assume(required('8080', 'ws')).true(); assume(required(8080, 'ws')).true(); assume(required(8080, 'ws://')).true(); assume(required(8080, 'ws://www.google.com')).true(); }); it('does not require port 443 for https', function () { assume(required('443', 'https')).false(); assume(required(443, 'https')).false(); assume(required(443, 'https://')).false(); assume(required(443, 'https://www.google.com')).false(); assume(required('8080', 'https')).true(); assume(required(8080, 'https')).true(); assume(required(8080, 'https://')).true(); assume(required(8080, 'https://www.google.com')).true(); }); it('does not require port 443 for wss', function () { assume(required('443', 'wss')).false(); assume(required(443, 'wss')).false(); assume(required(443, 'wss://')).false(); assume(required(443, 'wss://www.google.com')).false(); assume(required('8080', 'wss')).true(); assume(required(8080, 'wss')).true(); assume(required(8080, 'wss://')).true(); assume(required(8080, 'wss://www.google.com')).true(); }); it('does not require port 21 for ftp', function () { assume(required('21', 'ftp')).false(); assume(required(21, 'ftp')).false(); assume(required(21, 'ftp://')).false(); assume(required(21, 'ftp://www.google.com')).false(); assume(required('8080', 'ftp')).true(); assume(required(8080, 'ftp')).true(); assume(required(8080, 'ftp://')).true(); assume(required(8080, 'ftp://www.google.com')).true(); }); it('does not require port 70 for gopher', function () { assume(required('70', 'gopher')).false(); assume(required(70, 'gopher')).false(); assume(required(70, 'gopher://')).false(); assume(required(70, 'gopher://www.google.com')).false(); assume(required('8080', 'gopher')).true(); assume(required(8080, 'gopher')).true(); assume(required(8080, 'gopher://')).true(); assume(required(8080, 'gopher://www.google.com')).true(); }); }); LICENSE000066600000002133150513541220005552 0ustar00The MIT License (MIT) Copyright (c) 2015 Unshift.io, Arnout Kazemier, the Contributors. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. package.json000066600000002147150513541220007040 0ustar00{ "name": "requires-port", "version": "1.0.0", "description": "Check if a protocol requires a certain port number to be added to an URL.", "main": "index.js", "scripts": { "100%": "istanbul check-coverage --statements 100 --functions 100 --lines 100 --branches 100", "test-travis": "istanbul cover _mocha --report lcovonly -- test.js", "coverage": "istanbul cover _mocha -- test.js", "watch": "mocha --watch test.js", "test": "mocha test.js" }, "repository": { "type": "git", "url": "https://github.com/unshiftio/requires-port" }, "keywords": [ "port", "require", "http", "https", "ws", "wss", "gopher", "file", "ftp", "requires", "requried", "portnumber", "url", "parsing", "validation", "cows" ], "author": "Arnout Kazemier", "license": "MIT", "bugs": { "url": "https://github.com/unshiftio/requires-port/issues" }, "homepage": "https://github.com/unshiftio/requires-port", "devDependencies": { "assume": "1.3.x", "istanbul": "0.4.x", "mocha": "2.3.x", "pre-commit": "1.1.x" } }