Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
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.

index.js 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. "use strict";
  2. var fakeXhr = require("../fake-xhr");
  3. var push = [].push;
  4. var log = require("./log");
  5. var configureLogError = require("../configure-logger");
  6. var pathToRegexp = require("path-to-regexp");
  7. var supportsArrayBuffer = typeof ArrayBuffer !== "undefined";
  8. function responseArray(handler) {
  9. var response = handler;
  10. if (Object.prototype.toString.call(handler) !== "[object Array]") {
  11. response = [200, {}, handler];
  12. }
  13. if (typeof response[2] !== "string") {
  14. if (!supportsArrayBuffer) {
  15. throw new TypeError(
  16. `Fake server response body should be a string, but was ${typeof response[2]}`
  17. );
  18. } else if (!(response[2] instanceof ArrayBuffer)) {
  19. throw new TypeError(
  20. `Fake server response body should be a string or ArrayBuffer, but was ${typeof response[2]}`
  21. );
  22. }
  23. }
  24. return response;
  25. }
  26. function getDefaultWindowLocation() {
  27. var winloc = {
  28. hostname: "localhost",
  29. port: process.env.PORT || 80,
  30. protocol: "http:"
  31. };
  32. winloc.host =
  33. winloc.hostname +
  34. (String(winloc.port) === "80" ? "" : `:${winloc.port}`);
  35. return winloc;
  36. }
  37. function getWindowLocation() {
  38. if (typeof window === "undefined") {
  39. // Fallback
  40. return getDefaultWindowLocation();
  41. }
  42. if (typeof window.location !== "undefined") {
  43. // Browsers place location on window
  44. return window.location;
  45. }
  46. if (
  47. typeof window.window !== "undefined" &&
  48. typeof window.window.location !== "undefined"
  49. ) {
  50. // React Native on Android places location on window.window
  51. return window.window.location;
  52. }
  53. return getDefaultWindowLocation();
  54. }
  55. function matchOne(response, reqMethod, reqUrl) {
  56. var rmeth = response.method;
  57. var matchMethod = !rmeth || rmeth.toLowerCase() === reqMethod.toLowerCase();
  58. var url = response.url;
  59. var matchUrl =
  60. !url ||
  61. url === reqUrl ||
  62. (typeof url.test === "function" && url.test(reqUrl)) ||
  63. (typeof url === "function" && url(reqUrl) === true);
  64. return matchMethod && matchUrl;
  65. }
  66. function match(response, request) {
  67. var wloc = getWindowLocation();
  68. var rCurrLoc = new RegExp(`^${wloc.protocol}//${wloc.host}/`);
  69. var requestUrl = request.url;
  70. if (!/^https?:\/\//.test(requestUrl) || rCurrLoc.test(requestUrl)) {
  71. requestUrl = requestUrl.replace(rCurrLoc, "/");
  72. }
  73. if (matchOne(response, this.getHTTPMethod(request), requestUrl)) {
  74. if (typeof response.response === "function") {
  75. var ru = response.url;
  76. var args = [request].concat(
  77. ru && typeof ru.exec === "function"
  78. ? ru.exec(requestUrl).slice(1)
  79. : []
  80. );
  81. return response.response.apply(response, args);
  82. }
  83. return true;
  84. }
  85. return false;
  86. }
  87. function incrementRequestCount() {
  88. var count = ++this.requestCount;
  89. this.requested = true;
  90. this.requestedOnce = count === 1;
  91. this.requestedTwice = count === 2;
  92. this.requestedThrice = count === 3;
  93. this.firstRequest = this.getRequest(0);
  94. this.secondRequest = this.getRequest(1);
  95. this.thirdRequest = this.getRequest(2);
  96. this.lastRequest = this.getRequest(count - 1);
  97. }
  98. var fakeServer = {
  99. create: function(config) {
  100. var server = Object.create(this);
  101. server.configure(config);
  102. this.xhr = fakeXhr.useFakeXMLHttpRequest();
  103. server.requests = [];
  104. server.requestCount = 0;
  105. server.queue = [];
  106. server.responses = [];
  107. this.xhr.onCreate = function(xhrObj) {
  108. xhrObj.unsafeHeadersEnabled = function() {
  109. return !(server.unsafeHeadersEnabled === false);
  110. };
  111. server.addRequest(xhrObj);
  112. };
  113. return server;
  114. },
  115. configure: function(config) {
  116. var self = this;
  117. var allowlist = {
  118. autoRespond: true,
  119. autoRespondAfter: true,
  120. respondImmediately: true,
  121. fakeHTTPMethods: true,
  122. logger: true,
  123. unsafeHeadersEnabled: true
  124. };
  125. // eslint-disable-next-line no-param-reassign
  126. config = config || {};
  127. Object.keys(config).forEach(function(setting) {
  128. if (setting in allowlist) {
  129. self[setting] = config[setting];
  130. }
  131. });
  132. self.logError = configureLogError(config);
  133. },
  134. addRequest: function addRequest(xhrObj) {
  135. var server = this;
  136. push.call(this.requests, xhrObj);
  137. incrementRequestCount.call(this);
  138. xhrObj.onSend = function() {
  139. server.handleRequest(this);
  140. if (server.respondImmediately) {
  141. server.respond();
  142. } else if (server.autoRespond && !server.responding) {
  143. setTimeout(function() {
  144. server.responding = false;
  145. server.respond();
  146. }, server.autoRespondAfter || 10);
  147. server.responding = true;
  148. }
  149. };
  150. },
  151. getHTTPMethod: function getHTTPMethod(request) {
  152. if (this.fakeHTTPMethods && /post/i.test(request.method)) {
  153. var matches = (request.requestBody || "").match(
  154. /_method=([^\b;]+)/
  155. );
  156. return matches ? matches[1] : request.method;
  157. }
  158. return request.method;
  159. },
  160. handleRequest: function handleRequest(xhr) {
  161. if (xhr.async) {
  162. push.call(this.queue, xhr);
  163. } else {
  164. this.processRequest(xhr);
  165. }
  166. },
  167. logger: function() {
  168. // no-op; override via configure()
  169. },
  170. logError: configureLogError({}),
  171. log: log,
  172. respondWith: function respondWith(method, url, body) {
  173. if (arguments.length === 1 && typeof method !== "function") {
  174. this.response = responseArray(method);
  175. return;
  176. }
  177. if (arguments.length === 1) {
  178. // eslint-disable-next-line no-param-reassign
  179. body = method;
  180. // eslint-disable-next-line no-param-reassign
  181. url = method = null;
  182. }
  183. if (arguments.length === 2) {
  184. // eslint-disable-next-line no-param-reassign
  185. body = url;
  186. // eslint-disable-next-line no-param-reassign
  187. url = method;
  188. // eslint-disable-next-line no-param-reassign
  189. method = null;
  190. }
  191. // Escape port number to prevent "named" parameters in 'path-to-regexp' module
  192. if (typeof url === "string" && url !== "" && /:[0-9]+\//.test(url)) {
  193. var m = url.match(/^(https?:\/\/.*?):([0-9]+\/.*)$/);
  194. // eslint-disable-next-line no-param-reassign
  195. url = `${m[1]}\\:${m[2]}`;
  196. }
  197. push.call(this.responses, {
  198. method: method,
  199. url:
  200. typeof url === "string" && url !== "" ? pathToRegexp(url) : url,
  201. response: typeof body === "function" ? body : responseArray(body)
  202. });
  203. },
  204. respond: function respond() {
  205. if (arguments.length > 0) {
  206. this.respondWith.apply(this, arguments);
  207. }
  208. var queue = this.queue || [];
  209. var requests = queue.splice(0, queue.length);
  210. var self = this;
  211. requests.forEach(function(request) {
  212. self.processRequest(request);
  213. });
  214. },
  215. respondAll: function respondAll() {
  216. if (this.respondImmediately) {
  217. return;
  218. }
  219. this.queue = this.requests.slice(0);
  220. var request;
  221. while ((request = this.queue.shift())) {
  222. this.processRequest(request);
  223. }
  224. },
  225. processRequest: function processRequest(request) {
  226. try {
  227. if (request.aborted) {
  228. return;
  229. }
  230. var response = this.response || [404, {}, ""];
  231. if (this.responses) {
  232. for (var l = this.responses.length, i = l - 1; i >= 0; i--) {
  233. if (match.call(this, this.responses[i], request)) {
  234. response = this.responses[i].response;
  235. break;
  236. }
  237. }
  238. }
  239. if (request.readyState !== 4) {
  240. this.log(response, request);
  241. request.respond(response[0], response[1], response[2]);
  242. }
  243. } catch (e) {
  244. this.logError("Fake server request processing", e);
  245. }
  246. },
  247. restore: function restore() {
  248. return this.xhr.restore && this.xhr.restore.apply(this.xhr, arguments);
  249. },
  250. getRequest: function getRequest(index) {
  251. return this.requests[index] || null;
  252. },
  253. reset: function reset() {
  254. this.resetBehavior();
  255. this.resetHistory();
  256. },
  257. resetBehavior: function resetBehavior() {
  258. this.responses.length = this.queue.length = 0;
  259. },
  260. resetHistory: function resetHistory() {
  261. this.requests.length = this.requestCount = 0;
  262. this.requestedOnce = this.requestedTwice = this.requestedThrice = this.requested = false;
  263. this.firstRequest = this.secondRequest = this.thirdRequest = this.lastRequest = null;
  264. }
  265. };
  266. module.exports = fakeServer;