Skip to content
This repository was archived by the owner on Apr 3, 2024. It is now read-only.

Commit 638f902

Browse files
committed
refactorings
* rename debugletapi to controller * rename apiclasses.js to status-message.js * add a debuggee class * add a basic debuggee object
1 parent c5d3d22 commit 638f902

File tree

12 files changed

+230
-115
lines changed

12 files changed

+230
-115
lines changed

.jshintrc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
"indent": 2,
99
"latedef": "nofunc",
1010
"maxlen": 100,
11-
"newcap": true,
1211
"node": true,
1312
"noarg": true,
1413
"quotmark": "single",

src/agent/debuglet.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ var util = require('util');
2323
var semver = require('semver');
2424

2525
var v8debugapi = require('./v8debugapi.js');
26-
var DebugletApi = require('../debugletapi.js');
26+
var DebugletApi = require('../controller.js');
2727
var scanner = require('./scanner.js');
2828
var Logger = require('@google/cloud-diagnostics-common').logger;
29-
var StatusMessage = require('../apiclasses.js').StatusMessage;
29+
var StatusMessage = require('../status-message.js');
3030
var SourceMapper = require('./sourcemapper.js');
3131

3232
var assert = require('assert');

src/agent/state.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ var remove = lodash.remove;
3232
var flatten = lodash.flatten;
3333
var isEmpty = lodash.isEmpty;
3434

35-
var StatusMessage = require('../apiclasses.js').StatusMessage;
35+
var StatusMessage = require('../status-message.js');
3636

3737
// Error message indices into the resolved variable table.
3838
var BUFFER_FULL_MESSAGE_INDEX = 0;

src/agent/v8debugapi.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@
2323

2424
/** @const */ var state = require('./state.js');
2525
/** @const */ var logModule = require('@google/cloud-diagnostics-common').logger;
26-
/** @const */ var apiclasses = require('../apiclasses.js');
27-
/** @const */ var StatusMessage = apiclasses.StatusMessage;
26+
/** @const */ var StatusMessage = require('../status-message.js');
2827

2928
/** @const */ var messages = {
3029
INVALID_BREAKPOINT: 'invalid snapshot - id or location missing',
Lines changed: 17 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,23 @@
1616

1717
'use strict';
1818

19+
/*!
20+
* @module debug/controller
21+
*/
22+
1923
var fs = require('fs');
20-
var path = require('path');
2124
var assert = require('assert');
22-
var crypto = require('crypto');
23-
var pjson = require('../package.json');
2425
var qs = require('querystring');
2526
var utils = require('@google/cloud-diagnostics-common').utils;
26-
var StatusMessage = require('./apiclasses.js').StatusMessage;
27+
var Debuggee = require('./debuggee.js');
2728

2829
/** @const {string} Cloud Debug API endpoint */
2930
var API = 'https://clouddebugger.googleapis.com/v2/controller';
3031

31-
/* c.f. the Java Debugger agent */
32-
/** @const {string} */ var DEBUGGEE_MODULE_LABEL = 'module';
33-
/** @const {string} */ var DEBUGGEE_MAJOR_VERSION_LABEL = 'version';
34-
/** @const {string} */ var DEBUGGEE_MINOR_VERSION_LABEL = 'minorversion';
35-
36-
3732
/**
3833
* @constructor
3934
*/
40-
function DebugletApi(config, debug) {
35+
function Controller(config, debug) {
4136
config = config || {};
4237

4338
/** @priavate {Debug} */
@@ -68,7 +63,7 @@ function DebugletApi(config, debug) {
6863
* @param {Logger} logger a logger
6964
* @param {!function(?Error)} callback
7065
*/
71-
DebugletApi.prototype.init = function(uid, logger, callback) {
66+
Controller.prototype.init = function(uid, logger, callback) {
7267
var that = this;
7368
that.uid_ = uid;
7469
that.nextWaitToken_ = null;
@@ -105,7 +100,7 @@ DebugletApi.prototype.init = function(uid, logger, callback) {
105100
* Register to the API
106101
* @param {!function(?Error,Object=)} callback
107102
*/
108-
DebugletApi.prototype.register = function(callback) {
103+
Controller.prototype.register = function(callback) {
109104
this.register_(null, callback);
110105
};
111106

@@ -114,7 +109,7 @@ DebugletApi.prototype.register = function(callback) {
114109
* Register an error to the API
115110
* @param {!string} errorMessage to be reported to the Debug API
116111
*/
117-
DebugletApi.prototype.registerError = function(message) {
112+
Controller.prototype.registerError = function(message) {
118113
this.register_(message, function() {});
119114
};
120115

@@ -126,66 +121,12 @@ DebugletApi.prototype.registerError = function(message) {
126121
* @param {!function(?Error,Object=)} callback
127122
* @private
128123
*/
129-
DebugletApi.prototype.register_ = function(errorMessage, callback) {
124+
Controller.prototype.register_ = function(errorMessage, callback) {
130125
var that = this;
131-
132-
var cwd = process.cwd();
133-
var mainScript = path.relative(cwd, process.argv[1]);
134-
135-
var version = 'google.com/node-' +
136-
(that.onGCP ? 'gcp' : 'standalone') +
137-
'/v' + pjson.version;
138-
var desc = process.title + ' ' + mainScript;
139-
var labels = {
140-
'main script': mainScript,
141-
'process.title': process.title,
142-
'node version': process.versions.node,
143-
'V8 version': process.versions.v8,
144-
'agent.name': pjson.name,
145-
'agent.version': pjson.version,
146-
'projectid': that.project_
147-
};
148-
149-
var serviceName = that.serviceName_;
150-
if (serviceName) {
151-
desc += ' module:' + serviceName;
152-
labels[DEBUGGEE_MODULE_LABEL] = serviceName;
153-
}
154-
155-
var serviceVersion = that.serviceVersion_;
156-
if (serviceVersion) {
157-
desc += ' version:' + serviceVersion;
158-
if (serviceVersion !== 'default') {
159-
labels[DEBUGGEE_MAJOR_VERSION_LABEL] = serviceVersion;
160-
}
161-
}
162-
163-
var descriptor = that.descriptor_;
164-
if (descriptor) {
165-
desc += ' description:' + descriptor;
166-
}
167-
168-
if (process.env.GAE_MINOR_VERSION) {
169-
labels[DEBUGGEE_MINOR_VERSION_LABEL] = process.env.GAE_MINOR_VERSION;
170-
}
171-
172-
var uniquifier = desc + version + that.uid_ + that.sourceContext_ +
173-
JSON.stringify(labels);
174-
uniquifier = crypto.createHash('sha1').update(uniquifier).digest('hex');
175-
176-
var debuggee = {
177-
project: that.project_,
178-
uniquifier: uniquifier,
179-
description: desc,
180-
agentVersion: version,
181-
labels: labels,
182-
sourceContexts: [that.sourceContext_]
183-
};
184-
185-
if (errorMessage) {
186-
debuggee.status = new StatusMessage(StatusMessage.UNSPECIFIED, errorMessage,
187-
true);
188-
}
126+
var debuggee = new Debuggee(
127+
that.project_, that.uid_,
128+
{service: that.serviceName_, version: that.serviceVersion_},
129+
that.sourceContext_, that.descriptor_, errorMessage, that.onGCP);
189130

190131
var options = {
191132
uri: API + '/debuggees/register',
@@ -214,7 +155,7 @@ DebugletApi.prototype.register_ = function(errorMessage, callback) {
214155
* Fetch the list of breakpoints from the server. Assumes we have registered.
215156
* @param {!function(?Error,Object=,Object=)} callback accepting (err, response, body)
216157
*/
217-
DebugletApi.prototype.listBreakpoints = function(callback) {
158+
Controller.prototype.listBreakpoints = function(callback) {
218159
var that = this;
219160
assert(that.debuggeeId_, 'should register first');
220161
var query = { success_on_timeout: true };
@@ -250,7 +191,7 @@ DebugletApi.prototype.listBreakpoints = function(callback) {
250191
* @param {!Breakpoint} breakpoint
251192
* @param {!Function} callback accepting (err, body)
252193
*/
253-
DebugletApi.prototype.updateBreakpoint =
194+
Controller.prototype.updateBreakpoint =
254195
function(breakpoint, callback) {
255196
assert(this.debuggeeId_, 'should register first');
256197

@@ -280,4 +221,4 @@ DebugletApi.prototype.updateBreakpoint =
280221
}
281222
};
282223

283-
module.exports = DebugletApi;
224+
module.exports = Controller;

src/debuggee.js

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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+
'use strict';
17+
18+
var crypto = require('crypto');
19+
var path = require('path');
20+
var pjson = require('../package.json');
21+
var StatusMessage = require('./status-message.js');
22+
var _ = require('lodash');
23+
24+
/**
25+
* Creates a Debuggee service object.
26+
* @ref https://cloud.google.com/debugger/api/reference/rest/v2/Debuggee
27+
*
28+
* @param {string} projectId - Google Cloud Project ID
29+
* @param {string} uid - unique identified for the source code on this instance.
30+
* @param {?object} serviceContext
31+
* @param {string} serviceContext.service - A string identifying the service/
32+
* module that this instance belongs to.
33+
* @param {string} serviceContext.version - A string identifying the version of
34+
* the service.
35+
* @param {?object} sourceContext
36+
* @param {?string} description - A user specified string identifying this
37+
* debuggable instance.
38+
* @param {?string} errorMessage - A error string to register this as a erroring
39+
* debuggable instance. This is useful if we have a problem starting the
40+
* debugger support, and want to report to the API so that the users has a
41+
* way of noticing.
42+
* @param {?boolean} onGCP - set to true when the debuggee is running inside
43+
* Google Cloud Platform.
44+
*/
45+
function Debuggee(projectId, uid, serviceContext, sourceContext, description,
46+
errorMessage, onGCP) {
47+
if (!(this instanceof Debuggee)) {
48+
return new Debuggee(projectId, uid, serviceContext, sourceContext,
49+
description, errorMessage, onGCP);
50+
}
51+
52+
if (!_.isString(projectId)) {
53+
throw new Error('projectId must be a string');
54+
}
55+
if (!_.isString(uid)) {
56+
throw new Error('uid must be a string');
57+
}
58+
59+
var cwd = process.cwd();
60+
var mainScript = path.relative(cwd, process.argv[1]);
61+
62+
var version = 'google.com/node-' + (onGCP ? 'gcp' : 'standalone') + '/v' +
63+
pjson.version;
64+
var desc = process.title + ' ' + mainScript;
65+
66+
var labels = {
67+
'main script': mainScript,
68+
'process.title': process.title,
69+
'node version': process.versions.node,
70+
'V8 version': process.versions.v8,
71+
'agent.name': pjson.name,
72+
'agent.version': pjson.version,
73+
'projectid': projectId
74+
};
75+
76+
if (serviceContext) {
77+
if (_.isString(serviceContext.service) &&
78+
serviceContext.service !== 'default') {
79+
// As per app-engine-ids, the module label is not reported
80+
// when it happens to be 'default'.
81+
labels.module = serviceContext.service;
82+
desc += ' module:' + serviceContext.service;
83+
}
84+
85+
if (_.isString(serviceContext.version)) {
86+
labels.version = serviceContext.version;
87+
desc += ' version:' + serviceContext.version;
88+
}
89+
}
90+
91+
if (description) {
92+
desc += ' description:' + description;
93+
}
94+
95+
var uniquifier =
96+
desc + version + uid + sourceContext + JSON.stringify(labels);
97+
uniquifier = crypto.createHash('sha1').update(uniquifier).digest('hex');
98+
99+
if (errorMessage) {
100+
this.statusMessage =
101+
new StatusMessage(StatusMessage.UNSPECIFIED, errorMessage, true);
102+
}
103+
104+
this.project = projectId;
105+
this.uniquifier = uniquifier;
106+
this.description = desc;
107+
this.agentVersion = version;
108+
this.labels = labels;
109+
if (sourceContext) {
110+
this.sourceContexts = [sourceContext];
111+
}
112+
}
113+
114+
module.exports = Debuggee;

src/index.js

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,25 @@ var _ = require('lodash');
2424

2525
/**
2626
* <p class="notice">
27-
* **This is an experimental release of Stackdriver Debug.** This API is not
27+
* **This is an experimental release of Stackdriver Debug.** This API is not
2828
* covered by any SLA of deprecation policy and may be subject to backwards
2929
* incompatible changes.
3030
* </p>
31-
*
32-
* This module provides Stackdriver Debugger support for Node.js applications.
33-
* [Stackdriver Debugger](https://cloud.google.com/debug/) is a feature of
31+
*
32+
* This module provides Stackdriver Debugger support for Node.js applications.
33+
* [Stackdriver Debugger](https://cloud.google.com/debug/) is a feature of
3434
* [Google Cloud Platform](https://cloud.google.com/) that lets you debug your
3535
* applications in production without stopping or pausing your application.
36-
*
36+
*
3737
* This module provides an agent that lets you automatically enable debugging
38-
* without changes to your application.
39-
*
38+
* without changes to your application.
39+
*
4040
* @constructor
4141
* @alias module:debug
42-
*
42+
*
4343
* @resource [What is Stackdriver Debug]{@link https://cloud.google.com/debug/}
44-
*
45-
* @param {object} options - [Configuration object](#/docs). NOTE: at the moment
46-
* this parameter is ignored.
44+
*
45+
* @param {object} options - [Configuration object](#/docs)
4746
*/
4847
function Debug(options) {
4948
if (!(this instanceof Debug)) {
@@ -52,9 +51,10 @@ function Debug(options) {
5251
}
5352

5453
var config = {
54+
projectIdRequired: false,
5555
baseUrl: 'https://clouddebugger.googleapis.com/v2',
5656
scopes: [
57-
// TODO: do we still need cloud-platform scope?
57+
// TODO: do we still need cloud-platform scope?
5858
'https://www.googleapis.com/auth/cloud-platform',
5959
'https://www.googleapis.com/auth/cloud_debugletcontroller'
6060
// TODO: the client library probably wants cloud_debugger scope as well.
@@ -90,13 +90,14 @@ var debuglet;
9090
/**
9191
* Start the Debug agent that will make your application available for debugging
9292
* with Stackdriver Debug.
93-
*
93+
*
9494
* @param {object=} config - Debug configuration. TODO(ofrobots): get rid of
9595
* config.js and include jsdoc here?
9696
* TODO: add an optional callback function.
97-
*
98-
* @resource [Introductory video]{@link https://www.youtube.com/watch?v=tyHcK_kAOpw}
99-
*
97+
*
98+
* @resource [Introductory video]{@link
99+
* https://www.youtube.com/watch?v=tyHcK_kAOpw}
100+
*
100101
* @example
101102
* debug.startAgent();
102103
*/
@@ -114,4 +115,3 @@ Debug.prototype.startAgent = function(config) {
114115
};
115116

116117
module.exports = Debug;
117-

0 commit comments

Comments
 (0)