forked from utags/userscripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch.ts
More file actions
168 lines (143 loc) · 4.49 KB
/
Copy pathfetch.ts
File metadata and controls
168 lines (143 loc) · 4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import { xmlHttpRequest } from './gm/index'
type FetchStatus = 'unknown' | 'testing' | 'working' | 'broken'
const originStatus = new Map<string, FetchStatus>()
const originQueue = new Map<string, Array<() => void>>()
function getOrigin(url: string): string {
try {
return new URL(url, globalThis.location?.href).origin
} catch {
return 'default'
}
}
function flushOriginQueue(origin: string) {
const queue = originQueue.get(origin) || []
originQueue.delete(origin)
for (const req of queue) {
req()
}
}
export function resetFetchStatus() {
originStatus.clear()
originQueue.clear()
}
export function getFetchStatus(url: string): FetchStatus {
return originStatus.get(getOrigin(url)) || 'unknown'
}
export function fetchWithGmFallback(options: {
method?: string
url: string
responseType?: string
timeout?: number
onload?: (response: any) => void
onerror?: (error: unknown) => void
ontimeout?: () => void
}): void {
const {
method = 'GET',
url,
responseType,
timeout,
onload,
onerror,
ontimeout,
} = options
const origin = getOrigin(url)
const status = originStatus.get(origin) || 'unknown'
if (status === 'broken') {
xmlHttpRequest({ ...options, method })
return
}
const performFetch = () => {
void (async () => {
const controller = new AbortController()
let timeoutId: number | undefined
if (timeout && timeout > 0) {
timeoutId = setTimeout(() => {
controller.abort()
}, timeout) as unknown as number
}
try {
const res = await fetch(url, { method, signal: controller.signal })
if (timeoutId) clearTimeout(timeoutId)
// If fetch succeeds (even 404), we might want to use it if it's a valid HTTP response.
// However, for CSP/Network errors, fetch throws.
// For 404, fetch returns ok=false.
// If we want to fallback to GM on 404 (maybe GM has different headers/cookies?), we could.
// But usually for static assets, 404 is 404.
// The user's goal is caching.
// If we get a response (even 404), fetch is working (CSP didn't block it)
const currentStatus = originStatus.get(origin)
if (currentStatus === 'testing') {
originStatus.set(origin, 'working')
flushOriginQueue(origin)
}
if (res.ok || res.status === 304) {
let response: any
let responseText: string | undefined
// eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check
switch (responseType) {
case 'blob': {
response = await res.blob()
break
}
case 'json': {
response = await res.json()
responseText = JSON.stringify(response)
break
}
case 'arraybuffer': {
response = await res.arrayBuffer()
break
}
default: {
responseText = await res.text()
response = responseText
break
}
}
onload?.({
status: res.status,
statusText: res.statusText,
response,
responseText,
finalUrl: res.url,
})
return
}
throw new Error(`Fetch failed: ${res.status}`)
} catch (error: unknown) {
if (timeoutId) clearTimeout(timeoutId)
const isHttpError =
error instanceof Error && error.message.startsWith('Fetch failed:')
if (!isHttpError) {
const currentStatus = originStatus.get(origin)
if (currentStatus === 'testing') {
originStatus.set(origin, 'broken')
flushOriginQueue(origin)
} else if (currentStatus === 'working') {
// Should not happen if logic is correct, but network might fail later
// We assume if it worked once, it works. But if it fails now with network error,
// we should probably fallback for this request.
// For now, let's keep it simple: fallback this request.
}
}
xmlHttpRequest({ ...options, method })
}
})()
}
if (status === 'working') {
performFetch()
return
}
if (status === 'testing') {
const queue = originQueue.get(origin) || []
queue.push(() => {
fetchWithGmFallback(options)
})
originQueue.set(origin, queue)
return
}
// status === 'unknown'
originStatus.set(origin, 'testing')
performFetch()
}