feat: adopt shared internal SSO sessions and add no-sso launch mode

This commit is contained in:
SantaSpeen committed 2026-10-24 15:41:39 +03:00
1 parent c209f1effc
commit c02bf755f2
51 files changed
+2131 -362

No files matched your search

+10
View File
@@ -1,3 +1,4 @@
import { sessionScope, sessionIsCurrent } from "@/utils/session-scope";
import { useLayoutStore } from "@/stores/layout";
import { baseURL } from "@/utils/constants";
import { upload as postTus, useTus } from "./tus";
@@ -112,6 +113,7 @@ export async function post(
overwrite = false,
onupload: any = () => {}
) {
const scope = sessionScope();
// Use the pre-existing API if:
const useResourcesApi =
// a folder is being created
@@ -121,6 +123,7 @@ export async function post(
!["http:", "https:"].includes(window.location.protocol)) ||
// Tus is disabled / not applicable
!(await useTus(content));
if (!sessionIsCurrent(scope.generation)) throw new Error("Upload aborted");
return useResourcesApi
? postResources(url, content, overwrite, onupload)
: postTus(url, content, overwrite, onupload);
@@ -143,6 +146,7 @@ async function postResources(
}
return new Promise((resolve, reject) => {
const scope = sessionScope();
const request = new XMLHttpRequest();
resourceUploads.add(request);
request.onloadend = () => resourceUploads.delete(request);
@@ -153,12 +157,17 @@ async function postResources(
true
);
request.setRequestHeader("X-FB-Request", "1");
request.setRequestHeader("X-FB-Session-State", scope.state);
if (typeof onupload === "function") {
request.upload.onprogress = onupload;
}
request.onload = () => {
if (!sessionIsCurrent(scope.generation)) {
reject(new Error("Upload aborted"));
return;
}
if (request.status === 200) {
resolve(request.responseText);
} else if (request.status === 409) {
@@ -225,6 +234,7 @@ export function getPreviewURL(file: ResourceItem, size: string) {
const params = {
inline: "true",
key: Date.parse(file.modified),
account: sessionScope().generation,
};
return createURL("api/preview/" + size + file.path, params);
+7 -2
View File
@@ -1,3 +1,4 @@
import { sessionScope } from "@/utils/session-scope";
import * as tus from "tus-js-client";
import { baseURL, tusEndpoint, tusSettings, origin } from "@/utils/constants";
import { removePrefix } from "@/api/utils";
@@ -17,6 +18,7 @@ export async function upload(
throw new Error("Tus.io settings are not defined");
}
const scope = sessionScope();
filePath = removePrefix(filePath);
const resourcePath = `${tusEndpoint}${filePath}?override=${overwrite}`;
@@ -33,6 +35,7 @@ export async function upload(
storeFingerprintForResuming: false,
headers: {
"X-FB-Request": "1",
"X-FB-Session-State": scope.state,
},
onShouldRetry: function (err) {
const status = err.originalResponse
@@ -40,7 +43,7 @@ export async function upload(
: 0;
// Do not retry for file conflict.
if (status === 409 || status === 401 || status === 403) {
if ([409, 401, 403, 428, 503].includes(status)) {
return false;
}
@@ -109,7 +112,9 @@ function isTusSupported() {
export function abortAllUploads() {
for (const filePath in CURRENT_UPLOAD_LIST) {
if (CURRENT_UPLOAD_LIST[filePath]) {
CURRENT_UPLOAD_LIST[filePath].abort(true);
void CURRENT_UPLOAD_LIST[filePath].abort(true).catch(() => {
/* A switched account cannot cancel the previous account's server transfer. */
});
CURRENT_UPLOAD_LIST[filePath].options!.onError!(
new Error("Upload aborted")
);
+43 -6
View File
@@ -1,5 +1,9 @@
import { useAuthStore } from "@/stores/auth";
import { startLogin } from "@/utils/auth";
import {
sessionScope,
sessionIsCurrent,
mergeSignals,
} from "@/utils/session-scope";
import { validateLogin, acceptSession } from "@/utils/auth";
import { baseURL } from "@/utils/constants";
import { encodePath } from "@/utils/url";
@@ -19,7 +23,7 @@ export async function fetchURL(
opts: ApiOpts,
auth = true
): Promise<Response> {
const authStore = useAuthStore();
const scope = sessionScope();
opts = opts || {};
opts.headers = opts.headers || {};
@@ -30,9 +34,13 @@ export async function fetchURL(
res = await fetch(`${baseURL}${url}`, {
headers: {
"X-FB-Request": "1",
"X-FB-Session-State": scope.state,
...headers,
},
...rest,
signal: opts.signal
? mergeSignals(opts.signal, scope.signal)
: scope.signal,
});
} catch (e) {
// Check if the error is an intentional cancellation
@@ -42,6 +50,19 @@ export async function fetchURL(
throw new StatusError("000 No connection", 0);
}
if (!sessionIsCurrent(scope.generation))
throw new StatusError("Session changed", 409, true);
for (const method of ["json", "text", "arrayBuffer", "blob"] as const) {
const read = res[method].bind(res);
Object.defineProperty(res, method, {
value: async () => {
const result = await read();
if (!sessionIsCurrent(scope.generation))
throw new StatusError("Session changed", 409, true);
return result;
},
});
}
if (res.status < 200 || res.status > 299) {
const body = await res.text();
const error = new StatusError(
@@ -49,10 +70,26 @@ export async function fetchURL(
res.status
);
if (auth && res.status == 401) {
authStore.clearUser();
startLogin();
if (
[401, 403, 409, 428, 503].includes(res.status) &&
!url.startsWith("/api/public/")
) {
try {
const state = JSON.parse(body);
if (typeof state.status === "string")
acceptSession({
status:
state.status === "session_changed" ? "checking" : state.status,
authenticated: false,
session_state: state.session_state,
required_factors: state.required_factors,
});
} catch {
/* Ordinary file permission errors do not change identity. */
}
void validateLogin();
}
void auth;
throw error;
}