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

Commit 7cbee73

Browse files
authored
Get rid of module returning a constructor (#228)
1 parent fdbbea5 commit 7cbee73

File tree

10 files changed

+149
-124
lines changed

10 files changed

+149
-124
lines changed

src/agent/config.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,5 +183,21 @@ module.exports = {
183183
internal: {
184184
registerDelayOnFetcherErrorSec: 300, // 5 minutes.
185185
maxRegistrationRetryDelay: 40
186-
}
186+
},
187+
188+
/**
189+
* @property {boolean} Used by tests to force loading of a new agent if one
190+
* exists already
191+
* @memberof DebugAgentConfig
192+
* @private
193+
*/
194+
forceNewAgent_: false,
195+
196+
/**
197+
* @property {boolean} Uses by tests to cause the start() function to return
198+
* the debuglet.
199+
* @memberof DebugAgentConfig
200+
* @private
201+
*/
202+
testMode_: false
187203
};

src/debug.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
* Copyright 2015 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+
var common = require('@google-cloud/common');
19+
var util = require('util');
20+
21+
/**
22+
* <p class="notice">
23+
* **This is an experimental release of Stackdriver Debug.** This API is not
24+
* covered by any SLA of deprecation policy and may be subject to backwards
25+
* incompatible changes.
26+
* </p>
27+
*
28+
* This module provides Stackdriver Debugger support for Node.js applications.
29+
* [Stackdriver Debugger](https://cloud.google.com/debug/) is a feature of
30+
* [Google Cloud Platform](https://cloud.google.com/) that lets you debug your
31+
* applications in production without stopping or pausing your application.
32+
*
33+
* This module provides an agent that lets you automatically enable debugging
34+
* without changes to your application.
35+
*
36+
* @constructor
37+
* @alias module:debug
38+
*
39+
* @resource [What is Stackdriver Debug]{@link https://cloud.google.com/debug/}
40+
*
41+
* @param {object} options - [Configuration object](#/docs)
42+
*/
43+
function Debug(options) {
44+
if (!(this instanceof Debug)) {
45+
options = common.util.normalizeArguments(this, options);
46+
return new Debug(options);
47+
}
48+
49+
var config = {
50+
projectIdRequired: false,
51+
baseUrl: 'https://clouddebugger.googleapis.com/v2',
52+
scopes: ['https://www.googleapis.com/auth/cloud_debugger'],
53+
packageJson: require('../package.json')
54+
};
55+
56+
common.Service.call(this, config, options);
57+
58+
// FIXME(ofrobots): We need our own copy of options because Service may
59+
// default to '{{projectId}}' when options doesn't contain the `projectId`.
60+
// property. This breaks the SSOT principle. Remove this when
61+
// https://github.com/GoogleCloudPlatform/google-cloud-node/issues/1891
62+
// is resolved.
63+
this.options = options;
64+
}
65+
util.inherits(Debug, common.Service);
66+
67+
module.exports = Debug;

src/index.js

Lines changed: 20 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -16,64 +16,18 @@
1616

1717
'use strict';
1818

19-
var common = require('@google-cloud/common');
19+
var Debug = require('./debug.js');
2020
var Debuglet = require('./agent/debuglet.js');
21-
var util = require('util');
22-
23-
/**
24-
* <p class="notice">
25-
* **This is an experimental release of Stackdriver Debug.** This API is not
26-
* covered by any SLA of deprecation policy and may be subject to backwards
27-
* incompatible changes.
28-
* </p>
29-
*
30-
* This module provides Stackdriver Debugger support for Node.js applications.
31-
* [Stackdriver Debugger](https://cloud.google.com/debug/) is a feature of
32-
* [Google Cloud Platform](https://cloud.google.com/) that lets you debug your
33-
* applications in production without stopping or pausing your application.
34-
*
35-
* This module provides an agent that lets you automatically enable debugging
36-
* without changes to your application.
37-
*
38-
* @constructor
39-
* @alias module:debug
40-
*
41-
* @resource [What is Stackdriver Debug]{@link https://cloud.google.com/debug/}
42-
*
43-
* @param {object} options - [Configuration object](#/docs)
44-
*/
45-
function Debug(options) {
46-
if (!(this instanceof Debug)) {
47-
options = common.util.normalizeArguments(this, options);
48-
return new Debug(options);
49-
}
50-
51-
var config = {
52-
projectIdRequired: false,
53-
baseUrl: 'https://clouddebugger.googleapis.com/v2',
54-
scopes: ['https://www.googleapis.com/auth/cloud_debugger'],
55-
packageJson: require('../package.json')
56-
};
57-
58-
common.Service.call(this, config, options);
59-
60-
// FIXME(ofrobots): We need our own copy of options because Service may
61-
// default to '{{projectId}}' when options doesn't contain the `projectId`.
62-
// property. This breaks the SSOT principle. Remove this when
63-
// https://github.com/GoogleCloudPlatform/google-cloud-node/issues/1891
64-
// is resolved.
65-
this.options = options;
66-
}
67-
util.inherits(Debug, common.Service);
6821

22+
// Singleton.
6923
var debuglet;
7024

7125
/**
7226
* Start the Debug agent that will make your application available for debugging
7327
* with Stackdriver Debug.
7428
*
75-
* @param {object=} config - Debug configuration. TODO(ofrobots): get rid of
76-
* config.js and include jsdoc here?
29+
* @param {object=} options - Options
30+
* @param {object=} options.debugAgent - Debug agent configuration
7731
* TODO: add an optional callback function.
7832
*
7933
* @resource [Introductory video]{@link
@@ -82,15 +36,23 @@ var debuglet;
8236
* @example
8337
* debug.startAgent();
8438
*/
85-
Debug.prototype.startAgent = function(config) {
86-
// config.forceNewAgent_ is for testing purposes only.
87-
if (debuglet && !config.forceNewAgent_) {
88-
throw new Error('Debug Agent has already been started');
39+
function start(options) {
40+
options = options || {};
41+
var agentConfig = options.debug || {};
42+
43+
// forceNewAgent_ is for testing purposes only.
44+
if (debuglet && !agentConfig.forceNewAgent_) {
45+
throw new Error('Debug Agent has already been starterd');
8946
}
9047

91-
debuglet = new Debuglet(this, config);
48+
var debug = new Debug(options);
49+
debuglet = new Debuglet(debug, agentConfig);
9250
debuglet.start();
93-
this.private_ = debuglet;
94-
};
9551

96-
module.exports = Debug;
52+
// We return the debuglet to facilitate testing.
53+
return agentConfig.testMode_ ? debuglet : undefined;
54+
}
55+
56+
module.exports = {
57+
start: start
58+
};

system-test/test-controller.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ assert.ok(
2828

2929
var Controller = require('../src/controller.js');
3030
var Debuggee = require('../src/debuggee.js');
31-
var debug = require('../')();
31+
var debug = require('../src/debug.js')();
3232

3333

3434
describe('Controller', function() {

system-test/test-e2e.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ var _ = require('lodash'); // for _.find. Can't use ES6 yet.
2121
var cp = require('child_process');
2222
var semver = require('semver');
2323
var promisifyAll = require('@google-cloud/common').util.promisifyAll;
24-
var Debug = require('..');
24+
var Debug = require('../src/debug.js');
2525
var Debugger = require('../test/debugger.js');
2626

2727
var CLUSTER_WORKERS = 3;

test/fixtures/fib.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ function fib(n) {
2121
* limitations under the License.
2222
*/
2323

24-
var debug = require('../..')();
25-
debug.startAgent({
26-
logLevel: 2,
27-
maxLogsPerSecond: 2,
28-
logDelaySeconds: 5,
29-
breakpointUpdateIntervalSec: 1
24+
var debuglet = require('../..').start({
25+
debug: {
26+
logLevel: 2,
27+
maxLogsPerSecond: 2,
28+
logDelaySeconds: 5,
29+
breakpointUpdateIntervalSec: 1,
30+
testMode_: true
31+
}
3032
});
3133

3234
// Make troubleshooting easier if run by itself
@@ -43,7 +45,7 @@ var registrationTimeout = setTimeout(function() {
4345
}, 2000);
4446
}, 5000);
4547

46-
debug.private_.once('registered', function() {
48+
debuglet.once('registered', function() {
4749
if (timedOut) {
4850
return;
4951
}
@@ -56,7 +58,7 @@ debug.private_.once('registered', function() {
5658
}
5759
};
5860

59-
var debuggee = debug.private_.debuggee_;
61+
var debuggee = debuglet.debuggee_;
6062
setErrorIfNotOk(debuggee, 'should create debuggee');
6163
setErrorIfNotOk(debuggee.project, 'debuggee should have a project');
6264
setErrorIfNotOk(debuggee.id, 'debuggee should have registered');

0 commit comments

Comments
 (0)