Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 | 1x 1x 1x 1x 1x 1x 1x 16x 16x 1x 1x 1x 1x 1x 16x 16x 16x 16x 4x 6x 1x 5x 1x 4x 4x 1x 3x 1x 2x 6x 6x 6x 18x 18x 18x 18x 18x 18x 18x 6x 6x | import path from 'path';
import { EventEmitter } from 'events';
import grpc, { ServiceError, status as ServiceErrorStatus } from 'grpc';
import { loadSync as loadProtoSync } from '@grpc/proto-loader';
import { ApplicationConfiguration, getServiceConfiguration } from './configuration';
import {
ApplicationDeveloperSubscription,
ApplicationDeveloperKey,
GetApplicationDeveloperSubscriptionPayload,
GetApplicationDeveloperSubscriptionResponse
} from './protos/types/application';
const EVENTS = {
UPDATE_SUCCESS: Symbol(),
UPDATE_ERROR: Symbol()
};
export const DEVELOPER_KEY_VALIDATOR_EVENTS = EVENTS;
interface ApplicationDeveloperKeyWithSubscription extends ApplicationDeveloperKey {
subscription: ApplicationDeveloperSubscription;
}
class DeveloperSubscriptionReceiver extends EventEmitter {
private call: grpc.ServerDuplexStream<GetApplicationDeveloperSubscriptionPayload, any>;
private client: grpc.Client;
private config: ApplicationConfiguration;
get isRunning(): boolean {
return !!this.client;
}
constructor(config: ApplicationConfiguration) {
super();
this.config = config;
}
public start(): this {
if (this.isRunning) {
// Dont create a duplicate client if one exists
return;
}
const definition = grpc.loadPackageDefinition(
loadProtoSync(
`${path.join(__dirname, 'protos')}/application.proto`,
{
keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true
}
)
);
const { address, credentials } = getServiceConfiguration();
this.client = new (definition.application as any).Application(address, credentials);
this.createCall();
return this;
}
public stop(): this {
if (this.isRunning) {
this.client.close();
}
return this;
}
private createCall(): void {
this.call = (this.client as any).getApplicationDeveloperSubscriptions({
key: this.config.key
});
this.call
.on('data', data => this.emit('data', data)) // Just proxy
.on('error', (err: ServiceError) => {
// Connection was interrupted, recreate
const retriableErrorStatuses = [
ServiceErrorStatus.INTERNAL,
ServiceErrorStatus.UNKNOWN,
ServiceErrorStatus.CANCELLED,
ServiceErrorStatus.DEADLINE_EXCEEDED,
ServiceErrorStatus.ABORTED
];
if (retriableErrorStatuses.includes(err.code)) {
this.createCall();
} else {
this.emit(EVENTS.UPDATE_ERROR, err);
}
});
}
}
export interface DeveloperKeyValidator extends EventEmitter {
start(): void;
stop(): void;
isValidDeveloperKey(key: string): boolean;
validateDeveloperKey(key: string): DeveloperSubscriptionValidationError;
}
export interface DeveloperKeysUpdatedPayload {
keys: ApplicationDeveloperKeyWithSubscription[];
subscriptions: ApplicationDeveloperSubscription[];
}
export class DeveloperKeyNotSyncedError extends Error {}
export class DeveloperKeyNotFoundError extends Error {}
export class DeveloperKeyNotActiveError extends Error {}
export class DeveloperKeySubscriptionNotActiveError extends Error {}
export type DeveloperSubscriptionValidationError = null |
DeveloperKeyNotFoundError | DeveloperKeyNotActiveError | DeveloperKeySubscriptionNotActiveError;
export class DefaultDeveloperKeyValidator extends DeveloperSubscriptionReceiver implements DeveloperKeyValidator {
private developerSubscriptions: Map<number, ApplicationDeveloperSubscription> = new Map();
private developerKeys: Map<string, ApplicationDeveloperKeyWithSubscription> = new Map();
private developerKeysSynced = false;
constructor(config: ApplicationConfiguration) {
super(config);
}
public start(): this {
super.start();
super.on('data', this.onSubscriptionsUpdate.bind(this));
return this;
}
public stop(): this {
super.stop();
return this;
}
public isValidDeveloperKey(key: string): boolean {
return this.validateDeveloperKey(key) === null;
}
public validateDeveloperKey(key: string): DeveloperSubscriptionValidationError {
if (!this.developerKeysSynced) {
return new DeveloperKeyNotSyncedError('Developer keys not synced');
}
if (!this.developerKeys.has(key)) {
return new DeveloperKeyNotFoundError('No developer key found');
}
const developerKey = this.developerKeys.get(key);
if (!developerKey.active) {
return new DeveloperKeyNotActiveError('Developer key not active');
}
if (!developerKey.subscription.active) {
return new DeveloperKeySubscriptionNotActiveError('Developer subscription not active');
}
return null;
}
protected onSubscriptionsUpdate(update: GetApplicationDeveloperSubscriptionResponse): void {
const keys = [];
const subscriptions = [];
for (const updatedSubscription of update.subscriptions) {
this.developerSubscriptions.set(updatedSubscription.id, updatedSubscription);
for (const updatedKey of updatedSubscription.keys) {
const developerKey = updatedKey as ApplicationDeveloperKeyWithSubscription;
developerKey.subscription = updatedSubscription;
this.developerKeys.set(developerKey.key, developerKey);
keys.push(this.developerKeys.get(developerKey.key));
}
subscriptions.push(this.developerSubscriptions.get(updatedSubscription.id));
}
this.emit(EVENTS.UPDATE_SUCCESS, { keys, subscriptions });
this.developerKeysSynced = true;
}
}
|