@jmlq/logger-plugin-fs 0.1.0-alpha.0 → 0.1.0-alpha.2

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 CHANGED
@@ -1,11 +1,13 @@
1
- import { ILogDatasource, Log } from "@jmlq/logger";
1
+ import { ILogDatasource, ILog } 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 fd?;
5
- constructor(opts: {
6
- filePath: string;
7
- });
8
- private ensureOpen;
9
- save(log: Log): Promise<void>;
7
+ private stream;
8
+ constructor(opts: FsOpts);
9
+ save(log: ILog): 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 __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
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 fs_1 = require("fs");
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
- async ensureOpen() {
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
- await this.ensureOpen();
15
+ if (!this.stream)
16
+ throw new Error("FS stream closed");
53
17
  const line = JSON.stringify(log) + "\n";
54
- await fs_1.promises.appendFile(this.opts.filePath, line, "utf8");
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.fd) {
58
- await fs_1.promises.close(this.fd);
59
- this.fd = undefined;
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;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jmlq/logger-plugin-fs",
3
- "version": "0.1.0-alpha.0",
3
+ "version": "0.1.0-alpha.2",
4
4
  "author": "MLahuasi",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -9,14 +9,14 @@
9
9
  ],
10
10
  "scripts": {
11
11
  "build": "tsc -p tsconfig.json",
12
- "prepublishOnly": "pnpm build"
12
+ "prepublishOnly": "npm run build"
13
13
  },
14
14
  "devDependencies": {
15
15
  "@types/node": "^24.3.0",
16
16
  "tsx": "^4.20.5",
17
17
  "typescript": "^5.9.2"
18
18
  },
19
- "peerDependencies": {
20
- "@jmlq/logger": "^0.1.0-alpha.0"
19
+ "dependencies": {
20
+ "@jmlq/logger": "^0.1.0-alpha.2"
21
21
  }
22
22
  }