From 12cbdda54c0b47b62126e67fc76dd383a13a7de7 Mon Sep 17 00:00:00 2001 From: Cavan Date: Mon, 9 Dec 2019 16:24:37 -0700 Subject: [PATCH 1/2] Fix 'subscribeSingleInvoice' argument passing --- src/services/invoices.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/services/invoices.ts b/src/services/invoices.ts index d1ed624..dcc2fad 100644 --- a/src/services/invoices.ts +++ b/src/services/invoices.ts @@ -15,6 +15,7 @@ export function createInvoices(config: ConnectionConfig): any { const subscriptionMethods: SubscriptionMethod[] = [ { name: 'subscribeSingleInvoice', + skipEmptyArgDefault: true, }, ]; From aacf71ec8d052cbc06ddfe799bee5147a3685615 Mon Sep 17 00:00:00 2001 From: Cavan Date: Mon, 9 Dec 2019 16:50:28 -0700 Subject: [PATCH 2/2] Fix empty argument defaulting for all methods --- src/default-empty-arg.ts | 8 +++--- src/services/chain-notifier.ts | 16 ++++------- src/services/create-service-client.ts | 8 ++---- src/services/invoices.ts | 9 ++---- src/services/lightning.ts | 41 +++++++-------------------- src/services/router.ts | 12 +++----- src/types/general.ts | 7 +---- 7 files changed, 30 insertions(+), 71 deletions(-) diff --git a/src/default-empty-arg.ts b/src/default-empty-arg.ts index 30a2f35..e0623e2 100644 --- a/src/default-empty-arg.ts +++ b/src/default-empty-arg.ts @@ -1,12 +1,12 @@ /** * Add an empty object as the first argument if no argument was passed - * @param {Function} func + * @param {Function} func The function being called + * @param {boolean} hasCallback Whether or not the passed function has a callback * @return {any} */ -export function defaultEmptyArg(func: (...args: any[]) => any): any { +export function defaultEmptyArg(func: (...args: any[]) => any, hasCallback: boolean = true): any { return function(...args: any[]) { - // 2 is used, rather than 1, because of the callback. - if (args.length < 2) { + if (args.length < (hasCallback ? 2 : 1)) { args.unshift({}); } return func.apply(this, args); diff --git a/src/services/chain-notifier.ts b/src/services/chain-notifier.ts index a29dfbe..892f8fc 100644 --- a/src/services/chain-notifier.ts +++ b/src/services/chain-notifier.ts @@ -1,4 +1,4 @@ -import { ConnectionConfig, SubscriptionMethod } from '../types'; +import { ConnectionConfig } from '../types'; import { createServiceClient } from './create-service-client'; /** @@ -12,16 +12,10 @@ export function createChainNotifier(config: ConnectionConfig): any { server, credentials, ); - const subscriptionMethods: SubscriptionMethod[] = [ - { - name: 'registerConfirmationsNtfn', - }, - { - name: 'registerSpendNtfn', - }, - { - name: 'registerBlockEpochNtfn', - }, + const subscriptionMethods = [ + 'registerConfirmationsNtfn', + 'registerSpendNtfn', + 'registerBlockEpochNtfn', ]; return createServiceClient({ diff --git a/src/services/create-service-client.ts b/src/services/create-service-client.ts index e50289a..3598049 100644 --- a/src/services/create-service-client.ts +++ b/src/services/create-service-client.ts @@ -22,12 +22,8 @@ export function createServiceClient(config: GrpcServiceConfig) { if (typeof method !== 'function') { return target[key]; // forward } else if (config.subscriptionMethods) { - const sm = config.subscriptionMethods.find((m) => m.name === key); - if (sm) { - if (sm.skipEmptyArgDefault) { - return method; - } - return defaultEmptyArg(method); + if (config.subscriptionMethods.includes(key)) { + return defaultEmptyArg(method, false); } } return promisify(defaultEmptyArg(method)); diff --git a/src/services/invoices.ts b/src/services/invoices.ts index dcc2fad..9d74fac 100644 --- a/src/services/invoices.ts +++ b/src/services/invoices.ts @@ -1,4 +1,4 @@ -import { ConnectionConfig, SubscriptionMethod } from '../types'; +import { ConnectionConfig } from '../types'; import { createServiceClient } from './create-service-client'; /** @@ -12,11 +12,8 @@ export function createInvoices(config: ConnectionConfig): any { server, credentials, ); - const subscriptionMethods: SubscriptionMethod[] = [ - { - name: 'subscribeSingleInvoice', - skipEmptyArgDefault: true, - }, + const subscriptionMethods = [ + 'subscribeSingleInvoice', ]; return createServiceClient({ diff --git a/src/services/lightning.ts b/src/services/lightning.ts index 8a083f7..54ed370 100644 --- a/src/services/lightning.ts +++ b/src/services/lightning.ts @@ -1,4 +1,4 @@ -import { ConnectionConfig, SubscriptionMethod } from '../types'; +import { ConnectionConfig } from '../types'; import { createServiceClient } from './create-service-client'; /** @@ -12,35 +12,16 @@ export function createLightning(config: ConnectionConfig): any { // Increase max receive message size for describegraph 'grpc.max_receive_message_length': 50 * 1024 * 1024, }); - const subscriptionMethods: SubscriptionMethod[] = [ - { - name: 'subscribeInvoices', - }, - { - name: 'subscribeTransactions', - }, - { - name: 'subscribeChannelGraph', - }, - { - name: 'subscribeChannelEvents', - }, - { - name: 'subscribeChannelBackups', - }, - { - name: 'sendToRoute', - }, - { - name: 'sendPayment', - }, - { - name: 'openChannel', - }, - { - name: 'closeChannel', - skipEmptyArgDefault: true, - }, + const subscriptionMethods = [ + 'subscribeInvoices', + 'subscribeTransactions', + 'subscribeChannelGraph', + 'subscribeChannelEvents', + 'subscribeChannelBackups', + 'sendToRoute', + 'sendPayment', + 'openChannel', + 'closeChannel', ]; return createServiceClient({ diff --git a/src/services/router.ts b/src/services/router.ts index b729ad7..77c71ca 100644 --- a/src/services/router.ts +++ b/src/services/router.ts @@ -1,4 +1,4 @@ -import { ConnectionConfig, SubscriptionMethod } from '../types'; +import { ConnectionConfig } from '../types'; import { createServiceClient } from './create-service-client'; /** @@ -12,13 +12,9 @@ export function createRouter(config: ConnectionConfig): any { server, credentials, ); - const subscriptionMethods: SubscriptionMethod[] = [ - { - name: 'sendPayment', - }, - { - name: 'trackPayment', - }, + const subscriptionMethods = [ + 'sendPayment', + 'trackPayment', ]; return createServiceClient({ diff --git a/src/types/general.ts b/src/types/general.ts index f66128c..f16c21f 100644 --- a/src/types/general.ts +++ b/src/types/general.ts @@ -5,11 +5,6 @@ export type GrpcLoader = typeof grpcLoader; export type Grpc = typeof grpc; -export interface SubscriptionMethod { - name: string; - skipEmptyArgDefault?: boolean; -} - export interface NestedGrpcObject { [index: string]: { [index: string]: typeof Client; @@ -24,7 +19,7 @@ export interface ConnectionConfig { export interface GrpcServiceConfig extends ConnectionConfig { service: any; - subscriptionMethods?: SubscriptionMethod[]; + subscriptionMethods?: string[]; } export interface GrpcObjectConfig {