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

feat: export protobuf separately, avoid loading gRPC unless needed #1326

Merged
merged 6 commits into from
Aug 26, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/fallback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ import {generateServiceStub} from './fallbackServiceStub';
import {StreamType} from './streamingCalls/streaming';
import {toLowerCamelCase} from './util';
import {google} from '../protos/http';
import * as IamProtos from '../protos/iam_service';
import * as LocationProtos from '../protos/locations';
import * as operationsProtos from '../protos/operations';

export {FallbackServiceError};
export {PathTemplate} from './pathTemplate';
Expand Down Expand Up @@ -347,6 +350,7 @@ export function lro(options: GrpcClientOptions) {
const gaxGrpc = new GrpcClient(options);
return new OperationsClientBuilder(gaxGrpc, options.protoJson);
}
export {operationsProtos, IamProtos, LocationProtos};

/**
* gRPC-fallback version of createApiCall
Expand Down Expand Up @@ -406,6 +410,10 @@ export function createApiCall(
export {protobuf};
export * as protobufMinimal from 'protobufjs/minimal';

export {warn} from './warnings';
export {Operation, operation} from './longRunningCalls/longrunning';
export {GoogleError} from './googleError';

// Different environments or bundlers may or may not respect "browser" field
// in package.json (e.g. Electron does not respect it, but if you run the code
// through webpack first, it will follow the "browser" field).
Expand Down
12 changes: 4 additions & 8 deletions src/iamService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@
// ** All changes to this file may be overwritten. **

import * as gax from './gax';
import {GrpcClient, ClientStubOptions} from './grpc';
import {GrpcClient as FallbackGrpcClient} from './fallback';
import type {GrpcClient, ClientStubOptions} from './grpc';
import type {GrpcClient as FallbackGrpcClient} from './fallback';
import {createApiCall} from './createApiCall';
import {GoogleAuth, OAuth2Client} from 'google-auth-library';
import {ProjectIdCallback} from 'google-auth-library/build/src/auth/googleauth';
import * as routingHeader from './routingHeader';
import * as gapicConfig from './iam_policy_service_client_config.json';
import * as protos from '../protos/iam_service';
import * as fallback from './fallback';
import {Descriptors, ClientOptions, Callback} from './clientInterface';
import type {Descriptors, ClientOptions, Callback} from './clientInterface';
let version = require('../../package.json').version;
import jsonProtos = require('../protos/iam_service.json');

Expand All @@ -40,7 +40,6 @@ export class IamClient {
private _defaults: {[method: string]: gax.CallSettings};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private _protos: any;
private _gaxGrpc: GrpcClient | FallbackGrpcClient;
auth?: GoogleAuth | OAuth2Client;
descriptors: Descriptors = {page: {}, stream: {}, longrunning: {}};
innerApiCalls: {[name: string]: Function} = {};
Expand All @@ -65,9 +64,6 @@ export class IamClient {
options
) as ClientOptions & ClientStubOptions;
version = opts.fallback ? fallback.version : version;
this._gaxGrpc = opts.fallback
? new FallbackGrpcClient(opts)
: new GrpcClient(opts);
opts.scopes = (this.constructor as typeof IamClient).scopes;
// Save options to use in initialize() method.
this._opts = opts;
Expand All @@ -88,7 +84,7 @@ export class IamClient {
clientHeader.push(`${opts.libName}/${opts.libVersion}`);
}
// Load the applicable protos.
this._protos = this._gaxGrpc.loadProtoJSON(jsonProtos);
this._protos = this.gaxGrpc.loadProtoJSON(jsonProtos);
// Put together the default options sent with requests.
this._defaults = gaxGrpc.constructSettings(
'google.iam.v1.IAMPolicy',
Expand Down
8 changes: 4 additions & 4 deletions src/locationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
/* global window */
import * as gax from './gax';
import {warn} from './warnings';
import {GrpcClient} from './grpc';
import {GrpcClient as FallbackGrpcClient} from './fallback';
import type {GrpcClient} from './grpc';
import type {GrpcClient as FallbackGrpcClient} from './fallback';
import {createApiCall} from './createApiCall';
import {GoogleAuth} from 'google-auth-library';
import type {GoogleAuth} from 'google-auth-library';
import * as routingHeader from './routingHeader';
import * as protos from '../protos/locations';
import {
import type {
Descriptors,
ClientOptions,
Callback,
Expand Down
8 changes: 4 additions & 4 deletions src/operationsClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@
* limitations under the License.
*/

import {GoogleAuth, OAuth2Client} from 'google-auth-library';
import type {GoogleAuth, OAuth2Client} from 'google-auth-library';
import {ProjectIdCallback} from 'google-auth-library/build/src/auth/googleauth';
import {ClientOptions, Callback} from './clientInterface';
import type {ClientOptions, Callback} from './clientInterface';

import {GaxCall, ResultTuple, RequestType} from './apitypes';
import {createApiCall} from './createApiCall';
import {PageDescriptor} from './descriptor';
import * as gax from './gax';
import {ClientStubOptions, GrpcClient} from './grpc';
import {GrpcClient as FallbackGrpcClient} from './fallback';
import type {ClientStubOptions, GrpcClient} from './grpc';
import type {GrpcClient as FallbackGrpcClient} from './fallback';
import * as protos from '../protos/operations';
import configData = require('./operations_client_config.json');
import {Transform} from 'stream';
Expand Down
23 changes: 23 additions & 0 deletions src/protobuf.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Copyright 2022 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.
*/

// This file is here to re-export protobuf.js so that the proto.js files
// produced by tools/compileProtos.ts did not depend on protobuf.js
// directly.
// Usage:
// const {protobufMinimal} = require('google-gax/build/src/protobuf');

export * as protobufMinimal from 'protobufjs/minimal';
20 changes: 16 additions & 4 deletions test/unit/compileProtos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@ describe('compileProtos tool', () => {
assert(
js.toString().includes('http://www.apache.org/licenses/LICENSE-2.0')
);
assert(js.toString().includes('require("google-gax").protobufMinimal'));
assert(
js
.toString()
.includes('require("google-gax/build/src/protobuf").protobufMinimal')
);
assert(!js.toString().includes('require("protobufjs/minimal")'));

// check that it uses proper root object; it's taken from fixtures/package.json
Expand All @@ -85,7 +89,9 @@ describe('compileProtos tool', () => {
ts.toString().includes('http://www.apache.org/licenses/LICENSE-2.0')
);
assert(
ts.toString().includes('import {protobuf as $protobuf} from "google-gax"')
ts
.toString()
.includes('import type {protobuf as $protobuf} from "google-gax"')
);
assert(!ts.toString().includes('import * as $protobuf from "protobufjs"'));
});
Expand All @@ -106,7 +112,11 @@ describe('compileProtos tool', () => {
assert(
js.toString().includes('http://www.apache.org/licenses/LICENSE-2.0')
);
assert(js.toString().includes('require("google-gax").protobufMinimal'));
assert(
js
.toString()
.includes('require("google-gax/build/src/protobuf").protobufMinimal')
);
assert(!js.toString().includes('require("protobufjs/minimal")'));

// check that it uses proper root object; it's taken from fixtures/package.json
Expand All @@ -121,7 +131,9 @@ describe('compileProtos tool', () => {
ts.toString().includes('http://www.apache.org/licenses/LICENSE-2.0')
);
assert(
ts.toString().includes('import {protobuf as $protobuf} from "google-gax"')
ts
.toString()
.includes('import type {protobuf as $protobuf} from "google-gax"')
);
assert(!ts.toString().includes('import * as $protobuf from "protobufjs"'));
});
Expand Down
8 changes: 3 additions & 5 deletions tools/compileProtos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ function fixJsFile(js: string): string {
// depend on protobufjs, so we re-export it from google-gax
js = js.replace(
'require("protobufjs/minimal")',
'require("google-gax").protobufMinimal'
'require("google-gax/build/src/protobuf").protobufMinimal'
);

// 2. add Apache license to the generated .js file
Expand All @@ -173,9 +173,7 @@ function fixJsFile(js: string): string {
}

function fixDtsFile(dts: string): string {
// 1. fix for pbts output: the corresponding protobufjs PR
// https://github.com/protobufjs/protobuf.js/pull/1166
// is merged but not yet released.
// 1. fix for pbts output to make sure we import Long properly
dts = dts.replace(
'import * as Long from "long";',
'import Long = require("long");'
Expand All @@ -188,7 +186,7 @@ function fixDtsFile(dts: string): string {
// depend on protobufjs, so we re-export it from google-gax
dts = dts.replace(
'import * as $protobuf from "protobufjs"',
'import {protobuf as $protobuf} from "google-gax"'
'import type {protobuf as $protobuf} from "google-gax"'
);

// 3. add Apache license to the generated .d.ts file
Expand Down