@pylonsync/sync 0.3.172 → 0.3.174

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 (2) hide show
  1. package/package.json +1 -1
  2. package/src/index.ts +18 -17
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "0.3.172",
6
+ "version": "0.3.174",
7
7
  "type": "module",
8
8
  "main": "src/index.ts",
9
9
  "types": "src/index.ts",
package/src/index.ts CHANGED
@@ -1262,23 +1262,21 @@ export class SyncEngine {
1262
1262
  private deriveWsUrl(): string {
1263
1263
  const base = this.config.baseUrl;
1264
1264
  const url = new URL(base);
1265
- const isHttps = url.protocol === "https:";
1266
- const scheme = isHttps ? "wss" : "ws";
1267
-
1268
- // Production HTTPS deploys: multiplex WS on the same origin via
1269
- // `/api/sync/ws`. The Pylon runtime accepts the Upgrade on its
1270
- // main HTTP port (4321), so any reverse proxy that already
1271
- // forwards `/api/*` carries the WebSocket through too. No
1272
- // separate WS port to expose, no per-deployment wsUrl env var.
1265
+ const scheme = url.protocol === "https:" ? "wss" : "ws";
1266
+
1267
+ // Always multiplex WS on the same origin via `/api/sync/ws`. The
1268
+ // Pylon runtime accepts the Upgrade on its main HTTP port (4321),
1269
+ // so any reverse proxy that already forwards `/api/*` carries the
1270
+ // WebSocket through too (Vite's `ws: true` proxy, Next.js rewrites,
1271
+ // CDNs with WS support).
1273
1272
  //
1274
- // Local dev (`pylon dev` on `http://localhost:4321`) keeps the
1275
- // legacy port+1 fallback so existing tutorials still work without
1276
- // touching their app config the dedicated `:4322` listener is
1277
- // still running there too.
1278
- if (url.port) {
1279
- const port = parseInt(url.port, 10);
1280
- return `${scheme}://${url.hostname}:${port + 1}`;
1281
- }
1273
+ // The legacy port+1 fallback (`:4322` for a `:4321` API) is still
1274
+ // available on the runtime, but we don't derive it client-side
1275
+ // anymore: any setup where the page origin (e.g. Vite on :3000)
1276
+ // wasn't equal to the API origin would compute ws://localhost:3001
1277
+ // — which doesn't exist and bypasses the dev-server proxy. The
1278
+ // `/api/sync/ws` path goes through whatever proxies `/api/*`,
1279
+ // which is the same code path prod already relies on.
1282
1280
  return `${scheme}://${url.host}/api/sync/ws`;
1283
1281
  }
1284
1282
 
@@ -2385,8 +2383,11 @@ export function createSyncEngine(
2385
2383
  baseUrl?: string,
2386
2384
  options?: Partial<SyncEngineConfig>,
2387
2385
  ): SyncEngine {
2386
+ // `??` would treat an empty string as "set" — but `init({ baseUrl: "" })`
2387
+ // is the intuitive "use page origin" incantation and must fall back. Use
2388
+ // `||` so undefined/null/"" all route to the auto-detected origin.
2388
2389
  const resolved =
2389
- baseUrl ??
2390
+ (baseUrl && baseUrl.length > 0 ? baseUrl : undefined) ??
2390
2391
  (typeof window !== "undefined" && window.location?.origin
2391
2392
  ? window.location.origin
2392
2393
  : "http://localhost:4321");