Skip to content

Commit

Permalink
Update to latest official deno_cache rather than a patched version
Browse files Browse the repository at this point in the history
Update tests to use Deno.serve and latest std lib
  • Loading branch information
jollytoad committed Oct 31, 2023
1 parent fedb2ec commit bfb721d
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 33 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.vscode
deno.lock
22 changes: 9 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# deno_import_content

[![deno doc](https://doc.deno.land/badge.svg)](https://doc.deno.land/https://deno.land/x/import_content/mod.ts)
[![deno doc](https://doc.deno.land/badge.svg)](https://deno.land/x/import_content/mod.ts)

Import arbitrary file content in Deno using module resolution algorithms.

Expand All @@ -24,6 +24,10 @@ This module makes use of [`deno_cache`](https://deno.land/x/deno_cache) to
perform fetching, and therefore requires all permissions that it requires. See
those [docs](https://deno.land/x/deno_cache#permissions) for full details.

Although, `--allow-write` permission for the cache dir is optional. If it is
granted, then remote content will be cached, otherwise it will be fetched every
time if the content isn't already present in the cache.

## Example

```ts
Expand All @@ -41,16 +45,8 @@ const localReadme = await importText(import.meta.resolve("./README.md"));
const mappedReadme = await importText("bare/README.md");
```

## Known Bugs

Authentication via `DENO_AUTH_TOKENS` currently doesn't work due to a bug in
`deno_cache`. There is a [PR](https://github.com/denoland/deno_cache/pull/18) to
fix this, currently awaiting review.

There is also a bug in `deno_cache` that requires the `--allow-write` permission
to be given. There is another
[PR](https://github.com/denoland/deno_cache/pull/21) for this too.
## Fixed Dependencies

Until those issues are solved, this module will depend upon a patched fork of
`deno_cache` imported directly from my own
[repository](https://github.com/jollytoad/deno_cache/tree/fixes).
Previous versions of this module depended on my patched version of `deno_cache`,
due to bugs in that module. These have since been fixed, so this module now
depends on the official [`deno_cache`](https://deno.land/x/deno_cache@0.6.1).
3 changes: 2 additions & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"tasks": {
"test": "deno test --allow-read --allow-write --allow-net --allow-env --import-map=test/import_map.json"
"test": "deno test --allow-read --allow-write --allow-net --allow-env --import-map=test/import_map.json",
"check": "deno fmt && deno lint && deno check **/*.ts"
}
}
4 changes: 2 additions & 2 deletions deps.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { DenoDir } from "https://raw.githubusercontent.com/jollytoad/deno_cache/fixes/deno_dir.ts";
export { FileFetcher } from "https://raw.githubusercontent.com/jollytoad/deno_cache/fixes/file_fetcher.ts";
export { DenoDir } from "https://deno.land/x/deno_cache@0.6.1/deno_dir.ts";
export { FileFetcher } from "https://deno.land/x/deno_cache@0.6.1/file_fetcher.ts";
7 changes: 6 additions & 1 deletion mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ let fileFetcher: FileFetcher | undefined;
export async function importText(specifier: string): Promise<string> {
if (!fileFetcher) {
const denoDir = new DenoDir();
fileFetcher = new FileFetcher(denoDir.deps);
const writeGranted =
(await Deno.permissions.query({ name: "write", path: denoDir.root }))
.state === "granted";
fileFetcher = new FileFetcher(
denoDir.createHttpCache({ readOnly: !writeGranted }),
);
}

if (isRelative(specifier)) {
Expand Down
11 changes: 5 additions & 6 deletions test/deps.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export {
assertEquals,
assertRejects,
assertStringIncludes,
} from "https://deno.land/[email protected]/testing/asserts.ts";
export { generate as randomString } from "https://deno.land/[email protected]/uuid/v1.ts";
export { assertEquals } from "https://deno.land/[email protected]/assert/assert_equals.ts";
export { assertRejects } from "https://deno.land/[email protected]/assert/assert_rejects.ts";
export { assertStringIncludes } from "https://deno.land/[email protected]/assert/assert_string_includes.ts";

export { ulid as randomString } from "https://deno.land/[email protected]/ulid/mod.ts";
15 changes: 5 additions & 10 deletions test/with_server.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
import {
type Handler,
serve,
type ServeInit,
} from "https://deno.land/[email protected]/http/mod.ts";

/**
* Create a basic HTTP server to run a test against.
*
Expand All @@ -13,16 +7,17 @@ import {
* @returns a test function that can be passed directly to `Deno.test`
*/
export const withServer = (
handler: Handler,
opts: Pick<ServeInit, "hostname" | "port">,
handler: Deno.ServeHandler,
opts: Pick<Deno.ServeOptions, "hostname" | "port">,
test: (url: string, t: Deno.TestContext) => void | Promise<void>,
) =>
async (t: Deno.TestContext) => {
const controller = new AbortController();

let caught: unknown;

await serve(handler, {
await Deno.serve({
handler,
...opts,
signal: controller.signal,
onListen: ({ hostname, port }) => {
Expand All @@ -36,7 +31,7 @@ async (t: Deno.TestContext) => {
}
});
},
});
}).finished;

if (caught) {
throw caught;
Expand Down

0 comments on commit bfb721d

Please sign in to comment.