grpc-dc 0.32.8 → 0.32.9
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/http2.js +32 -4
package/package.json
CHANGED
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) {
|