Skip to content

Commit

Permalink
Prettify kv-asset-handler
Browse files Browse the repository at this point in the history
  • Loading branch information
GregBrimble committed Jan 18, 2024
1 parent 1e046ac commit 2e01544
Show file tree
Hide file tree
Showing 10 changed files with 825 additions and 725 deletions.
45 changes: 22 additions & 23 deletions packages/kv-asset-handler/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@

- **Allow configurable downgrade of ETag validator strength - [awwong1], [pull/315]**

This allows users to override the default strong ETag validator behaviour to use weak ETag validators. This change allows the developer to use weak ETags and preserve 304 responses (e.g. on *.workers.dev domains).
This allows users to override the default strong ETag validator behaviour to use weak ETag validators. This change allows the developer to use weak ETags and preserve 304 responses (e.g. on \*.workers.dev domains).

- ### Fixes

- **Fix length property call on ArrayBuffer instance - [philipatkinson], [pull/295**]

Previously when edge cached was enabled, the `content-length` of the response was not being set correctly. This was due to the `length` property of the `ArrayBuffer` instance being called instead of the `byteLength` property. This PR fixes this issue.

- ### Maintenance
Expand All @@ -51,12 +51,11 @@
- **chore: use tabs for indentation - [Cherry], [pull/355]**

This PR changes the indentation of the project to use tabs instead of spaces, falling more in line with other Cloudflare JavaScript projects like wrangler.

- **chore: bump dependencies - [Cherry], [pull/356]**

This bumps many dependencies of the project to their latest versions.


## 0.2.0

- ### Features
Expand Down Expand Up @@ -133,15 +132,15 @@
This PR improves performance of the `getAssetFromKV` function by only parsing the asset manifest once on startup, instead of on each request. This can have a significant improvement in response times for larger sites. An example of the performance improvement with an asset manifest of over 50k files:

> Before change:
100 iterations: Done. Mean kv response time is 16.61
1000 iterations: Done. Mean kv response time is 17.798
> 100 iterations: Done. Mean kv response time is 16.61
> 1000 iterations: Done. Mean kv response time is 17.798
> After change:
100 iterations: Done. Mean kv response time is 6.62
1000 iterations: Done. Mean kv response time is 7.296
> 100 iterations: Done. Mean kv response time is 6.62
> 1000 iterations: Done. Mean kv response time is 7.296

Initial work and credit to [groenlid] in [pull/143].

[Cherry]: https://github.com/Cherry
[cherry]: https://github.com/Cherry
[groenlid]: https://github.com/groenlid
[pull/185]: https://github.com/cloudflare/kv-asset-handler/pull/185
[pull/143]: https://github.com/cloudflare/kv-asset-handler/pull/143
Expand All @@ -165,15 +164,15 @@
This PR tweaks the GitHub Actions Workflow to test PRs properly, both in terms of linting and the repository tests. It runs `prettier` to maintain code quality and style, and all unit tests on every PR to ensure no regressions occur.

[pull/183]: https://github.com/cloudflare/kv-asset-handler/pull/185
[Cherry]: https://github.com/Cherry
[cherry]: https://github.com/Cherry

- **Add test for `mapRequestToAsset` asset override - [Cherry], [pull/186]**

This PR adds a test for the functionality added in [pull/159]. This tests that when overriding the `mapRequestToAsset` function in its entirety, this function is always run.

[pull/159]: https://github.com/cloudflare/kv-asset-handler/pull/159
[pull/186]: https://github.com/cloudflare/kv-asset-handler/pull/186
[Cherry]: https://github.com/Cherry
[cherry]: https://github.com/Cherry

- **Dependabot updates**

Expand Down Expand Up @@ -206,9 +205,9 @@
This PR adds support for customizing the `defaultDocument` option in `getAssetFromKV`. In situations where a project does not use `index.html` as the default document for a path, this can now be customized to values like `index.shtm`:

```js
return getAssetFromKV(event, {
defaultDocument: "index.shtm"
})
return getAssetFromKV(event, {
defaultDocument: "index.shtm",
});
```

[boemekeld]: https://github.com/boemekeld
Expand All @@ -222,7 +221,7 @@
**Note that this is a breaking change**, as previously, the mapRequestToAsset function was ignored if you set it, and an exact match was found in the `ASSET_MANIFEST`. That being said, this behavior was a bug, and unexpected behavior, as documented in [issue/158].
[Cherry]: https://github.com/Cherry
[cherry]: https://github.com/Cherry
[issue/158]: https://github.com/cloudflare/kv-asset-handler/pull/158
[pull/159]: https://github.com/cloudflare/kv-asset-handler/pull/159
Expand Down Expand Up @@ -279,7 +278,7 @@
New project maintainer Cherry did a ton of maintenance in this release, improving workflows, code quality, and more. Check out the full list in [the PR][pull/179].
[Cherry]: https://github.com/Cherry
[cherry]: https://github.com/Cherry
[pull/179]: https://github.com/cloudflare/kv-asset-handler/pull/179
- ### Documentation
Expand All @@ -304,10 +303,10 @@
- **kv-asset-handler can translate 206 responses to 200 - [harrishancock], [pull/166]**
Fixes [wrangler#1746](https://github.com/cloudflare/wrangler/issues/1746)
Fixes [wrangler#1746](https://github.com/cloudflare/wrangler/issues/1746)
[harrishancock](https://github.com/harrishancock)
[pull/166](https://github.com/cloudflare/kv-asset-handler/pull/166)
[harrishancock](https://github.com/harrishancock)
[pull/166](https://github.com/cloudflare/kv-asset-handler/pull/166)
## 0.0.12
Expand All @@ -328,11 +327,11 @@
- ### Fixes
- **Fix text/* charset - [EatonZ], [pull/130]**
- **Fix text/\* charset - [EatonZ], [pull/130]**
Adds a missing `-` to the `utf-8` charset value in response mime types.
[EatonZ]: https://github.com/EatonZ
[eatonz]: https://github.com/EatonZ
[pull/130]: https://github.com/cloudflare/kv-asset-handler/pull/130
- **Cache handling for HEAD requests - [klittlepage], [pull/141]**
Expand Down Expand Up @@ -385,7 +384,7 @@
Fixes an issue where non-ASCII paths were not URI-decoded before being looked up, causing non-ASCII paths to 404.
[SukkaW]: https://github.com/SukkaW
[sukkaw]: https://github.com/SukkaW
[pull/105]: https://github.com/cloudflare/kv-asset-handler/pull/105
[issue/99]: https://github.com/cloudflare/kv-asset-handler/issues/99
Expand Down Expand Up @@ -415,7 +414,7 @@
- **Add Code of Conduct - [EverlastingBugstopper], [pull/101]**
[EverlastingBugstopper]: https://github.com/EverlastingBugstopper
[everlastingbugstopper]: https://github.com/EverlastingBugstopper
[pull/101]: https://github.com/cloudflare/kv-asset-handler/pull/101
## 0.0.10
Expand Down
66 changes: 37 additions & 29 deletions packages/kv-asset-handler/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,63 +71,71 @@ Known errors to be thrown are:
#### ES Modules

```js
import { getAssetFromKV, NotFoundError, MethodNotAllowedError } from '@cloudflare/kv-asset-handler'
import manifestJSON from '__STATIC_CONTENT_MANIFEST'
const assetManifest = JSON.parse(manifestJSON)
import {
getAssetFromKV,
NotFoundError,
MethodNotAllowedError,
} from "@cloudflare/kv-asset-handler";
import manifestJSON from "__STATIC_CONTENT_MANIFEST";
const assetManifest = JSON.parse(manifestJSON);

export default {
async fetch(request, env, ctx) {
if (request.url.includes('/docs')) {
if (request.url.includes("/docs")) {
try {
return await getAssetFromKV(
{
request,
waitUntil(promise) {
return ctx.waitUntil(promise)
return ctx.waitUntil(promise);
},
},
{
ASSET_NAMESPACE: env.__STATIC_CONTENT,
ASSET_MANIFEST: assetManifest,
},
)
}
);
} catch (e) {
if (e instanceof NotFoundError) {
// ...
} else if (e instanceof MethodNotAllowedError) {
// ...
} else {
return new Response('An unexpected error occurred', { status: 500 })
return new Response("An unexpected error occurred", { status: 500 });
}
}
} else return fetch(request)
} else return fetch(request);
},
}
};
```

#### Service Worker

```js
import { getAssetFromKV, NotFoundError, MethodNotAllowedError } from '@cloudflare/kv-asset-handler'
import {
getAssetFromKV,
NotFoundError,
MethodNotAllowedError,
} from "@cloudflare/kv-asset-handler";

addEventListener('fetch', (event) => {
event.respondWith(handleEvent(event))
})
addEventListener("fetch", (event) => {
event.respondWith(handleEvent(event));
});

async function handleEvent(event) {
if (event.request.url.includes('/docs')) {
if (event.request.url.includes("/docs")) {
try {
return await getAssetFromKV(event)
return await getAssetFromKV(event);
} catch (e) {
if (e instanceof NotFoundError) {
// ...
} else if (e instanceof MethodNotAllowedError) {
// ...
} else {
return new Response('An unexpected error occurred', { status: 500 })
return new Response("An unexpected error occurred", { status: 500 });
}
}
} else return fetch(event.request)
} else return fetch(event.request);
}
```

Expand Down Expand Up @@ -176,7 +184,7 @@ let cacheControl = {
browserTTL: null, // do not set cache control ttl on responses
edgeTTL: 2 * 60 * 60 * 24, // 2 days
bypassCache: false, // do not bypass Cloudflare's cache
}
};
```

##### `browserTTL`
Expand Down Expand Up @@ -216,13 +224,13 @@ return getAssetFromKV(
{
request,
waitUntil(promise) {
return ctx.waitUntil(promise)
return ctx.waitUntil(promise);
},
},
{
ASSET_NAMESPACE: env.__STATIC_CONTENT,
},
)
}
);
```

##### Service Worker
Expand All @@ -244,22 +252,22 @@ In ES Modules format, this argument is required, and can be imported.
##### ES Module

```js
import manifestJSON from '__STATIC_CONTENT_MANIFEST'
let manifest = JSON.parse(manifestJSON)
manifest['index.html'] = 'index.special.html'
import manifestJSON from "__STATIC_CONTENT_MANIFEST";
let manifest = JSON.parse(manifestJSON);
manifest["index.html"] = "index.special.html";

return getAssetFromKV(
{
request,
waitUntil(promise) {
return ctx.waitUntil(promise)
return ctx.waitUntil(promise);
},
},
{
ASSET_MANIFEST: manifest,
// ...
},
)
}
);
```

##### Service Worker
Expand Down Expand Up @@ -329,7 +337,7 @@ To turn `etags` **off**, you must bypass cache:
/* Turn etags off */
let cacheControl = {
bypassCache: true,
}
};
```

#### Syntax and comparison context
Expand Down
Loading

0 comments on commit 2e01544

Please sign in to comment.