qhttpx 1.8.11 → 1.9.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 (206) hide show
  1. package/package.json +12 -3
  2. package/prebuilds/darwin-arm64/qhttpx.node +0 -0
  3. package/prebuilds/linux-x64/qhttpx.node +0 -0
  4. package/prebuilds/win32-x64/qhttpx.node +0 -0
  5. package/src/native/index.ts +104 -24
  6. package/src/native/picohttpparser.h +5 -0
  7. package/src/native/server.cc +2 -0
  8. package/dist/examples/api-server.d.ts +0 -1
  9. package/dist/examples/api-server.js +0 -77
  10. package/dist/examples/basic.d.ts +0 -1
  11. package/dist/examples/basic.js +0 -10
  12. package/dist/examples/compression.d.ts +0 -1
  13. package/dist/examples/compression.js +0 -17
  14. package/dist/examples/cors.d.ts +0 -1
  15. package/dist/examples/cors.js +0 -19
  16. package/dist/examples/errors.d.ts +0 -1
  17. package/dist/examples/errors.js +0 -25
  18. package/dist/examples/file-upload.d.ts +0 -1
  19. package/dist/examples/file-upload.js +0 -24
  20. package/dist/examples/fusion.d.ts +0 -1
  21. package/dist/examples/fusion.js +0 -21
  22. package/dist/examples/rate-limiting.d.ts +0 -1
  23. package/dist/examples/rate-limiting.js +0 -17
  24. package/dist/examples/validation.d.ts +0 -1
  25. package/dist/examples/validation.js +0 -23
  26. package/dist/examples/websockets.d.ts +0 -1
  27. package/dist/examples/websockets.js +0 -20
  28. package/dist/package.json +0 -101
  29. package/dist/src/benchmarks/quantam-users.d.ts +0 -1
  30. package/dist/src/benchmarks/quantam-users.js +0 -56
  31. package/dist/src/benchmarks/simple-json.d.ts +0 -1
  32. package/dist/src/benchmarks/simple-json.js +0 -60
  33. package/dist/src/benchmarks/ultra-mode.d.ts +0 -1
  34. package/dist/src/benchmarks/ultra-mode.js +0 -94
  35. package/dist/src/cli/index.d.ts +0 -2
  36. package/dist/src/cli/index.js +0 -222
  37. package/dist/src/client/index.d.ts +0 -17
  38. package/dist/src/client/index.js +0 -72
  39. package/dist/src/core/batch.d.ts +0 -24
  40. package/dist/src/core/batch.js +0 -97
  41. package/dist/src/core/body-parser.d.ts +0 -15
  42. package/dist/src/core/body-parser.js +0 -121
  43. package/dist/src/core/buffer-pool.d.ts +0 -41
  44. package/dist/src/core/buffer-pool.js +0 -70
  45. package/dist/src/core/config.d.ts +0 -7
  46. package/dist/src/core/config.js +0 -50
  47. package/dist/src/core/errors.d.ts +0 -34
  48. package/dist/src/core/errors.js +0 -70
  49. package/dist/src/core/fusion.d.ts +0 -14
  50. package/dist/src/core/fusion.js +0 -183
  51. package/dist/src/core/logger.d.ts +0 -22
  52. package/dist/src/core/logger.js +0 -49
  53. package/dist/src/core/metrics.d.ts +0 -45
  54. package/dist/src/core/metrics.js +0 -111
  55. package/dist/src/core/native-adapter.d.ts +0 -11
  56. package/dist/src/core/native-adapter.js +0 -211
  57. package/dist/src/core/resources.d.ts +0 -9
  58. package/dist/src/core/resources.js +0 -25
  59. package/dist/src/core/scheduler.d.ts +0 -34
  60. package/dist/src/core/scheduler.js +0 -85
  61. package/dist/src/core/scope.d.ts +0 -26
  62. package/dist/src/core/scope.js +0 -68
  63. package/dist/src/core/serializer.d.ts +0 -10
  64. package/dist/src/core/serializer.js +0 -44
  65. package/dist/src/core/server.d.ts +0 -138
  66. package/dist/src/core/server.js +0 -1082
  67. package/dist/src/core/stream.d.ts +0 -15
  68. package/dist/src/core/stream.js +0 -71
  69. package/dist/src/core/tasks.d.ts +0 -29
  70. package/dist/src/core/tasks.js +0 -87
  71. package/dist/src/core/types.d.ts +0 -173
  72. package/dist/src/core/types.js +0 -19
  73. package/dist/src/core/websocket.d.ts +0 -25
  74. package/dist/src/core/websocket.js +0 -86
  75. package/dist/src/core/worker-queue.d.ts +0 -41
  76. package/dist/src/core/worker-queue.js +0 -73
  77. package/dist/src/database/adapters/memory.d.ts +0 -21
  78. package/dist/src/database/adapters/memory.js +0 -90
  79. package/dist/src/database/adapters/mongo.d.ts +0 -11
  80. package/dist/src/database/adapters/mongo.js +0 -141
  81. package/dist/src/database/adapters/postgres.d.ts +0 -10
  82. package/dist/src/database/adapters/postgres.js +0 -111
  83. package/dist/src/database/adapters/sqlite.d.ts +0 -10
  84. package/dist/src/database/adapters/sqlite.js +0 -42
  85. package/dist/src/database/coalescer.d.ts +0 -14
  86. package/dist/src/database/coalescer.js +0 -134
  87. package/dist/src/database/manager.d.ts +0 -35
  88. package/dist/src/database/manager.js +0 -87
  89. package/dist/src/database/types.d.ts +0 -20
  90. package/dist/src/database/types.js +0 -2
  91. package/dist/src/index.d.ts +0 -50
  92. package/dist/src/index.js +0 -91
  93. package/dist/src/middleware/compression.d.ts +0 -2
  94. package/dist/src/middleware/compression.js +0 -133
  95. package/dist/src/middleware/cors.d.ts +0 -2
  96. package/dist/src/middleware/cors.js +0 -66
  97. package/dist/src/middleware/presets.d.ts +0 -16
  98. package/dist/src/middleware/presets.js +0 -52
  99. package/dist/src/middleware/rate-limit.d.ts +0 -14
  100. package/dist/src/middleware/rate-limit.js +0 -83
  101. package/dist/src/middleware/security.d.ts +0 -21
  102. package/dist/src/middleware/security.js +0 -69
  103. package/dist/src/middleware/static.d.ts +0 -11
  104. package/dist/src/middleware/static.js +0 -191
  105. package/dist/src/native/index.d.ts +0 -29
  106. package/dist/src/native/index.js +0 -64
  107. package/dist/src/openapi/generator.d.ts +0 -19
  108. package/dist/src/openapi/generator.js +0 -149
  109. package/dist/src/router/radix-router.d.ts +0 -18
  110. package/dist/src/router/radix-router.js +0 -89
  111. package/dist/src/router/radix-tree.d.ts +0 -18
  112. package/dist/src/router/radix-tree.js +0 -131
  113. package/dist/src/router/router.d.ts +0 -34
  114. package/dist/src/router/router.js +0 -186
  115. package/dist/src/testing/index.d.ts +0 -25
  116. package/dist/src/testing/index.js +0 -84
  117. package/dist/src/utils/cookies.d.ts +0 -3
  118. package/dist/src/utils/cookies.js +0 -59
  119. package/dist/src/utils/logger.d.ts +0 -12
  120. package/dist/src/utils/logger.js +0 -45
  121. package/dist/src/utils/signals.d.ts +0 -6
  122. package/dist/src/utils/signals.js +0 -31
  123. package/dist/src/utils/sse.d.ts +0 -6
  124. package/dist/src/utils/sse.js +0 -32
  125. package/dist/src/validation/index.d.ts +0 -3
  126. package/dist/src/validation/index.js +0 -19
  127. package/dist/src/validation/simple.d.ts +0 -5
  128. package/dist/src/validation/simple.js +0 -102
  129. package/dist/src/validation/types.d.ts +0 -32
  130. package/dist/src/validation/types.js +0 -12
  131. package/dist/src/validation/zod.d.ts +0 -4
  132. package/dist/src/validation/zod.js +0 -18
  133. package/dist/src/views/index.d.ts +0 -1
  134. package/dist/src/views/index.js +0 -17
  135. package/dist/src/views/types.d.ts +0 -3
  136. package/dist/src/views/types.js +0 -2
  137. package/dist/tests/adapters.test.d.ts +0 -1
  138. package/dist/tests/adapters.test.js +0 -106
  139. package/dist/tests/batch.test.d.ts +0 -1
  140. package/dist/tests/batch.test.js +0 -117
  141. package/dist/tests/body-parser.test.d.ts +0 -1
  142. package/dist/tests/body-parser.test.js +0 -52
  143. package/dist/tests/compression-sse.test.d.ts +0 -1
  144. package/dist/tests/compression-sse.test.js +0 -87
  145. package/dist/tests/cookies.test.d.ts +0 -1
  146. package/dist/tests/cookies.test.js +0 -63
  147. package/dist/tests/cors.test.d.ts +0 -1
  148. package/dist/tests/cors.test.js +0 -55
  149. package/dist/tests/database.test.d.ts +0 -1
  150. package/dist/tests/database.test.js +0 -80
  151. package/dist/tests/dx.test.d.ts +0 -1
  152. package/dist/tests/dx.test.js +0 -114
  153. package/dist/tests/ecosystem.test.d.ts +0 -1
  154. package/dist/tests/ecosystem.test.js +0 -133
  155. package/dist/tests/features.test.d.ts +0 -1
  156. package/dist/tests/features.test.js +0 -47
  157. package/dist/tests/fusion.test.d.ts +0 -1
  158. package/dist/tests/fusion.test.js +0 -92
  159. package/dist/tests/http-basic.test.d.ts +0 -1
  160. package/dist/tests/http-basic.test.js +0 -124
  161. package/dist/tests/logger.test.d.ts +0 -1
  162. package/dist/tests/logger.test.js +0 -33
  163. package/dist/tests/middleware.test.d.ts +0 -1
  164. package/dist/tests/middleware.test.js +0 -109
  165. package/dist/tests/native-adapter.test.d.ts +0 -1
  166. package/dist/tests/native-adapter.test.js +0 -71
  167. package/dist/tests/observability.test.d.ts +0 -1
  168. package/dist/tests/observability.test.js +0 -59
  169. package/dist/tests/openapi.test.d.ts +0 -1
  170. package/dist/tests/openapi.test.js +0 -64
  171. package/dist/tests/plugin.test.d.ts +0 -1
  172. package/dist/tests/plugin.test.js +0 -65
  173. package/dist/tests/plugins.test.d.ts +0 -1
  174. package/dist/tests/plugins.test.js +0 -71
  175. package/dist/tests/rate-limit.test.d.ts +0 -1
  176. package/dist/tests/rate-limit.test.js +0 -77
  177. package/dist/tests/resources.test.d.ts +0 -1
  178. package/dist/tests/resources.test.js +0 -47
  179. package/dist/tests/scheduler.test.d.ts +0 -1
  180. package/dist/tests/scheduler.test.js +0 -46
  181. package/dist/tests/schema-routes.test.d.ts +0 -1
  182. package/dist/tests/schema-routes.test.js +0 -77
  183. package/dist/tests/security.test.d.ts +0 -1
  184. package/dist/tests/security.test.js +0 -83
  185. package/dist/tests/server-db.test.d.ts +0 -1
  186. package/dist/tests/server-db.test.js +0 -72
  187. package/dist/tests/smoke.test.d.ts +0 -1
  188. package/dist/tests/smoke.test.js +0 -10
  189. package/dist/tests/sqlite-fusion.test.d.ts +0 -1
  190. package/dist/tests/sqlite-fusion.test.js +0 -92
  191. package/dist/tests/static.test.d.ts +0 -1
  192. package/dist/tests/static.test.js +0 -102
  193. package/dist/tests/stream.test.d.ts +0 -1
  194. package/dist/tests/stream.test.js +0 -44
  195. package/dist/tests/task-metrics.test.d.ts +0 -1
  196. package/dist/tests/task-metrics.test.js +0 -53
  197. package/dist/tests/tasks.test.d.ts +0 -1
  198. package/dist/tests/tasks.test.js +0 -62
  199. package/dist/tests/testing.test.d.ts +0 -1
  200. package/dist/tests/testing.test.js +0 -47
  201. package/dist/tests/validation.test.d.ts +0 -1
  202. package/dist/tests/validation.test.js +0 -107
  203. package/dist/tests/websocket.test.d.ts +0 -1
  204. package/dist/tests/websocket.test.js +0 -146
  205. package/dist/vitest.config.d.ts +0 -2
  206. package/dist/vitest.config.js +0 -9
@@ -1,121 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.BodyParser = void 0;
7
- const querystring_1 = require("querystring");
8
- const busboy_1 = __importDefault(require("busboy"));
9
- class BodyParser {
10
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
11
- static async parse(req, options = {}) {
12
- const method = (req.method || 'GET').toUpperCase();
13
- if (method === 'GET' || method === 'HEAD') {
14
- return { body: undefined };
15
- }
16
- const contentType = req.headers['content-type'] || '';
17
- if (contentType.includes('multipart/form-data')) {
18
- return this.parseMultipart(req, options);
19
- }
20
- const chunks = [];
21
- let totalBytes = 0;
22
- const maxBodyBytes = options.maxBodyBytes;
23
- for await (const chunk of req) {
24
- const bufferChunk = typeof chunk === 'string' ? Buffer.from(chunk) : chunk;
25
- chunks.push(bufferChunk);
26
- totalBytes += bufferChunk.length;
27
- if (maxBodyBytes !== undefined && totalBytes > maxBodyBytes) {
28
- throw new Error('QHTTPX_BODY_TOO_LARGE');
29
- }
30
- }
31
- if (chunks.length === 0) {
32
- return { body: undefined };
33
- }
34
- const buffer = Buffer.concat(chunks);
35
- if (typeof contentType === 'string' &&
36
- contentType.includes('application/json')) {
37
- const text = buffer.toString('utf8');
38
- try {
39
- return { body: JSON.parse(text) };
40
- }
41
- catch {
42
- throw new Error('QHTTPX_INVALID_JSON');
43
- }
44
- }
45
- if (typeof contentType === 'string' &&
46
- contentType.includes('application/x-www-form-urlencoded')) {
47
- const text = buffer.toString('utf8');
48
- return { body: (0, querystring_1.parse)(text) };
49
- }
50
- return { body: buffer };
51
- }
52
- static parseMultipart(req, options) {
53
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
54
- return new Promise((resolve, reject) => {
55
- let bb;
56
- try {
57
- bb = (0, busboy_1.default)({ headers: req.headers, limits: { fileSize: options.maxBodyBytes } });
58
- }
59
- catch (err) {
60
- return reject(err);
61
- }
62
- const body = {};
63
- const files = {};
64
- bb.on('field', (name, val) => {
65
- if (Object.prototype.hasOwnProperty.call(body, name)) {
66
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
67
- if (Array.isArray(body[name])) {
68
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
69
- body[name].push(val);
70
- }
71
- else {
72
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
73
- body[name] = [body[name], val];
74
- }
75
- }
76
- else {
77
- body[name] = val;
78
- }
79
- });
80
- bb.on('file', (name, file, info) => {
81
- const { filename, encoding, mimeType } = info;
82
- const chunks = [];
83
- file.on('data', (chunk) => {
84
- chunks.push(chunk);
85
- });
86
- file.on('end', () => {
87
- const buffer = Buffer.concat(chunks);
88
- const fileData = {
89
- filename,
90
- encoding,
91
- mimeType,
92
- data: buffer,
93
- size: buffer.length,
94
- };
95
- if (Object.prototype.hasOwnProperty.call(files, name)) {
96
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
97
- if (Array.isArray(files[name])) {
98
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
99
- files[name].push(fileData);
100
- }
101
- else {
102
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
103
- files[name] = [files[name], fileData];
104
- }
105
- }
106
- else {
107
- files[name] = fileData;
108
- }
109
- });
110
- });
111
- bb.on('close', () => {
112
- resolve({ body, files });
113
- });
114
- bb.on('error', (err) => {
115
- reject(err);
116
- });
117
- req.pipe(bb);
118
- });
119
- }
120
- }
121
- exports.BodyParser = BodyParser;
@@ -1,41 +0,0 @@
1
- /**
2
- * Buffer pool for response bodies (small, medium, large).
3
- * Reuses allocated buffers to reduce GC pressure.
4
- */
5
- export type BufferPoolConfig = {
6
- smallSize?: number;
7
- mediumSize?: number;
8
- largeSize?: number;
9
- smallPoolSize?: number;
10
- mediumPoolSize?: number;
11
- largePoolSize?: number;
12
- };
13
- export declare class BufferPool {
14
- private readonly smallSize;
15
- private readonly mediumSize;
16
- private readonly largeSize;
17
- private readonly smallBuffers;
18
- private readonly mediumBuffers;
19
- private readonly largeBuffers;
20
- private readonly smallPoolSize;
21
- private readonly mediumPoolSize;
22
- private readonly largePoolSize;
23
- constructor(config?: BufferPoolConfig);
24
- /**
25
- * Acquire a buffer suitable for the given size.
26
- * Returns a buffer from the appropriate pool.
27
- */
28
- acquire(size: number): Buffer;
29
- /**
30
- * Release a buffer back to the appropriate pool.
31
- */
32
- release(buffer: Buffer): void;
33
- /**
34
- * Get the number of available buffers in each pool.
35
- */
36
- getPoolStatus(): {
37
- small: number;
38
- medium: number;
39
- large: number;
40
- };
41
- }
@@ -1,70 +0,0 @@
1
- "use strict";
2
- /**
3
- * Buffer pool for response bodies (small, medium, large).
4
- * Reuses allocated buffers to reduce GC pressure.
5
- */
6
- Object.defineProperty(exports, "__esModule", { value: true });
7
- exports.BufferPool = void 0;
8
- class BufferPool {
9
- constructor(config = {}) {
10
- this.smallBuffers = [];
11
- this.mediumBuffers = [];
12
- this.largeBuffers = [];
13
- this.smallSize = config.smallSize ?? 4096; // 4KB
14
- this.mediumSize = config.mediumSize ?? 65536; // 64KB
15
- this.largeSize = config.largeSize ?? 262144; // 256KB
16
- this.smallPoolSize = config.smallPoolSize ?? 32;
17
- this.mediumPoolSize = config.mediumPoolSize ?? 8;
18
- this.largePoolSize = config.largePoolSize ?? 2;
19
- // Preallocate buffers
20
- for (let i = 0; i < this.smallPoolSize; i += 1) {
21
- this.smallBuffers.push(Buffer.allocUnsafe(this.smallSize));
22
- }
23
- for (let i = 0; i < this.mediumPoolSize; i += 1) {
24
- this.mediumBuffers.push(Buffer.allocUnsafe(this.mediumSize));
25
- }
26
- for (let i = 0; i < this.largePoolSize; i += 1) {
27
- this.largeBuffers.push(Buffer.allocUnsafe(this.largeSize));
28
- }
29
- }
30
- /**
31
- * Acquire a buffer suitable for the given size.
32
- * Returns a buffer from the appropriate pool.
33
- */
34
- acquire(size) {
35
- if (size <= this.smallSize) {
36
- return this.smallBuffers.pop() || Buffer.allocUnsafe(this.smallSize);
37
- }
38
- if (size <= this.mediumSize) {
39
- return this.mediumBuffers.pop() || Buffer.allocUnsafe(this.mediumSize);
40
- }
41
- return this.largeBuffers.pop() || Buffer.allocUnsafe(this.largeSize);
42
- }
43
- /**
44
- * Release a buffer back to the appropriate pool.
45
- */
46
- release(buffer) {
47
- if (buffer.length === this.smallSize && this.smallBuffers.length < this.smallPoolSize) {
48
- this.smallBuffers.push(buffer);
49
- }
50
- else if (buffer.length === this.mediumSize &&
51
- this.mediumBuffers.length < this.mediumPoolSize) {
52
- this.mediumBuffers.push(buffer);
53
- }
54
- else if (buffer.length === this.largeSize &&
55
- this.largeBuffers.length < this.largePoolSize) {
56
- this.largeBuffers.push(buffer);
57
- }
58
- }
59
- /**
60
- * Get the number of available buffers in each pool.
61
- */
62
- getPoolStatus() {
63
- return {
64
- small: this.smallBuffers.length,
65
- medium: this.mediumBuffers.length,
66
- large: this.largeBuffers.length,
67
- };
68
- }
69
- }
70
- exports.BufferPool = BufferPool;
@@ -1,7 +0,0 @@
1
- import type { QHTTPXOptions } from './types';
2
- export type LoadConfigOptions = {
3
- env?: NodeJS.ProcessEnv;
4
- defaults?: QHTTPXOptions;
5
- prefix?: string;
6
- };
7
- export declare function loadConfig(options?: LoadConfigOptions): QHTTPXOptions;
@@ -1,50 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.loadConfig = loadConfig;
4
- function loadConfig(options = {}) {
5
- const env = options.env ?? process.env;
6
- const defaults = options.defaults ?? {};
7
- const prefix = options.prefix ?? 'QHTTPX_';
8
- const getNumber = (key) => {
9
- const value = env[prefix + key];
10
- if (!value) {
11
- return undefined;
12
- }
13
- const n = Number(value);
14
- if (!Number.isFinite(n) || n <= 0) {
15
- return undefined;
16
- }
17
- return n;
18
- };
19
- const getBoolean = (key) => {
20
- const value = env[prefix + key];
21
- if (!value) {
22
- return undefined;
23
- }
24
- const lower = value.toLowerCase();
25
- if (lower === 'true' || lower === '1') {
26
- return true;
27
- }
28
- if (lower === 'false' || lower === '0') {
29
- return false;
30
- }
31
- return undefined;
32
- };
33
- const maxConcurrency = getNumber('MAX_CONCURRENCY');
34
- const requestTimeoutMs = getNumber('REQUEST_TIMEOUT_MS');
35
- const maxMemoryBytes = getNumber('MAX_MEMORY_BYTES');
36
- const maxBodyBytes = getNumber('MAX_BODY_BYTES');
37
- const keepAliveTimeoutMs = getNumber('KEEP_ALIVE_TIMEOUT_MS');
38
- const headersTimeoutMs = getNumber('HEADERS_TIMEOUT_MS');
39
- const metricsEnabled = getBoolean('METRICS_ENABLED');
40
- return {
41
- ...defaults,
42
- maxConcurrency: maxConcurrency ?? defaults.maxConcurrency,
43
- requestTimeoutMs: requestTimeoutMs ?? defaults.requestTimeoutMs,
44
- maxMemoryBytes: maxMemoryBytes ?? defaults.maxMemoryBytes,
45
- maxBodyBytes: maxBodyBytes ?? defaults.maxBodyBytes,
46
- keepAliveTimeoutMs: keepAliveTimeoutMs ?? defaults.keepAliveTimeoutMs,
47
- headersTimeoutMs: headersTimeoutMs ?? defaults.headersTimeoutMs,
48
- metricsEnabled: metricsEnabled ?? defaults.metricsEnabled,
49
- };
50
- }
@@ -1,34 +0,0 @@
1
- import { HttpError } from './types';
2
- export declare class BadRequestException extends HttpError {
3
- constructor(message?: string, details?: unknown);
4
- }
5
- export declare class UnauthorizedException extends HttpError {
6
- constructor(message?: string, details?: unknown);
7
- }
8
- export declare class ForbiddenException extends HttpError {
9
- constructor(message?: string, details?: unknown);
10
- }
11
- export declare class NotFoundException extends HttpError {
12
- constructor(message?: string, details?: unknown);
13
- }
14
- export declare class MethodNotAllowedException extends HttpError {
15
- constructor(message?: string, details?: unknown);
16
- }
17
- export declare class ConflictException extends HttpError {
18
- constructor(message?: string, details?: unknown);
19
- }
20
- export declare class PayloadTooLargeException extends HttpError {
21
- constructor(message?: string, details?: unknown);
22
- }
23
- export declare class UnsupportedMediaTypeException extends HttpError {
24
- constructor(message?: string, details?: unknown);
25
- }
26
- export declare class TooManyRequestsException extends HttpError {
27
- constructor(message?: string, details?: unknown);
28
- }
29
- export declare class InternalServerErrorException extends HttpError {
30
- constructor(message?: string, details?: unknown);
31
- }
32
- export declare class ServiceUnavailableException extends HttpError {
33
- constructor(message?: string, details?: unknown);
34
- }
@@ -1,70 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ServiceUnavailableException = exports.InternalServerErrorException = exports.TooManyRequestsException = exports.UnsupportedMediaTypeException = exports.PayloadTooLargeException = exports.ConflictException = exports.MethodNotAllowedException = exports.NotFoundException = exports.ForbiddenException = exports.UnauthorizedException = exports.BadRequestException = void 0;
4
- const types_1 = require("./types");
5
- class BadRequestException extends types_1.HttpError {
6
- constructor(message = 'Bad Request', details) {
7
- super(400, message, { code: 'BAD_REQUEST', details });
8
- }
9
- }
10
- exports.BadRequestException = BadRequestException;
11
- class UnauthorizedException extends types_1.HttpError {
12
- constructor(message = 'Unauthorized', details) {
13
- super(401, message, { code: 'UNAUTHORIZED', details });
14
- }
15
- }
16
- exports.UnauthorizedException = UnauthorizedException;
17
- class ForbiddenException extends types_1.HttpError {
18
- constructor(message = 'Forbidden', details) {
19
- super(403, message, { code: 'FORBIDDEN', details });
20
- }
21
- }
22
- exports.ForbiddenException = ForbiddenException;
23
- class NotFoundException extends types_1.HttpError {
24
- constructor(message = 'Not Found', details) {
25
- super(404, message, { code: 'NOT_FOUND', details });
26
- }
27
- }
28
- exports.NotFoundException = NotFoundException;
29
- class MethodNotAllowedException extends types_1.HttpError {
30
- constructor(message = 'Method Not Allowed', details) {
31
- super(405, message, { code: 'METHOD_NOT_ALLOWED', details });
32
- }
33
- }
34
- exports.MethodNotAllowedException = MethodNotAllowedException;
35
- class ConflictException extends types_1.HttpError {
36
- constructor(message = 'Conflict', details) {
37
- super(409, message, { code: 'CONFLICT', details });
38
- }
39
- }
40
- exports.ConflictException = ConflictException;
41
- class PayloadTooLargeException extends types_1.HttpError {
42
- constructor(message = 'Payload Too Large', details) {
43
- super(413, message, { code: 'PAYLOAD_TOO_LARGE', details });
44
- }
45
- }
46
- exports.PayloadTooLargeException = PayloadTooLargeException;
47
- class UnsupportedMediaTypeException extends types_1.HttpError {
48
- constructor(message = 'Unsupported Media Type', details) {
49
- super(415, message, { code: 'UNSUPPORTED_MEDIA_TYPE', details });
50
- }
51
- }
52
- exports.UnsupportedMediaTypeException = UnsupportedMediaTypeException;
53
- class TooManyRequestsException extends types_1.HttpError {
54
- constructor(message = 'Too Many Requests', details) {
55
- super(429, message, { code: 'TOO_MANY_REQUESTS', details });
56
- }
57
- }
58
- exports.TooManyRequestsException = TooManyRequestsException;
59
- class InternalServerErrorException extends types_1.HttpError {
60
- constructor(message = 'Internal Server Error', details) {
61
- super(500, message, { code: 'INTERNAL_SERVER_ERROR', details });
62
- }
63
- }
64
- exports.InternalServerErrorException = InternalServerErrorException;
65
- class ServiceUnavailableException extends types_1.HttpError {
66
- constructor(message = 'Service Unavailable', details) {
67
- super(503, message, { code: 'SERVICE_UNAVAILABLE', details });
68
- }
69
- }
70
- exports.ServiceUnavailableException = ServiceUnavailableException;
@@ -1,14 +0,0 @@
1
- import type { QHTTPXContext } from './types';
2
- export interface RequestFusionOptions {
3
- windowMs?: number;
4
- vary?: string[];
5
- }
6
- export declare class RequestFusion {
7
- private inflight;
8
- private cache;
9
- private options;
10
- constructor(options?: boolean | RequestFusionOptions);
11
- coalesce(ctx: QHTTPXContext, next: (ctx: QHTTPXContext) => Promise<void> | void): Promise<void>;
12
- private applyResult;
13
- private getKey;
14
- }
@@ -1,183 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.RequestFusion = void 0;
4
- const crypto_1 = require("crypto");
5
- class RequestFusion {
6
- constructor(options = {}) {
7
- this.inflight = new Map();
8
- this.cache = new Map();
9
- this.options = typeof options === 'boolean' ? {} : options;
10
- if (!this.options.vary) {
11
- this.options.vary = ['authorization', 'cookie'];
12
- }
13
- }
14
- async coalesce(ctx, next) {
15
- const key = this.getKey(ctx);
16
- // 1. Check Cache (Short-lived "Micro-TTL")
17
- if (this.options.windowMs && this.options.windowMs > 0) {
18
- const cached = this.cache.get(key);
19
- if (cached) {
20
- if (Date.now() < cached.expires) {
21
- this.applyResult(ctx, cached.result);
22
- return;
23
- }
24
- else {
25
- this.cache.delete(key);
26
- }
27
- }
28
- }
29
- // 2. Check In-Flight
30
- if (this.inflight.has(key)) {
31
- try {
32
- const result = await this.inflight.get(key);
33
- this.applyResult(ctx, result);
34
- return;
35
- }
36
- catch (err) {
37
- // If leader failed, we should probably fail too or retry?
38
- // For now, let's propagate the error.
39
- throw err;
40
- }
41
- }
42
- // 3. Be the Leader
43
- let resolve;
44
- let reject;
45
- const promise = new Promise((res, rej) => {
46
- resolve = res;
47
- reject = rej;
48
- });
49
- this.inflight.set(key, promise);
50
- // Hijack context methods to capture result
51
- // Cast to any to allow overwriting readonly methods
52
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
53
- const mutableCtx = ctx;
54
- const originalJson = mutableCtx.json;
55
- const originalSend = mutableCtx.send;
56
- const originalHtml = mutableCtx.html;
57
- const originalRedirect = mutableCtx.redirect;
58
- let captured = false;
59
- // Helper to cleanup and resolve
60
- const finish = (result) => {
61
- if (captured)
62
- return;
63
- captured = true;
64
- resolve(result);
65
- // Cache if needed
66
- if (this.options.windowMs && this.options.windowMs > 0) {
67
- this.cache.set(key, {
68
- result,
69
- expires: Date.now() + this.options.windowMs
70
- });
71
- // Cleanup cache later
72
- setTimeout(() => this.cache.delete(key), this.options.windowMs);
73
- }
74
- // Remove from inflight immediately (unless we rely on cache for window)
75
- // Actually, if we have windowMs, we rely on cache.
76
- // If windowMs=0, we remove immediately.
77
- this.inflight.delete(key);
78
- };
79
- mutableCtx.json = (payload, status = 200) => {
80
- finish({ type: 'json', payload, status });
81
- return originalJson.call(ctx, payload, status);
82
- };
83
- mutableCtx.send = (payload, status = 200) => {
84
- finish({ type: 'send', payload, status });
85
- return originalSend.call(ctx, payload, status);
86
- };
87
- mutableCtx.html = (payload, status = 200) => {
88
- finish({ type: 'html', payload, status });
89
- return originalHtml.call(ctx, payload, status);
90
- };
91
- mutableCtx.redirect = (url, status = 302) => {
92
- finish({ type: 'redirect', payload: url, status });
93
- return originalRedirect.call(ctx, url, status);
94
- };
95
- try {
96
- await next(ctx);
97
- // If next() completes but no response method was called,
98
- // it implies the handler might be async and forgot to await,
99
- // or it's a 404/middleware issue.
100
- // If not captured yet, we can't resolve the followers properly.
101
- // They will hang unless we reject or resolve with something.
102
- // However, typical QHTTPX usage implies ctx.json/send is called.
103
- }
104
- catch (err) {
105
- reject(err);
106
- this.inflight.delete(key);
107
- // Restore methods
108
- mutableCtx.json = originalJson;
109
- mutableCtx.send = originalSend;
110
- mutableCtx.html = originalHtml;
111
- mutableCtx.redirect = originalRedirect;
112
- throw err;
113
- }
114
- }
115
- applyResult(ctx, result) {
116
- switch (result.type) {
117
- case 'json':
118
- ctx.json(result.payload, result.status);
119
- break;
120
- case 'send': {
121
- if (typeof result.payload === 'string' || Buffer.isBuffer(result.payload)) {
122
- ctx.send(result.payload, result.status);
123
- }
124
- else {
125
- ctx.send(String(result.payload), result.status);
126
- }
127
- break;
128
- }
129
- case 'html': {
130
- if (typeof result.payload === 'string') {
131
- ctx.html(result.payload, result.status);
132
- }
133
- else {
134
- ctx.html(String(result.payload), result.status);
135
- }
136
- break;
137
- }
138
- case 'redirect': {
139
- if (typeof result.payload === 'string') {
140
- ctx.redirect(result.payload, result.status);
141
- }
142
- else {
143
- ctx.redirect(String(result.payload), result.status);
144
- }
145
- break;
146
- }
147
- }
148
- }
149
- getKey(ctx) {
150
- // Base: Method + Path
151
- let data = `${ctx.req.method}|${ctx.url?.pathname}|`;
152
- // Query
153
- if (ctx.query) {
154
- // Deterministic sort
155
- const keys = Object.keys(ctx.query).sort();
156
- for (const k of keys) {
157
- data += `${k}=${ctx.query[k]}|`;
158
- }
159
- }
160
- // Body (if parsed)
161
- if (ctx.body) {
162
- // We assume body is simple JSON.
163
- // For objects, order matters for hashing.
164
- // fast-json-stringify or JSON.stringify isn't always deterministic key order.
165
- // But for performance, JSON.stringify is often "good enough" if keys aren't shuffled.
166
- // For strict correctness, we should use a canonical stringify, but that's slow.
167
- // Let's use JSON.stringify for now.
168
- data += JSON.stringify(ctx.body);
169
- }
170
- // Vary Headers
171
- if (this.options.vary) {
172
- for (const header of this.options.vary) {
173
- const val = ctx.req.headers[header.toLowerCase()];
174
- if (val) {
175
- data += `|${header}:${val}`;
176
- }
177
- }
178
- }
179
- // We hash it to keep the key size manageable
180
- return (0, crypto_1.createHash)('sha1').update(data).digest('hex');
181
- }
182
- }
183
- exports.RequestFusion = RequestFusion;
@@ -1,22 +0,0 @@
1
- import pino from 'pino';
2
- export type LogLevel = 'fatal' | 'error' | 'warn' | 'info' | 'debug' | 'trace';
3
- export interface LoggerOptions {
4
- level?: LogLevel;
5
- pretty?: boolean;
6
- name?: string;
7
- }
8
- export declare class Logger {
9
- private pino;
10
- constructor(options?: LoggerOptions);
11
- info(msg: string, ...args: any[]): void;
12
- info(obj: object, msg?: string, ...args: any[]): void;
13
- error(msg: string, ...args: any[]): void;
14
- error(obj: object, msg?: string, ...args: any[]): void;
15
- warn(msg: string, ...args: any[]): void;
16
- warn(obj: object, msg?: string, ...args: any[]): void;
17
- debug(msg: string, ...args: any[]): void;
18
- debug(obj: object, msg?: string, ...args: any[]): void;
19
- fatal(msg: string, ...args: any[]): void;
20
- fatal(obj: object, msg?: string, ...args: any[]): void;
21
- child(bindings: pino.Bindings): Logger;
22
- }
@@ -1,49 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.Logger = void 0;
7
- /* eslint-disable @typescript-eslint/no-explicit-any */
8
- const pino_1 = __importDefault(require("pino"));
9
- class Logger {
10
- constructor(options = {}) {
11
- const isDev = process.env.NODE_ENV !== 'production';
12
- const pretty = options.pretty ?? isDev;
13
- this.pino = (0, pino_1.default)({
14
- name: options.name || 'qhttpx',
15
- level: options.level || 'info',
16
- transport: pretty
17
- ? {
18
- target: 'pino-pretty',
19
- options: {
20
- colorize: true,
21
- translateTime: 'HH:MM:ss Z',
22
- ignore: 'pid,hostname',
23
- },
24
- }
25
- : undefined,
26
- });
27
- }
28
- info(arg1, ...args) {
29
- this.pino.info(arg1, ...args);
30
- }
31
- error(arg1, ...args) {
32
- this.pino.error(arg1, ...args);
33
- }
34
- warn(arg1, ...args) {
35
- this.pino.warn(arg1, ...args);
36
- }
37
- debug(arg1, ...args) {
38
- this.pino.debug(arg1, ...args);
39
- }
40
- fatal(arg1, ...args) {
41
- this.pino.fatal(arg1, ...args);
42
- }
43
- child(bindings) {
44
- const child = new Logger();
45
- child.pino = this.pino.child(bindings);
46
- return child;
47
- }
48
- }
49
- exports.Logger = Logger;