|
| 1 | +/** |
| 2 | + * Copyright 2016 Google Inc. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +'use strict'; |
| 18 | + |
| 19 | +/*! |
| 20 | + * @module debug/debugger |
| 21 | + */ |
| 22 | + |
| 23 | +var pjson = require('../package.json'); |
| 24 | +var common = require('@google-cloud/common'); |
| 25 | +var qs = require('querystring'); |
| 26 | +var util = require('util'); |
| 27 | + |
| 28 | +/** @const {string} Cloud Debug API endpoint */ |
| 29 | +var API = 'https://clouddebugger.googleapis.com/v2/debugger'; |
| 30 | + |
| 31 | +/** |
| 32 | + * @constructor |
| 33 | + */ |
| 34 | +function Debugger(debug) { |
| 35 | + common.ServiceObject.call(this, { |
| 36 | + parent: debug, |
| 37 | + baseUrl: '/debugger' |
| 38 | + }); |
| 39 | + |
| 40 | + /** @private {string} */ |
| 41 | + this.nextWaitToken_ = null; |
| 42 | + |
| 43 | + this.clientVersion_ = pjson.name + '/client/v' + pjson.version; |
| 44 | +} |
| 45 | + |
| 46 | +util.inherits(Debugger, common.ServiceObject); |
| 47 | + |
| 48 | +/** |
| 49 | + * Gets a list of debuggees in a given project to which the user can set |
| 50 | + * breakpoints. |
| 51 | + * @param {string} projectId - The project ID for the project whose debuggees |
| 52 | + * should be listed. |
| 53 | + * @param {boolean=} includeInactive - Whether or not to include inactive |
| 54 | + * debuggees in the list (default false). |
| 55 | + * @param {!function(?Error,Debuggee[]=)} callback - A function that will be |
| 56 | + * called with a list of Debuggee objects as a parameter, or an Error object |
| 57 | + * if an error occurred in obtaining it. |
| 58 | + */ |
| 59 | +Debugger.prototype.listDebuggees = function(projectId, includeInactive, callback) { |
| 60 | + if (typeof(includeInactive) === 'function') { |
| 61 | + callback = includeInactive; |
| 62 | + includeInactive = false; |
| 63 | + } |
| 64 | + |
| 65 | + var query = { |
| 66 | + clientVersion: this.clientVersion_, |
| 67 | + includeInactive: includeInactive, |
| 68 | + project: projectId |
| 69 | + }; |
| 70 | + |
| 71 | + var uri = API + '/debuggees?' + qs.stringify(query); |
| 72 | + this.request({uri: uri, json: true}, function(err, body, response) { |
| 73 | + if (err) { |
| 74 | + callback(err); |
| 75 | + } else if (!response) { |
| 76 | + callback(new Error('unknown error - request response missing')); |
| 77 | + } else if (response.statusCode !== 200) { |
| 78 | + callback(new Error('unable to list debuggees, status code ' + |
| 79 | + response.statusCode)); |
| 80 | + } else if (!body) { |
| 81 | + callback(new Error('invalid response body from server')); |
| 82 | + } else { |
| 83 | + if (body.debuggees) { |
| 84 | + callback(null, body.debuggees); |
| 85 | + } else { |
| 86 | + callback(null, []); |
| 87 | + } |
| 88 | + } |
| 89 | + }); |
| 90 | +}; |
| 91 | + |
| 92 | +/** |
| 93 | + * Gets a list of breakpoints in a given debuggee. |
| 94 | + * @param {string} debuggeeId - The ID of the debuggee whose breakpoints should |
| 95 | + * be listed. |
| 96 | + * @param {object=} options - An object containing options on the list of |
| 97 | + * breakpoints. |
| 98 | + * @param {boolean=} options.includeAllUsers - If set to true, include |
| 99 | + * breakpoints set by all users, or just by the caller (default false). |
| 100 | + * @param {boolean=} options.includeInactive - Whether or not to include |
| 101 | + * inactive breakpoints in the list (default false). |
| 102 | + * @param {boolean=} options.stripResults - If set to true, breakpoints will be |
| 103 | + * stripped of the following fields: stackFrames, evaluatedExpressions, |
| 104 | + * and variableTable (default false). |
| 105 | + * @param {string=} options.action - Either 'CAPTURE' or 'LOG'. If specified, |
| 106 | + * only breakpoints with a matching action will be part of the list. |
| 107 | + * @param {!function(?Error,Breakpoint[]=)} callback - A function that will be |
| 108 | + * called with a list of Breakpoint objects as a parameter, or an Error |
| 109 | + * object if an error occurred in obtaining it. |
| 110 | + */ |
| 111 | +Debugger.prototype.listBreakpoints = function(debuggeeId, options, callback) { |
| 112 | + if (typeof(options) === 'function') { |
| 113 | + callback = options; |
| 114 | + options = {}; |
| 115 | + } |
| 116 | + |
| 117 | + var query = { |
| 118 | + clientVersion: this.clientVersion_, |
| 119 | + includeAllUsers: !!options.includeAllUsers, |
| 120 | + includeInactive: !!options.includeInactive, |
| 121 | + stripResults: !!options.stripResults |
| 122 | + }; |
| 123 | + if (options.action) { |
| 124 | + query.action = { value: options.action }; |
| 125 | + } |
| 126 | + if (this.nextWaitToken_) { |
| 127 | + query.waitToken = this.nextWaitToken_; |
| 128 | + } |
| 129 | + |
| 130 | + var uri = API + '/debuggees/' + encodeURIComponent(debuggeeId) + |
| 131 | + '/breakpoints?' + qs.stringify(query); |
| 132 | + this.request({uri: uri, json: true}, function(err, body, response) { |
| 133 | + if (err) { |
| 134 | + callback(err); |
| 135 | + } else if (!response) { |
| 136 | + callback(new Error('unknown error - request response missing')); |
| 137 | + } else if (response.statusCode !== 200) { |
| 138 | + callback(new Error('unable to list breakpoints, status code ' + |
| 139 | + response.statusCode)); |
| 140 | + } else if (!body) { |
| 141 | + callback(new Error('invalid response body from server')); |
| 142 | + } else { |
| 143 | + if (body.breakpoints) { |
| 144 | + callback(null, body.breakpoints); |
| 145 | + } else { |
| 146 | + callback(null, []); |
| 147 | + } |
| 148 | + } |
| 149 | + }); |
| 150 | +}; |
| 151 | + |
| 152 | +/** |
| 153 | + * Gets information about a given breakpoint. |
| 154 | + * @param {string} debuggee - The ID of the debuggee in which the breakpoint |
| 155 | + * is set. |
| 156 | + * @param {string} breakpointId - The ID of the breakpoint to get information |
| 157 | + * about. |
| 158 | + * @param {!function(?Error,Breakpoint=)} callback - A function that will be |
| 159 | + * called with information about the given breakpoint, or an Error object |
| 160 | + * if an error occurred in obtaining its information. |
| 161 | + */ |
| 162 | +Debugger.prototype.getBreakpoint = function(debuggeeId, breakpointId, callback) { |
| 163 | + var query = { |
| 164 | + clientVersion: this.clientVersion_ |
| 165 | + }; |
| 166 | + |
| 167 | + var uri = API + '/debuggees/' + encodeURIComponent(debuggeeId) + |
| 168 | + '/breakpoints/' + encodeURIComponent(breakpointId) + |
| 169 | + '?' + qs.stringify(query); |
| 170 | + this.request({uri: uri, json: true}, function(err, body, response) { |
| 171 | + if (err) { |
| 172 | + callback(err); |
| 173 | + } else if (!response) { |
| 174 | + callback(new Error('unknown error - request response missing')); |
| 175 | + } else if (response.statusCode !== 200) { |
| 176 | + callback(new Error('unable to get breakpoint info, status code ' + |
| 177 | + response.statusCode)); |
| 178 | + } else if (!body || !body.breakpoint) { |
| 179 | + callback(new Error('invalid response body from server')); |
| 180 | + } else { |
| 181 | + callback(null, body.breakpoint); |
| 182 | + } |
| 183 | + }); |
| 184 | +}; |
| 185 | + |
| 186 | +/** |
| 187 | + * Sets a new breakpoint. |
| 188 | + * @param {Debuggee} debuggeeId - The ID of the debuggee in which the breakpoint |
| 189 | + * should be set. |
| 190 | + * @param {Breakpoint} breakpoint - An object representing information about |
| 191 | + * the breakpoint to set. |
| 192 | + * @param {!function(?Error,Breakpoint=)} callback - A function that will be |
| 193 | + * called with information about the breakpoint that was just set, or an |
| 194 | + * Error object if an error occurred in obtaining its information. Note |
| 195 | + * that the Breakpoint object here will differ from the input object in |
| 196 | + * that its id field will be set. |
| 197 | + */ |
| 198 | +Debugger.prototype.setBreakpoint = function(debuggeeId, breakpoint, callback) { |
| 199 | + var query = { |
| 200 | + clientVersion: this.clientVersion_ |
| 201 | + }; |
| 202 | + var options = { |
| 203 | + uri: API + '/debuggees/' + encodeURIComponent(debuggeeId) + |
| 204 | + '/breakpoints/set?' + qs.stringify(query), |
| 205 | + method: 'POST', |
| 206 | + json: true, |
| 207 | + body: breakpoint |
| 208 | + }; |
| 209 | + |
| 210 | + this.request(options, function(err, body, response) { |
| 211 | + if (err) { |
| 212 | + callback(err); |
| 213 | + } else if (!response) { |
| 214 | + callback(new Error('unknown error - request response missing')); |
| 215 | + } else if (response.statusCode !== 200) { |
| 216 | + callback(new Error('unable to set breakpoint, status code ' + |
| 217 | + response.statusCode)); |
| 218 | + } else if (!body || !body.breakpoint) { |
| 219 | + callback(new Error('invalid response body from server')); |
| 220 | + } else { |
| 221 | + callback(null, body.breakpoint); |
| 222 | + } |
| 223 | + }); |
| 224 | +}; |
| 225 | + |
| 226 | +/** |
| 227 | + * Deletes a breakpoint. |
| 228 | + * @param {Debuggee} debuggeeId - The ID of the debuggee to which the breakpoint |
| 229 | + * belongs. |
| 230 | + * @param {Breakpoint} breakpointId - The ID of the breakpoint to delete. |
| 231 | + * @param {!function(?Error)} callback - A function that will be |
| 232 | + * called with a Error object as a parameter if an error occurred in |
| 233 | + * deleting a breakpoint. If no error occurred, the first argument will be |
| 234 | + * set to null. |
| 235 | + */ |
| 236 | +Debugger.prototype.deleteBreakpoint = function(debuggeeId, breakpointId, callback) { |
| 237 | + var query = { |
| 238 | + clientVersion: this.clientVersion_ |
| 239 | + }; |
| 240 | + var options = { |
| 241 | + uri: API + '/debuggees/' + encodeURIComponent(debuggeeId) + |
| 242 | + '/breakpoints/' + encodeURIComponent(breakpointId) + '?' + |
| 243 | + qs.stringify(query), |
| 244 | + method: 'DELETE', |
| 245 | + json: true |
| 246 | + }; |
| 247 | + |
| 248 | + this.request(options, function(err, body, response) { |
| 249 | + if (err) { |
| 250 | + callback(err); |
| 251 | + } else if (!response) { |
| 252 | + callback(new Error('unknown error - request response missing')); |
| 253 | + } else if (response.statusCode !== 200) { |
| 254 | + callback(new Error('unable to delete breakpoint, status code ' + |
| 255 | + response.statusCode)); |
| 256 | + } else if (Object.keys(body).length > 0) { |
| 257 | + callback(new Error('response body is non-empty')); |
| 258 | + } else { |
| 259 | + callback(null); |
| 260 | + } |
| 261 | + }); |
| 262 | +}; |
| 263 | + |
| 264 | +module.exports = Debugger; |
0 commit comments