http-link-header
Advanced tools
Comparing version 0.3.0 to 0.4.0
109
lib/link.js
@@ -14,2 +14,3 @@ var querystring = require( 'querystring' ) | ||
/** @type {Array} URI references */ | ||
this.refs = [] | ||
@@ -19,2 +20,14 @@ | ||
/** | ||
* General matching pattern | ||
* @type {RegExp} | ||
*/ | ||
Link.pattern = /(?:\<([^\>]+)\>)((\s*;\s*([a-z\*]+)=(("[^"]+")|('[^']+')|([^\,\;]+)))*)(\s*,\s*|$)/gi | ||
/** | ||
* Attribute matching pattern | ||
* @type {RegExp} | ||
*/ | ||
Link.attrPattern = /([a-z\*]+)=(?:(?:"([^"]+)")|(?:'([^']+)')|([^\,\;]+))/gi | ||
function trim( value ) { | ||
@@ -24,29 +37,27 @@ return value.replace( /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '' ) | ||
function parseLine( value ) { | ||
var pattern = /(?:[^;].*?)(?:[^\\](?=;)|$)/g | ||
var match = null | ||
var parts = [] | ||
while( match = pattern.exec( value ) ) { | ||
parts.push( trim( match[0] ) ) | ||
} | ||
return parts | ||
} | ||
// TODO: Prob. needs to check against more | ||
// encodings we can handle with `querystring` | ||
function isCompatibleEncoding( value ) { | ||
/** | ||
* Determines whether an encoding can be | ||
* natively handled with a `Buffer` | ||
* TODO: Prob. need to check against more | ||
* encodings we can handle with `querystring` | ||
* @param {String} value | ||
* @return {Boolean} | ||
*/ | ||
Link.isCompatibleEncoding = function( value ) { | ||
return /^utf-?8|ascii$/i.test( value ) | ||
} | ||
function parseExtendedValue( value ) { | ||
/** | ||
* Parses an extended value and attempts to decode it | ||
* @internal | ||
* @param {String} value | ||
* @return {Object} | ||
*/ | ||
Link.parseExtendedValue = function( value ) { | ||
var parts = /([^']+)?(?:'([^']+)')?(.+)/.exec( value ) | ||
return { | ||
language: parts[2].toLowerCase(), | ||
encoding: isCompatibleEncoding( parts[1] ) ? | ||
encoding: Link.isCompatibleEncoding( parts[1] ) ? | ||
null : parts[1].toLowerCase(), | ||
value: isCompatibleEncoding( parts[1] ) ? | ||
value: Link.isCompatibleEncoding( parts[1] ) ? | ||
querystring.unescape( parts[3] ) : parts[3] | ||
@@ -56,28 +67,25 @@ } | ||
function parseAttr( link, attr ) { | ||
/** | ||
* Parses out URI attributes | ||
* @internal | ||
* @param {Object} link | ||
* @param {String} parts | ||
* @return {Object} link | ||
*/ | ||
Link.parseAttrs = function( link, parts ) { | ||
var parts = /([^\=\*]+(\*)?)\=(["']?)(.+)(\3)/.exec( attr ) | ||
var key = parts[1].toLowerCase() | ||
var hasEncoding = !!parts[2] | ||
var quotes = parts[3] | ||
var value = parts[4] | ||
var match = null | ||
var key = '' | ||
var value = '' | ||
link[key] = hasEncoding ? | ||
parseExtendedValue( value ) : | ||
querystring.unescape( value ) | ||
return link | ||
} | ||
function parseAttrs( parts ) { | ||
var uri = parts.shift() | ||
.replace( /^\s*|\s*$/g, '' ) | ||
.replace( /^<|>$/g, '' ) | ||
var link = { uri: uri } | ||
for( var i = 0; i < parts.length; i++ ) { | ||
parseAttr( link, parts[i] ) | ||
while( match = Link.attrPattern.exec( parts ) ) { | ||
key = match[1].toLowerCase() | ||
value = match[4] || match[3] || match[2] | ||
if( /\*$/.test( key ) ) { | ||
link[ key ] = Link.parseExtendedValue( value ) | ||
} else if( /%/.test( value ) ) { | ||
link[ key ] = querystring.unescape( value ) | ||
} else { | ||
link[ key ] = value | ||
} | ||
} | ||
@@ -127,11 +135,10 @@ | ||
var pattern = /([^,].*?)(?:[^\\](?=,)|$)/g | ||
// Unfold folded lines | ||
value = trim( value ) | ||
.replace( /\r?\n[\x20\x09]+/g, '' ) | ||
var match = null | ||
var link = null | ||
// Unfold folded lines | ||
value = value.replace( /\r?\n[\x20\x09]+/g, '' ) | ||
while( match = pattern.exec( value ) ) { | ||
link = parseAttrs( parseLine( trim( match[0] ) ) ) | ||
while( match = Link.pattern.exec( value ) ) { | ||
var link = Link.parseAttrs({ uri: match[1] }, match[0] ) | ||
if( this.rel( link.rel ) != null ) continue; | ||
@@ -138,0 +145,0 @@ this.refs.push( link ) |
{ | ||
"name": "http-link-header", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"description": "Parse & format HTTP link headers according to RFC 5988", | ||
@@ -5,0 +5,0 @@ "author": "Jonas Hermsmeier <jhermsmeier@gmail.com> (https://jhermsmeier.de)", |
7125
133