-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw.js
184 lines (178 loc) · 5.35 KB
/
sw.js
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
const CACHE_NAME = 'ICDNCache';
let cachelist = [];
self.addEventListener('install', async function (installEvent) {
self.skipWaiting();
installEvent.waitUntil(
caches.open(CACHE_NAME)
.then(function (cache) {
console.log('Opened cache');
return cache.addAll(cachelist);
})
);
});
self.addEventListener('fetch', async event => {
try {
event.respondWith(handle(event.request))
} catch (msg) {
event.respondWith(handleerr(event.request, msg))
}
});
const handleerr = async (req, msg) => {
return new Response(`<h1>CDN分流器遇到了致命错误</h1>
<b>${msg}</b>`, { headers: { "content-type": "text/html; charset=utf-8" } })
}
let cdn = {
"gh": {
jsdelivr: {
"url": "https://cdn.jsdelivr.net/gh"
},
pigax_jsd: {
"url": "https://u.pigax.cn/gh"
},
pigax_chenyfan_jsd: {
"url": "https://cdn-jsd.pigax.cn/gh"
},
tianli: {
"url": "https://cdn1.tianli0.top/gh"
},
fastly: {
"url": "https://fastly.jsdelivr.net/gh"
},
//白嫖
//cdn.cnortles.top jsd.hin.cool
cnortles: {
"url": "https://cdn.cnortles.top/gh"
},
hin_cool: {
"url": "https://jsd.hin.cool/gh"
}
},
"combine": {
jsdelivr: {
"url": "https://cdn.jsdelivr.net/combine"
},
pigax_jsd: {
"url": "https://u.pigax.cn/combine"
},
pigax_chenyfan_jsd: {
"url": "https://cdn-jsd.pigax.cn/combine"
},
tianli: {
"url": "https://cdn1.tianli0.top/combine"
},
//cdn.cnortles.top jsd.hin.cool
cnortles: {
"url": "https://cdn.cnortles.top/combine"
},
hin_cool: {
"url": "https://jsd.hin.cool/combine"
}
},
"npm": {
eleme: {
"url": "https://npm.elemecdn.com"
},
jsdelivr: {
"url": "https://cdn.jsdelivr.net/npm"
},
zhimg: {
"url": "https://unpkg.zhimg.com"
},
unpkg: {
"url": "https://unpkg.com"
},
bdstatic: {
"url": "https://code.bdstatic.com/npm"
},
pigax_jsd: {
"url": "https://u.pigax.cn/npm"
},
pigax_unpkg: {
"url": "https://unpkg.pigax.cn/"
},
pigax_chenyfan_jsd: {
"url": "https://cdn-jsd.pigax.cn/npm"
},
tianli: {
"url": "https://cdn1.tianli0.top/npm"
},
//cdn.cnortles.top jsd.hin.cool
cnortles: {
"url": "https://cdn.cnortles.top/npm"
},
hin_cool: {
"url": "https://jsd.hin.cool/npm"
}
}
}
const handle = async function (req) {
const urlStr = req.url
const domain = (urlStr.split('/'))[2]
let urls = []
for (let i in cdn) {
for (let j in cdn[i]) {
if (domain == cdn[i][j].url.split('https://')[1].split('/')[0] && urlStr.match(cdn[i][j].url)) {
urls = []
for (let k in cdn[i]) {
urls.push(urlStr.replace(cdn[i][j].url, cdn[i][k].url))
}
if (urlStr.indexOf('@latest/') > -1) {
return lfetch(urls, urlStr)
} else {
return caches.match(req).then(function (resp) {
return resp || lfetch(urls, urlStr).then(function (res) {
return caches.open(CACHE_NAME).then(function (cache) {
cache.put(req, res.clone());
return res;
});
});
})
}
}
}
}
return fetch(req)
}
const lfetch = async (urls, url) => {
let controller = new AbortController();
const PauseProgress = async (res) => {
return new Response(await (res).arrayBuffer(), { status: res.status, headers: res.headers });
};
if (!Promise.any) {
Promise.any = function (promises) {
return new Promise((resolve, reject) => {
promises = Array.isArray(promises) ? promises : []
let len = promises.length
let errs = []
if (len === 0) return reject(new AggregateError('All promises were rejected'))
promises.forEach((promise) => {
promise.then(value => {
resolve(value)
}, err => {
len--
errs.push(err)
if (len === 0) {
reject(new AggregateError(errs))
}
})
})
})
}
}
return Promise.any(urls.map(urls => {
return new Promise((resolve, reject) => {
fetch(urls, {
signal: controller.signal
})
.then(PauseProgress)
.then(res => {
if (res.status == 200) {
controller.abort();
resolve(res)
} else {
reject(res)
}
})
})
}))
}