@budibase/backend-core 2.9.19 → 2.9.21-alpha.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 (250) hide show
  1. package/dist/index.js +266 -324
  2. package/dist/index.js.map +4 -4
  3. package/dist/index.js.meta.json +1 -1
  4. package/dist/package.json +19 -4
  5. package/dist/plugins.js +1 -1
  6. package/dist/plugins.js.map +1 -1
  7. package/dist/plugins.js.meta.json +1 -1
  8. package/dist/src/security/permissions.d.ts +1 -1
  9. package/dist/tests.js +222 -260
  10. package/dist/tests.js.map +4 -4
  11. package/dist/tests.js.meta.json +1 -1
  12. package/package.json +19 -4
  13. package/dist/tsconfig.build.tsbuildinfo +0 -1
  14. package/src/accounts/accounts.ts +0 -82
  15. package/src/accounts/api.ts +0 -59
  16. package/src/accounts/index.ts +0 -1
  17. package/src/auth/auth.ts +0 -208
  18. package/src/auth/index.ts +0 -1
  19. package/src/auth/tests/auth.spec.ts +0 -14
  20. package/src/blacklist/blacklist.ts +0 -54
  21. package/src/blacklist/index.ts +0 -1
  22. package/src/blacklist/tests/blacklist.spec.ts +0 -46
  23. package/src/cache/appMetadata.ts +0 -88
  24. package/src/cache/base/index.ts +0 -92
  25. package/src/cache/generic.ts +0 -30
  26. package/src/cache/index.ts +0 -5
  27. package/src/cache/tests/writethrough.spec.ts +0 -138
  28. package/src/cache/user.ts +0 -69
  29. package/src/cache/writethrough.ts +0 -133
  30. package/src/configs/configs.ts +0 -257
  31. package/src/configs/index.ts +0 -1
  32. package/src/configs/tests/configs.spec.ts +0 -184
  33. package/src/constants/db.ts +0 -63
  34. package/src/constants/index.ts +0 -2
  35. package/src/constants/misc.ts +0 -50
  36. package/src/context/Context.ts +0 -14
  37. package/src/context/identity.ts +0 -58
  38. package/src/context/index.ts +0 -3
  39. package/src/context/mainContext.ts +0 -310
  40. package/src/context/tests/index.spec.ts +0 -147
  41. package/src/context/types.ts +0 -11
  42. package/src/db/Replication.ts +0 -84
  43. package/src/db/constants.ts +0 -10
  44. package/src/db/couch/DatabaseImpl.ts +0 -238
  45. package/src/db/couch/connections.ts +0 -77
  46. package/src/db/couch/index.ts +0 -5
  47. package/src/db/couch/pouchDB.ts +0 -97
  48. package/src/db/couch/pouchDump.ts +0 -0
  49. package/src/db/couch/utils.ts +0 -50
  50. package/src/db/db.ts +0 -39
  51. package/src/db/errors.ts +0 -14
  52. package/src/db/index.ts +0 -12
  53. package/src/db/lucene.ts +0 -732
  54. package/src/db/searchIndexes/index.ts +0 -1
  55. package/src/db/searchIndexes/searchIndexes.ts +0 -62
  56. package/src/db/tests/index.spec.js +0 -25
  57. package/src/db/tests/lucene.spec.ts +0 -298
  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 -207
  61. package/src/db/views.ts +0 -241
  62. package/src/docIds/conversions.ts +0 -59
  63. package/src/docIds/ids.ts +0 -113
  64. package/src/docIds/index.ts +0 -2
  65. package/src/docIds/newid.ts +0 -5
  66. package/src/docIds/params.ts +0 -174
  67. package/src/docUpdates/index.ts +0 -29
  68. package/src/environment.ts +0 -201
  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 -40
  78. package/src/events/identification.ts +0 -310
  79. package/src/events/index.ts +0 -14
  80. package/src/events/processors/AnalyticsProcessor.ts +0 -64
  81. package/src/events/processors/AuditLogsProcessor.ts +0 -93
  82. package/src/events/processors/LoggingProcessor.ts +0 -37
  83. package/src/events/processors/Processors.ts +0 -52
  84. package/src/events/processors/async/DocumentUpdateProcessor.ts +0 -43
  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 -2
  88. package/src/events/processors/posthog/rateLimiting.ts +0 -106
  89. package/src/events/processors/posthog/tests/PosthogProcessor.spec.ts +0 -168
  90. package/src/events/processors/types.ts +0 -1
  91. package/src/events/publishers/account.ts +0 -35
  92. package/src/events/publishers/app.ts +0 -155
  93. package/src/events/publishers/auditLog.ts +0 -26
  94. package/src/events/publishers/auth.ts +0 -73
  95. package/src/events/publishers/automation.ts +0 -110
  96. package/src/events/publishers/backfill.ts +0 -74
  97. package/src/events/publishers/backup.ts +0 -42
  98. package/src/events/publishers/datasource.ts +0 -48
  99. package/src/events/publishers/email.ts +0 -17
  100. package/src/events/publishers/environmentVariable.ts +0 -38
  101. package/src/events/publishers/group.ts +0 -99
  102. package/src/events/publishers/index.ts +0 -24
  103. package/src/events/publishers/installation.ts +0 -38
  104. package/src/events/publishers/layout.ts +0 -26
  105. package/src/events/publishers/license.ts +0 -84
  106. package/src/events/publishers/org.ts +0 -37
  107. package/src/events/publishers/plugin.ts +0 -47
  108. package/src/events/publishers/query.ts +0 -88
  109. package/src/events/publishers/role.ts +0 -62
  110. package/src/events/publishers/rows.ts +0 -29
  111. package/src/events/publishers/screen.ts +0 -36
  112. package/src/events/publishers/serve.ts +0 -43
  113. package/src/events/publishers/table.ts +0 -70
  114. package/src/events/publishers/user.ts +0 -202
  115. package/src/events/publishers/view.ts +0 -107
  116. package/src/featureFlags/index.ts +0 -77
  117. package/src/featureFlags/tests/featureFlags.spec.ts +0 -85
  118. package/src/helpers.ts +0 -9
  119. package/src/index.ts +0 -53
  120. package/src/installation.ts +0 -107
  121. package/src/logging/alerts.ts +0 -26
  122. package/src/logging/correlation/correlation.ts +0 -13
  123. package/src/logging/correlation/index.ts +0 -1
  124. package/src/logging/correlation/middleware.ts +0 -17
  125. package/src/logging/index.ts +0 -4
  126. package/src/logging/pino/logger.ts +0 -232
  127. package/src/logging/pino/middleware.ts +0 -45
  128. package/src/logging/system.ts +0 -81
  129. package/src/logging/tests/system.spec.ts +0 -61
  130. package/src/middleware/adminOnly.ts +0 -9
  131. package/src/middleware/auditLog.ts +0 -6
  132. package/src/middleware/authenticated.ts +0 -193
  133. package/src/middleware/builderOnly.ts +0 -20
  134. package/src/middleware/builderOrAdmin.ts +0 -20
  135. package/src/middleware/csrf.ts +0 -81
  136. package/src/middleware/errorHandling.ts +0 -29
  137. package/src/middleware/index.ts +0 -21
  138. package/src/middleware/internalApi.ts +0 -23
  139. package/src/middleware/joi-validator.ts +0 -45
  140. package/src/middleware/matchers.ts +0 -47
  141. package/src/middleware/passport/datasource/google.ts +0 -95
  142. package/src/middleware/passport/local.ts +0 -54
  143. package/src/middleware/passport/sso/google.ts +0 -77
  144. package/src/middleware/passport/sso/oidc.ts +0 -154
  145. package/src/middleware/passport/sso/sso.ts +0 -165
  146. package/src/middleware/passport/sso/tests/google.spec.ts +0 -67
  147. package/src/middleware/passport/sso/tests/oidc.spec.ts +0 -152
  148. package/src/middleware/passport/sso/tests/sso.spec.ts +0 -197
  149. package/src/middleware/passport/utils.ts +0 -38
  150. package/src/middleware/querystringToBody.ts +0 -28
  151. package/src/middleware/tenancy.ts +0 -36
  152. package/src/middleware/tests/builder.spec.ts +0 -180
  153. package/src/middleware/tests/matchers.spec.ts +0 -134
  154. package/src/migrations/definitions.ts +0 -40
  155. package/src/migrations/index.ts +0 -2
  156. package/src/migrations/migrations.ts +0 -191
  157. package/src/migrations/tests/__snapshots__/migrations.spec.ts.snap +0 -11
  158. package/src/migrations/tests/migrations.spec.ts +0 -64
  159. package/src/objectStore/buckets/app.ts +0 -40
  160. package/src/objectStore/buckets/global.ts +0 -29
  161. package/src/objectStore/buckets/index.ts +0 -3
  162. package/src/objectStore/buckets/plugins.ts +0 -71
  163. package/src/objectStore/buckets/tests/app.spec.ts +0 -171
  164. package/src/objectStore/buckets/tests/global.spec.ts +0 -74
  165. package/src/objectStore/buckets/tests/plugins.spec.ts +0 -111
  166. package/src/objectStore/cloudfront.ts +0 -41
  167. package/src/objectStore/index.ts +0 -3
  168. package/src/objectStore/objectStore.ts +0 -440
  169. package/src/objectStore/utils.ts +0 -27
  170. package/src/platform/index.ts +0 -3
  171. package/src/platform/platformDb.ts +0 -6
  172. package/src/platform/tenants.ts +0 -101
  173. package/src/platform/tests/tenants.spec.ts +0 -26
  174. package/src/platform/users.ts +0 -90
  175. package/src/plugin/index.ts +0 -1
  176. package/src/plugin/tests/validation.spec.ts +0 -83
  177. package/src/plugin/utils.ts +0 -156
  178. package/src/queue/constants.ts +0 -6
  179. package/src/queue/inMemoryQueue.ts +0 -141
  180. package/src/queue/index.ts +0 -2
  181. package/src/queue/listeners.ts +0 -195
  182. package/src/queue/queue.ts +0 -54
  183. package/src/redis/index.ts +0 -6
  184. package/src/redis/init.ts +0 -86
  185. package/src/redis/redis.ts +0 -308
  186. package/src/redis/redlockImpl.ts +0 -139
  187. package/src/redis/utils.ts +0 -117
  188. package/src/security/encryption.ts +0 -179
  189. package/src/security/permissions.ts +0 -159
  190. package/src/security/roles.ts +0 -420
  191. package/src/security/sessions.ts +0 -120
  192. package/src/security/tests/encryption.spec.ts +0 -31
  193. package/src/security/tests/permissions.spec.ts +0 -145
  194. package/src/security/tests/sessions.spec.ts +0 -12
  195. package/src/tenancy/db.ts +0 -6
  196. package/src/tenancy/index.ts +0 -2
  197. package/src/tenancy/tenancy.ts +0 -140
  198. package/src/tenancy/tests/tenancy.spec.ts +0 -184
  199. package/src/timers/index.ts +0 -1
  200. package/src/timers/timers.ts +0 -22
  201. package/src/users/db.ts +0 -460
  202. package/src/users/events.ts +0 -176
  203. package/src/users/index.ts +0 -4
  204. package/src/users/lookup.ts +0 -102
  205. package/src/users/users.ts +0 -276
  206. package/src/users/utils.ts +0 -55
  207. package/src/utils/hashing.ts +0 -14
  208. package/src/utils/index.ts +0 -3
  209. package/src/utils/stringUtils.ts +0 -8
  210. package/src/utils/tests/utils.spec.ts +0 -191
  211. package/src/utils/utils.ts +0 -239
  212. package/tests/core/logging.ts +0 -34
  213. package/tests/core/utilities/index.ts +0 -6
  214. package/tests/core/utilities/jestUtils.ts +0 -30
  215. package/tests/core/utilities/mocks/alerts.ts +0 -3
  216. package/tests/core/utilities/mocks/date.ts +0 -2
  217. package/tests/core/utilities/mocks/events.ts +0 -131
  218. package/tests/core/utilities/mocks/fetch.ts +0 -17
  219. package/tests/core/utilities/mocks/index.ts +0 -10
  220. package/tests/core/utilities/mocks/licenses.ts +0 -107
  221. package/tests/core/utilities/mocks/posthog.ts +0 -7
  222. package/tests/core/utilities/structures/Chance.ts +0 -20
  223. package/tests/core/utilities/structures/accounts.ts +0 -115
  224. package/tests/core/utilities/structures/apps.ts +0 -21
  225. package/tests/core/utilities/structures/common.ts +0 -7
  226. package/tests/core/utilities/structures/db.ts +0 -12
  227. package/tests/core/utilities/structures/documents/index.ts +0 -1
  228. package/tests/core/utilities/structures/documents/platform/index.ts +0 -1
  229. package/tests/core/utilities/structures/documents/platform/installation.ts +0 -12
  230. package/tests/core/utilities/structures/generator.ts +0 -2
  231. package/tests/core/utilities/structures/index.ts +0 -15
  232. package/tests/core/utilities/structures/koa.ts +0 -16
  233. package/tests/core/utilities/structures/licenses.ts +0 -167
  234. package/tests/core/utilities/structures/plugins.ts +0 -19
  235. package/tests/core/utilities/structures/quotas.ts +0 -67
  236. package/tests/core/utilities/structures/scim.ts +0 -80
  237. package/tests/core/utilities/structures/shared.ts +0 -19
  238. package/tests/core/utilities/structures/sso.ts +0 -119
  239. package/tests/core/utilities/structures/tenants.ts +0 -5
  240. package/tests/core/utilities/structures/userGroups.ts +0 -10
  241. package/tests/core/utilities/structures/users.ts +0 -73
  242. package/tests/core/utilities/testContainerUtils.ts +0 -98
  243. package/tests/core/utilities/utils/index.ts +0 -1
  244. package/tests/core/utilities/utils/time.ts +0 -3
  245. package/tests/extra/DBTestConfiguration.ts +0 -36
  246. package/tests/extra/index.ts +0 -2
  247. package/tests/extra/testEnv.ts +0 -95
  248. package/tests/index.ts +0 -1
  249. package/tests/jestEnv.ts +0 -6
  250. package/tests/jestSetup.ts +0 -28
@@ -1,183 +0,0 @@
1
- import {
2
- Event,
3
- BackfillMetadata,
4
- CachedEvent,
5
- SSOCreatedEvent,
6
- AutomationCreatedEvent,
7
- AutomationStepCreatedEvent,
8
- DatasourceCreatedEvent,
9
- LayoutCreatedEvent,
10
- QueryCreatedEvent,
11
- RoleCreatedEvent,
12
- ScreenCreatedEvent,
13
- TableCreatedEvent,
14
- ViewCreatedEvent,
15
- ViewCalculationCreatedEvent,
16
- ViewFilterCreatedEvent,
17
- AppPublishedEvent,
18
- UserCreatedEvent,
19
- RoleAssignedEvent,
20
- UserPermissionAssignedEvent,
21
- AppCreatedEvent,
22
- } from "@budibase/types"
23
- import * as context from "../context"
24
- import { CacheKey } from "../cache/generic"
25
- import * as cache from "../cache/generic"
26
-
27
- // LIFECYCLE
28
-
29
- export const start = async (events: Event[]) => {
30
- const metadata: BackfillMetadata = {
31
- eventWhitelist: events,
32
- }
33
- return saveBackfillMetadata(metadata)
34
- }
35
-
36
- export const recordEvent = async (event: Event, properties: any) => {
37
- const eventKey = getEventKey(event, properties)
38
- // don't use a ttl - cleaned up by migration
39
- // don't use tenancy - already in the key
40
- await cache.store(eventKey, properties, undefined, { useTenancy: false })
41
- }
42
-
43
- export const end = async () => {
44
- await deleteBackfillMetadata()
45
- await clearEvents()
46
- }
47
-
48
- // CRUD
49
-
50
- const getBackfillMetadata = async (): Promise<BackfillMetadata | null> => {
51
- return cache.get(CacheKey.BACKFILL_METADATA)
52
- }
53
-
54
- const saveBackfillMetadata = async (
55
- backfill: BackfillMetadata
56
- ): Promise<void> => {
57
- // no TTL - deleted by backfill
58
- return cache.store(CacheKey.BACKFILL_METADATA, backfill)
59
- }
60
-
61
- const deleteBackfillMetadata = async (): Promise<void> => {
62
- await cache.destroy(CacheKey.BACKFILL_METADATA)
63
- }
64
-
65
- const clearEvents = async () => {
66
- // wildcard
67
- const pattern = getEventKey()
68
- const keys = await cache.keys(pattern)
69
-
70
- for (const key of keys) {
71
- // delete each key
72
- // don't use tenancy, already in the key
73
- await cache.destroy(key, { useTenancy: false })
74
- }
75
- }
76
-
77
- // HELPERS
78
-
79
- export const isBackfillingEvent = async (event: Event) => {
80
- const backfill = await getBackfillMetadata()
81
- const events = backfill?.eventWhitelist
82
- if (events && events.includes(event)) {
83
- return true
84
- } else {
85
- return false
86
- }
87
- }
88
-
89
- export const isAlreadySent = async (event: Event, properties: any) => {
90
- const eventKey = getEventKey(event, properties)
91
- const cachedEvent: CachedEvent = await cache.get(eventKey, {
92
- useTenancy: false,
93
- })
94
- return !!cachedEvent
95
- }
96
-
97
- const CUSTOM_PROPERTY_SUFFIX: any = {
98
- // APP EVENTS
99
- [Event.AUTOMATION_CREATED]: (properties: AutomationCreatedEvent) => {
100
- return properties.automationId
101
- },
102
- [Event.AUTOMATION_STEP_CREATED]: (properties: AutomationStepCreatedEvent) => {
103
- return properties.stepId
104
- },
105
- [Event.DATASOURCE_CREATED]: (properties: DatasourceCreatedEvent) => {
106
- return properties.datasourceId
107
- },
108
- [Event.LAYOUT_CREATED]: (properties: LayoutCreatedEvent) => {
109
- return properties.layoutId
110
- },
111
- [Event.QUERY_CREATED]: (properties: QueryCreatedEvent) => {
112
- return properties.queryId
113
- },
114
- [Event.ROLE_CREATED]: (properties: RoleCreatedEvent) => {
115
- return properties.roleId
116
- },
117
- [Event.SCREEN_CREATED]: (properties: ScreenCreatedEvent) => {
118
- return properties.screenId
119
- },
120
- [Event.TABLE_CREATED]: (properties: TableCreatedEvent) => {
121
- return properties.tableId
122
- },
123
- [Event.VIEW_CREATED]: (properties: ViewCreatedEvent) => {
124
- return properties.tableId // best uniqueness
125
- },
126
- [Event.VIEW_CALCULATION_CREATED]: (
127
- properties: ViewCalculationCreatedEvent
128
- ) => {
129
- return properties.tableId // best uniqueness
130
- },
131
- [Event.VIEW_FILTER_CREATED]: (properties: ViewFilterCreatedEvent) => {
132
- return properties.tableId // best uniqueness
133
- },
134
- [Event.APP_CREATED]: (properties: AppCreatedEvent) => {
135
- return properties.appId // best uniqueness
136
- },
137
- [Event.APP_PUBLISHED]: (properties: AppPublishedEvent) => {
138
- return properties.appId // best uniqueness
139
- },
140
- // GLOBAL EVENTS
141
- [Event.AUTH_SSO_CREATED]: (properties: SSOCreatedEvent) => {
142
- return properties.type
143
- },
144
- [Event.AUTH_SSO_ACTIVATED]: (properties: SSOCreatedEvent) => {
145
- return properties.type
146
- },
147
- [Event.USER_CREATED]: (properties: UserCreatedEvent) => {
148
- return properties.userId
149
- },
150
- [Event.USER_PERMISSION_ADMIN_ASSIGNED]: (
151
- properties: UserPermissionAssignedEvent
152
- ) => {
153
- return properties.userId
154
- },
155
- [Event.USER_PERMISSION_BUILDER_ASSIGNED]: (
156
- properties: UserPermissionAssignedEvent
157
- ) => {
158
- return properties.userId
159
- },
160
- [Event.ROLE_ASSIGNED]: (properties: RoleAssignedEvent) => {
161
- return `${properties.roleId}-${properties.userId}`
162
- },
163
- }
164
-
165
- const getEventKey = (event?: Event, properties?: any) => {
166
- let eventKey: string
167
-
168
- const tenantId = context.getTenantId()
169
- if (event) {
170
- eventKey = `${CacheKey.EVENTS}:${tenantId}:${event}`
171
-
172
- // use some properties to make the key more unique
173
- const custom = CUSTOM_PROPERTY_SUFFIX[event]
174
- const suffix = custom ? custom(properties) : undefined
175
- if (suffix) {
176
- eventKey = `${eventKey}:${suffix}`
177
- }
178
- } else {
179
- eventKey = `${CacheKey.EVENTS}:${tenantId}:*`
180
- }
181
-
182
- return eventKey
183
- }
@@ -1,56 +0,0 @@
1
- import {
2
- Event,
3
- UserCreatedEvent,
4
- UserUpdatedEvent,
5
- UserDeletedEvent,
6
- UserPermissionAssignedEvent,
7
- UserPermissionRemovedEvent,
8
- GroupCreatedEvent,
9
- GroupUpdatedEvent,
10
- GroupDeletedEvent,
11
- GroupUsersAddedEvent,
12
- GroupUsersDeletedEvent,
13
- GroupPermissionsEditedEvent,
14
- } from "@budibase/types"
15
-
16
- const getEventProperties: Record<
17
- string,
18
- (properties: any) => string | undefined
19
- > = {
20
- [Event.USER_CREATED]: (properties: UserCreatedEvent) => properties.userId,
21
- [Event.USER_UPDATED]: (properties: UserUpdatedEvent) => properties.userId,
22
- [Event.USER_DELETED]: (properties: UserDeletedEvent) => properties.userId,
23
- [Event.USER_PERMISSION_ADMIN_ASSIGNED]: (
24
- properties: UserPermissionAssignedEvent
25
- ) => properties.userId,
26
- [Event.USER_PERMISSION_ADMIN_REMOVED]: (
27
- properties: UserPermissionRemovedEvent
28
- ) => properties.userId,
29
- [Event.USER_PERMISSION_BUILDER_ASSIGNED]: (
30
- properties: UserPermissionAssignedEvent
31
- ) => properties.userId,
32
- [Event.USER_PERMISSION_BUILDER_REMOVED]: (
33
- properties: UserPermissionRemovedEvent
34
- ) => properties.userId,
35
- [Event.USER_GROUP_CREATED]: (properties: GroupCreatedEvent) =>
36
- properties.groupId,
37
- [Event.USER_GROUP_UPDATED]: (properties: GroupUpdatedEvent) =>
38
- properties.groupId,
39
- [Event.USER_GROUP_DELETED]: (properties: GroupDeletedEvent) =>
40
- properties.groupId,
41
- [Event.USER_GROUP_USERS_ADDED]: (properties: GroupUsersAddedEvent) =>
42
- properties.groupId,
43
- [Event.USER_GROUP_USERS_REMOVED]: (properties: GroupUsersDeletedEvent) =>
44
- properties.groupId,
45
- [Event.USER_GROUP_PERMISSIONS_EDITED]: (
46
- properties: GroupPermissionsEditedEvent
47
- ) => properties.groupId,
48
- }
49
-
50
- export function getDocumentId(event: Event, properties: any) {
51
- const extractor = getEventProperties[event]
52
- if (!extractor) {
53
- throw new Error("Event does not have a method of document ID extraction")
54
- }
55
- return extractor(properties)
56
- }
@@ -1,40 +0,0 @@
1
- import { Event } from "@budibase/types"
2
- import { processors } from "./processors"
3
- import identification from "./identification"
4
- import * as backfill from "./backfill"
5
- import { publishAsyncEvent } from "./asyncEvents"
6
-
7
- export const publishEvent = async (
8
- event: Event,
9
- properties: any,
10
- timestamp?: string | number
11
- ) => {
12
- // in future this should use async events via a distributed queue.
13
- const identity = await identification.getCurrentIdentity()
14
-
15
- const backfilling = await backfill.isBackfillingEvent(event)
16
- // no backfill - send the event and exit
17
- if (!backfilling) {
18
- // send off async events if required
19
- await publishAsyncEvent({
20
- event,
21
- identity,
22
- properties,
23
- timestamp,
24
- })
25
- // now handle the main sync event processing pipeline
26
- await processors.processEvent(event, identity, properties, timestamp)
27
- return
28
- }
29
-
30
- // backfill active - check if the event has been sent already
31
- const alreadySent = await backfill.isAlreadySent(event, properties)
32
- if (alreadySent) {
33
- // do nothing
34
- return
35
- } else {
36
- // send and record the event
37
- await processors.processEvent(event, identity, properties, timestamp)
38
- await backfill.recordEvent(event, properties)
39
- }
40
- }
@@ -1,310 +0,0 @@
1
- import * as context from "../context"
2
- import * as identityCtx from "../context/identity"
3
- import env from "../environment"
4
- import {
5
- Hosting,
6
- User,
7
- Identity,
8
- IdentityType,
9
- Account,
10
- isCloudAccount,
11
- isSSOAccount,
12
- TenantGroup,
13
- CloudAccount,
14
- UserIdentity,
15
- InstallationGroup,
16
- UserContext,
17
- Group,
18
- isSSOUser,
19
- } from "@budibase/types"
20
- import { processors } from "./processors"
21
- import { newid } from "../utils"
22
- import * as installation from "../installation"
23
- import * as configs from "../configs"
24
- import * as users from "../users"
25
- import { withCache, TTL, CacheKey } from "../cache/generic"
26
-
27
- /**
28
- * An identity can be:
29
- * - account user (Self host)
30
- * - budibase user
31
- * - tenant
32
- * - installation
33
- */
34
- const getCurrentIdentity = async (): Promise<Identity> => {
35
- let identityContext = identityCtx.getIdentity()
36
- const environment = getDeploymentEnvironment()
37
-
38
- let identityType
39
-
40
- if (!identityContext) {
41
- identityType = IdentityType.TENANT
42
- } else {
43
- identityType = identityContext.type
44
- }
45
-
46
- if (identityType === IdentityType.INSTALLATION) {
47
- const installationId = await getInstallationId()
48
- const hosting = getHostingFromEnv()
49
- return {
50
- id: formatDistinctId(installationId, identityType),
51
- hosting,
52
- type: identityType,
53
- installationId,
54
- environment,
55
- }
56
- } else if (identityType === IdentityType.TENANT) {
57
- const installationId = await getInstallationId()
58
- const tenantId = await getEventTenantId(context.getTenantId())
59
- const hosting = getHostingFromEnv()
60
-
61
- return {
62
- id: formatDistinctId(tenantId, identityType),
63
- type: identityType,
64
- hosting,
65
- installationId,
66
- tenantId,
67
- realTenantId: context.getTenantId(),
68
- environment,
69
- }
70
- } else if (identityType === IdentityType.USER) {
71
- const userContext = identityContext as UserContext
72
- const tenantId = await getEventTenantId(context.getTenantId())
73
- const installationId = await getInstallationId()
74
-
75
- const account = userContext.account
76
- let hosting
77
- if (account) {
78
- hosting = account.hosting
79
- } else {
80
- hosting = getHostingFromEnv()
81
- }
82
-
83
- return {
84
- id: userContext._id,
85
- type: identityType,
86
- hosting,
87
- installationId,
88
- tenantId,
89
- environment,
90
- realTenantId: context.getTenantId(),
91
- hostInfo: userContext.hostInfo,
92
- }
93
- } else {
94
- throw new Error("Unknown identity type")
95
- }
96
- }
97
-
98
- const identifyInstallationGroup = async (
99
- installId: string,
100
- timestamp?: string | number
101
- ): Promise<void> => {
102
- const id = installId
103
- const type = IdentityType.INSTALLATION
104
- const hosting = getHostingFromEnv()
105
- const version = env.VERSION
106
- const environment = getDeploymentEnvironment()
107
-
108
- const group: InstallationGroup = {
109
- id,
110
- type,
111
- hosting,
112
- version,
113
- environment,
114
- }
115
-
116
- await identifyGroup(group, timestamp)
117
- // need to create a normal identity for the group to be able to query it globally
118
- // match the posthog syntax to link this identity to the empty auto generated one
119
- await identify({ ...group, id: `$${type}_${id}` }, timestamp)
120
- }
121
-
122
- const identifyTenantGroup = async (
123
- tenantId: string,
124
- account: Account | undefined,
125
- timestamp?: string | number
126
- ): Promise<void> => {
127
- const id = await getEventTenantId(tenantId)
128
- const type = IdentityType.TENANT
129
- const installationId = await getInstallationId()
130
- const environment = getDeploymentEnvironment()
131
-
132
- let hosting: Hosting
133
- let profession: string | undefined
134
- let companySize: string | undefined
135
-
136
- if (account) {
137
- profession = account.profession
138
- companySize = account.size
139
- hosting = account.hosting
140
- } else {
141
- hosting = getHostingFromEnv()
142
- }
143
-
144
- const group: TenantGroup = {
145
- id,
146
- type,
147
- hosting,
148
- environment,
149
- installationId,
150
- profession,
151
- companySize,
152
- }
153
-
154
- await identifyGroup(group, timestamp)
155
- // need to create a normal identity for the group to be able to query it globally
156
- // match the posthog syntax to link this identity to the auto generated one
157
- await identify({ ...group, id: `$${type}_${id}` }, timestamp)
158
- }
159
-
160
- const identifyUser = async (
161
- user: User,
162
- account: CloudAccount | undefined,
163
- timestamp?: string | number
164
- ) => {
165
- const id = user._id as string
166
- const tenantId = await getEventTenantId(user.tenantId)
167
- const type = IdentityType.USER
168
- let builder = users.hasBuilderPermissions(user)
169
- let admin = users.hasAdminPermissions(user)
170
- let providerType
171
- if (isSSOUser(user)) {
172
- providerType = user.providerType
173
- }
174
- const accountHolder = account?.budibaseUserId === user._id || false
175
- const verified =
176
- account && account?.budibaseUserId === user._id ? account.verified : false
177
- const installationId = await getInstallationId()
178
- const hosting = account ? account.hosting : getHostingFromEnv()
179
- const environment = getDeploymentEnvironment()
180
-
181
- const identity: UserIdentity = {
182
- id,
183
- type,
184
- hosting,
185
- installationId,
186
- tenantId,
187
- verified,
188
- accountHolder,
189
- providerType,
190
- builder,
191
- admin,
192
- environment,
193
- }
194
-
195
- await identify(identity, timestamp)
196
- }
197
-
198
- const identifyAccount = async (account: Account) => {
199
- let id = account.accountId
200
- const tenantId = account.tenantId
201
- let type = IdentityType.USER
202
- let providerType = isSSOAccount(account) ? account.providerType : undefined
203
- const verified = account.verified
204
- const accountHolder = true
205
- const hosting = account.hosting
206
- const installationId = await getInstallationId()
207
- const environment = getDeploymentEnvironment()
208
-
209
- if (isCloudAccount(account)) {
210
- if (account.budibaseUserId) {
211
- // use the budibase user as the id if set
212
- id = account.budibaseUserId
213
- }
214
- }
215
-
216
- const identity: UserIdentity = {
217
- id,
218
- type,
219
- hosting,
220
- installationId,
221
- tenantId,
222
- providerType,
223
- verified,
224
- accountHolder,
225
- environment,
226
- }
227
-
228
- await identify(identity)
229
- }
230
-
231
- const identify = async (identity: Identity, timestamp?: string | number) => {
232
- await processors.identify(identity, timestamp)
233
- }
234
-
235
- const identifyGroup = async (group: Group, timestamp?: string | number) => {
236
- await processors.identifyGroup(group, timestamp)
237
- }
238
-
239
- const getDeploymentEnvironment = () => {
240
- if (env.isDev()) {
241
- return "development"
242
- } else {
243
- return env.DEPLOYMENT_ENVIRONMENT
244
- }
245
- }
246
-
247
- const getHostingFromEnv = () => {
248
- return env.SELF_HOSTED ? Hosting.SELF : Hosting.CLOUD
249
- }
250
-
251
- const getInstallationId = async () => {
252
- if (isAccountPortal()) {
253
- return "account-portal"
254
- }
255
- const install = await installation.getInstall()
256
- return install.installId
257
- }
258
-
259
- const getEventTenantId = async (tenantId: string): Promise<string> => {
260
- if (env.SELF_HOSTED) {
261
- return getUniqueTenantId(tenantId)
262
- } else {
263
- // tenant id's in the cloud are already unique
264
- return tenantId
265
- }
266
- }
267
-
268
- export const getUniqueTenantId = async (tenantId: string): Promise<string> => {
269
- // make sure this tenantId always matches the tenantId in context
270
- return context.doInTenant(tenantId, () => {
271
- return withCache(CacheKey.UNIQUE_TENANT_ID, TTL.ONE_DAY, async () => {
272
- const db = context.getGlobalDB()
273
- const config = await configs.getSettingsConfigDoc()
274
-
275
- let uniqueTenantId: string
276
- if (config.config.uniqueTenantId) {
277
- return config.config.uniqueTenantId
278
- } else {
279
- uniqueTenantId = `${newid()}_${tenantId}`
280
- config.config.uniqueTenantId = uniqueTenantId
281
- await db.put(config)
282
- return uniqueTenantId
283
- }
284
- })
285
- })
286
- }
287
-
288
- const isAccountPortal = () => {
289
- return env.SERVICE === "account-portal"
290
- }
291
-
292
- const formatDistinctId = (id: string, type: IdentityType) => {
293
- if (type === IdentityType.INSTALLATION || type === IdentityType.TENANT) {
294
- return `$${type}_${id}`
295
- } else {
296
- return id
297
- }
298
- }
299
-
300
- export default {
301
- getCurrentIdentity,
302
- identifyInstallationGroup,
303
- identifyTenantGroup,
304
- identifyUser,
305
- identifyAccount,
306
- identify,
307
- identifyGroup,
308
- getInstallationId,
309
- getUniqueTenantId,
310
- }
@@ -1,14 +0,0 @@
1
- export * from "./publishers"
2
- export * as processors from "./processors"
3
- export * as analytics from "./analytics"
4
- export { default as identification } from "./identification"
5
- export * as backfillCache from "./backfill"
6
-
7
- import { processors } from "./processors"
8
-
9
- export function initAsyncEvents() {}
10
-
11
- export const shutdown = () => {
12
- processors.shutdown()
13
- console.log("Events shutdown")
14
- }
@@ -1,64 +0,0 @@
1
- import { Event, Identity, Group, IdentityType } from "@budibase/types"
2
- import { EventProcessor } from "./types"
3
- import env from "../../environment"
4
- import * as analytics from "../analytics"
5
- import PosthogProcessor from "./posthog"
6
-
7
- /**
8
- * Events that are always captured.
9
- */
10
- const EVENT_WHITELIST = [
11
- Event.INSTALLATION_VERSION_UPGRADED,
12
- Event.INSTALLATION_VERSION_DOWNGRADED,
13
- ]
14
- const IDENTITY_WHITELIST = [IdentityType.INSTALLATION, IdentityType.TENANT]
15
-
16
- export default class AnalyticsProcessor implements EventProcessor {
17
- posthog: PosthogProcessor | undefined
18
-
19
- constructor() {
20
- if (env.POSTHOG_TOKEN && !env.isTest()) {
21
- this.posthog = new PosthogProcessor(env.POSTHOG_TOKEN)
22
- }
23
- }
24
-
25
- async processEvent(
26
- event: Event,
27
- identity: Identity,
28
- properties: any,
29
- timestamp?: string | number
30
- ): Promise<void> {
31
- if (!EVENT_WHITELIST.includes(event) && !(await analytics.enabled())) {
32
- return
33
- }
34
- if (this.posthog) {
35
- await this.posthog.processEvent(event, identity, properties, timestamp)
36
- }
37
- }
38
-
39
- async identify(identity: Identity, timestamp?: string | number) {
40
- // Group indentifications (tenant and installation) always on
41
- if (
42
- !IDENTITY_WHITELIST.includes(identity.type) &&
43
- !(await analytics.enabled())
44
- ) {
45
- return
46
- }
47
- if (this.posthog) {
48
- await this.posthog.identify(identity, timestamp)
49
- }
50
- }
51
-
52
- async identifyGroup(group: Group, timestamp?: string | number) {
53
- // Group indentifications (tenant and installation) always on
54
- if (this.posthog) {
55
- await this.posthog.identifyGroup(group, timestamp)
56
- }
57
- }
58
-
59
- shutdown() {
60
- if (this.posthog) {
61
- this.posthog.shutdown()
62
- }
63
- }
64
- }