original
Advanced tools
Comparing version 0.0.5 to 0.0.6
37
index.js
'use strict'; | ||
var parse = require('url-parse'); | ||
var required = require('requires-port') | ||
, parse = require('url-parse'); | ||
@@ -20,24 +21,26 @@ /** | ||
// | ||
if (!/^(http|ws|file)s?/.test(url)) url = 'http://'+ url; | ||
url = parse(url); | ||
if (!/^(http|ws|file)s?/i.test(url)) url = 'http://'+ url; | ||
url = parse(url.toLowerCase()); | ||
} | ||
var noport | ||
, protocol = url.protocol | ||
, port = url.port && +url.port; | ||
// | ||
// 6.2. ASCII Serialization of an Origin | ||
// http://tools.ietf.org/html/rfc6454#section-6.2 | ||
// | ||
// @TODO If we cannot generate a proper origin from the url because | ||
// origin/host/port information is missing we should return the string `null` | ||
// | ||
var protocol = url.protocol | ||
, port = url.port && +url.port | ||
, noport = !required(port, protocol); | ||
// | ||
// Origins should not include the default port number: | ||
// 4. Origin of a URI | ||
// http://tools.ietf.org/html/rfc6454#section-4 | ||
// | ||
// @see https://url.spec.whatwg.org/#default-port | ||
// @see https://url.spec.whatwg.org/#origin | ||
// States that url.scheme, host should be converted to lower case. This also | ||
// makes it easier to match origins as everything is just lower case. | ||
// | ||
if ( | ||
!port | ||
|| 'file:' === protocol | ||
|| (80 === port && ('http:' === protocol || 'ws:' === protocol )) | ||
|| (443 === port && ('https:' === protocol || 'wss:' === protocol)) | ||
) noport = true; | ||
return url.protocol +'//'+ url.hostname + (noport ? '' : ':'+ port); | ||
return (url.protocol +'//'+ url.hostname + (noport ? '' : ':'+ port)).toLowerCase(); | ||
} | ||
@@ -44,0 +47,0 @@ |
{ | ||
"name": "original", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"description": "Generate the origin from an URL or check if two URL/Origins are the same", | ||
@@ -23,3 +23,4 @@ "main": "index.js", | ||
"dependencies": { | ||
"url-parse": "0.0.x" | ||
"requires-port": "0.0.x", | ||
"url-parse": "0.1.x" | ||
}, | ||
@@ -29,5 +30,5 @@ "devDependencies": { | ||
"istanbul": "0.3.x", | ||
"mocha": "1.21.x", | ||
"mocha": "2.0.x", | ||
"pre-commit": "0.0.x" | ||
} | ||
} |
# origin(al) | ||
[![Build Status](https://travis-ci.org/unshiftio/original.svg?branch=master)](https://travis-ci.org/unshiftio/original) | ||
[![NPM version](https://badge.fury.io/js/original.svg)](http://badge.fury.io/js/original) | ||
[![Coverage Status](https://img.shields.io/coveralls/unshiftio/original.svg)](https://coveralls.io/r/unshiftio/original?branch=master) | ||
[![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/original.svg?style=flat-square)](http://browsenpm.org/package/original)[![Build Status](http://img.shields.io/travis/unshiftio/original/master.svg?style=flat-square)](https://travis-ci.org/unshiftio/original)[![Dependencies](https://img.shields.io/david/unshiftio/original.svg?style=flat-square)](https://david-dm.org/unshiftio/original)[![Coverage Status](http://img.shields.io/coveralls/unshiftio/original/master.svg?style=flat-square)](https://coveralls.io/r/unshiftio/original?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) | ||
@@ -7,0 +5,0 @@ Original generates the origin URL for a given URL or URL object. In addition to |
12
test.js
@@ -24,2 +24,14 @@ describe('original', function () { | ||
it('lowercases the origin', function () { | ||
var o = origin('hTtp://WwW.ExAMPLE.cOM:8080'); | ||
assume(o).equals('http://www.example.com:8080'); | ||
o = origin('https://www.EXAMPLE.com:8080'); | ||
assume(o).equals('https://www.example.com:8080'); | ||
o = origin('HTTPS://WWW.example.COM:8080'); | ||
assume(o).equals('https://www.example.com:8080'); | ||
}); | ||
it('also accepts missing protocols', function () { | ||
@@ -26,0 +38,0 @@ var o = origin('www.example.com'); |
7124
117
2
52
+ Addedrequires-port@0.0.x
+ Addedquerystringify@0.0.4(transitive)
+ Addedrequires-port@0.0.1(transitive)
+ Addedurl-parse@0.1.5(transitive)
- Removedurl-parse@0.0.4(transitive)
Updatedurl-parse@0.1.x