UNPKG

2.85 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.Doctor = void 0;
4/**
5 * Wechaty Chatbot SDK - https://github.com/wechaty/wechaty
6 *
7 * @copyright 2016 Huan LI (李卓桓) <https://github.com/huan>, and
8 * Wechaty Contributors <https://github.com/wechaty>.
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 *
22 */
23const net_1 = require("net");
24const config_1 = require("./config");
25class Doctor {
26 constructor() {
27 config_1.log.verbose('Doctor', 'constructor()');
28 }
29 chromedriverVersion() {
30 const spawn = require('child_process').spawnSync;
31 let version;
32 try {
33 const cmd = spawn('chromedriver', ['--version']);
34 version = cmd.error || cmd.stdout.toString() || cmd.stderr.toString();
35 }
36 catch (e) {
37 version = e.message;
38 }
39 return version;
40 }
41 /**
42 * https://gist.github.com/tedmiston/5935757
43 */
44 testTcp() {
45 config_1.log.verbose('Doctor', 'testTcp()');
46 return new Promise((resolve, reject) => {
47 /**
48 * Server
49 */
50 const server = net_1.createServer(socket => socket.pipe(socket));
51 /**
52 * Promise Reject
53 */
54 server.on('error', reject);
55 server.on('close', () => config_1.log.silly('Doctor', 'testTcp() server closed'));
56 server.listen(8788, 'localhost', () => {
57 /**
58 * Client
59 */
60 const client = new net_1.Socket();
61 client.connect(8788, 'localhost', () => {
62 config_1.log.silly('Doctor', 'testTcp() client connected');
63 client.write('ding');
64 });
65 client.on('data', () => {
66 /**
67 * Promise Resolve
68 */
69 resolve(true);
70 client.destroy(); // kill client after server's response
71 });
72 /**
73 * Promise Reject
74 */
75 client.on('error', reject);
76 client.on('close', () => server.close());
77 });
78 });
79 }
80}
81exports.Doctor = Doctor;
82//# sourceMappingURL=doctor.js.map
\No newline at end of file