grpc-dc 0.32.9 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "grpc-dc",
3
- "version": "0.32.9",
3
+ "version": "0.32.10",
4
4
  "description": "gRPC in the browser over WebRTC DataChannels: dependency-free generated gRPC clients & servers for JavaScript — no grpc-web proxy, no external runtime",
5
5
  "keywords": [
6
6
  "grpc",
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
- this.#endpoint.closeStream(id);
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
- sendData: (payload) => this.sendFrame(encodeFrame(FrameType.DATA, streamId, payload)),
255
- sendHeaders: (metadata = {}) => this.sendFrame(headersFrame(streamId, null, metadata)),
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
  };