glitch-javascript-sdk 2.6.8 → 2.6.9
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/cjs/index.js +35 -41
- package/dist/cjs/index.js.map +1 -1
- package/dist/esm/index.js +35 -41
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/util/Storage.d.ts +0 -6
- package/dist/index.d.ts +0 -6
- package/package.json +1 -1
- package/src/util/Storage.ts +35 -46
|
@@ -1,12 +1,6 @@
|
|
|
1
1
|
declare class Storage {
|
|
2
2
|
private static rootDomain;
|
|
3
3
|
private static data;
|
|
4
|
-
/**
|
|
5
|
-
* Sets a root level domain so the data can persist across
|
|
6
|
-
* subdomains
|
|
7
|
-
*
|
|
8
|
-
* @param rootDomain
|
|
9
|
-
*/
|
|
10
4
|
static setRootDomain(rootDomain: string): void;
|
|
11
5
|
private static getStorageKey;
|
|
12
6
|
static set(key: string, value: any): void;
|
package/dist/index.d.ts
CHANGED
|
@@ -7438,12 +7438,6 @@ declare class Session {
|
|
|
7438
7438
|
declare class Storage {
|
|
7439
7439
|
private static rootDomain;
|
|
7440
7440
|
private static data;
|
|
7441
|
-
/**
|
|
7442
|
-
* Sets a root level domain so the data can persist across
|
|
7443
|
-
* subdomains
|
|
7444
|
-
*
|
|
7445
|
-
* @param rootDomain
|
|
7446
|
-
*/
|
|
7447
7441
|
static setRootDomain(rootDomain: string): void;
|
|
7448
7442
|
private static getStorageKey;
|
|
7449
7443
|
static set(key: string, value: any): void;
|
package/package.json
CHANGED
package/src/util/Storage.ts
CHANGED
|
@@ -2,12 +2,6 @@ class Storage {
|
|
|
2
2
|
private static rootDomain: string = '';
|
|
3
3
|
private static data: { [key: string]: any } = {};
|
|
4
4
|
|
|
5
|
-
/**
|
|
6
|
-
* Sets a root level domain so the data can persist across
|
|
7
|
-
* subdomains
|
|
8
|
-
*
|
|
9
|
-
* @param rootDomain
|
|
10
|
-
*/
|
|
11
5
|
public static setRootDomain(rootDomain: string) {
|
|
12
6
|
Storage.rootDomain = rootDomain;
|
|
13
7
|
}
|
|
@@ -17,55 +11,55 @@ class Storage {
|
|
|
17
11
|
}
|
|
18
12
|
|
|
19
13
|
public static set(key: string, value: any) {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
14
|
+
// 1. Always update in-memory fallback for the current process
|
|
15
|
+
Storage.data[key] = value;
|
|
16
|
+
|
|
17
|
+
// 2. Only attempt browser storage if window exists
|
|
18
|
+
if (typeof window !== 'undefined') {
|
|
24
19
|
try {
|
|
25
20
|
const serializedValue = JSON.stringify(value);
|
|
26
|
-
window.
|
|
21
|
+
window.localStorage.setItem(Storage.getStorageKey(key), serializedValue);
|
|
27
22
|
} catch (e) {
|
|
28
23
|
try {
|
|
29
|
-
|
|
24
|
+
const serializedValue = JSON.stringify(value);
|
|
25
|
+
window.sessionStorage.setItem(Storage.getStorageKey(key), serializedValue);
|
|
30
26
|
} catch (e) {
|
|
31
|
-
|
|
27
|
+
try {
|
|
28
|
+
this.setCookie(key, value, 31);
|
|
29
|
+
} catch (e) {}
|
|
32
30
|
}
|
|
33
|
-
Storage.data[key] = value;
|
|
34
31
|
}
|
|
35
32
|
}
|
|
36
33
|
}
|
|
37
34
|
|
|
38
35
|
public static get(key: string): any {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
if (serializedValue !== null) {
|
|
42
|
-
return JSON.parse(serializedValue);
|
|
43
|
-
}
|
|
44
|
-
} catch (e) {
|
|
36
|
+
// 1. Try Browser Storage if available
|
|
37
|
+
if (typeof window !== 'undefined') {
|
|
45
38
|
try {
|
|
46
|
-
const serializedValue = window.
|
|
47
|
-
if (serializedValue !== null)
|
|
48
|
-
return JSON.parse(serializedValue);
|
|
49
|
-
}
|
|
39
|
+
const serializedValue = window.localStorage.getItem(Storage.getStorageKey(key));
|
|
40
|
+
if (serializedValue !== null) return JSON.parse(serializedValue);
|
|
50
41
|
} catch (e) {
|
|
51
|
-
let value = null;
|
|
52
|
-
|
|
53
42
|
try {
|
|
54
|
-
|
|
55
|
-
|
|
43
|
+
const serializedValue = window.sessionStorage.getItem(Storage.getStorageKey(key));
|
|
44
|
+
if (serializedValue !== null) return JSON.parse(serializedValue);
|
|
45
|
+
} catch (e) {}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
56
48
|
|
|
57
|
-
|
|
49
|
+
// 2. Try Cookie (getCookie is now SSR safe)
|
|
50
|
+
let value = null;
|
|
51
|
+
try {
|
|
52
|
+
value = Storage.getCookie(key);
|
|
53
|
+
} catch (e) {}
|
|
58
54
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
return value;
|
|
63
|
-
}
|
|
55
|
+
// 3. Fallback to in-memory data
|
|
56
|
+
if (!value) {
|
|
57
|
+
value = Storage.data[key];
|
|
64
58
|
}
|
|
59
|
+
return value;
|
|
65
60
|
}
|
|
66
61
|
|
|
67
62
|
public static setAuthToken(token: string | null) {
|
|
68
|
-
// Always set the cookie if we have a root domain to ensure cross-subdomain sync
|
|
69
63
|
if (Storage.rootDomain) {
|
|
70
64
|
if (token) {
|
|
71
65
|
this.setCookie('glitch_auth_token', token, 31);
|
|
@@ -73,15 +67,12 @@ class Storage {
|
|
|
73
67
|
this.eraseCookie('glitch_auth_token');
|
|
74
68
|
}
|
|
75
69
|
}
|
|
76
|
-
// Still set localStorage for the current domain
|
|
77
70
|
Storage.set('glitch_auth_token', token);
|
|
78
71
|
}
|
|
79
72
|
|
|
80
73
|
public static getAuthToken(): string | null {
|
|
81
|
-
// 1. Try Cookie first (best for cross-subdomain)
|
|
82
74
|
let token = Storage.getCookie('glitch_auth_token');
|
|
83
75
|
|
|
84
|
-
// 2. Fallback to LocalStorage
|
|
85
76
|
if (!token || token === 'null') {
|
|
86
77
|
token = Storage.get('glitch_auth_token');
|
|
87
78
|
}
|
|
@@ -90,8 +81,8 @@ class Storage {
|
|
|
90
81
|
}
|
|
91
82
|
|
|
92
83
|
public static eraseCookie(name: string) {
|
|
93
|
-
|
|
94
|
-
if (document) {
|
|
84
|
+
// Use typeof check to prevent ReferenceError
|
|
85
|
+
if (typeof document !== 'undefined') {
|
|
95
86
|
document.cookie =
|
|
96
87
|
name +
|
|
97
88
|
'=; Secure; HttpOnly=false; SameSite=none; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';
|
|
@@ -107,7 +98,6 @@ class Storage {
|
|
|
107
98
|
}
|
|
108
99
|
|
|
109
100
|
if (typeof document !== 'undefined') {
|
|
110
|
-
// If rootDomain is .glitch.fun, this works for all subdomains
|
|
111
101
|
document.cookie =
|
|
112
102
|
name +
|
|
113
103
|
'=' +
|
|
@@ -120,7 +110,8 @@ class Storage {
|
|
|
120
110
|
}
|
|
121
111
|
|
|
122
112
|
private static getCookie(name: string): string | null {
|
|
123
|
-
|
|
113
|
+
// Use typeof check to prevent ReferenceError
|
|
114
|
+
if (typeof document !== 'undefined') {
|
|
124
115
|
const nameEQ = name + '=';
|
|
125
116
|
const ca = document.cookie.split(';');
|
|
126
117
|
for (let i = 0; i < ca.length; i++) {
|
|
@@ -136,7 +127,6 @@ class Storage {
|
|
|
136
127
|
const expiryTime = Date.now() + (expiresInSeconds * 1000);
|
|
137
128
|
Storage.set('glitch_token_expiry', expiryTime);
|
|
138
129
|
|
|
139
|
-
// Also set a cookie for cross-subdomain consistency if rootDomain exists
|
|
140
130
|
if (Storage.rootDomain && typeof document !== 'undefined') {
|
|
141
131
|
this.setCookie('glitch_token_expiry', expiryTime.toString(), 31);
|
|
142
132
|
}
|
|
@@ -152,10 +142,9 @@ class Storage {
|
|
|
152
142
|
|
|
153
143
|
public static isTokenExpired(): boolean {
|
|
154
144
|
const expiry = this.getTokenExpiry();
|
|
155
|
-
if (!expiry) return false;
|
|
156
|
-
|
|
145
|
+
if (!expiry) return false;
|
|
157
146
|
return Date.now() > expiry;
|
|
158
147
|
}
|
|
159
148
|
}
|
|
160
149
|
|
|
161
|
-
export default Storage;
|
|
150
|
+
export default Storage;
|