@budibase/server 1.3.14 → 1.3.15-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (264) hide show
  1. package/__mocks__/mongodb.ts +1 -1
  2. package/builder/assets/index.2884f3c4.js +995 -0
  3. package/builder/assets/{index.a70e216a.css → index.ecdac070.css} +2 -2
  4. package/builder/index.html +2 -4
  5. package/coverage/clover.xml +296 -254
  6. package/coverage/coverage-final.json +17 -17
  7. package/coverage/lcov-report/api/controllers/apikeys.js.html +1 -1
  8. package/coverage/lcov-report/api/controllers/auth.js.html +1 -1
  9. package/coverage/lcov-report/api/controllers/automation.js.html +1 -1
  10. package/coverage/lcov-report/api/controllers/backup.js.html +1 -1
  11. package/coverage/lcov-report/api/controllers/cloud.js.html +1 -1
  12. package/coverage/lcov-report/api/controllers/component.js.html +82 -13
  13. package/coverage/lcov-report/api/controllers/datasource.js.html +20 -20
  14. package/coverage/lcov-report/api/controllers/dev.js.html +1 -1
  15. package/coverage/lcov-report/api/controllers/index.html +30 -30
  16. package/coverage/lcov-report/api/controllers/integration.js.html +6 -6
  17. package/coverage/lcov-report/api/controllers/layout.js.html +1 -1
  18. package/coverage/lcov-report/api/controllers/metadata.js.html +1 -1
  19. package/coverage/lcov-report/api/controllers/migrations.js.html +1 -1
  20. package/coverage/lcov-report/api/controllers/permission.js.html +1 -1
  21. package/coverage/lcov-report/api/controllers/query/index.html +1 -1
  22. package/coverage/lcov-report/api/controllers/query/validation.js.html +1 -1
  23. package/coverage/lcov-report/api/controllers/role.js.html +1 -1
  24. package/coverage/lcov-report/api/controllers/routing.js.html +1 -1
  25. package/coverage/lcov-report/api/controllers/row/external.js.html +1 -1
  26. package/coverage/lcov-report/api/controllers/row/index.html +1 -1
  27. package/coverage/lcov-report/api/controllers/row/internal.js.html +1 -1
  28. package/coverage/lcov-report/api/controllers/row/internalSearch.js.html +1 -1
  29. package/coverage/lcov-report/api/controllers/row/staticFormula.js.html +1 -1
  30. package/coverage/lcov-report/api/controllers/row/utils.js.html +1 -1
  31. package/coverage/lcov-report/api/controllers/screen.js.html +207 -12
  32. package/coverage/lcov-report/api/controllers/script.js.html +1 -1
  33. package/coverage/lcov-report/api/controllers/table/bulkFormula.js.html +1 -1
  34. package/coverage/lcov-report/api/controllers/table/external.js.html +1 -1
  35. package/coverage/lcov-report/api/controllers/table/index.html +1 -1
  36. package/coverage/lcov-report/api/controllers/table/index.js.html +1 -1
  37. package/coverage/lcov-report/api/controllers/templates.js.html +1 -1
  38. package/coverage/lcov-report/api/controllers/user.js.html +1 -1
  39. package/coverage/lcov-report/api/controllers/view/exporters.js.html +1 -1
  40. package/coverage/lcov-report/api/controllers/view/index.html +1 -1
  41. package/coverage/lcov-report/api/controllers/view/index.js.html +1 -1
  42. package/coverage/lcov-report/api/controllers/view/utils.js.html +1 -1
  43. package/coverage/lcov-report/api/controllers/view/viewBuilder.js.html +1 -1
  44. package/coverage/lcov-report/api/controllers/webhook.js.html +1 -1
  45. package/coverage/lcov-report/api/index.html +1 -1
  46. package/coverage/lcov-report/api/index.js.html +12 -12
  47. package/coverage/lcov-report/api/routes/analytics.js.html +1 -1
  48. package/coverage/lcov-report/api/routes/apikeys.js.html +1 -1
  49. package/coverage/lcov-report/api/routes/auth.js.html +1 -1
  50. package/coverage/lcov-report/api/routes/automation.js.html +1 -1
  51. package/coverage/lcov-report/api/routes/backup.js.html +1 -1
  52. package/coverage/lcov-report/api/routes/cloud.js.html +1 -1
  53. package/coverage/lcov-report/api/routes/component.js.html +1 -1
  54. package/coverage/lcov-report/api/routes/datasource.js.html +1 -1
  55. package/coverage/lcov-report/api/routes/deploy.js.html +1 -1
  56. package/coverage/lcov-report/api/routes/dev.js.html +1 -1
  57. package/coverage/lcov-report/api/routes/index.html +1 -1
  58. package/coverage/lcov-report/api/routes/integration.js.html +1 -1
  59. package/coverage/lcov-report/api/routes/layout.js.html +1 -1
  60. package/coverage/lcov-report/api/routes/metadata.js.html +1 -1
  61. package/coverage/lcov-report/api/routes/migrations.js.html +1 -1
  62. package/coverage/lcov-report/api/routes/permission.js.html +1 -1
  63. package/coverage/lcov-report/api/routes/query.js.html +1 -1
  64. package/coverage/lcov-report/api/routes/role.js.html +1 -1
  65. package/coverage/lcov-report/api/routes/routing.js.html +1 -1
  66. package/coverage/lcov-report/api/routes/screen.js.html +1 -1
  67. package/coverage/lcov-report/api/routes/script.js.html +1 -1
  68. package/coverage/lcov-report/api/routes/table.js.html +1 -1
  69. package/coverage/lcov-report/api/routes/templates.js.html +1 -1
  70. package/coverage/lcov-report/api/routes/user.js.html +1 -1
  71. package/coverage/lcov-report/api/routes/utils/index.html +1 -1
  72. package/coverage/lcov-report/api/routes/utils/validators.js.html +1 -1
  73. package/coverage/lcov-report/api/routes/view.js.html +1 -1
  74. package/coverage/lcov-report/api/routes/webhook.js.html +1 -1
  75. package/coverage/lcov-report/automations/actions.js.html +1 -1
  76. package/coverage/lcov-report/automations/automationUtils.js.html +1 -1
  77. package/coverage/lcov-report/automations/bullboard.js.html +1 -1
  78. package/coverage/lcov-report/automations/index.html +1 -1
  79. package/coverage/lcov-report/automations/index.js.html +1 -1
  80. package/coverage/lcov-report/automations/steps/bash.js.html +1 -1
  81. package/coverage/lcov-report/automations/steps/delay.js.html +1 -1
  82. package/coverage/lcov-report/automations/steps/discord.js.html +1 -1
  83. package/coverage/lcov-report/automations/steps/executeQuery.js.html +1 -1
  84. package/coverage/lcov-report/automations/steps/executeScript.js.html +1 -1
  85. package/coverage/lcov-report/automations/steps/filter.js.html +1 -1
  86. package/coverage/lcov-report/automations/steps/index.html +1 -1
  87. package/coverage/lcov-report/automations/steps/integromat.js.html +1 -1
  88. package/coverage/lcov-report/automations/steps/loop.js.html +1 -1
  89. package/coverage/lcov-report/automations/steps/outgoingWebhook.js.html +1 -1
  90. package/coverage/lcov-report/automations/steps/queryRows.js.html +1 -1
  91. package/coverage/lcov-report/automations/steps/sendSmtpEmail.js.html +1 -1
  92. package/coverage/lcov-report/automations/steps/serverLog.js.html +1 -1
  93. package/coverage/lcov-report/automations/steps/slack.js.html +1 -1
  94. package/coverage/lcov-report/automations/steps/updateRow.js.html +1 -1
  95. package/coverage/lcov-report/automations/steps/utils.js.html +1 -1
  96. package/coverage/lcov-report/automations/steps/zapier.js.html +1 -1
  97. package/coverage/lcov-report/automations/triggerInfo/app.js.html +1 -1
  98. package/coverage/lcov-report/automations/triggerInfo/cron.js.html +1 -1
  99. package/coverage/lcov-report/automations/triggerInfo/index.html +1 -1
  100. package/coverage/lcov-report/automations/triggerInfo/index.js.html +1 -1
  101. package/coverage/lcov-report/automations/triggerInfo/rowDeleted.js.html +1 -1
  102. package/coverage/lcov-report/automations/triggerInfo/rowSaved.js.html +1 -1
  103. package/coverage/lcov-report/automations/triggerInfo/rowUpdated.js.html +1 -1
  104. package/coverage/lcov-report/automations/triggerInfo/webhook.js.html +1 -1
  105. package/coverage/lcov-report/automations/triggers.js.html +1 -1
  106. package/coverage/lcov-report/constants/index.html +1 -1
  107. package/coverage/lcov-report/constants/index.js.html +1 -1
  108. package/coverage/lcov-report/constants/layouts.js.html +1 -1
  109. package/coverage/lcov-report/constants/screens.js.html +1 -1
  110. package/coverage/lcov-report/db/inMemoryView.js.html +1 -1
  111. package/coverage/lcov-report/db/index.html +18 -18
  112. package/coverage/lcov-report/db/index.js.html +1 -1
  113. package/coverage/lcov-report/db/linkedRows/LinkController.js.html +1 -1
  114. package/coverage/lcov-report/db/linkedRows/index.html +1 -1
  115. package/coverage/lcov-report/db/linkedRows/index.js.html +1 -1
  116. package/coverage/lcov-report/db/linkedRows/linkUtils.js.html +1 -1
  117. package/coverage/lcov-report/db/newid.js.html +2 -2
  118. package/coverage/lcov-report/db/utils.js.html +56 -20
  119. package/coverage/lcov-report/events/AutomationEmitter.js.html +1 -1
  120. package/coverage/lcov-report/events/index.html +1 -1
  121. package/coverage/lcov-report/events/index.js.html +1 -1
  122. package/coverage/lcov-report/events/utils.js.html +1 -1
  123. package/coverage/lcov-report/index.html +28 -28
  124. package/coverage/lcov-report/integrations/tests/TestConfiguration.js.html +1 -1
  125. package/coverage/lcov-report/integrations/tests/index.html +1 -1
  126. package/coverage/lcov-report/middleware/appInfo.js.html +1 -1
  127. package/coverage/lcov-report/middleware/builder.js.html +12 -12
  128. package/coverage/lcov-report/middleware/currentapp.js.html +33 -33
  129. package/coverage/lcov-report/middleware/index.html +1 -1
  130. package/coverage/lcov-report/middleware/joi-validator.js.html +1 -1
  131. package/coverage/lcov-report/middleware/publicApi.js.html +1 -1
  132. package/coverage/lcov-report/middleware/resourceId.js.html +7 -7
  133. package/coverage/lcov-report/middleware/selfhost.js.html +1 -1
  134. package/coverage/lcov-report/middleware/utils.js.html +2 -2
  135. package/coverage/lcov-report/utilities/budibaseDir.js.html +3 -3
  136. package/coverage/lcov-report/utilities/centralPath.js.html +4 -4
  137. package/coverage/lcov-report/utilities/csvParser.js.html +1 -1
  138. package/coverage/lcov-report/utilities/global.js.html +16 -16
  139. package/coverage/lcov-report/utilities/index.html +1 -1
  140. package/coverage/lcov-report/utilities/index.js.html +24 -24
  141. package/coverage/lcov-report/utilities/queue/inMemoryQueue.js.html +2 -2
  142. package/coverage/lcov-report/utilities/queue/index.html +1 -1
  143. package/coverage/lcov-report/utilities/routing/index.html +1 -1
  144. package/coverage/lcov-report/utilities/routing/index.js.html +1 -1
  145. package/coverage/lcov-report/utilities/rowProcessor/index.html +1 -1
  146. package/coverage/lcov-report/utilities/rowProcessor/index.js.html +1 -1
  147. package/coverage/lcov-report/utilities/rowProcessor/utils.js.html +1 -1
  148. package/coverage/lcov-report/utilities/scriptRunner.js.html +1 -1
  149. package/coverage/lcov-report/utilities/security.js.html +2 -2
  150. package/coverage/lcov-report/utilities/statusCodes.js.html +1 -1
  151. package/coverage/lcov-report/utilities/usageQuota/index.html +1 -1
  152. package/coverage/lcov-report/utilities/usageQuota/rows.js.html +1 -1
  153. package/coverage/lcov-report/utilities/usageQuota/usageQuoteReset.js.html +1 -1
  154. package/coverage/lcov-report/utilities/users.js.html +1 -1
  155. package/coverage/lcov-report/utilities/workerRequests.js.html +1 -1
  156. package/coverage/lcov.info +533 -445
  157. package/dist/api/controllers/application.js +5 -4
  158. package/dist/api/controllers/component.js +18 -1
  159. package/dist/api/controllers/datasource.js +4 -4
  160. package/dist/api/controllers/integration.js +4 -4
  161. package/dist/api/controllers/plugin/file.js +24 -0
  162. package/dist/api/controllers/plugin/github.js +69 -0
  163. package/dist/api/controllers/plugin/index.js +192 -0
  164. package/dist/api/controllers/plugin/npm.js +58 -0
  165. package/dist/api/controllers/plugin/uploaders.js +11 -0
  166. package/dist/api/controllers/plugin/url.js +24 -0
  167. package/dist/api/controllers/plugin/utils.js +26 -0
  168. package/dist/api/controllers/screen.js +52 -2
  169. package/dist/api/controllers/static/index.js +1 -0
  170. package/dist/api/controllers/static/templates/BudibaseApp.svelte +10 -0
  171. package/dist/api/controllers/static/templates/preview.hbs +5 -1
  172. package/dist/api/routes/index.js +2 -0
  173. package/dist/api/routes/plugin.js +39 -0
  174. package/dist/app.js +11 -0
  175. package/dist/db/utils.js +10 -1
  176. package/dist/environment.js +1 -0
  177. package/dist/integrations/airtable.js +126 -129
  178. package/dist/integrations/arangodb.js +86 -89
  179. package/dist/integrations/base/query.js +2 -2
  180. package/dist/integrations/couchdb.js +80 -83
  181. package/dist/integrations/dynamodb.js +161 -164
  182. package/dist/integrations/elasticsearch.js +135 -138
  183. package/dist/integrations/firebase.js +154 -157
  184. package/dist/integrations/googlesheets.js +323 -327
  185. package/dist/integrations/index.js +93 -51
  186. package/dist/integrations/microsoftSqlServer.js +240 -243
  187. package/dist/integrations/mongodb.js +239 -242
  188. package/dist/integrations/mysql.js +215 -219
  189. package/dist/integrations/oracle.js +318 -322
  190. package/dist/integrations/postgres.js +262 -265
  191. package/dist/integrations/redis.js +134 -139
  192. package/dist/integrations/rest.js +289 -292
  193. package/dist/integrations/s3.js +61 -64
  194. package/dist/integrations/snowflake.js +83 -86
  195. package/dist/package.json +9 -6
  196. package/dist/threads/query.js +2 -2
  197. package/dist/tsconfig.build.tsbuildinfo +1 -1
  198. package/dist/utilities/fileSystem/index.js +110 -1
  199. package/dist/watch.js +77 -0
  200. package/dist/websocket.js +22 -0
  201. package/package.json +10 -7
  202. package/scripts/dev/manage.js +1 -0
  203. package/src/api/controllers/application.ts +1 -1
  204. package/src/api/controllers/component.js +25 -2
  205. package/src/api/controllers/datasource.js +4 -4
  206. package/src/api/controllers/integration.js +4 -4
  207. package/src/api/controllers/plugin/file.ts +15 -0
  208. package/src/api/controllers/plugin/github.ts +75 -0
  209. package/src/api/controllers/plugin/index.ts +206 -0
  210. package/src/api/controllers/plugin/npm.ts +56 -0
  211. package/src/api/controllers/plugin/uploaders.ts +4 -0
  212. package/src/api/controllers/plugin/url.ts +12 -0
  213. package/src/api/controllers/plugin/utils.ts +19 -0
  214. package/src/api/controllers/screen.js +66 -1
  215. package/src/api/controllers/static/index.ts +1 -0
  216. package/src/api/controllers/static/templates/BudibaseApp.svelte +10 -0
  217. package/src/api/controllers/static/templates/preview.hbs +5 -1
  218. package/src/api/routes/index.ts +2 -0
  219. package/src/api/routes/plugin.ts +14 -0
  220. package/src/api/routes/tests/query.spec.js +8 -25
  221. package/src/app.ts +14 -0
  222. package/src/db/utils.js +12 -0
  223. package/src/environment.js +1 -0
  224. package/src/integrations/airtable.ts +116 -118
  225. package/src/integrations/arangodb.ts +87 -89
  226. package/src/integrations/base/query.ts +2 -2
  227. package/src/integrations/couchdb.ts +83 -89
  228. package/src/integrations/dynamodb.ts +173 -175
  229. package/src/integrations/elasticsearch.ts +120 -122
  230. package/src/integrations/firebase.ts +157 -159
  231. package/src/integrations/googlesheets.ts +328 -338
  232. package/src/integrations/index.ts +59 -21
  233. package/src/integrations/microsoftSqlServer.ts +249 -251
  234. package/src/integrations/mongodb.ts +261 -258
  235. package/src/integrations/mysql.ts +229 -234
  236. package/src/integrations/oracle.ts +373 -380
  237. package/src/integrations/postgres.ts +264 -268
  238. package/src/integrations/redis.ts +128 -132
  239. package/src/integrations/rest.ts +324 -329
  240. package/src/integrations/s3.ts +68 -71
  241. package/src/integrations/snowflake.ts +80 -82
  242. package/src/integrations/tests/{airtable.spec.js → airtable.spec.ts} +21 -18
  243. package/src/integrations/tests/{arangodb.spec.js → arangodb.spec.ts} +12 -9
  244. package/src/integrations/tests/{couchdb.spec.js → couchdb.spec.ts} +33 -27
  245. package/src/integrations/tests/{dynamodb.spec.js → dynamodb.spec.ts} +45 -41
  246. package/src/integrations/tests/{elasticsearch.spec.js → elasticsearch.spec.ts} +22 -21
  247. package/src/integrations/tests/{firebase.spec.js → firebase.spec.ts} +33 -28
  248. package/src/integrations/tests/{microsoftSqlServer.spec.js → microsoftSqlServer.spec.ts} +12 -11
  249. package/src/integrations/tests/{mongo.spec.js → mongo.spec.ts} +46 -35
  250. package/src/integrations/tests/{mysql.spec.js → mysql.spec.ts} +17 -15
  251. package/src/integrations/tests/{oracle.spec.js → oracle.spec.ts} +19 -17
  252. package/src/integrations/tests/{postgres.spec.js → postgres.spec.ts} +19 -17
  253. package/src/integrations/tests/{redis.spec.js → redis.spec.ts} +27 -18
  254. package/src/integrations/tests/{rest.spec.js → rest.spec.ts} +22 -14
  255. package/src/integrations/tests/{s3.spec.js → s3.spec.ts} +10 -8
  256. package/src/integrations/tests/{sql.spec.js → sql.spec.ts} +228 -177
  257. package/src/module.d.ts +2 -0
  258. package/src/threads/query.ts +2 -2
  259. package/src/utilities/fileSystem/index.js +129 -0
  260. package/src/watch.ts +38 -0
  261. package/src/websocket.ts +26 -0
  262. package/builder/assets/index.d8428ac8.js +0 -876
  263. package/builder/assets/vendor.5e1fe644.css +0 -1
  264. package/builder/assets/vendor.67dbd8fa.js +0 -248
@@ -0,0 +1,206 @@
1
+ import { ObjectStoreBuckets } from "../../../constants"
2
+ import { loadJSFile } from "../../../utilities/fileSystem"
3
+ import { npmUpload, urlUpload, githubUpload, fileUpload } from "./uploaders"
4
+ import { getGlobalDB } from "@budibase/backend-core/tenancy"
5
+ import { validate } from "@budibase/backend-core/plugins"
6
+ import { generatePluginID, getPluginParams } from "../../../db/utils"
7
+ import {
8
+ uploadDirectory,
9
+ deleteFolder,
10
+ } from "@budibase/backend-core/objectStore"
11
+ import { PluginType, FileType, PluginSource } from "@budibase/types"
12
+ import env from "../../../environment"
13
+ import { ClientAppSocket } from "../../../websocket"
14
+
15
+ export async function getPlugins(type?: PluginType) {
16
+ const db = getGlobalDB()
17
+ const response = await db.allDocs(
18
+ getPluginParams(null, {
19
+ include_docs: true,
20
+ })
21
+ )
22
+ const plugins = response.rows.map((row: any) => row.doc)
23
+ if (type) {
24
+ return plugins.filter((plugin: any) => plugin.schema?.type === type)
25
+ } else {
26
+ return plugins
27
+ }
28
+ }
29
+
30
+ export async function upload(ctx: any) {
31
+ const plugins: FileType[] =
32
+ ctx.request.files.file.length > 1
33
+ ? Array.from(ctx.request.files.file)
34
+ : [ctx.request.files.file]
35
+ try {
36
+ let docs = []
37
+ // can do single or multiple plugins
38
+ for (let plugin of plugins) {
39
+ const doc = await processPlugin(plugin, PluginSource.FILE)
40
+ docs.push(doc)
41
+ }
42
+ ctx.body = {
43
+ message: "Plugin(s) uploaded successfully",
44
+ plugins: docs,
45
+ }
46
+ } catch (err: any) {
47
+ const errMsg = err?.message ? err?.message : err
48
+
49
+ ctx.throw(400, `Failed to import plugin: ${errMsg}`)
50
+ }
51
+ }
52
+
53
+ export async function create(ctx: any) {
54
+ const { source, url, headers, githubToken } = ctx.request.body
55
+
56
+ if (!env.SELF_HOSTED) {
57
+ ctx.throw(400, "Plugins not supported outside of self-host.")
58
+ }
59
+
60
+ try {
61
+ let metadata
62
+ let directory
63
+ // Generating random name as a backup and needed for url
64
+ let name = "PLUGIN_" + Math.floor(100000 + Math.random() * 900000)
65
+
66
+ switch (source) {
67
+ case PluginSource.NPM:
68
+ const { metadata: metadataNpm, directory: directoryNpm } =
69
+ await npmUpload(url, name)
70
+ metadata = metadataNpm
71
+ directory = directoryNpm
72
+ break
73
+ case PluginSource.GITHUB:
74
+ const { metadata: metadataGithub, directory: directoryGithub } =
75
+ await githubUpload(url, name, githubToken)
76
+ metadata = metadataGithub
77
+ directory = directoryGithub
78
+ break
79
+ case PluginSource.URL:
80
+ const headersObj = headers || {}
81
+ const { metadata: metadataUrl, directory: directoryUrl } =
82
+ await urlUpload(url, name, headersObj)
83
+ metadata = metadataUrl
84
+ directory = directoryUrl
85
+ break
86
+ }
87
+
88
+ validate(metadata?.schema)
89
+
90
+ const doc = await storePlugin(metadata, directory, source)
91
+
92
+ ctx.body = {
93
+ message: "Plugin uploaded successfully",
94
+ plugins: [doc],
95
+ }
96
+ } catch (err: any) {
97
+ const errMsg = err?.message ? err?.message : err
98
+
99
+ ctx.throw(400, `Failed to import plugin: ${errMsg}`)
100
+ }
101
+ ctx.status = 200
102
+ }
103
+
104
+ export async function fetch(ctx: any) {
105
+ ctx.body = await getPlugins()
106
+ }
107
+
108
+ export async function destroy(ctx: any) {
109
+ const db = getGlobalDB()
110
+ const { pluginId } = ctx.params
111
+
112
+ try {
113
+ const plugin = await db.get(pluginId)
114
+ const bucketPath = `${plugin.name}/`
115
+ await deleteFolder(ObjectStoreBuckets.PLUGINS, bucketPath)
116
+
117
+ await db.remove(pluginId, plugin._rev)
118
+ } catch (err: any) {
119
+ const errMsg = err?.message ? err?.message : err
120
+
121
+ ctx.throw(400, `Failed to delete plugin: ${errMsg}`)
122
+ }
123
+
124
+ ctx.message = `Plugin ${ctx.params.pluginId} deleted.`
125
+ ctx.status = 200
126
+ }
127
+
128
+ export async function storePlugin(
129
+ metadata: any,
130
+ directory: any,
131
+ source?: string
132
+ ) {
133
+ const db = getGlobalDB()
134
+ const version = metadata.package.version,
135
+ name = metadata.package.name,
136
+ description = metadata.package.description,
137
+ hash = metadata.schema.hash
138
+
139
+ // first open the tarball into tmp directory
140
+ const bucketPath = `${name}/`
141
+ const files = await uploadDirectory(
142
+ ObjectStoreBuckets.PLUGINS,
143
+ directory,
144
+ bucketPath
145
+ )
146
+ const jsFile = files.find((file: any) => file.name.endsWith(".js"))
147
+ if (!jsFile) {
148
+ throw new Error(`Plugin missing .js file.`)
149
+ }
150
+ // validate the JS for a datasource
151
+ if (metadata.schema.type === PluginType.DATASOURCE) {
152
+ const js = loadJSFile(directory, jsFile.name)
153
+ // TODO: this isn't safe - but we need full node environment
154
+ // in future we should do this in a thread for safety
155
+ try {
156
+ eval(js)
157
+ } catch (err: any) {
158
+ const message = err?.message ? err.message : JSON.stringify(err)
159
+ throw new Error(`JS invalid: ${message}`)
160
+ }
161
+ }
162
+ const jsFileName = jsFile.name
163
+ const pluginId = generatePluginID(name)
164
+
165
+ // overwrite existing docs entirely if they exist
166
+ let rev
167
+ try {
168
+ const existing = await db.get(pluginId)
169
+ rev = existing._rev
170
+ } catch (err) {
171
+ rev = undefined
172
+ }
173
+ let doc = {
174
+ _id: pluginId,
175
+ _rev: rev,
176
+ ...metadata,
177
+ name,
178
+ version,
179
+ hash,
180
+ description,
181
+ jsUrl: `${bucketPath}${jsFileName}`,
182
+ }
183
+
184
+ if (source) {
185
+ doc = {
186
+ ...doc,
187
+ source,
188
+ }
189
+ }
190
+
191
+ const response = await db.put(doc)
192
+ ClientAppSocket.emit("plugin-update", { name, hash })
193
+ return {
194
+ ...doc,
195
+ _rev: response.rev,
196
+ }
197
+ }
198
+
199
+ export async function processPlugin(plugin: FileType, source?: string) {
200
+ if (!env.SELF_HOSTED) {
201
+ throw new Error("Plugins not supported outside of self-host.")
202
+ }
203
+
204
+ const { metadata, directory } = await fileUpload(plugin)
205
+ return await storePlugin(metadata, directory, source)
206
+ }
@@ -0,0 +1,56 @@
1
+ import {
2
+ getPluginMetadata,
3
+ findFileRec,
4
+ extractTarball,
5
+ deleteFolderFileSystem,
6
+ } from "../../../utilities/fileSystem"
7
+ import fetch from "node-fetch"
8
+ import { join } from "path"
9
+ import { downloadUnzipTarball } from "./utils"
10
+
11
+ export async function npmUpload(url: string, name: string, headers = {}) {
12
+ let npmTarballUrl = url
13
+ let pluginName = name
14
+
15
+ if (
16
+ !npmTarballUrl.includes("https://www.npmjs.com") &&
17
+ !npmTarballUrl.includes("https://registry.npmjs.org")
18
+ ) {
19
+ throw new Error("The plugin origin must be from NPM")
20
+ }
21
+
22
+ if (!npmTarballUrl.includes(".tgz")) {
23
+ const npmPackageURl = url.replace(
24
+ "https://www.npmjs.com/package/",
25
+ "https://registry.npmjs.org/"
26
+ )
27
+ const response = await fetch(npmPackageURl)
28
+ if (response.status !== 200) {
29
+ throw new Error("NPM Package not found")
30
+ }
31
+
32
+ let npmDetails = await response.json()
33
+ pluginName = npmDetails.name
34
+ const npmVersion = npmDetails["dist-tags"].latest
35
+ npmTarballUrl = npmDetails?.versions?.[npmVersion]?.dist?.tarball
36
+
37
+ if (!npmTarballUrl) {
38
+ throw new Error("NPM tarball url not found")
39
+ }
40
+ }
41
+
42
+ const path = await downloadUnzipTarball(npmTarballUrl, pluginName, headers)
43
+ const tarballPluginFile = findFileRec(path, ".tar.gz")
44
+ if (!tarballPluginFile) {
45
+ throw new Error("Tarball plugin file not found")
46
+ }
47
+
48
+ try {
49
+ await extractTarball(tarballPluginFile, path)
50
+ deleteFolderFileSystem(join(path, "package"))
51
+ } catch (err: any) {
52
+ throw new Error(err)
53
+ }
54
+
55
+ return await getPluginMetadata(path)
56
+ }
@@ -0,0 +1,4 @@
1
+ export { fileUpload } from "./file"
2
+ export { githubUpload } from "./github"
3
+ export { npmUpload } from "./npm"
4
+ export { urlUpload } from "./url"
@@ -0,0 +1,12 @@
1
+ import { downloadUnzipTarball } from "./utils"
2
+ import { getPluginMetadata } from "../../../utilities/fileSystem"
3
+
4
+ export async function urlUpload(url: string, name = "", headers = {}) {
5
+ if (!url.includes(".tar.gz")) {
6
+ throw new Error("Plugin must be compressed into a gzipped tarball.")
7
+ }
8
+
9
+ const path = await downloadUnzipTarball(url, name, headers)
10
+
11
+ return await getPluginMetadata(path)
12
+ }
@@ -0,0 +1,19 @@
1
+ import {
2
+ createTempFolder,
3
+ downloadTarballDirect,
4
+ } from "../../../utilities/fileSystem"
5
+
6
+ export async function downloadUnzipTarball(
7
+ url: string,
8
+ name: string,
9
+ headers = {}
10
+ ) {
11
+ try {
12
+ const path = createTempFolder(name)
13
+ await downloadTarballDirect(url, path, headers)
14
+
15
+ return path
16
+ } catch (e: any) {
17
+ throw new Error(e.message)
18
+ }
19
+ }
@@ -1,7 +1,14 @@
1
- const { getScreenParams, generateScreenID } = require("../../db/utils")
1
+ const {
2
+ getScreenParams,
3
+ generateScreenID,
4
+ getPluginParams,
5
+ DocumentType,
6
+ } = require("../../db/utils")
2
7
  const { AccessController } = require("@budibase/backend-core/roles")
3
8
  const { getAppDB } = require("@budibase/backend-core/context")
4
9
  const { events } = require("@budibase/backend-core")
10
+ const { getGlobalDB } = require("@budibase/backend-core/tenancy")
11
+ const { updateAppPackage } = require("./application")
5
12
 
6
13
  exports.fetch = async ctx => {
7
14
  const db = getAppDB()
@@ -32,6 +39,48 @@ exports.save = async ctx => {
32
39
 
33
40
  const response = await db.put(screen)
34
41
 
42
+ // Find any custom components being used
43
+ let pluginNames = []
44
+ let pluginAdded = false
45
+ findPlugins(screen.props, pluginNames)
46
+ if (pluginNames.length) {
47
+ const globalDB = getGlobalDB()
48
+ const pluginsResponse = await globalDB.allDocs(
49
+ getPluginParams(null, {
50
+ include_docs: true,
51
+ })
52
+ )
53
+ const requiredPlugins = pluginsResponse.rows
54
+ .map(row => row.doc)
55
+ .filter(plugin => {
56
+ return (
57
+ plugin.schema.type === "component" &&
58
+ pluginNames.includes(`plugin/${plugin.name}`)
59
+ )
60
+ })
61
+
62
+ // Update the app metadata
63
+ const application = await db.get(DocumentType.APP_METADATA)
64
+ let usedPlugins = application.usedPlugins || []
65
+
66
+ requiredPlugins.forEach(plugin => {
67
+ if (!usedPlugins.find(x => x._id === plugin._id)) {
68
+ pluginAdded = true
69
+ usedPlugins.push({
70
+ _id: plugin._id,
71
+ name: plugin.name,
72
+ version: plugin.version,
73
+ jsUrl: plugin.jsUrl,
74
+ hash: plugin.hash,
75
+ })
76
+ }
77
+ })
78
+
79
+ if (pluginAdded) {
80
+ await updateAppPackage({ usedPlugins }, ctx.appId)
81
+ }
82
+ }
83
+
35
84
  if (eventFn) {
36
85
  await eventFn(screen)
37
86
  }
@@ -40,6 +89,7 @@ exports.save = async ctx => {
40
89
  ...screen,
41
90
  _id: response.id,
42
91
  _rev: response.rev,
92
+ pluginAdded,
43
93
  }
44
94
  }
45
95
 
@@ -56,3 +106,18 @@ exports.destroy = async ctx => {
56
106
  }
57
107
  ctx.status = 200
58
108
  }
109
+
110
+ const findPlugins = (component, foundPlugins) => {
111
+ if (!component) {
112
+ return
113
+ }
114
+ if (component._component.startsWith("plugin")) {
115
+ if (!foundPlugins.includes(component._component)) {
116
+ foundPlugins.push(component._component)
117
+ }
118
+ }
119
+ if (!component._children || !component._children.length) {
120
+ return
121
+ }
122
+ component._children.forEach(child => findPlugins(child, foundPlugins))
123
+ }
@@ -113,6 +113,7 @@ export const serveApp = async function (ctx: any) {
113
113
  production: env.isProd(),
114
114
  appId,
115
115
  clientLibPath: clientLibraryPath(appId, appInfo.version, ctx),
116
+ usedPlugins: appInfo.usedPlugins,
116
117
  })
117
118
 
118
119
  const appHbs = loadHandlebarsFile(`${__dirname}/templates/app.hbs`)
@@ -3,6 +3,7 @@
3
3
  export let favicon = ""
4
4
 
5
5
  export let clientLibPath
6
+ export let usedPlugins
6
7
  </script>
7
8
 
8
9
  <svelte:head>
@@ -83,6 +84,15 @@
83
84
  </script>
84
85
  <script type="application/javascript" src={clientLibPath}>
85
86
  </script>
87
+ <!-- Custom components need inserted after the core client library -->
88
+ <!-- But before loadBudibase is called -->
89
+ {#if usedPlugins?.length}
90
+ {#each usedPlugins as plugin}
91
+ <script
92
+ type="application/javascript"
93
+ src={`/plugins/${plugin.jsUrl}`}></script>
94
+ {/each}
95
+ {/if}
86
96
  <script type="application/javascript">
87
97
  if (window.loadBudibase) {
88
98
  window.loadBudibase()
@@ -66,7 +66,9 @@
66
66
  customTheme,
67
67
  previewDevice,
68
68
  navigation,
69
- hiddenComponentIds
69
+ hiddenComponentIds,
70
+ usedPlugins,
71
+ location
70
72
  } = parsed
71
73
 
72
74
  // Set some flags so the app knows we're in the builder
@@ -81,6 +83,8 @@
81
83
  window["##BUDIBASE_PREVIEW_DEVICE##"] = previewDevice
82
84
  window["##BUDIBASE_PREVIEW_NAVIGATION##"] = navigation
83
85
  window["##BUDIBASE_HIDDEN_COMPONENT_IDS##"] = hiddenComponentIds
86
+ window["##BUDIBASE_USED_PLUGINS##"] = usedPlugins
87
+ window["##BUDIBASE_LOCATION##"] = location
84
88
 
85
89
  // Initialise app
86
90
  try {
@@ -24,6 +24,7 @@ import metadataRoutes from "./metadata"
24
24
  import devRoutes from "./dev"
25
25
  import cloudRoutes from "./cloud"
26
26
  import migrationRoutes from "./migrations"
27
+ import pluginRoutes from "./plugin"
27
28
 
28
29
  export { default as staticRoutes } from "./static"
29
30
  export { default as publicRoutes } from "./public"
@@ -57,4 +58,5 @@ export const mainRoutes = [
57
58
  tableRoutes,
58
59
  rowRoutes,
59
60
  migrationRoutes,
61
+ pluginRoutes,
60
62
  ]
@@ -0,0 +1,14 @@
1
+ import Router from "@koa/router"
2
+ import * as controller from "../controllers/plugin"
3
+ import authorized from "../../middleware/authorized"
4
+ import { BUILDER } from "@budibase/backend-core/permissions"
5
+
6
+ const router = new Router()
7
+
8
+ router
9
+ .post("/api/plugin/upload", authorized(BUILDER), controller.upload)
10
+ .post("/api/plugin", authorized(BUILDER), controller.create)
11
+ .get("/api/plugin", authorized(BUILDER), controller.fetch)
12
+ .delete("/api/plugin/:pluginId", authorized(BUILDER), controller.destroy)
13
+
14
+ export default router
@@ -231,20 +231,6 @@ describe("/queries", () => {
231
231
  url: `/api/queries/preview`,
232
232
  })
233
233
  })
234
-
235
- it("should fail with invalid integration type", async () => {
236
- const { datasource } = await createInvalidIntegration()
237
- await request
238
- .post(`/api/queries/preview`)
239
- .send({
240
- datasourceId: datasource._id,
241
- parameters: {},
242
- fields: {},
243
- queryVerb: "read",
244
- })
245
- .set(config.defaultHeaders())
246
- .expect(400)
247
- })
248
234
  })
249
235
 
250
236
  describe("execute", () => {
@@ -261,17 +247,14 @@ describe("/queries", () => {
261
247
  })
262
248
 
263
249
  it("should fail with invalid integration type", async () => {
264
- const { query, datasource } = await createInvalidIntegration()
265
- await request
266
- .post(`/api/queries/${query._id}`)
267
- .send({
268
- datasourceId: datasource._id,
269
- parameters: {},
270
- fields: {},
271
- queryVerb: "read",
272
- })
273
- .set(config.defaultHeaders())
274
- .expect(400)
250
+ let error
251
+ try {
252
+ await createInvalidIntegration()
253
+ } catch (err) {
254
+ error = err
255
+ }
256
+ expect(error).toBeDefined()
257
+ expect(error.message).toBe("No datasource implementation found.")
275
258
  })
276
259
  })
277
260
 
package/src/app.ts CHANGED
@@ -26,10 +26,13 @@ const bullboard = require("./automations/bullboard")
26
26
  const { logAlert } = require("@budibase/backend-core/logging")
27
27
  const { pinoSettings } = require("@budibase/backend-core")
28
28
  const { Thread } = require("./threads")
29
+ const fs = require("fs")
29
30
  import redis from "./utilities/redis"
30
31
  import * as migrations from "./migrations"
31
32
  import { events, installation, tenancy } from "@budibase/backend-core"
32
33
  import { createAdminUser, getChecklist } from "./utilities/workerRequests"
34
+ import { watch } from "./watch"
35
+ import { initialise as initialiseWebsockets } from "./websocket"
33
36
 
34
37
  const app = new Koa()
35
38
 
@@ -74,6 +77,7 @@ if (env.isProd()) {
74
77
 
75
78
  const server = http.createServer(app.callback())
76
79
  destroyable(server)
80
+ initialiseWebsockets(server)
77
81
 
78
82
  let shuttingDown = false,
79
83
  errCode = 0
@@ -139,6 +143,16 @@ module.exports = server.listen(env.PORT || 0, async () => {
139
143
  }
140
144
  }
141
145
 
146
+ // monitor plugin directory if required
147
+ if (
148
+ env.SELF_HOSTED &&
149
+ !env.MULTI_TENANCY &&
150
+ env.PLUGINS_DIR &&
151
+ fs.existsSync(env.PLUGINS_DIR)
152
+ ) {
153
+ watch()
154
+ }
155
+
142
156
  // check for version updates
143
157
  await installation.checkInstallVersion()
144
158
 
package/src/db/utils.js CHANGED
@@ -42,6 +42,7 @@ const DocumentType = {
42
42
  MEM_VIEW: "view",
43
43
  USER_FLAG: "flag",
44
44
  AUTOMATION_METADATA: "meta_au",
45
+ PLUGIN: "plg",
45
46
  }
46
47
 
47
48
  const InternalTables = {
@@ -370,6 +371,10 @@ exports.getMemoryViewParams = (otherProps = {}) => {
370
371
  return getDocParams(DocumentType.MEM_VIEW, null, otherProps)
371
372
  }
372
373
 
374
+ exports.generatePluginID = name => {
375
+ return `${DocumentType.PLUGIN}${SEPARATOR}${name}`
376
+ }
377
+
373
378
  /**
374
379
  * This can be used with the db.allDocs to get a list of IDs
375
380
  */
@@ -379,3 +384,10 @@ exports.getMultiIDParams = ids => {
379
384
  include_docs: true,
380
385
  }
381
386
  }
387
+
388
+ /**
389
+ * Gets parameters for retrieving automations, this is a utility function for the getDocParams function.
390
+ */
391
+ exports.getPluginParams = (pluginId = null, otherProps = {}) => {
392
+ return getDocParams(DocumentType.PLUGIN, pluginId, otherProps)
393
+ }
@@ -74,6 +74,7 @@ module.exports = {
74
74
  SQL_MAX_ROWS: process.env.SQL_MAX_ROWS,
75
75
  BB_ADMIN_USER_EMAIL: process.env.BB_ADMIN_USER_EMAIL,
76
76
  BB_ADMIN_USER_PASSWORD: process.env.BB_ADMIN_USER_PASSWORD,
77
+ PLUGINS_DIR: process.env.PLUGINS_DIR || "/plugins",
77
78
  // flags
78
79
  ALLOW_DEV_AUTOMATIONS: process.env.ALLOW_DEV_AUTOMATIONS,
79
80
  DISABLE_THREADING: process.env.DISABLE_THREADING,