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

Commit 215d748

Browse files
author
Matt Loring
committed
Only compute hash if no other uid available
1 parent d667fa6 commit 215d748

File tree

7 files changed

+61
-105
lines changed

7 files changed

+61
-105
lines changed

lib/debuglet.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ var semver = require('semver');
2525
var v8debugapi = require('./v8debugapi.js');
2626
var DebugletApi = require('./debugletapi.js');
2727
var scanner = require('./scanner.js');
28-
var uid = require('./uid.js');
2928
var Logger = require('@google/cloud-diagnostics-common').logger;
3029
var StatusMessage = require('./apiclasses.js').StatusMessage;
3130

@@ -90,15 +89,20 @@ Debuglet.prototype.start = function() {
9089
that.emit('error', new Error('No package.json found.'));
9190
return;
9291
}
93-
scanner.scan(that.config_.workingDirectory, function(err, hash, fileStats) {
92+
var id;
93+
if (process.env.GAE_MINOR_VERSION) {
94+
id = 'GAE-' + process.env.GAE_MINOR_VERSION;
95+
}
96+
scanner.scan(!!id, that.config_.workingDirectory,
97+
function(err, fileStats, hash) {
9498
if (err) {
9599
that.logger_.error('Error scanning the filesystem.', err);
96100
that.emit('error', err);
97101
return;
98102
}
99103
that.v8debug_ = v8debugapi.create(that.logger_, that.config_, fileStats);
100104

101-
var id = uid.get(hash);
105+
id = id || hash;
102106

103107
that.logger_.info('Unique ID for this Application: ' + id);
104108

lib/scanner.js

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ module.exports = {
2525
scan: scan
2626
};
2727

28-
function scan(baseDir, callback) {
28+
function scan(shouldHash, baseDir, callback) {
2929
findJSFiles(baseDir, function(err, fileList) {
3030
if (err) {
3131
callback(err);
3232
return;
3333
}
34-
computeStats(fileList, callback);
34+
computeStats(fileList, shouldHash, callback);
3535
});
3636
}
3737

@@ -40,36 +40,43 @@ function scan(baseDir, callback) {
4040
* based on the contents.
4141
*
4242
* @param {!Array<string>} fileList array of filenames
43-
* @param {!function(?Error, string, Object)} callback error-back style callback
43+
* @param {Boolean} shouldHash whether a hash should be computed
44+
* @param {!function(?Error, ?string, Object)} callback error-back style callback
4445
* returning the hash-code and an object containing file statistics.
4546
*/
46-
function computeStats(fileList, callback) {
47+
function computeStats(fileList, shouldHash, callback) {
4748
var pending = fileList.length;
4849
// return a valid, if fake, result when there are no js files to hash.
4950
if (pending === 0) {
50-
callback(null, 'EMPTY-no-js-files', {});
51+
callback(null, {}, 'EMPTY-no-js-files');
5152
return;
5253
}
5354

5455
var hashes = [];
5556
var statistics = {};
5657
fileList.forEach(function(filename) {
57-
stats(filename, function(err, fileStats) {
58+
stats(filename, shouldHash, function(err, fileStats) {
5859
if (err) {
5960
callback(err);
6061
return;
6162
}
6263

6364
pending--;
64-
hashes.push(fileStats.hash);
65+
if (shouldHash) {
66+
hashes.push(fileStats.hash);
67+
}
6568
statistics[filename] = fileStats;
6669

6770
if (pending === 0) {
68-
// Sort the hashes to get a deterministic order as the files may not
69-
// be in the same order each time we scan the disk.
70-
var buffer = hashes.sort().join();
71-
var sha1 = crypto.createHash('sha1').update(buffer).digest('hex');
72-
callback(null, 'SHA1-' + sha1, statistics);
71+
var hash;
72+
if (shouldHash) {
73+
// Sort the hashes to get a deterministic order as the files may not
74+
// be in the same order each time we scan the disk.
75+
var buffer = hashes.sort().join();
76+
var sha1 = crypto.createHash('sha1').update(buffer).digest('hex');
77+
hash = 'SHA1-' + sha1;
78+
}
79+
callback(null, statistics, hash);
7380
}
7481
});
7582
});
@@ -126,23 +133,32 @@ function findJSFiles(baseDir, callback) {
126133
/**
127134
* Compute a sha hash for the given file and record line counts along the way.
128135
* @param {string} filename
136+
* @param {Boolean} shouldHash whether a hash should be computed
129137
* @param {function} cb errorback style callback which returns the sha string
130138
* @private
131139
*/
132-
function stats(filename, cb) {
133-
var shasum = crypto.createHash('sha1');
140+
function stats(filename, shouldHash, cb) {
141+
var shasum;
142+
if (shouldHash) {
143+
shasum = crypto.createHash('sha1');
144+
}
134145
var s = fs.ReadStream(filename);
135146
var lines = 0;
136147
var byLine = s.pipe(split());
137148
byLine.on('error', function(e) {
138149
cb(e);
139150
});
140151
byLine.on('data', function(d) {
141-
shasum.update(d);
152+
if (shouldHash) {
153+
shasum.update(d);
154+
}
142155
lines++;
143156
});
144157
byLine.on('end', function() {
145-
var d = shasum.digest('hex');
158+
var d;
159+
if (shouldHash) {
160+
d = shasum.digest('hex');
161+
}
146162
cb(null, { hash: d, lines: lines});
147163
});
148164
}

lib/uid.js

Lines changed: 0 additions & 37 deletions
This file was deleted.

test/standalone/test-duplicate-expressions.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ describe('v8debugapi', function() {
4747

4848
beforeEach(function(done) {
4949
if (!api) {
50-
scanner.scan(config.workingDirectory, function(err, hash, fileStats) {
50+
scanner.scan(true, config.workingDirectory, function(err, fileStats, hash) {
5151
assert(!err);
5252
api = v8debugapi.create(logger, config, fileStats);
5353
assert.ok(api, 'should be able to create the api');

test/test-scanner.js

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ describe('scanner', function() {
3030

3131
describe('scan', function() {
3232
it('should complain when called without a path', function(done) {
33-
scanner.scan(null, function(err) {
33+
scanner.scan(true, null, function(err) {
3434
assert.ok(err);
3535
done();
3636
});
3737
});
3838

3939
it('should error when called on a bad path', function(done) {
40-
scanner.scan('./this directory does not exist',
40+
scanner.scan(true, './this directory does not exist',
4141
function(err) {
4242
assert(err);
4343
done();
@@ -46,10 +46,10 @@ describe('scanner', function() {
4646

4747
it('should return the same hash if the files don\'t change',
4848
function(done) {
49-
scanner.scan(process.cwd(), function(err1, hash1, filesStats1) {
49+
scanner.scan(true, process.cwd(), function(err1, filesStats1, hash1) {
5050
var files1 = Object.keys(filesStats1);
5151
assert.ifError(err1);
52-
scanner.scan(process.cwd(), function(err2, hash2, filesStats2) {
52+
scanner.scan(true, process.cwd(), function(err2, filesStats2, hash2) {
5353
var files2 = Object.keys(filesStats2);
5454
assert.ifError(err2);
5555
assert.deepEqual(files1.sort(), files2.sort());
@@ -59,8 +59,17 @@ describe('scanner', function() {
5959
});
6060
});
6161

62+
it('should return undefined hash if shouldHash is false',
63+
function(done) {
64+
scanner.scan(false, process.cwd(), function(err, filesStats, hash) {
65+
assert.ifError(err);
66+
assert(!hash);
67+
done();
68+
});
69+
});
70+
6271
it('should work with relative paths', function(done) {
63-
scanner.scan(fixtureDir, function(err, hash, fileStats) {
72+
scanner.scan(true, fixtureDir, function(err, fileStats, hash) {
6473
var files = Object.keys(fileStats);
6574
assert.ifError(err);
6675
assert.ok(hash);
@@ -71,7 +80,7 @@ describe('scanner', function() {
7180

7281
it('should return a valid hash even when there are no javascript files',
7382
function(done) {
74-
scanner.scan(fixture('nojs'), function(err, hash, fileStats) {
83+
scanner.scan(true, fixture('nojs'), function(err, fileStats, hash) {
7584
var files = Object.keys(fileStats);
7685
assert.ifError(err);
7786
assert.ok(hash);
@@ -83,12 +92,12 @@ describe('scanner', function() {
8392
it('should return a different hash if the files contents change',
8493
function(done) {
8594
fs.writeFileSync(fixture('tmp.js'), '1 + 1');
86-
scanner.scan(fixtureDir, function(err1, hash1, filesStats1) {
95+
scanner.scan(true, fixtureDir, function(err1, filesStats1, hash1) {
8796
var files1 = Object.keys(filesStats1);
8897
assert.ifError(err1);
8998
assert.ok(hash1);
9099
fs.writeFileSync(fixture('tmp.js'), '1 + 2');
91-
scanner.scan(fixtureDir, function(err2, hash2, filesStats2) {
100+
scanner.scan(true, fixtureDir, function(err2, filesStats2, hash2) {
92101
var files2 = Object.keys(filesStats2);
93102
assert.ifError(err2);
94103
assert.ok(hash2);
@@ -102,19 +111,19 @@ describe('scanner', function() {
102111

103112
it('should return an updated file list when file list changes',
104113
function(done) {
105-
scanner.scan(fixtureDir, function(err1, hash1, fileStats1) {
114+
scanner.scan(true, fixtureDir, function(err1, fileStats1, hash1) {
106115
var files1 = Object.keys(fileStats1);
107116
assert.ifError(err1);
108117
assert.ok(hash1);
109118
fs.writeFileSync(fixture('tmp.js'), ''); // empty.
110-
scanner.scan(fixtureDir, function(err2, hash2, fileStats2) {
119+
scanner.scan(true, fixtureDir, function(err2, fileStats2, hash2) {
111120
var files2 = Object.keys(fileStats2);
112121
assert.ifError(err2);
113122
assert.ok(hash2);
114123
assert.notStrictEqual(hash1, hash2);
115124
assert.ok(files1.length === files2.length - 1);
116125
fs.unlinkSync(fixture('tmp.js'));
117-
scanner.scan(fixtureDir, function(err3, hash3, fileStats3) {
126+
scanner.scan(true, fixtureDir, function(err3, fileStats3, hash3) {
118127
var files3 = Object.keys(fileStats3);
119128
assert.ifError(err3);
120129
assert.ok(hash3);

test/test-uid.js

Lines changed: 0 additions & 36 deletions
This file was deleted.

test/test-v8debugapi.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ describe('v8debugapi', function() {
4646

4747
beforeEach(function(done) {
4848
if (!api) {
49-
scanner.scan(config.workingDirectory, function(err, hash, fileStats) {
49+
scanner.scan(true, config.workingDirectory, function(err, fileStats, hash) {
5050
assert(!err);
5151
api = v8debugapi.create(logger, config, fileStats);
5252
assert.ok(api, 'should be able to create the api');

0 commit comments

Comments
 (0)