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

build!: update to latest gts and TypeScript #927

Merged
merged 2 commits into from Mar 26, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .eslintrc.json
@@ -0,0 +1,3 @@
{
"extends": "./node_modules/gts"
}
15 changes: 0 additions & 15 deletions .eslintrc.yml

This file was deleted.

2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Expand Up @@ -9,7 +9,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node: [8, 10, 12, 13]
node: [10, 12, 13]
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
Expand Down
8 changes: 5 additions & 3 deletions .prettierignore
@@ -1,3 +1,5 @@
node_modules/*
samples/node_modules/*
src/**/doc/*
**/node_modules
**/.coverage
build/
docs/
protos/
8 changes: 0 additions & 8 deletions .prettierrc

This file was deleted.

17 changes: 17 additions & 0 deletions .prettierrc.js
@@ -0,0 +1,17 @@
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

module.exports = {
...require('gts/.prettierrc.json')
}
1 change: 1 addition & 0 deletions browser-test/test.crypto.ts
Expand Up @@ -17,6 +17,7 @@ import {assert} from 'chai';
import {createCrypto} from '../src/crypto/crypto';
import {BrowserCrypto} from '../src/crypto/browser/crypto';
import {privateKey, publicKey} from './fixtures/keys';
import {describe, it} from 'mocha';

// Not all browsers support `TextEncoder`. The following `require` will
// provide a fast UTF8-only replacement for those browsers that don't support
Expand Down
8 changes: 7 additions & 1 deletion browser-test/test.oauth2.ts
Expand Up @@ -16,6 +16,7 @@ import * as base64js from 'base64-js';
import {assert} from 'chai';
import * as sinon from 'sinon';
import {privateKey, publicKey} from './fixtures/keys';
import {it, describe, beforeEach} from 'mocha';

// Not all browsers support `TextEncoder`. The following `require` will
// provide a fast UTF8-only replacement for those browsers that don't support
Expand Down Expand Up @@ -127,7 +128,7 @@ describe('Browser OAuth2 tests', () => {

it('should generate a valid code verifier and resulting challenge', async () => {
const codes = await client.generateCodeVerifierAsync();
assert.match(codes.codeVerifier, /^[a-zA-Z0-9\-\.~_]{128}$/);
assert.match(codes.codeVerifier, /^[a-zA-Z0-9-.~_]{128}$/);
});

it('should include code challenge and method in the url', async () => {
Expand Down Expand Up @@ -166,23 +167,28 @@ describe('Browser OAuth2 tests', () => {
'}';
const envelope = JSON.stringify({kid: 'keyid', alg: 'RS256'});
let data =
// eslint-disable-next-line node/no-unsupported-features/node-builtins
base64js.fromByteArray(new TextEncoder().encode(envelope)) +
'.' +
// eslint-disable-next-line node/no-unsupported-features/node-builtins
base64js.fromByteArray(new TextEncoder().encode(idToken));
const algo = {
name: 'RSASSA-PKCS1-v1_5',
hash: {name: 'SHA-256'},
};
// eslint-disable-next-line no-undef
const cryptoKey = await window.crypto.subtle.importKey(
'jwk',
privateKey,
algo,
true,
['sign']
);
// eslint-disable-next-line no-undef
const signature = await window.crypto.subtle.sign(
algo,
cryptoKey,
// eslint-disable-next-line node/no-unsupported-features/node-builtins
new TextEncoder().encode(data)
);
data += '.' + base64js.fromByteArray(new Uint8Array(signature));
Expand Down
2 changes: 1 addition & 1 deletion karma.conf.js
Expand Up @@ -19,7 +19,7 @@ const isDocker = require('is-docker')();
const webpackConfig = require('./webpack-tests.config.js');
process.env.CHROME_BIN = require('puppeteer').executablePath();

module.exports = function(config) {
module.exports = function (config) {
config.set({
// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '',
Expand Down
13 changes: 3 additions & 10 deletions package.json
Expand Up @@ -43,14 +43,9 @@
"c8": "^7.0.0",
"chai": "^4.2.0",
"codecov": "^3.0.2",
"eslint": "^6.0.0",
"eslint-config-prettier": "^6.0.0",
"eslint-plugin-node": "^11.0.0",
"eslint-plugin-prettier": "^3.0.0",
"execa": "^4.0.0",
"gts": "^1.1.2",
"gts": "^2.0.0-alpha.8",
"is-docker": "^2.0.0",
"js-green-licenses": "^1.0.0",
"karma": "^4.0.0",
"karma-chrome-launcher": "^3.0.0",
"karma-coverage": "^2.0.0",
Expand All @@ -66,12 +61,11 @@
"ncp": "^2.0.0",
"nock": "^12.0.0",
"null-loader": "^3.0.0",
"prettier": "^1.13.4",
"puppeteer": "^2.0.0",
"sinon": "^9.0.0",
"tmp": "^0.1.0",
"ts-loader": "^6.0.0",
"typescript": "3.6.4",
"typescript": "^3.8.3",
"webpack": "^4.20.2",
"webpack-cli": "^3.1.1"
},
Expand All @@ -83,11 +77,10 @@
"test": "c8 mocha build/test",
"clean": "gts clean",
"prepare": "npm run compile",
"lint": "gts check && eslint '**/*.js' && jsgl --local .",
"lint": "gts check",
"compile": "tsc -p .",
"fix": "gts fix && eslint --fix '**/*.js'",
"pretest": "npm run compile",
"license-check": "jsgl --local .",
"docs": "compodoc src/",
"samples-test": "cd samples/ && npm link ../ && npm test && cd ../",
"system-test": "mocha build/system-test --timeout 60000",
Expand Down
4 changes: 4 additions & 0 deletions src/auth/googleauth.ts
Expand Up @@ -45,6 +45,7 @@ export interface CredentialCallback {
(err: Error | null, result?: UserRefreshClient | JWT): void;
}

// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface DeprecatedGetClientOptions {}

export interface ADCCallback {
Expand Down Expand Up @@ -164,7 +165,10 @@ export class GoogleAuth {
// - Cloud SDK: `gcloud config config-helper --format json`
// - GCE project ID from metadata server)
if (!this._getDefaultProjectIdPromise) {
// TODO: refactor the below code so that it doesn't mix and match
// promises and async/await.
this._getDefaultProjectIdPromise = new Promise(
// eslint-disable-next-line no-async-promise-executor
async (resolve, reject) => {
try {
const projectId =
Expand Down
2 changes: 1 addition & 1 deletion src/auth/oauth2client.ts
Expand Up @@ -1216,7 +1216,7 @@ export class OAuth2Client extends AuthClient {
throw new Error("Can't parse token payload: " + segments[1]);
}

if (!certs.hasOwnProperty(envelope.kid)) {
if (!Object.prototype.hasOwnProperty.call(certs, envelope.kid)) {
// If this is not present, then there's no reason to attempt verification
throw new Error('No pem found for envelope: ' + JSON.stringify(envelope));
}
Expand Down
7 changes: 7 additions & 0 deletions src/crypto/browser/crypto.ts
Expand Up @@ -11,6 +11,7 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/* global window */

// This file implements crypto functions we need using in-browser
// SubtleCrypto interface `window.crypto.subtle`.
Expand All @@ -20,6 +21,7 @@ import * as base64js from 'base64-js';
// Not all browsers support `TextEncoder`. The following `require` will
// provide a fast UTF8-only replacement for those browsers that don't support
// text encoding natively.
// eslint-disable-next-line node/no-unsupported-features/node-builtins
if (typeof process === 'undefined' && typeof TextEncoder === 'undefined') {
require('fast-text-encoding');
}
Expand All @@ -45,6 +47,7 @@ export class BrowserCrypto implements Crypto {

// To calculate SHA256 digest using SubtleCrypto, we first
// need to convert an input string to an ArrayBuffer:
// eslint-disable-next-line node/no-unsupported-features/node-builtins
const inputBuffer = new TextEncoder().encode(str);

// Result is ArrayBuffer as well.
Expand Down Expand Up @@ -79,6 +82,7 @@ export class BrowserCrypto implements Crypto {
name: 'RSASSA-PKCS1-v1_5',
hash: {name: 'SHA-256'},
};
// eslint-disable-next-line node/no-unsupported-features/node-builtins
const dataArray = new TextEncoder().encode(data);
const signatureArray = base64js.toByteArray(
BrowserCrypto.padBase64(signature)
Expand Down Expand Up @@ -107,6 +111,7 @@ export class BrowserCrypto implements Crypto {
name: 'RSASSA-PKCS1-v1_5',
hash: {name: 'SHA-256'},
};
// eslint-disable-next-line node/no-unsupported-features/node-builtins
const dataArray = new TextEncoder().encode(data);
const cryptoKey = await window.crypto.subtle.importKey(
'jwk',
Expand All @@ -124,11 +129,13 @@ export class BrowserCrypto implements Crypto {

decodeBase64StringUtf8(base64: string): string {
const uint8array = base64js.toByteArray(BrowserCrypto.padBase64(base64));
// eslint-disable-next-line node/no-unsupported-features/node-builtins
const result = new TextDecoder().decode(uint8array);
return result;
}

encodeBase64StringUtf8(text: string): string {
// eslint-disable-next-line node/no-unsupported-features/node-builtins
const uint8array = new TextEncoder().encode(text);
const result = base64js.fromByteArray(uint8array);
return result;
Expand Down
1 change: 1 addition & 0 deletions src/crypto/crypto.ts
Expand Up @@ -11,6 +11,7 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/* global window */

import {BrowserCrypto} from './browser/crypto';
import {NodeCrypto} from './node/crypto';
Expand Down
5 changes: 1 addition & 4 deletions src/crypto/node/crypto.ts
Expand Up @@ -17,10 +17,7 @@ import {Crypto} from '../crypto';

export class NodeCrypto implements Crypto {
async sha256DigestBase64(str: string): Promise<string> {
return crypto
.createHash('sha256')
.update(str)
.digest('base64');
return crypto.createHash('sha256').update(str).digest('base64');
}

randomBytesBase64(count: number): string {
Expand Down
3 changes: 2 additions & 1 deletion src/transporters.ts
Expand Up @@ -21,8 +21,9 @@ import {
} from 'gaxios';
import {validate} from './options';

// tslint:disable-next-line no-var-requires
// eslint-disable-next-line @typescript-eslint/no-var-requires
const pkg = require('../../package.json');

const PRODUCT_NAME = 'google-api-nodejs-client';

export interface Transporter {
Expand Down
5 changes: 3 additions & 2 deletions system-test/test.kitchen.ts
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

import * as assert from 'assert';
import {describe, it} from 'mocha';
import {describe, it, before, after} from 'mocha';
import * as execa from 'execa';
import * as fs from 'fs';
import * as mv from 'mv';
Expand All @@ -27,14 +27,15 @@ const ncpp = promisify(ncp);
const keep = !!process.env.GALN_KEEP_TEMPDIRS;
const stagingDir = tmp.dirSync({keep, unsafeCleanup: true});
const stagingPath = stagingDir.name;
// eslint-disable-next-line @typescript-eslint/no-var-requires
const pkg = require('../../package.json');

describe('pack and install', () => {
/**
* Create a staging directory with temp fixtures used to test on a fresh
* application.
*/
before('should be able to use the d.ts', async function() {
before('should be able to use the d.ts', async function () {
this.timeout(40000);
console.log(`${__filename} staging area: ${stagingPath}`);
await execa('npm', ['pack'], {stdio: 'inherit'});
Expand Down
13 changes: 5 additions & 8 deletions test/test.compute.ts
Expand Up @@ -14,7 +14,6 @@

import * as assert from 'assert';
import {describe, it, beforeEach, afterEach} from 'mocha';
const assertRejects = require('assert-rejects');
import {BASE_PATH, HEADERS, HOST_ADDRESS} from 'gcp-metadata';
import * as nock from 'nock';
import * as sinon from 'sinon';
Expand All @@ -37,9 +36,7 @@ describe('compute', () => {
}

function mockExample() {
return nock(url)
.get('/')
.reply(200);
return nock(url).get('/').reply(200);
}

// set up compute client.
Expand Down Expand Up @@ -158,7 +155,7 @@ describe('compute', () => {
'Compute Engine instance does not have the correct permission scopes specified. ' +
'Could not refresh access token.'
);
await assertRejects(compute.request({url}), expected);
await assert.rejects(compute.request({url}), expected);
scope.done();
});

Expand All @@ -169,7 +166,7 @@ describe('compute', () => {
'token for the Compute Engine built-in service account. This may be because the ' +
'Compute Engine instance does not have any permission scopes specified.'
);
await assertRejects(compute.request({url}), expected);
await assert.rejects(compute.request({url}), expected);
scope.done();
});

Expand All @@ -188,7 +185,7 @@ describe('compute', () => {
'Compute Engine instance does not have the correct permission scopes specified. ' +
'Could not refresh access token.'
);
await assertRejects(compute.request({}), expected);
await assert.rejects(compute.request({}), expected);
scope.done();
});

Expand All @@ -210,7 +207,7 @@ describe('compute', () => {
'refresh access token.'
);

await assertRejects(compute.request({}), expected);
await assert.rejects(compute.request({}), expected);
scope.done();
});

Expand Down