Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use expiration_ttl to expire assets with [site] #649

Merged
merged 1 commit into from
Mar 20, 2022
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
7 changes: 7 additions & 0 deletions .changeset/blue-nails-unite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"wrangler": patch
---

fix: use `expiration_ttl` to expire assets with `[site]`

This switches how we expire static assets with `[site]` uploads to use `expiration_ttl` instead of `expiration`. This is because we can't trust the time that a deploy target may provide (like in https://github.com/cloudflare/wrangler/issues/2224).
5 changes: 3 additions & 2 deletions packages/wrangler/src/__tests__/kv.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,7 @@ describe("wrangler", () => {
const keys = [
{ name: "key-1" },
{ name: "key-2", expiration: 123456789 },
{ name: "key-3" },
{ name: "key-3", expiration_ttl: 666 },
];
mockKeyListRequest("some-namespace-id", keys);
await runWrangler("kv:key list --namespace-id some-namespace-id");
Expand All @@ -691,7 +691,8 @@ describe("wrangler", () => {
\\"expiration\\": 123456789
},
{
\\"name\\": \\"key-3\\"
\\"name\\": \\"key-3\\",
\\"expiration_ttl\\": 666
}
]"
`);
Expand Down
32 changes: 13 additions & 19 deletions packages/wrangler/src/__tests__/publish.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1239,18 +1239,6 @@ export default{
});

describe("expire unused assets", () => {
// it's a 100 seconds past epoch
// everyone's still talking about how great woodstock was
const DATE_NOW = 100000;
beforeEach(() => {
jest.spyOn(Date, "now").mockImplementation((): number => {
return DATE_NOW;
});
});
afterEach(() => {
(Date.now as jest.Mock).mockRestore();
});

it("should expire uploaded assets that aren't included anymore", async () => {
const assets = [
{ filePath: "assets/file-1.txt", content: "Content of file-1" },
Expand Down Expand Up @@ -1299,14 +1287,14 @@ export default{
{
filePath: "assets/file-3.txt",
content: "Content of file-3",
// expire this asset 300 seconds from now
expiration: DATE_NOW / 1000 + 300,
// expect to expire this asset 300 seconds from now
expiration_ttl: 300,
},
{
filePath: "assets/file-4.txt",
content: "Content of file-4",
// expire this asset 300 seconds from now
expiration: DATE_NOW / 1000 + 300,
// expect to expire this asset 300 seconds from now
expiration_ttl: 300,
},
]);

Expand Down Expand Up @@ -1375,8 +1363,8 @@ export default{
{
filePath: "assets/file-4.txt",
content: "Content of file-4",
// expire this asset 300 seconds from now
expiration: DATE_NOW / 1000 + 300,
// expect to expire this asset 300 seconds from now
expiration_ttl: 300,
},
]);

Expand Down Expand Up @@ -2580,7 +2568,12 @@ function mockListKVNamespacesRequest(...namespaces: KVNamespaceInfo[]) {
/** Create a mock handler for the request that tries to do a bulk upload of assets to a KV namespace. */
function mockUploadAssetsToKVRequest(
expectedNamespaceId: string,
assets: { filePath: string; content: string; expiration?: number }[]
assets: {
filePath: string;
content: string;
expiration?: number;
expiration_ttl?: number;
}[]
) {
setMockResponse(
"/accounts/:accountId/storage/kv/namespaces/:namespaceId/bulk",
Expand Down Expand Up @@ -2610,6 +2603,7 @@ function mockUploadAssetsToKVRequest(
asset.content
);
expect(upload.expiration).toEqual(asset.expiration);
expect(upload.expiration_ttl).toEqual(asset.expiration_ttl);
}
return null;
}
Expand Down
7 changes: 4 additions & 3 deletions packages/wrangler/src/sites.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const ALWAYS_IGNORE = new Set(["node_modules"]);
const HIDDEN_FILES_TO_INCLUDE = new Set([
".well-known", // See https://datatracker.ietf.org/doc/html/rfc8615
]);
const FIVE_MINUTES = 1000 * 60 * 5;
const FIVE_MINUTES = 60 * 5; // expressed in seconds, since that's what the api expects

async function* getFilesInFolder(dirPath: string): AsyncIterable<string> {
const files = await readdir(dirPath, { withFileTypes: true });
Expand Down Expand Up @@ -169,7 +169,6 @@ export async function syncAssets(
}

// `keyMap` now contains the assets that we need to expire
const FIVE_MINUTES_LATER = Math.ceil((Date.now() + FIVE_MINUTES) / 1000); // expressed in seconds, since that's what the API accepts

let toExpire: KeyValue[] = [];
for (const asset of Object.values(keyMap)) {
Expand All @@ -178,7 +177,9 @@ export async function syncAssets(
toExpire.push({
key: asset.name,
value: "", // we'll fill all the values in one go
expiration: FIVE_MINUTES_LATER,
// we use expiration_ttl, since we can't trust the time
// that a deploy source might provide (eg - https://github.com/cloudflare/wrangler/issues/2224)
expiration_ttl: FIVE_MINUTES,
base64: true,
});
}
Expand Down