@discordjs/proxy 3.0.0-dev.1793836959-ddbba3adb → 3.0.0-dev.1794787379-57db0bb10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,9 @@
1
+ import type { REST } from '@discordjs/rest';
2
+ import type { RequestHandler } from '../util/util.js';
3
+ /**
4
+ * Creates an HTTP handler used to forward requests to Discord
5
+ *
6
+ * @param rest - REST instance to use for the requests
7
+ */
8
+ export declare function proxyRequests(rest: REST): RequestHandler;
9
+ //# sourceMappingURL=proxyRequests.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"proxyRequests.d.ts","sourceRoot":"","sources":["../../src/handlers/proxyRequests.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAiB,IAAI,EAAa,MAAM,iBAAiB,CAAC;AAEtE,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAEtD;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE,IAAI,GAAG,cAAc,CAsDxD"}
@@ -0,0 +1,55 @@
1
+ import { populateSuccessfulResponse, populateErrorResponse } from '../util/responseHelpers.js';
2
+ /**
3
+ * Creates an HTTP handler used to forward requests to Discord
4
+ *
5
+ * @param rest - REST instance to use for the requests
6
+ */
7
+ export function proxyRequests(rest) {
8
+ return async (req, res) => {
9
+ const { method, url } = req;
10
+ if (!method || !url) {
11
+ throw new TypeError('Invalid request. Missing method and/or url, implying that this is not a Server IncomingMessage');
12
+ }
13
+ // The 2nd parameter is here so the URL constructor doesn't complain about an "invalid url" when the origin is missing
14
+ // we don't actually care about the origin and the value passed is irrelevant
15
+ const parsedUrl = new URL(url, 'http://noop');
16
+ // eslint-disable-next-line prefer-named-capture-group
17
+ const fullRoute = parsedUrl.pathname.replace(/^\/api(\/v\d+)?/, '');
18
+ const headers = {
19
+ 'Content-Type': req.headers['content-type'],
20
+ };
21
+ if (req.headers.authorization) {
22
+ headers.authorization = req.headers.authorization;
23
+ }
24
+ if (req.headers['x-audit-log-reason']) {
25
+ headers['x-audit-log-reason'] = req.headers['x-audit-log-reason'];
26
+ }
27
+ try {
28
+ const discordResponse = await rest.queueRequest({
29
+ body: req,
30
+ fullRoute,
31
+ // This type cast is technically incorrect, but we want Discord to throw Method Not Allowed for us
32
+ method: method,
33
+ // We forward the auth header anyway
34
+ auth: false,
35
+ passThroughBody: true,
36
+ query: parsedUrl.searchParams,
37
+ headers,
38
+ });
39
+ await populateSuccessfulResponse(res, discordResponse);
40
+ }
41
+ catch (error) {
42
+ const knownError = populateErrorResponse(res, error);
43
+ if (!knownError) {
44
+ // Unclear if there's better course of action here for unknown errors.
45
+ // Any web framework allows to pass in an error handler for something like this
46
+ // at which point the user could dictate what to do with the error - otherwise we could just 500
47
+ throw error;
48
+ }
49
+ }
50
+ finally {
51
+ res.end();
52
+ }
53
+ };
54
+ }
55
+ //# sourceMappingURL=proxyRequests.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"proxyRequests.js","sourceRoot":"","sources":["../../src/handlers/proxyRequests.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,0BAA0B,EAAE,qBAAqB,EAAE,MAAM,4BAA4B,CAAC;AAG/F;;;;GAIG;AACH,MAAM,UAAU,aAAa,CAAC,IAAU;IACvC,OAAO,KAAK,EAAE,GAAG,EAAE,GAAG,EAAE,EAAE;QACzB,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,GAAG,GAAG,CAAC;QAE5B,IAAI,CAAC,MAAM,IAAI,CAAC,GAAG,EAAE,CAAC;YACrB,MAAM,IAAI,SAAS,CAClB,gGAAgG,CAChG,CAAC;QACH,CAAC;QAED,sHAAsH;QACtH,6EAA6E;QAC7E,MAAM,SAAS,GAAG,IAAI,GAAG,CAAC,GAAG,EAAE,aAAa,CAAC,CAAC;QAC9C,sDAAsD;QACtD,MAAM,SAAS,GAAG,SAAS,CAAC,QAAQ,CAAC,OAAO,CAAC,iBAAiB,EAAE,EAAE,CAAc,CAAC;QAEjF,MAAM,OAAO,GAA2B;YACvC,cAAc,EAAE,GAAG,CAAC,OAAO,CAAC,cAAc,CAAE;SAC5C,CAAC;QAEF,IAAI,GAAG,CAAC,OAAO,CAAC,aAAa,EAAE,CAAC;YAC/B,OAAO,CAAC,aAAa,GAAG,GAAG,CAAC,OAAO,CAAC,aAAa,CAAC;QACnD,CAAC;QAED,IAAI,GAAG,CAAC,OAAO,CAAC,oBAAoB,CAAC,EAAE,CAAC;YACvC,OAAO,CAAC,oBAAoB,CAAC,GAAG,GAAG,CAAC,OAAO,CAAC,oBAAoB,CAAW,CAAC;QAC7E,CAAC;QAED,IAAI,CAAC;YACJ,MAAM,eAAe,GAAG,MAAM,IAAI,CAAC,YAAY,CAAC;gBAC/C,IAAI,EAAE,GAAG;gBACT,SAAS;gBACT,kGAAkG;gBAClG,MAAM,EAAE,MAAuB;gBAC/B,oCAAoC;gBACpC,IAAI,EAAE,KAAK;gBACX,eAAe,EAAE,IAAI;gBACrB,KAAK,EAAE,SAAS,CAAC,YAAY;gBAC7B,OAAO;aACP,CAAC,CAAC;YAEH,MAAM,0BAA0B,CAAC,GAAG,EAAE,eAAe,CAAC,CAAC;QACxD,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YAChB,MAAM,UAAU,GAAG,qBAAqB,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;YACrD,IAAI,CAAC,UAAU,EAAE,CAAC;gBACjB,sEAAsE;gBACtE,+EAA+E;gBAC/E,gGAAgG;gBAChG,MAAM,KAAK,CAAC;YACb,CAAC;QACF,CAAC;gBAAS,CAAC;YACV,GAAG,CAAC,GAAG,EAAE,CAAC;QACX,CAAC;IACF,CAAC,CAAC;AACH,CAAC"}
package/dist/index.d.ts CHANGED
@@ -1,61 +1,11 @@
1
- import { REST, DiscordAPIError, HTTPError, RateLimitError, ResponseLike } from '@discordjs/rest';
2
- import { IncomingMessage, ServerResponse } from 'node:http';
3
- import { Awaitable } from '@discordjs/util';
4
-
5
- /**
6
- * Represents a simple HTTP request handler
7
- */
8
- type RequestHandler = (req: IncomingMessage, res: ServerResponse) => Awaitable<void>;
9
-
10
- /**
11
- * Creates an HTTP handler used to forward requests to Discord
12
- *
13
- * @param rest - REST instance to use for the requests
14
- */
15
- declare function proxyRequests(rest: REST): RequestHandler;
16
-
17
- /**
18
- * Populates a server response with the data from a Discord 2xx REST response
19
- *
20
- * @param res - The server response to populate
21
- * @param data - The data to populate the response with
22
- */
23
- declare function populateSuccessfulResponse(res: ServerResponse, data: ResponseLike): Promise<void>;
24
- /**
25
- * Populates a server response with the data from a Discord non-2xx REST response that is NOT a 429
26
- *
27
- * @param res - The server response to populate
28
- * @param error - The error to populate the response with
29
- */
30
- declare function populateGeneralErrorResponse(res: ServerResponse, error: DiscordAPIError | HTTPError): void;
31
- /**
32
- * Populates a server response with the data from a Discord 429 REST response
33
- *
34
- * @param res - The server response to populate
35
- * @param error - The error to populate the response with
36
- */
37
- declare function populateRatelimitErrorResponse(res: ServerResponse, error: RateLimitError): void;
38
- /**
39
- * Populates a server response with data relevant for a timeout
40
- *
41
- * @param res - The sever response to populate
42
- */
43
- declare function populateAbortErrorResponse(res: ServerResponse): void;
44
- /**
45
- * Tries to populate a server response from an error object
46
- *
47
- * @param res - The server response to populate
48
- * @param error - The error to check and use
49
- * @returns `true` if the error is known and the response object was populated, otherwise `false`
50
- */
51
- declare function populateErrorResponse(res: ServerResponse, error: unknown): boolean;
52
-
1
+ export * from './handlers/proxyRequests.js';
2
+ export * from './util/responseHelpers.js';
3
+ export type { RequestHandler } from './util/util.js';
53
4
  /**
54
5
  * The {@link https://github.com/discordjs/discord.js/blob/main/packages/proxy#readme | @discordjs/proxy} version
55
6
  * that you are currently using.
56
7
  *
57
8
  * @privateRemarks This needs to explicitly be `string` so it is not typed as a "const string" that gets injected by esbuild.
58
9
  */
59
- declare const version: string;
60
-
61
- export { type RequestHandler, populateAbortErrorResponse, populateErrorResponse, populateGeneralErrorResponse, populateRatelimitErrorResponse, populateSuccessfulResponse, proxyRequests, version };
10
+ export declare const version: string;
11
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,6BAA6B,CAAC;AAC5C,cAAc,2BAA2B,CAAC;AAC1C,YAAY,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAErD;;;;;GAKG;AACH,eAAO,MAAM,OAAO,EAA4B,MAAM,CAAC"}
package/dist/index.js CHANGED
@@ -1,144 +1,10 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
7
- var __export = (target, all) => {
8
- for (var name in all)
9
- __defProp(target, name, { get: all[name], enumerable: true });
10
- };
11
- var __copyProps = (to, from, except, desc) => {
12
- if (from && typeof from === "object" || typeof from === "function") {
13
- for (let key of __getOwnPropNames(from))
14
- if (!__hasOwnProp.call(to, key) && key !== except)
15
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
- }
17
- return to;
18
- };
19
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
-
21
- // src/index.ts
22
- var index_exports = {};
23
- __export(index_exports, {
24
- populateAbortErrorResponse: () => populateAbortErrorResponse,
25
- populateErrorResponse: () => populateErrorResponse,
26
- populateGeneralErrorResponse: () => populateGeneralErrorResponse,
27
- populateRatelimitErrorResponse: () => populateRatelimitErrorResponse,
28
- populateSuccessfulResponse: () => populateSuccessfulResponse,
29
- proxyRequests: () => proxyRequests,
30
- version: () => version
31
- });
32
- module.exports = __toCommonJS(index_exports);
33
-
34
- // src/util/responseHelpers.ts
35
- var import_node_stream = require("stream");
36
- var import_promises = require("stream/promises");
37
- var import_rest = require("@discordjs/rest");
38
- async function populateSuccessfulResponse(res, data) {
39
- res.statusCode = data.status;
40
- for (const [header, value] of data.headers) {
41
- if (/^x-ratelimit/i.test(header)) {
42
- continue;
43
- }
44
- res.setHeader(header, value);
45
- }
46
- if (data.body) {
47
- await (0, import_promises.pipeline)(data.body instanceof import_node_stream.Readable ? data.body : import_node_stream.Readable.fromWeb(data.body), res);
48
- }
49
- }
50
- __name(populateSuccessfulResponse, "populateSuccessfulResponse");
51
- function populateGeneralErrorResponse(res, error) {
52
- res.statusCode = error.status;
53
- if ("rawError" in error) {
54
- res.setHeader("Content-Type", "application/json");
55
- res.write(JSON.stringify(error.rawError));
56
- }
57
- }
58
- __name(populateGeneralErrorResponse, "populateGeneralErrorResponse");
59
- function populateRatelimitErrorResponse(res, error) {
60
- res.statusCode = 429;
61
- res.setHeader("Retry-After", error.timeToReset / 1e3);
62
- if (error.global) {
63
- res.setHeader("X-RateLimit-Global", "true");
64
- }
65
- res.setHeader("X-RateLimit-Scope", error.scope);
66
- }
67
- __name(populateRatelimitErrorResponse, "populateRatelimitErrorResponse");
68
- function populateAbortErrorResponse(res) {
69
- res.statusCode = 504;
70
- res.statusMessage = "Upstream timed out";
71
- }
72
- __name(populateAbortErrorResponse, "populateAbortErrorResponse");
73
- function populateErrorResponse(res, error) {
74
- if (error instanceof import_rest.DiscordAPIError || error instanceof import_rest.HTTPError) {
75
- populateGeneralErrorResponse(res, error);
76
- } else if (error instanceof import_rest.RateLimitError) {
77
- populateRatelimitErrorResponse(res, error);
78
- } else if (error instanceof Error && error.name === "AbortError") {
79
- populateAbortErrorResponse(res);
80
- } else {
81
- return false;
82
- }
83
- return true;
84
- }
85
- __name(populateErrorResponse, "populateErrorResponse");
86
-
87
- // src/handlers/proxyRequests.ts
88
- function proxyRequests(rest) {
89
- return async (req, res) => {
90
- const { method, url } = req;
91
- if (!method || !url) {
92
- throw new TypeError(
93
- "Invalid request. Missing method and/or url, implying that this is not a Server IncomingMessage"
94
- );
95
- }
96
- const parsedUrl = new URL(url, "http://noop");
97
- const fullRoute = parsedUrl.pathname.replace(/^\/api(\/v\d+)?/, "");
98
- const headers = {
99
- "Content-Type": req.headers["content-type"]
100
- };
101
- if (req.headers.authorization) {
102
- headers.authorization = req.headers.authorization;
103
- }
104
- if (req.headers["x-audit-log-reason"]) {
105
- headers["x-audit-log-reason"] = req.headers["x-audit-log-reason"];
106
- }
107
- try {
108
- const discordResponse = await rest.queueRequest({
109
- body: req,
110
- fullRoute,
111
- // This type cast is technically incorrect, but we want Discord to throw Method Not Allowed for us
112
- method,
113
- // We forward the auth header anyway
114
- auth: false,
115
- passThroughBody: true,
116
- query: parsedUrl.searchParams,
117
- headers
118
- });
119
- await populateSuccessfulResponse(res, discordResponse);
120
- } catch (error) {
121
- const knownError = populateErrorResponse(res, error);
122
- if (!knownError) {
123
- throw error;
124
- }
125
- } finally {
126
- res.end();
127
- }
128
- };
129
- }
130
- __name(proxyRequests, "proxyRequests");
131
-
132
- // src/index.ts
133
- var version = "3.0.0-dev.1793836959-ddbba3adb";
134
- // Annotate the CommonJS export names for ESM import in node:
135
- 0 && (module.exports = {
136
- populateAbortErrorResponse,
137
- populateErrorResponse,
138
- populateGeneralErrorResponse,
139
- populateRatelimitErrorResponse,
140
- populateSuccessfulResponse,
141
- proxyRequests,
142
- version
143
- });
1
+ export * from './handlers/proxyRequests.js';
2
+ export * from './util/responseHelpers.js';
3
+ /**
4
+ * The {@link https://github.com/discordjs/discord.js/blob/main/packages/proxy#readme | @discordjs/proxy} version
5
+ * that you are currently using.
6
+ *
7
+ * @privateRemarks This needs to explicitly be `string` so it is not typed as a "const string" that gets injected by esbuild.
8
+ */
9
+ export const version = '3.0.0-dev.1794787379-57db0bb10';
144
10
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts","../src/util/responseHelpers.ts","../src/handlers/proxyRequests.ts"],"sourcesContent":["export * from './handlers/proxyRequests.js';\nexport * from './util/responseHelpers.js';\nexport type { RequestHandler } from './util/util.js';\n\n/**\n * The {@link https://github.com/discordjs/discord.js/blob/main/packages/proxy#readme | @discordjs/proxy} version\n * that you are currently using.\n *\n * @privateRemarks This needs to explicitly be `string` so it is not typed as a \"const string\" that gets injected by esbuild.\n */\nexport const version = '3.0.0-dev.1793836959-ddbba3adb' as string;\n","import type { ServerResponse } from 'node:http';\nimport { Readable } from 'node:stream';\nimport { pipeline } from 'node:stream/promises';\nimport { DiscordAPIError, HTTPError, RateLimitError, type ResponseLike } from '@discordjs/rest';\n\n/**\n * Populates a server response with the data from a Discord 2xx REST response\n *\n * @param res - The server response to populate\n * @param data - The data to populate the response with\n */\nexport async function populateSuccessfulResponse(res: ServerResponse, data: ResponseLike): Promise<void> {\n\tres.statusCode = data.status;\n\n\tfor (const [header, value] of data.headers) {\n\t\t// Strip ratelimit headers\n\t\tif (/^x-ratelimit/i.test(header)) {\n\t\t\tcontinue;\n\t\t}\n\n\t\tres.setHeader(header, value);\n\t}\n\n\tif (data.body) {\n\t\tawait pipeline(data.body instanceof Readable ? data.body : Readable.fromWeb(data.body), res);\n\t}\n}\n\n/**\n * Populates a server response with the data from a Discord non-2xx REST response that is NOT a 429\n *\n * @param res - The server response to populate\n * @param error - The error to populate the response with\n */\nexport function populateGeneralErrorResponse(res: ServerResponse, error: DiscordAPIError | HTTPError): void {\n\tres.statusCode = error.status;\n\n\tif ('rawError' in error) {\n\t\tres.setHeader('Content-Type', 'application/json');\n\t\tres.write(JSON.stringify(error.rawError));\n\t}\n}\n\n/**\n * Populates a server response with the data from a Discord 429 REST response\n *\n * @param res - The server response to populate\n * @param error - The error to populate the response with\n */\nexport function populateRatelimitErrorResponse(res: ServerResponse, error: RateLimitError): void {\n\tres.statusCode = 429;\n\tres.setHeader('Retry-After', error.timeToReset / 1_000);\n\n\tif (error.global) {\n\t\tres.setHeader('X-RateLimit-Global', 'true');\n\t}\n\n\tres.setHeader('X-RateLimit-Scope', error.scope);\n}\n\n/**\n * Populates a server response with data relevant for a timeout\n *\n * @param res - The sever response to populate\n */\nexport function populateAbortErrorResponse(res: ServerResponse): void {\n\tres.statusCode = 504;\n\tres.statusMessage = 'Upstream timed out';\n}\n\n/**\n * Tries to populate a server response from an error object\n *\n * @param res - The server response to populate\n * @param error - The error to check and use\n * @returns `true` if the error is known and the response object was populated, otherwise `false`\n */\nexport function populateErrorResponse(res: ServerResponse, error: unknown): boolean {\n\tif (error instanceof DiscordAPIError || error instanceof HTTPError) {\n\t\tpopulateGeneralErrorResponse(res, error);\n\t} else if (error instanceof RateLimitError) {\n\t\tpopulateRatelimitErrorResponse(res, error);\n\t} else if (error instanceof Error && error.name === 'AbortError') {\n\t\tpopulateAbortErrorResponse(res);\n\t} else {\n\t\treturn false;\n\t}\n\n\treturn true;\n}\n","import type { RequestMethod, REST, RouteLike } from '@discordjs/rest';\nimport { populateSuccessfulResponse, populateErrorResponse } from '../util/responseHelpers.js';\nimport type { RequestHandler } from '../util/util.js';\n\n/**\n * Creates an HTTP handler used to forward requests to Discord\n *\n * @param rest - REST instance to use for the requests\n */\nexport function proxyRequests(rest: REST): RequestHandler {\n\treturn async (req, res) => {\n\t\tconst { method, url } = req;\n\n\t\tif (!method || !url) {\n\t\t\tthrow new TypeError(\n\t\t\t\t'Invalid request. Missing method and/or url, implying that this is not a Server IncomingMessage',\n\t\t\t);\n\t\t}\n\n\t\t// The 2nd parameter is here so the URL constructor doesn't complain about an \"invalid url\" when the origin is missing\n\t\t// we don't actually care about the origin and the value passed is irrelevant\n\t\tconst parsedUrl = new URL(url, 'http://noop');\n\t\t// eslint-disable-next-line prefer-named-capture-group\n\t\tconst fullRoute = parsedUrl.pathname.replace(/^\\/api(\\/v\\d+)?/, '') as RouteLike;\n\n\t\tconst headers: Record<string, string> = {\n\t\t\t'Content-Type': req.headers['content-type']!,\n\t\t};\n\n\t\tif (req.headers.authorization) {\n\t\t\theaders.authorization = req.headers.authorization;\n\t\t}\n\n\t\tif (req.headers['x-audit-log-reason']) {\n\t\t\theaders['x-audit-log-reason'] = req.headers['x-audit-log-reason'] as string;\n\t\t}\n\n\t\ttry {\n\t\t\tconst discordResponse = await rest.queueRequest({\n\t\t\t\tbody: req,\n\t\t\t\tfullRoute,\n\t\t\t\t// This type cast is technically incorrect, but we want Discord to throw Method Not Allowed for us\n\t\t\t\tmethod: method as RequestMethod,\n\t\t\t\t// We forward the auth header anyway\n\t\t\t\tauth: false,\n\t\t\t\tpassThroughBody: true,\n\t\t\t\tquery: parsedUrl.searchParams,\n\t\t\t\theaders,\n\t\t\t});\n\n\t\t\tawait populateSuccessfulResponse(res, discordResponse);\n\t\t} catch (error) {\n\t\t\tconst knownError = populateErrorResponse(res, error);\n\t\t\tif (!knownError) {\n\t\t\t\t// Unclear if there's better course of action here for unknown errors.\n\t\t\t\t// Any web framework allows to pass in an error handler for something like this\n\t\t\t\t// at which point the user could dictate what to do with the error - otherwise we could just 500\n\t\t\t\tthrow error;\n\t\t\t}\n\t\t} finally {\n\t\t\tres.end();\n\t\t}\n\t};\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACCA,yBAAyB;AACzB,sBAAyB;AACzB,kBAA8E;AAQ9E,eAAsB,2BAA2B,KAAqB,MAAmC;AACxG,MAAI,aAAa,KAAK;AAEtB,aAAW,CAAC,QAAQ,KAAK,KAAK,KAAK,SAAS;AAE3C,QAAI,gBAAgB,KAAK,MAAM,GAAG;AACjC;AAAA,IACD;AAEA,QAAI,UAAU,QAAQ,KAAK;AAAA,EAC5B;AAEA,MAAI,KAAK,MAAM;AACd,cAAM,0BAAS,KAAK,gBAAgB,8BAAW,KAAK,OAAO,4BAAS,QAAQ,KAAK,IAAI,GAAG,GAAG;AAAA,EAC5F;AACD;AAfsB;AAuBf,SAAS,6BAA6B,KAAqB,OAA0C;AAC3G,MAAI,aAAa,MAAM;A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AI,IAAI,QAAQ,oBAAoB;AAAA,IACjE;AAEA,QAAI;AACH,YAAM,kBAAkB,MAAM,KAAK,aAAa;AAAA,QAC/C,MAAM;AAAA,QACN;AAAA;AAAA,QAEA;AAAA;AAAA,QAEA,MAAM;AAAA,QACN,iBAAiB;AAAA,QACjB,OAAO,UAAU;AAAA,QACjB;AAAA,MACD,CAAC;AAED,YAAM,2BAA2B,KAAK,eAAe;AAAA,IACtD,SAAS,OAAO;AACf,YAAM,aAAa,sBAAsB,KAAK,KAAK;AACnD,UAAI,CAAC,YAAY;AAIhB,cAAM;AAAA,MACP;AAAA,IACD,UAAE;AACD,UAAI,IAAI;AAAA,IACT;AAAA,EACD;AACD;AAtDgB;;;AFCT,IAAM,UAAU;","names":[]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,6BAA6B,CAAC;AAC5C,cAAc,2BAA2B,CAAC;AAG1C;;;;;GAKG;AACH,MAAM,CAAC,MAAM,OAAO,GAAG,qBAA+B,CAAC"}
@@ -0,0 +1,38 @@
1
+ import type { ServerResponse } from 'node:http';
2
+ import { DiscordAPIError, HTTPError, RateLimitError, type ResponseLike } from '@discordjs/rest';
3
+ /**
4
+ * Populates a server response with the data from a Discord 2xx REST response
5
+ *
6
+ * @param res - The server response to populate
7
+ * @param data - The data to populate the response with
8
+ */
9
+ export declare function populateSuccessfulResponse(res: ServerResponse, data: ResponseLike): Promise<void>;
10
+ /**
11
+ * Populates a server response with the data from a Discord non-2xx REST response that is NOT a 429
12
+ *
13
+ * @param res - The server response to populate
14
+ * @param error - The error to populate the response with
15
+ */
16
+ export declare function populateGeneralErrorResponse(res: ServerResponse, error: DiscordAPIError | HTTPError): void;
17
+ /**
18
+ * Populates a server response with the data from a Discord 429 REST response
19
+ *
20
+ * @param res - The server response to populate
21
+ * @param error - The error to populate the response with
22
+ */
23
+ export declare function populateRatelimitErrorResponse(res: ServerResponse, error: RateLimitError): void;
24
+ /**
25
+ * Populates a server response with data relevant for a timeout
26
+ *
27
+ * @param res - The sever response to populate
28
+ */
29
+ export declare function populateAbortErrorResponse(res: ServerResponse): void;
30
+ /**
31
+ * Tries to populate a server response from an error object
32
+ *
33
+ * @param res - The server response to populate
34
+ * @param error - The error to check and use
35
+ * @returns `true` if the error is known and the response object was populated, otherwise `false`
36
+ */
37
+ export declare function populateErrorResponse(res: ServerResponse, error: unknown): boolean;
38
+ //# sourceMappingURL=responseHelpers.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"responseHelpers.d.ts","sourceRoot":"","sources":["../../src/util/responseHelpers.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAGhD,OAAO,EAAE,eAAe,EAAE,SAAS,EAAE,cAAc,EAAE,KAAK,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAEhG;;;;;GAKG;AACH,wBAAsB,0BAA0B,CAAC,GAAG,EAAE,cAAc,EAAE,IAAI,EAAE,YAAY,GAAG,OAAO,CAAC,IAAI,CAAC,CAevG;AAED;;;;;GAKG;AACH,wBAAgB,4BAA4B,CAAC,GAAG,EAAE,cAAc,EAAE,KAAK,EAAE,eAAe,GAAG,SAAS,GAAG,IAAI,CAO1G;AAED;;;;;GAKG;AACH,wBAAgB,8BAA8B,CAAC,GAAG,EAAE,cAAc,EAAE,KAAK,EAAE,cAAc,GAAG,IAAI,CAS/F;AAED;;;;GAIG;AACH,wBAAgB,0BAA0B,CAAC,GAAG,EAAE,cAAc,GAAG,IAAI,CAGpE;AAED;;;;;;GAMG;AACH,wBAAgB,qBAAqB,CAAC,GAAG,EAAE,cAAc,EAAE,KAAK,EAAE,OAAO,GAAG,OAAO,CAYlF"}
@@ -0,0 +1,81 @@
1
+ import { Readable } from 'node:stream';
2
+ import { pipeline } from 'node:stream/promises';
3
+ import { DiscordAPIError, HTTPError, RateLimitError } from '@discordjs/rest';
4
+ /**
5
+ * Populates a server response with the data from a Discord 2xx REST response
6
+ *
7
+ * @param res - The server response to populate
8
+ * @param data - The data to populate the response with
9
+ */
10
+ export async function populateSuccessfulResponse(res, data) {
11
+ res.statusCode = data.status;
12
+ for (const [header, value] of data.headers) {
13
+ // Strip ratelimit headers
14
+ if (/^x-ratelimit/i.test(header)) {
15
+ continue;
16
+ }
17
+ res.setHeader(header, value);
18
+ }
19
+ if (data.body) {
20
+ await pipeline(data.body instanceof Readable ? data.body : Readable.fromWeb(data.body), res);
21
+ }
22
+ }
23
+ /**
24
+ * Populates a server response with the data from a Discord non-2xx REST response that is NOT a 429
25
+ *
26
+ * @param res - The server response to populate
27
+ * @param error - The error to populate the response with
28
+ */
29
+ export function populateGeneralErrorResponse(res, error) {
30
+ res.statusCode = error.status;
31
+ if ('rawError' in error) {
32
+ res.setHeader('Content-Type', 'application/json');
33
+ res.write(JSON.stringify(error.rawError));
34
+ }
35
+ }
36
+ /**
37
+ * Populates a server response with the data from a Discord 429 REST response
38
+ *
39
+ * @param res - The server response to populate
40
+ * @param error - The error to populate the response with
41
+ */
42
+ export function populateRatelimitErrorResponse(res, error) {
43
+ res.statusCode = 429;
44
+ res.setHeader('Retry-After', error.timeToReset / 1_000);
45
+ if (error.global) {
46
+ res.setHeader('X-RateLimit-Global', 'true');
47
+ }
48
+ res.setHeader('X-RateLimit-Scope', error.scope);
49
+ }
50
+ /**
51
+ * Populates a server response with data relevant for a timeout
52
+ *
53
+ * @param res - The sever response to populate
54
+ */
55
+ export function populateAbortErrorResponse(res) {
56
+ res.statusCode = 504;
57
+ res.statusMessage = 'Upstream timed out';
58
+ }
59
+ /**
60
+ * Tries to populate a server response from an error object
61
+ *
62
+ * @param res - The server response to populate
63
+ * @param error - The error to check and use
64
+ * @returns `true` if the error is known and the response object was populated, otherwise `false`
65
+ */
66
+ export function populateErrorResponse(res, error) {
67
+ if (error instanceof DiscordAPIError || error instanceof HTTPError) {
68
+ populateGeneralErrorResponse(res, error);
69
+ }
70
+ else if (error instanceof RateLimitError) {
71
+ populateRatelimitErrorResponse(res, error);
72
+ }
73
+ else if (error instanceof Error && error.name === 'AbortError') {
74
+ populateAbortErrorResponse(res);
75
+ }
76
+ else {
77
+ return false;
78
+ }
79
+ return true;
80
+ }
81
+ //# sourceMappingURL=responseHelpers.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"responseHelpers.js","sourceRoot":"","sources":["../../src/util/responseHelpers.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AACvC,OAAO,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAChD,OAAO,EAAE,eAAe,EAAE,SAAS,EAAE,cAAc,EAAqB,MAAM,iBAAiB,CAAC;AAEhG;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,0BAA0B,CAAC,GAAmB,EAAE,IAAkB;IACvF,GAAG,CAAC,UAAU,GAAG,IAAI,CAAC,MAAM,CAAC;IAE7B,KAAK,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,IAAI,IAAI,CAAC,OAAO,EAAE,CAAC;QAC5C,0BAA0B;QAC1B,IAAI,eAAe,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC;YAClC,SAAS;QACV,CAAC;QAED,GAAG,CAAC,SAAS,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;IAC9B,CAAC;IAED,IAAI,IAAI,CAAC,IAAI,EAAE,CAAC;QACf,MAAM,QAAQ,CAAC,IAAI,CAAC,IAAI,YAAY,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,QAAQ,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,GAAG,CAAC,CAAC;IAC9F,CAAC;AACF,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,4BAA4B,CAAC,GAAmB,EAAE,KAAkC;IACnG,GAAG,CAAC,UAAU,GAAG,KAAK,CAAC,MAAM,CAAC;IAE9B,IAAI,UAAU,IAAI,KAAK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IAAI,CAAC;AACb,CAAC"}
@@ -0,0 +1,7 @@
1
+ import type { IncomingMessage, ServerResponse } from 'node:http';
2
+ import type { Awaitable } from '@discordjs/util';
3
+ /**
4
+ * Represents a simple HTTP request handler
5
+ */
6
+ export type RequestHandler = (req: IncomingMessage, res: ServerResponse) => Awaitable<void>;
7
+ //# sourceMappingURL=util.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"util.d.ts","sourceRoot":"","sources":["../../src/util/util.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AACjE,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAEjD;;GAEG;AACH,MAAM,MAAM,cAAc,GAAG,CAAC,GAAG,EAAE,eAAe,EAAE,GAAG,EAAE,cAAc,KAAK,SAAS,CAAC,IAAI,CAAC,CAAC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=util.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"util.js","sourceRoot":"","sources":["../../src/util/util.ts"],"names":[],"mappings":""}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package.json",
3
3
  "name": "@discordjs/proxy",
4
- "version": "3.0.0-dev.1793836959-ddbba3adb",
4
+ "version": "3.0.0-dev.1794787379-57db0bb10",
5
5
  "description": "Tools for running an HTTP proxy for Discord's API",
6
6
  "keywords": [
7
7
  "api",
@@ -37,19 +37,13 @@
37
37
  "files": [
38
38
  "dist"
39
39
  ],
40
+ "type": "module",
40
41
  "main": "./dist/index.js",
41
- "module": "./dist/index.mjs",
42
42
  "types": "./dist/index.d.ts",
43
43
  "exports": {
44
44
  ".": {
45
- "require": {
46
- "types": "./dist/index.d.ts",
47
- "default": "./dist/index.js"
48
- },
49
- "import": {
50
- "types": "./dist/index.d.mts",
51
- "default": "./dist/index.mjs"
52
- }
45
+ "types": "./dist/index.d.ts",
46
+ "default": "./dist/index.js"
53
47
  }
54
48
  },
55
49
  "publishConfig": {
@@ -57,8 +51,8 @@
57
51
  "provenance": true
58
52
  },
59
53
  "dependencies": {
60
- "@discordjs/rest": "3.0.0-dev.1793836959-ddbba3adb",
61
- "@discordjs/util": "2.0.0-dev.1793836959-ddbba3adb",
54
+ "@discordjs/rest": "3.0.0-dev.1794787379-57db0bb10",
55
+ "@discordjs/util": "2.0.0-dev.1794787379-57db0bb10",
62
56
  "tslib": "^2.8.1",
63
57
  "undici": "^8.10.1"
64
58
  },
@@ -69,14 +63,12 @@
69
63
  "@types/node": "^24.13.3",
70
64
  "@types/supertest": "^7.2.1",
71
65
  "@vitest/coverage-v8": "^4.1.11",
72
- "esbuild-plugin-version-injector": "^1.2.1",
73
66
  "eslint-config-neon": "^0.5.4",
74
67
  "oxfmt": "^0.74.0",
75
68
  "oxlint": "^1.89.0",
76
69
  "supertest": "^7.2.2",
77
- "tsup": "^8.5.1",
78
70
  "turbo": "^2.10.12",
79
- "typescript": "~5.9.3",
71
+ "typescript": "~7.0.2",
80
72
  "vitest": "^4.1.11"
81
73
  },
82
74
  "engines": {
@@ -84,7 +76,7 @@
84
76
  },
85
77
  "scripts": {
86
78
  "test": "vitest run --config ../../vitest.config.ts",
87
- "build": "tsc --noEmit && tsup",
79
+ "build": "tsc && inject-version",
88
80
  "build:docs": "tsc -p tsconfig.docs.json",
89
81
  "lint": "oxfmt --check && oxlint --tsconfig=tsconfig.eslint.json src __tests__",
90
82
  "format": "oxfmt && oxlint --tsconfig=tsconfig.eslint.json src __tests__ --fix --fix-suggestions",
package/dist/index.d.mts DELETED
@@ -1,61 +0,0 @@
1
- import { REST, DiscordAPIError, HTTPError, RateLimitError, ResponseLike } from '@discordjs/rest';
2
- import { IncomingMessage, ServerResponse } from 'node:http';
3
- import { Awaitable } from '@discordjs/util';
4
-
5
- /**
6
- * Represents a simple HTTP request handler
7
- */
8
- type RequestHandler = (req: IncomingMessage, res: ServerResponse) => Awaitable<void>;
9
-
10
- /**
11
- * Creates an HTTP handler used to forward requests to Discord
12
- *
13
- * @param rest - REST instance to use for the requests
14
- */
15
- declare function proxyRequests(rest: REST): RequestHandler;
16
-
17
- /**
18
- * Populates a server response with the data from a Discord 2xx REST response
19
- *
20
- * @param res - The server response to populate
21
- * @param data - The data to populate the response with
22
- */
23
- declare function populateSuccessfulResponse(res: ServerResponse, data: ResponseLike): Promise<void>;
24
- /**
25
- * Populates a server response with the data from a Discord non-2xx REST response that is NOT a 429
26
- *
27
- * @param res - The server response to populate
28
- * @param error - The error to populate the response with
29
- */
30
- declare function populateGeneralErrorResponse(res: ServerResponse, error: DiscordAPIError | HTTPError): void;
31
- /**
32
- * Populates a server response with the data from a Discord 429 REST response
33
- *
34
- * @param res - The server response to populate
35
- * @param error - The error to populate the response with
36
- */
37
- declare function populateRatelimitErrorResponse(res: ServerResponse, error: RateLimitError): void;
38
- /**
39
- * Populates a server response with data relevant for a timeout
40
- *
41
- * @param res - The sever response to populate
42
- */
43
- declare function populateAbortErrorResponse(res: ServerResponse): void;
44
- /**
45
- * Tries to populate a server response from an error object
46
- *
47
- * @param res - The server response to populate
48
- * @param error - The error to check and use
49
- * @returns `true` if the error is known and the response object was populated, otherwise `false`
50
- */
51
- declare function populateErrorResponse(res: ServerResponse, error: unknown): boolean;
52
-
53
- /**
54
- * The {@link https://github.com/discordjs/discord.js/blob/main/packages/proxy#readme | @discordjs/proxy} version
55
- * that you are currently using.
56
- *
57
- * @privateRemarks This needs to explicitly be `string` so it is not typed as a "const string" that gets injected by esbuild.
58
- */
59
- declare const version: string;
60
-
61
- export { type RequestHandler, populateAbortErrorResponse, populateErrorResponse, populateGeneralErrorResponse, populateRatelimitErrorResponse, populateSuccessfulResponse, proxyRequests, version };
package/dist/index.mjs DELETED
@@ -1,113 +0,0 @@
1
- var __defProp = Object.defineProperty;
2
- var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
-
4
- // src/util/responseHelpers.ts
5
- import { Readable } from "stream";
6
- import { pipeline } from "stream/promises";
7
- import { DiscordAPIError, HTTPError, RateLimitError } from "@discordjs/rest";
8
- async function populateSuccessfulResponse(res, data) {
9
- res.statusCode = data.status;
10
- for (const [header, value] of data.headers) {
11
- if (/^x-ratelimit/i.test(header)) {
12
- continue;
13
- }
14
- res.setHeader(header, value);
15
- }
16
- if (data.body) {
17
- await pipeline(data.body instanceof Readable ? data.body : Readable.fromWeb(data.body), res);
18
- }
19
- }
20
- __name(populateSuccessfulResponse, "populateSuccessfulResponse");
21
- function populateGeneralErrorResponse(res, error) {
22
- res.statusCode = error.status;
23
- if ("rawError" in error) {
24
- res.setHeader("Content-Type", "application/json");
25
- res.write(JSON.stringify(error.rawError));
26
- }
27
- }
28
- __name(populateGeneralErrorResponse, "populateGeneralErrorResponse");
29
- function populateRatelimitErrorResponse(res, error) {
30
- res.statusCode = 429;
31
- res.setHeader("Retry-After", error.timeToReset / 1e3);
32
- if (error.global) {
33
- res.setHeader("X-RateLimit-Global", "true");
34
- }
35
- res.setHeader("X-RateLimit-Scope", error.scope);
36
- }
37
- __name(populateRatelimitErrorResponse, "populateRatelimitErrorResponse");
38
- function populateAbortErrorResponse(res) {
39
- res.statusCode = 504;
40
- res.statusMessage = "Upstream timed out";
41
- }
42
- __name(populateAbortErrorResponse, "populateAbortErrorResponse");
43
- function populateErrorResponse(res, error) {
44
- if (error instanceof DiscordAPIError || error instanceof HTTPError) {
45
- populateGeneralErrorResponse(res, error);
46
- } else if (error instanceof RateLimitError) {
47
- populateRatelimitErrorResponse(res, error);
48
- } else if (error instanceof Error && error.name === "AbortError") {
49
- populateAbortErrorResponse(res);
50
- } else {
51
- return false;
52
- }
53
- return true;
54
- }
55
- __name(populateErrorResponse, "populateErrorResponse");
56
-
57
- // src/handlers/proxyRequests.ts
58
- function proxyRequests(rest) {
59
- return async (req, res) => {
60
- const { method, url } = req;
61
- if (!method || !url) {
62
- throw new TypeError(
63
- "Invalid request. Missing method and/or url, implying that this is not a Server IncomingMessage"
64
- );
65
- }
66
- const parsedUrl = new URL(url, "http://noop");
67
- const fullRoute = parsedUrl.pathname.replace(/^\/api(\/v\d+)?/, "");
68
- const headers = {
69
- "Content-Type": req.headers["content-type"]
70
- };
71
- if (req.headers.authorization) {
72
- headers.authorization = req.headers.authorization;
73
- }
74
- if (req.headers["x-audit-log-reason"]) {
75
- headers["x-audit-log-reason"] = req.headers["x-audit-log-reason"];
76
- }
77
- try {
78
- const discordResponse = await rest.queueRequest({
79
- body: req,
80
- fullRoute,
81
- // This type cast is technically incorrect, but we want Discord to throw Method Not Allowed for us
82
- method,
83
- // We forward the auth header anyway
84
- auth: false,
85
- passThroughBody: true,
86
- query: parsedUrl.searchParams,
87
- headers
88
- });
89
- await populateSuccessfulResponse(res, discordResponse);
90
- } catch (error) {
91
- const knownError = populateErrorResponse(res, error);
92
- if (!knownError) {
93
- throw error;
94
- }
95
- } finally {
96
- res.end();
97
- }
98
- };
99
- }
100
- __name(proxyRequests, "proxyRequests");
101
-
102
- // src/index.ts
103
- var version = "3.0.0-dev.1793836959-ddbba3adb";
104
- export {
105
- populateAbortErrorResponse,
106
- populateErrorResponse,
107
- populateGeneralErrorResponse,
108
- populateRatelimitErrorResponse,
109
- populateSuccessfulResponse,
110
- proxyRequests,
111
- version
112
- };
113
- //# sourceMappingURL=index.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/util/responseHelpers.ts","../src/handlers/proxyRequests.ts","../src/index.ts"],"sourcesContent":["import type { ServerResponse } from 'node:http';\nimport { Readable } from 'node:stream';\nimport { pipeline } from 'node:stream/promises';\nimport { DiscordAPIError, HTTPError, RateLimitError, type ResponseLike } from '@discordjs/rest';\n\n/**\n * Populates a server response with the data from a Discord 2xx REST response\n *\n * @param res - The server response to populate\n * @param data - The data to populate the response with\n */\nexport async function populateSuccessfulResponse(res: ServerResponse, data: ResponseLike): Promise<void> {\n\tres.statusCode = data.status;\n\n\tfor (const [header, value] of data.headers) {\n\t\t// Strip ratelimit headers\n\t\tif (/^x-ratelimit/i.test(header)) {\n\t\t\tcontinue;\n\t\t}\n\n\t\tres.setHeader(header, value);\n\t}\n\n\tif (data.body) {\n\t\tawait pipeline(data.body instanceof Readable ? data.body : Readable.fromWeb(data.body), res);\n\t}\n}\n\n/**\n * Populates a server response with the data from a Discord non-2xx REST response that is NOT a 429\n *\n * @param res - The server response to populate\n * @param error - The error to populate the response with\n */\nexport function populateGeneralErrorResponse(res: ServerResponse, error: DiscordAPIError | HTTPError): void {\n\tres.statusCode = error.status;\n\n\tif ('rawError' in error) {\n\t\tres.setHeader('Content-Type', 'application/json');\n\t\tres.write(JSON.stringify(error.rawError));\n\t}\n}\n\n/**\n * Populates a server response with the data from a Discord 429 REST response\n *\n * @param res - The server response to populate\n * @param error - The error to populate the response with\n */\nexport function populateRatelimitErrorResponse(res: ServerResponse, error: RateLimitError): void {\n\tres.statusCode = 429;\n\tres.setHeader('Retry-After', error.timeToReset / 1_000);\n\n\tif (error.global) {\n\t\tres.setHeader('X-RateLimit-Global', 'true');\n\t}\n\n\tres.setHeader('X-RateLimit-Scope', error.scope);\n}\n\n/**\n * Populates a server response with data relevant for a timeout\n *\n * @param res - The sever response to populate\n */\nexport function populateAbortErrorResponse(res: ServerResponse): void {\n\tres.statusCode = 504;\n\tres.statusMessage = 'Upstream timed out';\n}\n\n/**\n * Tries to populate a server response from an error object\n *\n * @param res - The server response to populate\n * @param error - The error to check and use\n * @returns `true` if the error is known and the response object was populated, otherwise `false`\n */\nexport function populateErrorResponse(res: ServerResponse, error: unknown): boolean {\n\tif (error instanceof DiscordAPIError || error instanceof HTTPError) {\n\t\tpopulateGeneralErrorResponse(res, error);\n\t} else if (error instanceof RateLimitError) {\n\t\tpopulateRatelimitErrorResponse(res, error);\n\t} else if (error instanceof Error && error.name === 'AbortError') {\n\t\tpopulateAbortErrorResponse(res);\n\t} else {\n\t\treturn false;\n\t}\n\n\treturn true;\n}\n","import type { RequestMethod, REST, RouteLike } from '@discordjs/rest';\nimport { populateSuccessfulResponse, populateErrorResponse } from '../util/responseHelpers.js';\nimport type { RequestHandler } from '../util/util.js';\n\n/**\n * Creates an HTTP handler used to forward requests to Discord\n *\n * @param rest - REST instance to use for the requests\n */\nexport function proxyRequests(rest: REST): RequestHandler {\n\treturn async (req, res) => {\n\t\tconst { method, url } = req;\n\n\t\tif (!method || !url) {\n\t\t\tthrow new TypeError(\n\t\t\t\t'Invalid request. Missing method and/or url, implying that this is not a Server IncomingMessage',\n\t\t\t);\n\t\t}\n\n\t\t// The 2nd parameter is here so the URL constructor doesn't complain about an \"invalid url\" when the origin is missing\n\t\t// we don't actually care about the origin and the value passed is irrelevant\n\t\tconst parsedUrl = new URL(url, 'http://noop');\n\t\t// eslint-disable-next-line prefer-named-capture-group\n\t\tconst fullRoute = parsedUrl.pathname.replace(/^\\/api(\\/v\\d+)?/, '') as RouteLike;\n\n\t\tconst headers: Record<string, string> = {\n\t\t\t'Content-Type': req.headers['content-type']!,\n\t\t};\n\n\t\tif (req.headers.authorization) {\n\t\t\theaders.authorization = req.headers.authorization;\n\t\t}\n\n\t\tif (req.headers['x-audit-log-reason']) {\n\t\t\theaders['x-audit-log-reason'] = req.headers['x-audit-log-reason'] as string;\n\t\t}\n\n\t\ttry {\n\t\t\tconst discordResponse = await rest.queueRequest({\n\t\t\t\tbody: req,\n\t\t\t\tfullRoute,\n\t\t\t\t// This type cast is technically incorrect, but we want Discord to throw Method Not Allowed for us\n\t\t\t\tmethod: method as RequestMethod,\n\t\t\t\t// We forward the auth header anyway\n\t\t\t\tauth: false,\n\t\t\t\tpassThroughBody: true,\n\t\t\t\tquery: parsedUrl.searchParams,\n\t\t\t\theaders,\n\t\t\t});\n\n\t\t\tawait populateSuccessfulResponse(res, discordResponse);\n\t\t} catch (error) {\n\t\t\tconst knownError = populateErrorResponse(res, error);\n\t\t\tif (!knownError) {\n\t\t\t\t// Unclear if there's better course of action here for unknown errors.\n\t\t\t\t// Any web framework allows to pass in an error handler for something like this\n\t\t\t\t// at which point the user could dictate what to do with the error - otherwise we could just 500\n\t\t\t\tthrow error;\n\t\t\t}\n\t\t} finally {\n\t\t\tres.end();\n\t\t}\n\t};\n}\n","export * from './handlers/proxyRequests.js';\nexport * from './util/responseHelpers.js';\nexport type { RequestHandler } from './util/util.js';\n\n/**\n * The {@link https://github.com/discordjs/discord.js/blob/main/packages/proxy#readme | @discordjs/proxy} version\n * that you are currently using.\n *\n * @privateRemarks This needs to explicitly be `string` so it is not typed as a \"const string\" that gets injected by esbuild.\n */\nexport const version = '3.0.0-dev.1793836959-ddbba3adb' as string;\n"],"mappings":";;;;AACA,SAAS,gBAAgB;AACzB,SAAS,gBAAgB;AACzB,SAAS,iBAAiB,WAAW,sBAAyC;AAQ9E,eAAsB,2BAA2B,KAAqB,MAAmC;AACxG,MAAI,aAAa,KAAK;AAEtB,aAAW,CAAC,QAAQ,KAAK,KAAK,KAAK,SAAS;AAE3C,QAAI,gBAAgB,KAAK,MAAM,GAAG;AACjC;AAAA,IACD;AAEA,QAAI,UAAU,QAAQ,KAAK;AAAA,EAC5B;AAEA,MAAI,KAAK,MAAM;AACd,UAAM,SAAS,KAAK,gBAAgB,WAAW,KAAK,OAAO,SAAS,QAAQ,KAAK,IAAI,GAAG,GAAG;AAAA,EAC5F;AACD;AAfsB;AAuBf,SAAS,6BAA6B,KAAqB,OAA0C;AAC3G,MAAI,aAAa,MAAM;AAEvB,MAAI,cAAc,OAAO;AACxB,QAAI,UAAU,gBAAgB,kBAAkB;AAChD,QAAI,MAAM,KAAK,UAAU,MAAM,QAAQ,CAAC;AAAA,EACzC;AACD;AAPgB;AAeT,SAAS,+BAA+B,KAAqB,OAA6B;AAChG,MAAI,aAAa;AACjB,MAAI,UAAU,eAAe,MAAM,cAAc,GAAK;AAEtD,MAAI,MAAM,QAAQ;AACjB,QAAI,UAAU,sBAAsB,MAAM;AAAA,EAC3C;AAEA,MAAI,UAAU,qBAAqB,MAAM,KAAK;AAC/C;AATgB;AAgBT,SAAS,2BAA2B,KA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