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

Commit b908545

Browse files
Allow a single direct access point to package.json (#347)
1 parent eed03fa commit b908545

File tree

12 files changed

+107
-75
lines changed

12 files changed

+107
-75
lines changed

src/agent/debuglet.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,11 @@ import * as scanner from './io/scanner';
4343
import * as SourceMapper from './io/sourcemapper';
4444
import * as debugapi from './v8/debugapi';
4545

46-
const pjson = require('../../../package.json');
47-
4846
import * as assert from 'assert';
4947

5048
import * as stackdriver from '../types/stackdriver';
5149
import {DebugAgentConfig} from './config';
52-
import {Debug} from '../client/stackdriver/debug';
50+
import {Debug, PackageInfo} from '../client/stackdriver/debug';
5351
import {Logger} from '../types/common';
5452
import {DebugApi} from './v8/debugapi';
5553

@@ -161,8 +159,10 @@ export class Debuglet extends EventEmitter {
161159
this.fetcherActive_ = false;
162160

163161
/** @private {common.logger} */
164-
this.logger_ = new common.logger(
165-
{level: common.logger.LEVELS[this.config_.logLevel], tag: pjson.name});
162+
this.logger_ = new common.logger({
163+
level: common.logger.LEVELS[this.config_.logLevel],
164+
tag: this.debug_.packageInfo.name
165+
});
166166

167167
/** @private {DebugletApi} */
168168
this.controller_ = new Controller(this.debug_);
@@ -315,7 +315,8 @@ export class Debuglet extends EventEmitter {
315315
that.debuggee_ = Debuglet.createDebuggee(
316316
// TODO: Address the case when `id` is `undefined`.
317317
project, id as string, that.config_.serviceContext, sourceContext,
318-
onGCP, that.config_.description, undefined);
318+
onGCP, that.debug_.packageInfo, that.config_.description,
319+
undefined);
319320
that.scheduleRegistration_(0 /* immediately */);
320321
that.emit('started');
321322
});
@@ -332,21 +333,22 @@ export class Debuglet extends EventEmitter {
332333
serviceContext:
333334
{service?: string, version?: string, minorVersion_?: string},
334335
sourceContext: {[key: string]: string}, onGCP: boolean,
335-
description?: string, errorMessage?: string): Debuggee {
336+
packageInfo: PackageInfo, description?: string,
337+
errorMessage?: string): Debuggee {
336338
const cwd = process.cwd();
337339
const mainScript = path.relative(cwd, process.argv[1]);
338340

339341
const version = 'google.com/node-' + (onGCP ? 'gcp' : 'standalone') + '/v' +
340-
pjson.version;
342+
packageInfo.version;
341343
let desc = process.title + ' ' + mainScript;
342344

343345
const labels: {[key: string]: string} = {
344346
'main script': mainScript,
345347
'process.title': process.title,
346348
'node version': process.versions.node,
347349
'V8 version': process.versions.v8,
348-
'agent.name': pjson.name,
349-
'agent.version': pjson.version,
350+
'agent.name': packageInfo.name,
351+
'agent.version': packageInfo.version,
350352
'projectid': projectId
351353
};
352354

@@ -392,7 +394,8 @@ export class Debuglet extends EventEmitter {
392394
agentVersion: version,
393395
labels: labels,
394396
statusMessage: statusMessage,
395-
sourceContexts: [sourceContext]
397+
sourceContexts: [sourceContext],
398+
packageInfo: packageInfo
396399
};
397400
return new Debuggee(properties);
398401
}

src/client/stackdriver/debug.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,14 @@
1717
import {AuthenticationConfig, Common} from '../../types/common';
1818
export const common: Common = require('@google-cloud/common');
1919

20+
export interface PackageInfo {
21+
name: string;
22+
version: string;
23+
}
24+
2025
export class Debug extends common.Service {
2126
options: AuthenticationConfig;
27+
packageInfo: PackageInfo;
2228

2329
/**
2430
* <p class="notice">
@@ -43,17 +49,17 @@ export class Debug extends common.Service {
4349
*
4450
* @param options - [Authentication options](#/docs)
4551
*/
46-
constructor(options: AuthenticationConfig) {
52+
constructor(options: AuthenticationConfig, packageJson: any) {
4753
if (new.target !== Debug) {
4854
options = common.util.normalizeArguments(null, options);
49-
return new Debug(options);
55+
return new Debug(options, packageJson);
5056
}
5157

5258
const config = {
5359
projectIdRequired: false,
5460
baseUrl: 'https://clouddebugger.googleapis.com/v2',
5561
scopes: ['https://www.googleapis.com/auth/cloud_debugger'],
56-
packageJson: require('../../../../package.json')
62+
packageJson: packageJson
5763
};
5864

5965
// TODO: Update Service to provide types
@@ -67,5 +73,7 @@ export class Debug extends common.Service {
6773
// https://github.com/GoogleCloudPlatform/google-cloud-node/issues/1891
6874
// is resolved.
6975
this.options = options;
76+
77+
this.packageInfo = {name: packageJson.name, version: packageJson.version};
7078
}
7179
}

src/debuggee.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
* limitations under the License.
1515
*/
1616

17-
const pjson = require('../../package.json');
1817
import * as _ from 'lodash';
1918
import {StatusMessage} from './client/stackdriver/status-message';
2019

@@ -26,7 +25,7 @@ export interface DebuggeeProperties {
2625
project?: string;
2726
uniquifier?: string;
2827
description?: string;
29-
agentVersion?: string;
28+
agentVersion: string;
3029
labels?: {
3130
[key: string]: string,
3231
};
@@ -95,8 +94,7 @@ export class Debuggee {
9594
this.project = properties.project;
9695
this.uniquifier = properties.uniquifier;
9796
this.description = properties.description;
98-
this.agentVersion =
99-
properties.agentVersion || (pjson.name + '/client/v' + pjson.version);
97+
this.agentVersion = properties.agentVersion;
10098
if (properties.labels) {
10199
this.labels = properties.labels;
102100
}

src/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import {DebugAgentConfig, StackdriverConfig} from './agent/config';
1818
import {Debuglet} from './agent/debuglet';
1919
import {Debug} from './client/stackdriver/debug';
2020

21+
const pjson = require('../../package.json');
22+
2123
// Singleton.
2224
let debuglet: Debuglet;
2325

@@ -44,7 +46,7 @@ export function start(options: DebugAgentConfig|StackdriverConfig): Debuglet|
4446
throw new Error('Debug Agent has already been started');
4547
}
4648

47-
const debug = new Debug(options);
49+
const debug = new Debug(options, pjson);
4850
debuglet = new Debuglet(debug, agentConfig);
4951
debuglet.start();
5052

system-test/test-controller.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,12 @@ assert.ok(
2727
import * as stackdriver from '../src/types/stackdriver';
2828
import {Controller} from '../src/agent/controller';
2929
import {Debuggee} from '../src/debuggee';
30-
import {Debug} from '../src/client/stackdriver/debug';
31-
const debug = new Debug({});
30+
import {Debug, PackageInfo} from '../src/client/stackdriver/debug';
3231

32+
const packageInfo: PackageInfo = { name: 'SomeName', version: 'SomeVersion' };
33+
const agentVersion = `${packageInfo.name}/client/${packageInfo.version}`;
34+
35+
const debug = new Debug({}, packageInfo);
3336

3437
describe('Controller', function() {
3538
this.timeout(60 * 1000);
@@ -40,7 +43,8 @@ describe('Controller', function() {
4043
new Debuggee({
4144
project: process.env.GCLOUD_PROJECT,
4245
uniquifier: 'test-uid-' + Date.now(),
43-
description: 'this is a system test'
46+
description: 'this is a system test',
47+
agentVersion: agentVersion
4448
});
4549

4650
controller.register(debuggee, function(err, maybeBody) {
@@ -59,7 +63,8 @@ describe('Controller', function() {
5963
new Debuggee({
6064
project: process.env.GCLOUD_PROJECT,
6165
uniquifier: 'test-uid-' + Date.now(),
62-
description: 'this is a system test'
66+
description: 'this is a system test',
67+
agentVersion: agentVersion
6368
});
6469
// TODO: Determine if the body parameter should be used.
6570
controller.register(debuggee, function(err, _body) {
@@ -83,7 +88,8 @@ describe('Controller', function() {
8388
new Debuggee({
8489
project: process.env.GCLOUD_PROJECT,
8590
uniquifier: 'test-uid-' + Date.now(),
86-
description: 'this is a system test'
91+
description: 'this is a system test',
92+
agentVersion: agentVersion
8793
});
8894
// TODO: Determine if the body parameter should be used.
8995
controller.register(debuggee, function(err, _body) {

system-test/test-e2e.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ describe('@google-cloud/debug end-to-end behavior', function () {
5252

5353
before(function() {
5454
promisifyAll(Debugger);
55-
api = new Debugger(new Debug({}));
55+
const packageInfo = { name: 'Some name', vesion: 'Some version' };
56+
api = new Debugger(new Debug({}, packageInfo));
5657
});
5758

5859
beforeEach(function() {

test/debugger.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import * as stackdriver from '../src/types/stackdriver';
2323
import {Debug} from '../src/client/stackdriver/debug';
2424
import {Debuggee} from '../src/debuggee';
2525

26-
const pjson = require('../../package.json');
2726
export const common: commonTypes.Common = require('@google-cloud/common');
2827
// TODO: Verify these types are correct.
2928
const qs: { parse: (qs: any, sep?: string, eq?: string,
@@ -50,7 +49,7 @@ export class Debugger extends common.ServiceObject {
5049
/** @private {string} */
5150
this.nextWaitToken_ = null;
5251

53-
this.clientVersion_ = pjson.name + '/client-for-testing/v' + pjson.version;
52+
this.clientVersion_ = debug.packageInfo.name + '/client-for-testing/v' + debug.packageInfo.version;
5453
}
5554

5655
/**

test/test-controller.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const fakeDebug: Debug = {
3333
request: request
3434
} as any as Debug;
3535

36+
const agentVersion = `SomeName/client/SomeVersion`;
3637
const url = 'https://clouddebugger.googleapis.com';
3738
const api = '/v2/controller';
3839

@@ -50,7 +51,8 @@ describe('Controller API', function() {
5051
const debuggee = new Debuggee({
5152
project: 'fake-project',
5253
uniquifier: 'fake-id',
53-
description: 'unit test'
54+
description: 'unit test',
55+
agentVersion: agentVersion
5456
});
5557
const controller = new Controller(fakeDebug);
5658
// TODO: Determine if this type signature is correct.
@@ -73,7 +75,8 @@ describe('Controller API', function() {
7375
const debuggee = new Debuggee({
7476
project: 'fake-project',
7577
uniquifier: 'fake-id',
76-
description: 'unit test'
78+
description: 'unit test',
79+
agentVersion: agentVersion
7780
});
7881
const controller = new Controller(fakeDebug);
7982
controller.register(debuggee, function(err: Error, result: {debuggee: Debuggee}) {
@@ -101,7 +104,8 @@ describe('Controller API', function() {
101104
const debuggee = new Debuggee({
102105
project: 'fake-project',
103106
uniquifier: 'fake-id',
104-
description: 'unit test'
107+
description: 'unit test',
108+
agentVersion: agentVersion
105109
});
106110
const controller = new Controller(fakeDebug);
107111
controller.register(debuggee, function(err/*, result*/) {

test/test-debuggee.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,31 @@
1717
import * as assert from 'assert';
1818
import {Debuggee} from '../src/debuggee';
1919

20+
const agentVersion = `SomeName/client/SomeVersion`;
21+
2022
describe('Debuggee', function() {
2123

2224
it('should create a Debuggee instance on valid input', function() {
2325
const debuggee = new Debuggee(
24-
{project: 'project', uniquifier: 'uid', description: 'unit test'});
26+
{project: 'project', uniquifier: 'uid', description: 'unit test', agentVersion: agentVersion});
2527
assert.ok(debuggee instanceof Debuggee);
2628
});
2729

2830
it('should create a Debuggee on a call without new', function() {
2931
const debuggee = new Debuggee(
30-
{project: 'project', uniquifier: 'uid', description: 'unit test'});
32+
{project: 'project', uniquifier: 'uid', description: 'unit test', agentVersion: agentVersion});
3133
assert.ok(debuggee instanceof Debuggee);
3234
});
3335

3436
it('should throw on invalid input', function() {
35-
assert.throws(function() { new Debuggee({}); });
36-
assert.throws(function() { new Debuggee({project: '5'}); });
37-
assert.throws(function() { new Debuggee({project: undefined}); });
38-
assert.throws(function() { new Debuggee({project: 'test'}); });
37+
assert.throws(function() { new Debuggee({agentVersion: agentVersion}); });
38+
assert.throws(function() { new Debuggee({project: '5', agentVersion: agentVersion}); });
39+
assert.throws(function() { new Debuggee({project: undefined, agentVersion: agentVersion}); });
40+
assert.throws(function() { new Debuggee({project: 'test', agentVersion: agentVersion}); });
3941
assert.throws(function() {
40-
new Debuggee({project: 'test', uniquifier: undefined});
42+
new Debuggee({project: 'test', uniquifier: undefined, agentVersion: agentVersion});
4143
assert.throws(function() {
42-
new Debuggee({project: 'test', uniquifier: 'uid'});
44+
new Debuggee({project: 'test', uniquifier: 'uid', agentVersion: agentVersion});
4345
});
4446
});
4547
});

0 commit comments

Comments
 (0)