Skip to content

Commit

Permalink
Fix incorrect extension extraction from file paths (#1045)
Browse files Browse the repository at this point in the history
* fix: Incorrect extension extraction from file paths for pages prebuilt assets publish

Fixes #1029

Our extension extraction logic was taking into account folder names, which can include periods. The logic would incorrectly identify a file path of `.well-known/foo` as having the extension of `well-known/foo` when in reality it should be an empty string.

* Update .changeset/large-pets-cheer.md

Co-authored-by: Pete Bacon Darwin <[email protected]>

Co-authored-by: Sunil Pai <[email protected]>
Co-authored-by: Pete Bacon Darwin <[email protected]>
  • Loading branch information
3 people authored May 19, 2022
1 parent 6c00490 commit 8eeef9a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
7 changes: 7 additions & 0 deletions .changeset/large-pets-cheer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"wrangler": patch
---

fix: Incorrect extension extraction from file paths.

Our extension extraction logic was taking into account folder names, which can include periods. The logic would incorrectly identify a file path of .well-known/foo as having the extension of well-known/foo when in reality it should be an empty string.
25 changes: 24 additions & 1 deletion packages/wrangler/src/__tests__/pages.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { writeFileSync } from "node:fs";
import { mkdirSync, writeFileSync } from "node:fs";
import { mockAccountId, mockApiToken } from "./helpers/mock-account-id";
import { setMockResponse, unsetAllMocks } from "./helpers/mock-cfetch";
import { mockConsoleMethods } from "./helpers/mock-console";
Expand Down Expand Up @@ -323,5 +323,28 @@ describe("pages", () => {
// ✨ Deployment complete! Take a peek over at https://abcxyz.foo.pages.dev/"
// `);
});

it("should not error when directory names contain periods and houses a extensionless file", async () => {
mkdirSync(".well-known");
writeFileSync(".well-known/foobar", "foobar");

setMockResponse(
"/accounts/:accountId/pages/projects/foo/file",
async () => ({
id: "7b764dacfd211bebd8077828a7ddefd7",
})
);

setMockResponse(
"/accounts/:accountId/pages/projects/foo/deployments",
async () => ({
url: "https://abcxyz.foo.pages.dev/",
})
);

await runWrangler("pages publish . --project-name=foo");

expect(std.err).toMatchInlineSnapshot(`""`);
});
});
});
5 changes: 2 additions & 3 deletions packages/wrangler/src/pages.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { execSync, spawn } from "node:child_process";
import { existsSync, lstatSync, readFileSync, writeFileSync } from "node:fs";
import { readdir, readFile, stat } from "node:fs/promises";
import { tmpdir } from "node:os";
import { dirname, join, sep } from "node:path";
import { dirname, join, sep, extname, basename } from "node:path";
import { cwd } from "node:process";
import { URL } from "node:url";
import { hash } from "blake3-wasm";
Expand Down Expand Up @@ -1079,8 +1079,7 @@ const createDeployment: CommandModule<
const fileContent = await readFile(filepath);

const base64Content = fileContent.toString("base64");
const extension =
name.split(".").length > 1 ? name.split(".").at(-1) || "" : "";
const extension = extname(basename(name)).substring(1);

const content = base64Content + extension;

Expand Down

0 comments on commit 8eeef9a

Please sign in to comment.