-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodule.js
84 lines (71 loc) · 1.91 KB
/
module.js
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
const {
env,
errors: {
AddrInUse
},
listenDatagram
} = Deno;
const setupConnection = (port) => listenDatagram({ transport: "udp", port });
const defaultAnybarPort = Number(env.get("ANYBAR_PORT")) || 1738;
const minimumRegisteredPort = 2 ** 10;
const minimumDynamicPort = (2 ** 15) + (2 ** 14);
const maximumPort = (2 ** 16) - 1;
/**
* @param {(
* "white"|
* "red"|
* "orange"|
* "yellow"|
* "green"|
* "cyan"|
* "blue"|
* "purple"|
* "black"|
* "question"|
* "exclamation"|
* "filled"|
* "hollow"
* )} style - The style of the dot you want AnyBar to display.
* @param {object} [ports={anybar:Number(Deno.env.get("ANYBAR_PORT"))||1738}] - The port options.
* @param {number} [ports.anybar=Number(Deno.env.get("ANYBAR_PORT"))||1738]
* @param {number} [deno]
*
* @returns {Promise<void>}
*/
export default async (style, {
anybar = defaultAnybarPort,
deno
} = {
anybar: defaultAnybarPort
}) => {
let currentDenoPort = deno;
let connection;
if (typeof currentDenoPort === "undefined") {
for (currentDenoPort = minimumDynamicPort; currentDenoPort <= maximumPort; currentDenoPort++) {
try {
connection = setupConnection(currentDenoPort);
break;
} catch (error) {
if (!(error instanceof AddrInUse)) {
throw error;
}
}
}
if (currentDenoPort > maximumPort) {
throw new RangeError(`No free ports in range ${minimumDynamicPort}-${maximumPort}. How? 🤯`);
}
}
else {
if (deno < minimumRegisteredPort) {
throw new RangeError(`Port numbers below ${minimumRegisteredPort}, like ${deno}, are not supported.`);
}
if (deno > maximumPort) {
throw new RangeError(`Port numbers above ${maximumPort}, like ${deno}, are invalid.`);
}
connection = setupConnection(currentDenoPort);
}
const encoder = new TextEncoder();
const message = encoder.encode(style);
await connection.send(message, { transport: "udp", port: anybar });
connection.close();
}