Built motion from commit 6a09e18b.|2.6.11
[motion2.git] / legacy-libs / grpc-cloned / node_modules / @grpc / grpc-js / build / src / call.js
1 "use strict";
2 Object.defineProperty(exports, "__esModule", { value: true });
3 const events_1 = require("events");
4 const stream_1 = require("stream");
5 const constants_1 = require("./constants");
6 class ClientUnaryCallImpl extends events_1.EventEmitter {
7     constructor(call) {
8         super();
9         this.call = call;
10         call.on('metadata', (metadata) => {
11             this.emit('metadata', metadata);
12         });
13         call.on('status', (status) => {
14             this.emit('status', status);
15         });
16     }
17     cancel() {
18         this.call.cancelWithStatus(constants_1.Status.CANCELLED, 'Cancelled on client');
19     }
20     getPeer() {
21         return this.call.getPeer();
22     }
23 }
24 exports.ClientUnaryCallImpl = ClientUnaryCallImpl;
25 function setUpReadableStream(stream, call, deserialize) {
26     let statusEmitted = false;
27     call.on('data', (data) => {
28         let deserialized;
29         try {
30             deserialized = deserialize(data);
31         }
32         catch (e) {
33             call.cancelWithStatus(constants_1.Status.INTERNAL, 'Failed to parse server response');
34             return;
35         }
36         if (!stream.push(deserialized)) {
37             call.pause();
38         }
39     });
40     call.on('end', () => {
41         if (statusEmitted) {
42             stream.push(null);
43         }
44         else {
45             call.once('status', () => {
46                 stream.push(null);
47             });
48         }
49     });
50     call.on('status', (status) => {
51         if (status.code !== constants_1.Status.OK) {
52             const error = Object.assign(new Error(status.details), status);
53             stream.emit('error', error);
54         }
55         stream.emit('status', status);
56         statusEmitted = true;
57     });
58     call.pause();
59 }
60 class ClientReadableStreamImpl extends stream_1.Readable {
61     constructor(call, deserialize) {
62         super({ objectMode: true });
63         this.call = call;
64         this.deserialize = deserialize;
65         call.on('metadata', (metadata) => {
66             this.emit('metadata', metadata);
67         });
68         setUpReadableStream(this, call, deserialize);
69     }
70     cancel() {
71         this.call.cancelWithStatus(constants_1.Status.CANCELLED, 'Cancelled on client');
72     }
73     getPeer() {
74         return this.call.getPeer();
75     }
76     _read(_size) {
77         this.call.resume();
78     }
79 }
80 exports.ClientReadableStreamImpl = ClientReadableStreamImpl;
81 function tryWrite(call, serialize, chunk, encoding, cb) {
82     let message;
83     const flags = Number(encoding);
84     try {
85         message = serialize(chunk);
86     }
87     catch (e) {
88         call.cancelWithStatus(constants_1.Status.INTERNAL, 'Serialization failure');
89         cb(e);
90         return;
91     }
92     const writeObj = { message };
93     if (!Number.isNaN(flags)) {
94         writeObj.flags = flags;
95     }
96     call.write(writeObj, cb);
97 }
98 class ClientWritableStreamImpl extends stream_1.Writable {
99     constructor(call, serialize) {
100         super({ objectMode: true });
101         this.call = call;
102         this.serialize = serialize;
103         call.on('metadata', (metadata) => {
104             this.emit('metadata', metadata);
105         });
106         call.on('status', (status) => {
107             this.emit('status', status);
108         });
109     }
110     cancel() {
111         this.call.cancelWithStatus(constants_1.Status.CANCELLED, 'Cancelled on client');
112     }
113     getPeer() {
114         return this.call.getPeer();
115     }
116     _write(chunk, encoding, cb) {
117         tryWrite(this.call, this.serialize, chunk, encoding, cb);
118     }
119     _final(cb) {
120         this.call.end();
121         cb();
122     }
123 }
124 exports.ClientWritableStreamImpl = ClientWritableStreamImpl;
125 class ClientDuplexStreamImpl extends stream_1.Duplex {
126     constructor(call, serialize, deserialize) {
127         super({ objectMode: true });
128         this.call = call;
129         this.serialize = serialize;
130         this.deserialize = deserialize;
131         call.on('metadata', (metadata) => {
132             this.emit('metadata', metadata);
133         });
134         setUpReadableStream(this, call, deserialize);
135     }
136     cancel() {
137         this.call.cancelWithStatus(constants_1.Status.CANCELLED, 'Cancelled on client');
138     }
139     getPeer() {
140         return this.call.getPeer();
141     }
142     _read(_size) {
143         this.call.resume();
144     }
145     _write(chunk, encoding, cb) {
146         tryWrite(this.call, this.serialize, chunk, encoding, cb);
147     }
148     _final(cb) {
149         this.call.end();
150         cb();
151     }
152 }
153 exports.ClientDuplexStreamImpl = ClientDuplexStreamImpl;
154 //# sourceMappingURL=call.js.map