Skip to content

Commit

Permalink
Fix maxBytes. Closes #72
Browse files Browse the repository at this point in the history
  • Loading branch information
hueniverse committed Sep 13, 2019
1 parent 5e40124 commit 1e2aed6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
12 changes: 10 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ internals.writeFile = function (req, options, stream) {

const path = File.uniqueFilename(options.uploads || Os.tmpdir());
const file = Fs.createWriteStream(path, { flags: 'wx' });
const counter = new internals.Counter();
const counter = new internals.Counter(options);

const finalize = (err) => {

Expand Down Expand Up @@ -404,15 +404,23 @@ internals.pipe = function (from, to) {

internals.Counter = class extends Stream.Transform {

constructor() {
constructor(options) {

super();
this.bytes = 0;
this._maxBytes = options.maxBytes;
}

_transform(chunk, encoding, next) {

this.bytes = this.bytes + chunk.length;

if (this._maxBytes !== undefined &&
this.bytes > this._maxBytes) {

return next(Boom.entityTooLarge('Payload content length greater than maximum allowed: ' + this._maxBytes));
}

return next(null, chunk);
}
};
23 changes: 23 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,29 @@ describe('parse()', () => {
expect(err.output.statusCode).to.equal(413);
});

it('errors when content-length header greater than maxBytes (file)', async () => {

const body = '{"x":"1","y":"2","z":"3"}';
const request = Wreck.toReadableStream(body);
request.headers = {
'content-type': 'application/json'
};

const err = await expect(Subtext.parse(request, null, { parse: false, output: 'file', maxBytes: 10 })).to.reject('Payload content length greater than maximum allowed: 10');
expect(err.output.statusCode).to.equal(413);
});

it('allows file within the maxBytes limit', async () => {

const body = '{"x":"1","y":"2","z":"3"}';
const request = Wreck.toReadableStream(body);
request.headers = {
'content-type': 'application/json'
};

await expect(Subtext.parse(request, null, { parse: false, output: 'file', maxBytes: 100 })).to.not.reject();
});

it('limits maxBytes when content-length header missing', async () => {

const body = '{"x":"1","y":"2","z":"3"}';
Expand Down

0 comments on commit 1e2aed6

Please sign in to comment.