CoolFace
Apppublic

bbbing/bingo

sourceHugging Facemitupdated 3y agoView on Hugging Face
0likes
utils.ts139 linesDownload Raw Back to lib
1import { clsx, type ClassValue } from 'clsx'2import { customAlphabet } from 'nanoid'3import { twMerge } from 'tailwind-merge'4 5export function cn(...inputs: ClassValue[]) {6  return twMerge(clsx(inputs))7}8 9export const nanoid = customAlphabet(10  '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz',11  712) // 7-character random string13 14export function createChunkDecoder() {15  const decoder = new TextDecoder()16  return function (chunk: Uint8Array | undefined): string {17    if (!chunk) return ''18    return decoder.decode(chunk, { stream: true })19  }20}21 22export function random (start: number, end: number) {23  return start + Math.ceil(Math.random() * (end - start))24}25 26export function randomIP() {27  return `11.${random(104, 107)}.${random(1, 255)}.${random(1, 255)}`28}29 30export function parseHeadersFromCurl(content: string) {31  const re = /-H '([^:]+):\s*([^']+)/mg32  const headers: HeadersInit = {}33  content = content.replaceAll('-H "', '-H \'').replaceAll('" ^', '\'\\').replaceAll('^\\^"', '"') // 将 cmd curl 转成 bash curl34  content.replace(re, (_: string, key: string, value: string) => {35    headers[key] = value36    return ''37  })38 39  return headers40}41 42export const ChunkKeys = ['BING_HEADER', 'BING_HEADER1', 'BING_HEADER2']43export function encodeHeadersToCookie(content: string) {44  const base64Content = btoa(content)45  const contentChunks = base64Content.match(/.{1,4000}/g) || []46  return ChunkKeys.map((key, index) => `${key}=${contentChunks[index] ?? ''}`)47}48 49export function extraCurlFromCookie(cookies: Partial<{ [key: string]: string }>) {50  let base64Content = ''51  ChunkKeys.forEach((key) => {52    base64Content += (cookies[key] || '')53  })54  try {55    return atob(base64Content)56  } catch(e) {57    return ''58  }59}60 61export function extraHeadersFromCookie(cookies: Partial<{ [key: string]: string }>) {62  return parseHeadersFromCurl(extraCurlFromCookie(cookies))63}64 65export function formatDate(input: string | number | Date): string {66  const date = new Date(input)67  return date.toLocaleDateString('en-US', {68    month: 'long',69    day: 'numeric',70    year: 'numeric'71  })72}73 74export function parseCookie(cookie: string, cookieName: string) {75  const targetCookie = new RegExp(`(?:[; ]|^)${cookieName}=([^;]*)`).test(cookie) ? RegExp.$1 : cookie76  return targetCookie ? decodeURIComponent(targetCookie).trim() : cookie.indexOf('=') === -1 ? cookie.trim() : ''77}78 79export function parseCookies(cookie: string, cookieNames: string[]) {80  const cookies: { [key: string]: string } = {}81  cookieNames.forEach(cookieName => {82    cookies[cookieName] = parseCookie(cookie, cookieName)83  })84  return cookies85}86 87export const DEFAULT_UA = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36 Edg/115.0.0.0'88export const DEFAULT_IP = process.env.BING_IP || randomIP()89 90export function parseUA(ua?: string, default_ua = DEFAULT_UA) {91  return / EDGE?/i.test(decodeURIComponent(ua || '')) ? decodeURIComponent(ua!.trim()) : default_ua92}93 94export function createHeaders(cookies: Partial<{ [key: string]: string }>, defaultHeaders?: Partial<{ [key: string]: string }>) {95  let {96    BING_COOKIE = process.env.BING_COOKIE,97    BING_UA = process.env.BING_UA,98    BING_IP = process.env.BING_IP,99    BING_HEADER = process.env.BING_HEADER,100  } = cookies101 102  if (BING_HEADER) {103    return extraHeadersFromCookie({104      BING_HEADER,105      ...cookies,106    })107  }108 109  const ua = parseUA(BING_UA)110 111  if (!BING_COOKIE) {112    BING_COOKIE = defaultHeaders?.IMAGE_BING_COOKIE || 'xxx' // hf 暂时不用 Cookie 也可以正常使用113  }114 115  const parsedCookie = parseCookie(BING_COOKIE, '_U')116  if (!parsedCookie) {117    throw new Error('Invalid Cookie')118  }119  return {120    'x-forwarded-for': BING_IP || DEFAULT_IP,121    'Accept-Encoding': 'gzip, deflate, br',122    'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',123    'User-Agent': ua!,124    'x-ms-useragent': 'azsdk-js-api-client-factory/1.0.0-beta.1 core-rest-pipeline/1.10.0 OS/Win32',125    cookie: `_U=${parsedCookie}` || '',126  }127}128 129export class WatchDog {130  private tid = 0131  watch(fn: Function, timeout = 2000) {132    clearTimeout(this.tid)133    this.tid = setTimeout(fn, timeout + Math.random() * 1000)134  }135  reset() {136    clearTimeout(this.tid)137  }138}139