mongodb-log-writer
Advanced tools
Comparing version 1.2.0 to 1.3.0
/// <reference types="node" /> | ||
import { Writable } from 'stream'; | ||
type PlainWritable = Pick<Writable, 'write' | 'end'>; | ||
declare type PlainWritable = Pick<Writable, 'write' | 'end'>; | ||
export interface MongoLogId { | ||
@@ -50,2 +50,3 @@ __value: number; | ||
retentionDays: number; | ||
maxLogFileCount?: number; | ||
onerror: (err: Error, path: string) => unknown | Promise<void>; | ||
@@ -57,5 +58,5 @@ onwarn: (err: Error, path: string) => unknown | Promise<void>; | ||
constructor(options: MongoLogOptions); | ||
cleanupOldLogfiles(): Promise<void>; | ||
cleanupOldLogfiles(maxDurationMs?: number): Promise<void>; | ||
createLogWriter(): Promise<MongoLogWriter>; | ||
} | ||
export {}; |
@@ -14,2 +14,3 @@ "use strict"; | ||
const zlib_1 = require("zlib"); | ||
const heap_js_1 = require("heap-js"); | ||
function mongoLogId(id) { | ||
@@ -188,4 +189,4 @@ return { __value: id }; | ||
} | ||
async cleanupOldLogfiles() { | ||
var _a, _b; | ||
async cleanupOldLogfiles(maxDurationMs = 5000) { | ||
var _a, _b, _c; | ||
const dir = this._options.directory; | ||
@@ -196,6 +197,11 @@ let dirHandle; | ||
} | ||
catch (_c) { | ||
catch (_d) { | ||
return; | ||
} | ||
const deletionStartTimestamp = Date.now(); | ||
const deletionCutoffTimestamp = deletionStartTimestamp - this._options.retentionDays * 86400 * 1000; | ||
const leastRecentFileHeap = new heap_js_1.Heap((a, b) => a.fileTimestamp - b.fileTimestamp); | ||
for await (const dirent of dirHandle) { | ||
if (Date.now() - deletionStartTimestamp > maxDurationMs) | ||
break; | ||
if (!dirent.isFile()) | ||
@@ -206,10 +212,22 @@ continue; | ||
continue; | ||
if (+new bson_1.ObjectId(id).getTimestamp() / 1000 < | ||
(Date.now() / 1000) - this._options.retentionDays * 86400) { | ||
const toUnlink = path_1.default.join(dir, dirent.name); | ||
try { | ||
await fs_1.promises.unlink(toUnlink); | ||
const fileTimestamp = +new bson_1.ObjectId(id).getTimestamp(); | ||
const fullPath = path_1.default.join(dir, dirent.name); | ||
let toDelete; | ||
if (fileTimestamp < deletionCutoffTimestamp) { | ||
toDelete = fullPath; | ||
} | ||
else if (this._options.maxLogFileCount) { | ||
leastRecentFileHeap.push({ fullPath, fileTimestamp }); | ||
if (leastRecentFileHeap.size() > this._options.maxLogFileCount) { | ||
toDelete = (_c = leastRecentFileHeap.pop()) === null || _c === void 0 ? void 0 : _c.fullPath; | ||
} | ||
catch (err) { | ||
this._options.onerror(err, toUnlink); | ||
} | ||
if (!toDelete) | ||
continue; | ||
try { | ||
await fs_1.promises.unlink(toDelete); | ||
} | ||
catch (err) { | ||
if ((err === null || err === void 0 ? void 0 : err.code) !== 'ENOENT') { | ||
this._options.onerror(err, fullPath); | ||
} | ||
@@ -216,0 +234,0 @@ } |
{ | ||
"name": "mongodb-log-writer", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "A library for writing MongoDB logv2 messages", | ||
@@ -40,3 +40,4 @@ "keywords": [ | ||
"dependencies": { | ||
"bson": "^4.5.1 || ^5.0.0" | ||
"bson": "^4.5.1 || ^5.0.0", | ||
"heap-js": "^2.3.0" | ||
}, | ||
@@ -43,0 +44,0 @@ "devDependencies": { |
Sorry, the diff of this file is not supported yet
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
33471
337
0
2
+ Addedheap-js@^2.3.0
+ Addedheap-js@2.5.0(transitive)