@budibase/server 2.7.7-alpha.7 → 2.7.7-alpha.9

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.
@@ -9026,7 +9026,7 @@ function lowerBuiltinRoleID(roleId1, roleId2) {
9026
9026
  }
9027
9027
  return builtinRoleToNumber(roleId1) > builtinRoleToNumber(roleId2) ? roleId2 : roleId1;
9028
9028
  }
9029
- async function getRole(roleId) {
9029
+ async function getRole(roleId, opts) {
9030
9030
  if (!roleId) {
9031
9031
  return void 0;
9032
9032
  }
@@ -9042,6 +9042,9 @@ async function getRole(roleId) {
9042
9042
  role = Object.assign(role, dbRole);
9043
9043
  role._id = getExternalRoleID(role._id);
9044
9044
  } catch (err) {
9045
+ if (!isBuiltin(roleId) && (opts == null ? void 0 : opts.defaultPublic)) {
9046
+ return cloneDeep2(BUILTIN_ROLES.PUBLIC);
9047
+ }
9045
9048
  if (Object.keys(role).length === 0) {
9046
9049
  throw err;
9047
9050
  }
@@ -9879,7 +9882,9 @@ var encryption_exports = {};
9879
9882
  __export(encryption_exports, {
9880
9883
  SecretOption: () => SecretOption,
9881
9884
  decrypt: () => decrypt,
9885
+ decryptFile: () => decryptFile,
9882
9886
  encrypt: () => encrypt,
9887
+ encryptFile: () => encryptFile,
9883
9888
  getSecret: () => getSecret
9884
9889
  });
9885
9890
  function getSecret(secretOption) {
@@ -9900,11 +9905,11 @@ function getSecret(secretOption) {
9900
9905
  }
9901
9906
  return secret;
9902
9907
  }
9903
- function stretchString(string, salt) {
9904
- return import_crypto.default.pbkdf2Sync(string, salt, ITERATIONS, STRETCH_LENGTH, "sha512");
9908
+ function stretchString(secret, salt) {
9909
+ return import_crypto.default.pbkdf2Sync(secret, salt, ITERATIONS, STRETCH_LENGTH, "sha512");
9905
9910
  }
9906
9911
  function encrypt(input, secretOption = "api" /* API */) {
9907
- const salt = import_crypto.default.randomBytes(RANDOM_BYTES);
9912
+ const salt = import_crypto.default.randomBytes(SALT_LENGTH);
9908
9913
  const stretched = stretchString(getSecret(secretOption), salt);
9909
9914
  const cipher = import_crypto.default.createCipheriv(ALGO, stretched, salt);
9910
9915
  const base = cipher.update(input);
@@ -9921,16 +9926,101 @@ function decrypt(input, secretOption = "api" /* API */) {
9921
9926
  const final = decipher.final();
9922
9927
  return Buffer.concat([base, final]).toString();
9923
9928
  }
9924
- var import_crypto, ALGO, SEPARATOR3, ITERATIONS, RANDOM_BYTES, STRETCH_LENGTH, SecretOption;
9929
+ async function encryptFile({ dir, filename }, secret) {
9930
+ const outputFileName = `${filename}.enc`;
9931
+ const filePath = (0, import_path2.join)(dir, filename);
9932
+ const inputFile = import_fs2.default.createReadStream(filePath);
9933
+ const outputFile = import_fs2.default.createWriteStream((0, import_path2.join)(dir, outputFileName));
9934
+ const salt = import_crypto.default.randomBytes(SALT_LENGTH);
9935
+ const iv = import_crypto.default.randomBytes(IV_LENGTH);
9936
+ const stretched = stretchString(secret, salt);
9937
+ const cipher = import_crypto.default.createCipheriv(ALGO, stretched, iv);
9938
+ outputFile.write(salt);
9939
+ outputFile.write(iv);
9940
+ inputFile.pipe(import_zlib.default.createGzip()).pipe(cipher).pipe(outputFile);
9941
+ return new Promise((r) => {
9942
+ outputFile.on("finish", () => {
9943
+ r({
9944
+ filename: outputFileName,
9945
+ dir
9946
+ });
9947
+ });
9948
+ });
9949
+ }
9950
+ async function getSaltAndIV(path2) {
9951
+ const fileStream = import_fs2.default.createReadStream(path2);
9952
+ const salt = await readBytes(fileStream, SALT_LENGTH);
9953
+ const iv = await readBytes(fileStream, IV_LENGTH);
9954
+ fileStream.close();
9955
+ return { salt, iv };
9956
+ }
9957
+ async function decryptFile(inputPath, outputPath, secret) {
9958
+ const { salt, iv } = await getSaltAndIV(inputPath);
9959
+ const inputFile = import_fs2.default.createReadStream(inputPath, {
9960
+ start: SALT_LENGTH + IV_LENGTH
9961
+ });
9962
+ const outputFile = import_fs2.default.createWriteStream(outputPath);
9963
+ const stretched = stretchString(secret, salt);
9964
+ const decipher = import_crypto.default.createDecipheriv(ALGO, stretched, iv);
9965
+ const unzip = import_zlib.default.createGunzip();
9966
+ inputFile.pipe(decipher).pipe(unzip).pipe(outputFile);
9967
+ return new Promise((res, rej) => {
9968
+ outputFile.on("finish", () => {
9969
+ outputFile.close();
9970
+ res();
9971
+ });
9972
+ inputFile.on("error", (e) => {
9973
+ outputFile.close();
9974
+ rej(e);
9975
+ });
9976
+ decipher.on("error", (e) => {
9977
+ outputFile.close();
9978
+ rej(e);
9979
+ });
9980
+ unzip.on("error", (e) => {
9981
+ outputFile.close();
9982
+ rej(e);
9983
+ });
9984
+ outputFile.on("error", (e) => {
9985
+ outputFile.close();
9986
+ rej(e);
9987
+ });
9988
+ });
9989
+ }
9990
+ function readBytes(stream3, length) {
9991
+ return new Promise((resolve3, reject) => {
9992
+ let bytesRead = 0;
9993
+ const data2 = [];
9994
+ stream3.on("readable", () => {
9995
+ let chunk;
9996
+ while ((chunk = stream3.read(length - bytesRead)) !== null) {
9997
+ data2.push(chunk);
9998
+ bytesRead += chunk.length;
9999
+ }
10000
+ resolve3(Buffer.concat(data2));
10001
+ });
10002
+ stream3.on("end", () => {
10003
+ reject(new Error("Insufficient data in the stream."));
10004
+ });
10005
+ stream3.on("error", (error) => {
10006
+ reject(error);
10007
+ });
10008
+ });
10009
+ }
10010
+ var import_crypto, import_fs2, import_zlib, import_path2, ALGO, SEPARATOR3, ITERATIONS, STRETCH_LENGTH, SALT_LENGTH, IV_LENGTH, SecretOption;
9925
10011
  var init_encryption = __esm({
9926
10012
  "../backend-core/src/security/encryption.ts"() {
9927
10013
  import_crypto = __toESM(require("crypto"));
10014
+ import_fs2 = __toESM(require("fs"));
10015
+ import_zlib = __toESM(require("zlib"));
9928
10016
  init_environment3();
10017
+ import_path2 = require("path");
9929
10018
  ALGO = "aes-256-ctr";
9930
10019
  SEPARATOR3 = "-";
9931
10020
  ITERATIONS = 1e4;
9932
- RANDOM_BYTES = 16;
9933
10021
  STRETCH_LENGTH = 32;
10022
+ SALT_LENGTH = 16;
10023
+ IV_LENGTH = 16;
9934
10024
  SecretOption = /* @__PURE__ */ ((SecretOption2) => {
9935
10025
  SecretOption2["API"] = "api";
9936
10026
  SecretOption2["ENCRYPTION"] = "encryption";
@@ -10841,12 +10931,12 @@ var init_plugin4 = __esm({
10841
10931
  function budibaseTempDir() {
10842
10932
  return bbTmp;
10843
10933
  }
10844
- var import_path2, import_os, import_fs2, ObjectStoreBuckets, bbTmp;
10934
+ var import_path3, import_os, import_fs3, ObjectStoreBuckets, bbTmp;
10845
10935
  var init_utils8 = __esm({
10846
10936
  "../backend-core/src/objectStore/utils.ts"() {
10847
- import_path2 = require("path");
10937
+ import_path3 = require("path");
10848
10938
  import_os = require("os");
10849
- import_fs2 = __toESM(require("fs"));
10939
+ import_fs3 = __toESM(require("fs"));
10850
10940
  init_environment3();
10851
10941
  ObjectStoreBuckets = {
10852
10942
  BACKUPS: environment_default2.BACKUPS_BUCKET_NAME,
@@ -10855,9 +10945,9 @@ var init_utils8 = __esm({
10855
10945
  GLOBAL: environment_default2.GLOBAL_BUCKET_NAME,
10856
10946
  PLUGINS: environment_default2.PLUGIN_BUCKET_NAME
10857
10947
  };
10858
- bbTmp = (0, import_path2.join)((0, import_os.tmpdir)(), ".budibase");
10859
- if (!import_fs2.default.existsSync(bbTmp)) {
10860
- import_fs2.default.mkdirSync(bbTmp);
10948
+ bbTmp = (0, import_path3.join)((0, import_os.tmpdir)(), ".budibase");
10949
+ if (!import_fs3.default.existsSync(bbTmp)) {
10950
+ import_fs3.default.mkdirSync(bbTmp);
10861
10951
  }
10862
10952
  }
10863
10953
  });
@@ -10869,17 +10959,17 @@ function sanitizeKey(input) {
10869
10959
  function sanitizeBucket(input) {
10870
10960
  return input.replace(new RegExp(APP_DEV_PREFIX, "g"), APP_PREFIX);
10871
10961
  }
10872
- var import_aws_sdk, import_stream, import_node_fetch6, import_tar_fs, import_zlib, import_util, import_path3, import_fs3, import_uuid3, sanitize, streamPipeline, STATE, CONTENT_TYPE_MAP, STRING_CONTENT_TYPES, ObjectStore, makeSureBucketExists, upload, streamUpload, retrieve, listAllObjects, getPresignedUrl, retrieveToTmp, retrieveDirectory, deleteFile, deleteFiles, deleteFolder, uploadDirectory, downloadTarballDirect, downloadTarball;
10962
+ var import_aws_sdk, import_stream, import_node_fetch6, import_tar_fs, import_zlib2, import_util, import_path4, import_fs4, import_uuid3, sanitize, streamPipeline, STATE, CONTENT_TYPE_MAP, STRING_CONTENT_TYPES, ObjectStore, makeSureBucketExists, upload, streamUpload, retrieve, listAllObjects, getPresignedUrl, retrieveToTmp, retrieveDirectory, deleteFile, deleteFiles, deleteFolder, uploadDirectory, downloadTarballDirect, downloadTarball;
10873
10963
  var init_objectStore = __esm({
10874
10964
  "../backend-core/src/objectStore/objectStore.ts"() {
10875
10965
  import_aws_sdk = __toESM(require("aws-sdk"));
10876
10966
  import_stream = __toESM(require("stream"));
10877
10967
  import_node_fetch6 = __toESM(require("node-fetch"));
10878
10968
  import_tar_fs = __toESM(require("tar-fs"));
10879
- import_zlib = __toESM(require("zlib"));
10969
+ import_zlib2 = __toESM(require("zlib"));
10880
10970
  import_util = require("util");
10881
- import_path3 = require("path");
10882
- import_fs3 = __toESM(require("fs"));
10971
+ import_path4 = require("path");
10972
+ import_fs4 = __toESM(require("fs"));
10883
10973
  init_environment3();
10884
10974
  init_utils8();
10885
10975
  import_uuid3 = require("uuid");
@@ -10958,7 +11048,7 @@ var init_objectStore = __esm({
10958
11048
  metadata
10959
11049
  }) => {
10960
11050
  const extension = filename.split(".").pop();
10961
- const fileBytes = import_fs3.default.readFileSync(path2);
11051
+ const fileBytes = import_fs4.default.readFileSync(path2);
10962
11052
  const objectStore = ObjectStore(bucketName);
10963
11053
  await makeSureBucketExists(objectStore, bucketName);
10964
11054
  let contentType = type;
@@ -11060,13 +11150,13 @@ var init_objectStore = __esm({
11060
11150
  bucketName = sanitizeBucket(bucketName);
11061
11151
  filepath = sanitizeKey(filepath);
11062
11152
  const data2 = await retrieve(bucketName, filepath);
11063
- const outputPath = (0, import_path3.join)(budibaseTempDir(), (0, import_uuid3.v4)());
11064
- import_fs3.default.writeFileSync(outputPath, data2);
11153
+ const outputPath = (0, import_path4.join)(budibaseTempDir(), (0, import_uuid3.v4)());
11154
+ import_fs4.default.writeFileSync(outputPath, data2);
11065
11155
  return outputPath;
11066
11156
  };
11067
11157
  retrieveDirectory = async (bucketName, path2) => {
11068
- let writePath = (0, import_path3.join)(budibaseTempDir(), (0, import_uuid3.v4)());
11069
- import_fs3.default.mkdirSync(writePath);
11158
+ let writePath = (0, import_path4.join)(budibaseTempDir(), (0, import_uuid3.v4)());
11159
+ import_fs4.default.mkdirSync(writePath);
11070
11160
  const objects = await listAllObjects(bucketName, path2);
11071
11161
  let fullObjects = await Promise.all(
11072
11162
  objects.map((obj) => retrieve(bucketName, obj.Key))
@@ -11078,9 +11168,9 @@ var init_objectStore = __esm({
11078
11168
  const possiblePath = filename.split("/");
11079
11169
  if (possiblePath.length > 1) {
11080
11170
  const dirs = possiblePath.slice(0, possiblePath.length - 1);
11081
- import_fs3.default.mkdirSync((0, import_path3.join)(writePath, ...dirs), { recursive: true });
11171
+ import_fs4.default.mkdirSync((0, import_path4.join)(writePath, ...dirs), { recursive: true });
11082
11172
  }
11083
- import_fs3.default.writeFileSync((0, import_path3.join)(writePath, ...possiblePath), data2);
11173
+ import_fs4.default.writeFileSync((0, import_path4.join)(writePath, ...possiblePath), data2);
11084
11174
  }
11085
11175
  return writePath;
11086
11176
  };
@@ -11134,14 +11224,14 @@ var init_objectStore = __esm({
11134
11224
  uploadDirectory = async (bucketName, localPath, bucketPath) => {
11135
11225
  bucketName = sanitizeBucket(bucketName);
11136
11226
  let uploads = [];
11137
- const files = import_fs3.default.readdirSync(localPath, { withFileTypes: true });
11227
+ const files = import_fs4.default.readdirSync(localPath, { withFileTypes: true });
11138
11228
  for (let file of files) {
11139
- const path2 = sanitizeKey((0, import_path3.join)(bucketPath, file.name));
11140
- const local = (0, import_path3.join)(localPath, file.name);
11229
+ const path2 = sanitizeKey((0, import_path4.join)(bucketPath, file.name));
11230
+ const local = (0, import_path4.join)(localPath, file.name);
11141
11231
  if (file.isDirectory()) {
11142
11232
  uploads.push(uploadDirectory(bucketName, local, path2));
11143
11233
  } else {
11144
- uploads.push(streamUpload(bucketName, path2, import_fs3.default.createReadStream(local)));
11234
+ uploads.push(streamUpload(bucketName, path2, import_fs4.default.createReadStream(local)));
11145
11235
  }
11146
11236
  }
11147
11237
  await Promise.all(uploads);
@@ -11153,7 +11243,7 @@ var init_objectStore = __esm({
11153
11243
  if (!response2.ok) {
11154
11244
  throw new Error(`unexpected response ${response2.statusText}`);
11155
11245
  }
11156
- await streamPipeline(response2.body, import_zlib.default.createUnzip(), import_tar_fs.default.extract(path2));
11246
+ await streamPipeline(response2.body, import_zlib2.default.createUnzip(), import_tar_fs.default.extract(path2));
11157
11247
  };
11158
11248
  downloadTarball = async (url, bucketName, path2) => {
11159
11249
  bucketName = sanitizeBucket(bucketName);
@@ -11162,8 +11252,8 @@ var init_objectStore = __esm({
11162
11252
  if (!response2.ok) {
11163
11253
  throw new Error(`unexpected response ${response2.statusText}`);
11164
11254
  }
11165
- const tmpPath = (0, import_path3.join)(budibaseTempDir(), path2);
11166
- await streamPipeline(response2.body, import_zlib.default.createUnzip(), import_tar_fs.default.extract(tmpPath));
11255
+ const tmpPath = (0, import_path4.join)(budibaseTempDir(), path2);
11256
+ await streamPipeline(response2.body, import_zlib2.default.createUnzip(), import_tar_fs.default.extract(tmpPath));
11167
11257
  if (!environment_default2.isTest() && environment_default2.SELF_HOSTED) {
11168
11258
  await uploadDirectory(bucketName, tmpPath, path2);
11169
11259
  }
@@ -12616,21 +12706,21 @@ __export(version_exports, {
12616
12706
  getLicenseVersion: () => getLicenseVersion,
12617
12707
  getProVersion: () => getProVersion
12618
12708
  });
12619
- var import_fs4, import_path4, getLicenseVersion, getProVersion;
12709
+ var import_fs5, import_path5, getLicenseVersion, getProVersion;
12620
12710
  var init_version2 = __esm({
12621
12711
  "../pro/packages/pro/src/constants/version.ts"() {
12622
12712
  init_src2();
12623
- import_fs4 = __toESM(require("fs"));
12624
- import_path4 = __toESM(require("path"));
12713
+ import_fs5 = __toESM(require("fs"));
12714
+ import_path5 = __toESM(require("path"));
12625
12715
  getLicenseVersion = () => {
12626
12716
  if (environment_default2.isDev()) {
12627
12717
  const DEV_VER_FILENAME = "dev-version.txt";
12628
- const verFile = import_path4.default.join(objectStore_exports2.budibaseTempDir(), DEV_VER_FILENAME);
12629
- if (import_fs4.default.existsSync(verFile)) {
12630
- return import_fs4.default.readFileSync(verFile, "utf8");
12718
+ const verFile = import_path5.default.join(objectStore_exports2.budibaseTempDir(), DEV_VER_FILENAME);
12719
+ if (import_fs5.default.existsSync(verFile)) {
12720
+ return import_fs5.default.readFileSync(verFile, "utf8");
12631
12721
  } else {
12632
12722
  const devVer = utils_exports2.newid();
12633
- import_fs4.default.writeFileSync(verFile, devVer);
12723
+ import_fs5.default.writeFileSync(verFile, devVer);
12634
12724
  return devVer;
12635
12725
  }
12636
12726
  } else {
@@ -13076,31 +13166,31 @@ function getOfflineLicense() {
13076
13166
  }
13077
13167
  }
13078
13168
  function getOfflineLicenseFromDisk() {
13079
- if (import_fs5.default.existsSync(LICENSE_FILE_PATH)) {
13080
- const token = import_fs5.default.readFileSync(LICENSE_FILE_PATH, { encoding: "utf-8" });
13169
+ if (import_fs6.default.existsSync(LICENSE_FILE_PATH)) {
13170
+ const token = import_fs6.default.readFileSync(LICENSE_FILE_PATH, { encoding: "utf-8" });
13081
13171
  return import_jsonwebtoken.default.verify(token, PUBLIC_KEY, { algorithms: ["RS256"] });
13082
13172
  }
13083
13173
  }
13084
13174
  function writeOfflineLicenseToDisk(signedLicense) {
13085
- import_fs5.default.writeFileSync(LICENSE_FILE_PATH, signedLicense, { encoding: "utf-8" });
13175
+ import_fs6.default.writeFileSync(LICENSE_FILE_PATH, signedLicense, { encoding: "utf-8" });
13086
13176
  }
13087
13177
  function deleteOfflineLicense() {
13088
- import_fs5.default.rmSync(LICENSE_FILE_PATH, { force: true });
13178
+ import_fs6.default.rmSync(LICENSE_FILE_PATH, { force: true });
13089
13179
  }
13090
- var import_fs5, import_path5, import_os2, import_jsonwebtoken, SUB_DIRECTORY, DIRECTORY, OFFLINE_LICENSE_FILE, LICENSE_FILE_PATH, PUBLIC_KEY;
13180
+ var import_fs6, import_path6, import_os2, import_jsonwebtoken, SUB_DIRECTORY, DIRECTORY, OFFLINE_LICENSE_FILE, LICENSE_FILE_PATH, PUBLIC_KEY;
13091
13181
  var init_offline = __esm({
13092
13182
  "../pro/packages/pro/src/sdk/licensing/licenses/offline.ts"() {
13093
- import_fs5 = __toESM(require("fs"));
13094
- import_path5 = require("path");
13183
+ import_fs6 = __toESM(require("fs"));
13184
+ import_path6 = require("path");
13095
13185
  import_os2 = require("os");
13096
13186
  import_jsonwebtoken = __toESM(require("jsonwebtoken"));
13097
13187
  init_src2();
13098
13188
  SUB_DIRECTORY = environment_default2.isTest() ? ".budibase-test" : ".budibase";
13099
- DIRECTORY = (0, import_path5.join)((0, import_os2.tmpdir)(), SUB_DIRECTORY);
13189
+ DIRECTORY = (0, import_path6.join)((0, import_os2.tmpdir)(), SUB_DIRECTORY);
13100
13190
  OFFLINE_LICENSE_FILE = "offline_license.txt";
13101
- LICENSE_FILE_PATH = (0, import_path5.join)(DIRECTORY, OFFLINE_LICENSE_FILE);
13102
- if (!import_fs5.default.existsSync(DIRECTORY)) {
13103
- import_fs5.default.mkdirSync(DIRECTORY);
13191
+ LICENSE_FILE_PATH = (0, import_path6.join)(DIRECTORY, OFFLINE_LICENSE_FILE);
13192
+ if (!import_fs6.default.existsSync(DIRECTORY)) {
13193
+ import_fs6.default.mkdirSync(DIRECTORY);
13104
13194
  }
13105
13195
  PUBLIC_KEY = "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvz3jePLCFBXZ19c8Dpkv\nXtSgOhKFOcvQdo/LV0KJRUzQWDPWuO4ILtBtnqhjtIzZH4CH0qCYBet5L6Qr4CM1\nl2HXiAD1Q2rlHNW9wDaYyKb1F5f+v4RyqCAyzlkwRdksmkLeECTboojNnmRCrE3C\n8suunQP5bEScqEY2kclqzSf8e6xqMzPUg3mL/pNa1iEv7TuLbU9nJfgR36l0WmZY\n94fWnSaT8OSXSqcxsaByf06gfS3HAoTJNc7eqz1Hf9fUORQGPUAnFK8cT3SfdA36\nd/o3ZWE1TTj1zYwlCLN5qRKr3hU8nC3xEYNEbkB9SfTRaOq9Q7P8WmfLkoCPm3pR\nmwIDAQAB\n-----END PUBLIC KEY-----\n";
13106
13196
  }
@@ -14557,13 +14647,13 @@ var init_groups6 = __esm({
14557
14647
 
14558
14648
  // ../pro/packages/pro/src/utilities/fileSystem.ts
14559
14649
  function loadJSFile(directory, name) {
14560
- return import_fs6.default.readFileSync((0, import_path6.join)(directory, name), "utf8");
14650
+ return import_fs7.default.readFileSync((0, import_path7.join)(directory, name), "utf8");
14561
14651
  }
14562
- var import_fs6, import_path6;
14652
+ var import_fs7, import_path7;
14563
14653
  var init_fileSystem = __esm({
14564
14654
  "../pro/packages/pro/src/utilities/fileSystem.ts"() {
14565
- import_fs6 = __toESM(require("fs"));
14566
- import_path6 = require("path");
14655
+ import_fs7 = __toESM(require("fs"));
14656
+ import_path7 = require("path");
14567
14657
  }
14568
14658
  });
14569
14659
 
@@ -17531,8 +17621,8 @@ async function runBackup(trigger, tenantId, appId, opts) {
17531
17621
  }
17532
17622
  });
17533
17623
  await updateMetadata2("complete" /* COMPLETE */, { filename, contents });
17534
- if (import_fs7.default.existsSync(tarPath)) {
17535
- import_fs7.default.rmSync(tarPath);
17624
+ if (import_fs8.default.existsSync(tarPath)) {
17625
+ import_fs8.default.rmSync(tarPath);
17536
17626
  }
17537
17627
  } catch (err) {
17538
17628
  logging_exports.logAlert("App backup error", err);
@@ -17590,14 +17680,14 @@ async function exportProcessor(job, opts) {
17590
17680
  });
17591
17681
  });
17592
17682
  }
17593
- var import_fs7;
17683
+ var import_fs8;
17594
17684
  var init_processing = __esm({
17595
17685
  "../pro/packages/pro/src/sdk/backups/processing.ts"() {
17596
17686
  init_src2();
17597
17687
  init_backup5();
17598
17688
  init_src();
17599
17689
  init_queue4();
17600
- import_fs7 = __toESM(require("fs"));
17690
+ import_fs8 = __toESM(require("fs"));
17601
17691
  }
17602
17692
  });
17603
17693
 
@@ -18238,15 +18328,15 @@ async function downloadBackup(ctx) {
18238
18328
  const backupId = ctx.params.backupId;
18239
18329
  const { metadata, path: path2 } = await backups_default.downloadAppBackup(backupId);
18240
18330
  ctx.attachment(`backup-${metadata.timestamp}.tar.gz`);
18241
- ctx.body = import_fs8.default.createReadStream(path2);
18331
+ ctx.body = import_fs9.default.createReadStream(path2);
18242
18332
  }
18243
- var import_fs8;
18333
+ var import_fs9;
18244
18334
  var init_backups3 = __esm({
18245
18335
  "../pro/packages/pro/src/api/controllers/apps/backups.ts"() {
18246
18336
  init_src();
18247
18337
  init_sdk2();
18248
18338
  init_src2();
18249
- import_fs8 = __toESM(require("fs"));
18339
+ import_fs9 = __toESM(require("fs"));
18250
18340
  }
18251
18341
  });
18252
18342
 
@@ -19538,32 +19628,32 @@ var init_centralPath = __esm({
19538
19628
  });
19539
19629
 
19540
19630
  // src/utilities/fileSystem/filesystem.ts
19541
- var import_fs9, import_path7, import_tar, uuid2, TOP_LEVEL_PATH, apiFileReturn, streamFile, createTempFolder, extractTarball;
19631
+ var import_fs10, import_path8, import_tar, uuid2, TOP_LEVEL_PATH, apiFileReturn, streamFile, createTempFolder, extractTarball;
19542
19632
  var init_filesystem = __esm({
19543
19633
  "src/utilities/fileSystem/filesystem.ts"() {
19544
- import_fs9 = __toESM(require("fs"));
19634
+ import_fs10 = __toESM(require("fs"));
19545
19635
  init_budibaseDir();
19546
- import_path7 = require("path");
19636
+ import_path8 = require("path");
19547
19637
  init_environment();
19548
19638
  import_tar = __toESM(require("tar"));
19549
19639
  init_environment();
19550
19640
  uuid2 = require("uuid/v4");
19551
- TOP_LEVEL_PATH = environment_default.TOP_LEVEL_PATH || (0, import_path7.resolve)((0, import_path7.join)(__dirname, "..", "..", ".."));
19641
+ TOP_LEVEL_PATH = environment_default.TOP_LEVEL_PATH || (0, import_path8.resolve)((0, import_path8.join)(__dirname, "..", "..", ".."));
19552
19642
  apiFileReturn = (contents) => {
19553
- const path2 = (0, import_path7.join)(budibaseTempDir2(), uuid2());
19554
- import_fs9.default.writeFileSync(path2, contents);
19555
- return import_fs9.default.createReadStream(path2);
19643
+ const path2 = (0, import_path8.join)(budibaseTempDir2(), uuid2());
19644
+ import_fs10.default.writeFileSync(path2, contents);
19645
+ return import_fs10.default.createReadStream(path2);
19556
19646
  };
19557
19647
  streamFile = (path2) => {
19558
- return import_fs9.default.createReadStream(path2);
19648
+ return import_fs10.default.createReadStream(path2);
19559
19649
  };
19560
19650
  createTempFolder = (item) => {
19561
- const path2 = (0, import_path7.join)(budibaseTempDir2(), item);
19651
+ const path2 = (0, import_path8.join)(budibaseTempDir2(), item);
19562
19652
  try {
19563
- if (import_fs9.default.existsSync(path2)) {
19564
- import_fs9.default.rmSync(path2, { recursive: true, force: true });
19653
+ if (import_fs10.default.existsSync(path2)) {
19654
+ import_fs10.default.rmSync(path2, { recursive: true, force: true });
19565
19655
  }
19566
- import_fs9.default.mkdirSync(path2);
19656
+ import_fs10.default.mkdirSync(path2);
19567
19657
  } catch (err) {
19568
19658
  throw new Error(`Path cannot be created: ${err.message}`);
19569
19659
  }
@@ -19590,17 +19680,17 @@ var init_clientLibrary = __esm({
19590
19680
  });
19591
19681
 
19592
19682
  // src/utilities/fileSystem/app.ts
19593
- var import_path8, NODE_MODULES_PATH;
19683
+ var import_path9, NODE_MODULES_PATH;
19594
19684
  var init_app7 = __esm({
19595
19685
  "src/utilities/fileSystem/app.ts"() {
19596
19686
  init_budibaseDir();
19597
- import_path8 = require("path");
19687
+ import_path9 = require("path");
19598
19688
  init_constants6();
19599
19689
  init_clientLibrary();
19600
19690
  init_environment();
19601
19691
  init_src2();
19602
19692
  init_filesystem();
19603
- NODE_MODULES_PATH = (0, import_path8.join)(TOP_LEVEL_PATH, "node_modules");
19693
+ NODE_MODULES_PATH = (0, import_path9.join)(TOP_LEVEL_PATH, "node_modules");
19604
19694
  }
19605
19695
  });
19606
19696
 
@@ -19608,19 +19698,19 @@ var init_app7 = __esm({
19608
19698
  async function getPluginImpl(path2, plugin) {
19609
19699
  var _a;
19610
19700
  const hash2 = (_a = plugin.schema) == null ? void 0 : _a.hash;
19611
- if (!import_fs10.default.existsSync(path2)) {
19612
- import_fs10.default.mkdirSync(path2);
19701
+ if (!import_fs11.default.existsSync(path2)) {
19702
+ import_fs11.default.mkdirSync(path2);
19613
19703
  }
19614
- const filename = (0, import_path9.join)(path2, plugin.name);
19704
+ const filename = (0, import_path10.join)(path2, plugin.name);
19615
19705
  const metadataName = `${filename}.bbmetadata`;
19616
- if (import_fs10.default.existsSync(filename)) {
19617
- const currentHash = import_fs10.default.readFileSync(metadataName, "utf8");
19706
+ if (import_fs11.default.existsSync(filename)) {
19707
+ const currentHash = import_fs11.default.readFileSync(metadataName, "utf8");
19618
19708
  if (currentHash === hash2) {
19619
19709
  return require(filename);
19620
19710
  } else {
19621
19711
  console.log(`Updating plugin: ${plugin.name}`);
19622
19712
  delete require.cache[require.resolve(filename)];
19623
- import_fs10.default.unlinkSync(filename);
19713
+ import_fs11.default.unlinkSync(filename);
19624
19714
  }
19625
19715
  }
19626
19716
  const pluginKey = objectStore_exports2.getPluginJSKey(plugin);
@@ -19628,24 +19718,24 @@ async function getPluginImpl(path2, plugin) {
19628
19718
  objectStore_exports2.ObjectStoreBuckets.PLUGINS,
19629
19719
  pluginKey
19630
19720
  );
19631
- import_fs10.default.writeFileSync(filename, pluginJs);
19632
- import_fs10.default.writeFileSync(metadataName, hash2);
19721
+ import_fs11.default.writeFileSync(filename, pluginJs);
19722
+ import_fs11.default.writeFileSync(metadataName, hash2);
19633
19723
  return require(filename);
19634
19724
  }
19635
- var import_fs10, import_path9, DATASOURCE_PATH, AUTOMATION_PATH, getPluginMetadata, getDatasourcePlugin, getAutomationPlugin;
19725
+ var import_fs11, import_path10, DATASOURCE_PATH, AUTOMATION_PATH, getPluginMetadata, getDatasourcePlugin, getAutomationPlugin;
19636
19726
  var init_plugin5 = __esm({
19637
19727
  "src/utilities/fileSystem/plugin.ts"() {
19638
19728
  init_budibaseDir();
19639
- import_fs10 = __toESM(require("fs"));
19640
- import_path9 = require("path");
19729
+ import_fs11 = __toESM(require("fs"));
19730
+ import_path10 = require("path");
19641
19731
  init_src2();
19642
- DATASOURCE_PATH = (0, import_path9.join)(budibaseTempDir2(), "datasource");
19643
- AUTOMATION_PATH = (0, import_path9.join)(budibaseTempDir2(), "automation");
19732
+ DATASOURCE_PATH = (0, import_path10.join)(budibaseTempDir2(), "datasource");
19733
+ AUTOMATION_PATH = (0, import_path10.join)(budibaseTempDir2(), "automation");
19644
19734
  getPluginMetadata = async (path2) => {
19645
19735
  let metadata = {};
19646
19736
  try {
19647
- const pkg2 = import_fs10.default.readFileSync((0, import_path9.join)(path2, "package.json"), "utf8");
19648
- const schema = import_fs10.default.readFileSync((0, import_path9.join)(path2, "schema.json"), "utf8");
19737
+ const pkg2 = import_fs11.default.readFileSync((0, import_path10.join)(path2, "package.json"), "utf8");
19738
+ const schema = import_fs11.default.readFileSync((0, import_path10.join)(path2, "schema.json"), "utf8");
19649
19739
  metadata.schema = JSON.parse(schema);
19650
19740
  metadata.package = JSON.parse(pkg2);
19651
19741
  if (!metadata.package.name || !metadata.package.version || !metadata.package.description) {
@@ -19716,13 +19806,14 @@ __export(exports_exports, {
19716
19806
  streamExportApp: () => streamExportApp
19717
19807
  });
19718
19808
  function tarFilesToTmp(tmpDir, files) {
19719
- const exportFile = (0, import_path10.join)(budibaseTempDir2(), `${uuid3()}.tar.gz`);
19720
- tar3.create(
19809
+ const fileName = `${uuid3()}.tar.gz`;
19810
+ const exportFile = (0, import_path11.join)(budibaseTempDir2(), fileName);
19811
+ import_tar2.default.create(
19721
19812
  {
19722
19813
  sync: true,
19723
19814
  gzip: true,
19724
19815
  file: exportFile,
19725
- recursive: true,
19816
+ noDirRecurse: false,
19726
19817
  cwd: tmpDir
19727
19818
  },
19728
19819
  files
@@ -19739,7 +19830,7 @@ async function exportDB(dbName, opts = {}) {
19739
19830
  return db_exports.doWithDB(dbName, async (db2) => {
19740
19831
  if (opts == null ? void 0 : opts.exportPath) {
19741
19832
  const path2 = opts == null ? void 0 : opts.exportPath;
19742
- const writeStream = import_fs11.default.createWriteStream(path2);
19833
+ const writeStream = import_fs12.default.createWriteStream(path2);
19743
19834
  await db2.dump(writeStream, exportOpts);
19744
19835
  return path2;
19745
19836
  } else {
@@ -19772,9 +19863,9 @@ async function exportApp(appId, config) {
19772
19863
  for (let path2 of STATIC_APP_FILES) {
19773
19864
  const contents = await objectStore_exports2.retrieve(
19774
19865
  ObjectStoreBuckets2.APPS,
19775
- (0, import_path10.join)(appPath, path2)
19866
+ (0, import_path11.join)(appPath, path2)
19776
19867
  );
19777
- import_fs11.default.writeFileSync((0, import_path10.join)(tmpPath, path2), contents);
19868
+ import_fs12.default.writeFileSync((0, import_path11.join)(tmpPath, path2), contents);
19778
19869
  }
19779
19870
  } else {
19780
19871
  tmpPath = await objectStore_exports2.retrieveDirectory(
@@ -19783,37 +19874,52 @@ async function exportApp(appId, config) {
19783
19874
  );
19784
19875
  }
19785
19876
  }
19786
- const downloadedPath = (0, import_path10.join)(tmpPath, appPath);
19787
- if (import_fs11.default.existsSync(downloadedPath)) {
19788
- const allFiles = import_fs11.default.readdirSync(downloadedPath);
19877
+ const downloadedPath = (0, import_path11.join)(tmpPath, appPath);
19878
+ if (import_fs12.default.existsSync(downloadedPath)) {
19879
+ const allFiles = import_fs12.default.readdirSync(downloadedPath);
19789
19880
  for (let file of allFiles) {
19790
- const path2 = (0, import_path10.join)(downloadedPath, file);
19791
- import_fs11.default.renameSync(path2, (0, import_path10.join)(downloadedPath, "..", file));
19881
+ const path2 = (0, import_path11.join)(downloadedPath, file);
19882
+ import_fs12.default.renameSync(path2, (0, import_path11.join)(downloadedPath, "..", file));
19792
19883
  }
19793
- import_fs11.default.rmdirSync(downloadedPath);
19884
+ import_fs12.default.rmdirSync(downloadedPath);
19794
19885
  }
19795
- const dbPath = (0, import_path10.join)(tmpPath, DB_EXPORT_FILE);
19886
+ const dbPath = (0, import_path11.join)(tmpPath, DB_EXPORT_FILE);
19796
19887
  await exportDB(appId, {
19797
19888
  filter: defineFilter(config == null ? void 0 : config.excludeRows, config == null ? void 0 : config.excludeLogs),
19798
19889
  exportPath: dbPath
19799
19890
  });
19891
+ if (config == null ? void 0 : config.encryptPassword) {
19892
+ for (let file of import_fs12.default.readdirSync(tmpPath)) {
19893
+ const path2 = (0, import_path11.join)(tmpPath, file);
19894
+ await encryption_exports.encryptFile(
19895
+ { dir: tmpPath, filename: file },
19896
+ config.encryptPassword
19897
+ );
19898
+ import_fs12.default.rmSync(path2);
19899
+ }
19900
+ }
19800
19901
  if (config == null ? void 0 : config.tar) {
19801
- const tarPath = tarFilesToTmp(tmpPath, import_fs11.default.readdirSync(tmpPath));
19802
- import_fs11.default.rmSync(tmpPath, { recursive: true, force: true });
19902
+ const tarPath = tarFilesToTmp(tmpPath, import_fs12.default.readdirSync(tmpPath));
19903
+ import_fs12.default.rmSync(tmpPath, { recursive: true, force: true });
19803
19904
  return tarPath;
19804
19905
  } else {
19805
19906
  return tmpPath;
19806
19907
  }
19807
19908
  }
19808
- async function streamExportApp(appId, excludeRows) {
19909
+ async function streamExportApp({
19910
+ appId,
19911
+ excludeRows,
19912
+ encryptPassword
19913
+ }) {
19809
19914
  const tmpPath = await exportApp(appId, {
19810
19915
  excludeRows,
19811
19916
  excludeLogs: true,
19812
- tar: true
19917
+ tar: true,
19918
+ encryptPassword
19813
19919
  });
19814
19920
  return streamFile(tmpPath);
19815
19921
  }
19816
- var import_fs11, import_path10, uuid3, tar3, MemoryStream2;
19922
+ var import_fs12, import_path11, import_tar2, uuid3, MemoryStream2;
19817
19923
  var init_exports2 = __esm({
19818
19924
  "src/sdk/app/backups/exports.ts"() {
19819
19925
  init_src2();
@@ -19822,11 +19928,11 @@ var init_exports2 = __esm({
19822
19928
  init_constants6();
19823
19929
  init_utils13();
19824
19930
  init_constants7();
19825
- import_fs11 = __toESM(require("fs"));
19826
- import_path10 = require("path");
19931
+ import_fs12 = __toESM(require("fs"));
19932
+ import_path11 = require("path");
19827
19933
  init_environment();
19934
+ import_tar2 = __toESM(require("tar"));
19828
19935
  uuid3 = require("uuid/v4");
19829
- tar3 = require("tar");
19830
19936
  MemoryStream2 = require("memorystream");
19831
19937
  }
19832
19938
  });
@@ -19903,16 +20009,16 @@ async function getTemplateStream(template) {
19903
20009
  throw new Error("Cannot import a non-text based file.");
19904
20010
  }
19905
20011
  if (template.file) {
19906
- return import_fs12.default.createReadStream(template.file.path);
20012
+ return import_fs13.default.createReadStream(template.file.path);
19907
20013
  } else if (template.key) {
19908
20014
  const [type, name] = template.key.split("/");
19909
20015
  const tmpPath = await downloadTemplate(type, name);
19910
- return import_fs12.default.createReadStream((0, import_path11.join)(tmpPath, name, "db", "dump.txt"));
20016
+ return import_fs13.default.createReadStream((0, import_path12.join)(tmpPath, name, "db", "dump.txt"));
19911
20017
  }
19912
20018
  }
19913
20019
  function untarFile(file) {
19914
- const tmpPath = (0, import_path11.join)(budibaseTempDir2(), uuid4());
19915
- import_fs12.default.mkdirSync(tmpPath);
20020
+ const tmpPath = (0, import_path12.join)(budibaseTempDir2(), uuid4());
20021
+ import_fs13.default.mkdirSync(tmpPath);
19916
20022
  tar4.extract({
19917
20023
  sync: true,
19918
20024
  cwd: tmpPath,
@@ -19920,31 +20026,49 @@ function untarFile(file) {
19920
20026
  });
19921
20027
  return tmpPath;
19922
20028
  }
20029
+ async function decryptFiles(path2, password) {
20030
+ try {
20031
+ for (let file of import_fs13.default.readdirSync(path2)) {
20032
+ const inputPath = (0, import_path12.join)(path2, file);
20033
+ const outputPath = inputPath.replace(/\.enc$/, "");
20034
+ await encryption_exports.decryptFile(inputPath, outputPath, password);
20035
+ import_fs13.default.rmSync(inputPath);
20036
+ }
20037
+ } catch (err) {
20038
+ if (err.message === "incorrect header check") {
20039
+ throw new Error("File cannot be imported");
20040
+ }
20041
+ throw err;
20042
+ }
20043
+ }
19923
20044
  function getGlobalDBFile(tmpPath) {
19924
- return import_fs12.default.readFileSync((0, import_path11.join)(tmpPath, GLOBAL_DB_EXPORT_FILE), "utf8");
20045
+ return import_fs13.default.readFileSync((0, import_path12.join)(tmpPath, GLOBAL_DB_EXPORT_FILE), "utf8");
19925
20046
  }
19926
20047
  function getListOfAppsInMulti(tmpPath) {
19927
- return import_fs12.default.readdirSync(tmpPath).filter((dir) => dir !== GLOBAL_DB_EXPORT_FILE);
20048
+ return import_fs13.default.readdirSync(tmpPath).filter((dir) => dir !== GLOBAL_DB_EXPORT_FILE);
19928
20049
  }
19929
20050
  async function importApp(appId, db2, template) {
19930
20051
  var _a, _b;
19931
20052
  let prodAppId = db_exports.getProdAppID(appId);
19932
20053
  let dbStream;
19933
20054
  const isTar = template.file && ((_b = (_a = template == null ? void 0 : template.file) == null ? void 0 : _a.type) == null ? void 0 : _b.endsWith("gzip"));
19934
- const isDirectory = template.file && import_fs12.default.lstatSync(template.file.path).isDirectory();
20055
+ const isDirectory = template.file && import_fs13.default.lstatSync(template.file.path).isDirectory();
19935
20056
  if (template.file && (isTar || isDirectory)) {
19936
20057
  const tmpPath = isTar ? untarFile(template.file) : template.file.path;
19937
- const contents = import_fs12.default.readdirSync(tmpPath);
20058
+ if (isTar && template.file.password) {
20059
+ await decryptFiles(tmpPath, template.file.password);
20060
+ }
20061
+ const contents = import_fs13.default.readdirSync(tmpPath);
19938
20062
  if (contents.length) {
19939
20063
  let promises = [];
19940
20064
  let excludedFiles = [GLOBAL_DB_EXPORT_FILE, DB_EXPORT_FILE];
19941
20065
  for (let filename of contents) {
19942
- const path2 = (0, import_path11.join)(tmpPath, filename);
20066
+ const path2 = (0, import_path12.join)(tmpPath, filename);
19943
20067
  if (excludedFiles.includes(filename)) {
19944
20068
  continue;
19945
20069
  }
19946
- filename = (0, import_path11.join)(prodAppId, filename);
19947
- if (import_fs12.default.lstatSync(path2).isDirectory()) {
20070
+ filename = (0, import_path12.join)(prodAppId, filename);
20071
+ if (import_fs13.default.lstatSync(path2).isDirectory()) {
19948
20072
  promises.push(
19949
20073
  objectStore_exports2.uploadDirectory(ObjectStoreBuckets2.APPS, path2, filename)
19950
20074
  );
@@ -19960,7 +20084,7 @@ async function importApp(appId, db2, template) {
19960
20084
  }
19961
20085
  await Promise.all(promises);
19962
20086
  }
19963
- dbStream = import_fs12.default.createReadStream((0, import_path11.join)(tmpPath, DB_EXPORT_FILE));
20087
+ dbStream = import_fs13.default.createReadStream((0, import_path12.join)(tmpPath, DB_EXPORT_FILE));
19964
20088
  } else {
19965
20089
  dbStream = await getTemplateStream(template);
19966
20090
  }
@@ -19972,7 +20096,7 @@ async function importApp(appId, db2, template) {
19972
20096
  await updateAutomations(prodAppId, db2);
19973
20097
  return ok;
19974
20098
  }
19975
- var import_path11, import_fs12, uuid4, tar4;
20099
+ var import_path12, import_fs13, uuid4, tar4;
19976
20100
  var init_imports = __esm({
19977
20101
  "src/sdk/app/backups/imports.ts"() {
19978
20102
  init_src2();
@@ -19981,8 +20105,8 @@ var init_imports = __esm({
19981
20105
  init_constants7();
19982
20106
  init_fileSystem2();
19983
20107
  init_constants6();
19984
- import_path11 = require("path");
19985
- import_fs12 = __toESM(require("fs"));
20108
+ import_path12 = require("path");
20109
+ import_fs13 = __toESM(require("fs"));
19986
20110
  init_sdk3();
19987
20111
  init_src();
19988
20112
  uuid4 = require("uuid/v4");