eternal-timer 1.0.1 → 1.1.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/README.md CHANGED
@@ -1,2 +1,107 @@
1
1
  # eternal-timer
2
- timer for node.js package
2
+
3
+ A simple and persistent timer library for Node.js. Timers are saved to a file and maintain their state even after process restart.
4
+
5
+ ## Features
6
+
7
+ - **Create Timers**: Create timers for a specified duration
8
+ - **Remove Timers**: Delete timers by ID
9
+ - **Monitor Timers**: Handle expired timers with callback functions
10
+ - **List Timers**: Retrieve all active timers
11
+ - **Persistence**: Save timer data to a file that persists across process restarts
12
+
13
+ ## Installation
14
+
15
+ ```bash
16
+ npm install eternal-timer
17
+ ```
18
+
19
+ ## Usage
20
+
21
+ ### Basic Example
22
+
23
+ ```typescript
24
+ import { createTimer, checkTimers, removeTimer, showTimers } from 'eternal-timer';
25
+
26
+ // Create a timer (5 seconds)
27
+ const timerId = await createTimer(5000);
28
+ console.log('Timer created:', timerId);
29
+
30
+ // Monitor timers (executes when timer expires)
31
+ await checkTimers((timer) => {
32
+ console.log('Timer expired:', timer.id);
33
+ });
34
+
35
+ // Display all timers
36
+ const timers = await showTimers();
37
+ console.log('Active timers:', timers);
38
+
39
+ // Remove a timer
40
+ await removeTimer(timerId);
41
+ ```
42
+
43
+ ## API
44
+
45
+ ### `createTimer(length: number): Promise<string | null>`
46
+
47
+ Creates a new timer.
48
+
49
+ **Parameters:**
50
+ - `length` (number): Timer duration in milliseconds
51
+
52
+ **Returns:** Timer ID (UUID), or `null` on error
53
+
54
+ ### `removeTimer(id: string): Promise<boolean>`
55
+
56
+ Removes a timer by ID.
57
+
58
+ **Parameters:**
59
+ - `id` (string): ID of the timer to remove
60
+
61
+ **Returns:** `true` on success, `false` on failure
62
+
63
+ ### `checkTimers(callback: (timer: Timer) => void, interval?: number): Promise<void>`
64
+
65
+ Monitors expired timers and executes the callback function.
66
+
67
+ **Parameters:**
68
+ - `callback`: Function to execute when expired timers are found
69
+ - `interval` (number, optional): Check interval in milliseconds (default: 50ms)
70
+
71
+ ### `showTimers(): Promise<Timer[]>`
72
+
73
+ Retrieves all active timers.
74
+
75
+ **Returns:** Array of `Timer` objects
76
+
77
+ ## Type Definition
78
+
79
+ ```typescript
80
+ type Timer = {
81
+ id: string; // Unique timer identifier (UUID)
82
+ start: string; // Timer start timestamp
83
+ stop: string; // Timer end timestamp
84
+ }
85
+ ```
86
+
87
+ ## Scripts
88
+
89
+ - `npm run dev`: Run in development mode with nodemon
90
+ - `npm run build`: Compile TypeScript
91
+ - `npm start`: Run compiled JavaScript
92
+
93
+ ## Storage
94
+
95
+ Timer data is stored in the `.timers` file in the project root. Each line follows this format:
96
+
97
+ ```
98
+ {id} {start_timestamp} {stop_timestamp}
99
+ ```
100
+
101
+ ## License
102
+
103
+ ISC
104
+
105
+ ## Repository
106
+
107
+ https://github.com/SUKEsann2000/eternal-timer
@@ -0,0 +1,6 @@
1
+ import type { Timer } from "types/timersData.ts";
2
+ export declare function createTimer(length: number): Promise<string | null>;
3
+ export declare function removeTimer(id: string): Promise<boolean>;
4
+ export declare function checkTimers(callback: (timer: Timer) => void, interval?: number): Promise<void>;
5
+ export declare function showTimers(): Promise<Timer[]>;
6
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,qBAAqB,CAAC;AAejD,wBAAsB,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAaxE;AAED,wBAAsB,WAAW,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAc9D;AAED,wBAAsB,WAAW,CAAC,QAAQ,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,EAAE,QAAQ,GAAE,MAAW,iBA+BxF;AAED,wBAAsB,UAAU,IAAI,OAAO,CAAC,KAAK,EAAE,CAAC,CAoBnD"}
package/dist/index.js ADDED
@@ -0,0 +1,93 @@
1
+ import fs from "fs";
2
+ import path from "path";
3
+ import searchRoot from "./searchRoot.js";
4
+ import { v4 as uuidv4 } from "uuid";
5
+ // search root folder of project
6
+ const rootdir = searchRoot();
7
+ const timerfiledir = path.join(rootdir, ".timers");
8
+ async function createFile() {
9
+ if (!fs.existsSync(timerfiledir)) {
10
+ fs.writeFile(timerfiledir, "", (data) => {
11
+ console.log("The file was created in ", timerfiledir);
12
+ });
13
+ }
14
+ return;
15
+ }
16
+ export async function createTimer(length) {
17
+ try {
18
+ await createFile();
19
+ // uuid, start, end
20
+ const id = uuidv4();
21
+ const newTimerData = `${id} ${Date.now().toString()} ${(Date.now() + length).toString()}`;
22
+ fs.appendFileSync(timerfiledir, newTimerData + "\n");
23
+ return id;
24
+ }
25
+ catch (e) {
26
+ console.error(`Error when creating timer: ${e}`);
27
+ return null;
28
+ }
29
+ }
30
+ export async function removeTimer(id) {
31
+ try {
32
+ const timersRaw = fs.readFileSync(timerfiledir, "utf-8");
33
+ const timersData = timersRaw.split(/\r?\n/);
34
+ const filteredTimers = timersData.filter(line => line.split(" ")[0] !== id);
35
+ fs.writeFileSync(timerfiledir, filteredTimers.join("\n"), "utf-8");
36
+ return true;
37
+ }
38
+ catch (e) {
39
+ console.error(`Error when removing timer: ${e}`);
40
+ return false;
41
+ }
42
+ }
43
+ export async function checkTimers(callback, interval = 50) {
44
+ try {
45
+ await createFile();
46
+ setInterval(() => {
47
+ const timersData = fs.readFileSync(timerfiledir, "utf-8").split(/\r?\n/);
48
+ const timersSet = new Set();
49
+ for (const timerData of timersData) {
50
+ if (!timerData.trim())
51
+ continue;
52
+ const [id, startStr, stopStr] = timerData.split(" ");
53
+ timersSet.add({
54
+ id: id,
55
+ start: startStr,
56
+ stop: stopStr
57
+ });
58
+ }
59
+ const now = Date.now();
60
+ for (const timer of timersSet) {
61
+ if (Number(timer.stop) <= now) {
62
+ removeTimer(timer.id);
63
+ callback(timer);
64
+ }
65
+ }
66
+ }, interval);
67
+ }
68
+ catch (e) {
69
+ throw new Error(`Error when checking alarm: ${e}`);
70
+ }
71
+ }
72
+ export async function showTimers() {
73
+ try {
74
+ await createFile();
75
+ const timersRaw = fs.readFileSync(timerfiledir, "utf-8");
76
+ const timersData = timersRaw.split(/\r?\n/);
77
+ let timersJSON = [];
78
+ for (const timerData of timersData) {
79
+ const splitedTimerData = timerData.split(" ");
80
+ timersJSON.push({
81
+ id: splitedTimerData[0],
82
+ start: splitedTimerData[1],
83
+ stop: splitedTimerData[2]
84
+ });
85
+ }
86
+ return timersJSON;
87
+ }
88
+ catch (e) {
89
+ console.error(`Error when removing timer: ${e}`);
90
+ return [];
91
+ }
92
+ }
93
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,IAAI,CAAC;AACpB,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,UAAU,MAAM,iBAAiB,CAAC;AACzC,OAAO,EAAE,EAAE,IAAI,MAAM,EAAE,MAAM,MAAM,CAAC;AAGpC,gCAAgC;AAChC,MAAM,OAAO,GAAG,UAAU,EAAE,CAAC;AAC7B,MAAM,YAAY,GAAG,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,SAAS,CAAC,CAAC;AAEnD,KAAK,UAAU,UAAU;IACrB,IAAI,CAAC,EAAE,CAAC,UAAU,CAAC,YAAY,CAAC,EAAE,CAAC;QAC/B,EAAE,CAAC,SAAS,CAAC,YAAY,EAAE,EAAE,EAAE,CAAC,IAAI,EAAE,EAAE;YACpC,OAAO,CAAC,GAAG,CAAC,0BAA0B,EAAE,YAAY,CAAC,CAAC;QAC1D,CAAC,CAAC,CAAC;IACP,CAAC;IACD,OAAO;AACX,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,WAAW,CAAC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AC,KAAK,CAAC,OAAO,CAAC,CAAC;QAEtD,IAAI,UAAU,GAAY,EAAE,CAAC;QAC7B,KAAK,MAAM,SAAS,IAAI,UAAU,EAAE,CAAC;YACjC,MAAM,gBAAgB,GAAG,SAAS,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;YAC7C,UAAU,CAAC,IAAI,CAAC;gBACZ,EAAE,EAAE,gBAAgB,CAAC,CAAC,CAAE;gBACxB,KAAK,EAAE,gBAAgB,CAAC,CAAC,CAAE;gBAC3B,IAAI,EAAE,gBAAgB,CAAC,CAAC,CAAE;aAC7B,CAAC,CAAA;QACN,CAAC;QACD,OAAO,UAAU,CAAC;IACtB,CAAC;IAAC,OAAO,CAAC,EAAE,CAAC;QACT,OAAO,CAAC,KAAK,CAAC,8BAA8B,CAAC,EAAE,CAAC,CAAC;QACjD,OAAO,EAAE,CAAC;IACd,CAAC;AACL,CAAC"}
@@ -0,0 +1,2 @@
1
+ export default function searchRoot(): string;
2
+ //# sourceMappingURL=searchRoot.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"searchRoot.d.ts","sourceRoot":"","sources":["../src/searchRoot.ts"],"names":[],"mappings":"AAGA,MAAM,CAAC,OAAO,UAAU,UAAU,WAQjC"}
@@ -0,0 +1,13 @@
1
+ import fs from "fs";
2
+ import path from "path";
3
+ export default function searchRoot() {
4
+ let dir = process.cwd();
5
+ while (!fs.existsSync(path.join(dir, "package.json"))) {
6
+ const parent = path.dirname(dir);
7
+ if (parent === dir)
8
+ break;
9
+ dir = parent;
10
+ }
11
+ return dir;
12
+ }
13
+ //# sourceMappingURL=searchRoot.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"searchRoot.js","sourceRoot":"","sources":["../src/searchRoot.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,IAAI,CAAC;AACpB,OAAO,IAAI,MAAM,MAAM,CAAC;AAExB,MAAM,CAAC,OAAO,UAAU,UAAU;IAC9B,IAAI,GAAG,GAAG,OAAO,CAAC,GAAG,EAAE,CAAC;IACxB,OAAO,CAAC,EAAE,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,cAAc,CAAC,CAAC,EAAE,CAAC;QACpD,MAAM,MAAM,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;QACjC,IAAI,MAAM,KAAK,GAAG;YAAE,MAAM;QACtB,GAAG,GAAG,MAAM,CAAC;IACrB,CAAC;IACD,OAAO,GAAG,CAAC;AACf,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eternal-timer",
3
- "version": "1.0.1",
3
+ "version": "1.1.0",
4
4
  "description": "timer for node.js package",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -30,5 +30,9 @@
30
30
  "dependencies": {
31
31
  "uuid": "^13.0.0"
32
32
  },
33
- "files": [ "dist" ]
33
+ "files": [ "dist" ],
34
+ "publishConfig": {
35
+ "registry": "https://registry.npmjs.org/",
36
+ "access": "public"
37
+ }
34
38
  }