From 5ab0704f9e5435d934809a23c80d3a00fc479394 Mon Sep 17 00:00:00 2001 From: Alexander Fenster Date: Fri, 4 Jan 2019 17:47:27 -0800 Subject: [PATCH 01/13] feat: make it webpackable --- .gitignore | 1 + package.json | 8 ++++-- src/apirequest.ts | 5 +++- src/isbrowser.ts | 19 +++++++++++++ tsconfig.json | 1 + webpack.config.js | 70 +++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 101 insertions(+), 3 deletions(-) create mode 100644 src/isbrowser.ts create mode 100644 webpack.config.js diff --git a/.gitignore b/.gitignore index 330759d..1e66c0e 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ build package-lock.json .vscode yarn.lock +dist diff --git a/package.json b/package.json index 0706a38..faf2f92 100644 --- a/package.json +++ b/package.json @@ -41,13 +41,17 @@ "jsdoc": "^3.5.5", "mocha": "^5.2.0", "nock": "^10.0.0", + "null-loader": "^0.1.1", "nyc": "^13.1.0", "source-map-support": "^0.5.8", - "typescript": "~3.2.0" + "ts-loader": "^5.3.2", + "typescript": "~3.2.0", + "webpack": "^4.28.3", + "webpack-cli": "^3.2.0" }, "dependencies": { "axios": "^0.18.0", - "google-auth-library": "^2.0.0", + "google-auth-library": "googleapis/google-auth-library-nodejs#webbbpackkk", "pify": "^4.0.0", "qs": "^6.5.2", "url-template": "^2.0.8", diff --git a/src/apirequest.ts b/src/apirequest.ts index 20bf9a2..f0568fe 100644 --- a/src/apirequest.ts +++ b/src/apirequest.ts @@ -14,6 +14,7 @@ import {AxiosPromise} from 'axios'; import {DefaultTransporter, OAuth2Client} from 'google-auth-library'; import {BodyResponseCallback} from 'google-auth-library/build/src/transporters'; +import {isBrowser} from './isbrowser'; import * as qs from 'qs'; import * as stream from 'stream'; import * as urlTemplate from 'url-template'; @@ -228,7 +229,9 @@ async function createAPIRequestAsync(parameters: APIRequestParams) { // https://github.com/google/google-api-nodejs-client/issues/991 options.maxContentLength = options.maxContentLength || maxContentLength; options.headers['Accept-Encoding'] = 'gzip'; - options.headers['User-Agent'] = USER_AGENT; + if (!isBrowser()) { + options.headers['User-Agent'] = USER_AGENT; + } // By default Axios treats any 2xx as valid, and all non 2xx status // codes as errors. This is a problem for HTTP 304s when used along diff --git a/src/isbrowser.ts b/src/isbrowser.ts new file mode 100644 index 0000000..5ded266 --- /dev/null +++ b/src/isbrowser.ts @@ -0,0 +1,19 @@ +/** + * Copyright 2019 Google LLC. All Rights Reserved. + * + * 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. + */ + +export function isBrowser(): boolean { + return typeof (window) !== 'undefined'; +} diff --git a/tsconfig.json b/tsconfig.json index 0827e9b..6aadb93 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,6 +1,7 @@ { "extends": "./node_modules/gts/tsconfig-google.json", "compilerOptions": { + "lib": ["es2015", "dom"], "rootDir": ".", "outDir": "build" }, diff --git a/webpack.config.js b/webpack.config.js new file mode 100644 index 0000000..dfa519d --- /dev/null +++ b/webpack.config.js @@ -0,0 +1,70 @@ +/** + * Copyright 2019 Google LLC. All Rights Reserved. + * + * 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. + */ + +// Use `npm run webpack` to produce Webpack bundle for this library. + +const path = require('path'); + +module.exports = { + entry: './src/index.ts', + resolve: { + extensions: ['.ts', '.js', '.json'], + alias: { + '../../package.json': path.resolve(__dirname, 'package.json'), + }, + }, + output: { + library: 'GoogleapisCommon', + filename: 'googleapis-common.min.js', + path: path.resolve(__dirname, 'dist'), + }, + node: { + child_process: 'empty', + fs: 'empty', + crypto: 'empty', + }, + module: { + rules: [ + { + test: /src\/crypto\/node\/crypto/, + use: 'null-loader', + }, + { + test: /node_modules\/https-proxy-agent\//, + use: 'null-loader', + }, + { + test: /node_modules\/gtoken\//, + use: 'null-loader', + }, + { + test: /node_modules\/pkginfo\//, + use: 'null-loader', + }, + { + test: /node_modules\/semver\//, + use: 'null-loader', + }, + { + test: /\.ts$/, + use: 'ts-loader', + exclude: /node_modules/, + }, + ], + }, + mode: 'production', + plugins: [], +}; From 050715bb0501fac4606bae1f7095148e55e11abe Mon Sep 17 00:00:00 2001 From: Alexander Fenster Date: Mon, 7 Jan 2019 12:58:14 -0800 Subject: [PATCH 02/13] gts fix --- src/apirequest.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/apirequest.ts b/src/apirequest.ts index f0568fe..7a31473 100644 --- a/src/apirequest.ts +++ b/src/apirequest.ts @@ -14,13 +14,13 @@ import {AxiosPromise} from 'axios'; import {DefaultTransporter, OAuth2Client} from 'google-auth-library'; import {BodyResponseCallback} from 'google-auth-library/build/src/transporters'; -import {isBrowser} from './isbrowser'; import * as qs from 'qs'; import * as stream from 'stream'; import * as urlTemplate from 'url-template'; import * as uuid from 'uuid'; import {APIRequestParams, GlobalOptions} from './api'; +import {isBrowser} from './isbrowser'; import {SchemaParameters} from './schema'; const maxContentLength = Math.pow(2, 31); From 5b03d3a7ff1ece21f97ec1b67146e235314fd7b8 Mon Sep 17 00:00:00 2001 From: Alexander Fenster Date: Tue, 8 Jan 2019 00:25:05 -0800 Subject: [PATCH 03/13] use google-auth-library-nodejs master --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index faf2f92..492de62 100644 --- a/package.json +++ b/package.json @@ -51,7 +51,7 @@ }, "dependencies": { "axios": "^0.18.0", - "google-auth-library": "googleapis/google-auth-library-nodejs#webbbpackkk", + "google-auth-library": "git://github.com/googleapis/google-auth-library-nodejs.git", "pify": "^4.0.0", "qs": "^6.5.2", "url-template": "^2.0.8", From f2906844d7ec464a5f08966d158cbdd634c88dcc Mon Sep 17 00:00:00 2001 From: Alexander Fenster Date: Tue, 8 Jan 2019 13:06:10 -0800 Subject: [PATCH 04/13] use pre-released version of google-auth-library --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 492de62..288b412 100644 --- a/package.json +++ b/package.json @@ -51,7 +51,7 @@ }, "dependencies": { "axios": "^0.18.0", - "google-auth-library": "git://github.com/googleapis/google-auth-library-nodejs.git", + "google-auth-library": "3.0.0-pre1", "pify": "^4.0.0", "qs": "^6.5.2", "url-template": "^2.0.8", From 90cee3281ad542d9929dba016dc583ba4b6a4208 Mon Sep 17 00:00:00 2001 From: Alexander Fenster Date: Tue, 8 Jan 2019 13:07:15 -0800 Subject: [PATCH 05/13] special pre-release version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 288b412..8b34a9c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "googleapis-common", - "version": "0.4.0", + "version": "0.5.0-webpack", "description": "A common tooling library used by the googleapis npm module. You probably don't want to use this directly.", "repository": "googleapis/nodejs-googleapis-common", "main": "build/src/index.js", From ef8b8af5e180c71a04ac2b8859e4b59e3ba46507 Mon Sep 17 00:00:00 2001 From: Alexander Fenster Date: Tue, 8 Jan 2019 23:34:38 -0800 Subject: [PATCH 06/13] unsafe header --- package.json | 2 +- src/apirequest.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 8b34a9c..6da62a7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "googleapis-common", - "version": "0.5.0-webpack", + "version": "0.5.0-webpack1", "description": "A common tooling library used by the googleapis npm module. You probably don't want to use this directly.", "repository": "googleapis/nodejs-googleapis-common", "main": "build/src/index.js", diff --git a/src/apirequest.ts b/src/apirequest.ts index 7a31473..b0a11f6 100644 --- a/src/apirequest.ts +++ b/src/apirequest.ts @@ -228,8 +228,8 @@ async function createAPIRequestAsync(parameters: APIRequestParams) { // to 10MB. Setting to 2GB by default. // https://github.com/google/google-api-nodejs-client/issues/991 options.maxContentLength = options.maxContentLength || maxContentLength; - options.headers['Accept-Encoding'] = 'gzip'; if (!isBrowser()) { + options.headers['Accept-Encoding'] = 'gzip'; options.headers['User-Agent'] = USER_AGENT; } From 952fa22f19e02cddbf29064fb7b9ecbcbe4fa76f Mon Sep 17 00:00:00 2001 From: Alexander Fenster Date: Wed, 9 Jan 2019 22:50:24 -0800 Subject: [PATCH 07/13] release 0.5.0-webpack2 --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 6da62a7..423b4df 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "googleapis-common", - "version": "0.5.0-webpack1", + "version": "0.5.0-webpack2", "description": "A common tooling library used by the googleapis npm module. You probably don't want to use this directly.", "repository": "googleapis/nodejs-googleapis-common", "main": "build/src/index.js", @@ -51,7 +51,7 @@ }, "dependencies": { "axios": "^0.18.0", - "google-auth-library": "3.0.0-pre1", + "google-auth-library": "3.0.0-pre2", "pify": "^4.0.0", "qs": "^6.5.2", "url-template": "^2.0.8", From 1e602b3df4198bf94e796df22036fde2e3601288 Mon Sep 17 00:00:00 2001 From: Alexander Fenster Date: Wed, 9 Jan 2019 23:00:03 -0800 Subject: [PATCH 08/13] export OAuth2Client --- package.json | 2 +- src/index.ts | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 423b4df..e40fe1c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "googleapis-common", - "version": "0.5.0-webpack2", + "version": "0.5.0-webpack3", "description": "A common tooling library used by the googleapis npm module. You probably don't want to use this directly.", "repository": "googleapis/nodejs-googleapis-common", "main": "build/src/index.js", diff --git a/src/index.ts b/src/index.ts index 421a51d..b2dc70f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -11,6 +11,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +export {OAuth2Client} from 'google-auth-library'; export {APIEndpoint, APIRequestContext, APIRequestParams, BodyResponseCallback, GlobalOptions, GoogleConfigurable, MethodOptions, ServiceOptions} from './api'; export {getAPI} from './apiIndex'; export {createAPIRequest} from './apirequest'; From 0d8e2e1b1e207cf889219339e4e6056354ee0b99 Mon Sep 17 00:00:00 2001 From: Alexander Fenster Date: Wed, 16 Jan 2019 10:52:07 -0800 Subject: [PATCH 09/13] updating versions in package.json --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 40dce0e..343b5b2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "googleapis-common", - "version": "0.6.0-webpack", + "version": "0.5.0", "description": "A common tooling library used by the googleapis npm module. You probably don't want to use this directly.", "repository": "googleapis/nodejs-googleapis-common", "main": "build/src/index.js", @@ -51,7 +51,7 @@ }, "dependencies": { "axios": "^0.18.0", - "google-auth-library": "3.0.0-pre2", + "google-auth-library": "^3.0.0", "pify": "^4.0.0", "qs": "^6.5.2", "url-template": "^2.0.8", From 7c4f6eb476d6230dc0070211f34029b99e514159 Mon Sep 17 00:00:00 2001 From: Alexander Fenster Date: Wed, 16 Jan 2019 10:53:02 -0800 Subject: [PATCH 10/13] adding webpack script --- package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 343b5b2..f58244c 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,8 @@ "posttest": "npm run check", "lint": "npm run check", "samples-test": "mocha build/samples-test", - "docs": "compodoc src/" + "docs": "compodoc src/", + "webpack": "webpack" }, "keywords": [], "author": "Google LLC", From 608938472f0caa2eb7a46fd5e543561f5f0d9828 Mon Sep 17 00:00:00 2001 From: Justin Beckwith Date: Wed, 16 Jan 2019 22:54:20 -0800 Subject: [PATCH 11/13] upgrade gaxios and google auth lib --- package.json | 2 +- src/api.ts | 47 +++++++---------------------------------------- src/apirequest.ts | 19 ++++++------------- src/discovery.ts | 5 ++++- 4 files changed, 18 insertions(+), 55 deletions(-) diff --git a/package.json b/package.json index f58244c..a0cea6d 100644 --- a/package.json +++ b/package.json @@ -51,7 +51,7 @@ "webpack-cli": "^3.2.0" }, "dependencies": { - "axios": "^0.18.0", + "gaxios": "^1.2.2", "google-auth-library": "^3.0.0", "pify": "^4.0.0", "qs": "^6.5.2", diff --git a/src/api.ts b/src/api.ts index d629204..49904a2 100644 --- a/src/api.ts +++ b/src/api.ts @@ -11,13 +11,14 @@ // See the License for the specific language governing permissions and // limitations under the License. -import {AxiosAdapter, AxiosProxyConfig, AxiosRequestConfig, AxiosResponse, AxiosTransformer, CancelToken} from 'axios'; +import {GaxiosOptions, GaxiosResponse} from 'gaxios'; import {OAuth2Client} from 'google-auth-library'; + import {Endpoint} from './endpoint'; // tslint:disable-next-line no-any export interface APIRequestParams { - options: AxiosRequestConfig; + options: GaxiosOptions; params: T; requiredParams: string[]; pathParams: string[]; @@ -36,47 +37,13 @@ export interface APIRequestContext { /** * This interface is a mix of the AxiosRequestConfig options - * and our `auth: OAuth2Client|string` options. We need to redefine - * the interface here because the `auth` property already exists - * on AxiosRequestConfig, and uses an entirely different type. + * and our `auth: OAuth2Client|string` options. */ -export interface GlobalOptions { - url?: string; - method?: string; - baseURL?: string; - transformRequest?: AxiosTransformer|AxiosTransformer[]; - transformResponse?: AxiosTransformer|AxiosTransformer[]; - // tslint:disable-next-line no-any - headers?: any; - // tslint:disable-next-line no-any - params?: any; - // tslint:disable-next-line no-any - paramsSerializer?: (params: any) => string; - // tslint:disable-next-line no-any - data?: any; - timeout?: number; - withCredentials?: boolean; - adapter?: AxiosAdapter; +export interface GlobalOptions extends GaxiosOptions { auth?: OAuth2Client|string; - responseType?: string; - xsrfCookieName?: string; - xsrfHeaderName?: string; - // tslint:disable-next-line no-any - onUploadProgress?: (progressEvent: any) => void; - // tslint:disable-next-line no-any - onDownloadProgress?: (progressEvent: any) => void; - maxContentLength?: number; - validateStatus?: (status: number) => boolean; - maxRedirects?: number; - // tslint:disable-next-line no-any - httpAgent?: any; - // tslint:disable-next-line no-any - httpsAgent?: any; - proxy?: AxiosProxyConfig|false; - cancelToken?: CancelToken; } -export interface MethodOptions extends AxiosRequestConfig { +export interface MethodOptions extends GaxiosOptions { rootUrl?: string; } export interface ServiceOptions extends GlobalOptions { @@ -84,7 +51,7 @@ export interface ServiceOptions extends GlobalOptions { } export type BodyResponseCallback = - (err: Error|null, res?: AxiosResponse|null) => void; + (err: Error|null, res?: GaxiosResponse|null) => void; // tslint:disable-next-line: no-any export type APIEndpoint = Readonly; diff --git a/src/apirequest.ts b/src/apirequest.ts index b0a11f6..09713cd 100644 --- a/src/apirequest.ts +++ b/src/apirequest.ts @@ -11,20 +11,17 @@ // See the License for the specific language governing permissions and // limitations under the License. -import {AxiosPromise} from 'axios'; +import {GaxiosPromise} from 'gaxios'; import {DefaultTransporter, OAuth2Client} from 'google-auth-library'; -import {BodyResponseCallback} from 'google-auth-library/build/src/transporters'; import * as qs from 'qs'; import * as stream from 'stream'; import * as urlTemplate from 'url-template'; import * as uuid from 'uuid'; -import {APIRequestParams, GlobalOptions} from './api'; +import {APIRequestParams, BodyResponseCallback} from './api'; import {isBrowser} from './isbrowser'; import {SchemaParameters} from './schema'; -const maxContentLength = Math.pow(2, 31); - // tslint:disable-next-line no-var-requires const pkg = require('../../package.json'); const USER_AGENT = `google-api-nodejs-client/${pkg.version} (gzip)`; @@ -53,12 +50,12 @@ function getMissingParams(params: SchemaParameters, required: string[]) { * @param callback Callback when request finished or error found */ export function createAPIRequest(parameters: APIRequestParams): - AxiosPromise; + GaxiosPromise; export function createAPIRequest( parameters: APIRequestParams, callback: BodyResponseCallback): void; export function createAPIRequest( parameters: APIRequestParams, callback?: BodyResponseCallback): void| - AxiosPromise { + GaxiosPromise { if (callback) { createAPIRequestAsync(parameters).then(r => callback(null, r), callback); } else { @@ -224,13 +221,9 @@ async function createAPIRequestAsync(parameters: APIRequestParams) { options.headers = headers; options.params = params; - // We need to set a default content size, or the max defaults - // to 10MB. Setting to 2GB by default. - // https://github.com/google/google-api-nodejs-client/issues/991 - options.maxContentLength = options.maxContentLength || maxContentLength; if (!isBrowser()) { - options.headers['Accept-Encoding'] = 'gzip'; - options.headers['User-Agent'] = USER_AGENT; + options.headers!['Accept-Encoding'] = 'gzip'; + options.headers!['User-Agent'] = USER_AGENT; } // By default Axios treats any 2xx as valid, and all non 2xx status diff --git a/src/discovery.ts b/src/discovery.ts index e795944..06992c7 100644 --- a/src/discovery.ts +++ b/src/discovery.ts @@ -12,10 +12,12 @@ // limitations under the License. import * as fs from 'fs'; +import {Headers} from 'gaxios'; import {DefaultTransporter} from 'google-auth-library'; import * as pify from 'pify'; import * as url from 'url'; import * as util from 'util'; + import {GlobalOptions, ServiceOptions} from './api'; import {createAPIRequest} from './apirequest'; import {Endpoint} from './endpoint'; @@ -71,7 +73,8 @@ export class Discovery { * @param discoveryUrl */ async discoverAllAPIs(discoveryUrl: string): Promise<{}> { - const headers = this.options.includePrivate ? {} : {'X-User-Ip': '0.0.0.0'}; + const headers: Headers = + this.options.includePrivate ? {} : {'X-User-Ip': '0.0.0.0'}; const res = await this.transporter.request({url: discoveryUrl, headers}); const items = res.data.items; From e26f81cf75e5fb9b66275ac1c2100d2cbe055d20 Mon Sep 17 00:00:00 2001 From: Alexander Fenster Date: Wed, 16 Jan 2019 23:39:37 -0800 Subject: [PATCH 12/13] add some tests --- .gitignore | 1 + browser-test/test.isbrowser.ts | 24 +++++ karma.conf.js | 95 +++++++++++++++++++ package.json | 19 +++- system-test/fixtures/kitchen/package.json | 28 ++++++ system-test/fixtures/kitchen/src/index.ts | 9 ++ system-test/fixtures/kitchen/tsconfig.json | 12 +++ .../fixtures/kitchen/webpack.config.js | 67 +++++++++++++ system-test/googleapis.ts | 14 --- system-test/test.kitchen.ts | 67 +++++++++++++ test/test.isbrowser.ts | 24 +++++ tsconfig.json | 4 +- webpack-tests.config.js | 62 ++++++++++++ 13 files changed, 410 insertions(+), 16 deletions(-) create mode 100644 browser-test/test.isbrowser.ts create mode 100644 karma.conf.js create mode 100644 system-test/fixtures/kitchen/package.json create mode 100644 system-test/fixtures/kitchen/src/index.ts create mode 100644 system-test/fixtures/kitchen/tsconfig.json create mode 100644 system-test/fixtures/kitchen/webpack.config.js delete mode 100644 system-test/googleapis.ts create mode 100644 system-test/test.kitchen.ts create mode 100644 test/test.isbrowser.ts create mode 100644 webpack-tests.config.js diff --git a/.gitignore b/.gitignore index 68d9d92..0d26e42 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ yarn.lock .nyc_output dist docs +coverage diff --git a/browser-test/test.isbrowser.ts b/browser-test/test.isbrowser.ts new file mode 100644 index 0000000..28d64fe --- /dev/null +++ b/browser-test/test.isbrowser.ts @@ -0,0 +1,24 @@ +/** + * Copyright 2019 Google LLC. All Rights Reserved. + * + * 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. + */ + +import * as assert from 'assert'; +import {isBrowser} from '../src/isbrowser'; + +describe('isbrowser', () => { + it('should detect browser', () => { + assert(isBrowser()); + }); +}); diff --git a/karma.conf.js b/karma.conf.js new file mode 100644 index 0000000..3cc5904 --- /dev/null +++ b/karma.conf.js @@ -0,0 +1,95 @@ +/** + * Copyright 2019 Google LLC. All Rights Reserved. + * + * 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. + */ + +// Karma configuration +// Use `npm run browser-test` to run browser tests with Karma. +const isDocker = require('is-docker')(); + +const webpackConfig = require('./webpack-tests.config.js'); +process.env.CHROME_BIN = require('puppeteer').executablePath(); + +module.exports = function(config) { + config.set({ + // base path that will be used to resolve all patterns (eg. files, exclude) + basePath: '', + + // frameworks to use + // available frameworks: https://npmjs.org/browse/keyword/karma-adapter + frameworks: ['mocha'], + + // list of files / patterns to load in the browser + files: ['./browser-test/*.ts'], + + // list of files / patterns to exclude + exclude: [], + + // preprocess matching files before serving them to the browser + // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor + preprocessors: { + './src/*.ts': ['coverage'], + './src/**/*.ts': ['coverage'], + './browser-test/*.ts': ['webpack', 'sourcemap'], + }, + + webpack: webpackConfig, + + // test results reporter to use + // possible values: 'dots', 'progress' + // available reporters: https://npmjs.org/browse/keyword/karma-reporter + reporters: ['progress', 'coverage', 'remap-coverage'], + + coverageReporter: {type: 'in-memory'}, + remapCoverageReporter: {html: './coverage'}, + + // web server port + port: 9876, + + // enable / disable colors in the output (reporters and logs) + colors: true, + + // level of logging + // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG + logLevel: config.LOG_INFO, + + // enable / disable watching file and executing tests whenever any file changes + autoWatch: false, + + // start these browsers + // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher + browsers: ['ChromeCustom'], + customLaunchers: { + ChromeCustom: { + base: 'ChromeHeadless', + // We must disable the Chrome sandbox when running Chrome inside Docker (Chrome's sandbox needs + // more permissions than Docker allows by default) + flags: isDocker ? ['--no-sandbox'] : [], + }, + }, + + // Continuous Integration mode + // if true, Karma captures browsers, runs the tests and exits + singleRun: true, + + // Concurrency level + // how many browser should be started simultaneous + concurrency: Infinity, + + // set correct MIME type when serving .ts files (already compiled to JavaScript): + mime: { + 'text/javascript': ['ts'], + }, + }); +}; diff --git a/package.json b/package.json index a0cea6d..9633a45 100644 --- a/package.json +++ b/package.json @@ -23,27 +23,44 @@ "lint": "npm run check", "samples-test": "mocha build/samples-test", "docs": "compodoc src/", - "webpack": "webpack" + "webpack": "webpack", + "browser-test": "karma start" }, "keywords": [], "author": "Google LLC", "license": "Apache-2.0", "devDependencies": { "@compodoc/compodoc": "^1.1.7", + "@types/execa": "^0.9.0", "@types/mocha": "^5.2.5", + "@types/mv": "^2.1.0", + "@types/ncp": "^2.0.1", "@types/nock": "^9.3.0", "@types/pify": "^3.0.2", "@types/qs": "^6.5.1", + "@types/tmp": "0.0.33", "@types/url-template": "^2.0.28", "@types/uuid": "^3.4.3", "codecov": "^3.0.4", "gts": "^0.9.0", "ink-docstrap": "^1.3.2", "intelli-espower-loader": "^1.0.1", + "is-docker": "^1.1.0", + "karma": "^3.1.4", + "karma-chrome-launcher": "^2.2.0", + "karma-coverage": "^1.1.2", + "karma-firefox-launcher": "^1.1.0", + "karma-mocha": "^1.3.0", + "karma-remap-coverage": "^0.1.5", + "karma-sourcemap-loader": "^0.3.7", + "karma-webpack": "^3.0.5", "mocha": "^5.2.0", + "mv": "^2.1.1", + "ncp": "^2.0.0", "nock": "^10.0.0", "null-loader": "^0.1.1", "nyc": "^13.1.0", + "puppeteer": "^1.11.0", "source-map-support": "^0.5.8", "ts-loader": "^5.3.2", "typescript": "~3.2.0", diff --git a/system-test/fixtures/kitchen/package.json b/system-test/fixtures/kitchen/package.json new file mode 100644 index 0000000..e9c2ce8 --- /dev/null +++ b/system-test/fixtures/kitchen/package.json @@ -0,0 +1,28 @@ +{ + "name": "googleapis-common-fixture", + "version": "1.0.0", + "description": "An app we're using to test the library. ", + "scripts": { + "check": "gts check", + "clean": "gts clean", + "compile": "tsc -p .", + "fix": "gts fix", + "prepare": "npm run compile", + "pretest": "npm run compile", + "posttest": "npm run check", + "start": "node build/src/index.js" + }, + "license": "Apache-2.0", + "dependencies": { + "googleapis-common": "file:./googleapis-common.tgz" + }, + "devDependencies": { + "@types/node": "^10.3.0", + "typescript": "^3.0.0", + "gts": "^0.9.0", + "null-loader": "^0.1.1", + "ts-loader": "^5.0.0", + "webpack": "^4.20.2", + "webpack-cli": "^3.1.1" + } +} diff --git a/system-test/fixtures/kitchen/src/index.ts b/system-test/fixtures/kitchen/src/index.ts new file mode 100644 index 0000000..c0fa569 --- /dev/null +++ b/system-test/fixtures/kitchen/src/index.ts @@ -0,0 +1,9 @@ +import {AuthPlus} from 'googleapis-common'; + +const authPlus = new AuthPlus(); +const auth = new authPlus.OAuth2('client', 'secret', 'redirect'); +function doStuff() { + const oauth2Url = auth.generateAuthUrl(); + console.log(oauth2Url); +} +doStuff(); diff --git a/system-test/fixtures/kitchen/tsconfig.json b/system-test/fixtures/kitchen/tsconfig.json new file mode 100644 index 0000000..8f8e46b --- /dev/null +++ b/system-test/fixtures/kitchen/tsconfig.json @@ -0,0 +1,12 @@ +{ + "extends": "./node_modules/gts/tsconfig-google.json", + "compilerOptions": { + "rootDir": ".", + "outDir": "build", + "esModuleInterop": false // test default setting + }, + "include": [ + "src/*.ts", + "src/**/*.ts" + ] +} diff --git a/system-test/fixtures/kitchen/webpack.config.js b/system-test/fixtures/kitchen/webpack.config.js new file mode 100644 index 0000000..2b0a1f3 --- /dev/null +++ b/system-test/fixtures/kitchen/webpack.config.js @@ -0,0 +1,67 @@ +/** + * Copyright 2019 Google LLC. All Rights Reserved. + * + * 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. + */ + +const path = require('path'); + +module.exports = { + entry: './src/index.ts', + resolve: { + extensions: ['.ts', '.js', '.json'], + alias: { + '../../package.json': path.resolve(__dirname, 'package.json'), + }, + }, + output: { + filename: 'bundle.min.js', + path: path.resolve(__dirname, 'dist'), + }, + node: { + child_process: 'empty', + fs: 'empty', + crypto: 'empty', + }, + module: { + rules: [ + { + test: /src\/crypto\/node\/crypto/, + use: 'null-loader', + }, + { + test: /node_modules\/https-proxy-agent\//, + use: 'null-loader', + }, + { + test: /node_modules\/gtoken\//, + use: 'null-loader', + }, + { + test: /node_modules\/pkginfo\//, + use: 'null-loader', + }, + { + test: /node_modules\/semver\//, + use: 'null-loader', + }, + { + test: /\.ts$/, + use: 'ts-loader', + exclude: /node_modules/, + }, + ], + }, + mode: 'production', + plugins: [], +}; diff --git a/system-test/googleapis.ts b/system-test/googleapis.ts deleted file mode 100644 index 324528f..0000000 --- a/system-test/googleapis.ts +++ /dev/null @@ -1,14 +0,0 @@ -// Copyright 2018, 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. - -console.warn('There are no system tests 👻'); diff --git a/system-test/test.kitchen.ts b/system-test/test.kitchen.ts new file mode 100644 index 0000000..0b6f4f3 --- /dev/null +++ b/system-test/test.kitchen.ts @@ -0,0 +1,67 @@ +/** + * Copyright 2019 Google LLC. All Rights Reserved. + * + * 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. + */ + +import * as assert from 'assert'; +import * as cp from 'child_process'; +import * as execa from 'execa'; +import * as fs from 'fs'; +import * as mv from 'mv'; +import {ncp} from 'ncp'; +import * as path from 'path'; +import * as tmp from 'tmp'; +import {promisify} from 'util'; + +const mvp = promisify(mv) as {} as (...args: string[]) => Promise; +const ncpp = promisify(ncp); +const keep = !!process.env.GALN_KEEP_TEMPDIRS; +const stagingDir = tmp.dirSync({keep, unsafeCleanup: true}); +const stagingPath = stagingDir.name; +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() { + this.timeout(40000); + console.log(`${__filename} staging area: ${stagingPath}`); + await execa('npm', ['pack'], {stdio: 'inherit'}); + const tarball = `${pkg.name}-${pkg.version}.tgz`; + // stagingPath can be on another filesystem so fs.rename() will fail + // with EXDEV, hence we use `mv` module here. + await mvp(tarball, `${stagingPath}/googleapis-common.tgz`); + await ncpp('system-test/fixtures/kitchen', `${stagingPath}/`); + await execa('npm', ['install'], {cwd: `${stagingPath}/`, stdio: 'inherit'}); + }); + + it('should be able to webpack the library', async () => { + // we expect npm install is executed in the before hook + await execa('npx', ['webpack'], {cwd: `${stagingPath}/`, stdio: 'inherit'}); + const bundle = path.join(stagingPath, 'dist', 'bundle.min.js'); + const stat = fs.statSync(bundle); + assert(stat.size < 256 * 1024); + }).timeout(20000); + + /** + * CLEAN UP - remove the staging directory when done. + */ + after('cleanup staging', () => { + if (!keep) { + stagingDir.removeCallback(); + } + }); +}); diff --git a/test/test.isbrowser.ts b/test/test.isbrowser.ts new file mode 100644 index 0000000..ed6f3d7 --- /dev/null +++ b/test/test.isbrowser.ts @@ -0,0 +1,24 @@ +/** + * Copyright 2019 Google LLC. All Rights Reserved. + * + * 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. + */ + +import * as assert from 'assert'; +import {isBrowser} from '../src/isbrowser'; + +describe('isbrowser', () => { + it('should not detect browser', () => { + assert(!isBrowser()); + }); +}); diff --git a/tsconfig.json b/tsconfig.json index 6aadb93..83a2b8a 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -9,6 +9,8 @@ "src/*.ts", "test/*.ts", "system-test/*.ts", - "samples-test/*.ts" + "samples-test/*.ts", + "browser-test/*.ts", + "browser-test/**/*.ts" ] } diff --git a/webpack-tests.config.js b/webpack-tests.config.js new file mode 100644 index 0000000..35a5b56 --- /dev/null +++ b/webpack-tests.config.js @@ -0,0 +1,62 @@ +/** + * Copyright 2019 Google LLC. All Rights Reserved. + * + * 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. + */ + +// This configuration file is used for browser testing with Karma. +// See karma.conf.js for details. + +const path = require('path'); + +module.exports = { + resolve: { + extensions: ['.ts', '.js', '.json'], + alias: { + '../../package.json': path.resolve(__dirname, 'package.json'), + }, + }, + node: { + child_process: 'empty', + fs: 'empty', + crypto: 'empty', + }, + module: { + rules: [ + { + test: /src\/crypto\/node\/crypto/, + use: 'null-loader', + }, + { + test: /node_modules\/gtoken\//, + use: 'null-loader', + }, + { + test: /node_modules\/pkginfo\//, + use: 'null-loader', + }, + { + test: /node_modules\/https-proxy-agent\//, + use: 'null-loader', + }, + { + test: /\.ts$/, + use: 'ts-loader', + exclude: /node_modules/, + }, + ], + }, + mode: 'production', + plugins: [], + performance: {hints: false}, +}; From 2a46739fdf47cb6d17373b698e7681d8ddb352d7 Mon Sep 17 00:00:00 2001 From: Alexander Fenster Date: Wed, 16 Jan 2019 23:44:36 -0800 Subject: [PATCH 13/13] browser test settings in kokoro --- .kokoro/browser-test.sh | 29 +++++++++++++++++++++++ .kokoro/continuous/node8/browser-test.cfg | 12 ++++++++++ .kokoro/presubmit/node8/browser-test.cfg | 12 ++++++++++ 3 files changed, 53 insertions(+) create mode 100755 .kokoro/browser-test.sh create mode 100644 .kokoro/continuous/node8/browser-test.cfg create mode 100644 .kokoro/presubmit/node8/browser-test.cfg diff --git a/.kokoro/browser-test.sh b/.kokoro/browser-test.sh new file mode 100755 index 0000000..7af46ca --- /dev/null +++ b/.kokoro/browser-test.sh @@ -0,0 +1,29 @@ +#!/bin/bash + +# Copyright 2018 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 +# +# https://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. + +set -xeo pipefail + +export NPM_CONFIG_PREFIX=/home/node/.npm-global + +# Setup service account credentials. +export GOOGLE_APPLICATION_CREDENTIALS=${KOKORO_GFILE_DIR}/service-account.json +export GCLOUD_PROJECT=long-door-651 + +cd $(dirname $0)/.. + +npm install + +npm run browser-test diff --git a/.kokoro/continuous/node8/browser-test.cfg b/.kokoro/continuous/node8/browser-test.cfg new file mode 100644 index 0000000..3fb1f62 --- /dev/null +++ b/.kokoro/continuous/node8/browser-test.cfg @@ -0,0 +1,12 @@ +# Download resources for system tests (service account key, etc.) +gfile_resources: "/bigstore/cloud-devrel-kokoro-resources/google-cloud-nodejs" + +# Configure the docker image for kokoro-trampoline. +env_vars: { + key: "TRAMPOLINE_IMAGE" + value: "gcr.io/cloud-devrel-kokoro-resources/node:8-puppeteer" +} +env_vars: { + key: "TRAMPOLINE_BUILD_FILE" + value: "github/nodejs-googleapis-common/.kokoro/browser-test.sh" +} diff --git a/.kokoro/presubmit/node8/browser-test.cfg b/.kokoro/presubmit/node8/browser-test.cfg new file mode 100644 index 0000000..3fb1f62 --- /dev/null +++ b/.kokoro/presubmit/node8/browser-test.cfg @@ -0,0 +1,12 @@ +# Download resources for system tests (service account key, etc.) +gfile_resources: "/bigstore/cloud-devrel-kokoro-resources/google-cloud-nodejs" + +# Configure the docker image for kokoro-trampoline. +env_vars: { + key: "TRAMPOLINE_IMAGE" + value: "gcr.io/cloud-devrel-kokoro-resources/node:8-puppeteer" +} +env_vars: { + key: "TRAMPOLINE_BUILD_FILE" + value: "github/nodejs-googleapis-common/.kokoro/browser-test.sh" +}