a zip code crypto-currency system good for red ONLY

http-server.js 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. "use strict";
  2. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  3. return new (P || (P = Promise))(function (resolve, reject) {
  4. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  5. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  6. function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
  7. step((generator = generator.apply(thisArg, _arguments || [])).next());
  8. });
  9. };
  10. var __generator = (this && this.__generator) || function (thisArg, body) {
  11. var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
  12. return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
  13. function verb(n) { return function (v) { return step([n, v]); }; }
  14. function step(op) {
  15. if (f) throw new TypeError("Generator is already executing.");
  16. while (_) try {
  17. if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
  18. if (y = 0, t) op = [0, t.value];
  19. switch (op[0]) {
  20. case 0: case 1: t = op; break;
  21. case 4: _.label++; return { value: op[1], done: false };
  22. case 5: _.label++; y = op[1]; op = [0]; continue;
  23. case 7: op = _.ops.pop(); _.trys.pop(); continue;
  24. default:
  25. if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
  26. if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
  27. if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
  28. if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
  29. if (t[2]) _.ops.pop();
  30. _.trys.pop(); continue;
  31. }
  32. op = body.call(thisArg, _);
  33. } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
  34. if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
  35. }
  36. };
  37. Object.defineProperty(exports, "__esModule", { value: true });
  38. var path = require("path");
  39. var injector_1 = require("./injector");
  40. var live_reload_1 = require("./live-reload");
  41. var express = require("express");
  42. var fs = require("fs");
  43. var url = require("url");
  44. var serve_config_1 = require("./serve-config");
  45. var logger_1 = require("../logger/logger");
  46. var proxyMiddleware = require("proxy-middleware");
  47. var logger_diagnostics_1 = require("../logger/logger-diagnostics");
  48. var Constants = require("../util/constants");
  49. var helpers_1 = require("../util/helpers");
  50. var ionic_project_1 = require("../util/ionic-project");
  51. var lab_1 = require("./lab");
  52. /**
  53. * Create HTTP server
  54. */
  55. function createHttpServer(config) {
  56. var app = express();
  57. app.set('serveConfig', config);
  58. app.get('/', serveIndex);
  59. app.use('/', express.static(config.wwwDir));
  60. app.use("/" + serve_config_1.LOGGER_DIR, express.static(path.join(__dirname, '..', '..', 'bin'), { maxAge: 31536000 }));
  61. // Lab routes
  62. app.use(serve_config_1.IONIC_LAB_URL + '/static', express.static(path.join(__dirname, '..', '..', 'lab', 'static')));
  63. app.get(serve_config_1.IONIC_LAB_URL, lab_1.LabAppView);
  64. app.get(serve_config_1.IONIC_LAB_URL + '/api/v1/cordova', lab_1.ApiCordovaProject);
  65. app.get(serve_config_1.IONIC_LAB_URL + '/api/v1/app-config', lab_1.ApiPackageJson);
  66. app.get('/cordova.js', servePlatformResource, serveMockCordovaJS);
  67. app.get('/cordova_plugins.js', servePlatformResource);
  68. app.get('/plugins/*', servePlatformResource);
  69. if (config.useProxy) {
  70. setupProxies(app);
  71. }
  72. return app;
  73. }
  74. exports.createHttpServer = createHttpServer;
  75. function setupProxies(app) {
  76. if (helpers_1.getBooleanPropertyValue(Constants.ENV_READ_CONFIG_JSON)) {
  77. ionic_project_1.getProjectJson().then(function (projectConfig) {
  78. for (var _i = 0, _a = projectConfig.proxies || []; _i < _a.length; _i++) {
  79. var proxy = _a[_i];
  80. var opts = url.parse(proxy.proxyUrl);
  81. if (proxy.proxyNoAgent) {
  82. opts.agent = false;
  83. }
  84. opts.rejectUnauthorized = !(proxy.rejectUnauthorized === false);
  85. opts.cookieRewrite = proxy.cookieRewrite;
  86. app.use(proxy.path, proxyMiddleware(opts));
  87. logger_1.Logger.info('Proxy added:' + proxy.path + ' => ' + url.format(opts));
  88. }
  89. }).catch(function (err) {
  90. logger_1.Logger.error("Failed to read the projects ionic.config.json file: " + err.message);
  91. });
  92. }
  93. }
  94. /**
  95. * http responder for /index.html base entrypoint
  96. */
  97. function serveIndex(req, res) {
  98. var config = req.app.get('serveConfig');
  99. // respond with the index.html file
  100. var indexFileName = path.join(config.wwwDir, process.env[Constants.ENV_VAR_HTML_TO_SERVE]);
  101. fs.readFile(indexFileName, function (err, indexHtml) {
  102. if (!indexHtml) {
  103. logger_1.Logger.error("Failed to load index.html");
  104. res.send('try again later');
  105. return;
  106. }
  107. if (config.useLiveReload) {
  108. indexHtml = live_reload_1.injectLiveReloadScript(indexHtml, req.hostname, config.liveReloadPort);
  109. indexHtml = injector_1.injectNotificationScript(config.rootDir, indexHtml, config.notifyOnConsoleLog, config.notificationPort);
  110. }
  111. indexHtml = logger_diagnostics_1.injectDiagnosticsHtml(config.buildDir, indexHtml);
  112. res.set('Content-Type', 'text/html');
  113. res.send(indexHtml);
  114. });
  115. }
  116. /**
  117. * http responder for cordova.js file
  118. */
  119. function serveMockCordovaJS(req, res) {
  120. res.set('Content-Type', 'application/javascript');
  121. res.send('// mock cordova file during development');
  122. }
  123. /**
  124. * Middleware to serve platform resources
  125. */
  126. function servePlatformResource(req, res, next) {
  127. return __awaiter(this, void 0, void 0, function () {
  128. var config, userAgent, root;
  129. return __generator(this, function (_a) {
  130. switch (_a.label) {
  131. case 0:
  132. config = req.app.get('serveConfig');
  133. userAgent = req.header('user-agent');
  134. if (!config.isCordovaServe) {
  135. return [2 /*return*/, next()];
  136. }
  137. return [4 /*yield*/, getResourcePath(req.url, config, userAgent)];
  138. case 1:
  139. root = _a.sent();
  140. if (root) {
  141. res.sendFile(req.url, { root: root });
  142. }
  143. else {
  144. next();
  145. }
  146. return [2 /*return*/];
  147. }
  148. });
  149. });
  150. }
  151. /**
  152. * Determines the appropriate resource path, and checks if the specified url
  153. *
  154. * @returns string of the resource path or undefined if there is no match
  155. */
  156. function getResourcePath(url, config, userAgent) {
  157. return __awaiter(this, void 0, void 0, function () {
  158. var searchPaths, i, checkPath, result, e_1;
  159. return __generator(this, function (_a) {
  160. switch (_a.label) {
  161. case 0:
  162. searchPaths = [config.wwwDir];
  163. if (isUserAgentIOS(userAgent)) {
  164. searchPaths = serve_config_1.IOS_PLATFORM_PATHS.map(function (resourcePath) { return path.join(config.rootDir, resourcePath); });
  165. }
  166. else if (isUserAgentAndroid(userAgent)) {
  167. searchPaths = serve_config_1.ANDROID_PLATFORM_PATHS.map(function (resourcePath) { return path.join(config.rootDir, resourcePath); });
  168. }
  169. i = 0;
  170. _a.label = 1;
  171. case 1:
  172. if (!(i < searchPaths.length)) return [3 /*break*/, 6];
  173. checkPath = path.join(searchPaths[i], url);
  174. _a.label = 2;
  175. case 2:
  176. _a.trys.push([2, 4, , 5]);
  177. return [4 /*yield*/, checkFile(checkPath)];
  178. case 3:
  179. result = _a.sent();
  180. return [2 /*return*/, searchPaths[i]];
  181. case 4:
  182. e_1 = _a.sent();
  183. return [3 /*break*/, 5];
  184. case 5:
  185. i++;
  186. return [3 /*break*/, 1];
  187. case 6: return [2 /*return*/];
  188. }
  189. });
  190. });
  191. }
  192. /**
  193. * Checks if a file exists (responds to stat)
  194. */
  195. function checkFile(filePath) {
  196. return new Promise(function (resolve, reject) {
  197. fs.stat(filePath, function (err, stats) {
  198. if (err) {
  199. return reject();
  200. }
  201. resolve();
  202. });
  203. });
  204. }
  205. function isUserAgentIOS(ua) {
  206. ua = ua.toLowerCase();
  207. return (ua.indexOf('iphone') > -1 || ua.indexOf('ipad') > -1 || ua.indexOf('ipod') > -1);
  208. }
  209. function isUserAgentAndroid(ua) {
  210. ua = ua.toLowerCase();
  211. return ua.indexOf('android') > -1;
  212. }