@increase21/simplenodejs 1.0.28 → 1.0.30
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/README.md
CHANGED
|
@@ -93,7 +93,7 @@ app.listen(443);
|
|
|
93
93
|
|
|
94
94
|
## Controllers
|
|
95
95
|
|
|
96
|
-
Controllers are auto-loaded from `controllersDir` at startup. The file path maps directly to
|
|
96
|
+
Controllers are auto-loaded from `controllersDir` at startup. The file path maps directly to a URL. Controllers must be exported as the default export; otherwise, requests to that controller will return a 404 error.
|
|
97
97
|
|
|
98
98
|
```
|
|
99
99
|
controllers/
|
|
@@ -271,7 +271,7 @@ Parses the request body. Must be registered before controllers access `this.body
|
|
|
271
271
|
| Param | Type | Description |
|
|
272
272
|
|---|---|---|
|
|
273
273
|
| `limit` | `string \| number` | Max body size (e.g. `"2mb"`, `"500kb"`, or bytes as number). Default: `"1mb"` |
|
|
274
|
-
| `ignoreStream` | `string[] \| (req) => boolean` | Skip stream reading and pass the raw stream to the handler for matching requests. Accepts a list of path prefixes or a predicate function. |
|
|
274
|
+
| `ignoreStream` | `{url:string, method:string, type: exact or prefex}[] \| (req) => boolean` | Skip stream reading and pass the raw stream to the handler for matching requests. Accepts a list of path prefixes and their http menthods or a predicate function. The `type` field determines whether the URL should be matched exactly (`exact`) or as a prefix (`prefix`) |
|
|
275
275
|
|
|
276
276
|
```ts
|
|
277
277
|
app.use(SetBodyParser({ limit: "2mb" }));
|
|
@@ -283,7 +283,7 @@ For context where you need direct stream access (e.g. passing the request to a l
|
|
|
283
283
|
|
|
284
284
|
```ts
|
|
285
285
|
// Path-prefix list — skip body parsing for any URL under /upload
|
|
286
|
-
app.use(SetBodyParser({ limit: "10mb", ignoreStream: ["/upload", "/files"] }));
|
|
286
|
+
app.use(SetBodyParser({ limit: "10mb", ignoreStream: [{url:"/upload", method:"post"}, {url:"/files/profile-picture", method:"post"}] }));
|
|
287
287
|
|
|
288
288
|
// Predicate function — full control over which requests are skipped
|
|
289
289
|
app.use(SetBodyParser({
|
|
@@ -297,7 +297,7 @@ When a request is ignored, `next()` is called immediately with the stream untouc
|
|
|
297
297
|
```ts
|
|
298
298
|
import formidable from "formidable";
|
|
299
299
|
|
|
300
|
-
// Inside your
|
|
300
|
+
// Inside your controller handler
|
|
301
301
|
const form = formidable({ maxTotalFileSize: 10 * 1024 * 1024 });
|
|
302
302
|
form.parse(req, (err, fields, files) => {
|
|
303
303
|
if (err) {
|
|
@@ -6,6 +6,18 @@ export type HttpMethod = "get" | "post" | "put" | "patch" | "delete";
|
|
|
6
6
|
export type ObjectPayload = {
|
|
7
7
|
[key: string]: any;
|
|
8
8
|
};
|
|
9
|
+
export type RequestObject = IncomingMessage & {
|
|
10
|
+
body?: any;
|
|
11
|
+
query?: any;
|
|
12
|
+
id?: string;
|
|
13
|
+
_end_point_path?: string[];
|
|
14
|
+
_custom_data?: ObjectPayload;
|
|
15
|
+
};
|
|
16
|
+
export type ResponseObject = ServerResponse & {
|
|
17
|
+
status: (value: number) => ResponseObject;
|
|
18
|
+
json: (value: object) => void;
|
|
19
|
+
text: (value?: string) => void;
|
|
20
|
+
};
|
|
9
21
|
export type Middleware = (req: RequestObject, res: ResponseObject, next: () => Promise<any> | void) => Promise<any> | void;
|
|
10
22
|
export type ErrorMiddleware = (err: any, req: RequestObject, res: ResponseObject, next: Next) => Promise<boolean> | void;
|
|
11
23
|
export interface SimpleJsServer extends http.Server {
|
|
@@ -18,18 +30,6 @@ export interface SimpleJsHttpsServer extends https.Server {
|
|
|
18
30
|
useError: (mw: ErrorMiddleware) => void;
|
|
19
31
|
registerPlugin: (plugin: Plugin) => Promise<any> | void;
|
|
20
32
|
}
|
|
21
|
-
export interface RequestObject extends IncomingMessage {
|
|
22
|
-
body?: any;
|
|
23
|
-
query?: any;
|
|
24
|
-
id?: string;
|
|
25
|
-
_end_point_path?: string[];
|
|
26
|
-
_custom_data?: ObjectPayload;
|
|
27
|
-
}
|
|
28
|
-
export interface ResponseObject extends ServerResponse {
|
|
29
|
-
status: (value: number) => ResponseObject;
|
|
30
|
-
json: (value: object) => void;
|
|
31
|
-
text: (value?: string) => void;
|
|
32
|
-
}
|
|
33
33
|
export interface SimpleJsPrivateMethodProps {
|
|
34
34
|
body: ObjectPayload;
|
|
35
35
|
res: ResponseObject;
|
|
@@ -11,8 +11,13 @@ export type SimpleJSBodyParseType = {
|
|
|
11
11
|
* Skip stream reading (and pass the raw stream to the handler) for matching requests.
|
|
12
12
|
* Accepts a list of path prefixes or a predicate function.
|
|
13
13
|
* Multipart requests are always skipped regardless of this option.
|
|
14
|
+
* The `type` field determines whether the URL should be matched exactly (`exact`) or as a prefix (`prefix`).
|
|
14
15
|
*/
|
|
15
|
-
ignoreStream?:
|
|
16
|
+
ignoreStream?: {
|
|
17
|
+
url: string;
|
|
18
|
+
method: HttpMethod;
|
|
19
|
+
type: 'exact' | 'prefix';
|
|
20
|
+
}[] | ((req: RequestObject) => boolean);
|
|
16
21
|
};
|
|
17
22
|
export interface SimpleJsControllerMeta {
|
|
18
23
|
name: string;
|
|
@@ -192,7 +192,8 @@ function SetBodyParser(opts) {
|
|
|
192
192
|
if (typeof opts.ignoreStream === "function")
|
|
193
193
|
return opts.ignoreStream(req);
|
|
194
194
|
const url = req.url || "";
|
|
195
|
-
|
|
195
|
+
const method = (req.method || "").toLowerCase();
|
|
196
|
+
return opts.ignoreStream.some(p => (p.type === "exact" ? url === p.url : url.startsWith(p.url)) && method === p.method);
|
|
196
197
|
})();
|
|
197
198
|
// For simplicity, we only parse JSON and plain text. Multipart/form-data and other types are ignored.
|
|
198
199
|
if (shouldIgnoreStream)
|
package/package.json
CHANGED