Skip to content

Commit

Permalink
add module tracking string to library version
Browse files Browse the repository at this point in the history
  • Loading branch information
ddelgrosso1 committed Sep 22, 2023
1 parent 8c62f09 commit 3ccc9bc
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/nodejs-common/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
PackageJson,
util,
} from './util.js';
import {getRuntimeTrackingString} from '../util.js';
import {getModuleFormat, getRuntimeTrackingString} from '../util.js';

export const DEFAULT_PROJECT_ID_TOKEN = '{{projectId}}';

Expand Down Expand Up @@ -250,7 +250,7 @@ export class Service {
'User-Agent': userAgent,
'x-goog-api-client': `${getRuntimeTrackingString()} gccl/${
pkg.version
} gccl-invocation-id/${uuid.v4()}`,
}-${getModuleFormat()} gccl-invocation-id/${uuid.v4()}`,
};

if (reqOpts.shouldReturnStream) {
Expand Down
8 changes: 6 additions & 2 deletions src/nodejs-common/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ import {teenyRequest} from 'teeny-request';
import {Interceptor} from './service-object.js';
import * as uuid from 'uuid';
import {DEFAULT_PROJECT_ID_TOKEN} from './service.js';
import {getPackageJSON, getRuntimeTrackingString} from '../util.js';
import {
getModuleFormat,
getPackageJSON,
getRuntimeTrackingString,
} from '../util.js';
import duplexify from 'duplexify';

const packageJson = getPackageJSON();
Expand Down Expand Up @@ -1016,7 +1020,7 @@ export class Util {
'User-Agent': util.getUserAgentFromPackageJson(packageJson),
'x-goog-api-client': `${getRuntimeTrackingString()} gccl/${
packageJson.version
} gccl-invocation-id/${uuid.v4()}`,
}-${getModuleFormat()} gccl-invocation-id/${uuid.v4()}`,
};
}
}
Expand Down
18 changes: 14 additions & 4 deletions src/resumable-upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ import {Readable, Writable, WritableOptions} from 'stream';
import AsyncRetry from 'async-retry';
import {RetryOptions, PreconditionOptions} from './storage.js';
import * as uuid from 'uuid';
import {getRuntimeTrackingString, getPackageJSON} from './util.js';
import {
getRuntimeTrackingString,
getPackageJSON,
getModuleFormat,
} from './util.js';

const NOT_FOUND_STATUS_CODE = 404;
const RESUMABLE_INCOMPLETE_STATUS_CODE = 308;
Expand Down Expand Up @@ -600,7 +604,9 @@ export class Upload extends Writable {
headers: {
'x-goog-api-client': `${getRuntimeTrackingString()} gccl/${
packageJson.version
} gccl-invocation-id/${this.currentInvocationId.uri}`,
}-${getModuleFormat()} gccl-invocation-id/${
this.currentInvocationId.uri
}`,
...headers,
},
};
Expand Down Expand Up @@ -769,7 +775,9 @@ export class Upload extends Writable {
const headers: GaxiosOptions['headers'] = {
'x-goog-api-client': `${getRuntimeTrackingString()} gccl/${
packageJson.version
} gccl-invocation-id/${this.currentInvocationId.chunk}`,
}-${getModuleFormat()} gccl-invocation-id/${
this.currentInvocationId.chunk
}`,
};

// If using multiple chunk upload, set appropriate header
Expand Down Expand Up @@ -912,7 +920,9 @@ export class Upload extends Writable {
'Content-Range': 'bytes */*',
'x-goog-api-client': `${getRuntimeTrackingString()} gccl/${
packageJson.version
} gccl-invocation-id/${this.currentInvocationId.offset}`,
}-${getModuleFormat()} gccl-invocation-id/${
this.currentInvocationId.offset
}`,
},
};
try {
Expand Down
4 changes: 4 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,10 @@ export function getPackageJSON() {
);
}

export function getModuleFormat() {
return isEsm ? 'ESM' : 'CJS';
}

export class PassThroughShim extends PassThrough {
private shouldEmitReading = true;
private shouldEmitWriting = true;
Expand Down
5 changes: 4 additions & 1 deletion test/nodejs-common/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
util,
Util,
} from '../../src/nodejs-common/util.js';
import {getModuleFormat} from '../../src/util.js';

proxyquire.noPreserveCache();

Expand Down Expand Up @@ -517,7 +518,9 @@ describe('Service', () => {
service.makeAuthenticatedRequest = (reqOpts: DecorateRequestOptions) => {
const pkg = service.packageJson;
const r = new RegExp(
`^gl-node/${process.versions.node} gccl/${pkg.version} gccl-invocation-id/(?<gcclInvocationId>[^W]+)$`
`^gl-node/${process.versions.node} gccl/${
pkg.version
}-${getModuleFormat()} gccl-invocation-id/(?<gcclInvocationId>[^W]+)$`
);
assert.ok(r.test(reqOpts.headers!['x-goog-api-client']));
done();
Expand Down

0 comments on commit 3ccc9bc

Please sign in to comment.