lifecycleion 0.0.19 → 0.0.21

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.
Files changed (74) hide show
  1. package/README.md +39 -36
  2. package/dist/lib/event-emitter.cjs +195 -9
  3. package/dist/lib/event-emitter.cjs.map +1 -1
  4. package/dist/lib/event-emitter.js +195 -9
  5. package/dist/lib/event-emitter.js.map +1 -1
  6. package/dist/lib/global-event-target.cjs +217 -0
  7. package/dist/lib/global-event-target.cjs.map +1 -0
  8. package/dist/lib/global-event-target.d.cts +70 -0
  9. package/dist/lib/global-event-target.d.ts +70 -0
  10. package/dist/lib/global-event-target.js +189 -0
  11. package/dist/lib/global-event-target.js.map +1 -0
  12. package/dist/lib/http-client/index.cjs +554 -193
  13. package/dist/lib/http-client/index.cjs.map +1 -1
  14. package/dist/lib/http-client/index.d.cts +9 -3
  15. package/dist/lib/http-client/index.d.ts +9 -3
  16. package/dist/lib/http-client/index.js +554 -193
  17. package/dist/lib/http-client/index.js.map +1 -1
  18. package/dist/lib/http-client-mock/index.cjs +71 -24
  19. package/dist/lib/http-client-mock/index.cjs.map +1 -1
  20. package/dist/lib/http-client-mock/index.d.cts +50 -2
  21. package/dist/lib/http-client-mock/index.d.ts +50 -2
  22. package/dist/lib/http-client-mock/index.js +71 -24
  23. package/dist/lib/http-client-mock/index.js.map +1 -1
  24. package/dist/lib/http-client-node/index.cjs +135 -33
  25. package/dist/lib/http-client-node/index.cjs.map +1 -1
  26. package/dist/lib/http-client-node/index.d.cts +33 -2
  27. package/dist/lib/http-client-node/index.d.ts +33 -2
  28. package/dist/lib/http-client-node/index.js +135 -33
  29. package/dist/lib/http-client-node/index.js.map +1 -1
  30. package/dist/lib/http-client-xhr/index.cjs +0 -14
  31. package/dist/lib/http-client-xhr/index.cjs.map +1 -1
  32. package/dist/lib/http-client-xhr/index.d.cts +3 -2
  33. package/dist/lib/http-client-xhr/index.d.ts +3 -2
  34. package/dist/lib/http-client-xhr/index.js +0 -14
  35. package/dist/lib/http-client-xhr/index.js.map +1 -1
  36. package/dist/lib/lifecycle-manager/index.cjs +199 -13
  37. package/dist/lib/lifecycle-manager/index.cjs.map +1 -1
  38. package/dist/lib/lifecycle-manager/index.js +199 -13
  39. package/dist/lib/lifecycle-manager/index.js.map +1 -1
  40. package/dist/lib/logger/index.cjs +203 -21
  41. package/dist/lib/logger/index.cjs.map +1 -1
  42. package/dist/lib/logger/index.d.cts +5 -1
  43. package/dist/lib/logger/index.d.ts +5 -1
  44. package/dist/lib/logger/index.js +203 -21
  45. package/dist/lib/logger/index.js.map +1 -1
  46. package/dist/lib/lru-cache/index.cjs +195 -9
  47. package/dist/lib/lru-cache/index.cjs.map +1 -1
  48. package/dist/lib/lru-cache/index.js +195 -9
  49. package/dist/lib/lru-cache/index.js.map +1 -1
  50. package/dist/lib/process-signal-manager.cjs +199 -13
  51. package/dist/lib/process-signal-manager.cjs.map +1 -1
  52. package/dist/lib/process-signal-manager.js +199 -13
  53. package/dist/lib/process-signal-manager.js.map +1 -1
  54. package/dist/lib/promise-protected-resolver.cjs +195 -9
  55. package/dist/lib/promise-protected-resolver.cjs.map +1 -1
  56. package/dist/lib/promise-protected-resolver.js +195 -9
  57. package/dist/lib/promise-protected-resolver.js.map +1 -1
  58. package/dist/lib/retry-utils/index.cjs +195 -9
  59. package/dist/lib/retry-utils/index.cjs.map +1 -1
  60. package/dist/lib/retry-utils/index.js +195 -9
  61. package/dist/lib/retry-utils/index.js.map +1 -1
  62. package/dist/lib/safe-handle-callback.cjs +196 -18
  63. package/dist/lib/safe-handle-callback.cjs.map +1 -1
  64. package/dist/lib/safe-handle-callback.d.cts +14 -4
  65. package/dist/lib/safe-handle-callback.d.ts +14 -4
  66. package/dist/lib/safe-handle-callback.js +196 -18
  67. package/dist/lib/safe-handle-callback.js.map +1 -1
  68. package/dist/lib/single-event-observer.cjs +195 -9
  69. package/dist/lib/single-event-observer.cjs.map +1 -1
  70. package/dist/lib/single-event-observer.js +195 -9
  71. package/dist/lib/single-event-observer.js.map +1 -1
  72. package/dist/{types-CWw885iO.d.cts → types-BFvpJPMt.d.cts} +72 -11
  73. package/dist/{types-Mso0dYOS.d.ts → types-BONtIhQ5.d.ts} +72 -11
  74. package/package.json +31 -12
@@ -1,11 +1,16 @@
1
1
  /**
2
- * Safely handles a callback function by catching any errors and reporting them
3
- * using the global `reportError` event (supported in Node.js 25+, Bun, Deno, and browsers).
2
+ * Safely handles a callback function by catching any errors and reporting them via
3
+ * Lifecycleion's `'reportError'` reporting convention, built from web-standard primitives
4
+ * (`ErrorEvent` + global `EventTarget` methods).
4
5
  * This function can seamlessly handle both synchronous and asynchronous (Promise-based) callback functions.
5
6
  *
6
7
  * Errors are dispatched as ErrorEvent objects with type 'reportError' via `globalThis.dispatchEvent()`.
7
8
  * You can listen for these errors using `globalThis.addEventListener('reportError', handler)`.
8
9
  *
10
+ * Browsers, Bun, and Deno expose these globals natively. Node.js provides `ErrorEvent` (Node 25+)
11
+ * but not the global event methods, so importing this module installs them via a shared
12
+ * `EventTarget` (see `global-event-target`) without overwriting existing implementations.
13
+ *
9
14
  * This function is a "fire-and-forget" type of function, meaning it doesn't wait
10
15
  * for the callback to complete and doesn't return any result or error. If you need
11
16
  * to handle the result or error of the callback, consider using the
@@ -23,14 +28,19 @@ interface CallbackResult<T> {
23
28
  error?: Error;
24
29
  }
25
30
  /**
26
- * Safely handles a callback function by catching any errors and reporting them
27
- * using the global `reportError` event (supported in Node.js 25+, Bun, Deno, and browsers).
31
+ * Safely handles a callback function by catching any errors and reporting them via
32
+ * Lifecycleion's `'reportError'` reporting convention, built from web-standard primitives
33
+ * (`ErrorEvent` + global `EventTarget` methods).
28
34
  * This function can seamlessly handle both synchronous and asynchronous (Promise-based) callback
29
35
  * functions, and it waits for the callback to complete before returning the result or an error.
30
36
  *
31
37
  * Errors are dispatched as ErrorEvent objects with type 'reportError' via `globalThis.dispatchEvent()`.
32
38
  * You can listen for these errors using `globalThis.addEventListener('reportError', handler)`.
33
39
  *
40
+ * Browsers, Bun, and Deno expose these globals natively. Node.js provides `ErrorEvent` (Node 25+)
41
+ * but not the global event methods, so importing this module installs them via a shared
42
+ * `EventTarget` (see `global-event-target`) without overwriting existing implementations.
43
+ *
34
44
  * @param {string} callbackName - The name of the callback function, used for error reporting.
35
45
  * @param {unknown} callback - The callback function to be executed. It can be either a
36
46
  * synchronous function or a function that returns a Promise.
@@ -1,11 +1,16 @@
1
1
  /**
2
- * Safely handles a callback function by catching any errors and reporting them
3
- * using the global `reportError` event (supported in Node.js 25+, Bun, Deno, and browsers).
2
+ * Safely handles a callback function by catching any errors and reporting them via
3
+ * Lifecycleion's `'reportError'` reporting convention, built from web-standard primitives
4
+ * (`ErrorEvent` + global `EventTarget` methods).
4
5
  * This function can seamlessly handle both synchronous and asynchronous (Promise-based) callback functions.
5
6
  *
6
7
  * Errors are dispatched as ErrorEvent objects with type 'reportError' via `globalThis.dispatchEvent()`.
7
8
  * You can listen for these errors using `globalThis.addEventListener('reportError', handler)`.
8
9
  *
10
+ * Browsers, Bun, and Deno expose these globals natively. Node.js provides `ErrorEvent` (Node 25+)
11
+ * but not the global event methods, so importing this module installs them via a shared
12
+ * `EventTarget` (see `global-event-target`) without overwriting existing implementations.
13
+ *
9
14
  * This function is a "fire-and-forget" type of function, meaning it doesn't wait
10
15
  * for the callback to complete and doesn't return any result or error. If you need
11
16
  * to handle the result or error of the callback, consider using the
@@ -23,14 +28,19 @@ interface CallbackResult<T> {
23
28
  error?: Error;
24
29
  }
25
30
  /**
26
- * Safely handles a callback function by catching any errors and reporting them
27
- * using the global `reportError` event (supported in Node.js 25+, Bun, Deno, and browsers).
31
+ * Safely handles a callback function by catching any errors and reporting them via
32
+ * Lifecycleion's `'reportError'` reporting convention, built from web-standard primitives
33
+ * (`ErrorEvent` + global `EventTarget` methods).
28
34
  * This function can seamlessly handle both synchronous and asynchronous (Promise-based) callback
29
35
  * functions, and it waits for the callback to complete before returning the result or an error.
30
36
  *
31
37
  * Errors are dispatched as ErrorEvent objects with type 'reportError' via `globalThis.dispatchEvent()`.
32
38
  * You can listen for these errors using `globalThis.addEventListener('reportError', handler)`.
33
39
  *
40
+ * Browsers, Bun, and Deno expose these globals natively. Node.js provides `ErrorEvent` (Node 25+)
41
+ * but not the global event methods, so importing this module installs them via a shared
42
+ * `EventTarget` (see `global-event-target`) without overwriting existing implementations.
43
+ *
34
44
  * @param {string} callbackName - The name of the callback function, used for error reporting.
35
45
  * @param {unknown} callback - The callback function to be executed. It can be either a
36
46
  * synchronous function or a function that returns a Promise.
@@ -714,18 +714,204 @@ function isFunction(value) {
714
714
  return typeof value === "function" || value instanceof Function;
715
715
  }
716
716
 
717
+ // src/lib/global-event-target.ts
718
+ var GLOBAL_KEY = "__lifecycleion_global_event_target__";
719
+ var METHOD_NAMES = [
720
+ "addEventListener",
721
+ "removeEventListener",
722
+ "dispatchEvent"
723
+ ];
724
+ var g = globalThis;
725
+ function getState() {
726
+ const value = readGlobal(GLOBAL_KEY);
727
+ return value === UNREADABLE ? void 0 : asUsableState(value);
728
+ }
729
+ function asUsableState(value) {
730
+ if (typeof value !== "object" || value === null) {
731
+ return void 0;
732
+ }
733
+ return asUsableTarget(readMember(value, "target")) === null ? void 0 : value;
734
+ }
735
+ function asUsableTarget(value) {
736
+ if (typeof value !== "object" || value === null) {
737
+ return null;
738
+ }
739
+ for (const name of METHOD_NAMES) {
740
+ if (typeof readMember(value, name) !== "function") {
741
+ return null;
742
+ }
743
+ }
744
+ return value;
745
+ }
746
+ function bindTargetMethods(target) {
747
+ const bound = {};
748
+ for (const name of METHOD_NAMES) {
749
+ const method = readMember(target, name);
750
+ if (typeof method !== "function") {
751
+ return null;
752
+ }
753
+ let boundMethod;
754
+ try {
755
+ boundMethod = method.bind(target);
756
+ } catch {
757
+ return null;
758
+ }
759
+ if (typeof boundMethod !== "function") {
760
+ return null;
761
+ }
762
+ bound[name] = boundMethod;
763
+ }
764
+ return bound;
765
+ }
766
+ function isStateInstalled(state) {
767
+ return state !== void 0 && readMember(state, "isInstalled") === true;
768
+ }
769
+ function readStateTarget(state) {
770
+ if (state === void 0) {
771
+ return null;
772
+ }
773
+ return asUsableTarget(readMember(state, "target"));
774
+ }
775
+ var UNREADABLE = /* @__PURE__ */ Symbol("lifecycleion.unreadable");
776
+ function readGlobal(name) {
777
+ try {
778
+ return g[name];
779
+ } catch {
780
+ return UNREADABLE;
781
+ }
782
+ }
783
+ function readMember(source, key) {
784
+ try {
785
+ return source[key];
786
+ } catch {
787
+ return UNREADABLE;
788
+ }
789
+ }
790
+ function probeMember(name) {
791
+ const value = readGlobal(name);
792
+ if (value === UNREADABLE) {
793
+ return "hostile";
794
+ }
795
+ if (typeof value === "function") {
796
+ return "usable";
797
+ }
798
+ return value === void 0 ? "absent" : "occupied";
799
+ }
800
+ function canDefineProperty(key) {
801
+ const descriptor = Object.getOwnPropertyDescriptor(g, key);
802
+ if (descriptor === void 0) {
803
+ return Object.isExtensible(g);
804
+ }
805
+ return descriptor.configurable === true;
806
+ }
807
+ function installGlobalEventTarget() {
808
+ const members = METHOD_NAMES.map((name) => probeMember(name));
809
+ if (members.every((member) => member === "usable")) {
810
+ return isStateInstalled(getState()) ? "already-installed" : "native";
811
+ }
812
+ if (members.some((member) => member !== "absent")) {
813
+ return "partial";
814
+ }
815
+ const eventTargetConstructor = readGlobal("EventTarget");
816
+ if (typeof eventTargetConstructor !== "function") {
817
+ return "unsupported";
818
+ }
819
+ const isDefinable = [...METHOD_NAMES, GLOBAL_KEY].every(
820
+ (key) => canDefineProperty(key)
821
+ );
822
+ if (!isDefinable) {
823
+ return "blocked";
824
+ }
825
+ const existingState = getState();
826
+ const defined = [];
827
+ const priorStateDescriptor = Object.getOwnPropertyDescriptor(g, GLOBAL_KEY);
828
+ const rollback = () => {
829
+ for (const key of defined) {
830
+ if (key === GLOBAL_KEY && priorStateDescriptor !== void 0) {
831
+ try {
832
+ Object.defineProperty(g, GLOBAL_KEY, priorStateDescriptor);
833
+ } catch {
834
+ }
835
+ continue;
836
+ }
837
+ try {
838
+ Reflect.deleteProperty(g, key);
839
+ } catch {
840
+ }
841
+ }
842
+ };
843
+ const reusableTarget = readStateTarget(existingState);
844
+ const reusableMethods = reusableTarget === null ? null : bindTargetMethods(reusableTarget);
845
+ let state;
846
+ let methods;
847
+ try {
848
+ if (existingState === void 0 || reusableMethods === null) {
849
+ let target;
850
+ try {
851
+ target = new eventTargetConstructor();
852
+ } catch {
853
+ return "unsupported";
854
+ }
855
+ const freshMethods = bindTargetMethods(target);
856
+ if (freshMethods === null) {
857
+ return "unsupported";
858
+ }
859
+ state = { target, isInstalled: false };
860
+ methods = freshMethods;
861
+ Object.defineProperty(g, GLOBAL_KEY, {
862
+ value: state,
863
+ enumerable: false,
864
+ writable: false,
865
+ configurable: true
866
+ });
867
+ defined.push(GLOBAL_KEY);
868
+ } else {
869
+ state = existingState;
870
+ methods = reusableMethods;
871
+ }
872
+ for (const name of METHOD_NAMES) {
873
+ Object.defineProperty(g, name, {
874
+ value: methods[name],
875
+ enumerable: false,
876
+ writable: true,
877
+ configurable: true
878
+ });
879
+ defined.push(name);
880
+ }
881
+ if (readMember(state, "isInstalled") !== true) {
882
+ state.isInstalled = true;
883
+ }
884
+ } catch {
885
+ rollback();
886
+ return "blocked";
887
+ }
888
+ return "installed";
889
+ }
890
+ function isGlobalEventTargetAvailable() {
891
+ return METHOD_NAMES.every((name) => probeMember(name) === "usable") && probeMember("ErrorEvent") === "usable";
892
+ }
893
+
717
894
  // src/lib/safe-handle-callback.ts
895
+ installGlobalEventTarget();
896
+ function reportCallbackError(callbackName, error) {
897
+ installGlobalEventTarget();
898
+ if (!isGlobalEventTargetAvailable()) {
899
+ return;
900
+ }
901
+ try {
902
+ globalThis.dispatchEvent(
903
+ new ErrorEvent("reportError", {
904
+ error: new Error(
905
+ `Error in a callback ${callbackName}: ${DOUBLE_EOL}${errorToString(error)}`
906
+ )
907
+ })
908
+ );
909
+ } catch {
910
+ }
911
+ }
718
912
  function safeHandleCallback(callbackName, callback, ...args) {
719
913
  const handleError = (error) => {
720
- if (typeof globalThis.dispatchEvent === "function") {
721
- globalThis.dispatchEvent(
722
- new ErrorEvent("reportError", {
723
- error: new Error(
724
- `Error in a callback ${callbackName}: ${DOUBLE_EOL}${errorToString(error)}`
725
- )
726
- })
727
- );
728
- }
914
+ reportCallbackError(callbackName, error);
729
915
  };
730
916
  if (isFunction(callback)) {
731
917
  try {
@@ -746,15 +932,7 @@ function safeHandleCallback(callbackName, callback, ...args) {
746
932
  }
747
933
  async function safeHandleCallbackAndWait(callbackName, callback, ...args) {
748
934
  const handleError = (error) => {
749
- if (typeof globalThis.dispatchEvent === "function") {
750
- globalThis.dispatchEvent(
751
- new ErrorEvent("reportError", {
752
- error: new Error(
753
- `Error in a callback ${callbackName}: ${DOUBLE_EOL}${errorToString(error)}`
754
- )
755
- })
756
- );
757
- }
935
+ reportCallbackError(callbackName, error);
758
936
  return { success: false, error };
759
937
  };
760
938
  if (isFunction(callback)) {