grpc-dc 0.32.8 → 0.32.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/channel.js +8 -1
- package/src/endpoint.js +15 -2
- package/src/http2.js +32 -4
package/package.json
CHANGED
package/src/channel.js
CHANGED
|
@@ -152,6 +152,7 @@ export class Channel {
|
|
|
152
152
|
|
|
153
153
|
async *#responses(id, input, RespType, deadline = null, hooks = {}) {
|
|
154
154
|
const iterator = input[Symbol.asyncIterator]();
|
|
155
|
+
let completed = false;
|
|
155
156
|
try {
|
|
156
157
|
for (;;) {
|
|
157
158
|
const step = await this.#next(id, iterator, deadline);
|
|
@@ -162,11 +163,13 @@ export class Channel {
|
|
|
162
163
|
} else if (event.kind === "data") {
|
|
163
164
|
yield RespType.decode(event.payload);
|
|
164
165
|
} else if (event.kind === "trailers") {
|
|
166
|
+
completed = true;
|
|
165
167
|
hooks.onTrailers?.(event.trailers ?? {});
|
|
166
168
|
this.#endTrace(id, StatusName[event.status.code] ?? `CODE_${event.status.code}`);
|
|
167
169
|
if (event.status.code !== 0) throw event.status;
|
|
168
170
|
return;
|
|
169
171
|
} else if (event.kind === "rst") {
|
|
172
|
+
completed = true;
|
|
170
173
|
this.#endTrace(id, StatusName[event.status.code] ?? `CODE_${event.status.code}`);
|
|
171
174
|
throw event.status;
|
|
172
175
|
}
|
|
@@ -174,7 +177,11 @@ export class Channel {
|
|
|
174
177
|
throw StatusError.unavailable("data channel closed");
|
|
175
178
|
} finally {
|
|
176
179
|
this.#endTrace(id, "UNAVAILABLE");
|
|
177
|
-
|
|
180
|
+
// Consumer stopped early (broke out of iteration): cancel
|
|
181
|
+
// so the server stops producing instead of streaming into
|
|
182
|
+
// the void. A completed call just closes locally.
|
|
183
|
+
if (completed) this.#endpoint.closeStream(id);
|
|
184
|
+
else this.#endpoint.cancelStream(id);
|
|
178
185
|
}
|
|
179
186
|
}
|
|
180
187
|
|
package/src/endpoint.js
CHANGED
|
@@ -251,8 +251,21 @@ export class Endpoint {
|
|
|
251
251
|
const input = new StreamInput();
|
|
252
252
|
this.#streams.set(streamId, input);
|
|
253
253
|
const sink = {
|
|
254
|
-
|
|
255
|
-
|
|
254
|
+
// A stream dropped from the map was RST by the peer (or
|
|
255
|
+
// the channel went away): fail the send so a producing
|
|
256
|
+
// handler stops instead of streaming into the void.
|
|
257
|
+
sendData: (payload) => {
|
|
258
|
+
if (!this.#streams.has(streamId)) {
|
|
259
|
+
throw new StatusError(StatusCode.CANCELLED, "call cancelled");
|
|
260
|
+
}
|
|
261
|
+
return this.sendFrame(encodeFrame(FrameType.DATA, streamId, payload));
|
|
262
|
+
},
|
|
263
|
+
sendHeaders: (metadata = {}) => {
|
|
264
|
+
if (!this.#streams.has(streamId)) {
|
|
265
|
+
throw new StatusError(StatusCode.CANCELLED, "call cancelled");
|
|
266
|
+
}
|
|
267
|
+
return this.sendFrame(headersFrame(streamId, null, metadata));
|
|
268
|
+
},
|
|
256
269
|
sendTrailers: (status, trailers) =>
|
|
257
270
|
this.sendFrame(statusFrame(FrameType.TRAILERS, streamId, status, trailers)),
|
|
258
271
|
};
|
package/src/http2.js
CHANGED
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
// using WebRTC DataChannels.
|
|
12
12
|
|
|
13
13
|
import http2 from "node:http2";
|
|
14
|
+
import zlib from "node:zlib";
|
|
14
15
|
import { decodeFrame, encodeFrame, FrameType } from "./codec.js";
|
|
15
16
|
import { parseHeaders } from "./codec.js";
|
|
16
17
|
import { Channel } from "./channel.js";
|
|
@@ -19,6 +20,13 @@ export class Http2DataChannel {
|
|
|
19
20
|
#listeners = new Map();
|
|
20
21
|
#streams = new Map();
|
|
21
22
|
#buffers = new Map();
|
|
23
|
+
// Per-stream gzip state: requests compress when the call
|
|
24
|
+
// metadata carries grpc-encoding: gzip; responses decompress
|
|
25
|
+
// when the response headers do. Every stream advertises
|
|
26
|
+
// gzip in grpc-accept-encoding, and per-message compression
|
|
27
|
+
// still follows the LPM compressed flag.
|
|
28
|
+
#compressOut = new Map();
|
|
29
|
+
#decompressIn = new Map();
|
|
22
30
|
|
|
23
31
|
constructor(authority, { secure = false, ...connectOptions } = {}) {
|
|
24
32
|
this.readyState = "open";
|
|
@@ -66,11 +74,14 @@ export class Http2DataChannel {
|
|
|
66
74
|
":method": "POST",
|
|
67
75
|
"content-type": "application/grpc",
|
|
68
76
|
te: "trailers",
|
|
77
|
+
"grpc-accept-encoding": "gzip,identity",
|
|
69
78
|
...metadata,
|
|
70
79
|
});
|
|
71
80
|
this.#streams.set(id, stream);
|
|
72
81
|
this.#buffers.set(id, Buffer.alloc(0));
|
|
82
|
+
this.#compressOut.set(id, metadata["grpc-encoding"] === "gzip");
|
|
73
83
|
stream.on("response", (headers, flags) => {
|
|
84
|
+
this.#decompressIn.set(id, headers["grpc-encoding"] === "gzip");
|
|
74
85
|
const trailersOnly =
|
|
75
86
|
(flags & http2.constants.NGHTTP2_FLAG_END_STREAM) !== 0 && headers["grpc-status"] !== undefined;
|
|
76
87
|
if (trailersOnly) {
|
|
@@ -95,7 +106,17 @@ export class Http2DataChannel {
|
|
|
95
106
|
while (buf.length >= 5) {
|
|
96
107
|
const len = buf.readUInt32BE(1);
|
|
97
108
|
if (buf.length < 5 + len) break;
|
|
98
|
-
|
|
109
|
+
let payload = buf.subarray(5, 5 + len);
|
|
110
|
+
if (buf[0] === 1 && this.#decompressIn.get(id)) {
|
|
111
|
+
try {
|
|
112
|
+
payload = zlib.gunzipSync(payload);
|
|
113
|
+
} catch {
|
|
114
|
+
stream.close(http2.constants.NGHTTP2_CANCEL);
|
|
115
|
+
this.#emitFrame(FrameType.RST, id, JSON.stringify({ code: 3, message: "gzip decompression failed" }));
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
this.#emit(encodeFrame(FrameType.DATA, id, new Uint8Array(payload)));
|
|
99
120
|
buf = buf.subarray(5 + len);
|
|
100
121
|
}
|
|
101
122
|
this.#buffers.set(id, buf);
|
|
@@ -106,9 +127,16 @@ export class Http2DataChannel {
|
|
|
106
127
|
} else if (frame.type === FrameType.DATA) {
|
|
107
128
|
const stream = this.#streams.get(id);
|
|
108
129
|
if (stream) {
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
130
|
+
let payload = frame.payload;
|
|
131
|
+
let flag = 0;
|
|
132
|
+
if (this.#compressOut.get(id)) {
|
|
133
|
+
payload = zlib.gzipSync(payload);
|
|
134
|
+
flag = 1;
|
|
135
|
+
}
|
|
136
|
+
const lpm = Buffer.alloc(5 + payload.length);
|
|
137
|
+
lpm[0] = flag;
|
|
138
|
+
lpm.writeUInt32BE(payload.length, 1);
|
|
139
|
+
Buffer.from(payload).copy(lpm, 5);
|
|
112
140
|
stream.write(lpm);
|
|
113
141
|
}
|
|
114
142
|
} else if (frame.type === FrameType.HALF_CLOSE) {
|