concord.js
5.13 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
var globToRegExp = require("./globToRegExp").globToRegExp;
function parseType(type) {
var items = type.split("+");
var t = items.shift();
return {
type: t === "*" ? null : t,
features: items
};
}
function isTypeMatched(baseType, testedType) {
if(typeof baseType === "string") baseType = parseType(baseType);
if(typeof testedType === "string") testedType = parseType(testedType);
if(testedType.type && testedType.type !== baseType.type) return false;
return testedType.features.every(function(requiredFeature) {
return baseType.features.indexOf(requiredFeature) >= 0;
});
}
function isResourceTypeMatched(baseType, testedType) {
baseType = baseType.split("/");
testedType = testedType.split("/");
if(baseType.length !== testedType.length) return false;
for(var i = 0; i < baseType.length; i++) {
if(!isTypeMatched(baseType[i], testedType[i]))
return false;
}
return true;
}
function isResourceTypeSupported(context, type) {
return context.supportedResourceTypes && context.supportedResourceTypes.some(function(supportedType) {
return isResourceTypeMatched(supportedType, type);
});
}
function isEnvironment(context, env) {
return context.environments && context.environments.every(function(environment) {
return isTypeMatched(environment, env);
});
}
var globCache = {};
function getGlobRegExp(glob) {
var regExp = globCache[glob] || (globCache[glob] = globToRegExp(glob));
return regExp;
}
function matchGlob(glob, relativePath) {
var regExp = getGlobRegExp(glob);
return regExp.exec(relativePath);
}
function isGlobMatched(glob, relativePath) {
return !!matchGlob(glob, relativePath);
}
function isConditionMatched(context, condition) {
var items = condition.split("|");
return items.some(function testFn(item) {
item = item.trim();
var inverted = /^!/.test(item);
if(inverted) return !testFn(item.substr(1));
if(/^[a-z]+:/.test(item)) {
// match named condition
var match = /^([a-z]+):\s*/.exec(item);
var value = item.substr(match[0].length);
var name = match[1];
switch(name) {
case "referrer":
return isGlobMatched(value, context.referrer);
default:
return false;
}
} else if(item.indexOf("/") >= 0) {
// match supported type
return isResourceTypeSupported(context, item);
} else {
// match environment
return isEnvironment(context, item);
}
});
}
function isKeyMatched(context, key) {
while(true) { //eslint-disable-line
var match = /^\[([^\]]+)\]\s*/.exec(key);
if(!match) return key;
key = key.substr(match[0].length);
var condition = match[1];
if(!isConditionMatched(context, condition)) {
return false;
}
}
}
function getField(context, configuration, field) {
var value;
Object.keys(configuration).forEach(function(key) {
var pureKey = isKeyMatched(context, key);
if(pureKey === field) {
value = configuration[key];
}
});
return value;
}
function getMain(context, configuration) {
return getField(context, configuration, "main");
}
function getExtensions(context, configuration) {
return getField(context, configuration, "extensions");
}
function matchModule(context, configuration, request) {
var modulesField = getField(context, configuration, "modules");
if(!modulesField) return request;
var newRequest = request;
var keys = Object.keys(modulesField);
var iteration = 0;
for(var i = 0; i < keys.length; i++) {
var key = keys[i];
var pureKey = isKeyMatched(context, key);
var match = matchGlob(pureKey, newRequest);
if(match) {
var value = modulesField[key];
if(typeof value !== "string") {
return value;
} else if(/^\(.+\)$/.test(pureKey)) {
newRequest = newRequest.replace(getGlobRegExp(pureKey), value);
} else {
var index = 1;
newRequest = value.replace(/(\/?\*)?\*/g, replaceMatcher);
}
i = -1;
if(iteration++ > keys.length) {
throw new Error("Request '" + request + "' matches recursively");
}
}
}
return newRequest;
function replaceMatcher(find) {
switch(find) {
case "/**":
var m = match[index++];
return m ? "/" + m : "";
case "**":
case "*":
return match[index++];
}
}
}
function matchType(context, configuration, relativePath) {
var typesField = getField(context, configuration, "types");
if(!typesField) return undefined;
var type;
Object.keys(typesField).forEach(function(key) {
var pureKey = isKeyMatched(context, key);
if(isGlobMatched(pureKey, relativePath)) {
var value = typesField[key];
if(!type && /\/\*$/.test(value))
throw new Error("value ('" + value + "') of key '" + key + "' contains '*', but there is no previous value defined");
type = value.replace(/\/\*$/, "/" + type);
}
});
return type;
}
exports.parseType = parseType;
exports.isTypeMatched = isTypeMatched;
exports.isResourceTypeSupported = isResourceTypeSupported;
exports.isEnvironment = isEnvironment;
exports.isGlobMatched = isGlobMatched;
exports.isConditionMatched = isConditionMatched;
exports.isKeyMatched = isKeyMatched;
exports.getField = getField;
exports.getMain = getMain;
exports.getExtensions = getExtensions;
exports.matchModule = matchModule;
exports.matchType = matchType;