UNPKG

rjweb-server

Version:

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

194 lines (193 loc) 6.26 kB
"use strict"; 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 ws_exports = {}; __export(ws_exports, { default: () => RouteWS }); module.exports = __toCommonJS(ws_exports); var import_path = __toESM(require("../path")); class RouteWS { /** Generate WS Endpoint */ constructor(path, validations = [], headers = {}) { this.data = { type: "websocket", path: new import_path.default("GET", path), data: { validations, headers }, context: { data: {}, keep: true } }; } /** * Add a default State for the Request Context (which stays for the entire requests / websockets 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 message or request counter so you dont have to worry about * transferring it around. * @example * ``` * const controller = new Server({ }) * * controller.path('/', (path) => path * .ws('GET', '/ws', (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 * }) * .onConnect((ctr) => { * console.log('Connected to Client!', 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 someone wants to Upgrade from HTTP a Socket * * This will attach a callback for when the client sends an http request but * wants to upgrade to a websocket connection. This callback will probably only * ever be used to initialize the context or validate the request before transferring * to a connection. when wanting to stop the server from upgrading, call the 2nd * function argument to force ending the connection process and switching to a normal * http request. * @example * ``` * const controller = new Server({ }) * * controller.path('/', (path) => path * .ws('/ws', (ws) => ws * .onUpgrade((ctr, end) => { * if (!ctr.queries.has('confirm')) return end(ctr.status(Status.BAD_REQUEST).print('Forgor the Confirm query')) * }) * ) * ) * ``` * @since 5.10.0 */ onUpgrade(code) { this.data.onUpgrade = code; return this; } /** * Attach a Callback for when someone Establishes a connection to the Socket * * This will attach a callback for when the websocket connection is established * and ready for use. This callback will commonly be used to attach references using * `.printRef()` or similar. * @example * ``` * const controller = new Server({ }) * * controller.path('/', (path) => path * .ws('/ws', (ws) => ws * .onConnect((ctr) => { * console.log('Connected to Client') * }) * ) * ) * ``` * @since 5.4.0 */ onConnect(code) { this.data.onConnect = code; return this; } /** * Attach a Callback for when someone sends a Message to the Socket * * This will attach a callback for when the server recieves a message from * the Client. This event can be disabled completely by setting `message.enabled` * to false in the initial Server Options. * @example * ``` * const controller = new Server({ }) * * controller.path('/', (path) => path * .ws('/ws', (ws) => ws * .onMessage((ctr) => { * ctr.print(ctr.message) * }) * ) * ) * ``` * @since 5.4.0 */ onMessage(code) { this.data.onMessage = code; return this; } /** * Attach a Callback for when the Socket closes * * This will attach a callback for when the Socket is closed in any way * that should trigger this (basically all). In this callback `.message` * will be the message the client sent as reason for the close or just empty * when the client didnt send a reason or the server closed the socket. * @example * ``` * const controller = new Server({ }) * * controller.path('/', (path) => path * .ws('/ws', (ws) => ws * .onClose((ctr) => { * console.log('Closed Client Connection') * }) * ) * ) * ``` * @since 5.4.0 */ onClose(code) { this.data.onClose = code; return this; } /** * Internal Method for Generating WebSocket Object * @since 6.0.0 */ getData(prefix) { this.data.path.addPrefix(prefix); return { webSockets: [this.data] }; } }