rjweb-server
Version:
Easy and Robust Way to create a Web Server with Many Easy-to-use Features in NodeJS
156 lines (155 loc) • 5.29 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var http_exports = {};
__export(http_exports, {
default: () => RouteHTTP
});
module.exports = __toCommonJS(http_exports);
var import_path = __toESM(require("../path"));
class RouteHTTP {
/** Generate HTTP Endpoint */
constructor(path, method, validations = [], headers = {}) {
this.data = {
type: "http",
method,
path: new import_path.default(method, path),
onRequest: () => null,
data: {
validations,
headers
},
context: { data: {}, keep: true }
};
}
/**
* Add a default State for the Request Context (which stays for the entire requests lifecycle)
*
* This will set the default context for the request. This applies to all callbacks
* attached to this handler. When `keepForever` is enabled, the context will be shared
* between requests to this callback and therefore will be globally mutable. This may be
* useful for something like a request counter so you dont have to worry about transferring
* it around.
* @example
* ```
* const controller = new Server({ })
*
* controller.path('/', (path) => path
* .http('GET', '/context', (ws) => ws
* .context({
* text: 'hello world'
* }, {
* keepForever: true // If enabled this Context will be used & kept for every request on this route, so if you change something in it it will stay for the next time this request runs
* })
* .onRequest((ctr) => {
* ctr.print(ctr["@"].text)
* })
* )
* )
* ```
* @since 7.0.0
*/
context(context, options = {}) {
const keepForever = options?.keepForever ?? false;
this.data.context = {
data: context,
keep: keepForever
};
return 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.ctx.body.chunks.push(chunk)`.
* @warning when using this, `ctr.body`, `ctr.rawBody` and `ctr.rawBodyBytes` will always be empty
* @example
* ```
* const controller = new Server({ })
*
* controller.path('/', (path) => path
* .http('POST', '/hello', (http) => http
* .context({
* chunkCount: 0
* })
* .onRawBody((ctr, end, chunk, isLast) => {
* ctr["@"].chunkCount++
*
* console.log(`Recieved Chunk, isLast: ${isLast}`, chunk)
* if (ctr["@"].chunkCount > 10) end() // This stops recieving chunks and will continue to http
* })
* .onRequest((ctr) => {
* ctr.print(`I received ${ctr["@"].chunkCount} chunks!`)
* if (ctr["@"].chunkCount === 10) ctr.printPart(' You reached the maximum allowed!')
* })
* )
* )
* ```
* @since 6.0.0
*/
onRawBody(code) {
this.data.onRawBody = code;
return 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 controller = new Server({ })
*
* controller.path('/', (path) => path
* .http('GET', '/hello', (ws) => ws
* .onRequest((ctr) => {
* ctr.print('Hello')
* })
* )
* )
* ```
* @since 6.0.0
*/
onRequest(code) {
this.data.onRequest = code;
return this;
}
/**
* Internal Method for Generating Route Object
* @since 6.0.0
*/
getData(prefix) {
this.data.path.addPrefix(prefix);
return {
routes: [this.data]
};
}
}