1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | Object.defineProperty(exports, "__esModule", { value: true });
|
19 | exports.ClientDuplexStreamImpl = exports.ClientWritableStreamImpl = exports.ClientReadableStreamImpl = exports.ClientUnaryCallImpl = exports.callErrorFromStatus = void 0;
|
20 | const events_1 = require("events");
|
21 | const stream_1 = require("stream");
|
22 | const constants_1 = require("./constants");
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | function 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 | }
|
35 | exports.callErrorFromStatus = callErrorFromStatus;
|
36 | class 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 | }
|
49 | exports.ClientUnaryCallImpl = ClientUnaryCallImpl;
|
50 | class 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 | }
|
68 | exports.ClientReadableStreamImpl = ClientReadableStreamImpl;
|
69 | class 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 | }
|
99 | exports.ClientWritableStreamImpl = ClientWritableStreamImpl;
|
100 | class 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 | }
|
135 | exports.ClientDuplexStreamImpl = ClientDuplexStreamImpl;
|
136 |
|
\ | No newline at end of file |