@hivedev/hivesdk 1.0.17 → 1.0.25
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/hive-client.js +176 -5
- package/hive-server.cjs +421 -76
- package/hive-server.js +443 -75
- package/package.json +2 -1
package/hive-client.js
CHANGED
|
@@ -170,15 +170,186 @@ async function login() {
|
|
|
170
170
|
}
|
|
171
171
|
popup.close();
|
|
172
172
|
if (loginResponse.status == 200) {
|
|
173
|
-
window.location.href = "/
|
|
174
|
-
} else if (loginResponse.status == 400) {
|
|
175
|
-
alert("Invalid Format!");
|
|
176
|
-
} else if (loginResponse.status == 401) {
|
|
177
|
-
alert("Invalid Credentials!");
|
|
173
|
+
window.location.href = "/";
|
|
178
174
|
}
|
|
179
175
|
};
|
|
180
176
|
}
|
|
177
|
+
|
|
178
|
+
// Common/Enumerations/SessionTokenStorageMethod.js
|
|
179
|
+
var sessionStorageMethod = {
|
|
180
|
+
HTTP_ONLY_COOKIE: 0,
|
|
181
|
+
LOCAL_STORAGE: 1,
|
|
182
|
+
SESSION_STORAGE: 2
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
// Common/Utility/Clamp.js
|
|
186
|
+
function clamp(value, min, max) {
|
|
187
|
+
return Math.min(Math.max(value, min), max);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// Common/Utility/Delay.js
|
|
191
|
+
function delay(ms) {
|
|
192
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// Common/Enumerations/AppWebViewInteractions.js
|
|
196
|
+
var appWebViewInteractions = {
|
|
197
|
+
REQUEST_NOTIFICATION_TOKEN: 0,
|
|
198
|
+
BACK_PRESSED: 1
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
// Client/Utility/IsInApp.js
|
|
202
|
+
function isInApp() {
|
|
203
|
+
return window.self !== window.top;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// Client/Utility/SetupNotifications.js
|
|
207
|
+
async function setupNotifications2() {
|
|
208
|
+
if (!isInApp()) return;
|
|
209
|
+
const lastSetup = localStorage.getItem("lastNotificationSetup");
|
|
210
|
+
const FIFTEEN_MINUTES = 15 * 60 * 1e3;
|
|
211
|
+
if (lastSetup && Date.now() - Number(lastSetup) < FIFTEEN_MINUTES) {
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
window.parent.postMessage({
|
|
215
|
+
"interaction": appWebViewInteractions.REQUEST_NOTIFICATION_TOKEN
|
|
216
|
+
}, "*");
|
|
217
|
+
window.onmessage = async (event) => {
|
|
218
|
+
try {
|
|
219
|
+
const messageObject = typeof event.data === "string" ? JSON.parse(event.data) : event.data;
|
|
220
|
+
switch (messageObject.interaction) {
|
|
221
|
+
case appWebViewInteractions.REQUEST_NOTIFICATION_TOKEN: {
|
|
222
|
+
const response = await fetch(
|
|
223
|
+
"/PushNotificationToken",
|
|
224
|
+
{
|
|
225
|
+
method: "POST",
|
|
226
|
+
headers: {
|
|
227
|
+
"Content-Type": "application/json",
|
|
228
|
+
"x-device-id": await getDeviceId(),
|
|
229
|
+
"x-session-token": localStorage.getItem("sessionToken") || ""
|
|
230
|
+
},
|
|
231
|
+
body: JSON.stringify({
|
|
232
|
+
notificationToken: messageObject.notificationToken
|
|
233
|
+
})
|
|
234
|
+
}
|
|
235
|
+
);
|
|
236
|
+
if (response.ok) {
|
|
237
|
+
localStorage.setItem("lastNotificationSetup", Date.now().toString());
|
|
238
|
+
} else {
|
|
239
|
+
alert("Failed to enable notifications!");
|
|
240
|
+
}
|
|
241
|
+
break;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
} catch (error) {
|
|
245
|
+
console.error(error);
|
|
246
|
+
}
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
// Common/Utility/IsClientLoggedIn.js
|
|
251
|
+
async function isClientLoggedIn() {
|
|
252
|
+
const response = await fetch(
|
|
253
|
+
"/IsLoggedIn",
|
|
254
|
+
{
|
|
255
|
+
method: "GET",
|
|
256
|
+
credentials: "include",
|
|
257
|
+
headers: {
|
|
258
|
+
"x-device-id": await getDeviceId(),
|
|
259
|
+
"x-session-token": localStorage.getItem("sessionToken") || ""
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
);
|
|
263
|
+
if (response.status != 200) {
|
|
264
|
+
return false;
|
|
265
|
+
}
|
|
266
|
+
const isLoggedIn = await response.json();
|
|
267
|
+
if (isLoggedIn) {
|
|
268
|
+
setupNotifications2();
|
|
269
|
+
}
|
|
270
|
+
return isLoggedIn;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
// Client/Utility/ScheduleLoginCheck.js
|
|
274
|
+
function scheduleLoginCheck(interval = 3e4) {
|
|
275
|
+
interval = clamp(interval, 1e4, 6e4);
|
|
276
|
+
window.IS_LOGGED_IN = false;
|
|
277
|
+
const checkLoginState = async () => {
|
|
278
|
+
let attempts = 0;
|
|
279
|
+
let success = false;
|
|
280
|
+
while (attempts < 2 && !success) {
|
|
281
|
+
try {
|
|
282
|
+
const isLoggedIn = await isClientLoggedIn();
|
|
283
|
+
window.IS_LOGGED_IN = isLoggedIn;
|
|
284
|
+
window.dispatchEvent(new CustomEvent("login-state-changed", { detail: isLoggedIn }));
|
|
285
|
+
success = true;
|
|
286
|
+
} catch (error) {
|
|
287
|
+
attempts++;
|
|
288
|
+
if (attempts >= 3) {
|
|
289
|
+
window.IS_LOGGED_IN = false;
|
|
290
|
+
window.dispatchEvent(new CustomEvent("login-state-changed", { detail: window.IS_LOGGED_IN }));
|
|
291
|
+
}
|
|
292
|
+
await delay(1e3);
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
};
|
|
296
|
+
checkLoginState();
|
|
297
|
+
setInterval(checkLoginState, interval);
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
// Client/Utility/SetupAppUsage.js
|
|
301
|
+
function setupAppUsage() {
|
|
302
|
+
document.cookie = "usesCookies=true; path=/";
|
|
303
|
+
if (isInApp()) {
|
|
304
|
+
window.fallbackOpen = window.open;
|
|
305
|
+
window.open = (url, name, features) => {
|
|
306
|
+
const iframe = document.createElement("iframe");
|
|
307
|
+
iframe.src = url;
|
|
308
|
+
iframe.style.position = "fixed";
|
|
309
|
+
iframe.style.display = "block";
|
|
310
|
+
iframe.style.height = "100vh";
|
|
311
|
+
iframe.style.width = "100vw";
|
|
312
|
+
iframe.style.top = "0px";
|
|
313
|
+
iframe.style.left = "0px";
|
|
314
|
+
iframe.style.border = "none";
|
|
315
|
+
iframe.style.outline = "none";
|
|
316
|
+
iframe.style.boxShadow = "none";
|
|
317
|
+
iframe.style.zIndex = "10000";
|
|
318
|
+
document.body.appendChild(iframe);
|
|
319
|
+
return {
|
|
320
|
+
close: () => {
|
|
321
|
+
iframe.remove();
|
|
322
|
+
}
|
|
323
|
+
};
|
|
324
|
+
};
|
|
325
|
+
document.querySelectorAll("img").forEach((img) => {
|
|
326
|
+
if (img.getAttribute("src") && !img.src.startsWith("http")) {
|
|
327
|
+
img.src = new URL(img.getAttribute("src"), window.location.origin).href;
|
|
328
|
+
}
|
|
329
|
+
});
|
|
330
|
+
window.addEventListener("message", (event) => {
|
|
331
|
+
try {
|
|
332
|
+
const messageObject = typeof event.data === "string" ? JSON.parse(event.data) : event.data;
|
|
333
|
+
switch (messageObject.interaction) {
|
|
334
|
+
case appWebViewInteractions.BACK_PRESSED: {
|
|
335
|
+
window.history.back();
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
} catch (error) {
|
|
339
|
+
return;
|
|
340
|
+
}
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
// Client/InitClient.js
|
|
346
|
+
async function initClient({ loginTokenStorageMethod = sessionStorageMethod.HTTP_ONLY_COOKIE }) {
|
|
347
|
+
window.SESSION_TOKEN_STORAGE_METHOD = loginTokenStorageMethod || sessionStorageMethod.HTTP_ONLY_COOKIE;
|
|
348
|
+
setupAppUsage();
|
|
349
|
+
scheduleLoginCheck();
|
|
350
|
+
}
|
|
181
351
|
export {
|
|
182
352
|
getDeviceId,
|
|
353
|
+
initClient,
|
|
183
354
|
login
|
|
184
355
|
};
|