@budibase/backend-core 3.2.4 → 3.2.6

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 (272) hide show
  1. package/dist/index.js.map +1 -1
  2. package/dist/index.js.meta.json +1 -1
  3. package/dist/package.json +11 -4
  4. package/dist/plugins.js.meta.json +1 -1
  5. package/package.json +11 -4
  6. package/src/accounts/accounts.ts +0 -82
  7. package/src/accounts/api.ts +0 -59
  8. package/src/accounts/index.ts +0 -1
  9. package/src/auth/auth.ts +0 -210
  10. package/src/auth/index.ts +0 -1
  11. package/src/auth/tests/auth.spec.ts +0 -14
  12. package/src/blacklist/blacklist.ts +0 -54
  13. package/src/blacklist/index.ts +0 -1
  14. package/src/blacklist/tests/blacklist.spec.ts +0 -46
  15. package/src/cache/appMetadata.ts +0 -88
  16. package/src/cache/base/index.ts +0 -150
  17. package/src/cache/docWritethrough.ts +0 -105
  18. package/src/cache/generic.ts +0 -33
  19. package/src/cache/index.ts +0 -8
  20. package/src/cache/invite.ts +0 -86
  21. package/src/cache/passwordReset.ts +0 -49
  22. package/src/cache/tests/docWritethrough.spec.ts +0 -296
  23. package/src/cache/tests/user.spec.ts +0 -145
  24. package/src/cache/tests/writethrough.spec.ts +0 -139
  25. package/src/cache/user.ts +0 -154
  26. package/src/cache/writethrough.ts +0 -133
  27. package/src/configs/configs.ts +0 -263
  28. package/src/configs/index.ts +0 -1
  29. package/src/configs/tests/configs.spec.ts +0 -184
  30. package/src/constants/db.ts +0 -75
  31. package/src/constants/index.ts +0 -2
  32. package/src/constants/misc.ts +0 -36
  33. package/src/context/Context.ts +0 -14
  34. package/src/context/identity.ts +0 -58
  35. package/src/context/index.ts +0 -3
  36. package/src/context/mainContext.ts +0 -422
  37. package/src/context/tests/index.spec.ts +0 -255
  38. package/src/context/types.ts +0 -26
  39. package/src/db/Replication.ts +0 -94
  40. package/src/db/couch/DatabaseImpl.ts +0 -511
  41. package/src/db/couch/connections.ts +0 -89
  42. package/src/db/couch/index.ts +0 -4
  43. package/src/db/couch/pouchDB.ts +0 -97
  44. package/src/db/couch/pouchDump.ts +0 -0
  45. package/src/db/couch/tests/DatabaseImpl.spec.ts +0 -118
  46. package/src/db/couch/utils.ts +0 -55
  47. package/src/db/db.ts +0 -34
  48. package/src/db/errors.ts +0 -14
  49. package/src/db/index.ts +0 -12
  50. package/src/db/instrumentation.ts +0 -199
  51. package/src/db/lucene.ts +0 -721
  52. package/src/db/searchIndexes/index.ts +0 -1
  53. package/src/db/searchIndexes/searchIndexes.ts +0 -62
  54. package/src/db/tests/DatabaseImpl.spec.ts +0 -55
  55. package/src/db/tests/connections.spec.ts +0 -22
  56. package/src/db/tests/index.spec.ts +0 -32
  57. package/src/db/tests/lucene.spec.ts +0 -400
  58. package/src/db/tests/pouch.spec.js +0 -62
  59. package/src/db/tests/utils.spec.ts +0 -63
  60. package/src/db/utils.ts +0 -208
  61. package/src/db/views.ts +0 -245
  62. package/src/docIds/conversions.ts +0 -60
  63. package/src/docIds/ids.ts +0 -126
  64. package/src/docIds/index.ts +0 -2
  65. package/src/docIds/newid.ts +0 -5
  66. package/src/docIds/params.ts +0 -189
  67. package/src/docUpdates/index.ts +0 -24
  68. package/src/environment.ts +0 -293
  69. package/src/errors/errors.ts +0 -119
  70. package/src/errors/index.ts +0 -1
  71. package/src/events/analytics.ts +0 -6
  72. package/src/events/asyncEvents/index.ts +0 -2
  73. package/src/events/asyncEvents/publisher.ts +0 -12
  74. package/src/events/asyncEvents/queue.ts +0 -22
  75. package/src/events/backfill.ts +0 -183
  76. package/src/events/documentId.ts +0 -56
  77. package/src/events/events.ts +0 -47
  78. package/src/events/identification.ts +0 -311
  79. package/src/events/index.ts +0 -15
  80. package/src/events/processors/AnalyticsProcessor.ts +0 -64
  81. package/src/events/processors/AuditLogsProcessor.ts +0 -92
  82. package/src/events/processors/LoggingProcessor.ts +0 -36
  83. package/src/events/processors/Processors.ts +0 -52
  84. package/src/events/processors/async/DocumentUpdateProcessor.ts +0 -38
  85. package/src/events/processors/index.ts +0 -19
  86. package/src/events/processors/posthog/PosthogProcessor.ts +0 -118
  87. package/src/events/processors/posthog/index.ts +0 -3
  88. package/src/events/processors/posthog/rateLimiting.ts +0 -106
  89. package/src/events/processors/posthog/tests/PosthogProcessor.spec.ts +0 -164
  90. package/src/events/processors/types.ts +0 -1
  91. package/src/events/publishers/account.ts +0 -41
  92. package/src/events/publishers/ai.ts +0 -21
  93. package/src/events/publishers/app.ts +0 -168
  94. package/src/events/publishers/auditLog.ts +0 -26
  95. package/src/events/publishers/auth.ts +0 -73
  96. package/src/events/publishers/automation.ts +0 -110
  97. package/src/events/publishers/backfill.ts +0 -74
  98. package/src/events/publishers/backup.ts +0 -42
  99. package/src/events/publishers/datasource.ts +0 -48
  100. package/src/events/publishers/email.ts +0 -17
  101. package/src/events/publishers/environmentVariable.ts +0 -38
  102. package/src/events/publishers/group.ts +0 -99
  103. package/src/events/publishers/index.ts +0 -25
  104. package/src/events/publishers/installation.ts +0 -38
  105. package/src/events/publishers/layout.ts +0 -26
  106. package/src/events/publishers/license.ts +0 -84
  107. package/src/events/publishers/org.ts +0 -37
  108. package/src/events/publishers/plugin.ts +0 -47
  109. package/src/events/publishers/query.ts +0 -89
  110. package/src/events/publishers/role.ts +0 -62
  111. package/src/events/publishers/rows.ts +0 -29
  112. package/src/events/publishers/screen.ts +0 -36
  113. package/src/events/publishers/serve.ts +0 -43
  114. package/src/events/publishers/table.ts +0 -70
  115. package/src/events/publishers/user.ts +0 -202
  116. package/src/events/publishers/view.ts +0 -107
  117. package/src/features/features.ts +0 -277
  118. package/src/features/index.ts +0 -2
  119. package/src/features/tests/features.spec.ts +0 -267
  120. package/src/features/tests/utils.ts +0 -64
  121. package/src/helpers.ts +0 -9
  122. package/src/index.ts +0 -59
  123. package/src/installation.ts +0 -115
  124. package/src/logging/alerts.ts +0 -26
  125. package/src/logging/correlation/correlation.ts +0 -15
  126. package/src/logging/correlation/index.ts +0 -1
  127. package/src/logging/correlation/middleware.ts +0 -18
  128. package/src/logging/index.ts +0 -4
  129. package/src/logging/pino/logger.ts +0 -239
  130. package/src/logging/pino/middleware.ts +0 -48
  131. package/src/logging/system.ts +0 -81
  132. package/src/logging/tests/system.spec.ts +0 -61
  133. package/src/middleware/adminOnly.ts +0 -9
  134. package/src/middleware/auditLog.ts +0 -6
  135. package/src/middleware/authenticated.ts +0 -247
  136. package/src/middleware/builderOnly.ts +0 -21
  137. package/src/middleware/builderOrAdmin.ts +0 -21
  138. package/src/middleware/contentSecurityPolicy.ts +0 -113
  139. package/src/middleware/csrf.ts +0 -81
  140. package/src/middleware/errorHandling.ts +0 -43
  141. package/src/middleware/index.ts +0 -24
  142. package/src/middleware/internalApi.ts +0 -23
  143. package/src/middleware/ip.ts +0 -12
  144. package/src/middleware/joi-validator.ts +0 -58
  145. package/src/middleware/matchers.ts +0 -39
  146. package/src/middleware/passport/datasource/google.ts +0 -102
  147. package/src/middleware/passport/local.ts +0 -54
  148. package/src/middleware/passport/sso/google.ts +0 -77
  149. package/src/middleware/passport/sso/oidc.ts +0 -152
  150. package/src/middleware/passport/sso/sso.ts +0 -138
  151. package/src/middleware/passport/sso/tests/google.spec.ts +0 -68
  152. package/src/middleware/passport/sso/tests/oidc.spec.ts +0 -144
  153. package/src/middleware/passport/sso/tests/sso.spec.ts +0 -197
  154. package/src/middleware/passport/utils.ts +0 -38
  155. package/src/middleware/querystringToBody.ts +0 -28
  156. package/src/middleware/tenancy.ts +0 -36
  157. package/src/middleware/tests/builder.spec.ts +0 -181
  158. package/src/middleware/tests/contentSecurityPolicy.spec.ts +0 -75
  159. package/src/middleware/tests/matchers.spec.ts +0 -100
  160. package/src/migrations/definitions.ts +0 -40
  161. package/src/migrations/index.ts +0 -2
  162. package/src/migrations/migrations.ts +0 -186
  163. package/src/migrations/tests/__snapshots__/migrations.spec.ts.snap +0 -11
  164. package/src/migrations/tests/migrations.spec.ts +0 -64
  165. package/src/objectStore/buckets/app.ts +0 -53
  166. package/src/objectStore/buckets/global.ts +0 -29
  167. package/src/objectStore/buckets/index.ts +0 -3
  168. package/src/objectStore/buckets/plugins.ts +0 -71
  169. package/src/objectStore/buckets/tests/app.spec.ts +0 -161
  170. package/src/objectStore/buckets/tests/global.spec.ts +0 -74
  171. package/src/objectStore/buckets/tests/plugins.spec.ts +0 -111
  172. package/src/objectStore/cloudfront.ts +0 -41
  173. package/src/objectStore/index.ts +0 -3
  174. package/src/objectStore/objectStore.ts +0 -585
  175. package/src/objectStore/utils.ts +0 -113
  176. package/src/platform/index.ts +0 -3
  177. package/src/platform/platformDb.ts +0 -6
  178. package/src/platform/tenants.ts +0 -101
  179. package/src/platform/tests/tenants.spec.ts +0 -26
  180. package/src/platform/users.ts +0 -129
  181. package/src/plugin/index.ts +0 -1
  182. package/src/plugin/tests/validation.spec.ts +0 -209
  183. package/src/plugin/utils.ts +0 -175
  184. package/src/queue/constants.ts +0 -8
  185. package/src/queue/inMemoryQueue.ts +0 -189
  186. package/src/queue/index.ts +0 -2
  187. package/src/queue/listeners.ts +0 -199
  188. package/src/queue/queue.ts +0 -84
  189. package/src/redis/index.ts +0 -6
  190. package/src/redis/init.ts +0 -118
  191. package/src/redis/redis.ts +0 -358
  192. package/src/redis/redlockImpl.ts +0 -155
  193. package/src/redis/tests/redis.spec.ts +0 -207
  194. package/src/redis/tests/redlockImpl.spec.ts +0 -105
  195. package/src/redis/utils.ts +0 -128
  196. package/src/security/auth.ts +0 -24
  197. package/src/security/encryption.ts +0 -185
  198. package/src/security/index.ts +0 -1
  199. package/src/security/permissions.ts +0 -166
  200. package/src/security/roles.ts +0 -655
  201. package/src/security/secrets.ts +0 -20
  202. package/src/security/sessions.ts +0 -123
  203. package/src/security/tests/auth.spec.ts +0 -45
  204. package/src/security/tests/encryption.spec.ts +0 -31
  205. package/src/security/tests/permissions.spec.ts +0 -146
  206. package/src/security/tests/secrets.spec.ts +0 -35
  207. package/src/security/tests/sessions.spec.ts +0 -12
  208. package/src/sql/designDoc.ts +0 -17
  209. package/src/sql/index.ts +0 -5
  210. package/src/sql/sql.ts +0 -1854
  211. package/src/sql/sqlTable.ts +0 -319
  212. package/src/sql/utils.ts +0 -193
  213. package/src/tenancy/db.ts +0 -6
  214. package/src/tenancy/index.ts +0 -2
  215. package/src/tenancy/tenancy.ts +0 -148
  216. package/src/tenancy/tests/tenancy.spec.ts +0 -184
  217. package/src/timers/index.ts +0 -1
  218. package/src/timers/timers.ts +0 -22
  219. package/src/users/db.ts +0 -582
  220. package/src/users/events.ts +0 -176
  221. package/src/users/index.ts +0 -4
  222. package/src/users/lookup.ts +0 -99
  223. package/src/users/test/db.spec.ts +0 -188
  224. package/src/users/test/utils.spec.ts +0 -67
  225. package/src/users/users.ts +0 -353
  226. package/src/users/utils.ts +0 -81
  227. package/src/utils/Duration.ts +0 -56
  228. package/src/utils/hashing.ts +0 -15
  229. package/src/utils/index.ts +0 -4
  230. package/src/utils/stringUtils.ts +0 -8
  231. package/src/utils/tests/Duration.spec.ts +0 -19
  232. package/src/utils/tests/utils.spec.ts +0 -204
  233. package/src/utils/utils.ts +0 -249
  234. package/tests/core/logging.ts +0 -34
  235. package/tests/core/users/users.spec.js +0 -53
  236. package/tests/core/utilities/index.ts +0 -7
  237. package/tests/core/utilities/jestUtils.ts +0 -33
  238. package/tests/core/utilities/mocks/alerts.ts +0 -4
  239. package/tests/core/utilities/mocks/date.ts +0 -3
  240. package/tests/core/utilities/mocks/events.ts +0 -132
  241. package/tests/core/utilities/mocks/index.ts +0 -9
  242. package/tests/core/utilities/mocks/licenses.ts +0 -119
  243. package/tests/core/utilities/queue.ts +0 -9
  244. package/tests/core/utilities/structures/Chance.ts +0 -20
  245. package/tests/core/utilities/structures/accounts.ts +0 -80
  246. package/tests/core/utilities/structures/apps.ts +0 -21
  247. package/tests/core/utilities/structures/common.ts +0 -7
  248. package/tests/core/utilities/structures/db.ts +0 -12
  249. package/tests/core/utilities/structures/documents/index.ts +0 -1
  250. package/tests/core/utilities/structures/documents/platform/index.ts +0 -1
  251. package/tests/core/utilities/structures/documents/platform/installation.ts +0 -12
  252. package/tests/core/utilities/structures/generator.ts +0 -3
  253. package/tests/core/utilities/structures/index.ts +0 -15
  254. package/tests/core/utilities/structures/koa.ts +0 -16
  255. package/tests/core/utilities/structures/licenses.ts +0 -190
  256. package/tests/core/utilities/structures/plugins.ts +0 -19
  257. package/tests/core/utilities/structures/quotas.ts +0 -72
  258. package/tests/core/utilities/structures/scim.ts +0 -80
  259. package/tests/core/utilities/structures/sso.ts +0 -118
  260. package/tests/core/utilities/structures/tenants.ts +0 -5
  261. package/tests/core/utilities/structures/userGroups.ts +0 -10
  262. package/tests/core/utilities/structures/users.ts +0 -89
  263. package/tests/core/utilities/testContainerUtils.ts +0 -165
  264. package/tests/core/utilities/utils/index.ts +0 -2
  265. package/tests/core/utilities/utils/queue.ts +0 -27
  266. package/tests/core/utilities/utils/time.ts +0 -3
  267. package/tests/extra/DBTestConfiguration.ts +0 -36
  268. package/tests/extra/index.ts +0 -2
  269. package/tests/extra/testEnv.ts +0 -95
  270. package/tests/index.ts +0 -2
  271. package/tests/jestEnv.ts +0 -10
  272. package/tests/jestSetup.ts +0 -36
@@ -1,202 +0,0 @@
1
- import { publishEvent } from "../events"
2
- import {
3
- Event,
4
- User,
5
- UserCreatedEvent,
6
- UserDataCollaborationEvent,
7
- UserDeletedEvent,
8
- UserInviteAcceptedEvent,
9
- UserInvitedEvent,
10
- UserPasswordForceResetEvent,
11
- UserPasswordResetEvent,
12
- UserPasswordResetRequestedEvent,
13
- UserPasswordUpdatedEvent,
14
- UserPermissionAssignedEvent,
15
- UserPermissionRemovedEvent,
16
- UserUpdatedEvent,
17
- UserOnboardingEvent,
18
- } from "@budibase/types"
19
- import { isScim } from "../../context"
20
-
21
- async function created(user: User, timestamp?: number) {
22
- const properties: UserCreatedEvent = {
23
- userId: user._id as string,
24
- viaScim: isScim(),
25
- audited: {
26
- email: user.email,
27
- },
28
- }
29
- await publishEvent(Event.USER_CREATED, properties, timestamp)
30
- }
31
-
32
- async function updated(user: User) {
33
- const properties: UserUpdatedEvent = {
34
- userId: user._id as string,
35
- viaScim: isScim(),
36
- audited: {
37
- email: user.email,
38
- },
39
- }
40
- await publishEvent(Event.USER_UPDATED, properties)
41
- }
42
-
43
- async function deleted(user: User) {
44
- const properties: UserDeletedEvent = {
45
- userId: user._id as string,
46
- viaScim: isScim(),
47
- audited: {
48
- email: user.email,
49
- },
50
- }
51
- await publishEvent(Event.USER_DELETED, properties)
52
- }
53
-
54
- export async function onboardingComplete(user: User) {
55
- const properties: UserOnboardingEvent = {
56
- userId: user._id as string,
57
- audited: {
58
- email: user.email,
59
- },
60
- }
61
- await publishEvent(Event.USER_ONBOARDING_COMPLETE, properties)
62
- }
63
-
64
- // PERMISSIONS
65
-
66
- async function permissionAdminAssigned(user: User, timestamp?: number) {
67
- const properties: UserPermissionAssignedEvent = {
68
- userId: user._id as string,
69
- audited: {
70
- email: user.email,
71
- },
72
- }
73
- await publishEvent(
74
- Event.USER_PERMISSION_ADMIN_ASSIGNED,
75
- properties,
76
- timestamp
77
- )
78
- }
79
-
80
- async function permissionAdminRemoved(user: User) {
81
- const properties: UserPermissionRemovedEvent = {
82
- userId: user._id as string,
83
- audited: {
84
- email: user.email,
85
- },
86
- }
87
- await publishEvent(Event.USER_PERMISSION_ADMIN_REMOVED, properties)
88
- }
89
-
90
- async function permissionBuilderAssigned(user: User, timestamp?: number) {
91
- const properties: UserPermissionAssignedEvent = {
92
- userId: user._id as string,
93
- audited: {
94
- email: user.email,
95
- },
96
- }
97
- await publishEvent(
98
- Event.USER_PERMISSION_BUILDER_ASSIGNED,
99
- properties,
100
- timestamp
101
- )
102
- }
103
-
104
- async function permissionBuilderRemoved(user: User) {
105
- const properties: UserPermissionRemovedEvent = {
106
- userId: user._id as string,
107
- audited: {
108
- email: user.email,
109
- },
110
- }
111
- await publishEvent(Event.USER_PERMISSION_BUILDER_REMOVED, properties)
112
- }
113
-
114
- // INVITE
115
-
116
- async function invited(email: string) {
117
- const properties: UserInvitedEvent = {
118
- audited: {
119
- email,
120
- },
121
- }
122
- await publishEvent(Event.USER_INVITED, properties)
123
- }
124
-
125
- async function inviteAccepted(user: User) {
126
- const properties: UserInviteAcceptedEvent = {
127
- userId: user._id as string,
128
- audited: {
129
- email: user.email,
130
- },
131
- }
132
- await publishEvent(Event.USER_INVITED_ACCEPTED, properties)
133
- }
134
-
135
- // PASSWORD
136
-
137
- async function passwordForceReset(user: User) {
138
- const properties: UserPasswordForceResetEvent = {
139
- userId: user._id as string,
140
- audited: {
141
- email: user.email,
142
- },
143
- }
144
- await publishEvent(Event.USER_PASSWORD_FORCE_RESET, properties)
145
- }
146
-
147
- async function passwordUpdated(user: User) {
148
- const properties: UserPasswordUpdatedEvent = {
149
- userId: user._id as string,
150
- audited: {
151
- email: user.email,
152
- },
153
- }
154
- await publishEvent(Event.USER_PASSWORD_UPDATED, properties)
155
- }
156
-
157
- async function passwordResetRequested(user: User) {
158
- const properties: UserPasswordResetRequestedEvent = {
159
- userId: user._id as string,
160
- audited: {
161
- email: user.email,
162
- },
163
- }
164
- await publishEvent(Event.USER_PASSWORD_RESET_REQUESTED, properties)
165
- }
166
-
167
- async function passwordReset(user: User) {
168
- const properties: UserPasswordResetEvent = {
169
- userId: user._id as string,
170
- audited: {
171
- email: user.email,
172
- },
173
- }
174
- await publishEvent(Event.USER_PASSWORD_RESET, properties)
175
- }
176
-
177
- // COLLABORATION
178
-
179
- async function dataCollaboration(users: number) {
180
- const properties: UserDataCollaborationEvent = {
181
- users,
182
- }
183
- await publishEvent(Event.USER_DATA_COLLABORATION, properties)
184
- }
185
-
186
- export default {
187
- created,
188
- updated,
189
- deleted,
190
- permissionAdminAssigned,
191
- permissionAdminRemoved,
192
- permissionBuilderAssigned,
193
- permissionBuilderRemoved,
194
- onboardingComplete,
195
- invited,
196
- inviteAccepted,
197
- passwordForceReset,
198
- passwordUpdated,
199
- passwordResetRequested,
200
- passwordReset,
201
- dataCollaboration,
202
- }
@@ -1,107 +0,0 @@
1
- import { publishEvent } from "../events"
2
- import {
3
- Event,
4
- ViewCalculationCreatedEvent,
5
- ViewCalculationDeletedEvent,
6
- ViewCalculationUpdatedEvent,
7
- ViewCreatedEvent,
8
- ViewDeletedEvent,
9
- ViewExportedEvent,
10
- ViewFilterCreatedEvent,
11
- ViewFilterDeletedEvent,
12
- ViewFilterUpdatedEvent,
13
- ViewUpdatedEvent,
14
- View,
15
- ViewCalculation,
16
- Table,
17
- TableExportFormat,
18
- } from "@budibase/types"
19
-
20
- /* eslint-disable */
21
-
22
- async function created(view: View, timestamp?: string | number) {
23
- const properties: ViewCreatedEvent = {
24
- tableId: view.tableId,
25
- }
26
- await publishEvent(Event.VIEW_CREATED, properties, timestamp)
27
- }
28
-
29
- async function updated(view: View) {
30
- const properties: ViewUpdatedEvent = {
31
- tableId: view.tableId,
32
- }
33
- await publishEvent(Event.VIEW_UPDATED, properties)
34
- }
35
-
36
- async function deleted(view: View) {
37
- const properties: ViewDeletedEvent = {
38
- tableId: view.tableId,
39
- }
40
- await publishEvent(Event.VIEW_DELETED, properties)
41
- }
42
-
43
- async function exported(table: Table, format: TableExportFormat) {
44
- const properties: ViewExportedEvent = {
45
- tableId: table._id as string,
46
- format,
47
- }
48
- await publishEvent(Event.VIEW_EXPORTED, properties)
49
- }
50
-
51
- async function filterCreated(view: View, timestamp?: string | number) {
52
- const properties: ViewFilterCreatedEvent = {
53
- tableId: view.tableId,
54
- }
55
- await publishEvent(Event.VIEW_FILTER_CREATED, properties, timestamp)
56
- }
57
-
58
- async function filterUpdated(view: View) {
59
- const properties: ViewFilterUpdatedEvent = {
60
- tableId: view.tableId,
61
- }
62
- await publishEvent(Event.VIEW_FILTER_UPDATED, properties)
63
- }
64
-
65
- async function filterDeleted(view: View) {
66
- const properties: ViewFilterDeletedEvent = {
67
- tableId: view.tableId,
68
- }
69
- await publishEvent(Event.VIEW_FILTER_DELETED, properties)
70
- }
71
-
72
- async function calculationCreated(view: View, timestamp?: string | number) {
73
- const properties: ViewCalculationCreatedEvent = {
74
- tableId: view.tableId,
75
- calculation: view.calculation as ViewCalculation,
76
- }
77
- await publishEvent(Event.VIEW_CALCULATION_CREATED, properties, timestamp)
78
- }
79
-
80
- async function calculationUpdated(view: View) {
81
- const properties: ViewCalculationUpdatedEvent = {
82
- tableId: view.tableId,
83
- calculation: view.calculation as ViewCalculation,
84
- }
85
- await publishEvent(Event.VIEW_CALCULATION_UPDATED, properties)
86
- }
87
-
88
- async function calculationDeleted(existingView: View) {
89
- const properties: ViewCalculationDeletedEvent = {
90
- tableId: existingView.tableId,
91
- calculation: existingView.calculation as ViewCalculation,
92
- }
93
- await publishEvent(Event.VIEW_CALCULATION_DELETED, properties)
94
- }
95
-
96
- export default {
97
- created,
98
- updated,
99
- deleted,
100
- exported,
101
- filterCreated,
102
- filterUpdated,
103
- filterDeleted,
104
- calculationCreated,
105
- calculationUpdated,
106
- calculationDeleted,
107
- }
@@ -1,277 +0,0 @@
1
- import env from "../environment"
2
- import * as crypto from "crypto"
3
- import * as context from "../context"
4
- import { PostHog, PostHogOptions } from "posthog-node"
5
- import { FeatureFlag } from "@budibase/types"
6
- import tracer from "dd-trace"
7
- import { Duration } from "../utils"
8
-
9
- let posthog: PostHog | undefined
10
- export function init(opts?: PostHogOptions) {
11
- if (
12
- env.POSTHOG_TOKEN &&
13
- env.POSTHOG_API_HOST &&
14
- !env.SELF_HOSTED &&
15
- env.POSTHOG_FEATURE_FLAGS_ENABLED
16
- ) {
17
- console.log("initializing posthog client...")
18
- posthog = new PostHog(env.POSTHOG_TOKEN, {
19
- host: env.POSTHOG_API_HOST,
20
- personalApiKey: env.POSTHOG_PERSONAL_TOKEN,
21
- featureFlagsPollingInterval: Duration.fromMinutes(3).toMs(),
22
- ...opts,
23
- })
24
- } else {
25
- console.log("posthog disabled")
26
- }
27
- }
28
-
29
- export function shutdown() {
30
- posthog?.shutdown()
31
- }
32
-
33
- export abstract class Flag<T> {
34
- static boolean(defaultValue: boolean): Flag<boolean> {
35
- return new BooleanFlag(defaultValue)
36
- }
37
-
38
- static string(defaultValue: string): Flag<string> {
39
- return new StringFlag(defaultValue)
40
- }
41
-
42
- static number(defaultValue: number): Flag<number> {
43
- return new NumberFlag(defaultValue)
44
- }
45
-
46
- protected constructor(public defaultValue: T) {}
47
-
48
- abstract parse(value: any): T
49
- }
50
-
51
- type UnwrapFlag<F> = F extends Flag<infer U> ? U : never
52
-
53
- export type FlagValues<T> = {
54
- [K in keyof T]: UnwrapFlag<T[K]>
55
- }
56
-
57
- type KeysOfType<T, U> = {
58
- [K in keyof T]: T[K] extends Flag<U> ? K : never
59
- }[keyof T]
60
-
61
- class BooleanFlag extends Flag<boolean> {
62
- parse(value: any) {
63
- if (typeof value === "string") {
64
- return ["true", "t", "1"].includes(value.toLowerCase())
65
- }
66
-
67
- if (typeof value === "boolean") {
68
- return value
69
- }
70
-
71
- throw new Error(`could not parse value "${value}" as boolean`)
72
- }
73
- }
74
-
75
- class StringFlag extends Flag<string> {
76
- parse(value: any) {
77
- if (typeof value === "string") {
78
- return value
79
- }
80
- throw new Error(`could not parse value "${value}" as string`)
81
- }
82
- }
83
-
84
- class NumberFlag extends Flag<number> {
85
- parse(value: any) {
86
- if (typeof value === "number") {
87
- return value
88
- }
89
-
90
- if (typeof value === "string") {
91
- const parsed = parseFloat(value)
92
- if (!isNaN(parsed)) {
93
- return parsed
94
- }
95
- }
96
-
97
- throw new Error(`could not parse value "${value}" as number`)
98
- }
99
- }
100
-
101
- export interface EnvFlagEntry {
102
- tenantId: string
103
- key: string
104
- value: boolean
105
- }
106
-
107
- export function parseEnvFlags(flags: string): EnvFlagEntry[] {
108
- const split = flags.split(",").map(x => x.split(":"))
109
- const result: EnvFlagEntry[] = []
110
- for (const [tenantId, ...features] of split) {
111
- for (let feature of features) {
112
- let value = true
113
- if (feature.startsWith("!")) {
114
- feature = feature.slice(1)
115
- value = false
116
- }
117
- result.push({ tenantId, key: feature, value })
118
- }
119
- }
120
- return result
121
- }
122
-
123
- export class FlagSet<V extends Flag<any>, T extends { [key: string]: V }> {
124
- // This is used to safely cache flags sets in the current request context.
125
- // Because multiple sets could theoretically exist, we don't want the cache of
126
- // one to leak into another.
127
- private readonly setId: string
128
-
129
- constructor(private readonly flagSchema: T) {
130
- this.setId = crypto.randomUUID()
131
- }
132
-
133
- defaults(): FlagValues<T> {
134
- return Object.keys(this.flagSchema).reduce((acc, key) => {
135
- const typedKey = key as keyof T
136
- acc[typedKey] = this.flagSchema[key].defaultValue
137
- return acc
138
- }, {} as FlagValues<T>)
139
- }
140
-
141
- isFlagName(name: string | number | symbol): name is keyof T {
142
- return this.flagSchema[name as keyof T] !== undefined
143
- }
144
-
145
- async get<K extends keyof T>(key: K): Promise<FlagValues<T>[K]> {
146
- const flags = await this.fetch()
147
- return flags[key]
148
- }
149
-
150
- async isEnabled<K extends KeysOfType<T, boolean>>(key: K): Promise<boolean> {
151
- const flags = await this.fetch()
152
- return flags[key]
153
- }
154
-
155
- async fetch(): Promise<FlagValues<T>> {
156
- return await tracer.trace("features.fetch", async span => {
157
- const cachedFlags = context.getFeatureFlags<FlagValues<T>>(this.setId)
158
- if (cachedFlags) {
159
- span?.addTags({ fromCache: true })
160
- return cachedFlags
161
- }
162
-
163
- const tags: Record<string, any> = {}
164
- const flagValues = this.defaults()
165
- const currentTenantId = context.getTenantId()
166
- const specificallySetFalse = new Set<string>()
167
-
168
- for (const { tenantId, key, value } of parseEnvFlags(
169
- env.TENANT_FEATURE_FLAGS || ""
170
- )) {
171
- if (!tenantId || (tenantId !== "*" && tenantId !== currentTenantId)) {
172
- continue
173
- }
174
-
175
- tags[`readFromEnvironmentVars`] = true
176
-
177
- if (value === false) {
178
- specificallySetFalse.add(key)
179
- }
180
-
181
- // ignore unknown flags
182
- if (!this.isFlagName(key)) {
183
- continue
184
- }
185
-
186
- if (typeof flagValues[key] !== "boolean") {
187
- throw new Error(`Feature: ${key} is not a boolean`)
188
- }
189
-
190
- // @ts-expect-error - TS does not like you writing into a generic type,
191
- // but we know that it's okay in this case because it's just an object.
192
- flagValues[key as keyof FlagValues] = value
193
- tags[`flags.${key}.source`] = "environment"
194
- }
195
-
196
- const identity = context.getIdentity()
197
-
198
- let userId = identity?._id
199
- if (!userId) {
200
- const ip = context.getIP()
201
- if (ip) {
202
- userId = crypto.createHash("sha512").update(ip).digest("hex")
203
- }
204
- }
205
-
206
- let tenantId = identity?.tenantId
207
- if (!tenantId) {
208
- tenantId = currentTenantId
209
- }
210
-
211
- tags[`identity.type`] = identity?.type
212
- tags[`identity._id`] = identity?._id
213
- tags[`tenantId`] = tenantId
214
- tags[`userId`] = userId
215
-
216
- if (posthog && userId) {
217
- tags[`readFromPostHog`] = true
218
-
219
- const personProperties: Record<string, string> = { tenantId }
220
- const posthogFlags = await posthog.getAllFlagsAndPayloads(userId, {
221
- personProperties,
222
- })
223
-
224
- for (const [name, value] of Object.entries(posthogFlags.featureFlags)) {
225
- if (!this.isFlagName(name)) {
226
- // We don't want an unexpected PostHog flag to break the app, so we
227
- // just log it and continue.
228
- console.warn(`Unexpected posthog flag "${name}": ${value}`)
229
- continue
230
- }
231
-
232
- if (flagValues[name] === true || specificallySetFalse.has(name)) {
233
- // If the flag is already set to through environment variables, we
234
- // don't want to override it back to false here.
235
- continue
236
- }
237
-
238
- const payload = posthogFlags.featureFlagPayloads?.[name]
239
- const flag = this.flagSchema[name]
240
- try {
241
- // @ts-expect-error - TS does not like you writing into a generic
242
- // type, but we know that it's okay in this case because it's just
243
- // an object.
244
- flagValues[name] = flag.parse(payload || value)
245
- tags[`flags.${name}.source`] = "posthog"
246
- } catch (err) {
247
- // We don't want an invalid PostHog flag to break the app, so we just
248
- // log it and continue.
249
- console.warn(`Error parsing posthog flag "${name}": ${value}`, err)
250
- }
251
- }
252
- }
253
-
254
- context.setFeatureFlags(this.setId, flagValues)
255
- for (const [key, value] of Object.entries(flagValues)) {
256
- tags[`flags.${key}.value`] = value
257
- }
258
- span?.addTags(tags)
259
-
260
- return flagValues
261
- })
262
- }
263
- }
264
-
265
- // This is the primary source of truth for feature flags. If you want to add a
266
- // new flag, add it here and use the `fetch` and `get` functions to access it.
267
- // All of the machinery in this file is to make sure that flags have their
268
- // default values set correctly and their types flow through the system.
269
- export const flags = new FlagSet({
270
- [FeatureFlag.DEFAULT_VALUES]: Flag.boolean(true),
271
- [FeatureFlag.AUTOMATION_BRANCHING]: Flag.boolean(true),
272
- [FeatureFlag.AI_CUSTOM_CONFIGS]: Flag.boolean(true),
273
- [FeatureFlag.BUDIBASE_AI]: Flag.boolean(true),
274
- })
275
-
276
- type UnwrapPromise<T> = T extends Promise<infer U> ? U : T
277
- export type FeatureFlags = UnwrapPromise<ReturnType<typeof flags.fetch>>
@@ -1,2 +0,0 @@
1
- export * from "./features"
2
- export * as testutils from "./tests/utils"