@engjts/nexus 0.1.7 → 0.1.8

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 (72) hide show
  1. package/BENCHMARK_REPORT.md +343 -0
  2. package/dist/advanced/playground/generatePlaygroundHTML.d.ts.map +1 -1
  3. package/dist/advanced/playground/generatePlaygroundHTML.js +107 -0
  4. package/dist/advanced/playground/generatePlaygroundHTML.js.map +1 -1
  5. package/dist/advanced/playground/playground.d.ts +19 -0
  6. package/dist/advanced/playground/playground.d.ts.map +1 -1
  7. package/dist/advanced/playground/playground.js +70 -0
  8. package/dist/advanced/playground/playground.js.map +1 -1
  9. package/dist/advanced/playground/types.d.ts +20 -0
  10. package/dist/advanced/playground/types.d.ts.map +1 -1
  11. package/dist/core/application.d.ts +14 -0
  12. package/dist/core/application.d.ts.map +1 -1
  13. package/dist/core/application.js +173 -71
  14. package/dist/core/application.js.map +1 -1
  15. package/dist/core/context-pool.d.ts +2 -13
  16. package/dist/core/context-pool.d.ts.map +1 -1
  17. package/dist/core/context-pool.js +7 -45
  18. package/dist/core/context-pool.js.map +1 -1
  19. package/dist/core/context.d.ts +108 -5
  20. package/dist/core/context.d.ts.map +1 -1
  21. package/dist/core/context.js +449 -53
  22. package/dist/core/context.js.map +1 -1
  23. package/dist/core/index.d.ts +1 -0
  24. package/dist/core/index.d.ts.map +1 -1
  25. package/dist/core/index.js +9 -1
  26. package/dist/core/index.js.map +1 -1
  27. package/dist/core/middleware.d.ts +6 -0
  28. package/dist/core/middleware.d.ts.map +1 -1
  29. package/dist/core/middleware.js +83 -84
  30. package/dist/core/middleware.js.map +1 -1
  31. package/dist/core/performance/fast-json.d.ts +149 -0
  32. package/dist/core/performance/fast-json.d.ts.map +1 -0
  33. package/dist/core/performance/fast-json.js +473 -0
  34. package/dist/core/performance/fast-json.js.map +1 -0
  35. package/dist/core/router/file-router.d.ts +20 -7
  36. package/dist/core/router/file-router.d.ts.map +1 -1
  37. package/dist/core/router/file-router.js +41 -13
  38. package/dist/core/router/file-router.js.map +1 -1
  39. package/dist/core/router/index.d.ts +6 -0
  40. package/dist/core/router/index.d.ts.map +1 -1
  41. package/dist/core/router/index.js +33 -6
  42. package/dist/core/router/index.js.map +1 -1
  43. package/dist/core/router/radix-tree.d.ts +4 -1
  44. package/dist/core/router/radix-tree.d.ts.map +1 -1
  45. package/dist/core/router/radix-tree.js +7 -3
  46. package/dist/core/router/radix-tree.js.map +1 -1
  47. package/dist/core/serializer.d.ts +251 -0
  48. package/dist/core/serializer.d.ts.map +1 -0
  49. package/dist/core/serializer.js +290 -0
  50. package/dist/core/serializer.js.map +1 -0
  51. package/dist/core/types.d.ts +39 -1
  52. package/dist/core/types.d.ts.map +1 -1
  53. package/dist/core/types.js.map +1 -1
  54. package/dist/index.d.ts +1 -0
  55. package/dist/index.d.ts.map +1 -1
  56. package/dist/index.js +12 -2
  57. package/dist/index.js.map +1 -1
  58. package/package.json +3 -1
  59. package/src/advanced/playground/generatePlaygroundHTML.ts +107 -0
  60. package/src/advanced/playground/playground.ts +225 -145
  61. package/src/advanced/playground/types.ts +29 -0
  62. package/src/core/application.ts +202 -84
  63. package/src/core/context-pool.ts +8 -56
  64. package/src/core/context.ts +497 -53
  65. package/src/core/index.ts +14 -0
  66. package/src/core/middleware.ts +99 -89
  67. package/src/core/router/file-router.ts +41 -12
  68. package/src/core/router/index.ts +213 -180
  69. package/src/core/router/radix-tree.ts +20 -4
  70. package/src/core/serializer.ts +397 -0
  71. package/src/core/types.ts +43 -1
  72. package/src/index.ts +17 -0
@@ -5,28 +5,115 @@
5
5
  import { IncomingMessage, ServerResponse } from 'http';
6
6
  import { Context, Headers, Cookies, ResponseBuilder, Response, HTTPMethod } from './types';
7
7
  import { ContextStore, StoreConstructor, StoreRegistry, RequestStore, RequestStoreConstructor, RequestStoreRegistry } from './store';
8
+ import { SerializerFunction } from './serializer';
8
9
  /**
9
10
  * Context implementation
10
11
  */
11
12
  export declare class ContextImpl implements Context {
12
13
  method: HTTPMethod;
13
14
  path: string;
14
- url: URL;
15
+ private _url;
16
+ private _host;
15
17
  params: Record<string, string>;
16
- query: Record<string, any>;
17
- body: any;
18
+ private _query;
19
+ private _queryString;
18
20
  headers: Headers;
19
- cookies: Cookies;
21
+ private _cookieHeader;
22
+ private _cookies;
20
23
  raw: {
21
24
  req: IncomingMessage;
22
25
  res: ServerResponse;
23
26
  };
24
27
  response: ResponseBuilder;
28
+ private _parsedBody;
29
+ private _bodyPromise;
30
+ private _bodyParsed;
25
31
  private _storeRegistry?;
26
32
  private _requestStoreRegistry;
27
33
  private _data;
28
34
  private _debug;
29
35
  constructor(req: IncomingMessage, res: ServerResponse);
36
+ /**
37
+ * Lazy URL getter - only create URL object when accessed
38
+ * Most handlers don't need the full URL object
39
+ */
40
+ get url(): URL;
41
+ set url(value: URL);
42
+ /**
43
+ * Lazy query getter - only parse query string when accessed
44
+ * Most simple endpoints like /json don't need query parsing
45
+ * Inline fast-querystring for minimal overhead
46
+ */
47
+ get query(): Record<string, any>;
48
+ set query(value: Record<string, any>);
49
+ /**
50
+ * Lazy cookies getter - only parse cookies when accessed
51
+ */
52
+ get cookies(): Cookies;
53
+ set cookies(value: Cookies);
54
+ /**
55
+ * Reinitialize context for pooling (avoids new object creation)
56
+ */
57
+ reinitialize(req: IncomingMessage, res: ServerResponse): void;
58
+ /**
59
+ * Lazy body getter - parses body on first access
60
+ * This is the KEY optimization that fixes POST performance!
61
+ */
62
+ get body(): any;
63
+ /**
64
+ * Set body directly (for backwards compatibility)
65
+ */
66
+ set body(value: any);
67
+ /**
68
+ * Check if body is ready for sync access (no await needed)
69
+ */
70
+ get isBodyReady(): boolean;
71
+ /**
72
+ * Wait for body to be parsed
73
+ * Use this if you need to ensure body is available for sync access
74
+ * @example
75
+ * ```typescript
76
+ * app.post('/data', async (ctx) => {
77
+ * await ctx.waitForBody();
78
+ * console.log(ctx.body); // Now safe to access synchronously
79
+ * });
80
+ * ```
81
+ */
82
+ waitForBody(): Promise<any>;
83
+ /**
84
+ * Async body getter - use this in handlers for POST/PUT/PATCH
85
+ * @example
86
+ * ```typescript
87
+ * app.post('/data', async (ctx) => {
88
+ * const body = await ctx.getBody();
89
+ * return { received: body };
90
+ * });
91
+ * ```
92
+ */
93
+ getBody<T = any>(): Promise<T>;
94
+ /**
95
+ * Ultra-optimized body parser inspired by Fastify's approach
96
+ * Key optimizations:
97
+ * 1. Pre-check content-type before reading data
98
+ * 2. Use direct string concatenation with setEncoding
99
+ * 3. Minimal closure allocation
100
+ * 4. Fast-path for JSON (most common case)
101
+ */
102
+ private parseBodyOptimized;
103
+ /**
104
+ * Internal body parser - optimized for performance
105
+ * Uses string accumulation instead of Buffer.concat for better perf
106
+ * @deprecated Use parseBodyOptimized instead
107
+ */
108
+ private parseBodyInternal;
109
+ /**
110
+ * Parse body based on content type
111
+ */
112
+ private parseContentType;
113
+ /**
114
+ * Clear body state (for pooling)
115
+ */
116
+ clearBody(): void;
30
117
  json<T>(data: T, status?: number): Response;
31
118
  html(content: string, status?: number): Response;
32
119
  text(content: string, status?: number): Response;
@@ -48,6 +135,14 @@ export declare class ContextImpl implements Context {
48
135
  * ```
49
136
  */
50
137
  store<T extends ContextStore<any>>(StoreClass: StoreConstructor<T>): T;
138
+ /**
139
+ * Lazy getter for request store registry
140
+ */
141
+ private getOrCreateRequestStoreRegistry;
142
+ /**
143
+ * Lazy getter for request data map
144
+ */
145
+ private getOrCreateData;
51
146
  /**
52
147
  * Access a request-scoped store by its class
53
148
  * Store only exists for this request, disposed after response
@@ -133,7 +228,14 @@ export declare class ContextImpl implements Context {
133
228
  */
134
229
  setParams(params: Record<string, string>): void;
135
230
  /**
136
- * Set request body (called after parsing)
231
+ * Set response serializers for fast JSON serialization
232
+ * Called by router when route has response schema
233
+ * @internal
234
+ */
235
+ setSerializers(serializers: Map<number | string, SerializerFunction>): void;
236
+ /**
237
+ * Set request body (called after parsing or by middleware)
238
+ * @deprecated Use ctx.getBody() for async body access
137
239
  */
138
240
  setBody(body: any): void;
139
241
  /**
@@ -143,6 +245,7 @@ export declare class ContextImpl implements Context {
143
245
  }
144
246
  /**
145
247
  * Parse request body based on Content-Type
248
+ * @deprecated Use ctx.getBody() instead for lazy parsing
146
249
  */
147
250
  export declare function parseBody(req: IncomingMessage): Promise<any>;
148
251
  //# sourceMappingURL=context.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../../src/core/context.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,MAAM,CAAC;AAGvD,OAAO,EACH,OAAO,EACP,OAAO,EACP,OAAO,EAEP,eAAe,EACf,QAAQ,EACR,UAAU,EACb,MAAM,SAAS,CAAC;AACjB,OAAO,EAAE,YAAY,EAAE,gBAAgB,EAAE,aAAa,EAAE,YAAY,EAAE,uBAAuB,EAAE,oBAAoB,EAAE,MAAM,SAAS,CAAC;AAkIrI;;GAEG;AACH,qBAAa,WAAY,YAAW,OAAO;IACvC,MAAM,EAAE,UAAU,CAAC;IACnB,IAAI,EAAE,MAAM,CAAC;IACb,GAAG,EAAE,GAAG,CAAC;IACT,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAM;IACpC,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAM;IAChC,IAAI,EAAE,GAAG,CAAQ;IACjB,OAAO,EAAE,OAAO,CAAC;IACjB,OAAO,EAAE,OAAO,CAAC;IACjB,GAAG,EAAE;QAAE,GAAG,EAAE,eAAe,CAAC;QAAC,GAAG,EAAE,cAAc,CAAA;KAAE,CAAC;IACnD,QAAQ,EAAE,eAAe,CAAC;IAG1B,OAAO,CAAC,cAAc,CAAC,CAAgB;IAGvC,OAAO,CAAC,qBAAqB,CAAuB;IAGpD,OAAO,CAAC,KAAK,CAA+B;IAG5C,OAAO,CAAC,MAAM,CAAkB;gBAEpB,GAAG,EAAE,eAAe,EAAE,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