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

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.d.cts CHANGED
@@ -168,15 +168,25 @@ interface StoragePlugin {
168
168
  adapter: IStorageAdapter;
169
169
  /**
170
170
  * Handler for generating client-side upload URLs.
171
- * Called when clientUploads is enabled for a collection.
171
+ * Called when clientUploads is enabled for a collection, for a file whose
172
+ * name, type and size the upload checks have already allowed. The URL must
173
+ * bind the type and `options.size`, so storage refuses a different file:
174
+ * its bytes never pass Nextly's checks.
172
175
  */
173
- getClientUploadUrl?: (filename: string, mimeType: string, collection: string) => Promise<ClientUploadData>;
176
+ getClientUploadUrl?: (filename: string, mimeType: string, collection: string, options: ClientUploadOptions) => Promise<ClientUploadData>;
174
177
  /**
175
178
  * Handler for generating signed download URLs.
176
179
  * Called when signedDownloads is enabled for a collection.
177
180
  */
178
181
  getSignedDownloadUrl?: (path: string, expiresIn?: number) => Promise<string>;
179
182
  }
183
+ /** What a signed client upload is bound to, besides its key and type. */
184
+ interface ClientUploadOptions {
185
+ /** The file's exact size in bytes; storage refuses a body of another length. */
186
+ size: number;
187
+ /** How long the URL lasts, in seconds, within the adapter's own cap. */
188
+ expiresIn?: number;
189
+ }
180
190
  /**
181
191
  * Data returned for client-side (direct) uploads.
182
192
  * Contains pre-signed URL and headers for direct-to-storage uploads.
@@ -269,8 +279,13 @@ interface IStorageAdapter {
269
279
  getMetadata?(filePath: string): Promise<FileMetadata | null>;
270
280
  /** Generate signed URL for temporary private access (optional) */
271
281
  getSignedUrl?(filePath: string, expiresIn?: number): Promise<string>;
272
- /** Generate pre-signed upload URL for client-side uploads (optional) */
273
- getPresignedUploadUrl?(key: string, mimeType: string, expiresIn?: number): Promise<ClientUploadData>;
282
+ /**
283
+ * Generate pre-signed upload URL for client-side uploads (optional). With
284
+ * `options.contentLength`, the signature binds the body's exact length.
285
+ */
286
+ getPresignedUploadUrl?(key: string, mimeType: string, expiresIn?: number, options?: {
287
+ contentLength?: number;
288
+ }): Promise<ClientUploadData>;
274
289
  /**
275
290
  * Every file the storage holds, a page at a time (optional). Used to find
276
291
  * files no record refers to; a storage without it cannot be searched for
@@ -726,12 +741,21 @@ export declare class S3StorageAdapter implements IStorageAdapter {
726
741
  * Creates a pre-signed PutObject URL that allows direct uploads from
727
742
  * the browser to S3, bypassing server-side upload limits (e.g., Vercel's 4.5MB).
728
743
  *
744
+ * The signature covers the `Content-Type` and, given `options.contentLength`,
745
+ * the `Content-Length`: the bytes never pass Nextly's upload checks, so S3
746
+ * itself refuses a body of another type or length. The browser sets the
747
+ * length from the body; it is not a header the client sends.
748
+ *
729
749
  * @param key - Storage path/key for the upload
730
750
  * @param mimeType - MIME type of the file being uploaded
731
- * @param expiresIn - URL validity duration in seconds (default: 3600)
751
+ * @param expiresIn - URL validity in seconds, at most the configured
752
+ * `signedUrlExpiresIn` (its default)
753
+ * @param options - `contentLength`: the exact size the upload may have
732
754
  * @returns Client upload data with URL, method, and headers
733
755
  */
734
- getPresignedUploadUrl(key: string, mimeType: string, expiresIn?: number): Promise<ClientUploadData>;
756
+ getPresignedUploadUrl(key: string, mimeType: string, expiresIn?: number, options?: {
757
+ contentLength?: number;
758
+ }): Promise<ClientUploadData>;
735
759
  /**
736
760
  * Generate a unique storage key with date-based prefix.
737
761
  *
package/dist/index.d.ts CHANGED
@@ -168,15 +168,25 @@ interface StoragePlugin {
168
168
  adapter: IStorageAdapter;
169
169
  /**
170
170
  * Handler for generating client-side upload URLs.
171
- * Called when clientUploads is enabled for a collection.
171
+ * Called when clientUploads is enabled for a collection, for a file whose
172
+ * name, type and size the upload checks have already allowed. The URL must
173
+ * bind the type and `options.size`, so storage refuses a different file:
174
+ * its bytes never pass Nextly's checks.
172
175
  */
173
- getClientUploadUrl?: (filename: string, mimeType: string, collection: string) => Promise<ClientUploadData>;
176
+ getClientUploadUrl?: (filename: string, mimeType: string, collection: string, options: ClientUploadOptions) => Promise<ClientUploadData>;
174
177
  /**
175
178
  * Handler for generating signed download URLs.
176
179
  * Called when signedDownloads is enabled for a collection.
177
180
  */
178
181
  getSignedDownloadUrl?: (path: string, expiresIn?: number) => Promise<string>;
179
182
  }
183
+ /** What a signed client upload is bound to, besides its key and type. */
184
+ interface ClientUploadOptions {
185
+ /** The file's exact size in bytes; storage refuses a body of another length. */
186
+ size: number;
187
+ /** How long the URL lasts, in seconds, within the adapter's own cap. */
188
+ expiresIn?: number;
189
+ }
180
190
  /**
181
191
  * Data returned for client-side (direct) uploads.
182
192
  * Contains pre-signed URL and headers for direct-to-storage uploads.
@@ -269,8 +279,13 @@ interface IStorageAdapter {
269
279
  getMetadata?(filePath: string): Promise<FileMetadata | null>;
270
280
  /** Generate signed URL for temporary private access (optional) */
271
281
  getSignedUrl?(filePath: string, expiresIn?: number): Promise<string>;
272
- /** Generate pre-signed upload URL for client-side uploads (optional) */
273
- getPresignedUploadUrl?(key: string, mimeType: string, expiresIn?: number): Promise<ClientUploadData>;
282
+ /**
283
+ * Generate pre-signed upload URL for client-side uploads (optional). With
284
+ * `options.contentLength`, the signature binds the body's exact length.
285
+ */
286
+ getPresignedUploadUrl?(key: string, mimeType: string, expiresIn?: number, options?: {
287
+ contentLength?: number;
288
+ }): Promise<ClientUploadData>;
274
289
  /**
275
290
  * Every file the storage holds, a page at a time (optional). Used to find
276
291
  * files no record refers to; a storage without it cannot be searched for
@@ -726,12 +741,21 @@ export declare class S3StorageAdapter implements IStorageAdapter {
726
741
  * Creates a pre-signed PutObject URL that allows direct uploads from
727
742
  * the browser to S3, bypassing server-side upload limits (e.g., Vercel's 4.5MB).
728
743
  *
744
+ * The signature covers the `Content-Type` and, given `options.contentLength`,
745
+ * the `Content-Length`: the bytes never pass Nextly's upload checks, so S3
746
+ * itself refuses a body of another type or length. The browser sets the
747
+ * length from the body; it is not a header the client sends.
748
+ *
729
749
  * @param key - Storage path/key for the upload
730
750
  * @param mimeType - MIME type of the file being uploaded
731
- * @param expiresIn - URL validity duration in seconds (default: 3600)
751
+ * @param expiresIn - URL validity in seconds, at most the configured
752
+ * `signedUrlExpiresIn` (its default)
753
+ * @param options - `contentLength`: the exact size the upload may have
732
754
  * @returns Client upload data with URL, method, and headers
733
755
  */
734
- getPresignedUploadUrl(key: string, mimeType: string, expiresIn?: number): Promise<ClientUploadData>;
756
+ getPresignedUploadUrl(key: string, mimeType: string, expiresIn?: number, options?: {
757
+ contentLength?: number;
758
+ }): Promise<ClientUploadData>;
735
759
  /**
736
760
  * Generate a unique storage key with date-based prefix.
737
761
  *
package/dist/index.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  import { DeleteObjectCommand, DeleteObjectsCommand, GetObjectCommand, HeadObjectCommand, ListObjectsV2Command, PutObjectCommand, S3Client } from "@aws-sdk/client-s3";
2
2
  import { Upload } from "@aws-sdk/lib-storage";
3
3
  import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
4
- // ../nextly/dist/nextly-error-BElDQWyI.mjs
4
+ // ../nextly/dist/nextly-error-C_MDZmJc.mjs
5
5
  function isDbError(err) {
6
6
  if (!err || typeof err !== "object") return false;
7
7
  const obj = err;
@@ -115,10 +115,14 @@ const NEXTLY_ERROR_STATUS = {
115
115
  NEXTLY_UPGRADE_TABLE_NAME_COLLISION: 409,
116
116
  NEXTLY_UPGRADE_IN_PROGRESS: 409,
117
117
  NEXTLY_MIGRATE_LOCK_BUSY: 409,
118
+ NEXTLY_TABLE_BUSY: 409,
119
+ NEXTLY_LOCK_HELD_BY_THIS_PROCESS: 500,
118
120
  NEXTLY_BASELINE_LOCK_NOT_HELD: 409,
119
121
  NEXTLY_RESOLVE_LOCK_NOT_HELD: 409,
120
122
  NEXTLY_BOOT_MIGRATIONS_NOT_RUN: 503,
121
123
  NEXTLY_BOOT_MIGRATIONS_PENDING: 503,
124
+ NEXTLY_DATABASE_NOT_SET_UP: 503,
125
+ NEXTLY_DATABASE_NOT_MIGRATED: 503,
122
126
  NEXTLY_CORE_DESTRUCTIVE_REFUSED: 409,
123
127
  NEXTLY_MIGRATION_DRIFT: 409,
124
128
  NEXTLY_MIGRATION_APPLY_FAILED: 500,
@@ -174,7 +178,7 @@ const NEXTLY_ERROR_STATUS = {
174
178
  NEXTLY_PER_LANGUAGE_STORAGE_CHANGED: 500,
175
179
  NEXTLY_PER_LANGUAGE_STORAGE_MIXED: 500,
176
180
  NEXTLY_PER_LANGUAGE_STORAGE_LANGUAGES_UNDECLARED: 500,
177
- NEXTLY_POSTGRES_SCHEMA_UNSUPPORTED: 500
181
+ NEXTLY_POSTGRES_SCHEMA_MOVED: 500
178
182
  };
179
183
  ({ ...Object.fromEntries([
180
184
  "VALIDATION_ERROR",
@@ -788,6 +792,19 @@ var NextlyError = class NextlyError extends Error {
788
792
  logContext: opts.logContext
789
793
  });
790
794
  }
795
+ /**
796
+ * A service outside Nextly failed, such as an email provider, where trying again may help: the
797
+ * job queue records it as an attempt's error and tries again.
798
+ */
799
+ static externalService(opts) {
800
+ return new NextlyError({
801
+ code: "EXTERNAL_SERVICE_ERROR",
802
+ publicMessage: opts.publicMessage,
803
+ logMessage: opts.logMessage,
804
+ cause: opts.cause,
805
+ logContext: opts.logContext
806
+ });
807
+ }
791
808
  static serviceUnavailable(opts) {
792
809
  return new NextlyError({
793
810
  code: "SERVICE_UNAVAILABLE",
@@ -860,7 +877,7 @@ var NextlyError = class NextlyError extends Error {
860
877
  return NextlyError.isCode(err, "RATE_LIMITED");
861
878
  }
862
879
  };
863
- // ../nextly/dist/validate-external-url-DNl_UEVd.mjs
880
+ // ../nextly/dist/validate-external-url-DF_HNT6F.mjs
864
881
  /** Default response body cap: reject anything larger before buffering it all. */
865
882
  /**
866
883
  * How many bytes a bounded external read may buffer, absent a caller's own cap.
@@ -1378,24 +1395,36 @@ var S3StorageAdapter = class {
1378
1395
  * Creates a pre-signed PutObject URL that allows direct uploads from
1379
1396
  * the browser to S3, bypassing server-side upload limits (e.g., Vercel's 4.5MB).
1380
1397
  *
1398
+ * The signature covers the `Content-Type` and, given `options.contentLength`,
1399
+ * the `Content-Length`: the bytes never pass Nextly's upload checks, so S3
1400
+ * itself refuses a body of another type or length. The browser sets the
1401
+ * length from the body; it is not a header the client sends.
1402
+ *
1381
1403
  * @param key - Storage path/key for the upload
1382
1404
  * @param mimeType - MIME type of the file being uploaded
1383
- * @param expiresIn - URL validity duration in seconds (default: 3600)
1405
+ * @param expiresIn - URL validity in seconds, at most the configured
1406
+ * `signedUrlExpiresIn` (its default)
1407
+ * @param options - `contentLength`: the exact size the upload may have
1384
1408
  * @returns Client upload data with URL, method, and headers
1385
1409
  */
1386
- async getPresignedUploadUrl(key, mimeType, expiresIn) {
1410
+ async getPresignedUploadUrl(key, mimeType, expiresIn, options) {
1387
1411
  const client = this.ensureClient();
1388
- const expiration = expiresIn ?? this.resolvedConfig.signedUrlExpiresIn;
1412
+ const longest = this.resolvedConfig.signedUrlExpiresIn;
1413
+ const expiration = Math.min(expiresIn ?? longest, longest);
1389
1414
  const commandParams = {
1390
1415
  Bucket: this.resolvedConfig.bucket,
1391
1416
  Key: key,
1392
1417
  ContentType: mimeType,
1393
- CacheControl: this.resolvedConfig.cacheControl
1418
+ CacheControl: this.resolvedConfig.cacheControl,
1419
+ ...options?.contentLength !== void 0 ? { ContentLength: options.contentLength } : {}
1394
1420
  };
1395
1421
  if (!this.isR2) commandParams.ACL = this.resolvedConfig.acl;
1396
1422
  const command = new PutObjectCommand(commandParams);
1397
1423
  return {
1398
- uploadUrl: await getSignedUrl(client, command, { expiresIn: expiration }),
1424
+ uploadUrl: await getSignedUrl(client, command, {
1425
+ expiresIn: expiration,
1426
+ signableHeaders: /* @__PURE__ */ new Set(["content-type", "content-length"])
1427
+ }),
1399
1428
  path: key,
1400
1429
  method: "PUT",
1401
1430
  headers: { "Content-Type": mimeType },
@@ -1562,12 +1591,13 @@ function s3Storage(config) {
1562
1591
  * @param filename - Original filename from the client
1563
1592
  * @param mimeType - MIME type of the file
1564
1593
  * @param collection - Collection slug this upload belongs to
1594
+ * @param options - The file's exact size, and the URL's lifetime
1565
1595
  * @returns Client upload data with pre-signed URL
1566
1596
  */
1567
- async getClientUploadUrl(filename, mimeType, collection) {
1597
+ async getClientUploadUrl(filename, mimeType, collection, options) {
1568
1598
  const collectionConfig = config.collections[collection];
1569
1599
  const key = generateStorageKey(filename, typeof collectionConfig === "object" ? collectionConfig.prefix : void 0);
1570
- return adapter.getPresignedUploadUrl(key, mimeType);
1600
+ return adapter.getPresignedUploadUrl(key, mimeType, options?.expiresIn, { contentLength: options?.size });
1571
1601
  },
1572
1602
  /**
1573
1603
  * Generate a signed URL for private file downloads.