@increase21/simplenodejs 1.0.24 → 1.0.25

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
@@ -271,12 +271,44 @@ 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
275
 
275
276
  ```ts
276
277
  app.use(SetBodyParser({ limit: "2mb" }));
277
278
  ```
278
279
 
279
- > Multipart/form-data bodies are not buffered into memory the size limit still applies to prevent oversized uploads.
280
+ **`ignoreStream`file upload / raw stream endpoints**
281
+
282
+ For context where you need direct stream access (e.g. passing the request to a library like `formidable`), use `ignoreStream`:
283
+
284
+ ```ts
285
+ // Path-prefix list — skip body parsing for any URL under /upload
286
+ app.use(SetBodyParser({ limit: "10mb", ignoreStream: ["/upload", "/files"] }));
287
+
288
+ // Predicate function — full control over which requests are skipped
289
+ app.use(SetBodyParser({
290
+ limit: "10mb",
291
+ ignoreStream: (req) => req.url.startsWith("/upload"),
292
+ }));
293
+ ```
294
+
295
+ When a request is ignored, `next()` is called immediately with the stream untouched. Your handler is then responsible for consuming it:
296
+
297
+ ```ts
298
+ import formidable from "formidable";
299
+
300
+ // Inside your route handler
301
+ const form = formidable({ maxTotalFileSize: 10 * 1024 * 1024 });
302
+ form.parse(req, (err, fields, files) => {
303
+ if (err) {
304
+ // err.code 1009 = file too large, 1015 = total too large
305
+ if (err.code === 1009 || err.code === 1015)
306
+ return res.status(413).end("Payload Too Large");
307
+ return res.status(400).end("Upload Error");
308
+ }
309
+ res.json({ fields, files });
310
+ });
311
+ ```
280
312
 
281
313
  ---
282
314
 
@@ -7,6 +7,12 @@ export type SimpleJSRateLimitType = {
7
7
  };
8
8
  export type SimpleJSBodyParseType = {
9
9
  limit?: string | number;
10
+ /**
11
+ * Skip stream reading (and pass the raw stream to the handler) for matching requests.
12
+ * Accepts a list of path prefixes or a predicate function.
13
+ * Multipart requests are always skipped regardless of this option.
14
+ */
15
+ ignoreStream?: string[] | ((req: any) => boolean);
10
16
  };
11
17
  export interface SimpleJsControllerMeta {
12
18
  name: string;
@@ -186,10 +186,20 @@ function SetBodyParser(opts) {
186
186
  const maxSize = SetBodyLimit(opts.limit);
187
187
  return (req, res, next) => new Promise((resolve, reject) => {
188
188
  const contentType = req.headers["content-type"] || "";
189
- const isMultipart = contentType.includes("multipart/form-data");
189
+ const shouldIgnoreStream = (() => {
190
+ if (!opts.ignoreStream)
191
+ return false;
192
+ if (typeof opts.ignoreStream === "function")
193
+ return opts.ignoreStream(req);
194
+ const url = req.url || "";
195
+ return opts.ignoreStream.some(p => url === p || url.startsWith(p));
196
+ })();
197
+ // For simplicity, we only parse JSON and plain text. Multipart/form-data and other types are ignored.
198
+ if (shouldIgnoreStream)
199
+ return resolve(next());
190
200
  let size = 0;
191
201
  let body = "";
192
- !isMultipart && req.on("data", chunk => {
202
+ req.on("data", chunk => {
193
203
  size += chunk.length;
194
204
  if (maxSize && size > maxSize) {
195
205
  reject({ code: 413, error: "Payload Too Large" });
@@ -205,10 +215,10 @@ function SetBodyParser(opts) {
205
215
  if (res.writableEnded)
206
216
  return resolve();
207
217
  try {
208
- if (!isMultipart && body && contentType.includes("application/json")) {
218
+ if (body && contentType.includes("application/json")) {
209
219
  req.body = JSON.parse(body);
210
220
  }
211
- else if (!isMultipart) {
221
+ else {
212
222
  req.body = body;
213
223
  }
214
224
  resolve(next());
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@increase21/simplenodejs",
3
- "version": "1.0.24",
3
+ "version": "1.0.25",
4
4
  "description": "Lightweight Node.js HTTP framework with middlewares and plugins",
5
5
  "dev": "dist/index.js",
6
6
  "bugs": "https://github.com/increase21/simplenodejs/issues",