UNPKG

5.07 kBJavaScriptView Raw
1"use strict";
2/*
3 * Copyright 2019 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18Object.defineProperty(exports, "__esModule", { value: true });
19exports.ClientDuplexStreamImpl = exports.ClientWritableStreamImpl = exports.ClientReadableStreamImpl = exports.ClientUnaryCallImpl = exports.callErrorFromStatus = void 0;
20const events_1 = require("events");
21const stream_1 = require("stream");
22const constants_1 = require("./constants");
23/**
24 * Construct a ServiceError from a StatusObject. This function exists primarily
25 * as an attempt to make the error stack trace clearly communicate that the
26 * error is not necessarily a problem in gRPC itself.
27 * @param status
28 */
29function callErrorFromStatus(status, callerStack) {
30 const message = `${status.code} ${constants_1.Status[status.code]}: ${status.details}`;
31 const error = new Error(message);
32 const stack = `${error.stack}\nfor call at\n${callerStack}`;
33 return Object.assign(new Error(message), status, { stack });
34}
35exports.callErrorFromStatus = callErrorFromStatus;
36class ClientUnaryCallImpl extends events_1.EventEmitter {
37 constructor() {
38 super();
39 }
40 cancel() {
41 var _a;
42 (_a = this.call) === null || _a === void 0 ? void 0 : _a.cancelWithStatus(constants_1.Status.CANCELLED, 'Cancelled on client');
43 }
44 getPeer() {
45 var _a, _b;
46 return (_b = (_a = this.call) === null || _a === void 0 ? void 0 : _a.getPeer()) !== null && _b !== void 0 ? _b : 'unknown';
47 }
48}
49exports.ClientUnaryCallImpl = ClientUnaryCallImpl;
50class ClientReadableStreamImpl extends stream_1.Readable {
51 constructor(deserialize) {
52 super({ objectMode: true });
53 this.deserialize = deserialize;
54 }
55 cancel() {
56 var _a;
57 (_a = this.call) === null || _a === void 0 ? void 0 : _a.cancelWithStatus(constants_1.Status.CANCELLED, 'Cancelled on client');
58 }
59 getPeer() {
60 var _a, _b;
61 return (_b = (_a = this.call) === null || _a === void 0 ? void 0 : _a.getPeer()) !== null && _b !== void 0 ? _b : 'unknown';
62 }
63 _read(_size) {
64 var _a;
65 (_a = this.call) === null || _a === void 0 ? void 0 : _a.startRead();
66 }
67}
68exports.ClientReadableStreamImpl = ClientReadableStreamImpl;
69class ClientWritableStreamImpl extends stream_1.Writable {
70 constructor(serialize) {
71 super({ objectMode: true });
72 this.serialize = serialize;
73 }
74 cancel() {
75 var _a;
76 (_a = this.call) === null || _a === void 0 ? void 0 : _a.cancelWithStatus(constants_1.Status.CANCELLED, 'Cancelled on client');
77 }
78 getPeer() {
79 var _a, _b;
80 return (_b = (_a = this.call) === null || _a === void 0 ? void 0 : _a.getPeer()) !== null && _b !== void 0 ? _b : 'unknown';
81 }
82 _write(chunk, encoding, cb) {
83 var _a;
84 const context = {
85 callback: cb,
86 };
87 const flags = Number(encoding);
88 if (!Number.isNaN(flags)) {
89 context.flags = flags;
90 }
91 (_a = this.call) === null || _a === void 0 ? void 0 : _a.sendMessageWithContext(context, chunk);
92 }
93 _final(cb) {
94 var _a;
95 (_a = this.call) === null || _a === void 0 ? void 0 : _a.halfClose();
96 cb();
97 }
98}
99exports.ClientWritableStreamImpl = ClientWritableStreamImpl;
100class ClientDuplexStreamImpl extends stream_1.Duplex {
101 constructor(serialize, deserialize) {
102 super({ objectMode: true });
103 this.serialize = serialize;
104 this.deserialize = deserialize;
105 }
106 cancel() {
107 var _a;
108 (_a = this.call) === null || _a === void 0 ? void 0 : _a.cancelWithStatus(constants_1.Status.CANCELLED, 'Cancelled on client');
109 }
110 getPeer() {
111 var _a, _b;
112 return (_b = (_a = this.call) === null || _a === void 0 ? void 0 : _a.getPeer()) !== null && _b !== void 0 ? _b : 'unknown';
113 }
114 _read(_size) {
115 var _a;
116 (_a = this.call) === null || _a === void 0 ? void 0 : _a.startRead();
117 }
118 _write(chunk, encoding, cb) {
119 var _a;
120 const context = {
121 callback: cb,
122 };
123 const flags = Number(encoding);
124 if (!Number.isNaN(flags)) {
125 context.flags = flags;
126 }
127 (_a = this.call) === null || _a === void 0 ? void 0 : _a.sendMessageWithContext(context, chunk);
128 }
129 _final(cb) {
130 var _a;
131 (_a = this.call) === null || _a === void 0 ? void 0 : _a.halfClose();
132 cb();
133 }
134}
135exports.ClientDuplexStreamImpl = ClientDuplexStreamImpl;
136//# sourceMappingURL=call.js.map
\No newline at end of file