readeof 0.0.1-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Ashary Vermaysha
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,95 @@
1
+ # ReadEOF
2
+
3
+ A high-performance, memory-efficient library to read large files backward (line-by-line). Zero dependencies.
4
+
5
+ ## Features
6
+
7
+ - Fast file reading from the end without loading entire file into memory
8
+ - Memory efficient using configurable buffer sizes
9
+ - Supports various encodings
10
+
11
+ ## Installation
12
+
13
+ ```bash
14
+ npm install readeof
15
+ ```
16
+
17
+ ## Usage
18
+
19
+ ### Basic Example
20
+
21
+ ```typescript
22
+ import { readeof } from 'readeof';
23
+
24
+ // Read last 10 lines from log file
25
+ const lastLines = await readeof('/var/log/app.log', 10);
26
+ console.log(lastLines);
27
+ ```
28
+
29
+ ### Reading Recent Logs
30
+
31
+ ```typescript
32
+ import { readeof } from 'readeof';
33
+
34
+ // Read last 50 lines for debugging
35
+ const recentLogs = await readeof('./app.log', 50);
36
+ console.log(recentLogs);
37
+ ```
38
+
39
+ ### Custom Encoding
40
+
41
+ ```typescript
42
+ import { readeof } from 'readeof';
43
+
44
+ // Read file with latin1 encoding
45
+ const content = await readeof('./data.txt', 20, 'latin1');
46
+ ```
47
+
48
+ ### Custom Buffer Size
49
+
50
+ ```typescript
51
+ import { readeof } from 'readeof';
52
+
53
+ // Use 64KB buffer for very large files
54
+ const content = await readeof('./huge-log.log', 100, 'utf8', 64 * 1024);
55
+ ```
56
+
57
+ ### Real-time Log Monitoring
58
+
59
+ ```typescript
60
+ import { readeof } from 'readeof';
61
+
62
+ async function monitorLog(filePath: string) {
63
+ setInterval(async () => {
64
+ const latestLines = await readeof(filePath, 5);
65
+ console.clear();
66
+ console.log('=== Latest Logs ===');
67
+ console.log(latestLines);
68
+ }, 2000);
69
+ }
70
+
71
+ monitorLog('/var/log/application.log');
72
+ ```
73
+
74
+ ### Error Analysis
75
+
76
+ ```typescript
77
+ import { readeof } from 'readeof';
78
+
79
+ async function findRecentErrors(logPath: string) {
80
+ const recentLogs = await readeof(logPath, 1000);
81
+ const errors = recentLogs
82
+ .split('\n')
83
+ .filter(line => line.includes('ERROR'));
84
+
85
+ console.log(`Found ${errors.length} errors in last 1000 lines`);
86
+ }
87
+ ```
88
+
89
+ ## License
90
+
91
+ See [LICENSE](LICENSE) file for details.
92
+
93
+ ## Author
94
+
95
+ Ashary Vermaysha (<vermaysha@gmail.com>)
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Reads the last N lines from a file efficiently.
3
+ *
4
+ * @param filePath - Path to the file to read from
5
+ * @param maxLines - Total lines to read from the end of the file
6
+ * @param encoding - File encoding (default: 'utf8')
7
+ * Available encodings: https://nodejs.org/api/buffer.html#buffers-and-character-encodings
8
+ * @param bufferSize - Buffer size to use when reading the file (default: 16KB)
9
+ * A larger buffer size may improve performance for large files.
10
+ * @returns A promise that resolves to a string containing the last N lines of the file
11
+ */
12
+ export declare function readeof(
13
+ /**
14
+ * Path to the file to read from
15
+ */
16
+ filePath: string,
17
+ /**
18
+ * Total lines to read from the end of the file
19
+ */
20
+ maxLines: number,
21
+ /**
22
+ * File encoding (default: 'utf8')
23
+ *
24
+ * Available encodings: https://nodejs.org/api/buffer.html#buffers-and-character-encodings
25
+ */
26
+ encoding?: BufferEncoding,
27
+ /**
28
+ * Buffer size to use when reading the file (default: 16KB)
29
+ *
30
+ * A larger buffer size may improve performance for large files.
31
+ */
32
+ bufferSize?: number): Promise<string>;
package/dist/index.js ADDED
@@ -0,0 +1,81 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropNames = Object.getOwnPropertyNames;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __moduleCache = /* @__PURE__ */ new WeakMap;
6
+ var __toCommonJS = (from) => {
7
+ var entry = __moduleCache.get(from), desc;
8
+ if (entry)
9
+ return entry;
10
+ entry = __defProp({}, "__esModule", { value: true });
11
+ if (from && typeof from === "object" || typeof from === "function")
12
+ __getOwnPropNames(from).map((key) => !__hasOwnProp.call(entry, key) && __defProp(entry, key, {
13
+ get: () => from[key],
14
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
15
+ }));
16
+ __moduleCache.set(from, entry);
17
+ return entry;
18
+ };
19
+ var __export = (target, all) => {
20
+ for (var name in all)
21
+ __defProp(target, name, {
22
+ get: all[name],
23
+ enumerable: true,
24
+ configurable: true,
25
+ set: (newValue) => all[name] = () => newValue
26
+ });
27
+ };
28
+
29
+ // src/index.ts
30
+ var exports_src = {};
31
+ __export(exports_src, {
32
+ readeof: () => readeof
33
+ });
34
+ module.exports = __toCommonJS(exports_src);
35
+ var import_promises = require("node:fs/promises");
36
+ async function readeof(filePath, maxLines, encoding = "utf8", bufferSize = 16 * 1024) {
37
+ if (maxLines <= 0)
38
+ return "";
39
+ let fileHandle = null;
40
+ try {
41
+ fileHandle = await import_promises.open(filePath, "r");
42
+ const stat = await fileHandle.stat();
43
+ if (stat.size === 0)
44
+ return "";
45
+ const buffer = Buffer.alloc(bufferSize);
46
+ let linesFound = 0;
47
+ let position = stat.size;
48
+ let startReadPos = stat.size;
49
+ while (position > 0 && linesFound < maxLines) {
50
+ const readLength = Math.min(bufferSize, position);
51
+ position -= readLength;
52
+ const result = await fileHandle.read(buffer, 0, readLength, position);
53
+ const bytesRead = result.bytesRead;
54
+ for (let i = bytesRead - 1;i >= 0; i--) {
55
+ if (buffer[i] === 10) {
56
+ if (position + i !== stat.size - 1 || linesFound > 0) {
57
+ linesFound++;
58
+ }
59
+ if (linesFound >= maxLines) {
60
+ startReadPos = position + i + 1;
61
+ break;
62
+ }
63
+ }
64
+ }
65
+ }
66
+ if (linesFound < maxLines) {
67
+ startReadPos = 0;
68
+ }
69
+ const lengthToRead = stat.size - startReadPos;
70
+ const resultBuffer = Buffer.alloc(lengthToRead);
71
+ await fileHandle.read(resultBuffer, 0, lengthToRead, startReadPos);
72
+ return resultBuffer.toString(encoding);
73
+ } catch (error) {
74
+ throw error;
75
+ } finally {
76
+ console.log("Closing file handle");
77
+ if (fileHandle) {
78
+ await fileHandle.close();
79
+ }
80
+ }
81
+ }
package/dist/index.mjs ADDED
@@ -0,0 +1,51 @@
1
+ // src/index.ts
2
+ import { open } from "node:fs/promises";
3
+ async function readeof(filePath, maxLines, encoding = "utf8", bufferSize = 16 * 1024) {
4
+ if (maxLines <= 0)
5
+ return "";
6
+ let fileHandle = null;
7
+ try {
8
+ fileHandle = await open(filePath, "r");
9
+ const stat = await fileHandle.stat();
10
+ if (stat.size === 0)
11
+ return "";
12
+ const buffer = Buffer.alloc(bufferSize);
13
+ let linesFound = 0;
14
+ let position = stat.size;
15
+ let startReadPos = stat.size;
16
+ while (position > 0 && linesFound < maxLines) {
17
+ const readLength = Math.min(bufferSize, position);
18
+ position -= readLength;
19
+ const result = await fileHandle.read(buffer, 0, readLength, position);
20
+ const bytesRead = result.bytesRead;
21
+ for (let i = bytesRead - 1;i >= 0; i--) {
22
+ if (buffer[i] === 10) {
23
+ if (position + i !== stat.size - 1 || linesFound > 0) {
24
+ linesFound++;
25
+ }
26
+ if (linesFound >= maxLines) {
27
+ startReadPos = position + i + 1;
28
+ break;
29
+ }
30
+ }
31
+ }
32
+ }
33
+ if (linesFound < maxLines) {
34
+ startReadPos = 0;
35
+ }
36
+ const lengthToRead = stat.size - startReadPos;
37
+ const resultBuffer = Buffer.alloc(lengthToRead);
38
+ await fileHandle.read(resultBuffer, 0, lengthToRead, startReadPos);
39
+ return resultBuffer.toString(encoding);
40
+ } catch (error) {
41
+ throw error;
42
+ } finally {
43
+ console.log("Closing file handle");
44
+ if (fileHandle) {
45
+ await fileHandle.close();
46
+ }
47
+ }
48
+ }
49
+ export {
50
+ readeof
51
+ };
package/package.json ADDED
@@ -0,0 +1,60 @@
1
+ {
2
+ "name": "readeof",
3
+ "description": "A high-performance, memory-efficient library to read large files backward (line-by-line). Optimized for Node.js and Bun log parsing.",
4
+ "version": "0.0.1-beta.0",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "module": "./dist/index.mjs",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ "./package.json": "./package.json",
11
+ ".": {
12
+ "types": "./dist/index.d.ts",
13
+ "import": "./dist/index.mjs",
14
+ "require": "./dist/index.js"
15
+ }
16
+ },
17
+ "files": [
18
+ "dist/**"
19
+ ],
20
+ "scripts": {
21
+ "bundle": "bun run ./scripts/build.ts && tsc ./src/index.ts --declaration --emitDeclarationOnly --outDir dist",
22
+ "benchmark": "bun run ./scripts/benchmark.ts",
23
+ "test": "bun test",
24
+ "test:watch": "bun test --watch"
25
+ },
26
+ "devDependencies": {
27
+ "@types/bun": "latest"
28
+ },
29
+ "peerDependencies": {
30
+ "typescript": "^5.9.3"
31
+ },
32
+ "dependencies": {
33
+ "mitata": "^1.0.34"
34
+ },
35
+ "keywords": [
36
+ "file-system",
37
+ "reverse",
38
+ "backward",
39
+ "tail",
40
+ "read-reverse",
41
+ "line-reader",
42
+ "log-parser",
43
+ "large-files",
44
+ "read-last-lines"
45
+ ],
46
+ "author": {
47
+ "name": "Ashary Vermaysha",
48
+ "email": "vermaysha@gmail.com",
49
+ "url": "https://github.com/vermaysha"
50
+ },
51
+ "license": "MIT",
52
+ "repository": {
53
+ "type": "git",
54
+ "url": "git+https://github.com/vermaysha/readeof.git"
55
+ },
56
+ "bugs": {
57
+ "url": "https://github.com/vermaysha/readeof/issues"
58
+ },
59
+ "homepage": "https://github.com/vermaysha/readeof#readme"
60
+ }