@bitblit/ratchet-common 4.0.80-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 (220) hide show
  1. package/dist/cjs/2d/line-2d.js +2 -0
  2. package/dist/cjs/2d/plane-2d.js +2 -0
  3. package/dist/cjs/2d/point-2d.js +2 -0
  4. package/dist/cjs/2d/poly-line-2d.js +2 -0
  5. package/dist/cjs/2d/ratchet-2d.js +250 -0
  6. package/dist/cjs/build/build-information.js +2 -0
  7. package/dist/cjs/build/ratchet-common-info.js +18 -0
  8. package/dist/cjs/histogram/histogram-entry.js +2 -0
  9. package/dist/cjs/histogram/histogram.js +54 -0
  10. package/dist/cjs/index.js +74 -0
  11. package/dist/cjs/jwt/common-jwt-token.js +2 -0
  12. package/dist/cjs/jwt/expired-jwt-handling.js +9 -0
  13. package/dist/cjs/jwt/jwt-ratchet-like.js +2 -0
  14. package/dist/cjs/jwt/jwt-ratchet.js +164 -0
  15. package/dist/cjs/jwt/jwt-token-base.js +2 -0
  16. package/dist/cjs/lang/array-ratchet.js +87 -0
  17. package/dist/cjs/lang/base64-ratchet.js +49 -0
  18. package/dist/cjs/lang/boolean-ratchet.js +49 -0
  19. package/dist/cjs/lang/composite-last-success-provider.js +28 -0
  20. package/dist/cjs/lang/date-ratchet.js +33 -0
  21. package/dist/cjs/lang/duration-ratchet.js +46 -0
  22. package/dist/cjs/lang/enum-ratchet.js +34 -0
  23. package/dist/cjs/lang/error-ratchet.js +49 -0
  24. package/dist/cjs/lang/expiring-object.js +68 -0
  25. package/dist/cjs/lang/geolocation-ratchet.js +267 -0
  26. package/dist/cjs/lang/global-ratchet.js +48 -0
  27. package/dist/cjs/lang/key-value.js +6 -0
  28. package/dist/cjs/lang/last-success-provider.js +2 -0
  29. package/dist/cjs/lang/map-ratchet.js +168 -0
  30. package/dist/cjs/lang/no.js +8 -0
  31. package/dist/cjs/lang/number-ratchet.js +156 -0
  32. package/dist/cjs/lang/parsed-url.js +2 -0
  33. package/dist/cjs/lang/promise-ratchet.js +142 -0
  34. package/dist/cjs/lang/require-ratchet.js +60 -0
  35. package/dist/cjs/lang/stop-watch.js +117 -0
  36. package/dist/cjs/lang/string-ratchet.js +195 -0
  37. package/dist/cjs/lang/time-zone-ratchet.js +80 -0
  38. package/dist/cjs/lang/timeout-token.js +19 -0
  39. package/dist/cjs/lang/transform-ratchet.js +70 -0
  40. package/dist/cjs/logger/classic-single-line-log-message-formatter.js +18 -0
  41. package/dist/cjs/logger/log-message-builder.js +51 -0
  42. package/dist/cjs/logger/log-message-format-type.js +9 -0
  43. package/dist/cjs/logger/log-message-formatter.js +2 -0
  44. package/dist/cjs/logger/log-message-processor.js +2 -0
  45. package/dist/cjs/logger/log-message.js +2 -0
  46. package/dist/cjs/logger/log-snapshot.js +2 -0
  47. package/dist/cjs/logger/logger-instance.js +207 -0
  48. package/dist/cjs/logger/logger-level-name.js +12 -0
  49. package/dist/cjs/logger/logger-meta.js +2 -0
  50. package/dist/cjs/logger/logger-options.js +2 -0
  51. package/dist/cjs/logger/logger-output-function.js +9 -0
  52. package/dist/cjs/logger/logger-ring-buffer.js +76 -0
  53. package/dist/cjs/logger/logger-util.js +49 -0
  54. package/dist/cjs/logger/logger.js +139 -0
  55. package/dist/cjs/logger/none-log-message-formatter.js +9 -0
  56. package/dist/cjs/logger/structured-json-log-message-formatter.js +24 -0
  57. package/dist/cjs/network/browser-local-ip-provider.js +25 -0
  58. package/dist/cjs/network/fixed-local-ip-provider.js +12 -0
  59. package/dist/cjs/network/local-ip-provider.js +2 -0
  60. package/dist/cjs/network/network-ratchet.js +106 -0
  61. package/dist/cjs/stream/buffer-writable.js +20 -0
  62. package/dist/cjs/stream/stream-ratchet.js +72 -0
  63. package/dist/cjs/stream/string-writable.js +18 -0
  64. package/dist/cjs/third-party/google/google-recaptcha-ratchet.js +33 -0
  65. package/dist/cjs/third-party/twilio/twilio-ratchet.js +75 -0
  66. package/dist/cjs/transform/built-in-transforms.js +195 -0
  67. package/dist/cjs/transform/transform-rule.js +2 -0
  68. package/dist/cjs/tx/transaction-configuration.js +2 -0
  69. package/dist/cjs/tx/transaction-final-state.js +9 -0
  70. package/dist/cjs/tx/transaction-ratchet.js +80 -0
  71. package/dist/cjs/tx/transaction-result.js +2 -0
  72. package/dist/cjs/tx/transaction-step.js +2 -0
  73. package/dist/es/2d/line-2d.js +1 -0
  74. package/dist/es/2d/plane-2d.js +1 -0
  75. package/dist/es/2d/point-2d.js +1 -0
  76. package/dist/es/2d/poly-line-2d.js +1 -0
  77. package/dist/es/2d/ratchet-2d.js +245 -0
  78. package/dist/es/build/build-information.js +1 -0
  79. package/dist/es/build/ratchet-common-info.js +14 -0
  80. package/dist/es/histogram/histogram-entry.js +1 -0
  81. package/dist/es/histogram/histogram.js +50 -0
  82. package/dist/es/index.js +71 -0
  83. package/dist/es/jwt/common-jwt-token.js +1 -0
  84. package/dist/es/jwt/expired-jwt-handling.js +6 -0
  85. package/dist/es/jwt/jwt-ratchet-like.js +1 -0
  86. package/dist/es/jwt/jwt-ratchet.js +159 -0
  87. package/dist/es/jwt/jwt-token-base.js +1 -0
  88. package/dist/es/lang/array-ratchet.js +83 -0
  89. package/dist/es/lang/base64-ratchet.js +45 -0
  90. package/dist/es/lang/boolean-ratchet.js +45 -0
  91. package/dist/es/lang/composite-last-success-provider.js +24 -0
  92. package/dist/es/lang/date-ratchet.js +29 -0
  93. package/dist/es/lang/duration-ratchet.js +42 -0
  94. package/dist/es/lang/enum-ratchet.js +30 -0
  95. package/dist/es/lang/error-ratchet.js +44 -0
  96. package/dist/es/lang/expiring-object.js +63 -0
  97. package/dist/es/lang/geolocation-ratchet.js +263 -0
  98. package/dist/es/lang/global-ratchet.js +43 -0
  99. package/dist/es/lang/key-value.js +2 -0
  100. package/dist/es/lang/last-success-provider.js +1 -0
  101. package/dist/es/lang/map-ratchet.js +164 -0
  102. package/dist/es/lang/no.js +4 -0
  103. package/dist/es/lang/number-ratchet.js +152 -0
  104. package/dist/es/lang/parsed-url.js +1 -0
  105. package/dist/es/lang/promise-ratchet.js +138 -0
  106. package/dist/es/lang/require-ratchet.js +56 -0
  107. package/dist/es/lang/stop-watch.js +113 -0
  108. package/dist/es/lang/string-ratchet.js +191 -0
  109. package/dist/es/lang/time-zone-ratchet.js +76 -0
  110. package/dist/es/lang/timeout-token.js +15 -0
  111. package/dist/es/lang/transform-ratchet.js +66 -0
  112. package/dist/es/logger/classic-single-line-log-message-formatter.js +13 -0
  113. package/dist/es/logger/log-message-builder.js +47 -0
  114. package/dist/es/logger/log-message-format-type.js +6 -0
  115. package/dist/es/logger/log-message-formatter.js +1 -0
  116. package/dist/es/logger/log-message-processor.js +1 -0
  117. package/dist/es/logger/log-message.js +1 -0
  118. package/dist/es/logger/log-snapshot.js +1 -0
  119. package/dist/es/logger/logger-instance.js +201 -0
  120. package/dist/es/logger/logger-level-name.js +9 -0
  121. package/dist/es/logger/logger-meta.js +1 -0
  122. package/dist/es/logger/logger-options.js +1 -0
  123. package/dist/es/logger/logger-output-function.js +6 -0
  124. package/dist/es/logger/logger-ring-buffer.js +72 -0
  125. package/dist/es/logger/logger-util.js +44 -0
  126. package/dist/es/logger/logger.js +134 -0
  127. package/dist/es/logger/none-log-message-formatter.js +5 -0
  128. package/dist/es/logger/structured-json-log-message-formatter.js +19 -0
  129. package/dist/es/network/browser-local-ip-provider.js +21 -0
  130. package/dist/es/network/fixed-local-ip-provider.js +8 -0
  131. package/dist/es/network/local-ip-provider.js +1 -0
  132. package/dist/es/network/network-ratchet.js +102 -0
  133. package/dist/es/stream/buffer-writable.js +16 -0
  134. package/dist/es/stream/stream-ratchet.js +68 -0
  135. package/dist/es/stream/string-writable.js +14 -0
  136. package/dist/es/third-party/google/google-recaptcha-ratchet.js +28 -0
  137. package/dist/es/third-party/twilio/twilio-ratchet.js +70 -0
  138. package/dist/es/transform/built-in-transforms.js +191 -0
  139. package/dist/es/transform/transform-rule.js +1 -0
  140. package/dist/es/tx/transaction-configuration.js +1 -0
  141. package/dist/es/tx/transaction-final-state.js +6 -0
  142. package/dist/es/tx/transaction-ratchet.js +76 -0
  143. package/dist/es/tx/transaction-result.js +1 -0
  144. package/dist/es/tx/transaction-step.js +1 -0
  145. package/dist/tsconfig.cjs.tsbuildinfo +1 -0
  146. package/dist/tsconfig.es.tsbuildinfo +1 -0
  147. package/dist/tsconfig.types.tsbuildinfo +1 -0
  148. package/dist/types/2d/line-2d.d.ts +5 -0
  149. package/dist/types/2d/plane-2d.d.ts +6 -0
  150. package/dist/types/2d/point-2d.d.ts +4 -0
  151. package/dist/types/2d/poly-line-2d.d.ts +4 -0
  152. package/dist/types/2d/ratchet-2d.d.ts +37 -0
  153. package/dist/types/build/build-information.d.ts +8 -0
  154. package/dist/types/build/ratchet-common-info.d.ts +5 -0
  155. package/dist/types/histogram/histogram-entry.d.ts +4 -0
  156. package/dist/types/histogram/histogram.d.ts +15 -0
  157. package/dist/types/index.d.ts +74 -0
  158. package/dist/types/jwt/common-jwt-token.d.ts +17 -0
  159. package/dist/types/jwt/expired-jwt-handling.d.ts +5 -0
  160. package/dist/types/jwt/jwt-ratchet-like.d.ts +18 -0
  161. package/dist/types/jwt/jwt-ratchet.d.ts +39 -0
  162. package/dist/types/jwt/jwt-token-base.d.ts +13 -0
  163. package/dist/types/lang/array-ratchet.d.ts +30 -0
  164. package/dist/types/lang/base64-ratchet.d.ts +10 -0
  165. package/dist/types/lang/boolean-ratchet.d.ts +7 -0
  166. package/dist/types/lang/composite-last-success-provider.d.ts +8 -0
  167. package/dist/types/lang/date-ratchet.d.ts +11 -0
  168. package/dist/types/lang/duration-ratchet.d.ts +7 -0
  169. package/dist/types/lang/enum-ratchet.d.ts +12 -0
  170. package/dist/types/lang/error-ratchet.d.ts +6 -0
  171. package/dist/types/lang/expiring-object.d.ts +20 -0
  172. package/dist/types/lang/geolocation-ratchet.d.ts +40 -0
  173. package/dist/types/lang/global-ratchet.d.ts +6 -0
  174. package/dist/types/lang/key-value.d.ts +4 -0
  175. package/dist/types/lang/last-success-provider.d.ts +4 -0
  176. package/dist/types/lang/map-ratchet.d.ts +15 -0
  177. package/dist/types/lang/no.d.ts +3 -0
  178. package/dist/types/lang/number-ratchet.d.ts +20 -0
  179. package/dist/types/lang/parsed-url.d.ts +10 -0
  180. package/dist/types/lang/promise-ratchet.d.ts +32 -0
  181. package/dist/types/lang/require-ratchet.d.ts +11 -0
  182. package/dist/types/lang/stop-watch.d.ts +17 -0
  183. package/dist/types/lang/string-ratchet.d.ts +27 -0
  184. package/dist/types/lang/time-zone-ratchet.d.ts +50 -0
  185. package/dist/types/lang/timeout-token.d.ts +9 -0
  186. package/dist/types/lang/transform-ratchet.d.ts +7 -0
  187. package/dist/types/logger/classic-single-line-log-message-formatter.d.ts +6 -0
  188. package/dist/types/logger/log-message-builder.d.ts +14 -0
  189. package/dist/types/logger/log-message-format-type.d.ts +5 -0
  190. package/dist/types/logger/log-message-formatter.d.ts +5 -0
  191. package/dist/types/logger/log-message-processor.d.ts +5 -0
  192. package/dist/types/logger/log-message.d.ts +8 -0
  193. package/dist/types/logger/log-snapshot.d.ts +5 -0
  194. package/dist/types/logger/logger-instance.d.ts +46 -0
  195. package/dist/types/logger/logger-level-name.d.ts +8 -0
  196. package/dist/types/logger/logger-meta.d.ts +6 -0
  197. package/dist/types/logger/logger-options.d.ts +13 -0
  198. package/dist/types/logger/logger-output-function.d.ts +5 -0
  199. package/dist/types/logger/logger-ring-buffer.d.ts +18 -0
  200. package/dist/types/logger/logger-util.d.ts +8 -0
  201. package/dist/types/logger/logger.d.ts +52 -0
  202. package/dist/types/logger/none-log-message-formatter.d.ts +6 -0
  203. package/dist/types/logger/structured-json-log-message-formatter.d.ts +6 -0
  204. package/dist/types/network/browser-local-ip-provider.d.ts +7 -0
  205. package/dist/types/network/fixed-local-ip-provider.d.ts +6 -0
  206. package/dist/types/network/local-ip-provider.d.ts +4 -0
  207. package/dist/types/network/network-ratchet.d.ts +9 -0
  208. package/dist/types/stream/buffer-writable.d.ts +9 -0
  209. package/dist/types/stream/stream-ratchet.d.ts +12 -0
  210. package/dist/types/stream/string-writable.d.ts +8 -0
  211. package/dist/types/third-party/google/google-recaptcha-ratchet.d.ts +4 -0
  212. package/dist/types/third-party/twilio/twilio-ratchet.d.ts +15 -0
  213. package/dist/types/transform/built-in-transforms.d.ts +18 -0
  214. package/dist/types/transform/transform-rule.d.ts +3 -0
  215. package/dist/types/tx/transaction-configuration.d.ts +7 -0
  216. package/dist/types/tx/transaction-final-state.d.ts +5 -0
  217. package/dist/types/tx/transaction-ratchet.d.ts +6 -0
  218. package/dist/types/tx/transaction-result.d.ts +9 -0
  219. package/dist/types/tx/transaction-step.d.ts +5 -0
  220. package/package.json +65 -0
@@ -0,0 +1,45 @@
1
+ import { Logger } from '../logger/logger';
2
+ export class Base64Ratchet {
3
+ static safeObjectToBase64JSON(input) {
4
+ return !!input ? Base64Ratchet.generateBase64VersionOfString(JSON.stringify(input)) : null;
5
+ }
6
+ static safeBase64JSONParse(input) {
7
+ let rval = {};
8
+ try {
9
+ if (input) {
10
+ rval = JSON.parse(Base64Ratchet.base64StringToString(input));
11
+ }
12
+ }
13
+ catch (err) {
14
+ Logger.warn('Error parsing b64/json : %s as json, got %s', input, err, err);
15
+ rval = {};
16
+ }
17
+ return rval;
18
+ }
19
+ static generateBase64VersionOfBlob(blob) {
20
+ return new Promise(function (resolve, reject) {
21
+ if (!blob || blob.size == 0) {
22
+ reject('Wont convert null or non-blob or empty blob');
23
+ }
24
+ else {
25
+ const reader = new FileReader();
26
+ reader.onloadend = function () {
27
+ resolve(reader.result.toString());
28
+ };
29
+ reader.readAsDataURL(blob);
30
+ }
31
+ });
32
+ }
33
+ static generateBase64VersionOfString(input) {
34
+ return Base64Ratchet.generateBase64VersionOfBuffer(Buffer.from(input));
35
+ }
36
+ static generateBase64VersionOfBuffer(input) {
37
+ return input.toString('base64');
38
+ }
39
+ static base64StringToBuffer(input) {
40
+ return Buffer.from(input, 'base64');
41
+ }
42
+ static base64StringToString(input, encoding = 'utf8') {
43
+ return Buffer.from(input, 'base64').toString(encoding);
44
+ }
45
+ }
@@ -0,0 +1,45 @@
1
+ import { NumberRatchet } from './number-ratchet';
2
+ export class BooleanRatchet {
3
+ static allTrue(vals, emptyArraysReturn = false) {
4
+ let rval = null;
5
+ if (!!vals) {
6
+ if (vals.length > 0) {
7
+ rval = vals.reduce((a, i) => a && i, true);
8
+ }
9
+ else {
10
+ rval = emptyArraysReturn;
11
+ }
12
+ }
13
+ else {
14
+ rval = false;
15
+ }
16
+ return rval;
17
+ }
18
+ static anyTrue(vals, emptyArraysReturn = false) {
19
+ let rval = null;
20
+ if (!!vals) {
21
+ if (vals.length > 0) {
22
+ rval = vals.reduce((a, i) => a || i, false);
23
+ }
24
+ else {
25
+ rval = emptyArraysReturn;
26
+ }
27
+ }
28
+ else {
29
+ rval = false;
30
+ }
31
+ return rval;
32
+ }
33
+ static parseBool(val) {
34
+ return val === true || (val !== null && val !== undefined && typeof val === 'string' && val.toLowerCase() === 'true');
35
+ }
36
+ static intToBool(val) {
37
+ if (val === null || val === undefined) {
38
+ return false;
39
+ }
40
+ return NumberRatchet.safeNumber(val) !== 0;
41
+ }
42
+ static boolToInt(val) {
43
+ return BooleanRatchet.parseBool(val) ? 1 : 0;
44
+ }
45
+ }
@@ -0,0 +1,24 @@
1
+ export class CompositeLastSuccessProvider {
2
+ constructor(src, mostRecentSrc = true) {
3
+ if (!src || src.length == 0) {
4
+ throw Error('Cannot create composite provider with null/empty sources');
5
+ }
6
+ this.sources = src;
7
+ this.mostRecent = mostRecentSrc;
8
+ }
9
+ lastSuccess() {
10
+ let rval = null;
11
+ this.sources.forEach((s) => {
12
+ const val = s.lastSuccess();
13
+ if (val != null) {
14
+ if (rval == null) {
15
+ rval = val;
16
+ }
17
+ else {
18
+ rval = (val > rval && this.mostRecent) || (val < rval && !this.mostRecent) ? val : rval;
19
+ }
20
+ }
21
+ });
22
+ return rval;
23
+ }
24
+ }
@@ -0,0 +1,29 @@
1
+ import { DateTime } from 'luxon';
2
+ export class DateRatchet {
3
+ static formatFullDate(input) {
4
+ return DateTime.fromJSDate(input).toFormat(DateRatchet.FULL_DATE_FORMAT);
5
+ }
6
+ static formatDefaultDateOnly(input) {
7
+ return DateTime.fromJSDate(input).toFormat(DateRatchet.DEFAULT_DATE_FORMAT);
8
+ }
9
+ static parseDefaultDate(input) {
10
+ const rval = DateTime.fromFormat(input, DateRatchet.DEFAULT_DATE_FORMAT).toJSDate();
11
+ return rval;
12
+ }
13
+ static parseCommonUsDate(input) {
14
+ let rval = null;
15
+ if (!!input) {
16
+ let templ = DateRatchet.COMMON_US_DATE_FORMAT;
17
+ if (input.indexOf('-') === 2) {
18
+ templ = templ.split('/').join('-');
19
+ }
20
+ rval = DateTime.fromFormat(input, templ).toJSDate();
21
+ }
22
+ return rval;
23
+ }
24
+ }
25
+ DateRatchet.COMMON_US_DATE_FORMAT = 'MM/dd/yyyy';
26
+ DateRatchet.DEFAULT_DATE_FORMAT = 'yyyy-MM-dd';
27
+ DateRatchet.FULL_DATE_FORMAT = 'yyyy-MM-dd_HH_mm_ss';
28
+ DateRatchet.PACIFIC_TIME_ZONE = 'America/Los_Angeles';
29
+ DateRatchet.UTC_TIME_ZONE = 'etc/UTC';
@@ -0,0 +1,42 @@
1
+ import { DateTime } from 'luxon';
2
+ import { NumberRatchet } from './number-ratchet';
3
+ export class DurationRatchet {
4
+ static formatMsDuration(ms, includeMS = false) {
5
+ const rem_ms = Math.floor(ms % 1000);
6
+ const seconds = Math.floor(ms / 1000) % 60;
7
+ const minutes = Math.floor(ms / (1000 * 60)) % 60;
8
+ const hours = Math.floor(ms / (1000 * 60 * 60)) % 24;
9
+ const days = Math.floor(ms / (1000 * 60 * 60 * 24));
10
+ const f = NumberRatchet.leadingZeros;
11
+ let rval = f(hours, 2) + 'h' + f(minutes, 2) + 'm';
12
+ rval += includeMS ? f(seconds, 2) + '.' + f(rem_ms, 3) + 's' : f(seconds, 2) + 's';
13
+ if (days > 0) {
14
+ rval = days + 'd' + rval;
15
+ }
16
+ return rval;
17
+ }
18
+ static colonFormatMsDuration(ms, includeMS = false) {
19
+ const rem_ms = ms % 1000;
20
+ const seconds = Math.floor(ms / 1000) % 60;
21
+ const minutes = Math.floor(ms / (1000 * 60)) % 60;
22
+ const hours = Math.floor(ms / (1000 * 60 * 60));
23
+ const f = NumberRatchet.leadingZeros;
24
+ let rval = f(hours, 2) + ':' + f(minutes, 2) + ':';
25
+ rval += includeMS ? f(seconds, 2) + '.' + f(rem_ms, 3) : f(seconds, 2);
26
+ return rval;
27
+ }
28
+ static daysBetween(d1, d2) {
29
+ const dur = DateTime.fromJSDate(d1).diff(DateTime.fromJSDate(d2));
30
+ return dur.days;
31
+ }
32
+ static createSteps(startEpochMS, endEpochMS, timezone, outputFormat, stepUnit) {
33
+ let curDate = DateTime.fromMillis(startEpochMS).setZone(timezone);
34
+ const endDate = DateTime.fromMillis(endEpochMS);
35
+ const rval = [];
36
+ while (curDate < endDate) {
37
+ rval.push(curDate.toFormat(outputFormat));
38
+ curDate = curDate.plus(stepUnit);
39
+ }
40
+ return rval;
41
+ }
42
+ }
@@ -0,0 +1,30 @@
1
+ import { $enum } from 'ts-enum-util';
2
+ import { StringRatchet } from './string-ratchet';
3
+ export class EnumRatchet {
4
+ static listEnumKeys(enumeration) {
5
+ const rval = $enum(enumeration).getValues();
6
+ return rval;
7
+ }
8
+ static keyToEnum(enumeration, val, caseSensitive = false) {
9
+ const e = $enum(enumeration);
10
+ let rval = null;
11
+ if (!!val) {
12
+ rval = e.asValueOrDefault(val, null);
13
+ if (!rval && !caseSensitive) {
14
+ const keys = EnumRatchet.listEnumKeys(enumeration);
15
+ const mKey = keys.find((k) => k.toUpperCase() === val.toUpperCase());
16
+ if (!!mKey) {
17
+ rval = e.asValueOrDefault(mKey, null);
18
+ }
19
+ }
20
+ }
21
+ return rval;
22
+ }
23
+ static parseCsvToEnumArray(enumeration, input) {
24
+ const split = StringRatchet.trimToEmpty(input)
25
+ .split(',')
26
+ .map((s) => s.trim());
27
+ const rval = split.map((s) => EnumRatchet.keyToEnum(enumeration, s)).filter((s) => !!s);
28
+ return rval;
29
+ }
30
+ }
@@ -0,0 +1,44 @@
1
+ import { Logger } from '../logger/logger';
2
+ import util from 'util';
3
+ export class ErrorRatchet {
4
+ static safeStringifyErr(err, log = true) {
5
+ let rval = 'ERR WAS NULL';
6
+ if (err) {
7
+ if (err['message']) {
8
+ rval = err['message'];
9
+ }
10
+ else {
11
+ try {
12
+ rval = JSON.stringify(err);
13
+ }
14
+ catch (err2) {
15
+ Logger.error('Failed to json stringify');
16
+ rval = String(err);
17
+ }
18
+ }
19
+ }
20
+ if (log) {
21
+ Logger.error('%s', rval, err);
22
+ }
23
+ return rval;
24
+ }
25
+ static asErr(input) {
26
+ let rval = null;
27
+ if (input) {
28
+ if (input instanceof Error) {
29
+ rval = input;
30
+ }
31
+ else {
32
+ rval = new Error('Force-Cast to error : ' + String(input));
33
+ }
34
+ }
35
+ return rval;
36
+ }
37
+ static fErr(format, ...input) {
38
+ const msg = util.format(format, ...input);
39
+ return new Error(msg);
40
+ }
41
+ static throwFormattedErr(format, ...input) {
42
+ throw ErrorRatchet.fErr(format, ...input);
43
+ }
44
+ }
@@ -0,0 +1,63 @@
1
+ import { Logger } from '../logger/logger';
2
+ import { ErrorRatchet } from './error-ratchet';
3
+ import { LoggerLevelName } from '../logger/logger-level-name';
4
+ export class ExpiringObject {
5
+ constructor(inputConfig) {
6
+ this._config = Object.assign({}, this.defaultConfig(), inputConfig || {});
7
+ if (this._config.overrideTimeRemainingMS && this._config.timeToLiveMS) {
8
+ ErrorRatchet.throwFormattedErr('Cannot define both time to live and overrideTimeRemainingMS');
9
+ }
10
+ if (!this._config.overrideTimeRemainingMS && !this._config.timeToLiveMS) {
11
+ ErrorRatchet.throwFormattedErr('Must define exactly one of timeToLiveMS or overrideTimeRemainingMS');
12
+ }
13
+ if (this._config.initialValue) {
14
+ this.update(this._config.initialValue);
15
+ }
16
+ this._timeRemainingMS = this._config.overrideTimeRemainingMS || this.defaultTimeRemainingMS;
17
+ }
18
+ defaultConfig() {
19
+ const rval = {
20
+ generator: null,
21
+ initialValue: null,
22
+ logLevel: LoggerLevelName.debug,
23
+ overrideTimeRemainingMS: null,
24
+ timeToLiveMS: 1000 * 60,
25
+ };
26
+ return rval;
27
+ }
28
+ async defaultTimeRemainingMS(lastUpdatedEpochMS) {
29
+ let rval = 0;
30
+ if (lastUpdatedEpochMS) {
31
+ const ageMS = new Date().getTime() - lastUpdatedEpochMS;
32
+ rval = Math.max(0, this._config.timeToLiveMS - ageMS);
33
+ }
34
+ return rval;
35
+ }
36
+ update(newValue, doNotUpdateClock = false) {
37
+ this._cacheObject = newValue;
38
+ if (!doNotUpdateClock) {
39
+ this._lastUpdatedEpochMS = new Date().getTime();
40
+ }
41
+ Logger.logByLevel(this._config.logLevel, 'Updated cache value to %j', newValue);
42
+ }
43
+ async fetchCacheObjectTimeRemainingMS() {
44
+ return this._timeRemainingMS(this._lastUpdatedEpochMS);
45
+ }
46
+ async fetch() {
47
+ const remainMS = await this._timeRemainingMS(this._lastUpdatedEpochMS);
48
+ if (!remainMS) {
49
+ this._cacheObject = null;
50
+ this._lastUpdatedEpochMS = null;
51
+ }
52
+ if (!this._cacheObject) {
53
+ if (this._config.generator) {
54
+ const newValue = await this._config.generator();
55
+ Logger.logByLevel(this._config.logLevel, 'Auto call to generator returned %j', newValue);
56
+ this.update(newValue);
57
+ }
58
+ }
59
+ return this._cacheObject;
60
+ }
61
+ }
62
+ export class ExpiringObjectConfig {
63
+ }
@@ -0,0 +1,263 @@
1
+ import { RequireRatchet } from './require-ratchet';
2
+ import { NumberRatchet } from './number-ratchet';
3
+ import { ErrorRatchet } from './error-ratchet';
4
+ import { Logger } from '../logger/logger';
5
+ export class GeolocationRatchet {
6
+ constructor() { }
7
+ static distanceBetweenLocations(lat1, lon1, lat2, lon2, unit = 'M') {
8
+ const uU = !!unit ? unit.toUpperCase() : '';
9
+ if (['M', 'K', 'N', 'F', 'E'].indexOf(uU) === -1) {
10
+ throw new Error('Invalid unit');
11
+ }
12
+ if (lat1 == lat2 && lon1 == lon2) {
13
+ return 0;
14
+ }
15
+ else {
16
+ const radlat1 = (Math.PI * lat1) / 180;
17
+ const radlat2 = (Math.PI * lat2) / 180;
18
+ const theta = lon1 - lon2;
19
+ const radtheta = (Math.PI * theta) / 180;
20
+ let dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
21
+ if (dist > 1) {
22
+ dist = 1;
23
+ }
24
+ dist = Math.acos(dist);
25
+ dist = (dist * 180) / Math.PI;
26
+ dist = dist * 60 * 1.1515;
27
+ if (uU === 'F') {
28
+ dist *= 5280;
29
+ }
30
+ if (uU === 'K') {
31
+ dist *= 1.609344;
32
+ }
33
+ if (uU === 'E') {
34
+ dist *= 1609.344;
35
+ }
36
+ if (uU === 'N') {
37
+ dist *= 0.8684;
38
+ }
39
+ return dist;
40
+ }
41
+ }
42
+ static distanceBetweenRatchetGeoLocations(loc1, loc2, unit = 'M') {
43
+ return GeolocationRatchet.distanceBetweenLocations(loc1.lat, loc1.lng, loc2.lat, loc2.lng, unit);
44
+ }
45
+ static degreeOfLatLngInMiles(latitudeInDecimalDegress = 0) {
46
+ const latInRads = (latitudeInDecimalDegress * Math.PI) / 180;
47
+ const cosLat = Math.cos(latInRads);
48
+ const rval = NumberRatchet.safeNumber((cosLat * 69.172).toFixed(4));
49
+ return rval;
50
+ }
51
+ static milesInDegLatLng(miles, latitudeInDecimalDegress = 0) {
52
+ RequireRatchet.notNullOrUndefined(miles);
53
+ RequireRatchet.true(miles >= 0);
54
+ const degreeInMiles = GeolocationRatchet.degreeOfLatLngInMiles(latitudeInDecimalDegress);
55
+ return miles / degreeInMiles;
56
+ }
57
+ static centerOfBounds(bounds) {
58
+ RequireRatchet.notNullOrUndefined(bounds);
59
+ return {
60
+ lat: (bounds.extent.lat + bounds.origin.lat) / 2,
61
+ lng: (bounds.extent.lng + bounds.origin.lng) / 2,
62
+ };
63
+ }
64
+ static calculateSplits(input, slices, field) {
65
+ RequireRatchet.notNullOrUndefined(input);
66
+ RequireRatchet.notNullOrUndefined(slices);
67
+ RequireRatchet.notNullOrUndefined(field);
68
+ input.sort((a, b) => a.origin[field] - b.origin[field]);
69
+ const centers = input.map((i) => GeolocationRatchet.centerOfBounds(i));
70
+ const vals = centers.map((c) => c[field]);
71
+ const splits = [];
72
+ for (let i = 1; i < vals.length; i++) {
73
+ const size = vals[i] - vals[i - 1];
74
+ if (splits.length < slices) {
75
+ splits.push({ idx: i, size: size });
76
+ splits.sort((a, b) => a.size - b.size);
77
+ }
78
+ else if (size > splits[0].size) {
79
+ splits[0] = { idx: i, size: size };
80
+ splits.sort((a, b) => a.size - b.size);
81
+ }
82
+ else {
83
+ Logger.silly('Skipping, size : %d, %j', size, splits);
84
+ }
85
+ }
86
+ Logger.info('Splits at : %j', splits);
87
+ splits.sort((a, b) => a.idx - b.idx);
88
+ return splits;
89
+ }
90
+ static clusterGeoBounds(inputVal, latSlices = 2, lngSlices = 5) {
91
+ let rval = null;
92
+ if (latSlices * lngSlices < 2) {
93
+ ErrorRatchet.throwFormattedErr('Cannot set slices to less than 2 : %d x %d', latSlices, lngSlices);
94
+ }
95
+ if (!!inputVal) {
96
+ rval = [];
97
+ const input = Object.assign([], inputVal);
98
+ input.sort((a, b) => a.origin.lng - b.origin.lng);
99
+ const lngSplits = GeolocationRatchet.calculateSplits(inputVal, lngSlices - 1, 'lng');
100
+ lngSplits.sort((a, b) => a.idx - b.idx);
101
+ for (let i = 0; i <= lngSplits.length; i++) {
102
+ const lngStartIdx = i === 0 ? 0 : lngSplits[i - 1].idx;
103
+ const lngEndIdx = i === lngSplits.length ? input.length : lngSplits[i].idx;
104
+ const lngBatch = input.slice(lngStartIdx, lngEndIdx);
105
+ lngBatch.sort((a, b) => a.origin.lat - b.origin.lat);
106
+ const latSplits = GeolocationRatchet.calculateSplits(lngBatch, latSlices - 1, 'lat');
107
+ for (let j = 0; j <= latSplits.length; j++) {
108
+ const latStartIdx = j == 0 ? 0 : latSplits[j - 1].idx;
109
+ const latEndIdx = j === latSplits.length ? lngBatch.length : latSplits[j].idx;
110
+ const latBatch = lngBatch.slice(latStartIdx, latEndIdx);
111
+ rval.push(GeolocationRatchet.combineBounds(latBatch));
112
+ }
113
+ }
114
+ Logger.info('New bounds : %j', rval);
115
+ }
116
+ return rval;
117
+ }
118
+ static canonicalizeBounds(inp, allowCrossover = false) {
119
+ RequireRatchet.notNullOrUndefined(inp, 'RatchetLocationBounds');
120
+ const minLat = Math.min(inp.extent.lat, inp.origin.lat);
121
+ const maxLat = Math.max(inp.extent.lat, inp.origin.lat);
122
+ const minLng = Math.min(inp.extent.lng, inp.origin.lng);
123
+ const maxLng = Math.max(inp.extent.lng, inp.origin.lng);
124
+ const latXover = (minLat < 0 && maxLat > 0) || (minLat > 0 && maxLat < 0);
125
+ const lngXover = (minLat < 0 && maxLat > 0) || (minLat > 0 && maxLat < 0);
126
+ if (latXover || lngXover) {
127
+ if (allowCrossover) {
128
+ return inp;
129
+ }
130
+ else {
131
+ throw new Error('Cannot canonicalize, bounds crosses over boundary');
132
+ }
133
+ }
134
+ const rval = {
135
+ origin: {
136
+ lat: minLat,
137
+ lng: minLng,
138
+ },
139
+ extent: {
140
+ lat: maxLat,
141
+ lng: maxLng,
142
+ },
143
+ };
144
+ return rval;
145
+ }
146
+ static combineBounds(inp) {
147
+ let rval = null;
148
+ if (inp && inp.length > 0) {
149
+ rval = {
150
+ origin: {
151
+ lat: inp.map((i) => i.origin.lat).reduce((a, i) => Math.min(a, i)),
152
+ lng: inp.map((i) => i.origin.lng).reduce((a, i) => Math.min(a, i)),
153
+ },
154
+ extent: {
155
+ lat: inp.map((i) => i.extent.lat).reduce((a, i) => Math.max(a, i)),
156
+ lng: inp.map((i) => i.extent.lng).reduce((a, i) => Math.max(a, i)),
157
+ },
158
+ };
159
+ }
160
+ return rval;
161
+ }
162
+ static roundLocation(r, roundDigits) {
163
+ return {
164
+ lat: NumberRatchet.safeNumber(r.lat.toFixed(roundDigits)),
165
+ lng: NumberRatchet.safeNumber(r.lng.toFixed(roundDigits)),
166
+ };
167
+ }
168
+ static locationToBounds(loc, radiusMiles) {
169
+ const offset = GeolocationRatchet.milesInDegLatLng(radiusMiles, loc.lat);
170
+ const gfb = {
171
+ origin: {
172
+ lat: loc.lat - offset,
173
+ lng: loc.lng - offset,
174
+ },
175
+ extent: {
176
+ lat: loc.lat + offset,
177
+ lng: loc.lng + offset,
178
+ },
179
+ };
180
+ return gfb;
181
+ }
182
+ static sameLocation(loc1, loc2) {
183
+ return !!loc1 && !!loc2 && loc1.lat === loc2.lat && loc1.lng === loc2.lng;
184
+ }
185
+ static pointInBounds(pt, bound) {
186
+ return (!!pt &&
187
+ !!bound &&
188
+ NumberRatchet.between(pt.lat, bound.origin.lat, bound.extent.lat) &&
189
+ NumberRatchet.between(pt.lng, bound.origin.lng, bound.extent.lng));
190
+ }
191
+ static pointInAnyBound(pt, inBounds, minPointsBeforeMapping = 10) {
192
+ let rval = false;
193
+ if (inBounds.length > minPointsBeforeMapping) {
194
+ const mp = GeolocationRatchet.buildRatchetLocationBoundsMap(inBounds);
195
+ rval = GeolocationRatchet.pointInRatchetLocationBoundsMap(pt, mp);
196
+ }
197
+ else {
198
+ for (let i = 0; i < inBounds.length && !rval; i++) {
199
+ rval = GeolocationRatchet.pointInBounds(pt, inBounds[i]);
200
+ }
201
+ }
202
+ return rval;
203
+ }
204
+ static pointInRatchetLocationBoundsMap(pt, mp) {
205
+ let rval = false;
206
+ const entry = GeolocationRatchet.findRatchetLocationBoundsMapEntry(pt, mp);
207
+ if (!!entry) {
208
+ const bounds = entry.bounds;
209
+ for (let i = 0; i < bounds.length && !rval; i++) {
210
+ rval = GeolocationRatchet.pointInBounds(pt, bounds[i]);
211
+ }
212
+ }
213
+ return rval;
214
+ }
215
+ static findRatchetLocationBoundsMapEntry(pt, mp) {
216
+ let rval = null;
217
+ if (pt.lat >= mp.latOffset && pt.lat <= mp.maxLat && pt.lng >= mp.lngOffset && pt.lng <= mp.maxLng) {
218
+ const ltIdx = Math.trunc(pt.lat) - mp.latOffset;
219
+ const lngIdx = Math.trunc(pt.lng) - mp.lngOffset;
220
+ rval = mp.mapping[ltIdx][lngIdx];
221
+ }
222
+ return rval;
223
+ }
224
+ static buildRatchetLocationBoundsMap(inBounds) {
225
+ const minLat = inBounds.map((i) => i.origin.lat).reduce((a, i) => Math.min(a, i));
226
+ const minLng = inBounds.map((i) => i.origin.lng).reduce((a, i) => Math.min(a, i));
227
+ const maxLat = inBounds.map((i) => i.extent.lat).reduce((a, i) => Math.max(a, i));
228
+ const maxLng = inBounds.map((i) => i.extent.lng).reduce((a, i) => Math.max(a, i));
229
+ const latOffset = Math.trunc(minLat) - 1;
230
+ const lngOffset = Math.trunc(minLng) - 1;
231
+ const latEntries = Math.trunc(maxLat) - latOffset + 1;
232
+ const lngEntries = Math.trunc(maxLng) - lngOffset + 1;
233
+ const mapping = [];
234
+ for (let i = 0; i < latEntries; i++) {
235
+ const newRow = [];
236
+ for (let j = 0; j < lngEntries; j++) {
237
+ newRow.push({
238
+ lat: latOffset + i,
239
+ lng: lngOffset + j,
240
+ bounds: [],
241
+ });
242
+ }
243
+ mapping.push(newRow);
244
+ }
245
+ inBounds.forEach((b) => {
246
+ for (let i = Math.trunc(b.origin.lat); i <= Math.trunc(b.extent.lat); i++) {
247
+ const latIdx = i - latOffset;
248
+ const row = mapping[latIdx];
249
+ for (let j = Math.trunc(b.origin.lng); j <= Math.trunc(b.extent.lng); j++) {
250
+ const lngIdx = j - lngOffset;
251
+ row[lngIdx].bounds.push(b);
252
+ }
253
+ }
254
+ });
255
+ return {
256
+ latOffset: latOffset,
257
+ lngOffset: lngOffset,
258
+ maxLat: latOffset + latEntries,
259
+ maxLng: lngOffset + lngEntries,
260
+ mapping: mapping,
261
+ };
262
+ }
263
+ }
@@ -0,0 +1,43 @@
1
+ import { ErrorRatchet } from './error-ratchet';
2
+ import { RequireRatchet } from './require-ratchet';
3
+ import { Logger } from '../logger/logger';
4
+ export class GlobalRatchet {
5
+ constructor() { }
6
+ static fetchGlobalRecord(returnNullOnNone) {
7
+ let rval = null;
8
+ if (process?.env) {
9
+ Logger.silly('Using process.env as global');
10
+ rval = process.env;
11
+ }
12
+ else if (global?.process?.env) {
13
+ Logger.silly('Using global.process.env as global');
14
+ rval = global.process.env;
15
+ }
16
+ else if (global) {
17
+ Logger.silly('Using process.env as global');
18
+ rval = global;
19
+ }
20
+ if (!rval) {
21
+ if (returnNullOnNone) {
22
+ Logger.silly('Returning null as global');
23
+ rval = null;
24
+ }
25
+ else {
26
+ throw ErrorRatchet.fErr('Could not find process.env OR global in scope');
27
+ }
28
+ }
29
+ return rval;
30
+ }
31
+ static fetchGlobalVar(envVar) {
32
+ RequireRatchet.notNullOrUndefined(envVar, 'envVar');
33
+ const myGlobal = GlobalRatchet.fetchGlobalRecord();
34
+ const value = myGlobal[envVar];
35
+ return value;
36
+ }
37
+ static setGlobalVar(envVar, value) {
38
+ RequireRatchet.notNullOrUndefined(envVar, 'envVar');
39
+ RequireRatchet.notNullOrUndefined(value, 'value');
40
+ const myGlobal = GlobalRatchet.fetchGlobalRecord();
41
+ myGlobal[envVar] = value;
42
+ }
43
+ }
@@ -0,0 +1,2 @@
1
+ export class KeyValue {
2
+ }
@@ -0,0 +1 @@
1
+ export {};