Skip to content

Commit a0733a6

Browse files
committed
feat(cloudflare): rename Bucket.name to Bucket.bucketName and constraint name length
1 parent 273cb26 commit a0733a6

File tree

5 files changed

+38
-29
lines changed

5 files changed

+38
-29
lines changed

alchemy-effect/src/cloudflare/r2/bucket.binding.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export const bindFromWorker = () =>
1818
{
1919
type: "r2_bucket",
2020
name: source.id,
21-
bucket_name: source.attr.name,
21+
bucket_name: source.attr.bucketName,
2222
jurisdiction:
2323
source.attr.jurisdiction === "default"
2424
? undefined

alchemy-effect/src/cloudflare/r2/bucket.provider.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,20 @@ export const bucketProvider = () =>
1111
const api = yield* CloudflareApi;
1212
const accountId = yield* Account;
1313

14-
const createName = (id: string, name: string | undefined) =>
14+
const createBucketName = (id: string, name: string | undefined) =>
1515
Effect.gen(function* () {
1616
if (name) return name;
17-
return (yield* createPhysicalName({ id })).toLowerCase();
17+
return (yield* createPhysicalName({
18+
id,
19+
maxLength: 63,
20+
})).toLowerCase();
1821
});
1922

2023
const mapResult = <Props extends BucketProps>(
2124
bucket: R2.Bucket,
2225
): BucketAttr<Props> =>
2326
({
24-
name: bucket.name,
27+
bucketName: bucket.name,
2528
storageClass: bucket.storage_class ?? "Standard",
2629
jurisdiction: bucket.jurisdiction ?? "default",
2730
location: bucket.location,
@@ -30,10 +33,10 @@ export const bucketProvider = () =>
3033

3134
return {
3235
diff: Effect.fn(function* ({ id, olds, news, output }) {
33-
const name = yield* createName(id, news.name);
36+
const name = yield* createBucketName(id, news.name);
3437
if (
3538
output.accountId !== accountId ||
36-
output.name !== name ||
39+
output.bucketName !== name ||
3740
output.jurisdiction !== (news.jurisdiction ?? "default") ||
3841
olds.locationHint !== news.locationHint
3942
) {
@@ -42,13 +45,12 @@ export const bucketProvider = () =>
4245
if (output.storageClass !== (news.storageClass ?? "Standard")) {
4346
return {
4447
action: "update",
45-
stables: output.name === name ? ["name"] : undefined,
48+
stables: output.bucketName === name ? ["name"] : undefined,
4649
};
4750
}
48-
return { action: "noop" };
4951
}),
5052
create: Effect.fnUntraced(function* ({ id, news }) {
51-
const name = yield* createName(id, news.name);
53+
const name = yield* createBucketName(id, news.name);
5254
const bucket = yield* api.r2.buckets.create({
5355
account_id: accountId,
5456
name,
@@ -59,7 +61,7 @@ export const bucketProvider = () =>
5961
return mapResult<BucketProps>(bucket);
6062
}),
6163
update: Effect.fnUntraced(function* ({ news, output }) {
62-
const bucket = yield* api.r2.buckets.edit(output.name, {
64+
const bucket = yield* api.r2.buckets.edit(output.bucketName, {
6365
account_id: output.accountId,
6466
storage_class: news.storageClass ?? output.storageClass,
6567
jurisdiction: output.jurisdiction,
@@ -68,13 +70,14 @@ export const bucketProvider = () =>
6870
}),
6971
delete: Effect.fnUntraced(function* ({ output }) {
7072
yield* api.r2.buckets
71-
.delete(output.name, {
73+
.delete(output.bucketName, {
7274
account_id: output.accountId,
7375
})
7476
.pipe(Effect.catchTag("NotFound", () => Effect.void));
7577
}),
7678
read: Effect.fnUntraced(function* ({ id, output, olds }) {
77-
const name = output?.name ?? (yield* createName(id, olds?.name));
79+
const name =
80+
output?.bucketName ?? (yield* createBucketName(id, olds?.name));
7881
const params = {
7982
account_id: output?.accountId ?? accountId,
8083
name,

alchemy-effect/src/cloudflare/r2/bucket.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export type BucketProps = {
88
};
99

1010
export type BucketAttr<Props extends BucketProps> = {
11-
name: Props["name"] extends string ? Props["name"] : string;
11+
bucketName: Props["name"] extends string ? Props["name"] : string;
1212
storageClass: Props["storageClass"] extends Bucket.StorageClass
1313
? Props["storageClass"]
1414
: "Standard";

alchemy-effect/test/cloudflare/r2/bucket.provider.test.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,13 @@ test(
3131

3232
const stack = yield* apply(TestBucket);
3333

34-
const actualBucket = yield* api.r2.buckets.get(stack.TestBucket.name, {
35-
account_id: accountId,
36-
});
37-
expect(actualBucket.name).toEqual(stack.TestBucket.name);
34+
const actualBucket = yield* api.r2.buckets.get(
35+
stack.TestBucket.bucketName,
36+
{
37+
account_id: accountId,
38+
},
39+
);
40+
expect(actualBucket.name).toEqual(stack.TestBucket.bucketName);
3841
expect(actualBucket.storage_class).toEqual("Standard");
3942
}
4043

@@ -45,15 +48,18 @@ test(
4548

4649
const stack = yield* apply(TestBucket);
4750

48-
const actualBucket = yield* api.r2.buckets.get(stack.TestBucket.name, {
49-
account_id: accountId,
50-
});
51-
expect(actualBucket.name).toEqual(stack.TestBucket.name);
51+
const actualBucket = yield* api.r2.buckets.get(
52+
stack.TestBucket.bucketName,
53+
{
54+
account_id: accountId,
55+
},
56+
);
57+
expect(actualBucket.name).toEqual(stack.TestBucket.bucketName);
5258
expect(actualBucket.storage_class).toEqual("InfrequentAccess");
5359

5460
yield* destroy();
5561

56-
yield* waitForBucketToBeDeleted(stack.TestBucket.name, accountId);
62+
yield* waitForBucketToBeDeleted(stack.TestBucket.bucketName, accountId);
5763
}).pipe(Effect.provide(CloudflareLive.providers()), logLevel),
5864
);
5965

alchemy-effect/test/evaluate.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ const vpcAttrs = {
4141
} as const satisfies TestVpc["attr"];
4242

4343
const bucketAttrs = {
44-
name: "test-bucket",
44+
bucketName: "test-bucket",
4545
storageClass: "Standard",
4646
jurisdiction: "default",
4747
location: undefined,
@@ -162,7 +162,7 @@ it.live("Output.all($(TestVpc).vpcArn, $(TestVpc).vpcId)", () =>
162162

163163
it.live("Output.all($(TestVpc).vpcArn, $(Bucket).name)", () =>
164164
Effect.gen(function* () {
165-
const output = Output.all($(TestVpc).vpcArn, $(Bucket).name);
165+
const output = Output.all($(TestVpc).vpcArn, $(Bucket).bucketName);
166166
const upstream = Output.upstream(output);
167167
const result = yield* Output.evaluate(output, resources);
168168
expect(result).toEqual([vpcAttrs.vpcArn, "test-bucket"]);
@@ -177,12 +177,12 @@ it.live(
177177
"Output.all($(TestVpc).vpcId, $(Bucket).name).apply(([vpcId, name]) => `${vpcId}-${name}`)",
178178
() =>
179179
Effect.gen(function* () {
180-
const output = Output.all($(TestVpc).vpcId, $(Bucket).name).apply(
180+
const output = Output.all($(TestVpc).vpcId, $(Bucket).bucketName).apply(
181181
([vpcId, name]) => `${vpcId}-${name}`,
182182
);
183183
const upstream = Output.upstream(output);
184184
const result = yield* Output.evaluate(output, resources);
185-
expect(result).toEqual(`${vpcId}-${bucketAttrs.name}`);
185+
expect(result).toEqual(`${vpcId}-${bucketAttrs.bucketName}`);
186186
expect(upstream).toEqual({
187187
TestVpc,
188188
Bucket,
@@ -270,11 +270,11 @@ it.live(
270270
"Output.interpolate`VPC: ${$(TestVpc).vpcArn} -- Bucket: ${$(Bucket).name}`",
271271
() =>
272272
Effect.gen(function* () {
273-
const output = Output.interpolate`VPC: ${vpc.vpcArn} -- Bucket: ${bucket.name}`;
273+
const output = Output.interpolate`VPC: ${vpc.vpcArn} -- Bucket: ${bucket.bucketName}`;
274274
const upstream = Output.upstream(output);
275275
const result = yield* Output.evaluate(output, resources);
276276
expect(result).toEqual(
277-
`VPC: ${vpcAttrs.vpcArn} -- Bucket: ${bucketAttrs.name}`,
277+
`VPC: ${vpcAttrs.vpcArn} -- Bucket: ${bucketAttrs.bucketName}`,
278278
);
279279
expect(upstream).toEqual({
280280
TestVpc,
@@ -304,7 +304,7 @@ it.live(
304304
() =>
305305
Effect.gen(function* () {
306306
const upstream = Output.resolveUpstream({
307-
vpcArn: [$(TestVpc).vpcArn, $(Bucket).name],
307+
vpcArn: [$(TestVpc).vpcArn, $(Bucket).bucketName],
308308
});
309309
expect(upstream).toEqual({
310310
TestVpc,

0 commit comments

Comments
 (0)