@bitblit/ratchet-common 6.0.145-alpha → 6.0.147-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.
- package/package.json +2 -1
- package/src/2d/line-2d.ts +6 -0
- package/src/2d/matrix-factory.ts +94 -0
- package/src/2d/plane-2d-type.ts +6 -0
- package/src/2d/plane-2d.ts +7 -0
- package/src/2d/point-2d.ts +4 -0
- package/src/2d/poly-line-2d.ts +5 -0
- package/src/2d/ratchet-2d.spec.ts +205 -0
- package/src/2d/ratchet-2d.ts +350 -0
- package/src/2d/transformation-matrix.ts +19 -0
- package/src/build/build-information.ts +8 -0
- package/src/build/ratchet-common-info.ts +19 -0
- package/src/histogram/histogram-entry.ts +4 -0
- package/src/histogram/histogram.spec.ts +25 -0
- package/src/histogram/histogram.ts +61 -0
- package/src/jwt/common-jwt-token.ts +17 -0
- package/src/jwt/expired-jwt-handling.ts +5 -0
- package/src/jwt/jwt-decode-only-ratchet.ts +26 -0
- package/src/jwt/jwt-payload-expiration-ratchet.ts +45 -0
- package/src/jwt/jwt-token-base.ts +14 -0
- package/src/lang/array-ratchet.spec.ts +79 -0
- package/src/lang/array-ratchet.ts +141 -0
- package/src/lang/base64-ratchet.spec.ts +48 -0
- package/src/lang/base64-ratchet.ts +247 -0
- package/src/lang/boolean-ratchet.spec.ts +95 -0
- package/src/lang/boolean-ratchet.ts +52 -0
- package/src/lang/composite-last-success-provider.spec.ts +31 -0
- package/src/lang/composite-last-success-provider.ts +30 -0
- package/src/lang/currency-ratchet.ts +29 -0
- package/src/lang/date-ratchet.spec.ts +27 -0
- package/src/lang/date-ratchet.ts +42 -0
- package/src/lang/duration-ratchet.spec.ts +47 -0
- package/src/lang/duration-ratchet.ts +77 -0
- package/src/lang/enum-ratchet.spec.ts +45 -0
- package/src/lang/enum-ratchet.ts +41 -0
- package/src/lang/error-handling-approach.ts +6 -0
- package/src/lang/error-ratchet.spec.ts +25 -0
- package/src/lang/error-ratchet.ts +70 -0
- package/src/lang/esm-ratchet.ts +81 -0
- package/src/lang/expiring-object.spec.ts +56 -0
- package/src/lang/expiring-object.ts +84 -0
- package/src/lang/geolocation-ratchet.spec.ts +177 -0
- package/src/lang/geolocation-ratchet.ts +341 -0
- package/src/lang/global-ratchet.spec.ts +17 -0
- package/src/lang/global-ratchet.ts +105 -0
- package/src/lang/key-value.ts +8 -0
- package/src/lang/last-success-provider.ts +4 -0
- package/src/lang/map-ratchet.spec.ts +113 -0
- package/src/lang/map-ratchet.ts +220 -0
- package/src/lang/no.spec.ts +9 -0
- package/src/lang/no.ts +7 -0
- package/src/lang/number-ratchet.spec.ts +154 -0
- package/src/lang/number-ratchet.ts +253 -0
- package/src/lang/parsed-url.ts +10 -0
- package/src/lang/promise-ratchet.spec.ts +104 -0
- package/src/lang/promise-ratchet.ts +196 -0
- package/src/lang/range.ts +4 -0
- package/src/lang/require-ratchet.spec.ts +85 -0
- package/src/lang/require-ratchet.ts +68 -0
- package/src/lang/simple-arg-ratchet.spec.ts +13 -0
- package/src/lang/simple-arg-ratchet.ts +47 -0
- package/src/lang/simple-encryption-ratchet.ts +88 -0
- package/src/lang/sort-ratchet.spec.ts +58 -0
- package/src/lang/sort-ratchet.ts +50 -0
- package/src/lang/stop-watch.spec.ts +53 -0
- package/src/lang/stop-watch.ts +202 -0
- package/src/lang/string-ratchet.spec.ts +226 -0
- package/src/lang/string-ratchet.ts +676 -0
- package/src/lang/time-zone-ratchet.spec.ts +51 -0
- package/src/lang/time-zone-ratchet.ts +148 -0
- package/src/lang/timeout-token.spec.ts +12 -0
- package/src/lang/timeout-token.ts +21 -0
- package/src/lang/uint-8-array-ratchet.spec.ts +22 -0
- package/src/lang/uint-8-array-ratchet.ts +48 -0
- package/src/lang/web-stream-ratchet.spec.ts +12 -0
- package/src/lang/web-stream-ratchet.ts +96 -0
- package/src/logger/classic-single-line-log-message-formatter.ts +19 -0
- package/src/logger/log-message-builder.ts +60 -0
- package/src/logger/log-message-format-type.ts +11 -0
- package/src/logger/log-message-formatter.ts +6 -0
- package/src/logger/log-message-processor.ts +6 -0
- package/src/logger/log-message.ts +9 -0
- package/src/logger/log-snapshot.ts +6 -0
- package/src/logger/logger-instance.ts +269 -0
- package/src/logger/logger-level-name.ts +11 -0
- package/src/logger/logger-meta.ts +7 -0
- package/src/logger/logger-options.ts +14 -0
- package/src/logger/logger-output-function.ts +10 -0
- package/src/logger/logger-ring-buffer.ts +89 -0
- package/src/logger/logger-util.spec.ts +11 -0
- package/src/logger/logger-util.ts +68 -0
- package/src/logger/logger.spec.ts +177 -0
- package/src/logger/logger.ts +213 -0
- package/src/logger/none-log-message-formatter.ts +10 -0
- package/src/logger/single-line-no-level-log-message-formatter.ts +18 -0
- package/src/logger/structured-json-log-message-formatter.ts +25 -0
- package/src/mail/archive-email-result.ts +8 -0
- package/src/mail/email-attachment.ts +23 -0
- package/src/mail/mail-sending-provider.ts +21 -0
- package/src/mail/mailer-config.ts +30 -0
- package/src/mail/mailer-like.ts +38 -0
- package/src/mail/mailer-util.ts +65 -0
- package/src/mail/mailer.spec.ts +120 -0
- package/src/mail/mailer.ts +214 -0
- package/src/mail/ready-to-send-email.ts +67 -0
- package/src/mail/resolved-ready-to-send-email.ts +17 -0
- package/src/mail/send-email-result.ts +16 -0
- package/src/mail/test-mail-sending-provider.ts +35 -0
- package/src/network/browser-local-ip-provider.spec.ts +23 -0
- package/src/network/browser-local-ip-provider.ts +26 -0
- package/src/network/fixed-local-ip-provider.ts +9 -0
- package/src/network/local-ip-provider.ts +4 -0
- package/src/network/network-ratchet.spec.ts +17 -0
- package/src/network/network-ratchet.ts +209 -0
- package/src/network/remote-file-tracker/backup-result.ts +6 -0
- package/src/network/remote-file-tracker/file-transfer-result-type.ts +5 -0
- package/src/network/remote-file-tracker/file-transfer-result.ts +9 -0
- package/src/network/remote-file-tracker/remote-file-tracker-options.ts +6 -0
- package/src/network/remote-file-tracker/remote-file-tracker-push-options.ts +4 -0
- package/src/network/remote-file-tracker/remote-file-tracker.ts +117 -0
- package/src/network/remote-file-tracker/remote-file-tracking-provider.ts +19 -0
- package/src/network/remote-file-tracker/remote-status-data-and-content.ts +6 -0
- package/src/network/remote-file-tracker/remote-status-data.ts +7 -0
- package/src/network/restful-api-http-error.spec.ts +13 -0
- package/src/network/restful-api-http-error.ts +173 -0
- package/src/template/ratchet-template-renderer.ts +8 -0
- package/src/third-party/google/google-recaptcha-ratchet.spec.ts +27 -0
- package/src/third-party/google/google-recaptcha-ratchet.ts +36 -0
- package/src/third-party/twilio/twilio-ratchet.ts +92 -0
- package/src/third-party/twilio/twilio-verify-ratchet.ts +83 -0
- package/src/transform/built-in-transforms.ts +214 -0
- package/src/transform/transform-ratchet.spec.ts +134 -0
- package/src/transform/transform-ratchet.ts +88 -0
- package/src/transform/transform-rule.ts +7 -0
- package/src/tx/transaction-configuration.ts +8 -0
- package/src/tx/transaction-final-state.ts +7 -0
- package/src/tx/transaction-ratchet.spec.ts +150 -0
- package/src/tx/transaction-ratchet.ts +98 -0
- package/src/tx/transaction-result.ts +10 -0
- 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
|
+
}
|