Skip to content

Commit

Permalink
fix: remote url options and bad resource id caused by streaming (#19)
Browse files Browse the repository at this point in the history
Co-authored-by: Dean Srebnik <load1n9@users.noreply.github.com>
  • Loading branch information
eliassjogreen and load1n9 authored Sep 30, 2022
1 parent 376dee8 commit 7e85fa9
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 22 deletions.
25 changes: 13 additions & 12 deletions download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
join,
normalize,
resolve,
toFileUrl,
} from "./deps.ts";
import {
ArchRecord,
Expand All @@ -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<string> = {
darwin: "dylib",
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
}
Expand Down Expand Up @@ -244,10 +245,10 @@ export async function download(options: FetchOptions): Promise<string> {
}
}

const file = await Deno.create(cacheFilePath);
await response.body!.pipeTo(file.writable);
file.close();

await Deno.writeFile(
cacheFilePath,
new Uint8Array(await response.arrayBuffer()),
);
break;
}

Expand Down
58 changes: 49 additions & 9 deletions download_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,63 @@ async function isDirectory(filePath: string): Promise<boolean> {
}

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", () => {
Expand Down
18 changes: 17 additions & 1 deletion util.ts
Original file line number Diff line number Diff line change
@@ -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();
Expand Down Expand Up @@ -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<string> {
return decoder.decode(
hex(
Expand Down
30 changes: 30 additions & 0 deletions util_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
hash,
homeDir,
isFile,
stringToURL,
urlToFilename,
} from "./util.ts";

Expand All @@ -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(
Expand Down

0 comments on commit 7e85fa9

Please sign in to comment.