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 mqtt_1 = require("mqtt");
|
39 | var got = require("got");
|
40 | function getEndpoint(remoteOptions) {
|
41 | if (typeof remoteOptions === 'object' && remoteOptions.endpointUrl) {
|
42 | return remoteOptions;
|
43 | }
|
44 | if (process.env['CHROMELESS_ENDPOINT_URL'] &&
|
45 | process.env['CHROMELESS_ENDPOINT_API_KEY']) {
|
46 | return {
|
47 | endpointUrl: process.env['CHROMELESS_ENDPOINT_URL'],
|
48 | apiKey: process.env['CHROMELESS_ENDPOINT_API_KEY'],
|
49 | };
|
50 | }
|
51 | throw new Error('No Chromeless remote endpoint & API key provided. Either set as "remote" option in constructor or set as "CHROMELESS_ENDPOINT_URL" and "CHROMELESS_ENDPOINT_API_KEY" env variables.');
|
52 | }
|
53 | var RemoteChrome = (function () {
|
54 | function RemoteChrome(options) {
|
55 | this.options = options;
|
56 | this.connectionPromise = this.initConnection();
|
57 | }
|
58 | RemoteChrome.prototype.initConnection = function () {
|
59 | return __awaiter(this, void 0, void 0, function () {
|
60 | var _this = this;
|
61 | return __generator(this, function (_a) {
|
62 | switch (_a.label) {
|
63 | case 0: return [4 , new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
|
64 | var _this = this;
|
65 | var timeout, _a, endpointUrl, apiKey, _b, url, channelId_1, channel_1, error_1;
|
66 | return __generator(this, function (_c) {
|
67 | switch (_c.label) {
|
68 | case 0:
|
69 | timeout = setTimeout(function () {
|
70 | if (_this.channel) {
|
71 | _this.channel.end();
|
72 | }
|
73 | reject(new Error("Timed out after 30sec. Connection couldn't be established."));
|
74 | }, 30000);
|
75 | _c.label = 1;
|
76 | case 1:
|
77 | _c.trys.push([1, 3, , 4]);
|
78 | _a = getEndpoint(this.options.remote), endpointUrl = _a.endpointUrl, apiKey = _a.apiKey;
|
79 | return [4 , got(endpointUrl, {
|
80 | headers: apiKey
|
81 | ? {
|
82 | 'x-api-key': apiKey,
|
83 | }
|
84 | : undefined,
|
85 | json: true,
|
86 | })];
|
87 | case 2:
|
88 | _b = (_c.sent()).body, url = _b.url, channelId_1 = _b.channelId;
|
89 | this.channelId = channelId_1;
|
90 | this.TOPIC_NEW_SESSION = 'chrome/new-session';
|
91 | this.TOPIC_CONNECTED = "chrome/" + channelId_1 + "/connected";
|
92 | this.TOPIC_REQUEST = "chrome/" + channelId_1 + "/request";
|
93 | this.TOPIC_RESPONSE = "chrome/" + channelId_1 + "/response";
|
94 | this.TOPIC_END = "chrome/" + channelId_1 + "/end";
|
95 | channel_1 = mqtt_1.connect(url, {
|
96 | will: {
|
97 | topic: 'chrome/last-will',
|
98 | payload: JSON.stringify({ channelId: channelId_1 }),
|
99 | qos: 1,
|
100 | retain: false,
|
101 | },
|
102 | });
|
103 | this.channel = channel_1;
|
104 | if (this.options.debug) {
|
105 | channel_1.on('error', function (error) { return console.log('WebSocket error', error); });
|
106 | channel_1.on('offline', function () { return console.log('WebSocket offline'); });
|
107 | }
|
108 | channel_1.on('connect', function () {
|
109 | if (_this.options.debug) {
|
110 | console.log('Connected to message broker.');
|
111 | }
|
112 | channel_1.subscribe(_this.TOPIC_CONNECTED, { qos: 1 }, function () {
|
113 | channel_1.on('message', function (topic) { return __awaiter(_this, void 0, void 0, function () {
|
114 | return __generator(this, function (_a) {
|
115 | if (this.TOPIC_CONNECTED === topic) {
|
116 | clearTimeout(timeout);
|
117 | resolve();
|
118 | }
|
119 | return [2 ];
|
120 | });
|
121 | }); });
|
122 | channel_1.publish(_this.TOPIC_NEW_SESSION, JSON.stringify({ channelId: channelId_1, options: _this.options }), { qos: 1 });
|
123 | });
|
124 | channel_1.subscribe(_this.TOPIC_END, function () {
|
125 | channel_1.on('message', function (topic, buffer) { return __awaiter(_this, void 0, void 0, function () {
|
126 | var message, data;
|
127 | return __generator(this, function (_a) {
|
128 | switch (_a.label) {
|
129 | case 0:
|
130 | if (!(this.TOPIC_END === topic)) return [3 , 2];
|
131 | message = buffer.toString();
|
132 | data = JSON.parse(message);
|
133 | if (data.outOfTime) {
|
134 | console.warn("Chromeless Proxy disconnected because it reached it's execution time limit (5 minutes).");
|
135 | }
|
136 | else if (data.inactivity) {
|
137 | console.warn('Chromeless Proxy disconnected due to inactivity (no commands sent for 30 seconds).');
|
138 | }
|
139 | else {
|
140 | console.warn("Chromeless Proxy disconnected (we don't know why).", data);
|
141 | }
|
142 | return [4 , this.close()];
|
143 | case 1:
|
144 | _a.sent();
|
145 | _a.label = 2;
|
146 | case 2: return [2 ];
|
147 | }
|
148 | });
|
149 | }); });
|
150 | });
|
151 | });
|
152 | return [3 , 4];
|
153 | case 3:
|
154 | error_1 = _c.sent();
|
155 | console.error(error_1);
|
156 | reject(new Error('Unable to get presigned websocket URL and connect to it.'));
|
157 | return [3 , 4];
|
158 | case 4: return [2 ];
|
159 | }
|
160 | });
|
161 | }); })];
|
162 | case 1:
|
163 | _a.sent();
|
164 | return [2 ];
|
165 | }
|
166 | });
|
167 | });
|
168 | };
|
169 | RemoteChrome.prototype.process = function (command) {
|
170 | return __awaiter(this, void 0, void 0, function () {
|
171 | var _this = this;
|
172 | var promise;
|
173 | return __generator(this, function (_a) {
|
174 | switch (_a.label) {
|
175 | case 0:
|
176 |
|
177 | return [4 , this.connectionPromise];
|
178 | case 1:
|
179 |
|
180 | _a.sent();
|
181 | if (this.options.debug) {
|
182 | console.log("Running remotely: " + JSON.stringify(command));
|
183 | }
|
184 | promise = new Promise(function (resolve, reject) {
|
185 | _this.channel.subscribe(_this.TOPIC_RESPONSE, function () {
|
186 | _this.channel.on('message', function (topic, buffer) {
|
187 | if (_this.TOPIC_RESPONSE === topic) {
|
188 | var message = buffer.toString();
|
189 | var result = JSON.parse(message);
|
190 | if (result.error) {
|
191 | reject(result.error);
|
192 | }
|
193 | else if (result.value) {
|
194 | resolve(result.value);
|
195 | }
|
196 | else {
|
197 | resolve();
|
198 | }
|
199 | }
|
200 | });
|
201 | _this.channel.publish(_this.TOPIC_REQUEST, JSON.stringify(command));
|
202 | });
|
203 | });
|
204 | return [2 , promise];
|
205 | }
|
206 | });
|
207 | });
|
208 | };
|
209 | RemoteChrome.prototype.close = function () {
|
210 | return __awaiter(this, void 0, void 0, function () {
|
211 | return __generator(this, function (_a) {
|
212 | this.channel.publish(this.TOPIC_END, JSON.stringify({ channelId: this.channelId, client: true }));
|
213 | this.channel.end();
|
214 | return [2 ];
|
215 | });
|
216 | });
|
217 | };
|
218 | return RemoteChrome;
|
219 | }());
|
220 | exports.default = RemoteChrome;
|
221 |
|
\ | No newline at end of file |