Skip to content

Commit

Permalink
Merge branch 'thorn0-requestOptionsTransform'
Browse files Browse the repository at this point in the history
  • Loading branch information
jrit committed Mar 30, 2016
2 parents 4003533 + 21f8f6a commit 62b1884
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 21 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ If uglify is assigned `true`, JavaScript file will be minified before inlined.
#### `strict`, Boolean, default `false`
When strict is `true`, a missing resource will cause the inliner to halt and return an error in the callback. The default behavior is to log a warning to the console and continue inlining with the available resources, which is more similar to how a web page behaves.

#### `requestTransform`, Function, default `undefined`
Allows to adjust issued requests. E.g., add authentication tokens to requested URLs. The function is called with the request options object as its parameter. It can modify this object or return a new one. See [the list of available options](https://www.npmjs.com/package/request#request-options-callback).

## Contributing
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Run tests with `npm test`.
2 changes: 1 addition & 1 deletion src/css.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module.exports = function( options, callback )
return callback( null ); // Skip
}

inline.getFileReplacement( args.src, settings.relativeTo, function( err, datauriContent )
inline.getFileReplacement( args.src, settings, function( err, datauriContent )
{
if( err )
{
Expand Down
6 changes: 3 additions & 3 deletions src/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ module.exports = function( options, callback )

args.element = replaceInlineAttribute( args.element );

inline.getTextReplacement( args.src, settings.relativeTo, function( err, content )
inline.getTextReplacement( args.src, settings, function( err, content )
{
if( err )
{
Expand All @@ -50,7 +50,7 @@ module.exports = function( options, callback )

args.element = replaceInlineAttribute( args.element );

inline.getTextReplacement( args.src, settings.relativeTo, function( err, content )
inline.getTextReplacement( args.src, settings, function( err, content )
{
if( err )
{
Expand Down Expand Up @@ -86,7 +86,7 @@ module.exports = function( options, callback )

args.element = replaceInlineAttribute( args.element );

inline.getFileReplacement( args.src, settings.relativeTo, function( err, datauriContent )
inline.getFileReplacement( args.src, settings, function( err, datauriContent )
{
if( err )
{
Expand Down
41 changes: 24 additions & 17 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,19 +75,26 @@ util.getAttrs = function( tagMarkup, settings )
}
};

util.getRemote = function( uri, callback, toDataUri )
function getRemote( uri, settings, callback, toDataUri )
{
if( /^\/\//.test( uri ) )
{
uri = "https:" + uri;
}

var requestOptions = {
uri: uri,
encoding: toDataUri ? "binary" : "",
gzip: true
};

if( typeof settings.requestTransform === "function" )
{
requestOptions = settings.requestTransform( requestOptions ) || requestOptions;
}

request(
{
uri: uri,
encoding: toDataUri ? "binary" : "",
gzip: true
},
requestOptions,
function( err, response, body )
{
if( err )
Expand All @@ -110,7 +117,7 @@ util.getRemote = function( uri, callback, toDataUri )
return callback( null, body );
}
} );
};
}

util.getInlineFilePath = function( src, relativeTo )
{
Expand All @@ -123,21 +130,21 @@ util.getInlineFileContents = function( src, relativeTo )
return fs.readFileSync( util.getInlineFilePath( src, relativeTo ) );
};

util.getTextReplacement = function( src, relativeTo, callback )
util.getTextReplacement = function( src, settings, callback )
{
if( util.isRemotePath( relativeTo ) )
if( util.isRemotePath( settings.relativeTo ) || util.isRemotePath( src ) )
{
util.getRemote( url.resolve( relativeTo, src ), callback );
getRemote( url.resolve( settings.relativeTo, src ), settings, callback );
}
else if( util.isRemotePath( src ) )
{
util.getRemote( src, callback );
getRemote( src, settings, callback );
}
else
{
try
{
var replacement = util.getInlineFileContents( src, relativeTo );
var replacement = util.getInlineFileContents( src, settings.relativeTo );
}
catch( err )
{
Expand All @@ -147,19 +154,19 @@ util.getTextReplacement = function( src, relativeTo, callback )
}
};

util.getFileReplacement = function( src, relativeTo, callback )
util.getFileReplacement = function( src, settings, callback )
{
if( util.isRemotePath( relativeTo ) )
if( util.isRemotePath( settings.relativeTo ) )
{
util.getRemote( url.resolve( relativeTo, src ), callback, true );
getRemote( url.resolve( settings.relativeTo, src ), settings, callback, true );
}
else if( util.isRemotePath( src ) )
{
util.getRemote( src, callback, true );
getRemote( src, settings, callback, true );
}
else
{
var result = ( new datauri( util.getInlineFilePath( src, relativeTo ) ) ).content;
var result = ( new datauri( util.getInlineFilePath( src, settings.relativeTo ) ) ).content;
callback( result === undefined ? new Error( "Local file not found" ) : null, result );
}
};
Expand Down
20 changes: 20 additions & 0 deletions test/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,26 @@ describe( "html", function()
} );
} );

it( "should apply the requestTransform option", function( done ) {
fauxJax.on( "request", function( request )
{
assert( request.requestURL.indexOf( "foo=bar" ) !== -1 );
} );
inline.html( {
fileContent: "<img src=\"assets/icon.png\"><img src=\"assets/icon.png?a=1\">",
relativeTo: baseUrl,
scripts: true,
links: true,
images: true,
requestTransform: function(options)
{
options.qs = {
foo: "bar"
};
}
}, done );
} );

} );
} );

Expand Down

0 comments on commit 62b1884

Please sign in to comment.