@fincity/kirun-js 2.11.0 → 2.12.0
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.
|
@@ -0,0 +1,353 @@
|
|
|
1
|
+
import { HashData } from '../../../../src/engine/function/system/HashData';
|
|
2
|
+
import { FunctionExecutionParameters } from '../../../../src/engine/runtime/FunctionExecutionParameters';
|
|
3
|
+
import { KIRunFunctionRepository, KIRunSchemaRepository } from '../../../../src';
|
|
4
|
+
import { MapUtil } from '../../../../src/engine/util/MapUtil';
|
|
5
|
+
|
|
6
|
+
const hashData = new HashData();
|
|
7
|
+
|
|
8
|
+
describe('HashData', () => {
|
|
9
|
+
test('should hash integer value with default algorithm', async () => {
|
|
10
|
+
const fep = new FunctionExecutionParameters(
|
|
11
|
+
new KIRunFunctionRepository(),
|
|
12
|
+
new KIRunSchemaRepository()
|
|
13
|
+
).setArguments(
|
|
14
|
+
MapUtil.of(HashData.PARAMETER_DATA, 12345)
|
|
15
|
+
);
|
|
16
|
+
|
|
17
|
+
const result = (await hashData.execute(fep))
|
|
18
|
+
.allResults()[0]
|
|
19
|
+
.getResult()
|
|
20
|
+
.get(HashData.EVENT_RESULT_NAME);
|
|
21
|
+
|
|
22
|
+
expect(result).toBe('5994471abb01112afcc18159f6cc74b4f511b99806da59b3caf5a9c173cacfc5');
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
test('should hash double value with default algorithm', async () => {
|
|
26
|
+
const fep = new FunctionExecutionParameters(
|
|
27
|
+
new KIRunFunctionRepository(),
|
|
28
|
+
new KIRunSchemaRepository()
|
|
29
|
+
).setArguments(
|
|
30
|
+
MapUtil.of(HashData.PARAMETER_DATA, 123.45)
|
|
31
|
+
);
|
|
32
|
+
|
|
33
|
+
const result = (await hashData.execute(fep))
|
|
34
|
+
.allResults()[0]
|
|
35
|
+
.getResult()
|
|
36
|
+
.get(HashData.EVENT_RESULT_NAME);
|
|
37
|
+
|
|
38
|
+
expect(result).toBe('4ebc4a141b378980461430980948a55988fbf56f85d084ac33d8a8f61b9fab88');
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
test('should hash simple string with default algorithm', async () => {
|
|
42
|
+
const fep = new FunctionExecutionParameters(
|
|
43
|
+
new KIRunFunctionRepository(),
|
|
44
|
+
new KIRunSchemaRepository()
|
|
45
|
+
).setArguments(
|
|
46
|
+
MapUtil.of(HashData.PARAMETER_DATA, 'test string')
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
const result = (await hashData.execute(fep))
|
|
50
|
+
.allResults()[0]
|
|
51
|
+
.getResult()
|
|
52
|
+
.get(HashData.EVENT_RESULT_NAME);
|
|
53
|
+
|
|
54
|
+
expect(result).toBe('ee68a16fef8bf44a2b86b5614554b4079820f98dea14a67c3b507f59333cd591');
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
test('should hash object with default algorithm', async () => {
|
|
58
|
+
const testObject = { name: 'Kailash', age: 23, city: 'Bengaluru' };
|
|
59
|
+
|
|
60
|
+
const fep = new FunctionExecutionParameters(
|
|
61
|
+
new KIRunFunctionRepository(),
|
|
62
|
+
new KIRunSchemaRepository()
|
|
63
|
+
).setArguments(
|
|
64
|
+
MapUtil.of(HashData.PARAMETER_DATA, testObject)
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
const result = (await hashData.execute(fep))
|
|
68
|
+
.allResults()[0]
|
|
69
|
+
.getResult()
|
|
70
|
+
.get(HashData.EVENT_RESULT_NAME);
|
|
71
|
+
|
|
72
|
+
expect(result).toBe('99ca912ea0516d95398daa6503a1748b1e39a2f82350e8ece10f8f8355570a46');
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
test('should hash object with primitive level true and default algorithm', async () => {
|
|
76
|
+
const testObject = { name: "Kailash", age: 23, city: 'Bengaluru' };
|
|
77
|
+
|
|
78
|
+
const fep = new FunctionExecutionParameters(
|
|
79
|
+
new KIRunFunctionRepository(),
|
|
80
|
+
new KIRunSchemaRepository()
|
|
81
|
+
).setArguments(
|
|
82
|
+
new Map<string, any>([
|
|
83
|
+
[HashData.PARAMETER_DATA, testObject],
|
|
84
|
+
[HashData.PARAMETER_PRIMITIVE_LEVEL, true]
|
|
85
|
+
])
|
|
86
|
+
);
|
|
87
|
+
|
|
88
|
+
const result = (await hashData.execute(fep))
|
|
89
|
+
.allResults()[0]
|
|
90
|
+
.getResult()
|
|
91
|
+
.get(HashData.EVENT_RESULT_NAME);
|
|
92
|
+
|
|
93
|
+
// Expected object with hashed keys and values
|
|
94
|
+
const expectedObject = {
|
|
95
|
+
// "name": "Kailash"
|
|
96
|
+
'6ffe58b85cad95f1e86f1afebc4ffba738ef6c5520811cb5f393f6c0234ffcb0': '587ba9b0b3222f5af3ad1c7495536e2d0356a9881f45059dcf619923e543b7cc',
|
|
97
|
+
// "age": 23
|
|
98
|
+
'78eb1fc4755453033fe186d682406e53543f575fa2b98887eceb8736e7d24567': '535fa30d7e25dd8a49f1536779734ec8286108d115da5045d77f3b4185d8f790',
|
|
99
|
+
// "city": "Bengaluru"
|
|
100
|
+
'b295162ccd7483bcf4f715b22b84d053efb490c5fd1beb6799751456d37c1ad1': 'afd27606e80dfd77bac4f51dabbb2ecf0ac061dd07b0fe58fc1cdde9a70ad3be'
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
expect(JSON.stringify(result)).toEqual(JSON.stringify(expectedObject));
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
test('should hash array with primitive level true', async () => {
|
|
107
|
+
const testArray = ['test', 123, true, null];
|
|
108
|
+
|
|
109
|
+
const fep = new FunctionExecutionParameters(
|
|
110
|
+
new KIRunFunctionRepository(),
|
|
111
|
+
new KIRunSchemaRepository()
|
|
112
|
+
).setArguments(
|
|
113
|
+
new Map<string, any>([
|
|
114
|
+
[HashData.PARAMETER_DATA, testArray],
|
|
115
|
+
[HashData.PARAMETER_PRIMITIVE_LEVEL, true]
|
|
116
|
+
])
|
|
117
|
+
);
|
|
118
|
+
|
|
119
|
+
const result = (await hashData.execute(fep))
|
|
120
|
+
.allResults()[0]
|
|
121
|
+
.getResult()
|
|
122
|
+
.get(HashData.EVENT_RESULT_NAME);
|
|
123
|
+
|
|
124
|
+
expect(JSON.stringify(result)).toEqual(JSON.stringify([
|
|
125
|
+
'4d967a30111bf29f0eba01c448b375c1629b2fed01cdfcc3aed91f1b57d5dd5e',
|
|
126
|
+
'a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3',
|
|
127
|
+
'b5bea41b6c623f7c09f1bf24dcae58ebab3c0cdd90ad966bc43a45b44867e12b',
|
|
128
|
+
'null'
|
|
129
|
+
]));
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
test('should hash with different algorithm (md5)', async () => {
|
|
133
|
+
const fep = new FunctionExecutionParameters(
|
|
134
|
+
new KIRunFunctionRepository(),
|
|
135
|
+
new KIRunSchemaRepository()
|
|
136
|
+
).setArguments(
|
|
137
|
+
new Map<string, any>([
|
|
138
|
+
[HashData.PARAMETER_DATA, 'test string'],
|
|
139
|
+
[HashData.PARAMETER_ALGORITHM, 'md5']
|
|
140
|
+
])
|
|
141
|
+
);
|
|
142
|
+
|
|
143
|
+
const result = (await hashData.execute(fep))
|
|
144
|
+
.allResults()[0]
|
|
145
|
+
.getResult()
|
|
146
|
+
.get(HashData.EVENT_RESULT_NAME);
|
|
147
|
+
|
|
148
|
+
expect(result).toBe('520a193597c1170fc7f00c6e77df571f');
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
test('should handle null and undefined values', async () => {
|
|
152
|
+
const fepNull = new FunctionExecutionParameters(
|
|
153
|
+
new KIRunFunctionRepository(),
|
|
154
|
+
new KIRunSchemaRepository()
|
|
155
|
+
).setArguments(
|
|
156
|
+
MapUtil.of(HashData.PARAMETER_DATA, null)
|
|
157
|
+
);
|
|
158
|
+
|
|
159
|
+
const resultNull = (await hashData.execute(fepNull))
|
|
160
|
+
.allResults()[0]
|
|
161
|
+
.getResult()
|
|
162
|
+
.get(HashData.EVENT_RESULT_NAME);
|
|
163
|
+
|
|
164
|
+
expect(resultNull).toBe("null");
|
|
165
|
+
|
|
166
|
+
const fepUndefined = new FunctionExecutionParameters(
|
|
167
|
+
new KIRunFunctionRepository(),
|
|
168
|
+
new KIRunSchemaRepository()
|
|
169
|
+
).setArguments(
|
|
170
|
+
MapUtil.of(HashData.PARAMETER_DATA, undefined)
|
|
171
|
+
);
|
|
172
|
+
|
|
173
|
+
const resultUndefined = (await hashData.execute(fepUndefined))
|
|
174
|
+
.allResults()[0]
|
|
175
|
+
.getResult()
|
|
176
|
+
.get(HashData.EVENT_RESULT_NAME);
|
|
177
|
+
|
|
178
|
+
expect(resultUndefined).toBe("null");
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
test('should hash nested objects with primitive level', async () => {
|
|
182
|
+
const nestedObject = {
|
|
183
|
+
user: {
|
|
184
|
+
name: 'John',
|
|
185
|
+
contacts: {
|
|
186
|
+
email: 'john@example.com',
|
|
187
|
+
phone: '1234567890'
|
|
188
|
+
}
|
|
189
|
+
},
|
|
190
|
+
active: true
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
const fep = new FunctionExecutionParameters(
|
|
194
|
+
new KIRunFunctionRepository(),
|
|
195
|
+
new KIRunSchemaRepository()
|
|
196
|
+
).setArguments(
|
|
197
|
+
new Map<string, any>([
|
|
198
|
+
[HashData.PARAMETER_DATA, nestedObject],
|
|
199
|
+
[HashData.PARAMETER_PRIMITIVE_LEVEL, true]
|
|
200
|
+
])
|
|
201
|
+
);
|
|
202
|
+
|
|
203
|
+
const result = (await hashData.execute(fep))
|
|
204
|
+
.allResults()[0]
|
|
205
|
+
.getResult()
|
|
206
|
+
.get(HashData.EVENT_RESULT_NAME);
|
|
207
|
+
|
|
208
|
+
const expectedObject = {
|
|
209
|
+
"3190d261d186aeead3a8deec202737c7775af5c8d455a9e5ba958c48b5fd3f59": {
|
|
210
|
+
"6ffe58b85cad95f1e86f1afebc4ffba738ef6c5520811cb5f393f6c0234ffcb0": "152f3ca566617488c2450ba9a88bdb8ef1593c860c496c39386d68cfe351df3f",
|
|
211
|
+
"ffc121712a18c1513ded336647a0ccf7d369ba1927ac68c5d3272ac54535d633": {
|
|
212
|
+
"d55b99efba68c4d57a901b891ddbaa9d1d8ad6c52dd62390b00204efc3170441": "aaa07cfdec182c9ab13a25f43b0a84356bfe3ef199658fd4e15f96e5af54b665",
|
|
213
|
+
"d5511d58ae89a5136544686b7e471128789e3309c2ec9b520c217067f6abb75d": "9f191b3167af0649edeb8888946bee1c523b87aff6219fa7765ac731bca74fb1"
|
|
214
|
+
}
|
|
215
|
+
},
|
|
216
|
+
"db0fb2bf5ebc424454c3e11b5ee8bfb43af24a52e561aa49e94143831dc6fd93": "b5bea41b6c623f7c09f1bf24dcae58ebab3c0cdd90ad966bc43a45b44867e12b"
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
expect(JSON.stringify(result)).toEqual(JSON.stringify(expectedObject));
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
test('Hash Meta Audience - Primitive Level - true', async () => {
|
|
223
|
+
const hashData = new HashData();
|
|
224
|
+
|
|
225
|
+
const nestedObject = [
|
|
226
|
+
[
|
|
227
|
+
"kunal",
|
|
228
|
+
"jha",
|
|
229
|
+
NaN,
|
|
230
|
+
9922552168
|
|
231
|
+
],
|
|
232
|
+
[
|
|
233
|
+
"Saleem",
|
|
234
|
+
NaN,
|
|
235
|
+
"gssaleem22@gmail.com",
|
|
236
|
+
9845367369
|
|
237
|
+
],
|
|
238
|
+
[
|
|
239
|
+
"Sunil",
|
|
240
|
+
"Gayathri",
|
|
241
|
+
NaN,
|
|
242
|
+
9900511830
|
|
243
|
+
],
|
|
244
|
+
[
|
|
245
|
+
"Hemant",
|
|
246
|
+
"Hemant",
|
|
247
|
+
NaN,
|
|
248
|
+
9619006766
|
|
249
|
+
],
|
|
250
|
+
[
|
|
251
|
+
"Hazarath",
|
|
252
|
+
"Sarabu",
|
|
253
|
+
"hazarath@yahoo.com",
|
|
254
|
+
9848514909
|
|
255
|
+
]
|
|
256
|
+
];
|
|
257
|
+
|
|
258
|
+
const fep = new FunctionExecutionParameters(
|
|
259
|
+
new KIRunFunctionRepository(),
|
|
260
|
+
new KIRunSchemaRepository()
|
|
261
|
+
).setArguments(
|
|
262
|
+
new Map<string, any>([
|
|
263
|
+
[HashData.PARAMETER_DATA, nestedObject],
|
|
264
|
+
[HashData.PARAMETER_ALGORITHM, 'sha256'],
|
|
265
|
+
[HashData.PARAMETER_PRIMITIVE_LEVEL, true]
|
|
266
|
+
])
|
|
267
|
+
);
|
|
268
|
+
|
|
269
|
+
const result = (await hashData.execute(fep))
|
|
270
|
+
.allResults()[0]
|
|
271
|
+
.getResult()
|
|
272
|
+
.get(HashData.EVENT_RESULT_NAME);
|
|
273
|
+
|
|
274
|
+
const expectedObject = [
|
|
275
|
+
[
|
|
276
|
+
'5c5ec4214a4f90fc81d20698f3b58971a65f99be8f807167c0dc357dbfca6d64',
|
|
277
|
+
'1a14fb90285bbdc811ec90c31535f8cbfe7a190cdea4388ffe5bfb5149373c85',
|
|
278
|
+
'74234e98afe7498fb5daf1f36ac2d78acc339464f950703b8c019892f982b90b',
|
|
279
|
+
'c73e1c882b48b71df3346a638b86a361d4ad58bd5119bed829c193192ea0bd12'
|
|
280
|
+
],
|
|
281
|
+
[
|
|
282
|
+
'b0a368eae50806c434c5dc52ea1743c033e20e28b37e95da3f515d426c412f24',
|
|
283
|
+
"74234e98afe7498fb5daf1f36ac2d78acc339464f950703b8c019892f982b90b",
|
|
284
|
+
"6aa7533f4df03ce2cdb94fbd3af0df01d3ad798997224b31bbd972c8f4110d98",
|
|
285
|
+
"55dd6fc4584ff7971599839a58ea554e0ceabf8ea54108c2afe96b9600ad22de"
|
|
286
|
+
],
|
|
287
|
+
[
|
|
288
|
+
"d4ac1f574136969f501b11f8312613addf81a738bd32d318ff57b14c57074c46",
|
|
289
|
+
"d77c692372de396251e5a30d31f8f0d1e729fc2d2ddf38286d4f9133aa7849de",
|
|
290
|
+
"74234e98afe7498fb5daf1f36ac2d78acc339464f950703b8c019892f982b90b",
|
|
291
|
+
"859aee372e6161388c67a2ebe39ea30da2f09d6e4dca715be79109d8c9c9a946"
|
|
292
|
+
],
|
|
293
|
+
[
|
|
294
|
+
"8ff58050b37737f3b0d5b9541c1cb1f67394b41631bfdc927ab216ff580af445",
|
|
295
|
+
"8ff58050b37737f3b0d5b9541c1cb1f67394b41631bfdc927ab216ff580af445",
|
|
296
|
+
"74234e98afe7498fb5daf1f36ac2d78acc339464f950703b8c019892f982b90b",
|
|
297
|
+
"3aaf978a21568f3132e586b5fab896a46930831d8362ae3a630f9b0e1ac44d5a"
|
|
298
|
+
],
|
|
299
|
+
[
|
|
300
|
+
"ff1363632be96e64e74f8f6ce09e899dc93243a0aba24ae8d1e901df266e783b",
|
|
301
|
+
"60124d4663c3581f6d5ab01d3a8652e67c83a0eea14d2eebc617132386a63533",
|
|
302
|
+
"5df5828651d67e079dbaa41176ed84c87e1823a316abd2a55ade9c9045dadb0a",
|
|
303
|
+
"c6701bb10611df1b1ac0f1bc3d008200a81ccb78a2e3e71db8d0e6b6ad6bca83"
|
|
304
|
+
]
|
|
305
|
+
]
|
|
306
|
+
|
|
307
|
+
expect(JSON.stringify(result)).toEqual(JSON.stringify(expectedObject));
|
|
308
|
+
});
|
|
309
|
+
|
|
310
|
+
test('should hash 2D array with primitive level true', async () => {
|
|
311
|
+
const twoDArray = [
|
|
312
|
+
['a', 1, true],
|
|
313
|
+
['b', 2, false],
|
|
314
|
+
['c', 3, null]
|
|
315
|
+
];
|
|
316
|
+
|
|
317
|
+
const fep = new FunctionExecutionParameters(
|
|
318
|
+
new KIRunFunctionRepository(),
|
|
319
|
+
new KIRunSchemaRepository()
|
|
320
|
+
).setArguments(
|
|
321
|
+
new Map<string, any>([
|
|
322
|
+
[HashData.PARAMETER_DATA, twoDArray],
|
|
323
|
+
[HashData.PARAMETER_PRIMITIVE_LEVEL, true]
|
|
324
|
+
])
|
|
325
|
+
);
|
|
326
|
+
|
|
327
|
+
const result = (await hashData.execute(fep))
|
|
328
|
+
.allResults()[0]
|
|
329
|
+
.getResult()
|
|
330
|
+
.get(HashData.EVENT_RESULT_NAME);
|
|
331
|
+
|
|
332
|
+
const expectedArray = [
|
|
333
|
+
[
|
|
334
|
+
'ac8d8342bbb2362d13f0a559a3621bb407011368895164b628a54f7fc33fc43c', // hash of "a"
|
|
335
|
+
'6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b', // hash of 1
|
|
336
|
+
'b5bea41b6c623f7c09f1bf24dcae58ebab3c0cdd90ad966bc43a45b44867e12b' // hash of true
|
|
337
|
+
],
|
|
338
|
+
[
|
|
339
|
+
'c100f95c1913f9c72fc1f4ef0847e1e723ffe0bde0b36e5f36c13f81fe8c26ed', // hash of "b"
|
|
340
|
+
'd4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab35', // hash of 2
|
|
341
|
+
'fcbcf165908dd18a9e49f7ff27810176db8e9f63b4352213741664245224f8aa' // hash of false
|
|
342
|
+
],
|
|
343
|
+
[
|
|
344
|
+
'879923da020d1533f4d8e921ea7bac61e8ba41d3c89d17a4d14e3a89c6780d5d', // hash of "c"
|
|
345
|
+
'4e07408562bedb8b60ce05c1decfe3ad16b72230967de01f640b7e4729b49fce', // hash of 3
|
|
346
|
+
'null' // null value
|
|
347
|
+
]
|
|
348
|
+
];
|
|
349
|
+
|
|
350
|
+
expect(JSON.stringify(result)).toEqual(JSON.stringify(expectedArray));
|
|
351
|
+
});
|
|
352
|
+
|
|
353
|
+
});
|
package/package.json
CHANGED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { createHash } from 'crypto';
|
|
2
|
+
import { AbstractFunction } from '../AbstractFunction';
|
|
3
|
+
import { FunctionSignature } from '../../model/FunctionSignature';
|
|
4
|
+
import { Parameter } from '../../model/Parameter';
|
|
5
|
+
import { Schema } from '../../json/schema/Schema';
|
|
6
|
+
import { Event } from '../../model/Event';
|
|
7
|
+
import { Namespaces } from '../../namespaces/Namespaces';
|
|
8
|
+
import { FunctionExecutionParameters } from '../../runtime/FunctionExecutionParameters';
|
|
9
|
+
import { FunctionOutput } from '../../model/FunctionOutput';
|
|
10
|
+
import { EventResult } from '../../model/EventResult';
|
|
11
|
+
import { MapUtil } from '../../util/MapUtil';
|
|
12
|
+
|
|
13
|
+
export class HashData extends AbstractFunction {
|
|
14
|
+
private static readonly DEFAULT_ALGORITHM = 'sha256';
|
|
15
|
+
public static readonly PARAMETER_DATA = 'data';
|
|
16
|
+
public static readonly PARAMETER_ALGORITHM = 'algorithm';
|
|
17
|
+
public static readonly PARAMETER_PRIMITIVE_LEVEL = 'primitiveLevel';
|
|
18
|
+
public static readonly EVENT_RESULT_NAME = 'result';
|
|
19
|
+
|
|
20
|
+
private readonly signature: FunctionSignature;
|
|
21
|
+
|
|
22
|
+
public constructor() {
|
|
23
|
+
super();
|
|
24
|
+
this.signature = new FunctionSignature('Hash')
|
|
25
|
+
.setNamespace(Namespaces.SYSTEM)
|
|
26
|
+
.setParameters(
|
|
27
|
+
new Map([
|
|
28
|
+
Parameter.ofEntry(
|
|
29
|
+
HashData.PARAMETER_DATA,
|
|
30
|
+
Schema.ofAny(HashData.PARAMETER_DATA)
|
|
31
|
+
),
|
|
32
|
+
Parameter.ofEntry(
|
|
33
|
+
HashData.PARAMETER_ALGORITHM,
|
|
34
|
+
Schema.ofString(HashData.PARAMETER_ALGORITHM)
|
|
35
|
+
.setEnums([
|
|
36
|
+
'sha256',
|
|
37
|
+
'sha384',
|
|
38
|
+
'sha512',
|
|
39
|
+
'md5',
|
|
40
|
+
'md2',
|
|
41
|
+
'md4',
|
|
42
|
+
'sha1'])
|
|
43
|
+
.setDefaultValue(HashData.DEFAULT_ALGORITHM)
|
|
44
|
+
),
|
|
45
|
+
Parameter.ofEntry(
|
|
46
|
+
HashData.PARAMETER_PRIMITIVE_LEVEL,
|
|
47
|
+
Schema.ofBoolean(HashData.PARAMETER_PRIMITIVE_LEVEL)
|
|
48
|
+
.setDefaultValue(false)
|
|
49
|
+
)
|
|
50
|
+
])
|
|
51
|
+
)
|
|
52
|
+
.setEvents(
|
|
53
|
+
new Map([
|
|
54
|
+
[
|
|
55
|
+
Event.OUTPUT,
|
|
56
|
+
new Event(
|
|
57
|
+
Event.OUTPUT,
|
|
58
|
+
MapUtil.of(
|
|
59
|
+
HashData.EVENT_RESULT_NAME,
|
|
60
|
+
Schema.ofString(HashData.EVENT_RESULT_NAME)
|
|
61
|
+
)
|
|
62
|
+
)
|
|
63
|
+
]
|
|
64
|
+
])
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
public getSignature(): FunctionSignature {
|
|
69
|
+
return this.signature;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
protected async internalExecute(context: FunctionExecutionParameters): Promise<FunctionOutput> {
|
|
73
|
+
const data = context.getArguments()?.get(HashData.PARAMETER_DATA);
|
|
74
|
+
const algorithm = context.getArguments()?.get(HashData.PARAMETER_ALGORITHM) || HashData.DEFAULT_ALGORITHM;
|
|
75
|
+
const primitiveLevel = context.getArguments()?.get(HashData.PARAMETER_PRIMITIVE_LEVEL);
|
|
76
|
+
|
|
77
|
+
const result = primitiveLevel ?
|
|
78
|
+
this.processPrimitiveLevelData(data, algorithm) :
|
|
79
|
+
(data === null || data === undefined) ?
|
|
80
|
+
"null" :
|
|
81
|
+
createHash(algorithm).update(JSON.stringify(data)).digest('hex');
|
|
82
|
+
|
|
83
|
+
return new FunctionOutput([
|
|
84
|
+
EventResult.outputOf(new Map([[HashData.EVENT_RESULT_NAME, result]]))
|
|
85
|
+
]);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
private processPrimitiveLevelData(value: any, algorithm: string): any {
|
|
89
|
+
if (value === null || value === undefined) return "null";
|
|
90
|
+
|
|
91
|
+
if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
|
|
92
|
+
return createHash(algorithm).update(JSON.stringify(value)).digest('hex');
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (Array.isArray(value)) {
|
|
96
|
+
return value.map(item => this.processPrimitiveLevelData(item, algorithm));
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (typeof value === 'object') {
|
|
100
|
+
const result: any = {};
|
|
101
|
+
Object.entries(value).forEach(([key, val]) => {
|
|
102
|
+
result[createHash(algorithm).update(JSON.stringify(key)).digest('hex')] =
|
|
103
|
+
this.processPrimitiveLevelData(val, algorithm);
|
|
104
|
+
});
|
|
105
|
+
return result;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return createHash(algorithm).update(String(value)).digest('hex');
|
|
109
|
+
}
|
|
110
|
+
}
|