Skip to content

Commit 1cb2d34

Browse files
authored
Omit Content-Type header for files of unknown extension in Workers Assets (#8248)
* Omit Content-Type header for files of unknown extension in Workers Assets * Omit Content-Type header for files of unknown extension in Workers Assets
1 parent 9f05e8f commit 1cb2d34

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed

.changeset/quick-singers-stand.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"wrangler": minor
3+
---
4+
5+
feat: Omits Content-Type header for files of an unknown extension in Workers Assets

packages/wrangler/src/__tests__/deploy.test.ts

+53
Original file line numberDiff line numberDiff line change
@@ -5038,6 +5038,59 @@ addEventListener('fetch', event => {});`
50385038
`);
50395039
});
50405040

5041+
it("should attach an 'application/null' content-type header when uploading files with an unknown extension", async () => {
5042+
const assets = [{ filePath: "foobar.greg", content: "something-binary" }];
5043+
writeAssets(assets);
5044+
writeWranglerConfig({
5045+
assets: { directory: "assets" },
5046+
});
5047+
5048+
const manifestBodies: AssetManifest[] = [];
5049+
const mockBuckets = [["80e40c1f2422528cb2fba3f9389ce315"]];
5050+
await mockAUSRequest(manifestBodies, mockBuckets, "<<aus-token>>");
5051+
const uploadBodies: FormData[] = [];
5052+
const uploadAuthHeaders: (string | null)[] = [];
5053+
const uploadContentTypeHeaders: (string | null)[] = [];
5054+
await mockAssetUploadRequest(
5055+
mockBuckets.length,
5056+
uploadBodies,
5057+
uploadAuthHeaders,
5058+
uploadContentTypeHeaders
5059+
);
5060+
mockSubDomainRequest();
5061+
mockUploadWorkerRequest({
5062+
expectedAssets: {
5063+
jwt: "<<aus-completion-token>>",
5064+
config: {},
5065+
},
5066+
expectedType: "none",
5067+
});
5068+
await runWrangler("deploy");
5069+
expect(manifestBodies.length).toBe(1);
5070+
expect(manifestBodies[0]).toEqual({
5071+
manifest: {
5072+
"/foobar.greg": {
5073+
hash: "80e40c1f2422528cb2fba3f9389ce315",
5074+
size: 16,
5075+
},
5076+
},
5077+
});
5078+
const flatBodies = Object.fromEntries(
5079+
uploadBodies.flatMap((b) => [...b.entries()])
5080+
);
5081+
await expect(
5082+
flatBodies["80e40c1f2422528cb2fba3f9389ce315"]
5083+
).toBeAFileWhichMatches(
5084+
new File(
5085+
["c29tZXRoaW5nLWJpbmFyeQ=="],
5086+
"80e40c1f2422528cb2fba3f9389ce315",
5087+
{
5088+
type: "application/null",
5089+
}
5090+
)
5091+
);
5092+
});
5093+
50415094
it("should be able to upload files with special characters in filepaths", async () => {
50425095
// NB windows will disallow these characters in file paths anyway < > : " / \ | ? *
50435096
const assets = [

packages/wrangler/src/assets.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,11 @@ export const syncAssets = async (
138138
[(await readFile(absFilePath)).toString("base64")],
139139
manifestEntry[1].hash,
140140
{
141-
type: getContentType(absFilePath) || "application/octet-stream",
141+
// Most formdata body encoders (incl. undici's) will override with "application/octet-stream" if you use a falsy value here
142+
// Additionally, it appears that undici doesn't support non-standard main types (e.g. "null")
143+
// So, to make it easier for any other clients, we'll just parse "application/null" on the API
144+
// to mean actually null (signal to not send a Content-Type header with the response)
145+
type: getContentType(absFilePath) ?? "application/null",
142146
}
143147
),
144148
manifestEntry[1].hash

0 commit comments

Comments
 (0)