Skip to content

Commit

Permalink
v1.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
slaviczavik committed Jul 22, 2018
1 parent e7cc895 commit c67fee2
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 10 deletions.
58 changes: 52 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,34 @@ npm i @slaviczavik/http-data-parser

# Example

## Code

```JS
const HttpDataParser = require('@slaviczavik/http-data-parser')

// A boundary must be extracted from 'content-type' request header.
const boundary '--------------------------034172598905589540726558'
const parser = new HttpDataParser(boundary)

parser.on('header', function (buffer) {
parser.on('header', function (data, isLast) {
// Save headers somewhere...

if (isLast) {
// Here you may prepare for handling body data,
// like create a writable stream and so on.
}
})

parser.on('data', function (buffer) {
parser.on('data', function (data) {
// Save body content somewhere...
})

parser.on('part', function () {
// We reached the end of one body part.
// Here we can concate headers and/or body content together.
// Here we can concate body content together.
})

parser.on('end', function () {
// We reached the end of whole body.
// Or here we can concate headers and/or body content together.
// No other data will come.
})

req.on('data', function (data) {
Expand All @@ -48,3 +51,46 @@ req.on('end', function () {
parser.end()
})
```

# API

## Constructor
### `new HttpDataParser(boundary)`

| Name | Required | Type | Description | Default
| - | - | - | - | - |
| `boundary` | true | `string` | | `none` |

## Methods

### `add(buffer)`

| Parameter | Required | Type | Description
| - | - | - | - |
| `buffer` | true | `buffer` | Buffer from request body. |

### `end()`
Call this method when there is no more data to be consumed from the stream.

## Events

### `header(data, isLast)`
Emitted every time when a parser find a header part.

| Property | Type | Description
| - | - | - |
| `data` | `buffer` | - |
| `isLast` | `boolean` | Signals if it is a last header part. |

### `data(data)`
Emitted every time when a parser find a content data.

| Property | Type | Description
| - | - | - |
| `data` | `buffer` | - |

### `part()`
Emitted every time when a parser reach the end of one body part.

### `end()`
Emitted when a parser reached the end of request body.
4 changes: 2 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ class Parser extends EventEmitter {
// Double EOF between headers and content,
// before that are headers.
this.headState = STATE.AFTER_HEADERS
this.emit('header', data.slice(0, start))
this.emit('header', data.slice(0, start), true)
}
else {
// Headers before double EOF.
this.emit('header', data)
this.emit('header', data, false)
}
}
else if (this.headState === STATE.AFTER_HEADERS) {
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@slaviczavik/http-data-parser",
"version": "1.0.0",
"version": "1.1.0",
"description": "A really fast Node.js streaming multipart parser.",
"main": "./lib/index.js",
"scripts": {
Expand Down

0 comments on commit c67fee2

Please sign in to comment.