Skip to content

Fix query pagination when in desc order #2213

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 21 additions & 19 deletions lib/winston/transports/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
'use strict';

const fs = require('fs');
const fsReverse = require('fs-reverse-modern');
const path = require('path');
const asyncSeries = require('async/series');
const zlib = require('zlib');
Expand Down Expand Up @@ -218,9 +219,9 @@ module.exports = class File extends TransportStream {
let results = [];
let row = 0;

const stream = fs.createReadStream(file, {
const stream = options.order === 'asc' ? fs.createReadStream(file, {
encoding: 'utf8'
});
}) : fsReverse(file, {encoding: 'utf8'});

stream.on('error', err => {
if (stream.readable) {
Expand All @@ -236,25 +237,32 @@ module.exports = class File extends TransportStream {
stream.on('data', data => {
data = (buff + data).split(/\n+/);
const l = data.length - 1;
let i = 0;

for (; i < l; i++) {
if (!options.start || row >= options.start) {
add(data[i]);
if (options.order === 'asc') {
let i = 0;

for (; i < l; i++) {
if (!options.start || row >= options.start) {
add(data[i]);
}
row++;
}

buff = data[l];
} else {
if (data[0] && (!options.start || row >= options.start)) {
add(data[0]);
}
if (data[0]) {
row++;
}
row++;
}

buff = data[l];
});

stream.on('close', () => {
if (buff) {
add(buff, true);
}
if (options.order === 'desc') {
results = results.reverse();
}

// eslint-disable-next-line callback-return
if (callback) callback(null, results);
Expand All @@ -276,8 +284,7 @@ module.exports = class File extends TransportStream {
function push(log) {
if (
options.rows &&
results.length >= options.rows &&
options.order !== 'desc'
results.length >= options.rows
) {
if (stream.readable) {
stream.destroy();
Expand All @@ -292,11 +299,6 @@ module.exports = class File extends TransportStream {
}, {});
}

if (options.order === 'desc') {
if (results.length >= options.rows) {
results.shift();
}
}
results.push(log);
}

Expand Down
Loading