@deepkit/http 1.0.1-alpha.16 → 1.0.1-alpha.160
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.
- package/CHANGELOG.md +236 -0
- package/dist/cjs/index.d.ts +14 -10
- package/dist/cjs/index.js +19 -11
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/src/controllers.d.ts +10 -3
- package/dist/cjs/src/controllers.js +7 -3
- package/dist/cjs/src/controllers.js.map +1 -1
- package/dist/cjs/src/decorator.d.ts +144 -70
- package/dist/cjs/src/decorator.js +277 -102
- package/dist/cjs/src/decorator.js.map +1 -1
- package/dist/cjs/src/filter.d.ts +49 -0
- package/dist/cjs/src/filter.js +215 -0
- package/dist/cjs/src/filter.js.map +1 -0
- package/dist/cjs/src/http-app.d.ts +8 -0
- package/dist/cjs/src/http-app.js +16 -0
- package/dist/cjs/src/http-app.js.map +1 -0
- package/dist/cjs/src/http.d.ts +131 -32
- package/dist/cjs/src/http.js +523 -122
- package/dist/cjs/src/http.js.map +1 -1
- package/dist/cjs/src/kernel.d.ts +50 -9
- package/dist/cjs/src/kernel.js +109 -98
- package/dist/cjs/src/kernel.js.map +1 -1
- package/dist/cjs/src/logger.d.ts +3 -3
- package/dist/cjs/src/logger.js +9 -12
- package/dist/cjs/src/logger.js.map +1 -1
- package/dist/cjs/src/middleware.d.ts +61 -0
- package/dist/cjs/src/middleware.js +95 -0
- package/dist/cjs/src/middleware.js.map +1 -0
- package/dist/cjs/src/model.d.ts +270 -4
- package/dist/cjs/src/model.js +401 -3
- package/dist/cjs/src/model.js.map +1 -1
- package/dist/cjs/src/module.config.d.ts +149 -4
- package/dist/cjs/src/module.config.js +11 -6
- package/dist/cjs/src/module.config.js.map +1 -1
- package/dist/cjs/src/module.d.ts +14 -32
- package/dist/cjs/src/module.js +122 -7
- package/dist/cjs/src/module.js.map +1 -1
- package/dist/cjs/src/request-parser.d.ts +37 -0
- package/dist/cjs/src/request-parser.js +382 -0
- package/dist/cjs/src/request-parser.js.map +1 -0
- package/dist/cjs/src/router.d.ts +174 -75
- package/dist/cjs/src/router.js +469 -288
- package/dist/cjs/src/router.js.map +1 -1
- package/dist/cjs/src/static-serving.d.ts +32 -4
- package/dist/cjs/src/static-serving.js +115 -71
- package/dist/cjs/src/static-serving.js.map +1 -1
- package/dist/cjs/src/utils.js +2 -2
- package/dist/cjs/src/utils.js.map +1 -1
- package/dist/cjs/tsconfig.tsbuildinfo +1 -2916
- package/dist/esm/index.d.ts +14 -10
- package/dist/esm/index.js +14 -10
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/src/controllers.d.ts +10 -3
- package/dist/esm/src/controllers.js +7 -3
- package/dist/esm/src/controllers.js.map +1 -1
- package/dist/esm/src/decorator.d.ts +144 -70
- package/dist/esm/src/decorator.js +272 -99
- package/dist/esm/src/decorator.js.map +1 -1
- package/dist/esm/src/filter.d.ts +49 -0
- package/dist/esm/src/filter.js +209 -0
- package/dist/esm/src/filter.js.map +1 -0
- package/dist/esm/src/http-app.d.ts +8 -0
- package/dist/esm/src/http-app.js +12 -0
- package/dist/esm/src/http-app.js.map +1 -0
- package/dist/esm/src/http.d.ts +131 -32
- package/dist/esm/src/http.js +504 -121
- package/dist/esm/src/http.js.map +1 -1
- package/dist/esm/src/kernel.d.ts +50 -9
- package/dist/esm/src/kernel.js +110 -100
- package/dist/esm/src/kernel.js.map +1 -1
- package/dist/esm/src/logger.d.ts +3 -3
- package/dist/esm/src/logger.js +7 -11
- package/dist/esm/src/logger.js.map +1 -1
- package/dist/esm/src/middleware.d.ts +61 -0
- package/dist/esm/src/middleware.js +90 -0
- package/dist/esm/src/middleware.js.map +1 -0
- package/dist/esm/src/model.d.ts +270 -4
- package/dist/esm/src/model.js +357 -1
- package/dist/esm/src/model.js.map +1 -1
- package/dist/esm/src/module.config.d.ts +149 -4
- package/dist/esm/src/module.config.js +9 -5
- package/dist/esm/src/module.config.js.map +1 -1
- package/dist/esm/src/module.d.ts +14 -32
- package/dist/esm/src/module.js +121 -7
- package/dist/esm/src/module.js.map +1 -1
- package/dist/esm/src/request-parser.d.ts +37 -0
- package/dist/esm/src/request-parser.js +372 -0
- package/dist/esm/src/request-parser.js.map +1 -0
- package/dist/esm/src/router.d.ts +174 -75
- package/dist/esm/src/router.js +463 -285
- package/dist/esm/src/router.js.map +1 -1
- package/dist/esm/src/static-serving.d.ts +32 -4
- package/dist/esm/src/static-serving.js +107 -65
- package/dist/esm/src/static-serving.js.map +1 -1
- package/dist/esm/src/utils.js +1 -0
- package/dist/esm/src/utils.js.map +1 -1
- package/dist/esm/tsconfig.esm.tsbuildinfo +1 -2916
- package/index.ts +14 -10
- package/package.json +30 -19
- package/src/controllers.ts +4 -3
- package/src/decorator.ts +242 -104
- package/src/filter.ts +224 -0
- package/src/http-app.ts +11 -0
- package/src/http.ts +501 -100
- package/src/kernel.ts +128 -80
- package/src/logger.ts +4 -6
- package/src/middleware.ts +116 -0
- package/src/model.ts +516 -5
- package/src/module.config.ts +168 -5
- package/src/module.ts +120 -7
- package/src/request-parser.ts +430 -0
- package/src/router.ts +546 -317
- package/src/static-serving.ts +120 -50
- package/src/utils.ts +0 -2
- package/tsconfig.esm.json +7 -7
- package/tsconfig.json +18 -11
- package/tsconfig.spec.json +8 -0
- package/dist/cjs/tests/router.spec.d.ts +0 -1
- package/dist/cjs/tests/router.spec.js +0 -481
- package/dist/cjs/tests/router.spec.js.map +0 -1
- package/dist/esm/tests/router.spec.d.ts +0 -1
- package/dist/esm/tests/router.spec.js +0 -479
- package/dist/esm/tests/router.spec.js.map +0 -1
- package/tests/router.spec.ts +0 -347
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"middleware.js","sourceRoot":"","sources":["../../../src/middleware.ts"],"names":[],"mappings":"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"}
|
package/dist/esm/src/model.d.ts
CHANGED
|
@@ -1,14 +1,232 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
1
|
+
import { IncomingMessage, OutgoingHttpHeader, OutgoingHttpHeaders, ServerResponse } from 'http';
|
|
2
|
+
import { UploadedFile } from './router.js';
|
|
3
|
+
import { Type, ValidationErrorItem } from '@deepkit/type';
|
|
4
|
+
import { TypeAnnotation } from '@deepkit/core';
|
|
3
5
|
export declare class HttpResponse extends ServerResponse {
|
|
4
6
|
status(code: number): void;
|
|
5
7
|
}
|
|
6
|
-
|
|
8
|
+
/**
|
|
9
|
+
* Reads the body of the request and returns it as a Buffer.
|
|
10
|
+
* The result will be cached in the request object as `request.body`.
|
|
11
|
+
*
|
|
12
|
+
* Deepkit's router will automatically use `request.body` if available.
|
|
13
|
+
*/
|
|
14
|
+
export declare function readBody(request: IncomingMessage): Promise<Buffer>;
|
|
15
|
+
export declare function createRequestWithCachedBody(request: Partial<IncomingMessage>, body: Buffer): HttpRequest;
|
|
16
|
+
export type HttpRequestQuery = {
|
|
7
17
|
[name: string]: string;
|
|
8
18
|
};
|
|
9
|
-
export
|
|
19
|
+
export type HttpRequestResolvedParameters = {
|
|
10
20
|
[name: string]: any;
|
|
11
21
|
};
|
|
22
|
+
export type HttpRequestPositionedParameters = {
|
|
23
|
+
arguments: any[];
|
|
24
|
+
parameters: HttpRequestResolvedParameters;
|
|
25
|
+
};
|
|
26
|
+
export declare class BodyValidationError {
|
|
27
|
+
readonly errors: ValidationErrorItem[];
|
|
28
|
+
constructor(errors?: ValidationErrorItem[]);
|
|
29
|
+
hasErrors(prefix?: string): boolean;
|
|
30
|
+
getErrors(prefix?: string): ValidationErrorItem[];
|
|
31
|
+
getErrorsForPath(path: string): ValidationErrorItem[];
|
|
32
|
+
getErrorMessageForPath(path: string): string;
|
|
33
|
+
}
|
|
34
|
+
export declare class ValidatedBody<T> {
|
|
35
|
+
error: BodyValidationError;
|
|
36
|
+
value?: T | undefined;
|
|
37
|
+
constructor(error: BodyValidationError, value?: T | undefined);
|
|
38
|
+
valid(): this is {
|
|
39
|
+
value: T;
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Marks a parameter as HTTP body and reads the value from the request body.
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* ```typescript
|
|
47
|
+
* class Controller {
|
|
48
|
+
* @http.GET('/api')
|
|
49
|
+
* route(body: HttpBody<{username: string}>) {
|
|
50
|
+
* //body is {username: string} and required
|
|
51
|
+
* //could also reference an interface or type alias via `HttpBody<MyInterface>`
|
|
52
|
+
* }
|
|
53
|
+
* }
|
|
54
|
+
*
|
|
55
|
+
* // curl /api -d '{"username":"Peter"}'
|
|
56
|
+
*/
|
|
57
|
+
export type HttpBody<T> = T & TypeAnnotation<'httpBody'>;
|
|
58
|
+
export type HttpBodyValidation<T> = ValidatedBody<T> & TypeAnnotation<'httpBodyValidation'>;
|
|
59
|
+
export interface HttpRequestParserOptions {
|
|
60
|
+
withPath?: boolean;
|
|
61
|
+
withBody?: boolean;
|
|
62
|
+
withQuery?: boolean;
|
|
63
|
+
withHeader?: boolean;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Delays the parsing of the path/body/query/header to the very last moment, when the parameter is actually used.
|
|
67
|
+
*
|
|
68
|
+
* If no options are provided, the parser will receive data from path, header, body, and query, in this order.
|
|
69
|
+
* This basically allows to fetch data from all possible HTTP sources in one go.
|
|
70
|
+
*
|
|
71
|
+
* You can disable various sources by providing the options, e.g. `{withBody: false}` to disable body parsing.
|
|
72
|
+
* Or `{withQuery: false}` to disable query parsing. Or `{withHeader: false}` to disable header parsing.
|
|
73
|
+
* To only parse the body, use `{withQuery: false, withHeader: false}`.
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* ```typescript
|
|
77
|
+
* async route(parser: HttpRequestParser<{authorization: string}>) {
|
|
78
|
+
* const data = await parser();
|
|
79
|
+
* console.log(data.authorization);
|
|
80
|
+
* }
|
|
81
|
+
* ```
|
|
82
|
+
*
|
|
83
|
+
* Note that the parsers is based on all defined parameters (e.g. `userId: HttpQuery<string>` => {userId: string}),
|
|
84
|
+
* and then starts from there applying header, body, and then query values.
|
|
85
|
+
* This means you also get access to defined path parameters, like:
|
|
86
|
+
*
|
|
87
|
+
* ```typescript
|
|
88
|
+
* @http.GET('teams/:teamId')
|
|
89
|
+
* async route(teamId: string) {
|
|
90
|
+
* //teamId is string
|
|
91
|
+
* }
|
|
92
|
+
*
|
|
93
|
+
* httpWorkflow.onController.listen((event, parser: HttpRequestParser<{teamId: string}>) => {
|
|
94
|
+
* const data = await parser();
|
|
95
|
+
* console.log(data.teamId);
|
|
96
|
+
* });
|
|
97
|
+
* ```
|
|
98
|
+
*
|
|
99
|
+
* HttpRequestParser is necessary in event listeners, since they are instantiated synchronously,
|
|
100
|
+
* but body is parsed asynchronously. So use in event listeners HttpRequestParser instead of HttpBody.
|
|
101
|
+
*/
|
|
102
|
+
export type HttpRequestParser<T> = ((options?: HttpRequestParserOptions) => Promise<T>) & TypeAnnotation<'httpRequestParser', T>;
|
|
103
|
+
/**
|
|
104
|
+
* Marks a parameter as HTTP path and reads the value from the request path.
|
|
105
|
+
* This is normally not requires since the parameter name automatically maps to the path parameter,
|
|
106
|
+
* but in case of requesting the path parameter in for example listeners, this is required.
|
|
107
|
+
* The name option can be used to change the parameter name.
|
|
108
|
+
*
|
|
109
|
+
* @example
|
|
110
|
+
* ```typescript
|
|
111
|
+
* app.listen(httpWorkflow.onController, (event, request: HttpRequest, groupId: HttpPath<number>) => {
|
|
112
|
+
* console.log(groupId); //123
|
|
113
|
+
* });
|
|
114
|
+
*
|
|
115
|
+
* class Controller {
|
|
116
|
+
* @http.GET('/api/:groupId')
|
|
117
|
+
* route(groupId: number) {
|
|
118
|
+
* //groupId is number and required
|
|
119
|
+
* //same as `groupId: HttpPath<number>`
|
|
120
|
+
* }
|
|
121
|
+
* }
|
|
122
|
+
* ```
|
|
123
|
+
*/
|
|
124
|
+
export type HttpPath<T, Options extends {
|
|
125
|
+
name?: string;
|
|
126
|
+
} = {}> = T & TypeAnnotation<'httpPath', Options>;
|
|
127
|
+
/**
|
|
128
|
+
* Marks a parameter as HTTP header and reads the value from the request header.
|
|
129
|
+
*
|
|
130
|
+
* @example
|
|
131
|
+
* ```typescript
|
|
132
|
+
* class Controller {
|
|
133
|
+
* @http.GET('/api')
|
|
134
|
+
* route(authorization: HttpHeader<string>) {
|
|
135
|
+
* //authorization is string and required
|
|
136
|
+
* //use `authorization?: HttpHeader<string>` to make it optional
|
|
137
|
+
* }
|
|
138
|
+
* }
|
|
139
|
+
*
|
|
140
|
+
* // curl /api -H 'Authorization: 123'
|
|
141
|
+
* ```
|
|
142
|
+
*
|
|
143
|
+
* To change the header name, use `param: HttpHeader<string, {name: 'X-My-Header'}>`.
|
|
144
|
+
*/
|
|
145
|
+
export type HttpHeader<T, Options extends {
|
|
146
|
+
name?: string;
|
|
147
|
+
} = {}> = T & TypeAnnotation<'httpHeader', Options>;
|
|
148
|
+
/**
|
|
149
|
+
* Marks a parameter as HTTP query and reads the value from the request query string.
|
|
150
|
+
*
|
|
151
|
+
* @example
|
|
152
|
+
* ```typescript
|
|
153
|
+
* class Controller {
|
|
154
|
+
* @http.GET('/api')
|
|
155
|
+
* route(limit: HttpQuery<number>) {
|
|
156
|
+
* //limit is number and required
|
|
157
|
+
* //use `limit?: HttpQuery<number>` to make it optional
|
|
158
|
+
* }
|
|
159
|
+
* }
|
|
160
|
+
*
|
|
161
|
+
* // curl /api?limit=10
|
|
162
|
+
* ```
|
|
163
|
+
*/
|
|
164
|
+
export type HttpQuery<T, Options extends {
|
|
165
|
+
name?: string;
|
|
166
|
+
} = {}> = T & TypeAnnotation<'httpQuery', Options>;
|
|
167
|
+
/**
|
|
168
|
+
* Marks a parameter as HTTP query objects and reads multiple values from the request query string into an object.
|
|
169
|
+
*
|
|
170
|
+
* @example
|
|
171
|
+
* ```typescript
|
|
172
|
+
* interface Query {
|
|
173
|
+
* limit?: number;
|
|
174
|
+
* offset?: number;
|
|
175
|
+
* sort?: 'asc' | 'desc';
|
|
176
|
+
* }
|
|
177
|
+
*
|
|
178
|
+
* class Controller {
|
|
179
|
+
* @http.GET('/api')
|
|
180
|
+
* route(query: HttpQueries<Query>) {
|
|
181
|
+
* //query is an object containing limit, offset, and sort
|
|
182
|
+
* }
|
|
183
|
+
*
|
|
184
|
+
* // curl /api?limit=10&offset=20&sort=asc
|
|
185
|
+
*/
|
|
186
|
+
export type HttpQueries<T, Options extends {
|
|
187
|
+
name?: string;
|
|
188
|
+
} = {}> = T & TypeAnnotation<'httpQueries', Options>;
|
|
189
|
+
/**
|
|
190
|
+
* For all parameters used in the URL path, a regular expression of /[^/]+/ is used. To change that, use getRegExp.
|
|
191
|
+
*
|
|
192
|
+
* @example
|
|
193
|
+
* ```typescript
|
|
194
|
+
*
|
|
195
|
+
* class Controller {
|
|
196
|
+
* @http.GET('/user/:username')
|
|
197
|
+
* route(username: HttpRegExp<string, '.*'>) {
|
|
198
|
+
* //username is string
|
|
199
|
+
* }
|
|
200
|
+
* }
|
|
201
|
+
* ```
|
|
202
|
+
*/
|
|
203
|
+
export type HttpRegExp<T, Pattern extends string | RegExp> = T & TypeAnnotation<'httpRegExp', Pattern>;
|
|
204
|
+
export declare function getRegExp(type: Type): string | RegExp | undefined;
|
|
205
|
+
export declare class RequestBuilder {
|
|
206
|
+
protected path: string;
|
|
207
|
+
protected method: string;
|
|
208
|
+
protected contentBuffer: Buffer;
|
|
209
|
+
protected _headers: {
|
|
210
|
+
[name: string]: string;
|
|
211
|
+
};
|
|
212
|
+
protected queryPath?: string;
|
|
213
|
+
constructor(path: string, method?: string);
|
|
214
|
+
getUrl(): string;
|
|
215
|
+
build(): HttpRequest;
|
|
216
|
+
headers(headers: {
|
|
217
|
+
[name: string]: string;
|
|
218
|
+
}): this;
|
|
219
|
+
header(name: string, value: string | number): this;
|
|
220
|
+
json(body: object): this;
|
|
221
|
+
multiPart(items: {
|
|
222
|
+
name: string;
|
|
223
|
+
file?: Uint8Array;
|
|
224
|
+
fileName?: string;
|
|
225
|
+
json?: any;
|
|
226
|
+
}[]): this;
|
|
227
|
+
body(body: string | Buffer): this;
|
|
228
|
+
query(query: any | string): this;
|
|
229
|
+
}
|
|
12
230
|
export declare class HttpRequest extends IncomingMessage {
|
|
13
231
|
/**
|
|
14
232
|
* A store that can be used to transport data from guards/listeners to ParameterResolvers/controllers.
|
|
@@ -16,7 +234,55 @@ export declare class HttpRequest extends IncomingMessage {
|
|
|
16
234
|
store: {
|
|
17
235
|
[name: string]: any;
|
|
18
236
|
};
|
|
237
|
+
uploadedFiles: {
|
|
238
|
+
[name: string]: UploadedFile;
|
|
239
|
+
};
|
|
240
|
+
throwErrorOnNotFound: boolean;
|
|
241
|
+
/**
|
|
242
|
+
* The router sets the body when it was read.
|
|
243
|
+
*/
|
|
244
|
+
body?: Buffer;
|
|
245
|
+
readBody(): Promise<Buffer>;
|
|
246
|
+
readBodyText(): Promise<string>;
|
|
247
|
+
static GET(path: string): RequestBuilder;
|
|
248
|
+
static POST(path: string): RequestBuilder;
|
|
249
|
+
static OPTIONS(path: string): RequestBuilder;
|
|
250
|
+
static TRACE(path: string): RequestBuilder;
|
|
251
|
+
static HEAD(path: string): RequestBuilder;
|
|
252
|
+
static PATCH(path: string): RequestBuilder;
|
|
253
|
+
static PUT(path: string): RequestBuilder;
|
|
254
|
+
static DELETE(path: string): RequestBuilder;
|
|
19
255
|
getUrl(): string;
|
|
20
256
|
getMethod(): string;
|
|
21
257
|
getRemoteAddress(): string;
|
|
22
258
|
}
|
|
259
|
+
export declare function incomingMessageToHttpRequest(request: IncomingMessage): HttpRequest;
|
|
260
|
+
export declare function serverResponseToHttpResponse(response: ServerResponse): HttpResponse;
|
|
261
|
+
export declare class MemoryHttpResponse extends HttpResponse {
|
|
262
|
+
body: Buffer;
|
|
263
|
+
headers: {
|
|
264
|
+
[name: string]: number | string | string[] | undefined;
|
|
265
|
+
};
|
|
266
|
+
setHeader(name: string, value: number | string | ReadonlyArray<string>): this;
|
|
267
|
+
removeHeader(name: string): void;
|
|
268
|
+
getHeader(name: string): string | number | string[] | undefined;
|
|
269
|
+
getHeaders(): OutgoingHttpHeaders;
|
|
270
|
+
writeHead(statusCode: number, headersOrReasonPhrase?: string | OutgoingHttpHeaders | OutgoingHttpHeader[], headers?: OutgoingHttpHeaders | OutgoingHttpHeader[]): this;
|
|
271
|
+
get json(): any;
|
|
272
|
+
get text(): string;
|
|
273
|
+
get bodyString(): string;
|
|
274
|
+
write(chunk: any, encoding: any, callback?: any): any;
|
|
275
|
+
end(chunk: any, encoding?: any, callback?: any): any;
|
|
276
|
+
}
|
|
277
|
+
export declare type __ΩHttpRequestQuery = any[];
|
|
278
|
+
export declare type __ΩHttpRequestResolvedParameters = any[];
|
|
279
|
+
export declare type __ΩHttpRequestPositionedParameters = any[];
|
|
280
|
+
export declare type __ΩHttpBody = any[];
|
|
281
|
+
export declare type __ΩHttpBodyValidation = any[];
|
|
282
|
+
export declare type __ΩHttpRequestParserOptions = any[];
|
|
283
|
+
export declare type __ΩHttpRequestParser = any[];
|
|
284
|
+
export declare type __ΩHttpPath = any[];
|
|
285
|
+
export declare type __ΩHttpHeader = any[];
|
|
286
|
+
export declare type __ΩHttpQuery = any[];
|
|
287
|
+
export declare type __ΩHttpQueries = any[];
|
|
288
|
+
export declare type __ΩHttpRegExp = any[];
|
package/dist/esm/src/model.js
CHANGED
|
@@ -1,3 +1,13 @@
|
|
|
1
|
+
const __ΩError = ['name', 'message', 'stack', 'Error', 'P&4!&4"&4#8Mw$y'];
|
|
2
|
+
const __ΩPartial = ['T', 'Partial', 'l+e#!e"!fRb!Pde"!gN#"w"y'];
|
|
3
|
+
const __ΩReadonlyArray = ['T', 'length', 'toString', 'toLocaleString', () => __ΩConcatArray, 'items', 'concat', () => __ΩConcatArray, 'separator', 'join', 'start', 'end', 'slice', 'searchElement', 'fromIndex', 'indexOf', 'lastIndexOf', 'value', 'index', 'array', '', 'predicate', 'thisArg', 'every', 'some', 'callbackfn', 'forEach', 'map', 'filter', 'previousValue', 'currentValue', 'currentIndex', 'reduce', 'initialValue', 'reduceRight', 'ReadonlyArray', 'b!P\'4"9P&1#P&1$Pe#!o%"@2&e#!F1\'PPe$!e$!o("J@2&e#!F1\'P&2)8&1*P\'2+8\'2,8e#!F1-Pe#!2.\'2/8\'10Pe#!2.\'2/8\'11PPe$!22\'23e$!F924!/526"278!18PPe$!22\'23e$!F924#/526"278)18PPe$!22\'23e$!F924#/526"278)19PPe$!22\'23e$!F924$/52:"278$1;PPe$!22\'23e$!F924"/52:"278"F1<PPe$!22\'23e$!F924!/526"278"F1=PPe$!22\'23e$!F924#/526"278e#!F1=PPe$!2>e$!2?\'2@e$!F924e$!/52:e#!1APPe$!2>e$!2?\'2@e$!F924e$!/52:e#!2Be#!1APP"2>e$!2?\'2@e$!F924"/52:"2B"1APPe$!2>e$!2?\'2@e$!F924e$!/52:e#!1CPPe$!2>e$!2?\'2@e$!F924e$!/52:e#!2Be#!1CPP"2>e$!2?\'2@e$!F924"/52:"2B"1C\'e"!LMwDy'];
|
|
4
|
+
const __ΩConcatArray = ['T', 'length', 'separator', 'join', 'start', 'end', 'slice', 'ConcatArray', 'b!P\'4"9\'e"!LP&2#8&1$P\'2%8\'2&8e#!F1\'Mw(y'];
|
|
5
|
+
/*@ts-ignore*/
|
|
6
|
+
import { __ΩTypeAnnotation } from '@deepkit/core';
|
|
7
|
+
function __assignType(fn, args) {
|
|
8
|
+
fn.__type = args;
|
|
9
|
+
return fn;
|
|
10
|
+
}
|
|
1
11
|
/*
|
|
2
12
|
* Deepkit Framework
|
|
3
13
|
* Copyright (C) 2021 Deepkit UG, Marc J. Schmidt
|
|
@@ -8,12 +18,225 @@
|
|
|
8
18
|
* You should have received a copy of the MIT License along with this program.
|
|
9
19
|
*/
|
|
10
20
|
import { IncomingMessage, ServerResponse } from 'http';
|
|
21
|
+
import { UploadedFile } from './router.js';
|
|
22
|
+
import * as querystring from 'querystring';
|
|
23
|
+
import { Writable } from 'stream';
|
|
24
|
+
import { ReflectionKind, typeAnnotation, ValidationErrorItem } from '@deepkit/type';
|
|
25
|
+
import { asyncOperation, isArray } from '@deepkit/core';
|
|
11
26
|
export class HttpResponse extends ServerResponse {
|
|
12
27
|
status(code) {
|
|
13
28
|
this.writeHead(code);
|
|
14
29
|
this.end();
|
|
15
30
|
}
|
|
16
31
|
}
|
|
32
|
+
HttpResponse.__type = [() => ServerResponse, 'code', 'status', 'HttpResponse', 'P7!P\'2""0#5w$'];
|
|
33
|
+
/**
|
|
34
|
+
* Reads the body of the request and returns it as a Buffer.
|
|
35
|
+
* The result will be cached in the request object as `request.body`.
|
|
36
|
+
*
|
|
37
|
+
* Deepkit's router will automatically use `request.body` if available.
|
|
38
|
+
*/
|
|
39
|
+
export function readBody(request) {
|
|
40
|
+
return asyncOperation(__assignType((resolve, reject) => {
|
|
41
|
+
const chunks = [];
|
|
42
|
+
request.on('data', __assignType((chunk) => {
|
|
43
|
+
chunks.push(chunk);
|
|
44
|
+
}, ['chunk', '', 'P!2!"/"']));
|
|
45
|
+
request.on('end', () => {
|
|
46
|
+
const body = Buffer.concat(chunks);
|
|
47
|
+
//the router uses this now instead of trying to read the body again
|
|
48
|
+
request.body = body;
|
|
49
|
+
resolve(body);
|
|
50
|
+
});
|
|
51
|
+
request.on('error', __assignType((error) => {
|
|
52
|
+
reject(error);
|
|
53
|
+
}, [() => __ΩError, 'error', '', 'Pn!2""/#']));
|
|
54
|
+
}, ['resolve', 'reject', '', 'P"2!"2""/#']));
|
|
55
|
+
}
|
|
56
|
+
readBody.__type = ['request', 'readBody', 'P!2!!`/"'];
|
|
57
|
+
export function createRequestWithCachedBody(request, body) {
|
|
58
|
+
var _a;
|
|
59
|
+
const headers = request.headers;
|
|
60
|
+
const method = request.method;
|
|
61
|
+
const url = request.url;
|
|
62
|
+
const writable = new Writable({
|
|
63
|
+
write: __assignType(function write(chunk, encoding, callback) {
|
|
64
|
+
callback();
|
|
65
|
+
}, ['chunk', 'encoding', 'callback', 'write', 'P"2!"2""2#"/$']),
|
|
66
|
+
writev: __assignType(function writev(chunks, callback) {
|
|
67
|
+
callback();
|
|
68
|
+
}, ['chunks', 'callback', 'writev', 'P"2!"2""/#'])
|
|
69
|
+
});
|
|
70
|
+
return new (_a = class extends HttpRequest {
|
|
71
|
+
constructor() {
|
|
72
|
+
super(...arguments);
|
|
73
|
+
this.url = url;
|
|
74
|
+
this.method = method;
|
|
75
|
+
this.position = 0;
|
|
76
|
+
this.headers = headers;
|
|
77
|
+
}
|
|
78
|
+
_read(size) {
|
|
79
|
+
if (this.complete) {
|
|
80
|
+
this.push(null);
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
//in Node 18 this seems to be necessary to not trigger the abort event
|
|
84
|
+
this.complete = true;
|
|
85
|
+
this.push(body);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
},
|
|
89
|
+
_a.__type = [() => HttpRequest, 'url', function () { return url; }, 'method', function () { return method; }, 'position', function () { return 0; }, 'headers', function () { return headers; }, 'size', '_read', 'P7!!3">#!3$>%\'3&>\'!3(>)P\'2*"0+5'],
|
|
90
|
+
_a)(writable);
|
|
91
|
+
}
|
|
92
|
+
createRequestWithCachedBody.__type = [() => __ΩPartial, 'request', 'body', () => HttpRequest, 'createRequestWithCachedBody', 'P!o!"2"!2#P7$/%'];
|
|
93
|
+
const __ΩHttpRequestQuery = ['HttpRequestQuery', 'P&&LMw!y'];
|
|
94
|
+
export { __ΩHttpRequestQuery as __ΩHttpRequestQuery };
|
|
95
|
+
const __ΩHttpRequestResolvedParameters = ['HttpRequestResolvedParameters', 'P&"LMw!y'];
|
|
96
|
+
export { __ΩHttpRequestResolvedParameters as __ΩHttpRequestResolvedParameters };
|
|
97
|
+
const __ΩHttpRequestPositionedParameters = ['arguments', () => __ΩHttpRequestResolvedParameters, 'parameters', 'HttpRequestPositionedParameters', 'P"F4!n"4#Mw$y'];
|
|
98
|
+
export { __ΩHttpRequestPositionedParameters as __ΩHttpRequestPositionedParameters };
|
|
99
|
+
export class BodyValidationError {
|
|
100
|
+
constructor(errors = []) {
|
|
101
|
+
this.errors = errors;
|
|
102
|
+
}
|
|
103
|
+
hasErrors(prefix) {
|
|
104
|
+
return this.getErrors(prefix).length > 0;
|
|
105
|
+
}
|
|
106
|
+
getErrors(prefix) {
|
|
107
|
+
if (prefix)
|
|
108
|
+
return this.errors.filter(__assignType(v => v.path.startsWith(prefix), ['v', '', 'P"2!"/"']));
|
|
109
|
+
return this.errors;
|
|
110
|
+
}
|
|
111
|
+
getErrorsForPath(path) {
|
|
112
|
+
return this.errors.filter(__assignType(v => v.path === path, ['v', '', 'P"2!"/"']));
|
|
113
|
+
}
|
|
114
|
+
getErrorMessageForPath(path) {
|
|
115
|
+
return this.getErrorsForPath(path).map(__assignType(v => v.toString(), ['v', '', 'P"2!"/"'])).join(', ');
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
BodyValidationError.__type = [() => ValidationErrorItem, 'errors', () => [], 'constructor', 'prefix', 'hasErrors', () => ValidationErrorItem, 'getErrors', 'path', () => ValidationErrorItem, 'getErrorsForPath', 'getErrorMessageForPath', 'BodyValidationError', 'PP7!F2":9>#"0$P&2%8)0&P&2%8P7\'F0(P&2)P7*F0+P&2)&0,5w-'];
|
|
119
|
+
export class ValidatedBody {
|
|
120
|
+
constructor(error, value) {
|
|
121
|
+
this.error = error;
|
|
122
|
+
this.value = value;
|
|
123
|
+
}
|
|
124
|
+
valid() {
|
|
125
|
+
return this.value !== undefined;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
ValidatedBody.__type = ['T', () => BodyValidationError, 'error', 'value', 'constructor', 'valid', 'ValidatedBody', 'b!PP7"2#:e"!2$8:"0%P!0&5w\''];
|
|
129
|
+
const __ΩHttpBody = ['T', () => __ΩTypeAnnotation, "httpBody", 'HttpBody', 'b!Pe"!.#o""Kw$y'];
|
|
130
|
+
export { __ΩHttpBody as __ΩHttpBody };
|
|
131
|
+
const __ΩHttpBodyValidation = ['T', () => ValidatedBody, () => __ΩTypeAnnotation, "httpBodyValidation", 'HttpBodyValidation', 'b!PPe#!7".$o#"Kw%y'];
|
|
132
|
+
export { __ΩHttpBodyValidation as __ΩHttpBodyValidation };
|
|
133
|
+
const __ΩHttpRequestParserOptions = ['withPath', 'withBody', 'withQuery', 'withHeader', 'HttpRequestParserOptions', 'P)4!8)4"8)4#8)4$8Mw%y'];
|
|
134
|
+
export { __ΩHttpRequestParserOptions as __ΩHttpRequestParserOptions };
|
|
135
|
+
const __ΩHttpRequestParser = ['T', () => __ΩHttpRequestParserOptions, 'options', '', () => __ΩTypeAnnotation, "httpRequestParser", 'HttpRequestParser', 'b!PPn"2#8e#!`/$.&e"!o%#Kw\'y'];
|
|
136
|
+
export { __ΩHttpRequestParser as __ΩHttpRequestParser };
|
|
137
|
+
const __ΩHttpPath = ['T', 'Options', () => __ΩTypeAnnotation, "httpPath", 'HttpPath', 'b!PMc"Pe"!.$e""o##Kw%y'];
|
|
138
|
+
export { __ΩHttpPath as __ΩHttpPath };
|
|
139
|
+
const __ΩHttpHeader = ['T', 'Options', () => __ΩTypeAnnotation, "httpHeader", 'HttpHeader', 'b!PMc"Pe"!.$e""o##Kw%y'];
|
|
140
|
+
export { __ΩHttpHeader as __ΩHttpHeader };
|
|
141
|
+
const __ΩHttpQuery = ['T', 'Options', () => __ΩTypeAnnotation, "httpQuery", 'HttpQuery', 'b!PMc"Pe"!.$e""o##Kw%y'];
|
|
142
|
+
export { __ΩHttpQuery as __ΩHttpQuery };
|
|
143
|
+
const __ΩHttpQueries = ['T', 'Options', () => __ΩTypeAnnotation, "httpQueries", 'HttpQueries', 'b!PMc"Pe"!.$e""o##Kw%y'];
|
|
144
|
+
export { __ΩHttpQueries as __ΩHttpQueries };
|
|
145
|
+
const __ΩHttpRegExp = ['T', 'Pattern', () => __ΩTypeAnnotation, "httpRegExp", 'HttpRegExp', 'b!b"Pe"!.$e""o##Kw%y'];
|
|
146
|
+
export { __ΩHttpRegExp as __ΩHttpRegExp };
|
|
147
|
+
export function getRegExp(type) {
|
|
148
|
+
const options = typeAnnotation.getType(type, 'httpRegExp');
|
|
149
|
+
if (!options)
|
|
150
|
+
return;
|
|
151
|
+
if (options.kind === ReflectionKind.literal && 'string' === typeof options.literal)
|
|
152
|
+
return options.literal;
|
|
153
|
+
if (options.kind === ReflectionKind.literal && options.literal instanceof RegExp)
|
|
154
|
+
return options.literal;
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
getRegExp.__type = ['Type', 'type', 'getRegExp', 'P"w!2"P&A-J/#'];
|
|
158
|
+
export class RequestBuilder {
|
|
159
|
+
constructor(path, method = 'GET') {
|
|
160
|
+
this.path = path;
|
|
161
|
+
this.method = method;
|
|
162
|
+
this.contentBuffer = Buffer.alloc(0);
|
|
163
|
+
this._headers = {};
|
|
164
|
+
}
|
|
165
|
+
getUrl() {
|
|
166
|
+
if (this.queryPath) {
|
|
167
|
+
return this.path + '?' + this.queryPath;
|
|
168
|
+
}
|
|
169
|
+
return this.path;
|
|
170
|
+
}
|
|
171
|
+
build() {
|
|
172
|
+
return createRequestWithCachedBody({
|
|
173
|
+
method: this.method,
|
|
174
|
+
url: this.getUrl(),
|
|
175
|
+
headers: this._headers,
|
|
176
|
+
}, this.contentBuffer);
|
|
177
|
+
}
|
|
178
|
+
headers(headers) {
|
|
179
|
+
this._headers = headers;
|
|
180
|
+
return this;
|
|
181
|
+
}
|
|
182
|
+
header(name, value) {
|
|
183
|
+
this._headers[name] = String(value);
|
|
184
|
+
return this;
|
|
185
|
+
}
|
|
186
|
+
json(body) {
|
|
187
|
+
this.contentBuffer = Buffer.from(JSON.stringify(body), 'utf8');
|
|
188
|
+
this._headers['content-type'] = 'application/json; charset=utf-8';
|
|
189
|
+
this._headers['content-length'] = String(this.contentBuffer.byteLength);
|
|
190
|
+
return this;
|
|
191
|
+
}
|
|
192
|
+
multiPart(items) {
|
|
193
|
+
const boundary = '--------------------------' + Math.random().toString(36).substr(2, 10);
|
|
194
|
+
this._headers['content-type'] = 'multipart/form-data; boundary=' + boundary;
|
|
195
|
+
const parts = items.map(__assignType(item => {
|
|
196
|
+
if (item.file) {
|
|
197
|
+
const header = Buffer.from(`--${boundary}\r
|
|
198
|
+
Content-Disposition: form-data; name="${item.name}"; filename="${item.fileName || 'file'}"\r
|
|
199
|
+
Content-Type: application/octet-stream\r
|
|
200
|
+
\r\n`, 'utf8');
|
|
201
|
+
return Buffer.concat([header, item.file, Buffer.from('\r\n', 'utf8')]);
|
|
202
|
+
}
|
|
203
|
+
else if (item.json) {
|
|
204
|
+
const header = Buffer.from(`--${boundary}\r
|
|
205
|
+
Content-Disposition: form-data; name="${item.name}"\r
|
|
206
|
+
Content-Type: application/json\r
|
|
207
|
+
\r\n`, 'utf8');
|
|
208
|
+
return Buffer.concat([header, Buffer.from(JSON.stringify(item.json) + '\r\n', 'utf8')]);
|
|
209
|
+
}
|
|
210
|
+
else {
|
|
211
|
+
throw new Error('Invalid multiPart item');
|
|
212
|
+
}
|
|
213
|
+
}, ['item', '', 'P"2!"/"']));
|
|
214
|
+
parts.push(Buffer.from(`--${boundary}--`, 'utf8'));
|
|
215
|
+
this.contentBuffer = Buffer.concat(parts);
|
|
216
|
+
this._headers['content-length'] = String(this.contentBuffer.byteLength);
|
|
217
|
+
return this;
|
|
218
|
+
}
|
|
219
|
+
body(body) {
|
|
220
|
+
if ('string' === typeof body) {
|
|
221
|
+
this.contentBuffer = Buffer.from(body, 'utf8');
|
|
222
|
+
}
|
|
223
|
+
else {
|
|
224
|
+
this.contentBuffer = body;
|
|
225
|
+
}
|
|
226
|
+
this._headers['content-length'] = String(this.contentBuffer.byteLength);
|
|
227
|
+
return this;
|
|
228
|
+
}
|
|
229
|
+
query(query) {
|
|
230
|
+
if ('string' === typeof query) {
|
|
231
|
+
this.queryPath = query;
|
|
232
|
+
}
|
|
233
|
+
else {
|
|
234
|
+
this.queryPath = querystring.stringify(query);
|
|
235
|
+
}
|
|
236
|
+
return this;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
RequestBuilder.__type = ['contentBuffer', function () { return Buffer.alloc(0); }, '_headers', function () { return {}; }, 'queryPath', 'path', 'method', () => "GET", 'constructor', 'getUrl', () => HttpRequest, 'build', 'headers', 'name', 'value', 'header', 'body', 'json', 'file', 'fileName', 'items', 'multiPart', 'query', 'RequestBuilder', '!3!<>"P&&LM3#<>$&3%8<P&2&<&2\'<>("0)P"0*PP7+0,PP&&LM2-!0-P&2.P&\'J2/!00P%21!02PP&4.W438&448"428MF25!06PP&!J21!01PP"&J27!075w8'];
|
|
17
240
|
export class HttpRequest extends IncomingMessage {
|
|
18
241
|
constructor() {
|
|
19
242
|
super(...arguments);
|
|
@@ -21,6 +244,40 @@ export class HttpRequest extends IncomingMessage {
|
|
|
21
244
|
* A store that can be used to transport data from guards/listeners to ParameterResolvers/controllers.
|
|
22
245
|
*/
|
|
23
246
|
this.store = {};
|
|
247
|
+
this.uploadedFiles = {};
|
|
248
|
+
this.throwErrorOnNotFound = false;
|
|
249
|
+
}
|
|
250
|
+
async readBody() {
|
|
251
|
+
if (this.body)
|
|
252
|
+
return this.body;
|
|
253
|
+
return await readBody(this);
|
|
254
|
+
}
|
|
255
|
+
async readBodyText() {
|
|
256
|
+
return (await this.readBody()).toString('utf8');
|
|
257
|
+
}
|
|
258
|
+
static GET(path) {
|
|
259
|
+
return new RequestBuilder(path);
|
|
260
|
+
}
|
|
261
|
+
static POST(path) {
|
|
262
|
+
return new RequestBuilder(path, 'POST');
|
|
263
|
+
}
|
|
264
|
+
static OPTIONS(path) {
|
|
265
|
+
return new RequestBuilder(path, 'OPTIONS');
|
|
266
|
+
}
|
|
267
|
+
static TRACE(path) {
|
|
268
|
+
return new RequestBuilder(path, 'TRACE');
|
|
269
|
+
}
|
|
270
|
+
static HEAD(path) {
|
|
271
|
+
return new RequestBuilder(path, 'HEAD');
|
|
272
|
+
}
|
|
273
|
+
static PATCH(path) {
|
|
274
|
+
return new RequestBuilder(path, 'PATCH');
|
|
275
|
+
}
|
|
276
|
+
static PUT(path) {
|
|
277
|
+
return new RequestBuilder(path, 'PUT');
|
|
278
|
+
}
|
|
279
|
+
static DELETE(path) {
|
|
280
|
+
return new RequestBuilder(path, 'DELETE');
|
|
24
281
|
}
|
|
25
282
|
getUrl() {
|
|
26
283
|
return this.url || '/';
|
|
@@ -29,7 +286,106 @@ export class HttpRequest extends IncomingMessage {
|
|
|
29
286
|
return this.method || 'GET';
|
|
30
287
|
}
|
|
31
288
|
getRemoteAddress() {
|
|
32
|
-
return this.
|
|
289
|
+
return this.socket.remoteAddress || '';
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
HttpRequest.__type = [() => IncomingMessage, 'store', function () { return {}; }, () => UploadedFile, 'uploadedFiles', function () { return {}; }, 'throwErrorOnNotFound', function () { return false; }, 'body', 'readBody', 'readBodyText', 'path', () => RequestBuilder, 'GET', () => RequestBuilder, 'POST', () => RequestBuilder, 'OPTIONS', () => RequestBuilder, 'TRACE', () => RequestBuilder, 'HEAD', () => RequestBuilder, 'PATCH', () => RequestBuilder, 'PUT', () => RequestBuilder, 'DELETE', 'getUrl', 'getMethod', 'getRemoteAddress', 'HttpRequest', 'P7!P&"LM3">#P&P7$LM3%>&)3\'>(!3)8P!`0*P&`0+P&2,P7-0.sP&2,P7/00sP&2,P7102sP&2,P7304sP&2,P7506sP&2,P7708sP&2,P790:sP&2,P7;0<sP&0=P&0>P&0?5w@'];
|
|
293
|
+
export function incomingMessageToHttpRequest(request) {
|
|
294
|
+
if (request instanceof HttpRequest)
|
|
295
|
+
return request;
|
|
296
|
+
Object.setPrototypeOf(request, HttpRequest.prototype);
|
|
297
|
+
HttpRequest.constructor.call(request);
|
|
298
|
+
return request;
|
|
299
|
+
}
|
|
300
|
+
incomingMessageToHttpRequest.__type = ['request', () => HttpRequest, 'incomingMessageToHttpRequest', 'P!2!P7"/#'];
|
|
301
|
+
export function serverResponseToHttpResponse(response) {
|
|
302
|
+
if (response instanceof HttpResponse)
|
|
303
|
+
return response;
|
|
304
|
+
Object.setPrototypeOf(response, HttpResponse.prototype);
|
|
305
|
+
MemoryHttpResponse.constructor.call(response);
|
|
306
|
+
return response;
|
|
307
|
+
}
|
|
308
|
+
serverResponseToHttpResponse.__type = ['response', () => HttpResponse, 'serverResponseToHttpResponse', 'P!2!P7"/#'];
|
|
309
|
+
export class MemoryHttpResponse extends HttpResponse {
|
|
310
|
+
constructor() {
|
|
311
|
+
super(...arguments);
|
|
312
|
+
this.body = Buffer.alloc(0);
|
|
313
|
+
this.headers = Object.create(null);
|
|
314
|
+
}
|
|
315
|
+
setHeader(name, value) {
|
|
316
|
+
this.headers[name] = value;
|
|
317
|
+
super.setHeader(name, value);
|
|
318
|
+
return this;
|
|
319
|
+
}
|
|
320
|
+
removeHeader(name) {
|
|
321
|
+
delete this.headers[name];
|
|
322
|
+
super.removeHeader(name);
|
|
323
|
+
}
|
|
324
|
+
getHeader(name) {
|
|
325
|
+
return this.headers[name];
|
|
326
|
+
}
|
|
327
|
+
getHeaders() {
|
|
328
|
+
return this.headers;
|
|
329
|
+
}
|
|
330
|
+
writeHead(statusCode, headersOrReasonPhrase, headers) {
|
|
331
|
+
headers = typeof headersOrReasonPhrase === 'string' ? headers : headersOrReasonPhrase;
|
|
332
|
+
if (headers && !isArray(headers))
|
|
333
|
+
this.headers = headers;
|
|
334
|
+
if (typeof headersOrReasonPhrase === 'string')
|
|
335
|
+
return super.writeHead(statusCode, headersOrReasonPhrase, headers);
|
|
336
|
+
return super.writeHead(statusCode, headers);
|
|
337
|
+
}
|
|
338
|
+
get json() {
|
|
339
|
+
const json = this.bodyString;
|
|
340
|
+
try {
|
|
341
|
+
return JSON.parse(json);
|
|
342
|
+
}
|
|
343
|
+
catch (error) {
|
|
344
|
+
throw new Error(`Could not parse JSON: ${error.message}, body: ${json}`);
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
get text() {
|
|
348
|
+
return this.bodyString;
|
|
349
|
+
}
|
|
350
|
+
get bodyString() {
|
|
351
|
+
return this.body.toString('utf8');
|
|
352
|
+
}
|
|
353
|
+
write(chunk, encoding, callback) {
|
|
354
|
+
if (typeof encoding === 'function') {
|
|
355
|
+
callback = encoding;
|
|
356
|
+
encoding = null;
|
|
357
|
+
}
|
|
358
|
+
if (chunk) {
|
|
359
|
+
if ('string' === typeof chunk) {
|
|
360
|
+
chunk = Buffer.from(chunk, encoding || 'utf8');
|
|
361
|
+
}
|
|
362
|
+
this.body = Buffer.concat([this.body, chunk]);
|
|
363
|
+
}
|
|
364
|
+
if (callback)
|
|
365
|
+
callback();
|
|
366
|
+
return true;
|
|
367
|
+
}
|
|
368
|
+
end(chunk, encoding, callback) {
|
|
369
|
+
if (typeof chunk === 'function') {
|
|
370
|
+
callback = chunk;
|
|
371
|
+
chunk = null;
|
|
372
|
+
encoding = null;
|
|
373
|
+
}
|
|
374
|
+
else if (typeof encoding === 'function') {
|
|
375
|
+
callback = encoding;
|
|
376
|
+
encoding = null;
|
|
377
|
+
}
|
|
378
|
+
if (chunk) {
|
|
379
|
+
if ('string' === typeof chunk) {
|
|
380
|
+
chunk = Buffer.from(chunk, encoding || 'utf8');
|
|
381
|
+
}
|
|
382
|
+
this.body = Buffer.concat([this.body, chunk]);
|
|
383
|
+
}
|
|
384
|
+
this.emit('finish');
|
|
385
|
+
super.end(chunk, encoding, callback);
|
|
386
|
+
this.emit('close');
|
|
387
|
+
return this;
|
|
33
388
|
}
|
|
34
389
|
}
|
|
390
|
+
MemoryHttpResponse.__type = [() => HttpResponse, 'body', function () { return Buffer.alloc(0); }, 'headers', function () { return Object.create(null); }, 'name', () => __ΩReadonlyArray, 'value', 'setHeader', 'removeHeader', 'getHeader', 'getHeaders', 'statusCode', 'headersOrReasonPhrase', 'writeHead', 'chunk', 'encoding', 'callback', 'write', 'end', 'MemoryHttpResponse', 'P7!!3">#P&P\'&&F-JLM3$>%P&2&P\'&&o\'"J2(!0)P&2&"0*P&2&"0+P!0,P\'2-P&!!FJ2.8P!!FJ2$8!0/!!!P"20"21"228"03P"20"218"228"045w5'];
|
|
35
391
|
//# sourceMappingURL=model.js.map
|