-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathconnect-socket.js
64 lines (49 loc) · 1.93 KB
/
connect-socket.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import {
connectWebSocket,
isWebSocketCloseEvent,
isWebSocketPingEvent,
isWebSocketPongEvent,
} from "https://deno.land/std/ws/mod.ts";
import { encode } from "https://deno.land/std/encoding/utf8.ts";
import { BufReader } from "https://deno.land/std/io/bufio.ts";
import { TextProtoReader } from "https://deno.land/std/textproto/mod.ts";
import { blue, green, red, yellow } from "https://deno.land/std/fmt/colors.ts";
//const endpoint = 'ws://localhost:9222/devtools/page/A2F1DBAB8D8AB4F59B195CC5AEDC9FB1';
export default async (url='https://sheshbesh.nikfrank.com/', port=9222)=> {
const response = await fetch('http://localhost:'+port+'/json')
const tabs = await response.json();
// this entails trailing / (as browser fills that in)
const socketUrl = tabs
.find((tab={ url: null }) => tab.url === url)
.webSocketDebuggerUrl;
try {
const socket = await connectWebSocket(socketUrl);
console.log(green("ws connected! (type 'close' to quit)"));
const runCommand = (method='', params={})=>{
const id = 1*(''+Math.random()).slice(2, 10);
socket.send( JSON.stringify({ id, method, params }) );
return (new Promise(async (fulfill, reject)=> {
const respond = (res='')=> fulfill(res);
for await (const msg of socket) {
if (typeof msg === "string") {
try {
const { id: resId, result } = JSON.parse(msg);
if( id === resId )
respond( result );
} catch (err) {
console.log(err);
console.log('message not valid JSON', msg);
}
} else if (isWebSocketCloseEvent(msg)) {
console.log(red(`closed: code=${msg.code}, reason=${msg.reason}`));
}
}
}));
};
return {
socket, runCommand
};
} catch (err) {
console.error(red(`Could not connect to WebSocket: '${err}'`));
}
};