Skip to content

Commit

Permalink
fix: ensure asset keys are relative to the project root
Browse files Browse the repository at this point in the history
Previously, asset file paths were computed relative to the current working
directory, even if we had used `-c` to run Wrangler on a project in a different
directory to the current one.

Now, assets file paths are computed relative to the "project root", which is
either the directory containing the wrangler.toml or the current working directory
if there is no config specified.
  • Loading branch information
petebacondarwin committed Mar 18, 2022
1 parent b6340aa commit a1bdb72
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 18 deletions.
13 changes: 13 additions & 0 deletions .changeset/long-cameras-nail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
"wrangler": patch
---

fix: ensure asset keys are relative to the project root

Previously, asset file paths were computed relative to the current working
directory, even if we had used `-c` to run Wrangler on a project in a different
directory to the current one.

Now, assets file paths are computed relative to the "project root", which is
either the directory containing the wrangler.toml or the current working directory
if there is no config specified.
2 changes: 1 addition & 1 deletion packages/wrangler/src/dev/local.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ function useLocalWorker({
),
durableObjectsPersist: enableLocalPersistence,
cachePersist: !enableLocalPersistence,
sitePath: assetPaths?.baseDirectory,
sitePath: assetPaths?.assetDirectory,
siteInclude: assetPaths?.includePatterns.length
? assetPaths?.includePatterns
: undefined,
Expand Down
58 changes: 41 additions & 17 deletions packages/wrangler/src/sites.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -137,19 +137,24 @@ export async function syncAssets(
const exclude = createPatternMatcher(siteAssets.excludePatterns, true);
const hasher = await xxhash();

for await (const file of getFilesInFolder(siteAssets.baseDirectory)) {
if (!include(file)) {
const assetDirectory = path.join(
siteAssets.baseDirectory,
siteAssets.assetDirectory
);
for await (const absAssetFile of getFilesInFolder(assetDirectory)) {
const assetFile = path.relative(siteAssets.baseDirectory, absAssetFile);
if (!include(assetFile)) {
continue;
}
if (exclude(file)) {
if (exclude(assetFile)) {
continue;
}

await validateAssetSize(file);
console.log(`reading ${file}...`);
const content = await readFile(file, "base64");
await validateAssetSize(absAssetFile, assetFile);
console.log(`reading ${assetFile}...`);
const content = await readFile(absAssetFile, "base64");

const assetKey = hashAsset(hasher, file, content);
const assetKey = hashAsset(hasher, assetFile, content);
validateAssetKey(assetKey);

// now put each of the files into kv
Expand All @@ -165,7 +170,7 @@ export async function syncAssets(
}
// remove the key from the set so we know we've seen it
delete keyMap[assetKey];
manifest[path.relative(siteAssets.baseDirectory, file)] = assetKey;
manifest[path.relative(siteAssets.assetDirectory, absAssetFile)] = assetKey;
}

// `keyMap` now contains the assets that we need to expire
Expand Down Expand Up @@ -214,11 +219,14 @@ function createPatternMatcher(
}
}

async function validateAssetSize(filePath: string) {
const { size } = await stat(filePath);
async function validateAssetSize(
absFilePath: string,
relativeFilePath: string
) {
const { size } = await stat(absFilePath);
if (size > 25 * 1024 * 1024) {
throw new Error(
`File ${filePath} is too big, it should be under 25 MiB. See https://developers.cloudflare.com/workers/platform/limits#kv-limits`
`File ${relativeFilePath} is too big, it should be under 25 MiB. See https://developers.cloudflare.com/workers/platform/limits#kv-limits`
);
}
}
Expand All @@ -244,8 +252,23 @@ function urlSafe(filePath: string): string {
* Information about the assets that should be uploaded
*/
export interface AssetPaths {
/**
* Absolute path to the root of the project.
*
* This is the directory containing wrangler.toml or cwd if no config.
*/
baseDirectory: string;
/**
* The path to the assets directory, relative to the `baseDirectory`.
*/
assetDirectory: string;
/**
* An array of patterns that match files that should be uploaded.
*/
includePatterns: string[];
/**
* An array of patterns that match files that should not be uploaded.
*/
excludePatterns: string[];
}

Expand All @@ -258,16 +281,17 @@ export interface AssetPaths {
*/
export function getAssetPaths(
config: Config,
baseDirectory = config.site?.bucket,
assetDirectory = config.site?.bucket,
includePatterns = config.site?.include ?? [],
excludePatterns = config.site?.exclude ?? []
): undefined | AssetPaths {
return baseDirectory
const baseDirectory = path.resolve(
path.dirname(config.configPath ?? "wrangler.toml")
);
return assetDirectory
? {
baseDirectory: path.resolve(
path.dirname(config.configPath ?? "wrangler.toml"),
baseDirectory
),
baseDirectory,
assetDirectory,
includePatterns,
excludePatterns,
}
Expand Down

0 comments on commit a1bdb72

Please sign in to comment.