@salesforce/jsonrpc 10.11.1 → 10.12.0

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/dist/index.js CHANGED
@@ -1,41 +1,41 @@
1
- function o(s) {
2
- return typeof s == "object" && s !== null && !Array.isArray(s);
1
+ function o(e) {
2
+ return typeof e == "object" && e !== null && !Array.isArray(e);
3
3
  }
4
- function c(s) {
5
- return typeof s.id == "number" && Number.isInteger(s.id) && s.id >= 0;
4
+ function c(e) {
5
+ return typeof e.id == "number" && Number.isInteger(e.id) && e.id >= 0;
6
6
  }
7
- function i(s) {
8
- return o(s) && s.jsonrpc === "2.0";
7
+ function i(e) {
8
+ return o(e) && e.jsonrpc === "2.0";
9
9
  }
10
- function m(s) {
11
- if (!i(s)) return !1;
12
- const t = s;
10
+ function m(e) {
11
+ if (!i(e)) return !1;
12
+ const t = e;
13
13
  return c(t) && typeof t.method == "string";
14
14
  }
15
- function d(s) {
16
- if (!i(s)) return !1;
17
- const t = s;
15
+ function d(e) {
16
+ if (!i(e)) return !1;
17
+ const t = e;
18
18
  return !("id" in t) && typeof t.method == "string";
19
19
  }
20
- function p(s) {
21
- if (!i(s)) return !1;
22
- const t = s;
20
+ function p(e) {
21
+ if (!i(e)) return !1;
22
+ const t = e;
23
23
  return c(t) && "result" in t && !("error" in t) && !("method" in t);
24
24
  }
25
- function f(s) {
26
- if (!i(s)) return !1;
27
- const t = s;
25
+ function u(e) {
26
+ if (!i(e)) return !1;
27
+ const t = e;
28
28
  if (!c(t) || "result" in t || "method" in t) return !1;
29
- const e = t.error;
30
- return o(e) && typeof e.code == "number";
29
+ const s = t.error;
30
+ return o(s) && typeof s.code == "number";
31
31
  }
32
- function g(s) {
33
- return p(s) || f(s);
32
+ function g(e) {
33
+ return p(e) || u(e);
34
34
  }
35
- function y(s) {
36
- return o(s) && typeof s.code == "number";
35
+ function M(e) {
36
+ return o(e) && typeof e.code == "number";
37
37
  }
38
- class M {
38
+ class b {
39
39
  nextRequestId = 1;
40
40
  pending = /* @__PURE__ */ new Map();
41
41
  notificationHandlers = /* @__PURE__ */ new Map();
@@ -64,8 +64,8 @@ class M {
64
64
  * this.handleHostContextChanged(params);
65
65
  * });
66
66
  */
67
- registerNotificationHandler(t, e) {
68
- this.notificationHandlers.has(t) || this.notificationHandlers.set(t, /* @__PURE__ */ new Set()), this.notificationHandlers.get(t).add(e);
67
+ registerNotificationHandler(t, s) {
68
+ this.notificationHandlers.has(t) || this.notificationHandlers.set(t, /* @__PURE__ */ new Set()), this.notificationHandlers.get(t).add(s);
69
69
  }
70
70
  /**
71
71
  * Handle inbound JSON-RPC messages from the transport.
@@ -76,15 +76,21 @@ class M {
76
76
  */
77
77
  onMessage = (t) => {
78
78
  if (g(t)) {
79
- const e = this.pending.get(t.id);
80
- if (!e)
79
+ const s = this.pending.get(t.id);
80
+ if (!s)
81
81
  return;
82
- this.pending.delete(t.id), f(t) ? e.reject(new Error(t.error.message || "Request failed")) : e.resolve(t.result);
82
+ this.pending.delete(t.id), this.onInboundMeta(t, t._meta), u(t) ? s.reject(new Error(t.error.message || "Request failed")) : s.resolve(t.result);
83
83
  return;
84
84
  }
85
85
  if (d(t)) {
86
- const e = this.notificationHandlers.get(t.method);
87
- e && e.forEach((r) => r(t.params));
86
+ this.onInboundMeta(t, t._meta);
87
+ const s = this.notificationHandlers.get(t.method);
88
+ if (s) {
89
+ const n = t._meta;
90
+ s.forEach((r) => {
91
+ r(t.params, n);
92
+ });
93
+ }
88
94
  }
89
95
  };
90
96
  /**
@@ -100,18 +106,18 @@ class M {
100
106
  * content: { type: "text", text: "Hello" }
101
107
  * });
102
108
  */
103
- request(t, e) {
104
- const r = this.nextRequestId++, n = {
109
+ request(t, s) {
110
+ const n = this.nextRequestId++, r = {
105
111
  jsonrpc: "2.0",
106
- id: r,
112
+ id: n,
107
113
  method: t,
108
- params: e
114
+ params: s
109
115
  };
110
- return new Promise((h, u) => {
111
- this.pending.set(r, {
112
- resolve: h,
113
- reject: u
114
- }), this.transport.post(n);
116
+ return this.applyOutboundMeta(r, t), new Promise((f, h) => {
117
+ this.pending.set(n, {
118
+ resolve: f,
119
+ reject: h
120
+ }), this.transport.post(r);
115
121
  });
116
122
  }
117
123
  /**
@@ -137,16 +143,38 @@ class M {
137
143
  * height: 600
138
144
  * });
139
145
  */
140
- sendNotification(t, e) {
141
- const r = {
146
+ sendNotification(t, s) {
147
+ const n = {
142
148
  jsonrpc: "2.0",
143
149
  method: t,
144
- params: e
150
+ params: s
145
151
  };
146
- this.transport.post(r);
152
+ this.applyOutboundMeta(n, t), this.transport.post(n);
153
+ }
154
+ /**
155
+ * Outbound `_meta` hook. Override to attach protocol-meta (e.g. a
156
+ * per-message `traceId`) to every outgoing request and notification.
157
+ *
158
+ * Default returns `undefined` so the envelope ships without `_meta`,
159
+ * matching JSON-RPC 2.0 baseline behavior.
160
+ */
161
+ getOutboundMeta(t) {
162
+ }
163
+ /**
164
+ * Inbound `_meta` hook. Invoked once per validated inbound frame
165
+ * (response or notification) before the value is dispatched to the
166
+ * pending-request resolver or notification handlers.
167
+ *
168
+ * Default is a no-op. Override to feed log correlation, tracing, etc.
169
+ */
170
+ onInboundMeta(t, s) {
171
+ }
172
+ applyOutboundMeta(t, s) {
173
+ const n = this.getOutboundMeta(s);
174
+ n !== void 0 && (t._meta = n);
147
175
  }
148
176
  }
149
- class S {
177
+ class y {
150
178
  targetOrigin;
151
179
  /**
152
180
  * @param targetOrigin - origin passed as the second argument to
@@ -159,21 +187,21 @@ class S {
159
187
  window.parent?.postMessage(t, this.targetOrigin);
160
188
  }
161
189
  onMessage(t) {
162
- const e = (r) => {
163
- t(r.data);
190
+ const s = (n) => {
191
+ t(n.data);
164
192
  };
165
- return window.addEventListener("message", e), () => window.removeEventListener("message", e);
193
+ return window.addEventListener("message", s), () => window.removeEventListener("message", s);
166
194
  }
167
195
  }
168
196
  function R() {
169
197
  }
170
- class T {
198
+ class O {
171
199
  #t;
172
- #e = /* @__PURE__ */ new Set();
173
- #n = !1;
200
+ #s = /* @__PURE__ */ new Set();
174
201
  #r = !1;
202
+ #n = !1;
175
203
  /** Messages queued before `port.start()` has been called. Flushed on first subscribe. */
176
- #s = [];
204
+ #e = [];
177
205
  constructor(t) {
178
206
  this.#t = t;
179
207
  }
@@ -186,9 +214,9 @@ class T {
186
214
  * flushed on first subscription.
187
215
  */
188
216
  post(t) {
189
- if (!this.#r) {
190
- if (!this.#n) {
191
- this.#s.push(t);
217
+ if (!this.#n) {
218
+ if (!this.#r) {
219
+ this.#e.push(t);
192
220
  return;
193
221
  }
194
222
  try {
@@ -208,12 +236,12 @@ class T {
208
236
  * Returns an unsubscribe function. Calling it is idempotent.
209
237
  */
210
238
  onMessage(t) {
211
- if (this.#r)
239
+ if (this.#n)
212
240
  return R;
213
- this.#e.add(t), this.#n || this.#o();
214
- let e = !1;
241
+ this.#s.add(t), this.#r || this.#o();
242
+ let s = !1;
215
243
  return () => {
216
- e || (e = !0, this.#e.delete(t));
244
+ s || (s = !0, this.#s.delete(t));
217
245
  };
218
246
  }
219
247
  /**
@@ -225,13 +253,13 @@ class T {
225
253
  * - subsequent `onMessage` calls return a no-op unsubscribe
226
254
  */
227
255
  dispose() {
228
- if (!this.#r) {
229
- this.#r = !0;
256
+ if (!this.#n) {
257
+ this.#n = !0;
230
258
  try {
231
259
  this.#t.removeEventListener("message", this.#i);
232
260
  } catch {
233
261
  }
234
- this.#e.clear(), this.#s.length = 0;
262
+ this.#s.clear(), this.#e.length = 0;
235
263
  try {
236
264
  this.#t.close();
237
265
  } catch {
@@ -239,26 +267,26 @@ class T {
239
267
  }
240
268
  }
241
269
  #o() {
242
- this.#n = !0, this.#t.addEventListener("message", this.#i);
270
+ this.#r = !0, this.#t.addEventListener("message", this.#i);
243
271
  try {
244
272
  this.#t.start();
245
273
  } catch {
246
274
  }
247
- if (this.#s.length > 0) {
248
- const t = this.#s;
249
- this.#s = [];
250
- for (const e of t)
275
+ if (this.#e.length > 0) {
276
+ const t = this.#e;
277
+ this.#e = [];
278
+ for (const s of t)
251
279
  try {
252
- this.#t.postMessage(e);
280
+ this.#t.postMessage(s);
253
281
  } catch {
254
282
  }
255
283
  }
256
284
  }
257
285
  #i = (t) => {
258
- const e = [...this.#e];
259
- for (const r of e)
286
+ const s = [...this.#s];
287
+ for (const n of s)
260
288
  try {
261
- r(t.data);
289
+ n(t.data);
262
290
  } catch {
263
291
  }
264
292
  };
@@ -281,30 +309,30 @@ const a = {
281
309
  BACKPRESSURE: -32019,
282
310
  /** Terminal response to a $/cancel-ed request. */
283
311
  REQUEST_CANCELLED: -32024
284
- }, E = /* @__PURE__ */ new Set([
312
+ }, l = /* @__PURE__ */ new Set([
285
313
  a.REQUEST_TIMEOUT,
286
314
  a.BACKPRESSURE
287
315
  ]);
288
- function l(s) {
289
- return !!E.has(s);
316
+ function E(e) {
317
+ return !!l.has(e);
290
318
  }
291
- function b(s, t = {}) {
292
- const r = { retryable: t.retryable ?? l(s) };
293
- t.details !== void 0 && (r.details = t.details);
294
- const n = { code: s };
295
- return t.message !== void 0 && (n.message = t.message), n.data = r, n;
319
+ function I(e, t = {}) {
320
+ const n = { retryable: t.retryable ?? E(e) };
321
+ t.details !== void 0 && (n.details = t.details);
322
+ const r = { code: e };
323
+ return t.message !== void 0 && (r.message = t.message), r.data = n, r;
296
324
  }
297
325
  export {
298
- M as JsonRpcClient,
299
- T as MessageChannelTransport,
326
+ b as JsonRpcClient,
327
+ O as MessageChannelTransport,
300
328
  a as StandardErrorCode,
301
- S as WindowPostMessageTransport,
329
+ y as WindowPostMessageTransport,
302
330
  i as isJsonRpcBase,
303
- y as isJsonRpcError,
304
- f as isJsonRpcErrorResponse,
331
+ M as isJsonRpcError,
332
+ u as isJsonRpcErrorResponse,
305
333
  d as isJsonRpcNotification,
306
334
  m as isJsonRpcRequest,
307
335
  g as isJsonRpcResponse,
308
336
  p as isJsonRpcSuccessResponse,
309
- b as makeJsonRpcError
337
+ I as makeJsonRpcError
310
338
  };
@@ -1,3 +1,4 @@
1
+ import { JsonRpcFrame, JsonRpcMeta } from './jsonrpc';
1
2
  import { Transport } from './transport';
2
3
  /**
3
4
  * Base class for JSON-RPC 2.0 clients.
@@ -61,7 +62,7 @@ export declare class JsonRpcClient {
61
62
  * this.handleHostContextChanged(params);
62
63
  * });
63
64
  */
64
- protected registerNotificationHandler(method: string, handler: (params: unknown) => void): void;
65
+ protected registerNotificationHandler(method: string, handler: (params: unknown, meta?: JsonRpcMeta) => void): void;
65
66
  /**
66
67
  * Handle inbound JSON-RPC messages from the transport.
67
68
  *
@@ -108,5 +109,22 @@ export declare class JsonRpcClient {
108
109
  * });
109
110
  */
110
111
  protected sendNotification<TParams = unknown>(method: string, params?: TParams): void;
112
+ /**
113
+ * Outbound `_meta` hook. Override to attach protocol-meta (e.g. a
114
+ * per-message `traceId`) to every outgoing request and notification.
115
+ *
116
+ * Default returns `undefined` so the envelope ships without `_meta`,
117
+ * matching JSON-RPC 2.0 baseline behavior.
118
+ */
119
+ protected getOutboundMeta(_method: string): JsonRpcMeta | undefined;
120
+ /**
121
+ * Inbound `_meta` hook. Invoked once per validated inbound frame
122
+ * (response or notification) before the value is dispatched to the
123
+ * pending-request resolver or notification handlers.
124
+ *
125
+ * Default is a no-op. Override to feed log correlation, tracing, etc.
126
+ */
127
+ protected onInboundMeta(_frame: JsonRpcFrame, _meta: JsonRpcMeta | undefined): void;
128
+ private applyOutboundMeta;
111
129
  }
112
130
  //# sourceMappingURL=jsonrpc-client.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"jsonrpc-client.d.ts","sourceRoot":"","sources":["../src/jsonrpc-client.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAE7C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,qBAAa,aAAa;IACzB,OAAO,CAAC,aAAa,CAAK;IAC1B,OAAO,CAAC,OAAO,CAAuC;IACtD,OAAO,CAAC,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AI,CAAC,EAAE,WAAW,KAAK,IAAI,GACpD,IAAI;IAOP;;;;;;OAMG;IACH,OAAO,CAAC,SAAS,CAgCf;IAEF;;;;;;;;;;;;OAYG;IACH,SAAS,CAAC,OAAO,CAAC,OAAO,GAAG,OAAO,EAAE,OAAO,GAAG,OAAO,EACrD,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,OAAO,GACb,OAAO,CAAC,OAAO,CAAC;IAmBnB;;;;;;;;;;;;;;;;;;;;;;OAsBG;IACH,SAAS,CAAC,gBAAgB,CAAC,OAAO,GAAG,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,OAAO,GAAG,IAAI;IAUrF;;;;;;OAMG;IACH,SAAS,CAAC,eAAe,CAAC,OAAO,EAAE,MAAM,GAAG,WAAW,GAAG,SAAS;IAInE;;;;;;OAMG;IACH,SAAS,CAAC,aAAa,CAAC,MAAM,EAAE,YAAY,EAAE,KAAK,EAAE,WAAW,GAAG,SAAS,GAAG,IAAI;IAInF,OAAO,CAAC,iBAAiB;CAMzB"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@salesforce/jsonrpc",
3
- "version": "10.11.1",
3
+ "version": "10.12.0",
4
4
  "description": "Shareable JSON-RPC 2.0 primitives for cross-realm communication: envelope types and runtime guards, the request/response engine (JsonRpcClient), and transport adapters.",
5
5
  "license": "SEE LICENSE IN LICENSE.txt",
6
6
  "type": "module",