canopycms-cdk 0.1.0 → 0.2.0-int.119
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/constructs/asset-support.d.ts +8 -2
- package/dist/constructs/asset-support.js +12 -4
- package/dist/constructs/cms-service.js +3 -1
- package/lambda/asset-transform/dist/handler.js +106 -33
- package/package.json +4 -4
- package/worker/dist/index.js +1 -1
- package/worker/dist/index.js.sha256 +1 -1
|
@@ -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-
|
|
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
|
|
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-
|
|
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.
|
|
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.
|
|
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
|
|
452
|
-
* `link()`ed into place, which fails with EEXIST rather than replacing
|
|
453
|
-
* The sidecar is linked first, so a reader never finds a blob without
|
|
454
|
-
* already there was linked by an earlier writer of this key and is kept,
|
|
455
|
-
* first writer's headers win, as on S3 (a static's Content-Disposition names
|
|
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
|
-
|
|
469
|
-
|
|
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(
|
|
617
|
-
|
|
618
|
-
|
|
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
|
|
624
|
-
|
|
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 =
|
|
627
|
-
const hasMore = startIndex + limit <
|
|
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
|
|
632
|
-
const filePath = this.resolveKey(metaKey(hash32));
|
|
651
|
+
async hiddenHashes() {
|
|
633
652
|
try {
|
|
634
|
-
await import_promises2.default.
|
|
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] ?? []));
|
|
635
655
|
} catch (err2) {
|
|
636
|
-
if (isNotFoundError(err2)) return;
|
|
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;
|
|
667
|
+
} catch (err2) {
|
|
668
|
+
if (isNotFoundError(err2)) return false;
|
|
637
669
|
throw err2;
|
|
638
670
|
}
|
|
639
671
|
}
|
|
@@ -646,6 +678,7 @@ 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
683
|
var PRESIGNED_READ_EXPIRY_SECONDS = 5 * 60;
|
|
651
684
|
function matchesAwsError(err2, name, httpStatusCode) {
|
|
@@ -851,7 +884,10 @@ var S3AssetStore = class {
|
|
|
851
884
|
throw err2;
|
|
852
885
|
}
|
|
853
886
|
}
|
|
854
|
-
|
|
887
|
+
listPublicObjectKeys(prefix) {
|
|
888
|
+
return this.listKeys(prefix);
|
|
889
|
+
}
|
|
890
|
+
async *listKeys(prefix) {
|
|
855
891
|
const pages = (0, import_client_s3.paginateListObjectsV2)(
|
|
856
892
|
{ client: this.client },
|
|
857
893
|
{ Bucket: this.bucket, Prefix: prefix }
|
|
@@ -881,21 +917,33 @@ var S3AssetStore = class {
|
|
|
881
917
|
if (!bytes) return null;
|
|
882
918
|
return JSON.parse(Buffer.from(bytes).toString("utf-8"));
|
|
883
919
|
}
|
|
920
|
+
/**
|
|
921
|
+
* One page of meta keys, filtered by a full listing of the hidden markers: few files are hidden,
|
|
922
|
+
* and a listing needs only `s3:ListBucket`. A hidden file's meta is fetched only for
|
|
923
|
+
* `includeHidden`.
|
|
924
|
+
*/
|
|
884
925
|
async listMeta(input) {
|
|
885
|
-
const
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
926
|
+
const metaPrefix2 = this.keys.metaPrefix();
|
|
927
|
+
const [listed, hidden] = await Promise.all([
|
|
928
|
+
this.client.send(
|
|
929
|
+
new import_client_s3.ListObjectsV2Command({
|
|
930
|
+
Bucket: this.bucket,
|
|
931
|
+
Prefix: metaPrefix2,
|
|
932
|
+
MaxKeys: input?.limit,
|
|
933
|
+
ContinuationToken: input?.cursor
|
|
934
|
+
})
|
|
935
|
+
),
|
|
936
|
+
this.listHiddenHashes()
|
|
937
|
+
]);
|
|
893
938
|
const objectKeys = (listed.Contents ?? []).map((obj) => obj.Key).filter((key) => Boolean(key));
|
|
894
939
|
const fetched = await Promise.all(
|
|
895
940
|
objectKeys.map(async (key) => {
|
|
941
|
+
const isHidden = hidden.has(key.slice(metaPrefix2.length, -JSON_SUFFIX.length));
|
|
942
|
+
if (isHidden && !input?.includeHidden) return null;
|
|
896
943
|
const bytes = await this.getObjectBytes(key);
|
|
897
944
|
if (!bytes) return null;
|
|
898
|
-
|
|
945
|
+
const meta = JSON.parse(Buffer.from(bytes).toString("utf-8"));
|
|
946
|
+
return isHidden ? { ...meta, hidden: true } : meta;
|
|
899
947
|
})
|
|
900
948
|
);
|
|
901
949
|
return {
|
|
@@ -903,10 +951,35 @@ var S3AssetStore = class {
|
|
|
903
951
|
nextCursor: listed.IsTruncated ? listed.NextContinuationToken : void 0
|
|
904
952
|
};
|
|
905
953
|
}
|
|
906
|
-
async
|
|
907
|
-
|
|
908
|
-
|
|
954
|
+
async listHiddenHashes() {
|
|
955
|
+
const prefix = this.keys.hiddenPrefix();
|
|
956
|
+
const hashes = /* @__PURE__ */ new Set();
|
|
957
|
+
for await (const key of this.listKeys(prefix)) {
|
|
958
|
+
if (key.endsWith(JSON_SUFFIX)) hashes.add(key.slice(prefix.length, -JSON_SUFFIX.length));
|
|
959
|
+
}
|
|
960
|
+
return hashes;
|
|
961
|
+
}
|
|
962
|
+
async hideAsset(hash32, marker) {
|
|
963
|
+
return this.putIfAbsent({
|
|
964
|
+
Key: this.keys.hiddenKey(hash32),
|
|
965
|
+
Body: JSON.stringify(marker),
|
|
966
|
+
ContentType: "application/json"
|
|
967
|
+
});
|
|
968
|
+
}
|
|
969
|
+
/**
|
|
970
|
+
* Lists before deleting: a DELETE of an absent key writes a delete marker on a versioned bucket.
|
|
971
|
+
* The listing needs only `s3:ListBucket` covering `asset-hidden/` (a bucket-wide grant does; a
|
|
972
|
+
* policy scoping it by prefix must name this one), no object permission, so finalize can call
|
|
973
|
+
* this on every re-upload under a role that has never hidden a file.
|
|
974
|
+
*/
|
|
975
|
+
async unhideAsset(hash32) {
|
|
976
|
+
const key = this.keys.hiddenKey(hash32);
|
|
977
|
+
const listed = await this.client.send(
|
|
978
|
+
new import_client_s3.ListObjectsV2Command({ Bucket: this.bucket, Prefix: key, MaxKeys: 1 })
|
|
909
979
|
);
|
|
980
|
+
if (listed.Contents?.[0]?.Key !== key) return false;
|
|
981
|
+
await this.client.send(new import_client_s3.DeleteObjectCommand({ Bucket: this.bucket, Key: key }));
|
|
982
|
+
return true;
|
|
910
983
|
}
|
|
911
984
|
async getObject(key) {
|
|
912
985
|
try {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "canopycms-cdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0-int.119",
|
|
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.
|
|
37
|
+
"canopycms": "0.2.0-int.119"
|
|
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.
|
|
64
|
-
"canopycms-auth-clerk": "0.
|
|
63
|
+
"canopycms": "0.2.0-int.119",
|
|
64
|
+
"canopycms-auth-clerk": "0.2.0-int.119"
|
|
65
65
|
},
|
|
66
66
|
"scripts": {
|
|
67
67
|
"build": "rm -rf dist && tsc && node ../../scripts/add-js-extensions.mjs dist",
|
package/worker/dist/index.js
CHANGED
|
@@ -82491,7 +82491,7 @@ init_error();
|
|
|
82491
82491
|
init_worker_status();
|
|
82492
82492
|
|
|
82493
82493
|
// ../canopycms/src/version.ts
|
|
82494
|
-
var CANOPYCMS_VERSION = "0.
|
|
82494
|
+
var CANOPYCMS_VERSION = "0.2.0-int.119";
|
|
82495
82495
|
|
|
82496
82496
|
// ../canopycms/src/worker/schema-gate.ts
|
|
82497
82497
|
import fs14 from "node:fs/promises";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
8a7b7063f59c69ec21cd138b5543e669a5905966b38b21bcb9ed7a497ad91622 index.js
|