@jmlq/logger-plugin-fs 0.1.0-alpha.0 → 0.1.0-alpha.1
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/dist/index.d.ts +7 -5
- package/dist/index.js +26 -49
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import { ILogDatasource, Log } from "@jmlq/logger";
|
|
2
|
+
type FsOpts = {
|
|
3
|
+
filePath: string;
|
|
4
|
+
};
|
|
2
5
|
export declare class FileSystemDatasource implements ILogDatasource {
|
|
3
6
|
private readonly opts;
|
|
4
|
-
private
|
|
5
|
-
constructor(opts:
|
|
6
|
-
filePath: string;
|
|
7
|
-
});
|
|
8
|
-
private ensureOpen;
|
|
7
|
+
private stream;
|
|
8
|
+
constructor(opts: FsOpts);
|
|
9
9
|
save(log: Log): Promise<void>;
|
|
10
|
+
flush(): Promise<void>;
|
|
10
11
|
dispose(): Promise<void>;
|
|
11
12
|
}
|
|
13
|
+
export {};
|
package/dist/index.js
CHANGED
|
@@ -1,63 +1,40 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
-
var ownKeys = function(o) {
|
|
20
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
-
var ar = [];
|
|
22
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
-
return ar;
|
|
24
|
-
};
|
|
25
|
-
return ownKeys(o);
|
|
26
|
-
};
|
|
27
|
-
return function (mod) {
|
|
28
|
-
if (mod && mod.__esModule) return mod;
|
|
29
|
-
var result = {};
|
|
30
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
-
__setModuleDefault(result, mod);
|
|
32
|
-
return result;
|
|
33
|
-
};
|
|
34
|
-
})();
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
35
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
6
|
exports.FileSystemDatasource = void 0;
|
|
37
|
-
const
|
|
38
|
-
const path = __importStar(require("path"));
|
|
7
|
+
const node_fs_1 = __importDefault(require("node:fs"));
|
|
39
8
|
class FileSystemDatasource {
|
|
40
9
|
constructor(opts) {
|
|
41
10
|
this.opts = opts;
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
if (!this.fd) {
|
|
45
|
-
await fs_1.promises.mkdir(path.dirname(this.opts.filePath), { recursive: true });
|
|
46
|
-
this.fd = await fs_1.promises
|
|
47
|
-
.open(this.opts.filePath, "a")
|
|
48
|
-
.then((f) => f.fd);
|
|
49
|
-
}
|
|
11
|
+
// Abre una vez en modo append
|
|
12
|
+
this.stream = node_fs_1.default.createWriteStream(this.opts.filePath, { flags: "a" });
|
|
50
13
|
}
|
|
51
14
|
async save(log) {
|
|
52
|
-
|
|
15
|
+
if (!this.stream)
|
|
16
|
+
throw new Error("FS stream closed");
|
|
53
17
|
const line = JSON.stringify(log) + "\n";
|
|
54
|
-
|
|
18
|
+
if (!this.stream.write(line)) {
|
|
19
|
+
// backpressure: esperar a 'drain'
|
|
20
|
+
await new Promise((resolve) => this.stream.once("drain", resolve));
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
async flush() {
|
|
24
|
+
if (!this.stream)
|
|
25
|
+
return;
|
|
26
|
+
if (!this.stream.writableNeedDrain)
|
|
27
|
+
return;
|
|
28
|
+
await new Promise((resolve) => this.stream.once("drain", resolve));
|
|
55
29
|
}
|
|
56
30
|
async dispose() {
|
|
57
|
-
if (this.
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
31
|
+
if (!this.stream)
|
|
32
|
+
return;
|
|
33
|
+
await new Promise((resolve, reject) => {
|
|
34
|
+
this.stream.end(() => resolve()); // end -> cierra el fd
|
|
35
|
+
this.stream.once("error", reject);
|
|
36
|
+
});
|
|
37
|
+
this.stream = null;
|
|
61
38
|
}
|
|
62
39
|
}
|
|
63
40
|
exports.FileSystemDatasource = FileSystemDatasource;
|