|
| 1 | +# @eggjs/co-busboy |
| 2 | + |
| 3 | +Multipart form data handling with async/await support for Egg.js and Koa. |
| 4 | + |
| 5 | +A TypeScript port of [co-busboy](https://github.com/cojs/busboy), providing a promise-based wrapper around [busboy](https://github.com/mscdex/busboy) for parsing multipart/form-data. |
| 6 | + |
| 7 | +## Installation |
| 8 | + |
| 9 | +```bash |
| 10 | +npm install @eggjs/co-busboy |
| 11 | +``` |
| 12 | + |
| 13 | +## Usage |
| 14 | + |
| 15 | +```typescript |
| 16 | +import { parse } from '@eggjs/co-busboy'; |
| 17 | + |
| 18 | +// In a Koa middleware |
| 19 | +app.use(async (ctx) => { |
| 20 | + const parts = parse(ctx, { autoFields: true }); |
| 21 | + let part; |
| 22 | + |
| 23 | + while ((part = await parts())) { |
| 24 | + if (Array.isArray(part)) { |
| 25 | + // It's a field: [name, value, nameTruncated, valueTruncated] |
| 26 | + console.log('Field:', part[0], '=', part[1]); |
| 27 | + } else { |
| 28 | + // It's a file stream with additional properties |
| 29 | + console.log('File:', part.filename, part.mimeType); |
| 30 | + // Consume the stream |
| 31 | + part.pipe(fs.createWriteStream(`./uploads/${part.filename}`)); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + // Access auto-collected fields (when autoFields: true) |
| 36 | + console.log(parts.field); // { fieldName: value } |
| 37 | + console.log(parts.fields); // [[name, value, nameTrunc, valTrunc], ...] |
| 38 | +}); |
| 39 | +``` |
| 40 | + |
| 41 | +## API |
| 42 | + |
| 43 | +### `parse(request, options?)` |
| 44 | + |
| 45 | +Parse multipart form data from a request. |
| 46 | + |
| 47 | +#### Parameters |
| 48 | + |
| 49 | +- `request` - Node.js IncomingMessage or Koa context |
| 50 | +- `options` - Optional configuration object |
| 51 | + |
| 52 | +#### Options |
| 53 | + |
| 54 | +All standard [busboy options](https://github.com/mscdex/busboy#api) are supported, plus: |
| 55 | + |
| 56 | +- `autoFields` (boolean, default: `false`) - When true, automatically collects all form fields. Fields will be available via `parts.field` (object lookup) and `parts.fields` (array lookup). Only file streams will be returned in the iteration. |
| 57 | + |
| 58 | +- `checkField` (function) - Hook to validate form fields. Return an Error to reject the field. |
| 59 | + |
| 60 | + ```typescript |
| 61 | + checkField: (name, value, fieldnameTruncated, valueTruncated) => { |
| 62 | + if (name === '_csrf' && !isValidToken(value)) { |
| 63 | + return new Error('Invalid CSRF token'); |
| 64 | + } |
| 65 | + } |
| 66 | + ``` |
| 67 | + |
| 68 | +- `checkFile` (function) - Hook to validate file uploads. Return an Error to reject the file. |
| 69 | + ```typescript |
| 70 | + checkFile: (fieldname, stream, filename, encoding, mimetype) => { |
| 71 | + if (!filename.endsWith('.jpg')) { |
| 72 | + const err = new Error('Only JPG files allowed'); |
| 73 | + err.status = 400; |
| 74 | + return err; |
| 75 | + } |
| 76 | + } |
| 77 | + ``` |
| 78 | + |
| 79 | +#### Return Value |
| 80 | + |
| 81 | +Returns a `Parts` function that yields parts when called: |
| 82 | + |
| 83 | +```typescript |
| 84 | +interface Parts { |
| 85 | + (): Promise<Part | null>; |
| 86 | + field: Record<string, string | string[]>; |
| 87 | + fields: FieldTuple[]; |
| 88 | +} |
| 89 | +``` |
| 90 | + |
| 91 | +- Call `parts()` repeatedly to get each part |
| 92 | +- Returns `null` when parsing is complete |
| 93 | +- When `autoFields` is true, fields are collected in `parts.field` and `parts.fields` |
| 94 | + |
| 95 | +#### Part Types |
| 96 | + |
| 97 | +**Field** - An array with 4 elements: |
| 98 | + |
| 99 | +```typescript |
| 100 | +type FieldTuple = [ |
| 101 | + string, // name |
| 102 | + string, // value |
| 103 | + boolean, // fieldnameTruncated |
| 104 | + boolean // valueTruncated |
| 105 | +]; |
| 106 | +``` |
| 107 | + |
| 108 | +**File** - A Readable stream with additional properties: |
| 109 | + |
| 110 | +```typescript |
| 111 | +interface FileStream extends Readable { |
| 112 | + fieldname: string; |
| 113 | + filename: string; |
| 114 | + encoding: string; |
| 115 | + transferEncoding: string; |
| 116 | + mime: string; |
| 117 | + mimeType: string; |
| 118 | +} |
| 119 | +``` |
| 120 | + |
| 121 | +## Error Handling |
| 122 | + |
| 123 | +Limit errors include status and code properties: |
| 124 | + |
| 125 | +```typescript |
| 126 | +try { |
| 127 | + while ((part = await parts())) { |
| 128 | + // process part |
| 129 | + } |
| 130 | +} catch (err) { |
| 131 | + console.log(err.status); // 413 |
| 132 | + console.log(err.code); // 'Request_files_limit', 'Request_fields_limit', or 'Request_parts_limit' |
| 133 | +} |
| 134 | +``` |
| 135 | + |
| 136 | +## Compression Support |
| 137 | + |
| 138 | +Gzip and deflate compressed requests are automatically decompressed via the `inflation` library. |
| 139 | + |
| 140 | +## License |
| 141 | + |
| 142 | +[MIT](LICENSE) |
0 commit comments