@nextlyhq/storage-s3 0.0.2-alpha.73 → 0.0.2-alpha.74

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/index.cjs CHANGED
@@ -2,7 +2,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
2
  let _aws_sdk_client_s3 = require("@aws-sdk/client-s3");
3
3
  let _aws_sdk_lib_storage = require("@aws-sdk/lib-storage");
4
4
  let _aws_sdk_s3_request_presigner = require("@aws-sdk/s3-request-presigner");
5
- // ../nextly/dist/nextly-error-BLaA7N-v.mjs
5
+ // ../nextly/dist/nextly-error-D08zf_L_.mjs
6
6
  function isDbError(err) {
7
7
  if (!err || typeof err !== "object") return false;
8
8
  const obj = err;
@@ -78,12 +78,15 @@ const NEXTLY_ERROR_STATUS = {
78
78
  CSRF_FAILED: 403,
79
79
  SETUP_TOKEN_REQUIRED: 403,
80
80
  API_KEY_ROLE_ABOVE_CREATOR: 403,
81
+ ACTION_NOT_AVAILABLE: 403,
81
82
  NOT_FOUND: 404,
82
83
  ENTITY_REMOVED_FROM_CODE: 409,
83
84
  ENTITY_NOT_MIGRATED: 409,
84
85
  METHOD_NOT_ALLOWED: 405,
85
86
  RANGE_NOT_SATISFIABLE: 416,
86
87
  CONFLICT: 409,
88
+ STALE_VERSION: 409,
89
+ VERSION_REQUIRED: 428,
87
90
  DUPLICATE: 409,
88
91
  RATE_LIMITED: 429,
89
92
  PAYLOAD_TOO_LARGE: 413,
@@ -92,6 +95,7 @@ const NEXTLY_ERROR_STATUS = {
92
95
  INTERNAL_ERROR: 500,
93
96
  DATABASE_ERROR: 500,
94
97
  DATABASE_BUSY: 503,
98
+ RULE_COULD_NOT_RUN: 503,
95
99
  EXTERNAL_SERVICE_ERROR: 502,
96
100
  SERVICE_UNAVAILABLE: 503,
97
101
  CONFIGURATION_ERROR: 500,
@@ -138,9 +142,11 @@ const NEXTLY_ERROR_STATUS = {
138
142
  NEXTLY_PLUGIN_ADMIN_WIDGET_INVALID: 500,
139
143
  NEXTLY_PLUGIN_REMOVED_WIDGET_CHANNEL: 500,
140
144
  NEXTLY_PLUGIN_DOCUMENT_VIEW_INVALID: 500,
145
+ NEXTLY_PLUGIN_DOCUMENT_SURFACE_INVALID: 500,
141
146
  PLUGIN_RESOLUTION_ERROR: 500,
142
147
  PLUGIN_DEPENDENCY_NOT_INSTALLED: 409,
143
148
  PLUGIN_HAS_DEPENDENTS: 409,
149
+ PLUGIN_UNINSTALL_WOULD_DROP_OTHERS: 409,
144
150
  PLUGIN_UNINSTALL_IRREVERSIBLE: 409,
145
151
  PLUGIN_UNINSTALL_UNCONFIRMED: 409,
146
152
  PLUGIN_SCHEMA_BEHIND: 409,
@@ -164,6 +170,9 @@ const NEXTLY_ERROR_STATUS = {
164
170
  NEXTLY_EMAIL_TRANSPORT_UNAVAILABLE: 503,
165
171
  NEXTLY_CONFIG_TOOLING_UNAVAILABLE: 503,
166
172
  NEXTLY_POSTGRES_SCHEMA_MISMATCH: 500,
173
+ NEXTLY_PER_LANGUAGE_STORAGE_CHANGED: 500,
174
+ NEXTLY_PER_LANGUAGE_STORAGE_MIXED: 500,
175
+ NEXTLY_PER_LANGUAGE_STORAGE_LANGUAGES_UNDECLARED: 500,
167
176
  NEXTLY_POSTGRES_SCHEMA_UNSUPPORTED: 500
168
177
  };
169
178
  ({ ...Object.fromEntries([
@@ -172,6 +181,7 @@ const NEXTLY_ERROR_STATUS = {
172
181
  "FORBIDDEN",
173
182
  "NOT_FOUND",
174
183
  "CONFLICT",
184
+ "VERSION_REQUIRED",
175
185
  "PAYLOAD_TOO_LARGE",
176
186
  "UNSUPPORTED_MEDIA_TYPE",
177
187
  "RATE_LIMITED",
@@ -195,6 +205,7 @@ const GENERIC_PUBLIC_MESSAGE = {
195
205
  INVALID_INPUT: "The request could not be processed.",
196
206
  AUTH_REQUIRED: "Authentication required.",
197
207
  FORBIDDEN: "You don't have permission to perform this action.",
208
+ ACTION_NOT_AVAILABLE: "This action isn't available for this entry.",
198
209
  NOT_FOUND: "Not found.",
199
210
  METHOD_NOT_ALLOWED: "That method is not allowed on this address.",
200
211
  RANGE_NOT_SATISFIABLE: "The requested range is outside the file.",
@@ -206,6 +217,7 @@ const GENERIC_PUBLIC_MESSAGE = {
206
217
  EXTERNAL_SERVICE_ERROR: "An upstream service failed.",
207
218
  SERVICE_UNAVAILABLE: "The service is temporarily unavailable.",
208
219
  DATABASE_BUSY: "The database is busy. Please try again shortly.",
220
+ RULE_COULD_NOT_RUN: "A check this request needs could not run. Please try again shortly.",
209
221
  INTERNAL_ERROR: "An unexpected error occurred."
210
222
  };
211
223
  /** The generic sentence for a code; the internal one for anything unlisted. */
@@ -413,6 +425,7 @@ var NextlyError = class NextlyError extends Error {
413
425
  return new NextlyError({
414
426
  code: "FORBIDDEN",
415
427
  publicMessage: "You don't have permission to perform this action.",
428
+ publicData: opts?.publicData,
416
429
  cause: opts?.cause,
417
430
  logContext: opts?.logContext
418
431
  });
@@ -614,6 +627,60 @@ var NextlyError = class NextlyError extends Error {
614
627
  }
615
628
  });
616
629
  }
630
+ /**
631
+ * An update made from a version of the document that is no longer its
632
+ * current one. Never retried: the same version stays out of date, so the
633
+ * caller must read the document again and decide.
634
+ *
635
+ * @experimental
636
+ */
637
+ /**
638
+ * An update that said neither which version of a document it was made from
639
+ * nor that it overwrites whatever is stored. Without `bulk`, an update of
640
+ * one document, which sends back the version it read. `bulk` names an
641
+ * update that writes the same values to many documents, which can carry no
642
+ * one document's version: by its ids, which can be given a version each
643
+ * (`keys.revs`), or by a query, which cannot. The keys are the write path's
644
+ * to name, so the message spells them as the request does.
645
+ *
646
+ * @experimental
647
+ */
648
+ static versionRequired(opts) {
649
+ const { version, overwrite, revs } = opts.keys;
650
+ if (opts.bulk === void 0) {
651
+ const one = {
652
+ versionKey: version,
653
+ overwriteKey: overwrite
654
+ };
655
+ return new NextlyError({
656
+ code: "VERSION_REQUIRED",
657
+ publicMessage: `This update must say which version of the document it changes. Send back the \`${version}\` from your last read of it, or set \`${overwrite}: true\` to replace whatever is stored.`,
658
+ publicData: one,
659
+ logContext: opts.logContext
660
+ });
661
+ }
662
+ const byIds = opts.bulk === "ids";
663
+ const data = {
664
+ versionKey: version,
665
+ overwriteKey: overwrite,
666
+ ...byIds ? { revsKey: revs } : {}
667
+ };
668
+ const message = `An update that writes the same values to many documents must set \`${overwrite}: true\`: it can't carry each document's version. Or update each document with its own \`${version}\`.`;
669
+ return new NextlyError({
670
+ code: "VERSION_REQUIRED",
671
+ publicMessage: byIds ? `${message} Or give each document's \`${version}\` in \`${revs}\`.` : message,
672
+ publicData: data,
673
+ logContext: opts.logContext
674
+ });
675
+ }
676
+ static staleVersion(opts) {
677
+ return new NextlyError({
678
+ code: "STALE_VERSION",
679
+ publicMessage: `This document changed since the version you gave. Its current version is ${opts.data.currentVersion}.`,
680
+ publicData: opts.data,
681
+ logContext: opts.logContext
682
+ });
683
+ }
617
684
  static duplicate(opts) {
618
685
  return new NextlyError({
619
686
  code: "DUPLICATE",
@@ -647,6 +714,27 @@ var NextlyError = class NextlyError extends Error {
647
714
  });
648
715
  }
649
716
  /**
717
+ * An access or field rule the request needs could not run, because its own
718
+ * database query waited out the bound for a connection. Not a refusal: the
719
+ * rule never answered. Names the collection or Single whose rule it is, and
720
+ * the field for a field's rule, and says when to retry.
721
+ */
722
+ static ruleCouldNotRun(opts) {
723
+ const publicData = {
724
+ subject: opts.subject,
725
+ ...opts.field !== void 0 ? { field: opts.field } : {},
726
+ retryAfterSeconds: 5
727
+ };
728
+ return new NextlyError({
729
+ code: "RULE_COULD_NOT_RUN",
730
+ statusCode: NEXTLY_ERROR_STATUS.RULE_COULD_NOT_RUN,
731
+ publicMessage: genericPublicMessage("RULE_COULD_NOT_RUN"),
732
+ publicData,
733
+ cause: opts.cause,
734
+ logContext: opts.logContext
735
+ });
736
+ }
737
+ /**
650
738
  * What a request boundary answers with for a thrown value that is not a
651
739
  * NextlyError: "database busy" when the request waited out its bound for a
652
740
  * connection, which is retryable and no fault of the server's code, and an
@@ -766,7 +854,7 @@ var NextlyError = class NextlyError extends Error {
766
854
  return NextlyError.isCode(err, "RATE_LIMITED");
767
855
  }
768
856
  };
769
- // ../nextly/dist/validate-external-url-DpPuwVvV.mjs
857
+ // ../nextly/dist/validate-external-url-BKFCMuwY.mjs
770
858
  /** Default response body cap: reject anything larger before buffering it all. */
771
859
  /**
772
860
  * How many bytes a bounded external read may buffer, absent a caller's own cap.
@@ -934,19 +1022,24 @@ function abortAfter(options) {
934
1022
  }
935
1023
  var S3StorageAdapter = class {
936
1024
  config;
937
- client;
1025
+ client = null;
938
1026
  resolvedConfig;
939
1027
  isR2;
940
1028
  /**
941
1029
  * Create a new S3 storage adapter.
942
1030
  *
1031
+ * The bucket and region are deliberately NOT checked here, and the SDK
1032
+ * client is deliberately not built. This constructor runs while the host's
1033
+ * config file is evaluated, so a throw would stop every command —
1034
+ * including the schema-only ones that never touch storage. Both the checks
1035
+ * and the client construction move to the first storage operation
1036
+ * ({@link ensureClient} and {@link ensureConfigured}), which refuse with
1037
+ * the same messages a missing bucket or region has always produced.
1038
+ *
943
1039
  * @param config - S3 storage configuration
944
- * @throws Error if bucket or region is not provided
945
1040
  */
946
1041
  constructor(config) {
947
1042
  this.config = config;
948
- if (!config.bucket) throw new Error("@nextly/storage-s3: bucket is required");
949
- if (!config.region) throw new Error("@nextly/storage-s3: region is required");
950
1043
  this.isR2 = config.endpoint?.includes("r2.cloudflarestorage.com") ?? false;
951
1044
  this.resolvedConfig = {
952
1045
  bucket: config.bucket,
@@ -959,14 +1052,42 @@ var S3StorageAdapter = class {
959
1052
  contentDisposition: config.contentDisposition,
960
1053
  signedUrlExpiresIn: config.signedUrlExpiresIn ?? 3600
961
1054
  };
1055
+ }
1056
+ /**
1057
+ * Validate that bucket and region are present.
1058
+ *
1059
+ * @throws Error if bucket is not provided
1060
+ * @throws Error if region is not provided
1061
+ */
1062
+ ensureConfigured() {
1063
+ if (!this.config.bucket) throw new Error("@nextly/storage-s3: bucket is required");
1064
+ if (!this.config.region) throw new Error("@nextly/storage-s3: region is required");
1065
+ }
1066
+ /**
1067
+ * Validate the stored configuration and build the SDK client, at the
1068
+ * first use that needs them.
1069
+ *
1070
+ * The refusals are the ones the constructor used to throw, in the same
1071
+ * order, so an operator with a genuinely misconfigured deployment sees
1072
+ * the message they have always seen — at the moment the value was
1073
+ * actually needed rather than at config load. The client is built once
1074
+ * and reused.
1075
+ *
1076
+ * @throws Error if bucket is not provided
1077
+ * @throws Error if region is not provided
1078
+ */
1079
+ ensureClient() {
1080
+ if (this.client) return this.client;
1081
+ this.ensureConfigured();
962
1082
  const credentials = this.buildCredentials();
963
1083
  this.client = new _aws_sdk_client_s3.S3Client({
964
- region: config.region,
965
- endpoint: config.endpoint,
1084
+ region: this.config.region,
1085
+ endpoint: this.config.endpoint,
966
1086
  credentials,
967
1087
  forcePathStyle: this.resolvedConfig.forcePathStyle,
968
- ...config.config
1088
+ ...this.config.config
969
1089
  });
1090
+ return this.client;
970
1091
  }
971
1092
  /**
972
1093
  * Build AWS credentials from config.
@@ -993,6 +1114,7 @@ var S3StorageAdapter = class {
993
1114
  * @returns Upload result with URL and storage path
994
1115
  */
995
1116
  async upload(buffer, options) {
1117
+ const client = this.ensureClient();
996
1118
  const key = this.generateKey(options.filename, options.folder);
997
1119
  const uploadParams = {
998
1120
  Bucket: this.resolvedConfig.bucket,
@@ -1009,7 +1131,7 @@ var S3StorageAdapter = class {
1009
1131
  }
1010
1132
  uploadParams.Metadata = { "original-filename": options.filename };
1011
1133
  await new _aws_sdk_lib_storage.Upload({
1012
- client: this.client,
1134
+ client,
1013
1135
  params: uploadParams
1014
1136
  }).done();
1015
1137
  return {
@@ -1023,11 +1145,12 @@ var S3StorageAdapter = class {
1023
1145
  * @param filePath - Storage path/key to delete
1024
1146
  */
1025
1147
  async delete(filePath) {
1148
+ const client = this.ensureClient();
1026
1149
  const command = new _aws_sdk_client_s3.DeleteObjectCommand({
1027
1150
  Bucket: this.resolvedConfig.bucket,
1028
1151
  Key: filePath
1029
1152
  });
1030
- await this.client.send(command);
1153
+ await client.send(command);
1031
1154
  }
1032
1155
  /**
1033
1156
  * Bulk delete files from S3 using a single API call per 1000 keys.
@@ -1039,6 +1162,7 @@ var S3StorageAdapter = class {
1039
1162
  * @returns Object with arrays of successful and failed deletions
1040
1163
  */
1041
1164
  async bulkDelete(filePaths) {
1165
+ const client = this.ensureClient();
1042
1166
  const successful = [];
1043
1167
  const failed = [];
1044
1168
  const maxBatchSize = 1e3;
@@ -1051,7 +1175,7 @@ var S3StorageAdapter = class {
1051
1175
  Quiet: false
1052
1176
  }
1053
1177
  });
1054
- const response = await this.client.send(command);
1178
+ const response = await client.send(command);
1055
1179
  if (response.Errors && response.Errors.length > 0) {
1056
1180
  for (const err of response.Errors) if (err.Key) failed.push({
1057
1181
  filePath: err.Key,
@@ -1077,12 +1201,13 @@ var S3StorageAdapter = class {
1077
1201
  * @returns true if file exists, false otherwise
1078
1202
  */
1079
1203
  async exists(filePath) {
1204
+ const client = this.ensureClient();
1080
1205
  try {
1081
1206
  const command = new _aws_sdk_client_s3.HeadObjectCommand({
1082
1207
  Bucket: this.resolvedConfig.bucket,
1083
1208
  Key: filePath
1084
1209
  });
1085
- await this.client.send(command);
1210
+ await client.send(command);
1086
1211
  return true;
1087
1212
  } catch (error) {
1088
1213
  if (this.isNotFoundError(error)) return false;
@@ -1107,9 +1232,10 @@ var S3StorageAdapter = class {
1107
1232
  * of up to 1,000 at a time.
1108
1233
  */
1109
1234
  async *list() {
1235
+ const client = this.ensureClient();
1110
1236
  let token;
1111
1237
  do {
1112
- const page = await this.client.send(new _aws_sdk_client_s3.ListObjectsV2Command({
1238
+ const page = await client.send(new _aws_sdk_client_s3.ListObjectsV2Command({
1113
1239
  Bucket: this.resolvedConfig.bucket,
1114
1240
  ContinuationToken: token
1115
1241
  }));
@@ -1121,6 +1247,7 @@ var S3StorageAdapter = class {
1121
1247
  } while (token);
1122
1248
  }
1123
1249
  getPublicUrl(filePath) {
1250
+ this.ensureConfigured();
1124
1251
  if (this.resolvedConfig.publicUrl) return `${this.resolvedConfig.publicUrl.replace(/\/$/, "")}/${filePath}`;
1125
1252
  if (this.isR2) throw new Error("@nextly/storage-s3: Cloudflare R2 requires publicUrl configuration.\n\nR2 does not have default public URLs like AWS S3. Configure one of:\n1. Public bucket URL: publicUrl: 'https://pub-xxx.r2.dev'\n2. Custom domain: publicUrl: 'https://cdn.example.com'\n\nSet up public access in the Cloudflare R2 dashboard.");
1126
1253
  return `https://${this.resolvedConfig.bucket}.s3.${this.resolvedConfig.region}.amazonaws.com/${filePath}`;
@@ -1157,12 +1284,13 @@ var S3StorageAdapter = class {
1157
1284
  * @returns File metadata or null if file not found
1158
1285
  */
1159
1286
  async getMetadata(filePath) {
1287
+ const client = this.ensureClient();
1160
1288
  try {
1161
1289
  const command = new _aws_sdk_client_s3.HeadObjectCommand({
1162
1290
  Bucket: this.resolvedConfig.bucket,
1163
1291
  Key: filePath
1164
1292
  });
1165
- const response = await this.client.send(command);
1293
+ const response = await client.send(command);
1166
1294
  const filename = filePath.split("/").pop() || filePath;
1167
1295
  return {
1168
1296
  id: filePath,
@@ -1203,12 +1331,13 @@ var S3StorageAdapter = class {
1203
1331
  * @returns The object's bytes, or `null` when no such key exists
1204
1332
  */
1205
1333
  async read(filePath, options) {
1334
+ const client = this.ensureClient();
1206
1335
  try {
1207
1336
  const command = new _aws_sdk_client_s3.GetObjectCommand({
1208
1337
  Bucket: this.resolvedConfig.bucket,
1209
1338
  Key: filePath
1210
1339
  });
1211
- const response = await this.client.send(command, abortAfter(options));
1340
+ const response = await client.send(command, abortAfter(options));
1212
1341
  if (response.Body === void 0) return Buffer.alloc(0);
1213
1342
  const cap = resolveReadBounds(options).maxBytes;
1214
1343
  this.refuseOverCap(filePath, response.ContentLength, cap);
@@ -1230,11 +1359,12 @@ var S3StorageAdapter = class {
1230
1359
  * @returns Pre-signed URL for downloading the file
1231
1360
  */
1232
1361
  async getSignedUrl(filePath, expiresIn) {
1362
+ const client = this.ensureClient();
1233
1363
  const command = new _aws_sdk_client_s3.GetObjectCommand({
1234
1364
  Bucket: this.resolvedConfig.bucket,
1235
1365
  Key: filePath
1236
1366
  });
1237
- return (0, _aws_sdk_s3_request_presigner.getSignedUrl)(this.client, command, { expiresIn: expiresIn ?? this.resolvedConfig.signedUrlExpiresIn });
1367
+ return (0, _aws_sdk_s3_request_presigner.getSignedUrl)(client, command, { expiresIn: expiresIn ?? this.resolvedConfig.signedUrlExpiresIn });
1238
1368
  }
1239
1369
  /**
1240
1370
  * Generate pre-signed URL for client-side uploads.
@@ -1248,6 +1378,7 @@ var S3StorageAdapter = class {
1248
1378
  * @returns Client upload data with URL, method, and headers
1249
1379
  */
1250
1380
  async getPresignedUploadUrl(key, mimeType, expiresIn) {
1381
+ const client = this.ensureClient();
1251
1382
  const expiration = expiresIn ?? this.resolvedConfig.signedUrlExpiresIn;
1252
1383
  const commandParams = {
1253
1384
  Bucket: this.resolvedConfig.bucket,
@@ -1258,7 +1389,7 @@ var S3StorageAdapter = class {
1258
1389
  if (!this.isR2) commandParams.ACL = this.resolvedConfig.acl;
1259
1390
  const command = new _aws_sdk_client_s3.PutObjectCommand(commandParams);
1260
1391
  return {
1261
- uploadUrl: await (0, _aws_sdk_s3_request_presigner.getSignedUrl)(this.client, command, { expiresIn: expiration }),
1392
+ uploadUrl: await (0, _aws_sdk_s3_request_presigner.getSignedUrl)(client, command, { expiresIn: expiration }),
1262
1393
  path: key,
1263
1394
  method: "PUT",
1264
1395
  headers: { "Content-Type": mimeType },
@@ -1357,9 +1488,13 @@ var S3StorageAdapter = class {
1357
1488
  /**
1358
1489
  * Get the S3 client instance.
1359
1490
  * Useful for advanced operations not covered by the adapter interface.
1491
+ *
1492
+ * Handing out the client is itself a use of the adapter, so a caller
1493
+ * without the configuration a client needs is refused here like any
1494
+ * other operation.
1360
1495
  */
1361
1496
  getClient() {
1362
- return this.client;
1497
+ return this.ensureClient();
1363
1498
  }
1364
1499
  /**
1365
1500
  * Get the bucket name.
@@ -1396,8 +1531,8 @@ var S3StorageAdapter = class {
1396
1531
  * @param config - S3 storage configuration
1397
1532
  * @returns A StoragePlugin that MediaStorage can register
1398
1533
  *
1399
- * @throws Error if bucket is not provided (via adapter)
1400
- * @throws Error if region is not provided (via adapter)
1534
+ * Constructing the plugin never requires the bucket or region: a missing
1535
+ * one is refused when storage is first used, not while the config loads.
1401
1536
  */
1402
1537
  function s3Storage(config) {
1403
1538
  if (config.enabled === false) return {