@raytio/core 8.1.0 → 9.0.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.
Files changed (79) hide show
  1. package/README.md +460 -1
  2. package/dist/accessApplication/createAA.js +0 -1
  3. package/dist/crypto/decryptKeys.js +0 -2
  4. package/dist/crypto/decryptSharedData.js +7 -4
  5. package/dist/crypto/helpers.d.ts +1 -1
  6. package/dist/general/index.d.ts +1 -0
  7. package/dist/general/index.js +1 -0
  8. package/dist/general/types.d.ts +6 -0
  9. package/dist/general/types.js +13 -0
  10. package/dist/index.d.ts +1 -0
  11. package/dist/index.js +1 -0
  12. package/dist/rules/calculateScore.d.ts +11 -0
  13. package/dist/rules/calculateScore.js +52 -0
  14. package/dist/rules/convertInstanceToRuleInput.d.ts +3 -0
  15. package/dist/rules/convertInstanceToRuleInput.js +140 -0
  16. package/dist/rules/evaluateScoreCondition.d.ts +2 -0
  17. package/dist/rules/evaluateScoreCondition.js +45 -0
  18. package/dist/rules/helpers/addInfiniteThresholdBoundaries.d.ts +8 -0
  19. package/dist/rules/helpers/addInfiniteThresholdBoundaries.js +11 -0
  20. package/dist/rules/helpers/checkTypeofValue.d.ts +3 -0
  21. package/dist/rules/helpers/checkTypeofValue.js +43 -0
  22. package/dist/rules/helpers/getValuesFromPath.d.ts +3 -0
  23. package/dist/rules/helpers/getValuesFromPath.js +50 -0
  24. package/dist/rules/helpers/index.d.ts +5 -0
  25. package/dist/rules/helpers/index.js +17 -0
  26. package/dist/rules/helpers/symbols.d.ts +2 -0
  27. package/dist/rules/helpers/symbols.js +5 -0
  28. package/dist/rules/helpers/thresholds.d.ts +5 -0
  29. package/dist/rules/helpers/thresholds.js +47 -0
  30. package/dist/rules/index.d.ts +6 -0
  31. package/dist/rules/index.js +20 -0
  32. package/dist/rules/operators/bool.d.ts +2 -0
  33. package/dist/rules/operators/bool.js +17 -0
  34. package/dist/rules/operators/date.d.ts +2 -0
  35. package/dist/rules/operators/date.js +91 -0
  36. package/dist/rules/operators/hfield.d.ts +2 -0
  37. package/dist/rules/operators/hfield.js +33 -0
  38. package/dist/rules/operators/hschema.d.ts +2 -0
  39. package/dist/rules/operators/hschema.js +21 -0
  40. package/dist/rules/operators/index.d.ts +3 -0
  41. package/dist/rules/operators/index.js +11 -0
  42. package/dist/rules/operators/number.d.ts +2 -0
  43. package/dist/rules/operators/number.js +41 -0
  44. package/dist/rules/operators/string.d.ts +2 -0
  45. package/dist/rules/operators/string.js +58 -0
  46. package/dist/rules/types/config.d.ts +86 -0
  47. package/dist/rules/types/config.js +2 -0
  48. package/dist/rules/types/dataValueTypes.d.ts +19 -0
  49. package/dist/rules/types/dataValueTypes.js +4 -0
  50. package/dist/rules/types/index.d.ts +3 -0
  51. package/dist/rules/types/index.js +15 -0
  52. package/dist/rules/types/internal.d.ts +20 -0
  53. package/dist/rules/types/internal.js +2 -0
  54. package/dist/{verifications/checkVerifications.d.ts → util/canonicalJsonify.d.ts} +0 -0
  55. package/dist/util/canonicalJsonify.js +48 -0
  56. package/dist/util/handleResponse.js +0 -1
  57. package/dist/util/index.d.ts +1 -0
  58. package/dist/util/index.js +1 -0
  59. package/dist/verifications/cleanInstance.d.ts +9 -0
  60. package/dist/verifications/cleanInstance.js +15 -0
  61. package/dist/verifications/getPOVerification.d.ts +5 -2
  62. package/dist/verifications/getPOVerification.js +3 -4
  63. package/dist/verifications/index.d.ts +2 -1
  64. package/dist/verifications/index.js +2 -1
  65. package/dist/verifications/safeHarbour.d.ts +2 -3
  66. package/dist/verifications/safeHarbour.js +2 -2
  67. package/dist/verifications/verifyCheck/getOwnRealVerifications.d.ts +13 -0
  68. package/dist/verifications/verifyCheck/getOwnRealVerifications.js +63 -0
  69. package/dist/verifications/{getRealVerifications.d.ts → verifyCheck/getSomeoneElsesRealVerifications.d.ts} +6 -2
  70. package/dist/verifications/{getRealVerifications.js → verifyCheck/getSomeoneElsesRealVerifications.js} +26 -16
  71. package/dist/verifications/verifyCheck/index.d.ts +2 -0
  72. package/dist/verifications/verifyCheck/index.js +14 -0
  73. package/dist/verifications/verifyCheck/operations/checkOwnVerification.d.ts +9 -0
  74. package/dist/verifications/verifyCheck/operations/checkOwnVerification.js +31 -0
  75. package/dist/verifications/verifyCheck/operations/checkSomeoneElsesVerifications.d.ts +1 -0
  76. package/dist/verifications/{checkVerifications.js → verifyCheck/operations/checkSomeoneElsesVerifications.js} +4 -4
  77. package/dist/verifications/verifyCheck/operations/index.d.ts +2 -0
  78. package/dist/verifications/verifyCheck/operations/index.js +14 -0
  79. package/package.json +8 -8
package/README.md CHANGED
@@ -10,6 +10,465 @@ Nodejs does not support [fetch](https://developer.mozilla.org/en-US/docs/Web/API
10
10
 
11
11
  If you use the high-level [`@raytio/decrypt-helper`](https://npm.im/@raytio/decrypt-helper) module, you don't need to worry about this.
12
12
 
13
- If you wish to use `@raytio/core` directly, an example of configuring polyfills for nodejs is availble [here](https://gitlab.com/raytio/tools/decrypt-helper/-/blob/master/src/configureEnv.ts)
13
+ If you wish to use `@raytio/core` directly, an example of configuring polyfills for nodejs is availble [here](https://gitlab.com/raytio/tools/decrypt-helper/-/blob/main/src/configureEnv.ts)
14
14
 
15
15
  # API
16
+
17
+ ## Table of contents
18
+
19
+ ### Type aliases
20
+
21
+ - [SafeHarbourObj](#safeharbourobj)
22
+ - [SafeHarbourResult](#safeharbourresult)
23
+
24
+ ### Functions
25
+
26
+ - [calcSafeHarbourScore](#calcsafeharbourscore)
27
+ - [calculateScore](#calculatescore)
28
+ - [cleanInstance](#cleaninstance)
29
+ - [convertInstanceToRuleInput](#convertinstancetoruleinput)
30
+ - [createAA](#createaa)
31
+ - [decryptSharedData](#decryptshareddata)
32
+ - [findSchemaLabel](#findschemalabel)
33
+ - [fromCognitoAttributes](#fromcognitoattributes)
34
+ - [getAADecryptor](#getaadecryptor)
35
+ - [getOwnRealVerifications](#getownrealverifications)
36
+ - [getPOVerification](#getpoverification)
37
+ - [getSomeoneElsesRealVerifications](#getsomeoneelsesrealverifications)
38
+ - [hashPassword](#hashpassword)
39
+ - [isConditionMet](#isconditionmet)
40
+ - [isEncrypted](#isencrypted)
41
+ - [isEncryptedFile](#isencryptedfile)
42
+ - [someEncrypted](#someencrypted)
43
+ - [toCognitoAttributes](#tocognitoattributes)
44
+
45
+ ## Type aliases
46
+
47
+ ### SafeHarbourObj
48
+
49
+ Ƭ **SafeHarbourObj**: `Partial`<`Record`<`SafeHarbourCode`, `string`[]\>\>
50
+
51
+ an object listing the `xId`s for each SafeHarbourCode
52
+
53
+ ___
54
+
55
+ ### SafeHarbourResult
56
+
57
+ Ƭ **SafeHarbourResult**: `Object`
58
+
59
+ the response from [calcSafeHarbourScore](#calcsafeharbourscore)
60
+
61
+ #### Type declaration
62
+
63
+ | Name | Type |
64
+ | :------ | :------ |
65
+ | `flags` | [`SafeHarbourObj`](#safeharbourobj) |
66
+ | `isSafe` | `boolean` |
67
+
68
+ ## Functions
69
+
70
+ ### calcSafeHarbourScore
71
+
72
+ ▸ `Const` **calcSafeHarbourScore**(`data`): `Promise`<[`SafeHarbourResult`](#safeharbourresult)\>
73
+
74
+ The Safe Harbour Score indidicates whether a person's identity has been verified
75
+ to the extent requried for Safe Harbour Compliance. This requires multiple verifications
76
+ from different sources. For information, refer to the
77
+ [Raytio Documentation](https://dev-docs.rayt.io/docs/features/pep-checks).
78
+
79
+ #### Parameters
80
+
81
+ | Name | Type |
82
+ | :------ | :------ |
83
+ | `data` | `Object` |
84
+ | `data.person` | `ProfileObject`<`Json`\> |
85
+ | `data.profileObjects` | `ProfileObject`<`Json`\>[] |
86
+ | `data.realVers` | `RealVer`[] |
87
+ | `data.getSchema` | (`schemaName`: `string`) => `Promise`<`Schema`\> |
88
+
89
+ #### Returns
90
+
91
+ `Promise`<[`SafeHarbourResult`](#safeharbourresult)\>
92
+
93
+ ___
94
+
95
+ ### calculateScore
96
+
97
+ ▸ **calculateScore**(`ruleConfig`, `ruleInput`): `ScoreResult`
98
+
99
+ the main function to calculate a score and category.
100
+ Might throw an error.
101
+
102
+ #### Parameters
103
+
104
+ | Name | Type |
105
+ | :------ | :------ |
106
+ | `ruleConfig` | `ScoreConfig` |
107
+ | `ruleInput` | `RuleData` |
108
+
109
+ #### Returns
110
+
111
+ `ScoreResult`
112
+
113
+ ___
114
+
115
+ ### cleanInstance
116
+
117
+ ▸ **cleanInstance**(`instance`): `Instance`
118
+
119
+ The API response from share/v2/access_application/instance/:iId
120
+ returns a complicated hashed_n_id format, so you need to clean up
121
+ the API response using this function as soon as possible.
122
+
123
+ We relace `hashed_n_id`s with a string `HASHED::{NId}::{AId}`
124
+
125
+ #### Parameters
126
+
127
+ | Name | Type |
128
+ | :------ | :------ |
129
+ | `instance` | `Instance` |
130
+
131
+ #### Returns
132
+
133
+ `Instance`
134
+
135
+ ___
136
+
137
+ ### convertInstanceToRuleInput
138
+
139
+ ▸ `Const` **convertInstanceToRuleInput**(`instance`, `realVers`, `getSchema`): `Promise`<`RuleData`\>
140
+
141
+ #### Parameters
142
+
143
+ | Name | Type |
144
+ | :------ | :------ |
145
+ | `instance` | `Instance` |
146
+ | `realVers` | `RealVer`[] |
147
+ | `getSchema` | (`schemaName`: `string`) => `Promise`<`Schema`\> |
148
+
149
+ #### Returns
150
+
151
+ `Promise`<`RuleData`\>
152
+
153
+ ___
154
+
155
+ ### createAA
156
+
157
+ ▸ **createAA**(`__namedParameters`): `Promise`<`AA`\>
158
+
159
+ Creates an Access Application and associated public+private keys.
160
+
161
+ The user must be part of an organization, and you need to include the `orgId`.
162
+
163
+ You must also supply an apiToken and an instance of the maxcryptor for that user,
164
+ as well as the `userDoc` data which is stored in the user's cognito attributes.
165
+
166
+ #### Parameters
167
+
168
+ | Name | Type |
169
+ | :------ | :------ |
170
+ | `__namedParameters` | `Object` |
171
+ | `__namedParameters.apiToken` | `string` |
172
+ | `__namedParameters.apiUrl` | `string` |
173
+ | `__namedParameters.application` | `Omit`<`AA`, ``"a_id"``\> |
174
+ | `__namedParameters.maxcryptor` | `DataEncryptorI` |
175
+ | `__namedParameters.userDoc` | `UserDoc` |
176
+
177
+ #### Returns
178
+
179
+ `Promise`<`AA`\>
180
+
181
+ ___
182
+
183
+ ### decryptSharedData
184
+
185
+ ▸ `Const` **decryptSharedData**(`__namedParameters`): `Promise`<`Object`\>
186
+
187
+ Decrypts any encrypted properties included in the supplied `instanceData`.
188
+ If nothing is encrypted the supplied `instanceData` is returned.
189
+
190
+ It will reject if there are keys missing for any encrypted properties, or
191
+ if the encrypted data is invalid. If you don't want it to reject, you can
192
+ supply a `onCorruptedData` function which returns a value to use instead.
193
+
194
+ #### Parameters
195
+
196
+ | Name | Type |
197
+ | :------ | :------ |
198
+ | `__namedParameters` | `Object` |
199
+ | `__namedParameters.apiToken` | `string` |
200
+ | `__namedParameters.apiUrl` | `string` |
201
+ | `__namedParameters.instanceData` | `Instance` |
202
+ | `__namedParameters.maxcryptor` | `DataEncryptorI` |
203
+ | `__namedParameters.onCorruptedData?` | (`fieldName`: `string`, `fieldValue`: `Encrypted`<`string`\>, `error`: `Error`) => `any` |
204
+
205
+ #### Returns
206
+
207
+ `Promise`<`Object`\>
208
+
209
+ a copy of `instanceData` with all properties decrypted.
210
+
211
+ ___
212
+
213
+ ### findSchemaLabel
214
+
215
+ ▸ `Const` **findSchemaLabel**(`labels`): `undefined` \| `string`
216
+
217
+ Finds the label (on a profile object) which is the schema name
218
+
219
+ #### Parameters
220
+
221
+ | Name | Type |
222
+ | :------ | :------ |
223
+ | `labels` | `undefined` \| `string`[] |
224
+
225
+ #### Returns
226
+
227
+ `undefined` \| `string`
228
+
229
+ ___
230
+
231
+ ### fromCognitoAttributes
232
+
233
+ ▸ `Const` **fromCognitoAttributes**(`attributes`): `UserDoc`
234
+
235
+ This function converts Cognito's userAttributes into a maxcryptor UserDoc.
236
+ The userAttributes come from `const attributes = await Auth.userAttributes(user)`
237
+
238
+ #### Parameters
239
+
240
+ | Name | Type |
241
+ | :------ | :------ |
242
+ | `attributes` | `ICognitoUserAttributeData`[] |
243
+
244
+ #### Returns
245
+
246
+ `UserDoc`
247
+
248
+ ___
249
+
250
+ ### getAADecryptor
251
+
252
+ ▸ **getAADecryptor**(`__namedParameters`): `Promise`<`Object`\>
253
+
254
+ Fetchs the public and private keys for an Access Application, then initializes
255
+ the [Maxcryptor](https://npm.im/@raytio/maxcryptor)'s `ApplicationEncryptor`.
256
+
257
+ #### Parameters
258
+
259
+ | Name | Type |
260
+ | :------ | :------ |
261
+ | `__namedParameters` | `Arg` |
262
+
263
+ #### Returns
264
+
265
+ `Promise`<`Object`\>
266
+
267
+ an `ApplicationEncryptor` and the public key of the Access Application
268
+
269
+ ___
270
+
271
+ ### getOwnRealVerifications
272
+
273
+ ▸ `Const` **getOwnRealVerifications**(`__namedParameters`): `Promise`<`RealVer`[]\>
274
+
275
+ Given a list of verifications and decrypted profile objects, this function
276
+ locally verifies the credibility of the signatures in the verifications.
277
+
278
+ This function does NOT call the API, except to fetch the public key.
279
+
280
+ #### Parameters
281
+
282
+ | Name | Type |
283
+ | :------ | :------ |
284
+ | `__namedParameters` | `Object` |
285
+ | `__namedParameters.profileObjects` | `ProfileObject`<`Json`\>[] |
286
+ | `__namedParameters.userId` | `UId` |
287
+ | `__namedParameters.verifications` | `Verification`<``false``\>[] |
288
+
289
+ #### Returns
290
+
291
+ `Promise`<`RealVer`[]\>
292
+
293
+ a list of authentic RealVer
294
+
295
+ ___
296
+
297
+ ### getPOVerification
298
+
299
+ ▸ **getPOVerification**(`__namedParameters`): `Object`
300
+
301
+ Determines the verification status of a profile object, and its individual fields.
302
+
303
+ #### Parameters
304
+
305
+ | Name | Type |
306
+ | :------ | :------ |
307
+ | `__namedParameters` | `Object` |
308
+ | `__namedParameters.PO` | `ProfileObject`<`Json`\> \| `ProfileObjectForUpload`<`Json`\> |
309
+ | `__namedParameters.realVers` | `RealVer`[] |
310
+ | `__namedParameters.schema` | `Schema` |
311
+
312
+ #### Returns
313
+
314
+ `Object`
315
+
316
+ | Name | Type |
317
+ | :------ | :------ |
318
+ | `details` | `Object` |
319
+ | `details.sourceNId?` | `NId` |
320
+ | `details.verifiers` | `VerificationProvider`[] |
321
+ | `fieldVerifications` | `Record`<`string`, `FieldVerification`\> |
322
+ | `status` | `POVerification` |
323
+
324
+ ___
325
+
326
+ ### getSomeoneElsesRealVerifications
327
+
328
+ ▸ `Const` **getSomeoneElsesRealVerifications**(`__namedParameters`): `Promise`<`RealVer`[]\>
329
+
330
+ Given a list of verifications and decrypted profile objects, this function calls
331
+ the Raytio API to verify the credibility of these verifications, returning only valid
332
+ verifications.
333
+
334
+ ❗ prefer `getOwnRealVerifications` if the data to be verified belongs to the current user.
335
+
336
+ #### Parameters
337
+
338
+ | Name | Type |
339
+ | :------ | :------ |
340
+ | `__namedParameters` | `Props` |
341
+
342
+ #### Returns
343
+
344
+ `Promise`<`RealVer`[]\>
345
+
346
+ a list of fileNames/values that are verified.
347
+
348
+ ___
349
+
350
+ ### hashPassword
351
+
352
+ ▸ **hashPassword**(`password`): `Promise`<`string`\>
353
+
354
+ AWS Cognito never gets the raw password. We send them
355
+ a hashed verison using PBKDF2 with SHA-256 and 10,000
356
+ iterations.
357
+
358
+ #### Parameters
359
+
360
+ | Name | Type | Description |
361
+ | :------ | :------ | :------ |
362
+ | `password` | `string` | The raw password |
363
+
364
+ #### Returns
365
+
366
+ `Promise`<`string`\>
367
+
368
+ Promise resolving to the hashed password
369
+
370
+ ___
371
+
372
+ ### isConditionMet
373
+
374
+ ▸ `Const` **isConditionMet**(`condition`, `formValues`): `boolean`
375
+
376
+ Checks all other form values in case any have a
377
+ trigger value that makes this field requirted.
378
+
379
+ #### Parameters
380
+
381
+ | Name | Type |
382
+ | :------ | :------ |
383
+ | `condition` | `Record`<`string`, `ConditionValue`[]\> |
384
+ | `formValues` | `Record`<`string`, `unknown`\> |
385
+
386
+ #### Returns
387
+
388
+ `boolean`
389
+
390
+ ___
391
+
392
+ ### isEncrypted
393
+
394
+ ▸ `Const` **isEncrypted**(`value`): value is Encrypted<string\>
395
+
396
+ Determines where the input is an encrypted Raytio object
397
+
398
+ #### Parameters
399
+
400
+ | Name | Type | Description |
401
+ | :------ | :------ | :------ |
402
+ | `value` | `unknown` | anything |
403
+
404
+ #### Returns
405
+
406
+ value is Encrypted<string\>
407
+
408
+ true or false depending on whether the input is an encrypted Raytio object
409
+
410
+ ___
411
+
412
+ ### isEncryptedFile
413
+
414
+ ▸ `Const` **isEncryptedFile**(`value`): value is Encrypted<string\>
415
+
416
+ Determines where the input is an encrypted Raytio file
417
+
418
+ #### Parameters
419
+
420
+ | Name | Type | Description |
421
+ | :------ | :------ | :------ |
422
+ | `value` | `unknown` | anything |
423
+
424
+ #### Returns
425
+
426
+ value is Encrypted<string\>
427
+
428
+ true or false depending on whether the input is an encrypted Raytio file
429
+
430
+ ___
431
+
432
+ ### someEncrypted
433
+
434
+ ▸ `Const` **someEncrypted**<`T`, `K`\>(...`args`): `number`
435
+
436
+ Given a profile object's properties, returns the number
437
+ of properties that are encryted.
438
+
439
+ #### Type parameters
440
+
441
+ | Name | Type |
442
+ | :------ | :------ |
443
+ | `T` | extends `object` |
444
+ | `K` | extends `string` \| `number` \| `symbol` |
445
+
446
+ #### Parameters
447
+
448
+ | Name | Type |
449
+ | :------ | :------ |
450
+ | `...args` | [obj: T] |
451
+
452
+ #### Returns
453
+
454
+ `number`
455
+
456
+ ___
457
+
458
+ ### toCognitoAttributes
459
+
460
+ ▸ `Const` **toCognitoAttributes**(`userDoc`): `Object`
461
+
462
+ Given a `UserDoc` from the maxcryptor, this returns an object
463
+ which you can provide to `Auth.updateUserAttributes()`. It is
464
+ an object of stringified Json.
465
+
466
+ #### Parameters
467
+
468
+ | Name | Type |
469
+ | :------ | :------ |
470
+ | `userDoc` | `UserDoc` |
471
+
472
+ #### Returns
473
+
474
+ `Object`
@@ -46,7 +46,6 @@ async function createApplicationEncryptor(userDoc, maxcryptor) {
46
46
  */
47
47
  async function createAA({ apiUrl, apiToken, userDoc, maxcryptor, application, }) {
48
48
  if (!application.org_id) {
49
- // eslint-disable-next-line fp/no-throw
50
49
  throw new Error("Cannot create an AA without an org_id");
51
50
  }
52
51
  const newApp = await createApplication({
@@ -13,7 +13,6 @@ const decryptKeys = async ({ applicationDecryptor, keys, data, onCorruptedData,
13
13
  const error = new Error(`Can't decrypt shared data because there are no keys for ${key}`);
14
14
  if (onCorruptedData)
15
15
  return [key, onCorruptedData(key, value, error)];
16
- // eslint-disable-next-line fp/no-throw
17
16
  throw error;
18
17
  }
19
18
  try {
@@ -26,7 +25,6 @@ const decryptKeys = async ({ applicationDecryptor, keys, data, onCorruptedData,
26
25
  const error = _ex instanceof Error ? _ex : new Error(`${_ex}`);
27
26
  if (onCorruptedData)
28
27
  return [key, onCorruptedData(key, value, error)];
29
- // eslint-disable-next-line fp/no-throw
30
28
  throw error;
31
29
  }
32
30
  }));
@@ -20,15 +20,18 @@ const decryptSharedData = async ({ instanceData, maxcryptor, apiUrl, apiToken, o
20
20
  apiUrl,
21
21
  apiToken,
22
22
  });
23
- const instance = Object.assign(Object.assign({}, instanceData), { profile_objects: await Promise.all(instanceData.profile_objects.map(async (profileObject) => {
24
- const keys = instanceData.keys[profileObject.n_id];
23
+ const instance = Object.assign(Object.assign({}, instanceData), { profile_objects: await Promise.all(instanceData.profile_objects.map(async (PO) => {
24
+ const realNId = PO.n_id.startsWith("HASHED::")
25
+ ? PO.n_id.split("::")[1]
26
+ : PO.n_id;
27
+ const keys = instanceData.keys[realNId];
25
28
  const decryptedProperties = await (0, decryptKeys_1.decryptKeys)({
26
29
  applicationDecryptor,
27
30
  keys,
28
- data: profileObject.properties,
31
+ data: PO.properties,
29
32
  onCorruptedData,
30
33
  });
31
- return Object.assign(Object.assign({}, profileObject), { properties: decryptedProperties });
34
+ return Object.assign(Object.assign({}, PO), { properties: decryptedProperties });
32
35
  })) });
33
36
  return { instance, applicationDecryptor };
34
37
  };
@@ -15,4 +15,4 @@ export declare const isEncryptedFile: (value: unknown) => value is Encrypted<str
15
15
  * Given a profile object's properties, returns the number
16
16
  * of properties that are encryted.
17
17
  */
18
- export declare const someEncrypted: <T extends object, K extends keyof T>(x: T) => number;
18
+ export declare const someEncrypted: <T extends object, K extends keyof T>(obj: T) => number;
@@ -1,2 +1,3 @@
1
1
  export * from "./conditional";
2
2
  export * from "./password";
3
+ export * from "./types";
@@ -12,3 +12,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
12
12
  Object.defineProperty(exports, "__esModule", { value: true });
13
13
  __exportStar(require("./conditional"), exports);
14
14
  __exportStar(require("./password"), exports);
15
+ __exportStar(require("./types"), exports);
@@ -0,0 +1,6 @@
1
+ /**
2
+ * to prevent a key from the JS object prototype (e.g. `__proto__`) from
3
+ * returning an unexpected value (see #1162)
4
+ * @ignore
5
+ */
6
+ export declare function assertSafeProperty(key: string): asserts key is string;
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.assertSafeProperty = void 0;
4
+ /**
5
+ * to prevent a key from the JS object prototype (e.g. `__proto__`) from
6
+ * returning an unexpected value (see #1162)
7
+ * @ignore
8
+ */
9
+ function assertSafeProperty(key) {
10
+ if (key in {})
11
+ throw new Error("Unacceptable object property");
12
+ }
13
+ exports.assertSafeProperty = assertSafeProperty;
package/dist/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export * from "./accessApplication";
2
2
  export * from "./crypto";
3
3
  export * from "./general";
4
+ export * from "./rules";
4
5
  export * from "./schema";
5
6
  export * from "./verifications";
package/dist/index.js CHANGED
@@ -13,5 +13,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
13
13
  __exportStar(require("./accessApplication"), exports);
14
14
  __exportStar(require("./crypto"), exports);
15
15
  __exportStar(require("./general"), exports);
16
+ __exportStar(require("./rules"), exports);
16
17
  __exportStar(require("./schema"), exports);
17
18
  __exportStar(require("./verifications"), exports);
@@ -0,0 +1,11 @@
1
+ import { RuleData, ScoreConfig } from "./types";
2
+ /** @ignore */
3
+ export declare type ScoreResult = {
4
+ score: number;
5
+ category: string;
6
+ };
7
+ /**
8
+ * the main function to calculate a score and category.
9
+ * Might throw an error.
10
+ */
11
+ export declare function calculateScore(ruleConfig: ScoreConfig, ruleInput: RuleData): ScoreResult;
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.calculateScore = void 0;
4
+ const evaluateScoreCondition_1 = require("./evaluateScoreCondition");
5
+ const helpers_1 = require("./helpers");
6
+ function evaluateASTNode(node, data) {
7
+ if (node.type === "LanguageOperator") {
8
+ if (!node.children.length) {
9
+ // we allow a single child because why not.
10
+ throw new Error("LanguageOperator has no children");
11
+ }
12
+ if (node.operator === "AND") {
13
+ return node.children.every(n => evaluateASTNode(n, data));
14
+ }
15
+ if (node.operator === "OR") {
16
+ return node.children.some(n => evaluateASTNode(n, data));
17
+ }
18
+ throw new Error(`LanguageOperator node has invalid operator '${node.operator}'`);
19
+ }
20
+ if (node.type === "ScoreCondition") {
21
+ return (0, evaluateScoreCondition_1.evaluateScoreCondition)(node, data);
22
+ }
23
+ throw new Error("ASTNode has invalid type");
24
+ }
25
+ function evaluateRule(rule, data) {
26
+ if (rule.tree.length !== 1) {
27
+ throw new Error("A rule may only have 1 root node");
28
+ }
29
+ const rulePassed = rule.tree.every(n => evaluateASTNode(n, data));
30
+ return rulePassed ? rule.trueWeight : rule.falseWeight;
31
+ }
32
+ /**
33
+ * the main function to calculate a score and category.
34
+ * Might throw an error.
35
+ */
36
+ function calculateScore(ruleConfig, ruleInput) {
37
+ const result = ruleConfig.rules.map(rule => evaluateRule(rule, ruleInput));
38
+ const [addOrMultiply, initialValue] = (0, helpers_1.getCombinator)(ruleConfig.combinator);
39
+ const finalScore = result.reduce(addOrMultiply, initialValue);
40
+ const matchingCategories = (0, helpers_1.addInfiniteThresholdBoundaries)(ruleConfig.scoreThresholds).filter(threshold => finalScore >= threshold.from && finalScore <= threshold.to);
41
+ if (matchingCategories.length === 0) {
42
+ throw new Error(`No categories match the final score (${finalScore})`);
43
+ }
44
+ if (matchingCategories.length !== 1) {
45
+ throw new Error(`Multiple categories match the final score (${finalScore})`);
46
+ }
47
+ return {
48
+ score: finalScore,
49
+ category: matchingCategories[0].name,
50
+ };
51
+ }
52
+ exports.calculateScore = calculateScore;
@@ -0,0 +1,3 @@
1
+ import { Instance, RealVer, Schema } from "@raytio/types";
2
+ import { RuleData } from "./types";
3
+ export declare const convertInstanceToRuleInput: (instance: Instance, realVers: RealVer[], getSchema: (schemaName: string) => Promise<Schema>) => Promise<RuleData>;