@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.
Files changed (39) hide show
  1. package/dist/async-queue.d.ts +37 -0
  2. package/dist/async-queue.js +170 -0
  3. package/dist/async-queue.js.map +1 -0
  4. package/dist/cluster/cluster-setup.js +0 -4
  5. package/dist/cluster/cluster-setup.js.map +1 -1
  6. package/dist/plugin/plugin-api.d.ts +1 -0
  7. package/dist/plugin/plugin-host.js +1 -1
  8. package/dist/plugin/plugin-host.js.map +1 -1
  9. package/dist/plugin/plugin-lazy-remote.d.ts +1 -0
  10. package/dist/plugin/plugin-lazy-remote.js +6 -0
  11. package/dist/plugin/plugin-lazy-remote.js.map +1 -1
  12. package/dist/plugin/plugin-remote-worker.js +7 -7
  13. package/dist/plugin/plugin-remote-worker.js.map +1 -1
  14. package/dist/plugin/plugin-remote.d.ts +1 -0
  15. package/dist/plugin/plugin-remote.js +4 -2
  16. package/dist/plugin/plugin-remote.js.map +1 -1
  17. package/dist/plugin/plugin-repl.d.ts +1 -1
  18. package/dist/plugin/plugin-repl.js +86 -27
  19. package/dist/plugin/plugin-repl.js.map +1 -1
  20. package/dist/plugin/runtime/cluster-fork-worker.js +4 -1
  21. package/dist/plugin/runtime/cluster-fork-worker.js.map +1 -1
  22. package/dist/services/plugin.d.ts +1 -0
  23. package/dist/services/plugin.js +6 -0
  24. package/dist/services/plugin.js.map +1 -1
  25. package/dist/tsconfig.tsbuildinfo +1 -1
  26. package/package.json +1 -1
  27. package/python/cluster_setup.py +0 -6
  28. package/python/plugin_remote.py +18 -15
  29. package/python/plugin_repl.py +165 -82
  30. package/src/async-queue.ts +189 -0
  31. package/src/cluster/cluster-setup.ts +0 -4
  32. package/src/plugin/plugin-api.ts +2 -0
  33. package/src/plugin/plugin-host.ts +1 -1
  34. package/src/plugin/plugin-lazy-remote.ts +7 -0
  35. package/src/plugin/plugin-remote-worker.ts +8 -8
  36. package/src/plugin/plugin-remote.ts +6 -2
  37. package/src/plugin/plugin-repl.ts +93 -26
  38. package/src/plugin/runtime/cluster-fork-worker.ts +5 -1
  39. package/src/services/plugin.ts +7 -0
@@ -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
  }
@@ -67,7 +67,8 @@ export function createClusterForkWorker(
67
67
  const {
68
68
  stdout: stdoutGen,
69
69
  stderr: stderrGen,
70
- getRemote
70
+ getRemote,
71
+ ping
71
72
  } = await clusterGetRemote();
72
73
 
73
74
  writeWorkerGenerator(stdoutGen, stdout).catch(() => { });
@@ -76,9 +77,12 @@ export function createClusterForkWorker(
76
77
  const directGetRemote = await connectRPCObject(getRemote);
77
78
  if (directGetRemote === getRemote)
78
79
  throw new Error('cluster fork peer not direct connected');
80
+ const directPing = await connectRPCObject(ping);
79
81
  const peer: RpcPeer = directGetRemote[RpcPeer.PROPERTY_PROXY_PEER];
80
82
  if (!peer)
81
83
  throw new Error('cluster fork peer undefined?');
84
+ peer.tags.getRemote = directGetRemote;
85
+ peer.tags.ping = directPing;
82
86
  return peer;
83
87
  })();
84
88
 
@@ -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
  }