@science-corporation/synapse 0.12.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.
- package/README.md +4 -2
- package/dist/api/api.d.ts +124 -6
- package/dist/api/api.js +293 -20
- package/dist/api/api.js.map +1 -1
- package/dist/api/proto.json +25 -2
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +2 -0
- package/dist/config.js.map +1 -1
- package/dist/demo.js +21 -9
- package/dist/demo.js.map +1 -1
- package/dist/nodes/disk_writer.d.ts +11 -0
- package/dist/nodes/disk_writer.d.ts.map +1 -0
- package/dist/nodes/disk_writer.js +28 -0
- package/dist/nodes/disk_writer.js.map +1 -0
- package/dist/nodes/index.d.ts +1 -0
- package/dist/nodes/index.d.ts.map +1 -1
- package/dist/nodes/index.js +3 -1
- package/dist/nodes/index.js.map +1 -1
- package/dist/nodes/stream_out.d.ts +12 -5
- package/dist/nodes/stream_out.d.ts.map +1 -1
- package/dist/nodes/stream_out.js +58 -40
- package/dist/nodes/stream_out.js.map +1 -1
- package/dist/utils/ip.d.ts +2 -0
- package/dist/utils/ip.d.ts.map +1 -0
- package/dist/utils/ip.js +52 -0
- package/dist/utils/ip.js.map +1 -0
- package/package.json +6 -5
- package/scripts/postinstall.sh +50 -10
- package/src/api/api.d.ts +124 -6
- package/src/api/api.js +311 -20
- package/src/api/proto.json +25 -2
- package/src/config.ts +2 -0
- package/src/demo.ts +21 -9
- package/src/nodes/disk_writer.ts +30 -0
- package/src/nodes/index.ts +1 -0
- package/src/nodes/stream_out.ts +67 -50
- package/src/utils/ip.ts +35 -0
- package/synapse-api/README.md +1 -1
- package/synapse-api/api/nodes/stream_out.proto +31 -1
- package/dist/api-science-device/api.d.ts +0 -3
- package/dist/api-science-device/api.d.ts.map +0 -1
- package/dist/api-science-device/api.js +0 -3822
- package/dist/api-science-device/api.js.map +0 -1
- package/dist/api-science-device/proto.json +0 -258
- package/dist/nodejs.d.ts +0 -8
- package/dist/nodejs.d.ts.map +0 -1
- package/dist/nodejs.js +0 -32
- package/dist/nodejs.js.map +0 -1
- package/dist/science-device-api/api.d.ts +0 -3
- package/dist/science-device-api/api.d.ts.map +0 -1
- package/dist/science-device-api/api.js +0 -3822
- package/dist/science-device-api/api.js.map +0 -1
- package/dist/types/index.d.ts +0 -3
- package/dist/types/index.d.ts.map +0 -1
- package/dist/types/index.js +0 -19
- package/dist/types/index.js.map +0 -1
- package/dist/types.d.ts +0 -5
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js +0 -19
- package/dist/types.js.map +0 -1
- package/dist/utils/device-management.d.ts +0 -37
- package/dist/utils/device-management.d.ts.map +0 -1
- package/dist/utils/device-management.js +0 -126
- package/dist/utils/device-management.js.map +0 -1
package/src/nodes/stream_out.ts
CHANGED
|
@@ -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
|
|
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
|
-
|
|
11
|
-
|
|
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(
|
|
20
|
+
constructor(
|
|
21
|
+
config: synapse.IStreamOutConfig,
|
|
22
|
+
callbacks?: { onMessage?: (msg: Buffer) => void; onError?: (error: Error) => void }
|
|
23
|
+
) {
|
|
15
24
|
super();
|
|
16
25
|
|
|
17
|
-
|
|
18
|
-
this.
|
|
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<
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
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
|
-
|
|
54
|
+
this._socket.on("message", (msg: Buffer) => {
|
|
55
|
+
this._onMessage?.(msg);
|
|
56
|
+
});
|
|
47
57
|
|
|
48
|
-
|
|
58
|
+
this._socket.on("error", (error: Error) => {
|
|
59
|
+
this._onError?.(error);
|
|
60
|
+
});
|
|
49
61
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
});
|
|
62
|
+
await new Promise<void>((resolve, reject) => {
|
|
63
|
+
if (!this._socket) return reject(new Error("Socket is null"));
|
|
53
64
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
65
|
+
this._socket.bind(this._destinationPort, this._destinationAddress, () => {
|
|
66
|
+
this._socket.setRecvBufferSize(kSocketBufferSize);
|
|
67
|
+
resolve();
|
|
68
|
+
});
|
|
69
|
+
});
|
|
58
70
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
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<
|
|
77
|
+
async stop(): Promise<Status> {
|
|
69
78
|
if (this._socket) {
|
|
70
79
|
this._socket.close();
|
|
71
80
|
}
|
|
72
81
|
|
|
73
|
-
return
|
|
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:
|
|
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
|
|
package/src/utils/ip.ts
ADDED
|
@@ -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
|
+
};
|
package/synapse-api/README.md
CHANGED
|
@@ -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
|
-
|
|
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 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"api.d.ts","sourceRoot":"","sources":["../../src/api-science-device/api.js"],"names":[],"mappings":""}
|