@friggframework/core 0.2.31-v1-alpha-package-update.0 → 1.0.1-canary.a414ea2.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 (156) hide show
  1. package/assertions/CHANGELOG.md +87 -0
  2. package/assertions/LICENSE.md +9 -0
  3. package/assertions/README.md +3 -0
  4. package/assertions/bump.txt +1 -0
  5. package/assertions/get.js +139 -0
  6. package/assertions/index.js +19 -0
  7. package/assertions/is-equal.js +17 -0
  8. package/associations/LICENSE.md +9 -0
  9. package/associations/README.md +3 -0
  10. package/associations/association.js +78 -0
  11. package/associations/bump3.txt +0 -0
  12. package/associations/jest.config.js +5 -0
  13. package/associations/model.js +54 -0
  14. package/bump0.txt +1 -0
  15. package/core/.eslintrc.json +3 -0
  16. package/{Delegate.js → core/Delegate.js} +1 -1
  17. package/core/LICENSE.md +9 -0
  18. package/{Worker.js → core/Worker.js} +2 -2
  19. package/core/bump3.txt +0 -0
  20. package/{create-handler.js → core/create-handler.js} +2 -2
  21. package/core/index.js +6 -0
  22. package/core/jest.config.js +5 -0
  23. package/database/.eslintrc.json +3 -0
  24. package/database/CHANGELOG.md +97 -0
  25. package/database/LICENSE.md +9 -0
  26. package/database/README.md +3 -0
  27. package/database/bump3.txt +0 -0
  28. package/database/index.js +23 -0
  29. package/database/jest.config.js +5 -0
  30. package/database/models/IndividualUser.js +76 -0
  31. package/database/models/OrganizationUser.js +29 -0
  32. package/database/models/State.js +9 -0
  33. package/database/models/Token.js +70 -0
  34. package/database/models/UserModel.js +7 -0
  35. package/database/mongo.js +45 -0
  36. package/database/mongoose.js +5 -0
  37. package/encrypt/.eslintrc.json +3 -0
  38. package/encrypt/CHANGELOG.md +78 -0
  39. package/encrypt/Cryptor.js +236 -0
  40. package/encrypt/Cryptor.test.js +32 -0
  41. package/encrypt/LICENSE.md +9 -0
  42. package/encrypt/README.md +13 -0
  43. package/encrypt/aes.js +27 -0
  44. package/encrypt/bump3.txt +0 -0
  45. package/encrypt/encrypt.js +132 -0
  46. package/encrypt/encrypt.test.js +1069 -0
  47. package/encrypt/index.js +4 -0
  48. package/encrypt/jest.config.js +5 -0
  49. package/encrypt/test-encrypt.js +107 -0
  50. package/errors/.eslintrc.json +3 -0
  51. package/errors/CHANGELOG.md +44 -0
  52. package/errors/LICENSE.md +9 -0
  53. package/errors/README.md +3 -0
  54. package/errors/base-error.js +23 -0
  55. package/errors/base-error.test.js +32 -0
  56. package/errors/bump.txt +1 -0
  57. package/errors/bump3.txt +0 -0
  58. package/errors/fetch-error.js +76 -0
  59. package/errors/fetch-error.test.js +79 -0
  60. package/errors/halt-error.js +10 -0
  61. package/errors/halt-error.test.js +11 -0
  62. package/errors/index.js +15 -0
  63. package/errors/jest.config.js +5 -0
  64. package/errors/validation-errors.js +23 -0
  65. package/errors/validation-errors.test.js +120 -0
  66. package/index.js +123 -5
  67. package/integrations/.eslintrc.json +3 -0
  68. package/integrations/CHANGELOG.md +244 -0
  69. package/integrations/LICENSE.md +9 -0
  70. package/integrations/README.md +3 -0
  71. package/integrations/bump3.txt +0 -0
  72. package/integrations/create-frigg-backend.js +31 -0
  73. package/integrations/index.js +19 -0
  74. package/integrations/integration-base.js +162 -0
  75. package/integrations/integration-factory.js +166 -0
  76. package/integrations/integration-mapping.js +43 -0
  77. package/integrations/integration-model.js +42 -0
  78. package/integrations/integration-router.js +367 -0
  79. package/integrations/integration-user.js +144 -0
  80. package/integrations/jest-setup.js +2 -0
  81. package/integrations/jest-teardown.js +2 -0
  82. package/integrations/jest.config.js +12 -0
  83. package/integrations/options.js +54 -0
  84. package/integrations/test/integration-base.test.js +143 -0
  85. package/lambda/README.md +3 -0
  86. package/lambda/TimeoutCatcher.js +43 -0
  87. package/lambda/TimeoutCatcher.test.js +68 -0
  88. package/lambda/bump3.txt +0 -0
  89. package/lambda/index.js +3 -0
  90. package/lambda/jest.config.js +3 -0
  91. package/logs/.eslintrc.json +3 -0
  92. package/logs/CHANGELOG.md +57 -0
  93. package/logs/LICENSE.md +9 -0
  94. package/logs/README.md +3 -0
  95. package/logs/bump3.txt +0 -0
  96. package/logs/index.js +7 -0
  97. package/logs/jest.config.js +5 -0
  98. package/logs/logger.js +69 -0
  99. package/logs/logger.test.js +76 -0
  100. package/module-plugin/.eslintrc.json +3 -0
  101. package/module-plugin/CHANGELOG.md +224 -0
  102. package/module-plugin/LICENSE.md +9 -0
  103. package/module-plugin/ModuleConstants.js +10 -0
  104. package/module-plugin/README.md +3 -0
  105. package/module-plugin/auther.js +342 -0
  106. package/module-plugin/bump3.txt +0 -0
  107. package/module-plugin/credential.js +22 -0
  108. package/module-plugin/entity-manager.js +70 -0
  109. package/module-plugin/entity.js +46 -0
  110. package/module-plugin/index.js +25 -0
  111. package/module-plugin/jest-setup.js +3 -0
  112. package/module-plugin/jest-teardown.js +2 -0
  113. package/module-plugin/jest.config.js +20 -0
  114. package/module-plugin/manager.js +169 -0
  115. package/module-plugin/module-factory.js +60 -0
  116. package/module-plugin/requester/api-key.js +36 -0
  117. package/module-plugin/requester/basic.js +43 -0
  118. package/module-plugin/requester/oauth-2.js +219 -0
  119. package/module-plugin/requester/requester.js +151 -0
  120. package/module-plugin/requester/requester.test.js +28 -0
  121. package/module-plugin/test/auther.test.js +97 -0
  122. package/module-plugin/test/mock-api/api.js +29 -0
  123. package/module-plugin/test/mock-api/definition.js +48 -0
  124. package/module-plugin/test/mock-api/mocks/hubspot.js +43 -0
  125. package/package.json +28 -12
  126. package/syncs/README.md +3 -0
  127. package/syncs/bump3.txt +0 -0
  128. package/syncs/jest.config.js +5 -0
  129. package/syncs/manager.js +464 -0
  130. package/syncs/model.js +62 -0
  131. package/syncs/sync.js +114 -0
  132. package/types/CHANGELOG.md +49 -0
  133. package/types/README.md +24 -0
  134. package/types/assertions/index.d.ts +83 -0
  135. package/types/associations/index.d.ts +74 -0
  136. package/types/bump3.txt +0 -0
  137. package/types/core/index.d.ts +54 -0
  138. package/types/database/index.d.ts +3 -0
  139. package/types/encrypt/index.d.ts +5 -0
  140. package/types/errors/index.d.ts +66 -0
  141. package/types/eslint-config/index.d.ts +41 -0
  142. package/types/index.d.ts +14 -0
  143. package/types/integrations/index.d.ts +191 -0
  144. package/types/lambda/index.d.ts +31 -0
  145. package/types/logs/index.d.ts +5 -0
  146. package/types/module-plugin/index.d.ts +293 -0
  147. package/types/prettier-config/index.d.ts +6 -0
  148. package/types/syncs/index.d.ts +128 -0
  149. package/types/test-environment/index.d.ts +17 -0
  150. package/types/tsconfig.json +103 -0
  151. /package/{.eslintrc.json → assertions/.eslintrc.json} +0 -0
  152. /package/{bump3.txt → assertions/bump3.txt} +0 -0
  153. /package/{jest.config.js → assertions/jest.config.js} +0 -0
  154. /package/{CHANGELOG.md → core/CHANGELOG.md} +0 -0
  155. /package/{README.md → core/README.md} +0 -0
  156. /package/{load-installed-modules.js → core/load-installed-modules.js} +0 -0
@@ -0,0 +1,132 @@
1
+ const { Cryptor } = require('./Cryptor');
2
+
3
+ const updateOneEvents = [
4
+ 'updateOne',
5
+ 'replaceOne',
6
+ 'findOneAndUpdate',
7
+ 'findOneAndReplace',
8
+ ];
9
+ const findOneEvents = [
10
+ 'findOne',
11
+ 'findOneAndDelete',
12
+ 'findOneAndRemove',
13
+ 'findOneAndUpdate',
14
+ 'findOneAndReplace',
15
+ ];
16
+
17
+ const shouldBypassEncryption = (STAGE) => {
18
+ const defaultBypassStages = ['dev', 'test', 'local'];
19
+ const bypassStageEnv = process.env.BYPASS_ENCRYPTION_STAGE;
20
+ // If the env is set to anything or an empty string, use the env. Otherwise, use the default array
21
+ const useEnv = !String(bypassStageEnv) || !!bypassStageEnv;
22
+ const bypassStages = useEnv
23
+ ? bypassStageEnv.split(',').map((stage) => stage.trim())
24
+ : defaultBypassStages;
25
+ return bypassStages.includes(STAGE);
26
+ };
27
+
28
+ // The Mongoose plug-in function
29
+ function Encrypt(schema, options) {
30
+ const { STAGE, KMS_KEY_ARN, AES_KEY_ID } = process.env;
31
+
32
+ if (shouldBypassEncryption(STAGE)) {
33
+ return;
34
+ }
35
+
36
+ if (KMS_KEY_ARN && AES_KEY_ID) {
37
+ throw new Error(
38
+ 'Local and AWS encryption keys are both set in the environment.'
39
+ );
40
+ }
41
+
42
+ const fields = Object.values(schema.paths)
43
+ .map(({ path, options }) => (options.lhEncrypt === true ? path : ''))
44
+ .filter(Boolean);
45
+
46
+ if (!fields.length) {
47
+ return;
48
+ }
49
+
50
+ const cryptor = new Cryptor({
51
+ // Use AWS if the CMK is present
52
+ shouldUseAws: !!KMS_KEY_ARN,
53
+ // Find all the fields in the schema with lhEncrypt === true
54
+ fields: fields,
55
+ });
56
+
57
+ // ---------------------------------------------
58
+ // ### Encrypt fields before save/update/insert.
59
+ // ---------------------------------------------
60
+
61
+ schema.pre('save', async function encryptionPreSave() {
62
+ // `this` will be a doc
63
+ await cryptor.encryptFieldsInDocuments([this]);
64
+ });
65
+
66
+ schema.pre(
67
+ 'insertMany',
68
+ async function encryptionPreInsertMany(_, docs, options) {
69
+ // `this` will be the model
70
+ if (options?.rawResult) {
71
+ throw new Error(
72
+ 'Raw result not supported for insertMany with Encrypt plugin'
73
+ );
74
+ }
75
+
76
+ await cryptor.encryptFieldsInDocuments(docs);
77
+ }
78
+ );
79
+
80
+ schema.pre(updateOneEvents, async function encryptionPreUpdateOne() {
81
+ // `this` will be a query
82
+ await cryptor.encryptFieldsInQuery(this);
83
+ });
84
+
85
+ schema.pre('updateMany', async function encryptionPreUpdateMany() {
86
+ // `this` will be a query
87
+ cryptor.expectNotToUpdateManyEncrypted(this.getUpdate());
88
+ });
89
+
90
+ schema.pre('update', async function encryptionPreUpdate() {
91
+ // `this` will be a query
92
+ const { multiple } = this.getOptions();
93
+
94
+ if (multiple) {
95
+ cryptor.expectNotToUpdateManyEncrypted(this.getUpdate());
96
+ return;
97
+ }
98
+
99
+ await cryptor.encryptFieldsInQuery(this);
100
+ });
101
+
102
+ // --------------------------------------------
103
+ // ### Decrypt documents after they are loaded.
104
+ // --------------------------------------------
105
+ schema.post('save', async function encryptionPreSave() {
106
+ // `this` will be a doc
107
+ await cryptor.decryptFieldsInDocuments([this]);
108
+ });
109
+
110
+ schema.post(findOneEvents, async function encryptionPostFindOne(doc) {
111
+ // `this` will be a query
112
+ const { rawResult } = this.getOptions();
113
+
114
+ if (rawResult) {
115
+ return;
116
+ }
117
+
118
+ await cryptor.decryptFieldsInDocuments([doc]);
119
+ });
120
+
121
+ schema.post('find', async function encryptionPostFind(docs) {
122
+ // `this` will be a query
123
+ await cryptor.decryptFieldsInDocuments(docs);
124
+ });
125
+
126
+ schema.post('insertMany', async function encryptionPostInsertMany(docs) {
127
+ // `this` will be the model
128
+ await cryptor.decryptFieldsInDocuments(docs);
129
+ });
130
+ }
131
+
132
+ module.exports = { Encrypt };