fix(core): fix ios blob upload (#10263)

This commit is contained in:
EYHN
2025-02-19 10:07:46 +08:00
committed by GitHub
parent b20d316d60
commit 5a7ab880c1
21 changed files with 481 additions and 226 deletions

View File

@@ -1,62 +1,2 @@
import '@affine/core/bootstrap/browser';
/**
* the below code includes the custom fetch and xmlhttprequest implementation for ios webview.
* should be included in the entry file of the app or webworker.
*/
/*
* we override the browser's fetch function with our custom fetch function to
* overcome the restrictions of cross-domain and third-party cookies in ios webview.
*
* the custom fetch function will convert the request to `affine-http://` or `affine-https://`
* and send the request to the server.
*/
const rawFetch = globalThis.fetch;
globalThis.fetch = async (input: RequestInfo | URL, init?: RequestInit) => {
let url = new URL(
typeof input === 'string'
? input
: input instanceof URL
? input.toString()
: input.url,
globalThis.location.origin
).href;
if (url.startsWith('capacitor:')) {
return rawFetch(input, init);
}
if (url.startsWith('http:')) {
url = 'affine-http:' + url.slice(5);
}
if (url.startsWith('https:')) {
url = 'affine-https:' + url.slice(6);
}
return rawFetch(url, input instanceof Request ? input : init);
};
const rawXMLHttpRequest = globalThis.XMLHttpRequest;
globalThis.XMLHttpRequest = class extends rawXMLHttpRequest {
override open(
method: string,
url: string,
async?: boolean,
user?: string,
password?: string
) {
let normalizedUrl = new URL(url, globalThis.location.origin).href;
if (normalizedUrl.startsWith('http:')) {
url = 'affine-http:' + url.slice(5);
}
if (normalizedUrl.startsWith('https:')) {
url = 'affine-https:' + url.slice(6);
}
(super.open as any)(method, url, async, user, password);
}
};
import './proxy';