-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprotocol.ts
129 lines (115 loc) · 3.89 KB
/
protocol.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
/*
* Copyright (C) 2024 Delusoire
* SPDX-License-Identifier: GPL-3.0-or-later
*/
import type { ModuleInstance, ModuleBase } from "./module.ts";
// @deno-types="./static.ts"
import { LOCAL_PROXY_HOST } from "./static.js";
// @deno-types="./util.ts"
import { stringifyUrlSearchParams } from "./util.js";
const workerProtocol = "https://bespoke.delusoire.workers.dev/protocol/";
const websocketUrl = `ws://${LOCAL_PROXY_HOST}/rpc`;
const protocol = "spicetify:";
export const isOpen = () => daemonConn;
let daemonConn: WebSocket | undefined;
let lastDeamonConnAttempt = 0;
function tryConnectToDaemon() {
const timestamp = Date.now();
if (timestamp - lastDeamonConnAttempt < 5 * 60 * 1000) {
return Promise.resolve();
}
lastDeamonConnAttempt = timestamp;
const ws = new WebSocket(websocketUrl);
const { promise, resolve, reject } = Promise.withResolvers<Event>();
ws.onopen = (e) => resolve(e);
ws.onclose = (e) => reject(e);
return promise
.then(() => {
daemonConn = ws;
})
.catch(() => {
daemonConn = undefined;
});
}
tryConnectToDaemon();
export const nsUrlHandlers = new Map<string, (m: string) => void>();
const sendProtocolMessage = async (action: string, options: Record<string, string | string[]>) => {
const { promise, resolve, reject } = Promise.withResolvers<boolean>();
const hash = crypto.randomUUID();
const frame = protocol + hash + ":";
const uri = frame + action + "?" + stringifyUrlSearchParams(options);
let cancelSubscription: () => void;
const handleIncomingMessage = (m: string) => {
if (m.startsWith(frame)) {
const payload = m.slice(frame.length);
resolve(payload === "1");
cancelSubscription();
}
};
daemonConn ?? (await tryConnectToDaemon());
if (daemonConn) {
daemonConn.send(uri);
const listener = (e: any) => handleIncomingMessage(e.data);
cancelSubscription = () => daemonConn?.removeEventListener("message", listener);
daemonConn.addEventListener("message", listener);
} else {
open(workerProtocol + uri);
cancelSubscription = () => nsUrlHandlers.delete(hash);
nsUrlHandlers.set(hash, handleIncomingMessage);
}
setTimeout(() => {
reject(new Error("RPC timed out"));
cancelSubscription();
}, 30000);
return promise;
};
export const ModuleManager = {
async add(instance: ModuleInstance) {
return await sendProtocolMessage("add", {
id: instance.getIdentifier(),
artifacts: instance.artifacts,
checksum: instance.checksum,
});
},
async install(instance: ModuleInstance) {
return await sendProtocolMessage("install", { id: instance.getIdentifier() });
},
async fastInstall(instance: ModuleInstance) {
return await sendProtocolMessage("fast-install", {
id: instance.getIdentifier(),
artifacts: instance.artifacts,
checksum: instance.checksum,
});
},
async enable(instance: ModuleInstance) {
return await sendProtocolMessage("enable", { id: instance.getIdentifier() });
},
async fastEnable(instance: ModuleInstance) {
return await sendProtocolMessage("fast-enable", {
id: instance.getIdentifier(),
artifacts: instance.artifacts,
checksum: instance.checksum,
});
},
async disable(module: ModuleBase<any>) {
return await sendProtocolMessage("enable", { id: `${module.getIdentifier()}@` });
},
async delete(instance: ModuleInstance) {
return await sendProtocolMessage("delete", { id: instance.getIdentifier() });
},
async fastDelete(instance: ModuleInstance) {
return await sendProtocolMessage("fast-delete", { id: instance.getIdentifier() });
},
async remove(instance: ModuleInstance) {
return await sendProtocolMessage("remove", { id: instance.getIdentifier() });
},
async fastRemove(instance: ModuleInstance) {
return await sendProtocolMessage("fast-remove", { id: instance.getIdentifier() });
},
};
export function handleProtocol(uri: string) {
const uuidStart = "spicetify:".length;
const uuidLength = 36;
const hash = uri.slice(uuidStart, uuidStart + uuidLength);
nsUrlHandlers.get(hash)?.(uri);
}