Skip to content

Commit 8ba9826

Browse files
annich-MSDanny McCormick
authored andcommitted
change how useragent is formulated to support use in browser (#295)
* change how useragent is formulated to support use in browser * fix errant constant * Fix formatting error * Update User Agent Strings
1 parent ec4698f commit 8ba9826

3 files changed

Lines changed: 24 additions & 11 deletions

File tree

api/WebApi.ts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ import os = require('os');
4242
import url = require('url');
4343
import path = require('path');
4444

45+
const isBrowser: boolean =(function(){return typeof window !== 'undefined' && this===window})();
46+
4547
/**
4648
* Methods to return handler objects (see handlers folder)
4749
*/
@@ -137,15 +139,23 @@ export class WebApi {
137139

138140
let userAgent: string;
139141
const nodeApiName: string = 'azure-devops-node-api';
140-
const nodeApiVersion: string = JSON.parse(fs.readFileSync(path.resolve(__dirname, 'package.json'), 'utf8')).version;
141-
const osName: string = os.platform();
142-
const osVersion: string = os.release();
142+
if(isBrowser) {
143+
if(requestSettings) {
144+
userAgent = `${requestSettings.productName}/${requestSettings.productVersion} (${nodeApiName}; ${window.navigator.userAgent})`
145+
} else {
146+
userAgent = `${nodeApiName} (${window.navigator.userAgent})`;
147+
}
148+
} else {
149+
const nodeApiVersion: string = JSON.parse(fs.readFileSync(path.resolve(__dirname, 'package.json'), 'utf8')).version;
150+
const osName: string = os.platform();
151+
const osVersion: string = os.release();
143152

144-
if (requestSettings) {
145-
userAgent = `${requestSettings.productName}/${requestSettings.productVersion} (${nodeApiName} ${nodeApiVersion}; ${osName} ${osVersion})`;
146-
}
147-
else {
148-
userAgent = `${nodeApiName}/${nodeApiVersion} (${osName} ${osVersion})`;
153+
if (requestSettings) {
154+
userAgent = `${requestSettings.productName}/${requestSettings.productVersion} (${nodeApiName} ${nodeApiVersion}; ${osName} ${osVersion})`;
155+
}
156+
else {
157+
userAgent = `${nodeApiName}/${nodeApiVersion} (${osName} ${osVersion})`;
158+
}
149159
}
150160
this.rest = new rm.RestClient(userAgent, null, [this.authHandler], this.options);
151161
this.vsoClient = new vsom.VsoClient(defaultUrl, this.rest);
@@ -401,6 +411,9 @@ export class WebApi {
401411
}
402412

403413
private _readTaskLibSecrets(lookupKey: string): string {
414+
if(isBrowser) {
415+
throw new Error("Browsers can't securely keep secrets");
416+
}
404417
// the lookupKey should has following format
405418
// base64encoded<keyFilePath>:base64encoded<encryptedContent>
406419
if (lookupKey && lookupKey.indexOf(':') > 0) {

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)