@bikefinder/sdk 0.34.0 → 0.36.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
@@ -81,9 +81,13 @@ import {
81
81
  trackerLookupInput,
82
82
  trackerMemberAddInput,
83
83
  trackerMemberInput,
84
+ trackerMode,
85
+ trackerModeRead,
86
+ trackerModeStatus,
87
+ trackerModeWrite,
84
88
  trackerMovementAlertsInput,
85
89
  trackerPhotoContract
86
- } from "./chunk-425MMPO2.js";
90
+ } from "./chunk-ARMBM5BL.js";
87
91
 
88
92
  // ../../src/client/client.ts
89
93
  import {
@@ -92,6 +96,7 @@ import {
92
96
  ORPCError
93
97
  } from "@orpc/client";
94
98
  import { RPCLink as HttpLink } from "@orpc/client/fetch";
99
+ import { DedupeRequestsPlugin } from "@orpc/client/plugins";
95
100
  import { RPCLink as WebSocketLink } from "@orpc/client/websocket";
96
101
  import {
97
102
  getContractRouter,
@@ -112,22 +117,25 @@ import { z } from "zod";
112
117
  import { AsyncIteratorClass } from "@orpc/shared";
113
118
  var idleMs = 5 * 6e4;
114
119
  var replyMs = 15e3;
115
- function createBrowserKeepAlive(socket, page) {
120
+ function createBrowserKeepAlive(socket, page, probe) {
116
121
  let lastActivity = Date.now();
117
122
  const watches = /* @__PURE__ */ new Set();
118
123
  let timer;
124
+ let scheduled = 0;
119
125
  let disposed = false;
126
+ const lifetime = new AbortController();
120
127
  function schedule() {
128
+ const generation = ++scheduled;
121
129
  clearTimeout(timer);
122
130
  if (disposed || !watches.size || page.visibilityState !== "visible") return;
123
131
  timer = setTimeout(() => {
124
132
  if (socket.readyState !== socket.OPEN || page.visibilityState !== "visible")
125
133
  return;
126
- socket.send('{"action":"ping"}');
127
- timer = setTimeout(() => {
128
- if (socket.readyState === socket.OPEN && page.visibilityState === "visible")
134
+ const signal = AbortSignal.any([lifetime.signal, AbortSignal.timeout(replyMs)]);
135
+ void probe(signal).catch(() => {
136
+ if (!disposed && generation === scheduled && watches.size && socket.readyState === socket.OPEN && page.visibilityState === "visible")
129
137
  socket.reconnect(4e3, "Realtime keepalive timed out.");
130
- }, replyMs);
138
+ });
131
139
  }, Math.max(0, idleMs - (Date.now() - lastActivity)));
132
140
  }
133
141
  return {
@@ -158,6 +166,7 @@ function createBrowserKeepAlive(socket, page) {
158
166
  },
159
167
  dispose() {
160
168
  disposed = true;
169
+ lifetime.abort();
161
170
  watches.forEach((stop) => stop());
162
171
  }
163
172
  };
@@ -173,8 +182,18 @@ var RealtimeDisconnectedError = class extends ORPCError {
173
182
  this.name = "RealtimeDisconnectedError";
174
183
  }
175
184
  };
185
+ var deduplicatedStaffReads = /* @__PURE__ */ new Set([
186
+ "staff.trackers.getDetails",
187
+ "staff.trackers.list",
188
+ "staff.trackers.lookup",
189
+ "staff.favorites.list",
190
+ "staff.aws.resolve",
191
+ "staff.aws.observations.current",
192
+ "staff.aws.observations.history",
193
+ "staff.aws.observations.watchSeed",
194
+ "staff.aws.commands.list"
195
+ ]);
176
196
  var gatewayError = z.object({ message: z.string() });
177
- var gatewayPong = z.strictObject({ action: z.literal("pong") });
178
197
  function endpoint(value, transport) {
179
198
  const url = new URL(value);
180
199
  const secure = transport === "http" ? "https:" : "wss:";
@@ -185,7 +204,7 @@ function endpoint(value, transport) {
185
204
  );
186
205
  return url;
187
206
  }
188
- function createApiClient(contract, audience, options, createNegotiatedRealtime) {
207
+ function createApiClient(contract, audience, options, wrapRealtime) {
189
208
  const base = endpoint(options.baseUrl, "http");
190
209
  base.pathname = `${base.pathname.replace(/\/+$/, "")}/rpc`;
191
210
  const lifetime = new AbortController();
@@ -210,8 +229,22 @@ function createApiClient(contract, audience, options, createNegotiatedRealtime)
210
229
  ]
211
230
  };
212
231
  }
232
+ const httpOptions = linkOptions();
213
233
  const http = new HttpLink({
214
- ...linkOptions(),
234
+ ...httpOptions,
235
+ ...audience === "staff" && {
236
+ interceptors: [
237
+ ...httpOptions.interceptors ?? [],
238
+ ({ path, signal, next }) => signal && deduplicatedStaffReads.has(path.join(".")) ? pTimeout(next(), { milliseconds: Infinity, signal }) : next()
239
+ ],
240
+ plugins: [
241
+ ...httpOptions.plugins ?? [],
242
+ new DedupeRequestsPlugin({
243
+ filter: ({ path }) => deduplicatedStaffReads.has(path.join(".")),
244
+ groups: [{ condition: () => true, context: {} }]
245
+ })
246
+ ]
247
+ },
215
248
  url: base,
216
249
  fetch: (request, init) => {
217
250
  request.signal.throwIfAborted();
@@ -234,8 +267,6 @@ function createApiClient(contract, audience, options, createNegotiatedRealtime)
234
267
  let realtime;
235
268
  let ticketFailed = false;
236
269
  let ticketRejection = new AbortController();
237
- let staffFeeds = [];
238
- let negotiated;
239
270
  let supportsKeepAlive = false;
240
271
  let keepAlive;
241
272
  async function ticketUrl() {
@@ -256,7 +287,6 @@ function createApiClient(contract, audience, options, createNegotiatedRealtime)
256
287
  receivedTicket = true;
257
288
  const url = endpoint(result.webSocketUrl, "websocket");
258
289
  url.searchParams.set("ticket", result.ticket);
259
- staffFeeds = result.staffFeeds ?? [];
260
290
  supportsKeepAlive = result.keepAlive === true;
261
291
  return url.href;
262
292
  } catch (error) {
@@ -286,18 +316,16 @@ function createApiClient(contract, audience, options, createNegotiatedRealtime)
286
316
  { maxEnqueuedMessages: 0, WebSocket: options.webSocket }
287
317
  );
288
318
  const connection = socket;
289
- negotiated = createNegotiatedRealtime?.(
290
- connection,
291
- http,
292
- lifetime.signal,
293
- timeoutMs
294
- );
295
319
  connection.addEventListener("open", () => {
296
320
  if (disposed) return;
297
321
  connected = true;
298
322
  reconnecting = false;
299
323
  if (supportsKeepAlive && typeof document !== "undefined")
300
- keepAlive = createBrowserKeepAlive(connection, document);
324
+ keepAlive = createBrowserKeepAlive(
325
+ connection,
326
+ document,
327
+ (signal) => native.call([audience, "realtime", "ping"], void 0, { signal, context: {} })
328
+ );
301
329
  options.onConnectionStateChange?.("connected");
302
330
  if (opened && !disposed) {
303
331
  const previous = connectionGeneration;
@@ -310,7 +338,6 @@ function createApiClient(contract, audience, options, createNegotiatedRealtime)
310
338
  connection.addEventListener("close", () => {
311
339
  keepAlive?.dispose();
312
340
  keepAlive = void 0;
313
- negotiated?.disconnect();
314
341
  if (!connected) return;
315
342
  connected = false;
316
343
  reconnecting = true;
@@ -334,27 +361,21 @@ function createApiClient(contract, audience, options, createNegotiatedRealtime)
334
361
  return;
335
362
  }
336
363
  keepAlive?.activity();
337
- if (gatewayPong.safeParse(decoded).success) {
338
- event.stopImmediatePropagation();
339
- return;
340
- }
341
- if (negotiated?.consume(decoded)) {
342
- event.stopImmediatePropagation();
343
- return;
344
- }
345
364
  if (gatewayError.safeParse(decoded).success) {
346
365
  event.stopImmediatePropagation();
347
366
  connection.reconnect(4e3, "Gateway delivery failed.");
348
367
  }
349
368
  });
350
- realtime = new WebSocketLink({ ...linkOptions(), websocket: connection });
369
+ const native = new WebSocketLink({ ...linkOptions(), websocket: connection });
370
+ realtime = wrapRealtime?.(native, http, timeoutMs) ?? native;
351
371
  return realtime;
352
372
  }
353
373
  const link = new DynamicLink(async (request, path) => {
354
374
  lifetime.signal.throwIfAborted();
355
375
  request.signal?.throwIfAborted();
356
376
  const procedure = getContractRouter(root, path);
357
- if (!isContractProcedure(procedure) || !getEventIteratorSchemaDetails(procedure["~orpc"].outputSchema))
377
+ const ping = path.length === 3 && path[1] === "realtime" && path[2] === "ping";
378
+ if (!ping && (!isContractProcedure(procedure) || !getEventIteratorSchemaDetails(procedure["~orpc"].outputSchema)))
358
379
  return http;
359
380
  const link2 = websocket();
360
381
  const connection = socket;
@@ -369,7 +390,7 @@ function createApiClient(contract, audience, options, createNegotiatedRealtime)
369
390
  }
370
391
  lifetime.signal.throwIfAborted();
371
392
  request.signal?.throwIfAborted();
372
- const transport = negotiated?.select(path, staffFeeds) ?? link2;
393
+ const transport = link2;
373
394
  const monitor = keepAlive;
374
395
  if (!monitor) return transport;
375
396
  return {
@@ -430,7 +451,6 @@ function createApiClient(contract, audience, options, createNegotiatedRealtime)
430
451
  dispose() {
431
452
  if (disposed) return;
432
453
  disposed = true;
433
- negotiated?.disconnect();
434
454
  socket?.close(1e3, "Client disposed.");
435
455
  lifetime.abort(new DOMException("Client disposed.", "AbortError"));
436
456
  options.onConnectionStateChange?.("closed");
@@ -607,6 +627,10 @@ export {
607
627
  trackerLookupInput,
608
628
  trackerMemberAddInput,
609
629
  trackerMemberInput,
630
+ trackerMode,
631
+ trackerModeRead,
632
+ trackerModeStatus,
633
+ trackerModeWrite,
610
634
  trackerMovementAlertsInput,
611
635
  trackerPhotoContract,
612
636
  uploadPhoto