UNPKG

rjweb-server

Version:

Easy and Robust Way to create a Web Server with Many Easy-to-use Features in NodeJS

135 lines (134 loc) 4.66 kB
import Route from "../Route"; import RateLimit from "./RateLimit"; import { as, object } from "@rjweb/utils"; export default class Http { /** * Create a new HTTP Route Builder * @since 7.0.0 */ constructor(method, path, ratelimit) { this.route = new Route('http', method, path, {}); if (ratelimit) this.route.ratelimit = ratelimit; } /** * Add OpenAPI Documentation to all HTTP Endpoints in this Path (and all children) * @since 9.0.0 */ document(item) { this.route.openApi = object.deepMerge(this.route.openApi, item); return this; } /** * Add a Ratelimit to this Endpoint * * When a User requests this Endpoint, that will count against their hit count, if * the hits exceeds the `<maxHits>` in `<timeWindow>ms`, they wont be able to access * the route for `<penalty>ms`. * @example * ``` * import { time } from "@rjweb/utils" * const server = new Server(...) * * server.path('/', (path) => path * .http('GET', '/hello', (ws) => ws * .ratelimit((limit) => limit * .hits(5) * .timeWindow(time(20).s()) * .penalty(0) * ) // This will allow 5 requests every 20 seconds * .onRequest(async(ctr) => { * ctr.print('Hello bro!') * }) * ) * ) * * server.rateLimit('httpRequest', (ctr) => { * ctr.print(`Please wait ${ctr.getRateLimit()?.resetIn}ms!!!!!`) * }) * ``` * @since 8.6.0 */ ratelimit(callback) { const limit = new RateLimit(); if (this.route.ratelimit) limit['data'] = Object.assign({}, this.route.ratelimit); callback(limit); this.route.ratelimit = limit['data']; return as(this); } /** * Add additional Context to this Endpoint * * This will add additional context to this Endpoint, which will be available in the * `ctr` object when the Endpoint is executed. * @since 7.0.0 */ context() { return as(this); } /** * Use a Validator on this Endpoint * * This will attach a Validator to this Endpoint, which will be run before the * Endpoint is executed. If the Validator fails, the Endpoint will not be executed. * @since 9.0.0 */ validate(validator) { this.route.validators.push(validator); this.route.openApi = object.deepMerge(this.route.openApi, validator.openApi); return as(this); } /** * Attach a Callback for when someone makes an HTTP request * * This will attach a callback for when the server recieves a http request and * finishes parsing it for the user. This Handler should always be set unless you * are reserving a path for later or something. * @example * ``` * const server = new Server(...) * * server.path('/', (path) => path * .http('GET', '/hello', (http) => http * .onRequest((ctr) => { * ctr.print('Hello') * }) * ) * ) * ``` * @since 6.0.0 */ onRequest(callback) { this.route.data.onRequest = callback; return as(this); } /** * Attach a Callback for when the server recieves a HTTP body chunk * * This will attach a callback for when the server receives an http POST body chunk, the * request can always be ended by calling the 2nd function argument. Attaching this will * cause `ctr.body`, `ctr.rawBody` and `ctr.rawBodyBytes` to be empty unless you manually * assign them by doing `ctr.context.body.chunks.push(chunk)`. * @warning when using this, `ctr.body`, `ctr.rawBody` and `ctr.rawBodyBytes` will always be empty * @example * ``` * const server = new Server(...) * * server.path('/', (path) => path * .http('POST', '/hello', (http) => http * .context<{ * chunkCount: number * }>() * .onRawBody((ctr, end, chunk, isLast) => { * ctr["@"].chunkCount = (ctr["@"].chunkCount || 0) + 1 * * console.log(`Recieved Chunk, isLast: ${isLast}`, chunk) * if (ctr["@"].chunkCount > 10) end(ctr.status(ctr.$status.BAD_REUQEST).print('too many chunks')) // This stops recieving chunks and will end the http request * }) * .onRequest((ctr) => { * return ctr.print(`I received ${ctr["@"].chunkCount} chunks!`) * }) * ) * ) * ``` * @since 6.0.0 */ onRawBody(callback) { this.route.data.onRawBody = callback; return as(this); } }