75 lines
2.3 KiB
TypeScript
75 lines
2.3 KiB
TypeScript
export const jsonResponse = (data: any, status = 200) => {
|
|
return new Response(JSON.stringify(data, null, 2), {
|
|
status,
|
|
headers: { 'Content-Type': 'application/json' }
|
|
})
|
|
}
|
|
|
|
export const splitGd = (
|
|
input: string,
|
|
splitKey: string = ':'
|
|
): Record<string, string> => {
|
|
const parts = input.split(splitKey)
|
|
const result: Record<string, string> = {}
|
|
for (let i = 0; i < parts.length - 1; i += 2) {
|
|
result[parts[i]] = parts[i + 1]
|
|
}
|
|
return result
|
|
}
|
|
|
|
export const encodeGJP2 = async (password = '', salt = 'mI29fmAnxgTs') => {
|
|
const data = new TextEncoder().encode(password + salt)
|
|
const hashBuffer = await crypto.subtle.digest('SHA-1', data)
|
|
return Array.from(new Uint8Array(hashBuffer))
|
|
.map(b => b.toString(16).padStart(2, '0'))
|
|
.join('')
|
|
}
|
|
|
|
export const generateTimeLength = (ms: number): string => {
|
|
const minutes = Math.floor(ms / 60000)
|
|
const seconds = Math.floor((ms % 60000) / 1000)
|
|
const milliseconds = ms % 1000
|
|
|
|
const secStr = seconds.toString().padStart(2, '0')
|
|
const msStr = milliseconds.toString().padStart(3, '0')
|
|
|
|
return `${minutes}:${secStr}.${msStr}`
|
|
}
|
|
|
|
export const formatTime = (seconds: number): string => {
|
|
const units = [
|
|
{ label: 'y', value: 365 * 24 * 3600 },
|
|
{ label: 'mo', value: 30 * 24 * 3600 },
|
|
{ label: 'd', value: 24 * 3600 },
|
|
{ label: 'h', value: 3600 },
|
|
{ label: 'm', value: 60 },
|
|
{ label: 's', value: 1 }
|
|
]
|
|
|
|
const parts = []
|
|
for (const { label, value } of units) {
|
|
const amount = Math.floor(seconds / value)
|
|
if (amount) {
|
|
parts.push(`${amount}${label}`)
|
|
seconds %= value
|
|
}
|
|
}
|
|
|
|
return parts.join(' ') || '0s'
|
|
}
|
|
|
|
//Taken from https://github.com/GDColon/GDBrowser/blob/master/index.js#L164-L173
|
|
export const parseResponse = (responseBody: string, splitter = ':') => {
|
|
if (!responseBody || responseBody == '-1') return {}
|
|
if (responseBody.startsWith('\nWarning:'))
|
|
responseBody = responseBody.split('\n').slice(2).join('\n').trim() // GDPS'es are wild
|
|
if (responseBody.startsWith('<br />'))
|
|
responseBody = responseBody.split('<br />').slice(2).join('<br />').trim() // Seriously screw this
|
|
let response = responseBody.split('#')[0].split(splitter)
|
|
let res: any = {}
|
|
for (let i = 0; i < response.length; i += 2) {
|
|
res[response[i]] = response[i + 1]
|
|
}
|
|
return res
|
|
}
|