-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
28 lines (23 loc) · 879 Bytes
/
index.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
const welcome = async (): Promise<string> => {
return 'Hello World!'
}
console.log('acc', await getUtf8StrBodyContent('https://www.accommodation.cam.ac.uk/versionjson.txt'))
console.log('prs', await getUtf8StrBodyContent('https://www.prs.uk.com/versionjson.txt'))
async function getUtf8StrBodyContent(url: string) {
const bodyContent = await getBodyContent(url)
const decoder = new TextDecoder(getUtf8Or16FromFirstTwoBytes(bodyContent.value))
return decoder.decode(bodyContent.value)
}
async function getBodyContent(url: string) {
const res = await fetch(url)
const bodyContent = await res.body.getReader().read()
return bodyContent
}
function getUtf8Or16FromFirstTwoBytes(bodyContent: Uint8Array) {
if (bodyContent.length >= 2 && bodyContent.slice(0, 2).toString() === '255,254') {
return 'utf-16'
} else {
return 'utf-8'
}
}
export { welcome }