Skip to content

fix: Correct base path for GIF stickers #10330

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions packages/rest/src/lib/CDN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ export interface MakeURLOptions {
* The allowed extensions that can be used
*/
allowedExtensions?: readonly string[];
/**
* The base URL.
*
* @defaultValue `DefaultRestOptions.cdn`
*/
base?: string;
/**
* The extension to use for the image URL
*
Expand All @@ -62,7 +68,10 @@ export interface MakeURLOptions {
* The CDN link builder
*/
export class CDN {
public constructor(private readonly base: string = DefaultRestOptions.cdn) {}
public constructor(
private readonly cdn: string = DefaultRestOptions.cdn,
private readonly mediaProxy: string = DefaultRestOptions.mediaProxy,
) {}

/**
* Generates an app asset URL for a client's asset.
Expand Down Expand Up @@ -287,10 +296,15 @@ export class CDN {
* @param stickerId - The sticker id
* @param extension - The extension of the sticker
* @privateRemarks
* Stickers cannot have a `.webp` extension, so we default to a `.png`
* Stickers cannot have a `.webp` extension, so we default to a `.png`.
* Sticker GIFs do not use the CDN base URL.
*/
public sticker(stickerId: string, extension: StickerExtension = 'png'): string {
return this.makeURL(`/stickers/${stickerId}`, { allowedExtensions: ALLOWED_STICKER_EXTENSIONS, extension });
return this.makeURL(`/stickers/${stickerId}`, {
allowedExtensions: ALLOWED_STICKER_EXTENSIONS,
base: extension === 'gif' ? this.mediaProxy : this.cdn,
extension,
});
}

/**
Expand Down Expand Up @@ -352,7 +366,12 @@ export class CDN {
*/
private makeURL(
route: string,
{ allowedExtensions = ALLOWED_EXTENSIONS, extension = 'webp', size }: Readonly<MakeURLOptions> = {},
{
allowedExtensions = ALLOWED_EXTENSIONS,
base = this.cdn,
extension = 'webp',
size,
}: Readonly<MakeURLOptions> = {},
): string {
// eslint-disable-next-line no-param-reassign
extension = String(extension).toLowerCase();
Expand All @@ -365,7 +384,7 @@ export class CDN {
throw new RangeError(`Invalid size provided: ${size}\nMust be one of: ${ALLOWED_SIZES.join(', ')}`);
}

const url = new URL(`${this.base}${route}.${extension}`);
const url = new URL(`${base}${route}.${extension}`);

if (size) {
url.searchParams.set('size', String(size));
Expand Down
2 changes: 1 addition & 1 deletion packages/rest/src/lib/REST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export class REST extends AsyncEventEmitter<RestEvents> {

public constructor(options: Partial<RESTOptions> = {}) {
super();
this.cdn = new CDN(options.cdn ?? DefaultRestOptions.cdn);
this.cdn = new CDN(options.cdn ?? DefaultRestOptions.cdn, options.mediaProxy ?? DefaultRestOptions.mediaProxy);
this.options = { ...DefaultRestOptions, ...options };
this.globalRemaining = Math.max(1, this.options.globalRequestsPerSecond);
this.agent = options.agent ?? null;
Expand Down
1 change: 1 addition & 0 deletions packages/rest/src/lib/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const DefaultRestOptions = {
async makeRequest(...args): Promise<ResponseLike> {
return getDefaultStrategy()(...args);
},
mediaProxy: 'https://media.discordapp.net',
} as const satisfies Required<RESTOptions>;

/**
Expand Down
6 changes: 6 additions & 0 deletions packages/rest/src/lib/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ export interface RESTOptions {
* For example, to use global fetch, simply provide `makeRequest: fetch`
*/
makeRequest(url: string, init: RequestInit): Promise<ResponseLike>;
/**
* The media proxy path
*
* @defaultValue `'https://media.discordapp.net'`
*/
mediaProxy: string;
/**
* The extra offset to add to rate limits in milliseconds
*
Expand Down