Implement Labrinth Canary API flag (#5531)

This commit is contained in:
aecsocket
2026-03-11 15:28:09 +00:00
committed by GitHub
parent 086508be23
commit 3b21944a75
7 changed files with 71 additions and 1 deletions

View File

@@ -0,0 +1,37 @@
import { AbstractFeature, type FeatureConfig } from '../core/abstract-feature'
export const LABRINTH_CANARY_COOKIE = 'labrinth-canary=always'
export interface CanaryCookieConfig extends FeatureConfig {
getCookie?: () => string | undefined | Promise<string | undefined>
}
export class CanaryCookieFeature extends AbstractFeature {
declare protected config: CanaryCookieConfig
constructor(config?: CanaryCookieConfig) {
super(config)
}
shouldApply(context: Parameters<AbstractFeature['shouldApply']>[0]): boolean {
return super.shouldApply(context) && context.options.api === 'labrinth'
}
async execute<T>(next: () => Promise<T>, context: Parameters<AbstractFeature['execute']>[1]) {
const cookie = this.config.getCookie ? await this.config.getCookie() : LABRINTH_CANARY_COOKIE
if (!cookie) {
return next()
}
const headers = { ...(context.options.headers ?? {}) }
const existingCookie = headers.cookie ?? headers.Cookie
if (!existingCookie?.split('; ').includes(cookie)) {
headers.cookie = existingCookie ? `${existingCookie}; ${cookie}` : cookie
delete headers.Cookie
context.options.headers = headers
}
return next()
}
}