8000 feat(cloudflare): add URL support to WorkerStub (#464) · sam-goodwin/alchemy@4fda99d · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit 4fda99d

Browse files
authored
feat(cloudflare): add URL support to WorkerStub (#464)
1 parent 271331e commit 4fda99d

File tree

4 files changed

+67
-8
lines changed

4 files changed

+67
-8
lines changed

alchemy/src/cloudflare/worker-stub.ts

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
type CloudflareApi,
88
type CloudflareApiOptions,
99
} from "./api.ts";
10+
import { configureURL } from "./worker.ts";
1011

1112
/**
1213
* Properties for creating a Worker stub
@@ -19,6 +20,14 @@ export interface WorkerStubProps<
1920
*/
2021
name: string;
2122

23+
/**
24+
* Whether to enable a workers.dev URL for this worker
25+
*
26+
* If true, the worker will be available at {name}.{subdomain}.workers.dev
27+
* @default true
28+
*/
29+
url?: boolean;
30+
2231
/**
2332
* The RPC class to use for the worker.
2433
*
@@ -39,6 +48,12 @@ export interface WorkerStub<
3948
*/
4049
name: string;
4150

51+
/**
52+
* The worker's URL if enabled
53+
* Format: {name}.{subdomain}.workers.dev
54+
*/
55+
url?: string;
56+
4257
/**
4358
* Optional type branding for the worker's RPC entrypoint.
4459
*
@@ -59,12 +74,21 @@ export function isWorkerStub(resource: Resource): resource is WorkerStub {
5974
* exists and creates an empty one if needed.
6075
*
6176
* @example
62-
* // Reserve a worker name without deploying code
77+
* // Reserve a worker name without deploying code, with URL enabled (default)
6378
* const workerStub = await WorkerStub("my-worker", {
6479
* name: "my-reserved-worker"
6580
* });
6681
*
67-
* console.log(`Worker ${workerStub.name} exists: ${!workerStub.created}`);
82+
* console.log(`Worker ${workerStub.name} is available at: ${workerStub.url}`);
83+
*
84+
* @example
85+
* // Reserve a worker name without enabling URL
86+
* const workerStub = await WorkerStub("my-worker", {
87+
* name: "my-reserved-worker",
88+
* url: false
89+
* });
90+
*
91+
* console.log(`Worker ${workerStub.name} created without URL`);
6892
*/
6993
export const WorkerStub = Resource("cloudflare::WorkerStub", async function <
7094
RPC extends Rpc.WorkerEntrypointBranded = Rpc.WorkerEntrypointBranded,
@@ -84,11 +108,16 @@ export const WorkerStub = Resource("cloudflare::WorkerStub", async function <
84108
await createEmptyWorker(api, props.name);
85109
}
86110

111+
// Configure URL if requested (defaults to true)
112+
const enableUrl = props.url ?? true;
113+
const workerUrl = await configureURL(this, api, props.name, enableUrl);
114+
87115
// Return the worker stub info
88116
return this({
89117
type: "service",
90118
__rpc__: props.rpc as unknown as RPC,
91119
...props,
120+
url: workerUrl,
92121
}) as WorkerStub<RPC>;
93122
});
94123

alchemy/src/cloudflare/worker.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import path from "node:path";
2+
import { BUILD_DATE } from "../build-date.ts";
23
import type { Context } from "../context.ts";
34
import type { BundleProps } from "../esbuild/bundle.ts";
45
import { InnerResourceScope, Resource, ResourceKind } from "../resource.ts";
@@ -13,7 +14,6 @@ import { getContentType } from "../util/content-type.ts";
1314
import { logger } from "../util/logger.ts";
1415
import { withExponentialBackoff } from "../util/retry.ts";
1516
import { slugify } from "../util/slugify.ts";
16-
import { BUILD_DATE } from "../build-date.ts";
1717
import { CloudflareApiError, handleApiError } from "./api-error.ts";
1818
import {
1919
type CloudflareApi,
@@ -1468,7 +1468,7 @@ export async function assertWorkerDoesNotExist<B extends Bindings>(
14681468
}
14691469

14701470
export async function configureURL<B extends Bindings>(
1471-
ctx: Context<Worker<B>>,
1471+
ctx: Context<Worker<B>> | Context<WorkerStub>,
14721472
api: CloudflareApi,
14731473
workerName: string,
14741474
url: boolean,

alchemy/test/cloudflare/worker-stub.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,32 @@ test("worker-stub-types", () => {
4141
env.workerB.bar();
4242
}
4343
});< 341A /div>
44+
45+
test("worker-stub-url-types", () => {
46+
// type-only test for URL functionality
47+
async function _() {
48+
// Test with URL enabled (default)
49+
const stubWithUrl = await WorkerStub("stub-with-url", {
50+
name: "stub-with-url",
51+
});
52+
53+
// Should have URL property
54+
const _url1: string | undefined = stubWithUrl.url;
55+
56+
// Test with URL explicitly enabled
57+
const stubExplicitUrl = await WorkerStub("stub-explicit-url", {
58+
name: "stub-explicit-url",
59+
url: true,
60+
});
61+
62+
const _url2: string | undefined = stubExplicitUrl.url;
63+
64+
// Test with URL disabled
65+
const stubNoUrl = await WorkerStub("stub-no-url", {
66+
name: "stub-no-url",
67+
url: false,
68+
});
69+
70+
const _url3: string | undefined = stubNoUrl.url;
71+
}
72+
});

stacks/src/repo.run.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// ensure providers are registered (for deletion purposes)
2-
import "../alchemy/src/aws/index.ts";
3-
import "../alchemy/src/aws/oidc/index.ts";
4-
import "../alchemy/src/cloudflare/index.ts";
5-
import "../alchemy/src/os/index.ts";
2+
import "alchemy/aws";
3+
import "alchemy/aws/oidc";
4+
import "alchemy/cloudflare";
5+
import "alchemy/os";
66

77
import alchemy from "alchemy";
88
import { AccountId, Role, SSMParameter } from "alchemy/aws";
@@ -53,6 +53,7 @@ const githubRole = await Role("github-oidc-role", {
5353

5454
const stateStore = await R2Bucket("state-store", {
5555
name: "alchemy-state-store",
56+
adopt: true,
5657
});
5758

5859
const testEnvironment = await RepositoryEnvironment("test environment", {

0 commit comments

Comments
 (0)
0