Working on it..

This commit is contained in:
May 2023-10-26 19:00:49 +00:00
parent 0366c91f74
commit 70cad2d753
4 changed files with 3673 additions and 2919 deletions

1314
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -6,7 +6,7 @@ import fs from "fs"
import Files from "./lib/files" import Files from "./lib/files"
import { getAccount } from "./lib/middleware" import { getAccount } from "./lib/middleware"
import APIRouter from "./routes/api" import APIRouter from "./routes/api"
import preview from "./preview" import preview from "./routes/preview"
require("dotenv").config() require("dotenv").config()

View file

@ -0,0 +1,92 @@
const base = "https://discord.com/api/v10/"
const buckets = new Map<string, DiscordAPIBucket>()
class DiscordAPIBucket {
readonly name : string // bucket name (X-Ratelimit-Bucket)
// queue : RequestInfo[] = [] // queue of requests to send
readonly limit : number // bucket limit (X-Ratelimit-Limit)
remaining : number // requests remaining (X-Ratelimit-Remaining)
readonly expires : number // when this ratelimit expires (X-Ratelimit-Reset)
readonly expirationHold : ReturnType<typeof setTimeout> // Timeout which fires after this bucket expires
dead : boolean = false // True if bucket has expired
constructor(base: Response) {
this.name = base.headers.get("x-ratelimit-bucket")!
this.limit = parseInt(base.headers.get("x-ratelimit-limit")!)
this.remaining = parseInt(base.headers.get("x-ratelimit-remaining")!)
this.expires = parseFloat(base.headers.get("x-ratelimit-reset")!)
this.expirationHold =
setTimeout(
this.destroy,
parseFloat(base.headers.get("x-ratelimit-reset-after")!)
)
}
/**
* @description Renders this bucket invalid
*/
destroy() {
buckets.delete(this.name)
this.dead = true
Object.freeze(this)
}
/**
* @description update the remainding amount of requests
* @param remaining number to update to
*/
update(remaining: number) {
this.remaining = Math.max(Math.min(0, remaining), this.remaining)
return this
}
}
/**
* @description Returns whether or not a Response's Headers object includes Discord's ratelimit information headers
*/
function checkHeaders(headers: Headers) {
return Boolean(
headers.has("x-ratelimit-bucket")
&& headers.has("x-ratelimit-limit")
&& headers.has("x-ratelimit-remaining")
&& headers.has("x-ratelimit-reset")
&& headers.has("x-ratelimit-reset-after")
)
}
/**
* @description Returns or creates a DiscordAPIBucket from a Response
*/
function getBucket(response: Response) {
if (!checkHeaders(response.headers)) throw new Error("Required ratelimiting headers not found")
if (buckets.has(response.headers.get("x-ratelimit-bucket")!))
return buckets.get(response.headers.get("x-ratelimit-bucket")!)!
else
return new DiscordAPIBucket(response)
}
export class REST {
private readonly token : string
constructor(token:string) {
this.token = token;
}
async fetch(options: RequestInfo) {
}
}

View file

@ -1,9 +1,9 @@
import fs from "fs/promises" import fs from "fs/promises"
import bytes from "bytes" import bytes from "bytes"
import ServeError from "./lib/errors" import ServeError from "../lib/errors"
import * as Accounts from "./lib/accounts" import * as Accounts from "../lib/accounts"
import type { Handler } from "hono" import type { Handler } from "hono"
import type Files from "./lib/files" import type Files from "../lib/files"
const pkg = require(`${process.cwd()}/package.json`) const pkg = require(`${process.cwd()}/package.json`)
export = (files: Files): Handler => export = (files: Files): Handler =>
async (ctx) => { async (ctx) => {