@budibase/server 2.0.34-alpha.5 → 2.0.34-alpha.7

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 (182) hide show
  1. package/__mocks__/node-fetch.ts +12 -2
  2. package/builder/assets/{index.33bdfd8f.js → index.ffeaa56c.js} +1 -1
  3. package/builder/index.html +1 -1
  4. package/coverage/clover.xml +426 -496
  5. package/coverage/coverage-final.json +47 -49
  6. package/coverage/lcov-report/api/controllers/apikeys.js.html +6 -6
  7. package/coverage/lcov-report/api/controllers/automation.js.html +1 -1
  8. package/coverage/lcov-report/api/controllers/cloud.js.html +13 -13
  9. package/coverage/lcov-report/api/controllers/datasource.js.html +1 -1
  10. package/coverage/lcov-report/api/controllers/dev.js.html +20 -20
  11. package/coverage/lcov-report/api/controllers/index.html +9 -24
  12. package/coverage/lcov-report/api/controllers/integration.js.html +4 -4
  13. package/coverage/lcov-report/api/controllers/layout.js.html +1 -1
  14. package/coverage/lcov-report/api/controllers/metadata.js.html +9 -9
  15. package/coverage/lcov-report/api/controllers/migrations.js.html +4 -4
  16. package/coverage/lcov-report/api/controllers/permission.js.html +1 -1
  17. package/coverage/lcov-report/api/controllers/query/index.html +1 -1
  18. package/coverage/lcov-report/api/controllers/query/validation.js.html +4 -4
  19. package/coverage/lcov-report/api/controllers/role.js.html +1 -1
  20. package/coverage/lcov-report/api/controllers/routing.js.html +9 -9
  21. package/coverage/lcov-report/api/controllers/row/external.js.html +1 -1
  22. package/coverage/lcov-report/api/controllers/row/index.html +1 -1
  23. package/coverage/lcov-report/api/controllers/row/internal.js.html +1 -1
  24. package/coverage/lcov-report/api/controllers/row/internalSearch.js.html +1 -1
  25. package/coverage/lcov-report/api/controllers/row/staticFormula.js.html +1 -1
  26. package/coverage/lcov-report/api/controllers/row/utils.js.html +1 -1
  27. package/coverage/lcov-report/api/controllers/script.js.html +1 -1
  28. package/coverage/lcov-report/api/controllers/table/bulkFormula.js.html +1 -1
  29. package/coverage/lcov-report/api/controllers/table/external.js.html +1 -1
  30. package/coverage/lcov-report/api/controllers/table/index.html +1 -1
  31. package/coverage/lcov-report/api/controllers/table/index.js.html +1 -1
  32. package/coverage/lcov-report/api/controllers/templates.js.html +6 -6
  33. package/coverage/lcov-report/api/controllers/user.js.html +1 -1
  34. package/coverage/lcov-report/api/controllers/view/exporters.js.html +1 -1
  35. package/coverage/lcov-report/api/controllers/view/index.html +1 -1
  36. package/coverage/lcov-report/api/controllers/view/index.js.html +1 -1
  37. package/coverage/lcov-report/api/controllers/view/utils.js.html +1 -1
  38. package/coverage/lcov-report/api/controllers/view/viewBuilder.js.html +1 -1
  39. package/coverage/lcov-report/api/index.html +1 -1
  40. package/coverage/lcov-report/api/index.js.html +26 -26
  41. package/coverage/lcov-report/api/routes/analytics.js.html +7 -7
  42. package/coverage/lcov-report/api/routes/apikeys.js.html +8 -8
  43. package/coverage/lcov-report/api/routes/automation.js.html +11 -11
  44. package/coverage/lcov-report/api/routes/cloud.js.html +8 -8
  45. package/coverage/lcov-report/api/routes/component.js.html +8 -8
  46. package/coverage/lcov-report/api/routes/datasource.js.html +9 -9
  47. package/coverage/lcov-report/api/routes/deploy.js.html +8 -8
  48. package/coverage/lcov-report/api/routes/dev.js.html +13 -13
  49. package/coverage/lcov-report/api/routes/index.html +5 -20
  50. package/coverage/lcov-report/api/routes/integration.js.html +8 -8
  51. package/coverage/lcov-report/api/routes/layout.js.html +8 -8
  52. package/coverage/lcov-report/api/routes/metadata.js.html +9 -9
  53. package/coverage/lcov-report/api/routes/migrations.js.html +7 -7
  54. package/coverage/lcov-report/api/routes/permission.js.html +9 -9
  55. package/coverage/lcov-report/api/routes/query.js.html +10 -10
  56. package/coverage/lcov-report/api/routes/role.js.html +9 -9
  57. package/coverage/lcov-report/api/routes/routing.js.html +8 -8
  58. package/coverage/lcov-report/api/routes/screen.js.html +9 -9
  59. package/coverage/lcov-report/api/routes/script.js.html +1 -1
  60. package/coverage/lcov-report/api/routes/table.js.html +10 -10
  61. package/coverage/lcov-report/api/routes/templates.js.html +8 -8
  62. package/coverage/lcov-report/api/routes/user.js.html +8 -8
  63. package/coverage/lcov-report/api/routes/utils/index.html +1 -1
  64. package/coverage/lcov-report/api/routes/utils/validators.js.html +45 -45
  65. package/coverage/lcov-report/api/routes/view.js.html +10 -10
  66. package/coverage/lcov-report/automations/actions.js.html +1 -1
  67. package/coverage/lcov-report/automations/automationUtils.js.html +1 -1
  68. package/coverage/lcov-report/automations/bullboard.js.html +25 -10
  69. package/coverage/lcov-report/automations/index.html +16 -16
  70. package/coverage/lcov-report/automations/index.js.html +4 -4
  71. package/coverage/lcov-report/automations/steps/bash.js.html +1 -1
  72. package/coverage/lcov-report/automations/steps/delay.js.html +1 -1
  73. package/coverage/lcov-report/automations/steps/discord.js.html +1 -1
  74. package/coverage/lcov-report/automations/steps/executeQuery.js.html +1 -1
  75. package/coverage/lcov-report/automations/steps/executeScript.js.html +1 -1
  76. package/coverage/lcov-report/automations/steps/filter.js.html +1 -1
  77. package/coverage/lcov-report/automations/steps/index.html +1 -1
  78. package/coverage/lcov-report/automations/steps/integromat.js.html +1 -1
  79. package/coverage/lcov-report/automations/steps/loop.js.html +1 -1
  80. package/coverage/lcov-report/automations/steps/outgoingWebhook.js.html +1 -1
  81. package/coverage/lcov-report/automations/steps/queryRows.js.html +1 -1
  82. package/coverage/lcov-report/automations/steps/sendSmtpEmail.js.html +1 -1
  83. package/coverage/lcov-report/automations/steps/serverLog.js.html +1 -1
  84. package/coverage/lcov-report/automations/steps/slack.js.html +1 -1
  85. package/coverage/lcov-report/automations/steps/updateRow.js.html +1 -1
  86. package/coverage/lcov-report/automations/steps/utils.js.html +1 -1
  87. package/coverage/lcov-report/automations/steps/zapier.js.html +1 -1
  88. package/coverage/lcov-report/automations/triggerInfo/app.js.html +1 -1
  89. package/coverage/lcov-report/automations/triggerInfo/cron.js.html +1 -1
  90. package/coverage/lcov-report/automations/triggerInfo/index.html +1 -1
  91. package/coverage/lcov-report/automations/triggerInfo/index.js.html +1 -1
  92. package/coverage/lcov-report/automations/triggerInfo/rowDeleted.js.html +1 -1
  93. package/coverage/lcov-report/automations/triggerInfo/rowSaved.js.html +1 -1
  94. package/coverage/lcov-report/automations/triggerInfo/rowUpdated.js.html +1 -1
  95. package/coverage/lcov-report/automations/triggerInfo/webhook.js.html +1 -1
  96. package/coverage/lcov-report/automations/triggers.js.html +23 -23
  97. package/coverage/lcov-report/constants/index.html +5 -5
  98. package/coverage/lcov-report/constants/index.js.html +4 -16
  99. package/coverage/lcov-report/constants/layouts.js.html +1 -1
  100. package/coverage/lcov-report/constants/screens.js.html +1 -1
  101. package/coverage/lcov-report/db/inMemoryView.js.html +1 -1
  102. package/coverage/lcov-report/db/index.html +1 -1
  103. package/coverage/lcov-report/db/index.js.html +1 -1
  104. package/coverage/lcov-report/db/linkedRows/LinkController.js.html +1 -1
  105. package/coverage/lcov-report/db/linkedRows/index.html +1 -1
  106. package/coverage/lcov-report/db/linkedRows/index.js.html +1 -1
  107. package/coverage/lcov-report/db/linkedRows/linkUtils.js.html +1 -1
  108. package/coverage/lcov-report/db/newid.js.html +1 -1
  109. package/coverage/lcov-report/db/utils.js.html +2 -2
  110. package/coverage/lcov-report/events/AutomationEmitter.js.html +1 -1
  111. package/coverage/lcov-report/events/index.html +1 -1
  112. package/coverage/lcov-report/events/index.js.html +2 -2
  113. package/coverage/lcov-report/events/utils.js.html +1 -1
  114. package/coverage/lcov-report/index.html +45 -45
  115. package/coverage/lcov-report/integrations/tests/TestConfiguration.js.html +1 -1
  116. package/coverage/lcov-report/integrations/tests/index.html +1 -1
  117. package/coverage/lcov-report/middleware/appInfo.js.html +5 -5
  118. package/coverage/lcov-report/middleware/builder.js.html +9 -9
  119. package/coverage/lcov-report/middleware/currentapp.js.html +14 -14
  120. package/coverage/lcov-report/middleware/index.html +1 -1
  121. package/coverage/lcov-report/middleware/joi-validator.js.html +1 -1
  122. package/coverage/lcov-report/middleware/publicApi.js.html +5 -5
  123. package/coverage/lcov-report/middleware/resourceId.js.html +22 -22
  124. package/coverage/lcov-report/middleware/selfhost.js.html +1 -1
  125. package/coverage/lcov-report/middleware/utils.js.html +3 -3
  126. package/coverage/lcov-report/utilities/budibaseDir.js.html +1 -1
  127. package/coverage/lcov-report/utilities/centralPath.js.html +1 -1
  128. package/coverage/lcov-report/utilities/csvParser.js.html +1 -1
  129. package/coverage/lcov-report/utilities/global.js.html +1 -1
  130. package/coverage/lcov-report/utilities/index.html +20 -20
  131. package/coverage/lcov-report/utilities/index.js.html +2 -2
  132. package/coverage/lcov-report/utilities/plugins.js.html +1 -1
  133. package/coverage/lcov-report/utilities/queue/inMemoryQueue.js.html +1 -1
  134. package/coverage/lcov-report/utilities/queue/index.html +1 -1
  135. package/coverage/lcov-report/utilities/routing/index.html +1 -1
  136. package/coverage/lcov-report/utilities/routing/index.js.html +6 -6
  137. package/coverage/lcov-report/utilities/rowProcessor/index.html +1 -1
  138. package/coverage/lcov-report/utilities/rowProcessor/index.js.html +1 -1
  139. package/coverage/lcov-report/utilities/rowProcessor/utils.js.html +1 -1
  140. package/coverage/lcov-report/utilities/scriptRunner.js.html +1 -1
  141. package/coverage/lcov-report/utilities/security.js.html +1 -1
  142. package/coverage/lcov-report/utilities/statusCodes.js.html +1 -1
  143. package/coverage/lcov-report/utilities/usageQuota/index.html +1 -1
  144. package/coverage/lcov-report/utilities/usageQuota/rows.js.html +1 -1
  145. package/coverage/lcov-report/utilities/usageQuota/usageQuoteReset.js.html +1 -1
  146. package/coverage/lcov-report/utilities/users.js.html +1 -1
  147. package/coverage/lcov-report/utilities/workerRequests.js.html +46 -46
  148. package/coverage/lcov.info +492 -596
  149. package/dist/api/controllers/webhook.js +88 -89
  150. package/dist/api/routes/utils/validators.js +2 -2
  151. package/dist/api/routes/webhook.js +39 -11
  152. package/dist/app.js +2 -84
  153. package/dist/automations/bullboard.js +7 -2
  154. package/dist/automations/utils.js +5 -37
  155. package/dist/constants/index.js +0 -3
  156. package/dist/package.json +6 -6
  157. package/dist/sdk/app/automations/index.js +29 -0
  158. package/dist/sdk/app/automations/webhook.js +54 -0
  159. package/dist/sdk/app/backups/imports.js +24 -0
  160. package/dist/sdk/index.js +2 -0
  161. package/dist/startup.js +152 -0
  162. package/dist/tests/utilities/TestConfiguration.js +8 -0
  163. package/dist/tsconfig.build.tsbuildinfo +1 -1
  164. package/package.json +7 -7
  165. package/src/api/controllers/{webhook.js → webhook.ts} +33 -48
  166. package/src/api/routes/utils/validators.js +2 -2
  167. package/src/api/routes/{webhook.js → webhook.ts} +7 -6
  168. package/src/app.ts +3 -108
  169. package/src/automations/bullboard.js +6 -1
  170. package/src/automations/utils.ts +12 -20
  171. package/src/constants/index.js +0 -4
  172. package/src/definitions/automations.ts +1 -6
  173. package/src/migrations/functions/tests/syncQuotas.spec.js +1 -3
  174. package/src/migrations/functions/tests/userEmailViewCasing.spec.js +6 -4
  175. package/src/sdk/app/automations/index.ts +5 -0
  176. package/src/sdk/app/automations/webhook.ts +43 -0
  177. package/src/sdk/app/backups/imports.ts +39 -5
  178. package/src/sdk/index.ts +2 -0
  179. package/src/startup.ts +138 -0
  180. package/src/tests/utilities/TestConfiguration.js +7 -0
  181. package/coverage/lcov-report/api/controllers/webhook.js.html +0 -424
  182. package/coverage/lcov-report/api/routes/webhook.js.html +0 -163
@@ -8,103 +8,102 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
9
  });
10
10
  };
11
- const { generateWebhookID, getWebhookParams } = require("../../db/utils");
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.trigger = exports.buildSchema = exports.destroy = exports.save = exports.fetch = void 0;
16
+ const utils_1 = require("../../db/utils");
17
+ const triggers_1 = __importDefault(require("../../automations/triggers"));
18
+ const backend_core_1 = require("@budibase/backend-core");
19
+ const types_1 = require("@budibase/types");
20
+ const sdk_1 = __importDefault(require("../../sdk"));
12
21
  const toJsonSchema = require("to-json-schema");
13
22
  const validate = require("jsonschema").validate;
14
- const { WebhookType } = require("../../constants");
15
- const triggers = require("../../automations/triggers");
16
- const { getProdAppID } = require("@budibase/backend-core/db");
17
- const { getAppDB, updateAppId } = require("@budibase/backend-core/context");
18
23
  const AUTOMATION_DESCRIPTION = "Generated from Webhook Schema";
19
- function Webhook(name, type, target) {
20
- this.live = true;
21
- this.name = name;
22
- this.action = {
23
- type,
24
- target,
25
- };
24
+ function fetch(ctx) {
25
+ return __awaiter(this, void 0, void 0, function* () {
26
+ const db = backend_core_1.context.getAppDB();
27
+ const response = yield db.allDocs((0, utils_1.getWebhookParams)(null, {
28
+ include_docs: true,
29
+ }));
30
+ ctx.body = response.rows.map((row) => row.doc);
31
+ });
26
32
  }
27
- exports.Webhook = Webhook;
28
- exports.fetch = (ctx) => __awaiter(void 0, void 0, void 0, function* () {
29
- const db = getAppDB();
30
- const response = yield db.allDocs(getWebhookParams(null, {
31
- include_docs: true,
32
- }));
33
- ctx.body = response.rows.map(row => row.doc);
34
- });
35
- exports.save = (ctx) => __awaiter(void 0, void 0, void 0, function* () {
36
- const db = getAppDB();
37
- const webhook = ctx.request.body;
38
- webhook.appId = ctx.appId;
39
- // check that the webhook exists
40
- if (webhook._id) {
41
- yield db.get(webhook._id);
42
- }
43
- else {
44
- webhook._id = generateWebhookID();
45
- }
46
- const response = yield db.put(webhook);
47
- webhook._rev = response.rev;
48
- ctx.body = {
49
- message: "Webhook created successfully",
50
- webhook,
51
- };
52
- });
53
- exports.destroy = (ctx) => __awaiter(void 0, void 0, void 0, function* () {
54
- const db = getAppDB();
55
- ctx.body = yield db.remove(ctx.params.id, ctx.params.rev);
56
- });
57
- exports.buildSchema = (ctx) => __awaiter(void 0, void 0, void 0, function* () {
58
- yield updateAppId(ctx.params.instance);
59
- const db = getAppDB();
60
- const webhook = yield db.get(ctx.params.id);
61
- webhook.bodySchema = toJsonSchema(ctx.request.body);
62
- // update the automation outputs
63
- if (webhook.action.type === WebhookType.AUTOMATION) {
64
- let automation = yield db.get(webhook.action.target);
65
- const autoOutputs = automation.definition.trigger.schema.outputs;
66
- let properties = webhook.bodySchema.properties;
67
- // reset webhook outputs
68
- autoOutputs.properties = {
69
- body: autoOutputs.properties.body,
33
+ exports.fetch = fetch;
34
+ function save(ctx) {
35
+ return __awaiter(this, void 0, void 0, function* () {
36
+ const webhook = yield sdk_1.default.automations.webhook.save(ctx.request.body);
37
+ ctx.body = {
38
+ message: "Webhook created successfully",
39
+ webhook,
70
40
  };
71
- for (let prop of Object.keys(properties)) {
72
- autoOutputs.properties[prop] = {
73
- type: properties[prop].type,
74
- description: AUTOMATION_DESCRIPTION,
41
+ });
42
+ }
43
+ exports.save = save;
44
+ function destroy(ctx) {
45
+ return __awaiter(this, void 0, void 0, function* () {
46
+ ctx.body = yield sdk_1.default.automations.webhook.destroy(ctx.params.id, ctx.params.rev);
47
+ });
48
+ }
49
+ exports.destroy = destroy;
50
+ function buildSchema(ctx) {
51
+ return __awaiter(this, void 0, void 0, function* () {
52
+ yield backend_core_1.context.updateAppId(ctx.params.instance);
53
+ const db = backend_core_1.context.getAppDB();
54
+ const webhook = (yield db.get(ctx.params.id));
55
+ webhook.bodySchema = toJsonSchema(ctx.request.body);
56
+ // update the automation outputs
57
+ if (webhook.action.type === types_1.WebhookActionType.AUTOMATION) {
58
+ let automation = (yield db.get(webhook.action.target));
59
+ const autoOutputs = automation.definition.trigger.schema.outputs;
60
+ let properties = webhook.bodySchema.properties;
61
+ // reset webhook outputs
62
+ autoOutputs.properties = {
63
+ body: autoOutputs.properties.body,
75
64
  };
65
+ for (let prop of Object.keys(properties)) {
66
+ autoOutputs.properties[prop] = {
67
+ type: properties[prop].type,
68
+ description: AUTOMATION_DESCRIPTION,
69
+ };
70
+ }
71
+ yield db.put(automation);
76
72
  }
77
- yield db.put(automation);
78
- }
79
- ctx.body = yield db.put(webhook);
80
- });
81
- exports.trigger = (ctx) => __awaiter(void 0, void 0, void 0, function* () {
82
- const prodAppId = getProdAppID(ctx.params.instance);
83
- yield updateAppId(prodAppId);
84
- try {
85
- const db = getAppDB();
86
- const webhook = yield db.get(ctx.params.id);
87
- // validate against the schema
88
- if (webhook.bodySchema) {
89
- validate(ctx.request.body, webhook.bodySchema);
90
- }
91
- const target = yield db.get(webhook.action.target);
92
- if (webhook.action.type === WebhookType.AUTOMATION) {
93
- // trigger with both the pure request and then expand it
94
- // incase the user has produced a schema to bind to
95
- yield triggers.externalTrigger(target, Object.assign(Object.assign({ body: ctx.request.body }, ctx.request.body), { appId: prodAppId }));
96
- }
97
- ctx.status = 200;
98
- ctx.body = {
99
- message: "Webhook trigger fired successfully",
100
- };
101
- }
102
- catch (err) {
103
- if (err.status === 404) {
73
+ ctx.body = yield db.put(webhook);
74
+ });
75
+ }
76
+ exports.buildSchema = buildSchema;
77
+ function trigger(ctx) {
78
+ return __awaiter(this, void 0, void 0, function* () {
79
+ const prodAppId = backend_core_1.db.getProdAppID(ctx.params.instance);
80
+ yield backend_core_1.context.updateAppId(prodAppId);
81
+ try {
82
+ const db = backend_core_1.context.getAppDB();
83
+ const webhook = (yield db.get(ctx.params.id));
84
+ // validate against the schema
85
+ if (webhook.bodySchema) {
86
+ validate(ctx.request.body, webhook.bodySchema);
87
+ }
88
+ const target = yield db.get(webhook.action.target);
89
+ if (webhook.action.type === types_1.WebhookActionType.AUTOMATION) {
90
+ // trigger with both the pure request and then expand it
91
+ // incase the user has produced a schema to bind to
92
+ yield triggers_1.default.externalTrigger(target, Object.assign(Object.assign({ body: ctx.request.body }, ctx.request.body), { appId: prodAppId }));
93
+ }
104
94
  ctx.status = 200;
105
95
  ctx.body = {
106
- message: "Application not deployed yet.",
96
+ message: "Webhook trigger fired successfully",
107
97
  };
108
98
  }
109
- }
110
- });
99
+ catch (err) {
100
+ if (err.status === 404) {
101
+ ctx.status = 200;
102
+ ctx.body = {
103
+ message: "Application not deployed yet.",
104
+ };
105
+ }
106
+ }
107
+ });
108
+ }
109
+ exports.trigger = trigger;
@@ -1,8 +1,8 @@
1
1
  "use strict";
2
2
  const { joiValidator } = require("@budibase/backend-core/auth");
3
3
  const { DataSourceOperation } = require("../../../constants");
4
- const { WebhookType } = require("../../../constants");
5
4
  const { BUILTIN_PERMISSION_IDS, PermissionLevels, } = require("@budibase/backend-core/permissions");
5
+ const { WebhookActionType } = require("@budibase/types");
6
6
  const Joi = require("joi");
7
7
  const OPTIONAL_STRING = Joi.string().optional().allow(null).allow("");
8
8
  const OPTIONAL_NUMBER = Joi.number().optional().allow(null);
@@ -113,7 +113,7 @@ exports.webhookValidator = () => {
113
113
  name: Joi.string().required(),
114
114
  bodySchema: Joi.object().optional(),
115
115
  action: Joi.object({
116
- type: Joi.string().required().valid(WebhookType.AUTOMATION),
116
+ type: Joi.string().required().valid(WebhookActionType.AUTOMATION),
117
117
  target: Joi.string().required(),
118
118
  }).required(),
119
119
  }).unknown(true));
@@ -1,15 +1,43 @@
1
1
  "use strict";
2
- const Router = require("@koa/router");
3
- const controller = require("../controllers/webhook");
4
- const authorized = require("../../middleware/authorized");
5
- const { BUILDER } = require("@budibase/backend-core/permissions");
6
- const { webhookValidator } = require("./utils/validators");
7
- const router = new Router();
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ const router_1 = __importDefault(require("@koa/router"));
30
+ const controller = __importStar(require("../controllers/webhook"));
31
+ const authorized_1 = __importDefault(require("../../middleware/authorized"));
32
+ const backend_core_1 = require("@budibase/backend-core");
33
+ const validators_1 = require("./utils/validators");
34
+ const BUILDER = backend_core_1.permissions.BUILDER;
35
+ const router = new router_1.default();
8
36
  router
9
- .get("/api/webhooks", authorized(BUILDER), controller.fetch)
10
- .put("/api/webhooks", authorized(BUILDER), webhookValidator(), controller.save)
11
- .delete("/api/webhooks/:id/:rev", authorized(BUILDER), controller.destroy)
12
- .post("/api/webhooks/schema/:instance/:id", authorized(BUILDER), controller.buildSchema)
37
+ .get("/api/webhooks", (0, authorized_1.default)(BUILDER), controller.fetch)
38
+ .put("/api/webhooks", (0, authorized_1.default)(BUILDER), (0, validators_1.webhookValidator)(), controller.save)
39
+ .delete("/api/webhooks/:id/:rev", (0, authorized_1.default)(BUILDER), controller.destroy)
40
+ .post("/api/webhooks/schema/:instance/:id", (0, authorized_1.default)(BUILDER), controller.buildSchema)
13
41
  // this shouldn't have authorisation, right now its always public
14
42
  .post("/api/webhooks/trigger/:instance/:id", controller.trigger);
15
- module.exports = router;
43
+ exports.default = router;
package/dist/app.js CHANGED
@@ -49,26 +49,16 @@ db_1.default.init();
49
49
  const Koa = require("koa");
50
50
  const destroyable = require("server-destroy");
51
51
  const koaBody = require("koa-body");
52
- const pino = require("koa-pino-logger");
53
52
  const http = require("http");
54
53
  const api = require("./api");
55
- const eventEmitter = require("./events");
56
54
  const automations = require("./automations/index");
57
55
  const Sentry = require("@sentry/node");
58
- const fileSystem = require("./utilities/fileSystem");
59
- const bullboard = require("./automations/bullboard");
60
56
  const { logAlert } = require("@budibase/backend-core/logging");
61
- const { pinoSettings } = require("@budibase/backend-core");
62
57
  const { Thread } = require("./threads");
63
- const fs = require("fs");
64
58
  const redis_1 = __importDefault(require("./utilities/redis"));
65
- const migrations = __importStar(require("./migrations"));
66
59
  const backend_core_1 = require("@budibase/backend-core");
67
- const workerRequests_1 = require("./utilities/workerRequests");
68
- const watch_1 = require("./watch");
69
60
  const websocket_1 = require("./websocket");
70
- const sdk_1 = __importDefault(require("./sdk"));
71
- const pro = __importStar(require("@budibase/pro"));
61
+ const startup_1 = require("./startup");
72
62
  const app = new Koa();
73
63
  // set up top level koa middleware
74
64
  app.use(koaBody({
@@ -79,15 +69,6 @@ app.use(koaBody({
79
69
  enableTypes: ["json", "form", "text"],
80
70
  parsedMethods: ["POST", "PUT", "PATCH", "DELETE"],
81
71
  }));
82
- app.use(pino(pinoSettings()));
83
- if (!env.isTest()) {
84
- const plugin = bullboard.init();
85
- app.use(plugin);
86
- }
87
- app.context.eventEmitter = eventEmitter;
88
- app.context.auth = {};
89
- // api routes
90
- app.use(api.router.routes());
91
72
  if (env.isProd()) {
92
73
  env._set("NODE_ENV", "production");
93
74
  Sentry.init();
@@ -120,71 +101,8 @@ server.on("close", () => __awaiter(void 0, void 0, void 0, function* () {
120
101
  process.exit(errCode);
121
102
  }
122
103
  }));
123
- const initPro = () => __awaiter(void 0, void 0, void 0, function* () {
124
- yield pro.init({
125
- backups: {
126
- processing: {
127
- exportAppFn: sdk_1.default.backups.exportApp,
128
- importAppFn: sdk_1.default.backups.importApp,
129
- statsFn: sdk_1.default.backups.calculateBackupStats,
130
- },
131
- },
132
- });
133
- });
134
104
  module.exports = server.listen(env.PORT || 0, () => __awaiter(void 0, void 0, void 0, function* () {
135
- var _a;
136
- console.log(`Budibase running on ${JSON.stringify(server.address())}`);
137
- env._set("PORT", server.address().port);
138
- eventEmitter.emitPort(env.PORT);
139
- fileSystem.init();
140
- yield redis_1.default.init();
141
- // run migrations on startup if not done via http
142
- // not recommended in a clustered environment
143
- if (!env.HTTP_MIGRATIONS && !env.isTest()) {
144
- try {
145
- yield migrations.migrate();
146
- }
147
- catch (e) {
148
- logAlert("Error performing migrations. Exiting.", e);
149
- shutdown();
150
- }
151
- }
152
- // check and create admin user if required
153
- if (env.SELF_HOSTED &&
154
- !env.MULTI_TENANCY &&
155
- env.BB_ADMIN_USER_EMAIL &&
156
- env.BB_ADMIN_USER_PASSWORD) {
157
- const checklist = yield (0, workerRequests_1.getChecklist)();
158
- if (!((_a = checklist === null || checklist === void 0 ? void 0 : checklist.adminUser) === null || _a === void 0 ? void 0 : _a.checked)) {
159
- try {
160
- const tenantId = backend_core_1.tenancy.getTenantId();
161
- const user = yield (0, workerRequests_1.createAdminUser)(env.BB_ADMIN_USER_EMAIL, env.BB_ADMIN_USER_PASSWORD, tenantId);
162
- // Need to set up an API key for automated integration tests
163
- if (env.isTest()) {
164
- yield (0, workerRequests_1.generateApiKey)(user._id);
165
- }
166
- console.log("Admin account automatically created for", env.BB_ADMIN_USER_EMAIL);
167
- }
168
- catch (e) {
169
- logAlert("Error creating initial admin user. Exiting.", e);
170
- shutdown();
171
- }
172
- }
173
- }
174
- // monitor plugin directory if required
175
- if (env.SELF_HOSTED &&
176
- !env.MULTI_TENANCY &&
177
- env.PLUGINS_DIR &&
178
- fs.existsSync(env.PLUGINS_DIR)) {
179
- (0, watch_1.watch)();
180
- }
181
- // check for version updates
182
- yield backend_core_1.installation.checkInstallVersion();
183
- // done last - these will never complete
184
- let promises = [];
185
- promises.push(automations.init());
186
- promises.push(initPro());
187
- yield Promise.all(promises);
105
+ yield (0, startup_1.startup)(app, server);
188
106
  }));
189
107
  const shutdown = () => {
190
108
  server.close();
@@ -13,11 +13,16 @@ const { BullAdapter } = require("@bull-board/api/bullAdapter");
13
13
  const { KoaAdapter } = require("@bull-board/koa");
14
14
  const { queue } = require("@budibase/backend-core");
15
15
  const automation = require("../threads/automation");
16
+ const { backups } = require("@budibase/pro");
16
17
  let automationQueue = queue.createQueue(queue.JobQueue.AUTOMATION, automation.removeStalled);
17
18
  const PATH_PREFIX = "/bulladmin";
18
- exports.init = () => {
19
+ exports.init = () => __awaiter(void 0, void 0, void 0, function* () {
19
20
  // Set up queues for bull board admin
21
+ const backupQueue = yield backups.getBackupQueue();
20
22
  const queues = [automationQueue];
23
+ if (backupQueue) {
24
+ queues.push(backupQueue);
25
+ }
21
26
  const adapters = [];
22
27
  const serverAdapter = new KoaAdapter();
23
28
  for (let queue of queues) {
@@ -29,7 +34,7 @@ exports.init = () => {
29
34
  });
30
35
  serverAdapter.setBasePath(PATH_PREFIX);
31
36
  return serverAdapter.registerPlugin();
32
- };
37
+ });
33
38
  exports.shutdown = () => __awaiter(void 0, void 0, void 0, function* () {
34
39
  yield queue.shutdown();
35
40
  });
@@ -1,27 +1,4 @@
1
1
  "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
2
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
3
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
4
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -38,7 +15,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
38
15
  exports.isErrorInOutput = exports.isRecurring = exports.cleanupAutomations = exports.checkForWebhooks = exports.enableCronTrigger = exports.isRebootTrigger = exports.isCronTrigger = exports.clearMetadata = exports.disableCronById = exports.disableAllCrons = exports.removeDeprecated = exports.updateTestHistory = exports.processEvent = void 0;
39
16
  const threads_1 = require("../threads");
40
17
  const triggerInfo_1 = require("./triggerInfo");
41
- const webhooks = __importStar(require("../api/controllers/webhook"));
42
18
  const bullboard_1 = require("./bullboard");
43
19
  const newid_1 = __importDefault(require("../db/newid"));
44
20
  const utilities_1 = require("../utilities");
@@ -49,6 +25,8 @@ const fp_1 = require("lodash/fp");
49
25
  const context_1 = require("@budibase/backend-core/context");
50
26
  const backend_core_1 = require("@budibase/backend-core");
51
27
  const pro_1 = require("@budibase/pro");
28
+ const types_1 = require("@budibase/types");
29
+ const sdk_1 = __importDefault(require("../sdk"));
52
30
  const REBOOT_CRON = "@reboot";
53
31
  const WH_STEP_ID = triggerInfo_1.definitions.WEBHOOK.stepId;
54
32
  const CRON_STEP_ID = triggerInfo_1.definitions.CRON.stepId;
@@ -217,16 +195,12 @@ function checkForWebhooks({ oldAuto, newAuto }) {
217
195
  let db = (0, context_1.getAppDB)();
218
196
  // need to get the webhook to get the rev
219
197
  const webhook = yield db.get(oldTrigger.webhookId);
220
- const ctx = {
221
- appId,
222
- params: { id: webhook._id, rev: webhook._rev },
223
- };
224
198
  // might be updating - reset the inputs to remove the URLs
225
199
  if (newTrigger) {
226
200
  delete newTrigger.webhookId;
227
201
  newTrigger.inputs = {};
228
202
  }
229
- yield webhooks.destroy(ctx);
203
+ yield sdk_1.default.automations.webhook.destroy(webhook._id, webhook._rev);
230
204
  }
231
205
  catch (err) {
232
206
  // don't worry about not being able to delete, if it doesn't exist all good
@@ -235,14 +209,8 @@ function checkForWebhooks({ oldAuto, newAuto }) {
235
209
  // need to create webhook
236
210
  if ((!isWebhookTrigger(oldAuto) || triggerChanged) &&
237
211
  isWebhookTrigger(newAuto)) {
238
- const ctx = {
239
- appId,
240
- request: {
241
- body: new webhooks.Webhook("Automation webhook", constants_1.WebhookType.AUTOMATION, newAuto._id),
242
- },
243
- };
244
- yield webhooks.save(ctx);
245
- const id = ctx.body.webhook._id;
212
+ const webhook = yield sdk_1.default.automations.webhook.save(sdk_1.default.automations.webhook.newDoc("Automation webhook", types_1.WebhookActionType.AUTOMATION, newAuto._id));
213
+ const id = webhook._id;
246
214
  newTrigger.webhookId = id;
247
215
  // the app ID has to be development for this endpoint
248
216
  // it can only be used when building the app
@@ -176,9 +176,6 @@ exports.BuildSchemaErrors = {
176
176
  NO_KEY: "no_key",
177
177
  INVALID_COLUMN: "invalid_column",
178
178
  };
179
- exports.WebhookType = {
180
- AUTOMATION: "automation",
181
- };
182
179
  exports.AutomationErrors = {
183
180
  INCORRECT_TYPE: "INCORRECT_TYPE",
184
181
  MAX_ITERATIONS: "MAX_ITERATIONS_REACHED",
package/dist/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@budibase/server",
3
3
  "email": "hi@budibase.com",
4
- "version": "2.0.34-alpha.4",
4
+ "version": "2.0.34-alpha.6",
5
5
  "description": "Budibase Web Server",
6
6
  "main": "src/index.ts",
7
7
  "repository": {
@@ -77,11 +77,11 @@
77
77
  "license": "GPL-3.0",
78
78
  "dependencies": {
79
79
  "@apidevtools/swagger-parser": "10.0.3",
80
- "@budibase/backend-core": "2.0.34-alpha.4",
81
- "@budibase/client": "2.0.34-alpha.4",
82
- "@budibase/pro": "2.0.34-alpha.4",
83
- "@budibase/string-templates": "2.0.34-alpha.4",
84
- "@budibase/types": "2.0.34-alpha.4",
80
+ "@budibase/backend-core": "2.0.34-alpha.6",
81
+ "@budibase/client": "2.0.34-alpha.6",
82
+ "@budibase/pro": "2.0.34-alpha.6",
83
+ "@budibase/string-templates": "2.0.34-alpha.6",
84
+ "@budibase/types": "2.0.34-alpha.6",
85
85
  "@bull-board/api": "3.7.0",
86
86
  "@bull-board/koa": "3.9.4",
87
87
  "@elastic/elasticsearch": "7.10.0",
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ const webhook = __importStar(require("./webhook"));
27
+ exports.default = {
28
+ webhook,
29
+ };
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.destroy = exports.save = exports.newDoc = void 0;
13
+ const backend_core_1 = require("@budibase/backend-core");
14
+ const utils_1 = require("../../../db/utils");
15
+ function isWebhookID(id) {
16
+ return id.startsWith(backend_core_1.db.DocumentType.WEBHOOK);
17
+ }
18
+ function newDoc(name, type, target) {
19
+ return {
20
+ live: true,
21
+ name,
22
+ action: {
23
+ type,
24
+ target,
25
+ },
26
+ };
27
+ }
28
+ exports.newDoc = newDoc;
29
+ function save(webhook) {
30
+ return __awaiter(this, void 0, void 0, function* () {
31
+ const db = backend_core_1.context.getAppDB();
32
+ // check that the webhook exists
33
+ if (webhook._id && isWebhookID(webhook._id)) {
34
+ yield db.get(webhook._id);
35
+ }
36
+ else {
37
+ webhook._id = (0, utils_1.generateWebhookID)();
38
+ }
39
+ const response = yield db.put(webhook);
40
+ webhook._rev = response.rev;
41
+ return webhook;
42
+ });
43
+ }
44
+ exports.save = save;
45
+ function destroy(id, rev) {
46
+ return __awaiter(this, void 0, void 0, function* () {
47
+ const db = backend_core_1.context.getAppDB();
48
+ if (!id || !isWebhookID(id)) {
49
+ throw new Error("Provided webhook ID is not valid.");
50
+ }
51
+ return yield db.remove(id, rev);
52
+ });
53
+ }
54
+ exports.destroy = destroy;
@@ -23,6 +23,7 @@ const constants_2 = require("../../../constants");
23
23
  const path_1 = require("path");
24
24
  const fs_1 = __importDefault(require("fs"));
25
25
  const __1 = __importDefault(require("../../"));
26
+ const types_1 = require("@budibase/types");
26
27
  const uuid = require("uuid/v4");
27
28
  const tar = require("tar");
28
29
  function updateAttachmentColumns(prodAppId, db) {
@@ -76,6 +77,28 @@ function updateAttachmentColumns(prodAppId, db) {
76
77
  }
77
78
  });
78
79
  }
80
+ function updateAutomations(prodAppId, db) {
81
+ return __awaiter(this, void 0, void 0, function* () {
82
+ const automations = (yield db.allDocs((0, utils_1.getAutomationParams)(null, {
83
+ include_docs: true,
84
+ }))).rows.map(row => row.doc);
85
+ const devAppId = backend_core_1.db.getDevAppID(prodAppId);
86
+ let toSave = [];
87
+ for (let automation of automations) {
88
+ const oldDevAppId = automation.appId, oldProdAppId = backend_core_1.db.getProdAppID(automation.appId);
89
+ if (automation.definition.trigger.stepId === types_1.AutomationTriggerStepId.WEBHOOK) {
90
+ const old = automation.definition.trigger.inputs;
91
+ automation.definition.trigger.inputs = {
92
+ schemaUrl: old.schemaUrl.replace(oldDevAppId, devAppId),
93
+ triggerUrl: old.triggerUrl.replace(oldProdAppId, prodAppId),
94
+ };
95
+ }
96
+ automation.appId = devAppId;
97
+ toSave.push(automation);
98
+ }
99
+ yield db.bulkDocs(toSave);
100
+ });
101
+ }
79
102
  /**
80
103
  * This function manages temporary template files which are stored by Koa.
81
104
  * @param {Object} template The template object retrieved from the Koa context object.
@@ -156,6 +179,7 @@ function importApp(appId, db, template) {
156
179
  throw "Error loading database dump from template.";
157
180
  }
158
181
  yield updateAttachmentColumns(prodAppId, db);
182
+ yield updateAutomations(prodAppId, db);
159
183
  return ok;
160
184
  });
161
185
  }