mirror of
https://github.com/c9fe/22120.git
synced 2024-11-10 04:52:43 +01:00
152 lines
3.8 KiB
JavaScript
152 lines
3.8 KiB
JavaScript
import {DEBUG, context} from './common.js';
|
|
|
|
const ROOT_SESSION = "browser";
|
|
// actually we use 'tot' but in chrome.debugger.attach 'tot' is
|
|
// not a supported version string
|
|
const VERSION = "1.3";
|
|
|
|
function promisify(context, name, err) {
|
|
return async function(...args) {
|
|
let resolver, rejector;
|
|
const pr = new Promise((res,rej) => ([resolver, rejector] = [res,rej]));
|
|
|
|
args.push(promisifiedCallback);
|
|
|
|
context[name](...args);
|
|
|
|
return pr;
|
|
|
|
function promisifiedCallback(...result) {
|
|
let error = err(name);
|
|
if ( !! error ) {
|
|
return rejector(error);
|
|
}
|
|
return resolver(...result);
|
|
}
|
|
}
|
|
}
|
|
|
|
let Ws, Fetch;
|
|
|
|
async function loadDependencies() {
|
|
if ( context == 'extension' ) {
|
|
// no need to do anything here
|
|
} else if ( context == 'node' ) {
|
|
const {default:ws} = await import('ws');
|
|
const {default:nodeFetch} = await import('node-fetch');
|
|
|
|
Ws = ws;
|
|
Fetch = nodeFetch;
|
|
}
|
|
}
|
|
|
|
export async function connect({port:port = 9222} = {}) {
|
|
if ( ! Ws || ! Fetch ) {
|
|
await loadDependencies();
|
|
}
|
|
try {
|
|
const {webSocketDebuggerUrl} = await Fetch(`http://localhost:${port}/json/version`).then(r => r.json());
|
|
const socket = new Ws(webSocketDebuggerUrl);
|
|
const Resolvers = {};
|
|
const Handlers = {};
|
|
socket.on('message', handle);
|
|
let id = 0;
|
|
|
|
let resolve;
|
|
const promise = new Promise(res => resolve = res);
|
|
|
|
socket.on('open', () => resolve());
|
|
|
|
await promise;
|
|
|
|
return {
|
|
send,
|
|
on, ons,
|
|
close
|
|
}
|
|
|
|
async function send(method, params = {}, sessionId) {
|
|
const message = {
|
|
method, params, sessionId,
|
|
id: ++id
|
|
};
|
|
if ( ! sessionId ) {
|
|
delete message[sessionId];
|
|
}
|
|
const key = `${sessionId||ROOT_SESSION}:${message.id}`;
|
|
let resolve;
|
|
const promise = new Promise(res => resolve = res);
|
|
Resolvers[key] = resolve;
|
|
socket.send(JSON.stringify(message));
|
|
DEBUG && console.log("Sent", message);
|
|
return promise;
|
|
}
|
|
|
|
async function handle(message) {
|
|
const stringMessage = message;
|
|
message = JSON.parse(message);
|
|
if ( message.error ) {
|
|
DEBUG && console.warn(message);
|
|
}
|
|
const {sessionId} = message;
|
|
const {method, params} = message;
|
|
const {id, result} = message;
|
|
|
|
if ( id ) {
|
|
const key = `${sessionId||ROOT_SESSION}:${id}`;
|
|
const resolve = Resolvers[key];
|
|
if ( ! resolve ) {
|
|
console.warn(`No resolver for key`, key, stringMessage.slice(0,140));
|
|
} else {
|
|
Resolvers[key] = undefined;
|
|
try {
|
|
await resolve(result);
|
|
} catch(e) {
|
|
console.warn(`Resolver failed`, e, key, stringMessage.slice(0,140), resolve);
|
|
}
|
|
}
|
|
} else if ( method ) {
|
|
const listeners = Handlers[method];
|
|
if ( Array.isArray(listeners) ) {
|
|
for( const func of listeners ) {
|
|
try {
|
|
func({message, sessionId});
|
|
} catch(e) {
|
|
console.warn(`Listener failed`, method, e, func.toString().slice(0,140), stringMessage.slice(0,140));
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
console.warn(`Unknown message on socket`, message);
|
|
}
|
|
}
|
|
|
|
function on(method, handler) {
|
|
let listeners = Handlers[method];
|
|
if ( ! listeners ) {
|
|
Handlers[method] = listeners = [];
|
|
}
|
|
listeners.push(wrap(handler));
|
|
}
|
|
|
|
function ons(method, handler) {
|
|
let listeners = Handlers[method];
|
|
if ( ! listeners ) {
|
|
Handlers[method] = listeners = [];
|
|
}
|
|
listeners.push(handler);
|
|
}
|
|
|
|
function close() {
|
|
socket.close();
|
|
}
|
|
|
|
function wrap(fn) {
|
|
return ({message, sessionId}) => fn(message.params)
|
|
}
|
|
} catch(e) {
|
|
console.log("Error communicating with browser", e);
|
|
process.exit(1);
|
|
}
|
|
}
|