UNPKG

@emeraldpay/api

Version:
113 lines 4.78 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Executor = exports.isMethodExecutor = void 0; const Publisher_1 = require("./Publisher"); const Retry_1 = require("./Retry"); const grpc = __importStar(require("./typesGrpc")); function isMethodExecutor(executor) { return (typeof executor === 'object' && executor != null && 'execute' in executor && typeof executor.execute === 'function'); } exports.isMethodExecutor = isMethodExecutor; class Executor extends Publisher_1.ManagedPublisher { constructor(call, checker, request) { super(); this.connections = 0; if (call == null || typeof call !== 'function') { throw new Error('Call is not provided'); } if (checker == null || !(0, Retry_1.isContinueCheck)(checker)) { throw new Error('Continue checker is not provided'); } if (request == null) { throw new Error('Request is not provided'); } this.call = call; this.checker = checker; this.request = request; } setLogger(logger) { this.logger = logger; } cancel() { var _a; this.checker.onClose(); (_a = this.upstream) === null || _a === void 0 ? void 0 : _a.cancel(); } execute(reconnect) { if (this.upstream != null) { this.upstream.cancel(); this.upstream = null; } const callId = this.connections++; const upstream = this.call(this.request); upstream .onData((data) => { var _a, _b, _c; (_a = this.logger) === null || _a === void 0 ? void 0 : _a.log(`gRPC request ${callId} data`); (_c = (_b = this.checker).onSuccess) === null || _c === void 0 ? void 0 : _c.call(_b); this.emitData(data); }) .onError((error) => { var _a, _b, _c, _d, _e; (_b = (_a = this.checker).onFail) === null || _b === void 0 ? void 0 : _b.call(_a); if (typeof error === 'object' && 'code' in error && typeof error.code === 'number') { const grpcError = error; if (grpcError.code === grpc.GrpcStatus.UNKNOWN || grpcError.code === grpc.GrpcStatus.UNAVAILABLE || grpcError.code === grpc.GrpcStatus.INTERNAL) { (_c = this.logger) === null || _c === void 0 ? void 0 : _c.warn(`gRPC connection for ${callId} lost with code ${grpcError.code}: ${grpcError.message}. Reconnecting...`); this.reconnect(); setTimeout(reconnect.bind(this), 100); } else { (_d = this.logger) === null || _d === void 0 ? void 0 : _d.error(`gRPC connection for ${callId} lost with code ${grpcError.code}: ${grpcError.message}. Closing...`); this.cancel(); this.emitError(grpcError); } } else { (_e = this.logger) === null || _e === void 0 ? void 0 : _e.error(`gRPC client error: ${error.message}. Closing...`); this.cancel(); this.emitError(error); } }) .finally(() => { var _a; if (!this.reconnecting) { (_a = this.logger) === null || _a === void 0 ? void 0 : _a.log(`gRPC request ${callId} closed`); this.checker.onClose(); this.emitClosed(); } }); this.upstream = upstream; } terminate() { this.cancel(); this.emitError(new Error('Execution terminated')); } } exports.Executor = Executor; //# sourceMappingURL=Executor.js.map