Skip to content

Commit

Permalink
fix: typings (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
eliassjogreen authored Feb 3, 2023
1 parent dcda7cd commit 62a0504
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
8 changes: 4 additions & 4 deletions deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export {
normalize,
resolve,
toFileUrl,
} from "https://deno.land/std@0.173.0/path/mod.ts";
export { ensureDir } from "https://deno.land/std@0.173.0/fs/mod.ts";
export { encode as hex } from "https://deno.land/std@0.173.0/encoding/hex.ts";
export * as colors from "https://deno.land/std@0.173.0/fmt/colors.ts";
} from "https://deno.land/std@0.176.0/path/mod.ts";
export { ensureDir } from "https://deno.land/std@0.176.0/fs/mod.ts";
export { encode as hex } from "https://deno.land/std@0.176.0/encoding/hex.ts";
export { green } from "https://deno.land/std@0.176.0/fmt/colors.ts";
6 changes: 3 additions & 3 deletions download.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {
colors,
dirname,
ensureDir,
extname,
fromFileUrl,
green,
join,
normalize,
resolve,
Expand Down Expand Up @@ -232,7 +232,7 @@ export async function download(options: FetchOptions): Promise<string> {
switch (url.protocol) {
case "http:":
case "https:": {
console.log(`${colors.green("Downloading")} ${url}`);
console.log(`${green("Downloading")} ${url}`);
const response = await fetch(url.toString());

if (!response.ok) {
Expand All @@ -253,7 +253,7 @@ export async function download(options: FetchOptions): Promise<string> {
}

case "file:": {
console.log(`${colors.green("Copying")} ${url}`);
console.log(`${green("Copying")} ${url}`);
await Deno.copyFile(fromFileUrl(url), cacheFilePath);
if (Deno.build.os !== "windows") {
await Deno.chmod(cacheFilePath, 0o644);
Expand Down
14 changes: 12 additions & 2 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,15 @@ export type {
} from "./types.ts";
export { download } from "./download.ts";

/* Magic types from deno which help implement better FFI type checking */
type Cast<A, B> = A extends B ? A : B;
type Const<T> = Cast<
T,
| (T extends string | number | bigint | boolean ? T : never)
| { [K in keyof T]: Const<T[K]> }
| []
>;

/**
* Opens a dynamic library and registers symbols, compatible with
* {@link Deno.dlopen} yet with extended functionality allowing you to use
Expand Down Expand Up @@ -140,7 +149,8 @@ export { download } from "./download.ts";
*/
export async function dlopen<S extends Deno.ForeignLibraryInterface>(
options: FetchOptions,
symbols: S,
symbols: Const<S>,
): Promise<Deno.DynamicLibrary<S>> {
return Deno.dlopen(await download(options), symbols);
// deno-lint-ignore no-explicit-any
return Deno.dlopen<S>(await download(options), symbols as any);
}
4 changes: 2 additions & 2 deletions test_deps.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from "https://deno.land/std@0.173.0/testing/asserts.ts";
export * from "https://deno.land/std@0.173.0/path/mod.ts";
export * from "https://deno.land/std@0.176.0/testing/asserts.ts";
export * from "https://deno.land/std@0.176.0/path/mod.ts";

0 comments on commit 62a0504

Please sign in to comment.