@scrypted/server 0.148.2 → 0.148.4
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/dist/async-queue.d.ts +37 -0
- package/dist/async-queue.js +170 -0
- package/dist/async-queue.js.map +1 -0
- package/dist/cluster/cluster-setup.js +0 -4
- package/dist/cluster/cluster-setup.js.map +1 -1
- package/dist/plugin/plugin-api.d.ts +1 -0
- package/dist/plugin/plugin-host.js +1 -1
- package/dist/plugin/plugin-host.js.map +1 -1
- package/dist/plugin/plugin-lazy-remote.d.ts +1 -0
- package/dist/plugin/plugin-lazy-remote.js +6 -0
- package/dist/plugin/plugin-lazy-remote.js.map +1 -1
- package/dist/plugin/plugin-remote-worker.js +7 -7
- package/dist/plugin/plugin-remote-worker.js.map +1 -1
- package/dist/plugin/plugin-remote.d.ts +1 -0
- package/dist/plugin/plugin-remote.js +4 -2
- package/dist/plugin/plugin-remote.js.map +1 -1
- package/dist/plugin/plugin-repl.d.ts +1 -1
- package/dist/plugin/plugin-repl.js +86 -27
- package/dist/plugin/plugin-repl.js.map +1 -1
- package/dist/plugin/runtime/cluster-fork-worker.js +4 -1
- package/dist/plugin/runtime/cluster-fork-worker.js.map +1 -1
- package/dist/services/plugin.d.ts +1 -0
- package/dist/services/plugin.js +6 -0
- package/dist/services/plugin.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/python/cluster_setup.py +0 -6
- package/python/plugin_remote.py +18 -15
- package/python/plugin_repl.py +165 -82
- package/src/async-queue.ts +189 -0
- package/src/cluster/cluster-setup.ts +0 -4
- package/src/plugin/plugin-api.ts +2 -0
- package/src/plugin/plugin-host.ts +1 -1
- package/src/plugin/plugin-lazy-remote.ts +7 -0
- package/src/plugin/plugin-remote-worker.ts +8 -8
- package/src/plugin/plugin-remote.ts +6 -2
- package/src/plugin/plugin-repl.ts +93 -26
- package/src/plugin/runtime/cluster-fork-worker.ts +5 -1
- package/src/services/plugin.ts +7 -0
package/python/plugin_repl.py
CHANGED
|
@@ -22,7 +22,6 @@ from typing import List, Dict, Any
|
|
|
22
22
|
|
|
23
23
|
from scrypted_python.scrypted_sdk import ScryptedStatic, ScryptedDevice
|
|
24
24
|
|
|
25
|
-
from cluster_setup import cluster_listen_zero
|
|
26
25
|
from rpc import maybe_await
|
|
27
26
|
|
|
28
27
|
|
|
@@ -60,58 +59,80 @@ def configure(repl: PythonRepl) -> None:
|
|
|
60
59
|
repl.enable_system_bindings = False
|
|
61
60
|
|
|
62
61
|
|
|
63
|
-
class
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
methods for Vt100_Output.
|
|
67
|
-
"""
|
|
68
|
-
|
|
69
|
-
def __init__(self, reader: asyncio.StreamReader, writer: asyncio.StreamWriter):
|
|
70
|
-
self.reader = reader
|
|
71
|
-
self.writer = writer
|
|
72
|
-
self.loop = asyncio.get_event_loop()
|
|
62
|
+
class ReplOutputWriter:
|
|
63
|
+
def __init__(self, queue: asyncio.Queue):
|
|
64
|
+
self.queue = queue
|
|
73
65
|
|
|
74
66
|
def write(self, data: bytes) -> None:
|
|
75
67
|
if isinstance(data, str):
|
|
76
68
|
data = data.encode()
|
|
77
|
-
|
|
69
|
+
data = bytes(data)
|
|
70
|
+
if data:
|
|
71
|
+
self.queue.put_nowait(data)
|
|
78
72
|
|
|
79
73
|
def flush(self) -> None:
|
|
80
|
-
|
|
74
|
+
pass
|
|
81
75
|
|
|
82
76
|
def isatty(self) -> bool:
|
|
83
77
|
return True
|
|
84
78
|
|
|
85
79
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
if
|
|
94
|
-
return
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
80
|
+
def _to_bytes(value) -> bytes:
|
|
81
|
+
if value is None:
|
|
82
|
+
return b""
|
|
83
|
+
if isinstance(value, bytes):
|
|
84
|
+
return value
|
|
85
|
+
if isinstance(value, bytearray):
|
|
86
|
+
return bytes(value)
|
|
87
|
+
if isinstance(value, memoryview):
|
|
88
|
+
return value.tobytes()
|
|
89
|
+
if isinstance(value, str):
|
|
90
|
+
return value.encode()
|
|
91
|
+
return bytes(value)
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def createREPLStreamService(sdk: ScryptedStatic, plugin: ScryptedDevice):
|
|
95
|
+
async def get_repl_stream_service(input):
|
|
96
|
+
iterator = input.__aiter__()
|
|
97
|
+
try:
|
|
98
|
+
first = await iterator.__anext__()
|
|
99
|
+
except StopAsyncIteration:
|
|
100
|
+
async def empty():
|
|
101
|
+
if False:
|
|
102
|
+
yield
|
|
103
|
+
|
|
104
|
+
return empty()
|
|
105
|
+
|
|
106
|
+
first_bytes = _to_bytes(first)
|
|
107
|
+
remainder = None
|
|
108
|
+
|
|
109
|
+
newline = first_bytes.find(b"\n")
|
|
110
|
+
if newline != -1:
|
|
111
|
+
remainder = first_bytes[newline + 1 :]
|
|
112
|
+
filter_text = first_bytes[:newline].decode(errors="replace")
|
|
113
|
+
else:
|
|
114
|
+
filter_text = first_bytes.decode(errors="replace")
|
|
115
|
+
|
|
116
|
+
filter_text = filter_text.strip()
|
|
117
|
+
filter_id = None if filter_text in ("", "undefined") else filter_text
|
|
118
|
+
|
|
119
|
+
deviceManager = sdk.deviceManager
|
|
120
|
+
systemManager = sdk.systemManager
|
|
121
|
+
mediaManager = sdk.mediaManager
|
|
105
122
|
|
|
106
123
|
chain: List[str] = []
|
|
107
124
|
nativeIds: Dict[str, Any] = deviceManager.nativeIds
|
|
108
125
|
reversed: Dict[str, str] = {v.id: k for k, v in nativeIds.items()}
|
|
109
126
|
|
|
110
|
-
while
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
127
|
+
while filter_id is not None:
|
|
128
|
+
entry = nativeIds.get(filter_id)
|
|
129
|
+
if not entry:
|
|
130
|
+
break
|
|
131
|
+
chain.append(filter_id)
|
|
132
|
+
d = systemManager.getDeviceById(entry.id)
|
|
133
|
+
if not d or not d.providerId:
|
|
134
|
+
break
|
|
135
|
+
filter_id = reversed.get(d.providerId)
|
|
115
136
|
|
|
116
137
|
chain.reverse()
|
|
117
138
|
device = plugin
|
|
@@ -119,50 +140,112 @@ async def createREPLServer(sdk: ScryptedStatic, plugin: ScryptedDevice) -> int:
|
|
|
119
140
|
device = await maybe_await(device.getDevice(c))
|
|
120
141
|
realDevice = systemManager.getDeviceById(device.id)
|
|
121
142
|
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
)
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
"deviceManager": deviceManager,
|
|
153
|
-
"mediaManager": mediaManager,
|
|
154
|
-
"sdk": sdk,
|
|
155
|
-
"realDevice": realDevice,
|
|
156
|
-
}
|
|
157
|
-
vars_prompt = "\n".join([f" {k}" for k in locals_dict.keys()])
|
|
158
|
-
banner = f"Python REPL variables:\n{vars_prompt}"
|
|
159
|
-
print_formatted_text(banner)
|
|
160
|
-
await embed(
|
|
161
|
-
return_asyncio_coroutine=True,
|
|
162
|
-
globals=global_dict,
|
|
163
|
-
locals=locals_dict,
|
|
164
|
-
configure=configure,
|
|
143
|
+
global_dict = {
|
|
144
|
+
**base_globals.copy(),
|
|
145
|
+
"print": print_formatted_text,
|
|
146
|
+
"help": lambda *args, **kwargs: print_formatted_text(
|
|
147
|
+
"Help is not available in this environment"
|
|
148
|
+
),
|
|
149
|
+
"input": lambda *args, **kwargs: print_formatted_text(
|
|
150
|
+
"Input is not available in this environment"
|
|
151
|
+
),
|
|
152
|
+
}
|
|
153
|
+
locals_dict = {
|
|
154
|
+
"device": device,
|
|
155
|
+
"systemManager": systemManager,
|
|
156
|
+
"deviceManager": deviceManager,
|
|
157
|
+
"mediaManager": mediaManager,
|
|
158
|
+
"sdk": sdk,
|
|
159
|
+
"realDevice": realDevice,
|
|
160
|
+
}
|
|
161
|
+
vars_prompt = "\n".join([f" {k}" for k in locals_dict.keys()])
|
|
162
|
+
banner = f"Python REPL variables:\n{vars_prompt}"
|
|
163
|
+
|
|
164
|
+
async def output_generator():
|
|
165
|
+
output_queue = asyncio.Queue()
|
|
166
|
+
writer = ReplOutputWriter(output_queue)
|
|
167
|
+
|
|
168
|
+
with create_pipe_input() as vt100_input:
|
|
169
|
+
vt100_output = Vt100_Output(
|
|
170
|
+
writer,
|
|
171
|
+
lambda: Size(rows=24, columns=80),
|
|
172
|
+
term=None,
|
|
165
173
|
)
|
|
166
174
|
|
|
167
|
-
|
|
168
|
-
|
|
175
|
+
async def input_pump():
|
|
176
|
+
try:
|
|
177
|
+
if remainder:
|
|
178
|
+
vt100_input.send_bytes(remainder)
|
|
179
|
+
async for data in iterator:
|
|
180
|
+
data = _to_bytes(data)
|
|
181
|
+
if data:
|
|
182
|
+
vt100_input.send_bytes(data)
|
|
183
|
+
except Exception:
|
|
184
|
+
pass
|
|
185
|
+
finally:
|
|
186
|
+
try:
|
|
187
|
+
vt100_input.close()
|
|
188
|
+
except Exception:
|
|
189
|
+
pass
|
|
190
|
+
output_queue.put_nowait(None)
|
|
191
|
+
|
|
192
|
+
with create_app_session(input=vt100_input, output=vt100_output):
|
|
193
|
+
async def run_embed():
|
|
194
|
+
try:
|
|
195
|
+
print_formatted_text(banner)
|
|
196
|
+
await embed(
|
|
197
|
+
return_asyncio_coroutine=True,
|
|
198
|
+
globals=global_dict,
|
|
199
|
+
locals=locals_dict,
|
|
200
|
+
configure=configure,
|
|
201
|
+
)
|
|
202
|
+
finally:
|
|
203
|
+
output_queue.put_nowait(None)
|
|
204
|
+
|
|
205
|
+
embed_task = asyncio.create_task(run_embed())
|
|
206
|
+
input_task = asyncio.create_task(input_pump())
|
|
207
|
+
|
|
208
|
+
try:
|
|
209
|
+
while True:
|
|
210
|
+
item = await output_queue.get()
|
|
211
|
+
if item is None:
|
|
212
|
+
break
|
|
213
|
+
|
|
214
|
+
parts = [item]
|
|
215
|
+
done = False
|
|
216
|
+
while True:
|
|
217
|
+
try:
|
|
218
|
+
next_item = output_queue.get_nowait()
|
|
219
|
+
except asyncio.QueueEmpty:
|
|
220
|
+
break
|
|
221
|
+
if next_item is None:
|
|
222
|
+
done = True
|
|
223
|
+
break
|
|
224
|
+
parts.append(next_item)
|
|
225
|
+
|
|
226
|
+
if parts:
|
|
227
|
+
yield b"".join(parts)
|
|
228
|
+
if done:
|
|
229
|
+
break
|
|
230
|
+
finally:
|
|
231
|
+
input_task.cancel()
|
|
232
|
+
try:
|
|
233
|
+
vt100_input.close()
|
|
234
|
+
except Exception:
|
|
235
|
+
pass
|
|
236
|
+
await asyncio.wait([embed_task], timeout=5)
|
|
237
|
+
if not embed_task.done():
|
|
238
|
+
embed_task.cancel()
|
|
239
|
+
await asyncio.gather(
|
|
240
|
+
input_task, embed_task, return_exceptions=True
|
|
241
|
+
)
|
|
242
|
+
aclose = getattr(iterator, "aclose", None)
|
|
243
|
+
if aclose:
|
|
244
|
+
try:
|
|
245
|
+
await aclose()
|
|
246
|
+
except Exception:
|
|
247
|
+
pass
|
|
248
|
+
|
|
249
|
+
return output_generator()
|
|
250
|
+
|
|
251
|
+
return get_repl_stream_service
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import { Deferred } from "./deferred";
|
|
2
|
+
|
|
3
|
+
class EndError extends Error {
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export function createAsyncQueue<T>() {
|
|
7
|
+
let ended: Error | undefined;
|
|
8
|
+
const endDeferred = new Deferred<void>();
|
|
9
|
+
const waiting: Deferred<T>[] = [];
|
|
10
|
+
const queued: { item: T, dequeued?: Deferred<void> }[] = [];
|
|
11
|
+
|
|
12
|
+
const wait = async (index: number) => {
|
|
13
|
+
const q = queued[index];
|
|
14
|
+
if (!q)
|
|
15
|
+
return;
|
|
16
|
+
if (!q.dequeued) {
|
|
17
|
+
q.dequeued = new Deferred<void>();
|
|
18
|
+
}
|
|
19
|
+
return q.dequeued.promise;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const dequeue = async () => {
|
|
23
|
+
if (queued.length) {
|
|
24
|
+
const { item, dequeued: enqueue } = queued.shift()!;
|
|
25
|
+
enqueue?.resolve();
|
|
26
|
+
return item;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if (ended)
|
|
30
|
+
throw ended;
|
|
31
|
+
|
|
32
|
+
const deferred = new Deferred<T>();
|
|
33
|
+
waiting.push(deferred);
|
|
34
|
+
return deferred.promise;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const take = () => {
|
|
38
|
+
if (queued.length) {
|
|
39
|
+
const { item, dequeued: enqueue } = queued.shift()!;
|
|
40
|
+
enqueue?.resolve();
|
|
41
|
+
return item;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (ended)
|
|
45
|
+
throw ended;
|
|
46
|
+
|
|
47
|
+
return undefined;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const submit = (item: T, dequeued?: Deferred<void>, signal?: AbortSignal) => {
|
|
51
|
+
if (ended)
|
|
52
|
+
return false;
|
|
53
|
+
|
|
54
|
+
if (waiting.length) {
|
|
55
|
+
const deferred = waiting.shift()!;
|
|
56
|
+
dequeued?.resolve();
|
|
57
|
+
deferred.resolve(item);
|
|
58
|
+
return true;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (signal)
|
|
62
|
+
dequeued ||= new Deferred();
|
|
63
|
+
|
|
64
|
+
const qi = {
|
|
65
|
+
item,
|
|
66
|
+
dequeued,
|
|
67
|
+
};
|
|
68
|
+
queued!.push(qi);
|
|
69
|
+
|
|
70
|
+
if (!signal)
|
|
71
|
+
return true;
|
|
72
|
+
|
|
73
|
+
const h = () => {
|
|
74
|
+
const index = queued.indexOf(qi);
|
|
75
|
+
if (index === -1)
|
|
76
|
+
return;
|
|
77
|
+
queued.splice(index, 1);
|
|
78
|
+
dequeued?.reject(new Error('abort'));
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
dequeued?.promise.catch(() => { }).finally(() => signal.removeEventListener('abort', h));
|
|
82
|
+
signal.addEventListener('abort', h);
|
|
83
|
+
|
|
84
|
+
return true;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function end(e?: Error) {
|
|
88
|
+
if (ended)
|
|
89
|
+
return false;
|
|
90
|
+
// catch to prevent unhandled rejection.
|
|
91
|
+
ended = e || new EndError();
|
|
92
|
+
endDeferred.resolve();
|
|
93
|
+
while (waiting.length) {
|
|
94
|
+
waiting.shift()!.reject(ended);
|
|
95
|
+
}
|
|
96
|
+
return true;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function queue() {
|
|
100
|
+
return (async function* () {
|
|
101
|
+
try {
|
|
102
|
+
while (true) {
|
|
103
|
+
try {
|
|
104
|
+
const item = await dequeue();
|
|
105
|
+
yield item;
|
|
106
|
+
}
|
|
107
|
+
catch (e) {
|
|
108
|
+
// the yield above may raise an error, and the queue should be ended.
|
|
109
|
+
end(e as Error);
|
|
110
|
+
if (e instanceof EndError)
|
|
111
|
+
return;
|
|
112
|
+
throw e;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
finally {
|
|
117
|
+
// the yield above may cause an iterator return, and the queue should be ended.
|
|
118
|
+
end();
|
|
119
|
+
}
|
|
120
|
+
})();
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function clear(error?: Error) {
|
|
124
|
+
const ret: T[] = [];
|
|
125
|
+
const items = queued.splice(0, queued.length);
|
|
126
|
+
for (const item of items) {
|
|
127
|
+
if (error)
|
|
128
|
+
item.dequeued?.reject(error)
|
|
129
|
+
else
|
|
130
|
+
item.dequeued?.resolve(undefined);
|
|
131
|
+
ret.push(item.item);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return ret;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
return {
|
|
138
|
+
[Symbol.dispose]() {
|
|
139
|
+
end(new Error('async queue disposed'));
|
|
140
|
+
},
|
|
141
|
+
get ended() {
|
|
142
|
+
return ended;
|
|
143
|
+
},
|
|
144
|
+
endPromise: endDeferred.promise,
|
|
145
|
+
take,
|
|
146
|
+
clear() {
|
|
147
|
+
return clear();
|
|
148
|
+
},
|
|
149
|
+
queued,
|
|
150
|
+
async pipe(callback: (i: T) => void) {
|
|
151
|
+
for await (const i of queue()) {
|
|
152
|
+
callback(i as any);
|
|
153
|
+
}
|
|
154
|
+
},
|
|
155
|
+
submit(item: T, signal?: AbortSignal) {
|
|
156
|
+
return submit(item, undefined, signal);
|
|
157
|
+
},
|
|
158
|
+
end,
|
|
159
|
+
async enqueue(item: T, signal?: AbortSignal) {
|
|
160
|
+
const dequeued = new Deferred<void>();
|
|
161
|
+
if (!submit(item, dequeued, signal))
|
|
162
|
+
return false;
|
|
163
|
+
await dequeued.promise;
|
|
164
|
+
return true;
|
|
165
|
+
},
|
|
166
|
+
dequeue,
|
|
167
|
+
get queue() {
|
|
168
|
+
return queue();
|
|
169
|
+
},
|
|
170
|
+
wait,
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
export function createAsyncQueueFromGenerator<T>(generator: AsyncGenerator<T>) {
|
|
175
|
+
const q = createAsyncQueue<T>();
|
|
176
|
+
(async () => {
|
|
177
|
+
try {
|
|
178
|
+
for await (const i of generator) {
|
|
179
|
+
await q.enqueue(i);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
catch (e) {
|
|
183
|
+
q.end(e as Error);
|
|
184
|
+
}
|
|
185
|
+
q.end();
|
|
186
|
+
})();
|
|
187
|
+
|
|
188
|
+
return q;
|
|
189
|
+
}
|
|
@@ -98,8 +98,6 @@ export function setupCluster(peer: RpcPeer) {
|
|
|
98
98
|
}
|
|
99
99
|
|
|
100
100
|
const clusterPeer = createDuplexRpcPeer(peer.selfName, clusterPeerKey, socket, socket);
|
|
101
|
-
// set params from the primary peer, needed for get getRemote in cluster mode.
|
|
102
|
-
Object.assign(clusterPeer.params, peer.params);
|
|
103
101
|
clusterPeer.onProxySerialization = (value) => onProxySerialization(clusterPeer, value, clusterPeerKey);
|
|
104
102
|
return clusterPeer;
|
|
105
103
|
}
|
|
@@ -350,8 +348,6 @@ export function setupCluster(peer: RpcPeer) {
|
|
|
350
348
|
const clusterPeerPort = client.remotePort!;
|
|
351
349
|
const clusterPeerKey = getClusterPeerKey(clusterPeerAddress, clusterPeerPort);
|
|
352
350
|
const clusterPeer = createDuplexRpcPeer(peer.selfName, clusterPeerKey, client, client);
|
|
353
|
-
// set params from the primary peer, needed for get getRemote in cluster mode.
|
|
354
|
-
Object.assign(clusterPeer.params, peer.params);
|
|
355
351
|
// the listening peer sourceKey (client address/port) is used by the OTHER peer (the client)
|
|
356
352
|
// to determine if it is already connected to THIS peer (the server).
|
|
357
353
|
clusterPeer.onProxySerialization = (value) => onProxySerialization(clusterPeer, value, clusterPeerKey);
|
package/src/plugin/plugin-api.ts
CHANGED
|
@@ -191,6 +191,8 @@ export interface PluginRemote {
|
|
|
191
191
|
createDeviceState(id: string, setState: (property: string, value: any) => Promise<any>): Promise<any>;
|
|
192
192
|
|
|
193
193
|
getServicePort(name: string, ...args: any[]): Promise<[number, string]>;
|
|
194
|
+
|
|
195
|
+
getReplStreamService?(input: AsyncGenerator<Buffer>): Promise<AsyncGenerator<Buffer>>;
|
|
194
196
|
}
|
|
195
197
|
|
|
196
198
|
export interface MediaObjectRemote extends MediaObject {
|
|
@@ -301,7 +301,7 @@ export class PluginHost {
|
|
|
301
301
|
await sleep(30000);
|
|
302
302
|
if (this.killed)
|
|
303
303
|
return;
|
|
304
|
-
pingPromise ||= peer.getParam('ping');
|
|
304
|
+
pingPromise ||= peer.tags.ping || peer.getParam('ping');
|
|
305
305
|
const ping = await pingPromise;
|
|
306
306
|
lastPong = await ping(Date.now());
|
|
307
307
|
}
|
|
@@ -70,4 +70,11 @@ import { PluginRemote, PluginRemoteLoadZipOptions, PluginZipAPI } from './plugin
|
|
|
70
70
|
const remote = await this.remotePromise;
|
|
71
71
|
return remote.getServicePort(name, ...args);
|
|
72
72
|
}
|
|
73
|
+
|
|
74
|
+
async getReplStreamService(input: AsyncGenerator<Buffer>): Promise<AsyncGenerator<Buffer>> {
|
|
75
|
+
const remote = await this.remotePromise;
|
|
76
|
+
if (!remote.getReplStreamService)
|
|
77
|
+
throw new Error('REPL is not supported by this plugin.');
|
|
78
|
+
return remote.getReplStreamService(input);
|
|
79
|
+
}
|
|
73
80
|
}
|
|
@@ -16,7 +16,7 @@ import { PluginAPI, PluginAPIProxy, PluginRemote, PluginRemoteLoadZipOptions, Pl
|
|
|
16
16
|
import { pipeWorkerConsole, prepareConsoles } from './plugin-console';
|
|
17
17
|
import { getPluginNodePath, installOptionalDependencies } from './plugin-npm-dependencies';
|
|
18
18
|
import { attachPluginRemote, setupPluginRemote } from './plugin-remote';
|
|
19
|
-
import {
|
|
19
|
+
import { createREPLStreamService } from './plugin-repl';
|
|
20
20
|
import { getPluginVolume } from './plugin-volume';
|
|
21
21
|
import { ChildProcessWorker } from './runtime/child-process-worker';
|
|
22
22
|
import { createClusterForkWorker } from './runtime/cluster-fork-worker';
|
|
@@ -61,7 +61,7 @@ export function startPluginRemote(mainFilename: string, pluginId: string, peerSe
|
|
|
61
61
|
|
|
62
62
|
const { getDeviceConsole, getMixinConsole } = prepareConsoles(() => peer.selfName, () => systemManager, () => deviceManager, getPlugins);
|
|
63
63
|
|
|
64
|
-
let
|
|
64
|
+
let replStreamService: (input: AsyncGenerator<Buffer>) => Promise<AsyncGenerator<Buffer>>;
|
|
65
65
|
|
|
66
66
|
let _pluginConsole: Console;
|
|
67
67
|
const getPluginConsole = () => {
|
|
@@ -101,13 +101,13 @@ export function startPluginRemote(mainFilename: string, pluginId: string, peerSe
|
|
|
101
101
|
getDeviceConsole,
|
|
102
102
|
getMixinConsole,
|
|
103
103
|
async getServicePort(name) {
|
|
104
|
-
if (name === 'repl') {
|
|
105
|
-
if (!replPort)
|
|
106
|
-
throw new Error('REPL unavailable: Plugin not loaded.')
|
|
107
|
-
return [await replPort, process.env.SCRYPTED_CLUSTER_ADDRESS!] as [number, string];
|
|
108
|
-
}
|
|
109
104
|
throw new Error(`unknown service ${name}`);
|
|
110
105
|
},
|
|
106
|
+
async getReplStreamService(input) {
|
|
107
|
+
if (!replStreamService)
|
|
108
|
+
throw new Error('REPL unavailable: Plugin not loaded.');
|
|
109
|
+
return replStreamService(input);
|
|
110
|
+
},
|
|
111
111
|
async onLoadZip(scrypted: ScryptedStatic, params: any, packageJson: any, zipAPI: PluginZipAPI, zipOptions: PluginRemoteLoadZipOptions) {
|
|
112
112
|
const mainFile = zipOptions?.main || 'main';
|
|
113
113
|
const mainNodejs = `${mainFile}.nodejs.js`;
|
|
@@ -419,7 +419,7 @@ export function startPluginRemote(mainFilename: string, pluginId: string, peerSe
|
|
|
419
419
|
if (typeof pluginInstance === 'function')
|
|
420
420
|
pluginInstance = await pluginInstance();
|
|
421
421
|
|
|
422
|
-
|
|
422
|
+
replStreamService = createREPLStreamService(scrypted, params, pluginInstance);
|
|
423
423
|
postInstallSourceMapSupport(scrypted);
|
|
424
424
|
|
|
425
425
|
return pluginInstance;
|
|
@@ -53,7 +53,7 @@ export async function setupPluginRemote(peer: RpcPeer, api: PluginAPI, pluginId:
|
|
|
53
53
|
// but in plugin-host, mark Buffer as transport safe.
|
|
54
54
|
if (!peer.constructorSerializerMap.get(Buffer))
|
|
55
55
|
peer.addSerializer(Buffer, 'Buffer', new BufferSerializer());
|
|
56
|
-
const getRemote = await peer.getParam('getRemote');
|
|
56
|
+
const getRemote = peer.tags.getRemote || await peer.getParam('getRemote');
|
|
57
57
|
const remote = await getRemote(api, pluginId, hostInfo) as PluginRemote;
|
|
58
58
|
|
|
59
59
|
const accessControls: AccessControls = peer.tags.acl;
|
|
@@ -105,6 +105,7 @@ export interface PluginRemoteAttachOptions {
|
|
|
105
105
|
*/
|
|
106
106
|
createMediaManager?: (systemManager: SystemManager, deviceManager: DeviceManagerImpl) => Promise<MediaManager>;
|
|
107
107
|
getServicePort?: (name: string, ...args: any[]) => Promise<[number, string]>;
|
|
108
|
+
getReplStreamService?: (input: AsyncGenerator<Buffer>) => Promise<AsyncGenerator<Buffer>>;
|
|
108
109
|
getDeviceConsole?: (nativeId?: ScryptedNativeId) => Console;
|
|
109
110
|
getPluginConsole?: () => Console;
|
|
110
111
|
getMixinConsole?: (id: string, nativeId?: ScryptedNativeId) => Console;
|
|
@@ -113,7 +114,7 @@ export interface PluginRemoteAttachOptions {
|
|
|
113
114
|
}
|
|
114
115
|
|
|
115
116
|
export function attachPluginRemote(peer: RpcPeer, options?: PluginRemoteAttachOptions): Promise<ScryptedStatic> {
|
|
116
|
-
const { createMediaManager, getServicePort, getDeviceConsole, getMixinConsole } = options || {};
|
|
117
|
+
const { createMediaManager, getServicePort, getReplStreamService, getDeviceConsole, getMixinConsole } = options || {};
|
|
117
118
|
|
|
118
119
|
if (!peer.constructorSerializerMap.get(Buffer))
|
|
119
120
|
peer.addSerializer(Buffer, 'Buffer', new BufferSerializer());
|
|
@@ -311,6 +312,9 @@ export function attachPluginRemote(peer: RpcPeer, options?: PluginRemoteAttachOp
|
|
|
311
312
|
},
|
|
312
313
|
}
|
|
313
314
|
|
|
315
|
+
if (getReplStreamService)
|
|
316
|
+
remote.getReplStreamService = getReplStreamService;
|
|
317
|
+
|
|
314
318
|
ret.pluginRemoteAPI = remote;
|
|
315
319
|
|
|
316
320
|
return remote;
|