@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.
- package/dist/index.js.map +1 -1
- package/dist/index.js.meta.json +1 -1
- package/dist/package.json +11 -4
- package/dist/plugins.js.meta.json +1 -1
- package/package.json +11 -4
- package/src/accounts/accounts.ts +0 -82
- package/src/accounts/api.ts +0 -59
- package/src/accounts/index.ts +0 -1
- package/src/auth/auth.ts +0 -210
- package/src/auth/index.ts +0 -1
- package/src/auth/tests/auth.spec.ts +0 -14
- package/src/blacklist/blacklist.ts +0 -54
- package/src/blacklist/index.ts +0 -1
- package/src/blacklist/tests/blacklist.spec.ts +0 -46
- package/src/cache/appMetadata.ts +0 -88
- package/src/cache/base/index.ts +0 -150
- package/src/cache/docWritethrough.ts +0 -105
- package/src/cache/generic.ts +0 -33
- package/src/cache/index.ts +0 -8
- package/src/cache/invite.ts +0 -86
- package/src/cache/passwordReset.ts +0 -49
- package/src/cache/tests/docWritethrough.spec.ts +0 -296
- package/src/cache/tests/user.spec.ts +0 -145
- package/src/cache/tests/writethrough.spec.ts +0 -139
- package/src/cache/user.ts +0 -154
- package/src/cache/writethrough.ts +0 -133
- package/src/configs/configs.ts +0 -263
- package/src/configs/index.ts +0 -1
- package/src/configs/tests/configs.spec.ts +0 -184
- package/src/constants/db.ts +0 -75
- package/src/constants/index.ts +0 -2
- package/src/constants/misc.ts +0 -36
- package/src/context/Context.ts +0 -14
- package/src/context/identity.ts +0 -58
- package/src/context/index.ts +0 -3
- package/src/context/mainContext.ts +0 -422
- package/src/context/tests/index.spec.ts +0 -255
- package/src/context/types.ts +0 -26
- package/src/db/Replication.ts +0 -94
- package/src/db/couch/DatabaseImpl.ts +0 -511
- package/src/db/couch/connections.ts +0 -89
- package/src/db/couch/index.ts +0 -4
- package/src/db/couch/pouchDB.ts +0 -97
- package/src/db/couch/pouchDump.ts +0 -0
- package/src/db/couch/tests/DatabaseImpl.spec.ts +0 -118
- package/src/db/couch/utils.ts +0 -55
- package/src/db/db.ts +0 -34
- package/src/db/errors.ts +0 -14
- package/src/db/index.ts +0 -12
- package/src/db/instrumentation.ts +0 -199
- package/src/db/lucene.ts +0 -721
- package/src/db/searchIndexes/index.ts +0 -1
- package/src/db/searchIndexes/searchIndexes.ts +0 -62
- package/src/db/tests/DatabaseImpl.spec.ts +0 -55
- package/src/db/tests/connections.spec.ts +0 -22
- package/src/db/tests/index.spec.ts +0 -32
- package/src/db/tests/lucene.spec.ts +0 -400
- package/src/db/tests/pouch.spec.js +0 -62
- package/src/db/tests/utils.spec.ts +0 -63
- package/src/db/utils.ts +0 -208
- package/src/db/views.ts +0 -245
- package/src/docIds/conversions.ts +0 -60
- package/src/docIds/ids.ts +0 -126
- package/src/docIds/index.ts +0 -2
- package/src/docIds/newid.ts +0 -5
- package/src/docIds/params.ts +0 -189
- package/src/docUpdates/index.ts +0 -24
- package/src/environment.ts +0 -293
- package/src/errors/errors.ts +0 -119
- package/src/errors/index.ts +0 -1
- package/src/events/analytics.ts +0 -6
- package/src/events/asyncEvents/index.ts +0 -2
- package/src/events/asyncEvents/publisher.ts +0 -12
- package/src/events/asyncEvents/queue.ts +0 -22
- package/src/events/backfill.ts +0 -183
- package/src/events/documentId.ts +0 -56
- package/src/events/events.ts +0 -47
- package/src/events/identification.ts +0 -311
- package/src/events/index.ts +0 -15
- package/src/events/processors/AnalyticsProcessor.ts +0 -64
- package/src/events/processors/AuditLogsProcessor.ts +0 -92
- package/src/events/processors/LoggingProcessor.ts +0 -36
- package/src/events/processors/Processors.ts +0 -52
- package/src/events/processors/async/DocumentUpdateProcessor.ts +0 -38
- package/src/events/processors/index.ts +0 -19
- package/src/events/processors/posthog/PosthogProcessor.ts +0 -118
- package/src/events/processors/posthog/index.ts +0 -3
- package/src/events/processors/posthog/rateLimiting.ts +0 -106
- package/src/events/processors/posthog/tests/PosthogProcessor.spec.ts +0 -164
- package/src/events/processors/types.ts +0 -1
- package/src/events/publishers/account.ts +0 -41
- package/src/events/publishers/ai.ts +0 -21
- package/src/events/publishers/app.ts +0 -168
- package/src/events/publishers/auditLog.ts +0 -26
- package/src/events/publishers/auth.ts +0 -73
- package/src/events/publishers/automation.ts +0 -110
- package/src/events/publishers/backfill.ts +0 -74
- package/src/events/publishers/backup.ts +0 -42
- package/src/events/publishers/datasource.ts +0 -48
- package/src/events/publishers/email.ts +0 -17
- package/src/events/publishers/environmentVariable.ts +0 -38
- package/src/events/publishers/group.ts +0 -99
- package/src/events/publishers/index.ts +0 -25
- package/src/events/publishers/installation.ts +0 -38
- package/src/events/publishers/layout.ts +0 -26
- package/src/events/publishers/license.ts +0 -84
- package/src/events/publishers/org.ts +0 -37
- package/src/events/publishers/plugin.ts +0 -47
- package/src/events/publishers/query.ts +0 -89
- package/src/events/publishers/role.ts +0 -62
- package/src/events/publishers/rows.ts +0 -29
- package/src/events/publishers/screen.ts +0 -36
- package/src/events/publishers/serve.ts +0 -43
- package/src/events/publishers/table.ts +0 -70
- package/src/events/publishers/user.ts +0 -202
- package/src/events/publishers/view.ts +0 -107
- package/src/features/features.ts +0 -277
- package/src/features/index.ts +0 -2
- package/src/features/tests/features.spec.ts +0 -267
- package/src/features/tests/utils.ts +0 -64
- package/src/helpers.ts +0 -9
- package/src/index.ts +0 -59
- package/src/installation.ts +0 -115
- package/src/logging/alerts.ts +0 -26
- package/src/logging/correlation/correlation.ts +0 -15
- package/src/logging/correlation/index.ts +0 -1
- package/src/logging/correlation/middleware.ts +0 -18
- package/src/logging/index.ts +0 -4
- package/src/logging/pino/logger.ts +0 -239
- package/src/logging/pino/middleware.ts +0 -48
- package/src/logging/system.ts +0 -81
- package/src/logging/tests/system.spec.ts +0 -61
- package/src/middleware/adminOnly.ts +0 -9
- package/src/middleware/auditLog.ts +0 -6
- package/src/middleware/authenticated.ts +0 -247
- package/src/middleware/builderOnly.ts +0 -21
- package/src/middleware/builderOrAdmin.ts +0 -21
- package/src/middleware/contentSecurityPolicy.ts +0 -113
- package/src/middleware/csrf.ts +0 -81
- package/src/middleware/errorHandling.ts +0 -43
- package/src/middleware/index.ts +0 -24
- package/src/middleware/internalApi.ts +0 -23
- package/src/middleware/ip.ts +0 -12
- package/src/middleware/joi-validator.ts +0 -58
- package/src/middleware/matchers.ts +0 -39
- package/src/middleware/passport/datasource/google.ts +0 -102
- package/src/middleware/passport/local.ts +0 -54
- package/src/middleware/passport/sso/google.ts +0 -77
- package/src/middleware/passport/sso/oidc.ts +0 -152
- package/src/middleware/passport/sso/sso.ts +0 -138
- package/src/middleware/passport/sso/tests/google.spec.ts +0 -68
- package/src/middleware/passport/sso/tests/oidc.spec.ts +0 -144
- package/src/middleware/passport/sso/tests/sso.spec.ts +0 -197
- package/src/middleware/passport/utils.ts +0 -38
- package/src/middleware/querystringToBody.ts +0 -28
- package/src/middleware/tenancy.ts +0 -36
- package/src/middleware/tests/builder.spec.ts +0 -181
- package/src/middleware/tests/contentSecurityPolicy.spec.ts +0 -75
- package/src/middleware/tests/matchers.spec.ts +0 -100
- package/src/migrations/definitions.ts +0 -40
- package/src/migrations/index.ts +0 -2
- package/src/migrations/migrations.ts +0 -186
- package/src/migrations/tests/__snapshots__/migrations.spec.ts.snap +0 -11
- package/src/migrations/tests/migrations.spec.ts +0 -64
- package/src/objectStore/buckets/app.ts +0 -53
- package/src/objectStore/buckets/global.ts +0 -29
- package/src/objectStore/buckets/index.ts +0 -3
- package/src/objectStore/buckets/plugins.ts +0 -71
- package/src/objectStore/buckets/tests/app.spec.ts +0 -161
- package/src/objectStore/buckets/tests/global.spec.ts +0 -74
- package/src/objectStore/buckets/tests/plugins.spec.ts +0 -111
- package/src/objectStore/cloudfront.ts +0 -41
- package/src/objectStore/index.ts +0 -3
- package/src/objectStore/objectStore.ts +0 -585
- package/src/objectStore/utils.ts +0 -113
- package/src/platform/index.ts +0 -3
- package/src/platform/platformDb.ts +0 -6
- package/src/platform/tenants.ts +0 -101
- package/src/platform/tests/tenants.spec.ts +0 -26
- package/src/platform/users.ts +0 -129
- package/src/plugin/index.ts +0 -1
- package/src/plugin/tests/validation.spec.ts +0 -209
- package/src/plugin/utils.ts +0 -175
- package/src/queue/constants.ts +0 -8
- package/src/queue/inMemoryQueue.ts +0 -189
- package/src/queue/index.ts +0 -2
- package/src/queue/listeners.ts +0 -199
- package/src/queue/queue.ts +0 -84
- package/src/redis/index.ts +0 -6
- package/src/redis/init.ts +0 -118
- package/src/redis/redis.ts +0 -358
- package/src/redis/redlockImpl.ts +0 -155
- package/src/redis/tests/redis.spec.ts +0 -207
- package/src/redis/tests/redlockImpl.spec.ts +0 -105
- package/src/redis/utils.ts +0 -128
- package/src/security/auth.ts +0 -24
- package/src/security/encryption.ts +0 -185
- package/src/security/index.ts +0 -1
- package/src/security/permissions.ts +0 -166
- package/src/security/roles.ts +0 -655
- package/src/security/secrets.ts +0 -20
- package/src/security/sessions.ts +0 -123
- package/src/security/tests/auth.spec.ts +0 -45
- package/src/security/tests/encryption.spec.ts +0 -31
- package/src/security/tests/permissions.spec.ts +0 -146
- package/src/security/tests/secrets.spec.ts +0 -35
- package/src/security/tests/sessions.spec.ts +0 -12
- package/src/sql/designDoc.ts +0 -17
- package/src/sql/index.ts +0 -5
- package/src/sql/sql.ts +0 -1854
- package/src/sql/sqlTable.ts +0 -319
- package/src/sql/utils.ts +0 -193
- package/src/tenancy/db.ts +0 -6
- package/src/tenancy/index.ts +0 -2
- package/src/tenancy/tenancy.ts +0 -148
- package/src/tenancy/tests/tenancy.spec.ts +0 -184
- package/src/timers/index.ts +0 -1
- package/src/timers/timers.ts +0 -22
- package/src/users/db.ts +0 -582
- package/src/users/events.ts +0 -176
- package/src/users/index.ts +0 -4
- package/src/users/lookup.ts +0 -99
- package/src/users/test/db.spec.ts +0 -188
- package/src/users/test/utils.spec.ts +0 -67
- package/src/users/users.ts +0 -353
- package/src/users/utils.ts +0 -81
- package/src/utils/Duration.ts +0 -56
- package/src/utils/hashing.ts +0 -15
- package/src/utils/index.ts +0 -4
- package/src/utils/stringUtils.ts +0 -8
- package/src/utils/tests/Duration.spec.ts +0 -19
- package/src/utils/tests/utils.spec.ts +0 -204
- package/src/utils/utils.ts +0 -249
- package/tests/core/logging.ts +0 -34
- package/tests/core/users/users.spec.js +0 -53
- package/tests/core/utilities/index.ts +0 -7
- package/tests/core/utilities/jestUtils.ts +0 -33
- package/tests/core/utilities/mocks/alerts.ts +0 -4
- package/tests/core/utilities/mocks/date.ts +0 -3
- package/tests/core/utilities/mocks/events.ts +0 -132
- package/tests/core/utilities/mocks/index.ts +0 -9
- package/tests/core/utilities/mocks/licenses.ts +0 -119
- package/tests/core/utilities/queue.ts +0 -9
- package/tests/core/utilities/structures/Chance.ts +0 -20
- package/tests/core/utilities/structures/accounts.ts +0 -80
- package/tests/core/utilities/structures/apps.ts +0 -21
- package/tests/core/utilities/structures/common.ts +0 -7
- package/tests/core/utilities/structures/db.ts +0 -12
- package/tests/core/utilities/structures/documents/index.ts +0 -1
- package/tests/core/utilities/structures/documents/platform/index.ts +0 -1
- package/tests/core/utilities/structures/documents/platform/installation.ts +0 -12
- package/tests/core/utilities/structures/generator.ts +0 -3
- package/tests/core/utilities/structures/index.ts +0 -15
- package/tests/core/utilities/structures/koa.ts +0 -16
- package/tests/core/utilities/structures/licenses.ts +0 -190
- package/tests/core/utilities/structures/plugins.ts +0 -19
- package/tests/core/utilities/structures/quotas.ts +0 -72
- package/tests/core/utilities/structures/scim.ts +0 -80
- package/tests/core/utilities/structures/sso.ts +0 -118
- package/tests/core/utilities/structures/tenants.ts +0 -5
- package/tests/core/utilities/structures/userGroups.ts +0 -10
- package/tests/core/utilities/structures/users.ts +0 -89
- package/tests/core/utilities/testContainerUtils.ts +0 -165
- package/tests/core/utilities/utils/index.ts +0 -2
- package/tests/core/utilities/utils/queue.ts +0 -27
- package/tests/core/utilities/utils/time.ts +0 -3
- package/tests/extra/DBTestConfiguration.ts +0 -36
- package/tests/extra/index.ts +0 -2
- package/tests/extra/testEnv.ts +0 -95
- package/tests/index.ts +0 -2
- package/tests/jestEnv.ts +0 -10
- package/tests/jestSetup.ts +0 -36
|
@@ -1,267 +0,0 @@
|
|
|
1
|
-
import { IdentityContext, IdentityType } from "@budibase/types"
|
|
2
|
-
import { Flag, FlagSet, FlagValues, init, shutdown } from "../"
|
|
3
|
-
import * as context from "../../context"
|
|
4
|
-
import environment, { withEnv } from "../../environment"
|
|
5
|
-
import nodeFetch from "node-fetch"
|
|
6
|
-
import nock from "nock"
|
|
7
|
-
import * as crypto from "crypto"
|
|
8
|
-
|
|
9
|
-
const schema = {
|
|
10
|
-
TEST_BOOLEAN: Flag.boolean(false),
|
|
11
|
-
TEST_STRING: Flag.string("default value"),
|
|
12
|
-
TEST_NUMBER: Flag.number(0),
|
|
13
|
-
TEST_BOOLEAN_DEFAULT_TRUE: Flag.boolean(true),
|
|
14
|
-
}
|
|
15
|
-
const flags = new FlagSet(schema)
|
|
16
|
-
|
|
17
|
-
interface TestCase {
|
|
18
|
-
it: string
|
|
19
|
-
identity?: Partial<IdentityContext>
|
|
20
|
-
environmentFlags?: string
|
|
21
|
-
posthogFlags?: PostHogFlags
|
|
22
|
-
expected?: Partial<FlagValues<typeof schema>>
|
|
23
|
-
errorMessage?: string | RegExp
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
interface PostHogFlags {
|
|
27
|
-
featureFlags?: Record<string, boolean>
|
|
28
|
-
featureFlagPayloads?: Record<string, string>
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
function mockPosthogFlags(
|
|
32
|
-
flags: PostHogFlags,
|
|
33
|
-
opts?: { token?: string; distinct_id?: string }
|
|
34
|
-
) {
|
|
35
|
-
const { token = "test", distinct_id = "us_1234" } = opts || {}
|
|
36
|
-
nock("https://us.i.posthog.com")
|
|
37
|
-
.post("/decide/?v=3", body => {
|
|
38
|
-
return body.token === token && body.distinct_id === distinct_id
|
|
39
|
-
})
|
|
40
|
-
.reply(200, flags)
|
|
41
|
-
.persist()
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
describe("feature flags", () => {
|
|
45
|
-
beforeEach(() => {
|
|
46
|
-
nock.cleanAll()
|
|
47
|
-
})
|
|
48
|
-
|
|
49
|
-
it.each<TestCase>([
|
|
50
|
-
{
|
|
51
|
-
it: "should should find a simple boolean flag in the environment",
|
|
52
|
-
environmentFlags: "default:TEST_BOOLEAN",
|
|
53
|
-
expected: { TEST_BOOLEAN: true },
|
|
54
|
-
},
|
|
55
|
-
{
|
|
56
|
-
it: "should should find a simple netgative boolean flag in the environment",
|
|
57
|
-
environmentFlags: "default:!TEST_BOOLEAN",
|
|
58
|
-
expected: { TEST_BOOLEAN: false },
|
|
59
|
-
},
|
|
60
|
-
{
|
|
61
|
-
it: "should should match stars in the environment",
|
|
62
|
-
environmentFlags: "*:TEST_BOOLEAN",
|
|
63
|
-
expected: { TEST_BOOLEAN: true },
|
|
64
|
-
},
|
|
65
|
-
{
|
|
66
|
-
it: "should not match a different tenant's flags",
|
|
67
|
-
environmentFlags: "otherTenant:TEST_BOOLEAN",
|
|
68
|
-
expected: { TEST_BOOLEAN: false },
|
|
69
|
-
},
|
|
70
|
-
{
|
|
71
|
-
it: "should return the defaults when no flags are set",
|
|
72
|
-
expected: flags.defaults(),
|
|
73
|
-
},
|
|
74
|
-
{
|
|
75
|
-
it: "should ignore unknown feature flags",
|
|
76
|
-
environmentFlags: "default:TEST_BOOLEAN,default:FOO",
|
|
77
|
-
expected: { TEST_BOOLEAN: true },
|
|
78
|
-
},
|
|
79
|
-
{
|
|
80
|
-
it: "should be able to read boolean flags from PostHog",
|
|
81
|
-
posthogFlags: {
|
|
82
|
-
featureFlags: { TEST_BOOLEAN: true },
|
|
83
|
-
},
|
|
84
|
-
expected: { TEST_BOOLEAN: true },
|
|
85
|
-
},
|
|
86
|
-
{
|
|
87
|
-
it: "should be able to read string flags from PostHog",
|
|
88
|
-
posthogFlags: {
|
|
89
|
-
featureFlags: { TEST_STRING: true },
|
|
90
|
-
featureFlagPayloads: { TEST_STRING: "test" },
|
|
91
|
-
},
|
|
92
|
-
expected: { TEST_STRING: "test" },
|
|
93
|
-
},
|
|
94
|
-
{
|
|
95
|
-
it: "should be able to read numeric flags from PostHog",
|
|
96
|
-
posthogFlags: {
|
|
97
|
-
featureFlags: { TEST_NUMBER: true },
|
|
98
|
-
featureFlagPayloads: { TEST_NUMBER: "123" },
|
|
99
|
-
},
|
|
100
|
-
expected: { TEST_NUMBER: 123 },
|
|
101
|
-
},
|
|
102
|
-
{
|
|
103
|
-
it: "should not be able to override a negative environment flag from PostHog",
|
|
104
|
-
environmentFlags: "default:!TEST_BOOLEAN",
|
|
105
|
-
posthogFlags: {
|
|
106
|
-
featureFlags: { TEST_BOOLEAN: true },
|
|
107
|
-
},
|
|
108
|
-
expected: { TEST_BOOLEAN: false },
|
|
109
|
-
},
|
|
110
|
-
{
|
|
111
|
-
it: "should not be able to override a positive environment flag from PostHog",
|
|
112
|
-
environmentFlags: "default:TEST_BOOLEAN",
|
|
113
|
-
posthogFlags: {
|
|
114
|
-
featureFlags: {
|
|
115
|
-
TEST_BOOLEAN: false,
|
|
116
|
-
},
|
|
117
|
-
},
|
|
118
|
-
expected: { TEST_BOOLEAN: true },
|
|
119
|
-
},
|
|
120
|
-
{
|
|
121
|
-
it: "should not error on unrecognised PostHog flag",
|
|
122
|
-
posthogFlags: {
|
|
123
|
-
featureFlags: { UNDEFINED: true },
|
|
124
|
-
},
|
|
125
|
-
expected: flags.defaults(),
|
|
126
|
-
},
|
|
127
|
-
{
|
|
128
|
-
it: "should be possible to override a default true flag to false",
|
|
129
|
-
environmentFlags: "default:!TEST_BOOLEAN_DEFAULT_TRUE",
|
|
130
|
-
expected: { TEST_BOOLEAN_DEFAULT_TRUE: false },
|
|
131
|
-
},
|
|
132
|
-
])(
|
|
133
|
-
"$it",
|
|
134
|
-
async ({
|
|
135
|
-
identity,
|
|
136
|
-
environmentFlags,
|
|
137
|
-
posthogFlags,
|
|
138
|
-
expected,
|
|
139
|
-
errorMessage,
|
|
140
|
-
}) => {
|
|
141
|
-
const env: Partial<typeof environment> = {
|
|
142
|
-
TENANT_FEATURE_FLAGS: environmentFlags,
|
|
143
|
-
SELF_HOSTED: false,
|
|
144
|
-
POSTHOG_FEATURE_FLAGS_ENABLED: "true",
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
if (posthogFlags) {
|
|
148
|
-
mockPosthogFlags(posthogFlags)
|
|
149
|
-
env.POSTHOG_TOKEN = "test"
|
|
150
|
-
env.POSTHOG_API_HOST = "https://us.i.posthog.com"
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
await withEnv(env, async () => {
|
|
154
|
-
// We need to pass in node-fetch here otherwise nock won't get used
|
|
155
|
-
// because posthog-node uses axios under the hood.
|
|
156
|
-
init({
|
|
157
|
-
fetch: (url, opts) => {
|
|
158
|
-
return nodeFetch(url, opts)
|
|
159
|
-
},
|
|
160
|
-
})
|
|
161
|
-
|
|
162
|
-
const fullIdentity: IdentityContext = {
|
|
163
|
-
_id: "us_1234",
|
|
164
|
-
tenantId: "default",
|
|
165
|
-
type: IdentityType.USER,
|
|
166
|
-
email: "test@example.com",
|
|
167
|
-
firstName: "Test",
|
|
168
|
-
lastName: "User",
|
|
169
|
-
...identity,
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
await context.doInIdentityContext(fullIdentity, async () => {
|
|
173
|
-
if (errorMessage) {
|
|
174
|
-
await expect(flags.fetch()).rejects.toThrow(errorMessage)
|
|
175
|
-
} else if (expected) {
|
|
176
|
-
const values = await flags.fetch()
|
|
177
|
-
expect(values).toMatchObject(expected)
|
|
178
|
-
|
|
179
|
-
for (const [key, expectedValue] of Object.entries(expected)) {
|
|
180
|
-
const value = await flags.get(key as keyof typeof schema)
|
|
181
|
-
expect(value).toBe(expectedValue)
|
|
182
|
-
}
|
|
183
|
-
} else {
|
|
184
|
-
throw new Error("No expected value")
|
|
185
|
-
}
|
|
186
|
-
})
|
|
187
|
-
|
|
188
|
-
shutdown()
|
|
189
|
-
})
|
|
190
|
-
}
|
|
191
|
-
)
|
|
192
|
-
|
|
193
|
-
it("should not error if PostHog is down", async () => {
|
|
194
|
-
const identity: IdentityContext = {
|
|
195
|
-
_id: "us_1234",
|
|
196
|
-
tenantId: "default",
|
|
197
|
-
type: IdentityType.USER,
|
|
198
|
-
email: "test@example.com",
|
|
199
|
-
firstName: "Test",
|
|
200
|
-
lastName: "User",
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
// We need to pass in node-fetch here otherwise nock won't get used
|
|
204
|
-
// because posthog-node uses axios under the hood.
|
|
205
|
-
init({
|
|
206
|
-
fetch: (url, opts) => {
|
|
207
|
-
return nodeFetch(url, opts)
|
|
208
|
-
},
|
|
209
|
-
})
|
|
210
|
-
|
|
211
|
-
nock("https://us.i.posthog.com")
|
|
212
|
-
.post("/decide/?v=3", body => {
|
|
213
|
-
return body.token === "test" && body.distinct_id === "us_1234"
|
|
214
|
-
})
|
|
215
|
-
.reply(503)
|
|
216
|
-
.persist()
|
|
217
|
-
|
|
218
|
-
await withEnv(
|
|
219
|
-
{ POSTHOG_TOKEN: "test", POSTHOG_API_HOST: "https://us.i.posthog.com" },
|
|
220
|
-
async () => {
|
|
221
|
-
await context.doInIdentityContext(identity, async () => {
|
|
222
|
-
await flags.fetch()
|
|
223
|
-
})
|
|
224
|
-
}
|
|
225
|
-
)
|
|
226
|
-
})
|
|
227
|
-
|
|
228
|
-
it("should still get flags when user is logged out", async () => {
|
|
229
|
-
const env: Partial<typeof environment> = {
|
|
230
|
-
SELF_HOSTED: false,
|
|
231
|
-
POSTHOG_FEATURE_FLAGS_ENABLED: "true",
|
|
232
|
-
POSTHOG_API_HOST: "https://us.i.posthog.com",
|
|
233
|
-
POSTHOG_TOKEN: "test",
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
const ip = "127.0.0.1"
|
|
237
|
-
const hashedIp = crypto.createHash("sha512").update(ip).digest("hex")
|
|
238
|
-
|
|
239
|
-
await withEnv(env, async () => {
|
|
240
|
-
mockPosthogFlags(
|
|
241
|
-
{
|
|
242
|
-
featureFlags: { TEST_BOOLEAN: true },
|
|
243
|
-
},
|
|
244
|
-
{
|
|
245
|
-
distinct_id: hashedIp,
|
|
246
|
-
}
|
|
247
|
-
)
|
|
248
|
-
|
|
249
|
-
// We need to pass in node-fetch here otherwise nock won't get used
|
|
250
|
-
// because posthog-node uses axios under the hood.
|
|
251
|
-
init({
|
|
252
|
-
fetch: (url, opts) => {
|
|
253
|
-
return nodeFetch(url, opts)
|
|
254
|
-
},
|
|
255
|
-
})
|
|
256
|
-
|
|
257
|
-
await context.doInIPContext(ip, async () => {
|
|
258
|
-
await context.doInTenant("default", async () => {
|
|
259
|
-
const result = await flags.fetch()
|
|
260
|
-
expect(result.TEST_BOOLEAN).toBe(true)
|
|
261
|
-
})
|
|
262
|
-
})
|
|
263
|
-
|
|
264
|
-
shutdown()
|
|
265
|
-
})
|
|
266
|
-
})
|
|
267
|
-
})
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
import { FeatureFlags, parseEnvFlags } from ".."
|
|
2
|
-
import { setEnv } from "../../environment"
|
|
3
|
-
|
|
4
|
-
function getCurrentFlags(): Record<string, Record<string, boolean>> {
|
|
5
|
-
const result: Record<string, Record<string, boolean>> = {}
|
|
6
|
-
for (const { tenantId, key, value } of parseEnvFlags(
|
|
7
|
-
process.env.TENANT_FEATURE_FLAGS || ""
|
|
8
|
-
)) {
|
|
9
|
-
const tenantFlags = result[tenantId] || {}
|
|
10
|
-
// Don't allow overwriting specifically false flags, to match the beheaviour
|
|
11
|
-
// of FlagSet.
|
|
12
|
-
if (tenantFlags[key] === false) {
|
|
13
|
-
continue
|
|
14
|
-
}
|
|
15
|
-
tenantFlags[key] = value
|
|
16
|
-
result[tenantId] = tenantFlags
|
|
17
|
-
}
|
|
18
|
-
return result
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
function buildFlagString(
|
|
22
|
-
flags: Record<string, Record<string, boolean>>
|
|
23
|
-
): string {
|
|
24
|
-
const parts: string[] = []
|
|
25
|
-
for (const [tenantId, tenantFlags] of Object.entries(flags)) {
|
|
26
|
-
for (const [key, value] of Object.entries(tenantFlags)) {
|
|
27
|
-
if (value === false) {
|
|
28
|
-
parts.push(`${tenantId}:!${key}`)
|
|
29
|
-
} else {
|
|
30
|
-
parts.push(`${tenantId}:${key}`)
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
return parts.join(",")
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export function setFeatureFlags(
|
|
38
|
-
tenantId: string,
|
|
39
|
-
flags: Partial<FeatureFlags>
|
|
40
|
-
): () => void {
|
|
41
|
-
const current = getCurrentFlags()
|
|
42
|
-
for (const [key, value] of Object.entries(flags)) {
|
|
43
|
-
const tenantFlags = current[tenantId] || {}
|
|
44
|
-
tenantFlags[key] = value
|
|
45
|
-
current[tenantId] = tenantFlags
|
|
46
|
-
}
|
|
47
|
-
const flagString = buildFlagString(current)
|
|
48
|
-
return setEnv({ TENANT_FEATURE_FLAGS: flagString })
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export function withFeatureFlags<T>(
|
|
52
|
-
tenantId: string,
|
|
53
|
-
flags: Partial<FeatureFlags>,
|
|
54
|
-
f: () => T
|
|
55
|
-
) {
|
|
56
|
-
const cleanup = setFeatureFlags(tenantId, flags)
|
|
57
|
-
const result = f()
|
|
58
|
-
if (result instanceof Promise) {
|
|
59
|
-
return result.finally(cleanup)
|
|
60
|
-
} else {
|
|
61
|
-
cleanup()
|
|
62
|
-
return result
|
|
63
|
-
}
|
|
64
|
-
}
|
package/src/helpers.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Makes sure that a URL has the correct number of slashes, while maintaining the
|
|
3
|
-
* http(s):// double slashes.
|
|
4
|
-
* @param url The URL to test and remove any extra double slashes.
|
|
5
|
-
* @return The updated url.
|
|
6
|
-
*/
|
|
7
|
-
export function checkSlashesInUrl(url: string) {
|
|
8
|
-
return url.replace(/(https?:\/\/)|(\/)+/g, "$1$2")
|
|
9
|
-
}
|
package/src/index.ts
DELETED
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
export * as configs from "./configs"
|
|
2
|
-
export * as events from "./events"
|
|
3
|
-
export * as migrations from "./migrations"
|
|
4
|
-
export * as users from "./users"
|
|
5
|
-
export * as userUtils from "./users/utils"
|
|
6
|
-
export * as roles from "./security/roles"
|
|
7
|
-
export * as permissions from "./security/permissions"
|
|
8
|
-
export * as accounts from "./accounts"
|
|
9
|
-
export * as installation from "./installation"
|
|
10
|
-
export * as features from "./features"
|
|
11
|
-
export * as sessions from "./security/sessions"
|
|
12
|
-
export * as platform from "./platform"
|
|
13
|
-
export * as auth from "./auth"
|
|
14
|
-
export * as constants from "./constants"
|
|
15
|
-
export * as logging from "./logging"
|
|
16
|
-
export * as middleware from "./middleware"
|
|
17
|
-
export * as plugins from "./plugin"
|
|
18
|
-
export * as encryption from "./security/encryption"
|
|
19
|
-
export * as queue from "./queue"
|
|
20
|
-
export * as db from "./db"
|
|
21
|
-
export * as context from "./context"
|
|
22
|
-
export * as cache from "./cache"
|
|
23
|
-
export * as objectStore from "./objectStore"
|
|
24
|
-
export * as redis from "./redis"
|
|
25
|
-
export { Client as RedisClient } from "./redis"
|
|
26
|
-
export * as locks from "./redis/redlockImpl"
|
|
27
|
-
export * as utils from "./utils"
|
|
28
|
-
export * as errors from "./errors"
|
|
29
|
-
export * as timers from "./timers"
|
|
30
|
-
export { default as env, withEnv, setEnv } from "./environment"
|
|
31
|
-
export * as blacklist from "./blacklist"
|
|
32
|
-
export * as docUpdates from "./docUpdates"
|
|
33
|
-
export * from "./utils/Duration"
|
|
34
|
-
export * as docIds from "./docIds"
|
|
35
|
-
export * as security from "./security"
|
|
36
|
-
export * as sql from "./sql"
|
|
37
|
-
// Add context to tenancy for backwards compatibility
|
|
38
|
-
// only do this for external usages to prevent internal
|
|
39
|
-
// circular dependencies
|
|
40
|
-
import * as context from "./context"
|
|
41
|
-
import * as _tenancy from "./tenancy"
|
|
42
|
-
|
|
43
|
-
export const tenancy = {
|
|
44
|
-
..._tenancy,
|
|
45
|
-
...context,
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
// expose error classes directly
|
|
49
|
-
export * from "./errors"
|
|
50
|
-
|
|
51
|
-
// expose constants directly
|
|
52
|
-
export * from "./constants"
|
|
53
|
-
|
|
54
|
-
// expose package init function
|
|
55
|
-
import * as db from "./db"
|
|
56
|
-
|
|
57
|
-
export const init = (opts: any = {}) => {
|
|
58
|
-
db.init(opts.db)
|
|
59
|
-
}
|
package/src/installation.ts
DELETED
|
@@ -1,115 +0,0 @@
|
|
|
1
|
-
import { newid } from "./utils"
|
|
2
|
-
import * as events from "./events"
|
|
3
|
-
import { StaticDatabases, doWithDB } from "./db"
|
|
4
|
-
import { Installation, IdentityType, Database } from "@budibase/types"
|
|
5
|
-
import * as context from "./context"
|
|
6
|
-
import semver from "semver"
|
|
7
|
-
import { bustCache, withCache, TTL, CacheKey } from "./cache/generic"
|
|
8
|
-
import environment from "./environment"
|
|
9
|
-
import { logAlert } from "./logging"
|
|
10
|
-
|
|
11
|
-
export const getInstall = async (): Promise<Installation> => {
|
|
12
|
-
return withCache(CacheKey.INSTALLATION, TTL.ONE_DAY, getInstallFromDB, {
|
|
13
|
-
useTenancy: false,
|
|
14
|
-
})
|
|
15
|
-
}
|
|
16
|
-
async function createInstallDoc(platformDb: Database) {
|
|
17
|
-
const install: Installation = {
|
|
18
|
-
_id: StaticDatabases.PLATFORM_INFO.docs.install,
|
|
19
|
-
installId: newid(),
|
|
20
|
-
version: environment.VERSION,
|
|
21
|
-
}
|
|
22
|
-
try {
|
|
23
|
-
const resp = await platformDb.put(install)
|
|
24
|
-
install._rev = resp.rev
|
|
25
|
-
return install
|
|
26
|
-
} catch (err: any) {
|
|
27
|
-
if (err.status === 409) {
|
|
28
|
-
return getInstallFromDB()
|
|
29
|
-
} else {
|
|
30
|
-
throw err
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export const getInstallFromDB = async (): Promise<Installation> => {
|
|
36
|
-
return doWithDB(
|
|
37
|
-
StaticDatabases.PLATFORM_INFO.name,
|
|
38
|
-
async (platformDb: any) => {
|
|
39
|
-
let install: Installation
|
|
40
|
-
try {
|
|
41
|
-
install = await platformDb.get(
|
|
42
|
-
StaticDatabases.PLATFORM_INFO.docs.install
|
|
43
|
-
)
|
|
44
|
-
} catch (e: any) {
|
|
45
|
-
if (e.status === 404) {
|
|
46
|
-
install = await createInstallDoc(platformDb)
|
|
47
|
-
} else {
|
|
48
|
-
throw e
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
return install
|
|
52
|
-
}
|
|
53
|
-
)
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
const updateVersion = async (version: string): Promise<boolean> => {
|
|
57
|
-
try {
|
|
58
|
-
await doWithDB(
|
|
59
|
-
StaticDatabases.PLATFORM_INFO.name,
|
|
60
|
-
async (platformDb: any) => {
|
|
61
|
-
const install = await getInstall()
|
|
62
|
-
install.version = version
|
|
63
|
-
await platformDb.put(install)
|
|
64
|
-
await bustCache(CacheKey.INSTALLATION)
|
|
65
|
-
}
|
|
66
|
-
)
|
|
67
|
-
} catch (e: any) {
|
|
68
|
-
if (e.status === 409) {
|
|
69
|
-
// do nothing - version has already been updated
|
|
70
|
-
// likely in clustered environment
|
|
71
|
-
return false
|
|
72
|
-
}
|
|
73
|
-
throw e
|
|
74
|
-
}
|
|
75
|
-
return true
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
export const checkInstallVersion = async (): Promise<void> => {
|
|
79
|
-
const install = await getInstall()
|
|
80
|
-
|
|
81
|
-
const currentVersion = install.version
|
|
82
|
-
const newVersion = environment.VERSION
|
|
83
|
-
|
|
84
|
-
try {
|
|
85
|
-
if (currentVersion !== newVersion) {
|
|
86
|
-
const isUpgrade = semver.gt(newVersion, currentVersion)
|
|
87
|
-
const isDowngrade = semver.lt(newVersion, currentVersion)
|
|
88
|
-
|
|
89
|
-
const success = await updateVersion(newVersion)
|
|
90
|
-
|
|
91
|
-
if (success) {
|
|
92
|
-
await context.doInIdentityContext(
|
|
93
|
-
{
|
|
94
|
-
_id: install.installId,
|
|
95
|
-
type: IdentityType.INSTALLATION,
|
|
96
|
-
},
|
|
97
|
-
async () => {
|
|
98
|
-
if (isUpgrade) {
|
|
99
|
-
await events.installation.upgraded(currentVersion, newVersion)
|
|
100
|
-
} else if (isDowngrade) {
|
|
101
|
-
await events.installation.downgraded(currentVersion, newVersion)
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
)
|
|
105
|
-
await events.identification.identifyInstallationGroup(install.installId)
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
} catch (err: any) {
|
|
109
|
-
if (err?.message?.includes("Invalid Version")) {
|
|
110
|
-
logAlert(`Invalid version "${newVersion}" - is it semver?`)
|
|
111
|
-
} else {
|
|
112
|
-
logAlert("Failed to retrieve version", err)
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
}
|
package/src/logging/alerts.ts
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
const NonErrors = ["AccountError"]
|
|
2
|
-
|
|
3
|
-
function isSuppressed(e?: any) {
|
|
4
|
-
return e && e["suppressAlert"]
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
export function logAlert(message: string, e?: any) {
|
|
8
|
-
if (e && NonErrors.includes(e.name) && isSuppressed(e)) {
|
|
9
|
-
return
|
|
10
|
-
}
|
|
11
|
-
console.error(`bb-alert: ${message}`, e)
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export function logAlertWithInfo(
|
|
15
|
-
message: string,
|
|
16
|
-
db: string,
|
|
17
|
-
id: string,
|
|
18
|
-
error: any
|
|
19
|
-
) {
|
|
20
|
-
message = `${message} - db: ${db} - doc: ${id} - error: `
|
|
21
|
-
logAlert(message, error)
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export function logWarn(message: string, e?: any) {
|
|
25
|
-
console.warn(`bb-warn: ${message}`, e)
|
|
26
|
-
}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { Header } from "../../constants"
|
|
2
|
-
|
|
3
|
-
const correlator = require("correlation-id")
|
|
4
|
-
|
|
5
|
-
export const setHeader = (headers: Record<string, string>) => {
|
|
6
|
-
const correlationId = correlator.getId()
|
|
7
|
-
if (!correlationId) {
|
|
8
|
-
return
|
|
9
|
-
}
|
|
10
|
-
headers[Header.CORRELATION_ID] = correlationId
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export function getId() {
|
|
14
|
-
return correlator.getId()
|
|
15
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from "./correlation"
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { Header } from "../../constants"
|
|
2
|
-
import { v4 as uuid } from "uuid"
|
|
3
|
-
|
|
4
|
-
const correlator = require("correlation-id")
|
|
5
|
-
|
|
6
|
-
const correlation = (ctx: any, next: any) => {
|
|
7
|
-
// use the provided correlation id header if present
|
|
8
|
-
let correlationId = ctx.headers[Header.CORRELATION_ID]
|
|
9
|
-
if (!correlationId) {
|
|
10
|
-
correlationId = uuid()
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
return correlator.withId(correlationId, () => {
|
|
14
|
-
return next()
|
|
15
|
-
})
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export default correlation
|
package/src/logging/index.ts
DELETED