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

fix: make browser and rest use case work #1311

Merged
merged 1 commit into from
Aug 12, 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
4 changes: 3 additions & 1 deletion src/createApiCall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ import {StreamingApiCaller} from './streamingCalls/streamingApiCaller';
export function createApiCall(
func: Promise<GRPCCall> | GRPCCall,
settings: CallSettings,
descriptor?: Descriptor
descriptor?: Descriptor,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_fallback?: boolean | 'proto' | 'rest' // unused here, used in fallback.ts implementation
): GaxCall {
// we want to be able to accept both promise resolving to a function and a
// function. Currently client librares are only calling this method with a
Expand Down
28 changes: 25 additions & 3 deletions src/fallback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,12 @@ import {google} from '../protos/http';
export {FallbackServiceError};
export {PathTemplate} from './pathTemplate';
export {routingHeader};
export {CallSettings, constructSettings, RetryOptions} from './gax';
export {
CallSettings,
constructSettings,
RetryOptions,
createDefaultBackoffSettings,
} from './gax';
export const version = require('../../package.json').version + '-fallback';

export {
Expand All @@ -58,6 +63,10 @@ export {

export {StreamType} from './streamingCalls/streaming';

export {OperationsClient} from './operationsClient';
export {IamClient} from './iamService';
export {LocationsClient} from './locationService';

export const defaultToObjectOptions = {
keepCase: false,
longs: String,
Expand Down Expand Up @@ -365,16 +374,29 @@ export function lro(options: GrpcClientOptions) {
export function createApiCall(
func: Promise<GRPCCall> | GRPCCall,
settings: gax.CallSettings,
descriptor?: Descriptor
descriptor?: Descriptor,
fallback?: boolean | 'proto' | 'rest'
): GaxCall {
if (
(!fallback || fallback === 'rest') &&
descriptor &&
'streaming' in descriptor &&
(descriptor as StreamDescriptor).type !== StreamType.SERVER_STREAMING
) {
return () => {
throw new Error(
'The gRPC-fallback client library (e.g. browser version of the library) currently does not support client-streaming or bidi-stream calls.'
'The REST transport currently does not support client-streaming or bidi-stream calls.'
);
};
}
if (
(fallback === 'proto' || fallback === true) && // for legacy reasons, fallback === true means 'proto'
descriptor &&
'streaming' in descriptor
) {
return () => {
throw new Error(
'The gRPC-fallback (proto over HTTP) transport currently does not support streaming calls.'
);
};
}
Expand Down
15 changes: 2 additions & 13 deletions src/fallbackRest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,12 @@
import * as serializer from 'proto3-json-serializer';
import {defaultToObjectOptions} from './fallback';
import {FetchParameters} from './fallbackServiceStub';
import {hasTextDecoder, hasTextEncoder, isNodeJS} from './featureDetection';
import {hasTextDecoder, hasTextEncoder} from './featureDetection';
import {GoogleError} from './googleError';
import {transcode} from './transcoding';

if (!hasTextEncoder() || !hasTextDecoder()) {
if (isNodeJS()) {
// Node.js 10 does not have global TextDecoder
// TODO(@alexander-fenster): remove this logic after Node.js 10 is EOL.
// eslint-disable-next-line @typescript-eslint/no-var-requires
const util = require('util');
Object.assign(global, {
TextDecoder: util.TextDecoder,
TextEncoder: util.TextEncoder,
});
} else {
require('fast-text-encoding');
}
require('fast-text-encoding');
}

export function encodeRequest(
Expand Down
15 changes: 5 additions & 10 deletions src/locationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ export class LocationsClient {
private _terminated = false;
private _opts: ClientOptions;
private _providedCustomServicePath: boolean;
private _gaxGrpc: GrpcClient | FallbackGrpcClient;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private _protos: {};
private _defaults: {[method: string]: gax.CallSettings};
auth: GoogleAuth;
Expand Down Expand Up @@ -124,14 +122,11 @@ export class LocationsClient {
opts['scopes'] = staticMembers.scopes;
}

// Create a `gaxGrpc` object, with any grpc-specific options sent to the client.
this._gaxGrpc = new GrpcClient(opts);

// Save options to use in initialize() method.
this._opts = opts;

// Save the auth object to the client, for use by other methods.
this.auth = this._gaxGrpc.auth as GoogleAuth;
this.auth = gaxGrpc.auth as GoogleAuth;

// Set the default scopes in auth client if needed.
if (servicePath === staticMembers.servicePath) {
Expand All @@ -146,15 +141,15 @@ export class LocationsClient {
clientHeader.push(`gl-web/${version}`);
}
if (!opts.fallback) {
clientHeader.push(`grpc/${this._gaxGrpc.grpcVersion}`);
clientHeader.push(`grpc/${gaxGrpc.grpcVersion}`);
} else if (opts.fallback === 'rest') {
clientHeader.push(`rest/${this._gaxGrpc.grpcVersion}`);
clientHeader.push(`rest/${gaxGrpc.grpcVersion}`);
}
if (opts.libName && opts.libVersion) {
clientHeader.push(`${opts.libName}/${opts.libVersion}`);
}
// Load the applicable protos.
this._protos = this._gaxGrpc.loadProtoJSON(jsonProtos);
this._protos = gaxGrpc.loadProtoJSON(jsonProtos);

// Some of the methods on this service return "paged" results,
// (e.g. 50 results at a time, with tokens to get subsequent
Expand All @@ -168,7 +163,7 @@ export class LocationsClient {
};

// Put together the default options sent with requests.
this._defaults = this._gaxGrpc.constructSettings(
this._defaults = gaxGrpc.constructSettings(
'google.cloud.location.Locations',
gapicConfig as gax.ClientConfig,
opts.clientConfig || {},
Expand Down