config_source.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const fs = require("fs");
  4. const path = require("path");
  5. const request = require("request");
  6. const url = require("url");
  7. const xml2js = require("xml2js");
  8. const logger_1 = require("../cli/logger");
  9. const config_1 = require("../config");
  10. const http_utils_1 = require("../http_utils");
  11. let logger = new logger_1.Logger('config_source');
  12. class ConfigSource {
  13. constructor() {
  14. this.ostype = config_1.Config.osType();
  15. this.osarch = config_1.Config.osArch();
  16. this.out_dir = config_1.Config.getSeleniumDir();
  17. }
  18. }
  19. exports.ConfigSource = ConfigSource;
  20. class XmlConfigSource extends ConfigSource {
  21. constructor(name, xmlUrl) {
  22. super();
  23. this.name = name;
  24. this.xmlUrl = xmlUrl;
  25. }
  26. getFileName() {
  27. try {
  28. fs.statSync(this.out_dir);
  29. }
  30. catch (e) {
  31. fs.mkdirSync(this.out_dir);
  32. }
  33. return path.resolve(this.out_dir, this.name + '-response.xml');
  34. }
  35. getXml() {
  36. let fileName = this.getFileName();
  37. let content = this.readResponse();
  38. if (content) {
  39. return Promise.resolve(content);
  40. }
  41. else {
  42. return this.requestXml().then(text => {
  43. let xml = this.convertXml2js(text);
  44. fs.writeFileSync(fileName, text);
  45. return xml;
  46. });
  47. }
  48. }
  49. readResponse() {
  50. let fileName = this.getFileName();
  51. try {
  52. let contents = fs.readFileSync(fileName).toString();
  53. let timestamp = new Date(fs.statSync(fileName).mtime).getTime();
  54. let size = fs.statSync(fileName).size;
  55. let now = Date.now();
  56. // On start, read the file. If not on start, check use the cache as long as the
  57. // size > 0 and within the cache time.
  58. // 60 minutes * 60 seconds / minute * 1000 ms / second
  59. if (config_1.Config.runCommand === 'start' || (size > 0 && (now - (60 * 60 * 1000) < timestamp))) {
  60. return this.convertXml2js(contents);
  61. }
  62. else {
  63. return null;
  64. }
  65. }
  66. catch (err) {
  67. return null;
  68. }
  69. }
  70. requestXml() {
  71. return new Promise((resolve, reject) => {
  72. let options = http_utils_1.HttpUtils.initOptions(this.xmlUrl);
  73. let curl = this.getFileName() + ' ' + options.url;
  74. if (http_utils_1.HttpUtils.requestOpts.proxy) {
  75. let pathUrl = url.parse(options.url.toString()).path;
  76. let host = url.parse(options.url.toString()).host;
  77. let newFileUrl = url.resolve(http_utils_1.HttpUtils.requestOpts.proxy, pathUrl);
  78. curl = this.getFileName() + ' \'' + newFileUrl + '\' -H \'host:' + host + '\'';
  79. }
  80. if (http_utils_1.HttpUtils.requestOpts.ignoreSSL) {
  81. curl = 'k ' + curl;
  82. }
  83. logger.info('curl -o' + curl);
  84. let req = request(options);
  85. req.on('response', response => {
  86. if (response.statusCode === 200) {
  87. let output = '';
  88. response.on('data', (data) => {
  89. output += data;
  90. });
  91. response.on('end', () => {
  92. resolve(output);
  93. });
  94. }
  95. else {
  96. reject(new Error('response status code is not 200'));
  97. }
  98. });
  99. });
  100. }
  101. convertXml2js(xml) {
  102. let retResult = null;
  103. xml2js.parseString(xml, (err, result) => {
  104. retResult = result;
  105. });
  106. return retResult;
  107. }
  108. }
  109. exports.XmlConfigSource = XmlConfigSource;
  110. class JsonConfigSource extends ConfigSource {
  111. constructor(name, jsonUrl) {
  112. super();
  113. this.name = name;
  114. this.jsonUrl = jsonUrl;
  115. }
  116. getFileName() {
  117. try {
  118. fs.statSync(this.out_dir);
  119. }
  120. catch (e) {
  121. fs.mkdirSync(this.out_dir);
  122. }
  123. return path.resolve(this.out_dir, this.name + '-response.json');
  124. }
  125. }
  126. exports.JsonConfigSource = JsonConfigSource;
  127. class GithubApiConfigSource extends JsonConfigSource {
  128. constructor(name, url) {
  129. super(name, url);
  130. }
  131. /**
  132. * This is an unauthenticated request and since Github limits the rate, we will cache this
  133. * to a file. { timestamp: number, response: response }. We will check the timestamp and renew
  134. * this request if the file is older than an hour.
  135. */
  136. getJson() {
  137. let fileName = this.getFileName();
  138. let content = this.readResponse();
  139. if (content) {
  140. return Promise.resolve(JSON.parse(content));
  141. }
  142. else {
  143. return this.requestJson().then(body => {
  144. let json = JSON.parse(body);
  145. fs.writeFileSync(fileName, JSON.stringify(json, null, ' '));
  146. return json;
  147. });
  148. }
  149. }
  150. requestJson() {
  151. return new Promise((resolve, reject) => {
  152. let options = http_utils_1.HttpUtils.initOptions(this.jsonUrl);
  153. options = http_utils_1.HttpUtils.optionsHeader(options, 'Host', 'api.github.com');
  154. options = http_utils_1.HttpUtils.optionsHeader(options, 'User-Agent', 'request');
  155. let curl = this.getFileName() + ' ' + options.url;
  156. if (http_utils_1.HttpUtils.requestOpts.proxy) {
  157. let pathUrl = url.parse(options.url.toString()).path;
  158. let host = url.parse(options.url.toString()).host;
  159. let newFileUrl = url.resolve(http_utils_1.HttpUtils.requestOpts.proxy, pathUrl);
  160. curl = this.getFileName() + ' \'' + newFileUrl + '\' -H \'host:' + host + '\'';
  161. }
  162. if (http_utils_1.HttpUtils.requestOpts.ignoreSSL) {
  163. curl = 'k ' + curl;
  164. }
  165. logger.info('curl -o' + curl);
  166. let req = request(options);
  167. req.on('response', response => {
  168. if (response.statusCode === 200) {
  169. let output = '';
  170. response.on('data', (data) => {
  171. output += data;
  172. });
  173. response.on('end', () => {
  174. resolve(output);
  175. });
  176. }
  177. else if (response.statusCode == 403 && response.headers['x-ratelimit-remaining'] == '0') {
  178. reject(new Error('Failed to make Github request, rate limit reached.'));
  179. }
  180. else {
  181. reject(new Error('response status code is not 200. It was ' + response.statusCode));
  182. }
  183. });
  184. });
  185. }
  186. readResponse() {
  187. let fileName = this.getFileName();
  188. try {
  189. let contents = fs.readFileSync(fileName).toString();
  190. let timestamp = new Date(fs.statSync(fileName).mtime).getTime();
  191. let size = fs.statSync(fileName).size;
  192. let now = Date.now();
  193. // On start, read the file. If not on start, check use the cache as long as the
  194. // size > 0 and within the cache time.
  195. // 60 minutes * 60 seconds / minute * 1000 ms / second
  196. if (config_1.Config.runCommand === 'start' || (size > 0 && (now - (60 * 60 * 1000) < timestamp))) {
  197. return contents;
  198. }
  199. else {
  200. return null;
  201. }
  202. }
  203. catch (err) {
  204. return null;
  205. }
  206. }
  207. }
  208. exports.GithubApiConfigSource = GithubApiConfigSource;
  209. //# sourceMappingURL=config_source.js.map