@salespark/toolkit 2.1.11 → 2.1.13

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
@@ -29,6 +29,7 @@ npm i @salespark/toolkit
29
29
  - **String utilities**: slugify, template fill, deburr, sanitize, capitalize words/sentences.
30
30
  - **Number utilities**: clamp, round, safe arithmetic/comparisons, safe parse (locale-aware), random digits, etc.
31
31
  - **Function utilities**: debounce, throttle, safeJSONParse, formatCurrency, parseName, currency conversions, etc.
32
+ - **Defer utilities**: post-return microtask scheduling, non-critical timers, after-response hooks.
32
33
  - **Boolean utilities**: safe boolean conversion with common representations
33
34
  - **Validation utilities**: IBAN validator (ISO 13616), Portuguese tax ID validator
34
35
  - **Security utilities**: Markdown XSS protection, content sanitization, risk assessment
@@ -704,6 +705,43 @@ currencyToSymbol("UNKNOWN");
704
705
  // Result: "€" (fallback)
705
706
  ```
706
707
 
708
+ ### 🕒 Defer Utilities
709
+
710
+ **`deferPostReturn(fn: () => void | Promise<void>): void`** — Runs a function right after the current call stack completes (microtask), without blocking the caller.
711
+
712
+ ```javascript
713
+ const order = [];
714
+ order.push("a");
715
+ deferPostReturn(() => order.push("b"));
716
+ order.push("c");
717
+ // After the current stack finishes (microtask): order = ["a", "c", "b"]
718
+ ```
719
+
720
+ **`deferNonCritical(fn: () => void | Promise<void>): void`** — Schedules a function as late as possible (low priority) for non-critical work.
721
+
722
+ ```javascript
723
+ deferNonCritical(() => {
724
+ // logs, metrics, cleanup — runs later without competing with main work
725
+ });
726
+ ```
727
+
728
+ **`deferAfterResponse(res: { once: (event: "finish" | "close", listener: (...args: any[]) => void) => void; writableEnded?: boolean }, fn: () => void | Promise<void>): void`** — Runs a function after the HTTP response is finished (sent to the client), scheduled soon (microtask).
729
+
730
+ ```javascript
731
+ // Example with a response-like object
732
+ deferAfterResponse(res, () => {
733
+ // post-response tasks — lightweight, run shortly after sending
734
+ });
735
+ ```
736
+
737
+ **`deferAfterResponseNonCritical(res: { once: (event: "finish" | "close", listener: (...args: any[]) => void) => void; writableEnded?: boolean }, fn: () => void | Promise<void>): void`** — Runs a function after the HTTP response is finished, scheduled as low priority (as late as possible).
738
+
739
+ ```javascript
740
+ deferAfterResponseNonCritical(res, () => {
741
+ // non-critical post-response work — scheduled with low priority
742
+ });
743
+ ```
744
+
707
745
  ### 🔒 Security Utilities
708
746
 
709
747
  **`checkMarkdownSecurity(text: string | null | undefined): SecurityCheckResult`** — Comprehensive markdown security validation with XSS protection, threat detection, and automatic sanitization.
@@ -908,5 +946,5 @@ MIT © [SalesPark](https://salespark.io)
908
946
 
909
947
  ---
910
948
 
911
- _Document version: 12_
912
- _Last update: 21-12-2025_
949
+ _Document version: 13_
950
+ _Last update: 09-01-2026_
package/dist/index.cjs CHANGED
@@ -1745,6 +1745,90 @@ var assessSecurityRisks = (risks) => {
1745
1745
  return { score, level, recommendations };
1746
1746
  };
1747
1747
 
1748
+ // src/utils/defer.ts
1749
+ var swallow = (p) => p.catch(() => {
1750
+ });
1751
+ var deferPostReturn = (fn) => {
1752
+ if (typeof fn !== "function") return;
1753
+ const run = () => {
1754
+ try {
1755
+ const result = fn();
1756
+ swallow(Promise.resolve(result));
1757
+ } catch {
1758
+ }
1759
+ };
1760
+ if (typeof queueMicrotask === "function") {
1761
+ queueMicrotask(run);
1762
+ } else {
1763
+ Promise.resolve().then(run);
1764
+ }
1765
+ };
1766
+ var deferNonCritical = (fn) => {
1767
+ if (typeof fn !== "function") return;
1768
+ const run = () => {
1769
+ try {
1770
+ const result = fn();
1771
+ swallow(Promise.resolve(result));
1772
+ } catch {
1773
+ }
1774
+ };
1775
+ if (typeof setImmediate === "function") {
1776
+ setImmediate(run);
1777
+ } else {
1778
+ setTimeout(run, 0);
1779
+ }
1780
+ };
1781
+ var deferAfterResponse = (res, fn) => {
1782
+ if (typeof fn !== "function") return;
1783
+ if (res?.writableEnded === true) {
1784
+ deferPostReturn(fn);
1785
+ return;
1786
+ }
1787
+ if (!res || typeof res.once !== "function") {
1788
+ deferPostReturn(fn);
1789
+ return;
1790
+ }
1791
+ let executed = false;
1792
+ const executeOnce = () => {
1793
+ if (executed) return;
1794
+ executed = true;
1795
+ deferPostReturn(fn);
1796
+ };
1797
+ try {
1798
+ res.once("finish", executeOnce);
1799
+ res.once("close", executeOnce);
1800
+ } catch {
1801
+ if (!executed) {
1802
+ deferPostReturn(fn);
1803
+ }
1804
+ }
1805
+ };
1806
+ var deferAfterResponseNonCritical = (res, fn) => {
1807
+ if (typeof fn !== "function") return;
1808
+ if (res?.writableEnded === true) {
1809
+ deferNonCritical(fn);
1810
+ return;
1811
+ }
1812
+ if (!res || typeof res.once !== "function") {
1813
+ deferNonCritical(fn);
1814
+ return;
1815
+ }
1816
+ let executed = false;
1817
+ const executeOnce = () => {
1818
+ if (executed) return;
1819
+ executed = true;
1820
+ deferNonCritical(fn);
1821
+ };
1822
+ try {
1823
+ res.once("finish", executeOnce);
1824
+ res.once("close", executeOnce);
1825
+ } catch {
1826
+ if (!executed) {
1827
+ deferNonCritical(fn);
1828
+ }
1829
+ }
1830
+ };
1831
+
1748
1832
  // src/index.ts
1749
1833
  var isBrowser = typeof globalThis !== "undefined" && typeof globalThis.document !== "undefined";
1750
1834
  var isNode = typeof process !== "undefined" && !!process.versions?.node;
@@ -1765,6 +1849,10 @@ exports.compact = compact;
1765
1849
  exports.currencyToSymbol = currencyToSymbol;
1766
1850
  exports.debounce = debounce;
1767
1851
  exports.deburr = deburr;
1852
+ exports.deferAfterResponse = deferAfterResponse;
1853
+ exports.deferAfterResponseNonCritical = deferAfterResponseNonCritical;
1854
+ exports.deferNonCritical = deferNonCritical;
1855
+ exports.deferPostReturn = deferPostReturn;
1768
1856
  exports.delay = delay;
1769
1857
  exports.difference = difference;
1770
1858
  exports.fill = fill;