Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion packages/alchemy/src/Cloudflare/Workers/Assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,44 @@ const createIgnoreMatcher = (patterns: string[]) => {
return (file: string) => matcher.ignores(file);
};

/**
* Read the special `_headers` / `_redirects` files from an assets
* directory. They are excluded from the upload manifest, but their raw
* contents must be sent to Cloudflare in the script metadata's asset
* config (`config._headers` / `config._redirects`) for the rules to
* apply.
*/
export const readAssetsConfigFiles = Effect.fn(function* (directory: string) {
const path = yield* Path.Path;
const resolvedDirectory = path.resolve(directory);
const [_headers, _redirects] = yield* Effect.all([
maybeReadString(path.join(resolvedDirectory, "_headers")),
maybeReadString(path.join(resolvedDirectory, "_redirects")),
]);
return { _headers, _redirects };
});

/**
* Merge `_headers` / `_redirects` file contents into an asset config,
* producing the config to send in the script-upload metadata. Explicit
* `headers` / `redirects` props win over the files.
*/
export const mergeAssetsConfigFiles = (
config: AssetsConfig | undefined,
files: { _headers: string | undefined; _redirects: string | undefined },
): AssetsConfig | undefined => {
const headers = config?.headers ?? files._headers;
const redirects = config?.redirects ?? files._redirects;
if (headers === undefined && redirects === undefined) {
return config;
}
return {
...config,
...(headers !== undefined ? { headers } : undefined),
...(redirects !== undefined ? { redirects } : undefined),
};
};

export const readAssets = Effect.fn(function* ({
directory,
...config
Expand Down Expand Up @@ -193,7 +231,11 @@ export const readAssets = Effect.fn(function* ({
});
return {
directory,
config,
// Fold the `_headers` / `_redirects` file contents into the config
// that gets sent to Cloudflare (`metadata.assets.config`). Merged
// *after* hashing so the hash input shape stays stable for
// already-deployed workers.
config: mergeAssetsConfigFiles(config, { _headers, _redirects }),
manifest: sortedManifest,
_headers,
_redirects,
Expand Down
30 changes: 23 additions & 7 deletions packages/alchemy/src/Cloudflare/Workers/LocalWorkerProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ import { sha256, unwrapRedacted } from "../../Util/index.ts";
import { CloudflareEnvironment } from "../CloudflareEnvironment.ts";
import { LOCAL_ENTRY_URL, LocalRuntimeState } from "../LocalRuntime.ts";
import type { WorkerAssetsConfig, WorkerProps } from "../Workers/Worker.ts";
import { readAssetsConfigFiles } from "./Assets.ts";
import { getCompatibility } from "./Compatibility.ts";
import { isPythonMain, watchPythonWorkerBundle } from "./PythonWorkerBundle.ts";
import { Worker } from "./Worker.ts";
Expand Down Expand Up @@ -295,7 +296,7 @@ export const LocalWorkerProvider = () =>
durableObjectNamespaces: worker.durableObjectNamespaces,
queueConsumers,
modules: yield* toRuntimeModules(bundle),
assets: toRuntimeAssets(worker.assets),
assets: yield* toRuntimeAssets(worker.assets),
})
.pipe(Scope.provide(scope));
workerdScopes.set(worker.id, scope);
Expand Down Expand Up @@ -588,7 +589,7 @@ export const LocalWorkerProvider = () =>
durableObjectNamespaces: worker.durableObjectNamespaces,
hyperdrives: worker.hyperdrives,
queueConsumers: yield* getQueueConsumers(worker.name),
assets: toRuntimeAssets(worker.assets),
assets: yield* toRuntimeAssets(worker.assets),
},
context,
},
Expand Down Expand Up @@ -949,19 +950,34 @@ const structuralSignature = (value: unknown): Effect.Effect<string> => {
return sha256(JSON.stringify(normalize(value)));
};

const toRuntimeAssets = (
const toRuntimeAssets = Effect.fn(function* (
assets: WorkerAssetsConfig | undefined,
): RuntimeAssets | undefined => {
) {
if (!assets) return undefined;
// Mirror the deploy path: the special `_headers` / `_redirects` files
// in the assets directory carry the rules unless overridden by
// explicit `headers` / `redirects` props. The local runtime parses
// the raw string contents just like Cloudflare does.
const directory = typeof assets === "string" ? assets : assets.directory;
// An unreadable file just means no rules here — the assets plugin
// reports directory problems itself.
const files = yield* readAssetsConfigFiles(directory).pipe(
Effect.orElseSucceed(() => ({
_headers: undefined,
_redirects: undefined,
})),
);
if (typeof assets === "string") {
return {
directory: assets,
headers: files._headers,
redirects: files._redirects,
};
}
return {
directory: assets.directory,
headers: assets.headers,
redirects: assets.redirects,
headers: assets.headers ?? files._headers,
redirects: assets.redirects ?? files._redirects,
// Distilled widened generated string enums to open unions (`string & {}`);
// the API only ever returns the known variants here.
htmlHandling: assets.htmlHandling as
Expand All @@ -978,7 +994,7 @@ const toRuntimeAssets = (
runWorkerFirst: assets.runWorkerFirst,
serveDirectly: assets.serveDirectly,
};
};
});

const moduleTypeFromExtension = (ext: string): Module["type"] | "SourceMap" => {
switch (ext) {
Expand Down
22 changes: 18 additions & 4 deletions packages/alchemy/src/Cloudflare/Workers/WorkerProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ import { sha256Object } from "../../Util/sha256.ts";
import { CloudflareEnvironment } from "../CloudflareEnvironment.ts";
import { CloudflareLogs } from "../Logs.ts";
import { listAllZones, resolveZoneId } from "../Zone/lookup.ts";
import { readAssets, uploadAssets } from "./Assets.ts";
import {
mergeAssetsConfigFiles,
readAssets,
readAssetsConfigFiles,
uploadAssets,
} from "./Assets.ts";
import { getCompatibility } from "./Compatibility.ts";
import { isDurableObjectExport } from "./DurableObject.ts";
import { LocalWorkerProvider } from "./LocalWorkerProvider.ts";
Expand Down Expand Up @@ -1360,8 +1365,8 @@ export const LiveWorkerProvider = () =>
output: Worker["Attributes"] | undefined,
) => {
if (!Predicate.hasProperty(assets, "hash")) return undefined;
const { directory: _, hash, ...config } = assets;
return { config, hash, skip: hash === output?.hash?.assets };
const { directory, hash, ...config } = assets;
return { directory, config, hash, skip: hash === output?.hash?.assets };
};

const putWorker = Effect.fn(function* (
Expand Down Expand Up @@ -1445,7 +1450,16 @@ export const LiveWorkerProvider = () =>
`Cloudflare Worker update: assets unchanged for ${name}, keeping existing`,
);
keepAssets = true;
metadataAssets = { config: prebuiltAssets.config };
// `keepAssets` only preserves the uploaded files — the PUT
// replaces the asset config wholesale. The skip path never
// walked the directory, so read just `_headers`/`_redirects`
// here or a no-op deploy would wipe their rules.
metadataAssets = {
config: mergeAssetsConfigFiles(
prebuiltAssets.config,
yield* readAssetsConfigFiles(prebuiltAssets.directory),
),
};
metadataBindings.push({
type: "assets",
name: "ASSETS",
Expand Down
135 changes: 135 additions & 0 deletions packages/alchemy/test/Cloudflare/Utils/Http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,141 @@ export const expectUrlContains = (
);
};

export class HttpResponseMismatch extends Data.TaggedError(
"HttpResponseMismatch",
)<{
url: string;
expected: string;
actual: string;
}> {}

const fetchOnceResponse = (
url: string,
check: (res: Response) => HttpResponseMismatch | undefined,
) =>
Effect.tryPromise({
try: async (signal) => {
const u = new URL(url);
u.searchParams.set("__alchemy_cb", String(Date.now()));
const res = await fetch(u, {
signal,
cache: "no-store",
redirect: "manual",
headers: {
"cache-control": "no-cache",
pragma: "no-cache",
accept: "*/*",
},
});
const mismatch = check(res);
if (mismatch) {
throw mismatch;
}
return res;
},
catch: (e) =>
e instanceof HttpResponseMismatch
? e
: new HttpFetchFailed({
url,
message: e instanceof Error ? e.message : String(e),
}),
});

const retryResponse = (
url: string,
expected: string,
effect: Effect.Effect<Response, HttpResponseMismatch | HttpFetchFailed>,
options: ExpectUrlContainsOptions,
) => {
const totalTimeout = Duration.fromInputUnsafe(
options.timeout ?? "90 seconds",
);
const initial = options.initialBackoff ?? "750 millis";
const label = options.label ?? "url";
return effect.pipe(
Effect.retry({
schedule: Schedule.min([
Schedule.exponential(initial, 1.5),
Schedule.spaced("8 seconds"),
]),
}),
Effect.timeoutOrElse({
duration: totalTimeout,
orElse: () =>
Effect.fail(
new HttpResponseMismatch({
url,
expected,
actual: `[timed out after ${Duration.toMillis(totalTimeout)}ms waiting for ${expected}]`,
}),
),
}),
Effect.tapError((error) =>
Effect.logError(`expect response (${label}) failed`, error),
),
);
};

/**
* Fetch `url` without following redirects and assert the response is a
* redirect to `location` (with `status`, default 301). Retries through
* deploy propagation like {@link expectUrlContains}.
*/
export const expectUrlRedirect = (
url: string,
location: string,
options: ExpectUrlContainsOptions & { status?: number } = {},
) => {
const status = options.status ?? 301;
const expected = `${status} -> ${location}`;
return retryResponse(
url,
expected,
fetchOnceResponse(url, (res) => {
// Cloudflare appends the request's query string to the Location
// header, so compare pathnames rather than the raw value.
const actual = res.headers.get("location");
const actualPath = actual === null ? null : new URL(actual, url).pathname;
return res.status === status && actualPath === location
? undefined
: new HttpResponseMismatch({
url,
expected,
actual: `${res.status} -> ${actual}`,
});
}),
options,
);
};

/**
* Fetch `url` and assert response header `header` equals `value`.
* Retries through deploy propagation like {@link expectUrlContains}.
*/
export const expectUrlHeader = (
url: string,
header: string,
value: string,
options: ExpectUrlContainsOptions = {},
) => {
const expected = `${header}: ${value}`;
return retryResponse(
url,
expected,
fetchOnceResponse(url, (res) =>
res.headers.get(header) === value
? undefined
: new HttpResponseMismatch({
url,
expected,
actual: `${res.status} ${header}: ${res.headers.get(header)}`,
}),
),
options,
);
};

const fetchOnceAbsent = (url: string, marker: string) =>
Effect.tryPromise({
try: async (signal) => {
Expand Down
Loading
Loading