Built motion from commit 6a09e18b.|2.6.11
[motion2.git] / legacy-libs / grpc-cloned / node_modules / @grpc / grpc-js / build / src / client.js
1 "use strict";
2 Object.defineProperty(exports, "__esModule", { value: true });
3 const call_1 = require("./call");
4 const channel_1 = require("./channel");
5 const constants_1 = require("./constants");
6 const metadata_1 = require("./metadata");
7 // This symbol must be exported (for now).
8 // See: https://github.com/Microsoft/TypeScript/issues/20080
9 exports.kChannel = Symbol();
10 /**
11  * A generic gRPC client. Primarily useful as a base class for all generated
12  * clients.
13  */
14 class Client {
15     constructor(address, credentials, options = {}) {
16         if (options.channelOverride) {
17             this[exports.kChannel] = options.channelOverride;
18         }
19         else if (options.channelFactoryOverride) {
20             this[exports.kChannel] =
21                 options.channelFactoryOverride(address, credentials, options);
22         }
23         else {
24             this[exports.kChannel] = new channel_1.Http2Channel(address, credentials, options);
25         }
26     }
27     close() {
28         this[exports.kChannel].close();
29     }
30     getChannel() {
31         return this[exports.kChannel];
32     }
33     waitForReady(deadline, callback) {
34         const checkState = (err) => {
35             if (err) {
36                 callback(new Error('Failed to connect before the deadline'));
37                 return;
38             }
39             let newState;
40             try {
41                 newState = this[exports.kChannel].getConnectivityState(true);
42             }
43             catch (e) {
44                 callback(new Error('The channel has been closed'));
45                 return;
46             }
47             if (newState === channel_1.ConnectivityState.READY) {
48                 callback();
49             }
50             else {
51                 try {
52                     this[exports.kChannel].watchConnectivityState(newState, deadline, checkState);
53                 }
54                 catch (e) {
55                     callback(new Error('The channel has been closed'));
56                 }
57             }
58         };
59         setImmediate(checkState);
60     }
61     handleUnaryResponse(call, deserialize, callback) {
62         let responseMessage = null;
63         call.on('data', (data) => {
64             if (responseMessage != null) {
65                 call.cancelWithStatus(constants_1.Status.INTERNAL, 'Too many responses received');
66             }
67             try {
68                 responseMessage = deserialize(data);
69             }
70             catch (e) {
71                 call.cancelWithStatus(constants_1.Status.INTERNAL, 'Failed to parse server response');
72             }
73         });
74         call.on('end', () => {
75             if (responseMessage == null) {
76                 call.cancelWithStatus(constants_1.Status.INTERNAL, 'Not enough responses received');
77             }
78         });
79         call.on('status', (status) => {
80             /* We assume that call emits status after it emits end, and that it
81              * accounts for any cancelWithStatus calls up until it emits status.
82              * Therefore, considering the above event handlers, status.code should be
83              * OK if and only if we have a non-null responseMessage */
84             if (status.code === constants_1.Status.OK) {
85                 callback(null, responseMessage);
86             }
87             else {
88                 const error = Object.assign(new Error(status.details), status);
89                 callback(error);
90             }
91         });
92     }
93     checkOptionalUnaryResponseArguments(arg1, arg2, arg3) {
94         if (arg1 instanceof Function) {
95             return { metadata: new metadata_1.Metadata(), options: {}, callback: arg1 };
96         }
97         else if (arg2 instanceof Function) {
98             if (arg1 instanceof metadata_1.Metadata) {
99                 return { metadata: arg1, options: {}, callback: arg2 };
100             }
101             else {
102                 return { metadata: new metadata_1.Metadata(), options: arg1, callback: arg2 };
103             }
104         }
105         else {
106             if (!((arg1 instanceof metadata_1.Metadata) && (arg2 instanceof Object) &&
107                 (arg3 instanceof Function))) {
108                 throw new Error('Incorrect arguments passed');
109             }
110             return { metadata: arg1, options: arg2, callback: arg3 };
111         }
112     }
113     makeUnaryRequest(method, serialize, deserialize, argument, metadata, options, callback) {
114         ({ metadata, options, callback } =
115             this.checkOptionalUnaryResponseArguments(metadata, options, callback));
116         const call = this[exports.kChannel].createCall(method, options.deadline, options.host, options.parent, options.propagate_flags);
117         if (options.credentials) {
118             call.setCredentials(options.credentials);
119         }
120         const message = serialize(argument);
121         const writeObj = { message };
122         call.sendMetadata(metadata);
123         call.write(writeObj);
124         call.end();
125         this.handleUnaryResponse(call, deserialize, callback);
126         return new call_1.ClientUnaryCallImpl(call);
127     }
128     makeClientStreamRequest(method, serialize, deserialize, metadata, options, callback) {
129         ({ metadata, options, callback } =
130             this.checkOptionalUnaryResponseArguments(metadata, options, callback));
131         const call = this[exports.kChannel].createCall(method, options.deadline, options.host, options.parent, options.propagate_flags);
132         if (options.credentials) {
133             call.setCredentials(options.credentials);
134         }
135         call.sendMetadata(metadata);
136         this.handleUnaryResponse(call, deserialize, callback);
137         return new call_1.ClientWritableStreamImpl(call, serialize);
138     }
139     checkMetadataAndOptions(arg1, arg2) {
140         let metadata;
141         let options;
142         if (arg1 instanceof metadata_1.Metadata) {
143             metadata = arg1;
144             if (arg2) {
145                 options = arg2;
146             }
147             else {
148                 options = {};
149             }
150         }
151         else {
152             if (arg1) {
153                 options = arg1;
154             }
155             else {
156                 options = {};
157             }
158             metadata = new metadata_1.Metadata();
159         }
160         return { metadata, options };
161     }
162     makeServerStreamRequest(method, serialize, deserialize, argument, metadata, options) {
163         ({ metadata, options } = this.checkMetadataAndOptions(metadata, options));
164         const call = this[exports.kChannel].createCall(method, options.deadline, options.host, options.parent, options.propagate_flags);
165         if (options.credentials) {
166             call.setCredentials(options.credentials);
167         }
168         const message = serialize(argument);
169         const writeObj = { message };
170         call.sendMetadata(metadata);
171         call.write(writeObj);
172         call.end();
173         return new call_1.ClientReadableStreamImpl(call, deserialize);
174     }
175     makeBidiStreamRequest(method, serialize, deserialize, metadata, options) {
176         ({ metadata, options } = this.checkMetadataAndOptions(metadata, options));
177         const call = this[exports.kChannel].createCall(method, options.deadline, options.host, options.parent, options.propagate_flags);
178         if (options.credentials) {
179             call.setCredentials(options.credentials);
180         }
181         call.sendMetadata(metadata);
182         return new call_1.ClientDuplexStreamImpl(call, serialize, deserialize);
183     }
184 }
185 exports.Client = Client;
186 //# sourceMappingURL=client.js.map