s3db.js 7.3.3 → 7.3.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/s3db.cjs.js CHANGED
@@ -6,8 +6,9 @@ var nanoid = require('nanoid');
6
6
  var zlib = require('node:zlib');
7
7
  var promisePool = require('@supercharge/promise-pool');
8
8
  var web = require('node:stream/web');
9
- var lodashEs = require('lodash-es');
9
+ var promises = require('fs/promises');
10
10
  var crypto = require('crypto');
11
+ var lodashEs = require('lodash-es');
11
12
  var jsonStableStringify = require('json-stable-stringify');
12
13
  var clientS3 = require('@aws-sdk/client-s3');
13
14
  var flat = require('flat');
@@ -2081,16 +2082,6 @@ if (typeof Object.create === 'function'){
2081
2082
  // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
2082
2083
  // USE OR OTHER DEALINGS IN THE SOFTWARE.
2083
2084
 
2084
- var getOwnPropertyDescriptors = Object.getOwnPropertyDescriptors ||
2085
- function getOwnPropertyDescriptors(obj) {
2086
- var keys = Object.keys(obj);
2087
- var descriptors = {};
2088
- for (var i = 0; i < keys.length; i++) {
2089
- descriptors[keys[i]] = Object.getOwnPropertyDescriptor(obj, keys[i]);
2090
- }
2091
- return descriptors;
2092
- };
2093
-
2094
2085
  var formatRegExp = /%[sdj%]/g;
2095
2086
  function format(f) {
2096
2087
  if (!isString(f)) {
@@ -2576,64 +2567,6 @@ function hasOwnProperty(obj, prop) {
2576
2567
  return Object.prototype.hasOwnProperty.call(obj, prop);
2577
2568
  }
2578
2569
 
2579
- var kCustomPromisifiedSymbol = typeof Symbol !== 'undefined' ? Symbol('util.promisify.custom') : undefined;
2580
-
2581
- function promisify(original) {
2582
- if (typeof original !== 'function')
2583
- throw new TypeError('The "original" argument must be of type Function');
2584
-
2585
- if (kCustomPromisifiedSymbol && original[kCustomPromisifiedSymbol]) {
2586
- var fn = original[kCustomPromisifiedSymbol];
2587
- if (typeof fn !== 'function') {
2588
- throw new TypeError('The "util.promisify.custom" argument must be of type Function');
2589
- }
2590
- Object.defineProperty(fn, kCustomPromisifiedSymbol, {
2591
- value: fn, enumerable: false, writable: false, configurable: true
2592
- });
2593
- return fn;
2594
- }
2595
-
2596
- function fn() {
2597
- var promiseResolve, promiseReject;
2598
- var promise = new Promise(function (resolve, reject) {
2599
- promiseResolve = resolve;
2600
- promiseReject = reject;
2601
- });
2602
-
2603
- var args = [];
2604
- for (var i = 0; i < arguments.length; i++) {
2605
- args.push(arguments[i]);
2606
- }
2607
- args.push(function (err, value) {
2608
- if (err) {
2609
- promiseReject(err);
2610
- } else {
2611
- promiseResolve(value);
2612
- }
2613
- });
2614
-
2615
- try {
2616
- original.apply(this, args);
2617
- } catch (err) {
2618
- promiseReject(err);
2619
- }
2620
-
2621
- return promise;
2622
- }
2623
-
2624
- Object.setPrototypeOf(fn, Object.getPrototypeOf(original));
2625
-
2626
- if (kCustomPromisifiedSymbol) Object.defineProperty(fn, kCustomPromisifiedSymbol, {
2627
- value: fn, enumerable: false, writable: false, configurable: true
2628
- });
2629
- return Object.defineProperties(
2630
- fn,
2631
- getOwnPropertyDescriptors(original)
2632
- );
2633
- }
2634
-
2635
- promisify.custom = kCustomPromisifiedSymbol;
2636
-
2637
2570
  var lookup = [];
2638
2571
  var revLookup = [];
2639
2572
  var Arr = typeof Uint8Array !== 'undefined' ? Uint8Array : Array;
@@ -6886,12 +6819,6 @@ var memory_cache_class_default = MemoryCache;
6886
6819
 
6887
6820
  var fs = {};
6888
6821
 
6889
- const readFile$1 = promisify(fs.readFile);
6890
- const writeFile$1 = promisify(fs.writeFile);
6891
- const unlink = promisify(fs.unlink);
6892
- const readdir$1 = promisify(fs.readdir);
6893
- const stat$1 = promisify(fs.stat);
6894
- const mkdir = promisify(fs.mkdir);
6895
6822
  class FilesystemCache extends Cache {
6896
6823
  constructor({
6897
6824
  directory,
@@ -6968,7 +6895,7 @@ class FilesystemCache extends Cache {
6968
6895
  }
6969
6896
  async _ensureDirectory(dir) {
6970
6897
  const [ok, err] = await try_fn_default(async () => {
6971
- await mkdir(dir, { recursive: true });
6898
+ await promises.mkdir(dir, { recursive: true });
6972
6899
  });
6973
6900
  if (!ok && err.code !== "EEXIST") {
6974
6901
  throw new Error(`Failed to create cache directory: ${err.message}`);
@@ -7005,7 +6932,7 @@ class FilesystemCache extends Cache {
7005
6932
  await this._acquireLock(filePath);
7006
6933
  }
7007
6934
  try {
7008
- await writeFile$1(filePath, finalData, {
6935
+ await promises.writeFile(filePath, finalData, {
7009
6936
  encoding: compressed ? "utf8" : this.encoding,
7010
6937
  mode: this.fileMode
7011
6938
  });
@@ -7019,7 +6946,7 @@ class FilesystemCache extends Cache {
7019
6946
  compressedSize: compressed ? Buffer.byteLength(finalData, "utf8") : originalSize,
7020
6947
  compressionRatio: compressed ? (Buffer.byteLength(finalData, "utf8") / originalSize).toFixed(2) : 1
7021
6948
  };
7022
- await writeFile$1(this._getMetadataPath(filePath), JSON.stringify(metadata), {
6949
+ await promises.writeFile(this._getMetadataPath(filePath), JSON.stringify(metadata), {
7023
6950
  encoding: this.encoding,
7024
6951
  mode: this.fileMode
7025
6952
  });
@@ -7057,7 +6984,7 @@ class FilesystemCache extends Cache {
7057
6984
  const metadataPath = this._getMetadataPath(filePath);
7058
6985
  if (await this._fileExists(metadataPath)) {
7059
6986
  const [ok, err, metadata] = await try_fn_default(async () => {
7060
- const metaContent = await readFile$1(metadataPath, this.encoding);
6987
+ const metaContent = await promises.readFile(metadataPath, this.encoding);
7061
6988
  return JSON.parse(metaContent);
7062
6989
  });
7063
6990
  if (ok && metadata.ttl > 0) {
@@ -7066,7 +6993,7 @@ class FilesystemCache extends Cache {
7066
6993
  }
7067
6994
  }
7068
6995
  } else if (this.ttl > 0) {
7069
- const stats = await stat$1(filePath);
6996
+ const stats = await promises.stat(filePath);
7070
6997
  const age = Date.now() - stats.mtime.getTime();
7071
6998
  isExpired = age > this.ttl;
7072
6999
  }
@@ -7081,13 +7008,13 @@ class FilesystemCache extends Cache {
7081
7008
  await this._acquireLock(filePath);
7082
7009
  }
7083
7010
  try {
7084
- const content = await readFile$1(filePath, this.encoding);
7011
+ const content = await promises.readFile(filePath, this.encoding);
7085
7012
  let isCompressed = false;
7086
7013
  if (this.enableMetadata) {
7087
7014
  const metadataPath = this._getMetadataPath(filePath);
7088
7015
  if (await this._fileExists(metadataPath)) {
7089
7016
  const [ok, err, metadata] = await try_fn_default(async () => {
7090
- const metaContent = await readFile$1(metadataPath, this.encoding);
7017
+ const metaContent = await promises.readFile(metadataPath, this.encoding);
7091
7018
  return JSON.parse(metaContent);
7092
7019
  });
7093
7020
  if (ok) {
@@ -7126,18 +7053,18 @@ class FilesystemCache extends Cache {
7126
7053
  const filePath = this._getFilePath(key);
7127
7054
  try {
7128
7055
  if (await this._fileExists(filePath)) {
7129
- await unlink(filePath);
7056
+ await promises.unlink(filePath);
7130
7057
  }
7131
7058
  if (this.enableMetadata) {
7132
7059
  const metadataPath = this._getMetadataPath(filePath);
7133
7060
  if (await this._fileExists(metadataPath)) {
7134
- await unlink(metadataPath);
7061
+ await promises.unlink(metadataPath);
7135
7062
  }
7136
7063
  }
7137
7064
  if (this.enableBackup) {
7138
7065
  const backupPath = filePath + this.backupSuffix;
7139
7066
  if (await this._fileExists(backupPath)) {
7140
- await unlink(backupPath);
7067
+ await promises.unlink(backupPath);
7141
7068
  }
7142
7069
  }
7143
7070
  if (this.enableStats) {
@@ -7156,7 +7083,7 @@ class FilesystemCache extends Cache {
7156
7083
  }
7157
7084
  async _clear(prefix) {
7158
7085
  try {
7159
- const files = await readdir$1(this.directory);
7086
+ const files = await promises.readdir(this.directory);
7160
7087
  const cacheFiles = files.filter((file) => {
7161
7088
  if (!file.startsWith(this.prefix)) return false;
7162
7089
  if (!file.endsWith(this.fileExtension)) return false;
@@ -7169,18 +7096,18 @@ class FilesystemCache extends Cache {
7169
7096
  for (const file of cacheFiles) {
7170
7097
  const filePath = path.join(this.directory, file);
7171
7098
  if (await this._fileExists(filePath)) {
7172
- await unlink(filePath);
7099
+ await promises.unlink(filePath);
7173
7100
  }
7174
7101
  if (this.enableMetadata) {
7175
7102
  const metadataPath = this._getMetadataPath(filePath);
7176
7103
  if (await this._fileExists(metadataPath)) {
7177
- await unlink(metadataPath);
7104
+ await promises.unlink(metadataPath);
7178
7105
  }
7179
7106
  }
7180
7107
  if (this.enableBackup) {
7181
7108
  const backupPath = filePath + this.backupSuffix;
7182
7109
  if (await this._fileExists(backupPath)) {
7183
- await unlink(backupPath);
7110
+ await promises.unlink(backupPath);
7184
7111
  }
7185
7112
  }
7186
7113
  }
@@ -7204,7 +7131,7 @@ class FilesystemCache extends Cache {
7204
7131
  }
7205
7132
  async keys() {
7206
7133
  try {
7207
- const files = await readdir$1(this.directory);
7134
+ const files = await promises.readdir(this.directory);
7208
7135
  const cacheFiles = files.filter(
7209
7136
  (file) => file.startsWith(this.prefix) && file.endsWith(this.fileExtension)
7210
7137
  );
@@ -7221,14 +7148,14 @@ class FilesystemCache extends Cache {
7221
7148
  // Helper methods
7222
7149
  async _fileExists(filePath) {
7223
7150
  const [ok] = await try_fn_default(async () => {
7224
- await stat$1(filePath);
7151
+ await promises.stat(filePath);
7225
7152
  });
7226
7153
  return ok;
7227
7154
  }
7228
7155
  async _copyFile(src, dest) {
7229
7156
  const [ok, err] = await try_fn_default(async () => {
7230
- const content = await readFile$1(src);
7231
- await writeFile$1(dest, content);
7157
+ const content = await promises.readFile(src);
7158
+ await promises.writeFile(dest, content);
7232
7159
  });
7233
7160
  if (!ok) {
7234
7161
  console.warn("FilesystemCache: Failed to create backup:", err.message);
@@ -7237,7 +7164,7 @@ class FilesystemCache extends Cache {
7237
7164
  async _cleanup() {
7238
7165
  if (!this.ttl || this.ttl <= 0) return;
7239
7166
  try {
7240
- const files = await readdir$1(this.directory);
7167
+ const files = await promises.readdir(this.directory);
7241
7168
  const now = Date.now();
7242
7169
  for (const file of files) {
7243
7170
  if (!file.startsWith(this.prefix) || !file.endsWith(this.fileExtension)) {
@@ -7249,7 +7176,7 @@ class FilesystemCache extends Cache {
7249
7176
  const metadataPath = this._getMetadataPath(filePath);
7250
7177
  if (await this._fileExists(metadataPath)) {
7251
7178
  const [ok, err, metadata] = await try_fn_default(async () => {
7252
- const metaContent = await readFile$1(metadataPath, this.encoding);
7179
+ const metaContent = await promises.readFile(metadataPath, this.encoding);
7253
7180
  return JSON.parse(metaContent);
7254
7181
  });
7255
7182
  if (ok && metadata.ttl > 0) {
@@ -7259,7 +7186,7 @@ class FilesystemCache extends Cache {
7259
7186
  }
7260
7187
  } else {
7261
7188
  const [ok, err, stats] = await try_fn_default(async () => {
7262
- return await stat$1(filePath);
7189
+ return await promises.stat(filePath);
7263
7190
  });
7264
7191
  if (ok) {
7265
7192
  const age = now - stats.mtime.getTime();
@@ -7329,12 +7256,6 @@ class FilesystemCache extends Cache {
7329
7256
  }
7330
7257
  }
7331
7258
 
7332
- promisify(fs.mkdir);
7333
- const rmdir = promisify(fs.rm);
7334
- const readdir = promisify(fs.readdir);
7335
- const stat = promisify(fs.stat);
7336
- const writeFile = promisify(fs.writeFile);
7337
- const readFile = promisify(fs.readFile);
7338
7259
  class PartitionAwareFilesystemCache extends FilesystemCache {
7339
7260
  constructor({
7340
7261
  partitionStrategy = "hierarchical",
@@ -7456,7 +7377,7 @@ class PartitionAwareFilesystemCache extends FilesystemCache {
7456
7377
  const partitionDir = this._getPartitionDirectory(resource, partition, partitionValues);
7457
7378
  const [ok, err] = await try_fn_default(async () => {
7458
7379
  if (await this._fileExists(partitionDir)) {
7459
- await rmdir(partitionDir, { recursive: true });
7380
+ await promises.rm(partitionDir, { recursive: true });
7460
7381
  }
7461
7382
  });
7462
7383
  if (!ok) {
@@ -7474,7 +7395,7 @@ class PartitionAwareFilesystemCache extends FilesystemCache {
7474
7395
  const resourceDir = path.join(this.directory, `resource=${resource}`);
7475
7396
  const [ok, err] = await try_fn_default(async () => {
7476
7397
  if (await this._fileExists(resourceDir)) {
7477
- await rmdir(resourceDir, { recursive: true });
7398
+ await promises.rm(resourceDir, { recursive: true });
7478
7399
  }
7479
7400
  });
7480
7401
  for (const [key] of this.partitionUsage.entries()) {
@@ -7606,11 +7527,11 @@ class PartitionAwareFilesystemCache extends FilesystemCache {
7606
7527
  return filename.replace(/[<>:"/\\|?*]/g, "_");
7607
7528
  }
7608
7529
  async _calculateDirectoryStats(dir, stats) {
7609
- const [ok, err, files] = await try_fn_default(() => readdir(dir));
7530
+ const [ok, err, files] = await try_fn_default(() => promises.readdir(dir));
7610
7531
  if (!ok) return;
7611
7532
  for (const file of files) {
7612
7533
  const filePath = path.join(dir, file);
7613
- const [statOk, statErr, fileStat] = await try_fn_default(() => stat(filePath));
7534
+ const [statOk, statErr, fileStat] = await try_fn_default(() => promises.stat(filePath));
7614
7535
  if (statOk) {
7615
7536
  if (fileStat.isDirectory()) {
7616
7537
  await this._calculateDirectoryStats(filePath, stats);
@@ -7623,7 +7544,7 @@ class PartitionAwareFilesystemCache extends FilesystemCache {
7623
7544
  }
7624
7545
  async loadUsageStats() {
7625
7546
  const [ok, err, content] = await try_fn_default(async () => {
7626
- const data = await readFile(this.usageStatsFile, "utf8");
7547
+ const data = await promises.readFile(this.usageStatsFile, "utf8");
7627
7548
  return JSON.parse(data);
7628
7549
  });
7629
7550
  if (ok && content) {
@@ -7633,7 +7554,7 @@ class PartitionAwareFilesystemCache extends FilesystemCache {
7633
7554
  async _saveUsageStats() {
7634
7555
  const statsObject = Object.fromEntries(this.partitionUsage);
7635
7556
  await try_fn_default(async () => {
7636
- await writeFile(
7557
+ await promises.writeFile(
7637
7558
  this.usageStatsFile,
7638
7559
  JSON.stringify(statsObject, null, 2),
7639
7560
  "utf8"
@@ -7643,7 +7564,7 @@ class PartitionAwareFilesystemCache extends FilesystemCache {
7643
7564
  async _writeFileWithMetadata(filePath, data) {
7644
7565
  const content = JSON.stringify(data);
7645
7566
  const [ok, err] = await try_fn_default(async () => {
7646
- await writeFile(filePath, content, {
7567
+ await promises.writeFile(filePath, content, {
7647
7568
  encoding: this.encoding,
7648
7569
  mode: this.fileMode
7649
7570
  });
@@ -7655,7 +7576,7 @@ class PartitionAwareFilesystemCache extends FilesystemCache {
7655
7576
  }
7656
7577
  async _readFileWithMetadata(filePath) {
7657
7578
  const [ok, err, content] = await try_fn_default(async () => {
7658
- return await readFile(filePath, this.encoding);
7579
+ return await promises.readFile(filePath, this.encoding);
7659
7580
  });
7660
7581
  if (!ok || !content) return null;
7661
7582
  try {
@@ -8432,7 +8353,7 @@ class MetricsPlugin extends plugin_class_default {
8432
8353
  }
8433
8354
  async setup(database) {
8434
8355
  this.database = database;
8435
- if (process.env.NODE_ENV === "test") return;
8356
+ if (typeof process !== "undefined" && process.env.NODE_ENV === "test") return;
8436
8357
  const [ok, err] = await try_fn_default(async () => {
8437
8358
  const [ok1, err1, metricsResource] = await try_fn_default(() => database.createResource({
8438
8359
  name: "metrics",
@@ -8482,7 +8403,7 @@ class MetricsPlugin extends plugin_class_default {
8482
8403
  this.performanceResource = database.resources.performance_logs;
8483
8404
  }
8484
8405
  this.installMetricsHooks();
8485
- if (process.env.NODE_ENV !== "test") {
8406
+ if (typeof process !== "undefined" && process.env.NODE_ENV !== "test") {
8486
8407
  this.startFlushTimer();
8487
8408
  }
8488
8409
  }
@@ -8493,7 +8414,7 @@ class MetricsPlugin extends plugin_class_default {
8493
8414
  clearInterval(this.flushTimer);
8494
8415
  this.flushTimer = null;
8495
8416
  }
8496
- if (process.env.NODE_ENV !== "test") {
8417
+ if (typeof process !== "undefined" && process.env.NODE_ENV !== "test") {
8497
8418
  await this.flushMetrics();
8498
8419
  }
8499
8420
  }
@@ -8672,10 +8593,18 @@ class MetricsPlugin extends plugin_class_default {
8672
8593
  async flushMetrics() {
8673
8594
  if (!this.metricsResource) return;
8674
8595
  const [ok, err] = await try_fn_default(async () => {
8675
- const metadata = process.env.NODE_ENV === "test" ? {} : { global: "true" };
8676
- const perfMetadata = process.env.NODE_ENV === "test" ? {} : { perf: "true" };
8677
- const errorMetadata = process.env.NODE_ENV === "test" ? {} : { error: "true" };
8678
- const resourceMetadata = process.env.NODE_ENV === "test" ? {} : { resource: "true" };
8596
+ let metadata, perfMetadata, errorMetadata, resourceMetadata;
8597
+ if (typeof process !== "undefined" && process.env.NODE_ENV === "test") {
8598
+ metadata = {};
8599
+ perfMetadata = {};
8600
+ errorMetadata = {};
8601
+ resourceMetadata = {};
8602
+ } else {
8603
+ metadata = { global: "true" };
8604
+ perfMetadata = { perf: "true" };
8605
+ errorMetadata = { error: "true" };
8606
+ resourceMetadata = { resource: "true" };
8607
+ }
8679
8608
  for (const [operation, data] of Object.entries(this.metrics.operations)) {
8680
8609
  if (data.count > 0) {
8681
8610
  await this.metricsResource.insert({
@@ -13208,7 +13137,7 @@ class Database extends EventEmitter {
13208
13137
  super();
13209
13138
  this.version = "1";
13210
13139
  this.s3dbVersion = (() => {
13211
- const [ok, err, version] = try_fn_default(() => true ? "7.3.3" : "latest");
13140
+ const [ok, err, version] = try_fn_default(() => true ? "7.3.4" : "latest");
13212
13141
  return ok ? version : "latest";
13213
13142
  })();
13214
13143
  this.resources = {};
@@ -13251,14 +13180,16 @@ class Database extends EventEmitter {
13251
13180
  this.keyPrefix = this.client.keyPrefix;
13252
13181
  if (!this._exitListenerRegistered) {
13253
13182
  this._exitListenerRegistered = true;
13254
- process.on("exit", async () => {
13255
- if (this.isConnected()) {
13256
- try {
13257
- await this.disconnect();
13258
- } catch (err) {
13183
+ if (typeof process !== "undefined") {
13184
+ process.on("exit", async () => {
13185
+ if (this.isConnected()) {
13186
+ try {
13187
+ await this.disconnect();
13188
+ } catch (err) {
13189
+ }
13259
13190
  }
13260
- }
13261
- });
13191
+ });
13192
+ }
13262
13193
  }
13263
13194
  }
13264
13195
  async connect() {
@@ -13841,7 +13772,7 @@ class S3dbReplicator extends base_replicator_class_default {
13841
13772
  if (typeof entry[0] === "function") return resource;
13842
13773
  }
13843
13774
  if (typeof entry === "string") return entry;
13844
- if (resource && !targetResourceName) targetResourceName = resource;
13775
+ if (typeof entry === "function") return resource;
13845
13776
  if (typeof entry === "object" && entry.resource) return entry.resource;
13846
13777
  return resource;
13847
13778
  }
@@ -14390,31 +14321,11 @@ class ReplicatorPlugin extends plugin_class_default {
14390
14321
  }
14391
14322
  async stop() {
14392
14323
  }
14393
- filterInternalFields(data) {
14394
- if (!data || typeof data !== "object") return data;
14395
- const filtered = {};
14396
- for (const [key, value] of Object.entries(data)) {
14397
- if (!key.startsWith("_") && !key.startsWith("$")) {
14398
- filtered[key] = value;
14399
- }
14400
- }
14401
- return filtered;
14402
- }
14403
14324
  async uploadMetadataFile(database) {
14404
14325
  if (typeof database.uploadMetadataFile === "function") {
14405
14326
  await database.uploadMetadataFile();
14406
14327
  }
14407
14328
  }
14408
- async getCompleteData(resource, data) {
14409
- try {
14410
- const [ok, err, record] = await try_fn_default(() => resource.get(data.id));
14411
- if (ok && record) {
14412
- return record;
14413
- }
14414
- } catch (error) {
14415
- }
14416
- return data;
14417
- }
14418
14329
  async retryWithBackoff(operation, maxRetries = 3) {
14419
14330
  let lastError;
14420
14331
  for (let attempt = 1; attempt <= maxRetries; attempt++) {