@blazedpath/commons 0.0.4

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 (224) hide show
  1. package/README.md +3 -0
  2. package/blz-base/health/index.js +215 -0
  3. package/blz-base/index.js +1466 -0
  4. package/blz-cache/LruCache.js +44 -0
  5. package/blz-cache/index.js +29 -0
  6. package/blz-config/index.js +434 -0
  7. package/blz-core/index.js +364 -0
  8. package/blz-cryptography/index.js +54 -0
  9. package/blz-datetimes/index.js +356 -0
  10. package/blz-file/example.dat +2545 -0
  11. package/blz-file/fileService.js +205 -0
  12. package/blz-file/index.js +94 -0
  13. package/blz-file/index.test.js +31 -0
  14. package/blz-file/lab.js +33 -0
  15. package/blz-hazelcast/index.js +189 -0
  16. package/blz-hazelcast/lib/credentials.js +25 -0
  17. package/blz-hazelcast/lib/credentialsFactory.js +12 -0
  18. package/blz-hazelcast/lib/hazelcastCache.js +234 -0
  19. package/blz-iterable/index.js +446 -0
  20. package/blz-json-schema/index.js +11 -0
  21. package/blz-jwt/index.js +121 -0
  22. package/blz-kafka/index.js +522 -0
  23. package/blz-math/index.js +131 -0
  24. package/blz-mongodb/index.js +326 -0
  25. package/blz-rds/__test__/scape.test.js +58 -0
  26. package/blz-rds/blz-rds-executor.js +578 -0
  27. package/blz-rds/blz-rds-helper.js +310 -0
  28. package/blz-rds/commands/core/add.js +13 -0
  29. package/blz-rds/commands/core/and.js +18 -0
  30. package/blz-rds/commands/core/asc.js +10 -0
  31. package/blz-rds/commands/core/avg.js +10 -0
  32. package/blz-rds/commands/core/column-ref.js +8 -0
  33. package/blz-rds/commands/core/count-distinct.js +10 -0
  34. package/blz-rds/commands/core/count.js +10 -0
  35. package/blz-rds/commands/core/decimal.js +8 -0
  36. package/blz-rds/commands/core/desc.js +10 -0
  37. package/blz-rds/commands/core/distinct.js +10 -0
  38. package/blz-rds/commands/core/divide.js +11 -0
  39. package/blz-rds/commands/core/embedded-exists.js +17 -0
  40. package/blz-rds/commands/core/embedded-select.js +17 -0
  41. package/blz-rds/commands/core/equals.js +9 -0
  42. package/blz-rds/commands/core/false.js +8 -0
  43. package/blz-rds/commands/core/greater-or-equal.js +9 -0
  44. package/blz-rds/commands/core/greater.js +9 -0
  45. package/blz-rds/commands/core/in.js +9 -0
  46. package/blz-rds/commands/core/integer.js +8 -0
  47. package/blz-rds/commands/core/is-not-null.js +11 -0
  48. package/blz-rds/commands/core/is-null-or-value.js +10 -0
  49. package/blz-rds/commands/core/is-null.js +11 -0
  50. package/blz-rds/commands/core/less-or-equal.js +9 -0
  51. package/blz-rds/commands/core/less-unary.js +12 -0
  52. package/blz-rds/commands/core/less.js +9 -0
  53. package/blz-rds/commands/core/like.js +12 -0
  54. package/blz-rds/commands/core/max.js +10 -0
  55. package/blz-rds/commands/core/min.js +10 -0
  56. package/blz-rds/commands/core/multiply.js +13 -0
  57. package/blz-rds/commands/core/not-equals.js +9 -0
  58. package/blz-rds/commands/core/not-in.js +9 -0
  59. package/blz-rds/commands/core/not.js +13 -0
  60. package/blz-rds/commands/core/null.js +8 -0
  61. package/blz-rds/commands/core/nvl.js +11 -0
  62. package/blz-rds/commands/core/or.js +13 -0
  63. package/blz-rds/commands/core/parameter.js +34 -0
  64. package/blz-rds/commands/core/remainder.js +16 -0
  65. package/blz-rds/commands/core/string.js +8 -0
  66. package/blz-rds/commands/core/subtract.js +13 -0
  67. package/blz-rds/commands/core/sum.js +10 -0
  68. package/blz-rds/commands/core/true.js +8 -0
  69. package/blz-rds/commands/core/tuple.js +13 -0
  70. package/blz-rds/commands/datetimes/add-days.js +11 -0
  71. package/blz-rds/commands/datetimes/add-hours.js +11 -0
  72. package/blz-rds/commands/datetimes/add-milliseconds.js +11 -0
  73. package/blz-rds/commands/datetimes/add-minutes.js +11 -0
  74. package/blz-rds/commands/datetimes/add-months.js +11 -0
  75. package/blz-rds/commands/datetimes/add-seconds.js +11 -0
  76. package/blz-rds/commands/datetimes/add-years.js +11 -0
  77. package/blz-rds/commands/datetimes/date-diff.js +11 -0
  78. package/blz-rds/commands/datetimes/date.js +12 -0
  79. package/blz-rds/commands/datetimes/datetime-diff.js +11 -0
  80. package/blz-rds/commands/datetimes/datetime.js +15 -0
  81. package/blz-rds/commands/datetimes/day.js +10 -0
  82. package/blz-rds/commands/datetimes/hour.js +10 -0
  83. package/blz-rds/commands/datetimes/millisecond.js +10 -0
  84. package/blz-rds/commands/datetimes/minute.js +10 -0
  85. package/blz-rds/commands/datetimes/month-text.js +10 -0
  86. package/blz-rds/commands/datetimes/month.js +10 -0
  87. package/blz-rds/commands/datetimes/now.js +9 -0
  88. package/blz-rds/commands/datetimes/second.js +10 -0
  89. package/blz-rds/commands/datetimes/subtract-days.js +11 -0
  90. package/blz-rds/commands/datetimes/subtract-hours.js +11 -0
  91. package/blz-rds/commands/datetimes/subtract-milliseconds.js +11 -0
  92. package/blz-rds/commands/datetimes/subtract-minutes.js +11 -0
  93. package/blz-rds/commands/datetimes/subtract-seconds.js +11 -0
  94. package/blz-rds/commands/datetimes/time-diff.js +11 -0
  95. package/blz-rds/commands/datetimes/time.js +13 -0
  96. package/blz-rds/commands/datetimes/today.js +9 -0
  97. package/blz-rds/commands/datetimes/week-day-text.js +10 -0
  98. package/blz-rds/commands/datetimes/week-day.js +10 -0
  99. package/blz-rds/commands/datetimes/week.js +10 -0
  100. package/blz-rds/commands/datetimes/year.js +10 -0
  101. package/blz-rds/commands/math/abs.js +10 -0
  102. package/blz-rds/commands/math/acos.js +10 -0
  103. package/blz-rds/commands/math/asin.js +10 -0
  104. package/blz-rds/commands/math/atan.js +10 -0
  105. package/blz-rds/commands/math/atan2.js +11 -0
  106. package/blz-rds/commands/math/ceil.js +10 -0
  107. package/blz-rds/commands/math/cos.js +10 -0
  108. package/blz-rds/commands/math/cosh.js +10 -0
  109. package/blz-rds/commands/math/exp.js +10 -0
  110. package/blz-rds/commands/math/floor.js +10 -0
  111. package/blz-rds/commands/math/log.js +18 -0
  112. package/blz-rds/commands/math/log10.js +10 -0
  113. package/blz-rds/commands/math/pow.js +11 -0
  114. package/blz-rds/commands/math/random.js +9 -0
  115. package/blz-rds/commands/math/round.js +18 -0
  116. package/blz-rds/commands/math/sign.js +10 -0
  117. package/blz-rds/commands/math/sin.js +10 -0
  118. package/blz-rds/commands/math/sinh.js +10 -0
  119. package/blz-rds/commands/math/sqrt.js +10 -0
  120. package/blz-rds/commands/math/tan.js +10 -0
  121. package/blz-rds/commands/math/tanh.js +10 -0
  122. package/blz-rds/commands/math/trunc.js +18 -0
  123. package/blz-rds/commands/strings/concat.js +20 -0
  124. package/blz-rds/commands/strings/contains.js +12 -0
  125. package/blz-rds/commands/strings/ends-with.js +12 -0
  126. package/blz-rds/commands/strings/index-of.js +11 -0
  127. package/blz-rds/commands/strings/is-null-or-empty.js +11 -0
  128. package/blz-rds/commands/strings/is-null-or-white-space.js +11 -0
  129. package/blz-rds/commands/strings/join.js +22 -0
  130. package/blz-rds/commands/strings/last-index-of.js +11 -0
  131. package/blz-rds/commands/strings/length.js +10 -0
  132. package/blz-rds/commands/strings/pad-left.js +20 -0
  133. package/blz-rds/commands/strings/pad-right.js +20 -0
  134. package/blz-rds/commands/strings/replace.js +12 -0
  135. package/blz-rds/commands/strings/starts-with.js +12 -0
  136. package/blz-rds/commands/strings/substring.js +12 -0
  137. package/blz-rds/commands/strings/to-lower.js +10 -0
  138. package/blz-rds/commands/strings/to-upper.js +10 -0
  139. package/blz-rds/commands/strings/trim-end.js +10 -0
  140. package/blz-rds/commands/strings/trim-start.js +10 -0
  141. package/blz-rds/commands/strings/trim.js +10 -0
  142. package/blz-rds/index.js +744 -0
  143. package/blz-rds-mysql/base.js +857 -0
  144. package/blz-rds-mysql/connection-manager.js +129 -0
  145. package/blz-rds-mysql/execute-bulk-insert.js +35 -0
  146. package/blz-rds-mysql/execute-bulk-merge.js +45 -0
  147. package/blz-rds-mysql/execute-non-query.js +34 -0
  148. package/blz-rds-mysql/execute-query.js +50 -0
  149. package/blz-rds-mysql/index.js +41 -0
  150. package/blz-rds-mysql/stored-procedure.js +207 -0
  151. package/blz-rds-mysql/syntaxis.json +114 -0
  152. package/blz-rds-mysqlx/base.js +846 -0
  153. package/blz-rds-mysqlx/connection-manager.js +141 -0
  154. package/blz-rds-mysqlx/execute-bulk-insert.js +35 -0
  155. package/blz-rds-mysqlx/execute-bulk-merge.js +45 -0
  156. package/blz-rds-mysqlx/execute-non-query.js +29 -0
  157. package/blz-rds-mysqlx/execute-query.js +39 -0
  158. package/blz-rds-mysqlx/index.js +41 -0
  159. package/blz-rds-mysqlx/stored-procedure.js +179 -0
  160. package/blz-rds-mysqlx/syntaxis.json +105 -0
  161. package/blz-rds-oracle/index.js +540 -0
  162. package/blz-rds-oracle/syntaxis.json +112 -0
  163. package/blz-rds-postgres/base.js +861 -0
  164. package/blz-rds-postgres/connection-manager.js +225 -0
  165. package/blz-rds-postgres/execute-bulk-insert.js +81 -0
  166. package/blz-rds-postgres/execute-bulk-merge.js +93 -0
  167. package/blz-rds-postgres/execute-non-query.js +23 -0
  168. package/blz-rds-postgres/execute-query.js +37 -0
  169. package/blz-rds-postgres/index.js +41 -0
  170. package/blz-rds-postgres/result-set.js +51 -0
  171. package/blz-rds-postgres/stored-procedure.js +116 -0
  172. package/blz-rds-postgres/syntaxis.json +114 -0
  173. package/blz-redis/index.js +217 -0
  174. package/blz-redis/lib/redisCache.js +265 -0
  175. package/blz-regex/index.js +25 -0
  176. package/blz-security/.eslintrc.js +15 -0
  177. package/blz-security/__test__/AuthorizationKpn.yaml +1043 -0
  178. package/blz-security/__test__/FinancingSetting.yaml +177 -0
  179. package/blz-security/__test__/KpnConfigPortal.yaml +330 -0
  180. package/blz-security/__test__/OrderManagement.yaml +5190 -0
  181. package/blz-security/__test__/Security.yaml +128 -0
  182. package/blz-security/__test__/autorization.test.js +105 -0
  183. package/blz-security/__test__/orderManagement.test.js +26 -0
  184. package/blz-security/__test__/secureUrl.test.js +79 -0
  185. package/blz-security/__test__/solveMergeRule.test.js +109 -0
  186. package/blz-security/__test__/sqlInjectionGuard.test.js +203 -0
  187. package/blz-security/__test__/xssGuard.test.js +204 -0
  188. package/blz-security/authorizationService.js +536 -0
  189. package/blz-security/config/global.js +8 -0
  190. package/blz-security/config/welcome +8 -0
  191. package/blz-security/doc/README.md +75 -0
  192. package/blz-security/filescanner/index.js +46 -0
  193. package/blz-security/helpers/consts.js +229 -0
  194. package/blz-security/helpers/utils.js +267 -0
  195. package/blz-security/implementations/cache.js +90 -0
  196. package/blz-security/implementations/oidc.js +404 -0
  197. package/blz-security/implementations/pkceCacheStore.js +23 -0
  198. package/blz-security/implementations/saml.js +10 -0
  199. package/blz-security/implementations/uma.js +63 -0
  200. package/blz-security/implementations/webAuthn.js +9 -0
  201. package/blz-security/implementations/wstg.js +72 -0
  202. package/blz-security/index.js +77 -0
  203. package/blz-security/lab/index.js +27 -0
  204. package/blz-security/middleware/HapiServerAzureAd.js +641 -0
  205. package/blz-security/middleware/HapiServerKeycloak.js +840 -0
  206. package/blz-security/middleware/HapiServerSimToken.js +247 -0
  207. package/blz-security/middleware/hapi.js +515 -0
  208. package/blz-security/middleware/hapiServer.js +974 -0
  209. package/blz-security/navigationMemoryRepository.js +15 -0
  210. package/blz-security/navigationMongoDbRepository.js +73 -0
  211. package/blz-security/secureUrlService.js +47 -0
  212. package/blz-security/securityService.js +409 -0
  213. package/blz-security/sqlInjectionGuard.js +162 -0
  214. package/blz-security/templates/forbidden.html +0 -0
  215. package/blz-security/templates/session-iframe-azure-ad.html +7 -0
  216. package/blz-security/templates/session-iframe.html +73 -0
  217. package/blz-security/templates/unauthorized.html +1 -0
  218. package/blz-security/xssGuard.js +87 -0
  219. package/blz-strings/index.js +167 -0
  220. package/blz-uuid/index.js +7 -0
  221. package/blz-yaml/index.js +19 -0
  222. package/index.js +84 -0
  223. package/package.json +97 -0
  224. package/process-managers/index.js +422 -0
@@ -0,0 +1,861 @@
1
+ const toString = Object.prototype.toString;
2
+ const dayjs = require("dayjs");
3
+ const utc = require('dayjs/plugin/utc')
4
+ const _ = require('underscore');
5
+ const scalarProperty = _.property(['scalaroutput']);
6
+ const Fs = require('fs-extra');
7
+ const pgsql_ast_parser = require('pgsql-ast-parser');
8
+ dayjs.extend(utc);
9
+ module.exports = {
10
+ isArray: function (value) {
11
+ return _.isArray(value)
12
+ },
13
+ first: function (array) {
14
+ return _.first(array);
15
+ },
16
+ isEmpty: function (value) {
17
+ return _.isEmpty(value);
18
+ },
19
+ scalarOutput: function (rs) {
20
+ return scalarProperty(_.first(rs.rows));
21
+ },
22
+ convertValueToDb: function (value) {
23
+ if (_.isUndefined(value)) {
24
+ return null;
25
+ }
26
+ if (_.isNull(value)) {
27
+ return null;
28
+ }
29
+ if (value === true) {
30
+ return 1;
31
+ }
32
+ if (value === false) {
33
+ return 0;
34
+ }
35
+ let datetime = this.tryDatetime(value);
36
+ if (datetime) {
37
+ if (dayjs(datetime).utc().format("YYYY-MM-DD") === "1970-01-01") {
38
+ return dayjs(datetime).utc().format("HH:mm:ss");
39
+ }
40
+ return datetime;
41
+ }
42
+ return value;
43
+ },
44
+ replace: function (string, search, replace) {
45
+ return string.split(search).join(replace)
46
+ },
47
+ error: function (code, data, innerError = new Error("UndefinedError")) {
48
+ let err = new Error(`${code} ${innerError.message}`);
49
+ err.code = code;
50
+ err.data = data;
51
+ err.innerError = innerError;
52
+ return err;
53
+ },
54
+ convertSqlAndParameters: function (sql, rdsParameters, options) {
55
+ let query = sql;
56
+ for (let i = rdsParameters.length; i > 0; i--) {
57
+ let { name } = rdsParameters[i - 1];
58
+ query = this.replace(query, `:${name}`, `$${i}`);
59
+ }
60
+ const parameters = []
61
+ for (let i = 0, length = rdsParameters.length; length > i; i++) {
62
+ let { name, value } = rdsParameters[i];
63
+ const parameter = this.convertValueToDb(value);
64
+ parameters.push(parameter)
65
+
66
+ }
67
+ // let parameters = rdsParameters.map(({value}) => {
68
+ // return this.convertValueToDb(value);
69
+ // });
70
+ return { query, parameters };
71
+ },
72
+ errorUndefinedArgument: function (argName) {
73
+ return this.error('UndefinedArgument', { argName: argName });
74
+ },
75
+ tryString: function (value) {
76
+ if (toString.call(value) === '[object String]')
77
+ return value;
78
+ else
79
+ return null;
80
+ },
81
+ tryInteger: function (value) {
82
+ if (toString.call(value) === '[object Number]' && value === parseInt(value))
83
+ return value;
84
+ else
85
+ return null;
86
+ },
87
+ tryDecimal: function (value) {
88
+ if (toString.call(value) === '[object Number]')
89
+ return value;
90
+ else
91
+ return null;
92
+ },
93
+ tryBoolean: function (value) {
94
+ if (toString.call(value) === '[object Boolean]')
95
+ return value;
96
+ else
97
+ return null;
98
+ },
99
+ tryDatetime: function (value) {
100
+ let strType = toString.call(value);
101
+ if (strType === '[object Date]') {
102
+ return value;
103
+ }
104
+ else if (strType === '[object String]') {
105
+ let match = /^(\d{4})-(\d{1,2})-(\d{1,2})[T,\s](\d{1,2})\:(\d{1,2})\:(\d{1,2})\.?(\d+)?Z?$/.exec(value);
106
+ if (match) {
107
+ const probableDate = new Date(value);
108
+ if (probableDate.toString() !== 'Invalid Date') {
109
+ return probableDate;
110
+ }
111
+ }
112
+ }
113
+ return null;
114
+ },
115
+ tryDate: function (value) {
116
+ let strType = toString.call(value);
117
+ if (strType === '[object Date]') {
118
+ return new Date(Date.UTC(value.getUTCFullYear(), value.getUTCMonth(), value.getUTCDate(), 0, 0, 0, 0));
119
+ }
120
+ else if (strType === '[object String]') {
121
+ let match = /^(\d{4})-(\d{1,2})-(\d{1,2})$/.exec(value);
122
+ if (match && match[3] !== undefined) {
123
+ return new Date(Date.UTC(Number(match[1]), Number(match[2]) - 1, Number(match[3]), 0, 0, 0, 0));
124
+ } else {
125
+ const probableDate = new Date(value);
126
+ if (probableDate.toString() !== 'Invalid Date') {
127
+ return datetime.toISOString().split('T')[0];
128
+ }
129
+ }
130
+ }
131
+ return null;
132
+ },
133
+ tryTime: function (value) {
134
+ let convertMilliseconds = function (strMilliseconds) {
135
+ if (strMilliseconds) {
136
+ if (strMilliseconds.length < 3)
137
+ strMilliseconds = strMilliseconds.padEnd(3, '0');
138
+ if (strMilliseconds.length > 3)
139
+ strMilliseconds = strMilliseconds.substr(0, 3);
140
+ return Number(strMilliseconds);
141
+ }
142
+ else
143
+ return 0;
144
+ };
145
+ let strType = toString.call(value);
146
+ if (strType === '[object Date]') {
147
+ return new Date(Date.UTC(1970, 0, 1, value.getUTCHours(), value.getUTCMinutes(), value.getUTCSeconds(), value.getUTCMilliseconds()));
148
+ }
149
+ else if (strType === '[object String]') {
150
+ let match = /^(\d{1,2})\:(\d{1,2})\:(\d{1,2})\.?(\d+)?$/.exec(value);
151
+ if (match && match[3] !== undefined) {
152
+ return new Date(Date.UTC(1970, 0, 1, Number(match[1]), Number(match[2]), Number(match[3]), convertMilliseconds(match[4])));
153
+ } else {
154
+ const probableDate = new Date(value);
155
+ if (probableDate.toString() !== 'Invalid Date') {
156
+ return datetime.toISOString().split('T')[1];
157
+ }
158
+ }
159
+ }
160
+ return null;
161
+ },
162
+ tryBinary: function (value) {
163
+ if (Buffer.isBuffer(value)) {
164
+ return value;
165
+ }
166
+ else if (Array.isArray(value)) {
167
+ return Buffer.from(value);
168
+ }
169
+ return null;
170
+ },
171
+ tryObject: function (value) {
172
+ if (toString.call(value) === '[object Object]')
173
+ return value;
174
+ else
175
+ return null;
176
+ },
177
+ tryArray: function (value) {
178
+ if (toString.call(value) === '[object Array]')
179
+ return value;
180
+ else
181
+ return null;
182
+ },
183
+ ensureString: function (value) {
184
+ let string = this.tryString(value);
185
+ if (string !== null)
186
+ return string;
187
+ else
188
+ throw this.error('UnexpectedType', { value: value, expectedType: 'string' });
189
+ },
190
+ ensureInteger: function (value) {
191
+ let integer = this.tryInteger(value);
192
+ if (integer !== null)
193
+ return integer;
194
+ else
195
+ throw this.error('UnexpectedType', { value: value, expectedType: 'integer' });
196
+ },
197
+ ensureDecimal: function (value) {
198
+ let decimal = this.tryDecimal(value);
199
+ if (decimal !== null)
200
+ return decimal;
201
+ else
202
+ throw this.error('UnexpectedType', { value: value, expectedType: 'decimal' });
203
+ },
204
+ ensureBoolean: function (value) {
205
+ let boolean = this.tryBoolean(value);
206
+ if (boolean !== null)
207
+ return boolean;
208
+ else
209
+ throw this.error('UnexpectedType', { value: value, expectedType: 'boolean' });
210
+ },
211
+ ensureDatetime: function (value) {
212
+ let datetime = this.tryDatetime(value);
213
+ if (datetime !== null)
214
+ return datetime;
215
+ else
216
+ throw this.error('UnexpectedType', { value: value, expectedType: 'datetime' });
217
+ },
218
+ ensureDate: function (value) {
219
+ let date = this.tryDate(value);
220
+ if (date !== null)
221
+ return date;
222
+ else
223
+ throw this.error('UnexpectedType', { value: value, expectedType: 'date' });
224
+ },
225
+ ensureTime: function (value) {
226
+ let time = this.tryTime(value);
227
+ if (time !== null)
228
+ return time;
229
+ else
230
+ throw this.error('UnexpectedType', { value: value, expectedType: 'time' });
231
+ },
232
+ ensureBinary: function (value) {
233
+ let binary = this.tryBinary(value);
234
+ if (binary !== null)
235
+ return binary;
236
+ else
237
+ throw this.error('UnexpectedType', { value: value, expectedType: 'binary' });
238
+ },
239
+ ensureObject: function (value) {
240
+ let object = this.tryObject(value);
241
+ if (object !== null)
242
+ return array;
243
+ else
244
+ throw this.error('UnexpectedType', { value: object, expectedType: 'object' });
245
+ },
246
+ ensureArray: function (value) {
247
+ let array = this.tryArray(value);
248
+ if (array !== null)
249
+ return array;
250
+ else
251
+ throw this.error('UnexpectedType', { value: value, expectedType: 'array' });
252
+ },
253
+ convertToString: function (value) {
254
+ if (value === null)
255
+ return null;
256
+ let string = this.tryString(value);
257
+ if (string !== null)
258
+ return string;
259
+ let integer = this.tryInteger(value);
260
+ if (integer !== null)
261
+ return integer.toString();
262
+ let decimal = this.tryDecimal(value);
263
+ if (decimal !== null)
264
+ return decimal.toString();
265
+ let boolean = this.tryBoolean(value);
266
+ if (boolean !== null)
267
+ return boolean ? 'true' : 'false';
268
+ let datetime = this.tryDatetime(value);
269
+ if (datetime !== null)
270
+ return datetime.toJSON();
271
+ throw this.error('InvalidConversion', { value: value, targetType: 'string' });
272
+ },
273
+ convertToInteger: function (value) {
274
+ if (value === null)
275
+ return null;
276
+ let integer = this.tryInteger(value);
277
+ if (integer !== null)
278
+ return integer;
279
+ let string = this.tryString(value);
280
+ if (string !== null && !isNaN(string))
281
+ return Math.round(Number(string));
282
+ let decimal = this.tryDecimal(value);
283
+ if (decimal !== null)
284
+ return Math.round(decimal);
285
+ let boolean = this.tryBoolean(value);
286
+ if (boolean !== null)
287
+ return boolean ? 1 : 0;
288
+ throw this.error('InvalidConversion', { value: value, targetType: 'integer' });
289
+ },
290
+ convertToDecimal: function (value) {
291
+ if (value === null)
292
+ return null;
293
+ let decimal = this.tryDecimal(value);
294
+ if (decimal !== null)
295
+ return decimal;
296
+ let string = this.tryString(value);
297
+ if (string !== null && !isNaN(string))
298
+ return Number(string);
299
+ let integer = this.tryInteger(value);
300
+ if (integer !== null)
301
+ return integer;
302
+ let boolean = this.tryBoolean(value);
303
+ if (boolean !== null)
304
+ return boolean ? 1 : 0;
305
+ throw this.error('InvalidConversion', { value: value, targetType: 'decimal' });
306
+ },
307
+ convertToBoolean: function (value) {
308
+ if (value === null)
309
+ return null;
310
+ let boolean = this.tryBoolean(value);
311
+ if (boolean !== null)
312
+ return boolean;
313
+ let string = this.tryString(value);
314
+ if (string !== null && (string === '1' || string.toUpperCase() === 'T' || string.toUpperCase() === 'TRUE' || string.toUpperCase() === 'Y' || string.toUpperCase() === 'YES'))
315
+ return true;
316
+ if (string !== null && (string === '0' || string.toUpperCase() === 'F' || string.toUpperCase() === 'FALSE' || string.toUpperCase() === 'N' || string.toUpperCase() === 'NO'))
317
+ return false;
318
+ let integer = this.tryInteger(value);
319
+ if (integer !== null && integer === 1)
320
+ return true;
321
+ if (integer !== null && integer === 0)
322
+ return false;
323
+ throw this.error('InvalidConversion', { value: value, targetType: 'boolean' });
324
+ },
325
+ convertToDatetime: function (value) {
326
+ if (value === null)
327
+ return null;
328
+ let datetime = this.tryDatetime(value);
329
+ if (datetime !== null)
330
+ return datetime;
331
+ throw this.error('InvalidConversion', { value: value, targetType: 'datetime' });
332
+ },
333
+ convertToDate: function (value) {
334
+ if (value === null)
335
+ return null;
336
+ let date = this.tryDate(value);
337
+ if (date !== null)
338
+ return date;
339
+ let datetime = this.tryDatetime(value);
340
+ if (datetime !== null)
341
+ return new Date(Date.UTC(datetime.getUTCFullYear(), datetime.getUTCMonth(), datetime.getUTCDate(), 0, 0, 0, 0));
342
+ throw this.error('InvalidConversion', { value: value, targetType: 'date' });
343
+ },
344
+ convertToTime: function (value) {
345
+ if (value === null)
346
+ return null;
347
+ let time = this.tryTime(value);
348
+ if (time !== null)
349
+ return time;
350
+ let datetime = this.tryDatetime(value);
351
+ if (datetime !== null)
352
+ return new Date(Date.UTC(1970, 0, 1, datetime.getUTCHours(), datetime.getUTCMinutes(), datetime.getUTCSeconds(), datetime.getUTCMilliseconds()));
353
+ throw this.error('InvalidConversion', { value: value, targetType: 'time' });
354
+ },
355
+ convertToBinary: function (value) {
356
+ if (value == null)
357
+ return null;
358
+ let binary = this.tryBinary(value);
359
+ if (binary !== null)
360
+ return binary;
361
+ throw this.error('InvalidConversion', { value: value, targetType: 'binary' });
362
+ },
363
+ convertToListOfStrings: function (value) {
364
+ if (Array.isArray(value)) {
365
+ let list = [];
366
+ for (let i = 0; i < value.length; i++) {
367
+ list.push(this.convertToString(value[i]));
368
+ }
369
+ return list;
370
+ }
371
+ else {
372
+ return [this.convertToString(value)];
373
+ }
374
+ },
375
+ convertToListOfIntegers: function (value) {
376
+ if (Array.isArray(value)) {
377
+ let list = [];
378
+ for (let i = 0; i < value.length; i++) {
379
+ list.push(this.convertToInteger(value[i]));
380
+ }
381
+ return list;
382
+ }
383
+ else {
384
+ return [this.convertToInteger(value)];
385
+ }
386
+ },
387
+ convertToListOfDecimals: function (value) {
388
+ if (Array.isArray(value)) {
389
+ let list = [];
390
+ for (let i = 0; i < value.length; i++) {
391
+ list.push(this.convertToDecimal(value[i]));
392
+ }
393
+ return list;
394
+ }
395
+ else {
396
+ return [this.convertToDecimal(value)];
397
+ }
398
+ },
399
+ convertToListOfBooleans: function (value) {
400
+ if (Array.isArray(value)) {
401
+ let list = [];
402
+ for (let i = 0; i < value.length; i++) {
403
+ list.push(this.convertToBoolean(value[i]));
404
+ }
405
+ return list;
406
+ }
407
+ else {
408
+ return [this.convertToBoolean(value)];
409
+ }
410
+ },
411
+ convertToListOfDatetimes: function (value) {
412
+ if (Array.isArray(value)) {
413
+ let list = [];
414
+ for (let i = 0; i < value.length; i++) {
415
+ list.push(this.convertToDatetime(value[i]));
416
+ }
417
+ return list;
418
+ }
419
+ else {
420
+ return [this.convertToDatetime(value)];
421
+ }
422
+ },
423
+ convertToListOfDates: function (value) {
424
+ if (Array.isArray(value)) {
425
+ let list = [];
426
+ for (let i = 0; i < value.length; i++) {
427
+ list.push(this.convertToDate(value[i]));
428
+ }
429
+ return list;
430
+ }
431
+ else {
432
+ return [this.convertToDate(value)];
433
+ }
434
+ },
435
+ convertToListOfTimes: function (value) {
436
+ if (Array.isArray(value)) {
437
+ let list = [];
438
+ for (let i = 0; i < value.length; i++) {
439
+ list.push(this.convertToTime(value[i]));
440
+ }
441
+ return list;
442
+ }
443
+ else {
444
+ return [this.convertToTime(value)];
445
+ }
446
+ },
447
+ convertToListOfBinaries: function (value) {
448
+ if (Array.isArray(value)) {
449
+ let list = [];
450
+ for (let i = 0; i < value.length; i++) {
451
+ list.push(this.convertToBinary(value[i]));
452
+ }
453
+ return list;
454
+ }
455
+ else {
456
+ return [this.convertToBinary(value)];
457
+ }
458
+ },
459
+ httpCall: function (httpRequest) {
460
+ return new Promise(function (response, reject) {
461
+ let options = {
462
+ method: httpRequest.method ? httpRequest.method.toLowerCase() : 'get',
463
+ headers: {},
464
+ body: httpRequest.body
465
+ };
466
+ if (httpRequest.headers)
467
+ for (let i = 0; i < httpRequest.headers.length; i++) {
468
+ let httpHeader = httpRequest.headers[i];
469
+ options.headers[httpHeader.name] = httpHeader.value;
470
+ }
471
+ });
472
+ },
473
+ isNotNull: function (value) {
474
+ return value !== null;
475
+ },
476
+ isNull: function (value) {
477
+ return value === null;
478
+ },
479
+ isNullOrValue: function (value, valueToCompare) {
480
+ return value === null || this.compare(value, valueToCompare) === 0;
481
+ },
482
+ nvl: function (value, replacementValue) {
483
+ return value !== null ? value : replacementValue;
484
+ },
485
+ get: function (objOrTuple, codePathOrIndex) {
486
+ if (objOrTuple === null)
487
+ return null;
488
+ else {
489
+ let obj = this.tryObject(objOrTuple);
490
+ if (obj) {
491
+ let posPoint = codePathOrIndex.indexOf('.');
492
+ if (posPoint === -1)
493
+ return obj[codePathOrIndex] !== undefined ? obj[codePathOrIndex] : null;
494
+ else
495
+ return this.get(obj[codePathOrIndex.substring(0, posPoint)], codePathOrIndex.substring(posPoint + 1, codePathOrIndex.length));
496
+ }
497
+ if (toString.call(objOrTuple) === '[object Error]') {
498
+ let error = objOrTuple;
499
+ let posPoint = codePathOrIndex.indexOf('.');
500
+ if (posPoint === -1)
501
+ return error[codePathOrIndex] !== undefined ? error[codePathOrIndex] : null;
502
+ else
503
+ return this.get(error[codePathOrIndex.substring(0, posPoint)], codePathOrIndex.substring(posPoint + 1, codePathOrIndex.length));
504
+ }
505
+ let tuple = this.tryArray(objOrTuple);
506
+ if (tuple) {
507
+ return tuple[codePathOrIndex];
508
+ }
509
+ return null;
510
+ }
511
+ },
512
+ and: function (value1, value2) {
513
+ return this.ensureBoolean(value1) && this.ensureBoolean(value2);
514
+ },
515
+ or: function (value1, value2) {
516
+ return this.ensureBoolean(value1) || this.ensureBoolean(value2);
517
+ },
518
+ not: function (value) {
519
+ return !this.ensureBoolean(value);
520
+ },
521
+ compare: function (value1, value2) { // TODO: Sort require for objects
522
+ let compareClassic = function (value1, value2) {
523
+ if (value1 === value2)
524
+ return 0;
525
+ else if (value1 < value2)
526
+ return -1;
527
+ else if (value1 > value2)
528
+ return 1;
529
+ else
530
+ return null;
531
+ };
532
+ if (value1 === null && value2 === null)
533
+ return 0;
534
+ if (value1 === null && value2 !== null)
535
+ return -1;
536
+ if (value1 !== null && value2 === null)
537
+ return 1;
538
+ let string1 = this.tryString(value1);
539
+ let string2 = this.tryString(value2);
540
+ if (string1 !== null && string2 !== null)
541
+ return compareClassic(string1, string2);
542
+ let decimal1 = this.tryDecimal(value1);
543
+ let decimal2 = this.tryDecimal(value2);
544
+ if (decimal1 !== null && decimal2 !== null)
545
+ return compareClassic(decimal1, decimal2);
546
+ let boolean1 = this.tryBoolean(value1);
547
+ let boolean2 = this.tryBoolean(value2);
548
+ if (boolean1 !== null && boolean2 !== null)
549
+ return compareClassic(boolean1, boolean2);
550
+ let datetime1 = this.tryDatetime(value1);
551
+ let datetime2 = this.tryDatetime(value2);
552
+ if (datetime1 !== null && datetime2 !== null)
553
+ return compareClassic(datetime1.toJSON(), datetime2.toJSON());
554
+ let date1 = this.tryDate(value1);
555
+ let date2 = this.tryDate(value2);
556
+ if (date1 !== null && date2 !== null)
557
+ return compareClassic(date1.toJSON(), date2.toJSON());
558
+ let time1 = this.tryTime(value1);
559
+ let time2 = this.tryTime(value2);
560
+ if (time1 !== null && time2 !== null)
561
+ return compareClassic(time1.toJSON(), time2.toJSON());
562
+ let array1 = this.tryArray(value1);
563
+ let array2 = this.tryArray(value2);
564
+ if (array1 !== null && array2 !== null) {
565
+ let result;
566
+ result = compareClassic(array1.length, array2.length);
567
+ if (result !== 0)
568
+ return result;
569
+ for (let i = 0; i < array1.length; i++) {
570
+ if (result === 0) {
571
+ let item1 = array1[i];
572
+ let item2 = array2[i];
573
+ result = this.compare(item1, item2);
574
+ }
575
+ }
576
+ return result;
577
+ }
578
+ let binary1 = this.tryBinary(value1);
579
+ let binary2 = this.tryBinary(value2);
580
+ if (binary1 !== null && binary2 !== null)
581
+ return Buffer.compare(binary1, binary2);
582
+ let obj1 = this.tryObject(value1);
583
+ let obj2 = this.tryObject(value2);
584
+ if (obj1 !== null && obj2 !== null) {
585
+ ////Only other option is we have object. JsonStringify is more performant than deepAssert by 42%
586
+ return compareClassic(JSON.stringify(obj1), JSON.stringify(obj2));
587
+ }
588
+ return null;
589
+ },
590
+ equals: function (value1, value2) {
591
+ return this.compare(value1, value2) === 0;
592
+ },
593
+ notEquals: function (value1, value2) {
594
+ return this.compare(value1, value2) !== 0;
595
+ },
596
+ greater: function (value1, value2) {
597
+ return this.compare(value1, value2) > 0;
598
+ },
599
+ less: function (value1, value2) {
600
+ return this.compare(value1, value2) < 0;
601
+ },
602
+ greaterOrEqual: function (value1, value2) {
603
+ return this.compare(value1, value2) >= 0;
604
+ },
605
+ lessOrEqual: function (value1, value2) {
606
+ return this.compare(value1, value2) <= 0;
607
+ },
608
+ concat: function (value1, value2) {
609
+ if (value1 === null)
610
+ return value2;
611
+ if (value2 === null)
612
+ return value1;
613
+ let string1 = this.ensureString(value1);
614
+ let string2 = this.ensureString(value2);
615
+ return string1 + string2;
616
+ },
617
+ add: function (value1, value2) {
618
+ if (value1 === null)
619
+ return value2;
620
+ if (value2 === null)
621
+ return value1;
622
+ let integer1 = this.tryInteger(value1);
623
+ let integer2 = this.tryInteger(value2);
624
+ if (integer1 !== null && integer2 !== null)
625
+ return integer1 + integer2;
626
+ let decimal1 = this.ensureDecimal(value1);
627
+ let decimal2 = this.ensureDecimal(value2);
628
+ return decimal1 + decimal2;
629
+ },
630
+ subtract: function (value1, value2) {
631
+ if (value1 === null)
632
+ return null;
633
+ if (value2 === null)
634
+ return value1;
635
+ let integer1 = this.tryInteger(value1);
636
+ let integer2 = this.tryInteger(value2);
637
+ if (integer1 !== null && integer2 !== null)
638
+ return integer1 - integer2;
639
+ let decimal1 = this.ensureDecimal(value1);
640
+ let decimal2 = this.ensureDecimal(value2);
641
+ return decimal1 - decimal2;
642
+ },
643
+ multiply: function (value1, value2) {
644
+ if (value1 === null)
645
+ return null;
646
+ if (value2 === null)
647
+ return null;
648
+ let integer1 = this.tryInteger(value1);
649
+ let integer2 = this.tryInteger(value2);
650
+ if (integer1 !== null && integer2 !== null)
651
+ return integer1 * integer2;
652
+ let decimal1 = this.ensureDecimal(value1);
653
+ let decimal2 = this.ensureDecimal(value2);
654
+ return decimal1 * decimal2;
655
+ },
656
+ divide: function (value1, value2) {
657
+ if (value1 === null)
658
+ return null;
659
+ let decimal1 = this.ensureDecimal(value1);
660
+ let decimal2 = this.ensureDecimal(value2);
661
+ if (decimal2 === 0)
662
+ throw this.error('DivisionByZero');
663
+ return decimal1 / decimal2;
664
+ },
665
+ remainder: function (value1, value2) {
666
+ if (value1 === null)
667
+ return null;
668
+ let decimal1 = this.ensureDecimal(value1);
669
+ let decimal2 = this.ensureDecimal(value2);
670
+ if (decimal2 === 0)
671
+ throw this.error('DivisionByZero');
672
+ return decimal1 % decimal2;
673
+ },
674
+ lessUnary: function (value) {
675
+ if (value === null)
676
+ return null;
677
+ let integer = this.tryInteger(value);
678
+ if (integer !== null)
679
+ return -integer;
680
+ let decimal = this.ensureDecimal(value);
681
+ return -decimal;
682
+ },
683
+ datetimeAdd: function (value1, value2) {
684
+ if (value1 === null)
685
+ return null;
686
+ if (value2 === null)
687
+ return value1;
688
+ let datetime = this.ensureDatetime(value1);
689
+ let milliseconds = this.ensureInteger(value2);
690
+ return new Date(Date.UTC(datetime.getUTCFullYear(), datetime.getUTCMonth(), datetime.getUTCDate(), datetime.getUTCHours(), datetime.getUTCMinutes(), datetime.getUTCSeconds(), datetime.getUTCMilliseconds() + milliseconds));
691
+ },
692
+ datetimeDiff: function (value1, value2) {
693
+ if (value1 === null)
694
+ return null;
695
+ if (value2 === null)
696
+ return value1;
697
+ let datetime1 = this.ensureDatetime(value1);
698
+ let datetime2 = this.ensureDatetime(value2);
699
+ return datetime1 - datetime2;
700
+ },
701
+ dateAdd: function (value1, value2) {
702
+ if (value1 === null)
703
+ return null;
704
+ if (value2 === null)
705
+ return value1;
706
+ let date = this.ensureDate(value1);
707
+ let days = this.ensureInteger(value2);
708
+ return new Date(Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate() + days, 0, 0, 0, 0));
709
+ },
710
+ dateDiff: function (value1, value2) {
711
+ if (value1 === null)
712
+ return null;
713
+ if (value2 === null)
714
+ return value1;
715
+ let date1 = this.ensureDate(value1);
716
+ let date2 = this.ensureDate(value2);
717
+ return (date1 - date2) / 86400000;
718
+ },
719
+ timeAdd: function (value1, value2) {
720
+ if (value1 === null)
721
+ return null;
722
+ if (value2 === null)
723
+ return value1;
724
+ let time = this.ensureTime(value1);
725
+ let milliseconds = this.ensureInteger(value2);
726
+ return new Date(Date.UTC(1970, 0, 1, time.getUTCHours(), time.getUTCMinutes(), time.getUTCSeconds(), time.getUTCMilliseconds() + milliseconds));
727
+ },
728
+ timeDiff: function (value1, value2) {
729
+ if (value1 === null)
730
+ return null;
731
+ if (value2 === null)
732
+ return value1;
733
+ let time1 = this.ensureTime(value1);
734
+ let time2 = this.ensureTime(value2);
735
+ return time1 - time2;
736
+ },
737
+ jsonParse: function (value) {
738
+ if (value === undefined)
739
+ throw this.errorUndefinedArgument('value');
740
+ if (value === null)
741
+ return null;
742
+ let obj = JSON.parse(this.ensureString(value));
743
+ return obj;
744
+ },
745
+ jsonStringify: function (value, spaces) {
746
+ if (value === undefined)
747
+ throw this.errorUndefinedArgument('value');
748
+ if (value === null)
749
+ return null;
750
+ if (spaces === undefined || spaces === null) {
751
+ let stringified = JSON.stringify(value);
752
+ return stringified;
753
+ } else {
754
+ let identSpaces = this.ensureInteger(spaces);
755
+ let stringified = JSON.stringify(value, null, identSpaces);
756
+ return stringified;
757
+ }
758
+ },
759
+ base64Decode: function (value) {
760
+ if (value === undefined)
761
+ throw this.errorUndefinedArgument('value');
762
+ if (value === null)
763
+ return null;
764
+ let ensuredString = this.ensureString(value);
765
+ return Buffer.from(ensuredString, 'base64');
766
+ },
767
+ base64Encode: function (value) {
768
+ if (value === undefined)
769
+ throw this.errorUndefinedArgument('value');
770
+ if (value === null)
771
+ return null;
772
+ let ensuredBinary = this.ensureBinary(value);
773
+ return Buffer.from(ensuredBinary).toString('base64');
774
+ },
775
+ apply: function (obj1, obj2) {
776
+ let result = {};
777
+ if (obj1 === null)
778
+ return null;
779
+ let obj1Ok = this.tryObject(obj1);
780
+ if (obj1Ok === null)
781
+ throw fnError('InvalidObject', { value: obj1 });
782
+ for (let key in obj1Ok) {
783
+ result[key] = obj1Ok[key];
784
+ }
785
+ if (obj2 !== null) {
786
+ let obj2Ok = this.tryObject(obj2);
787
+ if (obj2Ok === null)
788
+ throw fnError('InvalidObject', { value: obj2 });
789
+ for (let key in obj2Ok) {
790
+ result[key] = obj2Ok[key];
791
+ }
792
+ }
793
+ return result;
794
+ },
795
+ stringFormat: function () {
796
+ if (arguments.length === 0) {
797
+ return null;
798
+ }
799
+ else if (arguments.length === 1) {
800
+ return arguments[0];
801
+ }
802
+ else {
803
+ let result = arguments[0];
804
+ for (let i = 1; i < arguments.length; i++) {
805
+ result = result.split('%' + i + '$s').join(arguments[i]);
806
+ }
807
+ return result;
808
+ }
809
+ },
810
+ loadResource: function (name) {
811
+ return Promise.resolve()
812
+ .then(function () {
813
+ if (name.indexOf('.') === -1) {
814
+ return require('../resources/main/' + name + '.json');
815
+ }
816
+ else {
817
+ let nameParts = name.split('.');
818
+ return require('../resources/edges/' + nameParts[0] + '/' + nameParts[1] + '.json');
819
+ }
820
+ });
821
+ },
822
+ existsPath: async function (sourcePath) {
823
+ const fullPath = this.resolvePath(sourcePath)
824
+ return new Promise((resolve) => {
825
+ Fs.access(fullPath, (err) => {
826
+ if (err) {
827
+ resolve(false)
828
+ } else {
829
+ resolve(true)
830
+ }
831
+ })
832
+ })
833
+ },
834
+ resolvePath: function (source) {
835
+ const _source = source.trim()
836
+ if (_source.startsWith('.')) {
837
+ return Path.join(process.cwd(), source)
838
+ }
839
+ if (_source.startsWith('~')) {
840
+ return _source.replace('~', process.env.HOME)
841
+ }
842
+ return source
843
+ },
844
+ readFile: async function (filePath) {
845
+ const fullPath = this.resolvePath(filePath)
846
+ if (!await this.existsPath(fullPath)) {
847
+ return null
848
+ }
849
+ return new Promise((resolve, reject) => {
850
+ Fs.readFile(fullPath, (err, data) => err ? reject(err) : resolve(data.toString('utf8')))
851
+ })
852
+ },
853
+ checkPostgresSQL: function (sql) {
854
+ try {
855
+ const ast = pgsql_ast_parser.parse(sql);
856
+ return { safe: true };
857
+ } catch (err) {
858
+ return { safe: false, reason: `PostgreSQL parser error: ${err.message}` };
859
+ }
860
+ }
861
+ };