Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Use a hashed id for default root name #1575

Merged
merged 4 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions tools/src/compileProtos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import * as path from 'path';
import * as util from 'util';
import * as pbjs from 'protobufjs-cli/pbjs';
import * as pbts from 'protobufjs-cli/pbts';
import * as crypto from 'crypto';
import {walkUp} from 'walk-up-path';

export const gaxProtos = path.join(
Expand Down Expand Up @@ -353,13 +354,13 @@ async function compileProtos(
*
* @param directories List of directories to process. Normally, just the
* `./src` folder of the given client library.
* @return {Promise<string>} Resolves to a unique name for protobuf root to use in the JS static module, or 'default'.
* @return {Promise<string>} Resolves to a unique name for protobuf root to use in the JS static module, or a hashed id.
*/
export async function generateRootName(directories: string[]): Promise<string> {
// We need to provide `-r root` option to `pbjs -t static-module`, otherwise
// we'll have big problems if two different libraries are used together.
// It's OK to play some guessing game here: if we locate `package.json`
// with a package name, we'll use it; otherwise, we'll fallback to 'default'.
// with a package name, we'll use it; otherwise, we'll fallback to a hashed id.
for (const directory of directories) {
for (const p of walkUp(path.resolve(directory, '..'))) {
const packageJson = path.join(p, 'package.json');
Expand All @@ -373,7 +374,9 @@ export async function generateRootName(directories: string[]): Promise<string> {
}
}
}
return 'default';
const sha1 = crypto.createHash('sha1');
sha1.update(directories.join(','));
return `default_${sha1.digest('hex').slice(0, 8)}`;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tools/test/compileProtos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ describe('compileProtos tool', () => {
const rootName = await compileProtos.generateRootName([
'/nonexistent/empty',
]);
assert.strictEqual(rootName, 'default');
assert.strictEqual(rootName, 'default_371767bb');
});

it('reformat the JSDOC link in the JS and TS file', async function () {
Expand Down
Loading