zero-http 0.2.4 → 0.3.1

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 (89) hide show
  1. package/README.md +1250 -283
  2. package/documentation/config/db.js +25 -0
  3. package/documentation/config/middleware.js +44 -0
  4. package/documentation/config/tls.js +12 -0
  5. package/documentation/controllers/cookies.js +34 -0
  6. package/documentation/controllers/tasks.js +108 -0
  7. package/documentation/full-server.js +28 -177
  8. package/documentation/models/Task.js +21 -0
  9. package/documentation/public/data/api.json +404 -24
  10. package/documentation/public/data/docs.json +1139 -0
  11. package/documentation/public/data/examples.json +80 -2
  12. package/documentation/public/data/options.json +23 -8
  13. package/documentation/public/index.html +138 -99
  14. package/documentation/public/scripts/app.js +1 -3
  15. package/documentation/public/scripts/custom-select.js +189 -0
  16. package/documentation/public/scripts/data-sections.js +233 -250
  17. package/documentation/public/scripts/playground.js +270 -0
  18. package/documentation/public/scripts/ui.js +4 -3
  19. package/documentation/public/styles.css +56 -5
  20. package/documentation/public/vendor/icons/compress.svg +17 -17
  21. package/documentation/public/vendor/icons/database.svg +21 -0
  22. package/documentation/public/vendor/icons/env.svg +21 -0
  23. package/documentation/public/vendor/icons/fetch.svg +11 -14
  24. package/documentation/public/vendor/icons/security.svg +15 -0
  25. package/documentation/public/vendor/icons/sse.svg +12 -13
  26. package/documentation/public/vendor/icons/static.svg +12 -26
  27. package/documentation/public/vendor/icons/stream.svg +7 -13
  28. package/documentation/public/vendor/icons/validate.svg +17 -0
  29. package/documentation/routes/api.js +41 -0
  30. package/documentation/routes/core.js +20 -0
  31. package/documentation/routes/playground.js +29 -0
  32. package/documentation/routes/realtime.js +49 -0
  33. package/documentation/routes/uploads.js +71 -0
  34. package/index.js +62 -1
  35. package/lib/app.js +200 -8
  36. package/lib/body/json.js +28 -5
  37. package/lib/body/multipart.js +29 -1
  38. package/lib/body/raw.js +1 -1
  39. package/lib/body/sendError.js +1 -0
  40. package/lib/body/text.js +1 -1
  41. package/lib/body/typeMatch.js +6 -2
  42. package/lib/body/urlencoded.js +5 -2
  43. package/lib/debug.js +345 -0
  44. package/lib/env/index.js +440 -0
  45. package/lib/errors.js +231 -0
  46. package/lib/http/request.js +219 -1
  47. package/lib/http/response.js +410 -6
  48. package/lib/middleware/compress.js +39 -6
  49. package/lib/middleware/cookieParser.js +237 -0
  50. package/lib/middleware/cors.js +13 -2
  51. package/lib/middleware/csrf.js +135 -0
  52. package/lib/middleware/errorHandler.js +90 -0
  53. package/lib/middleware/helmet.js +176 -0
  54. package/lib/middleware/index.js +7 -2
  55. package/lib/middleware/rateLimit.js +12 -1
  56. package/lib/middleware/requestId.js +54 -0
  57. package/lib/middleware/static.js +95 -11
  58. package/lib/middleware/timeout.js +72 -0
  59. package/lib/middleware/validator.js +257 -0
  60. package/lib/orm/adapters/json.js +215 -0
  61. package/lib/orm/adapters/memory.js +383 -0
  62. package/lib/orm/adapters/mongo.js +444 -0
  63. package/lib/orm/adapters/mysql.js +272 -0
  64. package/lib/orm/adapters/postgres.js +394 -0
  65. package/lib/orm/adapters/sql-base.js +142 -0
  66. package/lib/orm/adapters/sqlite.js +311 -0
  67. package/lib/orm/index.js +276 -0
  68. package/lib/orm/model.js +895 -0
  69. package/lib/orm/query.js +807 -0
  70. package/lib/orm/schema.js +172 -0
  71. package/lib/router/index.js +136 -47
  72. package/lib/sse/stream.js +15 -3
  73. package/lib/ws/connection.js +19 -3
  74. package/lib/ws/handshake.js +3 -0
  75. package/lib/ws/index.js +3 -1
  76. package/lib/ws/room.js +222 -0
  77. package/package.json +15 -5
  78. package/types/app.d.ts +120 -0
  79. package/types/env.d.ts +80 -0
  80. package/types/errors.d.ts +147 -0
  81. package/types/fetch.d.ts +43 -0
  82. package/types/index.d.ts +135 -0
  83. package/types/middleware.d.ts +292 -0
  84. package/types/orm.d.ts +610 -0
  85. package/types/request.d.ts +99 -0
  86. package/types/response.d.ts +142 -0
  87. package/types/router.d.ts +78 -0
  88. package/types/sse.d.ts +78 -0
  89. package/types/websocket.d.ts +119 -0
@@ -0,0 +1,99 @@
1
+ import { IncomingMessage, IncomingHttpHeaders } from 'http';
2
+ import { App } from './app';
3
+
4
+ export interface RangeResult {
5
+ type: string;
6
+ ranges: Array<{ start: number; end: number }>;
7
+ }
8
+
9
+ export interface Request {
10
+ /** Original Node request. */
11
+ raw: IncomingMessage;
12
+ /** HTTP method (e.g. 'GET'). */
13
+ method: string;
14
+ /** Full request URL including query string. */
15
+ url: string;
16
+ /** URL path without query string. */
17
+ path: string;
18
+ /** Lower-cased request headers. */
19
+ headers: IncomingHttpHeaders;
20
+ /** Parsed query-string key/value pairs. */
21
+ query: Record<string, string>;
22
+ /** Route parameters populated by the router. */
23
+ params: Record<string, string>;
24
+ /** Request body (set by body-parsing middleware). */
25
+ body: any;
26
+ /** Remote IP address. */
27
+ ip: string | null;
28
+ /** `true` when the connection is over TLS. */
29
+ secure: boolean;
30
+ /** Protocol string — `'https'` or `'http'`. */
31
+ protocol: 'http' | 'https';
32
+ /** Parsed cookies (populated by cookieParser middleware). */
33
+ cookies: Record<string, string>;
34
+ /** Verified signed cookies (populated by cookieParser middleware). */
35
+ signedCookies?: Record<string, string>;
36
+ /** Request-scoped locals store. */
37
+ locals: Record<string, any>;
38
+ /** Request ID (populated by requestId middleware). */
39
+ id?: string;
40
+ /** Whether the request timed out (populated by timeout middleware). */
41
+ timedOut?: boolean;
42
+ /** The original URL as received — never rewritten by middleware. */
43
+ originalUrl: string;
44
+ /** The URL path on which the current router was mounted. */
45
+ baseUrl: string;
46
+ /** Reference to the parent App instance. */
47
+ app: App | null;
48
+ /** CSRF token (populated by csrf middleware). */
49
+ csrfToken?: string;
50
+ /** First signing secret (populated by cookieParser middleware). */
51
+ secret?: string;
52
+ /** All signing secrets (populated by cookieParser middleware). */
53
+ secrets?: string[];
54
+
55
+ /**
56
+ * Get a specific request header (case-insensitive).
57
+ */
58
+ get(name: string): string | undefined;
59
+
60
+ /**
61
+ * Check if the request Content-Type matches the given type.
62
+ */
63
+ is(type: string): boolean;
64
+
65
+ /**
66
+ * Get the hostname from the Host header.
67
+ */
68
+ readonly hostname: string;
69
+
70
+ /**
71
+ * Get the subdomains as an array.
72
+ */
73
+ subdomains(offset?: number): string[];
74
+
75
+ /**
76
+ * Content negotiation — check which types the client accepts.
77
+ */
78
+ accepts(...types: string[]): string | false;
79
+
80
+ /**
81
+ * Check if the request is "fresh" (client cache valid).
82
+ */
83
+ readonly fresh: boolean;
84
+
85
+ /**
86
+ * Inverse of `fresh`.
87
+ */
88
+ readonly stale: boolean;
89
+
90
+ /**
91
+ * Check whether this request was made with XMLHttpRequest.
92
+ */
93
+ readonly xhr: boolean;
94
+
95
+ /**
96
+ * Parse the Range header.
97
+ */
98
+ range(size: number): RangeResult | -1 | -2;
99
+ }
@@ -0,0 +1,142 @@
1
+ import { ServerResponse } from 'http';
2
+ import { App } from './app';
3
+ import { SSEStream, SSEOptions } from './sse';
4
+
5
+ export interface SendFileOptions {
6
+ headers?: Record<string, string>;
7
+ root?: string;
8
+ }
9
+
10
+ export interface CookieOptions {
11
+ domain?: string;
12
+ path?: string;
13
+ expires?: Date | number;
14
+ maxAge?: number;
15
+ httpOnly?: boolean;
16
+ secure?: boolean;
17
+ sameSite?: 'Strict' | 'Lax' | 'None';
18
+ /** Auto-sign the cookie value using req.secret. */
19
+ signed?: boolean;
20
+ /** Cookie priority: 'Low', 'Medium', or 'High'. */
21
+ priority?: 'Low' | 'Medium' | 'High';
22
+ /** Partitioned attribute (CHIPS). Requires secure: true. */
23
+ partitioned?: boolean;
24
+ }
25
+
26
+ export interface Response {
27
+ /** Original Node response. */
28
+ raw: ServerResponse;
29
+ /** Request-scoped locals store. */
30
+ locals: Record<string, any>;
31
+ /** Reference to the parent App instance. */
32
+ app: App | null;
33
+
34
+ /**
35
+ * Set HTTP status code. Chainable.
36
+ */
37
+ status(code: number): Response;
38
+
39
+ /**
40
+ * Set a response header. Chainable.
41
+ */
42
+ set(name: string, value: string): Response;
43
+
44
+ /**
45
+ * Get a previously-set response header.
46
+ */
47
+ get(name: string): string | undefined;
48
+
49
+ /**
50
+ * Append a value to a header.
51
+ */
52
+ append(name: string, value: string): Response;
53
+
54
+ /**
55
+ * Add a field to the Vary response header.
56
+ */
57
+ vary(field: string): Response;
58
+
59
+ /**
60
+ * Set the Content-Type header. Chainable.
61
+ */
62
+ type(ct: string): Response;
63
+
64
+ /**
65
+ * Whether headers have been sent.
66
+ */
67
+ readonly headersSent: boolean;
68
+
69
+ /**
70
+ * Send a response body and finalize.
71
+ */
72
+ send(body?: string | Buffer | object | null): void;
73
+
74
+ /**
75
+ * Send a JSON response.
76
+ */
77
+ json(obj: any): void;
78
+
79
+ /**
80
+ * Send a plain-text response.
81
+ */
82
+ text(str: string): void;
83
+
84
+ /**
85
+ * Send an HTML response.
86
+ */
87
+ html(str: string): void;
88
+
89
+ /**
90
+ * Send only the status code with reason phrase.
91
+ */
92
+ sendStatus(code: number): void;
93
+
94
+ /**
95
+ * Send a file as the response.
96
+ */
97
+ sendFile(filePath: string, opts?: SendFileOptions, cb?: (err: Error | null) => void): void;
98
+ sendFile(filePath: string, cb?: (err: Error | null) => void): void;
99
+
100
+ /**
101
+ * Prompt a file download.
102
+ */
103
+ download(filePath: string, filename?: string, cb?: (err: Error | null) => void): void;
104
+ download(filePath: string, cb?: (err: Error | null) => void): void;
105
+
106
+ /**
107
+ * Set a cookie. Objects are auto-serialized as JSON cookies (j: prefix).
108
+ */
109
+ cookie(name: string, value: string | object, opts?: CookieOptions): Response;
110
+
111
+ /**
112
+ * Clear a cookie.
113
+ */
114
+ clearCookie(name: string, opts?: CookieOptions): Response;
115
+
116
+ /**
117
+ * Redirect to a URL.
118
+ */
119
+ redirect(url: string): void;
120
+ redirect(status: number, url: string): void;
121
+
122
+ /**
123
+ * Open a Server-Sent Events stream.
124
+ */
125
+ sse(opts?: SSEOptions): SSEStream;
126
+
127
+ /**
128
+ * Content-negotiated response based on Accept header.
129
+ * Keys are MIME types, values are handler functions.
130
+ */
131
+ format(types: Record<string, () => void> & { default?: () => void }): void;
132
+
133
+ /**
134
+ * Set the Link header from a map of rel → URL.
135
+ */
136
+ links(links: Record<string, string>): Response;
137
+
138
+ /**
139
+ * Set the Location response header.
140
+ */
141
+ location(url: string): Response;
142
+ }
@@ -0,0 +1,78 @@
1
+ import { Request } from './request';
2
+ import { Response } from './response';
3
+ import { MiddlewareFunction, NextFunction } from './middleware';
4
+
5
+ export interface RouteOptions {
6
+ /** When `true`, route matches only HTTPS; when `false`, only HTTP. */
7
+ secure?: boolean;
8
+ }
9
+
10
+ export type RouteHandler = (req: Request, res: Response, next?: NextFunction) => void | Promise<void>;
11
+
12
+ export interface RouteEntry {
13
+ method: string;
14
+ path: string;
15
+ regex: RegExp;
16
+ keys: string[];
17
+ handlers: RouteHandler[];
18
+ secure?: boolean;
19
+ }
20
+
21
+ export interface RouteInfo {
22
+ method: string;
23
+ path: string;
24
+ secure?: boolean;
25
+ maxPayload?: number;
26
+ pingInterval?: number;
27
+ }
28
+
29
+ export interface RouteChain {
30
+ get(...handlers: (RouteOptions | RouteHandler)[]): RouteChain;
31
+ post(...handlers: (RouteOptions | RouteHandler)[]): RouteChain;
32
+ put(...handlers: (RouteOptions | RouteHandler)[]): RouteChain;
33
+ delete(...handlers: (RouteOptions | RouteHandler)[]): RouteChain;
34
+ patch(...handlers: (RouteOptions | RouteHandler)[]): RouteChain;
35
+ options(...handlers: (RouteOptions | RouteHandler)[]): RouteChain;
36
+ head(...handlers: (RouteOptions | RouteHandler)[]): RouteChain;
37
+ all(...handlers: (RouteOptions | RouteHandler)[]): RouteChain;
38
+ }
39
+
40
+ export interface RouterInstance {
41
+ /** Registered routes. */
42
+ routes: RouteEntry[];
43
+
44
+ /**
45
+ * Register a route.
46
+ */
47
+ add(method: string, path: string, handlers: RouteHandler[], options?: RouteOptions): void;
48
+
49
+ /**
50
+ * Mount a child router under a path prefix.
51
+ */
52
+ use(prefix: string, router: RouterInstance): void;
53
+
54
+ /**
55
+ * Match and handle request.
56
+ */
57
+ handle(req: Request, res: Response): void;
58
+
59
+ /**
60
+ * Chainable route builder.
61
+ */
62
+ route(path: string): RouteChain;
63
+
64
+ /**
65
+ * Return a flat list of all registered routes.
66
+ */
67
+ inspect(prefix?: string): RouteInfo[];
68
+
69
+ // HTTP method shortcuts
70
+ get(path: string, ...handlers: (RouteOptions | RouteHandler)[]): RouterInstance;
71
+ post(path: string, ...handlers: (RouteOptions | RouteHandler)[]): RouterInstance;
72
+ put(path: string, ...handlers: (RouteOptions | RouteHandler)[]): RouterInstance;
73
+ delete(path: string, ...handlers: (RouteOptions | RouteHandler)[]): RouterInstance;
74
+ patch(path: string, ...handlers: (RouteOptions | RouteHandler)[]): RouterInstance;
75
+ options(path: string, ...handlers: (RouteOptions | RouteHandler)[]): RouterInstance;
76
+ head(path: string, ...handlers: (RouteOptions | RouteHandler)[]): RouterInstance;
77
+ all(path: string, ...handlers: (RouteOptions | RouteHandler)[]): RouterInstance;
78
+ }
package/types/sse.d.ts ADDED
@@ -0,0 +1,78 @@
1
+ export interface SSEOptions {
2
+ retry?: number;
3
+ headers?: Record<string, string>;
4
+ keepAlive?: number;
5
+ keepAliveComment?: string;
6
+ autoId?: boolean;
7
+ startId?: number;
8
+ pad?: number;
9
+ status?: number;
10
+ }
11
+
12
+ export interface SSEStream {
13
+ /** Whether the stream is still open. */
14
+ readonly connected: boolean;
15
+ /** The Last-Event-ID from client reconnection. */
16
+ lastEventId: string | null;
17
+ /** Total events sent. */
18
+ eventCount: number;
19
+ /** Total bytes written. */
20
+ bytesSent: number;
21
+ /** Timestamp when stream was opened. */
22
+ connectedAt: number;
23
+ /** Milliseconds since opened. */
24
+ readonly uptime: number;
25
+ /** Whether connection is over TLS. */
26
+ secure: boolean;
27
+ /** Arbitrary user-data store. */
28
+ data: Record<string, any>;
29
+
30
+ /**
31
+ * Send an unnamed data event.
32
+ */
33
+ send(data: string | object, id?: string | number): SSEStream;
34
+
35
+ /**
36
+ * Send a JSON event (alias for send).
37
+ */
38
+ sendJSON(obj: any, id?: string | number): SSEStream;
39
+
40
+ /**
41
+ * Send a named event.
42
+ */
43
+ event(eventName: string, data: string | object, id?: string | number): SSEStream;
44
+
45
+ /**
46
+ * Send a comment line.
47
+ */
48
+ comment(text: string): SSEStream;
49
+
50
+ /**
51
+ * Send or update the retry interval.
52
+ */
53
+ retry(ms: number): SSEStream;
54
+
55
+ /**
56
+ * Start/restart keep-alive timer.
57
+ */
58
+ keepAlive(intervalMs: number, comment?: string): SSEStream;
59
+
60
+ /**
61
+ * Flush buffered data.
62
+ */
63
+ flush(): SSEStream;
64
+
65
+ /**
66
+ * Close the SSE connection.
67
+ */
68
+ close(): void;
69
+
70
+ // Event emitter
71
+ on(event: 'close', fn: () => void): SSEStream;
72
+ on(event: 'error', fn: (err: Error) => void): SSEStream;
73
+ once(event: 'close', fn: () => void): SSEStream;
74
+ once(event: 'error', fn: (err: Error) => void): SSEStream;
75
+ off(event: string, fn: Function): SSEStream;
76
+ removeAllListeners(event?: string): SSEStream;
77
+ listenerCount(event: string): number;
78
+ }
@@ -0,0 +1,119 @@
1
+ import { IncomingMessage, IncomingHttpHeaders } from 'http';
2
+
3
+ export interface WebSocketOptions {
4
+ /** Maximum incoming frame size in bytes (default 1 MB). */
5
+ maxPayload?: number;
6
+ /** Auto-ping interval in ms. Set `0` to disable. */
7
+ pingInterval?: number;
8
+ /** Return false to reject the upgrade. */
9
+ verifyClient?: (req: IncomingMessage) => boolean;
10
+ }
11
+
12
+ export type WebSocketHandler = (ws: WebSocketConnection, req: IncomingMessage) => void;
13
+
14
+ export interface WebSocketConnection {
15
+ /** Unique connection identifier. */
16
+ id: string;
17
+ /** Current ready state (0-3). */
18
+ readyState: number;
19
+ /** Negotiated sub-protocol. */
20
+ protocol: string;
21
+ /** Requested extensions. */
22
+ extensions: string;
23
+ /** Request headers from the upgrade. */
24
+ headers: IncomingHttpHeaders;
25
+ /** Remote IP address. */
26
+ ip: string | null;
27
+ /** Parsed query params from the upgrade URL. */
28
+ query: Record<string, string>;
29
+ /** Full upgrade URL. */
30
+ url: string;
31
+ /** Whether connection is over TLS. */
32
+ secure: boolean;
33
+ /** Maximum incoming payload bytes. */
34
+ maxPayload: number;
35
+ /** Timestamp when connected. */
36
+ connectedAt: number;
37
+ /** Arbitrary user-data store. */
38
+ data: Record<string, any>;
39
+ /** Bytes waiting in send buffer. */
40
+ readonly bufferedAmount: number;
41
+ /** Milliseconds since connected. */
42
+ readonly uptime: number;
43
+
44
+ /**
45
+ * Send a text or binary message.
46
+ */
47
+ send(data: string | Buffer, opts?: { binary?: boolean; callback?: Function }): boolean;
48
+
49
+ /**
50
+ * Send a JSON-serialised message.
51
+ */
52
+ sendJSON(obj: any, cb?: Function): boolean;
53
+
54
+ /**
55
+ * Send a ping frame.
56
+ */
57
+ ping(payload?: string | Buffer, cb?: Function): boolean;
58
+
59
+ /**
60
+ * Send a pong frame.
61
+ */
62
+ pong(payload?: string | Buffer, cb?: Function): boolean;
63
+
64
+ /**
65
+ * Close the connection.
66
+ */
67
+ close(code?: number, reason?: string): void;
68
+
69
+ /**
70
+ * Forcefully destroy the socket.
71
+ */
72
+ terminate(): void;
73
+
74
+ // Event emitter
75
+ on(event: 'message', fn: (data: string | Buffer) => void): WebSocketConnection;
76
+ on(event: 'close', fn: (code: number, reason: string) => void): WebSocketConnection;
77
+ on(event: 'error', fn: (err: Error) => void): WebSocketConnection;
78
+ on(event: 'pong', fn: (payload: Buffer) => void): WebSocketConnection;
79
+ on(event: 'ping', fn: (payload: Buffer) => void): WebSocketConnection;
80
+ on(event: 'drain', fn: () => void): WebSocketConnection;
81
+ once(event: string, fn: Function): WebSocketConnection;
82
+ off(event: string, fn: Function): WebSocketConnection;
83
+ removeAllListeners(event?: string): WebSocketConnection;
84
+ listenerCount(event: string): number;
85
+ }
86
+
87
+ export interface WebSocketPool {
88
+ /** Total active connections. */
89
+ readonly size: number;
90
+ /** All active room names. */
91
+ readonly rooms: string[];
92
+ /** All active connections. */
93
+ readonly clients: WebSocketConnection[];
94
+
95
+ /** Add a connection to the pool. */
96
+ add(ws: WebSocketConnection): WebSocketPool;
97
+ /** Remove a connection from the pool. */
98
+ remove(ws: WebSocketConnection): WebSocketPool;
99
+ /** Join a connection to a room. */
100
+ join(ws: WebSocketConnection, room: string): WebSocketPool;
101
+ /** Remove a connection from a room. */
102
+ leave(ws: WebSocketConnection, room: string): WebSocketPool;
103
+ /** Get all rooms a connection belongs to. */
104
+ roomsOf(ws: WebSocketConnection): string[];
105
+ /** Broadcast to ALL connections. */
106
+ broadcast(data: string | Buffer, exclude?: WebSocketConnection): void;
107
+ /** Broadcast JSON to ALL connections. */
108
+ broadcastJSON(obj: any, exclude?: WebSocketConnection): void;
109
+ /** Send to all connections in a room. */
110
+ toRoom(room: string, data: string | Buffer, exclude?: WebSocketConnection): void;
111
+ /** Send JSON to all connections in a room. */
112
+ toRoomJSON(room: string, obj: any, exclude?: WebSocketConnection): void;
113
+ /** Get all connections in a room. */
114
+ in(room: string): WebSocketConnection[];
115
+ /** Number of connections in a room. */
116
+ roomSize(room: string): number;
117
+ /** Close all connections. */
118
+ closeAll(code?: number, reason?: string): void;
119
+ }