Merge pull request #1312 from murgatroid99/grpc-js_types_for_grpc-gcp

grpc-js: Update some types and type checks for compatibility with grpc-gcp
This commit is contained in:
Michael Lumish 2020-03-25 12:33:18 -07:00 committed by GitHub
commit a9298edb7c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 48 additions and 23 deletions

View File

@ -1,6 +1,6 @@
{
"name": "@grpc/grpc-js",
"version": "0.7.2",
"version": "0.7.3",
"description": "gRPC Library for Node - pure JS implementation",
"homepage": "https://grpc.io/",
"repository": "https://github.com/grpc/grpc-node/tree/master/packages/grpc-js",

View File

@ -110,9 +110,9 @@ export interface Channel {
*/
createCall(
method: string,
deadline: Deadline | null | undefined,
deadline: Deadline,
host: string | null | undefined,
parentCall: Call | null | undefined,
parentCall: any,
propagateFlags: number | null | undefined
): Call;
}
@ -140,6 +140,17 @@ export class ChannelImplementation implements Channel {
private readonly credentials: ChannelCredentials,
private readonly options: ChannelOptions
) {
if (typeof target !== 'string') {
throw new TypeError('Channel target must be a string');
}
if (!(credentials instanceof ChannelCredentials)) {
throw new TypeError('Channel credentials must be a ChannelCredentials object');
}
if (options) {
if ((typeof options !== 'object') || !Object.values(options).every(value => typeof value === 'string' || typeof value === 'number' || typeof value === 'undefined')) {
throw new TypeError('Channel options must be an object with string or number values');
}
}
/* The global boolean parameter to getSubchannelPool has the inverse meaning to what
* the grpc.use_local_subchannel_pool channel option means. */
this.subchannelPool = getSubchannelPool(
@ -430,11 +441,17 @@ export class ChannelImplementation implements Channel {
createCall(
method: string,
deadline: Deadline | null | undefined,
deadline: Deadline,
host: string | null | undefined,
parentCall: Call | null | undefined,
parentCall: any,
propagateFlags: number | null | undefined
): Call {
if (typeof method !== 'string') {
throw new TypeError('Channel#createCall: method must be a string');
}
if (!(typeof deadline === 'number' || deadline instanceof Date)) {
throw new TypeError('Channel#createCall: deadline must be a number or Date');
}
if (this.connectivityState === ConnectivityState.SHUTDOWN) {
throw new Error('Channel has been shut down');
}
@ -451,8 +468,7 @@ export class ChannelImplementation implements Channel {
deadline
);
const finalOptions: CallStreamOptions = {
deadline:
deadline === null || deadline === undefined ? Infinity : deadline,
deadline: deadline,
flags: propagateFlags || 0,
host: host || this.defaultAuthority,
parentCall: parentCall || null,

View File

@ -48,6 +48,7 @@ import {
InterceptorArguments,
InterceptingCallInterface,
} from './client-interceptors';
import { ServerUnaryCall, ServerReadableStream, ServerWritableStream, ServerDuplexStream } from './server-call';
const CHANNEL_SYMBOL = Symbol();
const INTERCEPTOR_SYMBOL = Symbol();
@ -61,8 +62,7 @@ export interface UnaryCallback<ResponseType> {
export interface CallOptions {
deadline?: Deadline;
host?: string;
/* There should be a parent option here that will accept a server call,
* but the server is not yet implemented so it makes no sense to have it */
parent?: ServerUnaryCall<any, any> | ServerReadableStream<any, any> | ServerWritableStream<any, any> | ServerDuplexStream<any, any>
propagate_flags?: number;
credentials?: CallCredentials;
interceptors?: Interceptor[];
@ -109,10 +109,28 @@ export class Client {
credentials: ChannelCredentials,
options: ClientOptions = {}
) {
options = Object.assign({}, options);
this[INTERCEPTOR_SYMBOL] = options.interceptors ?? [];
delete options.interceptors;
this[INTERCEPTOR_PROVIDER_SYMBOL] = options.interceptor_providers ?? [];
delete options.interceptor_providers;
if (
this[INTERCEPTOR_SYMBOL].length > 0 &&
this[INTERCEPTOR_PROVIDER_SYMBOL].length > 0
) {
throw new Error(
'Both interceptors and interceptor_providers were passed as options ' +
'to the client constructor. Only one of these is allowed.'
);
}
this[CALL_INVOCATION_TRANSFORMER_SYMBOL] = options.callInvocationTransformer;
delete options.callInvocationTransformer;
if (options.channelOverride) {
this[CHANNEL_SYMBOL] = options.channelOverride;
} else if (options.channelFactoryOverride) {
this[CHANNEL_SYMBOL] = options.channelFactoryOverride(
const channelFactoryOverride = options.channelFactoryOverride;
delete options.channelFactoryOverride;
this[CHANNEL_SYMBOL] = channelFactoryOverride(
address,
credentials,
options
@ -124,18 +142,6 @@ export class Client {
options
);
}
this[INTERCEPTOR_SYMBOL] = options.interceptors ?? [];
this[INTERCEPTOR_PROVIDER_SYMBOL] = options.interceptor_providers ?? [];
if (
this[INTERCEPTOR_SYMBOL].length > 0 &&
this[INTERCEPTOR_PROVIDER_SYMBOL].length > 0
) {
throw new Error(
'Both interceptors and interceptor_providers were passed as options ' +
'to the client constructor. Only one of these is allowed.'
);
}
this[CALL_INVOCATION_TRANSFORMER_SYMBOL] = options.callInvocationTransformer;
}
close(): void {

View File

@ -28,7 +28,7 @@ import { CallCredentials } from './call-credentials';
import { Deadline, StatusObject } from './call-stream';
import { Channel, ConnectivityState, ChannelImplementation } from './channel';
import { ChannelCredentials } from './channel-credentials';
import { CallOptions, Client } from './client';
import { CallOptions, Client, CallInvocationTransformer, CallProperties } from './client';
import { LogVerbosity, Status } from './constants';
import * as logging from './logging';
import {
@ -199,7 +199,10 @@ export {
loadPackageDefinition,
makeClientConstructor,
makeClientConstructor as makeGenericClientConstructor,
CallProperties,
CallInvocationTransformer,
ChannelImplementation as Channel,
Channel as ChannelInterface
};
/**