summaryrefslogtreecommitdiffstats
path: root/node_modules/chrome-remote-interface/lib/devtools.js
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/chrome-remote-interface/lib/devtools.js')
-rw-r--r--node_modules/chrome-remote-interface/lib/devtools.js127
1 files changed, 127 insertions, 0 deletions
diff --git a/node_modules/chrome-remote-interface/lib/devtools.js b/node_modules/chrome-remote-interface/lib/devtools.js
new file mode 100644
index 0000000..3975a00
--- /dev/null
+++ b/node_modules/chrome-remote-interface/lib/devtools.js
@@ -0,0 +1,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);