@endgame45/nodescript 1.1.0 → 1.2.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 horizonstudios
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,85 @@
1
+ # nodescript
2
+
3
+ Node.js logging and scheduling utility with easy chalk styling.
4
+
5
+ Features
6
+ - Simple styled logging: nsc.log, nsc.info, nsc.warn, nsc.error, nsc.debug
7
+ - Namespaced loggers: nsc.create(namespace, opts)
8
+ - Auto color detection (respects NO_COLOR and TTY)
9
+ - Timestamping (ISO/local/custom)
10
+ - Color parsing: hex, rgb/rgba, hsl/hsla, CSS keywords (alpha blended)
11
+ - Themes and tagged-template styling
12
+ - Flow control utilities: debounce, throttle, retry/backoff, pLimit
13
+ - Looping helpers with concurrency
14
+
15
+ Installation
16
+
17
+ npm install nodescript
18
+
19
+ Quick examples
20
+
21
+ Basic logging
22
+
23
+ ```javascript
24
+ const nsc = require('nodescript');
25
+ nsc.info('Hello %s', 'world');
26
+ nsc.warn('This is a warning');
27
+ nsc.error('Oops: %s', 'failure');
28
+
29
+ // change global level
30
+ nsc.level = 'debug';
31
+ ```
32
+
33
+ Namespaced logger
34
+
35
+ ```javascript
36
+ const db = nsc.create('db', { textColor: 'cyan', level: 'debug', timestamp: { enabled: true } });
37
+ db.debug('Connected to %s', 'postgres://...');
38
+ ```
39
+
40
+ Themes
41
+
42
+ ```javascript
43
+ nsc.registerTheme('errorTheme', { textColor: '#ff4444', font: 'bold', level: 'error' });
44
+ const err = nsc.useTheme('errorTheme');
45
+ err.error('Fatal: %s', 'something broke');
46
+ ```
47
+
48
+ Tagged template styling
49
+
50
+ ```javascript
51
+ const name = 'Alice';
52
+ console.log(nsc.style`Hello, ${name}!`);
53
+ nsc.tagLog`Processed ${42} items for ${() => name}`;
54
+ ```
55
+
56
+ Looping and concurrency
57
+
58
+ ```javascript
59
+ // run task 50 times with up to 5 concurrent executions
60
+ await nsc.loop.duration('200ms').call(asyncTask).concurrency(5).times(50);
61
+
62
+ // start infinite loop with 2 workers
63
+ const ctl = nsc.loop.duration('1s').call(task).concurrency(2).start();
64
+ // stop
65
+ ctl.stop();
66
+ await ctl.done;
67
+ ```
68
+
69
+ Utilities
70
+
71
+ ```javascript
72
+ const deb = nsc.debounce(() => console.log('debounced'), 200);
73
+ deb();
74
+
75
+ const safe = await nsc.retry(async () => fetchSomething(), { retries: 5 });
76
+ await safe();
77
+
78
+ const limit = nsc.pLimit(3);
79
+ const limited = limit(async (id) => fetch(id));
80
+ await Promise.all(ids.map(id => limited(id)));
81
+ ```
82
+
83
+ License
84
+
85
+ MIT — see LICENSE file.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@endgame45/nodescript",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "main": "src/nodescript.js",
5
5
  "files": [
6
6
  "src"
package/src/nodescript.js CHANGED
@@ -1,5 +1,6 @@
1
1
  const util = require('util');
2
2
  const chalk = require('chalk');
3
+ const utils = require('./utils');
3
4
 
4
5
  const SUPPORTED_FONTS = new Set([
5
6
  'bold', 'dim', 'italic', 'underline', 'inverse', 'hidden', 'strikethrough'
@@ -161,6 +162,7 @@ class LoopBuilder {
161
162
  this.fn = null;
162
163
  this.fnArgs = [];
163
164
  this._stopped = false;
165
+ this.concurrency = 1;
164
166
  }
165
167
 
166
168
  // set duration (accepts same inputs as parseDuration)
@@ -170,6 +172,12 @@ class LoopBuilder {
170
172
  }
171
173
  every(d) { return this.duration(d); }
172
174
 
175
+ // set concurrency for parallel execution (for times/start)
176
+ concurrency(n) {
177
+ this.concurrency = Math.max(1, Math.floor(Number(n) || 1));
178
+ return this;
179
+ }
180
+
173
181
  // set target function to call; fn can be sync or async
174
182
  call(fn, ...args) {
175
183
  if (typeof fn !== 'function') {
@@ -196,12 +204,41 @@ class LoopBuilder {
196
204
  if (!Number.isFinite(n) || n < 0) throw new TypeError('times(n) expects a non-negative number');
197
205
 
198
206
  this._stopped = false;
199
- for (let i = 0; i < n; i++) {
200
- if (this._stopped) break;
201
- const res = this.fn(...this.fnArgs);
202
- if (res && typeof res.then === 'function') await res;
203
- if (i < n - 1 && this.durationMs > 0) await sleep(this.durationMs);
207
+ if (!this.concurrency || this.concurrency <= 1) {
208
+ for (let i = 0; i < n; i++) {
209
+ if (this._stopped) break;
210
+ const res = this.fn(...this.fnArgs);
211
+ if (res && typeof res.then === 'function') await res;
212
+ if (i < n - 1 && this.durationMs > 0) await sleep(this.durationMs);
213
+ }
214
+ return this;
204
215
  }
216
+
217
+ // concurrency > 1: launch up to concurrency parallel runners
218
+ let launched = 0;
219
+ const errors = [];
220
+
221
+ const runner = async () => {
222
+ while (!this._stopped) {
223
+ const idx = launched++;
224
+ if (idx >= n) break;
225
+ try {
226
+ const res = this.fn(...this.fnArgs);
227
+ if (res && typeof res.then === 'function') await res;
228
+ } catch (err) {
229
+ errors.push(err);
230
+ this._stopped = true;
231
+ break;
232
+ }
233
+ if (this.durationMs > 0 && launched < n) await sleep(this.durationMs);
234
+ }
235
+ };
236
+
237
+ const runners = [];
238
+ const concurrency = Math.min(this.concurrency, n);
239
+ for (let i = 0; i < concurrency; i++) runners.push(runner());
240
+ await Promise.all(runners);
241
+ if (errors.length) throw errors[0];
205
242
  return this;
206
243
  }
207
244
 
@@ -249,9 +286,10 @@ class LoopBuilder {
249
286
  start() {
250
287
  if (!this.fn) throw new Error('No function set. Use .call(fn) before .start()');
251
288
  this._stopped = false;
252
- const done = (async () => {
253
- try {
254
- // run forever until stopped
289
+ const runners = [];
290
+ const concurrency = Math.max(1, this.concurrency || 1);
291
+ for (let i = 0; i < concurrency; i++) {
292
+ const p = (async () => {
255
293
  while (!this._stopped) {
256
294
  const res = this.fn(...this.fnArgs);
257
295
  if (res && typeof res.then === 'function') await res;
@@ -259,14 +297,12 @@ class LoopBuilder {
259
297
  await Promise.race([sleep(this.durationMs), new Promise(r => { this._stopResolve = r; })]);
260
298
  }
261
299
  }
262
- } finally {
263
- // noop
264
- }
265
- })();
266
- return {
267
- stop: () => this.stop(),
268
- done
269
- };
300
+ })();
301
+ runners.push(p);
302
+ }
303
+
304
+ const done = Promise.all(runners).then(() => undefined);
305
+ return { stop: () => this.stop(), done };
270
306
  }
271
307
  }
272
308
 
@@ -481,6 +517,12 @@ const nsc = {
481
517
  console.log(s);
482
518
  },
483
519
 
520
+ // utilities: debounce/throttle/retry/pLimit
521
+ debounce: utils.debounce,
522
+ throttle: utils.throttle,
523
+ retry: utils.retry,
524
+ pLimit: utils.pLimit,
525
+
484
526
  /**
485
527
  * Wait for the given duration and resolve the returned Promise.
486
528
  * - numeric values are treated as seconds
package/src/utils.js ADDED
@@ -0,0 +1,135 @@
1
+ // Utility helpers: debounce, throttle, retry/backoff, pLimit
2
+ 'use strict';
3
+
4
+ function debounce(fn, wait = 0, options = {}) {
5
+ let timer = null;
6
+ let lastArgs = null;
7
+ let lastThis = null;
8
+ let resolveFlush = null;
9
+
10
+ const debounced = function (...args) {
11
+ lastArgs = args;
12
+ lastThis = this;
13
+ if (timer) clearTimeout(timer);
14
+ if (options.leading && !timer) {
15
+ fn.apply(lastThis, lastArgs);
16
+ }
17
+ return new Promise((resolve) => {
18
+ resolveFlush = resolve;
19
+ timer = setTimeout(() => {
20
+ timer = null;
21
+ if (!options.leading) fn.apply(lastThis, lastArgs);
22
+ if (resolveFlush) {
23
+ resolveFlush();
24
+ resolveFlush = null;
25
+ }
26
+ }, wait);
27
+ });
28
+ };
29
+ debounced.cancel = () => {
30
+ if (timer) clearTimeout(timer);
31
+ timer = null;
32
+ lastArgs = null;
33
+ lastThis = null;
34
+ if (resolveFlush) { resolveFlush(); resolveFlush = null; }
35
+ };
36
+ debounced.flush = () => {
37
+ if (timer) {
38
+ clearTimeout(timer);
39
+ timer = null;
40
+ if (!options.leading && lastArgs) fn.apply(lastThis, lastArgs);
41
+ if (resolveFlush) { resolveFlush(); resolveFlush = null; }
42
+ }
43
+ };
44
+ return debounced;
45
+ }
46
+
47
+ function throttle(fn, wait = 0, options = {}) {
48
+ let lastCall = 0;
49
+ let timer = null;
50
+ let lastArgs = null;
51
+ let lastThis = null;
52
+
53
+ const throttled = function (...args) {
54
+ const now = Date.now();
55
+ const remaining = wait - (now - lastCall);
56
+ lastArgs = args;
57
+ lastThis = this;
58
+ if (remaining <= 0 || remaining > wait) {
59
+ if (timer) { clearTimeout(timer); timer = null; }
60
+ lastCall = now;
61
+ fn.apply(lastThis, lastArgs);
62
+ } else if (!timer && options.trailing !== false) {
63
+ timer = setTimeout(() => {
64
+ lastCall = Date.now();
65
+ timer = null;
66
+ fn.apply(lastThis, lastArgs);
67
+ }, remaining);
68
+ }
69
+ };
70
+ throttled.cancel = () => { if (timer) clearTimeout(timer); timer = null; };
71
+ return throttled;
72
+ }
73
+
74
+ async function retry(fn, opts = {}) {
75
+ const retries = Number(opts.retries == null ? 3 : opts.retries);
76
+ const minDelay = Number(opts.minDelay == null ? 100 : opts.minDelay);
77
+ const maxDelay = Number(opts.maxDelay == null ? 1000 : opts.maxDelay);
78
+ const factor = Number(opts.factor == null ? 2 : opts.factor);
79
+ const jitter = opts.jitter !== false;
80
+
81
+ let attempt = 0;
82
+ const exec = async (...args) => {
83
+ let lastErr;
84
+ while (attempt <= retries) {
85
+ try {
86
+ return await fn(...args);
87
+ } catch (err) {
88
+ lastErr = err;
89
+ if (attempt === retries) break;
90
+ attempt += 1;
91
+ let delay = Math.min(maxDelay, minDelay * Math.pow(factor, attempt - 1));
92
+ if (jitter) delay = Math.random() * delay;
93
+ await new Promise(r => setTimeout(r, delay));
94
+ }
95
+ }
96
+ throw lastErr;
97
+ };
98
+ return exec;
99
+ }
100
+
101
+ function pLimit(concurrency = 1) {
102
+ const queue = [];
103
+ let activeCount = 0;
104
+
105
+ const next = () => {
106
+ if (queue.length === 0) return;
107
+ if (activeCount >= concurrency) return;
108
+ activeCount += 1;
109
+ const item = queue.shift();
110
+ const { fn, resolve, reject } = item;
111
+ (async () => {
112
+ try {
113
+ const result = await fn();
114
+ resolve(result);
115
+ } catch (e) {
116
+ reject(e);
117
+ } finally {
118
+ activeCount -= 1;
119
+ next();
120
+ }
121
+ })();
122
+ };
123
+
124
+ return (fn) => (...args) => new Promise((resolve, reject) => {
125
+ queue.push({ fn: () => fn(...args), resolve, reject });
126
+ next();
127
+ });
128
+ }
129
+
130
+ module.exports = {
131
+ debounce,
132
+ throttle,
133
+ retry,
134
+ pLimit
135
+ };