Commit e6bb3eaa authored by Aral Balkan's avatar Aral Balkan
Browse files

Inline n-readlines modules into lib/LineByLine.js

(And remove external dependency.)
parent 1b780f6b
......@@ -780,6 +780,8 @@ Please open an issue before starting to work on pull requests.
For code coverage, run `npm run coverage`.
(Note: `lib/LineByLine.js` is excluded from coverage as it is the inlined version of [n-readlines](https://github.com/nacholibre/node-readlines). The tests for it can be found as part of that library.)
## Ideas for post 2.0.0.
- [ ] __Implement [transactions](https://github.com/small-tech/jsdb/issues/1).__
......
......@@ -25,7 +25,7 @@ import { log } from './Util.js'
import Time from './Time.js'
import { performance } from 'perf_hooks'
import LineByLine from 'n-readlines'
import LineByLine from './LineByLine.js'
export default class JSTable extends EventEmitter {
#data = null
......
// The MIT License (MIT)
// Copyright (c) 2013 Liucw
// 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.
// Inlined and converted to ESM from: n-readlines@1.0.1
// https://github.com/nacholibre/node-readlines
import fs from 'fs'
/**
* @class
*/
export default class LineByLine {
constructor(file, options) {
options = options || {};
if (!options.readChunk) options.readChunk = 1024;
if (!options.newLineCharacter) {
options.newLineCharacter = 0x0a; //linux line ending
} else {
options.newLineCharacter = options.newLineCharacter.charCodeAt(0);
}
if (typeof file === 'number') {
this.fd = file;
} else {
this.fd = fs.openSync(file, 'r');
}
this.options = options;
this.newLineCharacter = options.newLineCharacter;
this.reset();
}
_searchInBuffer(buffer, hexNeedle) {
let found = -1;
for (let i = 0; i <= buffer.length; i++) {
let b_byte = buffer[i];
if (b_byte === hexNeedle) {
found = i;
break;
}
}
return found;
}
reset() {
this.eofReached = false;
this.linesCache = [];
this.fdPosition = 0;
}
close() {
fs.closeSync(this.fd);
this.fd = null;
}
_extractLines(buffer) {
let line;
const lines = [];
let bufferPosition = 0;
let lastNewLineBufferPosition = 0;
while (true) {
let bufferPositionValue = buffer[bufferPosition++];
if (bufferPositionValue === this.newLineCharacter) {
line = buffer.slice(lastNewLineBufferPosition, bufferPosition);
lines.push(line);
lastNewLineBufferPosition = bufferPosition;
} else if (bufferPositionValue === undefined) {
break;
}
}
let leftovers = buffer.slice(lastNewLineBufferPosition, bufferPosition);
if (leftovers.length) {
lines.push(leftovers);
}
return lines;
};
_readChunk(lineLeftovers) {
let totalBytesRead = 0;
let bytesRead;
const buffers = [];
do {
const readBuffer = new Buffer(this.options.readChunk);
bytesRead = fs.readSync(this.fd, readBuffer, 0, this.options.readChunk, this.fdPosition);
totalBytesRead = totalBytesRead + bytesRead;
this.fdPosition = this.fdPosition + bytesRead;
buffers.push(readBuffer);
} while (bytesRead && this._searchInBuffer(buffers[buffers.length-1], this.options.newLineCharacter) === -1);
let bufferData = Buffer.concat(buffers);
if (bytesRead < this.options.readChunk) {
this.eofReached = true;
bufferData = bufferData.slice(0, totalBytesRead);
}
if (totalBytesRead) {
this.linesCache = this._extractLines(bufferData);
if (lineLeftovers) {
this.linesCache[0] = Buffer.concat([lineLeftovers, this.linesCache[0]]);
}
}
return totalBytesRead;
}
next() {
if (!this.fd) return false;
let line = false;
if (this.eofReached && this.linesCache.length === 0) {
return line;
}
let bytesRead;
if (!this.linesCache.length) {
bytesRead = this._readChunk();
}
if (this.linesCache.length) {
line = this.linesCache.shift();
const lastLineCharacter = line[line.length-1];
if (lastLineCharacter !== this.newLineCharacter) {
bytesRead = this._readChunk(line);
if (bytesRead) {
line = this.linesCache.shift();
}
}
}
if (this.eofReached && this.linesCache.length === 0) {
this.close();
}
if (line && line[line.length-1] === this.newLineCharacter) {
line = line.slice(0, line.length-1);
}
return line;
}
}
......@@ -24,7 +24,7 @@
"type": "module",
"scripts": {
"test": "node test/index.js | npx tap-spec",
"coverage": "npx c8 node test/index.js | npx tap-nyc"
"coverage": "npx c8 --exclude='lib/LineByLine.js' --exclude='test/**' node test/index.js | npx tap-nyc"
},
"funding": {
"type": "foundation",
......@@ -40,9 +40,7 @@
"url": "https://ar.al"
},
"license": "AGPL-3.0-or-later",
"dependencies": {
"n-readlines": "^1.0.1"
},
"dependencies": {},
"devDependencies": {
"c8": "^7.6.0",
"esbuild": "^0.8.52",
......
......@@ -22,14 +22,9 @@ import JSTable from '../lib/JSTable.js'
import JSDF from '../lib/JSDF.js'
import Time from '../lib/Time.js'
import QuerySanitiser from '../lib/QuerySanitiser.js'
import LineByLine from '../lib/LineByLine.js'
import { needsToBeProxified, log } from '../lib/Util.js'
import { types } from 'util'
const isProxy = types.isProxy
import LineByLine from 'n-readlines'
import { debugPort } from 'process'
// Implement __dirname functionality for ESM.
const __dirname = new URL('.', import.meta.url).pathname
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment