@insforge/sdk 1.3.0-ssr.1 → 1.3.0-ssr.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/dist/ssr.mjs CHANGED
@@ -2177,7 +2177,6 @@ var Functions = class _Functions {
2177
2177
  };
2178
2178
 
2179
2179
  // src/modules/realtime.ts
2180
- import { io } from "socket.io-client";
2181
2180
  var CONNECT_TIMEOUT = 1e4;
2182
2181
  var Realtime = class {
2183
2182
  constructor(baseUrl, tokenManager, anonKey) {
@@ -2212,61 +2211,69 @@ var Realtime = class {
2212
2211
  if (this.connectPromise) {
2213
2212
  return this.connectPromise;
2214
2213
  }
2215
- this.connectPromise = new Promise((resolve, reject) => {
2216
- const token = this.tokenManager.getAccessToken() ?? this.anonKey;
2217
- this.socket = io(this.baseUrl, {
2218
- transports: ["websocket"],
2219
- auth: token ? { token } : void 0
2220
- });
2221
- let initialConnection = true;
2222
- let timeoutId = null;
2223
- const cleanup = () => {
2224
- if (timeoutId) {
2225
- clearTimeout(timeoutId);
2226
- timeoutId = null;
2227
- }
2228
- };
2229
- timeoutId = setTimeout(() => {
2230
- if (initialConnection) {
2231
- initialConnection = false;
2232
- this.connectPromise = null;
2233
- this.socket?.disconnect();
2234
- this.socket = null;
2235
- reject(new Error(`Connection timeout after ${CONNECT_TIMEOUT}ms`));
2236
- }
2237
- }, CONNECT_TIMEOUT);
2238
- this.socket.on("connect", () => {
2239
- cleanup();
2240
- for (const channel of this.subscribedChannels) {
2241
- this.socket.emit("realtime:subscribe", { channel });
2242
- }
2243
- this.notifyListeners("connect");
2244
- if (initialConnection) {
2245
- initialConnection = false;
2246
- this.connectPromise = null;
2247
- resolve();
2248
- }
2249
- });
2250
- this.socket.on("connect_error", (error) => {
2251
- cleanup();
2252
- this.notifyListeners("connect_error", error);
2253
- if (initialConnection) {
2254
- initialConnection = false;
2255
- this.connectPromise = null;
2256
- reject(error);
2257
- }
2258
- });
2259
- this.socket.on("disconnect", (reason) => {
2260
- this.notifyListeners("disconnect", reason);
2261
- });
2262
- this.socket.on("realtime:error", (error) => {
2263
- this.notifyListeners("error", error);
2264
- });
2265
- this.socket.onAny((event, message) => {
2266
- if (event === "realtime:error") return;
2267
- this.notifyListeners(event, message);
2268
- });
2269
- });
2214
+ this.connectPromise = (async () => {
2215
+ try {
2216
+ const { io } = await import("socket.io-client");
2217
+ await new Promise((resolve, reject) => {
2218
+ const token = this.tokenManager.getAccessToken() ?? this.anonKey;
2219
+ this.socket = io(this.baseUrl, {
2220
+ transports: ["websocket"],
2221
+ auth: token ? { token } : void 0
2222
+ });
2223
+ let initialConnection = true;
2224
+ let timeoutId = null;
2225
+ const cleanup = () => {
2226
+ if (timeoutId) {
2227
+ clearTimeout(timeoutId);
2228
+ timeoutId = null;
2229
+ }
2230
+ };
2231
+ timeoutId = setTimeout(() => {
2232
+ if (initialConnection) {
2233
+ initialConnection = false;
2234
+ this.connectPromise = null;
2235
+ this.socket?.disconnect();
2236
+ this.socket = null;
2237
+ reject(new Error(`Connection timeout after ${CONNECT_TIMEOUT}ms`));
2238
+ }
2239
+ }, CONNECT_TIMEOUT);
2240
+ this.socket.on("connect", () => {
2241
+ cleanup();
2242
+ for (const channel of this.subscribedChannels) {
2243
+ this.socket.emit("realtime:subscribe", { channel });
2244
+ }
2245
+ this.notifyListeners("connect");
2246
+ if (initialConnection) {
2247
+ initialConnection = false;
2248
+ this.connectPromise = null;
2249
+ resolve();
2250
+ }
2251
+ });
2252
+ this.socket.on("connect_error", (error) => {
2253
+ cleanup();
2254
+ this.notifyListeners("connect_error", error);
2255
+ if (initialConnection) {
2256
+ initialConnection = false;
2257
+ this.connectPromise = null;
2258
+ reject(error);
2259
+ }
2260
+ });
2261
+ this.socket.on("disconnect", (reason) => {
2262
+ this.notifyListeners("disconnect", reason);
2263
+ });
2264
+ this.socket.on("realtime:error", (error) => {
2265
+ this.notifyListeners("error", error);
2266
+ });
2267
+ this.socket.onAny((event, message) => {
2268
+ if (event === "realtime:error") return;
2269
+ this.notifyListeners(event, message);
2270
+ });
2271
+ });
2272
+ } catch (error) {
2273
+ this.connectPromise = null;
2274
+ throw error;
2275
+ }
2276
+ })();
2270
2277
  return this.connectPromise;
2271
2278
  }
2272
2279
  /**
@@ -2596,10 +2603,9 @@ function decodeBase64Url(input) {
2596
2603
  normalized.length + (4 - normalized.length % 4) % 4,
2597
2604
  "="
2598
2605
  );
2599
- if (typeof atob === "function") {
2600
- return atob(padded);
2601
- }
2602
- return Buffer.from(padded, "base64").toString("utf8");
2606
+ const binary = atob(padded);
2607
+ const bytes = Uint8Array.from(binary, (char) => char.charCodeAt(0));
2608
+ return new TextDecoder().decode(bytes);
2603
2609
  }
2604
2610
  function getJwtExpiration(token) {
2605
2611
  if (!token) return null;
@@ -2697,11 +2703,11 @@ function setCookie(cookies, name, value, options) {
2697
2703
  }
2698
2704
  function deleteCookie(cookies, name, options) {
2699
2705
  if (!cookies) return;
2700
- if (cookies.delete) {
2701
- cookies.delete(name, options);
2706
+ if (cookies.set) {
2707
+ cookies.set(name, "", expiredCookieOptions(options));
2702
2708
  return;
2703
2709
  }
2704
- cookies.set?.(name, "", expiredCookieOptions(options));
2710
+ cookies.delete?.(name);
2705
2711
  }
2706
2712
  function serializeCookie(name, value, options = {}) {
2707
2713
  const parts = [`${encodeURIComponent(name)}=${encodeURIComponent(value)}`];
@@ -2720,32 +2726,17 @@ function serializeCookie(name, value, options = {}) {
2720
2726
  function appendSetCookie(headers, name, value, options) {
2721
2727
  headers.append("Set-Cookie", serializeCookie(name, value, options));
2722
2728
  }
2723
- function setAuthCookies(target, tokens, settings = {}) {
2729
+ function setAuthCookies(cookies, tokens, settings = {}) {
2724
2730
  const accessName = getAccessTokenCookieName(settings.names);
2725
2731
  const refreshName = getRefreshTokenCookieName(settings.names);
2726
2732
  const accessOptions = accessTokenCookieOptions(
2727
2733
  tokens.accessToken,
2728
2734
  settings.options?.accessToken
2729
2735
  );
2730
- if (target instanceof Headers) {
2731
- appendSetCookie(target, accessName, tokens.accessToken, accessOptions);
2732
- if (tokens.refreshToken) {
2733
- appendSetCookie(
2734
- target,
2735
- refreshName,
2736
- tokens.refreshToken,
2737
- refreshTokenCookieOptions(
2738
- tokens.refreshToken,
2739
- settings.options?.refreshToken
2740
- )
2741
- );
2742
- }
2743
- return;
2744
- }
2745
- setCookie(target, accessName, tokens.accessToken, accessOptions);
2736
+ setCookie(cookies, accessName, tokens.accessToken, accessOptions);
2746
2737
  if (tokens.refreshToken) {
2747
2738
  setCookie(
2748
- target,
2739
+ cookies,
2749
2740
  refreshName,
2750
2741
  tokens.refreshToken,
2751
2742
  refreshTokenCookieOptions(
@@ -2755,18 +2746,48 @@ function setAuthCookies(target, tokens, settings = {}) {
2755
2746
  );
2756
2747
  }
2757
2748
  }
2758
- function clearAuthCookies(target, settings = {}) {
2749
+ function clearAuthCookies(cookies, settings = {}) {
2759
2750
  const accessName = getAccessTokenCookieName(settings.names);
2760
2751
  const refreshName = getRefreshTokenCookieName(settings.names);
2761
2752
  const accessOptions = expiredCookieOptions(settings.options?.accessToken);
2762
2753
  const refreshOptions = expiredCookieOptions(settings.options?.refreshToken);
2763
- if (target instanceof Headers) {
2764
- appendSetCookie(target, accessName, "", accessOptions);
2765
- appendSetCookie(target, refreshName, "", refreshOptions);
2766
- return;
2754
+ deleteCookie(cookies, accessName, accessOptions);
2755
+ deleteCookie(cookies, refreshName, refreshOptions);
2756
+ }
2757
+ function setAuthCookieHeaders(headers, tokens, settings = {}) {
2758
+ const accessName = getAccessTokenCookieName(settings.names);
2759
+ const refreshName = getRefreshTokenCookieName(settings.names);
2760
+ appendSetCookie(
2761
+ headers,
2762
+ accessName,
2763
+ tokens.accessToken,
2764
+ accessTokenCookieOptions(tokens.accessToken, settings.options?.accessToken)
2765
+ );
2766
+ if (tokens.refreshToken) {
2767
+ appendSetCookie(
2768
+ headers,
2769
+ refreshName,
2770
+ tokens.refreshToken,
2771
+ refreshTokenCookieOptions(
2772
+ tokens.refreshToken,
2773
+ settings.options?.refreshToken
2774
+ )
2775
+ );
2767
2776
  }
2768
- deleteCookie(target, accessName, accessOptions);
2769
- deleteCookie(target, refreshName, refreshOptions);
2777
+ }
2778
+ function clearAuthCookieHeaders(headers, settings = {}) {
2779
+ appendSetCookie(
2780
+ headers,
2781
+ getAccessTokenCookieName(settings.names),
2782
+ "",
2783
+ expiredCookieOptions(settings.options?.accessToken)
2784
+ );
2785
+ appendSetCookie(
2786
+ headers,
2787
+ getRefreshTokenCookieName(settings.names),
2788
+ "",
2789
+ expiredCookieOptions(settings.options?.refreshToken)
2790
+ );
2770
2791
  }
2771
2792
 
2772
2793
  // src/ssr/browser-client.ts
@@ -2991,7 +3012,7 @@ async function refreshAuth(options = {}) {
2991
3012
  const headers = new Headers();
2992
3013
  const refreshToken = readRefreshToken(options);
2993
3014
  if (!refreshToken) {
2994
- clearAuthCookies(headers, options);
3015
+ clearAuthCookieHeaders(headers, options);
2995
3016
  const error2 = new InsForgeError(
2996
3017
  "Refresh token cookie is missing",
2997
3018
  401,
@@ -3061,7 +3082,7 @@ async function refreshAuth(options = {}) {
3061
3082
  error = normalizeError(caught);
3062
3083
  }
3063
3084
  if (error || !data?.accessToken) {
3064
- clearAuthCookies(headers, options);
3085
+ clearAuthCookieHeaders(headers, options);
3065
3086
  const normalized = normalizeError(error);
3066
3087
  return {
3067
3088
  response: jsonResponse(
@@ -3080,7 +3101,7 @@ async function refreshAuth(options = {}) {
3080
3101
  };
3081
3102
  }
3082
3103
  const nextRefreshToken = data.refreshToken ?? refreshToken;
3083
- setAuthCookies(
3104
+ setAuthCookieHeaders(
3084
3105
  headers,
3085
3106
  {
3086
3107
  accessToken: data.accessToken,