@bitblit/ratchet-common 6.0.146-alpha → 6.0.148-alpha

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 (140) hide show
  1. package/package.json +2 -1
  2. package/src/2d/line-2d.ts +6 -0
  3. package/src/2d/matrix-factory.ts +94 -0
  4. package/src/2d/plane-2d-type.ts +6 -0
  5. package/src/2d/plane-2d.ts +7 -0
  6. package/src/2d/point-2d.ts +4 -0
  7. package/src/2d/poly-line-2d.ts +5 -0
  8. package/src/2d/ratchet-2d.spec.ts +205 -0
  9. package/src/2d/ratchet-2d.ts +350 -0
  10. package/src/2d/transformation-matrix.ts +19 -0
  11. package/src/build/build-information.ts +8 -0
  12. package/src/build/ratchet-common-info.ts +19 -0
  13. package/src/histogram/histogram-entry.ts +4 -0
  14. package/src/histogram/histogram.spec.ts +25 -0
  15. package/src/histogram/histogram.ts +61 -0
  16. package/src/jwt/common-jwt-token.ts +17 -0
  17. package/src/jwt/expired-jwt-handling.ts +5 -0
  18. package/src/jwt/jwt-decode-only-ratchet.ts +26 -0
  19. package/src/jwt/jwt-payload-expiration-ratchet.ts +45 -0
  20. package/src/jwt/jwt-token-base.ts +14 -0
  21. package/src/lang/array-ratchet.spec.ts +79 -0
  22. package/src/lang/array-ratchet.ts +141 -0
  23. package/src/lang/base64-ratchet.spec.ts +48 -0
  24. package/src/lang/base64-ratchet.ts +247 -0
  25. package/src/lang/boolean-ratchet.spec.ts +95 -0
  26. package/src/lang/boolean-ratchet.ts +52 -0
  27. package/src/lang/composite-last-success-provider.spec.ts +31 -0
  28. package/src/lang/composite-last-success-provider.ts +30 -0
  29. package/src/lang/currency-ratchet.ts +29 -0
  30. package/src/lang/date-ratchet.spec.ts +27 -0
  31. package/src/lang/date-ratchet.ts +42 -0
  32. package/src/lang/duration-ratchet.spec.ts +47 -0
  33. package/src/lang/duration-ratchet.ts +77 -0
  34. package/src/lang/enum-ratchet.spec.ts +45 -0
  35. package/src/lang/enum-ratchet.ts +41 -0
  36. package/src/lang/error-handling-approach.ts +6 -0
  37. package/src/lang/error-ratchet.spec.ts +25 -0
  38. package/src/lang/error-ratchet.ts +70 -0
  39. package/src/lang/esm-ratchet.ts +81 -0
  40. package/src/lang/expiring-object.spec.ts +56 -0
  41. package/src/lang/expiring-object.ts +84 -0
  42. package/src/lang/geolocation-ratchet.spec.ts +177 -0
  43. package/src/lang/geolocation-ratchet.ts +341 -0
  44. package/src/lang/global-ratchet.spec.ts +17 -0
  45. package/src/lang/global-ratchet.ts +105 -0
  46. package/src/lang/key-value.ts +8 -0
  47. package/src/lang/last-success-provider.ts +4 -0
  48. package/src/lang/map-ratchet.spec.ts +113 -0
  49. package/src/lang/map-ratchet.ts +220 -0
  50. package/src/lang/no.spec.ts +9 -0
  51. package/src/lang/no.ts +7 -0
  52. package/src/lang/number-ratchet.spec.ts +154 -0
  53. package/src/lang/number-ratchet.ts +253 -0
  54. package/src/lang/parsed-url.ts +10 -0
  55. package/src/lang/promise-ratchet.spec.ts +104 -0
  56. package/src/lang/promise-ratchet.ts +196 -0
  57. package/src/lang/range.ts +4 -0
  58. package/src/lang/require-ratchet.spec.ts +85 -0
  59. package/src/lang/require-ratchet.ts +68 -0
  60. package/src/lang/simple-arg-ratchet.spec.ts +13 -0
  61. package/src/lang/simple-arg-ratchet.ts +47 -0
  62. package/src/lang/simple-encryption-ratchet.ts +88 -0
  63. package/src/lang/sort-ratchet.spec.ts +58 -0
  64. package/src/lang/sort-ratchet.ts +50 -0
  65. package/src/lang/stop-watch.spec.ts +53 -0
  66. package/src/lang/stop-watch.ts +202 -0
  67. package/src/lang/string-ratchet.spec.ts +226 -0
  68. package/src/lang/string-ratchet.ts +676 -0
  69. package/src/lang/time-zone-ratchet.spec.ts +51 -0
  70. package/src/lang/time-zone-ratchet.ts +148 -0
  71. package/src/lang/timeout-token.spec.ts +12 -0
  72. package/src/lang/timeout-token.ts +21 -0
  73. package/src/lang/uint-8-array-ratchet.spec.ts +22 -0
  74. package/src/lang/uint-8-array-ratchet.ts +48 -0
  75. package/src/lang/web-stream-ratchet.spec.ts +12 -0
  76. package/src/lang/web-stream-ratchet.ts +96 -0
  77. package/src/logger/classic-single-line-log-message-formatter.ts +19 -0
  78. package/src/logger/log-message-builder.ts +60 -0
  79. package/src/logger/log-message-format-type.ts +11 -0
  80. package/src/logger/log-message-formatter.ts +6 -0
  81. package/src/logger/log-message-processor.ts +6 -0
  82. package/src/logger/log-message.ts +9 -0
  83. package/src/logger/log-snapshot.ts +6 -0
  84. package/src/logger/logger-instance.ts +269 -0
  85. package/src/logger/logger-level-name.ts +11 -0
  86. package/src/logger/logger-meta.ts +7 -0
  87. package/src/logger/logger-options.ts +14 -0
  88. package/src/logger/logger-output-function.ts +10 -0
  89. package/src/logger/logger-ring-buffer.ts +89 -0
  90. package/src/logger/logger-util.spec.ts +11 -0
  91. package/src/logger/logger-util.ts +68 -0
  92. package/src/logger/logger.spec.ts +177 -0
  93. package/src/logger/logger.ts +213 -0
  94. package/src/logger/none-log-message-formatter.ts +10 -0
  95. package/src/logger/single-line-no-level-log-message-formatter.ts +18 -0
  96. package/src/logger/structured-json-log-message-formatter.ts +25 -0
  97. package/src/mail/archive-email-result.ts +8 -0
  98. package/src/mail/email-attachment.ts +23 -0
  99. package/src/mail/mail-sending-provider.ts +21 -0
  100. package/src/mail/mailer-config.ts +30 -0
  101. package/src/mail/mailer-like.ts +38 -0
  102. package/src/mail/mailer-util.ts +65 -0
  103. package/src/mail/mailer.spec.ts +120 -0
  104. package/src/mail/mailer.ts +214 -0
  105. package/src/mail/ready-to-send-email.ts +67 -0
  106. package/src/mail/resolved-ready-to-send-email.ts +17 -0
  107. package/src/mail/send-email-result.ts +16 -0
  108. package/src/mail/test-mail-sending-provider.ts +35 -0
  109. package/src/network/browser-local-ip-provider.spec.ts +23 -0
  110. package/src/network/browser-local-ip-provider.ts +26 -0
  111. package/src/network/fixed-local-ip-provider.ts +9 -0
  112. package/src/network/local-ip-provider.ts +4 -0
  113. package/src/network/network-ratchet.spec.ts +17 -0
  114. package/src/network/network-ratchet.ts +209 -0
  115. package/src/network/remote-file-tracker/backup-result.ts +6 -0
  116. package/src/network/remote-file-tracker/file-transfer-result-type.ts +5 -0
  117. package/src/network/remote-file-tracker/file-transfer-result.ts +9 -0
  118. package/src/network/remote-file-tracker/remote-file-tracker-options.ts +6 -0
  119. package/src/network/remote-file-tracker/remote-file-tracker-push-options.ts +4 -0
  120. package/src/network/remote-file-tracker/remote-file-tracker.ts +117 -0
  121. package/src/network/remote-file-tracker/remote-file-tracking-provider.ts +19 -0
  122. package/src/network/remote-file-tracker/remote-status-data-and-content.ts +6 -0
  123. package/src/network/remote-file-tracker/remote-status-data.ts +7 -0
  124. package/src/network/restful-api-http-error.spec.ts +13 -0
  125. package/src/network/restful-api-http-error.ts +173 -0
  126. package/src/template/ratchet-template-renderer.ts +8 -0
  127. package/src/third-party/google/google-recaptcha-ratchet.spec.ts +27 -0
  128. package/src/third-party/google/google-recaptcha-ratchet.ts +36 -0
  129. package/src/third-party/twilio/twilio-ratchet.ts +92 -0
  130. package/src/third-party/twilio/twilio-verify-ratchet.ts +83 -0
  131. package/src/transform/built-in-transforms.ts +214 -0
  132. package/src/transform/transform-ratchet.spec.ts +134 -0
  133. package/src/transform/transform-ratchet.ts +88 -0
  134. package/src/transform/transform-rule.ts +7 -0
  135. package/src/tx/transaction-configuration.ts +8 -0
  136. package/src/tx/transaction-final-state.ts +7 -0
  137. package/src/tx/transaction-ratchet.spec.ts +150 -0
  138. package/src/tx/transaction-ratchet.ts +98 -0
  139. package/src/tx/transaction-result.ts +10 -0
  140. package/src/tx/transaction-step.ts +5 -0
@@ -0,0 +1,98 @@
1
+ import { TransactionStep } from './transaction-step.js';
2
+ import { TransactionResult } from './transaction-result.js';
3
+ import { TransactionConfiguration } from './transaction-configuration.js';
4
+ import { Logger } from '../logger/logger.js';
5
+ import { RequireRatchet } from '../lang/require-ratchet.js';
6
+ import { LoggerLevelName } from '../logger/logger-level-name.js';
7
+ import { TransactionFinalState } from './transaction-final-state.js';
8
+
9
+ /*
10
+ TransactionRatchet is a framework for executing a series of steps in
11
+ node as a transaction, which is to say, it either executes all the steps in order
12
+ and succeeds completely, or if any step fails (by throwing an exception)
13
+ then the executes all of the rollback steps in reverse order.
14
+ */
15
+
16
+ export class TransactionRatchet {
17
+ public static async execute<T>(
18
+ steps: TransactionStep<T>[],
19
+ initialContext: T,
20
+ inConfiguration?: TransactionConfiguration<T>,
21
+ ): Promise<TransactionResult<T>> {
22
+ RequireRatchet.notNullOrUndefined(steps, 'steps');
23
+ RequireRatchet.notNullOrUndefined(initialContext, 'initialContext');
24
+ RequireRatchet.true(steps.length > 0, 'steps may not be empty');
25
+
26
+ // You must have a config, and it must set a log level
27
+ // Have to copy it here, since the provided one might only have a getter for the log level
28
+ const config: TransactionConfiguration<T> = Object.assign({}, inConfiguration || {});
29
+ config.stepLogLevel = config.stepLogLevel || LoggerLevelName.info;
30
+
31
+ const rval: TransactionResult<T> = {
32
+ finalContext: initialContext,
33
+ finalState: null,
34
+ };
35
+
36
+ const stepNames: string[] = steps.map((s, idx) => s.name || 'Step ' + idx);
37
+
38
+ Logger.info('Beginning transaction of %d steps', steps.length);
39
+ let idx = 0;
40
+ do {
41
+ Logger.logByLevel(config.stepLogLevel, 'Processing step %d of %d (%s)', idx + 1, steps.length, stepNames[idx]);
42
+ try {
43
+ await steps[idx].execute(rval.finalContext, idx);
44
+ idx++;
45
+ } catch (err) {
46
+ Logger.error('Failure detected on step %d : %s : %s : Rolling back', idx, stepNames[idx], err, err);
47
+ rval.error = err;
48
+ rval.errorStep = idx;
49
+ }
50
+ } while (!rval.error && idx < steps.length);
51
+
52
+ if (rval.error) {
53
+ // Do rollback
54
+ do {
55
+ Logger.logByLevel(config.stepLogLevel, 'Rolling back step %d of %d (%s)', idx + 1, steps.length, stepNames[idx]);
56
+ try {
57
+ if (steps[idx].rollback) {
58
+ await steps[idx].rollback(rval.finalContext, idx);
59
+ } else {
60
+ Logger.info('Skipping - no rollback defined');
61
+ }
62
+ } catch (err) {
63
+ Logger.error('Very bad - rollback code failed on step %d : %s : Aborting in invalid state: %s', idx, stepNames[idx], err, err);
64
+ rval.rollbackError = err;
65
+ rval.rollbackErrorStep = idx;
66
+ }
67
+ idx--;
68
+ } while (idx >= 0);
69
+ }
70
+
71
+ rval.finalState = rval.rollbackError
72
+ ? TransactionFinalState.RollbackFailed
73
+ : rval.error
74
+ ? TransactionFinalState.RolledBack
75
+ : TransactionFinalState.Success;
76
+ Logger.info('Transaction completed with status : %s', rval.finalState);
77
+
78
+ if (config?.executeAfterRollback && rval.finalState !== TransactionFinalState.Success) {
79
+ try {
80
+ Logger.info('Applying executeAfterRollback');
81
+ await config.executeAfterRollback(rval);
82
+ } catch (err) {
83
+ Logger.error('Very bad - failure in executeAfterRollback : %s', err, err);
84
+ }
85
+ }
86
+
87
+ if (config?.executeAfterRollbackFailure && rval.finalState !== TransactionFinalState.Success) {
88
+ try {
89
+ Logger.info('Applying executeAfterRollbackFailure');
90
+ await config.executeAfterRollbackFailure(rval);
91
+ } catch (err) {
92
+ Logger.error('Very bad - failure in executeAfterRollbackFailure : %s', err, err);
93
+ }
94
+ }
95
+
96
+ return rval;
97
+ }
98
+ }
@@ -0,0 +1,10 @@
1
+ import { TransactionFinalState } from './transaction-final-state.js';
2
+
3
+ export interface TransactionResult<T> {
4
+ finalContext: T;
5
+ finalState: TransactionFinalState;
6
+ error?: any; // Contains the error that initiated the rollback, if any
7
+ rollbackError?: any; // If an exception was thrown during rollback (this is very bad)
8
+ errorStep?: number; // If the tx failed, it failed at this step
9
+ rollbackErrorStep?: number; // If the rollback failed, it failed at this step
10
+ }
@@ -0,0 +1,5 @@
1
+ export interface TransactionStep<T> {
2
+ execute(context: T, stepNumber?: number): Promise<void>;
3
+ rollback?(context: T, stepNumber?: number): Promise<void>;
4
+ name?: string;
5
+ }