mirror of
https://github.com/anomalyco/opencode.git
synced 2026-08-24 06:33:01 -04:00
40 lines
1.3 KiB
TypeScript
40 lines
1.3 KiB
TypeScript
export function base64Encode(value: string) {
|
|
const bytes = new TextEncoder().encode(value)
|
|
const binary = Array.from(bytes, (byte) => String.fromCharCode(byte)).join("")
|
|
return btoa(binary).replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "")
|
|
}
|
|
|
|
export function base64Decode(value: string) {
|
|
const binary = atob(value.replace(/-/g, "+").replace(/_/g, "/"))
|
|
return new TextDecoder().decode(Uint8Array.from(binary, (character) => character.charCodeAt(0)))
|
|
}
|
|
|
|
export function checksum(content: string): string | undefined {
|
|
if (!content) return
|
|
let hash = 0x811c9dc5
|
|
for (let index = 0; index < content.length; index++) {
|
|
hash ^= content.charCodeAt(index)
|
|
hash = Math.imul(hash, 0x01000193)
|
|
}
|
|
return (hash >>> 0).toString(36)
|
|
}
|
|
|
|
export function sampledChecksum(content: string, limit = 500_000): string | undefined {
|
|
if (!content) return
|
|
if (content.length <= limit) return checksum(content)
|
|
|
|
const size = 4096
|
|
return `${content.length}:${[
|
|
0,
|
|
Math.floor(content.length * 0.25),
|
|
Math.floor(content.length * 0.5),
|
|
Math.floor(content.length * 0.75),
|
|
content.length - size,
|
|
]
|
|
.map((point) => {
|
|
const start = Math.max(0, Math.min(content.length - size, point - Math.floor(size / 2)))
|
|
return checksum(content.slice(start, start + size)) ?? ""
|
|
})
|
|
.join(":")}`
|
|
}
|