@dolittle/sdk.services
Version:
Dolittle is a decentralized, distributed, event-driven microservice platform built to harness the power of events.
181 lines • 12.7 kB
JavaScript
;
// Copyright (c) Dolittle. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (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.reactiveDuplex = exports.reactiveServerStream = exports.reactiveClientStream = exports.reactiveUnary = void 0;
const grpc = __importStar(require("@grpc/grpc-js"));
const rxjs_1 = require("rxjs");
const operators_1 = require("rxjs/operators");
const CouldNotConnectToRuntime_1 = require("./CouldNotConnectToRuntime");
const GrpcError_1 = require("./GrpcError");
/**
* Performs a unary call.
* @param {grpc.Client} client - The Runtime client.
* @param {UnaryMethod<TArgument, TResponse>} method - The method to call.
* @param {TArgument} argument - The argument to send to the server.
* @param {Cancellation} cancellation - Used to cancel the call.
* @returns {Observable<TResponse>} The response from the server.
* @template TArgument - The type of the argument.
* @template TResponse - The type of the response.
*/
function reactiveUnary(client, method, argument, cancellation) {
const subject = new rxjs_1.Subject();
const metadata = new grpc.Metadata();
const call = method.call(client, argument, metadata, {}, (error, message) => {
if (error) {
subject.error(getErrorFrom(error, client.getChannel().getTarget()));
}
else {
subject.next(message);
subject.complete();
}
});
handleCancellation(call, cancellation);
return subject;
}
exports.reactiveUnary = reactiveUnary;
/**
* Performs a client streaming call.
* @param {grpc.Client} client - The Runtime client.
* @param {ClientStreamMethod<TRequest, TResponse>} method - The method to call.
* @param {Observable<TRequest>} requests - The requests to send to the server.
* @param {Cancellation} cancellation - Used to cancel the call.
* @returns {Observable<TResponse>} The response from the server.
* @template TRequest - The type of the argument.
* @template TResponse - The type of the response.
*/
function reactiveClientStream(client, method, requests, cancellation) {
const subject = new rxjs_1.Subject();
const metadata = new grpc.Metadata();
const stream = method.call(client, metadata, {}, (error, message) => {
if (error) {
subject.error(getErrorFrom(error, client.getChannel().getTarget()));
}
else {
subject.next(message);
subject.complete();
}
});
handleCancellation(stream, cancellation);
handleClientRequests(stream, requests, subject, client.getChannel().getTarget());
return subject;
}
exports.reactiveClientStream = reactiveClientStream;
/**
* Performs a server streaming call.
* @param {grpc.Client} client - The Runtime client.
* @param {ServerStreamMethod<TArgument, TResponse>} method - The method to call.
* @param {TArgument} argument - The argument to send to the server.
* @param {Cancellation} cancellation - Used to cancel the call.
* @returns {Observable<TResponse>} The responses from the server.
* @template TArgument - The type of the argument.
* @template TResponse - The type of the response.
*/
function reactiveServerStream(client, method, argument, cancellation) {
const subject = new rxjs_1.Subject();
const metadata = new grpc.Metadata();
const stream = method.call(client, argument, metadata, {});
handleCancellation(stream, cancellation);
handleServerResponses(stream, subject);
return subject;
}
exports.reactiveServerStream = reactiveServerStream;
/**
* Performs a duplex streaming call between the client and the Runtime.
* @param {grpc.Client} client - The Runtime client.
* @param {DuplexMethod<TRequest, TResponse>} method - The method to call.
* @param {Observable<TRequest>} requests - The requests to send to the Runtime.
* @param {Cancellation} cancellation - Used to cancel the call.
* @returns {Observable<TResponse>} The responses from the Runtime and errors from the requests.
* @template TRequest - The type of the argument.
* @template TResponse - The type of the response.
*/
function reactiveDuplex(client, method, requests, cancellation) {
const subject = new rxjs_1.Subject();
const metadata = new grpc.Metadata();
const stream = method.call(client, metadata, {});
handleCancellation(stream, cancellation);
handleClientRequests(stream, requests, subject, client.getChannel().getTarget());
handleServerResponses(stream, subject);
return subject;
}
exports.reactiveDuplex = reactiveDuplex;
function handleCancellation(call, cancellation) {
const subscription = cancellation.subscribe({
complete: () => {
call.cancel();
}
});
call.on('end', () => {
subscription.unsubscribe();
});
}
/**
* Handles writing requests to the Runtime. If the request error, it cancels the stream and errors the
* subject containing the responses from the Runtime.
* @param {grpc.ClientWritableStream<TRequest>} stream - The stream between client and Runtime.
* @param {Observable<TRequest>} requests - The requests to write to the Runtime.
* @param {Subject<TResponse>} subject - The Subject which contains the responses from the Runtime.
* @param {string} address - The address of the Runtime that was connected to.
* @template TRequest - The type of the argument.
* @template TResponse - The type of the response.
*/
function handleClientRequests(stream, requests, subject, address) {
requests.pipe((0, operators_1.concatMap)((message) => {
const subject = new rxjs_1.Subject();
stream.write(message, undefined, () => {
subject.complete();
});
return subject;
})).subscribe({
complete: () => {
stream.end();
},
error: (error) => {
stream.cancel();
subject.error(getErrorFrom(error, address));
}
});
}
/**
* Handles the responses coming from the Runtime.
* @param {grpc.ClientWritableStream} stream - The stream between client and Runtime.
* @param {Subject} subject - The Subject to notify about the responses from the Runtime.
*/
function handleServerResponses(stream, subject) {
stream.on('data', (message) => {
subject.next(message);
});
stream.on('end', () => {
subject.complete();
});
stream.on('error', (error) => {
subject.error(error);
});
}
function getErrorFrom(error, address) {
if ((0, GrpcError_1.isGrpcError)(error) && error.code === grpc.status.UNAVAILABLE) {
return new CouldNotConnectToRuntime_1.CouldNotConnectToRuntime(address);
}
return error;
}
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiUmVhY3RpdmVHcnBjLmpzIiwic291cmNlUm9vdCI6Ii4uLyIsInNvdXJjZXMiOlsiUmVhY3RpdmVHcnBjLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7QUFBQSwrQ0FBK0M7QUFDL0MscUdBQXFHOzs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7O0FBRXJHLG9EQUFzQztBQUN0QywrQ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