@scrypted/server 0.148.3 → 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.
@@ -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;
@@ -1,16 +1,36 @@
1
1
  import { ScryptedStatic } from '@scrypted/types';
2
- import { once } from 'events';
3
2
  import repl from 'repl';
4
- import { clusterListenZero } from '../cluster/cluster-setup';
3
+ import { PassThrough } from 'stream';
4
+ import { createAsyncQueue } from '../async-queue';
5
5
 
6
- export async function createREPLServer(scrypted: ScryptedStatic, params: any, plugin: any): Promise<number> {
7
- const { deviceManager, systemManager } = scrypted;
8
- const { server, port } = await clusterListenZero(async (socket) => {
9
- let [filter] = await once(socket, 'data');
10
- filter = filter.toString().trim();
11
- if (filter === 'undefined')
12
- filter = undefined;
6
+ function toBuffer(value: any): Buffer {
7
+ return Buffer.isBuffer(value) ? value : Buffer.from(value);
8
+ }
9
+
10
+ export function createREPLStreamService(scrypted: ScryptedStatic, params: any, plugin: any): (input: AsyncGenerator<Buffer>) => Promise<AsyncGenerator<Buffer>> {
11
+ return async (input) => {
12
+ const iterator = input[Symbol.asyncIterator]();
13
+ const first = await iterator.next();
14
+ if (first.done)
15
+ return (async function* () { })();
16
+
17
+ const firstBuffer = toBuffer(first.value);
18
+ let filterText: string;
19
+ let remaining: Buffer | undefined;
20
+
21
+ const newline = firstBuffer.indexOf(0x0a);
22
+ if (newline !== -1) {
23
+ remaining = firstBuffer.subarray(newline + 1);
24
+ filterText = firstBuffer.subarray(0, newline).toString();
25
+ }
26
+ else {
27
+ filterText = firstBuffer.toString();
28
+ }
13
29
 
30
+ filterText = filterText.trim();
31
+ let filter: string | undefined = !filterText || filterText === 'undefined' ? undefined : filterText;
32
+
33
+ const { deviceManager, systemManager } = scrypted;
14
34
  const chain: string[] = [];
15
35
  const nativeIds: Map<string, any> = (deviceManager as any).nativeIds;
16
36
  const reversed = new Map<string, string>();
@@ -53,31 +73,78 @@ export async function createREPLServer(scrypted: ScryptedStatic, params: any, pl
53
73
  const replVariables = Object.keys(ctx).filter(key => !replFilter.has(key));
54
74
 
55
75
  const welcome = `JavaScript REPL variables:\n${replVariables.map(key => ' ' + key).join('\n')}\n\n`;
56
- socket.write(welcome);
57
76
 
58
- const r = repl.start({
77
+ const inputStream = new PassThrough();
78
+ (async () => {
79
+ try {
80
+ if (remaining?.length)
81
+ inputStream.write(remaining);
82
+ for await (const data of iterator) {
83
+ if (inputStream.destroyed)
84
+ return;
85
+ inputStream.write(toBuffer(data));
86
+ }
87
+ }
88
+ catch (e) {
89
+ }
90
+ finally {
91
+ inputStream.end();
92
+ }
93
+ })();
94
+
95
+ const outputStream = new PassThrough();
96
+ const outputQueue = createAsyncQueue<Buffer>();
97
+ outputStream.on('data', data => outputQueue.submit(toBuffer(data)));
98
+ outputStream.on('end', () => outputQueue.end());
99
+ outputStream.on('close', () => outputQueue.end());
100
+ outputStream.on('error', e => outputQueue.end(e as Error));
101
+
102
+ outputStream.write(welcome);
103
+
104
+ const replServer = repl.start({
59
105
  terminal: true,
60
- input: socket,
61
- output: socket,
62
- // writer(this: REPLServer, obj: any) {
63
- // const ret = util.inspect(obj, {
64
- // colors: true,
65
- // });
66
- // return ret;//.replaceAll('\n', '\r\n');
67
- // },
106
+ input: inputStream,
107
+ output: outputStream,
68
108
  preview: false,
69
109
  });
70
110
 
71
- Object.assign(r.context, ctx);
111
+ Object.assign(replServer.context, ctx);
72
112
 
113
+ let cleaned = false;
73
114
  const cleanup = () => {
74
- r.close();
115
+ if (cleaned)
116
+ return;
117
+ cleaned = true;
118
+ replServer.close();
119
+ inputStream.destroy();
120
+ outputStream.end();
121
+ iterator.return?.(undefined).catch(() => { });
75
122
  };
76
123
 
77
- socket.on('close', cleanup);
78
- socket.on('error', cleanup);
79
- socket.on('end', cleanup);
80
- });
124
+ replServer.on('exit', cleanup);
125
+ inputStream.on('end', cleanup);
126
+ inputStream.on('error', e => {
127
+ outputQueue.end(e);
128
+ cleanup();
129
+ });
130
+
131
+ return (async function* () {
132
+ try {
133
+ while (true) {
134
+ const buffers = outputQueue.clear();
135
+ if (buffers.length) {
136
+ yield Buffer.concat(buffers);
137
+ continue;
138
+ }
81
139
 
82
- return port;
140
+ yield await outputQueue.dequeue();
141
+ }
142
+ }
143
+ catch (e) {
144
+ }
145
+ finally {
146
+ cleanup();
147
+ }
148
+ })();
149
+ };
83
150
  }
@@ -190,4 +190,11 @@ export class PluginComponent {
190
190
 
191
191
  return this.scrypted.plugins[pluginId]!.remote.getServicePort(name, ...args);
192
192
  }
193
+
194
+ async getReplStreamService(pluginId: string, input: AsyncGenerator<Buffer>): Promise<AsyncGenerator<Buffer>> {
195
+ const remote = this.scrypted.plugins[pluginId]?.remote;
196
+ if (!remote?.getReplStreamService)
197
+ throw new Error('REPL is not supported by this plugin.');
198
+ return remote.getReplStreamService(input);
199
+ }
193
200
  }