Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

koa-pagination

Package Overview
Dependencies
Maintainers
2
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

koa-pagination - npm Package Compare versions

Comparing version 0.1.0 to 0.1.1

errors/invalid-configuration-error.js

35

index.js

@@ -7,2 +7,3 @@

var _ = require('lodash');
var InvalidConfigurationError = require('./errors/invalid-configuration-error');
var MalformedRangeError = require('./errors/malformed-range-error');

@@ -28,2 +29,7 @@ var RangeNotSatisfiableError = require('./errors/range-not-satisfiable-error');

// Prevent invalid `maximum` value configuration.
if (!_.isFinite(maximum) || maximum <= 0) {
throw new InvalidConfigurationError();
}
// Handle `Range` header.

@@ -41,3 +47,3 @@ if (this.get('Range')) {

// Update `limit` and `offset` values.
// Update `limit`, `offset` and `unit` values.
first = range.first;

@@ -48,13 +54,13 @@ last = range.last;

// Prevent pages to be longer than allowed.
if ((last - first + 1) > maximum) {
last = first + maximum - 1;
}
// Set pagination object on context.
this.pagination = {
limit: last + 1,
limit: last - first + 1,
offset: first
};
// Prevent pages with more items than allowed.
if ((last - first + 1) > maximum) {
last = first + maximum - 1;
}
yield* next;

@@ -64,8 +70,19 @@

// Prevent nonexistent pages.
if (first > length - 1 && length > 0) {
throw new RangeNotSatisfiableError();
}
// Fix `last` value if `length` is lower.
if (last > length) {
if (last + 1 > length && length !== 0) {
last = length - 1;
}
// Set `Content-Range` based on available items.
// Set `byte-range-spec` to undefined value - `*`.
if (length === 0) {
first = undefined;
last = undefined;
}
// Set `Content-Range` based on available units.
this.set('Content-Range', contentRangeFormat({

@@ -72,0 +89,0 @@ first: first,

{
"name": "koa-pagination",
"version": "0.1.0",
"version": "0.1.1",
"description": "Koa Pagination",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -34,5 +34,3 @@

app.use(paginate({
maximum: 3
}));
app.use(paginate({ maximum: 3 }));

@@ -46,2 +44,35 @@ yield request(app.listen())

it('should give and error if `maximum` is not a number', function *() {
var app = koa();
app.use(paginate({ maximum: 'invalid' }));
yield request(app.listen())
.get('/')
.expect(500)
.end();
});
it('should give and error if `maximum` is 0', function *() {
var app = koa();
app.use(paginate({ maximum: 0 }));
yield request(app.listen())
.get('/')
.expect(500)
.end();
});
it('should give and error if `maximum` is lower than 0', function *() {
var app = koa();
app.use(paginate({ maximum: -1 }));
yield request(app.listen())
.get('/')
.expect(500)
.end();
});
it('should accept a `Range` header', function *() {

@@ -84,18 +115,19 @@ var app = koa();

it('should not allow `limit` value superior to `maximum`', function *() {
it('should give and error if `first position` value is higher than `length`', function *() {
var app = koa();
app.use(paginate({
maximum: 3
}));
app.use(paginate());
app.use(function *() {
this.pagination.length = 10;
});
yield request(app.listen())
.get('/')
.expect(206)
.set('Range', 'items=0-5')
.expect('Content-Range', 'items 0-2/*')
.set('Range', 'items=10-12')
.expect(416)
.end();
});
it('should not allow `limit` value superior to `length`', function *() {
it('should give and error if `first position` and `last position` have equal values and are equal to `length`', function *() {
var app = koa();

@@ -106,2 +138,34 @@

app.use(function *() {
this.pagination.length = 10;
});
yield request(app.listen())
.get('/')
.set('Range', 'items=10-10')
.expect(416)
.end();
});
it('should give and error if `first position` and `last position` have equal values and are higher than `length`', function *() {
var app = koa();
app.use(paginate());
app.use(function *() {
this.pagination.length = 10;
});
yield request(app.listen())
.get('/')
.set('Range', 'items=11-11')
.expect(416)
.end();
});
it('should not allow `last position` value to be higher than `length`', function *() {
var app = koa();
app.use(paginate());
app.use(function *() {
this.pagination.length = 3;

@@ -118,5 +182,4 @@ });

it('should set `limit` to `N+1` when `Range` is `items=0-N`', function *() {
it('should not allow `last position` to be equal to `length`', function *() {
var app = koa();
var n = 5;

@@ -126,3 +189,3 @@ app.use(paginate());

app.use(function *() {
this.pagination.limit.should.equal(n + 1);
this.pagination.length = 20;
});

@@ -132,15 +195,31 @@

.get('/')
.set('Range', 'items=0-20')
.expect(206)
.set('Range', util.format('items=0-%s', n))
.expect('Content-Range', 'items 0-19/20')
.end();
});
it('should set `offset` to `N` when `Range` is `items=N-5`', function *() {
it('should not allow `last position` value to be higher than `maximum`', function *() {
var app = koa();
var n = 2;
app.use(paginate({ maximum: 3 }));
yield request(app.listen())
.get('/')
.expect(206)
.set('Range', 'items=0-5')
.expect('Content-Range', 'items 0-2/*')
.end();
});
it('should use the diference between `last position` and `first position`, plus one, as `limit`', function *() {
var app = koa();
var lastPosition = 6;
var firstPosition = 2;
app.use(paginate());
app.use(function *() {
this.pagination.offset.should.equal(n);
this.pagination.limit.should.equal(lastPosition - firstPosition + 1);
});

@@ -151,5 +230,38 @@

.expect(206)
.set('Range', util.format('items=%s-5', n))
.set('Range', util.format('items=%s-%s', firstPosition, lastPosition))
.end();
});
it('should use the `first position` as `offset`', function *() {
var app = koa();
var firstPosition = 2;
app.use(paginate());
app.use(function *() {
this.pagination.offset.should.equal(firstPosition);
});
yield request(app.listen())
.get('/')
.expect(206)
.set('Range', util.format('items=%s-5', firstPosition))
.end();
});
it('should set the `byte-range-spec` to `*` if length is 0', function *() {
var app = koa();
app.use(paginate());
app.use(function *() {
this.pagination.length = 0;
});
yield request(app.listen())
.get('/')
.expect(206)
.expect('Content-Range', 'bytes */0')
.end();
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc