@bikefinder/sdk 0.33.0 → 0.33.2

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.
Files changed (3) hide show
  1. package/README.md +9 -1
  2. package/dist/index.js +40 -33
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -78,6 +78,9 @@ Full reconnects end existing subscriptions. Use `onReconnect` to explicitly
78
78
  subscribe again. The SDK does not replay events or retry writes automatically.
79
79
  Each ticket attempt has its own deadline (`realtimeConnectTimeoutMs`, 30 seconds
80
80
  by default), so a stalled token provider or ticket request can time out and retry.
81
+ Pending subscriptions wait through transient connection failures within the same
82
+ connection-wait deadline. Ticket admission errors such as a denied sign-in or
83
+ rate limit are returned immediately.
81
84
  Retain the original request ID when explicitly retrying an uncertain mutation.
82
85
 
83
86
  Photo metadata supports HTTP `list` and WebSocket `watchPage`, with the same
@@ -88,9 +91,10 @@ helper sends JPEG bytes directly to private staging storage, then asks the API
88
91
  to sanitize and publish the image:
89
92
 
90
93
  ```ts
94
+ const requestId = crypto.randomUUID();
91
95
  const result = await client.uploadPhoto({
92
96
  deviceId,
93
- requestId: crypto.randomUUID(),
97
+ requestId,
94
98
  kind: "bike",
95
99
  body: jpegBlob,
96
100
  });
@@ -100,6 +104,10 @@ const download = await client.api.trackers.photos.download({
100
104
  });
101
105
  ```
102
106
 
107
+ If the upload gets no storage response, the helper asks the API to verify and
108
+ complete the original upload. If the call still fails, keep the same request ID
109
+ and JPEG bytes when retrying.
110
+
103
111
  Uploads are limited to 10 MiB. Download URLs expire after two minutes; request a
104
112
  fresh reference if one expires. Photo bytes do not travel over WebSockets.
105
113
  Runtime schemas are available from `@bikefinder/sdk/schemas`.
package/dist/index.js CHANGED
@@ -170,12 +170,14 @@ function createApiClient(contract, audience, options, createNegotiatedRealtime)
170
170
  const tickets = createORPCClient(http, { path: [audience, "realtime"] });
171
171
  let socket;
172
172
  let realtime;
173
- let ticketError;
173
+ let ticketFailed = false;
174
+ let ticketRejection = new AbortController();
174
175
  let staffFeeds = [];
175
176
  let negotiated;
176
177
  async function ticketUrl() {
177
178
  lifetime.signal.throwIfAborted();
178
- ticketError = void 0;
179
+ ticketFailed = false;
180
+ let receivedTicket = false;
179
181
  const attempt = new AbortController();
180
182
  try {
181
183
  const result = await pTimeout(
@@ -187,17 +189,24 @@ function createApiClient(contract, audience, options, createNegotiatedRealtime)
187
189
  }
188
190
  );
189
191
  lifetime.signal.throwIfAborted();
192
+ receivedTicket = true;
190
193
  const url = endpoint(result.webSocketUrl, "websocket");
191
194
  url.searchParams.set("ticket", result.ticket);
192
195
  staffFeeds = result.staffFeeds ?? [];
193
196
  return url.href;
194
197
  } catch (error) {
195
- attempt.abort(error);
196
- if (error instanceof Error && !disposed) {
197
- ticketError = error;
198
- options.onConnectionError?.(error);
198
+ const failure = error instanceof Error ? error : new Error("Realtime ticket request failed.", { cause: error });
199
+ attempt.abort(failure);
200
+ ticketFailed = true;
201
+ if (!disposed) {
202
+ if (receivedTicket || failure instanceof ORPCError && failure.status >= 400 && failure.status < 500 && failure.status !== 408) {
203
+ const rejected = ticketRejection;
204
+ ticketRejection = new AbortController();
205
+ rejected.abort(failure);
206
+ }
207
+ options.onConnectionError?.(failure);
199
208
  }
200
- throw error;
209
+ throw failure;
201
210
  }
202
211
  }
203
212
  function websocket() {
@@ -232,7 +241,7 @@ function createApiClient(contract, audience, options, createNegotiatedRealtime)
232
241
  if (!disposed) options.onConnectionStateChange?.("reconnecting");
233
242
  });
234
243
  connection.addEventListener("error", () => {
235
- if (!disposed && !ticketError)
244
+ if (!disposed && !ticketFailed)
236
245
  options.onConnectionError?.(
237
246
  new Error("Realtime WebSocket connection failed.")
238
247
  );
@@ -262,28 +271,20 @@ function createApiClient(contract, audience, options, createNegotiatedRealtime)
262
271
  }
263
272
  const link = new DynamicLink(async (request, path) => {
264
273
  lifetime.signal.throwIfAborted();
274
+ request.signal?.throwIfAborted();
265
275
  const procedure = getContractRouter(root, path);
266
276
  if (!isContractProcedure(procedure) || !getEventIteratorSchemaDetails(procedure["~orpc"].outputSchema))
267
277
  return http;
268
278
  const link2 = websocket();
269
279
  const connection = socket;
270
280
  if (connection.readyState !== connection.OPEN) {
271
- try {
272
- await pEvent(connection, "open", {
273
- rejectionEvents: ["close", "error"],
274
- signal: request.signal ? AbortSignal.any([lifetime.signal, request.signal]) : lifetime.signal,
275
- timeout: timeoutMs
276
- });
277
- } catch (error) {
278
- lifetime.signal.throwIfAborted();
279
- request.signal?.throwIfAborted();
280
- if (ticketError) throw ticketError;
281
- if (error instanceof Event && error.type === "close")
282
- throw new RealtimeDisconnectedError();
283
- if (error && typeof error === "object" && "error" in error && error.error instanceof Error)
284
- throw error.error;
285
- throw error instanceof Error ? error : new Error("Realtime connection failed.", { cause: error });
286
- }
281
+ const signals = [lifetime.signal, ticketRejection.signal];
282
+ if (request.signal) signals.push(request.signal);
283
+ await pEvent(connection, "open", {
284
+ rejectionEvents: [],
285
+ signal: AbortSignal.any(signals),
286
+ timeout: timeoutMs
287
+ });
287
288
  }
288
289
  lifetime.signal.throwIfAborted();
289
290
  request.signal?.throwIfAborted();
@@ -333,6 +334,7 @@ async function uploadPhoto(uploads, input, fetch = globalThis.fetch) {
333
334
  },
334
335
  { signal: input.signal }
335
336
  );
337
+ input.signal?.throwIfAborted();
336
338
  if (session.status === "published") return { photoId: session.photoId };
337
339
  if (Date.parse(session.expiresAt) <= Date.now())
338
340
  throw new Error(
@@ -345,15 +347,20 @@ async function uploadPhoto(uploads, input, fetch = globalThis.fetch) {
345
347
  throw new TypeError(
346
348
  "The photo upload session targets an unexpected storage endpoint."
347
349
  );
348
- const uploaded = await fetch(url, {
349
- method: "PUT",
350
- body: input.body,
351
- credentials: "omit",
352
- redirect: "error",
353
- signal: input.signal,
354
- headers: session.headers
355
- });
356
- if (!uploaded.ok && uploaded.status !== 412)
350
+ let uploaded;
351
+ try {
352
+ uploaded = await fetch(url, {
353
+ method: "PUT",
354
+ body: input.body,
355
+ credentials: "omit",
356
+ redirect: "error",
357
+ signal: input.signal,
358
+ headers: session.headers
359
+ });
360
+ } catch {
361
+ }
362
+ input.signal?.throwIfAborted();
363
+ if (uploaded && !uploaded.ok && uploaded.status !== 412)
357
364
  throw new Error(
358
365
  "The private photo upload failed (" + uploaded.status + "). Retry with the same request ID."
359
366
  );
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bikefinder/sdk",
3
- "version": "0.33.0",
3
+ "version": "0.33.2",
4
4
  "description": "Typed HTTP and WebSocket client for BikeFinder.",
5
5
  "license": "UNLICENSED",
6
6
  "repository": {