Initial commit
This commit is contained in:
21
node_modules/cbor-body-parser/LICENSE
generated
vendored
Normal file
21
node_modules/cbor-body-parser/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2019 Sven Eliasson
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
80
node_modules/cbor-body-parser/README.md
generated
vendored
Normal file
80
node_modules/cbor-body-parser/README.md
generated
vendored
Normal file
@@ -0,0 +1,80 @@
|
||||
|
||||
|
||||
# cbor body parser
|
||||
## A cbor parser for Express
|
||||
|
||||
This is a very simple cbor body parser for express by extending the existing npm module 'body-parser'.
|
||||
It wraps https://github.com/hildjj/node-cbor into a middleware.
|
||||
|
||||
## Installation
|
||||
```sh
|
||||
$ npm install cbor-body-parser
|
||||
```
|
||||
|
||||
## Example server side usage:
|
||||
```js
|
||||
let express = require("express");
|
||||
let bodyParser = require("body-parser");
|
||||
bodyParser.cbor = require("cbor-body-parser");
|
||||
|
||||
let app = express();
|
||||
app.use(bodyParser.json());
|
||||
app.use(bodyParser.cbor({limit: "100kB"}));
|
||||
|
||||
app.post("/", (req, res) => {
|
||||
console.log("Got Payload: " + JSON.stringify(res.body));
|
||||
res.status(200).json(req.body);
|
||||
})
|
||||
|
||||
app.listen(3000, function () {
|
||||
console.log('Example app listening on port 3000!');
|
||||
});
|
||||
```
|
||||
## Client side test
|
||||
```sh
|
||||
$ gem install cbor-diag
|
||||
$ echo '{"user":"marry"}' | json2cbor.rb | curl -d @- -H "Content-Type: application/cbor" -X POST http://localhost:3000/
|
||||
```
|
||||
|
||||
## API
|
||||
|
||||
```js
|
||||
let cborBodyParser = require("cbor-body-parser");
|
||||
```
|
||||
|
||||
### cborBodyParser.cbor([options])
|
||||
|
||||
The optional "options" object contains:
|
||||
|
||||
#### limit
|
||||
Controls the maximum request body size. If this is a number, then the value
|
||||
specifies the number of bytes; if it is a string, the value is passed to the
|
||||
[bytes](https://www.npmjs.com/package/bytes) library for parsing. Defaults
|
||||
to `'100kb'`.
|
||||
|
||||
#### type
|
||||
The `type` option is used to determine what media type the middleware will
|
||||
parse. This option can be a string, array of strings, or a function. If not a
|
||||
function, `type` option is passed directly to the
|
||||
[type-is](https://www.npmjs.org/package/type-is#readme) library and this can
|
||||
be an extension name (like `cbor`), a mime type (like `application/cbor`), or
|
||||
a mime type with a wildcard (like `*/*` or `*/cbor`). If a function, the `type`
|
||||
option is called as `fn(req)` and the request is parsed if it returns a truthy
|
||||
value. Defaults to `application/cbor`.
|
||||
|
||||
#### verify
|
||||
The `verify` option, if supplied, is called as `verify(req, res, buf, encoding)`,
|
||||
where `buf` is a `Buffer` of the raw request body and `encoding` is the
|
||||
encoding of the request. The parsing can be aborted by throwing an error.
|
||||
|
||||
## Test
|
||||
```sh
|
||||
$ npm run test
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
59
node_modules/cbor-body-parser/index.js
generated
vendored
Normal file
59
node_modules/cbor-body-parser/index.js
generated
vendored
Normal file
@@ -0,0 +1,59 @@
|
||||
let read = require("body-parser/lib/read");
|
||||
let cborEncoder = require('cbor');
|
||||
var bytes = require('bytes');
|
||||
var debug = require('debug')('cbor-parser');
|
||||
var typeis = require('type-is');
|
||||
|
||||
module.exports = function cbor(options) {
|
||||
|
||||
const opts = options || {};
|
||||
const limit = typeof opts.limit !== 'number' ? bytes.parse(opts.limit || '100kb') : opts.limit;
|
||||
const type = opts.type || 'application/cbor';
|
||||
const verify = opts.verify || false;
|
||||
|
||||
if (verify !== false && typeof verify !== 'function') {
|
||||
throw new TypeError('option verify must be function')
|
||||
}
|
||||
|
||||
const shouldParse = typeof type !== 'function' ? typeChecker(type) : type;
|
||||
|
||||
function parse(buf) {
|
||||
if (buf.length === 0) {
|
||||
debug('buffer is zero');
|
||||
return {};
|
||||
}
|
||||
debug('parsing cbor content');
|
||||
return cborEncoder.decode(buf);
|
||||
}
|
||||
|
||||
return function cborParser(req, res, next) {
|
||||
if (req._body) {
|
||||
return debug('body already parsed'), next();
|
||||
}
|
||||
|
||||
req.body = req.body || {};
|
||||
|
||||
// skip requests without bodies
|
||||
if (!typeis.hasBody(req)) {
|
||||
return debug('skip empty body'), next();
|
||||
}
|
||||
|
||||
debug('content-type %j', req.headers['content-type'])
|
||||
|
||||
if (!shouldParse(req)) {
|
||||
return debug('skip parsing'), next();
|
||||
}
|
||||
|
||||
read(req, res, next, parse, debug, {
|
||||
encoding: null,
|
||||
limit: limit,
|
||||
verify: verify
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
function typeChecker(type) {
|
||||
return function checkType(req) {
|
||||
return Boolean(typeis(req, type))
|
||||
}
|
||||
}
|
||||
88
node_modules/cbor-body-parser/package.json
generated
vendored
Normal file
88
node_modules/cbor-body-parser/package.json
generated
vendored
Normal file
@@ -0,0 +1,88 @@
|
||||
{
|
||||
"_args": [
|
||||
[
|
||||
"cbor-body-parser@1.0.3",
|
||||
"C:\\Users\\anelissen.DOMAINE\\Development\\tools\\node\\cbor"
|
||||
]
|
||||
],
|
||||
"_from": "cbor-body-parser@1.0.3",
|
||||
"_id": "cbor-body-parser@1.0.3",
|
||||
"_inBundle": false,
|
||||
"_integrity": "sha512-RXXRzQ7hckzLg3cCtdbCzxFRNfK8RoHTf7yO/uTEE44tF8DAeBQnSYkjXWNFFXL9byRfjg4XqCaZitcVAcEruA==",
|
||||
"_location": "/cbor-body-parser",
|
||||
"_phantomChildren": {},
|
||||
"_requested": {
|
||||
"type": "version",
|
||||
"registry": true,
|
||||
"raw": "cbor-body-parser@1.0.3",
|
||||
"name": "cbor-body-parser",
|
||||
"escapedName": "cbor-body-parser",
|
||||
"rawSpec": "1.0.3",
|
||||
"saveSpec": null,
|
||||
"fetchSpec": "1.0.3"
|
||||
},
|
||||
"_requiredBy": [
|
||||
"/"
|
||||
],
|
||||
"_resolved": "https://registry.npmjs.org/cbor-body-parser/-/cbor-body-parser-1.0.3.tgz",
|
||||
"_spec": "1.0.3",
|
||||
"_where": "C:\\Users\\anelissen.DOMAINE\\Development\\tools\\node\\cbor",
|
||||
"author": {
|
||||
"name": "Sven Eliasson",
|
||||
"email": "Sven.Eliasson@gmail.com"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/comino/cbor-body-parser/issues"
|
||||
},
|
||||
"contributors": [
|
||||
{
|
||||
"name": "Sven Eliasson",
|
||||
"email": "sven.eliasson@gmail.com"
|
||||
}
|
||||
],
|
||||
"dependencies": {
|
||||
"body-parser": "^1.19.0",
|
||||
"cbor": "^5.1.0"
|
||||
},
|
||||
"description": "A simple cbor body parser for express",
|
||||
"devDependencies": {
|
||||
"eslint": "7.11.0",
|
||||
"eslint-config-standard": "14.1.1",
|
||||
"eslint-plugin-import": "2.22.1",
|
||||
"eslint-plugin-markdown": "1.0.2",
|
||||
"eslint-plugin-node": "11.1.0",
|
||||
"eslint-plugin-promise": "4.2.1",
|
||||
"eslint-plugin-standard": "4.0.1",
|
||||
"express": "^4.17.1",
|
||||
"istanbul": "0.4.5",
|
||||
"methods": "1.1.2",
|
||||
"mocha": "8.1.3",
|
||||
"safe-buffer": "5.2.1",
|
||||
"should": "^13.2.3",
|
||||
"supertest": "5.0.0",
|
||||
"underscore": "^1.11.0"
|
||||
},
|
||||
"directories": {
|
||||
"test": "test"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=6.0.0"
|
||||
},
|
||||
"homepage": "https://github.com/comino/cbor-body-parser#readme",
|
||||
"keywords": [
|
||||
"body-parser",
|
||||
"express",
|
||||
"cbor"
|
||||
],
|
||||
"license": "MIT",
|
||||
"main": "index.js",
|
||||
"name": "cbor-body-parser",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/comino/cbor-body-parser.git"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "mocha --reporter spec --check-leaks --bail test"
|
||||
},
|
||||
"version": "1.0.3"
|
||||
}
|
||||
101
node_modules/cbor-body-parser/test/cbor.js
generated
vendored
Normal file
101
node_modules/cbor-body-parser/test/cbor.js
generated
vendored
Normal file
@@ -0,0 +1,101 @@
|
||||
const should = require('should');
|
||||
const request = require('supertest');
|
||||
const cborParser = require('..');
|
||||
const cbor = require("cbor");
|
||||
|
||||
const _ = require("underscore");
|
||||
const express = require("express");
|
||||
const bodyParser = require("body-parser");
|
||||
bodyParser.cbor = cborParser;
|
||||
|
||||
describe('cborparser as express app', function () {
|
||||
it('should parse json', function (done) {
|
||||
request(createExpressServer())
|
||||
.post('/')
|
||||
.set('Content-Type', 'application/json')
|
||||
.send('{"user":"tobi"}')
|
||||
.expect(200)
|
||||
.end( (err, res) => {
|
||||
should.exist(res.body);
|
||||
let data = res.body;
|
||||
should.exist(data);
|
||||
should.exist(data.cbor);
|
||||
should(data.cbor).be.equal(false);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should parse cbor with a correct content type', function (done) {
|
||||
request(createExpressServer())
|
||||
.post('/')
|
||||
.set('Content-Type', 'application/cbor')
|
||||
.send(cbor.encode({"user":"tobi"}))
|
||||
.expect(200)
|
||||
.responseType('blob')
|
||||
.end( (err, res) => {
|
||||
const decodedData = cbor.decode(res.body);
|
||||
should.exist(decodedData);
|
||||
should.exist(decodedData.user);
|
||||
should.exist(decodedData.cbor);
|
||||
should(decodedData.cbor).be.equal(true);
|
||||
should(decodedData.user).be.equal("tobi");
|
||||
done();
|
||||
});
|
||||
});
|
||||
it('should reject cbor with a too big body according to limit option', function (done) {
|
||||
const bigPayload = {
|
||||
"user":"tobi",
|
||||
"1": "dsfjkhaosdfhsijdhfaisdhfakjsdhfklajshdfkjahsdflkasdkjsofgjsd9u239ruaosidfa0shfdaos09jdf",
|
||||
"2": "dsfjkhaosdfhsijdhfaisdhfakjsdhfklajshdfkjahsdflkasdkjsofgjsd9u239ruaosidfa0shfdaos09jdf",
|
||||
"3": "dsfjkhaosdfhsijdhfaisdhfakjsdhfklajshdfkjahsdflkasdkjsofgjsd9u239ruaosidfa0shfdaos09jdf",
|
||||
"4": "dsfjkhaosdfhsijdhfaisdhfakjsdhfklajshdfkjahsdflkasdkjsofgjsd9u239ruaosidfa0shfdaos09jdf",
|
||||
"5": "dsfjkhaosdfhsijdhfaisdhfakjsdhfklajshdfkjahsdflkasdkjsofgjsd9u239ruaosidfa0shfdaos09jdf",
|
||||
"6": "dsfjkhaosdfhsijdhfaisdhfakjsdhfklajshdfkjahsdflkasdkjsofgjsd9u239ruaosidfa0shfdaos09jdf",
|
||||
};
|
||||
request(createExpressServer())
|
||||
.post('/')
|
||||
.set('Content-Type', 'application/cbor')
|
||||
.send(cbor.encode(bigPayload))
|
||||
.expect(422)
|
||||
.responseType('blob')
|
||||
.end( (err, res) => {
|
||||
let data = cbor.decode(res.body);
|
||||
should.not.exist(data.user);
|
||||
should.exist(data.err);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
function createExpressServer() {
|
||||
const app = express();
|
||||
app.use(bodyParser.json({ limit: "4mb"}));
|
||||
app.use(bodyParser.cbor({ limit: '500B'}));
|
||||
|
||||
app.use((err, req, res, next) => {
|
||||
if(err) {
|
||||
if( req.is("application/cbor")){
|
||||
return res.status(422).end(cbor.encode({err: err.message}));
|
||||
}
|
||||
return res.status(422).json({err: err.message});
|
||||
}
|
||||
else next();
|
||||
});
|
||||
|
||||
app.post("/", (req, res, next) => {
|
||||
if( req.is("application/cbor")){
|
||||
let returnData = req.body;
|
||||
returnData.cbor = true;
|
||||
const encodedData = cbor.encode(returnData);
|
||||
res.setHeader("content-type", "application/cbor");
|
||||
res.setHeader('content-length', _.size(encodedData));
|
||||
return res.status(200).end(encodedData);
|
||||
}else{
|
||||
let returnData = req.body;
|
||||
returnData.cbor = false;
|
||||
return res.status(200).json(returnData);
|
||||
}
|
||||
});
|
||||
return app;
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user