@bikefinder/sdk 0.33.2 → 0.33.3

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/README.md CHANGED
@@ -76,6 +76,9 @@ does not call `onConnectionError`.
76
76
 
77
77
  Full reconnects end existing subscriptions. Use `onReconnect` to explicitly
78
78
  subscribe again. The SDK does not replay events or retry writes automatically.
79
+ When the API advertises keepalive support, the browser SDK probes an idle socket
80
+ after five minutes while a visible page has active watches. A missed reply
81
+ triggers the usual reconnect. The reply checks the Gateway connection.
79
82
  Each ticket attempt has its own deadline (`realtimeConnectTimeoutMs`, 30 seconds
80
83
  by default), so a stalled token provider or ticket request can time out and retry.
81
84
  Pending subscriptions wait through transient connection failures within the same
@@ -424,6 +424,7 @@ var realtimeTicket = z7.object({
424
424
  ticket: z7.string().regex(/^[A-Za-z0-9_-]{43}$/),
425
425
  expiresAt: z7.iso.datetime(),
426
426
  webSocketUrl: z7.url({ protocol: /^wss?$/ }),
427
+ keepAlive: z7.literal(true).optional(),
427
428
  staffFeeds: z7.array(z7.string()).transform(
428
429
  (values) => values.filter(
429
430
  (value) => staffRealtimeFeed.safeParse(value).success
package/dist/index.d.ts CHANGED
@@ -15,6 +15,7 @@ export declare const realtimeTicket: z.ZodObject<{
15
15
  ticket: z.ZodString;
16
16
  expiresAt: z.ZodISODateTime;
17
17
  webSocketUrl: z.ZodURL;
18
+ keepAlive: z.ZodOptional<z.ZodLiteral<true>>;
18
19
  staffFeeds: z.ZodOptional<z.ZodPipe<z.ZodArray<z.ZodString>, z.ZodTransform<("observations" | "observation-watches" | "commands" | "settings")[], string[]>>>;
19
20
  }, z.core.$strip>;
20
21
  export declare const realtimeContract: {
@@ -22,6 +23,7 @@ export declare const realtimeContract: {
22
23
  ticket: z.ZodString;
23
24
  expiresAt: z.ZodISODateTime;
24
25
  webSocketUrl: z.ZodURL;
26
+ keepAlive: z.ZodOptional<z.ZodLiteral<true>>;
25
27
  staffFeeds: z.ZodOptional<z.ZodPipe<z.ZodArray<z.ZodString>, z.ZodTransform<("observations" | "observation-watches" | "commands" | "settings")[], string[]>>>;
26
28
  }, z.core.$strip>, import("@orpc/contract").MergedErrorMap<Record<never, never>, {
27
29
  readonly invalid: {
package/dist/index.js CHANGED
@@ -82,7 +82,7 @@ import {
82
82
  trackerMemberInput,
83
83
  trackerMovementAlertsInput,
84
84
  trackerPhotoContract
85
- } from "./chunk-IYM3P32E.js";
85
+ } from "./chunk-425MMPO2.js";
86
86
 
87
87
  // ../../src/client/client.ts
88
88
  import {
@@ -101,10 +101,68 @@ import {
101
101
  RequestValidationPlugin,
102
102
  ResponseValidationPlugin
103
103
  } from "@orpc/contract/plugins";
104
+ import { isAsyncIteratorObject } from "@orpc/shared";
104
105
  import ReconnectingWebSocket from "partysocket/ws";
105
106
  import { pEvent } from "p-event";
106
107
  import pTimeout from "p-timeout";
107
108
  import { z } from "zod";
109
+
110
+ // ../../src/client/keep-alive.ts
111
+ import { AsyncIteratorClass } from "@orpc/shared";
112
+ var idleMs = 5 * 6e4;
113
+ var replyMs = 15e3;
114
+ function createBrowserKeepAlive(socket, page) {
115
+ let lastActivity = Date.now();
116
+ const watches = /* @__PURE__ */ new Set();
117
+ let timer;
118
+ let disposed = false;
119
+ function schedule() {
120
+ clearTimeout(timer);
121
+ if (disposed || !watches.size || page.visibilityState !== "visible") return;
122
+ timer = setTimeout(() => {
123
+ if (socket.readyState !== socket.OPEN || page.visibilityState !== "visible")
124
+ return;
125
+ socket.send('{"action":"ping"}');
126
+ timer = setTimeout(() => {
127
+ if (socket.readyState === socket.OPEN && page.visibilityState === "visible")
128
+ socket.reconnect(4e3, "Realtime keepalive timed out.");
129
+ }, replyMs);
130
+ }, Math.max(0, idleMs - (Date.now() - lastActivity)));
131
+ }
132
+ return {
133
+ activity() {
134
+ lastActivity = Date.now();
135
+ schedule();
136
+ },
137
+ track(iterator, signal) {
138
+ if (disposed || signal?.aborted) return iterator;
139
+ function stop() {
140
+ if (!watches.delete(stop)) return;
141
+ signal?.removeEventListener("abort", stop);
142
+ if (!watches.size) {
143
+ page.removeEventListener("visibilitychange", schedule);
144
+ clearTimeout(timer);
145
+ }
146
+ }
147
+ watches.add(stop);
148
+ if (watches.size === 1) {
149
+ page.addEventListener("visibilitychange", schedule);
150
+ schedule();
151
+ }
152
+ signal?.addEventListener("abort", stop, { once: true });
153
+ return new AsyncIteratorClass(() => iterator.next(), async (reason) => {
154
+ stop();
155
+ if (reason !== "next") await iterator.return?.();
156
+ });
157
+ },
158
+ dispose() {
159
+ disposed = true;
160
+ watches.forEach((stop) => stop());
161
+ }
162
+ };
163
+ }
164
+
165
+ // ../../src/client/client.ts
108
166
  var RealtimeDisconnectedError = class extends ORPCError {
109
167
  constructor() {
110
168
  super("stream_unavailable", {
@@ -115,6 +173,7 @@ var RealtimeDisconnectedError = class extends ORPCError {
115
173
  }
116
174
  };
117
175
  var gatewayError = z.object({ message: z.string() });
176
+ var gatewayPong = z.strictObject({ action: z.literal("pong") });
118
177
  function endpoint(value, transport) {
119
178
  const url = new URL(value);
120
179
  const secure = transport === "http" ? "https:" : "wss:";
@@ -174,6 +233,8 @@ function createApiClient(contract, audience, options, createNegotiatedRealtime)
174
233
  let ticketRejection = new AbortController();
175
234
  let staffFeeds = [];
176
235
  let negotiated;
236
+ let supportsKeepAlive = false;
237
+ let keepAlive;
177
238
  async function ticketUrl() {
178
239
  lifetime.signal.throwIfAborted();
179
240
  ticketFailed = false;
@@ -193,6 +254,7 @@ function createApiClient(contract, audience, options, createNegotiatedRealtime)
193
254
  const url = endpoint(result.webSocketUrl, "websocket");
194
255
  url.searchParams.set("ticket", result.ticket);
195
256
  staffFeeds = result.staffFeeds ?? [];
257
+ supportsKeepAlive = result.keepAlive === true;
196
258
  return url.href;
197
259
  } catch (error) {
198
260
  const failure = error instanceof Error ? error : new Error("Realtime ticket request failed.", { cause: error });
@@ -230,11 +292,15 @@ function createApiClient(contract, audience, options, createNegotiatedRealtime)
230
292
  connection.addEventListener("open", () => {
231
293
  if (disposed) return;
232
294
  connected = true;
295
+ if (supportsKeepAlive && typeof document !== "undefined")
296
+ keepAlive = createBrowserKeepAlive(connection, document);
233
297
  options.onConnectionStateChange?.("connected");
234
298
  if (opened && !disposed) options.onReconnect?.();
235
299
  opened = true;
236
300
  });
237
301
  connection.addEventListener("close", () => {
302
+ keepAlive?.dispose();
303
+ keepAlive = void 0;
238
304
  negotiated?.disconnect();
239
305
  if (!connected) return;
240
306
  connected = false;
@@ -257,6 +323,11 @@ function createApiClient(contract, audience, options, createNegotiatedRealtime)
257
323
  connection.reconnect(4e3, "Invalid Gateway response.");
258
324
  return;
259
325
  }
326
+ keepAlive?.activity();
327
+ if (gatewayPong.safeParse(decoded).success) {
328
+ event.stopImmediatePropagation();
329
+ return;
330
+ }
260
331
  if (negotiated?.consume(decoded)) {
261
332
  event.stopImmediatePropagation();
262
333
  return;
@@ -288,7 +359,15 @@ function createApiClient(contract, audience, options, createNegotiatedRealtime)
288
359
  }
289
360
  lifetime.signal.throwIfAborted();
290
361
  request.signal?.throwIfAborted();
291
- return negotiated?.select(path, staffFeeds) ?? link2;
362
+ const transport = negotiated?.select(path, staffFeeds) ?? link2;
363
+ const monitor = keepAlive;
364
+ if (!monitor) return transport;
365
+ return {
366
+ async call(path2, input, options2) {
367
+ const result = await transport.call(path2, input, options2);
368
+ return isAsyncIteratorObject(result) ? monitor.track(result, options2.signal) : result;
369
+ }
370
+ };
292
371
  });
293
372
  const api = createORPCClient(link, {
294
373
  path: [audience]
package/dist/schemas.d.ts CHANGED
@@ -15,6 +15,7 @@ export declare const realtimeTicket: z.ZodObject<{
15
15
  ticket: z.ZodString;
16
16
  expiresAt: z.ZodISODateTime;
17
17
  webSocketUrl: z.ZodURL;
18
+ keepAlive: z.ZodOptional<z.ZodLiteral<true>>;
18
19
  staffFeeds: z.ZodOptional<z.ZodPipe<z.ZodArray<z.ZodString>, z.ZodTransform<("observations" | "observation-watches" | "commands" | "settings")[], string[]>>>;
19
20
  }, z.core.$strip>;
20
21
  export declare const realtimeContract: {
@@ -22,6 +23,7 @@ export declare const realtimeContract: {
22
23
  ticket: z.ZodString;
23
24
  expiresAt: z.ZodISODateTime;
24
25
  webSocketUrl: z.ZodURL;
26
+ keepAlive: z.ZodOptional<z.ZodLiteral<true>>;
25
27
  staffFeeds: z.ZodOptional<z.ZodPipe<z.ZodArray<z.ZodString>, z.ZodTransform<("observations" | "observation-watches" | "commands" | "settings")[], string[]>>>;
26
28
  }, z.core.$strip>, import("@orpc/contract").MergedErrorMap<Record<never, never>, {
27
29
  readonly invalid: {
package/dist/schemas.js CHANGED
@@ -96,7 +96,7 @@ import {
96
96
  trackerSubscription,
97
97
  trackerSummary,
98
98
  trackerSummaryFields
99
- } from "./chunk-IYM3P32E.js";
99
+ } from "./chunk-425MMPO2.js";
100
100
  export {
101
101
  account,
102
102
  accountPreferencePatch,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bikefinder/sdk",
3
- "version": "0.33.2",
3
+ "version": "0.33.3",
4
4
  "description": "Typed HTTP and WebSocket client for BikeFinder.",
5
5
  "license": "UNLICENSED",
6
6
  "repository": {
@@ -35,6 +35,7 @@
35
35
  "dependencies": {
36
36
  "@orpc/client": "1.16.3",
37
37
  "@orpc/contract": "1.16.3",
38
+ "@orpc/shared": "1.16.3",
38
39
  "decimal.js": "10.6.0",
39
40
  "p-event": "8.0.2",
40
41
  "p-timeout": "8.1.0",