-
Notifications
You must be signed in to change notification settings - Fork 351
/
Copy pathURIHandler.ts
82 lines (74 loc) · 2.19 KB
/
URIHandler.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
interface LibURIHandler {
tag: "lib";
pkg: string;
version?: string;
import: string;
}
type SplitArgMethod = {
tag: "split";
separator: RegExp | string;
};
type SingleArgMethod = {
tag: "single";
};
interface AppURIHandler {
tag: "app";
pkg: string;
method: SplitArgMethod | SingleArgMethod;
}
interface URIHandlerOptions {
handler: LibURIHandler | AppURIHandler;
prefix?: string;
}
class URIHandlerAPI {
// Handles a URI like "protocol:something/etc" by opening the appropriate app or library.
async handle(uri: string): Promise<void> {
// const url = new URL(uri);
// const protocol = url.protocol.slice(0, -1);
const [protocol, ...path] = uri.split(":");
const pathname = path.join(":");
const handlers = anura.settings.get("URIHandlers") || {};
const handler = handlers[protocol as string];
if (!handler) {
throw new Error(`No handler for URI protocol ${protocol}`);
}
if (handler.handler.tag === "lib") {
let lib;
if (handler.handler.version) {
lib = await anura.import(
handler.handler.pkg + "@" + handler.handler.version,
);
} else {
lib = await anura.import(handler.handler.pkg);
}
await lib[handler.handler.import]((handler.prefix || "") + pathname);
} else if (handler.handler.tag === "app") {
const app = handler.handler;
if (app.method.tag === "split") {
const args = pathname.split(app.method.separator);
await anura.apps[app.pkg].open(
handler.prefix ? [handler.prefix, ...args] : args,
);
} else {
await anura.apps[app.pkg].open((handler.prefix || "") + pathname);
}
}
}
// Sets a handler for a URI protocol.
set(protocol: string, options: URIHandlerOptions): void {
const handlers = anura.settings.get("URIHandlers") || {};
handlers[protocol] = options;
anura.settings.set("URIHandlers", handlers);
}
// Removes a handler for a URI protocol.
remove(protocol: string): void {
const handlers = anura.settings.get("URIHandlers") || {};
delete handlers[protocol];
anura.settings.set("URIHandlers", handlers);
}
// Determines if a handler is set for a URI protocol.
has(protocol: string): boolean {
const handlers = anura.settings.get("URIHandlers") || {};
return !!handlers[protocol];
}
}