Comparing version 2.2.0 to 2.3.0
@@ -0,1 +1,13 @@ | ||
2.3.0 / 2017-08-04 | ||
================== | ||
* Add TypeScript definitions | ||
* Use `http-errors` for standard emitted errors | ||
* deps: bytes@2.5.0 | ||
* deps: iconv-lite@0.4.18 | ||
- Add support for React Native | ||
- Add a warning if not loaded as utf-8 | ||
- Fix CESU-8 decoding in Node.js 8 | ||
- Improve speed of ISO-8859-1 encoding | ||
2.2.0 / 2017-01-02 | ||
@@ -2,0 +14,0 @@ ================== |
86
index.js
@@ -16,2 +16,3 @@ /*! | ||
var bytes = require('bytes') | ||
var createError = require('http-errors') | ||
var iconv = require('iconv-lite') | ||
@@ -32,3 +33,3 @@ var unpipe = require('unpipe') | ||
var iconvEncodingMessageRegExp = /^Encoding not recognized: / | ||
var ICONV_ENCODING_MESSAGE_REGEXP = /^Encoding not recognized: / | ||
@@ -49,7 +50,8 @@ /** | ||
// error getting decoder | ||
if (!iconvEncodingMessageRegExp.test(e.message)) throw e | ||
if (!ICONV_ENCODING_MESSAGE_REGEXP.test(e.message)) throw e | ||
// the encoding was not found | ||
throw createError(415, 'specified encoding unsupported', 'encoding.unsupported', { | ||
encoding: encoding | ||
throw createError(415, 'specified encoding unsupported', { | ||
encoding: encoding, | ||
type: 'encoding.unsupported' | ||
}) | ||
@@ -138,45 +140,2 @@ } | ||
/** | ||
* Make a serializable error object. | ||
* | ||
* To create serializable errors you must re-set message so | ||
* that it is enumerable and you must re configure the type | ||
* property so that is writable and enumerable. | ||
* | ||
* @param {number} status | ||
* @param {string} message | ||
* @param {string} type | ||
* @param {object} props | ||
* @private | ||
*/ | ||
function createError (status, message, type, props) { | ||
var error = new Error() | ||
// capture stack trace | ||
Error.captureStackTrace(error, createError) | ||
// set free-form properties | ||
for (var prop in props) { | ||
error[prop] = props[prop] | ||
} | ||
// set message | ||
error.message = message | ||
// set status | ||
error.status = status | ||
error.statusCode = status | ||
// set type | ||
Object.defineProperty(error, 'type', { | ||
value: type, | ||
enumerable: true, | ||
writable: true, | ||
configurable: true | ||
}) | ||
return error | ||
} | ||
/** | ||
* Read the data from the stream. | ||
@@ -200,6 +159,7 @@ * | ||
if (limit !== null && length !== null && length > limit) { | ||
return done(createError(413, 'request entity too large', 'entity.too.large', { | ||
return done(createError(413, 'request entity too large', { | ||
expected: length, | ||
length: length, | ||
limit: limit | ||
limit: limit, | ||
type: 'entity.too.large' | ||
})) | ||
@@ -216,3 +176,5 @@ } | ||
// developer error | ||
return done(createError(500, 'stream encoding should not be set', 'stream.encoding.set')) | ||
return done(createError(500, 'stream encoding should not be set', { | ||
type: 'stream.encoding.set' | ||
})) | ||
} | ||
@@ -275,7 +237,8 @@ | ||
done(createError(400, 'request aborted', 'request.aborted', { | ||
done(createError(400, 'request aborted', { | ||
code: 'ECONNABORTED', | ||
expected: length, | ||
length: length, | ||
received: received | ||
received: received, | ||
type: 'request.aborted' | ||
})) | ||
@@ -288,10 +251,14 @@ } | ||
received += chunk.length | ||
decoder | ||
? buffer += decoder.write(chunk) | ||
: buffer.push(chunk) | ||
if (decoder) { | ||
buffer += decoder.write(chunk) | ||
} else { | ||
buffer.push(chunk) | ||
} | ||
if (limit !== null && received > limit) { | ||
done(createError(413, 'request entity too large', 'entity.too.large', { | ||
done(createError(413, 'request entity too large', { | ||
limit: limit, | ||
received: received | ||
received: received, | ||
type: 'entity.too.large' | ||
})) | ||
@@ -306,6 +273,7 @@ } | ||
if (length !== null && received !== length) { | ||
done(createError(400, 'request size did not match content length', 'request.size.invalid', { | ||
done(createError(400, 'request size did not match content length', { | ||
expected: length, | ||
length: length, | ||
received: received | ||
received: received, | ||
type: 'request.size.invalid' | ||
})) | ||
@@ -312,0 +280,0 @@ } else { |
{ | ||
"name": "raw-body", | ||
"description": "Get and validate the raw body of a readable stream.", | ||
"version": "2.2.0", | ||
"version": "2.3.0", | ||
"author": "Jonathan Ong <me@jongleberry.com> (http://jongleberry.com)", | ||
@@ -13,17 +13,20 @@ "contributors": [ | ||
"dependencies": { | ||
"bytes": "2.4.0", | ||
"iconv-lite": "0.4.15", | ||
"bytes": "2.5.0", | ||
"http-errors": "1.6.1", | ||
"iconv-lite": "0.4.18", | ||
"unpipe": "1.0.0" | ||
}, | ||
"devDependencies": { | ||
"bluebird": "3.4.7", | ||
"eslint": "3.12.2", | ||
"eslint-config-standard": "6.2.1", | ||
"eslint-plugin-markdown": "1.0.0-beta.3", | ||
"eslint-plugin-promise": "3.4.0", | ||
"eslint-plugin-standard": "2.0.1", | ||
"bluebird": "3.5.0", | ||
"eslint": "3.19.0", | ||
"eslint-config-standard": "10.2.1", | ||
"eslint-plugin-import": "2.7.0", | ||
"eslint-plugin-markdown": "1.0.0-beta.6", | ||
"eslint-plugin-node": "5.1.1", | ||
"eslint-plugin-promise": "3.5.0", | ||
"eslint-plugin-standard": "3.0.1", | ||
"istanbul": "0.4.5", | ||
"mocha": "2.5.3", | ||
"readable-stream": "2.1.2", | ||
"through2": "2.0.1" | ||
"readable-stream": "2.3.3", | ||
"safe-buffer": "5.1.1" | ||
}, | ||
@@ -37,2 +40,3 @@ "engines": { | ||
"README.md", | ||
"index.d.ts", | ||
"index.js" | ||
@@ -39,0 +43,0 @@ ], |
100
README.md
@@ -13,2 +13,23 @@ # raw-body | ||
## Install | ||
This is a [Node.js](https://nodejs.org/en/) module available through the | ||
[npm registry](https://www.npmjs.com/). Installation is done using the | ||
[`npm install` command](https://docs.npmjs.com/getting-started/installing-npm-packages-locally): | ||
```sh | ||
$ npm install raw-body | ||
``` | ||
### TypeScript | ||
This module includes a [TypeScript](https://www.typescriptlang.org/) | ||
declarition file to enable auto complete in compatible editors and type | ||
information for TypeScript projects. This module depends on the Node.js | ||
types, so install `@types/node`: | ||
```sh | ||
$ npm install @types/node | ||
``` | ||
## API | ||
@@ -44,15 +65,2 @@ | ||
`callback(err, res)`: | ||
- `err` - the following attributes will be defined if applicable: | ||
- `limit` - the limit in bytes | ||
- `length` and `expected` - the expected length of the stream | ||
- `received` - the received bytes | ||
- `encoding` - the invalid encoding | ||
- `status` and `statusCode` - the corresponding status code for the error | ||
- `type` - either `entity.too.large`, `request.aborted`, `request.size.invalid`, `stream.encoding.set`, or `encoding.unsupported` | ||
- `res` - the result, either as a `String` if an encoding was set or a `Buffer` otherwise. | ||
If an error occurs, the stream will be paused, everything unpiped, | ||
@@ -63,2 +71,47 @@ and you are responsible for correctly disposing the stream. | ||
## Errors | ||
This module creates errors depending on the error condition during reading. | ||
The error may be an error from the underlying Node.js implementation, but is | ||
otherwise an error created by this module, which has the following attributes: | ||
* `limit` - the limit in bytes | ||
* `length` and `expected` - the expected length of the stream | ||
* `received` - the received bytes | ||
* `encoding` - the invalid encoding | ||
* `status` and `statusCode` - the corresponding status code for the error | ||
* `type` - the error type | ||
### Types | ||
The errors from this module have a `type` property which allows for the progamatic | ||
determination of the type of error returned. | ||
#### encoding.unsupported | ||
This error will occur when the `encoding` option is specified, but the value does | ||
not map to an encoding supported by the [iconv-lite](https://www.npmjs.org/package/iconv-lite#readme) | ||
module. | ||
#### entity.too.large | ||
This error will occur when the `limit` option is specified, but the stream has | ||
an entity that is larger. | ||
#### request.aborted | ||
This error will occur when the request stream is aborted by the client before | ||
reading the body has finished. | ||
#### request.size.invalid | ||
This error will occur when the `length` option is specified, but the stream has | ||
emitted more bytes. | ||
#### stream.encoding.set | ||
This error will occur when the given stream has an encoding set on it, making it | ||
a decoded stream. The stream should not have an encoding set and is expected to | ||
emit `Buffer` objects. | ||
## Examples | ||
@@ -135,2 +188,23 @@ | ||
### Using with TypeScript | ||
```ts | ||
import * as getRawBody from 'raw-body'; | ||
import * as http from 'http'; | ||
const server = http.createServer((req, res) => { | ||
getRawBody(req) | ||
.then((buf) => { | ||
res.statusCode = 200; | ||
res.end(buf.length + ' bytes submitted'); | ||
}) | ||
.catch((err) => { | ||
res.statusCode = err.statusCode; | ||
res.end(err.message); | ||
}); | ||
}); | ||
server.listen(3000); | ||
``` | ||
## License | ||
@@ -137,0 +211,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
21827
6
315
220
4
12
1
+ Addedhttp-errors@1.6.1
+ Addedbytes@2.5.0(transitive)
+ Addeddepd@1.1.0(transitive)
+ Addedhttp-errors@1.6.1(transitive)
+ Addediconv-lite@0.4.18(transitive)
+ Addedinherits@2.0.3(transitive)
+ Addedsetprototypeof@1.0.3(transitive)
+ Addedstatuses@1.5.0(transitive)
- Removedbytes@2.4.0(transitive)
- Removediconv-lite@0.4.15(transitive)
Updatedbytes@2.5.0
Updatediconv-lite@0.4.18