@science-corporation/synapse 0.13.0 → 1.1.0

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 (54) hide show
  1. package/README.md +4 -2
  2. package/dist/api/api.d.ts +124 -6
  3. package/dist/api/api.js +293 -20
  4. package/dist/api/api.js.map +1 -1
  5. package/dist/api/proto.json +25 -2
  6. package/dist/demo.js +125 -32
  7. package/dist/demo.js.map +1 -1
  8. package/dist/nodes/stream_out.d.ts +12 -5
  9. package/dist/nodes/stream_out.d.ts.map +1 -1
  10. package/dist/nodes/stream_out.js +58 -40
  11. package/dist/nodes/stream_out.js.map +1 -1
  12. package/dist/utils/ip.d.ts +2 -0
  13. package/dist/utils/ip.d.ts.map +1 -0
  14. package/dist/utils/ip.js +52 -0
  15. package/dist/utils/ip.js.map +1 -0
  16. package/package.json +6 -5
  17. package/scripts/postinstall.sh +50 -10
  18. package/src/api/api.d.ts +124 -6
  19. package/src/api/api.js +311 -20
  20. package/src/api/proto.json +25 -2
  21. package/src/demo.ts +20 -8
  22. package/src/nodes/stream_out.ts +67 -50
  23. package/src/utils/ip.ts +35 -0
  24. package/synapse-api/README.md +1 -1
  25. package/synapse-api/api/nodes/stream_out.proto +31 -1
  26. package/dist/api-science-device/api.d.ts +0 -3
  27. package/dist/api-science-device/api.d.ts.map +0 -1
  28. package/dist/api-science-device/api.js +0 -3822
  29. package/dist/api-science-device/api.js.map +0 -1
  30. package/dist/api-science-device/proto.json +0 -258
  31. package/dist/nodejs.d.ts +0 -8
  32. package/dist/nodejs.d.ts.map +0 -1
  33. package/dist/nodejs.js +0 -32
  34. package/dist/nodejs.js.map +0 -1
  35. package/dist/science-device-api/api.d.ts +0 -3
  36. package/dist/science-device-api/api.d.ts.map +0 -1
  37. package/dist/science-device-api/api.js +0 -3822
  38. package/dist/science-device-api/api.js.map +0 -1
  39. package/dist/types/index.d.ts +0 -3
  40. package/dist/types/index.d.ts.map +0 -1
  41. package/dist/types/index.js +0 -19
  42. package/dist/types/index.js.map +0 -1
  43. package/dist/types.d.ts +0 -5
  44. package/dist/types.d.ts.map +0 -1
  45. package/dist/types.js +0 -19
  46. package/dist/types.js.map +0 -1
  47. package/dist/utils/device-management.d.ts +0 -37
  48. package/dist/utils/device-management.d.ts.map +0 -1
  49. package/dist/utils/device-management.js +0 -126
  50. package/dist/utils/device-management.js.map +0 -1
  51. package/dist/utils/ndtp.d.ts +0 -40
  52. package/dist/utils/ndtp.d.ts.map +0 -1
  53. package/dist/utils/ndtp.js +0 -140
  54. package/dist/utils/ndtp.js.map +0 -1
@@ -2,80 +2,97 @@ import dgram from "dgram";
2
2
 
3
3
  import { synapse } from "../api/api";
4
4
  import Node from "../node";
5
+ import { Status, StatusCode } from "../utils/status";
6
+ import { getClientIp } from "../utils/ip";
5
7
 
6
- const kMulticastTTL = 3;
8
+ const kDefaultStreamOutPort = 50038;
9
+ const kSocketBufferSize = 5 * 1024 * 1024; // 5MB
7
10
 
8
11
  class StreamOut extends Node {
9
12
  type = synapse.NodeType.kStreamOut;
10
- config: synapse.IStreamOutConfig;
11
- _socket: dgram.Socket;
13
+ _destinationAddress: string;
14
+ _destinationPort: number;
15
+ _label: string;
16
+ _socket: dgram.Socket | null;
12
17
  _onMessage: ((msg: Buffer) => void) | null;
18
+ _onError: ((error: Error) => void) | null;
13
19
 
14
- constructor(config: synapse.IStreamOutConfig, onMessage?: (msg: Buffer) => void) {
20
+ constructor(
21
+ config: synapse.IStreamOutConfig,
22
+ callbacks?: { onMessage?: (msg: Buffer) => void; onError?: (error: Error) => void }
23
+ ) {
15
24
  super();
16
25
 
17
- this.config = config;
18
- this._onMessage = onMessage;
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;
19
32
  }
20
33
 
21
- async start(): Promise<boolean> {
22
- if (this.device === null) {
23
- return false;
24
- }
25
-
26
- const socket = this.device.sockets.find((s) => s.nodeId === this.id);
27
- if (!socket) {
28
- return false;
29
- }
30
-
31
- const split = socket.bind.split(":");
32
- if (split.length !== 2) {
33
- return false;
34
- }
35
-
36
- const port = parseInt(split[1]);
37
- if (isNaN(port)) {
38
- return false;
39
- }
40
-
41
- const host = this.config.multicastGroup;
42
- if (!host) {
43
- return false;
44
- }
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
+ }
45
53
 
46
- this._socket = dgram.createSocket("udp4");
54
+ this._socket.on("message", (msg: Buffer) => {
55
+ this._onMessage?.(msg);
56
+ });
47
57
 
48
- this._socket.on("error", () => {});
58
+ this._socket.on("error", (error: Error) => {
59
+ this._onError?.(error);
60
+ });
49
61
 
50
- this._socket.on("message", (msg: Buffer) => {
51
- this._onMessage?.(msg);
52
- });
62
+ await new Promise<void>((resolve, reject) => {
63
+ if (!this._socket) return reject(new Error("Socket is null"));
53
64
 
54
- this._socket.bind(port, host, () => {
55
- if (!this._socket) {
56
- return;
57
- }
65
+ this._socket.bind(this._destinationPort, this._destinationAddress, () => {
66
+ this._socket.setRecvBufferSize(kSocketBufferSize);
67
+ resolve();
68
+ });
69
+ });
58
70
 
59
- if (this.config.multicastGroup) {
60
- this._socket.setMulticastTTL(kMulticastTTL);
61
- this._socket.addMembership(this.config.multicastGroup);
62
- }
63
- });
64
-
65
- return true;
71
+ return new Status();
72
+ } catch (e) {
73
+ return new Status(StatusCode.INTERNAL, `failed to start stream out node: ${e}`);
74
+ }
66
75
  }
67
76
 
68
- async stop(): Promise<boolean> {
77
+ async stop(): Promise<Status> {
69
78
  if (this._socket) {
70
79
  this._socket.close();
71
80
  }
72
81
 
73
- return true;
82
+ return new Status();
74
83
  }
75
84
 
76
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
+
77
94
  return super.toProto({
78
- streamOut: this.config,
95
+ streamOut: config,
79
96
  });
80
97
  }
81
98
 
@@ -85,7 +102,7 @@ class StreamOut extends Node {
85
102
  throw new Error("Invalid config, missing streamOut");
86
103
  }
87
104
 
88
- return new StreamOut(streamOut);
105
+ return new StreamOut(streamOut, undefined);
89
106
  }
90
107
  }
91
108
 
@@ -0,0 +1,35 @@
1
+ import dgram from "dgram";
2
+
3
+ export const getClientIp = async (): Promise<string | null> => {
4
+ return new Promise((resolve, reject) => {
5
+ try {
6
+ const socket = dgram.createSocket("udp4");
7
+
8
+ socket.on("error", (err) => {
9
+ console.error(err);
10
+ socket.close();
11
+ reject(err);
12
+ });
13
+
14
+ socket.bind(0, () => {
15
+ try {
16
+ socket.connect(53, "8.8.8.8", () => {
17
+ try {
18
+ const address = socket.address();
19
+ socket.close();
20
+ resolve(address.address);
21
+ } catch (e) {
22
+ socket.close();
23
+ reject(e);
24
+ }
25
+ });
26
+ } catch (e) {
27
+ socket.close();
28
+ reject(e);
29
+ }
30
+ });
31
+ } catch (e) {
32
+ reject(e);
33
+ }
34
+ });
35
+ };
@@ -1,4 +1,4 @@
1
- # Synapse API 1.1
1
+ # Synapse API 2.0.0
2
2
 
3
3
  The Synapse Protocol defines a standard interface for interacting with a wide range of possible neural interface devices.
4
4
 
@@ -2,11 +2,41 @@ syntax = "proto3";
2
2
 
3
3
  package synapse;
4
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
+ */
5
20
  message StreamOutConfig {
21
+ // Human-readable identifier for the stream
6
22
  string label = 1;
7
- string multicast_group = 2;
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
+ }
8
30
  }
9
31
 
32
+ /**
33
+ * StreamOutStatus provides status information for an outbound stream.
34
+ * It contains data about the instantaneous performance of the stream
35
+ */
10
36
  message StreamOutStatus {
37
+ // Current throughput of the stream in megabits per second
11
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;
12
42
  }
@@ -1,3 +0,0 @@
1
- declare const _exports: any;
2
- export = _exports;
3
- //# sourceMappingURL=api.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"api.d.ts","sourceRoot":"","sources":["../../src/api-science-device/api.js"],"names":[],"mappings":""}