canopycms-cdk 0.1.0 → 0.2.0-int.121

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.
@@ -489,8 +489,14 @@ export declare class AssetSupport extends Construct {
489
489
  *
490
490
  * - put on `asset-staging/` (presigned POST + proxied `writeStaging`)
491
491
  * - get on `asset-staging/` + `asset-originals/` + `asset-meta/` + `assets/`
492
- * - put on `asset-originals/` + `asset-meta/` + `assets/`
493
- * - delete on `asset-staging/` + `asset-meta/`
492
+ * - put on `asset-originals/` + `asset-meta/` + `asset-hidden/` + `assets/`
493
+ * - delete on `asset-staging/` + `asset-hidden/` (restoring a file removed from the library)
494
+ *
495
+ * Hidden markers are only listed, written and deleted, never read; the bucket-wide
496
+ * `s3:ListBucket` the read grants carry covers their listing.
497
+ *
498
+ * No delete on `asset-meta/`: meta is never deleted, since every transform not yet stored is
499
+ * computed from it.
494
500
  */
495
501
  grantUploadAccess(grantee: iam.IGrantable): void;
496
502
  }
@@ -29,7 +29,7 @@ const transformAssetDir = path.join(__dirname, '..', '..', 'lambda', 'asset-tran
29
29
  */
30
30
  const DEPLOYABLE_MARKER = '.deployable';
31
31
  /**
32
- * The four S3 key prefixes the asset system uses. Source of truth is
32
+ * The S3 key prefixes the asset system uses. Source of truth is
33
33
  * `packages/canopycms/src/assets/asset-prefixes.ts`; these are copied as
34
34
  * literals rather than imported so this construct synths in a consumer CDK app
35
35
  * that has no `canopycms` resolvable. (The transform Lambda does import them -
@@ -39,6 +39,7 @@ const PREFIXES = {
39
39
  originals: 'asset-originals',
40
40
  staging: 'asset-staging',
41
41
  meta: 'asset-meta',
42
+ hidden: 'asset-hidden',
42
43
  public: 'assets',
43
44
  transform: 'assets/t',
44
45
  };
@@ -924,8 +925,14 @@ export class AssetSupport extends Construct {
924
925
  *
925
926
  * - put on `asset-staging/` (presigned POST + proxied `writeStaging`)
926
927
  * - get on `asset-staging/` + `asset-originals/` + `asset-meta/` + `assets/`
927
- * - put on `asset-originals/` + `asset-meta/` + `assets/`
928
- * - delete on `asset-staging/` + `asset-meta/`
928
+ * - put on `asset-originals/` + `asset-meta/` + `asset-hidden/` + `assets/`
929
+ * - delete on `asset-staging/` + `asset-hidden/` (restoring a file removed from the library)
930
+ *
931
+ * Hidden markers are only listed, written and deleted, never read; the bucket-wide
932
+ * `s3:ListBucket` the read grants carry covers their listing.
933
+ *
934
+ * No delete on `asset-meta/`: meta is never deleted, since every transform not yet stored is
935
+ * computed from it.
929
936
  */
930
937
  grantUploadAccess(grantee) {
931
938
  this.bucket.grantPut(grantee, `${PREFIXES.staging}/*`);
@@ -935,8 +942,9 @@ export class AssetSupport extends Construct {
935
942
  this.bucket.grantRead(grantee, `${PREFIXES.public}/*`);
936
943
  this.bucket.grantPut(grantee, `${PREFIXES.originals}/*`);
937
944
  this.bucket.grantPut(grantee, `${PREFIXES.meta}/*`);
945
+ this.bucket.grantPut(grantee, `${PREFIXES.hidden}/*`);
938
946
  this.bucket.grantPut(grantee, `${PREFIXES.public}/*`);
939
947
  this.bucket.grantDelete(grantee, `${PREFIXES.staging}/*`);
940
- this.bucket.grantDelete(grantee, `${PREFIXES.meta}/*`);
948
+ this.bucket.grantDelete(grantee, `${PREFIXES.hidden}/*`);
941
949
  }
942
950
  }
@@ -963,6 +963,7 @@ export class CanopyCmsService extends Construct {
963
963
  staging: 'asset-staging',
964
964
  originals: 'asset-originals',
965
965
  meta: 'asset-meta',
966
+ hidden: 'asset-hidden',
966
967
  public: 'assets',
967
968
  };
968
969
  props.assetBucket.grantPut(this.lambdaFunction, `${prefixes.staging}/*`);
@@ -972,9 +973,10 @@ export class CanopyCmsService extends Construct {
972
973
  props.assetBucket.grantRead(this.lambdaFunction, `${prefixes.public}/*`);
973
974
  props.assetBucket.grantPut(this.lambdaFunction, `${prefixes.originals}/*`);
974
975
  props.assetBucket.grantPut(this.lambdaFunction, `${prefixes.meta}/*`);
976
+ props.assetBucket.grantPut(this.lambdaFunction, `${prefixes.hidden}/*`);
975
977
  props.assetBucket.grantPut(this.lambdaFunction, `${prefixes.public}/*`);
976
978
  props.assetBucket.grantDelete(this.lambdaFunction, `${prefixes.staging}/*`);
977
- props.assetBucket.grantDelete(this.lambdaFunction, `${prefixes.meta}/*`);
979
+ props.assetBucket.grantDelete(this.lambdaFunction, `${prefixes.hidden}/*`);
978
980
  }
979
981
  const workerSg = new ec2.SecurityGroup(this, 'WorkerSg', {
980
982
  vpc: this.vpc,
@@ -354,6 +354,8 @@ var ASSET_PREFIXES = {
354
354
  originals: "asset-originals",
355
355
  staging: "asset-staging",
356
356
  meta: "asset-meta",
357
+ /** One marker per file removed from the media library; its meta stays (see `hideAsset`). */
358
+ hidden: "asset-hidden",
357
359
  public: "assets",
358
360
  /** Transform-output prefix; directive-aware key building lives in transform-directives.ts. */
359
361
  transform: "assets/t"
@@ -368,11 +370,13 @@ function createKeyBuilders(prefixes = ASSET_PREFIXES) {
368
370
  stagingKey: (uuid) => `${prefixes.staging}/${uuid}`,
369
371
  metaKey: (hash32) => `${prefixes.meta}/${hash32}.json`,
370
372
  metaPrefix: () => `${prefixes.meta}/`,
373
+ hiddenKey: (hash32) => `${prefixes.hidden}/${hash32}.json`,
374
+ hiddenPrefix: () => `${prefixes.hidden}/`,
371
375
  publicKey: (hash32, slug, ext) => `${prefixes.public}/${hash32}/${slug}.${ext}`
372
376
  };
373
377
  }
374
378
  var keyBuilders = createKeyBuilders();
375
- var { originalKey, stagingKey, metaKey, publicKey } = keyBuilders;
379
+ var { originalKey, stagingKey, metaKey, hiddenKey, publicKey } = keyBuilders;
376
380
  var { originalPrefix, metaPrefix } = keyBuilders;
377
381
 
378
382
  // ../canopycms/src/assets/store-local.ts
@@ -381,6 +385,12 @@ var DEFAULT_LIST_LIMIT = 24;
381
385
  var HEADERS_SIDECAR_SUFFIX = ".headers.json";
382
386
  var TEMP_DIR = ".asset-tmp";
383
387
  var ORIGINAL_EXT_RE = /^[a-z0-9]+$/;
388
+ var HIDDEN_MARKER_RE = /^([a-f0-9]{32})\.json$/;
389
+ function compareNewestFirst(a, b) {
390
+ if (a.uploadedAt !== b.uploadedAt) return a.uploadedAt < b.uploadedAt ? 1 : -1;
391
+ if (a.hash32 === b.hash32) return 0;
392
+ return a.hash32 < b.hash32 ? 1 : -1;
393
+ }
384
394
  var LocalAssetStore = class {
385
395
  constructor(options) {
386
396
  this.capabilities = { directUpload: false };
@@ -448,11 +458,12 @@ var LocalAssetStore = class {
448
458
  }
449
459
  }
450
460
  /**
451
- * Create-only write of a blob and its headers sidecar. Each is written whole to a temp file, then
452
- * `link()`ed into place, which fails with EEXIST rather than replacing (see docs/concurrency.md).
453
- * The sidecar is linked first, so a reader never finds a blob without its headers. A sidecar
454
- * already there was linked by an earlier writer of this key and is kept, as the blob is: the
455
- * first writer's headers win, as on S3 (a static's Content-Disposition names its uploaded file).
461
+ * Create-only write of a blob and, when `headers` is given, its headers sidecar. Each is written
462
+ * whole to a temp file, then `link()`ed into place, which fails with EEXIST rather than replacing
463
+ * (see docs/concurrency.md). The sidecar is linked first, so a reader never finds a blob without
464
+ * its headers. A sidecar already there was linked by an earlier writer of this key and is kept,
465
+ * as the blob is: the first writer's headers win, as on S3 (a static's Content-Disposition names
466
+ * its uploaded file).
456
467
  */
457
468
  async createExclusive(key, data, headers) {
458
469
  const filePath = this.resolveKey(key);
@@ -465,8 +476,10 @@ var LocalAssetStore = class {
465
476
  const tempHeaders = import_node_path2.default.join(tempDir, (0, import_node_crypto.randomUUID)());
466
477
  try {
467
478
  await import_promises2.default.writeFile(tempBlob, data);
468
- await import_promises2.default.writeFile(tempHeaders, JSON.stringify(headers), "utf-8");
469
- await this.linkIfAbsent(tempHeaders, this.headersSidecarPath(filePath));
479
+ if (headers) {
480
+ await import_promises2.default.writeFile(tempHeaders, JSON.stringify(headers), "utf-8");
481
+ await this.linkIfAbsent(tempHeaders, this.headersSidecarPath(filePath));
482
+ }
470
483
  return await this.linkIfAbsent(tempBlob, filePath) ? "created" : "already-exists";
471
484
  } finally {
472
485
  await Promise.all([tempBlob, tempHeaders].map((temp) => import_promises2.default.unlink(temp).catch(() => {
@@ -592,6 +605,11 @@ var LocalAssetStore = class {
592
605
  throw err2;
593
606
  }
594
607
  }
608
+ /**
609
+ * The cursor names the last item returned, and the next page starts after it in sort order, so a
610
+ * page boundary holds when that item is hidden in between: its meta is never deleted, and the
611
+ * comparison needs only its place, not its presence in the filtered list.
612
+ */
595
613
  async listMeta(input) {
596
614
  const limit = input?.limit ?? DEFAULT_LIST_LIMIT;
597
615
  const dir = this.resolveKey(ASSET_PREFIXES.meta);
@@ -613,27 +631,41 @@ var LocalAssetStore = class {
613
631
  }
614
632
  }
615
633
  }
616
- all.sort((a, b) => {
617
- if (a.uploadedAt !== b.uploadedAt) return a.uploadedAt < b.uploadedAt ? 1 : -1;
618
- return a.hash32 < b.hash32 ? 1 : -1;
619
- });
634
+ all.sort(compareNewestFirst);
635
+ const hidden = await this.hiddenHashes();
636
+ const listed = input?.includeHidden ? all.map((meta) => hidden.has(meta.hash32) ? { ...meta, hidden: true } : meta) : all.filter((meta) => !hidden.has(meta.hash32));
620
637
  let startIndex = 0;
621
638
  if (input?.cursor) {
622
639
  const afterHash32 = Buffer.from(input.cursor, "base64").toString("utf-8");
623
- const idx = all.findIndex((m) => m.hash32 === afterHash32);
624
- startIndex = idx === -1 ? 0 : idx + 1;
640
+ const anchor = all.find((m) => m.hash32 === afterHash32);
641
+ if (anchor) {
642
+ const after = listed.findIndex((m) => compareNewestFirst(m, anchor) > 0);
643
+ startIndex = after === -1 ? listed.length : after;
644
+ }
625
645
  }
626
- const items = all.slice(startIndex, startIndex + limit);
627
- const hasMore = startIndex + limit < all.length;
646
+ const items = listed.slice(startIndex, startIndex + limit);
647
+ const hasMore = startIndex + limit < listed.length;
628
648
  const nextCursor = hasMore ? Buffer.from(items[items.length - 1].hash32, "utf-8").toString("base64") : void 0;
629
649
  return { items, nextCursor };
630
650
  }
631
- async deleteMeta(hash32) {
632
- const filePath = this.resolveKey(metaKey(hash32));
651
+ async hiddenHashes() {
633
652
  try {
634
- await import_promises2.default.unlink(filePath);
653
+ const names = await import_promises2.default.readdir(this.resolveKey(ASSET_PREFIXES.hidden));
654
+ return new Set(names.flatMap((name) => HIDDEN_MARKER_RE.exec(name)?.[1] ?? []));
655
+ } catch (err2) {
656
+ if (isNotFoundError(err2)) return /* @__PURE__ */ new Set();
657
+ throw err2;
658
+ }
659
+ }
660
+ async hideAsset(hash32, marker) {
661
+ return this.createExclusive(hiddenKey(hash32), new TextEncoder().encode(JSON.stringify(marker)));
662
+ }
663
+ async unhideAsset(hash32) {
664
+ try {
665
+ await import_promises2.default.unlink(this.resolveKey(hiddenKey(hash32)));
666
+ return true;
635
667
  } catch (err2) {
636
- if (isNotFoundError(err2)) return;
668
+ if (isNotFoundError(err2)) return false;
637
669
  throw err2;
638
670
  }
639
671
  }
@@ -646,8 +678,11 @@ var import_s3_presigned_post = require("@aws-sdk/s3-presigned-post");
646
678
  var import_s3_request_presigner = require("@aws-sdk/s3-request-presigner");
647
679
  init_sanitize_href();
648
680
  var DEFAULT_MAX_UPLOAD_BYTES2 = 50 * 1024 * 1024;
681
+ var JSON_SUFFIX = ".json";
649
682
  var PRESIGN_EXPIRY_SECONDS = 15 * 60;
650
- var PRESIGNED_READ_EXPIRY_SECONDS = 5 * 60;
683
+ var PRESIGNED_READ_WINDOW_SECONDS = 60 * 60;
684
+ var PRESIGNED_READ_EXPIRY_SECONDS = 2 * PRESIGNED_READ_WINDOW_SECONDS;
685
+ var CREDENTIAL_EXPIRY_MARGIN_SECONDS = 60;
651
686
  function matchesAwsError(err2, name, httpStatusCode) {
652
687
  if (!(err2 instanceof Error)) return false;
653
688
  const shaped = err2;
@@ -851,7 +886,10 @@ var S3AssetStore = class {
851
886
  throw err2;
852
887
  }
853
888
  }
854
- async *listPublicObjectKeys(prefix) {
889
+ listPublicObjectKeys(prefix) {
890
+ return this.listKeys(prefix);
891
+ }
892
+ async *listKeys(prefix) {
855
893
  const pages = (0, import_client_s3.paginateListObjectsV2)(
856
894
  { client: this.client },
857
895
  { Bucket: this.bucket, Prefix: prefix }
@@ -862,12 +900,27 @@ var S3AssetStore = class {
862
900
  }
863
901
  }
864
902
  }
865
- /** HEADs first: signing is local and succeeds for a missing key too. */
903
+ /**
904
+ * HEADs first: signing is local and succeeds for a missing key too. A URL signed with temporary
905
+ * credentials dies when they expire, whatever its own expiry, so `maxAgeSeconds` also stops short
906
+ * of the credentials' `expiration` when the provider reports one.
907
+ */
866
908
  async presignPublicObjectRead(key) {
867
909
  if (!await this.hasPublicObject(key)) return null;
868
- return (0, import_s3_request_presigner.getSignedUrl)(this.client, new import_client_s3.GetObjectCommand({ Bucket: this.bucket, Key: key }), {
869
- expiresIn: PRESIGNED_READ_EXPIRY_SECONDS
870
- });
910
+ const { expiration } = await this.client.config.credentials();
911
+ const nowSeconds = Math.floor(Date.now() / 1e3);
912
+ const windowStart = nowSeconds - nowSeconds % PRESIGNED_READ_WINDOW_SECONDS;
913
+ const url = await (0, import_s3_request_presigner.getSignedUrl)(
914
+ this.client,
915
+ new import_client_s3.GetObjectCommand({ Bucket: this.bucket, Key: key }),
916
+ { expiresIn: PRESIGNED_READ_EXPIRY_SECONDS, signingDate: new Date(windowStart * 1e3) }
917
+ );
918
+ let maxAgeSeconds = windowStart + PRESIGNED_READ_WINDOW_SECONDS - nowSeconds;
919
+ if (expiration) {
920
+ const credentialSeconds = Math.floor(expiration.getTime() / 1e3) - nowSeconds;
921
+ maxAgeSeconds = Math.min(maxAgeSeconds, credentialSeconds - CREDENTIAL_EXPIRY_MARGIN_SECONDS);
922
+ }
923
+ return { url, maxAgeSeconds: Math.max(0, maxAgeSeconds) };
871
924
  }
872
925
  async putMetaIfAbsent(hash32, meta) {
873
926
  return this.putIfAbsent({
@@ -881,21 +934,33 @@ var S3AssetStore = class {
881
934
  if (!bytes) return null;
882
935
  return JSON.parse(Buffer.from(bytes).toString("utf-8"));
883
936
  }
937
+ /**
938
+ * One page of meta keys, filtered by a full listing of the hidden markers: few files are hidden,
939
+ * and a listing needs only `s3:ListBucket`. A hidden file's meta is fetched only for
940
+ * `includeHidden`.
941
+ */
884
942
  async listMeta(input) {
885
- const listed = await this.client.send(
886
- new import_client_s3.ListObjectsV2Command({
887
- Bucket: this.bucket,
888
- Prefix: this.keys.metaPrefix(),
889
- MaxKeys: input?.limit,
890
- ContinuationToken: input?.cursor
891
- })
892
- );
943
+ const metaPrefix2 = this.keys.metaPrefix();
944
+ const [listed, hidden] = await Promise.all([
945
+ this.client.send(
946
+ new import_client_s3.ListObjectsV2Command({
947
+ Bucket: this.bucket,
948
+ Prefix: metaPrefix2,
949
+ MaxKeys: input?.limit,
950
+ ContinuationToken: input?.cursor
951
+ })
952
+ ),
953
+ this.listHiddenHashes()
954
+ ]);
893
955
  const objectKeys = (listed.Contents ?? []).map((obj) => obj.Key).filter((key) => Boolean(key));
894
956
  const fetched = await Promise.all(
895
957
  objectKeys.map(async (key) => {
958
+ const isHidden = hidden.has(key.slice(metaPrefix2.length, -JSON_SUFFIX.length));
959
+ if (isHidden && !input?.includeHidden) return null;
896
960
  const bytes = await this.getObjectBytes(key);
897
961
  if (!bytes) return null;
898
- return JSON.parse(Buffer.from(bytes).toString("utf-8"));
962
+ const meta = JSON.parse(Buffer.from(bytes).toString("utf-8"));
963
+ return isHidden ? { ...meta, hidden: true } : meta;
899
964
  })
900
965
  );
901
966
  return {
@@ -903,10 +968,35 @@ var S3AssetStore = class {
903
968
  nextCursor: listed.IsTruncated ? listed.NextContinuationToken : void 0
904
969
  };
905
970
  }
906
- async deleteMeta(hash32) {
907
- await this.client.send(
908
- new import_client_s3.DeleteObjectCommand({ Bucket: this.bucket, Key: this.keys.metaKey(hash32) })
971
+ async listHiddenHashes() {
972
+ const prefix = this.keys.hiddenPrefix();
973
+ const hashes = /* @__PURE__ */ new Set();
974
+ for await (const key of this.listKeys(prefix)) {
975
+ if (key.endsWith(JSON_SUFFIX)) hashes.add(key.slice(prefix.length, -JSON_SUFFIX.length));
976
+ }
977
+ return hashes;
978
+ }
979
+ async hideAsset(hash32, marker) {
980
+ return this.putIfAbsent({
981
+ Key: this.keys.hiddenKey(hash32),
982
+ Body: JSON.stringify(marker),
983
+ ContentType: "application/json"
984
+ });
985
+ }
986
+ /**
987
+ * Lists before deleting: a DELETE of an absent key writes a delete marker on a versioned bucket.
988
+ * The listing needs only `s3:ListBucket` covering `asset-hidden/` (a bucket-wide grant does; a
989
+ * policy scoping it by prefix must name this one), no object permission, so finalize can call
990
+ * this on every re-upload under a role that has never hidden a file.
991
+ */
992
+ async unhideAsset(hash32) {
993
+ const key = this.keys.hiddenKey(hash32);
994
+ const listed = await this.client.send(
995
+ new import_client_s3.ListObjectsV2Command({ Bucket: this.bucket, Prefix: key, MaxKeys: 1 })
909
996
  );
997
+ if (listed.Contents?.[0]?.Key !== key) return false;
998
+ await this.client.send(new import_client_s3.DeleteObjectCommand({ Bucket: this.bucket, Key: key }));
999
+ return true;
910
1000
  }
911
1001
  async getObject(key) {
912
1002
  try {
@@ -1089,7 +1179,11 @@ async function applyTransform(input, directives) {
1089
1179
  var TRANSFORM_CACHE_CONTROL = "public, max-age=31536000, immutable";
1090
1180
  var LAZY_TRANSFORM_TAG = { key: "canopy-transform", value: "lazy" };
1091
1181
  async function storeTransform(store2, parsed, key, options = {}) {
1092
- const meta = await store2.getMeta(parsed.hash32);
1182
+ const { source } = options;
1183
+ if (source && source.meta.hash32 !== parsed.hash32) {
1184
+ throw new Error(`source is asset ${source.meta.hash32}, not ${parsed.hash32}`);
1185
+ }
1186
+ const meta = source?.meta ?? await store2.getMeta(parsed.hash32);
1093
1187
  if (!meta) {
1094
1188
  return { ok: false, status: 404, error: `Asset ${parsed.hash32} has no meta in the store` };
1095
1189
  }
@@ -1107,7 +1201,7 @@ async function storeTransform(store2, parsed, key, options = {}) {
1107
1201
  if (requestedFormat === void 0 && parsed.ext !== meta.ext) {
1108
1202
  return { ok: false, status: 400, error: "Extension does not match the source format" };
1109
1203
  }
1110
- const original = await store2.readOriginal(parsed.hash32, meta.ext);
1204
+ const original = source ? { data: source.data, ext: meta.ext } : await store2.readOriginal(parsed.hash32, meta.ext);
1111
1205
  if (!original) {
1112
1206
  return { ok: false, status: 404, error: `Asset ${parsed.hash32} has no original in the store` };
1113
1207
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "canopycms-cdk",
3
- "version": "0.1.0",
3
+ "version": "0.2.0-int.121",
4
4
  "description": "AWS CDK constructs and EC2 worker for CanopyCMS deployment",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -34,7 +34,7 @@
34
34
  "peerDependencies": {
35
35
  "aws-cdk-lib": "^2.260.0",
36
36
  "constructs": "^10.0.0",
37
- "canopycms": "0.1.0"
37
+ "canopycms": "0.2.0-int.121"
38
38
  },
39
39
  "peerDependenciesMeta": {
40
40
  "aws-cdk-lib": {
@@ -60,8 +60,8 @@
60
60
  "tsx": "^4.19.0",
61
61
  "typescript": "^5.6.3",
62
62
  "vitest": "^4.1.0",
63
- "canopycms": "0.1.0",
64
- "canopycms-auth-clerk": "0.1.0"
63
+ "canopycms-auth-clerk": "0.2.0-int.121",
64
+ "canopycms": "0.2.0-int.121"
65
65
  },
66
66
  "scripts": {
67
67
  "build": "rm -rf dist && tsc && node ../../scripts/add-js-extensions.mjs dist",
@@ -82491,7 +82491,7 @@ init_error();
82491
82491
  init_worker_status();
82492
82492
 
82493
82493
  // ../canopycms/src/version.ts
82494
- var CANOPYCMS_VERSION = "0.1.0";
82494
+ var CANOPYCMS_VERSION = "0.2.0-int.121";
82495
82495
 
82496
82496
  // ../canopycms/src/worker/schema-gate.ts
82497
82497
  import fs14 from "node:fs/promises";
@@ -1 +1 @@
1
- fbba7d902caff4a42500d3e4072acb67a7e4e070cb83115e94f6993d6b90f0b6 index.js
1
+ b819cdeaa615cddf220a82198cb6e069d33073ddae11b0a764e49e46cc986696 index.js