@science-corporation/synapse 2.0.0 → 2.2.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.
Files changed (127) hide show
  1. package/README.md +7 -0
  2. package/package.json +3 -2
  3. package/src/config.ts +1 -5
  4. package/src/device.ts +1 -5
  5. package/src/nodes/index.ts +0 -2
  6. package/synapse-api/README.md +1 -1
  7. package/synapse-api/api/app.proto +55 -0
  8. package/synapse-api/api/channel.proto +15 -1
  9. package/synapse-api/api/datatype.proto +97 -1
  10. package/synapse-api/api/device.proto +65 -0
  11. package/synapse-api/api/node.proto +19 -18
  12. package/synapse-api/api/nodes/application.proto +24 -0
  13. package/synapse-api/api/nodes/broadband_source.proto +1 -0
  14. package/synapse-api/api/nodes/disk_writer.proto +17 -0
  15. package/synapse-api/api/nodes/optical_stimulation.proto +29 -0
  16. package/synapse-api/api/nodes/signal_config.proto +2 -0
  17. package/synapse-api/api/performance.proto +29 -0
  18. package/synapse-api/api/query.proto +8 -0
  19. package/synapse-api/api/status.proto +6 -1
  20. package/synapse-api/api/synapse.proto +8 -31
  21. package/synapse-api/api/tap.proto +35 -0
  22. package/synapse-api/api/time.proto +53 -0
  23. package/dist/api/api.d.ts +0 -6755
  24. package/dist/api/api.d.ts.map +0 -1
  25. package/dist/api/api.js +0 -15729
  26. package/dist/api/api.js.map +0 -1
  27. package/dist/api/proto.json +0 -1144
  28. package/dist/browser.d.ts +0 -4
  29. package/dist/browser.d.ts.map +0 -1
  30. package/dist/browser.js +0 -22
  31. package/dist/browser.js.map +0 -1
  32. package/dist/config.d.ts +0 -17
  33. package/dist/config.d.ts.map +0 -1
  34. package/dist/config.js +0 -115
  35. package/dist/config.js.map +0 -1
  36. package/dist/demo.d.ts +0 -2
  37. package/dist/demo.d.ts.map +0 -1
  38. package/dist/demo.js +0 -285
  39. package/dist/demo.js.map +0 -1
  40. package/dist/device.d.ts +0 -38
  41. package/dist/device.d.ts.map +0 -1
  42. package/dist/device.js +0 -185
  43. package/dist/device.js.map +0 -1
  44. package/dist/index.d.ts +0 -7
  45. package/dist/index.d.ts.map +0 -1
  46. package/dist/index.js +0 -30
  47. package/dist/index.js.map +0 -1
  48. package/dist/node.d.ts +0 -10
  49. package/dist/node.d.ts.map +0 -1
  50. package/dist/node.js +0 -14
  51. package/dist/node.js.map +0 -1
  52. package/dist/nodes/broadband_source.d.ts +0 -11
  53. package/dist/nodes/broadband_source.d.ts.map +0 -1
  54. package/dist/nodes/broadband_source.js +0 -28
  55. package/dist/nodes/broadband_source.js.map +0 -1
  56. package/dist/nodes/disk_writer.d.ts +0 -11
  57. package/dist/nodes/disk_writer.d.ts.map +0 -1
  58. package/dist/nodes/disk_writer.js +0 -28
  59. package/dist/nodes/disk_writer.js.map +0 -1
  60. package/dist/nodes/electrical_stimulation.d.ts +0 -11
  61. package/dist/nodes/electrical_stimulation.d.ts.map +0 -1
  62. package/dist/nodes/electrical_stimulation.js +0 -28
  63. package/dist/nodes/electrical_stimulation.js.map +0 -1
  64. package/dist/nodes/index.d.ts +0 -12
  65. package/dist/nodes/index.d.ts.map +0 -1
  66. package/dist/nodes/index.js +0 -27
  67. package/dist/nodes/index.js.map +0 -1
  68. package/dist/nodes/optical_stimulation.d.ts +0 -11
  69. package/dist/nodes/optical_stimulation.d.ts.map +0 -1
  70. package/dist/nodes/optical_stimulation.js +0 -28
  71. package/dist/nodes/optical_stimulation.js.map +0 -1
  72. package/dist/nodes/spectral_filter.d.ts +0 -11
  73. package/dist/nodes/spectral_filter.d.ts.map +0 -1
  74. package/dist/nodes/spectral_filter.js +0 -28
  75. package/dist/nodes/spectral_filter.js.map +0 -1
  76. package/dist/nodes/spike_binner.d.ts +0 -11
  77. package/dist/nodes/spike_binner.d.ts.map +0 -1
  78. package/dist/nodes/spike_binner.js +0 -28
  79. package/dist/nodes/spike_binner.js.map +0 -1
  80. package/dist/nodes/spike_detector.d.ts +0 -11
  81. package/dist/nodes/spike_detector.d.ts.map +0 -1
  82. package/dist/nodes/spike_detector.js +0 -28
  83. package/dist/nodes/spike_detector.js.map +0 -1
  84. package/dist/nodes/spike_source.d.ts +0 -11
  85. package/dist/nodes/spike_source.d.ts.map +0 -1
  86. package/dist/nodes/spike_source.js +0 -28
  87. package/dist/nodes/spike_source.js.map +0 -1
  88. package/dist/nodes/stream_in.d.ts +0 -15
  89. package/dist/nodes/stream_in.d.ts.map +0 -1
  90. package/dist/nodes/stream_in.js +0 -66
  91. package/dist/nodes/stream_in.js.map +0 -1
  92. package/dist/nodes/stream_out.d.ts +0 -23
  93. package/dist/nodes/stream_out.d.ts.map +0 -1
  94. package/dist/nodes/stream_out.js +0 -105
  95. package/dist/nodes/stream_out.js.map +0 -1
  96. package/dist/utils/client.d.ts +0 -10
  97. package/dist/utils/client.d.ts.map +0 -1
  98. package/dist/utils/client.js +0 -39
  99. package/dist/utils/client.js.map +0 -1
  100. package/dist/utils/discover.d.ts +0 -10
  101. package/dist/utils/discover.d.ts.map +0 -1
  102. package/dist/utils/discover.js +0 -49
  103. package/dist/utils/discover.js.map +0 -1
  104. package/dist/utils/enum.d.ts +0 -2
  105. package/dist/utils/enum.d.ts.map +0 -1
  106. package/dist/utils/enum.js +0 -13
  107. package/dist/utils/enum.js.map +0 -1
  108. package/dist/utils/index.d.ts +0 -2
  109. package/dist/utils/index.d.ts.map +0 -1
  110. package/dist/utils/index.js +0 -6
  111. package/dist/utils/index.js.map +0 -1
  112. package/dist/utils/ip.d.ts +0 -2
  113. package/dist/utils/ip.d.ts.map +0 -1
  114. package/dist/utils/ip.js +0 -52
  115. package/dist/utils/ip.js.map +0 -1
  116. package/dist/utils/status.d.ts +0 -28
  117. package/dist/utils/status.d.ts.map +0 -1
  118. package/dist/utils/status.js +0 -54
  119. package/dist/utils/status.js.map +0 -1
  120. package/src/api/api.d.ts +0 -6755
  121. package/src/api/api.js +0 -16783
  122. package/src/api/proto.json +0 -1144
  123. package/src/demo.ts +0 -325
  124. package/src/nodes/stream_in.ts +0 -78
  125. package/src/nodes/stream_out.ts +0 -109
  126. package/synapse-api/api/nodes/stream_in.proto +0 -14
  127. package/synapse-api/api/nodes/stream_out.proto +0 -42
package/src/demo.ts DELETED
@@ -1,325 +0,0 @@
1
- import fs from "fs";
2
- import yargs from "yargs";
3
- import { hideBin } from "yargs/helpers";
4
-
5
- import { synapse } from "./api/api";
6
- import Config from "./config";
7
- import Device from "./device";
8
- import ElectricalBroadband from "./nodes/broadband_source";
9
- import OpticalStimulation from "./nodes/optical_stimulation";
10
- import StreamIn from "./nodes/stream_in";
11
- import StreamOut from "./nodes/stream_out";
12
- import { discover } from "./utils/discover";
13
- import { getName } from "./utils/enum";
14
- import { getClientIp } from "./utils/ip";
15
-
16
- const cli = yargs(hideBin(process.argv))
17
- .help()
18
- .command("discover", "Discover synapse devices")
19
- .command("read", "Read from StreamOut node", {
20
- "udp-port": {
21
- alias: "p",
22
- type: "string",
23
- description: "UDP port",
24
- default: "50038",
25
- },
26
- output: {
27
- alias: "o",
28
- type: "string",
29
- description: "Output file",
30
- },
31
- uri: {
32
- alias: "u",
33
- type: "string",
34
- demandOption: true,
35
- },
36
- })
37
- .command("write", "Write to StreamIn node", {
38
- input: {
39
- alias: "i",
40
- type: "string",
41
- description: "Input file",
42
- },
43
- uri: {
44
- alias: "u",
45
- type: "string",
46
- demandOption: true,
47
- },
48
- });
49
-
50
- const info = async (device: Device) => {
51
- console.log("Info()");
52
-
53
- const { status: s, response } = await device.info();
54
- if (!s.ok()) {
55
- console.error(s.message);
56
- return false;
57
- }
58
-
59
- const { serial, name, status, synapseVersion, firmwareVersion, peripherals, configuration } = response;
60
-
61
- console.log(` - serial: ${serial}`);
62
- console.log(` - name: ${name}`);
63
- console.log(` - synapse version: ${synapseVersion || "<unknown>"}`);
64
- console.log(` - firmware version: ${firmwareVersion || "<unknown>"}`);
65
-
66
- console.log(
67
- ` - status: ${status.code} (${getName(synapse.StatusCode, status.code)})${
68
- status.message ? `: "${status.message}"` : ""
69
- }`
70
- );
71
- console.log(` - state: ${getName(synapse.DeviceState, status.state)}`);
72
-
73
- console.log(` - sockets: ${status.sockets?.length || 0}`);
74
- status.sockets?.forEach(({ nodeId, bind }) => {
75
- console.log(` - node [${nodeId}] ${bind}`);
76
- });
77
-
78
- console.log(` - peripherals: ${peripherals?.length || 0}`);
79
- peripherals?.forEach(({ name, peripheralId: id, vendor }) => {
80
- console.log(` - [${id}] ${name} (${vendor})`);
81
- });
82
-
83
- if (configuration) {
84
- const { nodes } = configuration;
85
- console.log(` - configuration:`);
86
- console.log(` - nodes: ${nodes?.length || 0}`);
87
- nodes?.forEach(({ id, type }) => {
88
- console.log(` - [${id}] (${getName(synapse.NodeType, type)})`);
89
- });
90
- }
91
-
92
- return true;
93
- };
94
-
95
- const read = async (device: Device, argv: any) => {
96
- console.log("Reading from device's StreamOut node...");
97
-
98
- const { udpPort, output } = argv;
99
- let status = null;
100
- let stream = null;
101
- if (output) {
102
- console.log(`Writing to file: ${output}`);
103
- stream = fs.createWriteStream(output);
104
- }
105
-
106
- const onMessage = (msg: Buffer) => {
107
- const value = msg.readUInt32BE();
108
- console.log(`StreamOut | recv: ${value}`);
109
- if (stream) {
110
- stream.write(msg);
111
- }
112
- };
113
-
114
- const config = new Config();
115
- const nodeEphys = new ElectricalBroadband({
116
- peripheralId: 100,
117
- signal: {
118
- electrode: {
119
- channels: [{ id: 0, referenceId: 0, electrodeId: 1 }],
120
- },
121
- },
122
- bitWidth: 12,
123
- sampleRateHz: 16000,
124
- });
125
- const nodeStreamOut = new StreamOut(
126
- {
127
- udpUnicast: {
128
- destinationPort: udpPort,
129
- destinationAddress: await getClientIp(),
130
- },
131
- },
132
- { onMessage }
133
- );
134
-
135
- status = config.add([nodeEphys, nodeStreamOut]);
136
- status = config.connect(nodeEphys, nodeStreamOut);
137
- if (!status.ok()) {
138
- console.error("failed to connect nodes: ", status.message);
139
- return;
140
- }
141
-
142
- console.log("Configuring device...");
143
- status = await device.configure(config);
144
- if (!status.ok()) {
145
- console.error("failed to configure device: ", status.message);
146
- return;
147
- }
148
-
149
- console.log("Starting device...");
150
- status = await device.start();
151
- if (!status.ok()) {
152
- console.error("failed to start device: ", status.message);
153
- return;
154
- }
155
-
156
- status = await nodeStreamOut.start();
157
- if (!status.ok()) {
158
- console.error("failed to start stream out node: ", status.message);
159
- return;
160
- }
161
-
162
- let running = true;
163
- process.on("SIGINT", function () {
164
- if (!running) {
165
- return;
166
- }
167
-
168
- running = false;
169
- nodeStreamOut.stop();
170
- device.stop();
171
- process.exit();
172
- });
173
- };
174
-
175
- const write = async (device: Device, argv: any) => {
176
- const { input } = argv;
177
-
178
- if (input && !fs.existsSync(input)) {
179
- console.error(`File not found: ${input}`);
180
- return;
181
- }
182
-
183
- console.log("Writing to device's StreamOut node...");
184
- let status = null;
185
-
186
- const config = new Config();
187
- const nodeStreamIn = new StreamIn({
188
- dataType: synapse.DataType.kBroadband,
189
- });
190
- const nodeOptical = new OpticalStimulation();
191
-
192
- status = config.add([nodeStreamIn, nodeOptical]);
193
- if (!status.ok()) {
194
- console.error("Failed to add nodes: ", status.message);
195
- return;
196
- }
197
-
198
- status = config.connect(nodeStreamIn, nodeOptical);
199
- if (!status.ok()) {
200
- console.error("Failed to connect nodes: ", status.message);
201
- return;
202
- }
203
-
204
- console.log("Configuring device...");
205
- status = await device.configure(config);
206
- if (!status.ok()) {
207
- console.error("Failed to configure device: ", status.message);
208
- return;
209
- }
210
-
211
- console.log("Starting device...");
212
- status = await device.start();
213
- if (!status.ok()) {
214
- console.error("Failed to start device: ", status.message);
215
- return;
216
- }
217
-
218
- let running = true;
219
- process.on("SIGINT", function () {
220
- console.log("SIGINT", running);
221
- if (!running) return;
222
- console.log("stopping...");
223
-
224
- running = false;
225
- device.stop();
226
-
227
- process.exit();
228
- });
229
-
230
- if (input) {
231
- console.log(`Reading from file: ${input}`);
232
- fs.open(input, "r", (err, fd) => {
233
- console.log("Opened file");
234
- const kBufSize = 4;
235
- const buffer = Buffer.alloc(kBufSize);
236
-
237
- if (err) {
238
- console.error(err);
239
- return;
240
- }
241
-
242
- const read = () => {
243
- if (!running) {
244
- fs.close(fd, () => {});
245
- return;
246
- }
247
-
248
- fs.read(fd, buffer, 0, kBufSize, null, (err, n) => {
249
- if (err) {
250
- console.error(err);
251
- return;
252
- }
253
-
254
- if (n === 0) {
255
- return;
256
- }
257
-
258
- console.log(`StreamIn | send: ${buffer.readUInt32BE()}`);
259
- nodeStreamIn.write(buffer.slice(0, n));
260
- read();
261
- });
262
- };
263
-
264
- read();
265
- });
266
-
267
- return;
268
- }
269
-
270
- let i = 0;
271
- const kInterval = 1;
272
- const buffer = Buffer.alloc(4);
273
- const write = () => {
274
- if (!running) {
275
- device.stop();
276
- return;
277
- }
278
-
279
- buffer.writeUInt32BE(i++);
280
- nodeStreamIn.write(buffer);
281
-
282
- setTimeout(write, kInterval);
283
- };
284
- setTimeout(write, kInterval);
285
- };
286
-
287
- const main = async () => {
288
- const argv = cli.parseSync();
289
-
290
- if (argv._.includes("discover")) {
291
- const devices = await discover();
292
- for (const device of devices) {
293
- const addr = `${device.host}:${device.port}`;
294
- const serial = `[${device.serial}]`;
295
- console.log(`${addr.padEnd(21)} ${device.capability.padStart(16)} ${serial.padStart(34)} "${device.name}"`);
296
- }
297
- return;
298
- }
299
-
300
- const { uri } = argv;
301
- if (!uri) {
302
- console.error("Missing URI");
303
- return;
304
- }
305
-
306
- console.log(`Connecting to device @ ${uri}`);
307
- const device = new Device(uri as string);
308
-
309
- try {
310
- await info(device);
311
- } catch (err) {
312
- console.error(`Failed to get device info (${err.code}): ${err.message}`);
313
- return;
314
- }
315
-
316
- if (argv._.includes("read")) {
317
- return read(device, argv);
318
- }
319
-
320
- if (argv._.includes("write")) {
321
- return write(device, argv);
322
- }
323
- };
324
-
325
- main();
@@ -1,78 +0,0 @@
1
- import dgram from "dgram";
2
-
3
- import { synapse } from "../api/api";
4
- import Node from "../node";
5
-
6
- class StreamIn extends Node {
7
- type = synapse.NodeType.kStreamIn;
8
- config: synapse.IStreamInConfig;
9
- _socket: dgram.Socket;
10
-
11
- constructor(config: synapse.IStreamInConfig = {}) {
12
- super();
13
- this.config = config;
14
- }
15
-
16
- write(data: string | Buffer): boolean {
17
- if (this.device === null) {
18
- return false;
19
- }
20
-
21
- const socket = this.device.sockets.find((s) => s.nodeId === this.id);
22
- if (!socket) {
23
- return false;
24
- }
25
-
26
- const split = socket.bind.split(":");
27
- if (split.length !== 2) {
28
- return false;
29
- }
30
-
31
- const port = parseInt(split[1]);
32
- if (isNaN(port)) {
33
- return false;
34
- }
35
-
36
- const host = split[0];
37
- if (!host || !port) {
38
- return false;
39
- }
40
-
41
- if (!this._socket) {
42
- this._socket = dgram.createSocket("udp4");
43
- }
44
-
45
- try {
46
- this._socket.send(data, port, host);
47
- } catch {
48
- return false;
49
- }
50
-
51
- return true;
52
- }
53
-
54
- toProto(): synapse.NodeConfig {
55
- return super.toProto({
56
- streamIn: this.config,
57
- });
58
- }
59
-
60
- _getAddr = (): string | null => {
61
- if (this.device === null) {
62
- return null;
63
- }
64
-
65
- return this.device.uri.split(":")[0];
66
- };
67
-
68
- static fromProto(proto: synapse.INodeConfig): StreamIn {
69
- const { streamIn } = proto;
70
- if (!streamIn) {
71
- throw new Error("Invalid config, missing streamIn");
72
- }
73
-
74
- return new StreamIn(streamIn);
75
- }
76
- }
77
-
78
- export default StreamIn;
@@ -1,109 +0,0 @@
1
- import dgram from "dgram";
2
-
3
- import { synapse } from "../api/api";
4
- import Node from "../node";
5
- import { Status, StatusCode } from "../utils/status";
6
- import { getClientIp } from "../utils/ip";
7
-
8
- const kDefaultStreamOutPort = 50038;
9
- const kSocketBufferSize = 5 * 1024 * 1024; // 5MB
10
-
11
- class StreamOut extends Node {
12
- type = synapse.NodeType.kStreamOut;
13
- _destinationAddress: string;
14
- _destinationPort: number;
15
- _label: string;
16
- _socket: dgram.Socket | null;
17
- _onMessage: ((msg: Buffer) => void) | null;
18
- _onError: ((error: Error) => void) | null;
19
-
20
- constructor(
21
- config: synapse.IStreamOutConfig,
22
- callbacks?: { onMessage?: (msg: Buffer) => void; onError?: (error: Error) => void }
23
- ) {
24
- super();
25
-
26
- const { udpUnicast } = config || {};
27
- this._destinationAddress = udpUnicast?.destinationAddress;
28
- this._destinationPort = udpUnicast?.destinationPort || kDefaultStreamOutPort;
29
- this._label = config.label;
30
- this._onMessage = callbacks?.onMessage;
31
- this._onError = callbacks?.onError;
32
- }
33
-
34
- async start(): Promise<Status> {
35
- try {
36
- this._socket = dgram.createSocket("udp4");
37
-
38
- if (!this._destinationAddress) {
39
- try {
40
- const ip = await getClientIp();
41
- if (!ip) {
42
- return new Status(StatusCode.INTERNAL, "failed to get client ip");
43
- }
44
- this._destinationAddress = ip;
45
- } catch (e) {
46
- console.error(e);
47
- return new Status(StatusCode.INTERNAL, `failed to get client ip: ${e}`);
48
- }
49
- }
50
- if (!this._destinationPort) {
51
- this._destinationPort = kDefaultStreamOutPort;
52
- }
53
-
54
- this._socket.on("message", (msg: Buffer) => {
55
- this._onMessage?.(msg);
56
- });
57
-
58
- this._socket.on("error", (error: Error) => {
59
- this._onError?.(error);
60
- });
61
-
62
- await new Promise<void>((resolve, reject) => {
63
- if (!this._socket) return reject(new Error("Socket is null"));
64
-
65
- this._socket.bind(this._destinationPort, this._destinationAddress, () => {
66
- this._socket.setRecvBufferSize(kSocketBufferSize);
67
- resolve();
68
- });
69
- });
70
-
71
- return new Status();
72
- } catch (e) {
73
- return new Status(StatusCode.INTERNAL, `failed to start stream out node: ${e}`);
74
- }
75
- }
76
-
77
- async stop(): Promise<Status> {
78
- if (this._socket) {
79
- this._socket.close();
80
- }
81
-
82
- return new Status();
83
- }
84
-
85
- toProto(): synapse.NodeConfig {
86
- const config: synapse.IStreamOutConfig = {
87
- label: this._label,
88
- udpUnicast: {
89
- destinationAddress: this._destinationAddress,
90
- destinationPort: this._destinationPort,
91
- },
92
- };
93
-
94
- return super.toProto({
95
- streamOut: config,
96
- });
97
- }
98
-
99
- static fromProto(proto: synapse.INodeConfig): StreamOut {
100
- const { streamOut } = proto;
101
- if (!streamOut) {
102
- throw new Error("Invalid config, missing streamOut");
103
- }
104
-
105
- return new StreamOut(streamOut, undefined);
106
- }
107
- }
108
-
109
- export default StreamOut;
@@ -1,14 +0,0 @@
1
- syntax = "proto3";
2
-
3
- import "api/datatype.proto";
4
-
5
- package synapse;
6
-
7
- message StreamInConfig {
8
- DataType data_type = 1;
9
- repeated uint32 shape = 2;
10
- }
11
-
12
- message StreamInStatus {
13
- float throughput_mbps = 1;
14
- }
@@ -1,42 +0,0 @@
1
- syntax = "proto3";
2
-
3
- package synapse;
4
-
5
- /**
6
- * UDPUnicastConfig defines the configuration parameters for UDP unicast transport.
7
- */
8
- message UDPUnicastConfig {
9
- // IPv4 or IPv6 address of the destination endpoint
10
- string destination_address = 1;
11
-
12
- // Destination port number, 0 to 65535
13
- uint32 destination_port = 2;
14
- }
15
-
16
- /**
17
- * StreamOutConfig defines the configuration for an outbound data stream.
18
- * Clients can request to create a new outbound stream by providing a transport
19
- */
20
- message StreamOutConfig {
21
- // Human-readable identifier for the stream
22
- string label = 1;
23
-
24
- // Transport-specific configuration
25
- // Only one transport type can be specified at a time
26
- oneof transport {
27
- // Configure for UDP unicast support. Only one destination is supported
28
- UDPUnicastConfig udp_unicast = 2;
29
- }
30
- }
31
-
32
- /**
33
- * StreamOutStatus provides status information for an outbound stream.
34
- * It contains data about the instantaneous performance of the stream
35
- */
36
- message StreamOutStatus {
37
- // Current throughput of the stream in megabits per second
38
- float throughput_mbps = 1;
39
-
40
- // How many failed packet sends have happened since the start of the stream
41
- uint64 failed_send_count = 2;
42
- }