UNPKG

@nasriya/hypercloud

Version:

Nasriya HyperCloud is a lightweight Node.js HTTP2 framework.

188 lines (187 loc) 8 kB
import { HelmetConfigOptions, HyperCloudInitFile, HyperCloudManagementOptions, HyperCloudRequestErrorHandler, HyperCloudRequestHandler, SecureServerOptions, ServerListeningConfigs, ServerOptions } from './docs/docs'; import RenderingManager from './services/renderer/manager'; import RoutesManager from './services/routes/manager'; import Router from './services/routes/assets/router'; import RateLimitingManager from './services/rateLimiter/rateLimiter'; import LanguagesManager from './services/languages/manager'; import Uploads from './services/uploads/uploads'; /**HyperCloud HTTP2 server */ export declare class HyperCloudServer { #private; constructor(userOptions?: SecureServerOptions | ServerOptions | HyperCloudInitFile, addOpt?: HyperCloudManagementOptions); /** * Configuration object for handling file uploads. * * This object provides settings to manage and enforce upload limits for different types of files. It includes general limits for images and videos, as well as specific limits based on MIME types. The MIME type-specific limits take precedence over the general image and video limits. The configuration allows for the following: * * - Setting and retrieving maximum file size limits for images and videos. * - Setting and retrieving file size limits for specific MIME types. * - Removing limits by setting them to `0`. * * The configuration is intended to help manage resource usage and ensure that uploads adhere to defined size constraints. * * Example usage: * * ```ts * // Set a maximum file size of 10 MB for images * server.uploads.limits.images.set(10 * 1024 * 1024); * * // Set a maximum file size of 50 MB for videos * server.uploads.limits.videos.set(50 * 1024 * 1024); * * // Set a maximum file size of 5 MB for application/pdf MIME type * server.uploads.limits.mime.set('application/pdf', 5 * 1024 * 1024); * * // Get the maximum file size limit for images * const imageLimit = server.uploads.limits.images.get(); * * // Get the maximum file size limit for a specific MIME type * const pdfLimit = server.uploads.limits.mime.get('application/pdf'); * ``` */ readonly uploads: Uploads; /** * Set or get your site/brand name. This name is used * for rendering pages and in other places */ readonly siteName: { /** * Set your site's name * @example * server.siteName.set('Nasriya Software'); // Setting a name for the default language * server.siteName.set('ناصرية سوفتوير', 'ar'); // Setting a name for the "ar" language * @param name The name of your site or brand * @param lang The language you want your site name to be associated with */ readonly set: (name: string, lang?: string) => void; /** * Get the name of your site/brand based on the language * @example * // Getting the name of the default language * server.siteName.get(); // returns: "Nasriya Software" * server.siteName.get('ar'); // returns: "ناصرية سوفتوير" * @param lang The language your site name is associated with */ readonly get: (lang?: string) => string; /** * Set multiple site names for different languages. * @example * server.siteName.multilingual({ * default: 'Nasriya Software', * ar: 'ناصرية سوفتوير' * }); * @param record An object where the keys are language codes and the values are the site names. */ readonly multilingual: (record: Record<string, string>) => void; }; readonly languages: LanguagesManager; readonly rateLimiter: RateLimitingManager; readonly rendering: RenderingManager; /**@private */ readonly _routesManager: RoutesManager; /**@private */ get _handlers(): Record<string, Function>; readonly handlers: Readonly<{ notFound: (handler: HyperCloudRequestHandler) => void; serverError: (handler: HyperCloudRequestHandler) => void; unauthorized: (handler: HyperCloudRequestHandler) => void; forbidden: (handler: HyperCloudRequestHandler) => void; userSessions: (handler: HyperCloudRequestHandler) => void; logger: (handler: HyperCloudRequestHandler) => void; onHTTPError: (handler: HyperCloudRequestErrorHandler) => void; }>; /** * A protection "helmet" module that serves as a middleware or multiple middlewares * that you can use on your routes. * * You can customize the behavior with options */ helmet(options?: HelmetConfigOptions): void; /** * Increase productivity by spreading routes into multiple files. All * you need to do is to `export` the created server into the file that * you want to create routes on, then mount the routes on the `Router`. * * **Example**: * ```ts * // Main file: main.js * import hypercloud from '@nasriya/hypercloud'; * const server = hypercloud.Server(); * * const router = server.Router(); * * // Create routes on the main file * router.get('/', (request, response) => { * response.status(200).end(<h1>HyperCloud</h1>); * }) * * // Export the router * module.exports = server; * ``` * Now import the server on the API file: * ```ts * import server from './main.js'; * * // Define a router for the APIs. All routes defined on this * // router will be under the `api` sub-domain, unless * // explicitly specified. * const router = server.Router({subDomain: 'api'}); * * router.get('v1/users', (request, response) => { * response.status(200).json([{id: 'ahmad_id', name: 'Ahmad', role: 'Admin'}]) * }) * * router.post('v1/users', (request, response) => { * response.status(201).json(request.body) * }) * ``` * Each created `Router` has a reference to the `HyperCloudServer` * that created it. So routes are automatically mounted on the server. * @param {{ caseSensitive?: boolean, subDomain?: string}} [options] * @returns {Router} */ Router(options?: { caseSensitive?: boolean; subDomain?: string; }): Router; /** * Extend the functionality of the server * @param value * @example * import { Router } from '@nasriya/hypercloud'; * * const router = new Router(); * * router.get('/', (req, res, next) => { * console.log(req.__toJSON()); * next(); * }) * * server.extend(router); */ extend(value: Router): void; /** * Starts the server and makes it listen on a specified port or the default port. * @param options - The options object that can be used to configure the server. * @returns A promise that resolves when the server is listening. * @example * server.listen({ * host: 'localhost', * port: 8080, * onListen: (host, port) => console.log(`Server is listening on ${host}:${port}`), * backlog: 100, * exclusive: false * }); */ listen(options: ServerListeningConfigs): Promise<void>; /** * Stops the server from accepting new connections and keeps existing connections. * This method is asynchronous, the server is finally closed when all connections * are ended and the server emits a `close` event. The optional callback will be * called once the `close` event occurs. Unlike that event, it will be called with * an Error as its only argument if the server was not open when it was closed. * @param callback Called when the server is closed. */ close(callback: (err?: Error) => void): this; } export default HyperCloudServer;