-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
server_settings.ts
150 lines (124 loc) · 4.6 KB
/
server_settings.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
import { parseShareAddress } from "../../core-validators/addresses.ts";
import { Crypto } from "../../crypto/crypto.ts";
import { Peer } from "../../peer/peer.ts";
import {
extractTemplateVariablesFromPath,
globToQueryAndRegex,
parseTemplate,
} from "../../query/query-helpers.ts";
import { Replica } from "../../replica/replica.ts";
import { ShareAddress } from "../../util/doc-types.ts";
import { notErr } from "../../util/errors.ts";
import { IServerExtension } from "./extension.ts";
interface ExtensionServerSettingsOpts {
/** The address of the share to read settings from. */
settingsShare: ShareAddress;
/** The method used to create the replica for the settings share. */
onCreateReplica: (
settingsShareAddress: string,
) => Replica;
}
const HOSTED_SHARE_TEMPLATE =
`/server-settings/1.*/shares/{shareHash}/{hostType}`;
/** A server extension which reads settings from a specified share, e.g. which shares to host on the server. Settings are modified by other peers syncing with this server. */
export class ExtensionServerSettings implements IServerExtension {
private configReplica: Replica;
private onCreateReplica: (
configurationShareAddress: string,
) => Replica;
constructor(opts: ExtensionServerSettingsOpts) {
this.configReplica = opts.onCreateReplica(opts.settingsShare);
this.onCreateReplica = opts.onCreateReplica;
}
register(peer: Peer): Promise<void> {
peer.addReplica(this.configReplica);
const onCreateReplica = this.onCreateReplica;
const { glob } = parseTemplate(HOSTED_SHARE_TEMPLATE);
const { query, regex } = globToQueryAndRegex(glob);
const configShareAddress = this.configReplica.share;
this.configReplica.getQueryStream(query, "everything").pipeTo(
new WritableStream({
async write(event) {
if (event.kind === "existing" || event.kind === "success") {
if (
regex != null &&
new RegExp(regex).test(event.doc.path)
) {
const pathVariables = extractTemplateVariablesFromPath(
HOSTED_SHARE_TEMPLATE,
event.doc.path,
);
if (!pathVariables) {
return;
}
const shareHash = pathVariables["shareHash"];
const configHash = await Crypto.sha256base32(
configShareAddress,
);
if (shareHash === configHash) {
return;
}
if (
event.doc.text.length > 0 &&
notErr(parseShareAddress(event.doc.text)) &&
!peer.hasShare(event.doc.text)
) {
// Add
const replica = onCreateReplica(event.doc.text);
peer.addReplica(replica);
console.log("Server settings:", `now hosting ${replica.share}`);
} else if (event.doc.text.length === 0) {
let replicaToClose;
for (const replica of peer.replicas()) {
const rShareHash = await Crypto.sha256base32(
replica.share,
);
if (rShareHash === shareHash) {
replicaToClose = replica;
}
}
if (replicaToClose) {
await replicaToClose.close(true);
peer.removeReplica(replicaToClose);
console.log(
"Server settings:",
`stopped hosting ${replicaToClose.share}`,
);
}
}
}
}
if (event.kind === "expire") {
if (
regex != null &&
new RegExp(regex).test(event.doc.path)
) {
// Remove
const pathVariables = extractTemplateVariablesFromPath(
HOSTED_SHARE_TEMPLATE,
event.doc.path,
);
if (!pathVariables) {
return;
}
const shareAddress = pathVariables["shareAddress"];
const replicaToClose = peer.getReplica(shareAddress);
if (replicaToClose) {
await replicaToClose.close(true);
peer.removeReplica(replicaToClose);
console.log(
"Server settings:",
`stopped hosting ${replicaToClose.share}`,
);
}
}
}
},
}),
);
return Promise.resolve();
}
handler(_req: Request): Promise<Response | null> {
return Promise.resolve(null);
}
}