glitch-javascript-sdk 3.0.3 → 3.0.5
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 +76 -58
- package/dist/cjs/index.js.map +1 -1
- package/dist/esm/api/Titles.d.ts +1 -1
- package/dist/esm/index.js +76 -58
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/util/Storage.d.ts +2 -0
- package/dist/index.d.ts +3 -1
- package/package.json +1 -1
- package/src/api/Titles.ts +2 -2
- package/src/routes/TitlesRoute.ts +1 -1
- package/src/util/Storage.ts +84 -57
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
declare class Storage {
|
|
2
2
|
private static rootDomain;
|
|
3
3
|
private static data;
|
|
4
|
+
private static crossDomainKeys;
|
|
4
5
|
static setRootDomain(rootDomain: string): void;
|
|
5
6
|
private static getStorageKey;
|
|
7
|
+
private static shouldShareAcrossSubdomains;
|
|
6
8
|
static set(key: string, value: any): void;
|
|
7
9
|
static get(key: string): any;
|
|
8
10
|
static setAuthToken(token: string | null): void;
|
package/dist/index.d.ts
CHANGED
|
@@ -4410,7 +4410,7 @@ declare class Titles {
|
|
|
4410
4410
|
* Initializes a play session. Handles age-gating and license verification.
|
|
4411
4411
|
* Returns the CDN URL for WASM/iFrame or Signaling URL for Pixel Streaming.
|
|
4412
4412
|
*/
|
|
4413
|
-
static getPlaySession<T>(title_id: string, params?: Record<string, any>): AxiosPromise<Response<T>>;
|
|
4413
|
+
static getPlaySession<T>(title_id: string, data?: object, params?: Record<string, any>): AxiosPromise<Response<T>>;
|
|
4414
4414
|
/**
|
|
4415
4415
|
* List all developer payouts for a title.
|
|
4416
4416
|
*/
|
|
@@ -8070,8 +8070,10 @@ declare class Session {
|
|
|
8070
8070
|
declare class Storage {
|
|
8071
8071
|
private static rootDomain;
|
|
8072
8072
|
private static data;
|
|
8073
|
+
private static crossDomainKeys;
|
|
8073
8074
|
static setRootDomain(rootDomain: string): void;
|
|
8074
8075
|
private static getStorageKey;
|
|
8076
|
+
private static shouldShareAcrossSubdomains;
|
|
8075
8077
|
static set(key: string, value: any): void;
|
|
8076
8078
|
static get(key: string): any;
|
|
8077
8079
|
static setAuthToken(token: string | null): void;
|
package/package.json
CHANGED
package/src/api/Titles.ts
CHANGED
|
@@ -952,8 +952,8 @@ class Titles {
|
|
|
952
952
|
* Initializes a play session. Handles age-gating and license verification.
|
|
953
953
|
* Returns the CDN URL for WASM/iFrame or Signaling URL for Pixel Streaming.
|
|
954
954
|
*/
|
|
955
|
-
public static getPlaySession<T>(title_id: string, params?: Record<string, any>): AxiosPromise<Response<T>> {
|
|
956
|
-
return Requests.processRoute(TitlesRoute.routes.getPlaySession,
|
|
955
|
+
public static getPlaySession<T>(title_id: string, data?: object, params?: Record<string, any>): AxiosPromise<Response<T>> {
|
|
956
|
+
return Requests.processRoute(TitlesRoute.routes.getPlaySession, data, { title_id }, params);
|
|
957
957
|
}
|
|
958
958
|
|
|
959
959
|
/**
|
|
@@ -183,7 +183,7 @@ class TitlesRoute {
|
|
|
183
183
|
// Aegis Deployment
|
|
184
184
|
getDeploymentUploadUrl: { url: '/titles/{title_id}/deployments/presigned-url', method: HTTP_METHODS.POST },
|
|
185
185
|
confirmDeployment: { url: '/titles/{title_id}/deployments/confirm', method: HTTP_METHODS.POST },
|
|
186
|
-
getPlaySession: { url: '/titles/{title_id}/play', method: HTTP_METHODS.
|
|
186
|
+
getPlaySession: { url: '/titles/{title_id}/play', method: HTTP_METHODS.POST },
|
|
187
187
|
|
|
188
188
|
initiateMultipartUpload: { url: '/titles/{title_id}/deployments/multipart/initiate', method: HTTP_METHODS.POST },
|
|
189
189
|
getMultipartUrls: { url: '/titles/{title_id}/deployments/multipart/urls', method: HTTP_METHODS.POST },
|
package/src/util/Storage.ts
CHANGED
|
@@ -2,6 +2,18 @@ class Storage {
|
|
|
2
2
|
private static rootDomain: string = '';
|
|
3
3
|
private static data: { [key: string]: any } = {};
|
|
4
4
|
|
|
5
|
+
private static crossDomainKeys = new Set([
|
|
6
|
+
'glitch_auth_token',
|
|
7
|
+
'glitch_token_expiry',
|
|
8
|
+
'user_id',
|
|
9
|
+
'user_first_name',
|
|
10
|
+
'user_last_name',
|
|
11
|
+
'username',
|
|
12
|
+
'email',
|
|
13
|
+
'session_id',
|
|
14
|
+
'community_id',
|
|
15
|
+
]);
|
|
16
|
+
|
|
5
17
|
public static setRootDomain(rootDomain: string) {
|
|
6
18
|
Storage.rootDomain = rootDomain;
|
|
7
19
|
}
|
|
@@ -10,11 +22,13 @@ class Storage {
|
|
|
10
22
|
return Storage.rootDomain ? `${Storage.rootDomain}:${key}` : key;
|
|
11
23
|
}
|
|
12
24
|
|
|
25
|
+
private static shouldShareAcrossSubdomains(key: string): boolean {
|
|
26
|
+
return !!Storage.rootDomain && Storage.crossDomainKeys.has(key);
|
|
27
|
+
}
|
|
28
|
+
|
|
13
29
|
public static set(key: string, value: any) {
|
|
14
|
-
// 1. Always update in-memory fallback for the current process
|
|
15
30
|
Storage.data[key] = value;
|
|
16
31
|
|
|
17
|
-
// 2. Only attempt browser storage if window exists
|
|
18
32
|
if (typeof window !== 'undefined') {
|
|
19
33
|
try {
|
|
20
34
|
const serializedValue = JSON.stringify(value);
|
|
@@ -23,17 +37,32 @@ class Storage {
|
|
|
23
37
|
try {
|
|
24
38
|
const serializedValue = JSON.stringify(value);
|
|
25
39
|
window.sessionStorage.setItem(Storage.getStorageKey(key), serializedValue);
|
|
26
|
-
} catch (e) {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
40
|
+
} catch (e) {}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Important: shared session keys must be written to a root-domain cookie.
|
|
45
|
+
if (Storage.shouldShareAcrossSubdomains(key)) {
|
|
46
|
+
if (value === null || value === undefined) {
|
|
47
|
+
Storage.eraseCookie(key);
|
|
48
|
+
} else {
|
|
49
|
+
Storage.setCookie(key, value, 31);
|
|
31
50
|
}
|
|
32
51
|
}
|
|
33
52
|
}
|
|
34
53
|
|
|
35
54
|
public static get(key: string): any {
|
|
36
|
-
//
|
|
55
|
+
// Important: for shared session keys, cookie must win over localStorage.
|
|
56
|
+
// Otherwise stale www.glitch.fun localStorage can override the real shared cookie.
|
|
57
|
+
if (Storage.shouldShareAcrossSubdomains(key)) {
|
|
58
|
+
try {
|
|
59
|
+
const cookieValue = Storage.getCookie(key);
|
|
60
|
+
if (cookieValue !== null && cookieValue !== undefined && cookieValue !== 'null') {
|
|
61
|
+
return cookieValue;
|
|
62
|
+
}
|
|
63
|
+
} catch (e) {}
|
|
64
|
+
}
|
|
65
|
+
|
|
37
66
|
if (typeof window !== 'undefined') {
|
|
38
67
|
try {
|
|
39
68
|
const serializedValue = window.localStorage.getItem(Storage.getStorageKey(key));
|
|
@@ -46,98 +75,96 @@ class Storage {
|
|
|
46
75
|
}
|
|
47
76
|
}
|
|
48
77
|
|
|
49
|
-
// 2. Try Cookie (getCookie is now SSR safe)
|
|
50
|
-
let value = null;
|
|
51
78
|
try {
|
|
52
|
-
|
|
79
|
+
const cookieValue = Storage.getCookie(key);
|
|
80
|
+
if (cookieValue !== null && cookieValue !== undefined && cookieValue !== 'null') {
|
|
81
|
+
return cookieValue;
|
|
82
|
+
}
|
|
53
83
|
} catch (e) {}
|
|
54
84
|
|
|
55
|
-
|
|
56
|
-
if (!value) {
|
|
57
|
-
value = Storage.data[key];
|
|
58
|
-
}
|
|
59
|
-
return value;
|
|
85
|
+
return Storage.data[key];
|
|
60
86
|
}
|
|
61
87
|
|
|
62
88
|
public static setAuthToken(token: string | null) {
|
|
63
|
-
if (Storage.rootDomain) {
|
|
64
|
-
if (token) {
|
|
65
|
-
this.setCookie('glitch_auth_token', token, 31);
|
|
66
|
-
} else {
|
|
67
|
-
this.eraseCookie('glitch_auth_token');
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
89
|
Storage.set('glitch_auth_token', token);
|
|
71
90
|
}
|
|
72
91
|
|
|
73
92
|
public static getAuthToken(): string | null {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
if (!token || token === 'null') {
|
|
77
|
-
token = Storage.get('glitch_auth_token');
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
return (token === 'null' || !token) ? null : token;
|
|
93
|
+
const token = Storage.get('glitch_auth_token');
|
|
94
|
+
return token === 'null' || !token ? null : token;
|
|
81
95
|
}
|
|
82
96
|
|
|
83
97
|
public static eraseCookie(name: string) {
|
|
84
|
-
|
|
85
|
-
|
|
98
|
+
if (typeof document === 'undefined') return;
|
|
99
|
+
|
|
100
|
+
// Clear host-only cookie.
|
|
101
|
+
document.cookie =
|
|
102
|
+
`${name}=; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT; SameSite=Lax; Secure`;
|
|
103
|
+
|
|
104
|
+
// Clear root-domain cookie.
|
|
105
|
+
if (Storage.rootDomain) {
|
|
86
106
|
document.cookie =
|
|
87
|
-
name
|
|
88
|
-
'=; Secure; HttpOnly=false; SameSite=none; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';
|
|
107
|
+
`${name}=; Path=/; Domain=${Storage.rootDomain}; Expires=Thu, 01 Jan 1970 00:00:00 GMT; SameSite=Lax; Secure`;
|
|
89
108
|
}
|
|
90
109
|
}
|
|
91
110
|
|
|
92
|
-
private static setCookie(name: string, value:
|
|
111
|
+
private static setCookie(name: string, value: any, days: number) {
|
|
93
112
|
let expires = '';
|
|
113
|
+
|
|
94
114
|
if (days) {
|
|
95
115
|
const date = new Date();
|
|
96
116
|
date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000);
|
|
97
|
-
expires = ';
|
|
117
|
+
expires = '; Expires=' + date.toUTCString();
|
|
98
118
|
}
|
|
99
119
|
|
|
100
120
|
if (typeof document !== 'undefined') {
|
|
121
|
+
const encodedValue = encodeURIComponent(JSON.stringify(value));
|
|
122
|
+
|
|
101
123
|
document.cookie =
|
|
102
|
-
name
|
|
103
|
-
'=' +
|
|
104
|
-
(value || '') +
|
|
105
|
-
expires +
|
|
106
|
-
'; path=/; domain=' +
|
|
107
|
-
Storage.rootDomain +
|
|
108
|
-
'; SameSite=Lax; Secure';
|
|
124
|
+
`${name}=${encodedValue}${expires}; Path=/; Domain=${Storage.rootDomain}; SameSite=Lax; Secure`;
|
|
109
125
|
}
|
|
110
126
|
}
|
|
111
127
|
|
|
112
|
-
private static getCookie(name: string):
|
|
113
|
-
// Use typeof check to prevent ReferenceError
|
|
128
|
+
private static getCookie(name: string): any {
|
|
114
129
|
if (typeof document !== 'undefined') {
|
|
115
130
|
const nameEQ = name + '=';
|
|
116
131
|
const ca = document.cookie.split(';');
|
|
132
|
+
|
|
117
133
|
for (let i = 0; i < ca.length; i++) {
|
|
118
134
|
let c = ca[i];
|
|
119
|
-
|
|
120
|
-
|
|
135
|
+
|
|
136
|
+
while (c.charAt(0) === ' ') {
|
|
137
|
+
c = c.substring(1, c.length);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
if (c.indexOf(nameEQ) === 0) {
|
|
141
|
+
const rawValue = c.substring(nameEQ.length, c.length);
|
|
142
|
+
|
|
143
|
+
try {
|
|
144
|
+
const decodedValue = decodeURIComponent(rawValue);
|
|
145
|
+
return JSON.parse(decodedValue);
|
|
146
|
+
} catch (e) {
|
|
147
|
+
try {
|
|
148
|
+
return decodeURIComponent(rawValue);
|
|
149
|
+
} catch (e2) {
|
|
150
|
+
return rawValue;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
121
154
|
}
|
|
122
155
|
}
|
|
156
|
+
|
|
123
157
|
return null;
|
|
124
158
|
}
|
|
125
159
|
|
|
126
160
|
public static setTokenExpiry(expiresInSeconds: number) {
|
|
127
|
-
const expiryTime = Date.now() +
|
|
161
|
+
const expiryTime = Date.now() + expiresInSeconds * 1000;
|
|
128
162
|
Storage.set('glitch_token_expiry', expiryTime);
|
|
129
|
-
|
|
130
|
-
if (Storage.rootDomain && typeof document !== 'undefined') {
|
|
131
|
-
this.setCookie('glitch_token_expiry', expiryTime.toString(), 31);
|
|
132
|
-
}
|
|
133
163
|
}
|
|
134
164
|
|
|
135
165
|
public static getTokenExpiry(): number | null {
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
expiry = Storage.get('glitch_token_expiry');
|
|
139
|
-
}
|
|
140
|
-
return expiry ? parseInt(expiry) : null;
|
|
166
|
+
const expiry = Storage.get('glitch_token_expiry');
|
|
167
|
+
return expiry ? parseInt(String(expiry), 10) : null;
|
|
141
168
|
}
|
|
142
169
|
|
|
143
170
|
public static isTokenExpired(): boolean {
|