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

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