From e5bc7863627ebdae5e639d88bd50e686377456c3 Mon Sep 17 00:00:00 2001 From: Erika Tharp Date: Tue, 5 Apr 2022 09:47:17 -0700 Subject: [PATCH] chore: Create new `Header` class (#66) --- src/Momento.ts | 12 +++------- src/MomentoCache.ts | 17 ++++---------- src/grpc/HeadersInterceptor.ts | 43 +++++++++++++++++++++------------- 3 files changed, 34 insertions(+), 38 deletions(-) diff --git a/src/Momento.ts b/src/Momento.ts index 97e9328cc..4dba30756 100644 --- a/src/Momento.ts +++ b/src/Momento.ts @@ -1,5 +1,5 @@ import {control} from '@gomomento/generated-types'; -import {HeaderInterceptor} from './grpc/HeadersInterceptor'; +import {Header, HeaderInterceptor} from './grpc/HeadersInterceptor'; import {ClientTimeoutInterceptor} from './grpc/ClientTimeoutInterceptor'; import { InvalidArgumentError, @@ -29,14 +29,8 @@ export class Momento { */ constructor(props: MomentoProps) { const headers = [ - { - name: 'Authorization', - value: props.authToken, - }, - { - name: 'Agent', - value: `javascript:${version}`, - }, + new Header('Authorization', props.authToken), + new Header('Agent', `javascript:${version}`), ]; this.interceptors = [ new HeaderInterceptor(headers).addHeadersInterceptor(), diff --git a/src/MomentoCache.ts b/src/MomentoCache.ts index 62aecdf60..c1e20f2b0 100644 --- a/src/MomentoCache.ts +++ b/src/MomentoCache.ts @@ -1,7 +1,7 @@ import {cache} from '@gomomento/generated-types'; // older versions of node don't have the global util variables https://github.com/nodejs/node/issues/20365 import {TextEncoder} from 'util'; -import {HeaderInterceptor} from './grpc/HeadersInterceptor'; +import {Header, HeaderInterceptor} from './grpc/HeadersInterceptor'; import {ClientTimeoutInterceptor} from './grpc/ClientTimeoutInterceptor'; import {CacheGetStatus, momentoResultConverter} from './messages/Result'; import {InvalidArgumentError, UnknownServiceError} from './Errors'; @@ -166,18 +166,9 @@ export class MomentoCache { private getInterceptors(cacheName: string): Interceptor[] { const headers = [ - { - name: 'Authorization', - value: this.authToken, - }, - { - name: 'cache', - value: cacheName, - }, - { - name: 'Agent', - value: `javascript:${version}`, - }, + new Header('Authorization', this.authToken), + new Header('cache', cacheName), + new Header('Agent', `javascript:${version}`), ]; return [ new HeaderInterceptor(headers).addHeadersInterceptor(), diff --git a/src/grpc/HeadersInterceptor.ts b/src/grpc/HeadersInterceptor.ts index 8ba1a4d00..b5cd20398 100644 --- a/src/grpc/HeadersInterceptor.ts +++ b/src/grpc/HeadersInterceptor.ts @@ -1,35 +1,46 @@ import {InterceptingCall, Interceptor} from '@grpc/grpc-js'; -interface Header { - name: string; - value: string; +export class Header { + public readonly onceOnlyHeaders: string[] = ['Agent']; + public readonly name: string; + public readonly value: string; + + /** + * @param {string} name + * @param {string} value + */ + constructor(name: string, value: string) { + this.name = name; + this.value = value; + } } export class HeaderInterceptor { - private readonly headers: Header[]; - private static isUserAgentSent = false; + private readonly headersToAddEveryTime: Header[]; + private readonly headersToAddOnce: Header[]; + private static areOnlyOnceHeadersSent = false; /** * @param {Header[]} headers */ constructor(headers: Header[]) { - this.headers = headers; + this.headersToAddOnce = headers.filter(header => + header.onceOnlyHeaders.includes(header.name) + ); + this.headersToAddEveryTime = headers.filter( + header => !header.onceOnlyHeaders.includes(header.name) + ); } public addHeadersInterceptor(): Interceptor { return (options, nextCall) => { return new InterceptingCall(nextCall(options), { start: (metadata, listener, next) => { - if (!HeaderInterceptor.isUserAgentSent) { - this.headers.forEach(h => metadata.add(h.name, h.value)); - HeaderInterceptor.isUserAgentSent = true; - } else { - // Only add Authorization/cache metadata - this.headers.forEach(h => { - if (h.name !== 'Agent') { - metadata.add(h.name, h.value); - } - }); + this.headersToAddEveryTime.forEach(h => + metadata.add(h.name, h.value) + ); + if (!HeaderInterceptor.areOnlyOnceHeadersSent) { + this.headersToAddOnce.forEach(h => metadata.add(h.name, h.value)); } next(metadata, {}); },