@raytio/core 8.1.2 → 9.0.2

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