From bfb721d436abdf6ee26d58c3934ac375025d310b Mon Sep 17 00:00:00 2001 From: Mark Gibson Date: Tue, 31 Oct 2023 11:27:32 +0000 Subject: [PATCH] Update to latest official deno_cache rather than a patched version Update tests to use Deno.serve and latest std lib --- .gitignore | 1 + README.md | 22 +++++++++------------- deno.json | 3 ++- deps.ts | 4 ++-- mod.ts | 7 ++++++- test/deps.ts | 11 +++++------ test/with_server.ts | 15 +++++---------- 7 files changed, 30 insertions(+), 33 deletions(-) diff --git a/.gitignore b/.gitignore index 722d5e7..1331696 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .vscode +deno.lock diff --git a/README.md b/README.md index c350896..8d11aa7 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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 @@ -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). diff --git a/deno.json b/deno.json index 41f6e14..50a0a76 100644 --- a/deno.json +++ b/deno.json @@ -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" } } diff --git a/deps.ts b/deps.ts index 8933f4b..f1fd3b1 100644 --- a/deps.ts +++ b/deps.ts @@ -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"; diff --git a/mod.ts b/mod.ts index 633dd01..0652b80 100644 --- a/mod.ts +++ b/mod.ts @@ -13,7 +13,12 @@ let fileFetcher: FileFetcher | undefined; export async function importText(specifier: string): Promise { 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)) { diff --git a/test/deps.ts b/test/deps.ts index 4cbd106..17ec536 100644 --- a/test/deps.ts +++ b/test/deps.ts @@ -1,6 +1,5 @@ -export { - assertEquals, - assertRejects, - assertStringIncludes, -} from "https://deno.land/std@0.158.0/testing/asserts.ts"; -export { generate as randomString } from "https://deno.land/std@0.158.0/uuid/v1.ts"; +export { assertEquals } from "https://deno.land/std@0.204.0/assert/assert_equals.ts"; +export { assertRejects } from "https://deno.land/std@0.204.0/assert/assert_rejects.ts"; +export { assertStringIncludes } from "https://deno.land/std@0.204.0/assert/assert_string_includes.ts"; + +export { ulid as randomString } from "https://deno.land/std@0.204.0/ulid/mod.ts"; diff --git a/test/with_server.ts b/test/with_server.ts index f1c3073..0fc7001 100644 --- a/test/with_server.ts +++ b/test/with_server.ts @@ -1,9 +1,3 @@ -import { - type Handler, - serve, - type ServeInit, -} from "https://deno.land/std@0.158.0/http/mod.ts"; - /** * Create a basic HTTP server to run a test against. * @@ -13,8 +7,8 @@ import { * @returns a test function that can be passed directly to `Deno.test` */ export const withServer = ( - handler: Handler, - opts: Pick, + handler: Deno.ServeHandler, + opts: Pick, test: (url: string, t: Deno.TestContext) => void | Promise, ) => async (t: Deno.TestContext) => { @@ -22,7 +16,8 @@ async (t: Deno.TestContext) => { let caught: unknown; - await serve(handler, { + await Deno.serve({ + handler, ...opts, signal: controller.signal, onListen: ({ hostname, port }) => { @@ -36,7 +31,7 @@ async (t: Deno.TestContext) => { } }); }, - }); + }).finished; if (caught) { throw caught;