@discordjs/proxy 1.0.0-dev.1656547799-27d8deb → 1.0.0-dev.1656677077-741b3c8

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/dist/index.d.ts CHANGED
@@ -13,31 +13,36 @@ declare type RequestHandler = (req: IncomingMessage, res: ServerResponse) => Awa
13
13
 
14
14
  /**
15
15
  * Creates an HTTP handler used to forward requests to Discord
16
- * @param rest REST instance to use for the requests
16
+ *
17
+ * @param rest - REST instance to use for the requests
17
18
  */
18
19
  declare function proxyRequests(rest: REST): RequestHandler;
19
20
 
20
21
  /**
21
22
  * Populates a server response with the data from a Discord 2xx REST response
22
- * @param res The server response to populate
23
- * @param data The data to populate the response with
23
+ *
24
+ * @param res - The server response to populate
25
+ * @param data - The data to populate the response with
24
26
  */
25
27
  declare function populateSuccessfulResponse(res: ServerResponse, data: Dispatcher.ResponseData): Promise<void>;
26
28
  /**
27
29
  * Populates a server response with the data from a Discord non-2xx REST response that is NOT a 429
28
- * @param res The server response to populate
29
- * @param error The error to populate the response with
30
+ *
31
+ * @param res - The server response to populate
32
+ * @param error - The error to populate the response with
30
33
  */
31
34
  declare function populateGeneralErrorResponse(res: ServerResponse, error: DiscordAPIError | HTTPError): void;
32
35
  /**
33
36
  * Populates a server response with the data from a Discord 429 REST response
34
- * @param res The server response to populate
35
- * @param error The error to populate the response with
37
+ *
38
+ * @param res - The server response to populate
39
+ * @param error - The error to populate the response with
36
40
  */
37
41
  declare function populateRatelimitErrorResponse(res: ServerResponse, error: RateLimitError): void;
38
42
  /**
39
- * Populates a server response with data relevant for a time out
40
- * @param res The sever response to populate
43
+ * Populates a server response with data relevant for a timeout
44
+ *
45
+ * @param res - The sever response to populate
41
46
  */
42
47
  declare function populateAbortErrorResponse(res: ServerResponse): void;
43
48
 
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';\nexport * from './util/responseHelpers';\nexport { RequestHandler } from './util/util';\n","import { URL } from 'node:url';\nimport { DiscordAPIError, HTTPError, RateLimitError, RequestMethod, REST, RouteLike } from '@discordjs/rest';\nimport {\n\tpopulateAbortErrorResponse,\n\tpopulateGeneralErrorResponse,\n\tpopulateSuccessfulResponse,\n\tpopulateRatelimitErrorResponse,\n} from '../util/responseHelpers';\nimport type { RequestHandler } from '../util/util';\n\n/**\n * Creates an HTTP handler used to forward requests to Discord\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 IncomingMesage',\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 fullRoute = new URL(url, 'http://noop').pathname.replace(/^\\/api(\\/v\\d+)?/, '') as RouteLike;\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\tpassThroughBody: true,\n\t\t\t\theaders: {\n\t\t\t\t\t'Content-Type': req.headers['content-type']!,\n\t\t\t\t},\n\t\t\t});\n\n\t\t\tawait populateSuccessfulResponse(res, discordResponse);\n\t\t} catch (error) {\n\t\t\tif (error instanceof DiscordAPIError || error instanceof HTTPError) {\n\t\t\t\tpopulateGeneralErrorResponse(res, error);\n\t\t\t} else if (error instanceof RateLimitError) {\n\t\t\t\tpopulateRatelimitErrorResponse(res, error);\n\t\t\t} else if (error instanceof Error && error.name === 'AbortError') {\n\t\t\t\tpopulateAbortErrorResponse(res);\n\t\t\t} else {\n\t\t\t\t// Unclear if there's better course of action here for unknown erorrs. 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 type { 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 * @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 * @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 * @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 / 1000);\n}\n\n/**\n * Populates a server response with data relevant for a time out\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"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,sBAAoB;AACpB,kBAA2F;;;ACA3F,sBAAyB;AASzB,0CAAiD,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,OAAQ;AAAA,EAC5C;AAEA,QAAM,8BAAS,KAAK,MAAM,GAAG;AAC9B;AAbsB;AAoBf,sCAAsC,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;AAcT,wCAAwC,KAAqB,OAA6B;AAChG,MAAI,aAAa;AACjB,MAAI,UAAU,eAAe,MAAM,cAAc,GAAI;AACtD;AAHgB;AAST,oCAAoC,KAA2B;AACrE,MAAI,aAAa;AACjB,MAAI,gBAAgB;AACrB;AAHgB;;;ADvCT,uBAAuB,MAA4B;AACzD,SAAO,OAAO,KAAK,QAAQ;AAC1B,UAAM,EAAE,QAAQ,QAAQ;AAExB,QAAI,CAAC,UAAU,CAAC,KAAK;AACpB,YAAM,IAAI,UACT,+FACD;AAAA,IACD;AAIA,UAAM,YAAY,IAAI,oBAAI,KAAK,aAAa,EAAE,SAAS,QAAQ,mBAAmB,EAAE;AAEpF,QAAI;AACH,YAAM,kBAAkB,MAAM,KAAK,IAAI;AAAA,QACtC,MAAM;AAAA,QACN;AAAA,QAEA;AAAA,QACA,iBAAiB;AAAA,QACjB,SAAS;AAAA,UACR,gBAAgB,IAAI,QAAQ;AAAA,QAC7B;AAAA,MACD,CAAC;AAED,YAAM,2BAA2B,KAAK,eAAe;AAAA,IACtD,SAAS,OAAP;AACD,UAAI,iBAAiB,+BAAmB,iBAAiB,uBAAW;AACnE,qCAA6B,KAAK,KAAK;AAAA,MACxC,WAAW,iBAAiB,4BAAgB;AAC3C,uCAA+B,KAAK,KAAK;AAAA,MAC1C,WAAW,iBAAiB,SAAS,MAAM,SAAS,cAAc;AACjE,mCAA2B,GAAG;AAAA,MAC/B,OAAO;AAGN,cAAM;AAAA,MACP;AAAA,IACD,UAAE;AACD,UAAI,IAAI;AAAA,IACT;AAAA,EACD;AACD;AA3CgB;","names":[]}
1
+ {"version":3,"sources":["../src/index.ts","../src/handlers/proxyRequests.ts","../src/util/responseHelpers.ts"],"sourcesContent":["export * from './handlers/proxyRequests';\nexport * from './util/responseHelpers';\nexport { RequestHandler } from './util/util';\n","import { URL } from 'node:url';\nimport { DiscordAPIError, HTTPError, RateLimitError, RequestMethod, REST, RouteLike } from '@discordjs/rest';\nimport {\n\tpopulateAbortErrorResponse,\n\tpopulateGeneralErrorResponse,\n\tpopulateSuccessfulResponse,\n\tpopulateRatelimitErrorResponse,\n} from '../util/responseHelpers';\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 IncomingMesage',\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 fullRoute = new URL(url, 'http://noop').pathname.replace(/^\\/api(\\/v\\d+)?/, '') as RouteLike;\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\tpassThroughBody: true,\n\t\t\t\theaders: {\n\t\t\t\t\t'Content-Type': req.headers['content-type']!,\n\t\t\t\t},\n\t\t\t});\n\n\t\t\tawait populateSuccessfulResponse(res, discordResponse);\n\t\t} catch (error) {\n\t\t\tif (error instanceof DiscordAPIError || error instanceof HTTPError) {\n\t\t\t\tpopulateGeneralErrorResponse(res, error);\n\t\t\t} else if (error instanceof RateLimitError) {\n\t\t\t\tpopulateRatelimitErrorResponse(res, error);\n\t\t\t} else if (error instanceof Error && error.name === 'AbortError') {\n\t\t\t\tpopulateAbortErrorResponse(res);\n\t\t\t} else {\n\t\t\t\t// Unclear if there's better course of action here for unknown erorrs. 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 type { 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 / 1000);\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"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,sBAAoB;AACpB,kBAA2F;;;ACA3F,sBAAyB;AAUzB,0CAAiD,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,OAAQ;AAAA,EAC5C;AAEA,QAAM,8BAAS,KAAK,MAAM,GAAG;AAC9B;AAbsB;AAqBf,sCAAsC,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,wCAAwC,KAAqB,OAA6B;AAChG,MAAI,aAAa;AACjB,MAAI,UAAU,eAAe,MAAM,cAAc,GAAI;AACtD;AAHgB;AAUT,oCAAoC,KAA2B;AACrE,MAAI,aAAa;AACjB,MAAI,gBAAgB;AACrB;AAHgB;;;AD1CT,uBAAuB,MAA4B;AACzD,SAAO,OAAO,KAAK,QAAQ;AAC1B,UAAM,EAAE,QAAQ,QAAQ;AAExB,QAAI,CAAC,UAAU,CAAC,KAAK;AACpB,YAAM,IAAI,UACT,+FACD;AAAA,IACD;AAIA,UAAM,YAAY,IAAI,oBAAI,KAAK,aAAa,EAAE,SAAS,QAAQ,mBAAmB,EAAE;AAEpF,QAAI;AACH,YAAM,kBAAkB,MAAM,KAAK,IAAI;AAAA,QACtC,MAAM;AAAA,QACN;AAAA,QAEA;AAAA,QACA,iBAAiB;AAAA,QACjB,SAAS;AAAA,UACR,gBAAgB,IAAI,QAAQ;AAAA,QAC7B;AAAA,MACD,CAAC;AAED,YAAM,2BAA2B,KAAK,eAAe;AAAA,IACtD,SAAS,OAAP;AACD,UAAI,iBAAiB,+BAAmB,iBAAiB,uBAAW;AACnE,qCAA6B,KAAK,KAAK;AAAA,MACxC,WAAW,iBAAiB,4BAAgB;AAC3C,uCAA+B,KAAK,KAAK;AAAA,MAC1C,WAAW,iBAAiB,SAAS,MAAM,SAAS,cAAc;AACjE,mCAA2B,GAAG;AAAA,MAC/B,OAAO;AAGN,cAAM;AAAA,MACP;AAAA,IACD,UAAE;AACD,UAAI,IAAI;AAAA,IACT;AAAA,EACD;AACD;AA3CgB;","names":[]}
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/handlers/proxyRequests.ts","../src/util/responseHelpers.ts"],"sourcesContent":["import { URL } from 'node:url';\nimport { DiscordAPIError, HTTPError, RateLimitError, RequestMethod, REST, RouteLike } from '@discordjs/rest';\nimport {\n\tpopulateAbortErrorResponse,\n\tpopulateGeneralErrorResponse,\n\tpopulateSuccessfulResponse,\n\tpopulateRatelimitErrorResponse,\n} from '../util/responseHelpers';\nimport type { RequestHandler } from '../util/util';\n\n/**\n * Creates an HTTP handler used to forward requests to Discord\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 IncomingMesage',\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 fullRoute = new URL(url, 'http://noop').pathname.replace(/^\\/api(\\/v\\d+)?/, '') as RouteLike;\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\tpassThroughBody: true,\n\t\t\t\theaders: {\n\t\t\t\t\t'Content-Type': req.headers['content-type']!,\n\t\t\t\t},\n\t\t\t});\n\n\t\t\tawait populateSuccessfulResponse(res, discordResponse);\n\t\t} catch (error) {\n\t\t\tif (error instanceof DiscordAPIError || error instanceof HTTPError) {\n\t\t\t\tpopulateGeneralErrorResponse(res, error);\n\t\t\t} else if (error instanceof RateLimitError) {\n\t\t\t\tpopulateRatelimitErrorResponse(res, error);\n\t\t\t} else if (error instanceof Error && error.name === 'AbortError') {\n\t\t\t\tpopulateAbortErrorResponse(res);\n\t\t\t} else {\n\t\t\t\t// Unclear if there's better course of action here for unknown erorrs. 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 type { 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 * @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 * @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 * @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 / 1000);\n}\n\n/**\n * Populates a server response with data relevant for a time out\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"],"mappings":";;;;;AAAA;AACA;;;ACAA;AASA,0CAAiD,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,OAAQ;AAAA,EAC5C;AAEA,QAAM,SAAS,KAAK,MAAM,GAAG;AAC9B;AAbsB;AAoBf,sCAAsC,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;AAcT,wCAAwC,KAAqB,OAA6B;AAChG,MAAI,aAAa;AACjB,MAAI,UAAU,eAAe,MAAM,cAAc,GAAI;AACtD;AAHgB;AAST,oCAAoC,KAA2B;AACrE,MAAI,aAAa;AACjB,MAAI,gBAAgB;AACrB;AAHgB;;;ADvCT,uBAAuB,MAA4B;AACzD,SAAO,OAAO,KAAK,QAAQ;AAC1B,UAAM,EAAE,QAAQ,QAAQ;AAExB,QAAI,CAAC,UAAU,CAAC,KAAK;AACpB,YAAM,IAAI,UACT,+FACD;AAAA,IACD;AAIA,UAAM,YAAY,IAAI,IAAI,KAAK,aAAa,EAAE,SAAS,QAAQ,mBAAmB,EAAE;AAEpF,QAAI;AACH,YAAM,kBAAkB,MAAM,KAAK,IAAI;AAAA,QACtC,MAAM;AAAA,QACN;AAAA,QAEA;AAAA,QACA,iBAAiB;AAAA,QACjB,SAAS;AAAA,UACR,gBAAgB,IAAI,QAAQ;AAAA,QAC7B;AAAA,MACD,CAAC;AAED,YAAM,2BAA2B,KAAK,eAAe;AAAA,IACtD,SAAS,OAAP;AACD,UAAI,iBAAiB,mBAAmB,iBAAiB,WAAW;AACnE,qCAA6B,KAAK,KAAK;AAAA,MACxC,WAAW,iBAAiB,gBAAgB;AAC3C,uCAA+B,KAAK,KAAK;AAAA,MAC1C,WAAW,iBAAiB,SAAS,MAAM,SAAS,cAAc;AACjE,mCAA2B,GAAG;AAAA,MAC/B,OAAO;AAGN,cAAM;AAAA,MACP;AAAA,IACD,UAAE;AACD,UAAI,IAAI;AAAA,IACT;AAAA,EACD;AACD;AA3CgB;","names":[]}
1
+ {"version":3,"sources":["../src/handlers/proxyRequests.ts","../src/util/responseHelpers.ts"],"sourcesContent":["import { URL } from 'node:url';\nimport { DiscordAPIError, HTTPError, RateLimitError, RequestMethod, REST, RouteLike } from '@discordjs/rest';\nimport {\n\tpopulateAbortErrorResponse,\n\tpopulateGeneralErrorResponse,\n\tpopulateSuccessfulResponse,\n\tpopulateRatelimitErrorResponse,\n} from '../util/responseHelpers';\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 IncomingMesage',\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 fullRoute = new URL(url, 'http://noop').pathname.replace(/^\\/api(\\/v\\d+)?/, '') as RouteLike;\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\tpassThroughBody: true,\n\t\t\t\theaders: {\n\t\t\t\t\t'Content-Type': req.headers['content-type']!,\n\t\t\t\t},\n\t\t\t});\n\n\t\t\tawait populateSuccessfulResponse(res, discordResponse);\n\t\t} catch (error) {\n\t\t\tif (error instanceof DiscordAPIError || error instanceof HTTPError) {\n\t\t\t\tpopulateGeneralErrorResponse(res, error);\n\t\t\t} else if (error instanceof RateLimitError) {\n\t\t\t\tpopulateRatelimitErrorResponse(res, error);\n\t\t\t} else if (error instanceof Error && error.name === 'AbortError') {\n\t\t\t\tpopulateAbortErrorResponse(res);\n\t\t\t} else {\n\t\t\t\t// Unclear if there's better course of action here for unknown erorrs. 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 type { 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 / 1000);\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"],"mappings":";;;;;AAAA;AACA;;;ACAA;AAUA,0CAAiD,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,OAAQ;AAAA,EAC5C;AAEA,QAAM,SAAS,KAAK,MAAM,GAAG;AAC9B;AAbsB;AAqBf,sCAAsC,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,wCAAwC,KAAqB,OAA6B;AAChG,MAAI,aAAa;AACjB,MAAI,UAAU,eAAe,MAAM,cAAc,GAAI;AACtD;AAHgB;AAUT,oCAAoC,KAA2B;AACrE,MAAI,aAAa;AACjB,MAAI,gBAAgB;AACrB;AAHgB;;;AD1CT,uBAAuB,MAA4B;AACzD,SAAO,OAAO,KAAK,QAAQ;AAC1B,UAAM,EAAE,QAAQ,QAAQ;AAExB,QAAI,CAAC,UAAU,CAAC,KAAK;AACpB,YAAM,IAAI,UACT,+FACD;AAAA,IACD;AAIA,UAAM,YAAY,IAAI,IAAI,KAAK,aAAa,EAAE,SAAS,QAAQ,mBAAmB,EAAE;AAEpF,QAAI;AACH,YAAM,kBAAkB,MAAM,KAAK,IAAI;AAAA,QACtC,MAAM;AAAA,QACN;AAAA,QAEA;AAAA,QACA,iBAAiB;AAAA,QACjB,SAAS;AAAA,UACR,gBAAgB,IAAI,QAAQ;AAAA,QAC7B;AAAA,MACD,CAAC;AAED,YAAM,2BAA2B,KAAK,eAAe;AAAA,IACtD,SAAS,OAAP;AACD,UAAI,iBAAiB,mBAAmB,iBAAiB,WAAW;AACnE,qCAA6B,KAAK,KAAK;AAAA,MACxC,WAAW,iBAAiB,gBAAgB;AAC3C,uCAA+B,KAAK,KAAK;AAAA,MAC1C,WAAW,iBAAiB,SAAS,MAAM,SAAS,cAAc;AACjE,mCAA2B,GAAG;AAAA,MAC/B,OAAO;AAGN,cAAM;AAAA,MACP;AAAA,IACD,UAAE;AACD,UAAI,IAAI;AAAA,IACT;AAAA,EACD;AACD;AA3CgB;","names":[]}
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@discordjs/proxy",
3
- "version": "1.0.0-dev.1656547799-27d8deb",
3
+ "version": "1.0.0-dev.1656677077-741b3c8",
4
4
  "description": "Tools for running an HTTP proxy for Discord's API",
5
5
  "scripts": {
6
6
  "build": "tsup",
7
7
  "lint": "prettier --cache --check . && eslint src __tests__ --ext mjs,js,ts --cache",
8
8
  "format": "prettier --cache --write . && eslint src __tests__ --ext mjs,js,ts --fix --cache",
9
- "docs": "docgen -i src/index.ts -c docs/index.json -o docs/docs.json --typescript",
9
+ "docs": "docgen -i src/index.ts -c docs/index.json -o docs/docs.json --typescript && api-extractor run --local",
10
10
  "prepack": "yarn build && yarn lint",
11
11
  "changelog": "git cliff --prepend ./CHANGELOG.md -u -c ./cliff.toml -r ../../ --include-path 'packages/proxy/*'",
12
12
  "release": "cliff-jumper"
@@ -60,6 +60,7 @@
60
60
  "@discordjs/docgen": "^0.12.0-dev",
61
61
  "@discordjs/scripts": "^0.1.0-dev",
62
62
  "@favware/cliff-jumper": "^1.8.3",
63
+ "@microsoft/api-extractor": "^7.28.2",
63
64
  "@types/node": "^18.0.0",
64
65
  "@types/supertest": "^2.0.12",
65
66
  "eslint": "^8.18.0",