forked from nuxt/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch.ts
31 lines (28 loc) · 883 Bytes
/
fetch.ts
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
export async function getScriptSize(url: string) {
const compressedResponse = await fetch(url, { headers: { 'Accept-Encoding': 'gzip' } })
return bytesToSize(await getResponseSize(compressedResponse))
}
async function getResponseSize(response) {
const reader = response.body.getReader()
const contentLength = +response.headers.get('Content-Length')
if (contentLength) {
return contentLength
}
else {
let total = 0
while (true) {
const { done, value } = await reader.read()
if (done)
return total
total += value.length
}
}
}
function bytesToSize(bytes: number) {
// be precise to 2 decimal places
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB']
if (bytes === 0)
return '0 Byte'
const i = Math.floor(Math.log(bytes) / Math.log(1024))
return `${Number.parseFloat((bytes / 1024 ** i).toFixed(2))} ${sizes[i]}`
}