README
nicer
nicer
is An Http Multipart/Form-Data Request Body Parser. It can receive form data fields and files headers and stream their data.
yarn add nicer
Benchmark | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
Table Of Contents
API
The package is available by importing its default constructor function:
import Nicer from 'nicer'
constructor(
boundary: string,
): Nicer
Creates a transform that emits objects with a header buffer and the body stream. The body stream is a pass-through so all data must be written as it comes, the request doesn't pause for data to be consumed. The header is a buffer which can be parsed more and/or decrypted, but it does not stream. The assumption is the headers are short therefore a header buffer is accumulated until \r\n
is found. Just make sure to run behind NginX then it should be alright.
| |||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
A new instance of Nicer can be piped into by an http.IncomingMessage stream in the Node.JS server. Then a transform stream must be created to listen for the data emitted by Nicer in object mode.
| |||||||||||||
| |||||||||||||
The data received by the 'transform' method, contains the { header, stream } properties. The data from the stream must be accumulated. |
Errors
The errors are spawned when the buffer remaining the stream after the final
event, and processed to extract the rest of the fields, still contains symbols different from -- ([45,45]
).
Extra Buffer (Source) |
---|
|
The data remaining after the last boundary detected after the final method is called does not have any meaning and is discarded. This is not the case with parts that arrived before the stream was closed, i.e., the file limit is not implemented. |
|
The parser will always check for the closing -- and emit an error in the end, however the headers and data streams emitted by it, would have been all closed, i.e., the data can still be used. |
Debug
The software can write debug information, when the DEBUG=nicer
env variable is set.
Debug (Source) |
---|
|
The transform method appends data to the left-over buffer (which can usually be small enough to accommodate [--boundary.length-1] symbols) and consumes data. The data is consumed by first trying to find the boundary in the new buffer. If this is possible, then depending on the state of the parser, the data found before the separator is either flushed in an existing data stream, or appended to the existing header, which can then lead to body-flushing. |
|
After knowing what's left after the last found boundary, the Nicer parser takes only the safe amount of data to consume more which equals to the length of the boundary (including prior --), otherwise there might be a partial boundary leaking into the data stream. The remainder is saved as the new buffer, to which the following chunk in the transform method will be appended, and so on. |
Copyright
(c) Idio 2019