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

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-BElDQWyI.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,
@@ -126,6 +130,8 @@ const NEXTLY_ERROR_STATUS = {
126
130
  NEXTLY_MIGRATION_SNAPSHOT_MISSING: 404,
127
131
  NEXTLY_MIGRATION_RESOLVE_DRIFT: 409,
128
132
  NEXTLY_MIGRATION_RESOLVE_PRECONDITION: 409,
133
+ NEXTLY_MIGRATION_HISTORY_REFUSED: 409,
134
+ NEXTLY_SCHEMA_CONVERSION_UNCONFIRMED: 409,
129
135
  NEXTLY_UI_SCHEMA_INVALID: 400,
130
136
  NEXTLY_SCHEMA_SLUG_COLLISION: 409,
131
137
  NEXTLY_SCHEMA_RELATION_TARGET_MISSING: 400,
@@ -138,9 +144,11 @@ const NEXTLY_ERROR_STATUS = {
138
144
  NEXTLY_PLUGIN_ADMIN_WIDGET_INVALID: 500,
139
145
  NEXTLY_PLUGIN_REMOVED_WIDGET_CHANNEL: 500,
140
146
  NEXTLY_PLUGIN_DOCUMENT_VIEW_INVALID: 500,
147
+ NEXTLY_PLUGIN_DOCUMENT_SURFACE_INVALID: 500,
141
148
  PLUGIN_RESOLUTION_ERROR: 500,
142
149
  PLUGIN_DEPENDENCY_NOT_INSTALLED: 409,
143
150
  PLUGIN_HAS_DEPENDENTS: 409,
151
+ PLUGIN_UNINSTALL_WOULD_DROP_OTHERS: 409,
144
152
  PLUGIN_UNINSTALL_IRREVERSIBLE: 409,
145
153
  PLUGIN_UNINSTALL_UNCONFIRMED: 409,
146
154
  PLUGIN_SCHEMA_BEHIND: 409,
@@ -164,6 +172,9 @@ const NEXTLY_ERROR_STATUS = {
164
172
  NEXTLY_EMAIL_TRANSPORT_UNAVAILABLE: 503,
165
173
  NEXTLY_CONFIG_TOOLING_UNAVAILABLE: 503,
166
174
  NEXTLY_POSTGRES_SCHEMA_MISMATCH: 500,
175
+ NEXTLY_PER_LANGUAGE_STORAGE_CHANGED: 500,
176
+ NEXTLY_PER_LANGUAGE_STORAGE_MIXED: 500,
177
+ NEXTLY_PER_LANGUAGE_STORAGE_LANGUAGES_UNDECLARED: 500,
167
178
  NEXTLY_POSTGRES_SCHEMA_UNSUPPORTED: 500
168
179
  };
169
180
  ({ ...Object.fromEntries([
@@ -172,6 +183,7 @@ const NEXTLY_ERROR_STATUS = {
172
183
  "FORBIDDEN",
173
184
  "NOT_FOUND",
174
185
  "CONFLICT",
186
+ "VERSION_REQUIRED",
175
187
  "PAYLOAD_TOO_LARGE",
176
188
  "UNSUPPORTED_MEDIA_TYPE",
177
189
  "RATE_LIMITED",
@@ -195,6 +207,7 @@ const GENERIC_PUBLIC_MESSAGE = {
195
207
  INVALID_INPUT: "The request could not be processed.",
196
208
  AUTH_REQUIRED: "Authentication required.",
197
209
  FORBIDDEN: "You don't have permission to perform this action.",
210
+ ACTION_NOT_AVAILABLE: "This action isn't available for this entry.",
198
211
  NOT_FOUND: "Not found.",
199
212
  METHOD_NOT_ALLOWED: "That method is not allowed on this address.",
200
213
  RANGE_NOT_SATISFIABLE: "The requested range is outside the file.",
@@ -206,6 +219,7 @@ const GENERIC_PUBLIC_MESSAGE = {
206
219
  EXTERNAL_SERVICE_ERROR: "An upstream service failed.",
207
220
  SERVICE_UNAVAILABLE: "The service is temporarily unavailable.",
208
221
  DATABASE_BUSY: "The database is busy. Please try again shortly.",
222
+ RULE_COULD_NOT_RUN: "A check this request needs could not run. Please try again shortly.",
209
223
  INTERNAL_ERROR: "An unexpected error occurred."
210
224
  };
211
225
  /** The generic sentence for a code; the internal one for anything unlisted. */
@@ -413,6 +427,7 @@ var NextlyError = class NextlyError extends Error {
413
427
  return new NextlyError({
414
428
  code: "FORBIDDEN",
415
429
  publicMessage: "You don't have permission to perform this action.",
430
+ publicData: opts?.publicData,
416
431
  cause: opts?.cause,
417
432
  logContext: opts?.logContext
418
433
  });
@@ -614,10 +629,69 @@ var NextlyError = class NextlyError extends Error {
614
629
  }
615
630
  });
616
631
  }
632
+ /**
633
+ * An update made from a version of the document that is no longer its
634
+ * current one. Never retried: the same version stays out of date, so the
635
+ * caller must read the document again and decide.
636
+ *
637
+ * @experimental
638
+ */
639
+ /**
640
+ * An update that said neither which version of a document it was made from
641
+ * nor that it overwrites whatever is stored. Without `bulk`, an update of
642
+ * one document, which sends back the version it read. `bulk` names an
643
+ * update that writes the same values to many documents, which can carry no
644
+ * one document's version: by its ids, which can be given a version each
645
+ * (`keys.revs`), or by a query, which cannot. The keys are the write path's
646
+ * to name, so the message spells them as the request does.
647
+ *
648
+ * @experimental
649
+ */
650
+ static versionRequired(opts) {
651
+ const { version, overwrite, revs } = opts.keys;
652
+ if (opts.bulk === void 0) {
653
+ const one = {
654
+ versionKey: version,
655
+ overwriteKey: overwrite
656
+ };
657
+ return new NextlyError({
658
+ code: "VERSION_REQUIRED",
659
+ 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.`,
660
+ publicData: one,
661
+ logContext: opts.logContext
662
+ });
663
+ }
664
+ const byIds = opts.bulk === "ids";
665
+ const data = {
666
+ versionKey: version,
667
+ overwriteKey: overwrite,
668
+ ...byIds ? { revsKey: revs } : {}
669
+ };
670
+ 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}\`.`;
671
+ return new NextlyError({
672
+ code: "VERSION_REQUIRED",
673
+ publicMessage: byIds ? `${message} Or give each document's \`${version}\` in \`${revs}\`.` : message,
674
+ publicData: data,
675
+ logContext: opts.logContext
676
+ });
677
+ }
678
+ static staleVersion(opts) {
679
+ return new NextlyError({
680
+ code: "STALE_VERSION",
681
+ publicMessage: `This document changed since the version you gave. Its current version is ${opts.data.currentVersion}.`,
682
+ publicData: opts.data,
683
+ logContext: opts.logContext
684
+ });
685
+ }
686
+ /**
687
+ * `message` replaces the generic sentence where the caller can name what is
688
+ * already taken without leaking anything, such as a public address and the
689
+ * page holding it.
690
+ */
617
691
  static duplicate(opts) {
618
692
  return new NextlyError({
619
693
  code: "DUPLICATE",
620
- publicMessage: "Resource already exists.",
694
+ publicMessage: opts?.message ?? "Resource already exists.",
621
695
  logContext: opts?.logContext
622
696
  });
623
697
  }
@@ -647,6 +721,27 @@ var NextlyError = class NextlyError extends Error {
647
721
  });
648
722
  }
649
723
  /**
724
+ * An access or field rule the request needs could not run, because its own
725
+ * database query waited out the bound for a connection. Not a refusal: the
726
+ * rule never answered. Names the collection or Single whose rule it is, and
727
+ * the field for a field's rule, and says when to retry.
728
+ */
729
+ static ruleCouldNotRun(opts) {
730
+ const publicData = {
731
+ subject: opts.subject,
732
+ ...opts.field !== void 0 ? { field: opts.field } : {},
733
+ retryAfterSeconds: 5
734
+ };
735
+ return new NextlyError({
736
+ code: "RULE_COULD_NOT_RUN",
737
+ statusCode: NEXTLY_ERROR_STATUS.RULE_COULD_NOT_RUN,
738
+ publicMessage: genericPublicMessage("RULE_COULD_NOT_RUN"),
739
+ publicData,
740
+ cause: opts.cause,
741
+ logContext: opts.logContext
742
+ });
743
+ }
744
+ /**
650
745
  * What a request boundary answers with for a thrown value that is not a
651
746
  * NextlyError: "database busy" when the request waited out its bound for a
652
747
  * connection, which is retryable and no fault of the server's code, and an
@@ -766,7 +861,7 @@ var NextlyError = class NextlyError extends Error {
766
861
  return NextlyError.isCode(err, "RATE_LIMITED");
767
862
  }
768
863
  };
769
- // ../nextly/dist/validate-external-url-DpPuwVvV.mjs
864
+ // ../nextly/dist/validate-external-url-DNl_UEVd.mjs
770
865
  /** Default response body cap: reject anything larger before buffering it all. */
771
866
  /**
772
867
  * How many bytes a bounded external read may buffer, absent a caller's own cap.
@@ -934,19 +1029,24 @@ function abortAfter(options) {
934
1029
  }
935
1030
  var S3StorageAdapter = class {
936
1031
  config;
937
- client;
1032
+ client = null;
938
1033
  resolvedConfig;
939
1034
  isR2;
940
1035
  /**
941
1036
  * Create a new S3 storage adapter.
942
1037
  *
1038
+ * The bucket and region are deliberately NOT checked here, and the SDK
1039
+ * client is deliberately not built. This constructor runs while the host's
1040
+ * config file is evaluated, so a throw would stop every command —
1041
+ * including the schema-only ones that never touch storage. Both the checks
1042
+ * and the client construction move to the first storage operation
1043
+ * ({@link ensureClient} and {@link ensureConfigured}), which refuse with
1044
+ * the same messages a missing bucket or region has always produced.
1045
+ *
943
1046
  * @param config - S3 storage configuration
944
- * @throws Error if bucket or region is not provided
945
1047
  */
946
1048
  constructor(config) {
947
1049
  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
1050
  this.isR2 = config.endpoint?.includes("r2.cloudflarestorage.com") ?? false;
951
1051
  this.resolvedConfig = {
952
1052
  bucket: config.bucket,
@@ -959,14 +1059,42 @@ var S3StorageAdapter = class {
959
1059
  contentDisposition: config.contentDisposition,
960
1060
  signedUrlExpiresIn: config.signedUrlExpiresIn ?? 3600
961
1061
  };
1062
+ }
1063
+ /**
1064
+ * Validate that bucket and region are present.
1065
+ *
1066
+ * @throws Error if bucket is not provided
1067
+ * @throws Error if region is not provided
1068
+ */
1069
+ ensureConfigured() {
1070
+ if (!this.config.bucket) throw new Error("@nextly/storage-s3: bucket is required");
1071
+ if (!this.config.region) throw new Error("@nextly/storage-s3: region is required");
1072
+ }
1073
+ /**
1074
+ * Validate the stored configuration and build the SDK client, at the
1075
+ * first use that needs them.
1076
+ *
1077
+ * The refusals are the ones the constructor used to throw, in the same
1078
+ * order, so an operator with a genuinely misconfigured deployment sees
1079
+ * the message they have always seen — at the moment the value was
1080
+ * actually needed rather than at config load. The client is built once
1081
+ * and reused.
1082
+ *
1083
+ * @throws Error if bucket is not provided
1084
+ * @throws Error if region is not provided
1085
+ */
1086
+ ensureClient() {
1087
+ if (this.client) return this.client;
1088
+ this.ensureConfigured();
962
1089
  const credentials = this.buildCredentials();
963
1090
  this.client = new _aws_sdk_client_s3.S3Client({
964
- region: config.region,
965
- endpoint: config.endpoint,
1091
+ region: this.config.region,
1092
+ endpoint: this.config.endpoint,
966
1093
  credentials,
967
1094
  forcePathStyle: this.resolvedConfig.forcePathStyle,
968
- ...config.config
1095
+ ...this.config.config
969
1096
  });
1097
+ return this.client;
970
1098
  }
971
1099
  /**
972
1100
  * Build AWS credentials from config.
@@ -993,6 +1121,7 @@ var S3StorageAdapter = class {
993
1121
  * @returns Upload result with URL and storage path
994
1122
  */
995
1123
  async upload(buffer, options) {
1124
+ const client = this.ensureClient();
996
1125
  const key = this.generateKey(options.filename, options.folder);
997
1126
  const uploadParams = {
998
1127
  Bucket: this.resolvedConfig.bucket,
@@ -1009,7 +1138,7 @@ var S3StorageAdapter = class {
1009
1138
  }
1010
1139
  uploadParams.Metadata = { "original-filename": options.filename };
1011
1140
  await new _aws_sdk_lib_storage.Upload({
1012
- client: this.client,
1141
+ client,
1013
1142
  params: uploadParams
1014
1143
  }).done();
1015
1144
  return {
@@ -1023,11 +1152,12 @@ var S3StorageAdapter = class {
1023
1152
  * @param filePath - Storage path/key to delete
1024
1153
  */
1025
1154
  async delete(filePath) {
1155
+ const client = this.ensureClient();
1026
1156
  const command = new _aws_sdk_client_s3.DeleteObjectCommand({
1027
1157
  Bucket: this.resolvedConfig.bucket,
1028
1158
  Key: filePath
1029
1159
  });
1030
- await this.client.send(command);
1160
+ await client.send(command);
1031
1161
  }
1032
1162
  /**
1033
1163
  * Bulk delete files from S3 using a single API call per 1000 keys.
@@ -1039,6 +1169,7 @@ var S3StorageAdapter = class {
1039
1169
  * @returns Object with arrays of successful and failed deletions
1040
1170
  */
1041
1171
  async bulkDelete(filePaths) {
1172
+ const client = this.ensureClient();
1042
1173
  const successful = [];
1043
1174
  const failed = [];
1044
1175
  const maxBatchSize = 1e3;
@@ -1051,7 +1182,7 @@ var S3StorageAdapter = class {
1051
1182
  Quiet: false
1052
1183
  }
1053
1184
  });
1054
- const response = await this.client.send(command);
1185
+ const response = await client.send(command);
1055
1186
  if (response.Errors && response.Errors.length > 0) {
1056
1187
  for (const err of response.Errors) if (err.Key) failed.push({
1057
1188
  filePath: err.Key,
@@ -1077,12 +1208,13 @@ var S3StorageAdapter = class {
1077
1208
  * @returns true if file exists, false otherwise
1078
1209
  */
1079
1210
  async exists(filePath) {
1211
+ const client = this.ensureClient();
1080
1212
  try {
1081
1213
  const command = new _aws_sdk_client_s3.HeadObjectCommand({
1082
1214
  Bucket: this.resolvedConfig.bucket,
1083
1215
  Key: filePath
1084
1216
  });
1085
- await this.client.send(command);
1217
+ await client.send(command);
1086
1218
  return true;
1087
1219
  } catch (error) {
1088
1220
  if (this.isNotFoundError(error)) return false;
@@ -1107,9 +1239,10 @@ var S3StorageAdapter = class {
1107
1239
  * of up to 1,000 at a time.
1108
1240
  */
1109
1241
  async *list() {
1242
+ const client = this.ensureClient();
1110
1243
  let token;
1111
1244
  do {
1112
- const page = await this.client.send(new _aws_sdk_client_s3.ListObjectsV2Command({
1245
+ const page = await client.send(new _aws_sdk_client_s3.ListObjectsV2Command({
1113
1246
  Bucket: this.resolvedConfig.bucket,
1114
1247
  ContinuationToken: token
1115
1248
  }));
@@ -1121,6 +1254,7 @@ var S3StorageAdapter = class {
1121
1254
  } while (token);
1122
1255
  }
1123
1256
  getPublicUrl(filePath) {
1257
+ this.ensureConfigured();
1124
1258
  if (this.resolvedConfig.publicUrl) return `${this.resolvedConfig.publicUrl.replace(/\/$/, "")}/${filePath}`;
1125
1259
  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
1260
  return `https://${this.resolvedConfig.bucket}.s3.${this.resolvedConfig.region}.amazonaws.com/${filePath}`;
@@ -1157,12 +1291,13 @@ var S3StorageAdapter = class {
1157
1291
  * @returns File metadata or null if file not found
1158
1292
  */
1159
1293
  async getMetadata(filePath) {
1294
+ const client = this.ensureClient();
1160
1295
  try {
1161
1296
  const command = new _aws_sdk_client_s3.HeadObjectCommand({
1162
1297
  Bucket: this.resolvedConfig.bucket,
1163
1298
  Key: filePath
1164
1299
  });
1165
- const response = await this.client.send(command);
1300
+ const response = await client.send(command);
1166
1301
  const filename = filePath.split("/").pop() || filePath;
1167
1302
  return {
1168
1303
  id: filePath,
@@ -1203,12 +1338,13 @@ var S3StorageAdapter = class {
1203
1338
  * @returns The object's bytes, or `null` when no such key exists
1204
1339
  */
1205
1340
  async read(filePath, options) {
1341
+ const client = this.ensureClient();
1206
1342
  try {
1207
1343
  const command = new _aws_sdk_client_s3.GetObjectCommand({
1208
1344
  Bucket: this.resolvedConfig.bucket,
1209
1345
  Key: filePath
1210
1346
  });
1211
- const response = await this.client.send(command, abortAfter(options));
1347
+ const response = await client.send(command, abortAfter(options));
1212
1348
  if (response.Body === void 0) return Buffer.alloc(0);
1213
1349
  const cap = resolveReadBounds(options).maxBytes;
1214
1350
  this.refuseOverCap(filePath, response.ContentLength, cap);
@@ -1230,11 +1366,12 @@ var S3StorageAdapter = class {
1230
1366
  * @returns Pre-signed URL for downloading the file
1231
1367
  */
1232
1368
  async getSignedUrl(filePath, expiresIn) {
1369
+ const client = this.ensureClient();
1233
1370
  const command = new _aws_sdk_client_s3.GetObjectCommand({
1234
1371
  Bucket: this.resolvedConfig.bucket,
1235
1372
  Key: filePath
1236
1373
  });
1237
- return (0, _aws_sdk_s3_request_presigner.getSignedUrl)(this.client, command, { expiresIn: expiresIn ?? this.resolvedConfig.signedUrlExpiresIn });
1374
+ return (0, _aws_sdk_s3_request_presigner.getSignedUrl)(client, command, { expiresIn: expiresIn ?? this.resolvedConfig.signedUrlExpiresIn });
1238
1375
  }
1239
1376
  /**
1240
1377
  * Generate pre-signed URL for client-side uploads.
@@ -1248,6 +1385,7 @@ var S3StorageAdapter = class {
1248
1385
  * @returns Client upload data with URL, method, and headers
1249
1386
  */
1250
1387
  async getPresignedUploadUrl(key, mimeType, expiresIn) {
1388
+ const client = this.ensureClient();
1251
1389
  const expiration = expiresIn ?? this.resolvedConfig.signedUrlExpiresIn;
1252
1390
  const commandParams = {
1253
1391
  Bucket: this.resolvedConfig.bucket,
@@ -1258,7 +1396,7 @@ var S3StorageAdapter = class {
1258
1396
  if (!this.isR2) commandParams.ACL = this.resolvedConfig.acl;
1259
1397
  const command = new _aws_sdk_client_s3.PutObjectCommand(commandParams);
1260
1398
  return {
1261
- uploadUrl: await (0, _aws_sdk_s3_request_presigner.getSignedUrl)(this.client, command, { expiresIn: expiration }),
1399
+ uploadUrl: await (0, _aws_sdk_s3_request_presigner.getSignedUrl)(client, command, { expiresIn: expiration }),
1262
1400
  path: key,
1263
1401
  method: "PUT",
1264
1402
  headers: { "Content-Type": mimeType },
@@ -1357,9 +1495,13 @@ var S3StorageAdapter = class {
1357
1495
  /**
1358
1496
  * Get the S3 client instance.
1359
1497
  * Useful for advanced operations not covered by the adapter interface.
1498
+ *
1499
+ * Handing out the client is itself a use of the adapter, so a caller
1500
+ * without the configuration a client needs is refused here like any
1501
+ * other operation.
1360
1502
  */
1361
1503
  getClient() {
1362
- return this.client;
1504
+ return this.ensureClient();
1363
1505
  }
1364
1506
  /**
1365
1507
  * Get the bucket name.
@@ -1396,8 +1538,8 @@ var S3StorageAdapter = class {
1396
1538
  * @param config - S3 storage configuration
1397
1539
  * @returns A StoragePlugin that MediaStorage can register
1398
1540
  *
1399
- * @throws Error if bucket is not provided (via adapter)
1400
- * @throws Error if region is not provided (via adapter)
1541
+ * Constructing the plugin never requires the bucket or region: a missing
1542
+ * one is refused when storage is first used, not while the config loads.
1401
1543
  */
1402
1544
  function s3Storage(config) {
1403
1545
  if (config.enabled === false) return {