Skip to content

Commit

Permalink
Prep JSR release
Browse files Browse the repository at this point in the history
  • Loading branch information
jollytoad committed Apr 15, 2024
1 parent 059ce51 commit 6427ea7
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 24 deletions.
29 changes: 18 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
# deno_import_content

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

Import arbitrary file content in Deno using module resolution algorithms.

Expand All @@ -20,9 +18,8 @@ still, a PR.

## Permissions

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.
This module makes use of [`@deno/cache-dir`](https://jsr.io/@deno/cache-dir) to
perform fetching, and therefore requires all permissions that it requires.

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
Expand All @@ -31,7 +28,7 @@ time if the content isn't already present in the cache.
## Example

```ts
import { importText } from "https://deno.land/x/import_content/mod.ts";
import { importText } from "jsr:@jollytoad/import-content";

// Fetch the text content of a remote file
const remoteReadme = await importText(
Expand All @@ -45,8 +42,18 @@ const localReadme = await importText(import.meta.resolve("./README.md"));
const mappedReadme = await importText("bare/README.md");
```

## Fixed Dependencies
## Planned Obsolescence

Hopefully this entire package will become completely obsolete once Deno fully
supports importing of arbitrary files via import attributes...

```ts
const remoteReadme = await import(
"https://deno.land/x/import_content/README.md",
{ with: { type: "text" } }
);

const localReadme = await import("./README.md", { with: { type: "text" } });

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/[email protected]).
const mappedReadme = await import("bare/README.md", { with: { type: "text" } });
```
8 changes: 8 additions & 0 deletions deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
{
"name": "@jollytoad/import-content",
"version": "1.0.0",
"exports": "./mod.ts",
"tasks": {
"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"
},
"imports": {
"@deno/cache-dir": "jsr:@deno/[email protected]",
"@std/assert": "jsr:@std/[email protected]",
"@std/ulid": "jsr:@std/[email protected]"
}
}
2 changes: 0 additions & 2 deletions deps.ts

This file was deleted.

10 changes: 6 additions & 4 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DenoDir, FileFetcher } from "./deps.ts";
import { DenoDir, FileFetcher } from "@deno/cache-dir";

let fileFetcher: FileFetcher | undefined;

Expand All @@ -16,8 +16,8 @@ export async function importText(specifier: string): Promise<string> {
const writeGranted =
(await Deno.permissions.query({ name: "write", path: denoDir.root }))
.state === "granted";
fileFetcher = new FileFetcher(
denoDir.createHttpCache({ readOnly: !writeGranted }),
fileFetcher = new FileFetcher(() =>
denoDir.createHttpCache({ readOnly: !writeGranted })
);
}

Expand All @@ -31,7 +31,9 @@ export async function importText(specifier: string): Promise<string> {
const response = await fileFetcher.fetch(resolved);

if (response?.kind === "module") {
return response.content;
return typeof response.content === "string"
? response.content
: new TextDecoder().decode(response.content);
} else {
throw new TypeError(`Module content not found "${specifier.toString()}".`);
}
Expand Down
5 changes: 0 additions & 5 deletions test/deps.ts

This file was deleted.

5 changes: 4 additions & 1 deletion test/import_map.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"imports": {
"test/": "http://localhost:8910/"
"test/": "http://localhost:8910/",
"@std/assert": "jsr:@std/[email protected]",
"@std/ulid": "jsr:@std/[email protected]",
"@deno/cache-dir": "jsr:@deno/[email protected]"
}
}
3 changes: 2 additions & 1 deletion test/import_text_test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { __reset__, importText } from "../mod.ts";
import { assertEquals, assertRejects, randomString } from "./deps.ts";
import { assertEquals, assertRejects } from "@std/assert";
import { ulid as randomString } from "@std/ulid";
import { withServer } from "./with_server.ts";

Deno.env.set(
Expand Down

0 comments on commit 6427ea7

Please sign in to comment.