mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-13 01:53:30 +01:00
feat: no-de-no-de, now with extra buns (#9683)
BREAKING CHANGE: The REST and RequestManager classes now extend AsyncEventEmitter from `@vladfrangu/async_event_emitter`, which aids in cross-compatibility between Node, Deno, Bun, CF Workers, Vercel Functions, etc. BREAKING CHANGE: DefaultUserAgentAppendix has been adapted to support multiple different platforms (previously mentioned Deno, Bun, CF Workers, etc) BREAKING CHANGE: the entry point for `@discordjs/rest` will now differ in non-node-like environments (CF Workers, etc.) Co-authored-by: Suneet Tipirneni <77477100+suneettipirneni@users.noreply.github.com> Co-authored-by: Jiralite <33201955+Jiralite@users.noreply.github.com> Co-authored-by: suneettipirneni <suneettipirneni@icloud.com>
This commit is contained in:
52
packages/util/src/functions/userAgentAppendix.ts
Normal file
52
packages/util/src/functions/userAgentAppendix.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
/* eslint-disable n/prefer-global/process */
|
||||
/* eslint-disable no-restricted-globals */
|
||||
|
||||
/**
|
||||
* Resolves the user agent appendix string for the current environment.
|
||||
*/
|
||||
export function getUserAgentAppendix(): string {
|
||||
// https://vercel.com/docs/concepts/functions/edge-functions/edge-runtime#check-if-you're-running-on-the-edge-runtime
|
||||
// @ts-expect-error Vercel Edge functions
|
||||
if (typeof globalThis.EdgeRuntime !== 'undefined') {
|
||||
return 'Vercel-Edge-Functions';
|
||||
}
|
||||
|
||||
// @ts-expect-error Cloudflare Workers
|
||||
if (typeof globalThis.R2 !== 'undefined' && typeof globalThis.WebSocketPair !== 'undefined') {
|
||||
// https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent
|
||||
return 'Cloudflare-Workers';
|
||||
}
|
||||
|
||||
// https://docs.netlify.com/edge-functions/api/#netlify-global-object
|
||||
// @ts-expect-error Netlify Edge functions
|
||||
if (typeof globalThis.Netlify !== 'undefined') {
|
||||
return 'Netlify-Edge-Functions';
|
||||
}
|
||||
|
||||
// Most (if not all) edge environments will have `process` defined. Within a web browser we'll extract it using `navigator.userAgent`.
|
||||
if (typeof globalThis.process !== 'object') {
|
||||
// @ts-expect-error web env
|
||||
if (typeof globalThis.navigator === 'object') {
|
||||
// @ts-expect-error web env
|
||||
return globalThis.navigator.userAgent;
|
||||
}
|
||||
|
||||
return 'UnknownEnvironment';
|
||||
}
|
||||
|
||||
if ('versions' in globalThis.process) {
|
||||
if ('deno' in globalThis.process.versions) {
|
||||
return `Deno/${globalThis.process.versions.deno}`;
|
||||
}
|
||||
|
||||
if ('bun' in globalThis.process.versions) {
|
||||
return `Bun/${globalThis.process.versions.bun}`;
|
||||
}
|
||||
|
||||
if ('node' in globalThis.process.versions) {
|
||||
return `Node.js/${globalThis.process.versions.node}`;
|
||||
}
|
||||
}
|
||||
|
||||
return 'UnknownEnvironment';
|
||||
}
|
||||
Reference in New Issue
Block a user