You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

concord.js 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const globToRegExp = require("./globToRegExp").globToRegExp;
  7. function parseType(type) {
  8. const items = type.split("+");
  9. const t = items.shift();
  10. return {
  11. type: t === "*" ? null : t,
  12. features: items
  13. };
  14. }
  15. function isTypeMatched(baseType, testedType) {
  16. if(typeof baseType === "string") baseType = parseType(baseType);
  17. if(typeof testedType === "string") testedType = parseType(testedType);
  18. if(testedType.type && testedType.type !== baseType.type) return false;
  19. return testedType.features.every(requiredFeature => {
  20. return baseType.features.indexOf(requiredFeature) >= 0;
  21. });
  22. }
  23. function isResourceTypeMatched(baseType, testedType) {
  24. baseType = baseType.split("/");
  25. testedType = testedType.split("/");
  26. if(baseType.length !== testedType.length) return false;
  27. for(let i = 0; i < baseType.length; i++) {
  28. if(!isTypeMatched(baseType[i], testedType[i]))
  29. return false;
  30. }
  31. return true;
  32. }
  33. function isResourceTypeSupported(context, type) {
  34. return context.supportedResourceTypes && context.supportedResourceTypes.some(supportedType => {
  35. return isResourceTypeMatched(supportedType, type);
  36. });
  37. }
  38. function isEnvironment(context, env) {
  39. return context.environments && context.environments.every(environment => {
  40. return isTypeMatched(environment, env);
  41. });
  42. }
  43. const globCache = {};
  44. function getGlobRegExp(glob) {
  45. const regExp = globCache[glob] || (globCache[glob] = globToRegExp(glob));
  46. return regExp;
  47. }
  48. function matchGlob(glob, relativePath) {
  49. const regExp = getGlobRegExp(glob);
  50. return regExp.exec(relativePath);
  51. }
  52. function isGlobMatched(glob, relativePath) {
  53. return !!matchGlob(glob, relativePath);
  54. }
  55. function isConditionMatched(context, condition) {
  56. const items = condition.split("|");
  57. return items.some(function testFn(item) {
  58. item = item.trim();
  59. const inverted = /^!/.test(item);
  60. if(inverted) return !testFn(item.substr(1));
  61. if(/^[a-z]+:/.test(item)) {
  62. // match named condition
  63. const match = /^([a-z]+):\s*/.exec(item);
  64. const value = item.substr(match[0].length);
  65. const name = match[1];
  66. switch(name) {
  67. case "referrer":
  68. return isGlobMatched(value, context.referrer);
  69. default:
  70. return false;
  71. }
  72. } else if(item.indexOf("/") >= 0) {
  73. // match supported type
  74. return isResourceTypeSupported(context, item);
  75. } else {
  76. // match environment
  77. return isEnvironment(context, item);
  78. }
  79. });
  80. }
  81. function isKeyMatched(context, key) {
  82. while(true) { //eslint-disable-line
  83. const match = /^\[([^\]]+)\]\s*/.exec(key);
  84. if(!match) return key;
  85. key = key.substr(match[0].length);
  86. const condition = match[1];
  87. if(!isConditionMatched(context, condition)) {
  88. return false;
  89. }
  90. }
  91. }
  92. function getField(context, configuration, field) {
  93. let value;
  94. Object.keys(configuration).forEach(key => {
  95. const pureKey = isKeyMatched(context, key);
  96. if(pureKey === field) {
  97. value = configuration[key];
  98. }
  99. });
  100. return value;
  101. }
  102. function getMain(context, configuration) {
  103. return getField(context, configuration, "main");
  104. }
  105. function getExtensions(context, configuration) {
  106. return getField(context, configuration, "extensions");
  107. }
  108. function matchModule(context, configuration, request) {
  109. const modulesField = getField(context, configuration, "modules");
  110. if(!modulesField) return request;
  111. let newRequest = request;
  112. const keys = Object.keys(modulesField);
  113. let iteration = 0;
  114. let match;
  115. let index;
  116. for(let i = 0; i < keys.length; i++) {
  117. const key = keys[i];
  118. const pureKey = isKeyMatched(context, key);
  119. match = matchGlob(pureKey, newRequest);
  120. if(match) {
  121. const value = modulesField[key];
  122. if(typeof value !== "string") {
  123. return value;
  124. } else if(/^\(.+\)$/.test(pureKey)) {
  125. newRequest = newRequest.replace(getGlobRegExp(pureKey), value);
  126. } else {
  127. index = 1;
  128. newRequest = value.replace(/(\/?\*)?\*/g, replaceMatcher);
  129. }
  130. i = -1;
  131. if(iteration++ > keys.length) {
  132. throw new Error("Request '" + request + "' matches recursively");
  133. }
  134. }
  135. }
  136. return newRequest;
  137. function replaceMatcher(find) {
  138. switch(find) {
  139. case "/**":
  140. {
  141. const m = match[index++];
  142. return m ? "/" + m : "";
  143. }
  144. case "**":
  145. case "*":
  146. return match[index++];
  147. }
  148. }
  149. }
  150. function matchType(context, configuration, relativePath) {
  151. const typesField = getField(context, configuration, "types");
  152. if(!typesField) return undefined;
  153. let type;
  154. Object.keys(typesField).forEach(key => {
  155. const pureKey = isKeyMatched(context, key);
  156. if(isGlobMatched(pureKey, relativePath)) {
  157. const value = typesField[key];
  158. if(!type && /\/\*$/.test(value))
  159. throw new Error("value ('" + value + "') of key '" + key + "' contains '*', but there is no previous value defined");
  160. type = value.replace(/\/\*$/, "/" + type);
  161. }
  162. });
  163. return type;
  164. }
  165. exports.parseType = parseType;
  166. exports.isTypeMatched = isTypeMatched;
  167. exports.isResourceTypeSupported = isResourceTypeSupported;
  168. exports.isEnvironment = isEnvironment;
  169. exports.isGlobMatched = isGlobMatched;
  170. exports.isConditionMatched = isConditionMatched;
  171. exports.isKeyMatched = isKeyMatched;
  172. exports.getField = getField;
  173. exports.getMain = getMain;
  174. exports.getExtensions = getExtensions;
  175. exports.matchModule = matchModule;
  176. exports.matchType = matchType;