@@ -9,6 +9,30 @@ const mongopagingFindWrapper = async (collection, opts) => {
99 const pageNextOriginalCursor = getCursorFromCursorWrapper ( opts . next ) ;
1010 const pagePrevOriginalCursor = getCursorFromCursorWrapper ( opts . previous ) ;
1111
12+ if ( opts . paginatedField && opts . paginatedField !== '_id' ) {
13+ // If the paginatedField is not just _id the the cursor is expected to be an array of 2 elements
14+ // applies to both cursors
15+
16+ for ( const pageOriginalCursor of [ pagePrevOriginalCursor , pageNextOriginalCursor ] ) {
17+ const cursorData = getCursorDataFromCursor ( pageOriginalCursor ) ;
18+
19+ if ( pageOriginalCursor && cursorData ) {
20+ if ( ! Array . isArray ( cursorData ) ) {
21+ const err = new Error ( 'Invalid paging cursor' ) ;
22+ err . code = 'cursorerr' ;
23+ throw err ;
24+ }
25+
26+ if ( cursorData . length < 2 ) {
27+ // only 1 element
28+ const err = new Error ( 'Invalid paging cursor' ) ;
29+ err . code = 'cursorerr' ;
30+ throw err ;
31+ }
32+ }
33+ }
34+ }
35+
1236 if ( pageNextOriginalCursor ) {
1337 // Have next cursor
1438 const pageFromNextCursor = getPageFromMongopagingCursor ( opts . next ) ;
@@ -103,9 +127,17 @@ function getCursorFromCursorWrapper(cursorWrapperString) {
103127 }
104128}
105129
130+ function getCursorDataFromCursor ( cursorString ) {
131+ if ( ! cursorString ) {
132+ return false ;
133+ }
134+ return EJSON . parse ( Buffer . from ( cursorString , 'base64url' ) . toString ( ) ) ;
135+ }
136+
106137module . exports = {
107138 mongopagingFindWrapper,
108139 getPageFromMongopagingCursor,
109140 setPageToMongopagingCursor,
110- getCursorFromCursorWrapper
141+ getCursorFromCursorWrapper,
142+ getCursorDataFromCursor
111143} ;
0 commit comments