@meowpanel/api 0.1.0-alpha.11 → 1.0.0-beta.2
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.
- package/LICENSE +1 -1
- package/README.md +40 -0
- package/esm/mod.d.ts +87 -1
- package/esm/mod.d.ts.map +1 -1
- package/esm/mod.js +85 -1
- package/esm/src/client.d.ts +248 -0
- package/esm/src/client.d.ts.map +1 -0
- package/esm/src/client.js +235 -0
- package/esm/src/emitter.d.ts +53 -0
- package/esm/src/emitter.d.ts.map +1 -0
- package/esm/src/emitter.js +55 -0
- package/esm/src/errors.d.ts +42 -0
- package/esm/src/errors.d.ts.map +1 -0
- package/esm/src/errors.js +45 -0
- package/esm/src/http.d.ts +157 -0
- package/esm/src/http.d.ts.map +1 -0
- package/esm/src/http.js +317 -0
- package/esm/src/namespace.d.ts +59 -0
- package/esm/src/namespace.d.ts.map +1 -0
- package/esm/src/namespace.js +79 -0
- package/esm/src/reactive.d.ts +29 -0
- package/esm/src/reactive.d.ts.map +1 -0
- package/esm/src/reactive.js +35 -0
- package/esm/src/resources/account.d.ts +125 -0
- package/esm/src/resources/account.d.ts.map +1 -0
- package/esm/src/resources/account.js +151 -0
- package/esm/src/resources/api-keys.d.ts +116 -0
- package/esm/src/resources/api-keys.d.ts.map +1 -0
- package/esm/src/resources/api-keys.js +105 -0
- package/esm/src/resources/auth.d.ts +57 -0
- package/esm/src/resources/auth.d.ts.map +1 -0
- package/esm/src/resources/auth.js +39 -0
- package/esm/src/resources/eggs.d.ts +238 -0
- package/esm/src/resources/eggs.d.ts.map +1 -0
- package/esm/src/resources/eggs.js +187 -0
- package/esm/src/resources/hosts.d.ts +62 -0
- package/esm/src/resources/hosts.d.ts.map +1 -0
- package/esm/src/resources/hosts.js +62 -0
- package/esm/src/resources/members.d.ts +138 -0
- package/esm/src/resources/members.d.ts.map +1 -0
- package/esm/src/resources/members.js +122 -0
- package/esm/src/resources/nodes.d.ts +397 -0
- package/esm/src/resources/nodes.d.ts.map +1 -0
- package/esm/src/resources/nodes.js +352 -0
- package/esm/src/resources/server-groups.d.ts +150 -0
- package/esm/src/resources/server-groups.d.ts.map +1 -0
- package/esm/src/resources/server-groups.js +111 -0
- package/esm/src/resources/servers/activity.d.ts +120 -0
- package/esm/src/resources/servers/activity.d.ts.map +1 -0
- package/esm/src/resources/servers/activity.js +163 -0
- package/esm/src/resources/servers/allocations.d.ts +125 -0
- package/esm/src/resources/servers/allocations.d.ts.map +1 -0
- package/esm/src/resources/servers/allocations.js +137 -0
- package/esm/src/resources/servers/connection.d.ts +108 -0
- package/esm/src/resources/servers/connection.d.ts.map +1 -0
- package/esm/src/resources/servers/connection.js +169 -0
- package/esm/src/resources/servers/files.d.ts +164 -0
- package/esm/src/resources/servers/files.d.ts.map +1 -0
- package/esm/src/resources/servers/files.js +281 -0
- package/esm/src/resources/servers/index.d.ts +126 -0
- package/esm/src/resources/servers/index.d.ts.map +1 -0
- package/esm/src/resources/servers/index.js +135 -0
- package/esm/src/resources/servers/profiles.d.ts +138 -0
- package/esm/src/resources/servers/profiles.d.ts.map +1 -0
- package/esm/src/resources/servers/profiles.js +177 -0
- package/esm/src/resources/servers/server.d.ts +136 -0
- package/esm/src/resources/servers/server.d.ts.map +1 -0
- package/esm/src/resources/servers/server.js +202 -0
- package/esm/src/resources/servers/sftp.d.ts +147 -0
- package/esm/src/resources/servers/sftp.d.ts.map +1 -0
- package/esm/src/resources/servers/sftp.js +153 -0
- package/esm/src/resources/servers/types.d.ts +199 -0
- package/esm/src/resources/servers/types.d.ts.map +1 -0
- package/esm/src/resources/servers/types.js +1 -0
- package/esm/src/sse.d.ts +53 -0
- package/esm/src/sse.d.ts.map +1 -0
- package/esm/src/sse.js +133 -0
- package/esm/src/types.d.ts +126 -0
- package/esm/src/types.d.ts.map +1 -0
- package/esm/src/types.js +21 -0
- package/package.json +4 -92
- package/script/mod.d.ts +87 -1
- package/script/mod.d.ts.map +1 -1
- package/script/mod.js +132 -3
- package/script/src/client.d.ts +248 -0
- package/script/src/client.d.ts.map +1 -0
- package/script/src/client.js +239 -0
- package/script/src/emitter.d.ts +53 -0
- package/script/src/emitter.d.ts.map +1 -0
- package/script/src/emitter.js +59 -0
- package/script/src/errors.d.ts +42 -0
- package/script/src/errors.d.ts.map +1 -0
- package/script/src/errors.js +49 -0
- package/script/src/http.d.ts +157 -0
- package/script/src/http.d.ts.map +1 -0
- package/script/src/http.js +321 -0
- package/script/src/namespace.d.ts +59 -0
- package/script/src/namespace.d.ts.map +1 -0
- package/script/src/namespace.js +83 -0
- package/script/src/reactive.d.ts +29 -0
- package/script/src/reactive.d.ts.map +1 -0
- package/script/src/reactive.js +39 -0
- package/script/src/resources/account.d.ts +125 -0
- package/script/src/resources/account.d.ts.map +1 -0
- package/script/src/resources/account.js +158 -0
- package/script/src/resources/api-keys.d.ts +116 -0
- package/script/src/resources/api-keys.d.ts.map +1 -0
- package/script/src/resources/api-keys.js +110 -0
- package/script/src/resources/auth.d.ts +57 -0
- package/script/src/resources/auth.d.ts.map +1 -0
- package/script/src/resources/auth.js +43 -0
- package/script/src/resources/eggs.d.ts +238 -0
- package/script/src/resources/eggs.d.ts.map +1 -0
- package/script/src/resources/eggs.js +193 -0
- package/script/src/resources/hosts.d.ts +62 -0
- package/script/src/resources/hosts.d.ts.map +1 -0
- package/script/src/resources/hosts.js +67 -0
- package/script/src/resources/members.d.ts +138 -0
- package/script/src/resources/members.d.ts.map +1 -0
- package/script/src/resources/members.js +127 -0
- package/script/src/resources/nodes.d.ts +397 -0
- package/script/src/resources/nodes.d.ts.map +1 -0
- package/script/src/resources/nodes.js +362 -0
- package/script/src/resources/server-groups.d.ts +150 -0
- package/script/src/resources/server-groups.d.ts.map +1 -0
- package/script/src/resources/server-groups.js +116 -0
- package/script/src/resources/servers/activity.d.ts +120 -0
- package/script/src/resources/servers/activity.d.ts.map +1 -0
- package/script/src/resources/servers/activity.js +170 -0
- package/script/src/resources/servers/allocations.d.ts +125 -0
- package/script/src/resources/servers/allocations.d.ts.map +1 -0
- package/script/src/resources/servers/allocations.js +142 -0
- package/script/src/resources/servers/connection.d.ts +108 -0
- package/script/src/resources/servers/connection.d.ts.map +1 -0
- package/script/src/resources/servers/connection.js +173 -0
- package/script/src/resources/servers/files.d.ts +164 -0
- package/script/src/resources/servers/files.d.ts.map +1 -0
- package/script/src/resources/servers/files.js +286 -0
- package/script/src/resources/servers/index.d.ts +126 -0
- package/script/src/resources/servers/index.d.ts.map +1 -0
- package/script/src/resources/servers/index.js +139 -0
- package/script/src/resources/servers/profiles.d.ts +138 -0
- package/script/src/resources/servers/profiles.d.ts.map +1 -0
- package/script/src/resources/servers/profiles.js +182 -0
- package/script/src/resources/servers/server.d.ts +136 -0
- package/script/src/resources/servers/server.d.ts.map +1 -0
- package/script/src/resources/servers/server.js +206 -0
- package/script/src/resources/servers/sftp.d.ts +147 -0
- package/script/src/resources/servers/sftp.d.ts.map +1 -0
- package/script/src/resources/servers/sftp.js +158 -0
- package/script/src/resources/servers/types.d.ts +199 -0
- package/script/src/resources/servers/types.d.ts.map +1 -0
- package/script/src/resources/servers/types.js +2 -0
- package/script/src/sse.d.ts +53 -0
- package/script/src/sse.d.ts.map +1 -0
- package/script/src/sse.js +137 -0
- package/script/src/types.d.ts +126 -0
- package/script/src/types.d.ts.map +1 -0
- package/script/src/types.js +25 -0
- package/esm/api.d.ts +0 -76
- package/esm/api.d.ts.map +0 -1
- package/esm/api.js +0 -187
- package/esm/components/auth.d.ts +0 -15
- package/esm/components/auth.d.ts.map +0 -1
- package/esm/components/auth.js +0 -13
- package/esm/components/connection/abstract.d.ts +0 -23
- package/esm/components/connection/abstract.d.ts.map +0 -1
- package/esm/components/connection/abstract.js +0 -43
- package/esm/components/connection/sse.d.ts +0 -20
- package/esm/components/connection/sse.d.ts.map +0 -1
- package/esm/components/connection/sse.js +0 -65
- package/esm/components/egg/egg.d.ts +0 -32
- package/esm/components/egg/egg.d.ts.map +0 -1
- package/esm/components/egg/egg.js +0 -70
- package/esm/components/group.d.ts +0 -14
- package/esm/components/group.d.ts.map +0 -1
- package/esm/components/group.js +0 -33
- package/esm/components/member_2.d.ts +0 -69
- package/esm/components/member_2.d.ts.map +0 -1
- package/esm/components/member_2.js +0 -140
- package/esm/components/node.d.ts +0 -78
- package/esm/components/node.d.ts.map +0 -1
- package/esm/components/node.js +0 -342
- package/esm/components/server/activity.d.ts +0 -45
- package/esm/components/server/activity.d.ts.map +0 -1
- package/esm/components/server/activity.js +0 -158
- package/esm/components/server/allocations.d.ts +0 -45
- package/esm/components/server/allocations.d.ts.map +0 -1
- package/esm/components/server/allocations.js +0 -146
- package/esm/components/server/files.d.ts +0 -50
- package/esm/components/server/files.d.ts.map +0 -1
- package/esm/components/server/files.js +0 -90
- package/esm/components/server/power.d.ts +0 -21
- package/esm/components/server/power.d.ts.map +0 -1
- package/esm/components/server/power.js +0 -43
- package/esm/components/server/profiles.d.ts +0 -63
- package/esm/components/server/profiles.d.ts.map +0 -1
- package/esm/components/server/profiles.js +0 -163
- package/esm/components/server/server.d.ts +0 -141
- package/esm/components/server/server.d.ts.map +0 -1
- package/esm/components/server/server.js +0 -358
- package/esm/components/server/sftp.d.ts +0 -31
- package/esm/components/server/sftp.d.ts.map +0 -1
- package/esm/components/server/sftp.js +0 -84
- package/esm/components/server/startup.d.ts +0 -17
- package/esm/components/server/startup.d.ts.map +0 -1
- package/esm/components/server/startup.js +0 -21
- package/esm/components/server/stats.d.ts +0 -19
- package/esm/components/server/stats.d.ts.map +0 -1
- package/esm/components/server/stats.js +0 -46
- package/esm/utils/cache.d.ts +0 -20
- package/esm/utils/cache.d.ts.map +0 -1
- package/esm/utils/cache.js +0 -66
- package/esm/utils/event.d.ts +0 -38
- package/esm/utils/event.d.ts.map +0 -1
- package/esm/utils/event.js +0 -71
- package/esm/utils/object.d.ts +0 -58
- package/esm/utils/object.d.ts.map +0 -1
- package/esm/utils/object.js +0 -86
- package/esm/utils/subscribe.d.ts +0 -37
- package/esm/utils/subscribe.d.ts.map +0 -1
- package/esm/utils/subscribe.js +0 -29
- package/esm/utils/timer.d.ts +0 -13
- package/esm/utils/timer.d.ts.map +0 -1
- package/esm/utils/timer.js +0 -32
- package/esm/utils/units.d.ts +0 -18
- package/esm/utils/units.d.ts.map +0 -1
- package/esm/utils/units.js +0 -82
- package/script/api.d.ts +0 -76
- package/script/api.d.ts.map +0 -1
- package/script/api.js +0 -192
- package/script/components/auth.d.ts +0 -15
- package/script/components/auth.d.ts.map +0 -1
- package/script/components/auth.js +0 -17
- package/script/components/connection/abstract.d.ts +0 -23
- package/script/components/connection/abstract.d.ts.map +0 -1
- package/script/components/connection/abstract.js +0 -47
- package/script/components/connection/sse.d.ts +0 -20
- package/script/components/connection/sse.d.ts.map +0 -1
- package/script/components/connection/sse.js +0 -69
- package/script/components/egg/egg.d.ts +0 -32
- package/script/components/egg/egg.d.ts.map +0 -1
- package/script/components/egg/egg.js +0 -74
- package/script/components/group.d.ts +0 -14
- package/script/components/group.d.ts.map +0 -1
- package/script/components/group.js +0 -37
- package/script/components/member_2.d.ts +0 -69
- package/script/components/member_2.d.ts.map +0 -1
- package/script/components/member_2.js +0 -146
- package/script/components/node.d.ts +0 -78
- package/script/components/node.d.ts.map +0 -1
- package/script/components/node.js +0 -349
- package/script/components/server/activity.d.ts +0 -45
- package/script/components/server/activity.d.ts.map +0 -1
- package/script/components/server/activity.js +0 -164
- package/script/components/server/allocations.d.ts +0 -45
- package/script/components/server/allocations.d.ts.map +0 -1
- package/script/components/server/allocations.js +0 -152
- package/script/components/server/files.d.ts +0 -50
- package/script/components/server/files.d.ts.map +0 -1
- package/script/components/server/files.js +0 -94
- package/script/components/server/power.d.ts +0 -21
- package/script/components/server/power.d.ts.map +0 -1
- package/script/components/server/power.js +0 -47
- package/script/components/server/profiles.d.ts +0 -63
- package/script/components/server/profiles.d.ts.map +0 -1
- package/script/components/server/profiles.js +0 -168
- package/script/components/server/server.d.ts +0 -141
- package/script/components/server/server.d.ts.map +0 -1
- package/script/components/server/server.js +0 -362
- package/script/components/server/sftp.d.ts +0 -31
- package/script/components/server/sftp.d.ts.map +0 -1
- package/script/components/server/sftp.js +0 -88
- package/script/components/server/startup.d.ts +0 -17
- package/script/components/server/startup.d.ts.map +0 -1
- package/script/components/server/startup.js +0 -25
- package/script/components/server/stats.d.ts +0 -19
- package/script/components/server/stats.d.ts.map +0 -1
- package/script/components/server/stats.js +0 -50
- package/script/utils/cache.d.ts +0 -20
- package/script/utils/cache.d.ts.map +0 -1
- package/script/utils/cache.js +0 -70
- package/script/utils/event.d.ts +0 -38
- package/script/utils/event.d.ts.map +0 -1
- package/script/utils/event.js +0 -75
- package/script/utils/object.d.ts +0 -58
- package/script/utils/object.d.ts.map +0 -1
- package/script/utils/object.js +0 -90
- package/script/utils/subscribe.d.ts +0 -37
- package/script/utils/subscribe.d.ts.map +0 -1
- package/script/utils/subscribe.js +0 -32
- package/script/utils/timer.d.ts +0 -13
- package/script/utils/timer.d.ts.map +0 -1
- package/script/utils/timer.js +0 -36
- package/script/utils/units.d.ts +0 -18
- package/script/utils/units.d.ts.map +0 -1
- package/script/utils/units.js +0 -86
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ServerConnection = void 0;
|
|
4
|
+
const reactive_js_1 = require("../../reactive.js");
|
|
5
|
+
const sse_js_1 = require("../../sse.js");
|
|
6
|
+
/**
|
|
7
|
+
* A real-time SSE connection to a specific server on its Whisker node daemon.
|
|
8
|
+
*
|
|
9
|
+
* Emits typed events for console output, power state changes, and resource utilization.
|
|
10
|
+
* Also maintains a rolling buffer of console output lines in {@link output}, pre-populated
|
|
11
|
+
* with recent history fetched from the node on {@link connect}.
|
|
12
|
+
*
|
|
13
|
+
* Obtain a connection via {@link import('./server.ts').Server.connect}.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts ignore
|
|
17
|
+
* const conn = await server.connect();
|
|
18
|
+
*
|
|
19
|
+
* conn.on('status', (state) => console.log('Power state:', state));
|
|
20
|
+
* conn.on('stats', (stats) => console.log('CPU:', stats.cpuAbsolute + '%'));
|
|
21
|
+
*
|
|
22
|
+
* await conn.connect();
|
|
23
|
+
*
|
|
24
|
+
* // Reactive: $conn.output is the full rolling buffer of console lines
|
|
25
|
+
* // Live lines are appended automatically as they arrive over SSE
|
|
26
|
+
*
|
|
27
|
+
* // Later...
|
|
28
|
+
* conn.disconnect();
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
class ServerConnection extends reactive_js_1.ReactiveResource {
|
|
32
|
+
serverId;
|
|
33
|
+
nodeUrl;
|
|
34
|
+
token;
|
|
35
|
+
/** Maximum number of console output lines retained in {@link ServerConnection.output}. */
|
|
36
|
+
static maxOutputLines = 1024;
|
|
37
|
+
listeners = new Map();
|
|
38
|
+
sse;
|
|
39
|
+
connected = false;
|
|
40
|
+
/**
|
|
41
|
+
* Rolling buffer of console output lines, capped at {@link MAX_OUTPUT_LINES}.
|
|
42
|
+
*
|
|
43
|
+
* Pre-populated with recent history from the node when {@link connect} is called,
|
|
44
|
+
* then appended to live as `console_output` SSE events arrive.
|
|
45
|
+
*
|
|
46
|
+
* Use as a reactive store: `$conn.output`.
|
|
47
|
+
*/
|
|
48
|
+
output = [];
|
|
49
|
+
/** @internal */
|
|
50
|
+
constructor(serverId, nodeUrl, token) {
|
|
51
|
+
super();
|
|
52
|
+
this.serverId = serverId;
|
|
53
|
+
this.nodeUrl = nodeUrl;
|
|
54
|
+
this.token = token;
|
|
55
|
+
this.sse = new sse_js_1.SseConnection(`${nodeUrl}/api/servers/${serverId}/sse`, token);
|
|
56
|
+
}
|
|
57
|
+
/** `true` if the SSE connection is currently open. */
|
|
58
|
+
get isConnected() {
|
|
59
|
+
return this.connected;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Fetch console history and open the SSE stream.
|
|
63
|
+
*
|
|
64
|
+
* Populates {@link output} with recent history from the node, then begins streaming
|
|
65
|
+
* live events.
|
|
66
|
+
*
|
|
67
|
+
* Maintains only one instances of the stream.
|
|
68
|
+
*
|
|
69
|
+
* @param onError Optional callback invoked if the connection drops unexpectedly.
|
|
70
|
+
*/
|
|
71
|
+
async open(onError) {
|
|
72
|
+
this.sse.disconnect(); // close any existing stream
|
|
73
|
+
try {
|
|
74
|
+
const res = await fetch(`${this.nodeUrl}/api/servers/${this.serverId}/logs?size=100`, { headers: { Authorization: `Bearer ${this.token}` } });
|
|
75
|
+
if (res.ok) {
|
|
76
|
+
const json = (await res.json());
|
|
77
|
+
this.output = json.data ?? [];
|
|
78
|
+
this.notify();
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
catch {
|
|
82
|
+
// History is best-effort - don't block the SSE connection
|
|
83
|
+
}
|
|
84
|
+
const url = `${this.nodeUrl}/api/servers/${this.serverId}/sse`;
|
|
85
|
+
this.sse = new sse_js_1.SseConnection(url, this.token);
|
|
86
|
+
this.connected = true;
|
|
87
|
+
this.sse.connect((event, data) => {
|
|
88
|
+
this.dispatch(event, data);
|
|
89
|
+
}, (err) => {
|
|
90
|
+
this.connected = false;
|
|
91
|
+
this.notify();
|
|
92
|
+
onError?.(err);
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
/** Close the SSE stream and stop receiving events. */
|
|
96
|
+
close() {
|
|
97
|
+
this.sse.disconnect();
|
|
98
|
+
this.connected = false;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Register a handler for a typed server event.
|
|
102
|
+
*
|
|
103
|
+
* @param event The event name.
|
|
104
|
+
* @param handler The callback to invoke when the event is received.
|
|
105
|
+
*/
|
|
106
|
+
on(event, handler) {
|
|
107
|
+
if (!this.listeners.has(event))
|
|
108
|
+
this.listeners.set(event, new Set());
|
|
109
|
+
this.listeners
|
|
110
|
+
.get(event)
|
|
111
|
+
.add(handler);
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Unregister a previously registered event handler.
|
|
115
|
+
*
|
|
116
|
+
* @param event The event name.
|
|
117
|
+
* @param handler The exact handler reference passed to {@link on}.
|
|
118
|
+
*/
|
|
119
|
+
off(event, handler) {
|
|
120
|
+
this.listeners
|
|
121
|
+
.get(event)
|
|
122
|
+
?.delete(handler);
|
|
123
|
+
}
|
|
124
|
+
/** Remove all event handlers for all event types. */
|
|
125
|
+
removeAllListeners() {
|
|
126
|
+
this.listeners.clear();
|
|
127
|
+
}
|
|
128
|
+
/** Parse and dispatch a raw SSE event to registered handlers. */
|
|
129
|
+
dispatch(event, rawData) {
|
|
130
|
+
let payload;
|
|
131
|
+
try {
|
|
132
|
+
if (event === 'console_output') {
|
|
133
|
+
payload = rawData;
|
|
134
|
+
this.output.push(payload);
|
|
135
|
+
if (this.output.length > ServerConnection.maxOutputLines) {
|
|
136
|
+
this.output.shift();
|
|
137
|
+
}
|
|
138
|
+
this.notify();
|
|
139
|
+
}
|
|
140
|
+
else if (event === 'status') {
|
|
141
|
+
const parsed = JSON.parse(rawData);
|
|
142
|
+
if (typeof parsed?.state !== 'string')
|
|
143
|
+
return;
|
|
144
|
+
payload = parsed.state;
|
|
145
|
+
}
|
|
146
|
+
else if (event === 'stats') {
|
|
147
|
+
const raw = JSON.parse(rawData);
|
|
148
|
+
if (typeof raw?.cpu_absolute !== 'number' ||
|
|
149
|
+
typeof raw?.memory_bytes !== 'number') {
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
payload = {
|
|
153
|
+
cpuAbsolute: raw.cpu_absolute,
|
|
154
|
+
memoryBytes: raw.memory_bytes,
|
|
155
|
+
memoryLimitBytes: raw.memory_limit_bytes,
|
|
156
|
+
diskBytes: raw.disk_bytes,
|
|
157
|
+
networkRxBytes: raw.network?.rx_bytes ?? 0,
|
|
158
|
+
networkTxBytes: raw.network?.tx_bytes ?? 0,
|
|
159
|
+
uptimeSeconds: raw.uptime,
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
else {
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
catch {
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
const handlers = this.listeners.get(event);
|
|
170
|
+
handlers?.forEach((h) => h(payload));
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
exports.ServerConnection = ServerConnection;
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import type { NanoID } from '../../types.js';
|
|
2
|
+
import type { FileEntryData } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* A single file or directory entry in a server's file system.
|
|
5
|
+
*/
|
|
6
|
+
export declare class FileEntry {
|
|
7
|
+
/** The name of the file or directory. */
|
|
8
|
+
readonly name: string;
|
|
9
|
+
/** Symbolic file mode string, e.g. `"-rw-r--r--"`. */
|
|
10
|
+
readonly mode: string;
|
|
11
|
+
/** Numeric mode bits (octal), e.g. `"644"`. */
|
|
12
|
+
readonly modeBits: string;
|
|
13
|
+
/** Size of the file in bytes. `0` for directories. */
|
|
14
|
+
readonly size: number;
|
|
15
|
+
/** `true` if this entry is a directory. */
|
|
16
|
+
readonly isDirectory: boolean;
|
|
17
|
+
/** MIME type of the file, e.g. `"text/plain"`. */
|
|
18
|
+
readonly mime: string;
|
|
19
|
+
/** `true` if this entry is a symbolic link. */
|
|
20
|
+
readonly isSymlink: boolean;
|
|
21
|
+
/** When this file or directory was created. */
|
|
22
|
+
readonly createdAt: Date;
|
|
23
|
+
/** When this file or directory was last modified. */
|
|
24
|
+
readonly modifiedAt: Date;
|
|
25
|
+
/** @internal */
|
|
26
|
+
constructor(data: FileEntryData);
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Manages file system operations for a specific server via the Whisker node daemon API.
|
|
30
|
+
*
|
|
31
|
+
* File operations communicate directly with the node daemon.
|
|
32
|
+
* The node URL is resolved lazily on first use and cached for subsequent calls.
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```ts ignore
|
|
36
|
+
* const entries = await server.files.list('/');
|
|
37
|
+
* const content = await server.files.read('/server.properties');
|
|
38
|
+
* await server.files.write('/server.properties', 'motd=Hello World\n');
|
|
39
|
+
* await server.files.move({ from: '/old.txt', to: '/new.txt' });
|
|
40
|
+
* await server.files.delete(['/backup.zip']);
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
export declare class ServerFilesManager {
|
|
44
|
+
private readonly serverId;
|
|
45
|
+
private readonly token;
|
|
46
|
+
private readonly resolveNodeUrl;
|
|
47
|
+
private nodeUrl?;
|
|
48
|
+
/**
|
|
49
|
+
* @param serverId The server's NanoID.
|
|
50
|
+
* @param token Bearer token for node authentication.
|
|
51
|
+
* @param resolveNodeUrl Async function that returns the daemon base URL for this server's node.
|
|
52
|
+
*/
|
|
53
|
+
constructor(serverId: NanoID, token: string, resolveNodeUrl: () => Promise<string>);
|
|
54
|
+
/** Lazily resolve and cache the node daemon URL. */
|
|
55
|
+
private getNodeUrl;
|
|
56
|
+
/** Build a full node API URL for a file path endpoint. */
|
|
57
|
+
private buildFileUrl;
|
|
58
|
+
/** Shared auth headers for node requests. */
|
|
59
|
+
private get authHeaders();
|
|
60
|
+
/** Perform a raw fetch to the node and throw on non-2xx. */
|
|
61
|
+
private nodeFetch;
|
|
62
|
+
/**
|
|
63
|
+
* List the contents of a directory.
|
|
64
|
+
*
|
|
65
|
+
* @param path Absolute path to the directory, e.g. `"/"` or `"/plugins"`.
|
|
66
|
+
* @returns An array of {@link FileEntry} objects.
|
|
67
|
+
*/
|
|
68
|
+
list(path: string): Promise<FileEntry[]>;
|
|
69
|
+
/**
|
|
70
|
+
* Read the text content of a file.
|
|
71
|
+
*
|
|
72
|
+
* @param path Absolute path to the file, e.g. `"/server.properties"`.
|
|
73
|
+
* @returns The file content as a UTF-8 string.
|
|
74
|
+
*/
|
|
75
|
+
read(path: string): Promise<string>;
|
|
76
|
+
/**
|
|
77
|
+
* Read the raw binary content of a file.
|
|
78
|
+
*
|
|
79
|
+
* @param path Absolute path to the file.
|
|
80
|
+
* @returns The file content as an `ArrayBuffer`.
|
|
81
|
+
*/
|
|
82
|
+
readBinary(path: string): Promise<ArrayBuffer>;
|
|
83
|
+
/**
|
|
84
|
+
* Write text content to a file, creating it if it doesn't exist.
|
|
85
|
+
*
|
|
86
|
+
* @param path Absolute path to the file.
|
|
87
|
+
* @param content The UTF-8 text content to write.
|
|
88
|
+
*/
|
|
89
|
+
write(path: string, content: string): Promise<void>;
|
|
90
|
+
/**
|
|
91
|
+
* Write raw binary content to a file.
|
|
92
|
+
*
|
|
93
|
+
* @param path Absolute path to the file.
|
|
94
|
+
* @param content Binary content to write.
|
|
95
|
+
* @param contentType MIME type of the content.
|
|
96
|
+
*/
|
|
97
|
+
writeBinary(path: string, content: ArrayBuffer | Uint8Array, contentType: string): Promise<void>;
|
|
98
|
+
/**
|
|
99
|
+
* Delete one or more files or directories.
|
|
100
|
+
*
|
|
101
|
+
* @param paths An array of absolute paths to delete.
|
|
102
|
+
*/
|
|
103
|
+
delete(paths: string[]): Promise<void>;
|
|
104
|
+
/**
|
|
105
|
+
* Move (rename) a file or directory.
|
|
106
|
+
*
|
|
107
|
+
* @param from Absolute source path.
|
|
108
|
+
* @param to Absolute destination path.
|
|
109
|
+
*/
|
|
110
|
+
move(from: string, to: string): Promise<void>;
|
|
111
|
+
/**
|
|
112
|
+
* Copy a file or directory to a new location.
|
|
113
|
+
*
|
|
114
|
+
* @param source Absolute path of the source file or directory.
|
|
115
|
+
* @param location Absolute path of the destination directory.
|
|
116
|
+
*/
|
|
117
|
+
copy(source: string, location: string): Promise<void>;
|
|
118
|
+
/**
|
|
119
|
+
* Create an empty file or directory.
|
|
120
|
+
*
|
|
121
|
+
* @param path Absolute path of the new file or directory.
|
|
122
|
+
* @param directory When `true`, creates a directory instead of a file.
|
|
123
|
+
*/
|
|
124
|
+
create(path: string, directory?: boolean): Promise<void>;
|
|
125
|
+
/**
|
|
126
|
+
* Compress one or more files into an archive.
|
|
127
|
+
*
|
|
128
|
+
* @param files Array of absolute paths to include in the archive.
|
|
129
|
+
* @param name Name of the output archive file, e.g. `"backup.tar.gz"`.
|
|
130
|
+
* @param rootPath Root directory for resolving relative paths. Defaults to `"/"`.
|
|
131
|
+
*/
|
|
132
|
+
compress(files: string[], name: string, rootPath?: string): Promise<void>;
|
|
133
|
+
/**
|
|
134
|
+
* Decompress (extract) an archive file.
|
|
135
|
+
*
|
|
136
|
+
* @param file Absolute path to the archive to extract.
|
|
137
|
+
* @param rootPath Directory to extract the archive into. Defaults to `"/"`.
|
|
138
|
+
*/
|
|
139
|
+
decompress(file: string, rootPath?: string): Promise<void>;
|
|
140
|
+
/**
|
|
141
|
+
* Change the permissions (chmod) of one or more files.
|
|
142
|
+
*
|
|
143
|
+
* @param entries Array of `{ file, mode }` objects.
|
|
144
|
+
* @param rootPath Root directory for resolving paths. Defaults to `"/"`.
|
|
145
|
+
*
|
|
146
|
+
* @example
|
|
147
|
+
* ```ts ignore
|
|
148
|
+
* await server.files.chmod([{ file: 'start.sh', mode: '755' }]);
|
|
149
|
+
* ```
|
|
150
|
+
*/
|
|
151
|
+
chmod(entries: {
|
|
152
|
+
file: string;
|
|
153
|
+
mode: string;
|
|
154
|
+
}[], rootPath?: string): Promise<void>;
|
|
155
|
+
/**
|
|
156
|
+
* Upload a file to the server using multipart form data.
|
|
157
|
+
*
|
|
158
|
+
* @param path Absolute directory path to upload the file into.
|
|
159
|
+
* @param filename The name for the uploaded file.
|
|
160
|
+
* @param content The file content as a `Blob`, `File`, or `ArrayBuffer`.
|
|
161
|
+
*/
|
|
162
|
+
upload(path: string, filename: string, content: Blob | File | ArrayBuffer): Promise<void>;
|
|
163
|
+
}
|
|
164
|
+
//# sourceMappingURL=files.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"files.d.ts","sourceRoot":"","sources":["../../../../src/src/resources/servers/files.ts"],"names":[],"mappings":"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"}
|
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ServerFilesManager = exports.FileEntry = void 0;
|
|
4
|
+
const errors_js_1 = require("../../errors.js");
|
|
5
|
+
/**
|
|
6
|
+
* A single file or directory entry in a server's file system.
|
|
7
|
+
*/
|
|
8
|
+
class FileEntry {
|
|
9
|
+
/** The name of the file or directory. */
|
|
10
|
+
name;
|
|
11
|
+
/** Symbolic file mode string, e.g. `"-rw-r--r--"`. */
|
|
12
|
+
mode;
|
|
13
|
+
/** Numeric mode bits (octal), e.g. `"644"`. */
|
|
14
|
+
modeBits;
|
|
15
|
+
/** Size of the file in bytes. `0` for directories. */
|
|
16
|
+
size;
|
|
17
|
+
/** `true` if this entry is a directory. */
|
|
18
|
+
isDirectory;
|
|
19
|
+
/** MIME type of the file, e.g. `"text/plain"`. */
|
|
20
|
+
mime;
|
|
21
|
+
/** `true` if this entry is a symbolic link. */
|
|
22
|
+
isSymlink;
|
|
23
|
+
/** When this file or directory was created. */
|
|
24
|
+
createdAt;
|
|
25
|
+
/** When this file or directory was last modified. */
|
|
26
|
+
modifiedAt;
|
|
27
|
+
/** @internal */
|
|
28
|
+
constructor(data) {
|
|
29
|
+
this.name = data.name;
|
|
30
|
+
this.mode = data.mode;
|
|
31
|
+
this.modeBits = data.mode_bits;
|
|
32
|
+
this.size = data.size;
|
|
33
|
+
this.isDirectory = data.type === 'directory';
|
|
34
|
+
this.mime = data.mime;
|
|
35
|
+
this.isSymlink = data.symlink;
|
|
36
|
+
this.createdAt = new Date(data.created);
|
|
37
|
+
this.modifiedAt = new Date(data.modified);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
exports.FileEntry = FileEntry;
|
|
41
|
+
/**
|
|
42
|
+
* Manages file system operations for a specific server via the Whisker node daemon API.
|
|
43
|
+
*
|
|
44
|
+
* File operations communicate directly with the node daemon.
|
|
45
|
+
* The node URL is resolved lazily on first use and cached for subsequent calls.
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```ts ignore
|
|
49
|
+
* const entries = await server.files.list('/');
|
|
50
|
+
* const content = await server.files.read('/server.properties');
|
|
51
|
+
* await server.files.write('/server.properties', 'motd=Hello World\n');
|
|
52
|
+
* await server.files.move({ from: '/old.txt', to: '/new.txt' });
|
|
53
|
+
* await server.files.delete(['/backup.zip']);
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
class ServerFilesManager {
|
|
57
|
+
serverId;
|
|
58
|
+
token;
|
|
59
|
+
resolveNodeUrl;
|
|
60
|
+
nodeUrl;
|
|
61
|
+
/**
|
|
62
|
+
* @param serverId The server's NanoID.
|
|
63
|
+
* @param token Bearer token for node authentication.
|
|
64
|
+
* @param resolveNodeUrl Async function that returns the daemon base URL for this server's node.
|
|
65
|
+
*/
|
|
66
|
+
constructor(serverId, token, resolveNodeUrl) {
|
|
67
|
+
this.serverId = serverId;
|
|
68
|
+
this.token = token;
|
|
69
|
+
this.resolveNodeUrl = resolveNodeUrl;
|
|
70
|
+
}
|
|
71
|
+
/** Lazily resolve and cache the node daemon URL. */
|
|
72
|
+
async getNodeUrl() {
|
|
73
|
+
if (!this.nodeUrl)
|
|
74
|
+
this.nodeUrl = await this.resolveNodeUrl();
|
|
75
|
+
return this.nodeUrl;
|
|
76
|
+
}
|
|
77
|
+
/** Build a full node API URL for a file path endpoint. */
|
|
78
|
+
async buildFileUrl(endpoint) {
|
|
79
|
+
const base = await this.getNodeUrl();
|
|
80
|
+
return `${base}/api/servers/${this.serverId}/files${endpoint}`;
|
|
81
|
+
}
|
|
82
|
+
/** Shared auth headers for node requests. */
|
|
83
|
+
get authHeaders() {
|
|
84
|
+
return { 'Authorization': `Bearer ${this.token}` };
|
|
85
|
+
}
|
|
86
|
+
/** Perform a raw fetch to the node and throw on non-2xx. */
|
|
87
|
+
async nodeFetch(url, init = {}) {
|
|
88
|
+
const res = await globalThis.fetch(url, {
|
|
89
|
+
...init,
|
|
90
|
+
headers: { ...this.authHeaders, ...init.headers },
|
|
91
|
+
});
|
|
92
|
+
if (!res.ok) {
|
|
93
|
+
let message = `HTTP ${res.status}`;
|
|
94
|
+
try {
|
|
95
|
+
const body = await res.json();
|
|
96
|
+
if (typeof body.error === 'string')
|
|
97
|
+
message = body.error;
|
|
98
|
+
}
|
|
99
|
+
catch { /* ignore */ }
|
|
100
|
+
throw new errors_js_1.ApiError({ message, status: res.status });
|
|
101
|
+
}
|
|
102
|
+
return res;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* List the contents of a directory.
|
|
106
|
+
*
|
|
107
|
+
* @param path Absolute path to the directory, e.g. `"/"` or `"/plugins"`.
|
|
108
|
+
* @returns An array of {@link FileEntry} objects.
|
|
109
|
+
*/
|
|
110
|
+
async list(path) {
|
|
111
|
+
const url = await this.buildFileUrl(path.startsWith('/') ? path : `/${path}`);
|
|
112
|
+
const res = await this.nodeFetch(url);
|
|
113
|
+
const data = await res.json();
|
|
114
|
+
return data.entries.map((e) => new FileEntry(e));
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Read the text content of a file.
|
|
118
|
+
*
|
|
119
|
+
* @param path Absolute path to the file, e.g. `"/server.properties"`.
|
|
120
|
+
* @returns The file content as a UTF-8 string.
|
|
121
|
+
*/
|
|
122
|
+
async read(path) {
|
|
123
|
+
const url = await this.buildFileUrl(path.startsWith('/') ? path : `/${path}`);
|
|
124
|
+
const res = await this.nodeFetch(url);
|
|
125
|
+
return res.text();
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Read the raw binary content of a file.
|
|
129
|
+
*
|
|
130
|
+
* @param path Absolute path to the file.
|
|
131
|
+
* @returns The file content as an `ArrayBuffer`.
|
|
132
|
+
*/
|
|
133
|
+
async readBinary(path) {
|
|
134
|
+
const url = await this.buildFileUrl(path.startsWith('/') ? path : `/${path}`);
|
|
135
|
+
const res = await this.nodeFetch(url);
|
|
136
|
+
return res.arrayBuffer();
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Write text content to a file, creating it if it doesn't exist.
|
|
140
|
+
*
|
|
141
|
+
* @param path Absolute path to the file.
|
|
142
|
+
* @param content The UTF-8 text content to write.
|
|
143
|
+
*/
|
|
144
|
+
async write(path, content) {
|
|
145
|
+
const url = await this.buildFileUrl(path.startsWith('/') ? path : `/${path}`);
|
|
146
|
+
await this.nodeFetch(url, {
|
|
147
|
+
method: 'PUT',
|
|
148
|
+
body: content,
|
|
149
|
+
headers: { 'Content-Type': 'text/plain' },
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Write raw binary content to a file.
|
|
154
|
+
*
|
|
155
|
+
* @param path Absolute path to the file.
|
|
156
|
+
* @param content Binary content to write.
|
|
157
|
+
* @param contentType MIME type of the content.
|
|
158
|
+
*/
|
|
159
|
+
async writeBinary(path, content, contentType) {
|
|
160
|
+
const url = await this.buildFileUrl(path.startsWith('/') ? path : `/${path}`);
|
|
161
|
+
const body = content instanceof ArrayBuffer ? new Uint8Array(content) : content;
|
|
162
|
+
await this.nodeFetch(url, {
|
|
163
|
+
method: 'PUT',
|
|
164
|
+
body: body,
|
|
165
|
+
headers: { 'Content-Type': contentType },
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Delete one or more files or directories.
|
|
170
|
+
*
|
|
171
|
+
* @param paths An array of absolute paths to delete.
|
|
172
|
+
*/
|
|
173
|
+
async delete(paths) {
|
|
174
|
+
const url = await this.buildFileUrl('');
|
|
175
|
+
await this.nodeFetch(url, {
|
|
176
|
+
method: 'DELETE',
|
|
177
|
+
body: JSON.stringify({ root: '/', files: paths }),
|
|
178
|
+
headers: { 'Content-Type': 'application/json' },
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Move (rename) a file or directory.
|
|
183
|
+
*
|
|
184
|
+
* @param from Absolute source path.
|
|
185
|
+
* @param to Absolute destination path.
|
|
186
|
+
*/
|
|
187
|
+
async move(from, to) {
|
|
188
|
+
const url = await this.buildFileUrl('/move');
|
|
189
|
+
await this.nodeFetch(url, {
|
|
190
|
+
method: 'POST',
|
|
191
|
+
body: JSON.stringify({ files: [{ from, to }] }),
|
|
192
|
+
headers: { 'Content-Type': 'application/json' },
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Copy a file or directory to a new location.
|
|
197
|
+
*
|
|
198
|
+
* @param source Absolute path of the source file or directory.
|
|
199
|
+
* @param location Absolute path of the destination directory.
|
|
200
|
+
*/
|
|
201
|
+
async copy(source, location) {
|
|
202
|
+
const url = await this.buildFileUrl('/copy');
|
|
203
|
+
await this.nodeFetch(url, {
|
|
204
|
+
method: 'POST',
|
|
205
|
+
body: JSON.stringify({ location, files: [source] }),
|
|
206
|
+
headers: { 'Content-Type': 'application/json' },
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Create an empty file or directory.
|
|
211
|
+
*
|
|
212
|
+
* @param path Absolute path of the new file or directory.
|
|
213
|
+
* @param directory When `true`, creates a directory instead of a file.
|
|
214
|
+
*/
|
|
215
|
+
async create(path, directory = false) {
|
|
216
|
+
const url = await this.buildFileUrl('/create');
|
|
217
|
+
await this.nodeFetch(url, {
|
|
218
|
+
method: 'POST',
|
|
219
|
+
body: JSON.stringify({ name: path, directory }),
|
|
220
|
+
headers: { 'Content-Type': 'application/json' },
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Compress one or more files into an archive.
|
|
225
|
+
*
|
|
226
|
+
* @param files Array of absolute paths to include in the archive.
|
|
227
|
+
* @param name Name of the output archive file, e.g. `"backup.tar.gz"`.
|
|
228
|
+
* @param rootPath Root directory for resolving relative paths. Defaults to `"/"`.
|
|
229
|
+
*/
|
|
230
|
+
async compress(files, name, rootPath = '/') {
|
|
231
|
+
const url = await this.buildFileUrl('/compress');
|
|
232
|
+
await this.nodeFetch(url, {
|
|
233
|
+
method: 'POST',
|
|
234
|
+
body: JSON.stringify({ root_path: rootPath, files, name }),
|
|
235
|
+
headers: { 'Content-Type': 'application/json' },
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* Decompress (extract) an archive file.
|
|
240
|
+
*
|
|
241
|
+
* @param file Absolute path to the archive to extract.
|
|
242
|
+
* @param rootPath Directory to extract the archive into. Defaults to `"/"`.
|
|
243
|
+
*/
|
|
244
|
+
async decompress(file, rootPath = '/') {
|
|
245
|
+
const url = await this.buildFileUrl('/decompress');
|
|
246
|
+
await this.nodeFetch(url, {
|
|
247
|
+
method: 'POST',
|
|
248
|
+
body: JSON.stringify({ root_path: rootPath, file }),
|
|
249
|
+
headers: { 'Content-Type': 'application/json' },
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* Change the permissions (chmod) of one or more files.
|
|
254
|
+
*
|
|
255
|
+
* @param entries Array of `{ file, mode }` objects.
|
|
256
|
+
* @param rootPath Root directory for resolving paths. Defaults to `"/"`.
|
|
257
|
+
*
|
|
258
|
+
* @example
|
|
259
|
+
* ```ts ignore
|
|
260
|
+
* await server.files.chmod([{ file: 'start.sh', mode: '755' }]);
|
|
261
|
+
* ```
|
|
262
|
+
*/
|
|
263
|
+
async chmod(entries, rootPath = '/') {
|
|
264
|
+
const url = await this.buildFileUrl('/chmod');
|
|
265
|
+
await this.nodeFetch(url, {
|
|
266
|
+
method: 'POST',
|
|
267
|
+
body: JSON.stringify({ root_path: rootPath, files: entries }),
|
|
268
|
+
headers: { 'Content-Type': 'application/json' },
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
/**
|
|
272
|
+
* Upload a file to the server using multipart form data.
|
|
273
|
+
*
|
|
274
|
+
* @param path Absolute directory path to upload the file into.
|
|
275
|
+
* @param filename The name for the uploaded file.
|
|
276
|
+
* @param content The file content as a `Blob`, `File`, or `ArrayBuffer`.
|
|
277
|
+
*/
|
|
278
|
+
async upload(path, filename, content) {
|
|
279
|
+
const url = await this.buildFileUrl(`/upload?directory=${encodeURIComponent(path)}`);
|
|
280
|
+
const form = new FormData();
|
|
281
|
+
const blob = content instanceof ArrayBuffer ? new Blob([content]) : content;
|
|
282
|
+
form.append('files', blob, filename);
|
|
283
|
+
await this.nodeFetch(url, { method: 'POST', body: form });
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
exports.ServerFilesManager = ServerFilesManager;
|