@deepkit/http 1.0.1-alpha.94 → 1.0.1-alpha.96

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 (38) hide show
  1. package/dist/cjs/src/model.d.ts +1 -0
  2. package/dist/cjs/src/model.js +4 -1
  3. package/dist/cjs/src/model.js.map +1 -1
  4. package/dist/cjs/src/module.config.d.ts +81 -0
  5. package/dist/cjs/src/module.config.js +5 -2
  6. package/dist/cjs/src/module.config.js.map +1 -1
  7. package/dist/cjs/src/module.d.ts +2 -3
  8. package/dist/cjs/src/module.js +9 -6
  9. package/dist/cjs/src/module.js.map +1 -1
  10. package/dist/cjs/src/request-parser.d.ts +3 -2
  11. package/dist/cjs/src/request-parser.js +13 -11
  12. package/dist/cjs/src/request-parser.js.map +1 -1
  13. package/dist/cjs/src/router.d.ts +4 -2
  14. package/dist/cjs/src/router.js +7 -5
  15. package/dist/cjs/src/router.js.map +1 -1
  16. package/dist/cjs/tsconfig.tsbuildinfo +1 -1
  17. package/dist/esm/src/model.d.ts +1 -0
  18. package/dist/esm/src/model.js +4 -1
  19. package/dist/esm/src/model.js.map +1 -1
  20. package/dist/esm/src/module.config.d.ts +81 -0
  21. package/dist/esm/src/module.config.js +4 -1
  22. package/dist/esm/src/module.config.js.map +1 -1
  23. package/dist/esm/src/module.d.ts +2 -3
  24. package/dist/esm/src/module.js +9 -6
  25. package/dist/esm/src/module.js.map +1 -1
  26. package/dist/esm/src/request-parser.d.ts +3 -2
  27. package/dist/esm/src/request-parser.js +13 -11
  28. package/dist/esm/src/request-parser.js.map +1 -1
  29. package/dist/esm/src/router.d.ts +4 -2
  30. package/dist/esm/src/router.js +7 -5
  31. package/dist/esm/src/router.js.map +1 -1
  32. package/dist/esm/tsconfig.esm.tsbuildinfo +1 -1
  33. package/package.json +9 -9
  34. package/src/model.ts +4 -0
  35. package/src/module.config.ts +92 -0
  36. package/src/module.ts +7 -5
  37. package/src/request-parser.ts +15 -10
  38. package/src/router.ts +10 -5
@@ -1,3 +1,95 @@
1
+ export interface HttpParserOptions {
2
+ /**
3
+ * sets encoding for incoming form fields
4
+ *
5
+ * @default 'utf-8'
6
+ */
7
+ encoding?: BufferEncoding | undefined;
8
+
9
+ /**
10
+ * the directory for placing file uploads in. You can move them later by using fs.rename()
11
+ *
12
+ * @default os.tmpdir()
13
+ */
14
+ uploadDir?: string | undefined;
15
+
16
+ /**
17
+ * to include the extensions of the original files or not
18
+ *
19
+ * @default false
20
+ */
21
+ keepExtensions?: boolean | undefined;
22
+
23
+ /**
24
+ * allow upload empty files
25
+ *
26
+ * @default true
27
+ */
28
+ allowEmptyFiles?: boolean | undefined;
29
+
30
+ /**
31
+ * the minium size of uploaded file
32
+ *
33
+ * @default 1
34
+ */
35
+ minFileSize?: number | undefined;
36
+
37
+ /**
38
+ * limit the amount of uploaded files, set Infinity for unlimited
39
+ *
40
+ * @default Infinity
41
+ */
42
+ maxFiles?: number | undefined;
43
+
44
+ /**
45
+ * limit the size of uploaded file
46
+ *
47
+ * @default 200 * 1024 * 1024
48
+ */
49
+ maxFileSize?: number | undefined;
50
+
51
+ /**
52
+ * limit the size of the batch of uploaded files
53
+ *
54
+ * @default options.maxFileSize
55
+ */
56
+ maxTotalFileSize?: number | undefined;
57
+
58
+ /**
59
+ * limit the number of fields, set 0 for unlimited
60
+ *
61
+ * @default 1000
62
+ */
63
+ maxFields?: number | undefined;
64
+
65
+ /**
66
+ * limit the amount of memory all fields together (except files) can allocate in bytes
67
+ *
68
+ * @default 20 * 1024 * 1024
69
+ */
70
+ maxFieldsSize?: number | undefined;
71
+
72
+ /**
73
+ * include checksums calculated for incoming files, set this to some hash algorithm, see
74
+ * crypto.createHash for available algorithms
75
+ *
76
+ * @default false
77
+ */
78
+ hashAlgorithm?: string | false | undefined;
79
+
80
+ /**
81
+ * when you call the .parse method, the files argument (of the callback) will contain arrays of
82
+ * files for inputs which submit multiple files using the HTML5 multiple attribute. Also, the
83
+ * fields argument will contain arrays of values for fields that have names ending with '[]'
84
+ *
85
+ * @default false
86
+ */
87
+ multiples?: boolean | undefined;
88
+
89
+ enabledPlugins?: string[] | undefined;
90
+ }
91
+
1
92
  export class HttpConfig {
2
93
  debug: boolean = false;
94
+ parser: HttpParserOptions = {};
3
95
  }
package/src/module.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { HttpListener, HttpResultFormatter, httpWorkflow } from './http.js';
2
2
  import { HttpConfig } from './module.config.js';
3
- import { AddedListener, AppModule, createModule, stringifyListener } from '@deepkit/app';
3
+ import { AddedListener, AppModule, ControllerConfig, createModule, stringifyListener } from '@deepkit/app';
4
4
  import { HttpRouter, HttpRouterRegistry, RouteConfig } from './router.js';
5
5
  import { HttpKernel } from './kernel.js';
6
6
  import { HttpRouterFilterResolver } from './filter.js';
@@ -8,7 +8,6 @@ import { HttpControllers } from './controllers.js';
8
8
  import { ConsoleTransport, Logger } from '@deepkit/logger';
9
9
  import { HttpRequest, HttpResponse } from './model.js';
10
10
  import '@deepkit/type';
11
- import { ClassType } from '@deepkit/core';
12
11
  import { httpClass } from './decorator.js';
13
12
  import { EventToken } from '@deepkit/event';
14
13
  import { metaAnnotation, ReflectionKind, ReflectionParameter, Type } from '@deepkit/type';
@@ -89,10 +88,10 @@ export class HttpModule extends createModule({
89
88
  let i = index;
90
89
 
91
90
  this.addProvider({
92
- provide: uniqueType, useFactory: (request: HttpRequest, injector: InjectorContext, config?: RouteConfig) => {
91
+ provide: uniqueType, useFactory: (httpConfig: HttpConfig, request: HttpRequest, injector: InjectorContext, config?: RouteConfig) => {
93
92
  if (!build) {
94
93
  const params = listener.reflection.getParameters().slice(1);
95
- build = buildRequestParser(params, config?.getFullPath());
94
+ build = buildRequestParser(httpConfig.parser, params, config?.getFullPath());
96
95
  }
97
96
 
98
97
  const parser = build(request);
@@ -104,7 +103,10 @@ export class HttpModule extends createModule({
104
103
  }
105
104
  }
106
105
 
107
- processController(module: AppModule<any>, controller: ClassType) {
106
+ processController(module: AppModule<any>, config: ControllerConfig) {
107
+ const controller = config.controller;
108
+ if (!controller) return;
109
+
108
110
  const httpConfig = httpClass._fetch(controller);
109
111
  if (!httpConfig) return;
110
112
 
@@ -5,14 +5,15 @@ import {
5
5
  findMember,
6
6
  getSerializeFunction,
7
7
  getValidatorFunction,
8
- metaAnnotation, ReflectionFunction,
8
+ metaAnnotation,
9
9
  ReflectionKind,
10
10
  ReflectionParameter,
11
11
  resolveReceiveType,
12
12
  serializer,
13
13
  stringifyType,
14
14
  Type,
15
- typeToObject, ValidationError
15
+ typeToObject,
16
+ ValidationError
16
17
  } from '@deepkit/type';
17
18
  import { BodyValidationError, createRequestWithCachedBody, getRegExp, HttpRequest, ValidatedBody } from './model.js';
18
19
  import { RouteConfig, UploadedFile } from './router.js';
@@ -22,13 +23,15 @@ import qs from 'qs';
22
23
 
23
24
  // @ts-ignore
24
25
  import formidable from 'formidable';
26
+ import { HttpParserOptions } from './module.config.js';
25
27
 
26
28
 
27
- function parseBody(req: HttpRequest, foundFiles: { [name: string]: UploadedFile }) {
28
- const form = formidable({
29
+ function parseBody(
30
+ options: HttpParserOptions,
31
+ req: HttpRequest, foundFiles: { [name: string]: UploadedFile }) {
32
+ const form = formidable(Object.assign(options, {
29
33
  multiples: true,
30
- hash: 'sha1',
31
- });
34
+ }));
32
35
  return asyncOperation((resolve, reject) => {
33
36
  function parseData(err: any, fields: any, files: any) {
34
37
  if (err) {
@@ -149,7 +152,7 @@ export function parseRoutePathToRegex(path: string, params: ReflectionParameter[
149
152
  return { regex: path, parameterNames };
150
153
  }
151
154
 
152
- export function buildRequestParser(parameters: ReflectionParameter[], path?: string): (request: HttpRequest) => any[] {
155
+ export function buildRequestParser(parseOptions: HttpParserOptions, parameters: ReflectionParameter[], path?: string): (request: HttpRequest) => any[] {
153
156
  const compiler = new CompilerContext();
154
157
  const params = parameters.map(v => new ParameterForRequestParser(v));
155
158
 
@@ -164,7 +167,7 @@ export function buildRequestParser(parameters: ReflectionParameter[], path?: str
164
167
  }
165
168
  }
166
169
 
167
- const code = getRequestParserCodeForParameters(compiler, params, {});
170
+ const code = getRequestParserCodeForParameters(compiler, parseOptions, params, {});
168
171
  compiler.context.set('ValidationError', ValidationError);
169
172
  compiler.context.set('qs', qs);
170
173
 
@@ -188,13 +191,14 @@ export function buildRequestParser(parameters: ReflectionParameter[], path?: str
188
191
 
189
192
  export function getRequestParserCodeForParameters(
190
193
  compiler: CompilerContext,
194
+ parseOptions: HttpParserOptions,
191
195
  parameters: ParameterForRequestParser[],
192
196
  config: {
193
197
  module?: InjectorModule<any>,
194
198
  resolverForParameterName?: Map<string, ClassType>,
195
199
  resolverForToken?: Map<any, ClassType>,
196
200
  pathParameterNames?: { [name: string]: number },
197
- routeConfig?: RouteConfig
201
+ routeConfig?: RouteConfig,
198
202
  }
199
203
  ) {
200
204
  let enableParseBody = false;
@@ -314,9 +318,10 @@ export function getRequestParserCodeForParameters(
314
318
 
315
319
  let parseBodyLoading = '';
316
320
  if (enableParseBody) {
321
+ const parseOptionsVar = compiler.reserveVariable('parseOptions', parseOptions);
317
322
  const parseBodyVar = compiler.reserveVariable('parseBody', parseBody);
318
323
  parseBodyLoading = `
319
- const bodyFields = (await ${parseBodyVar}(request, uploadedFiles));`;
324
+ const bodyFields = (await ${parseBodyVar}(${parseOptionsVar}, request, uploadedFiles));`;
320
325
  requiresAsyncParameters = true;
321
326
  }
322
327
 
package/src/router.ts CHANGED
@@ -21,6 +21,7 @@ import { HttpMiddlewareConfig, HttpMiddlewareFn } from './middleware.js';
21
21
  import qs from 'qs';
22
22
  import { HtmlResponse, JSONResponse, Response } from './http.js';
23
23
  import { getRequestParserCodeForParameters, ParameterForRequestParser, parseRoutePathToRegex } from './request-parser.js';
24
+ import { HttpConfig, HttpParserOptions } from './module.config.js';
24
25
 
25
26
  export type RouteParameterResolverForInjector = ((injector: InjectorContext) => HttpRequestPositionedParameters | Promise<HttpRequestPositionedParameters>);
26
27
 
@@ -215,7 +216,10 @@ export interface RouteParameterResolverContext {
215
216
  type: ReflectionParameter;
216
217
  }
217
218
 
218
- function filterMiddlewaresForRoute(middlewareRawConfigs: MiddlewareRegistryEntry[], routeConfig: RouteConfig, fullPath: string): { config: HttpMiddlewareConfig, module: InjectorModule<any> }[] {
219
+ function filterMiddlewaresForRoute(middlewareRawConfigs: MiddlewareRegistryEntry[], routeConfig: RouteConfig, fullPath: string): {
220
+ config: HttpMiddlewareConfig,
221
+ module: InjectorModule<any>
222
+ }[] {
219
223
  const middlewares = middlewareRawConfigs.slice(0);
220
224
  middlewares.push(...routeConfig.middlewares as any[]);
221
225
 
@@ -526,11 +530,11 @@ export class HttpRouter {
526
530
  protected buildId: number = 0;
527
531
  protected resolveFn?: (name: string, parameters: { [name: string]: any }) => string;
528
532
 
529
-
530
533
  constructor(
531
534
  controllers: HttpControllers,
532
535
  private logger: LoggerInterface,
533
536
  tagRegistry: TagRegistry,
537
+ private config: HttpConfig,
534
538
  private middlewareRegistry: MiddlewareRegistry = new MiddlewareRegistry,
535
539
  private registry: HttpRouterRegistry = new HttpRouterRegistry,
536
540
  ) {
@@ -547,11 +551,12 @@ export class HttpRouter {
547
551
  controllers: (ClassType | { module: InjectorModule<any>, controller: ClassType })[],
548
552
  tagRegistry: TagRegistry = new TagRegistry(),
549
553
  middlewareRegistry: MiddlewareRegistry = new MiddlewareRegistry(),
550
- module: InjectorModule<any> = new InjectorModule()
554
+ module: InjectorModule<any> = new InjectorModule(),
555
+ config: HttpConfig = new HttpConfig()
551
556
  ): HttpRouter {
552
557
  return new this(new HttpControllers(controllers.map(v => {
553
558
  return isClass(v) ? { controller: v, module } : v;
554
- })), new Logger([], []), tagRegistry, middlewareRegistry);
559
+ })), new Logger([], []), tagRegistry, config, middlewareRegistry);
555
560
  }
556
561
 
557
562
  protected getRouteCode(compiler: CompilerContext, routeConfig: RouteConfig): string {
@@ -600,7 +605,7 @@ export class HttpRouter {
600
605
  `;
601
606
  }
602
607
 
603
- const parametersLoader = getRequestParserCodeForParameters(compiler, parameters, {
608
+ const parametersLoader = getRequestParserCodeForParameters(compiler, this.config.parser, parameters, {
604
609
  module: routeConfig.module,
605
610
  resolverForToken: routeConfig.resolverForToken,
606
611
  resolverForParameterName: routeConfig.resolverForParameterName,