diff --git a/download.ts b/download.ts index 9e7a660..7514f20 100644 --- a/download.ts +++ b/download.ts @@ -7,7 +7,6 @@ import { join, normalize, resolve, - toFileUrl, } from "./deps.ts"; import { ArchRecord, @@ -16,7 +15,13 @@ import { NestedCrossRecord, OsRecord, } from "./types.ts"; -import { cacheDir, denoCacheDir, isFile, urlToFilename } from "./util.ts"; +import { + cacheDir, + denoCacheDir, + isFile, + stringToURL, + urlToFilename, +} from "./util.ts"; export const defaultExtensions: OsRecord = { darwin: "dylib", @@ -99,9 +104,7 @@ export function createDownloadURL(options: FetchOptions): URL { if (options.url instanceof URL) { url = options.url; } else if (typeof options.url === "string") { - url = options.url.startsWith("file://") - ? new URL(options.url) - : toFileUrl(resolve(options.url)); + url = stringToURL(options.url); } else { const tmpUrl = getCrossOption(options.url); if (tmpUrl === undefined) { @@ -111,9 +114,7 @@ export function createDownloadURL(options: FetchOptions): URL { } if (typeof tmpUrl === "string") { - url = tmpUrl.startsWith("file://") - ? new URL(tmpUrl) - : toFileUrl(resolve(tmpUrl)); + url = stringToURL(tmpUrl); } else { url = tmpUrl; } @@ -244,10 +245,10 @@ export async function download(options: FetchOptions): Promise { } } - const file = await Deno.create(cacheFilePath); - await response.body!.pipeTo(file.writable); - file.close(); - + await Deno.writeFile( + cacheFilePath, + new Uint8Array(await response.arrayBuffer()), + ); break; } diff --git a/download_test.ts b/download_test.ts index 18d6bef..5439f2a 100644 --- a/download_test.ts +++ b/download_test.ts @@ -22,23 +22,63 @@ async function isDirectory(filePath: string): Promise { } Deno.test("createDownloadURL", async ({ step }) => { - await step("string", () => { - const path = "./test/example"; - const result = createDownloadURL(path); - assert(result.pathname.endsWith("/test/example")); + await step("string", async ({ step }) => { + await step("relative", () => { + const path = "./test/example"; + const result = createDownloadURL(path); + assertEquals( + result.toString(), + new URL("./test/example", import.meta.url).toString(), + ); + }); + + await step("file", () => { + const path = "file:///test/example"; + const result = createDownloadURL(path); + assertEquals(result.toString(), "file:///test/example"); + }); + + await step("http", () => { + const path = "http://example.com/example"; + const result = createDownloadURL(path); + assertEquals(result.toString(), "http://example.com/example"); + }); + + await step("https", () => { + const path = "https://example.com/example"; + const result = createDownloadURL(path); + assertEquals(result.toString(), "https://example.com/example"); + }); }); - await step("URL", () => { - const path = new URL("file://./test/example"); - const result = createDownloadURL(path); - assertEquals(result, path); + await step("URL", async ({ step }) => { + await step("file", () => { + const path = new URL("file:///test/example"); + const result = createDownloadURL(path); + assertEquals(result.toString(), "file:///test/example"); + }); + + await step("http", () => { + const path = new URL("http://example.com/example"); + const result = createDownloadURL(path); + assertEquals(result.toString(), "http://example.com/example"); + }); + + await step("https", () => { + const path = new URL("https://example.com/example"); + const result = createDownloadURL(path); + assertEquals(result.toString(), "https://example.com/example"); + }); }); await step("URLOptions", async ({ step }) => { await step("string", () => { const path = "./test/example"; const result = createDownloadURL({ url: path }); - assert(result.pathname.endsWith("/test/example")); + assertEquals( + result.toString(), + new URL("./test/example", import.meta.url).toString(), + ); }); await step("URL", () => { diff --git a/util.ts b/util.ts index 659814f..cca5b5f 100644 --- a/util.ts +++ b/util.ts @@ -1,4 +1,11 @@ -import { hex, isAbsolute, join, normalize } from "./deps.ts"; +import { + hex, + isAbsolute, + join, + normalize, + resolve, + toFileUrl, +} from "./deps.ts"; export const encoder = new TextEncoder(); export const decoder = new TextDecoder(); @@ -29,6 +36,15 @@ function baseUrlToFilename(url: URL): string { return join(...out); } +export function stringToURL(url: string): URL { + // deno-fmt-ignore + return url.startsWith("file://") + || url.startsWith("http://") + || url.startsWith("https://") + ? new URL(url) + : toFileUrl(resolve(url)); +} + export async function hash(value: string): Promise { return decoder.decode( hex( diff --git a/util_test.ts b/util_test.ts index 1e7a4a9..21e9341 100644 --- a/util_test.ts +++ b/util_test.ts @@ -15,6 +15,7 @@ import { hash, homeDir, isFile, + stringToURL, urlToFilename, } from "./util.ts"; @@ -25,6 +26,35 @@ Deno.test("hash", async () => { ); }); +Deno.test("stringToURL", async ({ step }) => { + await step("relative", () => { + const path = "./test/example"; + const result = stringToURL(path); + assertEquals( + result.toString(), + new URL("./test/example", import.meta.url).toString(), + ); + }); + + await step("file", () => { + const path = "file:///test/example"; + const result = stringToURL(path); + assertEquals(result.toString(), "file:///test/example"); + }); + + await step("http", () => { + const path = "http://example.com/example"; + const result = stringToURL(path); + assertEquals(result.toString(), "http://example.com/example"); + }); + + await step("https", () => { + const path = "https://example.com/example"; + const result = stringToURL(path); + assertEquals(result.toString(), "https://example.com/example"); + }); +}); + Deno.test("urlToFilename", async ({ step }) => { await step("http", async () => { assertEquals(