@discordjs/proxy 1.4.1-dev.1682856232-ad217cc.0 → 1.4.1

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.
package/CHANGELOG.md CHANGED
@@ -2,6 +2,20 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file.
4
4
 
5
+ # [@discordjs/proxy@1.4.1](https://github.com/discordjs/discord.js/compare/@discordjs/proxy@1.4.0...@discordjs/proxy@1.4.1) - (2023-05-01)
6
+
7
+ ## Bug Fixes
8
+
9
+ - Fix external links (#9313) ([a7425c2](https://github.com/discordjs/discord.js/commit/a7425c29c4f23f1b31f4c6a463107ca9eb7fd7e2))
10
+
11
+ ## Documentation
12
+
13
+ - Generate static imports for types with api-extractor ([98a76db](https://github.com/discordjs/discord.js/commit/98a76db482879f79d6bb2fb2e5fc65ac2c34e2d9))
14
+
15
+ ## Refactor
16
+
17
+ - **proxy:** Rely on auth header instead (#9422) ([a49ed0a](https://github.com/discordjs/discord.js/commit/a49ed0a2d5934ad7af2e9cfbf7c5ccf171599591))
18
+
5
19
  # [@discordjs/proxy@1.4.0](https://github.com/discordjs/discord.js/compare/@discordjs/proxy@1.3.0...@discordjs/proxy@1.4.0) - (2023-04-01)
6
20
 
7
21
  ## Bug Fixes
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts","../src/handlers/proxyRequests.ts","../src/util/responseHelpers.ts"],"sourcesContent":["export * from './handlers/proxyRequests.js';\nexport * from './util/responseHelpers.js';\nexport type { RequestHandler } from './util/util.js';\n","import { URL } from 'node:url';\nimport type { RequestMethod, REST, RouteLike } from '@discordjs/rest';\nimport { populateSuccessfulResponse, populateErrorResponse } from '../util/responseHelpers.js';\nimport type { RequestHandler } from '../util/util';\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 unicorn/no-unsafe-regex, 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\ttry {\n\t\t\tconst discordResponse = await rest.raw({\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","import type { ServerResponse } from 'node:http';\nimport { pipeline } from 'node:stream/promises';\nimport { DiscordAPIError, HTTPError, RateLimitError } from '@discordjs/rest';\nimport type { Dispatcher } from 'undici';\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: Dispatcher.ResponseData): Promise<void> {\n\tres.statusCode = data.statusCode;\n\n\tfor (const header of Object.keys(data.headers)) {\n\t\t// Strip ratelimit headers\n\t\tif (header.startsWith('x-ratelimit')) {\n\t\t\tcontinue;\n\t\t}\n\n\t\tres.setHeader(header, data.headers[header]!);\n\t}\n\n\tawait pipeline(data.body, res);\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\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"],"mappings":";;;;;;;;;;;;;;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,IAAI;AAAA,QACT;AAAA,MACD;AAAA,IACD;AAIA,UAAM,YAAY,IAAI,oBAAI,KAAK,aAAa;AAE5C,UAAM,YAAY,UAAU,SAAS,QAAQ,mBAAmB,EAAE;AAElE,UAAM,UAAkC;AAAA,MACvC,gBAAgB,IAAI,QAAQ,cAAc;AAAA,IAC3C;AAEA,QAAI,IAAI,QAAQ,eAAe;AAC9B,cAAQ,gBAAgB,IAAI,QAAQ;AAAA,IACrC;AAEA,QAAI;AACH,YAAM,kBAAkB,MAAM,KAAK,IAAI;AAAA,QACtC,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,OAAP;AACD,YAAM,aAAa,sBAAsB,KAAK,KAAK;AACnD,UAAI,CAAC,YAAY;AAIhB,cAAM;AAAA,MACP;AAAA,IACD,UAAE;AACD,UAAI,IAAI;AAAA,IACT;AAAA,EACD;AACD;AAlDgB;","names":[]}
1
+ {"version":3,"sources":["../src/index.ts","../src/handlers/proxyRequests.ts","../src/util/responseHelpers.ts"],"sourcesContent":["export * from './handlers/proxyRequests.js';\nexport * from './util/responseHelpers.js';\nexport type { RequestHandler } from './util/util.js';\n","import { URL } from 'node:url';\nimport 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 unicorn/no-unsafe-regex, 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\ttry {\n\t\t\tconst discordResponse = await rest.raw({\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","import type { ServerResponse } from 'node:http';\nimport { pipeline } from 'node:stream/promises';\nimport { DiscordAPIError, HTTPError, RateLimitError } from '@discordjs/rest';\nimport type { Dispatcher } from 'undici';\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: Dispatcher.ResponseData): Promise<void> {\n\tres.statusCode = data.statusCode;\n\n\tfor (const header of Object.keys(data.headers)) {\n\t\t// Strip ratelimit headers\n\t\tif (header.startsWith('x-ratelimit')) {\n\t\t\tcontinue;\n\t\t}\n\n\t\tres.setHeader(header, data.headers[header]!);\n\t}\n\n\tawait pipeline(data.body, res);\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\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"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,sBAAoB;;;ACCpB,sBAAyB;AACzB,kBAA2D;AAS3D,eAAsB,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AE;AAElE,UAAM,UAAkC;AAAA,MACvC,gBAAgB,IAAI,QAAQ,cAAc;AAAA,IAC3C;AAEA,QAAI,IAAI,QAAQ,eAAe;AAC9B,cAAQ,gBAAgB,IAAI,QAAQ;AAAA,IACrC;AAEA,QAAI;AACH,YAAM,kBAAkB,MAAM,KAAK,IAAI;AAAA,QACtC,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,OAAP;AACD,YAAM,aAAa,sBAAsB,KAAK,KAAK;AACnD,UAAI,CAAC,YAAY;AAIhB,cAAM;AAAA,MACP;AAAA,IACD,UAAE;AACD,UAAI,IAAI;AAAA,IACT;AAAA,EACD;AACD;AAlDgB;","names":[]}
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/handlers/proxyRequests.ts","../src/util/responseHelpers.ts"],"sourcesContent":["import { URL } from 'node:url';\nimport type { RequestMethod, REST, RouteLike } from '@discordjs/rest';\nimport { populateSuccessfulResponse, populateErrorResponse } from '../util/responseHelpers.js';\nimport type { RequestHandler } from '../util/util';\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 unicorn/no-unsafe-regex, 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\ttry {\n\t\t\tconst discordResponse = await rest.raw({\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","import type { ServerResponse } from 'node:http';\nimport { pipeline } from 'node:stream/promises';\nimport { DiscordAPIError, HTTPError, RateLimitError } from '@discordjs/rest';\nimport type { Dispatcher } from 'undici';\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: Dispatcher.ResponseData): Promise<void> {\n\tres.statusCode = data.statusCode;\n\n\tfor (const header of Object.keys(data.headers)) {\n\t\t// Strip ratelimit headers\n\t\tif (header.startsWith('x-ratelimit')) {\n\t\t\tcontinue;\n\t\t}\n\n\t\tres.setHeader(header, data.headers[header]!);\n\t}\n\n\tawait pipeline(data.body, res);\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\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"],"mappings":";;;;AAAA,SAAS,WAAW;;;ACCpB,SAAS,gBAAgB;AACzB,SAAS,iBAAiB,WAAW,sBAAsB;AAS3D,eAAsB,2BAA2B,KAAqB,MAA8C;AACnH,MAAI,aAAa,KAAK;AAEtB,aAAW,UAAU,OAAO,KAAK,KAAK,OAAO,GAAG;AAE/C,QAAI,OAAO,WAAW,aAAa,GAAG;AACrC;AAAA,IACD;AAEA,QAAI,UAAU,QAAQ,KAAK,QAAQ,MAAM,CAAE;AAAA,EAC5C;AAEA,QAAM,SAAS,KAAK,MAAM,GAAG;AAC9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AAA,QAEA,MAAM;AAAA,QACN,iBAAiB;AAAA,QACjB,OAAO,UAAU;AAAA,QACjB;AAAA,MACD,CAAC;AAED,YAAM,2BAA2B,KAAK,eAAe;AAAA,IACtD,SAAS,OAAP;AACD,YAAM,aAAa,sBAAsB,KAAK,KAAK;AACnD,UAAI,CAAC,YAAY;AAIhB,cAAM;AAAA,MACP;AAAA,IACD,UAAE;AACD,UAAI,IAAI;AAAA,IACT;AAAA,EACD;AACD;AAlDgB;","names":[]}
1
+ {"version":3,"sources":["../src/handlers/proxyRequests.ts","../src/util/responseHelpers.ts"],"sourcesContent":["import { URL } from 'node:url';\nimport 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 unicorn/no-unsafe-regex, 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\ttry {\n\t\t\tconst discordResponse = await rest.raw({\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","import type { ServerResponse } from 'node:http';\nimport { pipeline } from 'node:stream/promises';\nimport { DiscordAPIError, HTTPError, RateLimitError } from '@discordjs/rest';\nimport type { Dispatcher } from 'undici';\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: Dispatcher.ResponseData): Promise<void> {\n\tres.statusCode = data.statusCode;\n\n\tfor (const header of Object.keys(data.headers)) {\n\t\t// Strip ratelimit headers\n\t\tif (header.startsWith('x-ratelimit')) {\n\t\t\tcontinue;\n\t\t}\n\n\t\tres.setHeader(header, data.headers[header]!);\n\t}\n\n\tawait pipeline(data.body, res);\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\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"],"mappings":";;;;AAAA,SAAS,WAAW;;;ACCpB,SAAS,gBAAgB;AACzB,SAAS,iBAAiB,WAAW,sBAAsB;AAS3D,eAAsB,2BAA2B,KAAqB,MAA8C;AACnH,MAAI,aAAa,KAAK;AAEtB,aAAW,UAAU,OAAO,KAAK,KAAK,OAAO,GAAG;AAE/C,QAAI,OAAO,WAAW,aAAa,GAAG;AACrC;AAAA,IACD;AAEA,QAAI,UAAU,QAAQ,KAAK,QAAQ,MAAM,CAAE;AAAA,EAC5C;AAEA,QAAM,SAAS,KAAK,MAAM,GAAG;AAC9B;AAbsB;AAqBf,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,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IACT;AAAA,EACD;AACD;AAlDgB;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@discordjs/proxy",
3
- "version": "1.4.1-dev.1682856232-ad217cc.0",
3
+ "version": "1.4.1",
4
4
  "description": "Tools for running an HTTP proxy for Discord's API",
5
5
  "scripts": {
6
6
  "test": "vitest run",
@@ -18,9 +18,9 @@
18
18
  "module": "./dist/index.mjs",
19
19
  "typings": "./dist/index.d.ts",
20
20
  "exports": {
21
+ "types": "./dist/index.d.ts",
21
22
  "import": "./dist/index.mjs",
22
- "require": "./dist/index.js",
23
- "types": "./dist/index.d.ts"
23
+ "require": "./dist/index.js"
24
24
  },
25
25
  "directories": {
26
26
  "lib": "src",
@@ -56,8 +56,8 @@
56
56
  },
57
57
  "homepage": "https://discord.js.org",
58
58
  "dependencies": {
59
- "@discordjs/rest": "^1.7.0",
60
- "@discordjs/util": "^0.2.0",
59
+ "@discordjs/rest": "^1.7.1",
60
+ "@discordjs/util": "^0.3.0",
61
61
  "tslib": "^2.5.0",
62
62
  "undici": "^5.22.0"
63
63
  },