fullstx 1.0.0

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.
Files changed (83) hide show
  1. package/README.md +86 -0
  2. package/bin/create.js +68 -0
  3. package/bin/generate.js +78 -0
  4. package/coverage/clover.xml +1356 -0
  5. package/coverage/coverage-final.json +30 -0
  6. package/coverage/lcov-report/base.css +224 -0
  7. package/coverage/lcov-report/block-navigation.js +87 -0
  8. package/coverage/lcov-report/favicon.png +0 -0
  9. package/coverage/lcov-report/fullstx/index.html +146 -0
  10. package/coverage/lcov-report/fullstx/index.js.html +379 -0
  11. package/coverage/lcov-report/fullstx/lib/application.js.html +1840 -0
  12. package/coverage/lcov-report/fullstx/lib/browser-sync.js.html +199 -0
  13. package/coverage/lcov-report/fullstx/lib/database.js.html +535 -0
  14. package/coverage/lcov-report/fullstx/lib/errors.js.html +154 -0
  15. package/coverage/lcov-report/fullstx/lib/features/hooks.js.html +151 -0
  16. package/coverage/lcov-report/fullstx/lib/features/index.html +176 -0
  17. package/coverage/lcov-report/fullstx/lib/features/jwt.js.html +214 -0
  18. package/coverage/lcov-report/fullstx/lib/features/plugins.js.html +172 -0
  19. package/coverage/lcov-report/fullstx/lib/features/routing.js.html +211 -0
  20. package/coverage/lcov-report/fullstx/lib/features/utils.js.html +1216 -0
  21. package/coverage/lcov-report/fullstx/lib/index.html +236 -0
  22. package/coverage/lcov-report/fullstx/lib/middlewares/apiKey.js.html +127 -0
  23. package/coverage/lcov-report/fullstx/lib/middlewares/authorize.js.html +115 -0
  24. package/coverage/lcov-report/fullstx/lib/middlewares/basicAuth.js.html +148 -0
  25. package/coverage/lcov-report/fullstx/lib/middlewares/csp.js.html +124 -0
  26. package/coverage/lcov-report/fullstx/lib/middlewares/csrf.js.html +178 -0
  27. package/coverage/lcov-report/fullstx/lib/middlewares/htmx.js.html +157 -0
  28. package/coverage/lcov-report/fullstx/lib/middlewares/i18n.js.html +175 -0
  29. package/coverage/lcov-report/fullstx/lib/middlewares/index.html +281 -0
  30. package/coverage/lcov-report/fullstx/lib/middlewares/rateLimit.js.html +169 -0
  31. package/coverage/lcov-report/fullstx/lib/middlewares/redisCache.js.html +154 -0
  32. package/coverage/lcov-report/fullstx/lib/middlewares/requirePermission.js.html +118 -0
  33. package/coverage/lcov-report/fullstx/lib/middlewares/routeLocalization.js.html +118 -0
  34. package/coverage/lcov-report/fullstx/lib/middlewares/session.js.html +133 -0
  35. package/coverage/lcov-report/fullstx/lib/middlewares.js.html +340 -0
  36. package/coverage/lcov-report/fullstx/lib/radix-router.js.html +418 -0
  37. package/coverage/lcov-report/fullstx/lib/request.js.html +328 -0
  38. package/coverage/lcov-report/fullstx/lib/response.js.html +610 -0
  39. package/coverage/lcov-report/fullstx/lib/websocket.js.html +166 -0
  40. package/coverage/lcov-report/fullstx/router.js.html +226 -0
  41. package/coverage/lcov-report/fullstx/server.js.html +664 -0
  42. package/coverage/lcov-report/index.html +161 -0
  43. package/coverage/lcov-report/prettify.css +1 -0
  44. package/coverage/lcov-report/prettify.js +2 -0
  45. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  46. package/coverage/lcov-report/sorter.js +210 -0
  47. package/coverage/lcov.info +2753 -0
  48. package/dist/index.js +238 -0
  49. package/dist/index.js.map +1 -0
  50. package/dist/index.mjs +238 -0
  51. package/dist/index.mjs.map +1 -0
  52. package/index.d.ts +115 -0
  53. package/index.js +98 -0
  54. package/lib/application.js +585 -0
  55. package/lib/browser-sync.js +38 -0
  56. package/lib/database.js +150 -0
  57. package/lib/errors.js +23 -0
  58. package/lib/features/hooks.js +22 -0
  59. package/lib/features/jwt.js +43 -0
  60. package/lib/features/plugins.js +29 -0
  61. package/lib/features/routing.js +42 -0
  62. package/lib/features/utils.js +377 -0
  63. package/lib/middlewares/apiKey.js +14 -0
  64. package/lib/middlewares/authorize.js +10 -0
  65. package/lib/middlewares/basicAuth.js +21 -0
  66. package/lib/middlewares/csp.js +13 -0
  67. package/lib/middlewares/csrf.js +31 -0
  68. package/lib/middlewares/htmx.js +24 -0
  69. package/lib/middlewares/i18n.js +30 -0
  70. package/lib/middlewares/rateLimit.js +28 -0
  71. package/lib/middlewares/redisCache.js +23 -0
  72. package/lib/middlewares/requirePermission.js +11 -0
  73. package/lib/middlewares/routeLocalization.js +11 -0
  74. package/lib/middlewares/session.js +16 -0
  75. package/lib/middlewares.js +85 -0
  76. package/lib/radix-router.js +111 -0
  77. package/lib/request.js +81 -0
  78. package/lib/response.js +175 -0
  79. package/lib/websocket.js +27 -0
  80. package/package.json +61 -0
  81. package/router.js +47 -0
  82. package/server.js +193 -0
  83. package/session.js +109 -0
package/index.d.ts ADDED
@@ -0,0 +1,115 @@
1
+ // Type definitions for fullstx
2
+ // Project: https://github.com/terzogenito/fullstx
3
+
4
+ /// <reference types="node" />
5
+
6
+ import { Server } from "http";
7
+
8
+ export interface FullstxRequest<Body = any, Query = any, Params = any> {
9
+ body: Body;
10
+ query: Query;
11
+ params: Params;
12
+ cookies: Record<string, string>;
13
+ url: string;
14
+ method: string;
15
+ headers: Record<string, string>;
16
+ }
17
+
18
+ export type Middleware<B = any, Q = any, P = any> = (req: FullstxRequest<B, Q, P>, res: any, next: (err?: any) => void) => void;
19
+ export type RouteHandler<B = any, Q = any, P = any> = (req: FullstxRequest<B, Q, P>, res: any) => void | Promise<void>;
20
+
21
+ export interface RouteSchema {
22
+ body?: any;
23
+ query?: any;
24
+ params?: any;
25
+ response?: any;
26
+ }
27
+
28
+ export interface RouteOptions {
29
+ schema?: RouteSchema;
30
+ [key: string]: any;
31
+ }
32
+
33
+ export interface FullstxOptions {
34
+ logger?: boolean;
35
+ cors?: boolean | object;
36
+ helmet?: boolean;
37
+ xss?: boolean;
38
+ compression?: boolean | object;
39
+ session?: boolean | object;
40
+ htmx?: boolean;
41
+ staticDir?: string;
42
+ liveReload?: boolean;
43
+ port?: number;
44
+ jwtSecret?: string;
45
+ healthCheck?: boolean;
46
+ internalPort?: number;
47
+ internalPortOffset?: number;
48
+ db?: any;
49
+ ws?: any;
50
+ sqlite?: any;
51
+ sentry?: any;
52
+ }
53
+
54
+ export class FullstxApplication {
55
+ /**
56
+ * The HTTP Server instance.
57
+ */
58
+ server: Server;
59
+
60
+ /**
61
+ * Database instance (if configured).
62
+ */
63
+ db: any;
64
+
65
+ // Routing methods
66
+ get<B = any, Q = any, P = any>(path: string, options: RouteOptions, ...handlers: (Middleware<B, Q, P> | RouteHandler<B, Q, P>)[]): this;
67
+ get<B = any, Q = any, P = any>(path: string, ...handlers: (Middleware<B, Q, P> | RouteHandler<B, Q, P>)[]): this;
68
+ post<B = any, Q = any, P = any>(path: string, options: RouteOptions, ...handlers: (Middleware<B, Q, P> | RouteHandler<B, Q, P>)[]): this;
69
+ post<B = any, Q = any, P = any>(path: string, ...handlers: (Middleware<B, Q, P> | RouteHandler<B, Q, P>)[]): this;
70
+ put<B = any, Q = any, P = any>(path: string, options: RouteOptions, ...handlers: (Middleware<B, Q, P> | RouteHandler<B, Q, P>)[]): this;
71
+ put<B = any, Q = any, P = any>(path: string, ...handlers: (Middleware<B, Q, P> | RouteHandler<B, Q, P>)[]): this;
72
+ patch<B = any, Q = any, P = any>(path: string, options: RouteOptions, ...handlers: (Middleware<B, Q, P> | RouteHandler<B, Q, P>)[]): this;
73
+ patch<B = any, Q = any, P = any>(path: string, ...handlers: (Middleware<B, Q, P> | RouteHandler<B, Q, P>)[]): this;
74
+ delete<B = any, Q = any, P = any>(path: string, options: RouteOptions, ...handlers: (Middleware<B, Q, P> | RouteHandler<B, Q, P>)[]): this;
75
+ delete<B = any, Q = any, P = any>(path: string, ...handlers: (Middleware<B, Q, P> | RouteHandler<B, Q, P>)[]): this;
76
+ use(...middlewares: Middleware[]): this;
77
+
78
+ // Lazy loaded middlewares
79
+ apiKey(key: string): Middleware;
80
+ basicAuth(credentials: Record<string, string>): Middleware;
81
+ authorize(roles: string[]): Middleware;
82
+ requirePermission(permission: string): Middleware;
83
+ rateLimit(options?: { windowMs?: number; max?: number; message?: string }): Middleware;
84
+ session(options?: { secret?: string; cookieName?: string; maxAge?: number }): Middleware;
85
+ csrf(): Middleware;
86
+ i18n(config?: { defaultLocale?: string; locales?: Record<string, any> }): Middleware;
87
+ redisCache(redisClient: any, options?: { prefix?: string; expire?: number }): Middleware;
88
+ csp(options?: Record<string, string | string[]>): Middleware;
89
+ htmx(): Middleware;
90
+ routeLocalization(supportedLocales?: string[]): Middleware;
91
+
92
+ // Helpers
93
+ sanitize(html: string, options?: any): string;
94
+ metaTags(tags?: Record<string, string>): string;
95
+ wsBroadcast(data: any): void;
96
+ factory(count: number, generatorFn: (faker: any, i: number) => any): any[];
97
+
98
+ /**
99
+ * Registers a plugin.
100
+ */
101
+ registerPlugin(plugin: Function | { register: Function }, options?: any): this;
102
+
103
+ /**
104
+ * Starts the server.
105
+ */
106
+ listen(port: number, callback?: () => void): Server;
107
+ }
108
+
109
+ /**
110
+ * Initializes the Fullstx Framework.
111
+ * @param options Framework configuration options.
112
+ */
113
+ export declare function Framework(options?: FullstxOptions): FullstxApplication;
114
+
115
+ export default Framework;
package/index.js ADDED
@@ -0,0 +1,98 @@
1
+ const util = require('util');
2
+ Object.defineProperty(util, '_extend', {
3
+ value: Object.assign,
4
+ writable: true,
5
+ enumerable: true,
6
+ configurable: true
7
+ });
8
+
9
+ require('dotenv').config({ quiet: true });
10
+
11
+ const createServer = require('./server');
12
+ const router = require('./router');
13
+ const setupDatabase = require('./lib/database');
14
+ const setupMiddlewares = require('./lib/middlewares');
15
+ const setupWebsocket = require('./lib/websocket');
16
+ const FullstxApplication = require('./lib/application');
17
+
18
+ let faker;
19
+ try {
20
+ faker = require('@faker-js/faker').faker;
21
+ } catch {}
22
+
23
+ let sanitizeHtml;
24
+ try {
25
+ sanitizeHtml = require('sanitize-html');
26
+ } catch {}
27
+
28
+ let Sentry;
29
+ try {
30
+ Sentry = require('@sentry/node');
31
+ } catch {}
32
+
33
+ function Framework(options = {}) {
34
+ const defaults = {
35
+ logger: true,
36
+ cors: true,
37
+ helmet: true,
38
+ xss: true,
39
+ compression: true,
40
+ session: false,
41
+ htmx: true,
42
+ staticDir: 'public',
43
+ liveReload: true,
44
+ port: 3000,
45
+ jwtSecret: process.env.JWT_SECRET || 'supersecret',
46
+ healthCheck: true,
47
+ internalPort: undefined,
48
+ db: null,
49
+ ws: null,
50
+ sqlite: null,
51
+ sentry: null
52
+ };
53
+
54
+ const opts = Object.assign({ ...defaults }, options);
55
+
56
+ if (opts.sentry && Sentry) {
57
+ Sentry.init(opts.sentry);
58
+ global.sentryCapture = Sentry.captureException;
59
+ console.log('[fullstx] Sentry Error Tracking enabled.');
60
+ }
61
+
62
+ const ws = setupWebsocket(opts, {});
63
+ const db = setupDatabase(opts, router);
64
+
65
+ setupMiddlewares(opts, router, sanitizeHtml);
66
+
67
+ const server = createServer(router, router.middlewares, {
68
+ staticDir: opts.staticDir
69
+ });
70
+
71
+ const app = new FullstxApplication(
72
+ opts,
73
+ router,
74
+ db,
75
+ ws.wsRedisPub,
76
+ ws.wsRedisSub,
77
+ null,
78
+ Sentry,
79
+ faker,
80
+ sanitizeHtml,
81
+ server
82
+ );
83
+
84
+ if (ws.wsRedisSub) {
85
+ const channel = opts.ws.pubSubChannel || 'fullstx_ws_pubsub';
86
+ ws.wsRedisSub.subscribe(channel, (message) => {
87
+ if (app.wss) {
88
+ app.wss.clients.forEach((client) => {
89
+ if (client.readyState === 1) client.send(message);
90
+ });
91
+ }
92
+ });
93
+ }
94
+
95
+ return app;
96
+ }
97
+
98
+ module.exports = Framework;