@pelican.ts/sdk 0.4.15 → 0.4.16-next.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,1096 +1,585 @@
1
- // src/api/client/account.ts
1
+ // src/api/application/database_hosts.ts
2
2
  import z from "zod";
3
- var Account = class {
3
+ var DatabaseHosts = class {
4
4
  r;
5
- constructor(requester) {
6
- this.r = requester;
5
+ constructor(r) {
6
+ this.r = r;
7
7
  }
8
- info = async () => {
9
- const { data } = await this.r.get("/account");
10
- return data.attributes;
8
+ list = async (page = 1) => {
9
+ const { data } = await this.r.get("/database-hosts", { params: { page } });
10
+ return data.data.map((d) => d.attributes);
11
11
  };
12
- updateEmail = async (newEmail, password) => {
13
- newEmail = z.email().parse(newEmail);
14
- await this.r.put("/account/email", { email: newEmail, password });
12
+ info = async (id) => {
13
+ const { data } = await this.r.get(`/database-hosts/${id}`);
14
+ return data.attributes;
15
15
  };
16
- updatePassword = async (newPassword) => {
17
- newPassword = z.string().min(8).parse(newPassword);
18
- await this.r.put("/account/password", {
19
- password: newPassword,
20
- password_confirmation: newPassword
16
+ // TODO: find out why API returns 500
17
+ create = async (opts) => {
18
+ opts = CreateDBHostSchema.parse(opts);
19
+ await this.r.post(
20
+ "/database-hosts",
21
+ opts
22
+ ).catch((e) => {
21
23
  });
22
24
  };
23
- apiKeys = {
24
- list: async () => {
25
- const { data } = await this.r.get("/account/api-keys");
26
- return data.data.map((k) => k.attributes);
27
- },
28
- create: async (description, allowed_ips) => {
29
- allowed_ips = z.array(z.ipv4()).optional().parse(allowed_ips);
30
- const { data } = await this.r.post("/account/api-keys", { description, allowed_ips });
31
- return { ...data.attributes, secret_token: data.meta.secret_token };
32
- },
33
- delete: async (identifier) => {
34
- await this.r.delete(`/account/api-keys/${identifier}`);
35
- }
25
+ update = async (id, opts) => {
26
+ opts = CreateDBHostSchema.parse(opts);
27
+ const { data } = await this.r.patch(`/database-hosts/${id}`, opts);
28
+ return data.attributes;
36
29
  };
37
- sshKeys = {
38
- list: async () => {
39
- const { data } = await this.r.get("/account/ssh-keys");
40
- return data.data.map((k) => k.attributes);
41
- },
42
- create: async (name, public_key) => {
43
- const { data } = await this.r.post("/account/ssh-keys", { name, public_key });
44
- return data.attributes;
45
- },
46
- delete: async (fingerprint) => {
47
- await this.r.delete(`/account/ssh-keys/${fingerprint}`);
48
- }
30
+ delete = async (id) => {
31
+ await this.r.delete(`/database-hosts/${id}`);
49
32
  };
50
33
  };
34
+ var CreateDBHostSchema = z.object({
35
+ name: z.string().min(1).max(255),
36
+ host: z.string(),
37
+ port: z.number().min(1).max(65535),
38
+ username: z.string().min(1).max(255),
39
+ password: z.string().optional(),
40
+ node_ids: z.array(z.string()).optional(),
41
+ max_databases: z.number().optional()
42
+ });
51
43
 
52
- // src/api/client/client.ts
53
- import z5 from "zod";
54
-
55
- // src/api/client/server_databases.ts
56
- import z2 from "zod";
57
- var ServerDatabases = class {
44
+ // src/api/application/eggs.ts
45
+ var Eggs = class {
58
46
  r;
59
- id;
60
- constructor(requester, id) {
61
- this.r = requester;
62
- this.id = id;
47
+ constructor(r) {
48
+ this.r = r;
63
49
  }
64
- list = async (include, page = 1) => {
65
- z2.number().positive().parse(page);
66
- const { data } = await this.r.get(`/servers/${this.id}/databases`, {
67
- params: { include: include?.join(","), page }
68
- });
50
+ list = async () => {
51
+ const { data } = await this.r.get(
52
+ "/eggs"
53
+ );
69
54
  return data.data.map((d) => d.attributes);
70
55
  };
71
- create = async (database, remote) => {
72
- const { data } = await this.r.post(`/servers/${this.id}/databases`, { database, remote });
56
+ info = async (id) => {
57
+ const { data } = await this.r.get(
58
+ `/eggs/${id}`
59
+ );
73
60
  return data.attributes;
74
61
  };
75
- rotatePassword = async (database_id) => {
76
- const { data } = await this.r.post(`/servers/${this.id}/databases/${database_id}/rotate-password`);
77
- return data.attributes;
62
+ export = async (id, format) => {
63
+ const { data } = await this.r.get(`/eggs/${id}/export`, {
64
+ params: { format },
65
+ transformResponse: (r) => r
66
+ });
67
+ return data;
78
68
  };
79
- delete = async (database_id) => {
80
- await this.r.delete(`/servers/${this.id}/databases/${database_id}`);
69
+ infoExportable = async (id) => {
70
+ const { data } = await this.r.get(`/eggs/${id}/export`, {
71
+ params: { format: "json" }
72
+ });
73
+ return data;
81
74
  };
82
75
  };
83
76
 
84
- // src/api/client/server_files.ts
85
- import axios from "axios";
86
- var ServerFiles = class {
77
+ // src/api/application/mounts.ts
78
+ import z2 from "zod";
79
+ var Mounts = class {
87
80
  r;
88
- id;
89
- constructor(requester, id) {
90
- this.r = requester;
91
- this.id = id;
81
+ constructor(r) {
82
+ this.r = r;
92
83
  }
93
- list = async (path) => {
94
- const { data } = await this.r.get(`/servers/${this.id}/files/list`, { params: { directory: path } });
95
- return data.data.map((r) => r.attributes);
84
+ list = async () => {
85
+ const { data } = await this.r.get("/mounts");
86
+ return data.data.map((d) => d.attributes);
96
87
  };
97
- /**
98
- * Return the contents of a file. To read binary file (non-editable) use {@link download} instead
99
- */
100
- contents = async (path) => {
88
+ info = async (id) => {
101
89
  const { data } = await this.r.get(
102
- `/servers/${this.id}/files/contents`,
103
- { params: { file: path } }
90
+ `/mounts/${id}`
104
91
  );
105
- return data;
106
- };
107
- downloadGetUrl = async (path) => {
108
- const { data } = await this.r.get(`/servers/${this.id}/files/download`, { params: { file: path } });
109
- return data.attributes.url;
92
+ return data.attributes;
110
93
  };
111
- download = async (path) => {
112
- const url = await this.downloadGetUrl(path);
113
- const { data } = await axios.get(url, {
114
- responseType: "arraybuffer"
115
- });
116
- return data;
94
+ create = async (opts) => {
95
+ opts = CreateMountSchema.parse(opts);
96
+ const { data } = await this.r.post(
97
+ "/mounts",
98
+ opts
99
+ );
100
+ return data.attributes;
117
101
  };
118
- rename = async (root = "/", files) => {
119
- await this.r.put(`/servers/${this.id}/files/rename`, { root, files });
102
+ update = async (id, opts) => {
103
+ opts = CreateMountSchema.parse(opts);
104
+ const { data } = await this.r.patch(
105
+ `/mounts/${id}`,
106
+ opts
107
+ );
108
+ return data.attributes;
120
109
  };
121
- copy = async (location) => {
122
- await this.r.post(`/servers/${this.id}/files/copy`, { location });
110
+ delete = async (id) => {
111
+ await this.r.delete(`/mounts/${id}`);
123
112
  };
124
- write = async (path, content) => {
125
- await this.r.post(`/servers/${this.id}/files/write`, content, {
126
- params: { file: path }
127
- });
113
+ listAssignedEggs = async (id) => {
114
+ const { data } = await this.r.get(`/mounts/${id}/eggs`);
115
+ return data.data.map((d) => d.attributes);
128
116
  };
129
- compress = async (root = "/", files, archive_name, extension) => {
130
- const { data } = await this.r.post(`/servers/${this.id}/files/compress`, {
131
- root,
132
- files,
133
- archive_name,
134
- extension
135
- });
136
- return data.attributes;
117
+ assignEggs = async (id, eggs) => {
118
+ await this.r.post(`/mounts/${id}/eggs`, { eggs });
137
119
  };
138
- decompress = async (root = "/", file) => {
139
- await this.r.post(`/servers/${this.id}/files/decompress`, { root, file });
120
+ unassignEgg = async (id, egg_id) => {
121
+ await this.r.delete(`/mounts/${id}/eggs/${egg_id}`);
140
122
  };
141
- delete = async (root = "/", files) => {
142
- await this.r.post(`/servers/${this.id}/files/delete`, { root, files });
123
+ listAssignedNodes = async (id) => {
124
+ const { data } = await this.r.get(`/mounts/${id}/nodes`);
125
+ return data.data.map((d) => d.attributes);
143
126
  };
144
- createFolder = async (root = "/", name) => {
145
- await this.r.post(`/servers/${this.id}/files/create-folder`, {
146
- root,
147
- name
148
- });
127
+ assignNodes = async (id, nodes) => {
128
+ await this.r.post(`/mounts/${id}/nodes`, { nodes });
149
129
  };
150
- chmod = async (root = "/", files) => {
151
- await this.r.post(`/servers/${this.id}/files/chmod`, { root, files });
130
+ unassignNode = async (id, node_id) => {
131
+ await this.r.delete(`/mounts/${id}/nodes/${node_id}`);
152
132
  };
153
- pullFromRemote = async (url, directory, filename, use_header = false, foreground = false) => {
154
- await this.r.post(`/servers/${this.id}/files/pull`, {
155
- url,
156
- directory,
157
- filename,
158
- use_header,
159
- foreground
160
- });
133
+ listAssignedServers = async (id) => {
134
+ const { data } = await this.r.get(`/mounts/${id}/servers`);
135
+ return data.data.map((d) => d.attributes);
161
136
  };
162
- uploadGetUrl = async () => {
163
- const { data } = await this.r.get(`/servers/${this.id}/files/upload`);
164
- return data.attributes.url;
137
+ assignServers = async (id, servers) => {
138
+ await this.r.post(`/mounts/${id}/servers`, { servers });
165
139
  };
166
- upload = async (file, root = "/") => {
167
- const url = await this.uploadGetUrl();
168
- await axios.post(
169
- url,
170
- { files: file },
171
- {
172
- headers: { "Content-Type": "multipart/form-data" },
173
- params: { directory: root }
174
- }
175
- );
140
+ unassignServer = async (id, server_id) => {
141
+ await this.r.delete(`/mounts/${id}/servers/${server_id}`);
176
142
  };
177
143
  };
144
+ var CreateMountSchema = z2.object({
145
+ name: z2.string().min(1).max(255),
146
+ description: z2.string().optional(),
147
+ source: z2.string(),
148
+ target: z2.string(),
149
+ read_only: z2.boolean().optional()
150
+ });
178
151
 
179
- // src/api/client/server_schedules.ts
180
- var ServerSchedules = class {
152
+ // src/api/application/nodes.ts
153
+ import z4 from "zod";
154
+
155
+ // src/api/application/nodes_allocations.ts
156
+ import z3 from "zod";
157
+ var NodesAllocations = class {
181
158
  r;
182
159
  id;
183
160
  constructor(requester, id) {
184
161
  this.r = requester;
185
162
  this.id = id;
186
163
  }
187
- list = async () => {
188
- const { data } = await this.r.get(`/servers/${this.id}/schedules`);
164
+ list = async (page = 1, per_page = 50, include) => {
165
+ const { data } = await this.r.get(`/nodes/${this.id}/allocations`, {
166
+ params: {
167
+ page,
168
+ per_page,
169
+ include: include?.join(",")
170
+ }
171
+ });
189
172
  return data.data.map((d) => d.attributes);
190
173
  };
191
- create = async (params) => {
192
- const { data } = await this.r.post(`/servers/${this.id}/schedules`, params);
193
- return data.attributes;
174
+ create = async (ip, ports, alias) => {
175
+ z3.ipv4().parse(ip);
176
+ z3.ipv4().or(z3.url().max(255)).optional().parse(alias);
177
+ z3.array(z3.number()).or(z3.string().regex(/\d+-\d+/)).parse(ports);
178
+ await this.r.post(`/nodes/${this.id}/allocations`, { ip, ports, alias });
179
+ };
180
+ delete = async (alloc_id) => {
181
+ await this.r.delete(`/nodes/${this.id}/allocations/${alloc_id}`);
194
182
  };
195
- control = (sched_id) => new ScheduleControl(this.r, this.id, sched_id);
196
183
  };
197
- var ScheduleControl = class {
184
+
185
+ // src/api/application/nodes.ts
186
+ var Nodes = class {
198
187
  r;
199
- id;
200
- sched_id;
201
- constructor(requester, id, sched_id) {
188
+ constructor(requester) {
202
189
  this.r = requester;
203
- this.id = id;
204
- this.sched_id = sched_id;
205
190
  }
206
- info = async () => {
207
- const { data } = await this.r.get(`/servers/${this.id}/schedules/${this.sched_id}`);
191
+ list = async (include, page = 1) => {
192
+ z4.number().positive().parse(page);
193
+ const { data } = await this.r.get("/nodes", { params: { include: include?.join(","), page } });
194
+ return data.data.map((s) => s.attributes);
195
+ };
196
+ listDeployable = async (filters, include, page = 1) => {
197
+ z4.number().positive().parse(page);
198
+ const { data } = await this.r.get("/nodes/deployable", {
199
+ params: {
200
+ include: include?.join(","),
201
+ disk: filters.disk,
202
+ memory: filters.memory,
203
+ cpu: filters.cpu,
204
+ location_ids: filters.location_ids,
205
+ tags: filters.tags,
206
+ page
207
+ }
208
+ });
209
+ return data.data.map((s) => s.attributes);
210
+ };
211
+ info = async (id, include) => {
212
+ z4.number().positive().parse(id);
213
+ const { data } = await this.r.get(
214
+ `/nodes/${id}`,
215
+ { params: { include: include?.join(",") } }
216
+ );
208
217
  return data.attributes;
209
218
  };
210
- update = async (params) => {
211
- const { data } = await this.r.post(`/servers/${this.id}/schedules/${this.sched_id}`, params);
219
+ create = async (node) => {
220
+ node = NodeCreateSchema.parse(node);
221
+ const { data } = await this.r.post(
222
+ "/nodes",
223
+ node
224
+ );
212
225
  return data.attributes;
213
226
  };
214
- delete = async () => {
215
- await this.r.delete(`/servers/${this.id}/schedules/${this.sched_id}`);
227
+ get_configuration = async (id) => {
228
+ z4.number().positive().parse(id);
229
+ const { data } = await this.r.get(
230
+ `/nodes/${id}/configuration`
231
+ );
232
+ return data;
216
233
  };
217
- execute = async () => {
218
- await this.r.post(
219
- `/servers/${this.id}/schedules/${this.sched_id}/execute`
234
+ update = async (id, node) => {
235
+ z4.number().positive().parse(id);
236
+ node = NodeCreateSchema.parse(node);
237
+ const { data } = await this.r.patch(
238
+ `/nodes/${id}`,
239
+ node
220
240
  );
241
+ return data.attributes;
221
242
  };
222
- tasks = {
223
- create: async (opts) => {
224
- const { data } = await this.r.post(`/servers/${this.id}/schedules/${this.sched_id}/tasks`, opts);
225
- return data.attributes;
226
- },
227
- update: async (task_id, opts) => {
228
- const { data } = await this.r.post(
229
- `/servers/${this.id}/schedules/${this.sched_id}/tasks/${task_id}`,
230
- opts
231
- );
232
- return data.attributes;
233
- },
234
- delete: async (task_id) => {
235
- await this.r.delete(
236
- `/servers/${this.id}/schedules/${this.sched_id}/tasks/${task_id}`
237
- );
238
- }
243
+ delete = async (id) => {
244
+ z4.number().positive().parse(id);
245
+ await this.r.delete(`/nodes/${id}`);
239
246
  };
247
+ allocations = (server_id) => new NodesAllocations(this.r, server_id);
240
248
  };
249
+ var NodeCreateSchema = z4.object({
250
+ name: z4.string().min(1).max(100),
251
+ description: z4.string().optional(),
252
+ public: z4.boolean().optional(),
253
+ fqdn: z4.string().nonempty(),
254
+ scheme: z4.enum(["http", "https"]),
255
+ behind_proxy: z4.boolean().optional(),
256
+ memory: z4.number().min(0),
257
+ memory_overallocate: z4.number().min(-1),
258
+ disk: z4.number().min(0),
259
+ disk_overallocate: z4.number().min(-1),
260
+ cpu: z4.number().min(0),
261
+ cpu_overallocate: z4.number().min(-1),
262
+ daemon_base: z4.string().nonempty().optional(),
263
+ daemon_sftp: z4.number().min(1).max(65535),
264
+ daemon_sftp_alias: z4.string().optional(),
265
+ daemon_listen: z4.number().min(1).max(65535),
266
+ daemon_connect: z4.number().min(1).max(65535),
267
+ maintenance_mode: z4.boolean().optional(),
268
+ upload_size: z4.number().min(1).max(1024),
269
+ tags: z4.array(z4.string()).optional()
270
+ });
241
271
 
242
- // src/api/client/server_allocations.ts
243
- var ServerAllocations = class {
272
+ // src/api/application/roles.ts
273
+ var Roles = class {
244
274
  r;
245
- id;
246
- constructor(requester, id) {
247
- this.r = requester;
248
- this.id = id;
275
+ constructor(r) {
276
+ this.r = r;
249
277
  }
250
- list = async () => {
251
- const { data } = await this.r.get(`/servers/${this.id}/network/allocations`);
278
+ list = async (page = 1) => {
279
+ const { data } = await this.r.get(`/roles`, { params: { page } });
252
280
  return data.data.map((r) => r.attributes);
253
281
  };
254
- autoAssign = async () => {
255
- const { data } = await this.r.post(`/servers/${this.id}/network/allocations`);
282
+ info = async (id) => {
283
+ const { data } = await this.r.get(
284
+ `/roles/${id}`
285
+ );
256
286
  return data.attributes;
257
287
  };
258
- setNotes = async (alloc_id, notes) => {
259
- const { data } = await this.r.post(`/servers/${this.id}/network/allocations/${alloc_id}`, { notes });
260
- return data.attributes;
288
+ create = async (opts) => {
289
+ await this.r.post(`/roles`, opts);
261
290
  };
262
- setPrimary = async (alloc_id) => {
263
- const { data } = await this.r.post(`/servers/${this.id}/network/allocations/${alloc_id}/primary`);
264
- return data.attributes;
291
+ update = async (id, opts) => {
292
+ await this.r.patch(`/roles/${id}`, opts);
265
293
  };
266
- unassign = async (alloc_id) => {
267
- await this.r.delete(
268
- `/servers/${this.id}/network/allocations/${alloc_id}`
269
- );
294
+ delete = async (id) => {
295
+ await this.r.delete(`/roles/${id}`);
270
296
  };
271
297
  };
272
298
 
273
- // src/api/client/server_users.ts
274
- var ServerUsers = class {
299
+ // src/api/application/servers.ts
300
+ import z6 from "zod";
301
+
302
+ // src/api/application/servers_databases.ts
303
+ import z5 from "zod";
304
+ var ServersDatabases = class {
275
305
  r;
276
306
  id;
277
- constructor(requester, id) {
278
- this.r = requester;
279
- this.id = id;
307
+ constructor(r, server_id) {
308
+ this.r = r;
309
+ this.id = server_id;
280
310
  }
281
311
  list = async () => {
282
- const { data } = await this.r.get(`/servers/${this.id}/users`);
312
+ const { data } = await this.r.get(`/servers/${this.id}/databases`);
283
313
  return data.data.map((d) => d.attributes);
284
314
  };
285
- create = async (email, permissions) => {
286
- const { data } = await this.r.post(`/servers/${this.id}/users`, { email, permissions });
315
+ create = async (database, remote, host) => {
316
+ database = z5.string().min(1).max(48).parse(database);
317
+ const { data } = await this.r.post(`/servers/${this.id}/databases`, { database, remote, host });
287
318
  return data.attributes;
288
319
  };
289
- info = async (user_uuid) => {
290
- const { data } = await this.r.get(
291
- `/servers/${this.id}/users/${user_uuid}`
292
- );
320
+ info = async (database_id) => {
321
+ const { data } = await this.r.get(`/servers/${this.id}/databases/${database_id}`);
293
322
  return data.attributes;
294
323
  };
295
- update = async (user_uuid, permissions) => {
296
- const { data } = await this.r.put(
297
- `/servers/${this.id}/users/${user_uuid}`,
298
- { permissions }
299
- );
300
- return data.attributes;
324
+ delete = async (database_id) => {
325
+ await this.r.delete(`/servers/${this.id}/databases/${database_id}`);
301
326
  };
302
- delete = async (user_uuid) => {
303
- await this.r.delete(`/servers/${this.id}/users/${user_uuid}`);
327
+ resetPassword = async (database_id) => {
328
+ await this.r.post(
329
+ `/servers/${this.id}/databases/${database_id}/reset-password`
330
+ );
304
331
  };
305
332
  };
306
333
 
307
- // src/api/client/server_backups.ts
308
- import axios2 from "axios";
309
- import z3 from "zod";
310
- var ServerBackups = class {
334
+ // src/api/application/servers.ts
335
+ var Servers = class {
311
336
  r;
312
337
  id;
313
- constructor(requester, id) {
314
- this.r = requester;
315
- this.id = id;
338
+ databases;
339
+ constructor(r, server_id) {
340
+ this.r = r;
341
+ this.id = server_id;
342
+ this.databases = new ServersDatabases(this.r, this.id);
316
343
  }
317
- list = async (page = 1) => {
318
- z3.number().positive().parse(page);
319
- const { data } = await this.r.get(`/servers/${this.id}/backups`, { params: { page } });
320
- return data.data.map((d) => d.attributes);
321
- };
322
- create = async (args) => {
323
- args.name = z3.string().max(255).optional().parse(args.name);
324
- const { data } = await this.r.post(`/servers/${this.id}/backups`, {
325
- name: args.name,
326
- is_locked: args.is_locked,
327
- ignored_files: args.ignored_files.join("\n")
328
- });
344
+ info = async (include) => {
345
+ const { data } = await this.r.get(`/servers/${this.id}`, { params: { include: include?.join(",") } });
329
346
  return data.attributes;
330
347
  };
331
- info = async (backup_uuid) => {
332
- const { data } = await this.r.get(`/servers/${this.id}/backups/${backup_uuid}`);
333
- return data.attributes;
348
+ delete = async (force = false) => {
349
+ await this.r.delete(`/servers/${this.id}${force ? "/force" : ""}`);
334
350
  };
335
- downloadGetUrl = async (backup_uuid) => {
336
- const { data } = await this.r.get(`/servers/${this.id}/backups/${backup_uuid}/download`);
337
- return data.attributes.url;
351
+ updateDetails = async (opts) => {
352
+ opts = UpdateDetailsSchema.parse(opts);
353
+ await this.r.patch(`/servers/${this.id}/details`, opts);
338
354
  };
339
- download = async (backup_uuid) => {
340
- const url = await this.downloadGetUrl(backup_uuid);
341
- const { data } = await axios2.get(url, {
342
- responseType: "arraybuffer"
343
- });
344
- return data;
355
+ updateBuild = async (opts) => {
356
+ opts = UpdateBuildSchema.parse(opts);
357
+ await this.r.patch(`/servers/${this.id}/build`, opts);
345
358
  };
346
- delete = async (backup_uuid) => {
347
- await this.r.delete(`/servers/${this.id}/backups/${backup_uuid}`);
359
+ updateStartup = async (opts) => {
360
+ opts = UpdateStartupSchema.parse(opts);
361
+ await this.r.patch(`/servers/${this.id}/startup`, opts);
348
362
  };
349
- rename = async (backup_uuid, name) => {
350
- await this.r.put(`/servers/${this.id}/backups/${backup_uuid}/rename`, {
351
- name
352
- });
363
+ suspend = async () => {
364
+ await this.r.post(`/servers/${this.id}/suspend`);
353
365
  };
354
- toggleLock = async (backup_uuid) => {
355
- await this.r.post(`/servers/${this.id}/backups/${backup_uuid}/lock`);
366
+ unsuspend = async () => {
367
+ await this.r.post(`/servers/${this.id}/unsuspend`);
356
368
  };
357
- restore = async (backup_uuid, truncate) => {
358
- await this.r.post(
359
- `/servers/${this.id}/backups/${backup_uuid}/restore`,
360
- { truncate }
361
- );
369
+ reinstall = async () => {
370
+ await this.r.post(`/servers/${this.id}/reinstall`);
362
371
  };
363
- };
364
-
365
- // src/api/client/server_startup.ts
366
- var ServerStartup = class {
367
- r;
368
- id;
369
- constructor(requester, id) {
370
- this.r = requester;
371
- this.id = id;
372
- }
373
- list = async () => {
374
- const { data } = await this.r.get(`/servers/${this.id}/startup`);
375
- return {
376
- object: "list",
377
- meta: data.meta,
378
- data: data.data.map((d) => d.attributes)
379
- };
372
+ transferStart = async (node_id, allocation_id, allocation_additional) => {
373
+ await this.r.post(`/servers/${this.id}/transfer`, {
374
+ node_id,
375
+ allocation_id,
376
+ allocation_additional
377
+ });
380
378
  };
381
- set = async (key, value) => {
382
- const { data } = await this.r.put(`/servers/${this.id}/startup/variable`, { key, value });
383
- return data.attributes;
384
- };
385
- };
386
-
387
- // src/api/client/server_settings.ts
388
- import z4 from "zod";
389
- var ServerSettings = class {
390
- r;
391
- id;
392
- constructor(requester, id) {
393
- this.r = requester;
394
- this.id = id;
395
- }
396
- rename = async (name) => {
397
- name = z4.string().max(255).parse(name);
398
- await this.r.post(`/servers/${this.id}/settings/rename`, { name });
399
- };
400
- updateDescription = async (description) => {
401
- await this.r.post(`/servers/${this.id}/settings/description`, {
402
- description
403
- });
404
- };
405
- reinstall = async () => {
406
- await this.r.post(`/servers/${this.id}/settings/reinstall`);
407
- };
408
- changeDockerImage = async (image) => {
409
- await this.r.put(`/servers/${this.id}/settings/docker-image`, {
410
- docker_image: image
411
- });
379
+ transferCancel = async () => {
380
+ await this.r.post(`/servers/${this.id}/transfer/cancel`);
412
381
  };
413
382
  };
383
+ var CreateServerSchema = z6.object({
384
+ external_id: z6.string().min(1).max(255).optional(),
385
+ name: z6.string().min(1).max(255),
386
+ description: z6.string().optional(),
387
+ user: z6.number(),
388
+ egg: z6.number(),
389
+ docker_image: z6.string().optional(),
390
+ startup: z6.string().optional(),
391
+ environment: z6.record(z6.string(), z6.string()),
392
+ skip_scripts: z6.boolean().optional(),
393
+ oom_killer: z6.boolean().optional(),
394
+ start_on_completion: z6.boolean().optional(),
395
+ docker_labels: z6.record(z6.string(), z6.string()).optional(),
396
+ limits: z6.object({
397
+ memory: z6.number().min(0),
398
+ swap: z6.number().min(-1),
399
+ disk: z6.number().min(0),
400
+ io: z6.number().min(0),
401
+ threads: z6.string().optional(),
402
+ cpu: z6.number().min(0)
403
+ }),
404
+ feature_limits: z6.object({
405
+ databases: z6.number().min(0),
406
+ allocations: z6.number().min(0),
407
+ backups: z6.number().min(0)
408
+ }),
409
+ allocation: z6.object({
410
+ default: z6.string().nullable(),
411
+ additional: z6.array(z6.string()).optional()
412
+ }).optional(),
413
+ deploy: z6.object({
414
+ tags: z6.array(z6.string()).optional(),
415
+ dedicated_ip: z6.boolean().optional(),
416
+ port_range: z6.array(z6.string()).optional()
417
+ }).optional()
418
+ });
419
+ var UpdateDetailsSchema = CreateServerSchema.pick({
420
+ external_id: true,
421
+ name: true,
422
+ user: true,
423
+ description: true,
424
+ docker_labels: true
425
+ });
426
+ var UpdateBuildSchema = CreateServerSchema.pick({
427
+ oom_killer: true,
428
+ limits: true,
429
+ feature_limits: true
430
+ }).extend({
431
+ allocation: z6.number().optional(),
432
+ add_allocations: z6.array(z6.string()).optional(),
433
+ remove_allocations: z6.array(z6.string()).optional()
434
+ });
435
+ var UpdateStartupSchema = CreateServerSchema.pick({
436
+ startup: true,
437
+ environment: true,
438
+ egg: true
439
+ }).extend({ image: z6.string().optional(), skip_scripts: z6.boolean() });
414
440
 
415
- // src/api/client/server_websocket.ts
416
- import { EventEmitter } from "events";
417
- import WebSocket from "isomorphic-ws";
418
- import stripColor from "strip-color";
419
- var isBrowser = typeof window !== "undefined";
420
- var RECONNECT_ERRORS = /* @__PURE__ */ new Set([
421
- "jwt: exp claim is invalid",
422
- "jwt: created too far in past (denylist)"
441
+ // src/api/application/users.ts
442
+ import z8 from "zod";
443
+
444
+ // src/api/common/types/enums.ts
445
+ import z7 from "zod";
446
+ var languagesSchema = z7.enum([
447
+ "af",
448
+ "ak",
449
+ "am",
450
+ "ar",
451
+ "as",
452
+ "az",
453
+ "be",
454
+ "bg",
455
+ "bm",
456
+ "bn",
457
+ "bo",
458
+ "br",
459
+ "bs",
460
+ "ca",
461
+ "ce",
462
+ "cs",
463
+ "cv",
464
+ "cy",
465
+ "da",
466
+ "de",
467
+ "dz",
468
+ "ee",
469
+ "el",
470
+ "en",
471
+ "eo",
472
+ "es",
473
+ "et",
474
+ "eu",
475
+ "fa",
476
+ "ff",
477
+ "fi",
478
+ "fo",
479
+ "fr",
480
+ "fy",
481
+ "ga",
482
+ "gd",
483
+ "gl",
484
+ "gu",
485
+ "gv",
486
+ "ha",
487
+ "he",
488
+ "hi",
489
+ "hr",
490
+ "hu",
491
+ "hy",
492
+ "ia",
493
+ "id",
494
+ "ig",
495
+ "ii",
496
+ "is",
497
+ "it",
498
+ "ja",
499
+ "jv",
500
+ "ka",
501
+ "ki",
502
+ "kk",
503
+ "kl",
504
+ "km",
505
+ "kn",
506
+ "ko",
507
+ "ks",
508
+ "ku",
509
+ "kw",
510
+ "ky",
511
+ "lb",
512
+ "lg",
513
+ "ln",
514
+ "lo",
515
+ "lt",
516
+ "lu",
517
+ "lv",
518
+ "mg",
519
+ "mi",
520
+ "mk",
521
+ "ml",
522
+ "mn",
523
+ "mr",
524
+ "ms",
525
+ "mt",
526
+ "my",
527
+ "nb",
528
+ "nd",
529
+ "ne",
530
+ "nl",
531
+ "nn",
532
+ "no",
533
+ "om",
534
+ "or",
535
+ "os",
536
+ "pa",
537
+ "pl",
538
+ "ps",
539
+ "pt",
540
+ "qu",
541
+ "rm",
542
+ "rn",
543
+ "ro",
544
+ "ru",
545
+ "rw",
546
+ "sa",
547
+ "sc",
548
+ "sd",
549
+ "se",
550
+ "sg",
551
+ "si",
552
+ "sk",
553
+ "sl",
554
+ "sn",
555
+ "so",
556
+ "sq",
557
+ "sr",
558
+ "su",
559
+ "sv",
560
+ "sw",
561
+ "ta",
562
+ "te",
563
+ "tg",
564
+ "th",
565
+ "ti",
566
+ "tk",
567
+ "to",
568
+ "tr",
569
+ "tt",
570
+ "ug",
571
+ "uk",
572
+ "ur",
573
+ "uz",
574
+ "vi",
575
+ "wo",
576
+ "xh",
577
+ "yi",
578
+ "yo",
579
+ "zh",
580
+ "zu"
423
581
  ]);
424
- var FALLBACK_LOG_MESSAGE = "No logs - is the server online?";
425
- var ServerWebsocket = class {
426
- r;
427
- serverId;
428
- socket;
429
- currentToken;
430
- bus = new EventEmitter();
431
- debugLogging = false;
432
- stripColors;
433
- detachMessageListener;
434
- constructor(requester, id, stripColors = false) {
435
- this.r = requester;
436
- this.serverId = id;
437
- this.stripColors = stripColors;
438
- }
439
- on(event, listener) {
440
- const handler = listener;
441
- this.bus.on(event, handler);
442
- return () => {
443
- this.bus.removeListener(event, handler);
444
- };
445
- }
446
- deregister(event, listener) {
447
- const handler = listener;
448
- this.bus.removeListener(event, handler);
449
- }
450
- emit(event, ...args) {
451
- if (args.length === 0) {
452
- this.bus.emit(event);
453
- } else {
454
- this.bus.emit(event, args[0]);
455
- }
456
- }
457
- async connect(resumable, debugLogging) {
458
- this.debugLogging = debugLogging ?? false;
459
- if (this.socket) {
460
- return;
461
- }
462
- const socketUrl = await this.refreshCredentials();
463
- this.socket = isBrowser ? new WebSocket(socketUrl) : new WebSocket(socketUrl, void 0, {
464
- origin: new URL(socketUrl).origin
465
- });
466
- await new Promise((resolve, reject) => {
467
- const socket = this.socket;
468
- if (!socket) {
469
- reject(new Error("Failed to create socket connection"));
470
- return;
471
- }
472
- socket.onopen = async () => {
473
- try {
474
- await this.authenticate();
475
- this.attachMessageListener();
476
- socket.onopen = null;
477
- socket.onerror = null;
478
- resolve();
479
- } catch (error) {
480
- socket.onopen = null;
481
- socket.onerror = null;
482
- reject(
483
- error instanceof Error ? error : new Error("Websocket authentication failed")
484
- );
485
- }
486
- };
487
- socket.onerror = (event) => {
488
- socket.onopen = null;
489
- socket.onerror = null;
490
- reject(
491
- event instanceof Error ? event : new Error("Websocket connection error")
492
- );
493
- };
494
- });
495
- if (resumable) {
496
- this.makeResumable(true);
497
- }
498
- }
499
- onSocketDisconnect(handler) {
500
- if (!this.socket) {
501
- console.error(new Error("No socket connection"));
502
- return;
503
- }
504
- this.socket.onclose = handler;
505
- }
506
- onSocketError(handler) {
507
- if (!this.socket) {
508
- console.error(new Error("No socket connection"));
509
- return;
510
- }
511
- this.socket.onerror = handler;
512
- }
513
- makeResumable(disconnectsToo) {
514
- const scheduleReconnect = () => {
515
- setTimeout(() => {
516
- const previous = this.socket;
517
- this.detachMessageListener?.();
518
- this.detachMessageListener = void 0;
519
- this.socket = void 0;
520
- previous?.close();
521
- void this.connect(true, this.debugLogging);
522
- }, 1e3);
523
- };
524
- this.onSocketError(() => scheduleReconnect());
525
- if (disconnectsToo) {
526
- this.onSocketDisconnect(() => scheduleReconnect());
527
- }
528
- }
529
- attachMessageListener() {
530
- if (!this.socket) {
531
- throw new Error("No socket connection");
532
- }
533
- this.detachMessageListener?.();
534
- const handler = (event) => {
535
- void this.handleIncomingMessage(event);
536
- };
537
- if (typeof this.socket.addEventListener === "function") {
538
- this.socket.addEventListener(
539
- "message",
540
- handler
541
- );
542
- this.detachMessageListener = () => {
543
- this.socket?.removeEventListener?.(
544
- "message",
545
- handler
546
- );
547
- };
548
- } else {
549
- const fallback = (data) => handler({ data });
550
- const socket = this.socket;
551
- socket.on?.("message", fallback);
552
- this.detachMessageListener = () => {
553
- const target = this.socket;
554
- if (!target) {
555
- return;
556
- }
557
- if (typeof target.off === "function") {
558
- target.off("message", fallback);
559
- } else if (typeof target.removeListener === "function") {
560
- target.removeListener("message", fallback);
561
- }
562
- };
563
- }
564
- }
565
- async handleIncomingMessage(event) {
566
- const message = this.parseMessage(event);
567
- if (!message) {
568
- return;
569
- }
570
- try {
571
- await this.dispatchMessage(message);
572
- } catch (error) {
573
- if (this.debugLogging) {
574
- console.error("Error while handling websocket message", error);
575
- }
576
- }
577
- }
578
- parseMessage(event) {
579
- const payload = this.normalisePayload(event);
580
- if (!payload) {
581
- return null;
582
- }
583
- try {
584
- return JSON.parse(payload);
585
- } catch (error) {
586
- if (this.debugLogging) {
587
- console.warn("Failed to parse websocket payload", error);
588
- }
589
- return null;
590
- }
591
- }
592
- normalisePayload(event) {
593
- if (typeof event === "string") {
594
- return event;
595
- }
596
- if (typeof event === "object" && event !== null && "data" in event) {
597
- return this.normalisePayload(event.data);
598
- }
599
- if (typeof Buffer !== "undefined" && Buffer.isBuffer(event)) {
600
- return event.toString("utf8");
601
- }
602
- if (typeof ArrayBuffer !== "undefined" && event instanceof ArrayBuffer) {
603
- if (typeof TextDecoder !== "undefined") {
604
- return new TextDecoder().decode(new Uint8Array(event));
605
- }
606
- if (typeof Buffer !== "undefined") {
607
- return Buffer.from(event).toString("utf8");
608
- }
609
- }
610
- return null;
611
- }
612
- async dispatchMessage(message) {
613
- switch (message.event) {
614
- case "auth success" /* AUTH_SUCCESS */: {
615
- if (this.debugLogging) {
616
- console.debug("Auth success");
617
- }
618
- this.emit("auth success" /* AUTH_SUCCESS */);
619
- break;
620
- }
621
- case "status" /* STATUS */: {
622
- if (this.debugLogging) {
623
- console.debug("Received status event", message.args[0]);
624
- }
625
- this.emit("status" /* STATUS */, message.args[0]);
626
- break;
627
- }
628
- case "console output" /* CONSOLE_OUTPUT */: {
629
- let output = message.args[0];
630
- if (this.stripColors) {
631
- output = stripColor(output);
632
- }
633
- if (this.debugLogging) {
634
- console.debug("Received console output", output);
635
- }
636
- this.emit("console output" /* CONSOLE_OUTPUT */, output);
637
- break;
638
- }
639
- case "stats" /* STATS */: {
640
- try {
641
- const payload = JSON.parse(message.args[0]);
642
- this.emit("stats" /* STATS */, payload);
643
- } catch (error) {
644
- if (this.debugLogging) {
645
- console.warn("Failed to parse stats payload", error);
646
- }
647
- }
648
- break;
649
- }
650
- case "daemon error" /* DAEMON_ERROR */: {
651
- this.emit("daemon error" /* DAEMON_ERROR */);
652
- break;
653
- }
654
- case "backup completed" /* BACKUP_COMPLETED */: {
655
- try {
656
- const payload = JSON.parse(
657
- message.args[0]
658
- );
659
- this.emit("backup completed" /* BACKUP_COMPLETED */, payload);
660
- } catch (error) {
661
- if (this.debugLogging) {
662
- console.warn("Failed to parse backup payload", error);
663
- }
664
- }
665
- break;
666
- }
667
- case "daemon message" /* DAEMON_MESSAGE */: {
668
- let output = message.args[0];
669
- if (this.stripColors) {
670
- output = stripColor(output);
671
- }
672
- this.emit("daemon message" /* DAEMON_MESSAGE */, output);
673
- break;
674
- }
675
- case "install output" /* INSTALL_OUTPUT */: {
676
- let output = message.args[0];
677
- if (this.stripColors) {
678
- output = stripColor(output);
679
- }
680
- this.emit("install output" /* INSTALL_OUTPUT */, output);
681
- break;
682
- }
683
- case "backup restore completed" /* BACKUP_RESTORE_COMPLETED */: {
684
- this.emit("backup restore completed" /* BACKUP_RESTORE_COMPLETED */);
685
- break;
686
- }
687
- case "install completed" /* INSTALL_COMPLETED */: {
688
- this.emit("install completed" /* INSTALL_COMPLETED */);
689
- break;
690
- }
691
- case "install started" /* INSTALL_STARTED */: {
692
- this.emit("install started" /* INSTALL_STARTED */);
693
- break;
694
- }
695
- case "transfer logs" /* TRANSFER_LOGS */: {
696
- this.emit("transfer logs" /* TRANSFER_LOGS */, message.args[0]);
697
- break;
698
- }
699
- case "transfer status" /* TRANSFER_STATUS */: {
700
- this.emit("transfer status" /* TRANSFER_STATUS */, message.args[0]);
701
- break;
702
- }
703
- case "token expiring" /* TOKEN_EXPIRING */: {
704
- this.emit("token expiring" /* TOKEN_EXPIRING */);
705
- if (this.debugLogging) {
706
- console.warn("Token expiring, renewing...");
707
- }
708
- await this.refreshCredentials();
709
- await this.authenticate();
710
- break;
711
- }
712
- case "token expired" /* TOKEN_EXPIRED */: {
713
- this.emit("token expired" /* TOKEN_EXPIRED */);
714
- throw new Error("Token expired");
715
- }
716
- case "jwt error" /* JWT_ERROR */: {
717
- const reason = message.args[0];
718
- if (RECONNECT_ERRORS.has(reason)) {
719
- this.emit("token expiring" /* TOKEN_EXPIRING */);
720
- if (this.debugLogging) {
721
- console.warn("Token expiring (JWT error), renewing...");
722
- }
723
- await this.refreshCredentials();
724
- await this.authenticate();
725
- } else {
726
- this.emit("jwt error" /* JWT_ERROR */, reason);
727
- throw new Error("Token expired");
728
- }
729
- break;
730
- }
731
- default: {
732
- if (this.debugLogging) {
733
- console.warn("Unknown websocket event", message);
734
- }
735
- break;
736
- }
737
- }
738
- }
739
- async refreshCredentials() {
740
- const { data } = await this.r.get(
741
- `/servers/${this.serverId}/websocket`
742
- );
743
- this.currentToken = data.data.token;
744
- return data.data.socket;
745
- }
746
- async authenticate() {
747
- if (!this.socket) {
748
- throw new Error("No socket connection");
749
- }
750
- if (!this.currentToken) {
751
- throw new Error("Missing websocket token");
752
- }
753
- this.socket.send(
754
- JSON.stringify({ event: "auth", args: [this.currentToken] })
755
- );
756
- }
757
- disconnect() {
758
- this.detachMessageListener?.();
759
- this.detachMessageListener = void 0;
760
- if (this.socket) {
761
- this.socket.close();
762
- this.socket = void 0;
763
- }
764
- }
765
- requestStats() {
766
- this.send("send stats", [null]);
767
- }
768
- requestLogs() {
769
- this.send("send logs", [null]);
770
- }
771
- send(event, args) {
772
- if (!this.socket) {
773
- if (this.debugLogging) {
774
- console.warn(
775
- `Attempted to send "${event}" without an active websocket connection`
776
- );
777
- }
778
- return;
779
- }
780
- this.socket.send(JSON.stringify({ event, args }));
781
- }
782
- getStats() {
783
- return new Promise((resolve, reject) => {
784
- if (!this.socket) {
785
- reject(new Error("No socket connection"));
786
- return;
787
- }
788
- let off;
789
- const timeout = setTimeout(() => {
790
- off?.();
791
- reject(new Error("Timed out waiting for stats"));
792
- }, 5e3);
793
- off = this.on("stats" /* STATS */, (payload) => {
794
- clearTimeout(timeout);
795
- off?.();
796
- resolve(payload);
797
- });
798
- this.requestStats();
799
- });
800
- }
801
- getLogs() {
802
- return new Promise((resolve, reject) => {
803
- if (!this.socket) {
804
- reject(new Error("No socket connection"));
805
- return;
806
- }
807
- const lines = [];
808
- let off;
809
- let initialTimeout;
810
- let idleTimeout;
811
- const finalize = (payload) => {
812
- off?.();
813
- if (initialTimeout) {
814
- clearTimeout(initialTimeout);
815
- }
816
- if (idleTimeout) {
817
- clearTimeout(idleTimeout);
818
- }
819
- resolve(payload);
820
- };
821
- initialTimeout = setTimeout(() => {
822
- finalize(lines.length > 0 ? lines : [FALLBACK_LOG_MESSAGE]);
823
- }, 5e3);
824
- off = this.on("console output" /* CONSOLE_OUTPUT */, (line) => {
825
- lines.push(line);
826
- if (initialTimeout) {
827
- clearTimeout(initialTimeout);
828
- initialTimeout = void 0;
829
- }
830
- if (idleTimeout) {
831
- clearTimeout(idleTimeout);
832
- }
833
- idleTimeout = setTimeout(() => {
834
- finalize(lines);
835
- }, 1e3);
836
- });
837
- this.requestLogs();
838
- });
839
- }
840
- sendPoweraction(action) {
841
- this.send("set state", [action]);
842
- }
843
- sendCommand(cmd) {
844
- this.send("send command", [cmd]);
845
- }
846
- };
847
-
848
- // src/api/client/server_activity.ts
849
- var ServerActivity = class {
850
- r;
851
- id;
852
- constructor(r, id) {
853
- this.r = r;
854
- this.id = id;
855
- }
856
- list = async (page = 1, per_page = 25) => {
857
- const { data } = await this.r.get(`/server/${this.id}/activity`, { params: { page, per_page } });
858
- return data.data.map((log) => log.attributes);
859
- };
860
- };
861
-
862
- // src/api/client/server.ts
863
- var ServerClient = class {
864
- r;
865
- id;
866
- activity;
867
- databases;
868
- files;
869
- schedules;
870
- allocations;
871
- users;
872
- backups;
873
- startup;
874
- variables;
875
- settings;
876
- constructor(requester, id) {
877
- this.r = requester;
878
- this.id = id;
879
- this.activity = new ServerActivity(requester, id);
880
- this.databases = new ServerDatabases(requester, id);
881
- this.files = new ServerFiles(requester, id);
882
- this.schedules = new ServerSchedules(requester, id);
883
- this.allocations = new ServerAllocations(requester, id);
884
- this.users = new ServerUsers(requester, id);
885
- this.backups = new ServerBackups(requester, id);
886
- this.startup = new ServerStartup(requester, id);
887
- this.variables = this.startup;
888
- this.settings = new ServerSettings(requester, id);
889
- }
890
- info = async (include) => {
891
- const { data } = await this.r.get(
892
- `/servers/${this.id}`,
893
- { params: { include: include?.join(",") } }
894
- );
895
- return data.attributes;
896
- };
897
- websocket = (stripColors = false) => {
898
- return new ServerWebsocket(this.r, this.id, stripColors);
899
- };
900
- resources = async () => {
901
- const { data } = await this.r.get(
902
- `/servers/${this.id}/resources`
903
- );
904
- return data.attributes;
905
- };
906
- command = async (command) => {
907
- await this.r.post(`/servers/${this.id}/command`, { command });
908
- };
909
- power = async (signal) => {
910
- await this.r.post(`/servers/${this.id}/power`, { signal });
911
- };
912
- };
913
-
914
- // src/api/client/client.ts
915
- var Client = class {
916
- account;
917
- r;
918
- constructor(requester) {
919
- this.r = requester;
920
- this.account = new Account(requester);
921
- }
922
- get $r() {
923
- return this.r;
924
- }
925
- listPermissions = async () => {
926
- const { data } = await this.r.get("/permissions");
927
- return data.attributes.permissions;
928
- };
929
- listServers = async (type = "accessible", page = 1, per_page = 50, include) => {
930
- z5.number().positive().parse(page);
931
- const { data } = await this.r.get("/", { params: { type, page, include: include?.join(",") } });
932
- return data.data.map((s) => s.attributes);
933
- };
934
- server = (uuid) => new ServerClient(this.r, uuid);
935
- };
936
-
937
- // src/api/application/users.ts
938
- import z7 from "zod";
939
-
940
- // src/utils/transform.ts
941
- var ArrayQueryParams = (p) => {
942
- const params = new URLSearchParams();
943
- const o = {};
944
- for (const [param, value] of Object.entries(p)) {
945
- for (const [key, val] of Object.entries(value)) {
946
- o[`${param}[${key}]`] = val;
947
- }
948
- }
949
- return o;
950
- };
951
- var SortParam = (key, p) => {
952
- return `${p === "desc" ? "-" : ""}${key}`;
953
- };
954
-
955
- // src/api/common/types/enums.ts
956
- import z6 from "zod";
957
- var languagesSchema = z6.enum([
958
- "af",
959
- "ak",
960
- "am",
961
- "ar",
962
- "as",
963
- "az",
964
- "be",
965
- "bg",
966
- "bm",
967
- "bn",
968
- "bo",
969
- "br",
970
- "bs",
971
- "ca",
972
- "ce",
973
- "cs",
974
- "cv",
975
- "cy",
976
- "da",
977
- "de",
978
- "dz",
979
- "ee",
980
- "el",
981
- "en",
982
- "eo",
983
- "es",
984
- "et",
985
- "eu",
986
- "fa",
987
- "ff",
988
- "fi",
989
- "fo",
990
- "fr",
991
- "fy",
992
- "ga",
993
- "gd",
994
- "gl",
995
- "gu",
996
- "gv",
997
- "ha",
998
- "he",
999
- "hi",
1000
- "hr",
1001
- "hu",
1002
- "hy",
1003
- "ia",
1004
- "id",
1005
- "ig",
1006
- "ii",
1007
- "is",
1008
- "it",
1009
- "ja",
1010
- "jv",
1011
- "ka",
1012
- "ki",
1013
- "kk",
1014
- "kl",
1015
- "km",
1016
- "kn",
1017
- "ko",
1018
- "ks",
1019
- "ku",
1020
- "kw",
1021
- "ky",
1022
- "lb",
1023
- "lg",
1024
- "ln",
1025
- "lo",
1026
- "lt",
1027
- "lu",
1028
- "lv",
1029
- "mg",
1030
- "mi",
1031
- "mk",
1032
- "ml",
1033
- "mn",
1034
- "mr",
1035
- "ms",
1036
- "mt",
1037
- "my",
1038
- "nb",
1039
- "nd",
1040
- "ne",
1041
- "nl",
1042
- "nn",
1043
- "no",
1044
- "om",
1045
- "or",
1046
- "os",
1047
- "pa",
1048
- "pl",
1049
- "ps",
1050
- "pt",
1051
- "qu",
1052
- "rm",
1053
- "rn",
1054
- "ro",
1055
- "ru",
1056
- "rw",
1057
- "sa",
1058
- "sc",
1059
- "sd",
1060
- "se",
1061
- "sg",
1062
- "si",
1063
- "sk",
1064
- "sl",
1065
- "sn",
1066
- "so",
1067
- "sq",
1068
- "sr",
1069
- "su",
1070
- "sv",
1071
- "sw",
1072
- "ta",
1073
- "te",
1074
- "tg",
1075
- "th",
1076
- "ti",
1077
- "tk",
1078
- "to",
1079
- "tr",
1080
- "tt",
1081
- "ug",
1082
- "uk",
1083
- "ur",
1084
- "uz",
1085
- "vi",
1086
- "wo",
1087
- "xh",
1088
- "yi",
1089
- "yo",
1090
- "zh",
1091
- "zu"
1092
- ]);
1093
- var timezonesSchema = z6.enum([
582
+ var timezonesSchema = z7.enum([
1094
583
  "Africa/Abidjan",
1095
584
  "Africa/Accra",
1096
585
  "Africa/Addis_Ababa",
@@ -1512,605 +1001,1122 @@ var timezonesSchema = z6.enum([
1512
1001
  "UTC"
1513
1002
  ]);
1514
1003
 
1515
- // src/api/application/users.ts
1516
- var Users = class {
1004
+ // src/utils/transform.ts
1005
+ var ArrayQueryParams = (p) => {
1006
+ const params = new URLSearchParams();
1007
+ const o = {};
1008
+ for (const [param, value] of Object.entries(p)) {
1009
+ for (const [key, val] of Object.entries(value)) {
1010
+ o[`${param}[${key}]`] = val;
1011
+ }
1012
+ }
1013
+ return o;
1014
+ };
1015
+ var SortParam = (key, p) => {
1016
+ return `${p === "desc" ? "-" : ""}${key}`;
1017
+ };
1018
+
1019
+ // src/api/application/users.ts
1020
+ var Users = class {
1021
+ r;
1022
+ constructor(requester) {
1023
+ this.r = requester;
1024
+ }
1025
+ list = async (opts, page = 1) => {
1026
+ z8.number().positive().parse(page);
1027
+ const { data } = await this.r.get("/users", {
1028
+ params: {
1029
+ include: opts.include?.join(","),
1030
+ page,
1031
+ ...ArrayQueryParams({ filters: opts.filters || {} }),
1032
+ sort: opts.sort && (opts.sort.id ? SortParam("id", opts.sort.id) : SortParam("uuid", opts.sort.uuid))
1033
+ }
1034
+ });
1035
+ return data.data.map((d) => d.attributes);
1036
+ };
1037
+ info = async (id, { include }) => {
1038
+ z8.number().positive().parse(id);
1039
+ const { data } = await this.r.get(`/users/${id}`, { params: { include: include?.join(",") } });
1040
+ return data.attributes;
1041
+ };
1042
+ infoByExternal = async (external_id, { include }) => {
1043
+ const { data } = await this.r.get(`/users/external/${external_id}`, {
1044
+ params: { include: include?.join(",") }
1045
+ });
1046
+ return data.attributes;
1047
+ };
1048
+ create = async (user) => {
1049
+ user = CreateSchema.parse(user);
1050
+ const { data } = await this.r.post("/users", user);
1051
+ return data.attributes;
1052
+ };
1053
+ update = async (id, user) => {
1054
+ user = CreateSchema.parse(user);
1055
+ const { data } = await this.r.patch(`/users/${id}`, user);
1056
+ return data.attributes;
1057
+ };
1058
+ delete = async (id) => {
1059
+ z8.number().positive().parse(id);
1060
+ await this.r.delete(`/users/${id}`);
1061
+ };
1062
+ addRoles = async (id, roles) => {
1063
+ z8.number().positive().parse(id);
1064
+ await this.r.patch(`/users/${id}/roles/assign`, { roles });
1065
+ };
1066
+ removeRoles = async (id, roles) => {
1067
+ z8.number().positive().parse(id);
1068
+ await this.r.patch(`/users/${id}/roles/remove`, { roles });
1069
+ };
1070
+ apiKeys = {
1071
+ list: async (id) => {
1072
+ const { data } = await this.r.get(`/users/${id}/api-keys`);
1073
+ return data.data.map((k) => k.attributes);
1074
+ },
1075
+ create: async (id, description, allowed_ips) => {
1076
+ allowed_ips = z8.array(z8.ipv4()).optional().parse(allowed_ips);
1077
+ const { data } = await this.r.post(`/users/${id}/api-keys`, { description, allowed_ips });
1078
+ return { ...data.attributes, secret_token: data.meta.secret_token };
1079
+ },
1080
+ delete: async (id, identifier) => {
1081
+ await this.r.delete(`/users/${id}/api-keys/${identifier}`);
1082
+ }
1083
+ };
1084
+ };
1085
+ var CreateSchema = z8.object({
1086
+ email: z8.email(),
1087
+ external_id: z8.string().max(255).optional(),
1088
+ username: z8.string().min(1).max(255),
1089
+ password: z8.string().optional(),
1090
+ language: languagesSchema,
1091
+ timezone: timezonesSchema
1092
+ });
1093
+
1094
+ // src/api/application/client.ts
1095
+ var Client = class {
1096
+ r;
1097
+ users;
1098
+ nodes;
1099
+ databaseHosts;
1100
+ roles;
1101
+ eggs;
1102
+ mounts;
1103
+ constructor(requester) {
1104
+ this.r = requester;
1105
+ this.users = new Users(requester);
1106
+ this.nodes = new Nodes(requester);
1107
+ this.databaseHosts = new DatabaseHosts(requester);
1108
+ this.roles = new Roles(requester);
1109
+ this.eggs = new Eggs(requester);
1110
+ this.mounts = new Mounts(requester);
1111
+ }
1112
+ get $r() {
1113
+ return this.r;
1114
+ }
1115
+ listServers = async (search, page = 1) => {
1116
+ const { data } = await this.r.get("/servers", { params: { search, page } });
1117
+ return data.data.map((s) => s.attributes);
1118
+ };
1119
+ createServer = async (opts) => {
1120
+ opts = CreateServerSchema.parse(opts);
1121
+ const { data } = await this.r.post("/servers", opts);
1122
+ return data.attributes;
1123
+ };
1124
+ getServerByExternalId = async (external_id, include) => {
1125
+ const { data } = await this.r.get(`/servers/external/${external_id}`, {
1126
+ params: { include: include?.join(",") }
1127
+ });
1128
+ return data.attributes;
1129
+ };
1130
+ servers = (server_id) => new Servers(this.r, server_id);
1131
+ };
1132
+
1133
+ // src/api/base/request.ts
1134
+ import axios from "axios";
1135
+ import z9 from "zod";
1136
+
1137
+ // src/api/base/types.ts
1138
+ var PterodactylException = class extends Error {
1139
+ data;
1140
+ status;
1141
+ constructor(message, data, status) {
1142
+ super(message);
1143
+ this.data = data;
1144
+ this.status = status;
1145
+ }
1146
+ };
1147
+
1148
+ // src/api/base/request.ts
1149
+ var Agent = class {
1150
+ base_url;
1151
+ token;
1152
+ requester;
1153
+ constructor(url, token, type, suffix = "/api") {
1154
+ this.base_url = z9.url("Invalid URL Schema").transform((url2) => new URL(url2).href).parse(url);
1155
+ this.token = z9.string().regex(/^(ptl[ac]|pacc|papp)_.+$/, "Invalid token type").parse(token);
1156
+ this.requester = axios.create({
1157
+ baseURL: this.base_url.replace(/\/+$/, "") + `${suffix}/${type}`,
1158
+ timeout: 3e3,
1159
+ headers: { Authorization: `Bearer ${this.token}` }
1160
+ });
1161
+ this.requester.interceptors.response.use(void 0, (error) => {
1162
+ if (error.response && error.response.status === 400) {
1163
+ return Promise.reject(
1164
+ new PterodactylException(
1165
+ "Invalid request data",
1166
+ error.response.data,
1167
+ error.response.status
1168
+ )
1169
+ );
1170
+ }
1171
+ return Promise.reject(error);
1172
+ });
1173
+ }
1174
+ };
1175
+
1176
+ // src/api/client/client.ts
1177
+ import z14 from "zod";
1178
+
1179
+ // src/api/client/account.ts
1180
+ import z10 from "zod";
1181
+ var Account = class {
1182
+ r;
1183
+ constructor(requester) {
1184
+ this.r = requester;
1185
+ }
1186
+ info = async () => {
1187
+ const { data } = await this.r.get("/account");
1188
+ return data.attributes;
1189
+ };
1190
+ updateEmail = async (newEmail, password) => {
1191
+ newEmail = z10.email().parse(newEmail);
1192
+ await this.r.put("/account/email", { email: newEmail, password });
1193
+ };
1194
+ updatePassword = async (newPassword) => {
1195
+ newPassword = z10.string().min(8).parse(newPassword);
1196
+ await this.r.put("/account/password", {
1197
+ password: newPassword,
1198
+ password_confirmation: newPassword
1199
+ });
1200
+ };
1201
+ apiKeys = {
1202
+ list: async () => {
1203
+ const { data } = await this.r.get("/account/api-keys");
1204
+ return data.data.map((k) => k.attributes);
1205
+ },
1206
+ create: async (description, allowed_ips) => {
1207
+ allowed_ips = z10.array(z10.ipv4()).optional().parse(allowed_ips);
1208
+ const { data } = await this.r.post("/account/api-keys", { description, allowed_ips });
1209
+ return { ...data.attributes, secret_token: data.meta.secret_token };
1210
+ },
1211
+ delete: async (identifier) => {
1212
+ await this.r.delete(`/account/api-keys/${identifier}`);
1213
+ }
1214
+ };
1215
+ sshKeys = {
1216
+ list: async () => {
1217
+ const { data } = await this.r.get("/account/ssh-keys");
1218
+ return data.data.map((k) => k.attributes);
1219
+ },
1220
+ create: async (name, public_key) => {
1221
+ const { data } = await this.r.post("/account/ssh-keys", { name, public_key });
1222
+ return data.attributes;
1223
+ },
1224
+ delete: async (fingerprint) => {
1225
+ await this.r.delete(`/account/ssh-keys/${fingerprint}`);
1226
+ }
1227
+ };
1228
+ };
1229
+
1230
+ // src/api/client/server_activity.ts
1231
+ var ServerActivity = class {
1232
+ r;
1233
+ id;
1234
+ constructor(r, id) {
1235
+ this.r = r;
1236
+ this.id = id;
1237
+ }
1238
+ list = async (page = 1, per_page = 25) => {
1239
+ const { data } = await this.r.get(`/server/${this.id}/activity`, { params: { page, per_page } });
1240
+ return data.data.map((log) => log.attributes);
1241
+ };
1242
+ };
1243
+
1244
+ // src/api/client/server_allocations.ts
1245
+ var ServerAllocations = class {
1246
+ r;
1247
+ id;
1248
+ constructor(requester, id) {
1249
+ this.r = requester;
1250
+ this.id = id;
1251
+ }
1252
+ list = async () => {
1253
+ const { data } = await this.r.get(`/servers/${this.id}/network/allocations`);
1254
+ return data.data.map((r) => r.attributes);
1255
+ };
1256
+ autoAssign = async () => {
1257
+ const { data } = await this.r.post(`/servers/${this.id}/network/allocations`);
1258
+ return data.attributes;
1259
+ };
1260
+ setNotes = async (alloc_id, notes) => {
1261
+ const { data } = await this.r.post(`/servers/${this.id}/network/allocations/${alloc_id}`, { notes });
1262
+ return data.attributes;
1263
+ };
1264
+ setPrimary = async (alloc_id) => {
1265
+ const { data } = await this.r.post(`/servers/${this.id}/network/allocations/${alloc_id}/primary`);
1266
+ return data.attributes;
1267
+ };
1268
+ unassign = async (alloc_id) => {
1269
+ await this.r.delete(
1270
+ `/servers/${this.id}/network/allocations/${alloc_id}`
1271
+ );
1272
+ };
1273
+ };
1274
+
1275
+ // src/api/client/server_backups.ts
1276
+ import axios2 from "axios";
1277
+ import z11 from "zod";
1278
+ var ServerBackups = class {
1279
+ r;
1280
+ id;
1281
+ constructor(requester, id) {
1282
+ this.r = requester;
1283
+ this.id = id;
1284
+ }
1285
+ list = async (page = 1) => {
1286
+ z11.number().positive().parse(page);
1287
+ const { data } = await this.r.get(`/servers/${this.id}/backups`, { params: { page } });
1288
+ return data.data.map((d) => d.attributes);
1289
+ };
1290
+ create = async (args) => {
1291
+ args.name = z11.string().max(255).optional().parse(args.name);
1292
+ const { data } = await this.r.post(`/servers/${this.id}/backups`, {
1293
+ name: args.name,
1294
+ is_locked: args.is_locked,
1295
+ ignored_files: args.ignored_files.join("\n")
1296
+ });
1297
+ return data.attributes;
1298
+ };
1299
+ info = async (backup_uuid) => {
1300
+ const { data } = await this.r.get(`/servers/${this.id}/backups/${backup_uuid}`);
1301
+ return data.attributes;
1302
+ };
1303
+ downloadGetUrl = async (backup_uuid) => {
1304
+ const { data } = await this.r.get(`/servers/${this.id}/backups/${backup_uuid}/download`);
1305
+ return data.attributes.url;
1306
+ };
1307
+ download = async (backup_uuid) => {
1308
+ const url = await this.downloadGetUrl(backup_uuid);
1309
+ const { data } = await axios2.get(url, {
1310
+ responseType: "arraybuffer"
1311
+ });
1312
+ return data;
1313
+ };
1314
+ delete = async (backup_uuid) => {
1315
+ await this.r.delete(`/servers/${this.id}/backups/${backup_uuid}`);
1316
+ };
1317
+ rename = async (backup_uuid, name) => {
1318
+ await this.r.put(`/servers/${this.id}/backups/${backup_uuid}/rename`, {
1319
+ name
1320
+ });
1321
+ };
1322
+ toggleLock = async (backup_uuid) => {
1323
+ await this.r.post(`/servers/${this.id}/backups/${backup_uuid}/lock`);
1324
+ };
1325
+ restore = async (backup_uuid, truncate) => {
1326
+ await this.r.post(
1327
+ `/servers/${this.id}/backups/${backup_uuid}/restore`,
1328
+ { truncate }
1329
+ );
1330
+ };
1331
+ };
1332
+
1333
+ // src/api/client/server_databases.ts
1334
+ import z12 from "zod";
1335
+ var ServerDatabases = class {
1336
+ r;
1337
+ id;
1338
+ constructor(requester, id) {
1339
+ this.r = requester;
1340
+ this.id = id;
1341
+ }
1342
+ list = async (include, page = 1) => {
1343
+ z12.number().positive().parse(page);
1344
+ const { data } = await this.r.get(`/servers/${this.id}/databases`, {
1345
+ params: { include: include?.join(","), page }
1346
+ });
1347
+ return data.data.map((d) => d.attributes);
1348
+ };
1349
+ create = async (database, remote) => {
1350
+ const { data } = await this.r.post(`/servers/${this.id}/databases`, { database, remote });
1351
+ return data.attributes;
1352
+ };
1353
+ rotatePassword = async (database_id) => {
1354
+ const { data } = await this.r.post(`/servers/${this.id}/databases/${database_id}/rotate-password`);
1355
+ return data.attributes;
1356
+ };
1357
+ delete = async (database_id) => {
1358
+ await this.r.delete(`/servers/${this.id}/databases/${database_id}`);
1359
+ };
1360
+ };
1361
+
1362
+ // src/api/client/server_files.ts
1363
+ import axios3 from "axios";
1364
+ var ServerFiles = class {
1365
+ r;
1366
+ id;
1367
+ constructor(requester, id) {
1368
+ this.r = requester;
1369
+ this.id = id;
1370
+ }
1371
+ list = async (path) => {
1372
+ const { data } = await this.r.get(`/servers/${this.id}/files/list`, { params: { directory: path } });
1373
+ return data.data.map((r) => r.attributes);
1374
+ };
1375
+ /**
1376
+ * Return the contents of a file. To read binary file (non-editable) use {@link download} instead
1377
+ */
1378
+ contents = async (path) => {
1379
+ const { data } = await this.r.get(
1380
+ `/servers/${this.id}/files/contents`,
1381
+ { params: { file: path } }
1382
+ );
1383
+ return data;
1384
+ };
1385
+ downloadGetUrl = async (path) => {
1386
+ const { data } = await this.r.get(`/servers/${this.id}/files/download`, { params: { file: path } });
1387
+ return data.attributes.url;
1388
+ };
1389
+ download = async (path) => {
1390
+ const url = await this.downloadGetUrl(path);
1391
+ const { data } = await axios3.get(url, {
1392
+ responseType: "arraybuffer"
1393
+ });
1394
+ return data;
1395
+ };
1396
+ rename = async (root = "/", files) => {
1397
+ await this.r.put(`/servers/${this.id}/files/rename`, { root, files });
1398
+ };
1399
+ copy = async (location) => {
1400
+ await this.r.post(`/servers/${this.id}/files/copy`, { location });
1401
+ };
1402
+ write = async (path, content) => {
1403
+ await this.r.post(`/servers/${this.id}/files/write`, content, {
1404
+ params: { file: path }
1405
+ });
1406
+ };
1407
+ compress = async (root = "/", files, archive_name, extension) => {
1408
+ const { data } = await this.r.post(`/servers/${this.id}/files/compress`, {
1409
+ root,
1410
+ files,
1411
+ archive_name,
1412
+ extension
1413
+ });
1414
+ return data.attributes;
1415
+ };
1416
+ decompress = async (root = "/", file) => {
1417
+ await this.r.post(`/servers/${this.id}/files/decompress`, { root, file });
1418
+ };
1419
+ delete = async (root = "/", files) => {
1420
+ await this.r.post(`/servers/${this.id}/files/delete`, { root, files });
1421
+ };
1422
+ createFolder = async (root = "/", name) => {
1423
+ await this.r.post(`/servers/${this.id}/files/create-folder`, {
1424
+ root,
1425
+ name
1426
+ });
1427
+ };
1428
+ chmod = async (root = "/", files) => {
1429
+ await this.r.post(`/servers/${this.id}/files/chmod`, { root, files });
1430
+ };
1431
+ pullFromRemote = async (url, directory, filename, use_header = false, foreground = false) => {
1432
+ await this.r.post(`/servers/${this.id}/files/pull`, {
1433
+ url,
1434
+ directory,
1435
+ filename,
1436
+ use_header,
1437
+ foreground
1438
+ });
1439
+ };
1440
+ uploadGetUrl = async () => {
1441
+ const { data } = await this.r.get(`/servers/${this.id}/files/upload`);
1442
+ return data.attributes.url;
1443
+ };
1444
+ upload = async (file, root = "/") => {
1445
+ const url = await this.uploadGetUrl();
1446
+ await axios3.post(
1447
+ url,
1448
+ { files: file },
1449
+ {
1450
+ headers: { "Content-Type": "multipart/form-data" },
1451
+ params: { directory: root }
1452
+ }
1453
+ );
1454
+ };
1455
+ };
1456
+
1457
+ // src/api/client/server_schedules.ts
1458
+ var ServerSchedules = class {
1459
+ r;
1460
+ id;
1461
+ constructor(requester, id) {
1462
+ this.r = requester;
1463
+ this.id = id;
1464
+ }
1465
+ list = async () => {
1466
+ const { data } = await this.r.get(`/servers/${this.id}/schedules`);
1467
+ return data.data.map((d) => d.attributes);
1468
+ };
1469
+ create = async (params) => {
1470
+ const { data } = await this.r.post(`/servers/${this.id}/schedules`, params);
1471
+ return data.attributes;
1472
+ };
1473
+ control = (sched_id) => new ScheduleControl(this.r, this.id, sched_id);
1474
+ };
1475
+ var ScheduleControl = class {
1476
+ r;
1477
+ id;
1478
+ sched_id;
1479
+ constructor(requester, id, sched_id) {
1480
+ this.r = requester;
1481
+ this.id = id;
1482
+ this.sched_id = sched_id;
1483
+ }
1484
+ info = async () => {
1485
+ const { data } = await this.r.get(`/servers/${this.id}/schedules/${this.sched_id}`);
1486
+ return data.attributes;
1487
+ };
1488
+ update = async (params) => {
1489
+ const { data } = await this.r.post(`/servers/${this.id}/schedules/${this.sched_id}`, params);
1490
+ return data.attributes;
1491
+ };
1492
+ delete = async () => {
1493
+ await this.r.delete(`/servers/${this.id}/schedules/${this.sched_id}`);
1494
+ };
1495
+ execute = async () => {
1496
+ await this.r.post(
1497
+ `/servers/${this.id}/schedules/${this.sched_id}/execute`
1498
+ );
1499
+ };
1500
+ tasks = {
1501
+ create: async (opts) => {
1502
+ const { data } = await this.r.post(`/servers/${this.id}/schedules/${this.sched_id}/tasks`, opts);
1503
+ return data.attributes;
1504
+ },
1505
+ update: async (task_id, opts) => {
1506
+ const { data } = await this.r.post(
1507
+ `/servers/${this.id}/schedules/${this.sched_id}/tasks/${task_id}`,
1508
+ opts
1509
+ );
1510
+ return data.attributes;
1511
+ },
1512
+ delete: async (task_id) => {
1513
+ await this.r.delete(
1514
+ `/servers/${this.id}/schedules/${this.sched_id}/tasks/${task_id}`
1515
+ );
1516
+ }
1517
+ };
1518
+ };
1519
+
1520
+ // src/api/client/server_settings.ts
1521
+ import z13 from "zod";
1522
+ var ServerSettings = class {
1517
1523
  r;
1518
- constructor(requester) {
1524
+ id;
1525
+ constructor(requester, id) {
1526
+ this.r = requester;
1527
+ this.id = id;
1528
+ }
1529
+ rename = async (name) => {
1530
+ name = z13.string().max(255).parse(name);
1531
+ await this.r.post(`/servers/${this.id}/settings/rename`, { name });
1532
+ };
1533
+ updateDescription = async (description) => {
1534
+ await this.r.post(`/servers/${this.id}/settings/description`, {
1535
+ description
1536
+ });
1537
+ };
1538
+ reinstall = async () => {
1539
+ await this.r.post(`/servers/${this.id}/settings/reinstall`);
1540
+ };
1541
+ changeDockerImage = async (image) => {
1542
+ await this.r.put(`/servers/${this.id}/settings/docker-image`, {
1543
+ docker_image: image
1544
+ });
1545
+ };
1546
+ };
1547
+
1548
+ // src/api/client/server_startup.ts
1549
+ var ServerStartup = class {
1550
+ r;
1551
+ id;
1552
+ constructor(requester, id) {
1553
+ this.r = requester;
1554
+ this.id = id;
1555
+ }
1556
+ list = async () => {
1557
+ const { data } = await this.r.get(`/servers/${this.id}/startup`);
1558
+ return {
1559
+ object: "list",
1560
+ meta: data.meta,
1561
+ data: data.data.map((d) => d.attributes)
1562
+ };
1563
+ };
1564
+ set = async (key, value) => {
1565
+ const { data } = await this.r.put(`/servers/${this.id}/startup/variable`, { key, value });
1566
+ return data.attributes;
1567
+ };
1568
+ };
1569
+
1570
+ // src/api/client/server_users.ts
1571
+ var ServerUsers = class {
1572
+ r;
1573
+ id;
1574
+ constructor(requester, id) {
1575
+ this.r = requester;
1576
+ this.id = id;
1577
+ }
1578
+ list = async () => {
1579
+ const { data } = await this.r.get(`/servers/${this.id}/users`);
1580
+ return data.data.map((d) => d.attributes);
1581
+ };
1582
+ create = async (email, permissions) => {
1583
+ const { data } = await this.r.post(`/servers/${this.id}/users`, { email, permissions });
1584
+ return data.attributes;
1585
+ };
1586
+ info = async (user_uuid) => {
1587
+ const { data } = await this.r.get(
1588
+ `/servers/${this.id}/users/${user_uuid}`
1589
+ );
1590
+ return data.attributes;
1591
+ };
1592
+ update = async (user_uuid, permissions) => {
1593
+ const { data } = await this.r.put(
1594
+ `/servers/${this.id}/users/${user_uuid}`,
1595
+ { permissions }
1596
+ );
1597
+ return data.attributes;
1598
+ };
1599
+ delete = async (user_uuid) => {
1600
+ await this.r.delete(`/servers/${this.id}/users/${user_uuid}`);
1601
+ };
1602
+ };
1603
+
1604
+ // src/api/client/server_websocket.ts
1605
+ import { EventEmitter } from "events";
1606
+ import WebSocket from "isomorphic-ws";
1607
+ import stripColor from "strip-color";
1608
+ var isBrowser = typeof window !== "undefined";
1609
+ var RECONNECT_ERRORS = /* @__PURE__ */ new Set([
1610
+ "jwt: exp claim is invalid",
1611
+ "jwt: created too far in past (denylist)"
1612
+ ]);
1613
+ var FALLBACK_LOG_MESSAGE = "No logs - is the server online?";
1614
+ var ServerWebsocket = class {
1615
+ r;
1616
+ serverId;
1617
+ socket;
1618
+ currentToken;
1619
+ bus = new EventEmitter();
1620
+ debugLogging = false;
1621
+ stripColors;
1622
+ detachMessageListener;
1623
+ constructor(requester, id, stripColors = false) {
1519
1624
  this.r = requester;
1625
+ this.serverId = id;
1626
+ this.stripColors = stripColors;
1627
+ }
1628
+ on(event, listener) {
1629
+ const handler = listener;
1630
+ this.bus.on(event, handler);
1631
+ return () => {
1632
+ this.bus.removeListener(event, handler);
1633
+ };
1634
+ }
1635
+ deregister(event, listener) {
1636
+ const handler = listener;
1637
+ this.bus.removeListener(event, handler);
1638
+ }
1639
+ emit(event, ...args) {
1640
+ if (args.length === 0) {
1641
+ this.bus.emit(event);
1642
+ } else {
1643
+ this.bus.emit(event, args[0]);
1644
+ }
1645
+ }
1646
+ async connect(resumable, debugLogging) {
1647
+ this.debugLogging = debugLogging ?? false;
1648
+ if (this.socket) {
1649
+ return;
1650
+ }
1651
+ const socketUrl = await this.refreshCredentials();
1652
+ this.socket = isBrowser ? new WebSocket(socketUrl) : new WebSocket(socketUrl, void 0, {
1653
+ origin: new URL(socketUrl).origin
1654
+ });
1655
+ await new Promise((resolve, reject) => {
1656
+ const socket = this.socket;
1657
+ if (!socket) {
1658
+ reject(new Error("Failed to create socket connection"));
1659
+ return;
1660
+ }
1661
+ socket.onopen = async () => {
1662
+ try {
1663
+ await this.authenticate();
1664
+ this.attachMessageListener();
1665
+ socket.onopen = null;
1666
+ socket.onerror = null;
1667
+ resolve();
1668
+ } catch (error) {
1669
+ socket.onopen = null;
1670
+ socket.onerror = null;
1671
+ reject(
1672
+ error instanceof Error ? error : new Error("Websocket authentication failed")
1673
+ );
1674
+ }
1675
+ };
1676
+ socket.onerror = (event) => {
1677
+ socket.onopen = null;
1678
+ socket.onerror = null;
1679
+ reject(
1680
+ event instanceof Error ? event : new Error("Websocket connection error")
1681
+ );
1682
+ };
1683
+ });
1684
+ if (resumable) {
1685
+ this.makeResumable(true);
1686
+ }
1687
+ }
1688
+ onSocketDisconnect(handler) {
1689
+ if (!this.socket) {
1690
+ console.error(new Error("No socket connection"));
1691
+ return;
1692
+ }
1693
+ this.socket.onclose = handler;
1694
+ }
1695
+ onSocketError(handler) {
1696
+ if (!this.socket) {
1697
+ console.error(new Error("No socket connection"));
1698
+ return;
1699
+ }
1700
+ this.socket.onerror = handler;
1701
+ }
1702
+ makeResumable(disconnectsToo) {
1703
+ const scheduleReconnect = () => {
1704
+ setTimeout(() => {
1705
+ const previous = this.socket;
1706
+ this.detachMessageListener?.();
1707
+ this.detachMessageListener = void 0;
1708
+ this.socket = void 0;
1709
+ previous?.close();
1710
+ void this.connect(true, this.debugLogging);
1711
+ }, 1e3);
1712
+ };
1713
+ this.onSocketError(() => scheduleReconnect());
1714
+ if (disconnectsToo) {
1715
+ this.onSocketDisconnect(() => scheduleReconnect());
1716
+ }
1717
+ }
1718
+ attachMessageListener() {
1719
+ if (!this.socket) {
1720
+ throw new Error("No socket connection");
1721
+ }
1722
+ this.detachMessageListener?.();
1723
+ const handler = (event) => {
1724
+ void this.handleIncomingMessage(event);
1725
+ };
1726
+ if (typeof this.socket.addEventListener === "function") {
1727
+ this.socket.addEventListener(
1728
+ "message",
1729
+ handler
1730
+ );
1731
+ this.detachMessageListener = () => {
1732
+ this.socket?.removeEventListener?.(
1733
+ "message",
1734
+ handler
1735
+ );
1736
+ };
1737
+ } else {
1738
+ const fallback = (data) => handler({ data });
1739
+ const socket = this.socket;
1740
+ socket.on?.("message", fallback);
1741
+ this.detachMessageListener = () => {
1742
+ const target = this.socket;
1743
+ if (!target) {
1744
+ return;
1745
+ }
1746
+ if (typeof target.off === "function") {
1747
+ target.off("message", fallback);
1748
+ } else if (typeof target.removeListener === "function") {
1749
+ target.removeListener("message", fallback);
1750
+ }
1751
+ };
1752
+ }
1520
1753
  }
1521
- list = async (opts, page = 1) => {
1522
- z7.number().positive().parse(page);
1523
- const { data } = await this.r.get("/users", {
1524
- params: {
1525
- include: opts.include?.join(","),
1526
- page,
1527
- ...ArrayQueryParams({ filters: opts.filters || {} }),
1528
- sort: opts.sort?.id ? SortParam("id", opts.sort?.id) : SortParam("uuid", opts.sort?.uuid)
1754
+ async handleIncomingMessage(event) {
1755
+ const message = this.parseMessage(event);
1756
+ if (!message) {
1757
+ return;
1758
+ }
1759
+ try {
1760
+ await this.dispatchMessage(message);
1761
+ } catch (error) {
1762
+ if (this.debugLogging) {
1763
+ console.error("Error while handling websocket message", error);
1529
1764
  }
1530
- });
1531
- return data.data.map((d) => d.attributes);
1532
- };
1533
- info = async (id, { include }) => {
1534
- z7.number().positive().parse(id);
1535
- const { data } = await this.r.get(`/users/${id}`, { params: { include: include?.join(",") } });
1536
- return data.attributes;
1537
- };
1538
- infoByExternal = async (external_id, { include }) => {
1539
- const { data } = await this.r.get(`/users/external/${external_id}`, {
1540
- params: { include: include?.join(",") }
1541
- });
1542
- return data.attributes;
1543
- };
1544
- create = async (user) => {
1545
- user = CreateSchema.parse(user);
1546
- const { data } = await this.r.post("/users", user);
1547
- return data.attributes;
1548
- };
1549
- update = async (id, user) => {
1550
- user = CreateSchema.parse(user);
1551
- const { data } = await this.r.patch(`/users/${id}`, user);
1552
- return data.attributes;
1553
- };
1554
- delete = async (id) => {
1555
- z7.number().positive().parse(id);
1556
- await this.r.delete(`/users/${id}`);
1557
- };
1558
- addRoles = async (id, roles) => {
1559
- z7.number().positive().parse(id);
1560
- await this.r.patch(`/users/${id}/roles/assign`, { roles });
1561
- };
1562
- removeRoles = async (id, roles) => {
1563
- z7.number().positive().parse(id);
1564
- await this.r.patch(`/users/${id}/roles/remove`, { roles });
1565
- };
1566
- apiKeys = {
1567
- list: async (id) => {
1568
- const { data } = await this.r.get(`/users/${id}/api-keys`);
1569
- return data.data.map((k) => k.attributes);
1570
- },
1571
- create: async (id, description, allowed_ips) => {
1572
- allowed_ips = z7.array(z7.ipv4()).optional().parse(allowed_ips);
1573
- const { data } = await this.r.post(`/users/${id}/api-keys`, { description, allowed_ips });
1574
- return { ...data.attributes, secret_token: data.meta.secret_token };
1575
- },
1576
- delete: async (id, identifier) => {
1577
- await this.r.delete(`/users/${id}/api-keys/${identifier}`);
1578
1765
  }
1579
- };
1580
- };
1581
- var CreateSchema = z7.object({
1582
- email: z7.email(),
1583
- external_id: z7.string().max(255).optional(),
1584
- username: z7.string().min(1).max(255),
1585
- password: z7.string().optional(),
1586
- language: languagesSchema,
1587
- timezone: timezonesSchema
1588
- });
1589
-
1590
- // src/api/application/nodes_allocations.ts
1591
- import z8 from "zod";
1592
- var NodesAllocations = class {
1593
- r;
1594
- id;
1595
- constructor(requester, id) {
1596
- this.r = requester;
1597
- this.id = id;
1598
1766
  }
1599
- list = async (include) => {
1600
- const { data } = await this.r.get(`/nodes/${this.id}/allocations`, {
1601
- params: { include: include?.join(",") }
1602
- });
1603
- return data.data.map((d) => d.attributes);
1604
- };
1605
- create = async (ip, ports, alias) => {
1606
- z8.ipv4().parse(ip);
1607
- z8.ipv4().or(z8.url().max(255)).optional().parse(alias);
1608
- z8.array(z8.number()).or(z8.string().regex(/\d+-\d+/)).parse(ports);
1609
- await this.r.post(`/nodes/${this.id}/allocations`, { ip, ports, alias });
1610
- };
1611
- delete = async (alloc_id) => {
1612
- await this.r.delete(`/nodes/${this.id}/allocations/${alloc_id}`);
1613
- };
1614
- };
1615
-
1616
- // src/api/application/nodes.ts
1617
- import z9 from "zod";
1618
- var Nodes = class {
1619
- r;
1620
- constructor(requester) {
1621
- this.r = requester;
1767
+ parseMessage(event) {
1768
+ const payload = this.normalisePayload(event);
1769
+ if (!payload) {
1770
+ return null;
1771
+ }
1772
+ try {
1773
+ return JSON.parse(payload);
1774
+ } catch (error) {
1775
+ if (this.debugLogging) {
1776
+ console.warn("Failed to parse websocket payload", error);
1777
+ }
1778
+ return null;
1779
+ }
1622
1780
  }
1623
- list = async (include, page = 1) => {
1624
- z9.number().positive().parse(page);
1625
- const { data } = await this.r.get("/nodes", { params: { include: include?.join(","), page } });
1626
- return data.data.map((s) => s.attributes);
1627
- };
1628
- listDeployable = async (filters, include, page = 1) => {
1629
- z9.number().positive().parse(page);
1630
- const { data } = await this.r.get("/nodes/deployable", {
1631
- params: {
1632
- include: include?.join(","),
1633
- disk: filters.disk,
1634
- memory: filters.memory,
1635
- cpu: filters.cpu,
1636
- location_ids: filters.location_ids,
1637
- tags: filters.tags,
1638
- page
1781
+ normalisePayload(event) {
1782
+ if (typeof event === "string") {
1783
+ return event;
1784
+ }
1785
+ if (typeof event === "object" && event !== null && "data" in event) {
1786
+ return this.normalisePayload(event.data);
1787
+ }
1788
+ if (typeof Buffer !== "undefined" && Buffer.isBuffer(event)) {
1789
+ return event.toString("utf8");
1790
+ }
1791
+ if (typeof ArrayBuffer !== "undefined" && event instanceof ArrayBuffer) {
1792
+ if (typeof TextDecoder !== "undefined") {
1793
+ return new TextDecoder().decode(new Uint8Array(event));
1794
+ }
1795
+ if (typeof Buffer !== "undefined") {
1796
+ return Buffer.from(event).toString("utf8");
1797
+ }
1798
+ }
1799
+ return null;
1800
+ }
1801
+ async dispatchMessage(message) {
1802
+ switch (message.event) {
1803
+ case "auth success" /* AUTH_SUCCESS */: {
1804
+ if (this.debugLogging) {
1805
+ console.debug("Auth success");
1806
+ }
1807
+ this.emit("auth success" /* AUTH_SUCCESS */);
1808
+ break;
1809
+ }
1810
+ case "status" /* STATUS */: {
1811
+ if (this.debugLogging) {
1812
+ console.debug("Received status event", message.args[0]);
1813
+ }
1814
+ this.emit("status" /* STATUS */, message.args[0]);
1815
+ break;
1816
+ }
1817
+ case "console output" /* CONSOLE_OUTPUT */: {
1818
+ let output = message.args[0];
1819
+ if (this.stripColors) {
1820
+ output = stripColor(output);
1821
+ }
1822
+ if (this.debugLogging) {
1823
+ console.debug("Received console output", output);
1824
+ }
1825
+ this.emit("console output" /* CONSOLE_OUTPUT */, output);
1826
+ break;
1827
+ }
1828
+ case "stats" /* STATS */: {
1829
+ try {
1830
+ const payload = JSON.parse(message.args[0]);
1831
+ this.emit("stats" /* STATS */, payload);
1832
+ } catch (error) {
1833
+ if (this.debugLogging) {
1834
+ console.warn("Failed to parse stats payload", error);
1835
+ }
1836
+ }
1837
+ break;
1838
+ }
1839
+ case "daemon error" /* DAEMON_ERROR */: {
1840
+ this.emit("daemon error" /* DAEMON_ERROR */);
1841
+ break;
1842
+ }
1843
+ case "backup completed" /* BACKUP_COMPLETED */: {
1844
+ try {
1845
+ const payload = JSON.parse(
1846
+ message.args[0]
1847
+ );
1848
+ this.emit("backup completed" /* BACKUP_COMPLETED */, payload);
1849
+ } catch (error) {
1850
+ if (this.debugLogging) {
1851
+ console.warn("Failed to parse backup payload", error);
1852
+ }
1853
+ }
1854
+ break;
1855
+ }
1856
+ case "daemon message" /* DAEMON_MESSAGE */: {
1857
+ let output = message.args[0];
1858
+ if (this.stripColors) {
1859
+ output = stripColor(output);
1860
+ }
1861
+ this.emit("daemon message" /* DAEMON_MESSAGE */, output);
1862
+ break;
1863
+ }
1864
+ case "install output" /* INSTALL_OUTPUT */: {
1865
+ let output = message.args[0];
1866
+ if (this.stripColors) {
1867
+ output = stripColor(output);
1868
+ }
1869
+ this.emit("install output" /* INSTALL_OUTPUT */, output);
1870
+ break;
1871
+ }
1872
+ case "backup restore completed" /* BACKUP_RESTORE_COMPLETED */: {
1873
+ this.emit("backup restore completed" /* BACKUP_RESTORE_COMPLETED */);
1874
+ break;
1875
+ }
1876
+ case "install completed" /* INSTALL_COMPLETED */: {
1877
+ this.emit("install completed" /* INSTALL_COMPLETED */);
1878
+ break;
1879
+ }
1880
+ case "install started" /* INSTALL_STARTED */: {
1881
+ this.emit("install started" /* INSTALL_STARTED */);
1882
+ break;
1883
+ }
1884
+ case "transfer logs" /* TRANSFER_LOGS */: {
1885
+ this.emit("transfer logs" /* TRANSFER_LOGS */, message.args[0]);
1886
+ break;
1887
+ }
1888
+ case "transfer status" /* TRANSFER_STATUS */: {
1889
+ this.emit("transfer status" /* TRANSFER_STATUS */, message.args[0]);
1890
+ break;
1891
+ }
1892
+ case "token expiring" /* TOKEN_EXPIRING */: {
1893
+ this.emit("token expiring" /* TOKEN_EXPIRING */);
1894
+ if (this.debugLogging) {
1895
+ console.warn("Token expiring, renewing...");
1896
+ }
1897
+ await this.refreshCredentials();
1898
+ await this.authenticate();
1899
+ break;
1900
+ }
1901
+ case "token expired" /* TOKEN_EXPIRED */: {
1902
+ this.emit("token expired" /* TOKEN_EXPIRED */);
1903
+ throw new Error("Token expired");
1904
+ }
1905
+ case "jwt error" /* JWT_ERROR */: {
1906
+ const reason = message.args[0];
1907
+ if (RECONNECT_ERRORS.has(reason)) {
1908
+ this.emit("token expiring" /* TOKEN_EXPIRING */);
1909
+ if (this.debugLogging) {
1910
+ console.warn("Token expiring (JWT error), renewing...");
1911
+ }
1912
+ await this.refreshCredentials();
1913
+ await this.authenticate();
1914
+ } else {
1915
+ this.emit("jwt error" /* JWT_ERROR */, reason);
1916
+ throw new Error("Token expired");
1917
+ }
1918
+ break;
1639
1919
  }
1640
- });
1641
- return data.data.map((s) => s.attributes);
1642
- };
1643
- info = async (id, include) => {
1644
- z9.number().positive().parse(id);
1645
- const { data } = await this.r.get(
1646
- `/nodes/${id}`,
1647
- { params: { include: include?.join(",") } }
1648
- );
1649
- return data.attributes;
1650
- };
1651
- create = async (node) => {
1652
- node = NodeCreateSchema.parse(node);
1653
- const { data } = await this.r.post(
1654
- "/nodes",
1655
- node
1656
- );
1657
- return data.attributes;
1658
- };
1659
- get_configuration = async (id) => {
1660
- z9.number().positive().parse(id);
1920
+ default: {
1921
+ if (this.debugLogging) {
1922
+ console.warn("Unknown websocket event", message);
1923
+ }
1924
+ break;
1925
+ }
1926
+ }
1927
+ }
1928
+ async refreshCredentials() {
1661
1929
  const { data } = await this.r.get(
1662
- `/nodes/${id}/configuration`
1663
- );
1664
- return data;
1665
- };
1666
- update = async (id, node) => {
1667
- z9.number().positive().parse(id);
1668
- node = NodeCreateSchema.parse(node);
1669
- const { data } = await this.r.patch(
1670
- `/nodes/${id}`,
1671
- node
1930
+ `/servers/${this.serverId}/websocket`
1672
1931
  );
1673
- return data.attributes;
1674
- };
1675
- delete = async (id) => {
1676
- z9.number().positive().parse(id);
1677
- await this.r.delete(`/nodes/${id}`);
1678
- };
1679
- allocations = (server_id) => new NodesAllocations(this.r, server_id);
1680
- };
1681
- var NodeCreateSchema = z9.object({
1682
- name: z9.string().min(1).max(100),
1683
- description: z9.string().optional(),
1684
- public: z9.boolean().optional(),
1685
- fqdn: z9.string().nonempty(),
1686
- scheme: z9.enum(["http", "https"]),
1687
- behind_proxy: z9.boolean().optional(),
1688
- memory: z9.number().min(0),
1689
- memory_overallocate: z9.number().min(-1),
1690
- disk: z9.number().min(0),
1691
- disk_overallocate: z9.number().min(-1),
1692
- cpu: z9.number().min(0),
1693
- cpu_overallocate: z9.number().min(-1),
1694
- daemon_base: z9.string().nonempty().optional(),
1695
- daemon_sftp: z9.number().min(1).max(65535),
1696
- daemon_sftp_alias: z9.string().optional(),
1697
- daemon_listen: z9.number().min(1).max(65535),
1698
- daemon_connect: z9.number().min(1).max(65535),
1699
- maintenance_mode: z9.boolean().optional(),
1700
- upload_size: z9.number().min(1).max(1024),
1701
- tags: z9.array(z9.string()).optional()
1702
- });
1703
-
1704
- // src/api/application/servers.ts
1705
- import z11 from "zod";
1706
-
1707
- // src/api/application/servers_databases.ts
1708
- import z10 from "zod";
1709
- var ServersDatabases = class {
1710
- r;
1711
- id;
1712
- constructor(r, server_id) {
1713
- this.r = r;
1714
- this.id = server_id;
1932
+ this.currentToken = data.data.token;
1933
+ return data.data.socket;
1715
1934
  }
1716
- list = async () => {
1717
- const { data } = await this.r.get(`/servers/${this.id}/databases`);
1718
- return data.data.map((d) => d.attributes);
1719
- };
1720
- create = async (database, remote, host) => {
1721
- database = z10.string().min(1).max(48).parse(database);
1722
- const { data } = await this.r.post(`/servers/${this.id}/databases`, { database, remote, host });
1723
- return data.attributes;
1724
- };
1725
- info = async (database_id) => {
1726
- const { data } = await this.r.get(`/servers/${this.id}/databases/${database_id}`);
1727
- return data.attributes;
1728
- };
1729
- delete = async (database_id) => {
1730
- await this.r.delete(`/servers/${this.id}/databases/${database_id}`);
1731
- };
1732
- resetPassword = async (database_id) => {
1733
- await this.r.post(
1734
- `/servers/${this.id}/databases/${database_id}/reset-password`
1935
+ async authenticate() {
1936
+ if (!this.socket) {
1937
+ throw new Error("No socket connection");
1938
+ }
1939
+ if (!this.currentToken) {
1940
+ throw new Error("Missing websocket token");
1941
+ }
1942
+ this.socket.send(
1943
+ JSON.stringify({ event: "auth", args: [this.currentToken] })
1735
1944
  );
1736
- };
1737
- };
1738
-
1739
- // src/api/application/servers.ts
1740
- var Servers = class {
1741
- r;
1742
- id;
1743
- databases;
1744
- constructor(r, server_id) {
1745
- this.r = r;
1746
- this.id = server_id;
1747
- this.databases = new ServersDatabases(this.r, this.id);
1748
1945
  }
1749
- info = async (include) => {
1750
- const { data } = await this.r.get(`/servers/${this.id}`, { params: { include: include?.join(",") } });
1751
- return data.attributes;
1752
- };
1753
- delete = async (force = false) => {
1754
- await this.r.delete(`/servers/${this.id}${force ? "/force" : ""}`);
1755
- };
1756
- updateDetails = async (opts) => {
1757
- opts = UpdateDetailsSchema.parse(opts);
1758
- await this.r.patch(`/servers/${this.id}/details`, opts);
1759
- };
1760
- updateBuild = async (opts) => {
1761
- opts = UpdateBuildSchema.parse(opts);
1762
- await this.r.patch(`/servers/${this.id}/build`, opts);
1763
- };
1764
- updateStartup = async (opts) => {
1765
- opts = UpdateStartupSchema.parse(opts);
1766
- await this.r.patch(`/servers/${this.id}/startup`, opts);
1767
- };
1768
- suspend = async () => {
1769
- await this.r.post(`/servers/${this.id}/suspend`);
1770
- };
1771
- unsuspend = async () => {
1772
- await this.r.post(`/servers/${this.id}/unsuspend`);
1773
- };
1774
- reinstall = async () => {
1775
- await this.r.post(`/servers/${this.id}/reinstall`);
1776
- };
1777
- transferStart = async (node_id, allocation_id, allocation_additional) => {
1778
- await this.r.post(`/servers/${this.id}/transfer`, {
1779
- node_id,
1780
- allocation_id,
1781
- allocation_additional
1782
- });
1783
- };
1784
- transferCancel = async () => {
1785
- await this.r.post(`/servers/${this.id}/transfer/cancel`);
1786
- };
1787
- };
1788
- var CreateServerSchema = z11.object({
1789
- external_id: z11.string().min(1).max(255).optional(),
1790
- name: z11.string().min(1).max(255),
1791
- description: z11.string().optional(),
1792
- user: z11.number(),
1793
- egg: z11.number(),
1794
- docker_image: z11.string().optional(),
1795
- startup: z11.string().optional(),
1796
- environment: z11.record(z11.string(), z11.string()),
1797
- skip_scripts: z11.boolean().optional(),
1798
- oom_killer: z11.boolean().optional(),
1799
- start_on_completion: z11.boolean().optional(),
1800
- docker_labels: z11.record(z11.string(), z11.string()).optional(),
1801
- limits: z11.object({
1802
- memory: z11.number().min(0),
1803
- swap: z11.number().min(-1),
1804
- disk: z11.number().min(0),
1805
- io: z11.number().min(0),
1806
- threads: z11.string().optional(),
1807
- cpu: z11.number().min(0)
1808
- }),
1809
- feature_limits: z11.object({
1810
- databases: z11.number().min(0),
1811
- allocations: z11.number().min(0),
1812
- backups: z11.number().min(0)
1813
- }),
1814
- allocation: z11.object({
1815
- default: z11.string(),
1816
- additional: z11.array(z11.string()).optional()
1817
- }).optional(),
1818
- deploy: z11.object({
1819
- tags: z11.array(z11.string()).optional(),
1820
- dedicated_ip: z11.boolean().optional(),
1821
- port_range: z11.array(z11.string()).optional()
1822
- }).optional()
1823
- });
1824
- var UpdateDetailsSchema = CreateServerSchema.pick({
1825
- external_id: true,
1826
- name: true,
1827
- user: true,
1828
- description: true,
1829
- docker_labels: true
1830
- });
1831
- var UpdateBuildSchema = CreateServerSchema.pick({
1832
- oom_killer: true,
1833
- limits: true,
1834
- feature_limits: true
1835
- }).extend({
1836
- allocation: z11.number().optional(),
1837
- add_allocations: z11.array(z11.string()).optional(),
1838
- remove_allocations: z11.array(z11.string()).optional()
1839
- });
1840
- var UpdateStartupSchema = CreateServerSchema.pick({
1841
- startup: true,
1842
- environment: true,
1843
- egg: true
1844
- }).extend({ image: z11.string().optional(), skip_scripts: z11.boolean() });
1845
-
1846
- // src/api/application/database_hosts.ts
1847
- import z12 from "zod";
1848
- var DatabaseHosts = class {
1849
- r;
1850
- constructor(r) {
1851
- this.r = r;
1946
+ disconnect() {
1947
+ this.detachMessageListener?.();
1948
+ this.detachMessageListener = void 0;
1949
+ if (this.socket) {
1950
+ this.socket.close();
1951
+ this.socket = void 0;
1952
+ }
1852
1953
  }
1853
- list = async (page = 1) => {
1854
- const { data } = await this.r.get("/database-hosts", { params: { page } });
1855
- return data.data.map((d) => d.attributes);
1856
- };
1857
- info = async (id) => {
1858
- const { data } = await this.r.get(`/database-hosts/${id}`);
1859
- return data.attributes;
1860
- };
1861
- // TODO: find out why API returns 500
1862
- create = async (opts) => {
1863
- opts = CreateDBHostSchema.parse(opts);
1864
- await this.r.post(
1865
- "/database-hosts",
1866
- opts
1867
- ).catch((e) => {
1868
- });
1869
- };
1870
- update = async (id, opts) => {
1871
- opts = CreateDBHostSchema.parse(opts);
1872
- const { data } = await this.r.patch(`/database-hosts/${id}`, opts);
1873
- return data.attributes;
1874
- };
1875
- delete = async (id) => {
1876
- await this.r.delete(`/database-hosts/${id}`);
1877
- };
1878
- };
1879
- var CreateDBHostSchema = z12.object({
1880
- name: z12.string().min(1).max(255),
1881
- host: z12.string(),
1882
- port: z12.number().min(1).max(65535),
1883
- username: z12.string().min(1).max(255),
1884
- password: z12.string().optional(),
1885
- node_ids: z12.array(z12.string()).optional(),
1886
- max_databases: z12.number().optional()
1887
- });
1888
-
1889
- // src/api/application/roles.ts
1890
- var Roles = class {
1891
- r;
1892
- constructor(r) {
1893
- this.r = r;
1954
+ requestStats() {
1955
+ this.send("send stats", [null]);
1894
1956
  }
1895
- list = async (page = 1) => {
1896
- const { data } = await this.r.get(`/roles`, { params: { page } });
1897
- return data.data.map((r) => r.attributes);
1898
- };
1899
- info = async (id) => {
1900
- const { data } = await this.r.get(
1901
- `/roles/${id}`
1902
- );
1903
- return data.attributes;
1904
- };
1905
- create = async (opts) => {
1906
- await this.r.post(`/roles`, opts);
1907
- };
1908
- update = async (id, opts) => {
1909
- await this.r.patch(`/roles/${id}`, opts);
1910
- };
1911
- delete = async (id) => {
1912
- await this.r.delete(`/roles/${id}`);
1913
- };
1914
- };
1915
-
1916
- // src/api/application/eggs.ts
1917
- var Eggs = class {
1918
- r;
1919
- constructor(r) {
1920
- this.r = r;
1957
+ requestLogs() {
1958
+ this.send("send logs", [null]);
1959
+ }
1960
+ send(event, args) {
1961
+ if (!this.socket) {
1962
+ if (this.debugLogging) {
1963
+ console.warn(
1964
+ `Attempted to send "${event}" without an active websocket connection`
1965
+ );
1966
+ }
1967
+ return;
1968
+ }
1969
+ this.socket.send(JSON.stringify({ event, args }));
1921
1970
  }
1922
- list = async () => {
1923
- const { data } = await this.r.get(
1924
- "/eggs"
1925
- );
1926
- return data.data.map((d) => d.attributes);
1927
- };
1928
- info = async (id) => {
1929
- const { data } = await this.r.get(
1930
- `/eggs/${id}`
1931
- );
1932
- return data.attributes;
1933
- };
1934
- export = async (id, format) => {
1935
- const { data } = await this.r.get(`/eggs/${id}/export`, {
1936
- params: { format },
1937
- transformResponse: (r) => r
1971
+ getStats() {
1972
+ return new Promise((resolve, reject) => {
1973
+ if (!this.socket) {
1974
+ reject(new Error("No socket connection"));
1975
+ return;
1976
+ }
1977
+ let off;
1978
+ const timeout = setTimeout(() => {
1979
+ off?.();
1980
+ reject(new Error("Timed out waiting for stats"));
1981
+ }, 5e3);
1982
+ off = this.on("stats" /* STATS */, (payload) => {
1983
+ clearTimeout(timeout);
1984
+ off?.();
1985
+ resolve(payload);
1986
+ });
1987
+ this.requestStats();
1938
1988
  });
1939
- return data;
1940
- };
1941
- infoExportable = async (id) => {
1942
- const { data } = await this.r.get(`/eggs/${id}/export`, {
1943
- params: { format: "json" }
1989
+ }
1990
+ getLogs() {
1991
+ return new Promise((resolve, reject) => {
1992
+ if (!this.socket) {
1993
+ reject(new Error("No socket connection"));
1994
+ return;
1995
+ }
1996
+ const lines = [];
1997
+ let off;
1998
+ let initialTimeout;
1999
+ let idleTimeout;
2000
+ const finalize = (payload) => {
2001
+ off?.();
2002
+ if (initialTimeout) {
2003
+ clearTimeout(initialTimeout);
2004
+ }
2005
+ if (idleTimeout) {
2006
+ clearTimeout(idleTimeout);
2007
+ }
2008
+ resolve(payload);
2009
+ };
2010
+ initialTimeout = setTimeout(() => {
2011
+ finalize(lines.length > 0 ? lines : [FALLBACK_LOG_MESSAGE]);
2012
+ }, 5e3);
2013
+ off = this.on("console output" /* CONSOLE_OUTPUT */, (line) => {
2014
+ lines.push(line);
2015
+ if (initialTimeout) {
2016
+ clearTimeout(initialTimeout);
2017
+ initialTimeout = void 0;
2018
+ }
2019
+ if (idleTimeout) {
2020
+ clearTimeout(idleTimeout);
2021
+ }
2022
+ idleTimeout = setTimeout(() => {
2023
+ finalize(lines);
2024
+ }, 1e3);
2025
+ });
2026
+ this.requestLogs();
1944
2027
  });
1945
- return data;
1946
- };
2028
+ }
2029
+ sendPoweraction(action) {
2030
+ this.send("set state", [action]);
2031
+ }
2032
+ sendCommand(cmd) {
2033
+ this.send("send command", [cmd]);
2034
+ }
1947
2035
  };
1948
2036
 
1949
- // src/api/application/mounts.ts
1950
- import z13 from "zod";
1951
- var Mounts = class {
2037
+ // src/api/client/server.ts
2038
+ var ServerClient = class {
1952
2039
  r;
1953
- constructor(r) {
1954
- this.r = r;
2040
+ id;
2041
+ activity;
2042
+ databases;
2043
+ files;
2044
+ schedules;
2045
+ allocations;
2046
+ users;
2047
+ backups;
2048
+ startup;
2049
+ variables;
2050
+ settings;
2051
+ constructor(requester, id) {
2052
+ this.r = requester;
2053
+ this.id = id;
2054
+ this.activity = new ServerActivity(requester, id);
2055
+ this.databases = new ServerDatabases(requester, id);
2056
+ this.files = new ServerFiles(requester, id);
2057
+ this.schedules = new ServerSchedules(requester, id);
2058
+ this.allocations = new ServerAllocations(requester, id);
2059
+ this.users = new ServerUsers(requester, id);
2060
+ this.backups = new ServerBackups(requester, id);
2061
+ this.startup = new ServerStartup(requester, id);
2062
+ this.variables = this.startup;
2063
+ this.settings = new ServerSettings(requester, id);
1955
2064
  }
1956
- list = async () => {
1957
- const { data } = await this.r.get("/mounts");
1958
- return data.data.map((d) => d.attributes);
1959
- };
1960
- info = async (id) => {
2065
+ info = async (include) => {
1961
2066
  const { data } = await this.r.get(
1962
- `/mounts/${id}`
2067
+ `/servers/${this.id}`,
2068
+ { params: { include: include?.join(",") } }
1963
2069
  );
1964
2070
  return data.attributes;
1965
2071
  };
1966
- create = async (opts) => {
1967
- opts = CreateMountSchema.parse(opts);
1968
- const { data } = await this.r.post(
1969
- "/mounts",
1970
- opts
1971
- );
1972
- return data.attributes;
2072
+ websocket = (stripColors = false) => {
2073
+ return new ServerWebsocket(this.r, this.id, stripColors);
1973
2074
  };
1974
- update = async (id, opts) => {
1975
- opts = CreateMountSchema.parse(opts);
1976
- const { data } = await this.r.patch(
1977
- `/mounts/${id}`,
1978
- opts
2075
+ resources = async () => {
2076
+ const { data } = await this.r.get(
2077
+ `/servers/${this.id}/resources`
1979
2078
  );
1980
2079
  return data.attributes;
1981
2080
  };
1982
- delete = async (id) => {
1983
- await this.r.delete(`/mounts/${id}`);
1984
- };
1985
- listAssignedEggs = async (id) => {
1986
- const { data } = await this.r.get(`/mounts/${id}/eggs`);
1987
- return data.data.map((d) => d.attributes);
1988
- };
1989
- assignEggs = async (id, eggs) => {
1990
- await this.r.post(`/mounts/${id}/eggs`, { eggs });
1991
- };
1992
- unassignEgg = async (id, egg_id) => {
1993
- await this.r.delete(`/mounts/${id}/eggs/${egg_id}`);
1994
- };
1995
- listAssignedNodes = async (id) => {
1996
- const { data } = await this.r.get(`/mounts/${id}/nodes`);
1997
- return data.data.map((d) => d.attributes);
1998
- };
1999
- assignNodes = async (id, nodes) => {
2000
- await this.r.post(`/mounts/${id}/nodes`, { nodes });
2001
- };
2002
- unassignNode = async (id, node_id) => {
2003
- await this.r.delete(`/mounts/${id}/nodes/${node_id}`);
2004
- };
2005
- listAssignedServers = async (id) => {
2006
- const { data } = await this.r.get(`/mounts/${id}/servers`);
2007
- return data.data.map((d) => d.attributes);
2008
- };
2009
- assignServers = async (id, servers) => {
2010
- await this.r.post(`/mounts/${id}/servers`, { servers });
2081
+ command = async (command) => {
2082
+ await this.r.post(`/servers/${this.id}/command`, { command });
2011
2083
  };
2012
- unassignServer = async (id, server_id) => {
2013
- await this.r.delete(`/mounts/${id}/servers/${server_id}`);
2084
+ power = async (signal) => {
2085
+ await this.r.post(`/servers/${this.id}/power`, { signal });
2014
2086
  };
2015
2087
  };
2016
- var CreateMountSchema = z13.object({
2017
- name: z13.string().min(1).max(255),
2018
- description: z13.string().optional(),
2019
- source: z13.string(),
2020
- target: z13.string(),
2021
- read_only: z13.boolean().optional()
2022
- });
2023
2088
 
2024
- // src/api/application/client.ts
2089
+ // src/api/client/client.ts
2025
2090
  var Client2 = class {
2091
+ account;
2026
2092
  r;
2027
- users;
2028
- nodes;
2029
- databaseHosts;
2030
- roles;
2031
- eggs;
2032
- mounts;
2033
2093
  constructor(requester) {
2034
2094
  this.r = requester;
2035
- this.users = new Users(requester);
2036
- this.nodes = new Nodes(requester);
2037
- this.databaseHosts = new DatabaseHosts(requester);
2038
- this.roles = new Roles(requester);
2039
- this.eggs = new Eggs(requester);
2040
- this.mounts = new Mounts(requester);
2095
+ this.account = new Account(requester);
2041
2096
  }
2042
2097
  get $r() {
2043
2098
  return this.r;
2044
2099
  }
2045
- listServers = async (search, page = 1) => {
2046
- const { data } = await this.r.get("/servers", { params: { search, page } });
2047
- return data.data.map((s) => s.attributes);
2048
- };
2049
- createServer = async (opts) => {
2050
- opts = CreateServerSchema.parse(opts);
2051
- const { data } = await this.r.post("/servers", opts);
2052
- return data.attributes;
2100
+ listPermissions = async () => {
2101
+ const { data } = await this.r.get("/permissions");
2102
+ return data.attributes.permissions;
2053
2103
  };
2054
- getServerByExternalId = async (external_id, include) => {
2055
- const { data } = await this.r.get(`/servers/external/${external_id}`, {
2056
- params: { include: include?.join(",") }
2057
- });
2058
- return data.attributes;
2104
+ listServers = async (type = "accessible", page = 1, per_page = 50, include) => {
2105
+ z14.number().positive().parse(page);
2106
+ const { data } = await this.r.get("/", { params: { type, page, include: include?.join(",") } });
2107
+ return data.data.map((s) => s.attributes);
2059
2108
  };
2060
- servers = (server_id) => new Servers(this.r, server_id);
2061
- };
2062
-
2063
- // src/api/base/request.ts
2064
- import z14 from "zod";
2065
- import axios3 from "axios";
2066
-
2067
- // src/api/base/types.ts
2068
- var PterodactylException = class extends Error {
2069
- data;
2070
- status;
2071
- constructor(message, data, status) {
2072
- super(message);
2073
- this.data = data;
2074
- this.status = status;
2075
- }
2076
- };
2077
-
2078
- // src/api/base/request.ts
2079
- var Agent = class {
2080
- base_url;
2081
- token;
2082
- requester;
2083
- constructor(url, token, type, suffix = "/api") {
2084
- this.base_url = z14.url("Invalid URL Schema").transform((url2) => new URL(url2).href).parse(url);
2085
- this.token = z14.string().regex(/^(ptl[ac]|pacc|papp)_.+$/, "Invalid token type").parse(token);
2086
- this.requester = axios3.create({
2087
- baseURL: this.base_url.replace(/\/+$/, "") + `${suffix}/${type}`,
2088
- timeout: 3e3,
2089
- headers: { Authorization: `Bearer ${this.token}` }
2090
- });
2091
- this.requester.interceptors.response.use(void 0, (error) => {
2092
- if (error.response && error.response.status === 400) {
2093
- return Promise.reject(
2094
- new PterodactylException(
2095
- "Invalid request data",
2096
- error.response.data,
2097
- error.response.status
2098
- )
2099
- );
2100
- }
2101
- return Promise.reject(error);
2102
- });
2103
- }
2109
+ server = (uuid) => new ServerClient(this.r, uuid);
2104
2110
  };
2105
2111
 
2106
2112
  // src/api/index.ts
2107
- var PelicanAPIClient = class extends Client {
2113
+ var PelicanAPIClient = class extends Client2 {
2108
2114
  constructor(url, token, suffix = "/api") {
2109
2115
  const ax = new Agent(url, token, "client", suffix);
2110
2116
  super(ax.requester);
2111
2117
  }
2112
2118
  };
2113
- var PelicanAPIApplication = class extends Client2 {
2119
+ var PelicanAPIApplication = class extends Client {
2114
2120
  constructor(url, token, suffix = "/api") {
2115
2121
  const ax = new Agent(url, token, "application", suffix);
2116
2122
  super(ax.requester);