Skip to content

Commit

Permalink
chore: Create new Header class (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
poppoerika authored Apr 5, 2022
1 parent 8767f83 commit e5bc786
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 38 deletions.
12 changes: 3 additions & 9 deletions src/Momento.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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(),
Expand Down
17 changes: 4 additions & 13 deletions src/MomentoCache.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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(),
Expand Down
43 changes: 27 additions & 16 deletions src/grpc/HeadersInterceptor.ts
Original file line number Diff line number Diff line change
@@ -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, {});
},
Expand Down

0 comments on commit e5bc786

Please sign in to comment.