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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
'use strict';
const http = require('http');
const https = require('https');
const defaults = require('./defaults.js');
const externalRequest = require('./external-request.js');
// options.path must be specified; callback(err, data)
function devToolsInterface(path, options, callback) {
const transport = options.secure ? https : http;
const requestOptions = {
method: options.method,
host: options.host || defaults.HOST,
port: options.port || defaults.PORT,
useHostName: options.useHostName,
path: (options.alterPath ? options.alterPath(path) : path)
};
externalRequest(transport, requestOptions, callback);
}
// wrapper that allows to return a promise if the callback is omitted, it works
// for DevTools methods
function promisesWrapper(func) {
return (options, callback) => {
// options is an optional argument
if (typeof options === 'function') {
callback = options;
options = undefined;
}
options = options || {};
// just call the function otherwise wrap a promise around its execution
if (typeof callback === 'function') {
func(options, callback);
return undefined;
} else {
return new Promise((fulfill, reject) => {
func(options, (err, result) => {
if (err) {
reject(err);
} else {
fulfill(result);
}
});
});
}
};
}
function Protocol(options, callback) {
// if the local protocol is requested
if (options.local) {
const localDescriptor = require('./protocol.json');
callback(null, localDescriptor);
return;
}
// try to fetch the protocol remotely
devToolsInterface('/json/protocol', options, (err, descriptor) => {
if (err) {
callback(err);
} else {
callback(null, JSON.parse(descriptor));
}
});
}
function List(options, callback) {
devToolsInterface('/json/list', options, (err, tabs) => {
if (err) {
callback(err);
} else {
callback(null, JSON.parse(tabs));
}
});
}
function New(options, callback) {
let path = '/json/new';
if (Object.prototype.hasOwnProperty.call(options, 'url')) {
path += `?${options.url}`;
}
options.method = options.method || 'PUT'; // see #497
devToolsInterface(path, options, (err, tab) => {
if (err) {
callback(err);
} else {
callback(null, JSON.parse(tab));
}
});
}
function Activate(options, callback) {
devToolsInterface('/json/activate/' + options.id, options, (err) => {
if (err) {
callback(err);
} else {
callback(null);
}
});
}
function Close(options, callback) {
devToolsInterface('/json/close/' + options.id, options, (err) => {
if (err) {
callback(err);
} else {
callback(null);
}
});
}
function Version(options, callback) {
devToolsInterface('/json/version', options, (err, versionInfo) => {
if (err) {
callback(err);
} else {
callback(null, JSON.parse(versionInfo));
}
});
}
module.exports.Protocol = promisesWrapper(Protocol);
module.exports.List = promisesWrapper(List);
module.exports.New = promisesWrapper(New);
module.exports.Activate = promisesWrapper(Activate);
module.exports.Close = promisesWrapper(Close);
module.exports.Version = promisesWrapper(Version);
|