-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
71 lines (58 loc) · 2.04 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
var url = require('url');
var _ = require('underscore');
var SolidusAssetsProxy = function(options) {
if (!(this instanceof SolidusAssetsProxy)) return new SolidusAssetsProxy(options);
this.origins = options.origins.map(function(origin) {
// Match URL strings from origin up to the ending delimiter
var regexp = _.isRegExp(origin) ? origin.source : escapeRegExp(origin);
return new RegExp('(^|[\'"\\s\\(])(' + regexp + '.*?)($|[\'"\\s\\)])', 'g');
});
this.proxy = options.proxy + (options.proxy.slice(-1) == '/' ? '' : '/');
};
SolidusAssetsProxy.prototype.proxyAssets = function(string) {
if (!string || !string.substring) return string;
var self = this;
return self.origins.reduce(function(result, origin) {
return result.replace(origin, function(match, starting_delimiter, asset, ending_delimiter) {
return starting_delimiter + self.proxy + encodeURIComponent(asset) + ending_delimiter;
});
}, string);
};
SolidusAssetsProxy.prototype.proxyContextAssets = function(context, paths) {
if (paths) {
for (var i in paths) {
proxyObjectAssetsByPath.call(this, context, paths[i].split('.'));
}
} else {
proxyObjectAssets.call(this, context, true);
}
};
module.exports = SolidusAssetsProxy;
// PRIVATE
function escapeRegExp(string) {
return string.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
};
function proxyObjectAssetsByPath(object, path) {
if (!object) return;
if (_.isArray(object)) {
for (var i in object) proxyObjectAssetsByPath.call(this, object[i], path);
return;
}
var key = path[0];
if (path.length > 1) {
proxyObjectAssetsByPath.call(this, object[key], path.slice(1));
} else {
object[key] = proxyObjectAssets.call(this, object[key], false);
}
};
function proxyObjectAssets(object, recursive) {
var self = this;
if (_.isArray(object) || (recursive && _.isObject(object))) {
_.each(object, function(value, key) {
object[key] = proxyObjectAssets.call(self, value, recursive);
});
return object;
} else {
return self.proxyAssets(object);
}
};