Skip to content

Commit 83a5ba5

Browse files
fix: feature detection to check for browser (#738)
1 parent 7a6c8e7 commit 83a5ba5

File tree

14 files changed

+18
-51
lines changed

14 files changed

+18
-51
lines changed

package.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424
"gcp-metadata": "^2.0.0",
2525
"gtoken": "^3.0.0",
2626
"jws": "^3.1.5",
27-
"lru-cache": "^5.0.0",
28-
"semver": "^6.0.0"
27+
"lru-cache": "^5.0.0"
2928
},
3029
"devDependencies": {
3130
"@compodoc/compodoc": "^1.1.7",
@@ -39,7 +38,6 @@
3938
"@types/ncp": "^2.0.1",
4039
"@types/nock": "^10.0.3",
4140
"@types/node": "^10.5.1",
42-
"@types/semver": "^6.0.0",
4341
"@types/sinon": "^7.0.0",
4442
"@types/tmp": "^0.1.0",
4543
"assert-rejects": "^1.0.0",
@@ -68,7 +66,7 @@
6866
"ncp": "^2.0.0",
6967
"nock": "^10.0.0",
7068
"null-loader": "^3.0.0",
71-
"nyc": "^14.0.0",
69+
"nyc": "^14.1.1",
7270
"prettier": "^1.13.4",
7371
"puppeteer": "^1.11.0",
7472
"sinon": "^7.0.0",

src/auth/authclient.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import {EventEmitter} from 'events';
1818
import {GaxiosOptions, GaxiosPromise} from 'gaxios';
1919

2020
import {DefaultTransporter} from '../transporters';
21-
2221
import {Credentials} from './credentials';
2322

2423
export declare interface AuthClient {

src/auth/googleauth.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import * as path from 'path';
2323
import * as stream from 'stream';
2424

2525
import {createCrypto} from '../crypto/crypto';
26-
import {isBrowser} from '../isbrowser';
2726
import * as messages from '../messages';
2827
import {DefaultTransporter, Transporter} from '../transporters';
2928

src/auth/jwtaccess.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import * as LRU from 'lru-cache';
1919
import * as stream from 'stream';
2020

2121
import * as messages from '../messages';
22-
2322
import {JWTInput} from './credentials';
2423
import {Headers, RequestMetadataResponse} from './oauth2client';
2524

src/auth/oauth2client.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ import {
2323
import * as querystring from 'querystring';
2424
import * as stream from 'stream';
2525

26-
import {createCrypto, JwkCertificate} from '../crypto/crypto';
27-
import {isBrowser} from '../isbrowser';
26+
import {createCrypto, JwkCertificate, hasBrowserCrypto} from '../crypto/crypto';
2827
import * as messages from '../messages';
2928
import {BodyResponseCallback} from '../transporters';
3029

@@ -1015,7 +1014,7 @@ export class OAuth2Client extends AuthClient {
10151014

10161015
async getFederatedSignonCertsAsync(): Promise<FederatedSignonCertsResponse> {
10171016
const nowTime = new Date().getTime();
1018-
const format: CertificateFormat = isBrowser()
1017+
const format = hasBrowserCrypto()
10191018
? CertificateFormat.JWK
10201019
: CertificateFormat.PEM;
10211020
if (

src/crypto/browser/crypto.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,10 @@
1919

2020
import * as base64js from 'base64-js';
2121

22-
import {isBrowser} from '../../isbrowser';
23-
import {CryptoSigner} from '../crypto';
24-
2522
// Not all browsers support `TextEncoder`. The following `require` will
2623
// provide a fast UTF8-only replacement for those browsers that don't support
2724
// text encoding natively.
28-
if (isBrowser() && typeof TextEncoder === 'undefined') {
25+
if (typeof TextEncoder === 'undefined') {
2926
require('fast-text-encoding');
3027
}
3128

src/crypto/crypto.ts

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

17-
import {isBrowser} from '../isbrowser';
1817
import {BrowserCrypto} from './browser/crypto';
1918
import {NodeCrypto} from './node/crypto';
2019

@@ -56,8 +55,16 @@ export interface Crypto {
5655
}
5756

5857
export function createCrypto(): Crypto {
59-
if (isBrowser()) {
58+
if (hasBrowserCrypto()) {
6059
return new BrowserCrypto();
6160
}
6261
return new NodeCrypto();
6362
}
63+
64+
export function hasBrowserCrypto() {
65+
return (
66+
typeof window !== 'undefined' &&
67+
typeof window.crypto !== 'undefined' &&
68+
typeof window.crypto.subtle !== 'undefined'
69+
);
70+
}

src/crypto/node/crypto.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616

1717
import * as crypto from 'crypto';
18-
import {Crypto, CryptoSigner} from '../crypto';
18+
import {Crypto} from '../crypto';
1919

2020
export class NodeCrypto implements Crypto {
2121
async sha256DigestBase64(str: string): Promise<string> {

src/isbrowser.ts

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

src/messages.ts

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

17-
import * as semver from 'semver';
18-
import {isBrowser} from './isbrowser';
19-
2017
export enum WarningTypes {
2118
WARNING = 'Warning',
2219
DEPRECATION = 'DeprecationWarning',
@@ -28,18 +25,14 @@ export function warn(warning: Warning) {
2825
return;
2926
}
3027
warning.warned = true;
31-
if (isBrowser()) {
32-
console.warn(warning.message);
33-
} else if (semver.satisfies(process.version, '>=8')) {
28+
if (typeof process !== 'undefined' && process.emitWarning) {
3429
// @types/node doesn't recognize the emitWarning syntax which
3530
// accepts a config object, so `as any` it is
3631
// https://nodejs.org/docs/latest-v8.x/api/process.html#process_process_emitwarning_warning_options
3732
// tslint:disable-next-line no-any
3833
process.emitWarning(warning.message, warning as any);
3934
} else {
40-
// This path can be removed once we drop support for Node 6.
41-
// https://nodejs.org/docs/latest-v6.x/api/process.html#process_process_emitwarning_warning_name_ctor
42-
process.emitWarning(warning.message, warning.type);
35+
console.warn(warning.message);
4336
}
4437
}
4538

0 commit comments

Comments
 (0)