@nextlyhq/storage-s3 0.0.2-alpha.80 → 0.0.2-alpha.81

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-jIKkC0DY.mjs
5
+ // ../nextly/dist/nextly-error-D8GUAFzw.mjs
6
6
  function isDbError(err) {
7
7
  if (!err || typeof err !== "object") return false;
8
8
  const obj = err;
@@ -86,6 +86,7 @@ const NEXTLY_ERROR_STATUS = {
86
86
  RANGE_NOT_SATISFIABLE: 416,
87
87
  CONFLICT: 409,
88
88
  STALE_VERSION: 409,
89
+ COLLECTION_BEING_CHANGED: 409,
89
90
  VERSION_REQUIRED: 428,
90
91
  DUPLICATE: 409,
91
92
  RATE_LIMITED: 429,
@@ -917,7 +918,7 @@ var NextlyError = class NextlyError extends Error {
917
918
  return NextlyError.isCode(err, "RATE_LIMITED");
918
919
  }
919
920
  };
920
- // ../nextly/dist/validate-external-url-ZJNqaKd2.mjs
921
+ // ../nextly/dist/validate-external-url-Dpn-k4nU.mjs
921
922
  /** Default response body cap: reject anything larger before buffering it all. */
922
923
  /**
923
924
  * How many bytes a bounded external read may buffer, absent a caller's own cap.
@@ -1118,6 +1119,12 @@ function generateStorageKey(filename, folder) {
1118
1119
  * ```
1119
1120
  */
1120
1121
  /**
1122
+ * The Cache-Control a private object is stored and answered with: no cache,
1123
+ * shared or the browser's own, may keep a copy, so every later request has to
1124
+ * come back through Nextly's checked route.
1125
+ */
1126
+ const PRIVATE_CACHE_CONTROL = "private, no-store";
1127
+ /**
1121
1128
  * S3 Storage Adapter
1122
1129
  *
1123
1130
  * Implements the IStorageAdapter interface for AWS S3 and S3-compatible services.
@@ -1250,12 +1257,13 @@ var S3StorageAdapter = class {
1250
1257
  uploadParams.ContentDisposition = disposition === "attachment" ? `attachment; filename="${filename}"` : "inline";
1251
1258
  }
1252
1259
  uploadParams.Metadata = { "original-filename": options.filename };
1260
+ const url = this.getPublicUrl(key);
1253
1261
  await new _aws_sdk_lib_storage.Upload({
1254
1262
  client,
1255
1263
  params: uploadParams
1256
1264
  }).done();
1257
1265
  return {
1258
- url: this.getPublicUrl(key),
1266
+ url,
1259
1267
  path: key
1260
1268
  };
1261
1269
  }
@@ -1279,13 +1287,74 @@ var S3StorageAdapter = class {
1279
1287
  CacheControl: this.resolvedConfig.cacheControl
1280
1288
  };
1281
1289
  if (!this.isR2) params.ACL = this.resolvedConfig.acl;
1290
+ const url = this.getPublicUrl(filePath);
1282
1291
  await client.send(new _aws_sdk_client_s3.PutObjectCommand(params));
1283
1292
  return {
1284
- url: this.getPublicUrl(filePath),
1293
+ url,
1285
1294
  path: filePath
1286
1295
  };
1287
1296
  }
1288
1297
  /**
1298
+ * Store a private file at exactly `key`, the key core made for it.
1299
+ *
1300
+ * Unlike {@link upload}: no public address is formed (so an R2 bucket needs
1301
+ * no `publicUrl`), the original file name goes into neither the key nor the
1302
+ * object's metadata, the object is stored never to be cached, and its ACL is
1303
+ * `private` whatever the adapter's own `acl` says (R2 has no ACLs, and keeps
1304
+ * a bucket private unless public access is turned on for it).
1305
+ *
1306
+ * @experimental
1307
+ */
1308
+ async putPrivate(key, buffer, options) {
1309
+ const client = this.ensureClient();
1310
+ const params = {
1311
+ Bucket: this.resolvedConfig.bucket,
1312
+ Key: key,
1313
+ Body: buffer,
1314
+ ContentType: options.mimeType,
1315
+ CacheControl: PRIVATE_CACHE_CONTROL
1316
+ };
1317
+ if (!this.isR2) params.ACL = "private";
1318
+ await new _aws_sdk_lib_storage.Upload({
1319
+ client,
1320
+ params
1321
+ }).done();
1322
+ }
1323
+ /**
1324
+ * A pre-signed URL a browser sends one private file to, at exactly `key`.
1325
+ * Binds the type and exact length as {@link getPresignedUploadUrl} does,
1326
+ * and writes the object private and never cached.
1327
+ *
1328
+ * @experimental
1329
+ */
1330
+ async getPrivateUploadUrl(key, mimeType, options) {
1331
+ return this.presignPut(key, mimeType, {
1332
+ cacheControl: PRIVATE_CACHE_CONTROL,
1333
+ acl: "private",
1334
+ expiresIn: options.expiresIn,
1335
+ contentLength: options.contentLength
1336
+ });
1337
+ }
1338
+ /**
1339
+ * A link to a private file that lasts `options.expiresIn` seconds (at most
1340
+ * the configured `signedUrlExpiresIn`). The bucket answers it with the
1341
+ * given Content-Disposition and type, and with a Cache-Control that lets no
1342
+ * cache keep it, whatever the object itself was stored with.
1343
+ *
1344
+ * @experimental
1345
+ */
1346
+ async getPrivateDownloadUrl(key, options) {
1347
+ const client = this.ensureClient();
1348
+ const command = new _aws_sdk_client_s3.GetObjectCommand({
1349
+ Bucket: this.resolvedConfig.bucket,
1350
+ Key: key,
1351
+ ResponseContentDisposition: options.contentDisposition,
1352
+ ResponseCacheControl: PRIVATE_CACHE_CONTROL,
1353
+ ...options.contentType !== void 0 ? { ResponseContentType: options.contentType } : {}
1354
+ });
1355
+ return (0, _aws_sdk_s3_request_presigner.getSignedUrl)(client, command, { expiresIn: Math.min(options.expiresIn, this.resolvedConfig.signedUrlExpiresIn) });
1356
+ }
1357
+ /**
1289
1358
  * Delete file from S3.
1290
1359
  *
1291
1360
  * @param filePath - Storage path/key to delete
@@ -1395,7 +1464,7 @@ var S3StorageAdapter = class {
1395
1464
  getPublicUrl(filePath) {
1396
1465
  this.ensureConfigured();
1397
1466
  if (this.resolvedConfig.publicUrl) return `${this.resolvedConfig.publicUrl.replace(/\/$/, "")}/${filePath}`;
1398
- 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.");
1467
+ 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. A collection of private files needs none: its files have no public address.");
1399
1468
  return `https://${this.resolvedConfig.bucket}.s3.${this.resolvedConfig.region}.amazonaws.com/${filePath}`;
1400
1469
  }
1401
1470
  /**
@@ -1417,7 +1486,8 @@ var S3StorageAdapter = class {
1417
1486
  type: "s3",
1418
1487
  name: "S3StorageAdapter",
1419
1488
  supportsSignedUrls: true,
1420
- supportsClientUploads: true
1489
+ supportsClientUploads: true,
1490
+ supportsPrivateFiles: !this.resolvedConfig.publicUrl
1421
1491
  };
1422
1492
  }
1423
1493
  /**
@@ -1495,6 +1565,61 @@ var S3StorageAdapter = class {
1495
1565
  }
1496
1566
  }
1497
1567
  /**
1568
+ * The stored object's size and type, without reading it, or `null` when the
1569
+ * key is not in the bucket. A missing bucket throws rather than reading as a
1570
+ * missing key, as for {@link read}.
1571
+ *
1572
+ * @experimental
1573
+ */
1574
+ async head(filePath) {
1575
+ const client = this.ensureClient();
1576
+ try {
1577
+ const response = await client.send(new _aws_sdk_client_s3.HeadObjectCommand({
1578
+ Bucket: this.resolvedConfig.bucket,
1579
+ Key: filePath
1580
+ }));
1581
+ if (response.ContentLength === void 0) throw new Error("@nextly/storage-s3: the bucket did not report the object's size");
1582
+ return {
1583
+ size: response.ContentLength,
1584
+ ...response.ContentType !== void 0 ? { contentType: response.ContentType } : {},
1585
+ ...response.LastModified !== void 0 ? { lastModified: response.LastModified } : {}
1586
+ };
1587
+ } catch (error) {
1588
+ if (error instanceof Error && error.name === "NoSuchBucket") throw error;
1589
+ if (this.isNotFoundError(error)) return null;
1590
+ throw error;
1591
+ }
1592
+ }
1593
+ /**
1594
+ * The stored object as a stream, or `null` when the key is not in the
1595
+ * bucket, for a response that should not buffer a whole file.
1596
+ *
1597
+ * The deadline covers the bucket's answer only, then is cleared: the body
1598
+ * flows as fast as its reader takes it, and a deadline on the whole
1599
+ * transfer would cut off a slow but healthy download.
1600
+ *
1601
+ * @experimental
1602
+ */
1603
+ async readStream(filePath, options) {
1604
+ const client = this.ensureClient();
1605
+ const answered = new AbortController();
1606
+ const timer = setTimeout(() => answered.abort(), resolveReadBounds(options).timeoutMs);
1607
+ try {
1608
+ const response = await client.send(new _aws_sdk_client_s3.GetObjectCommand({
1609
+ Bucket: this.resolvedConfig.bucket,
1610
+ Key: filePath
1611
+ }), { abortSignal: answered.signal });
1612
+ if (response.Body === void 0) return new ReadableStream({ start: (controller) => controller.close() });
1613
+ return response.Body.transformToWebStream();
1614
+ } catch (error) {
1615
+ if (error instanceof Error && error.name === "NoSuchBucket") throw error;
1616
+ if (this.isNotFoundError(error)) return null;
1617
+ throw error;
1618
+ } finally {
1619
+ clearTimeout(timer);
1620
+ }
1621
+ }
1622
+ /**
1498
1623
  * Generate signed URL for temporary private file access.
1499
1624
  *
1500
1625
  * Creates a pre-signed GetObject URL that grants temporary read access
@@ -1537,17 +1662,31 @@ var S3StorageAdapter = class {
1537
1662
  * @returns Client upload data with URL, method, and headers
1538
1663
  */
1539
1664
  async getPresignedUploadUrl(key, mimeType, expiresIn, options) {
1665
+ return this.presignPut(key, mimeType, {
1666
+ cacheControl: this.resolvedConfig.cacheControl,
1667
+ acl: this.resolvedConfig.acl,
1668
+ expiresIn,
1669
+ contentLength: options?.contentLength
1670
+ });
1671
+ }
1672
+ /**
1673
+ * Sign one direct upload at exactly `key`: the type, the length when given,
1674
+ * and the Cache-Control are signed, so S3 refuses a body that differs. The
1675
+ * one signer for public and private uploads alike, which differ only in the
1676
+ * caching and access they write.
1677
+ */
1678
+ async presignPut(key, mimeType, put) {
1540
1679
  const client = this.ensureClient();
1541
1680
  const longest = this.resolvedConfig.signedUrlExpiresIn;
1542
- const expiration = Math.min(expiresIn ?? longest, longest);
1681
+ const expiration = Math.min(put.expiresIn ?? longest, longest);
1543
1682
  const commandParams = {
1544
1683
  Bucket: this.resolvedConfig.bucket,
1545
1684
  Key: key,
1546
1685
  ContentType: mimeType,
1547
- CacheControl: this.resolvedConfig.cacheControl,
1548
- ...options?.contentLength !== void 0 ? { ContentLength: options.contentLength } : {}
1686
+ CacheControl: put.cacheControl,
1687
+ ...put.contentLength !== void 0 ? { ContentLength: put.contentLength } : {}
1549
1688
  };
1550
- if (!this.isR2) commandParams.ACL = this.resolvedConfig.acl;
1689
+ if (!this.isR2) commandParams.ACL = put.acl;
1551
1690
  const command = new _aws_sdk_client_s3.PutObjectCommand(commandParams);
1552
1691
  return {
1553
1692
  uploadUrl: await (0, _aws_sdk_s3_request_presigner.getSignedUrl)(client, command, {
@@ -1562,7 +1701,7 @@ var S3StorageAdapter = class {
1562
1701
  method: "PUT",
1563
1702
  headers: {
1564
1703
  "Content-Type": mimeType,
1565
- "Cache-Control": this.resolvedConfig.cacheControl
1704
+ "Cache-Control": put.cacheControl
1566
1705
  },
1567
1706
  expiresAt: new Date(Date.now() + expiration * 1e3)
1568
1707
  };