├── .github └── workflows │ └── ci.yml ├── .gitignore ├── .npmignore ├── .npmrc ├── LICENSE ├── README.md ├── index.js ├── package.json └── test.js /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: 4 | - push 5 | - pull_request 6 | 7 | jobs: 8 | test: 9 | runs-on: ubuntu-latest 10 | strategy: 11 | matrix: 12 | node: 13 | - 14 14 | - 16 15 | - 18 16 | steps: 17 | - uses: actions/checkout@v3 18 | - uses: actions/setup-node@v3 19 | with: 20 | node-version: ${{ matrix.node }} 21 | - run: npm install 22 | - run: npm test 23 | - uses: coverallsapp/github-action@1.1.3 24 | if: matrix.node == 18 25 | with: 26 | github-token: ${{ secrets.GITHUB_TOKEN }} 27 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | coverage 3 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | coverage/ 2 | test.js 3 | .* 4 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Unshift.io, Arnout Kazemier, the Contributors. 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 all 13 | 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 THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # requires-port 2 | 3 | [![Version npm](https://img.shields.io/npm/v/requires-port.svg?style=flat-square)](https://www.npmjs.com/package/requires-port)[![CI](https://img.shields.io/github/actions/workflow/status/unshiftio/requires-port/ci.yml?branch=master&label=CI&style=flat-square)](https://github.com/unshiftio/requires-port/actions?query=workflow%3ACI+branch%3Amaster)[![Coverage Status](https://img.shields.io/coveralls/unshiftio/requires-port/master.svg?style=flat-square)](https://coveralls.io/r/unshiftio/requires-port?branch=master) 4 | 5 | The module name says it all, check if a protocol requires a given port. 6 | 7 | ## Installation 8 | 9 | This module is intended to be used with browserify or Node.js and is distributed 10 | in the public npm registry. To install it simply run the following command from 11 | your CLI: 12 | 13 | ``` 14 | npm install --save requires-port 15 | ``` 16 | 17 | ## Usage 18 | 19 | The module exports it self as function and requires 2 arguments: 20 | 21 | 1. The port number, can be a string or number. 22 | 2. Protocol, can be `http`, `http:` or even `https://yomoma.com`. We just split 23 | it at `:` and use the first result. We currently accept the following 24 | protocols: 25 | - `http` 26 | - `https` 27 | - `ws` 28 | - `wss` 29 | - `ftp` 30 | - `gopher` 31 | - `file` 32 | 33 | It returns a boolean that indicates if protocol requires this port to be added 34 | to your URL. 35 | 36 | ```js 37 | 'use strict'; 38 | 39 | var required = require('requires-port'); 40 | 41 | console.log(required('8080', 'http')) // true 42 | console.log(required('80', 'http')) // false 43 | ``` 44 | 45 | # License 46 | 47 | MIT 48 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | /** 4 | * Check if we're required to add a port number. 5 | * 6 | * @see https://url.spec.whatwg.org/#default-port 7 | * @param {Number|String} port Port number we need to check 8 | * @param {String} protocol Protocol we need to check against. 9 | * @returns {Boolean} Is it a default port for the given protocol 10 | * @api private 11 | */ 12 | module.exports = function required(port, protocol) { 13 | protocol = protocol.split(':')[0]; 14 | port = +port; 15 | 16 | if (!port) return false; 17 | 18 | switch (protocol) { 19 | case 'http': 20 | case 'ws': 21 | return port !== 80; 22 | 23 | case 'https': 24 | case 'wss': 25 | return port !== 443; 26 | 27 | case 'ftp': 28 | return port !== 21; 29 | 30 | case 'gopher': 31 | return port !== 70; 32 | 33 | case 'file': 34 | return false; 35 | } 36 | 37 | return port !== 0; 38 | }; 39 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "requires-port", 3 | "version": "1.0.0", 4 | "description": "Check if a protocol requires a certain port number to be added to an URL.", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "c8 --reporter=lcov --reporter=text mocha test.js", 8 | "watch": "mocha --watch test.js" 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "https://github.com/unshiftio/requires-port" 13 | }, 14 | "keywords": [ 15 | "port", 16 | "require", 17 | "http", 18 | "https", 19 | "ws", 20 | "wss", 21 | "gopher", 22 | "file", 23 | "ftp", 24 | "requires", 25 | "required", 26 | "portnumber", 27 | "url", 28 | "parsing", 29 | "validation", 30 | "cows" 31 | ], 32 | "author": "Arnout Kazemier", 33 | "license": "MIT", 34 | "bugs": { 35 | "url": "https://github.com/unshiftio/requires-port/issues" 36 | }, 37 | "homepage": "https://github.com/unshiftio/requires-port", 38 | "devDependencies": { 39 | "assume": "~2.3.0", 40 | "c8": "~7.12.0", 41 | "mocha": "~10.2.0", 42 | "pre-commit": "~1.2.0" 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | describe('requires-port', function () { 2 | 'use strict'; 3 | 4 | var assume = require('assume') 5 | , required = require('./'); 6 | 7 | it('is exported as a function', function () { 8 | assume(required).is.a('function'); 9 | }); 10 | 11 | it('does not require empty ports', function () { 12 | assume(required('', 'http')).false(); 13 | assume(required('', 'wss')).false(); 14 | assume(required('', 'ws')).false(); 15 | assume(required('', 'cowsack')).false(); 16 | }); 17 | 18 | it('assumes true for unknown protocols',function () { 19 | assume(required('808', 'foo')).true(); 20 | assume(required('80', 'bar')).true(); 21 | }); 22 | 23 | it('never requires port numbers for file', function () { 24 | assume(required(8080, 'file')).false(); 25 | }); 26 | 27 | it('does not require port 80 for http', function () { 28 | assume(required('80', 'http')).false(); 29 | assume(required(80, 'http')).false(); 30 | assume(required(80, 'http://')).false(); 31 | assume(required(80, 'http://www.google.com')).false(); 32 | 33 | assume(required('8080', 'http')).true(); 34 | assume(required(8080, 'http')).true(); 35 | assume(required(8080, 'http://')).true(); 36 | assume(required(8080, 'http://www.google.com')).true(); 37 | }); 38 | 39 | it('does not require port 80 for ws', function () { 40 | assume(required('80', 'ws')).false(); 41 | assume(required(80, 'ws')).false(); 42 | assume(required(80, 'ws://')).false(); 43 | assume(required(80, 'ws://www.google.com')).false(); 44 | 45 | assume(required('8080', 'ws')).true(); 46 | assume(required(8080, 'ws')).true(); 47 | assume(required(8080, 'ws://')).true(); 48 | assume(required(8080, 'ws://www.google.com')).true(); 49 | }); 50 | 51 | it('does not require port 443 for https', function () { 52 | assume(required('443', 'https')).false(); 53 | assume(required(443, 'https')).false(); 54 | assume(required(443, 'https://')).false(); 55 | assume(required(443, 'https://www.google.com')).false(); 56 | 57 | assume(required('8080', 'https')).true(); 58 | assume(required(8080, 'https')).true(); 59 | assume(required(8080, 'https://')).true(); 60 | assume(required(8080, 'https://www.google.com')).true(); 61 | }); 62 | 63 | it('does not require port 443 for wss', function () { 64 | assume(required('443', 'wss')).false(); 65 | assume(required(443, 'wss')).false(); 66 | assume(required(443, 'wss://')).false(); 67 | assume(required(443, 'wss://www.google.com')).false(); 68 | 69 | assume(required('8080', 'wss')).true(); 70 | assume(required(8080, 'wss')).true(); 71 | assume(required(8080, 'wss://')).true(); 72 | assume(required(8080, 'wss://www.google.com')).true(); 73 | }); 74 | 75 | it('does not require port 21 for ftp', function () { 76 | assume(required('21', 'ftp')).false(); 77 | assume(required(21, 'ftp')).false(); 78 | assume(required(21, 'ftp://')).false(); 79 | assume(required(21, 'ftp://www.google.com')).false(); 80 | 81 | assume(required('8080', 'ftp')).true(); 82 | assume(required(8080, 'ftp')).true(); 83 | assume(required(8080, 'ftp://')).true(); 84 | assume(required(8080, 'ftp://www.google.com')).true(); 85 | }); 86 | 87 | it('does not require port 70 for gopher', function () { 88 | assume(required('70', 'gopher')).false(); 89 | assume(required(70, 'gopher')).false(); 90 | assume(required(70, 'gopher://')).false(); 91 | assume(required(70, 'gopher://www.google.com')).false(); 92 | 93 | assume(required('8080', 'gopher')).true(); 94 | assume(required(8080, 'gopher')).true(); 95 | assume(required(8080, 'gopher://')).true(); 96 | assume(required(8080, 'gopher://www.google.com')).true(); 97 | }); 98 | }); 99 | --------------------------------------------------------------------------------