grpc-dc 0.45.0 → 0.46.1

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/src/channel.js CHANGED
@@ -19,6 +19,12 @@ import {
19
19
  traceStart,
20
20
  } from "./codec.js";
21
21
  import { Endpoint } from "./endpoint.js";
22
+ import {
23
+ STRUCT_CODEC_HEADER,
24
+ STRUCT_CODEC_V1,
25
+ marshalForCodec,
26
+ unmarshalForCodec,
27
+ } from "./structdc.js";
22
28
 
23
29
  // AsyncIterable wrapper whose iterator keeps ONE read in
24
30
  // flight at a time (no read-ahead). for-await on an async
@@ -91,6 +97,23 @@ export class Channel {
91
97
  #traces = new Map();
92
98
  #traceContext = null;
93
99
  #metadataProvider = null;
100
+ #structCodec = false;
101
+
102
+ /**
103
+ * Make every call on this channel request the struct codec
104
+ * (HEADERS marker dc-codec: struct-v1) and encode requests /
105
+ * decode responses with it. The server must enable it too
106
+ * (see Server.setStructCodec); otherwise calls fail
107
+ * UNIMPLEMENTED. Mirrors the Go Channel.SetStructCodec.
108
+ */
109
+ setStructCodec(enabled) {
110
+ this.#structCodec = !!enabled;
111
+ }
112
+
113
+ /** Whether this channel requests the struct codec on every call. */
114
+ get structCodec() {
115
+ return this.#structCodec;
116
+ }
94
117
 
95
118
  // A Channel view sharing this endpoint whose outbound spans are
96
119
  // parented to traceContext (a server span context from
@@ -101,6 +124,7 @@ export class Channel {
101
124
  const ch = new Channel(this.#endpoint);
102
125
  ch.#traceContext = traceContext ?? null;
103
126
  ch.#metadataProvider = this.#metadataProvider;
127
+ ch.#structCodec = this.#structCodec;
104
128
  return ch;
105
129
  }
106
130
 
@@ -113,6 +137,7 @@ export class Channel {
113
137
  const ch = new Channel(this.#endpoint);
114
138
  ch.#traceContext = this.#traceContext;
115
139
  ch.#metadataProvider = provider ?? null;
140
+ ch.#structCodec = this.#structCodec;
116
141
  return ch;
117
142
  }
118
143
 
@@ -139,6 +164,9 @@ export class Channel {
139
164
  deadline = Date.now() + timeout;
140
165
  metadata = { ...metadata, "grpc-timeout": encodeTimeout(timeout) };
141
166
  }
167
+ if (this.#structCodec) {
168
+ metadata = { ...metadata, [STRUCT_CODEC_HEADER]: STRUCT_CODEC_V1 };
169
+ }
142
170
  let traceCtx = null;
143
171
  if (getTracer() !== null) {
144
172
  traceCtx = traceStart(
@@ -214,7 +242,9 @@ export class Channel {
214
242
  }
215
243
 
216
244
  async #sendMessage(id, ReqType, request) {
217
- await this.#endpoint.sendFrame(encodeFrame(FrameType.DATA, id, ReqType.encode(request)));
245
+ await this.#endpoint.sendFrame(
246
+ encodeFrame(FrameType.DATA, id, marshalForCodec(this.#structCodec, ReqType, request)),
247
+ );
218
248
  }
219
249
 
220
250
  // hooks: optional {onHeaders, onTrailers} from call options,
@@ -235,7 +265,7 @@ export class Channel {
235
265
  if (response !== null) {
236
266
  throw StatusError.internal("unary call received more than one response");
237
267
  }
238
- response = RespType.decode(event.payload);
268
+ response = unmarshalForCodec(this.#structCodec, RespType, event.payload);
239
269
  } else if (event.kind === "trailers") {
240
270
  hooks.onTrailers?.(event.trailers ?? {});
241
271
  this.#endTrace(id, StatusName[event.status.code] ?? `CODE_${event.status.code}`);
@@ -296,7 +326,7 @@ export class Channel {
296
326
  if (event.kind === "headers") {
297
327
  hooks.onHeaders?.(event.headers?.metadata ?? {});
298
328
  } else if (event.kind === "data") {
299
- yield RespType.decode(event.payload);
329
+ yield unmarshalForCodec(this.#structCodec, RespType, event.payload);
300
330
  } else if (event.kind === "trailers") {
301
331
  completed = true;
302
332
  hooks.onTrailers?.(event.trailers ?? {});
package/src/dynamic.js CHANGED
@@ -282,7 +282,15 @@ function parseMessage(pkg, outer, data, fileModel) {
282
282
  const fields = [...new PbReader(data).fields()];
283
283
  const name = pbString(fields, 1);
284
284
  const full = (pkg ? pkg + "." : "") + (outer ? outer + "." : "") + name;
285
- const model = { fullName: full, fields: new Map() };
285
+ const model = { fullName: full, fields: new Map(), mapEntry: false };
286
+ for (const [fnum, , fval] of fields) {
287
+ if (fnum === 7) {
288
+ // MessageOptions.map_entry (field 7) marks map entries.
289
+ for (const [onum, , oval] of new PbReader(fval).fields()) {
290
+ if (onum === 7 && oval !== 0n) model.mapEntry = true;
291
+ }
292
+ }
293
+ }
286
294
  fileModel.messages.set("." + full, model);
287
295
  for (const [fnum, , fval] of fields) {
288
296
  if (fnum === 2) {
package/src/server.js CHANGED
@@ -13,11 +13,17 @@ import {
13
13
  traceStart,
14
14
  } from "./codec.js";
15
15
  import { Endpoint } from "./endpoint.js";
16
+ import {
17
+ STRUCT_CODEC_HEADER,
18
+ STRUCT_CODEC_V1,
19
+ marshalForCodec,
20
+ unmarshalForCodec,
21
+ } from "./structdc.js";
16
22
 
17
- async function* typedRequests(input, ReqType) {
23
+ async function* typedRequests(input, ReqType, structCodec = false) {
18
24
  for await (const event of input) {
19
25
  if (event.kind === "data") {
20
- yield ReqType.decode(event.payload);
26
+ yield unmarshalForCodec(structCodec, ReqType, event.payload);
21
27
  } else if (event.kind === "half-close") {
22
28
  return;
23
29
  } else if (event.kind === "trailers" || event.kind === "rst") {
@@ -40,6 +46,18 @@ export class Server {
40
46
  #authVerifier = null;
41
47
  #authScopes = {};
42
48
  #interceptors = [];
49
+ #structCodec = false;
50
+
51
+ /**
52
+ * Enable the struct codec on this server: a call carrying
53
+ * HEADERS dc-codec: struct-v1 is decoded with the struct codec
54
+ * and answered in kind. A struct-v1 request to a server without
55
+ * it enabled fails UNIMPLEMENTED, as does any other dc-codec
56
+ * value. Mirrors the Go Server.SetStructCodec.
57
+ */
58
+ setStructCodec(enabled) {
59
+ this.#structCodec = !!enabled;
60
+ }
43
61
 
44
62
  addService(definition) {
45
63
  for (const [name, method] of Object.entries(definition.methods)) {
@@ -143,6 +161,16 @@ export class Server {
143
161
  claims = extra;
144
162
  }
145
163
  }
164
+ // Struct codec negotiation: an explicit dc-codec marker
165
+ // must be struct-v1 and enabled here, else UNIMPLEMENTED.
166
+ let useStruct = false;
167
+ const codecMarker = metadata[STRUCT_CODEC_HEADER];
168
+ if (codecMarker !== undefined) {
169
+ if (codecMarker !== STRUCT_CODEC_V1 || !this.#structCodec) {
170
+ throw StatusError.unimplemented("struct codec not enabled");
171
+ }
172
+ useStruct = true;
173
+ }
146
174
  const timeoutMs = parseTimeout(metadata["grpc-timeout"]);
147
175
  const deadline = timeoutMs === null ? null : Date.now() + timeoutMs;
148
176
  // Initial/trailing metadata the handler accumulates via
@@ -171,6 +199,8 @@ export class Server {
171
199
  path,
172
200
  metadata,
173
201
  claims,
202
+ structCodec: useStruct,
203
+ encodeMessage: (resp) => marshalForCodec(useStruct, method.responseType, resp),
174
204
  service: method.service,
175
205
  method: method.method,
176
206
  deadline,
@@ -200,8 +230,8 @@ export class Server {
200
230
  }
201
231
 
202
232
  async #dispatchTraced(path, method, ctx, input, sink) {
203
- const requests = typedRequests(input, method.requestType);
204
- const encode = (resp) => method.responseType.encode(resp);
233
+ const requests = typedRequests(input, method.requestType, ctx.structCodec);
234
+ const encode = (resp) => ctx.encodeMessage(resp);
205
235
  switch (method.kind) {
206
236
  case "unary": {
207
237
  const req = await firstRequest(requests);