Skip to content

Commit

Permalink
Merge pull request #16 from teohhanhui/external-regex
Browse files Browse the repository at this point in the history
Support regex matching of URL in external source
  • Loading branch information
teohhanhui authored Jul 4, 2016
2 parents b857b46 + 3a2dd42 commit 86af0d6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
25 changes: 22 additions & 3 deletions src/streams/sources/external.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

'use strict';

var stream, util, request;
var string, stream, util, request;

string = require('../../utils/string');
stream = require('stream');
util = require('util');
request = require('request');
Expand All @@ -15,6 +16,8 @@ function contentLength(bufs){
}

function External(image, key, prefix){
var regexMatch;

/* jshint validthis:true */
if (!(this instanceof External)){
return new External(image, key, prefix);
Expand All @@ -23,7 +26,12 @@ function External(image, key, prefix){
this.image = image;
this.ended = false;
this.key = key;
this.prefix = prefix;
if (string.REGEX_REGEX.test(prefix)) {
regexMatch = prefix.match(string.REGEX_REGEX);
this.pattern = new RegExp(regexMatch[1], regexMatch[2]);
} else {
this.prefix = prefix;
}
}

util.inherits(External, stream.Readable);
Expand All @@ -43,7 +51,18 @@ External.prototype._read = function(){
return this.push(null);
}

url = this.prefix + '/' + this.image.path;
if (this.pattern) {
url = this.image.path;

if (!this.pattern.test(url)) {
this.image.error = new Error('URL "' + url + '" does not match pattern');
this.ended = true;
this.push(this.image);
return this.push(null);
}
} else {
url = this.prefix + '/' + this.image.path;
}

this.image.log.time(this.key);

Expand Down
5 changes: 5 additions & 0 deletions src/utils/string.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
'use strict';

var REGEX_REGEX;


REGEX_REGEX = /^\/((?:\\\/|[^\/])+)\/((?!.*(.).*\3)[gimuy]*)$/;
exports.REGEX_REGEX = REGEX_REGEX;

exports.sanitize = function(value, type) {
if (typeof type === 'undefined') {
Expand Down

0 comments on commit 86af0d6

Please sign in to comment.