webdriver_proxy.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. Object.defineProperty(exports, "__esModule", { value: true });
  11. const http = require("http");
  12. const url = require("url");
  13. const webdriver_commands_1 = require("./webdriver_commands");
  14. /**
  15. * A proxy that understands WebDriver commands. Users can add barriers (similar to middleware in
  16. * express) that will be called before forwarding the request to WebDriver. The proxy will wait for
  17. * each barrier to finish, calling them in the order in which they were added.
  18. */
  19. class WebDriverProxy {
  20. constructor(seleniumAddress) {
  21. this.barriers = [];
  22. this.seleniumAddress = seleniumAddress;
  23. }
  24. addBarrier(barrier) {
  25. this.barriers.push(barrier);
  26. }
  27. handleRequest(originalRequest, response) {
  28. return __awaiter(this, void 0, void 0, function* () {
  29. let command = webdriver_commands_1.parseWebDriverCommand(originalRequest.url, originalRequest.method);
  30. let replyWithError = (err) => {
  31. response.writeHead(502);
  32. if (err && err.toString) {
  33. response.write(err.toString());
  34. }
  35. response.end();
  36. };
  37. // Process barriers in order, one at a time.
  38. try {
  39. for (let barrier of this.barriers) {
  40. yield barrier.onCommand(command);
  41. }
  42. }
  43. catch (err) {
  44. replyWithError(err);
  45. // Don't call through if a barrier fails.
  46. return;
  47. }
  48. let parsedUrl = url.parse(this.seleniumAddress);
  49. let options = {};
  50. options.method = originalRequest.method;
  51. options.path = parsedUrl.path + originalRequest.url;
  52. options.hostname = parsedUrl.hostname;
  53. options.port = parseInt(parsedUrl.port);
  54. options.headers = originalRequest.headers;
  55. let forwardedRequest = http.request(options);
  56. // clang-format off
  57. let reqData = '';
  58. originalRequest.on('data', (d) => {
  59. reqData += d;
  60. forwardedRequest.write(d);
  61. }).on('end', () => {
  62. command.handleData(reqData);
  63. forwardedRequest.end();
  64. }).on('error', replyWithError);
  65. forwardedRequest.on('response', (seleniumResponse) => {
  66. response.writeHead(seleniumResponse.statusCode, seleniumResponse.headers);
  67. let respData = '';
  68. seleniumResponse.on('data', (d) => {
  69. respData += d;
  70. response.write(d);
  71. }).on('end', () => {
  72. command.handleResponse(seleniumResponse.statusCode, respData);
  73. response.end();
  74. }).on('error', replyWithError);
  75. }).on('error', replyWithError);
  76. // clang-format on
  77. });
  78. }
  79. }
  80. exports.WebDriverProxy = WebDriverProxy;
  81. //# sourceMappingURL=webdriver_proxy.js.map