-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
mod.ts
159 lines (155 loc) Β· 4.65 KB
/
mod.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/**
* Plug is a drop in extension for using remote dynamic libraries in deno. It
* automatically handles caching and loading with minimal overhead. It can
* automatically create the URL for your cross-operating-system, cross-architecture
* libraries if you so wish using a simple configuration which deviates from
* the standard URL/string path input.
*
* @example
* ```ts
* import { dlopen } from "@denosaurs/plug";
*
* // Drop-in replacement for `Deno.dlopen` which fetches the following depending
* // on operating system:
* // * darwin: "https://example.com/some/path/libexample.dylib"
* // * windows: "https://example.com/some/path/example.dll"
* // * linux: "https://example.com/some/path/libexample.so"
* const library = await dlopen("https://example.com/some/path/", {
* noop: { parameters: [], result: "void" },
* });
*
* library.symbols.noop();
* ```
*
* @example
* ```ts
* import { dlopen, FetchOptions } from "@denosaurs/plug";
*
* // If you want plug to guess your binary names
* const options: FetchOptions = {
* name: "example",
* url: "https://example.com/some/path/",
* // Becomes:
* // darwin: "https://example.com/some/path/libexample.dylib"
* // windows: "https://example.com/some/path/example.dll"
* // linux: "https://example.com/some/path/libexample.so"
* };
*
* const library = await dlopen(options, {
* noop: { parameters: [], result: "void" },
* });
*
* library.symbols.noop();
* ```
*
* @example
* ```ts
* import { dlopen, FetchOptions } from "@denosaurs/plug";
*
* // Also you can specify the path for certain architecture
* const options: FetchOptions = {
* name: "example",
* url: {
* darwin: {
* aarch64: `https://example.com/some/path/libexample.aarch64.dylib`,
* x86_64: `https://example.com/some/path/libexample.x86_64.dylib`,
* },
* windows: `https://example.com/some/path/example.dll`,
* linux: `https://example.com/some/path/libexample.so`,
* },
* };
*
* await dlopen(options, {});
* ```
*
* @example
* ```ts
* import { dlopen, FetchOptions } from "@denosaurs/plug";
*
* // Or even configure plug to automatically guess the binary names for you,
* // even when there are special rules for naming on specific architectures
* const options: FetchOptions = {
* name: "test",
* url: "https://example.com/some/path/",
* suffixes: {
* darwin: {
* aarch64: ".aarch64",
* x86_64: ".x86_64",
* },
* },
* // Becomes:
* // darwin-aarch64: "https://example.com/some/path/libexample.aarch64.dylib"
* // darwin-x86_64: "https://example.com/some/path/libexample.x86_64.dylib"
* };
*
* await dlopen(options, {});
* ```
*
* @module
*/
import { download } from "./download.ts";
import type { FetchOptions } from "./types.ts";
export type {
ArchRecord,
CacheLocation,
CacheOptions,
CacheSetting,
CrossOptions,
FetchOptions,
NamedOptions,
NestedCrossRecord,
OsRecord,
URLOptions,
} 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
* remote (or local) binaries, automatically building the binary name and
* controlling the caching policy.
*
* @example
* ```ts
* import { dlopen, FetchOptions } from "@denosaurs/plug";
*
* // Configure plug to automatically guess the binary names for you, even when
* // there for example are special rules for naming on specific architectures
* const options: FetchOptions = {
* name: "test",
* url: "https://example.com/some/path/",
* suffixes: {
* darwin: {
* aarch64: ".aarch64",
* x86_64: ".x86_64",
* },
* },
* // Becomes:
* // darwin-aarch64: "https://example.com/some/path/libexample.aarch64.dylib"
* // darwin-x86_64: "https://example.com/some/path/libexample.x86_64.dylib"
* };
*
* await dlopen(options, {});
* ```
*
* @param options See {@link FetchOptions}
* @param symbols A record extending {@link Deno.ForeignLibraryInterface}
* @returns An opened {@link Deno.DynamicLibrary}
*/
export async function dlopen<S extends Deno.ForeignLibraryInterface>(
options: FetchOptions,
symbols: Const<S>,
): Promise<Deno.DynamicLibrary<S>> {
if (Deno.dlopen === undefined) {
throw new Error("`--unstable-ffi` is required");
}
// deno-lint-ignore no-explicit-any
return Deno.dlopen<S>(await download(options), symbols as any);
}