UNPKG

@digitalasset/daml-ledger

Version:
70 lines 2.46 kB
"use strict"; // Copyright (c) 2021 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. // SPDX-License-Identifier: Apache-2.0 Object.defineProperty(exports, "__esModule", { value: true }); exports.ClientReadableObjectStream = void 0; const stream_1 = require("stream"); const Deserializer_1 = require("./Deserializer"); /** * A stream of objects that are pushed from the server and are readable from * the client. It it a Readable Node.js stream and wraps the call to gRPC. */ class ClientReadableObjectStream extends stream_1.Readable { constructor(wrappedStream, codec, opts) { super(Object.assign(opts || {}, { objectMode: true })); if (!(wrappedStream instanceof Error) && codec !== undefined) { this.wrappedStream = wrappedStream; this.deserializedStream = this.wrappedStream.pipe(new Deserializer_1.Deserializer(codec)); this.deserializedStream.on('readable', () => this._read()); this.deserializedStream.on('finish', () => this.emit('end')); } else if (wrappedStream instanceof Error) { process.nextTick(() => { this.emit('error', wrappedStream); process.nextTick(() => { this.emit('end'); }); }); } } static from(wrapped, codec, opts) { if (wrapped instanceof Error) { return new ClientReadableObjectStream(wrapped); } else { return new ClientReadableObjectStream(wrapped, codec, opts); } } _read() { if (this.deserializedStream) { const object = this.deserializedStream.read(); if (object) { this.push(object); } } } /** * Cancel the ongoing call. Results in the call ending with a CANCELLED status, * unless it has already ended with some other status. */ cancel() { if (this.wrappedStream) { this.wrappedStream.cancel(); } } /** * Get the endpoint this call/stream is connected to. * * @returns {string} The URI of the endpoint */ getPeer() { if (this.wrappedStream) { return this.wrappedStream.getPeer(); } else { return ''; } } } exports.ClientReadableObjectStream = ClientReadableObjectStream; //# sourceMappingURL=ClientReadableObjectStream.js.map